Commit | Line | Data |
---|---|---|
852c2936 MD |
1 | /* |
2 | * ring_buffer_frontend.c | |
3 | * | |
e92f3e28 MD |
4 | * Copyright (C) 2005-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
5 | * | |
6 | * This library is free software; you can redistribute it and/or | |
7 | * modify it under the terms of the GNU Lesser General Public | |
8 | * License as published by the Free Software Foundation; only | |
9 | * version 2.1 of the License. | |
10 | * | |
11 | * This library is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | * Lesser General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU Lesser General Public | |
17 | * License along with this library; if not, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
19 | * | |
852c2936 MD |
20 | * |
21 | * Ring buffer wait-free buffer synchronization. Producer-consumer and flight | |
22 | * recorder (overwrite) modes. See thesis: | |
23 | * | |
24 | * Desnoyers, Mathieu (2009), "Low-Impact Operating System Tracing", Ph.D. | |
25 | * dissertation, Ecole Polytechnique de Montreal. | |
26 | * http://www.lttng.org/pub/thesis/desnoyers-dissertation-2009-12.pdf | |
27 | * | |
28 | * - Algorithm presentation in Chapter 5: | |
29 | * "Lockless Multi-Core High-Throughput Buffering". | |
30 | * - Algorithm formal verification in Section 8.6: | |
31 | * "Formal verification of LTTng" | |
32 | * | |
33 | * Author: | |
34 | * Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
35 | * | |
36 | * Inspired from LTT and RelayFS: | |
37 | * Karim Yaghmour <karim@opersys.com> | |
38 | * Tom Zanussi <zanussi@us.ibm.com> | |
39 | * Bob Wisniewski <bob@watson.ibm.com> | |
40 | * And from K42 : | |
41 | * Bob Wisniewski <bob@watson.ibm.com> | |
42 | * | |
43 | * Buffer reader semantic : | |
44 | * | |
45 | * - get_subbuf_size | |
46 | * while buffer is not finalized and empty | |
47 | * - get_subbuf | |
48 | * - if return value != 0, continue | |
49 | * - splice one subbuffer worth of data to a pipe | |
50 | * - splice the data from pipe to disk/network | |
51 | * - put_subbuf | |
852c2936 MD |
52 | */ |
53 | ||
5ad63a16 | 54 | #define _GNU_SOURCE |
3fbec7dc | 55 | #define _LGPL_SOURCE |
a6352fd4 | 56 | #include <sys/types.h> |
431d5cf0 MD |
57 | #include <sys/mman.h> |
58 | #include <sys/stat.h> | |
03d2d293 | 59 | #include <unistd.h> |
431d5cf0 | 60 | #include <fcntl.h> |
03d2d293 MD |
61 | #include <signal.h> |
62 | #include <time.h> | |
14641deb | 63 | #include <urcu/compiler.h> |
a6352fd4 | 64 | #include <urcu/ref.h> |
8c90a710 | 65 | #include <urcu/tls-compat.h> |
327d9447 | 66 | #include <poll.h> |
35897f8b | 67 | #include <helper.h> |
14641deb | 68 | |
a6352fd4 | 69 | #include "smp.h" |
4318ae1b | 70 | #include <lttng/ringbuffer-config.h> |
2fed87ae | 71 | #include "vatomic.h" |
4931a13e MD |
72 | #include "backend.h" |
73 | #include "frontend.h" | |
a6352fd4 | 74 | #include "shm.h" |
f645cfa7 | 75 | #include "tlsfixup.h" |
bdcf8d82 | 76 | #include "../liblttng-ust/compat.h" /* For ENODATA */ |
852c2936 | 77 | |
64493e4f MD |
78 | /* Print DBG() messages about events lost only every 1048576 hits */ |
79 | #define DBG_PRINT_NR_LOST (1UL << 20) | |
80 | ||
34a91bdb MD |
81 | #define LTTNG_UST_RB_SIG_FLUSH SIGRTMIN |
82 | #define LTTNG_UST_RB_SIG_READ SIGRTMIN + 1 | |
83 | #define LTTNG_UST_RB_SIG_TEARDOWN SIGRTMIN + 2 | |
03d2d293 | 84 | #define CLOCKID CLOCK_MONOTONIC |
dddba398 MD |
85 | #define LTTNG_UST_RING_BUFFER_GET_RETRY 10 |
86 | #define LTTNG_UST_RING_BUFFER_RETRY_DELAY_MS 10 | |
03d2d293 | 87 | |
a9ff648c MD |
88 | /* |
89 | * Non-static to ensure the compiler does not optimize away the xor. | |
90 | */ | |
91 | uint8_t lttng_crash_magic_xor[] = RB_CRASH_DUMP_ABI_MAGIC_XOR; | |
92 | ||
2432c3c9 MD |
93 | /* |
94 | * Use POSIX SHM: shm_open(3) and shm_unlink(3). | |
95 | * close(2) to close the fd returned by shm_open. | |
96 | * shm_unlink releases the shared memory object name. | |
97 | * ftruncate(2) sets the size of the memory object. | |
98 | * mmap/munmap maps the shared memory obj to a virtual address in the | |
99 | * calling proceess (should be done both in libust and consumer). | |
100 | * See shm_overview(7) for details. | |
101 | * Pass file descriptor returned by shm_open(3) to ltt-sessiond through | |
102 | * a UNIX socket. | |
103 | * | |
104 | * Since we don't need to access the object using its name, we can | |
105 | * immediately shm_unlink(3) it, and only keep the handle with its file | |
106 | * descriptor. | |
107 | */ | |
108 | ||
852c2936 MD |
109 | /* |
110 | * Internal structure representing offsets to use at a sub-buffer switch. | |
111 | */ | |
112 | struct switch_offsets { | |
113 | unsigned long begin, end, old; | |
114 | size_t pre_header_padding, size; | |
1ad21f70 MD |
115 | unsigned int switch_new_start:1, switch_new_end:1, switch_old_start:1, |
116 | switch_old_end:1; | |
852c2936 MD |
117 | }; |
118 | ||
8c90a710 | 119 | DEFINE_URCU_TLS(unsigned int, lib_ring_buffer_nesting); |
852c2936 | 120 | |
cb7378b3 MD |
121 | /* |
122 | * wakeup_fd_mutex protects wakeup fd use by timer from concurrent | |
123 | * close. | |
124 | */ | |
125 | static pthread_mutex_t wakeup_fd_mutex = PTHREAD_MUTEX_INITIALIZER; | |
126 | ||
852c2936 MD |
127 | static |
128 | void lib_ring_buffer_print_errors(struct channel *chan, | |
009769ca MD |
129 | struct lttng_ust_lib_ring_buffer *buf, int cpu, |
130 | struct lttng_ust_shm_handle *handle); | |
852c2936 | 131 | |
03d2d293 MD |
132 | /* |
133 | * Handle timer teardown race wrt memory free of private data by | |
134 | * ring buffer signals are handled by a single thread, which permits | |
135 | * a synchronization point between handling of each signal. | |
64bf51a6 | 136 | * Protected by the lock within the structure. |
03d2d293 MD |
137 | */ |
138 | struct timer_signal_data { | |
139 | pthread_t tid; /* thread id managing signals */ | |
140 | int setup_done; | |
141 | int qs_done; | |
64bf51a6 | 142 | pthread_mutex_t lock; |
03d2d293 MD |
143 | }; |
144 | ||
64bf51a6 MD |
145 | static struct timer_signal_data timer_signal = { |
146 | .tid = 0, | |
147 | .setup_done = 0, | |
148 | .qs_done = 0, | |
149 | .lock = PTHREAD_MUTEX_INITIALIZER, | |
150 | }; | |
03d2d293 | 151 | |
852c2936 MD |
152 | /** |
153 | * lib_ring_buffer_reset - Reset ring buffer to initial values. | |
154 | * @buf: Ring buffer. | |
155 | * | |
156 | * Effectively empty the ring buffer. Should be called when the buffer is not | |
157 | * used for writing. The ring buffer can be opened for reading, but the reader | |
158 | * should not be using the iterator concurrently with reset. The previous | |
159 | * current iterator record is reset. | |
160 | */ | |
4cfec15c | 161 | void lib_ring_buffer_reset(struct lttng_ust_lib_ring_buffer *buf, |
38fae1d3 | 162 | struct lttng_ust_shm_handle *handle) |
852c2936 | 163 | { |
34daae3e MD |
164 | struct channel *chan; |
165 | const struct lttng_ust_lib_ring_buffer_config *config; | |
852c2936 MD |
166 | unsigned int i; |
167 | ||
34daae3e MD |
168 | chan = shmp(handle, buf->backend.chan); |
169 | if (!chan) | |
15500a1b | 170 | return; |
34daae3e | 171 | config = &chan->backend.config; |
852c2936 MD |
172 | /* |
173 | * Reset iterator first. It will put the subbuffer if it currently holds | |
174 | * it. | |
175 | */ | |
852c2936 MD |
176 | v_set(config, &buf->offset, 0); |
177 | for (i = 0; i < chan->backend.num_subbuf; i++) { | |
15500a1b MD |
178 | struct commit_counters_hot *cc_hot; |
179 | struct commit_counters_cold *cc_cold; | |
180 | ||
181 | cc_hot = shmp_index(handle, buf->commit_hot, i); | |
182 | if (!cc_hot) | |
183 | return; | |
184 | cc_cold = shmp_index(handle, buf->commit_cold, i); | |
185 | if (!cc_cold) | |
186 | return; | |
187 | v_set(config, &cc_hot->cc, 0); | |
188 | v_set(config, &cc_hot->seq, 0); | |
189 | v_set(config, &cc_cold->cc_sb, 0); | |
852c2936 | 190 | } |
a6352fd4 MD |
191 | uatomic_set(&buf->consumed, 0); |
192 | uatomic_set(&buf->record_disabled, 0); | |
852c2936 | 193 | v_set(config, &buf->last_tsc, 0); |
1d498196 | 194 | lib_ring_buffer_backend_reset(&buf->backend, handle); |
852c2936 MD |
195 | /* Don't reset number of active readers */ |
196 | v_set(config, &buf->records_lost_full, 0); | |
197 | v_set(config, &buf->records_lost_wrap, 0); | |
198 | v_set(config, &buf->records_lost_big, 0); | |
199 | v_set(config, &buf->records_count, 0); | |
200 | v_set(config, &buf->records_overrun, 0); | |
201 | buf->finalized = 0; | |
202 | } | |
852c2936 MD |
203 | |
204 | /** | |
205 | * channel_reset - Reset channel to initial values. | |
206 | * @chan: Channel. | |
207 | * | |
208 | * Effectively empty the channel. Should be called when the channel is not used | |
209 | * for writing. The channel can be opened for reading, but the reader should not | |
210 | * be using the iterator concurrently with reset. The previous current iterator | |
211 | * record is reset. | |
212 | */ | |
213 | void channel_reset(struct channel *chan) | |
214 | { | |
215 | /* | |
216 | * Reset iterators first. Will put the subbuffer if held for reading. | |
217 | */ | |
a6352fd4 | 218 | uatomic_set(&chan->record_disabled, 0); |
852c2936 MD |
219 | /* Don't reset commit_count_mask, still valid */ |
220 | channel_backend_reset(&chan->backend); | |
221 | /* Don't reset switch/read timer interval */ | |
222 | /* Don't reset notifiers and notifier enable bits */ | |
223 | /* Don't reset reader reference count */ | |
224 | } | |
852c2936 | 225 | |
a9ff648c MD |
226 | static |
227 | void init_crash_abi(const struct lttng_ust_lib_ring_buffer_config *config, | |
228 | struct lttng_crash_abi *crash_abi, | |
229 | struct lttng_ust_lib_ring_buffer *buf, | |
230 | struct channel_backend *chanb, | |
231 | struct shm_object *shmobj, | |
232 | struct lttng_ust_shm_handle *handle) | |
233 | { | |
234 | int i; | |
235 | ||
236 | for (i = 0; i < RB_CRASH_DUMP_ABI_MAGIC_LEN; i++) | |
237 | crash_abi->magic[i] = lttng_crash_magic_xor[i] ^ 0xFF; | |
238 | crash_abi->mmap_length = shmobj->memory_map_size; | |
239 | crash_abi->endian = RB_CRASH_ENDIAN; | |
240 | crash_abi->major = RB_CRASH_DUMP_ABI_MAJOR; | |
241 | crash_abi->minor = RB_CRASH_DUMP_ABI_MINOR; | |
242 | crash_abi->word_size = sizeof(unsigned long); | |
243 | crash_abi->layout_type = LTTNG_CRASH_TYPE_UST; | |
244 | ||
245 | /* Offset of fields */ | |
246 | crash_abi->offset.prod_offset = | |
247 | (uint32_t) ((char *) &buf->offset - (char *) buf); | |
248 | crash_abi->offset.consumed_offset = | |
249 | (uint32_t) ((char *) &buf->consumed - (char *) buf); | |
250 | crash_abi->offset.commit_hot_array = | |
251 | (uint32_t) ((char *) shmp(handle, buf->commit_hot) - (char *) buf); | |
252 | crash_abi->offset.commit_hot_seq = | |
253 | offsetof(struct commit_counters_hot, seq); | |
254 | crash_abi->offset.buf_wsb_array = | |
255 | (uint32_t) ((char *) shmp(handle, buf->backend.buf_wsb) - (char *) buf); | |
256 | crash_abi->offset.buf_wsb_id = | |
257 | offsetof(struct lttng_ust_lib_ring_buffer_backend_subbuffer, id); | |
258 | crash_abi->offset.sb_array = | |
259 | (uint32_t) ((char *) shmp(handle, buf->backend.array) - (char *) buf); | |
260 | crash_abi->offset.sb_array_shmp_offset = | |
261 | offsetof(struct lttng_ust_lib_ring_buffer_backend_pages_shmp, | |
262 | shmp._ref.offset); | |
263 | crash_abi->offset.sb_backend_p_offset = | |
264 | offsetof(struct lttng_ust_lib_ring_buffer_backend_pages, | |
265 | p._ref.offset); | |
266 | ||
267 | /* Field length */ | |
268 | crash_abi->length.prod_offset = sizeof(buf->offset); | |
269 | crash_abi->length.consumed_offset = sizeof(buf->consumed); | |
270 | crash_abi->length.commit_hot_seq = | |
271 | sizeof(((struct commit_counters_hot *) NULL)->seq); | |
272 | crash_abi->length.buf_wsb_id = | |
273 | sizeof(((struct lttng_ust_lib_ring_buffer_backend_subbuffer *) NULL)->id); | |
274 | crash_abi->length.sb_array_shmp_offset = | |
275 | sizeof(((struct lttng_ust_lib_ring_buffer_backend_pages_shmp *) NULL)->shmp._ref.offset); | |
276 | crash_abi->length.sb_backend_p_offset = | |
277 | sizeof(((struct lttng_ust_lib_ring_buffer_backend_pages *) NULL)->p._ref.offset); | |
278 | ||
279 | /* Array stride */ | |
280 | crash_abi->stride.commit_hot_array = | |
281 | sizeof(struct commit_counters_hot); | |
282 | crash_abi->stride.buf_wsb_array = | |
283 | sizeof(struct lttng_ust_lib_ring_buffer_backend_subbuffer); | |
284 | crash_abi->stride.sb_array = | |
285 | sizeof(struct lttng_ust_lib_ring_buffer_backend_pages_shmp); | |
286 | ||
287 | /* Buffer constants */ | |
288 | crash_abi->buf_size = chanb->buf_size; | |
289 | crash_abi->subbuf_size = chanb->subbuf_size; | |
290 | crash_abi->num_subbuf = chanb->num_subbuf; | |
291 | crash_abi->mode = (uint32_t) chanb->config.mode; | |
292 | ||
293 | if (config->cb.content_size_field) { | |
294 | size_t offset, length; | |
295 | ||
296 | config->cb.content_size_field(config, &offset, &length); | |
297 | crash_abi->offset.content_size = offset; | |
298 | crash_abi->length.content_size = length; | |
299 | } else { | |
300 | crash_abi->offset.content_size = 0; | |
301 | crash_abi->length.content_size = 0; | |
302 | } | |
303 | if (config->cb.packet_size_field) { | |
304 | size_t offset, length; | |
305 | ||
306 | config->cb.packet_size_field(config, &offset, &length); | |
307 | crash_abi->offset.packet_size = offset; | |
308 | crash_abi->length.packet_size = length; | |
309 | } else { | |
310 | crash_abi->offset.packet_size = 0; | |
311 | crash_abi->length.packet_size = 0; | |
312 | } | |
313 | } | |
314 | ||
852c2936 MD |
315 | /* |
316 | * Must be called under cpu hotplug protection. | |
317 | */ | |
4cfec15c | 318 | int lib_ring_buffer_create(struct lttng_ust_lib_ring_buffer *buf, |
a6352fd4 | 319 | struct channel_backend *chanb, int cpu, |
38fae1d3 | 320 | struct lttng_ust_shm_handle *handle, |
1d498196 | 321 | struct shm_object *shmobj) |
852c2936 | 322 | { |
4cfec15c | 323 | const struct lttng_ust_lib_ring_buffer_config *config = &chanb->config; |
14641deb | 324 | struct channel *chan = caa_container_of(chanb, struct channel, backend); |
15500a1b MD |
325 | struct lttng_ust_lib_ring_buffer_backend_subbuffer *wsb; |
326 | struct channel *shmp_chan; | |
327 | struct commit_counters_hot *cc_hot; | |
a3f61e7f | 328 | void *priv = channel_get_private(chan); |
852c2936 | 329 | size_t subbuf_header_size; |
2fed87ae | 330 | uint64_t tsc; |
852c2936 MD |
331 | int ret; |
332 | ||
333 | /* Test for cpu hotplug */ | |
334 | if (buf->backend.allocated) | |
335 | return 0; | |
336 | ||
1d498196 MD |
337 | align_shm(shmobj, __alignof__(struct commit_counters_hot)); |
338 | set_shmp(buf->commit_hot, | |
339 | zalloc_shm(shmobj, | |
340 | sizeof(struct commit_counters_hot) * chan->backend.num_subbuf)); | |
341 | if (!shmp(handle, buf->commit_hot)) { | |
a9ff648c | 342 | return -ENOMEM; |
852c2936 MD |
343 | } |
344 | ||
1d498196 MD |
345 | align_shm(shmobj, __alignof__(struct commit_counters_cold)); |
346 | set_shmp(buf->commit_cold, | |
347 | zalloc_shm(shmobj, | |
348 | sizeof(struct commit_counters_cold) * chan->backend.num_subbuf)); | |
349 | if (!shmp(handle, buf->commit_cold)) { | |
852c2936 MD |
350 | ret = -ENOMEM; |
351 | goto free_commit; | |
352 | } | |
353 | ||
a9ff648c MD |
354 | ret = lib_ring_buffer_backend_create(&buf->backend, &chan->backend, |
355 | cpu, handle, shmobj); | |
356 | if (ret) { | |
357 | goto free_init; | |
358 | } | |
359 | ||
852c2936 MD |
360 | /* |
361 | * Write the subbuffer header for first subbuffer so we know the total | |
362 | * duration of data gathering. | |
363 | */ | |
364 | subbuf_header_size = config->cb.subbuffer_header_size(); | |
365 | v_set(config, &buf->offset, subbuf_header_size); | |
15500a1b MD |
366 | wsb = shmp_index(handle, buf->backend.buf_wsb, 0); |
367 | if (!wsb) { | |
368 | ret = -EPERM; | |
369 | goto free_chanbuf; | |
370 | } | |
371 | subbuffer_id_clear_noref(config, &wsb->id); | |
372 | shmp_chan = shmp(handle, buf->backend.chan); | |
373 | if (!shmp_chan) { | |
374 | ret = -EPERM; | |
375 | goto free_chanbuf; | |
376 | } | |
377 | tsc = config->cb.ring_buffer_clock_read(shmp_chan); | |
1d498196 | 378 | config->cb.buffer_begin(buf, tsc, 0, handle); |
15500a1b MD |
379 | cc_hot = shmp_index(handle, buf->commit_hot, 0); |
380 | if (!cc_hot) { | |
381 | ret = -EPERM; | |
382 | goto free_chanbuf; | |
383 | } | |
384 | v_add(config, subbuf_header_size, &cc_hot->cc); | |
385 | v_add(config, subbuf_header_size, &cc_hot->seq); | |
852c2936 MD |
386 | |
387 | if (config->cb.buffer_create) { | |
1d498196 | 388 | ret = config->cb.buffer_create(buf, priv, cpu, chanb->name, handle); |
852c2936 | 389 | if (ret) |
a9ff648c | 390 | goto free_chanbuf; |
852c2936 | 391 | } |
a9ff648c MD |
392 | |
393 | init_crash_abi(config, &buf->crash_abi, buf, chanb, shmobj, handle); | |
394 | ||
852c2936 | 395 | buf->backend.allocated = 1; |
852c2936 MD |
396 | return 0; |
397 | ||
398 | /* Error handling */ | |
399 | free_init: | |
a6352fd4 | 400 | /* commit_cold will be freed by shm teardown */ |
852c2936 | 401 | free_commit: |
a6352fd4 | 402 | /* commit_hot will be freed by shm teardown */ |
852c2936 | 403 | free_chanbuf: |
852c2936 MD |
404 | return ret; |
405 | } | |
406 | ||
03d2d293 MD |
407 | static |
408 | void lib_ring_buffer_channel_switch_timer(int sig, siginfo_t *si, void *uc) | |
852c2936 | 409 | { |
03d2d293 MD |
410 | const struct lttng_ust_lib_ring_buffer_config *config; |
411 | struct lttng_ust_shm_handle *handle; | |
412 | struct channel *chan; | |
413 | int cpu; | |
414 | ||
415 | assert(CMM_LOAD_SHARED(timer_signal.tid) == pthread_self()); | |
416 | ||
417 | chan = si->si_value.sival_ptr; | |
418 | handle = chan->handle; | |
419 | config = &chan->backend.config; | |
852c2936 | 420 | |
34a91bdb | 421 | DBG("Switch timer for channel %p\n", chan); |
03d2d293 | 422 | |
4dcd7e80 MD |
423 | /* |
424 | * Only flush buffers periodically if readers are active. | |
425 | */ | |
cb7378b3 | 426 | pthread_mutex_lock(&wakeup_fd_mutex); |
03d2d293 MD |
427 | if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) { |
428 | for_each_possible_cpu(cpu) { | |
429 | struct lttng_ust_lib_ring_buffer *buf = | |
430 | shmp(handle, chan->backend.buf[cpu].shmp); | |
34daae3e MD |
431 | |
432 | if (!buf) | |
15500a1b | 433 | goto end; |
4dcd7e80 MD |
434 | if (uatomic_read(&buf->active_readers)) |
435 | lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE, | |
436 | chan->handle); | |
03d2d293 MD |
437 | } |
438 | } else { | |
439 | struct lttng_ust_lib_ring_buffer *buf = | |
440 | shmp(handle, chan->backend.buf[0].shmp); | |
441 | ||
34daae3e | 442 | if (!buf) |
15500a1b | 443 | goto end; |
34a91bdb MD |
444 | if (uatomic_read(&buf->active_readers)) |
445 | lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE, | |
446 | chan->handle); | |
447 | } | |
15500a1b | 448 | end: |
34a91bdb MD |
449 | pthread_mutex_unlock(&wakeup_fd_mutex); |
450 | return; | |
451 | } | |
452 | ||
b07cd987 MD |
453 | static |
454 | int lib_ring_buffer_poll_deliver(const struct lttng_ust_lib_ring_buffer_config *config, | |
455 | struct lttng_ust_lib_ring_buffer *buf, | |
456 | struct channel *chan, | |
457 | struct lttng_ust_shm_handle *handle) | |
458 | { | |
459 | unsigned long consumed_old, consumed_idx, commit_count, write_offset; | |
15500a1b | 460 | struct commit_counters_cold *cc_cold; |
b07cd987 MD |
461 | |
462 | consumed_old = uatomic_read(&buf->consumed); | |
463 | consumed_idx = subbuf_index(consumed_old, chan); | |
15500a1b MD |
464 | cc_cold = shmp_index(handle, buf->commit_cold, consumed_idx); |
465 | if (!cc_cold) | |
466 | return 0; | |
467 | commit_count = v_read(config, &cc_cold->cc_sb); | |
b07cd987 MD |
468 | /* |
469 | * No memory barrier here, since we are only interested | |
470 | * in a statistically correct polling result. The next poll will | |
471 | * get the data is we are racing. The mb() that ensures correct | |
472 | * memory order is in get_subbuf. | |
473 | */ | |
474 | write_offset = v_read(config, &buf->offset); | |
475 | ||
476 | /* | |
477 | * Check that the subbuffer we are trying to consume has been | |
478 | * already fully committed. | |
479 | */ | |
480 | ||
481 | if (((commit_count - chan->backend.subbuf_size) | |
482 | & chan->commit_count_mask) | |
483 | - (buf_trunc(consumed_old, chan) | |
484 | >> chan->backend.num_subbuf_order) | |
485 | != 0) | |
486 | return 0; | |
487 | ||
488 | /* | |
489 | * Check that we are not about to read the same subbuffer in | |
490 | * which the writer head is. | |
491 | */ | |
492 | if (subbuf_trunc(write_offset, chan) - subbuf_trunc(consumed_old, chan) | |
493 | == 0) | |
494 | return 0; | |
495 | ||
496 | return 1; | |
497 | } | |
498 | ||
499 | static | |
500 | void lib_ring_buffer_wakeup(struct lttng_ust_lib_ring_buffer *buf, | |
501 | struct lttng_ust_shm_handle *handle) | |
502 | { | |
503 | int wakeup_fd = shm_get_wakeup_fd(handle, &buf->self._ref); | |
504 | sigset_t sigpipe_set, pending_set, old_set; | |
505 | int ret, sigpipe_was_pending = 0; | |
506 | ||
507 | if (wakeup_fd < 0) | |
508 | return; | |
509 | ||
510 | /* | |
511 | * Wake-up the other end by writing a null byte in the pipe | |
512 | * (non-blocking). Important note: Because writing into the | |
513 | * pipe is non-blocking (and therefore we allow dropping wakeup | |
514 | * data, as long as there is wakeup data present in the pipe | |
515 | * buffer to wake up the consumer), the consumer should perform | |
516 | * the following sequence for waiting: | |
517 | * 1) empty the pipe (reads). | |
518 | * 2) check if there is data in the buffer. | |
519 | * 3) wait on the pipe (poll). | |
520 | * | |
521 | * Discard the SIGPIPE from write(), not disturbing any SIGPIPE | |
522 | * that might be already pending. If a bogus SIGPIPE is sent to | |
523 | * the entire process concurrently by a malicious user, it may | |
524 | * be simply discarded. | |
525 | */ | |
526 | ret = sigemptyset(&pending_set); | |
527 | assert(!ret); | |
528 | /* | |
529 | * sigpending returns the mask of signals that are _both_ | |
530 | * blocked for the thread _and_ pending for either the thread or | |
531 | * the entire process. | |
532 | */ | |
533 | ret = sigpending(&pending_set); | |
534 | assert(!ret); | |
535 | sigpipe_was_pending = sigismember(&pending_set, SIGPIPE); | |
536 | /* | |
537 | * If sigpipe was pending, it means it was already blocked, so | |
538 | * no need to block it. | |
539 | */ | |
540 | if (!sigpipe_was_pending) { | |
541 | ret = sigemptyset(&sigpipe_set); | |
542 | assert(!ret); | |
543 | ret = sigaddset(&sigpipe_set, SIGPIPE); | |
544 | assert(!ret); | |
545 | ret = pthread_sigmask(SIG_BLOCK, &sigpipe_set, &old_set); | |
546 | assert(!ret); | |
547 | } | |
548 | do { | |
549 | ret = write(wakeup_fd, "", 1); | |
550 | } while (ret == -1L && errno == EINTR); | |
551 | if (ret == -1L && errno == EPIPE && !sigpipe_was_pending) { | |
552 | struct timespec timeout = { 0, 0 }; | |
553 | do { | |
554 | ret = sigtimedwait(&sigpipe_set, NULL, | |
555 | &timeout); | |
556 | } while (ret == -1L && errno == EINTR); | |
557 | } | |
558 | if (!sigpipe_was_pending) { | |
559 | ret = pthread_sigmask(SIG_SETMASK, &old_set, NULL); | |
560 | assert(!ret); | |
561 | } | |
562 | } | |
563 | ||
34a91bdb MD |
564 | static |
565 | void lib_ring_buffer_channel_do_read(struct channel *chan) | |
566 | { | |
567 | const struct lttng_ust_lib_ring_buffer_config *config; | |
568 | struct lttng_ust_shm_handle *handle; | |
569 | int cpu; | |
570 | ||
571 | handle = chan->handle; | |
572 | config = &chan->backend.config; | |
573 | ||
574 | /* | |
575 | * Only flush buffers periodically if readers are active. | |
576 | */ | |
577 | pthread_mutex_lock(&wakeup_fd_mutex); | |
578 | if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) { | |
579 | for_each_possible_cpu(cpu) { | |
580 | struct lttng_ust_lib_ring_buffer *buf = | |
581 | shmp(handle, chan->backend.buf[cpu].shmp); | |
582 | ||
34daae3e | 583 | if (!buf) |
15500a1b | 584 | goto end; |
34a91bdb MD |
585 | if (uatomic_read(&buf->active_readers) |
586 | && lib_ring_buffer_poll_deliver(config, buf, | |
587 | chan, handle)) { | |
588 | lib_ring_buffer_wakeup(buf, handle); | |
589 | } | |
590 | } | |
591 | } else { | |
592 | struct lttng_ust_lib_ring_buffer *buf = | |
593 | shmp(handle, chan->backend.buf[0].shmp); | |
594 | ||
34daae3e | 595 | if (!buf) |
15500a1b | 596 | goto end; |
34a91bdb MD |
597 | if (uatomic_read(&buf->active_readers) |
598 | && lib_ring_buffer_poll_deliver(config, buf, | |
599 | chan, handle)) { | |
600 | lib_ring_buffer_wakeup(buf, handle); | |
601 | } | |
03d2d293 | 602 | } |
15500a1b | 603 | end: |
cb7378b3 | 604 | pthread_mutex_unlock(&wakeup_fd_mutex); |
34a91bdb MD |
605 | } |
606 | ||
607 | static | |
608 | void lib_ring_buffer_channel_read_timer(int sig, siginfo_t *si, void *uc) | |
609 | { | |
610 | struct channel *chan; | |
611 | ||
612 | assert(CMM_LOAD_SHARED(timer_signal.tid) == pthread_self()); | |
613 | chan = si->si_value.sival_ptr; | |
614 | DBG("Read timer for channel %p\n", chan); | |
615 | lib_ring_buffer_channel_do_read(chan); | |
03d2d293 MD |
616 | return; |
617 | } | |
618 | ||
619 | static | |
620 | void rb_setmask(sigset_t *mask) | |
621 | { | |
622 | int ret; | |
623 | ||
624 | ret = sigemptyset(mask); | |
625 | if (ret) { | |
626 | PERROR("sigemptyset"); | |
627 | } | |
34a91bdb MD |
628 | ret = sigaddset(mask, LTTNG_UST_RB_SIG_FLUSH); |
629 | if (ret) { | |
630 | PERROR("sigaddset"); | |
631 | } | |
632 | ret = sigaddset(mask, LTTNG_UST_RB_SIG_READ); | |
03d2d293 MD |
633 | if (ret) { |
634 | PERROR("sigaddset"); | |
635 | } | |
636 | ret = sigaddset(mask, LTTNG_UST_RB_SIG_TEARDOWN); | |
637 | if (ret) { | |
638 | PERROR("sigaddset"); | |
639 | } | |
640 | } | |
641 | ||
642 | static | |
643 | void *sig_thread(void *arg) | |
644 | { | |
645 | sigset_t mask; | |
646 | siginfo_t info; | |
647 | int signr; | |
648 | ||
649 | /* Only self thread will receive signal mask. */ | |
650 | rb_setmask(&mask); | |
651 | CMM_STORE_SHARED(timer_signal.tid, pthread_self()); | |
652 | ||
653 | for (;;) { | |
654 | signr = sigwaitinfo(&mask, &info); | |
655 | if (signr == -1) { | |
34a91bdb MD |
656 | if (errno != EINTR) |
657 | PERROR("sigwaitinfo"); | |
03d2d293 MD |
658 | continue; |
659 | } | |
34a91bdb | 660 | if (signr == LTTNG_UST_RB_SIG_FLUSH) { |
03d2d293 MD |
661 | lib_ring_buffer_channel_switch_timer(info.si_signo, |
662 | &info, NULL); | |
34a91bdb MD |
663 | } else if (signr == LTTNG_UST_RB_SIG_READ) { |
664 | lib_ring_buffer_channel_read_timer(info.si_signo, | |
665 | &info, NULL); | |
03d2d293 MD |
666 | } else if (signr == LTTNG_UST_RB_SIG_TEARDOWN) { |
667 | cmm_smp_mb(); | |
668 | CMM_STORE_SHARED(timer_signal.qs_done, 1); | |
669 | cmm_smp_mb(); | |
670 | } else { | |
671 | ERR("Unexptected signal %d\n", info.si_signo); | |
672 | } | |
673 | } | |
674 | return NULL; | |
675 | } | |
676 | ||
677 | /* | |
03d2d293 MD |
678 | * Ensure only a single thread listens on the timer signal. |
679 | */ | |
680 | static | |
681 | void lib_ring_buffer_setup_timer_thread(void) | |
682 | { | |
683 | pthread_t thread; | |
684 | int ret; | |
685 | ||
64bf51a6 | 686 | pthread_mutex_lock(&timer_signal.lock); |
03d2d293 | 687 | if (timer_signal.setup_done) |
64bf51a6 | 688 | goto end; |
03d2d293 MD |
689 | |
690 | ret = pthread_create(&thread, NULL, &sig_thread, NULL); | |
691 | if (ret) { | |
692 | errno = ret; | |
693 | PERROR("pthread_create"); | |
694 | } | |
695 | ret = pthread_detach(thread); | |
696 | if (ret) { | |
697 | errno = ret; | |
698 | PERROR("pthread_detach"); | |
699 | } | |
700 | timer_signal.setup_done = 1; | |
64bf51a6 MD |
701 | end: |
702 | pthread_mutex_unlock(&timer_signal.lock); | |
852c2936 MD |
703 | } |
704 | ||
03d2d293 | 705 | /* |
64bf51a6 | 706 | * Wait for signal-handling thread quiescent state. |
03d2d293 | 707 | */ |
64bf51a6 MD |
708 | static |
709 | void lib_ring_buffer_wait_signal_thread_qs(unsigned int signr) | |
710 | { | |
711 | sigset_t pending_set; | |
712 | int ret; | |
713 | ||
714 | /* | |
715 | * We need to be the only thread interacting with the thread | |
716 | * that manages signals for teardown synchronization. | |
717 | */ | |
718 | pthread_mutex_lock(&timer_signal.lock); | |
719 | ||
720 | /* | |
721 | * Ensure we don't have any signal queued for this channel. | |
722 | */ | |
723 | for (;;) { | |
724 | ret = sigemptyset(&pending_set); | |
725 | if (ret == -1) { | |
726 | PERROR("sigemptyset"); | |
727 | } | |
728 | ret = sigpending(&pending_set); | |
729 | if (ret == -1) { | |
730 | PERROR("sigpending"); | |
731 | } | |
732 | if (!sigismember(&pending_set, signr)) | |
733 | break; | |
734 | caa_cpu_relax(); | |
735 | } | |
736 | ||
737 | /* | |
738 | * From this point, no new signal handler will be fired that | |
739 | * would try to access "chan". However, we still need to wait | |
740 | * for any currently executing handler to complete. | |
741 | */ | |
742 | cmm_smp_mb(); | |
743 | CMM_STORE_SHARED(timer_signal.qs_done, 0); | |
744 | cmm_smp_mb(); | |
745 | ||
746 | /* | |
747 | * Kill with LTTNG_UST_RB_SIG_TEARDOWN, so signal management | |
748 | * thread wakes up. | |
749 | */ | |
750 | kill(getpid(), LTTNG_UST_RB_SIG_TEARDOWN); | |
751 | ||
752 | while (!CMM_LOAD_SHARED(timer_signal.qs_done)) | |
753 | caa_cpu_relax(); | |
754 | cmm_smp_mb(); | |
755 | ||
756 | pthread_mutex_unlock(&timer_signal.lock); | |
757 | } | |
758 | ||
03d2d293 MD |
759 | static |
760 | void lib_ring_buffer_channel_switch_timer_start(struct channel *chan) | |
852c2936 | 761 | { |
03d2d293 MD |
762 | struct sigevent sev; |
763 | struct itimerspec its; | |
764 | int ret; | |
852c2936 | 765 | |
03d2d293 | 766 | if (!chan->switch_timer_interval || chan->switch_timer_enabled) |
852c2936 MD |
767 | return; |
768 | ||
03d2d293 MD |
769 | chan->switch_timer_enabled = 1; |
770 | ||
771 | lib_ring_buffer_setup_timer_thread(); | |
772 | ||
773 | sev.sigev_notify = SIGEV_SIGNAL; | |
34a91bdb | 774 | sev.sigev_signo = LTTNG_UST_RB_SIG_FLUSH; |
03d2d293 MD |
775 | sev.sigev_value.sival_ptr = chan; |
776 | ret = timer_create(CLOCKID, &sev, &chan->switch_timer); | |
777 | if (ret == -1) { | |
778 | PERROR("timer_create"); | |
779 | } | |
780 | ||
781 | its.it_value.tv_sec = chan->switch_timer_interval / 1000000; | |
f8921d33 | 782 | its.it_value.tv_nsec = (chan->switch_timer_interval % 1000000) * 1000; |
03d2d293 MD |
783 | its.it_interval.tv_sec = its.it_value.tv_sec; |
784 | its.it_interval.tv_nsec = its.it_value.tv_nsec; | |
785 | ||
786 | ret = timer_settime(chan->switch_timer, 0, &its, NULL); | |
787 | if (ret == -1) { | |
788 | PERROR("timer_settime"); | |
789 | } | |
790 | } | |
791 | ||
03d2d293 MD |
792 | static |
793 | void lib_ring_buffer_channel_switch_timer_stop(struct channel *chan) | |
794 | { | |
34a91bdb | 795 | int ret; |
03d2d293 MD |
796 | |
797 | if (!chan->switch_timer_interval || !chan->switch_timer_enabled) | |
798 | return; | |
799 | ||
800 | ret = timer_delete(chan->switch_timer); | |
801 | if (ret == -1) { | |
802 | PERROR("timer_delete"); | |
803 | } | |
804 | ||
64bf51a6 | 805 | lib_ring_buffer_wait_signal_thread_qs(LTTNG_UST_RB_SIG_FLUSH); |
03d2d293 MD |
806 | |
807 | chan->switch_timer = 0; | |
808 | chan->switch_timer_enabled = 0; | |
852c2936 MD |
809 | } |
810 | ||
34a91bdb MD |
811 | static |
812 | void lib_ring_buffer_channel_read_timer_start(struct channel *chan) | |
852c2936 | 813 | { |
4cfec15c | 814 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
34a91bdb MD |
815 | struct sigevent sev; |
816 | struct itimerspec its; | |
817 | int ret; | |
852c2936 | 818 | |
34a91bdb MD |
819 | if (config->wakeup != RING_BUFFER_WAKEUP_BY_TIMER |
820 | || !chan->read_timer_interval || chan->read_timer_enabled) | |
821 | return; | |
852c2936 | 822 | |
34a91bdb | 823 | chan->read_timer_enabled = 1; |
852c2936 | 824 | |
34a91bdb | 825 | lib_ring_buffer_setup_timer_thread(); |
852c2936 | 826 | |
34a91bdb MD |
827 | sev.sigev_notify = SIGEV_SIGNAL; |
828 | sev.sigev_signo = LTTNG_UST_RB_SIG_READ; | |
829 | sev.sigev_value.sival_ptr = chan; | |
830 | ret = timer_create(CLOCKID, &sev, &chan->read_timer); | |
831 | if (ret == -1) { | |
832 | PERROR("timer_create"); | |
833 | } | |
852c2936 | 834 | |
34a91bdb | 835 | its.it_value.tv_sec = chan->read_timer_interval / 1000000; |
f8921d33 | 836 | its.it_value.tv_nsec = (chan->read_timer_interval % 1000000) * 1000; |
34a91bdb MD |
837 | its.it_interval.tv_sec = its.it_value.tv_sec; |
838 | its.it_interval.tv_nsec = its.it_value.tv_nsec; | |
852c2936 | 839 | |
34a91bdb MD |
840 | ret = timer_settime(chan->read_timer, 0, &its, NULL); |
841 | if (ret == -1) { | |
842 | PERROR("timer_settime"); | |
843 | } | |
852c2936 MD |
844 | } |
845 | ||
34a91bdb MD |
846 | static |
847 | void lib_ring_buffer_channel_read_timer_stop(struct channel *chan) | |
852c2936 | 848 | { |
4cfec15c | 849 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
34a91bdb | 850 | int ret; |
852c2936 MD |
851 | |
852 | if (config->wakeup != RING_BUFFER_WAKEUP_BY_TIMER | |
34a91bdb | 853 | || !chan->read_timer_interval || !chan->read_timer_enabled) |
852c2936 MD |
854 | return; |
855 | ||
34a91bdb MD |
856 | ret = timer_delete(chan->read_timer); |
857 | if (ret == -1) { | |
858 | PERROR("timer_delete"); | |
859 | } | |
860 | ||
852c2936 MD |
861 | /* |
862 | * do one more check to catch data that has been written in the last | |
863 | * timer period. | |
864 | */ | |
34a91bdb MD |
865 | lib_ring_buffer_channel_do_read(chan); |
866 | ||
64bf51a6 | 867 | lib_ring_buffer_wait_signal_thread_qs(LTTNG_UST_RB_SIG_READ); |
34a91bdb MD |
868 | |
869 | chan->read_timer = 0; | |
870 | chan->read_timer_enabled = 0; | |
852c2936 MD |
871 | } |
872 | ||
1d498196 | 873 | static void channel_unregister_notifiers(struct channel *chan, |
38fae1d3 | 874 | struct lttng_ust_shm_handle *handle) |
852c2936 | 875 | { |
03d2d293 | 876 | lib_ring_buffer_channel_switch_timer_stop(chan); |
34a91bdb | 877 | lib_ring_buffer_channel_read_timer_stop(chan); |
852c2936 MD |
878 | } |
879 | ||
009769ca MD |
880 | static void channel_print_errors(struct channel *chan, |
881 | struct lttng_ust_shm_handle *handle) | |
882 | { | |
883 | const struct lttng_ust_lib_ring_buffer_config *config = | |
884 | &chan->backend.config; | |
885 | int cpu; | |
886 | ||
887 | if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) { | |
888 | for_each_possible_cpu(cpu) { | |
889 | struct lttng_ust_lib_ring_buffer *buf = | |
890 | shmp(handle, chan->backend.buf[cpu].shmp); | |
15500a1b MD |
891 | if (buf) |
892 | lib_ring_buffer_print_errors(chan, buf, cpu, handle); | |
009769ca MD |
893 | } |
894 | } else { | |
895 | struct lttng_ust_lib_ring_buffer *buf = | |
896 | shmp(handle, chan->backend.buf[0].shmp); | |
897 | ||
15500a1b MD |
898 | if (buf) |
899 | lib_ring_buffer_print_errors(chan, buf, -1, handle); | |
009769ca MD |
900 | } |
901 | } | |
902 | ||
903 | static void channel_free(struct channel *chan, | |
6548fca4 MD |
904 | struct lttng_ust_shm_handle *handle, |
905 | int consumer) | |
852c2936 | 906 | { |
74d81a6c | 907 | channel_backend_free(&chan->backend, handle); |
431d5cf0 | 908 | /* chan is freed by shm teardown */ |
6548fca4 | 909 | shm_object_table_destroy(handle->table, consumer); |
1d498196 | 910 | free(handle); |
852c2936 MD |
911 | } |
912 | ||
913 | /** | |
914 | * channel_create - Create channel. | |
915 | * @config: ring buffer instance configuration | |
916 | * @name: name of the channel | |
a3f61e7f MD |
917 | * @priv_data: ring buffer client private data area pointer (output) |
918 | * @priv_data_size: length, in bytes, of the private data area. | |
d028eddb | 919 | * @priv_data_init: initialization data for private data. |
852c2936 MD |
920 | * @buf_addr: pointer the the beginning of the preallocated buffer contiguous |
921 | * address mapping. It is used only by RING_BUFFER_STATIC | |
922 | * configuration. It can be set to NULL for other backends. | |
923 | * @subbuf_size: subbuffer size | |
924 | * @num_subbuf: number of subbuffers | |
925 | * @switch_timer_interval: Time interval (in us) to fill sub-buffers with | |
926 | * padding to let readers get those sub-buffers. | |
927 | * Used for live streaming. | |
928 | * @read_timer_interval: Time interval (in us) to wake up pending readers. | |
5ea386c3 MD |
929 | * @stream_fds: array of stream file descriptors. |
930 | * @nr_stream_fds: number of file descriptors in array. | |
852c2936 MD |
931 | * |
932 | * Holds cpu hotplug. | |
933 | * Returns NULL on failure. | |
934 | */ | |
4cfec15c | 935 | struct lttng_ust_shm_handle *channel_create(const struct lttng_ust_lib_ring_buffer_config *config, |
a3f61e7f MD |
936 | const char *name, |
937 | void **priv_data, | |
938 | size_t priv_data_align, | |
939 | size_t priv_data_size, | |
d028eddb | 940 | void *priv_data_init, |
a3f61e7f | 941 | void *buf_addr, size_t subbuf_size, |
852c2936 | 942 | size_t num_subbuf, unsigned int switch_timer_interval, |
a9ff648c | 943 | unsigned int read_timer_interval, |
5ea386c3 | 944 | const int *stream_fds, int nr_stream_fds) |
852c2936 | 945 | { |
24622edc | 946 | int ret; |
a3f61e7f | 947 | size_t shmsize, chansize; |
852c2936 | 948 | struct channel *chan; |
38fae1d3 | 949 | struct lttng_ust_shm_handle *handle; |
1d498196 | 950 | struct shm_object *shmobj; |
74d81a6c MD |
951 | unsigned int nr_streams; |
952 | ||
953 | if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) | |
954 | nr_streams = num_possible_cpus(); | |
955 | else | |
956 | nr_streams = 1; | |
852c2936 | 957 | |
5ea386c3 MD |
958 | if (nr_stream_fds != nr_streams) |
959 | return NULL; | |
960 | ||
852c2936 MD |
961 | if (lib_ring_buffer_check_config(config, switch_timer_interval, |
962 | read_timer_interval)) | |
963 | return NULL; | |
964 | ||
38fae1d3 | 965 | handle = zmalloc(sizeof(struct lttng_ust_shm_handle)); |
431d5cf0 MD |
966 | if (!handle) |
967 | return NULL; | |
968 | ||
1d498196 MD |
969 | /* Allocate table for channel + per-cpu buffers */ |
970 | handle->table = shm_object_table_create(1 + num_possible_cpus()); | |
971 | if (!handle->table) | |
972 | goto error_table_alloc; | |
852c2936 | 973 | |
1d498196 MD |
974 | /* Calculate the shm allocation layout */ |
975 | shmsize = sizeof(struct channel); | |
c1fca457 | 976 | shmsize += offset_align(shmsize, __alignof__(struct lttng_ust_lib_ring_buffer_shmp)); |
74d81a6c | 977 | shmsize += sizeof(struct lttng_ust_lib_ring_buffer_shmp) * nr_streams; |
a3f61e7f | 978 | chansize = shmsize; |
74d81a6c MD |
979 | if (priv_data_align) |
980 | shmsize += offset_align(shmsize, priv_data_align); | |
a3f61e7f | 981 | shmsize += priv_data_size; |
a6352fd4 | 982 | |
74d81a6c | 983 | /* Allocate normal memory for channel (not shared) */ |
a9ff648c | 984 | shmobj = shm_object_table_alloc(handle->table, shmsize, SHM_OBJECT_MEM, |
5ea386c3 | 985 | -1); |
b5a14697 MD |
986 | if (!shmobj) |
987 | goto error_append; | |
57773204 | 988 | /* struct channel is at object 0, offset 0 (hardcoded) */ |
a3f61e7f | 989 | set_shmp(handle->chan, zalloc_shm(shmobj, chansize)); |
57773204 MD |
990 | assert(handle->chan._ref.index == 0); |
991 | assert(handle->chan._ref.offset == 0); | |
1d498196 | 992 | chan = shmp(handle, handle->chan); |
a6352fd4 | 993 | if (!chan) |
1d498196 | 994 | goto error_append; |
74d81a6c | 995 | chan->nr_streams = nr_streams; |
a6352fd4 | 996 | |
a3f61e7f MD |
997 | /* space for private data */ |
998 | if (priv_data_size) { | |
999 | DECLARE_SHMP(void, priv_data_alloc); | |
1000 | ||
1001 | align_shm(shmobj, priv_data_align); | |
1002 | chan->priv_data_offset = shmobj->allocated_len; | |
1003 | set_shmp(priv_data_alloc, zalloc_shm(shmobj, priv_data_size)); | |
1004 | if (!shmp(handle, priv_data_alloc)) | |
1005 | goto error_append; | |
1006 | *priv_data = channel_get_private(chan); | |
d028eddb | 1007 | memcpy(*priv_data, priv_data_init, priv_data_size); |
a3f61e7f MD |
1008 | } else { |
1009 | chan->priv_data_offset = -1; | |
74d81a6c MD |
1010 | if (priv_data) |
1011 | *priv_data = NULL; | |
a3f61e7f MD |
1012 | } |
1013 | ||
1014 | ret = channel_backend_init(&chan->backend, name, config, | |
a9ff648c | 1015 | subbuf_size, num_subbuf, handle, |
5ea386c3 | 1016 | stream_fds); |
852c2936 | 1017 | if (ret) |
1d498196 | 1018 | goto error_backend_init; |
852c2936 | 1019 | |
03d2d293 | 1020 | chan->handle = handle; |
852c2936 | 1021 | chan->commit_count_mask = (~0UL >> chan->backend.num_subbuf_order); |
852c2936 | 1022 | |
34a91bdb MD |
1023 | chan->switch_timer_interval = switch_timer_interval; |
1024 | chan->read_timer_interval = read_timer_interval; | |
03d2d293 | 1025 | lib_ring_buffer_channel_switch_timer_start(chan); |
34a91bdb | 1026 | lib_ring_buffer_channel_read_timer_start(chan); |
852c2936 | 1027 | |
431d5cf0 | 1028 | return handle; |
852c2936 | 1029 | |
1d498196 MD |
1030 | error_backend_init: |
1031 | error_append: | |
6548fca4 | 1032 | shm_object_table_destroy(handle->table, 1); |
1d498196 | 1033 | error_table_alloc: |
431d5cf0 | 1034 | free(handle); |
852c2936 MD |
1035 | return NULL; |
1036 | } | |
852c2936 | 1037 | |
74d81a6c | 1038 | struct lttng_ust_shm_handle *channel_handle_create(void *data, |
ff0f5728 MD |
1039 | uint64_t memory_map_size, |
1040 | int wakeup_fd) | |
193183fb | 1041 | { |
38fae1d3 | 1042 | struct lttng_ust_shm_handle *handle; |
193183fb MD |
1043 | struct shm_object *object; |
1044 | ||
38fae1d3 | 1045 | handle = zmalloc(sizeof(struct lttng_ust_shm_handle)); |
193183fb MD |
1046 | if (!handle) |
1047 | return NULL; | |
1048 | ||
1049 | /* Allocate table for channel + per-cpu buffers */ | |
1050 | handle->table = shm_object_table_create(1 + num_possible_cpus()); | |
1051 | if (!handle->table) | |
1052 | goto error_table_alloc; | |
1053 | /* Add channel object */ | |
74d81a6c | 1054 | object = shm_object_table_append_mem(handle->table, data, |
ff0f5728 | 1055 | memory_map_size, wakeup_fd); |
193183fb MD |
1056 | if (!object) |
1057 | goto error_table_object; | |
57773204 MD |
1058 | /* struct channel is at object 0, offset 0 (hardcoded) */ |
1059 | handle->chan._ref.index = 0; | |
1060 | handle->chan._ref.offset = 0; | |
193183fb MD |
1061 | return handle; |
1062 | ||
1063 | error_table_object: | |
6548fca4 | 1064 | shm_object_table_destroy(handle->table, 0); |
193183fb MD |
1065 | error_table_alloc: |
1066 | free(handle); | |
1067 | return NULL; | |
1068 | } | |
1069 | ||
38fae1d3 | 1070 | int channel_handle_add_stream(struct lttng_ust_shm_handle *handle, |
74d81a6c MD |
1071 | int shm_fd, int wakeup_fd, uint32_t stream_nr, |
1072 | uint64_t memory_map_size) | |
193183fb MD |
1073 | { |
1074 | struct shm_object *object; | |
1075 | ||
1076 | /* Add stream object */ | |
74d81a6c MD |
1077 | object = shm_object_table_append_shm(handle->table, |
1078 | shm_fd, wakeup_fd, stream_nr, | |
1079 | memory_map_size); | |
193183fb | 1080 | if (!object) |
74d81a6c | 1081 | return -EINVAL; |
193183fb MD |
1082 | return 0; |
1083 | } | |
1084 | ||
74d81a6c MD |
1085 | unsigned int channel_handle_get_nr_streams(struct lttng_ust_shm_handle *handle) |
1086 | { | |
1087 | assert(handle->table); | |
1088 | return handle->table->allocated_len - 1; | |
1089 | } | |
1090 | ||
852c2936 | 1091 | static |
6548fca4 MD |
1092 | void channel_release(struct channel *chan, struct lttng_ust_shm_handle *handle, |
1093 | int consumer) | |
852c2936 | 1094 | { |
6548fca4 | 1095 | channel_free(chan, handle, consumer); |
852c2936 MD |
1096 | } |
1097 | ||
1098 | /** | |
1099 | * channel_destroy - Finalize, wait for q.s. and destroy channel. | |
1100 | * @chan: channel to destroy | |
1101 | * | |
1102 | * Holds cpu hotplug. | |
431d5cf0 MD |
1103 | * Call "destroy" callback, finalize channels, decrement the channel |
1104 | * reference count. Note that when readers have completed data | |
1105 | * consumption of finalized channels, get_subbuf() will return -ENODATA. | |
a3f61e7f | 1106 | * They should release their handle at that point. |
852c2936 | 1107 | */ |
a3f61e7f | 1108 | void channel_destroy(struct channel *chan, struct lttng_ust_shm_handle *handle, |
74d81a6c | 1109 | int consumer) |
852c2936 | 1110 | { |
74d81a6c MD |
1111 | if (consumer) { |
1112 | /* | |
1113 | * Note: the consumer takes care of finalizing and | |
1114 | * switching the buffers. | |
1115 | */ | |
1116 | channel_unregister_notifiers(chan, handle); | |
3d0ef9f6 MD |
1117 | /* |
1118 | * The consumer prints errors. | |
1119 | */ | |
1120 | channel_print_errors(chan, handle); | |
824f40b8 MD |
1121 | } |
1122 | ||
431d5cf0 MD |
1123 | /* |
1124 | * sessiond/consumer are keeping a reference on the shm file | |
1125 | * descriptor directly. No need to refcount. | |
1126 | */ | |
6548fca4 | 1127 | channel_release(chan, handle, consumer); |
a3f61e7f | 1128 | return; |
852c2936 | 1129 | } |
852c2936 | 1130 | |
4cfec15c MD |
1131 | struct lttng_ust_lib_ring_buffer *channel_get_ring_buffer( |
1132 | const struct lttng_ust_lib_ring_buffer_config *config, | |
1d498196 | 1133 | struct channel *chan, int cpu, |
38fae1d3 | 1134 | struct lttng_ust_shm_handle *handle, |
74d81a6c MD |
1135 | int *shm_fd, int *wait_fd, |
1136 | int *wakeup_fd, | |
1137 | uint64_t *memory_map_size) | |
852c2936 | 1138 | { |
381c0f1e MD |
1139 | struct shm_ref *ref; |
1140 | ||
1141 | if (config->alloc == RING_BUFFER_ALLOC_GLOBAL) { | |
74d81a6c | 1142 | cpu = 0; |
381c0f1e | 1143 | } else { |
e095d803 MD |
1144 | if (cpu >= num_possible_cpus()) |
1145 | return NULL; | |
381c0f1e | 1146 | } |
74d81a6c MD |
1147 | ref = &chan->backend.buf[cpu].shmp._ref; |
1148 | *shm_fd = shm_get_shm_fd(handle, ref); | |
1149 | *wait_fd = shm_get_wait_fd(handle, ref); | |
1150 | *wakeup_fd = shm_get_wakeup_fd(handle, ref); | |
1151 | if (shm_get_shm_size(handle, ref, memory_map_size)) | |
1152 | return NULL; | |
1153 | return shmp(handle, chan->backend.buf[cpu].shmp); | |
852c2936 | 1154 | } |
852c2936 | 1155 | |
ff0f5728 MD |
1156 | int ring_buffer_channel_close_wait_fd(const struct lttng_ust_lib_ring_buffer_config *config, |
1157 | struct channel *chan, | |
1158 | struct lttng_ust_shm_handle *handle) | |
1159 | { | |
1160 | struct shm_ref *ref; | |
1161 | ||
1162 | ref = &handle->chan._ref; | |
1163 | return shm_close_wait_fd(handle, ref); | |
1164 | } | |
1165 | ||
1166 | int ring_buffer_channel_close_wakeup_fd(const struct lttng_ust_lib_ring_buffer_config *config, | |
1167 | struct channel *chan, | |
1168 | struct lttng_ust_shm_handle *handle) | |
1169 | { | |
1170 | struct shm_ref *ref; | |
1171 | ||
1172 | ref = &handle->chan._ref; | |
1173 | return shm_close_wakeup_fd(handle, ref); | |
1174 | } | |
1175 | ||
1176 | int ring_buffer_stream_close_wait_fd(const struct lttng_ust_lib_ring_buffer_config *config, | |
74d81a6c MD |
1177 | struct channel *chan, |
1178 | struct lttng_ust_shm_handle *handle, | |
1179 | int cpu) | |
852c2936 | 1180 | { |
74d81a6c MD |
1181 | struct shm_ref *ref; |
1182 | ||
1183 | if (config->alloc == RING_BUFFER_ALLOC_GLOBAL) { | |
1184 | cpu = 0; | |
1185 | } else { | |
1186 | if (cpu >= num_possible_cpus()) | |
1187 | return -EINVAL; | |
824f40b8 | 1188 | } |
74d81a6c MD |
1189 | ref = &chan->backend.buf[cpu].shmp._ref; |
1190 | return shm_close_wait_fd(handle, ref); | |
1191 | } | |
1192 | ||
ff0f5728 | 1193 | int ring_buffer_stream_close_wakeup_fd(const struct lttng_ust_lib_ring_buffer_config *config, |
74d81a6c MD |
1194 | struct channel *chan, |
1195 | struct lttng_ust_shm_handle *handle, | |
1196 | int cpu) | |
1197 | { | |
1198 | struct shm_ref *ref; | |
cb7378b3 | 1199 | int ret; |
74d81a6c MD |
1200 | |
1201 | if (config->alloc == RING_BUFFER_ALLOC_GLOBAL) { | |
1202 | cpu = 0; | |
1203 | } else { | |
1204 | if (cpu >= num_possible_cpus()) | |
1205 | return -EINVAL; | |
1206 | } | |
1207 | ref = &chan->backend.buf[cpu].shmp._ref; | |
cb7378b3 MD |
1208 | pthread_mutex_lock(&wakeup_fd_mutex); |
1209 | ret = shm_close_wakeup_fd(handle, ref); | |
1210 | pthread_mutex_unlock(&wakeup_fd_mutex); | |
1211 | return ret; | |
74d81a6c MD |
1212 | } |
1213 | ||
1214 | int lib_ring_buffer_open_read(struct lttng_ust_lib_ring_buffer *buf, | |
1215 | struct lttng_ust_shm_handle *handle) | |
1216 | { | |
a6352fd4 | 1217 | if (uatomic_cmpxchg(&buf->active_readers, 0, 1) != 0) |
852c2936 | 1218 | return -EBUSY; |
a6352fd4 | 1219 | cmm_smp_mb(); |
852c2936 MD |
1220 | return 0; |
1221 | } | |
852c2936 | 1222 | |
4cfec15c | 1223 | void lib_ring_buffer_release_read(struct lttng_ust_lib_ring_buffer *buf, |
74d81a6c | 1224 | struct lttng_ust_shm_handle *handle) |
852c2936 | 1225 | { |
1d498196 | 1226 | struct channel *chan = shmp(handle, buf->backend.chan); |
852c2936 | 1227 | |
15500a1b MD |
1228 | if (!chan) |
1229 | return; | |
a6352fd4 MD |
1230 | CHAN_WARN_ON(chan, uatomic_read(&buf->active_readers) != 1); |
1231 | cmm_smp_mb(); | |
1232 | uatomic_dec(&buf->active_readers); | |
852c2936 MD |
1233 | } |
1234 | ||
1235 | /** | |
1236 | * lib_ring_buffer_snapshot - save subbuffer position snapshot (for read) | |
1237 | * @buf: ring buffer | |
1238 | * @consumed: consumed count indicating the position where to read | |
1239 | * @produced: produced count, indicates position when to stop reading | |
1240 | * | |
1241 | * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no | |
1242 | * data to read at consumed position, or 0 if the get operation succeeds. | |
852c2936 MD |
1243 | */ |
1244 | ||
4cfec15c | 1245 | int lib_ring_buffer_snapshot(struct lttng_ust_lib_ring_buffer *buf, |
1d498196 | 1246 | unsigned long *consumed, unsigned long *produced, |
38fae1d3 | 1247 | struct lttng_ust_shm_handle *handle) |
852c2936 | 1248 | { |
15500a1b MD |
1249 | struct channel *chan; |
1250 | const struct lttng_ust_lib_ring_buffer_config *config; | |
852c2936 MD |
1251 | unsigned long consumed_cur, write_offset; |
1252 | int finalized; | |
1253 | ||
15500a1b MD |
1254 | chan = shmp(handle, buf->backend.chan); |
1255 | if (!chan) | |
1256 | return -EPERM; | |
1257 | config = &chan->backend.config; | |
14641deb | 1258 | finalized = CMM_ACCESS_ONCE(buf->finalized); |
852c2936 MD |
1259 | /* |
1260 | * Read finalized before counters. | |
1261 | */ | |
a6352fd4 MD |
1262 | cmm_smp_rmb(); |
1263 | consumed_cur = uatomic_read(&buf->consumed); | |
852c2936 MD |
1264 | /* |
1265 | * No need to issue a memory barrier between consumed count read and | |
1266 | * write offset read, because consumed count can only change | |
1267 | * concurrently in overwrite mode, and we keep a sequence counter | |
1268 | * identifier derived from the write offset to check we are getting | |
1269 | * the same sub-buffer we are expecting (the sub-buffers are atomically | |
1270 | * "tagged" upon writes, tags are checked upon read). | |
1271 | */ | |
1272 | write_offset = v_read(config, &buf->offset); | |
1273 | ||
1274 | /* | |
1275 | * Check that we are not about to read the same subbuffer in | |
1276 | * which the writer head is. | |
1277 | */ | |
1278 | if (subbuf_trunc(write_offset, chan) - subbuf_trunc(consumed_cur, chan) | |
1279 | == 0) | |
1280 | goto nodata; | |
1281 | ||
1282 | *consumed = consumed_cur; | |
1283 | *produced = subbuf_trunc(write_offset, chan); | |
1284 | ||
1285 | return 0; | |
1286 | ||
1287 | nodata: | |
1288 | /* | |
1289 | * The memory barriers __wait_event()/wake_up_interruptible() take care | |
1290 | * of "raw_spin_is_locked" memory ordering. | |
1291 | */ | |
1292 | if (finalized) | |
1293 | return -ENODATA; | |
852c2936 MD |
1294 | else |
1295 | return -EAGAIN; | |
1296 | } | |
852c2936 MD |
1297 | |
1298 | /** | |
d1a996f5 | 1299 | * lib_ring_buffer_move_consumer - move consumed counter forward |
852c2936 MD |
1300 | * @buf: ring buffer |
1301 | * @consumed_new: new consumed count value | |
1302 | */ | |
4cfec15c | 1303 | void lib_ring_buffer_move_consumer(struct lttng_ust_lib_ring_buffer *buf, |
1d498196 | 1304 | unsigned long consumed_new, |
38fae1d3 | 1305 | struct lttng_ust_shm_handle *handle) |
852c2936 | 1306 | { |
4cfec15c | 1307 | struct lttng_ust_lib_ring_buffer_backend *bufb = &buf->backend; |
15500a1b | 1308 | struct channel *chan; |
852c2936 MD |
1309 | unsigned long consumed; |
1310 | ||
15500a1b MD |
1311 | chan = shmp(handle, bufb->chan); |
1312 | if (!chan) | |
1313 | return; | |
74d81a6c | 1314 | CHAN_WARN_ON(chan, uatomic_read(&buf->active_readers) != 1); |
852c2936 MD |
1315 | |
1316 | /* | |
1317 | * Only push the consumed value forward. | |
1318 | * If the consumed cmpxchg fails, this is because we have been pushed by | |
1319 | * the writer in flight recorder mode. | |
1320 | */ | |
a6352fd4 | 1321 | consumed = uatomic_read(&buf->consumed); |
852c2936 | 1322 | while ((long) consumed - (long) consumed_new < 0) |
a6352fd4 MD |
1323 | consumed = uatomic_cmpxchg(&buf->consumed, consumed, |
1324 | consumed_new); | |
852c2936 | 1325 | } |
852c2936 MD |
1326 | |
1327 | /** | |
1328 | * lib_ring_buffer_get_subbuf - get exclusive access to subbuffer for reading | |
1329 | * @buf: ring buffer | |
1330 | * @consumed: consumed count indicating the position where to read | |
1331 | * | |
1332 | * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no | |
1333 | * data to read at consumed position, or 0 if the get operation succeeds. | |
852c2936 | 1334 | */ |
4cfec15c | 1335 | int lib_ring_buffer_get_subbuf(struct lttng_ust_lib_ring_buffer *buf, |
1d498196 | 1336 | unsigned long consumed, |
38fae1d3 | 1337 | struct lttng_ust_shm_handle *handle) |
852c2936 | 1338 | { |
15500a1b MD |
1339 | struct channel *chan; |
1340 | const struct lttng_ust_lib_ring_buffer_config *config; | |
852c2936 | 1341 | unsigned long consumed_cur, consumed_idx, commit_count, write_offset; |
dddba398 | 1342 | int ret, finalized, nr_retry = LTTNG_UST_RING_BUFFER_GET_RETRY; |
15500a1b | 1343 | struct commit_counters_cold *cc_cold; |
852c2936 | 1344 | |
15500a1b MD |
1345 | chan = shmp(handle, buf->backend.chan); |
1346 | if (!chan) | |
1347 | return -EPERM; | |
1348 | config = &chan->backend.config; | |
852c2936 | 1349 | retry: |
14641deb | 1350 | finalized = CMM_ACCESS_ONCE(buf->finalized); |
852c2936 MD |
1351 | /* |
1352 | * Read finalized before counters. | |
1353 | */ | |
a6352fd4 MD |
1354 | cmm_smp_rmb(); |
1355 | consumed_cur = uatomic_read(&buf->consumed); | |
852c2936 | 1356 | consumed_idx = subbuf_index(consumed, chan); |
15500a1b MD |
1357 | cc_cold = shmp_index(handle, buf->commit_cold, consumed_idx); |
1358 | if (!cc_cold) | |
1359 | return -EPERM; | |
1360 | commit_count = v_read(config, &cc_cold->cc_sb); | |
852c2936 MD |
1361 | /* |
1362 | * Make sure we read the commit count before reading the buffer | |
1363 | * data and the write offset. Correct consumed offset ordering | |
1364 | * wrt commit count is insured by the use of cmpxchg to update | |
1365 | * the consumed offset. | |
852c2936 | 1366 | */ |
a6352fd4 MD |
1367 | /* |
1368 | * Local rmb to match the remote wmb to read the commit count | |
1369 | * before the buffer data and the write offset. | |
1370 | */ | |
1371 | cmm_smp_rmb(); | |
852c2936 MD |
1372 | |
1373 | write_offset = v_read(config, &buf->offset); | |
1374 | ||
1375 | /* | |
1376 | * Check that the buffer we are getting is after or at consumed_cur | |
1377 | * position. | |
1378 | */ | |
1379 | if ((long) subbuf_trunc(consumed, chan) | |
1380 | - (long) subbuf_trunc(consumed_cur, chan) < 0) | |
1381 | goto nodata; | |
1382 | ||
1383 | /* | |
1384 | * Check that the subbuffer we are trying to consume has been | |
dddba398 MD |
1385 | * already fully committed. There are a few causes that can make |
1386 | * this unavailability situation occur: | |
1387 | * | |
1388 | * Temporary (short-term) situation: | |
1389 | * - Application is running on a different CPU, between reserve | |
1390 | * and commit ring buffer operations, | |
1391 | * - Application is preempted between reserve and commit ring | |
1392 | * buffer operations, | |
1393 | * | |
1394 | * Long-term situation: | |
1395 | * - Application is stopped (SIGSTOP) between reserve and commit | |
1396 | * ring buffer operations. Could eventually be resumed by | |
1397 | * SIGCONT. | |
1398 | * - Application is killed (SIGTERM, SIGINT, SIGKILL) between | |
1399 | * reserve and commit ring buffer operation. | |
1400 | * | |
1401 | * From a consumer perspective, handling short-term | |
1402 | * unavailability situations is performed by retrying a few | |
1403 | * times after a delay. Handling long-term unavailability | |
1404 | * situations is handled by failing to get the sub-buffer. | |
1405 | * | |
1406 | * In all of those situations, if the application is taking a | |
1407 | * long time to perform its commit after ring buffer space | |
1408 | * reservation, we can end up in a situation where the producer | |
1409 | * will fill the ring buffer and try to write into the same | |
1410 | * sub-buffer again (which has a missing commit). This is | |
1411 | * handled by the producer in the sub-buffer switch handling | |
1412 | * code of the reserve routine by detecting unbalanced | |
1413 | * reserve/commit counters and discarding all further events | |
1414 | * until the situation is resolved in those situations. Two | |
1415 | * scenarios can occur: | |
1416 | * | |
1417 | * 1) The application causing the reserve/commit counters to be | |
1418 | * unbalanced has been terminated. In this situation, all | |
1419 | * further events will be discarded in the buffers, and no | |
1420 | * further buffer data will be readable by the consumer | |
1421 | * daemon. Tearing down the UST tracing session and starting | |
1422 | * anew is a work-around for those situations. Note that this | |
1423 | * only affects per-UID tracing. In per-PID tracing, the | |
1424 | * application vanishes with the termination, and therefore | |
1425 | * no more data needs to be written to the buffers. | |
1426 | * 2) The application causing the unbalance has been delayed for | |
1427 | * a long time, but will eventually try to increment the | |
1428 | * commit counter after eventually writing to the sub-buffer. | |
1429 | * This situation can cause events to be discarded until the | |
1430 | * application resumes its operations. | |
852c2936 MD |
1431 | */ |
1432 | if (((commit_count - chan->backend.subbuf_size) | |
1433 | & chan->commit_count_mask) | |
0bade047 | 1434 | - (buf_trunc(consumed, chan) |
852c2936 | 1435 | >> chan->backend.num_subbuf_order) |
dddba398 MD |
1436 | != 0) { |
1437 | if (nr_retry-- > 0) { | |
1438 | if (nr_retry <= (LTTNG_UST_RING_BUFFER_GET_RETRY >> 1)) | |
1439 | (void) poll(NULL, 0, LTTNG_UST_RING_BUFFER_RETRY_DELAY_MS); | |
1440 | goto retry; | |
1441 | } else { | |
1442 | goto nodata; | |
1443 | } | |
1444 | } | |
852c2936 MD |
1445 | |
1446 | /* | |
1447 | * Check that we are not about to read the same subbuffer in | |
1448 | * which the writer head is. | |
1449 | */ | |
0bade047 | 1450 | if (subbuf_trunc(write_offset, chan) - subbuf_trunc(consumed, chan) |
852c2936 MD |
1451 | == 0) |
1452 | goto nodata; | |
1453 | ||
1454 | /* | |
1455 | * Failure to get the subbuffer causes a busy-loop retry without going | |
1456 | * to a wait queue. These are caused by short-lived race windows where | |
1457 | * the writer is getting access to a subbuffer we were trying to get | |
1458 | * access to. Also checks that the "consumed" buffer count we are | |
1459 | * looking for matches the one contained in the subbuffer id. | |
dddba398 MD |
1460 | * |
1461 | * The short-lived race window described here can be affected by | |
1462 | * application signals and preemption, thus requiring to bound | |
1463 | * the loop to a maximum number of retry. | |
852c2936 MD |
1464 | */ |
1465 | ret = update_read_sb_index(config, &buf->backend, &chan->backend, | |
1d498196 MD |
1466 | consumed_idx, buf_trunc_val(consumed, chan), |
1467 | handle); | |
dddba398 MD |
1468 | if (ret) { |
1469 | if (nr_retry-- > 0) { | |
1470 | if (nr_retry <= (LTTNG_UST_RING_BUFFER_GET_RETRY >> 1)) | |
1471 | (void) poll(NULL, 0, LTTNG_UST_RING_BUFFER_RETRY_DELAY_MS); | |
1472 | goto retry; | |
1473 | } else { | |
1474 | goto nodata; | |
1475 | } | |
1476 | } | |
852c2936 MD |
1477 | subbuffer_id_clear_noref(config, &buf->backend.buf_rsb.id); |
1478 | ||
1479 | buf->get_subbuf_consumed = consumed; | |
1480 | buf->get_subbuf = 1; | |
1481 | ||
1482 | return 0; | |
1483 | ||
1484 | nodata: | |
1485 | /* | |
1486 | * The memory barriers __wait_event()/wake_up_interruptible() take care | |
1487 | * of "raw_spin_is_locked" memory ordering. | |
1488 | */ | |
1489 | if (finalized) | |
1490 | return -ENODATA; | |
852c2936 MD |
1491 | else |
1492 | return -EAGAIN; | |
1493 | } | |
852c2936 MD |
1494 | |
1495 | /** | |
1496 | * lib_ring_buffer_put_subbuf - release exclusive subbuffer access | |
1497 | * @buf: ring buffer | |
1498 | */ | |
4cfec15c | 1499 | void lib_ring_buffer_put_subbuf(struct lttng_ust_lib_ring_buffer *buf, |
38fae1d3 | 1500 | struct lttng_ust_shm_handle *handle) |
852c2936 | 1501 | { |
4cfec15c | 1502 | struct lttng_ust_lib_ring_buffer_backend *bufb = &buf->backend; |
15500a1b MD |
1503 | struct channel *chan; |
1504 | const struct lttng_ust_lib_ring_buffer_config *config; | |
1505 | unsigned long sb_bindex, consumed_idx, consumed; | |
1506 | struct lttng_ust_lib_ring_buffer_backend_pages_shmp *rpages; | |
1507 | struct lttng_ust_lib_ring_buffer_backend_pages *backend_pages; | |
852c2936 | 1508 | |
15500a1b MD |
1509 | chan = shmp(handle, bufb->chan); |
1510 | if (!chan) | |
1511 | return; | |
1512 | config = &chan->backend.config; | |
74d81a6c | 1513 | CHAN_WARN_ON(chan, uatomic_read(&buf->active_readers) != 1); |
852c2936 MD |
1514 | |
1515 | if (!buf->get_subbuf) { | |
1516 | /* | |
1517 | * Reader puts a subbuffer it did not get. | |
1518 | */ | |
1519 | CHAN_WARN_ON(chan, 1); | |
1520 | return; | |
1521 | } | |
1522 | consumed = buf->get_subbuf_consumed; | |
1523 | buf->get_subbuf = 0; | |
1524 | ||
1525 | /* | |
1526 | * Clear the records_unread counter. (overruns counter) | |
1527 | * Can still be non-zero if a file reader simply grabbed the data | |
1528 | * without using iterators. | |
1529 | * Can be below zero if an iterator is used on a snapshot more than | |
1530 | * once. | |
1531 | */ | |
15500a1b MD |
1532 | sb_bindex = subbuffer_id_get_index(config, bufb->buf_rsb.id); |
1533 | rpages = shmp_index(handle, bufb->array, sb_bindex); | |
1534 | if (!rpages) | |
1535 | return; | |
1536 | backend_pages = shmp(handle, rpages->shmp); | |
1537 | if (!backend_pages) | |
1538 | return; | |
1539 | v_add(config, v_read(config, &backend_pages->records_unread), | |
1540 | &bufb->records_read); | |
1541 | v_set(config, &backend_pages->records_unread, 0); | |
852c2936 MD |
1542 | CHAN_WARN_ON(chan, config->mode == RING_BUFFER_OVERWRITE |
1543 | && subbuffer_id_is_noref(config, bufb->buf_rsb.id)); | |
1544 | subbuffer_id_set_noref(config, &bufb->buf_rsb.id); | |
1545 | ||
1546 | /* | |
1547 | * Exchange the reader subbuffer with the one we put in its place in the | |
1548 | * writer subbuffer table. Expect the original consumed count. If | |
1549 | * update_read_sb_index fails, this is because the writer updated the | |
1550 | * subbuffer concurrently. We should therefore keep the subbuffer we | |
1551 | * currently have: it has become invalid to try reading this sub-buffer | |
1552 | * consumed count value anyway. | |
1553 | */ | |
1554 | consumed_idx = subbuf_index(consumed, chan); | |
1555 | update_read_sb_index(config, &buf->backend, &chan->backend, | |
1d498196 MD |
1556 | consumed_idx, buf_trunc_val(consumed, chan), |
1557 | handle); | |
852c2936 MD |
1558 | /* |
1559 | * update_read_sb_index return value ignored. Don't exchange sub-buffer | |
1560 | * if the writer concurrently updated it. | |
1561 | */ | |
1562 | } | |
852c2936 MD |
1563 | |
1564 | /* | |
1565 | * cons_offset is an iterator on all subbuffer offsets between the reader | |
1566 | * position and the writer position. (inclusive) | |
1567 | */ | |
1568 | static | |
4cfec15c | 1569 | void lib_ring_buffer_print_subbuffer_errors(struct lttng_ust_lib_ring_buffer *buf, |
852c2936 MD |
1570 | struct channel *chan, |
1571 | unsigned long cons_offset, | |
1d498196 | 1572 | int cpu, |
38fae1d3 | 1573 | struct lttng_ust_shm_handle *handle) |
852c2936 | 1574 | { |
4cfec15c | 1575 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 | 1576 | unsigned long cons_idx, commit_count, commit_count_sb; |
15500a1b MD |
1577 | struct commit_counters_hot *cc_hot; |
1578 | struct commit_counters_cold *cc_cold; | |
852c2936 MD |
1579 | |
1580 | cons_idx = subbuf_index(cons_offset, chan); | |
15500a1b MD |
1581 | cc_hot = shmp_index(handle, buf->commit_hot, cons_idx); |
1582 | if (!cc_hot) | |
1583 | return; | |
1584 | cc_cold = shmp_index(handle, buf->commit_cold, cons_idx); | |
1585 | if (!cc_cold) | |
1586 | return; | |
1587 | commit_count = v_read(config, &cc_hot->cc); | |
1588 | commit_count_sb = v_read(config, &cc_cold->cc_sb); | |
852c2936 MD |
1589 | |
1590 | if (subbuf_offset(commit_count, chan) != 0) | |
4d3c9523 | 1591 | DBG("ring buffer %s, cpu %d: " |
852c2936 MD |
1592 | "commit count in subbuffer %lu,\n" |
1593 | "expecting multiples of %lu bytes\n" | |
1594 | " [ %lu bytes committed, %lu bytes reader-visible ]\n", | |
1595 | chan->backend.name, cpu, cons_idx, | |
1596 | chan->backend.subbuf_size, | |
1597 | commit_count, commit_count_sb); | |
1598 | ||
4d3c9523 | 1599 | DBG("ring buffer: %s, cpu %d: %lu bytes committed\n", |
852c2936 MD |
1600 | chan->backend.name, cpu, commit_count); |
1601 | } | |
1602 | ||
1603 | static | |
4cfec15c | 1604 | void lib_ring_buffer_print_buffer_errors(struct lttng_ust_lib_ring_buffer *buf, |
852c2936 | 1605 | struct channel *chan, |
1d498196 | 1606 | void *priv, int cpu, |
38fae1d3 | 1607 | struct lttng_ust_shm_handle *handle) |
852c2936 | 1608 | { |
4cfec15c | 1609 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
1610 | unsigned long write_offset, cons_offset; |
1611 | ||
852c2936 MD |
1612 | /* |
1613 | * No need to order commit_count, write_offset and cons_offset reads | |
1614 | * because we execute at teardown when no more writer nor reader | |
1615 | * references are left. | |
1616 | */ | |
1617 | write_offset = v_read(config, &buf->offset); | |
a6352fd4 | 1618 | cons_offset = uatomic_read(&buf->consumed); |
852c2936 | 1619 | if (write_offset != cons_offset) |
4d3c9523 | 1620 | DBG("ring buffer %s, cpu %d: " |
852c2936 MD |
1621 | "non-consumed data\n" |
1622 | " [ %lu bytes written, %lu bytes read ]\n", | |
1623 | chan->backend.name, cpu, write_offset, cons_offset); | |
1624 | ||
a6352fd4 | 1625 | for (cons_offset = uatomic_read(&buf->consumed); |
852c2936 MD |
1626 | (long) (subbuf_trunc((unsigned long) v_read(config, &buf->offset), |
1627 | chan) | |
1628 | - cons_offset) > 0; | |
1629 | cons_offset = subbuf_align(cons_offset, chan)) | |
1630 | lib_ring_buffer_print_subbuffer_errors(buf, chan, cons_offset, | |
1d498196 | 1631 | cpu, handle); |
852c2936 MD |
1632 | } |
1633 | ||
1634 | static | |
1635 | void lib_ring_buffer_print_errors(struct channel *chan, | |
009769ca MD |
1636 | struct lttng_ust_lib_ring_buffer *buf, int cpu, |
1637 | struct lttng_ust_shm_handle *handle) | |
852c2936 | 1638 | { |
4cfec15c | 1639 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
a3f61e7f | 1640 | void *priv = channel_get_private(chan); |
852c2936 | 1641 | |
a1360615 MD |
1642 | if (!strcmp(chan->backend.name, "relay-metadata-mmap")) { |
1643 | DBG("ring buffer %s: %lu records written, " | |
1644 | "%lu records overrun\n", | |
1645 | chan->backend.name, | |
1646 | v_read(config, &buf->records_count), | |
1647 | v_read(config, &buf->records_overrun)); | |
1648 | } else { | |
1649 | DBG("ring buffer %s, cpu %d: %lu records written, " | |
1650 | "%lu records overrun\n", | |
1651 | chan->backend.name, cpu, | |
1652 | v_read(config, &buf->records_count), | |
1653 | v_read(config, &buf->records_overrun)); | |
1654 | ||
1655 | if (v_read(config, &buf->records_lost_full) | |
1656 | || v_read(config, &buf->records_lost_wrap) | |
1657 | || v_read(config, &buf->records_lost_big)) | |
1658 | DBG("ring buffer %s, cpu %d: records were lost. Caused by:\n" | |
1659 | " [ %lu buffer full, %lu nest buffer wrap-around, " | |
1660 | "%lu event too big ]\n", | |
1661 | chan->backend.name, cpu, | |
1662 | v_read(config, &buf->records_lost_full), | |
1663 | v_read(config, &buf->records_lost_wrap), | |
1664 | v_read(config, &buf->records_lost_big)); | |
1665 | } | |
1d498196 | 1666 | lib_ring_buffer_print_buffer_errors(buf, chan, priv, cpu, handle); |
852c2936 MD |
1667 | } |
1668 | ||
1669 | /* | |
1670 | * lib_ring_buffer_switch_old_start: Populate old subbuffer header. | |
1671 | * | |
48f22436 MD |
1672 | * Only executed by SWITCH_FLUSH, which can be issued while tracing is |
1673 | * active or at buffer finalization (destroy). | |
852c2936 MD |
1674 | */ |
1675 | static | |
4cfec15c | 1676 | void lib_ring_buffer_switch_old_start(struct lttng_ust_lib_ring_buffer *buf, |
852c2936 MD |
1677 | struct channel *chan, |
1678 | struct switch_offsets *offsets, | |
2fed87ae | 1679 | uint64_t tsc, |
38fae1d3 | 1680 | struct lttng_ust_shm_handle *handle) |
852c2936 | 1681 | { |
4cfec15c | 1682 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
1683 | unsigned long oldidx = subbuf_index(offsets->old, chan); |
1684 | unsigned long commit_count; | |
3d3c3833 | 1685 | struct commit_counters_hot *cc_hot; |
852c2936 | 1686 | |
1d498196 | 1687 | config->cb.buffer_begin(buf, tsc, oldidx, handle); |
852c2936 MD |
1688 | |
1689 | /* | |
1690 | * Order all writes to buffer before the commit count update that will | |
1691 | * determine that the subbuffer is full. | |
1692 | */ | |
a6352fd4 | 1693 | cmm_smp_wmb(); |
3d3c3833 | 1694 | cc_hot = shmp_index(handle, buf->commit_hot, oldidx); |
15500a1b MD |
1695 | if (!cc_hot) |
1696 | return; | |
852c2936 | 1697 | v_add(config, config->cb.subbuffer_header_size(), |
3d3c3833 MD |
1698 | &cc_hot->cc); |
1699 | commit_count = v_read(config, &cc_hot->cc); | |
852c2936 MD |
1700 | /* Check if the written buffer has to be delivered */ |
1701 | lib_ring_buffer_check_deliver(config, buf, chan, offsets->old, | |
1b7b0501 | 1702 | commit_count, oldidx, handle, tsc); |
d2fe4771 | 1703 | lib_ring_buffer_write_commit_counter(config, buf, chan, |
80249235 | 1704 | offsets->old + config->cb.subbuffer_header_size(), |
3d3c3833 | 1705 | commit_count, handle, cc_hot); |
852c2936 MD |
1706 | } |
1707 | ||
1708 | /* | |
1709 | * lib_ring_buffer_switch_old_end: switch old subbuffer | |
1710 | * | |
1711 | * Note : offset_old should never be 0 here. It is ok, because we never perform | |
1712 | * buffer switch on an empty subbuffer in SWITCH_ACTIVE mode. The caller | |
1713 | * increments the offset_old value when doing a SWITCH_FLUSH on an empty | |
1714 | * subbuffer. | |
1715 | */ | |
1716 | static | |
4cfec15c | 1717 | void lib_ring_buffer_switch_old_end(struct lttng_ust_lib_ring_buffer *buf, |
852c2936 MD |
1718 | struct channel *chan, |
1719 | struct switch_offsets *offsets, | |
2fed87ae | 1720 | uint64_t tsc, |
38fae1d3 | 1721 | struct lttng_ust_shm_handle *handle) |
852c2936 | 1722 | { |
4cfec15c | 1723 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
1724 | unsigned long oldidx = subbuf_index(offsets->old - 1, chan); |
1725 | unsigned long commit_count, padding_size, data_size; | |
3d3c3833 | 1726 | struct commit_counters_hot *cc_hot; |
852c2936 MD |
1727 | |
1728 | data_size = subbuf_offset(offsets->old - 1, chan) + 1; | |
1729 | padding_size = chan->backend.subbuf_size - data_size; | |
1d498196 MD |
1730 | subbuffer_set_data_size(config, &buf->backend, oldidx, data_size, |
1731 | handle); | |
852c2936 MD |
1732 | |
1733 | /* | |
1734 | * Order all writes to buffer before the commit count update that will | |
1735 | * determine that the subbuffer is full. | |
1736 | */ | |
a6352fd4 | 1737 | cmm_smp_wmb(); |
3d3c3833 | 1738 | cc_hot = shmp_index(handle, buf->commit_hot, oldidx); |
15500a1b MD |
1739 | if (!cc_hot) |
1740 | return; | |
3d3c3833 MD |
1741 | v_add(config, padding_size, &cc_hot->cc); |
1742 | commit_count = v_read(config, &cc_hot->cc); | |
852c2936 | 1743 | lib_ring_buffer_check_deliver(config, buf, chan, offsets->old - 1, |
1b7b0501 | 1744 | commit_count, oldidx, handle, tsc); |
d2fe4771 MD |
1745 | lib_ring_buffer_write_commit_counter(config, buf, chan, |
1746 | offsets->old + padding_size, commit_count, handle, | |
3d3c3833 | 1747 | cc_hot); |
852c2936 MD |
1748 | } |
1749 | ||
1750 | /* | |
1751 | * lib_ring_buffer_switch_new_start: Populate new subbuffer. | |
1752 | * | |
1753 | * This code can be executed unordered : writers may already have written to the | |
1754 | * sub-buffer before this code gets executed, caution. The commit makes sure | |
1755 | * that this code is executed before the deliver of this sub-buffer. | |
1756 | */ | |
1757 | static | |
4cfec15c | 1758 | void lib_ring_buffer_switch_new_start(struct lttng_ust_lib_ring_buffer *buf, |
852c2936 MD |
1759 | struct channel *chan, |
1760 | struct switch_offsets *offsets, | |
2fed87ae | 1761 | uint64_t tsc, |
38fae1d3 | 1762 | struct lttng_ust_shm_handle *handle) |
852c2936 | 1763 | { |
4cfec15c | 1764 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
1765 | unsigned long beginidx = subbuf_index(offsets->begin, chan); |
1766 | unsigned long commit_count; | |
3d3c3833 | 1767 | struct commit_counters_hot *cc_hot; |
852c2936 | 1768 | |
1d498196 | 1769 | config->cb.buffer_begin(buf, tsc, beginidx, handle); |
852c2936 MD |
1770 | |
1771 | /* | |
1772 | * Order all writes to buffer before the commit count update that will | |
1773 | * determine that the subbuffer is full. | |
1774 | */ | |
a6352fd4 | 1775 | cmm_smp_wmb(); |
3d3c3833 | 1776 | cc_hot = shmp_index(handle, buf->commit_hot, beginidx); |
15500a1b MD |
1777 | if (!cc_hot) |
1778 | return; | |
1779 | v_add(config, config->cb.subbuffer_header_size(), &cc_hot->cc); | |
3d3c3833 | 1780 | commit_count = v_read(config, &cc_hot->cc); |
852c2936 MD |
1781 | /* Check if the written buffer has to be delivered */ |
1782 | lib_ring_buffer_check_deliver(config, buf, chan, offsets->begin, | |
1b7b0501 | 1783 | commit_count, beginidx, handle, tsc); |
d2fe4771 | 1784 | lib_ring_buffer_write_commit_counter(config, buf, chan, |
80249235 | 1785 | offsets->begin + config->cb.subbuffer_header_size(), |
3d3c3833 | 1786 | commit_count, handle, cc_hot); |
852c2936 MD |
1787 | } |
1788 | ||
1ad21f70 MD |
1789 | /* |
1790 | * lib_ring_buffer_switch_new_end: finish switching current subbuffer | |
1791 | * | |
3962118d MD |
1792 | * Calls subbuffer_set_data_size() to set the data size of the current |
1793 | * sub-buffer. We do not need to perform check_deliver nor commit here, | |
1794 | * since this task will be done by the "commit" of the event for which | |
1795 | * we are currently doing the space reservation. | |
1ad21f70 MD |
1796 | */ |
1797 | static | |
1798 | void lib_ring_buffer_switch_new_end(struct lttng_ust_lib_ring_buffer *buf, | |
1799 | struct channel *chan, | |
1800 | struct switch_offsets *offsets, | |
1801 | uint64_t tsc, | |
1802 | struct lttng_ust_shm_handle *handle) | |
1803 | { | |
1804 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; | |
3962118d | 1805 | unsigned long endidx, data_size; |
1ad21f70 | 1806 | |
3962118d | 1807 | endidx = subbuf_index(offsets->end - 1, chan); |
1ad21f70 | 1808 | data_size = subbuf_offset(offsets->end - 1, chan) + 1; |
1ad21f70 MD |
1809 | subbuffer_set_data_size(config, &buf->backend, endidx, data_size, |
1810 | handle); | |
1ad21f70 MD |
1811 | } |
1812 | ||
852c2936 MD |
1813 | /* |
1814 | * Returns : | |
1815 | * 0 if ok | |
1816 | * !0 if execution must be aborted. | |
1817 | */ | |
1818 | static | |
1819 | int lib_ring_buffer_try_switch_slow(enum switch_mode mode, | |
4cfec15c | 1820 | struct lttng_ust_lib_ring_buffer *buf, |
852c2936 MD |
1821 | struct channel *chan, |
1822 | struct switch_offsets *offsets, | |
2dac206d MD |
1823 | uint64_t *tsc, |
1824 | struct lttng_ust_shm_handle *handle) | |
852c2936 | 1825 | { |
4cfec15c | 1826 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
2dac206d | 1827 | unsigned long off, reserve_commit_diff; |
852c2936 MD |
1828 | |
1829 | offsets->begin = v_read(config, &buf->offset); | |
1830 | offsets->old = offsets->begin; | |
1831 | offsets->switch_old_start = 0; | |
1832 | off = subbuf_offset(offsets->begin, chan); | |
1833 | ||
1834 | *tsc = config->cb.ring_buffer_clock_read(chan); | |
1835 | ||
1836 | /* | |
1837 | * Ensure we flush the header of an empty subbuffer when doing the | |
1838 | * finalize (SWITCH_FLUSH). This ensures that we end up knowing the | |
1839 | * total data gathering duration even if there were no records saved | |
1840 | * after the last buffer switch. | |
1841 | * In SWITCH_ACTIVE mode, switch the buffer when it contains events. | |
1842 | * SWITCH_ACTIVE only flushes the current subbuffer, dealing with end of | |
1843 | * subbuffer header as appropriate. | |
1844 | * The next record that reserves space will be responsible for | |
1845 | * populating the following subbuffer header. We choose not to populate | |
1846 | * the next subbuffer header here because we want to be able to use | |
a6352fd4 MD |
1847 | * SWITCH_ACTIVE for periodical buffer flush, which must |
1848 | * guarantee that all the buffer content (records and header | |
1849 | * timestamps) are visible to the reader. This is required for | |
1850 | * quiescence guarantees for the fusion merge. | |
852c2936 | 1851 | */ |
2dac206d MD |
1852 | if (mode != SWITCH_FLUSH && !off) |
1853 | return -1; /* we do not have to switch : buffer is empty */ | |
1854 | ||
1855 | if (caa_unlikely(off == 0)) { | |
1856 | unsigned long sb_index, commit_count; | |
15500a1b | 1857 | struct commit_counters_cold *cc_cold; |
2dac206d MD |
1858 | |
1859 | /* | |
48f22436 MD |
1860 | * We are performing a SWITCH_FLUSH. There may be concurrent |
1861 | * writes into the buffer if e.g. invoked while performing a | |
1862 | * snapshot on an active trace. | |
2dac206d | 1863 | * |
48f22436 MD |
1864 | * If the client does not save any header information |
1865 | * (sub-buffer header size == 0), don't switch empty subbuffer | |
1866 | * on finalize, because it is invalid to deliver a completely | |
1867 | * empty subbuffer. | |
2dac206d MD |
1868 | */ |
1869 | if (!config->cb.subbuffer_header_size()) | |
1870 | return -1; | |
1871 | ||
1872 | /* Test new buffer integrity */ | |
1873 | sb_index = subbuf_index(offsets->begin, chan); | |
15500a1b MD |
1874 | cc_cold = shmp_index(handle, buf->commit_cold, sb_index); |
1875 | if (!cc_cold) | |
1876 | return -1; | |
1877 | commit_count = v_read(config, &cc_cold->cc_sb); | |
2dac206d MD |
1878 | reserve_commit_diff = |
1879 | (buf_trunc(offsets->begin, chan) | |
1880 | >> chan->backend.num_subbuf_order) | |
1881 | - (commit_count & chan->commit_count_mask); | |
1882 | if (caa_likely(reserve_commit_diff == 0)) { | |
1883 | /* Next subbuffer not being written to. */ | |
1884 | if (caa_unlikely(config->mode != RING_BUFFER_OVERWRITE && | |
1885 | subbuf_trunc(offsets->begin, chan) | |
1886 | - subbuf_trunc((unsigned long) | |
1887 | uatomic_read(&buf->consumed), chan) | |
1888 | >= chan->backend.buf_size)) { | |
1889 | /* | |
1890 | * We do not overwrite non consumed buffers | |
1891 | * and we are full : don't switch. | |
1892 | */ | |
852c2936 | 1893 | return -1; |
2dac206d MD |
1894 | } else { |
1895 | /* | |
1896 | * Next subbuffer not being written to, and we | |
1897 | * are either in overwrite mode or the buffer is | |
1898 | * not full. It's safe to write in this new | |
1899 | * subbuffer. | |
1900 | */ | |
1901 | } | |
1902 | } else { | |
852c2936 | 1903 | /* |
2dac206d MD |
1904 | * Next subbuffer reserve offset does not match the |
1905 | * commit offset. Don't perform switch in | |
1906 | * producer-consumer and overwrite mode. Caused by | |
1907 | * either a writer OOPS or too many nested writes over a | |
1908 | * reserve/commit pair. | |
852c2936 | 1909 | */ |
2dac206d | 1910 | return -1; |
852c2936 | 1911 | } |
2dac206d MD |
1912 | |
1913 | /* | |
1914 | * Need to write the subbuffer start header on finalize. | |
1915 | */ | |
1916 | offsets->switch_old_start = 1; | |
1917 | } | |
1918 | offsets->begin = subbuf_align(offsets->begin, chan); | |
852c2936 MD |
1919 | /* Note: old points to the next subbuf at offset 0 */ |
1920 | offsets->end = offsets->begin; | |
1921 | return 0; | |
1922 | } | |
1923 | ||
1924 | /* | |
1925 | * Force a sub-buffer switch. This operation is completely reentrant : can be | |
1926 | * called while tracing is active with absolutely no lock held. | |
1927 | * | |
1928 | * Note, however, that as a v_cmpxchg is used for some atomic | |
1929 | * operations, this function must be called from the CPU which owns the buffer | |
1930 | * for a ACTIVE flush. | |
1931 | */ | |
4cfec15c | 1932 | void lib_ring_buffer_switch_slow(struct lttng_ust_lib_ring_buffer *buf, enum switch_mode mode, |
38fae1d3 | 1933 | struct lttng_ust_shm_handle *handle) |
852c2936 | 1934 | { |
15500a1b MD |
1935 | struct channel *chan; |
1936 | const struct lttng_ust_lib_ring_buffer_config *config; | |
852c2936 MD |
1937 | struct switch_offsets offsets; |
1938 | unsigned long oldidx; | |
2fed87ae | 1939 | uint64_t tsc; |
852c2936 | 1940 | |
15500a1b MD |
1941 | chan = shmp(handle, buf->backend.chan); |
1942 | if (!chan) | |
1943 | return; | |
1944 | config = &chan->backend.config; | |
1945 | ||
852c2936 MD |
1946 | offsets.size = 0; |
1947 | ||
1948 | /* | |
1949 | * Perform retryable operations. | |
1950 | */ | |
1951 | do { | |
1952 | if (lib_ring_buffer_try_switch_slow(mode, buf, chan, &offsets, | |
2dac206d | 1953 | &tsc, handle)) |
852c2936 MD |
1954 | return; /* Switch not needed */ |
1955 | } while (v_cmpxchg(config, &buf->offset, offsets.old, offsets.end) | |
1956 | != offsets.old); | |
1957 | ||
1958 | /* | |
1959 | * Atomically update last_tsc. This update races against concurrent | |
1960 | * atomic updates, but the race will always cause supplementary full TSC | |
1961 | * records, never the opposite (missing a full TSC record when it would | |
1962 | * be needed). | |
1963 | */ | |
1964 | save_last_tsc(config, buf, tsc); | |
1965 | ||
1966 | /* | |
1967 | * Push the reader if necessary | |
1968 | */ | |
1969 | lib_ring_buffer_reserve_push_reader(buf, chan, offsets.old); | |
1970 | ||
1971 | oldidx = subbuf_index(offsets.old, chan); | |
1d498196 | 1972 | lib_ring_buffer_clear_noref(config, &buf->backend, oldidx, handle); |
852c2936 MD |
1973 | |
1974 | /* | |
1975 | * May need to populate header start on SWITCH_FLUSH. | |
1976 | */ | |
1977 | if (offsets.switch_old_start) { | |
1d498196 | 1978 | lib_ring_buffer_switch_old_start(buf, chan, &offsets, tsc, handle); |
852c2936 MD |
1979 | offsets.old += config->cb.subbuffer_header_size(); |
1980 | } | |
1981 | ||
1982 | /* | |
1983 | * Switch old subbuffer. | |
1984 | */ | |
1d498196 | 1985 | lib_ring_buffer_switch_old_end(buf, chan, &offsets, tsc, handle); |
852c2936 | 1986 | } |
852c2936 MD |
1987 | |
1988 | /* | |
1989 | * Returns : | |
1990 | * 0 if ok | |
1991 | * -ENOSPC if event size is too large for packet. | |
1992 | * -ENOBUFS if there is currently not enough space in buffer for the event. | |
1993 | * -EIO if data cannot be written into the buffer for any other reason. | |
1994 | */ | |
1995 | static | |
4cfec15c | 1996 | int lib_ring_buffer_try_reserve_slow(struct lttng_ust_lib_ring_buffer *buf, |
852c2936 MD |
1997 | struct channel *chan, |
1998 | struct switch_offsets *offsets, | |
4cfec15c | 1999 | struct lttng_ust_lib_ring_buffer_ctx *ctx) |
852c2936 | 2000 | { |
4cfec15c | 2001 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
38fae1d3 | 2002 | struct lttng_ust_shm_handle *handle = ctx->handle; |
e185cf4b | 2003 | unsigned long reserve_commit_diff, offset_cmp; |
852c2936 | 2004 | |
e185cf4b MD |
2005 | retry: |
2006 | offsets->begin = offset_cmp = v_read(config, &buf->offset); | |
852c2936 MD |
2007 | offsets->old = offsets->begin; |
2008 | offsets->switch_new_start = 0; | |
1ad21f70 | 2009 | offsets->switch_new_end = 0; |
852c2936 MD |
2010 | offsets->switch_old_end = 0; |
2011 | offsets->pre_header_padding = 0; | |
2012 | ||
2013 | ctx->tsc = config->cb.ring_buffer_clock_read(chan); | |
2014 | if ((int64_t) ctx->tsc == -EIO) | |
2015 | return -EIO; | |
2016 | ||
2017 | if (last_tsc_overflow(config, buf, ctx->tsc)) | |
2018 | ctx->rflags |= RING_BUFFER_RFLAG_FULL_TSC; | |
2019 | ||
b5a3dfa5 | 2020 | if (caa_unlikely(subbuf_offset(offsets->begin, ctx->chan) == 0)) { |
852c2936 MD |
2021 | offsets->switch_new_start = 1; /* For offsets->begin */ |
2022 | } else { | |
2023 | offsets->size = config->cb.record_header_size(config, chan, | |
2024 | offsets->begin, | |
2025 | &offsets->pre_header_padding, | |
2026 | ctx); | |
2027 | offsets->size += | |
2028 | lib_ring_buffer_align(offsets->begin + offsets->size, | |
2029 | ctx->largest_align) | |
2030 | + ctx->data_size; | |
b5a3dfa5 | 2031 | if (caa_unlikely(subbuf_offset(offsets->begin, chan) + |
852c2936 MD |
2032 | offsets->size > chan->backend.subbuf_size)) { |
2033 | offsets->switch_old_end = 1; /* For offsets->old */ | |
2034 | offsets->switch_new_start = 1; /* For offsets->begin */ | |
2035 | } | |
2036 | } | |
b5a3dfa5 | 2037 | if (caa_unlikely(offsets->switch_new_start)) { |
e185cf4b | 2038 | unsigned long sb_index, commit_count; |
15500a1b | 2039 | struct commit_counters_cold *cc_cold; |
852c2936 MD |
2040 | |
2041 | /* | |
2042 | * We are typically not filling the previous buffer completely. | |
2043 | */ | |
b5a3dfa5 | 2044 | if (caa_likely(offsets->switch_old_end)) |
852c2936 MD |
2045 | offsets->begin = subbuf_align(offsets->begin, chan); |
2046 | offsets->begin = offsets->begin | |
2047 | + config->cb.subbuffer_header_size(); | |
2048 | /* Test new buffer integrity */ | |
2049 | sb_index = subbuf_index(offsets->begin, chan); | |
e185cf4b MD |
2050 | /* |
2051 | * Read buf->offset before buf->commit_cold[sb_index].cc_sb. | |
2052 | * lib_ring_buffer_check_deliver() has the matching | |
2053 | * memory barriers required around commit_cold cc_sb | |
2054 | * updates to ensure reserve and commit counter updates | |
2055 | * are not seen reordered when updated by another CPU. | |
2056 | */ | |
2057 | cmm_smp_rmb(); | |
15500a1b MD |
2058 | cc_cold = shmp_index(handle, buf->commit_cold, sb_index); |
2059 | if (!cc_cold) | |
2060 | return -1; | |
2061 | commit_count = v_read(config, &cc_cold->cc_sb); | |
e185cf4b MD |
2062 | /* Read buf->commit_cold[sb_index].cc_sb before buf->offset. */ |
2063 | cmm_smp_rmb(); | |
2064 | if (caa_unlikely(offset_cmp != v_read(config, &buf->offset))) { | |
2065 | /* | |
2066 | * The reserve counter have been concurrently updated | |
2067 | * while we read the commit counter. This means the | |
2068 | * commit counter we read might not match buf->offset | |
2069 | * due to concurrent update. We therefore need to retry. | |
2070 | */ | |
2071 | goto retry; | |
2072 | } | |
852c2936 MD |
2073 | reserve_commit_diff = |
2074 | (buf_trunc(offsets->begin, chan) | |
2075 | >> chan->backend.num_subbuf_order) | |
e185cf4b | 2076 | - (commit_count & chan->commit_count_mask); |
b5a3dfa5 | 2077 | if (caa_likely(reserve_commit_diff == 0)) { |
852c2936 | 2078 | /* Next subbuffer not being written to. */ |
b5a3dfa5 | 2079 | if (caa_unlikely(config->mode != RING_BUFFER_OVERWRITE && |
852c2936 MD |
2080 | subbuf_trunc(offsets->begin, chan) |
2081 | - subbuf_trunc((unsigned long) | |
a6352fd4 | 2082 | uatomic_read(&buf->consumed), chan) |
852c2936 | 2083 | >= chan->backend.buf_size)) { |
64493e4f MD |
2084 | unsigned long nr_lost; |
2085 | ||
852c2936 MD |
2086 | /* |
2087 | * We do not overwrite non consumed buffers | |
2088 | * and we are full : record is lost. | |
2089 | */ | |
64493e4f | 2090 | nr_lost = v_read(config, &buf->records_lost_full); |
852c2936 | 2091 | v_inc(config, &buf->records_lost_full); |
64493e4f MD |
2092 | if ((nr_lost & (DBG_PRINT_NR_LOST - 1)) == 0) { |
2093 | DBG("%lu or more records lost in (%s:%d) (buffer full)\n", | |
2094 | nr_lost + 1, chan->backend.name, | |
2095 | buf->backend.cpu); | |
2096 | } | |
852c2936 MD |
2097 | return -ENOBUFS; |
2098 | } else { | |
2099 | /* | |
2100 | * Next subbuffer not being written to, and we | |
2101 | * are either in overwrite mode or the buffer is | |
2102 | * not full. It's safe to write in this new | |
2103 | * subbuffer. | |
2104 | */ | |
2105 | } | |
2106 | } else { | |
64493e4f MD |
2107 | unsigned long nr_lost; |
2108 | ||
852c2936 MD |
2109 | /* |
2110 | * Next subbuffer reserve offset does not match the | |
e185cf4b MD |
2111 | * commit offset, and this did not involve update to the |
2112 | * reserve counter. Drop record in producer-consumer and | |
852c2936 MD |
2113 | * overwrite mode. Caused by either a writer OOPS or too |
2114 | * many nested writes over a reserve/commit pair. | |
2115 | */ | |
64493e4f | 2116 | nr_lost = v_read(config, &buf->records_lost_wrap); |
852c2936 | 2117 | v_inc(config, &buf->records_lost_wrap); |
64493e4f MD |
2118 | if ((nr_lost & (DBG_PRINT_NR_LOST - 1)) == 0) { |
2119 | DBG("%lu or more records lost in (%s:%d) (wrap-around)\n", | |
2120 | nr_lost + 1, chan->backend.name, | |
2121 | buf->backend.cpu); | |
2122 | } | |
852c2936 MD |
2123 | return -EIO; |
2124 | } | |
2125 | offsets->size = | |
2126 | config->cb.record_header_size(config, chan, | |
2127 | offsets->begin, | |
2128 | &offsets->pre_header_padding, | |
2129 | ctx); | |
2130 | offsets->size += | |
2131 | lib_ring_buffer_align(offsets->begin + offsets->size, | |
2132 | ctx->largest_align) | |
2133 | + ctx->data_size; | |
b5a3dfa5 | 2134 | if (caa_unlikely(subbuf_offset(offsets->begin, chan) |
852c2936 | 2135 | + offsets->size > chan->backend.subbuf_size)) { |
64493e4f MD |
2136 | unsigned long nr_lost; |
2137 | ||
852c2936 MD |
2138 | /* |
2139 | * Record too big for subbuffers, report error, don't | |
2140 | * complete the sub-buffer switch. | |
2141 | */ | |
64493e4f | 2142 | nr_lost = v_read(config, &buf->records_lost_big); |
852c2936 | 2143 | v_inc(config, &buf->records_lost_big); |
64493e4f MD |
2144 | if ((nr_lost & (DBG_PRINT_NR_LOST - 1)) == 0) { |
2145 | DBG("%lu or more records lost in (%s:%d) record size " | |
2146 | " of %zu bytes is too large for buffer\n", | |
2147 | nr_lost + 1, chan->backend.name, | |
2148 | buf->backend.cpu, offsets->size); | |
2149 | } | |
852c2936 MD |
2150 | return -ENOSPC; |
2151 | } else { | |
2152 | /* | |
2153 | * We just made a successful buffer switch and the | |
2154 | * record fits in the new subbuffer. Let's write. | |
2155 | */ | |
2156 | } | |
2157 | } else { | |
2158 | /* | |
2159 | * Record fits in the current buffer and we are not on a switch | |
2160 | * boundary. It's safe to write. | |
2161 | */ | |
2162 | } | |
2163 | offsets->end = offsets->begin + offsets->size; | |
1ad21f70 MD |
2164 | |
2165 | if (caa_unlikely(subbuf_offset(offsets->end, chan) == 0)) { | |
2166 | /* | |
2167 | * The offset_end will fall at the very beginning of the next | |
2168 | * subbuffer. | |
2169 | */ | |
2170 | offsets->switch_new_end = 1; /* For offsets->begin */ | |
2171 | } | |
852c2936 MD |
2172 | return 0; |
2173 | } | |
2174 | ||
2175 | /** | |
2176 | * lib_ring_buffer_reserve_slow - Atomic slot reservation in a buffer. | |
2177 | * @ctx: ring buffer context. | |
2178 | * | |
2179 | * Return : -NOBUFS if not enough space, -ENOSPC if event size too large, | |
2180 | * -EIO for other errors, else returns 0. | |
2181 | * It will take care of sub-buffer switching. | |
2182 | */ | |
4cfec15c | 2183 | int lib_ring_buffer_reserve_slow(struct lttng_ust_lib_ring_buffer_ctx *ctx) |
852c2936 MD |
2184 | { |
2185 | struct channel *chan = ctx->chan; | |
38fae1d3 | 2186 | struct lttng_ust_shm_handle *handle = ctx->handle; |
4cfec15c MD |
2187 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
2188 | struct lttng_ust_lib_ring_buffer *buf; | |
852c2936 MD |
2189 | struct switch_offsets offsets; |
2190 | int ret; | |
2191 | ||
2192 | if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) | |
1d498196 | 2193 | buf = shmp(handle, chan->backend.buf[ctx->cpu].shmp); |
852c2936 | 2194 | else |
1d498196 | 2195 | buf = shmp(handle, chan->backend.buf[0].shmp); |
15500a1b MD |
2196 | if (!buf) |
2197 | return -EIO; | |
852c2936 MD |
2198 | ctx->buf = buf; |
2199 | ||
2200 | offsets.size = 0; | |
2201 | ||
2202 | do { | |
2203 | ret = lib_ring_buffer_try_reserve_slow(buf, chan, &offsets, | |
2204 | ctx); | |
b5a3dfa5 | 2205 | if (caa_unlikely(ret)) |
852c2936 | 2206 | return ret; |
b5a3dfa5 | 2207 | } while (caa_unlikely(v_cmpxchg(config, &buf->offset, offsets.old, |
852c2936 MD |
2208 | offsets.end) |
2209 | != offsets.old)); | |
2210 | ||
2211 | /* | |
2212 | * Atomically update last_tsc. This update races against concurrent | |
2213 | * atomic updates, but the race will always cause supplementary full TSC | |
2214 | * records, never the opposite (missing a full TSC record when it would | |
2215 | * be needed). | |
2216 | */ | |
2217 | save_last_tsc(config, buf, ctx->tsc); | |
2218 | ||
2219 | /* | |
2220 | * Push the reader if necessary | |
2221 | */ | |
2222 | lib_ring_buffer_reserve_push_reader(buf, chan, offsets.end - 1); | |
2223 | ||
2224 | /* | |
2225 | * Clear noref flag for this subbuffer. | |
2226 | */ | |
2227 | lib_ring_buffer_clear_noref(config, &buf->backend, | |
1d498196 MD |
2228 | subbuf_index(offsets.end - 1, chan), |
2229 | handle); | |
852c2936 MD |
2230 | |
2231 | /* | |
2232 | * Switch old subbuffer if needed. | |
2233 | */ | |
b5a3dfa5 | 2234 | if (caa_unlikely(offsets.switch_old_end)) { |
852c2936 | 2235 | lib_ring_buffer_clear_noref(config, &buf->backend, |
1d498196 MD |
2236 | subbuf_index(offsets.old - 1, chan), |
2237 | handle); | |
2238 | lib_ring_buffer_switch_old_end(buf, chan, &offsets, ctx->tsc, handle); | |
852c2936 MD |
2239 | } |
2240 | ||
2241 | /* | |
2242 | * Populate new subbuffer. | |
2243 | */ | |
b5a3dfa5 | 2244 | if (caa_unlikely(offsets.switch_new_start)) |
1d498196 | 2245 | lib_ring_buffer_switch_new_start(buf, chan, &offsets, ctx->tsc, handle); |
852c2936 | 2246 | |
1ad21f70 MD |
2247 | if (caa_unlikely(offsets.switch_new_end)) |
2248 | lib_ring_buffer_switch_new_end(buf, chan, &offsets, ctx->tsc, handle); | |
2249 | ||
852c2936 MD |
2250 | ctx->slot_size = offsets.size; |
2251 | ctx->pre_offset = offsets.begin; | |
2252 | ctx->buf_offset = offsets.begin + offsets.pre_header_padding; | |
2253 | return 0; | |
2254 | } | |
f645cfa7 | 2255 | |
b07cd987 MD |
2256 | static |
2257 | void lib_ring_buffer_vmcore_check_deliver(const struct lttng_ust_lib_ring_buffer_config *config, | |
2258 | struct lttng_ust_lib_ring_buffer *buf, | |
2259 | unsigned long commit_count, | |
2260 | unsigned long idx, | |
2261 | struct lttng_ust_shm_handle *handle) | |
2262 | { | |
15500a1b MD |
2263 | struct commit_counters_hot *cc_hot; |
2264 | ||
2265 | if (config->oops != RING_BUFFER_OOPS_CONSISTENCY) | |
2266 | return; | |
2267 | cc_hot = shmp_index(handle, buf->commit_hot, idx); | |
2268 | if (!cc_hot) | |
2269 | return; | |
2270 | v_set(config, &cc_hot->seq, commit_count); | |
b07cd987 MD |
2271 | } |
2272 | ||
9c995331 MD |
2273 | /* |
2274 | * The ring buffer can count events recorded and overwritten per buffer, | |
2275 | * but it is disabled by default due to its performance overhead. | |
2276 | */ | |
2277 | #ifdef LTTNG_RING_BUFFER_COUNT_EVENTS | |
2278 | static | |
2279 | void deliver_count_events(const struct lttng_ust_lib_ring_buffer_config *config, | |
2280 | struct lttng_ust_lib_ring_buffer *buf, | |
2281 | unsigned long idx, | |
2282 | struct lttng_ust_shm_handle *handle) | |
2283 | { | |
2284 | v_add(config, subbuffer_get_records_count(config, | |
2285 | &buf->backend, idx, handle), | |
2286 | &buf->records_count); | |
2287 | v_add(config, subbuffer_count_records_overrun(config, | |
2288 | &buf->backend, idx, handle), | |
2289 | &buf->records_overrun); | |
2290 | } | |
2291 | #else /* LTTNG_RING_BUFFER_COUNT_EVENTS */ | |
2292 | static | |
2293 | void deliver_count_events(const struct lttng_ust_lib_ring_buffer_config *config, | |
2294 | struct lttng_ust_lib_ring_buffer *buf, | |
2295 | unsigned long idx, | |
2296 | struct lttng_ust_shm_handle *handle) | |
2297 | { | |
2298 | } | |
2299 | #endif /* #else LTTNG_RING_BUFFER_COUNT_EVENTS */ | |
2300 | ||
b07cd987 MD |
2301 | void lib_ring_buffer_check_deliver_slow(const struct lttng_ust_lib_ring_buffer_config *config, |
2302 | struct lttng_ust_lib_ring_buffer *buf, | |
2303 | struct channel *chan, | |
2304 | unsigned long offset, | |
2305 | unsigned long commit_count, | |
2306 | unsigned long idx, | |
2307 | struct lttng_ust_shm_handle *handle, | |
2308 | uint64_t tsc) | |
2309 | { | |
2310 | unsigned long old_commit_count = commit_count | |
2311 | - chan->backend.subbuf_size; | |
15500a1b | 2312 | struct commit_counters_cold *cc_cold; |
b07cd987 MD |
2313 | |
2314 | /* | |
2315 | * If we succeeded at updating cc_sb below, we are the subbuffer | |
2316 | * writer delivering the subbuffer. Deals with concurrent | |
2317 | * updates of the "cc" value without adding a add_return atomic | |
2318 | * operation to the fast path. | |
2319 | * | |
2320 | * We are doing the delivery in two steps: | |
2321 | * - First, we cmpxchg() cc_sb to the new value | |
2322 | * old_commit_count + 1. This ensures that we are the only | |
2323 | * subbuffer user successfully filling the subbuffer, but we | |
2324 | * do _not_ set the cc_sb value to "commit_count" yet. | |
2325 | * Therefore, other writers that would wrap around the ring | |
2326 | * buffer and try to start writing to our subbuffer would | |
2327 | * have to drop records, because it would appear as | |
2328 | * non-filled. | |
2329 | * We therefore have exclusive access to the subbuffer control | |
2330 | * structures. This mutual exclusion with other writers is | |
2331 | * crucially important to perform record overruns count in | |
2332 | * flight recorder mode locklessly. | |
2333 | * - When we are ready to release the subbuffer (either for | |
2334 | * reading or for overrun by other writers), we simply set the | |
2335 | * cc_sb value to "commit_count" and perform delivery. | |
2336 | * | |
2337 | * The subbuffer size is least 2 bytes (minimum size: 1 page). | |
2338 | * This guarantees that old_commit_count + 1 != commit_count. | |
2339 | */ | |
2340 | ||
2341 | /* | |
2342 | * Order prior updates to reserve count prior to the | |
2343 | * commit_cold cc_sb update. | |
2344 | */ | |
2345 | cmm_smp_wmb(); | |
15500a1b MD |
2346 | cc_cold = shmp_index(handle, buf->commit_cold, idx); |
2347 | if (!cc_cold) | |
2348 | return; | |
2349 | if (caa_likely(v_cmpxchg(config, &cc_cold->cc_sb, | |
b07cd987 MD |
2350 | old_commit_count, old_commit_count + 1) |
2351 | == old_commit_count)) { | |
2352 | /* | |
2353 | * Start of exclusive subbuffer access. We are | |
2354 | * guaranteed to be the last writer in this subbuffer | |
2355 | * and any other writer trying to access this subbuffer | |
2356 | * in this state is required to drop records. | |
2357 | */ | |
9c995331 | 2358 | deliver_count_events(config, buf, idx, handle); |
b07cd987 MD |
2359 | config->cb.buffer_end(buf, tsc, idx, |
2360 | lib_ring_buffer_get_data_size(config, | |
2361 | buf, | |
2362 | idx, | |
2363 | handle), | |
2364 | handle); | |
2365 | ||
2366 | /* | |
2367 | * Increment the packet counter while we have exclusive | |
2368 | * access. | |
2369 | */ | |
2370 | subbuffer_inc_packet_count(config, &buf->backend, idx, handle); | |
2371 | ||
2372 | /* | |
2373 | * Set noref flag and offset for this subbuffer id. | |
2374 | * Contains a memory barrier that ensures counter stores | |
2375 | * are ordered before set noref and offset. | |
2376 | */ | |
2377 | lib_ring_buffer_set_noref_offset(config, &buf->backend, idx, | |
2378 | buf_trunc_val(offset, chan), handle); | |
2379 | ||
2380 | /* | |
2381 | * Order set_noref and record counter updates before the | |
2382 | * end of subbuffer exclusive access. Orders with | |
2383 | * respect to writers coming into the subbuffer after | |
2384 | * wrap around, and also order wrt concurrent readers. | |
2385 | */ | |
2386 | cmm_smp_mb(); | |
2387 | /* End of exclusive subbuffer access */ | |
15500a1b | 2388 | v_set(config, &cc_cold->cc_sb, commit_count); |
b07cd987 MD |
2389 | /* |
2390 | * Order later updates to reserve count after | |
2391 | * the commit cold cc_sb update. | |
2392 | */ | |
2393 | cmm_smp_wmb(); | |
2394 | lib_ring_buffer_vmcore_check_deliver(config, buf, | |
2395 | commit_count, idx, handle); | |
2396 | ||
2397 | /* | |
2398 | * RING_BUFFER_WAKEUP_BY_WRITER wakeup is not lock-free. | |
2399 | */ | |
2400 | if (config->wakeup == RING_BUFFER_WAKEUP_BY_WRITER | |
2401 | && uatomic_read(&buf->active_readers) | |
2402 | && lib_ring_buffer_poll_deliver(config, buf, chan, handle)) { | |
2403 | lib_ring_buffer_wakeup(buf, handle); | |
2404 | } | |
2405 | } | |
2406 | } | |
2407 | ||
f645cfa7 MD |
2408 | /* |
2409 | * Force a read (imply TLS fixup for dlopen) of TLS variables. | |
2410 | */ | |
2411 | void lttng_fixup_ringbuffer_tls(void) | |
2412 | { | |
8c90a710 | 2413 | asm volatile ("" : : "m" (URCU_TLS(lib_ring_buffer_nesting))); |
f645cfa7 | 2414 | } |
03d2d293 MD |
2415 | |
2416 | void lib_ringbuffer_signal_init(void) | |
2417 | { | |
2418 | sigset_t mask; | |
2419 | int ret; | |
2420 | ||
2421 | /* | |
2422 | * Block signal for entire process, so only our thread processes | |
2423 | * it. | |
2424 | */ | |
2425 | rb_setmask(&mask); | |
2426 | ret = pthread_sigmask(SIG_BLOCK, &mask, NULL); | |
2427 | if (ret) { | |
2428 | errno = ret; | |
2429 | PERROR("pthread_sigmask"); | |
2430 | } | |
2431 | } |