Skip to content

Commit cbae660

Browse files
committed
add modules section
Signed-off-by: Diogo Behrens <[email protected]>
1 parent 58923d3 commit cbae660

File tree

1 file changed

+74
-3
lines changed

1 file changed

+74
-3
lines changed

doc/design.md

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,9 @@ The Pubsub system introduces several key advantages:
150150
metadata to the subscribers. Actual type defined by `chain`.
151151

152152
2. **Publishing**: Publishers (such as intercept modules or the Self module)
153-
publish events by calling `PS_PUBLISH(chain_id, type_id, void*, metadata_t*)`. The
154-
publisher specifies the chain, event type, event payload, and a potentially
155-
`NULL` metadata object.
153+
publish events by calling `PS_PUBLISH(chain_id, type_id, void*,
154+
metadata_t*)`. The publisher specifies the chain, event type, event payload,
155+
and a potentially `NULL` metadata object.
156156

157157
3. **Callback Handling**: The callback function is where the action happens for
158158
the subscriber. Upon receiving an event, the callback can inspect and process
@@ -482,3 +482,74 @@ By using these interpose modules, Dice can gather detailed execution data,
482482
track thread behavior, monitor memory usage, and enable advanced testing and
483483
debugging features.
484484

485+
486+
# 6. Loading Modules
487+
488+
Dice is designed to be loaded as a shared library with `LD_PRELOAD`.
489+
The core library in Dice has only the Pubsub and the Mempool modules inside.
490+
Addtional modules can be loaded in two ways.
491+
492+
493+
## 6.1. Shared Library Modules
494+
495+
Consider a multithreaded application `foo`. To run `foo` with Dice the user
496+
simply starts it with the following command:
497+
498+
```
499+
env LD_PRELOAD=/path/to/libdice.so foo <arg1>
500+
```
501+
502+
Additional modules can be loaded with the `LD_PRELOAD` variable as well:
503+
504+
```
505+
env LD_PRELOAD=/path/to/libdice.so:/path/to/mod-pthread_create.so:... foo <arg1>
506+
```
507+
508+
Subscription callbacks are internally kept as lists of function pointers. The
509+
corresponding indirection cost has to be considered when deploying Dice in this
510+
way.
511+
512+
### Subscription Order
513+
514+
One has to be careful in which order the constructors of the modules are
515+
executed. On NetBSD the constructors are executed left-to-right. Given a list
516+
of libraries `LD_PRELOAD=libdice.so:mod1.so:mod2.so`, the subscribers in
517+
`mod1.so` will be registered earlier than subscribers in `mod2.so`; therefore,
518+
callbacks in `mod1.so` will be called first. On Linux, the constructors are
519+
executed right-to-left. So the opposite subscription order will result.
520+
521+
In general, the user has to figure out the order the constructors are called by
522+
the linker and order the shared libraries accordingly.
523+
524+
## 6.2. Monolithic Shared Library
525+
526+
Modules in Dice can also linked together with the core components in a single
527+
shared library. In this configuration, each module is a compilation unit, i.e.,
528+
a `.o` file. When compiling each of these files, the user should specify a
529+
priority for the subscription order by passing `-DDICE_XTOR_PRIO=VAL` to the
530+
compiler. `VAL` should be a number between 200 and 1000. The lower the value,
531+
the higher the priority when subscribing chains.
532+
533+
Assume the user creates a library `libmydice.so` containing `pubsub.o`,
534+
`mempool.o`, `mod-pthread_create.o` and a user-defined module `mymod.o`. To load
535+
this bundle, the user simply uses `LD_PRELOAD`:
536+
537+
```
538+
env LD_PRELOAD=/path/to/libmydice.so foo <arg1>
539+
```
540+
541+
If the user compiles all modules with LTO option, the resulting library can be
542+
much faster than relying on the approach of section 6.1. Nevertheless, without
543+
further steps, the resulting library above will use function pointers when
544+
executing the callbacks of the subscribers.
545+
546+
### Fast-Chain Module
547+
548+
Dice provides a auto-generated module called Fast-Chain module or `fastch.o`,
549+
which directly call the subscriber callbacks without relying on function
550+
pointers. To do that, the Fast-Chain module employs large switch cases on the
551+
`chain_id`, `type_id` and priority values. However, the ranges of these three
552+
dimensions has to be limited to keep the size of the generated code manageable.
553+
554+
For instructions on how to use Fast-Chain, see the `CMakeLists.txt` inside
555+
`src/fastch`.

0 commit comments

Comments
 (0)