| 1 | Userspace RCU Implementation |
| 2 | ============================ |
| 3 | |
| 4 | by Mathieu Desnoyers and Paul E. McKenney |
| 5 | |
| 6 | |
| 7 | Building |
| 8 | -------- |
| 9 | |
| 10 | ./bootstrap # skip if using tarball |
| 11 | ./configure |
| 12 | make |
| 13 | make install |
| 14 | ldconfig |
| 15 | |
| 16 | Hints: |
| 17 | |
| 18 | - Forcing 32-bit build: |
| 19 | |
| 20 | CFLAGS="-m32 -g -O2" ./configure |
| 21 | |
| 22 | - Forcing 64-bit build: |
| 23 | |
| 24 | CFLAGS="-m64 -g -O2" ./configure |
| 25 | |
| 26 | - Forcing a 32-bit build with 386 backward compatibility: |
| 27 | |
| 28 | CFLAGS="-m32 -g -O2" ./configure --host=i386-pc-linux-gnu |
| 29 | |
| 30 | - Forcing a 32-bit build for Sparcv9 (typical for Sparc v9) |
| 31 | |
| 32 | CFLAGS="-m32 -Wa,-Av9a -g -O2" ./configure |
| 33 | |
| 34 | |
| 35 | Architectures supported |
| 36 | ----------------------- |
| 37 | |
| 38 | Currently, the following architectures are supported: |
| 39 | |
| 40 | - Linux x86 (i386, i486, i586, i686) |
| 41 | - x86 64-bit |
| 42 | - PowerPC 32/64 |
| 43 | - S390, S390x |
| 44 | - ARM 32/64 |
| 45 | - MIPS |
| 46 | - NIOS2 |
| 47 | - Alpha |
| 48 | - ia64 |
| 49 | - Sparcv9 32/64 |
| 50 | - Tilera |
| 51 | - hppa/PA-RISC |
| 52 | |
| 53 | Tested on Linux, FreeBSD 8.2/8.3/9.0/9.1/10.0 i386/amd64, and Cygwin. |
| 54 | Should also work on: |
| 55 | |
| 56 | - Android |
| 57 | - NetBSD 5 |
| 58 | - OpenBSD |
| 59 | - Darwin |
| 60 | |
| 61 | (more testing needed before claiming support for these OS). |
| 62 | |
| 63 | Linux ARM depends on running a Linux kernel 2.6.15 or better, GCC 4.4 or |
| 64 | better. |
| 65 | |
| 66 | The GCC compiler versions 3.3, 3.4, 4.0, 4.1, 4.2, 4.3, 4.4 and 4.5 are |
| 67 | supported, with the following exceptions: |
| 68 | |
| 69 | - GCC 3.3 and 3.4 have a bug that prevents them from generating volatile |
| 70 | accesses to offsets in a TLS structure on 32-bit x86. These versions are |
| 71 | therefore not compatible with `liburcu` on x86 32-bit |
| 72 | (i386, i486, i586, i686). |
| 73 | The problem has been reported to the GCC community: |
| 74 | http://www.mail-archive.com/gcc-bugs@gcc.gnu.org/msg281255.html |
| 75 | - GCC 3.3 cannot match the "xchg" instruction on 32-bit x86 build. |
| 76 | See http://kerneltrap.org/node/7507 |
| 77 | - Alpha, ia64 and ARM architectures depend on GCC 4.x with atomic builtins |
| 78 | support. For ARM this was introduced with GCC 4.4: |
| 79 | http://gcc.gnu.org/gcc-4.4/changes.html. |
| 80 | |
| 81 | Clang version 3.0 (based on LLVM 3.0) is supported. |
| 82 | |
| 83 | Building on MacOS X (Darwin) requires a work-around for processor |
| 84 | detection: |
| 85 | |
| 86 | - 32-bit: |
| 87 | |
| 88 | ./configure --build=i686-apple-darwin11 |
| 89 | |
| 90 | - 64-bit: |
| 91 | |
| 92 | ./configure --build=x86_64-apple-darwin11 |
| 93 | |
| 94 | For developers using the Git tree: |
| 95 | |
| 96 | This source tree is based on the autotools suite from GNU to simplify |
| 97 | portability. Here are some things you should have on your system in order to |
| 98 | compile the git repository tree : |
| 99 | |
| 100 | - GNU autotools (automake >=1.10, autoconf >=2.50, autoheader >=2.50) |
| 101 | (make sure your system wide `automake` points to a recent version!) |
| 102 | - GNU Libtool >=2.2 |
| 103 | (for more information, go to http://www.gnu.org/software/autoconf/) |
| 104 | |
| 105 | If you get the tree from the repository, you will need to use the `bootstrap` |
| 106 | script in the root of the tree. It calls all the GNU tools needed to prepare |
| 107 | the tree configuration. |
| 108 | |
| 109 | Test scripts provided in the `tests/` directory of the source tree depend |
| 110 | on `bash` and the `seq` program. |
| 111 | |
| 112 | |
| 113 | API |
| 114 | --- |
| 115 | |
| 116 | See the relevant API documentation files in `doc/`. The APIs provided by |
| 117 | Userspace RCU are, by prefix: |
| 118 | |
| 119 | - `rcu_`: Read-Copy Update (see [`doc/rcu-api.md`](doc/rcu-api.md)) |
| 120 | - `cmm_`: Concurrent Memory Model |
| 121 | - `caa_`: Concurrent Architecture Abstraction |
| 122 | - `cds_`: Concurrent Data Structures |
| 123 | (see [`doc/cds-api.md`](doc/cds-api.md)) |
| 124 | - `uatomic_`: Userspace Atomic |
| 125 | (see [`doc/uatomic-api.md`](doc/uatomic-api.md)) |
| 126 | |
| 127 | |
| 128 | Quick start guide |
| 129 | ----------------- |
| 130 | |
| 131 | ### Usage of all urcu libraries: |
| 132 | |
| 133 | - Define `_LGPL_SOURCE` (only) if your code is LGPL or GPL compatible |
| 134 | before including the `urcu.h` or `urcu-qsbr.h` header. If your application |
| 135 | is distributed under another license, function calls will be generated |
| 136 | instead of inlines, so your application can link with the library. |
| 137 | - Linking with one of the libraries below is always necessary even for |
| 138 | LGPL and GPL applications. |
| 139 | - Define `URCU_INLINE_SMALL_FUNCTIONS` before including Userspace RCU |
| 140 | headers if you want Userspace RCU to inline small functions (10 |
| 141 | lines or less) into the application. It can be used by applications |
| 142 | distributed under any kind of license, and does *not* make the |
| 143 | application a derived work of Userspace RCU. |
| 144 | |
| 145 | Those small inlined functions are guaranteed to match the library |
| 146 | content as long as the library major version is unchanged. |
| 147 | Therefore, the application *must* be compiled with headers matching |
| 148 | the library major version number. Applications using |
| 149 | `URCU_INLINE_SMALL_FUNCTIONS` may be unable to use debugging |
| 150 | features of Userspace RCU without being recompiled. |
| 151 | |
| 152 | |
| 153 | ### Usage of `liburcu` |
| 154 | |
| 155 | 1. `#include <urcu.h>` |
| 156 | 2. Link the application with `-lurcu` |
| 157 | |
| 158 | This is the preferred version of the library, in terms of |
| 159 | grace-period detection speed, read-side speed and flexibility. |
| 160 | Dynamically detects kernel support for `sys_membarrier()`. Falls back |
| 161 | on `urcu-mb` scheme if support is not present, which has slower |
| 162 | read-side. Use the --disable-sys-membarrier-fallback configure option |
| 163 | to disable the fall back, thus requiring `sys_membarrier()` to be |
| 164 | available. This gives a small speedup when `sys_membarrier()` is |
| 165 | supported by the kernel, and aborts in the library constructor if not |
| 166 | supported. |
| 167 | |
| 168 | |
| 169 | ### Usage of `liburcu-qsbr` |
| 170 | |
| 171 | 1. `#include <urcu-qsbr.h>` |
| 172 | 2. Link with `-lurcu-qsbr` |
| 173 | |
| 174 | The QSBR flavor of RCU needs to have each reader thread executing |
| 175 | `rcu_quiescent_state()` periodically to progress. `rcu_thread_online()` |
| 176 | and `rcu_thread_offline()` can be used to mark long periods for which |
| 177 | the threads are not active. It provides the fastest read-side at the |
| 178 | expense of more intrusiveness in the application code. |
| 179 | |
| 180 | |
| 181 | ### Usage of `liburcu-mb` |
| 182 | |
| 183 | 1. `#include <urcu.h>` |
| 184 | 2. Compile any `_LGPL_SOURCE` code using this library with `-DRCU_MB` |
| 185 | 3. Link with `-lurcu-mb` |
| 186 | |
| 187 | This version of the urcu library uses memory barriers on the writer |
| 188 | and reader sides. This results in faster grace-period detection, but |
| 189 | results in slower reads. |
| 190 | |
| 191 | |
| 192 | ### Usage of `liburcu-signal` |
| 193 | |
| 194 | 1. `#include <urcu.h>` |
| 195 | 2. Compile any `_LGPL_SOURCE` code using this library with `-DRCU_SIGNAL` |
| 196 | 3. Link the application with `-lurcu-signal` |
| 197 | |
| 198 | Version of the library that requires a signal, typically `SIGUSR1`. Can |
| 199 | be overridden with `-DSIGRCU` by modifying `Makefile.build.inc`. |
| 200 | |
| 201 | |
| 202 | ### Usage of `liburcu-bp` |
| 203 | |
| 204 | 1. `#include <urcu-bp.h>` |
| 205 | 2. Link with `-lurcu-bp` |
| 206 | |
| 207 | The BP library flavor stands for "bulletproof". It is specifically |
| 208 | designed to help tracing library to hook on applications without |
| 209 | requiring to modify these applications. `rcu_init()`, |
| 210 | `rcu_register_thread()` and `rcu_unregister_thread()` all become nops. |
| 211 | The state is dealt with by the library internally at the expense of |
| 212 | read-side and write-side performance. |
| 213 | |
| 214 | |
| 215 | ### Initialization |
| 216 | |
| 217 | Each thread that has reader critical sections (that uses |
| 218 | `rcu_read_lock()`/`rcu_read_unlock()` must first register to the URCU |
| 219 | library. This is done by calling `rcu_register_thread()`. Unregistration |
| 220 | must be performed before exiting the thread by using |
| 221 | `rcu_unregister_thread()`. |
| 222 | |
| 223 | |
| 224 | ### Reading |
| 225 | |
| 226 | Reader critical sections must be protected by locating them between |
| 227 | calls to `rcu_read_lock()` and `rcu_read_unlock()`. Inside that lock, |
| 228 | `rcu_dereference()` may be called to read an RCU protected pointer. |
| 229 | |
| 230 | |
| 231 | ### Writing |
| 232 | |
| 233 | `rcu_assign_pointer()` and `rcu_xchg_pointer()` may be called anywhere. |
| 234 | After, `synchronize_rcu()` must be called. When it returns, the old |
| 235 | values are not in usage anymore. |
| 236 | |
| 237 | |
| 238 | ### Usage of `liburcu-defer` |
| 239 | |
| 240 | - Follow instructions for either `liburcu`, `liburcu-qsbr`, |
| 241 | `liburcu-mb`, `liburcu-signal`, or `liburcu-bp` above. |
| 242 | The `liburcu-defer` functionality is pulled into each of |
| 243 | those library modules. |
| 244 | - Provides `defer_rcu()` primitive to enqueue delayed callbacks. Queued |
| 245 | callbacks are executed in batch periodically after a grace period. |
| 246 | Do _not_ use `defer_rcu()` within a read-side critical section, because |
| 247 | it may call `synchronize_rcu()` if the thread queue is full. |
| 248 | This can lead to deadlock or worse. |
| 249 | - Requires that `rcu_defer_barrier()` must be called in library destructor |
| 250 | if a library queues callbacks and is expected to be unloaded with |
| 251 | `dlclose()`. |
| 252 | |
| 253 | Its API is currently experimental. It may change in future library releases. |
| 254 | |
| 255 | |
| 256 | ### Usage of `urcu-call-rcu` |
| 257 | |
| 258 | - Follow instructions for either `liburcu`, `liburcu-qsbr`, |
| 259 | `liburcu-mb`, `liburcu-signal`, or `liburcu-bp` above. |
| 260 | The `urcu-call-rcu` functionality is pulled into each of |
| 261 | those library modules. |
| 262 | - Provides the `call_rcu()` primitive to enqueue delayed callbacks |
| 263 | in a manner similar to `defer_rcu()`, but without ever delaying |
| 264 | for a grace period. On the other hand, `call_rcu()`'s best-case |
| 265 | overhead is not quite as good as that of `defer_rcu()`. |
| 266 | - Provides `call_rcu()` to allow asynchronous handling of RCU |
| 267 | grace periods. A number of additional functions are provided |
| 268 | to manage the helper threads used by `call_rcu()`, but reasonable |
| 269 | defaults are used if these additional functions are not invoked. |
| 270 | See [`doc/rcu-api.md`](doc/rcu-api.md) in userspace-rcu documentation |
| 271 | for more details. |
| 272 | |
| 273 | |
| 274 | ### Being careful with signals |
| 275 | |
| 276 | The `liburcu` library uses signals internally. The signal handler is |
| 277 | registered with the `SA_RESTART` flag. However, these signals may cause |
| 278 | some non-restartable system calls to fail with `errno = EINTR`. Care |
| 279 | should be taken to restart system calls manually if they fail with this |
| 280 | error. A list of non-restartable system calls may be found in |
| 281 | `signal(7)`. The `liburcu-mb` and `liburcu-qsbr` versions of the Userspace RCU |
| 282 | library do not require any signal. |
| 283 | |
| 284 | Read-side critical sections are allowed in a signal handler, |
| 285 | except those setup with `sigaltstack(2)`, with `liburcu` and |
| 286 | `liburcu-mb`. Be careful, however, to disable these signals |
| 287 | between thread creation and calls to `rcu_register_thread()`, because a |
| 288 | signal handler nesting on an unregistered thread would not be |
| 289 | allowed to call `rcu_read_lock()`. |
| 290 | |
| 291 | Read-side critical sections are _not_ allowed in a signal handler with |
| 292 | `liburcu-qsbr`, unless signals are disabled explicitly around each |
| 293 | `rcu_quiescent_state()` calls, when threads are put offline and around |
| 294 | calls to `synchronize_rcu()`. Even then, we do not recommend it. |
| 295 | |
| 296 | |
| 297 | ### Interaction with mutexes |
| 298 | |
| 299 | One must be careful to do not cause deadlocks due to interaction of |
| 300 | `synchronize_rcu()` and RCU read-side with mutexes. If `synchronize_rcu()` |
| 301 | is called with a mutex held, this mutex (or any mutex which has this |
| 302 | mutex in its dependency chain) should not be acquired from within a RCU |
| 303 | read-side critical section. |
| 304 | |
| 305 | This is especially important to understand in the context of the |
| 306 | QSBR flavor: a registered reader thread being "online" by |
| 307 | default should be considered as within a RCU read-side critical |
| 308 | section unless explicitly put "offline". Therefore, if |
| 309 | `synchronize_rcu()` is called with a mutex held, this mutex, as |
| 310 | well as any mutex which has this mutex in its dependency chain |
| 311 | should only be taken when the RCU reader thread is "offline" |
| 312 | (this can be performed by calling `rcu_thread_offline()`). |
| 313 | |
| 314 | |
| 315 | ### Interaction with `fork()` |
| 316 | |
| 317 | Special care must be taken for applications performing `fork()` without |
| 318 | any following `exec()`. This is caused by the fact that Linux only clones |
| 319 | the thread calling `fork()`, and thus never replicates any of the other |
| 320 | parent thread into the child process. Most `liburcu` implementations |
| 321 | require that all registrations (as reader, `defer_rcu` and `call_rcu` |
| 322 | threads) should be released before a `fork()` is performed, except for the |
| 323 | rather common scenario where `fork()` is immediately followed by `exec()` in |
| 324 | the child process. The only implementation not subject to that rule is |
| 325 | `liburcu-bp`, which is designed to handle `fork()` by calling |
| 326 | `rcu_bp_before_fork`, `rcu_bp_after_fork_parent` and |
| 327 | `rcu_bp_after_fork_child`. |
| 328 | |
| 329 | Applications that use `call_rcu()` and that `fork()` without |
| 330 | doing an immediate `exec()` must take special action. The parent |
| 331 | must invoke `call_rcu_before_fork()` before the `fork()` and |
| 332 | `call_rcu_after_fork_parent()` after the `fork()`. The child |
| 333 | process must invoke `call_rcu_after_fork_child()`. |
| 334 | Even though these three APIs are suitable for passing to |
| 335 | `pthread_atfork()`, use of `pthread_atfork()` is **STRONGLY |
| 336 | DISCOURAGED** for programs calling the glibc memory allocator |
| 337 | (`malloc()`, `calloc()`, `free()`, ...) within `call_rcu` callbacks. |
| 338 | This is due to limitations in the way glibc memory allocator |
| 339 | handles calls to the memory allocator from concurrent threads |
| 340 | while the `pthread_atfork()` handlers are executing. |
| 341 | |
| 342 | Combining e.g.: |
| 343 | |
| 344 | - call to `free()` from callbacks executed within `call_rcu` worker |
| 345 | threads, |
| 346 | - executing `call_rcu` atfork handlers within the glibc pthread |
| 347 | atfork mechanism, |
| 348 | |
| 349 | will sometimes trigger interesting process hangs. This usually |
| 350 | hangs on a memory allocator lock within glibc. |
| 351 | |
| 352 | |
| 353 | ### Thread Local Storage (TLS) |
| 354 | |
| 355 | Userspace RCU can fall back on `pthread_getspecific()` to emulate |
| 356 | TLS variables on systems where it is not available. This behavior |
| 357 | can be forced by specifying `--disable-compiler-tls` as configure |
| 358 | argument. |
| 359 | |
| 360 | |
| 361 | ### Usage of `DEBUG_RCU` |
| 362 | |
| 363 | `DEBUG_RCU` is used to add internal debugging self-checks to the |
| 364 | RCU library. This define adds a performance penalty when enabled. |
| 365 | Can be enabled by uncommenting the corresponding line in |
| 366 | `Makefile.build.inc`. |
| 367 | |
| 368 | |
| 369 | ### Usage of `DEBUG_YIELD` |
| 370 | |
| 371 | `DEBUG_YIELD` is used to add random delays in the code for testing |
| 372 | purposes. |
| 373 | |
| 374 | |
| 375 | ### SMP support |
| 376 | |
| 377 | By default the library is configured to use synchronization primitives |
| 378 | adequate for SMP systems. On uniprocessor systems, support for SMP |
| 379 | systems can be disabled with: |
| 380 | |
| 381 | ./configure --disable-smp-support |
| 382 | |
| 383 | theoretically yielding slightly better performance. |
| 384 | |
| 385 | |
| 386 | Make targets |
| 387 | ------------ |
| 388 | |
| 389 | In addition to the usual `make check` target, Userspace RCU features |
| 390 | `make regtest` and `make bench` targets: |
| 391 | |
| 392 | - `make check`: short tests, meant to be run when rebuilding or |
| 393 | porting Userspace RCU. |
| 394 | - `make regtest`: long (many hours) test, meant to be run when |
| 395 | modifying Userspace RCU or porting it to a new architecture or |
| 396 | operating system. |
| 397 | - `make bench`: long (many hours) benchmarks. |
| 398 | |
| 399 | |
| 400 | Contacts |
| 401 | -------- |
| 402 | |
| 403 | You can contact the maintainers on the following mailing list: |
| 404 | `lttng-dev@lists.lttng.org`. |