Commit | Line | Data |
---|---|---|
d589a916 PP |
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 | ||
f328865f | 40 | - x86 (i386, i486, i586, i686) |
72886af7 | 41 | - amd64 / x86\_64 |
d589a916 PP |
42 | - PowerPC 32/64 |
43 | - S390, S390x | |
44 | - ARM 32/64 | |
45 | - MIPS | |
859050b3 | 46 | - NIOS2 |
d589a916 PP |
47 | - Alpha |
48 | - ia64 | |
49 | - Sparcv9 32/64 | |
50 | - Tilera | |
51 | - hppa/PA-RISC | |
f328865f MD |
52 | - m68k |
53 | - RISC-V | |
54 | ||
55 | Tested on: | |
56 | ||
57 | - Linux all architectures | |
0966bbf4 | 58 | - FreeBSD 13 i386/amd64 |
f328865f | 59 | - Cygwin i386/amd64 |
0af4b40c | 60 | - MacOS amd64/arm64 |
d589a916 | 61 | |
d589a916 PP |
62 | Should also work on: |
63 | ||
64 | - Android | |
65 | - NetBSD 5 | |
66 | - OpenBSD | |
1320034b | 67 | - Solaris |
d589a916 PP |
68 | |
69 | (more testing needed before claiming support for these OS). | |
70 | ||
d589a916 | 71 | |
d2916ca5 YN |
72 | Toolchain support |
73 | ----------------- | |
4d1f67b9 | 74 | |
d2916ca5 YN |
75 | The C compiler used needs to support at least C99. The C++ compiler used needs |
76 | to support at least C++11. The oldest GCC version officialy supported and | |
77 | tested is 4.8. | |
78 | ||
79 | Older GCC versions might still work with the following exceptions: | |
d589a916 PP |
80 | |
81 | - GCC 3.3 and 3.4 have a bug that prevents them from generating volatile | |
82 | accesses to offsets in a TLS structure on 32-bit x86. These versions are | |
83 | therefore not compatible with `liburcu` on x86 32-bit | |
84 | (i386, i486, i586, i686). | |
85 | The problem has been reported to the GCC community: | |
efa4515d | 86 | <http://www.mail-archive.com/gcc-bugs@gcc.gnu.org/msg281255.html> |
d589a916 | 87 | - GCC 3.3 cannot match the "xchg" instruction on 32-bit x86 build. |
efa4515d | 88 | See <http://kerneltrap.org/node/7507> |
d589a916 PP |
89 | - Alpha, ia64 and ARM architectures depend on GCC 4.x with atomic builtins |
90 | support. For ARM this was introduced with GCC 4.4: | |
efa4515d | 91 | <http://gcc.gnu.org/gcc-4.4/changes.html>. |
ddec79fd MD |
92 | - Linux aarch64 depends on GCC 5.1 or better because prior versions |
93 | perform unsafe access to deallocated stack. | |
d589a916 PP |
94 | |
95 | Clang version 3.0 (based on LLVM 3.0) is supported. | |
96 | ||
ef6da886 MJ |
97 | Glibc >= 2.4 should work but the older version we test against is |
98 | currently 2.17. | |
99 | ||
d2916ca5 YN |
100 | |
101 | Build system | |
102 | ------------ | |
103 | ||
d589a916 PP |
104 | For developers using the Git tree: |
105 | ||
106 | This source tree is based on the autotools suite from GNU to simplify | |
107 | portability. Here are some things you should have on your system in order to | |
108 | compile the git repository tree : | |
109 | ||
afb6113f | 110 | - GNU autotools (automake >=1.12, autoconf >=2.69) |
d589a916 PP |
111 | (make sure your system wide `automake` points to a recent version!) |
112 | - GNU Libtool >=2.2 | |
efa4515d | 113 | (for more information, go to <http://www.gnu.org/software/autoconf/>) |
d589a916 PP |
114 | |
115 | If you get the tree from the repository, you will need to use the `bootstrap` | |
116 | script in the root of the tree. It calls all the GNU tools needed to prepare | |
117 | the tree configuration. | |
118 | ||
119 | Test scripts provided in the `tests/` directory of the source tree depend | |
120 | on `bash` and the `seq` program. | |
121 | ||
122 | ||
123 | API | |
124 | --- | |
125 | ||
126 | See the relevant API documentation files in `doc/`. The APIs provided by | |
127 | Userspace RCU are, by prefix: | |
128 | ||
dcb9c05a | 129 | - `rcu_`: Read-Copy Update (see [`doc/rcu-api.md`](doc/rcu-api.md)) |
d589a916 PP |
130 | - `cmm_`: Concurrent Memory Model |
131 | - `caa_`: Concurrent Architecture Abstraction | |
132 | - `cds_`: Concurrent Data Structures | |
dcb9c05a | 133 | (see [`doc/cds-api.md`](doc/cds-api.md)) |
d589a916 | 134 | - `uatomic_`: Userspace Atomic |
dcb9c05a | 135 | (see [`doc/uatomic-api.md`](doc/uatomic-api.md)) |
d589a916 PP |
136 | |
137 | ||
138 | Quick start guide | |
139 | ----------------- | |
140 | ||
141 | ### Usage of all urcu libraries: | |
142 | ||
143 | - Define `_LGPL_SOURCE` (only) if your code is LGPL or GPL compatible | |
144 | before including the `urcu.h` or `urcu-qsbr.h` header. If your application | |
145 | is distributed under another license, function calls will be generated | |
146 | instead of inlines, so your application can link with the library. | |
147 | - Linking with one of the libraries below is always necessary even for | |
148 | LGPL and GPL applications. | |
149 | - Define `URCU_INLINE_SMALL_FUNCTIONS` before including Userspace RCU | |
150 | headers if you want Userspace RCU to inline small functions (10 | |
151 | lines or less) into the application. It can be used by applications | |
152 | distributed under any kind of license, and does *not* make the | |
153 | application a derived work of Userspace RCU. | |
154 | ||
155 | Those small inlined functions are guaranteed to match the library | |
156 | content as long as the library major version is unchanged. | |
157 | Therefore, the application *must* be compiled with headers matching | |
158 | the library major version number. Applications using | |
159 | `URCU_INLINE_SMALL_FUNCTIONS` may be unable to use debugging | |
160 | features of Userspace RCU without being recompiled. | |
161 | ||
f328865f MD |
162 | There are multiple flavors of liburcu available: |
163 | ||
164 | - `memb`, | |
165 | - `qsbr`, | |
166 | - `mb`, | |
167 | - `signal`, | |
168 | - `bp`. | |
169 | ||
72886af7 WY |
170 | The API members start with the prefix `urcu_<flavor>_`, where |
171 | `<flavor>` is the chosen flavor name. | |
f328865f | 172 | |
d589a916 | 173 | |
f328865f | 174 | ### Usage of `liburcu-memb` |
d589a916 | 175 | |
f328865f MD |
176 | 1. `#include <urcu/urcu-memb.h>` |
177 | 2. Link the application with `-lurcu-memb` | |
d589a916 PP |
178 | |
179 | This is the preferred version of the library, in terms of | |
180 | grace-period detection speed, read-side speed and flexibility. | |
181 | Dynamically detects kernel support for `sys_membarrier()`. Falls back | |
182 | on `urcu-mb` scheme if support is not present, which has slower | |
cef5f31d | 183 | read-side. Use the `--disable-sys-membarrier-fallback` configure option |
d8d9a340 MD |
184 | to disable the fall back, thus requiring `sys_membarrier()` to be |
185 | available. This gives a small speedup when `sys_membarrier()` is | |
186 | supported by the kernel, and aborts in the library constructor if not | |
187 | supported. | |
d589a916 PP |
188 | |
189 | ||
190 | ### Usage of `liburcu-qsbr` | |
191 | ||
f328865f | 192 | 1. `#include <urcu/urcu-qsbr.h>` |
d589a916 PP |
193 | 2. Link with `-lurcu-qsbr` |
194 | ||
195 | The QSBR flavor of RCU needs to have each reader thread executing | |
196 | `rcu_quiescent_state()` periodically to progress. `rcu_thread_online()` | |
197 | and `rcu_thread_offline()` can be used to mark long periods for which | |
198 | the threads are not active. It provides the fastest read-side at the | |
199 | expense of more intrusiveness in the application code. | |
200 | ||
201 | ||
202 | ### Usage of `liburcu-mb` | |
203 | ||
f328865f MD |
204 | 1. `#include <urcu/urcu-mb.h>` |
205 | 2. Link with `-lurcu-mb` | |
d589a916 PP |
206 | |
207 | This version of the urcu library uses memory barriers on the writer | |
208 | and reader sides. This results in faster grace-period detection, but | |
209 | results in slower reads. | |
210 | ||
211 | ||
212 | ### Usage of `liburcu-signal` | |
213 | ||
f328865f MD |
214 | 1. `#include <urcu/urcu-signal.h>` |
215 | 2. Link the application with `-lurcu-signal` | |
d589a916 PP |
216 | |
217 | Version of the library that requires a signal, typically `SIGUSR1`. Can | |
218 | be overridden with `-DSIGRCU` by modifying `Makefile.build.inc`. | |
219 | ||
220 | ||
221 | ### Usage of `liburcu-bp` | |
222 | ||
f328865f | 223 | 1. `#include <urcu/urcu-bp.h>` |
d589a916 PP |
224 | 2. Link with `-lurcu-bp` |
225 | ||
226 | The BP library flavor stands for "bulletproof". It is specifically | |
227 | designed to help tracing library to hook on applications without | |
5b46e39d MD |
228 | requiring to modify these applications. `urcu_bp_init()`, and |
229 | `urcu_bp_unregister_thread()` all become nops, whereas calling | |
230 | `urcu_bp_register_thread()` becomes optional. The state is dealt with by | |
231 | the library internally at the expense of read-side and write-side | |
232 | performance. | |
d589a916 PP |
233 | |
234 | ||
235 | ### Initialization | |
236 | ||
237 | Each thread that has reader critical sections (that uses | |
f328865f MD |
238 | `urcu_<flavor>_read_lock()`/`urcu_<flavor>_read_unlock()` must first |
239 | register to the URCU library. This is done by calling | |
240 | `urcu_<flavor>_register_thread()`. Unregistration must be performed | |
241 | before exiting the thread by using `urcu_<flavor>_unregister_thread()`. | |
d589a916 PP |
242 | |
243 | ||
244 | ### Reading | |
245 | ||
246 | Reader critical sections must be protected by locating them between | |
f328865f MD |
247 | calls to `urcu_<flavor>_read_lock()` and `urcu_<flavor>_read_unlock()`. |
248 | Inside that lock, `rcu_dereference()` may be called to read an RCU | |
249 | protected pointer. | |
d589a916 PP |
250 | |
251 | ||
252 | ### Writing | |
253 | ||
254 | `rcu_assign_pointer()` and `rcu_xchg_pointer()` may be called anywhere. | |
f328865f MD |
255 | After, `urcu_<flavor>_synchronize_rcu()` must be called. When it |
256 | returns, the old values are not in usage anymore. | |
d589a916 | 257 | |
111bda8f MD |
258 | As an alternative to `urcu_<flavor>_synchronize_rcu()`, |
259 | it is also possible to use the urcu polling mechanism to wait for a | |
260 | grace period to elapse. This can be done by using | |
261 | `urcu_<flavor>_start_poll_synchronize_rcu()` | |
262 | to start the grace period polling, and then invoke | |
263 | `urcu_<flavor>_poll_state_synchronize_rcu()`, which returns true if | |
264 | the grace period has completed, false otherwise. | |
265 | ||
d589a916 PP |
266 | |
267 | ### Usage of `liburcu-defer` | |
268 | ||
f328865f | 269 | - Follow instructions for either `liburcu-memb`, `liburcu-qsbr`, |
d589a916 PP |
270 | `liburcu-mb`, `liburcu-signal`, or `liburcu-bp` above. |
271 | The `liburcu-defer` functionality is pulled into each of | |
272 | those library modules. | |
f328865f MD |
273 | - Provides `urcu_<flavor>_defer_rcu()` primitive to enqueue delayed |
274 | callbacks. Queued callbacks are executed in batch periodically after | |
275 | a grace period. Do _not_ use `urcu_<flavor>_defer_rcu()` within a | |
276 | read-side critical section, because it may call | |
277 | `urcu_<flavor>_synchronize_rcu()` if the thread queue is full. This | |
278 | can lead to deadlock or worse. | |
279 | - Requires that `urcu_<flavor>_defer_barrier()` must be called in | |
280 | library destructor if a library queues callbacks and is expected to | |
281 | be unloaded with `dlclose()`. | |
d589a916 PP |
282 | |
283 | Its API is currently experimental. It may change in future library releases. | |
284 | ||
285 | ||
286 | ### Usage of `urcu-call-rcu` | |
287 | ||
f328865f | 288 | - Follow instructions for either `liburcu-memb`, `liburcu-qsbr`, |
d589a916 PP |
289 | `liburcu-mb`, `liburcu-signal`, or `liburcu-bp` above. |
290 | The `urcu-call-rcu` functionality is pulled into each of | |
291 | those library modules. | |
f328865f MD |
292 | - Provides the `urcu_<flavor>_call_rcu()` primitive to enqueue delayed |
293 | callbacks in a manner similar to `urcu_<flavor>_defer_rcu()`, but | |
294 | without ever delaying for a grace period. On the other hand, | |
295 | `urcu_<flavor>_call_rcu()`'s best-case overhead is not quite as good | |
296 | as that of `urcu_<flavor>_defer_rcu()`. | |
297 | - Provides `urcu_<flavor>_call_rcu()` to allow asynchronous handling | |
298 | of RCU grace periods. A number of additional functions are provided | |
299 | to manage the helper threads used by `urcu_<flavor>_call_rcu()`, but | |
300 | reasonable defaults are used if these additional functions are not | |
301 | invoked. See [`doc/rcu-api.md`](doc/rcu-api.md) in userspace-rcu | |
302 | documentation for more details. | |
d589a916 PP |
303 | |
304 | ||
305 | ### Being careful with signals | |
306 | ||
f328865f | 307 | The `liburcu-signal` library uses signals internally. The signal handler is |
d589a916 PP |
308 | registered with the `SA_RESTART` flag. However, these signals may cause |
309 | some non-restartable system calls to fail with `errno = EINTR`. Care | |
310 | should be taken to restart system calls manually if they fail with this | |
311 | error. A list of non-restartable system calls may be found in | |
f328865f | 312 | `signal(7)`. |
d589a916 PP |
313 | |
314 | Read-side critical sections are allowed in a signal handler, | |
f328865f | 315 | except those setup with `sigaltstack(2)`, with `liburcu-memb` and |
d589a916 | 316 | `liburcu-mb`. Be careful, however, to disable these signals |
f328865f MD |
317 | between thread creation and calls to `urcu_<flavor>_register_thread()`, |
318 | because a signal handler nesting on an unregistered thread would not be | |
319 | allowed to call `urcu_<flavor>_read_lock()`. | |
d589a916 PP |
320 | |
321 | Read-side critical sections are _not_ allowed in a signal handler with | |
322 | `liburcu-qsbr`, unless signals are disabled explicitly around each | |
f328865f MD |
323 | `urcu_qsbr_quiescent_state()` calls, when threads are put offline and around |
324 | calls to `urcu_qsbr_synchronize_rcu()`. Even then, we do not recommend it. | |
d589a916 PP |
325 | |
326 | ||
327 | ### Interaction with mutexes | |
328 | ||
329 | One must be careful to do not cause deadlocks due to interaction of | |
f328865f MD |
330 | `urcu_<flavor>_synchronize_rcu()` and RCU read-side with mutexes. If |
331 | `urcu_<flavor>_synchronize_rcu()` is called with a mutex held, this | |
332 | mutex (or any mutex which has this mutex in its dependency chain) should | |
333 | not be acquired from within a RCU read-side critical section. | |
d589a916 PP |
334 | |
335 | This is especially important to understand in the context of the | |
336 | QSBR flavor: a registered reader thread being "online" by | |
337 | default should be considered as within a RCU read-side critical | |
338 | section unless explicitly put "offline". Therefore, if | |
f328865f MD |
339 | `urcu_qsbr_synchronize_rcu()` is called with a mutex held, this mutex, |
340 | as well as any mutex which has this mutex in its dependency chain should | |
341 | only be taken when the RCU reader thread is "offline" (this can be | |
342 | performed by calling `urcu_qsbr_thread_offline()`). | |
d589a916 PP |
343 | |
344 | ||
345 | ### Interaction with `fork()` | |
346 | ||
347 | Special care must be taken for applications performing `fork()` without | |
348 | any following `exec()`. This is caused by the fact that Linux only clones | |
349 | the thread calling `fork()`, and thus never replicates any of the other | |
350 | parent thread into the child process. Most `liburcu` implementations | |
351 | require that all registrations (as reader, `defer_rcu` and `call_rcu` | |
352 | threads) should be released before a `fork()` is performed, except for the | |
353 | rather common scenario where `fork()` is immediately followed by `exec()` in | |
354 | the child process. The only implementation not subject to that rule is | |
355 | `liburcu-bp`, which is designed to handle `fork()` by calling | |
f328865f MD |
356 | `urcu_bp_before_fork`, `urcu_bp_after_fork_parent` and |
357 | `urcu_bp_after_fork_child`. | |
358 | ||
359 | Applications that use `urcu_<flavor>_call_rcu()` and that `fork()` | |
360 | without doing an immediate `exec()` must take special action. The | |
361 | parent must invoke `urcu_<flavor>_call_rcu_before_fork()` before the | |
362 | `fork()` and `urcu_<flavor>_call_rcu_after_fork_parent()` after the | |
363 | `fork()`. The child process must invoke | |
364 | `urcu_<flavor>_call_rcu_after_fork_child()`. Even though these three | |
365 | APIs are suitable for passing to `pthread_atfork()`, use of | |
366 | `pthread_atfork()` is **STRONGLY DISCOURAGED** for programs calling the | |
367 | glibc memory allocator (`malloc()`, `calloc()`, `free()`, ...) within | |
368 | `urcu_<flavor>_call_rcu` callbacks. This is due to limitations in the | |
369 | way glibc memory allocator handles calls to the memory allocator from | |
370 | concurrent threads while the `pthread_atfork()` handlers are executing. | |
d589a916 PP |
371 | |
372 | Combining e.g.: | |
373 | ||
f328865f MD |
374 | - call to `free()` from callbacks executed within |
375 | `urcu_<flavor>_call_rcu` worker threads, | |
376 | - executing `urcu_<flavor>_call_rcu` atfork handlers within the glibc | |
377 | pthread atfork mechanism, | |
d589a916 PP |
378 | |
379 | will sometimes trigger interesting process hangs. This usually | |
380 | hangs on a memory allocator lock within glibc. | |
381 | ||
382 | ||
383 | ### Thread Local Storage (TLS) | |
384 | ||
385 | Userspace RCU can fall back on `pthread_getspecific()` to emulate | |
386 | TLS variables on systems where it is not available. This behavior | |
387 | can be forced by specifying `--disable-compiler-tls` as configure | |
388 | argument. | |
389 | ||
390 | ||
d4e640c0 | 391 | ### Usage of `DEBUG_RCU` & `--enable-rcu-debug` |
d589a916 | 392 | |
d4e640c0 JR |
393 | By default the library is configured with internal debugging |
394 | self-checks disabled. | |
395 | ||
396 | For always-on debugging self-checks: | |
af9254ab WY |
397 | |
398 | ./configure --enable-rcu-debug | |
d4e640c0 JR |
399 | |
400 | For fine grained enabling of debugging self-checks, build | |
72886af7 WY |
401 | userspace-rcu with `DEBUG_RCU` defined and compile dependent |
402 | applications with `DEBUG_RCU` defined when necessary. | |
d4e640c0 JR |
403 | |
404 | Warning: Enabling this feature result in a performance penalty. | |
d589a916 PP |
405 | |
406 | ||
407 | ### Usage of `DEBUG_YIELD` | |
408 | ||
409 | `DEBUG_YIELD` is used to add random delays in the code for testing | |
410 | purposes. | |
411 | ||
412 | ||
413 | ### SMP support | |
414 | ||
415 | By default the library is configured to use synchronization primitives | |
416 | adequate for SMP systems. On uniprocessor systems, support for SMP | |
417 | systems can be disabled with: | |
418 | ||
419 | ./configure --disable-smp-support | |
420 | ||
421 | theoretically yielding slightly better performance. | |
422 | ||
423 | ||
d7c76f85 MD |
424 | ### Usage of `--enable-cds-lfht-iter-debug` |
425 | ||
426 | By default the library is configured with extra debugging checks for | |
427 | lock-free hash table iterator traversal disabled. | |
428 | ||
cef5f31d | 429 | Building liburcu with `--enable-cds-lfht-iter-debug` and rebuilding |
d7c76f85 MD |
430 | application to match the ABI change allows finding cases where the hash |
431 | table iterator is re-purposed to be used on a different hash table while | |
432 | still being used to iterate on a hash table. | |
433 | ||
434 | This option alters the rculfhash ABI. Make sure to compile both library | |
435 | and application with matching configuration. | |
436 | ||
437 | ||
d589a916 PP |
438 | Make targets |
439 | ------------ | |
440 | ||
441 | In addition to the usual `make check` target, Userspace RCU features | |
ff59d427 | 442 | `make regtest`, `make short_bench` and `make long_bench` targets: |
d589a916 PP |
443 | |
444 | - `make check`: short tests, meant to be run when rebuilding or | |
445 | porting Userspace RCU. | |
446 | - `make regtest`: long (many hours) test, meant to be run when | |
447 | modifying Userspace RCU or porting it to a new architecture or | |
448 | operating system. | |
ff59d427 STH |
449 | - `make short_bench`: short benchmarks, 3 seconds per test. |
450 | - `make long_bench`: long (many hours) benchmarks, 30 seconds per test. | |
d589a916 PP |
451 | |
452 | ||
43f53c96 MD |
453 | Known issues |
454 | ------------ | |
455 | ||
456 | There is an application vs library compatibility issue between | |
457 | applications built using Userspace RCU 0.10 headers linked against | |
458 | Userspace RCU 0.11 or 0.12 shared objects. The problem occurs as | |
459 | follows: | |
460 | ||
72886af7 | 461 | - An application executable is built with `_LGPL_SOURCE` defined, includes |
43f53c96 | 462 | any of the Userspace RCU 0.10 urcu flavor headers, and is built |
cef5f31d | 463 | without the `-fpic` compiler option. |
43f53c96 MD |
464 | |
465 | - The Userspace RCU 0.10 library shared objects are updated to 0.11 | |
466 | or 0.12 without rebuilding the application. | |
467 | ||
468 | - The application will hang, typically when RCU grace period | |
469 | (synchronize_rcu) is invoked. | |
470 | ||
471 | Some possible work-arounds for this are: | |
472 | ||
473 | - Rebuild the application against Userspace RCU 0.11+. | |
474 | ||
cef5f31d | 475 | - Rebuild the application with `-fpic`. |
43f53c96 MD |
476 | |
477 | - Upgrade Userspace RCU to 0.13+ without installing 0.11 nor 0.12. | |
478 | ||
479 | ||
d589a916 PP |
480 | Contacts |
481 | -------- | |
482 | ||
483 | You can contact the maintainers on the following mailing list: | |
dcb9c05a | 484 | `lttng-dev@lists.lttng.org`. |