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 |
a6352fd4 | 55 | #include <sys/types.h> |
431d5cf0 MD |
56 | #include <sys/mman.h> |
57 | #include <sys/stat.h> | |
03d2d293 | 58 | #include <unistd.h> |
431d5cf0 | 59 | #include <fcntl.h> |
03d2d293 MD |
60 | #include <signal.h> |
61 | #include <time.h> | |
14641deb | 62 | #include <urcu/compiler.h> |
a6352fd4 | 63 | #include <urcu/ref.h> |
8c90a710 | 64 | #include <urcu/tls-compat.h> |
35897f8b | 65 | #include <helper.h> |
14641deb | 66 | |
a6352fd4 | 67 | #include "smp.h" |
4318ae1b | 68 | #include <lttng/ringbuffer-config.h> |
2fed87ae | 69 | #include "vatomic.h" |
4931a13e MD |
70 | #include "backend.h" |
71 | #include "frontend.h" | |
a6352fd4 | 72 | #include "shm.h" |
f645cfa7 | 73 | #include "tlsfixup.h" |
bdcf8d82 | 74 | #include "../liblttng-ust/compat.h" /* For ENODATA */ |
852c2936 | 75 | |
431d5cf0 MD |
76 | #ifndef max |
77 | #define max(a, b) ((a) > (b) ? (a) : (b)) | |
78 | #endif | |
79 | ||
64493e4f MD |
80 | /* Print DBG() messages about events lost only every 1048576 hits */ |
81 | #define DBG_PRINT_NR_LOST (1UL << 20) | |
82 | ||
34a91bdb MD |
83 | #define LTTNG_UST_RB_SIG_FLUSH SIGRTMIN |
84 | #define LTTNG_UST_RB_SIG_READ SIGRTMIN + 1 | |
85 | #define LTTNG_UST_RB_SIG_TEARDOWN SIGRTMIN + 2 | |
03d2d293 MD |
86 | #define CLOCKID CLOCK_MONOTONIC |
87 | ||
2432c3c9 MD |
88 | /* |
89 | * Use POSIX SHM: shm_open(3) and shm_unlink(3). | |
90 | * close(2) to close the fd returned by shm_open. | |
91 | * shm_unlink releases the shared memory object name. | |
92 | * ftruncate(2) sets the size of the memory object. | |
93 | * mmap/munmap maps the shared memory obj to a virtual address in the | |
94 | * calling proceess (should be done both in libust and consumer). | |
95 | * See shm_overview(7) for details. | |
96 | * Pass file descriptor returned by shm_open(3) to ltt-sessiond through | |
97 | * a UNIX socket. | |
98 | * | |
99 | * Since we don't need to access the object using its name, we can | |
100 | * immediately shm_unlink(3) it, and only keep the handle with its file | |
101 | * descriptor. | |
102 | */ | |
103 | ||
852c2936 MD |
104 | /* |
105 | * Internal structure representing offsets to use at a sub-buffer switch. | |
106 | */ | |
107 | struct switch_offsets { | |
108 | unsigned long begin, end, old; | |
109 | size_t pre_header_padding, size; | |
110 | unsigned int switch_new_start:1, switch_new_end:1, switch_old_start:1, | |
111 | switch_old_end:1; | |
112 | }; | |
113 | ||
8c90a710 | 114 | DEFINE_URCU_TLS(unsigned int, lib_ring_buffer_nesting); |
852c2936 | 115 | |
cb7378b3 MD |
116 | /* |
117 | * wakeup_fd_mutex protects wakeup fd use by timer from concurrent | |
118 | * close. | |
119 | */ | |
120 | static pthread_mutex_t wakeup_fd_mutex = PTHREAD_MUTEX_INITIALIZER; | |
121 | ||
852c2936 MD |
122 | static |
123 | void lib_ring_buffer_print_errors(struct channel *chan, | |
009769ca MD |
124 | struct lttng_ust_lib_ring_buffer *buf, int cpu, |
125 | struct lttng_ust_shm_handle *handle); | |
852c2936 | 126 | |
03d2d293 MD |
127 | /* |
128 | * Handle timer teardown race wrt memory free of private data by | |
129 | * ring buffer signals are handled by a single thread, which permits | |
130 | * a synchronization point between handling of each signal. | |
131 | * Protected by the ust mutex. | |
132 | */ | |
133 | struct timer_signal_data { | |
134 | pthread_t tid; /* thread id managing signals */ | |
135 | int setup_done; | |
136 | int qs_done; | |
137 | }; | |
138 | ||
139 | static struct timer_signal_data timer_signal; | |
140 | ||
852c2936 MD |
141 | /** |
142 | * lib_ring_buffer_reset - Reset ring buffer to initial values. | |
143 | * @buf: Ring buffer. | |
144 | * | |
145 | * Effectively empty the ring buffer. Should be called when the buffer is not | |
146 | * used for writing. The ring buffer can be opened for reading, but the reader | |
147 | * should not be using the iterator concurrently with reset. The previous | |
148 | * current iterator record is reset. | |
149 | */ | |
4cfec15c | 150 | void lib_ring_buffer_reset(struct lttng_ust_lib_ring_buffer *buf, |
38fae1d3 | 151 | struct lttng_ust_shm_handle *handle) |
852c2936 | 152 | { |
1d498196 | 153 | struct channel *chan = shmp(handle, buf->backend.chan); |
4cfec15c | 154 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
155 | unsigned int i; |
156 | ||
157 | /* | |
158 | * Reset iterator first. It will put the subbuffer if it currently holds | |
159 | * it. | |
160 | */ | |
852c2936 MD |
161 | v_set(config, &buf->offset, 0); |
162 | for (i = 0; i < chan->backend.num_subbuf; i++) { | |
4746ae29 MD |
163 | v_set(config, &shmp_index(handle, buf->commit_hot, i)->cc, 0); |
164 | v_set(config, &shmp_index(handle, buf->commit_hot, i)->seq, 0); | |
165 | v_set(config, &shmp_index(handle, buf->commit_cold, i)->cc_sb, 0); | |
852c2936 | 166 | } |
a6352fd4 MD |
167 | uatomic_set(&buf->consumed, 0); |
168 | uatomic_set(&buf->record_disabled, 0); | |
852c2936 | 169 | v_set(config, &buf->last_tsc, 0); |
1d498196 | 170 | lib_ring_buffer_backend_reset(&buf->backend, handle); |
852c2936 MD |
171 | /* Don't reset number of active readers */ |
172 | v_set(config, &buf->records_lost_full, 0); | |
173 | v_set(config, &buf->records_lost_wrap, 0); | |
174 | v_set(config, &buf->records_lost_big, 0); | |
175 | v_set(config, &buf->records_count, 0); | |
176 | v_set(config, &buf->records_overrun, 0); | |
177 | buf->finalized = 0; | |
178 | } | |
852c2936 MD |
179 | |
180 | /** | |
181 | * channel_reset - Reset channel to initial values. | |
182 | * @chan: Channel. | |
183 | * | |
184 | * Effectively empty the channel. Should be called when the channel is not used | |
185 | * for writing. The channel can be opened for reading, but the reader should not | |
186 | * be using the iterator concurrently with reset. The previous current iterator | |
187 | * record is reset. | |
188 | */ | |
189 | void channel_reset(struct channel *chan) | |
190 | { | |
191 | /* | |
192 | * Reset iterators first. Will put the subbuffer if held for reading. | |
193 | */ | |
a6352fd4 | 194 | uatomic_set(&chan->record_disabled, 0); |
852c2936 MD |
195 | /* Don't reset commit_count_mask, still valid */ |
196 | channel_backend_reset(&chan->backend); | |
197 | /* Don't reset switch/read timer interval */ | |
198 | /* Don't reset notifiers and notifier enable bits */ | |
199 | /* Don't reset reader reference count */ | |
200 | } | |
852c2936 MD |
201 | |
202 | /* | |
203 | * Must be called under cpu hotplug protection. | |
204 | */ | |
4cfec15c | 205 | int lib_ring_buffer_create(struct lttng_ust_lib_ring_buffer *buf, |
a6352fd4 | 206 | struct channel_backend *chanb, int cpu, |
38fae1d3 | 207 | struct lttng_ust_shm_handle *handle, |
1d498196 | 208 | struct shm_object *shmobj) |
852c2936 | 209 | { |
4cfec15c | 210 | const struct lttng_ust_lib_ring_buffer_config *config = &chanb->config; |
14641deb | 211 | struct channel *chan = caa_container_of(chanb, struct channel, backend); |
a3f61e7f | 212 | void *priv = channel_get_private(chan); |
852c2936 | 213 | size_t subbuf_header_size; |
2fed87ae | 214 | uint64_t tsc; |
852c2936 MD |
215 | int ret; |
216 | ||
217 | /* Test for cpu hotplug */ | |
218 | if (buf->backend.allocated) | |
219 | return 0; | |
220 | ||
a6352fd4 | 221 | ret = lib_ring_buffer_backend_create(&buf->backend, &chan->backend, |
1d498196 | 222 | cpu, handle, shmobj); |
852c2936 MD |
223 | if (ret) |
224 | return ret; | |
225 | ||
1d498196 MD |
226 | align_shm(shmobj, __alignof__(struct commit_counters_hot)); |
227 | set_shmp(buf->commit_hot, | |
228 | zalloc_shm(shmobj, | |
229 | sizeof(struct commit_counters_hot) * chan->backend.num_subbuf)); | |
230 | if (!shmp(handle, buf->commit_hot)) { | |
852c2936 MD |
231 | ret = -ENOMEM; |
232 | goto free_chanbuf; | |
233 | } | |
234 | ||
1d498196 MD |
235 | align_shm(shmobj, __alignof__(struct commit_counters_cold)); |
236 | set_shmp(buf->commit_cold, | |
237 | zalloc_shm(shmobj, | |
238 | sizeof(struct commit_counters_cold) * chan->backend.num_subbuf)); | |
239 | if (!shmp(handle, buf->commit_cold)) { | |
852c2936 MD |
240 | ret = -ENOMEM; |
241 | goto free_commit; | |
242 | } | |
243 | ||
852c2936 MD |
244 | /* |
245 | * Write the subbuffer header for first subbuffer so we know the total | |
246 | * duration of data gathering. | |
247 | */ | |
248 | subbuf_header_size = config->cb.subbuffer_header_size(); | |
249 | v_set(config, &buf->offset, subbuf_header_size); | |
4746ae29 | 250 | subbuffer_id_clear_noref(config, &shmp_index(handle, buf->backend.buf_wsb, 0)->id); |
1d498196 MD |
251 | tsc = config->cb.ring_buffer_clock_read(shmp(handle, buf->backend.chan)); |
252 | config->cb.buffer_begin(buf, tsc, 0, handle); | |
4746ae29 | 253 | v_add(config, subbuf_header_size, &shmp_index(handle, buf->commit_hot, 0)->cc); |
852c2936 MD |
254 | |
255 | if (config->cb.buffer_create) { | |
1d498196 | 256 | ret = config->cb.buffer_create(buf, priv, cpu, chanb->name, handle); |
852c2936 MD |
257 | if (ret) |
258 | goto free_init; | |
259 | } | |
852c2936 | 260 | buf->backend.allocated = 1; |
852c2936 MD |
261 | return 0; |
262 | ||
263 | /* Error handling */ | |
264 | free_init: | |
a6352fd4 | 265 | /* commit_cold will be freed by shm teardown */ |
852c2936 | 266 | free_commit: |
a6352fd4 | 267 | /* commit_hot will be freed by shm teardown */ |
852c2936 | 268 | free_chanbuf: |
852c2936 MD |
269 | return ret; |
270 | } | |
271 | ||
03d2d293 MD |
272 | static |
273 | void lib_ring_buffer_channel_switch_timer(int sig, siginfo_t *si, void *uc) | |
852c2936 | 274 | { |
03d2d293 MD |
275 | const struct lttng_ust_lib_ring_buffer_config *config; |
276 | struct lttng_ust_shm_handle *handle; | |
277 | struct channel *chan; | |
278 | int cpu; | |
279 | ||
280 | assert(CMM_LOAD_SHARED(timer_signal.tid) == pthread_self()); | |
281 | ||
282 | chan = si->si_value.sival_ptr; | |
283 | handle = chan->handle; | |
284 | config = &chan->backend.config; | |
852c2936 | 285 | |
34a91bdb | 286 | DBG("Switch timer for channel %p\n", chan); |
03d2d293 | 287 | |
4dcd7e80 MD |
288 | /* |
289 | * Only flush buffers periodically if readers are active. | |
290 | */ | |
cb7378b3 | 291 | pthread_mutex_lock(&wakeup_fd_mutex); |
03d2d293 MD |
292 | if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) { |
293 | for_each_possible_cpu(cpu) { | |
294 | struct lttng_ust_lib_ring_buffer *buf = | |
295 | shmp(handle, chan->backend.buf[cpu].shmp); | |
4dcd7e80 MD |
296 | if (uatomic_read(&buf->active_readers)) |
297 | lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE, | |
298 | chan->handle); | |
03d2d293 MD |
299 | } |
300 | } else { | |
301 | struct lttng_ust_lib_ring_buffer *buf = | |
302 | shmp(handle, chan->backend.buf[0].shmp); | |
303 | ||
34a91bdb MD |
304 | if (uatomic_read(&buf->active_readers)) |
305 | lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE, | |
306 | chan->handle); | |
307 | } | |
308 | pthread_mutex_unlock(&wakeup_fd_mutex); | |
309 | return; | |
310 | } | |
311 | ||
312 | static | |
313 | void lib_ring_buffer_channel_do_read(struct channel *chan) | |
314 | { | |
315 | const struct lttng_ust_lib_ring_buffer_config *config; | |
316 | struct lttng_ust_shm_handle *handle; | |
317 | int cpu; | |
318 | ||
319 | handle = chan->handle; | |
320 | config = &chan->backend.config; | |
321 | ||
322 | /* | |
323 | * Only flush buffers periodically if readers are active. | |
324 | */ | |
325 | pthread_mutex_lock(&wakeup_fd_mutex); | |
326 | if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) { | |
327 | for_each_possible_cpu(cpu) { | |
328 | struct lttng_ust_lib_ring_buffer *buf = | |
329 | shmp(handle, chan->backend.buf[cpu].shmp); | |
330 | ||
331 | if (uatomic_read(&buf->active_readers) | |
332 | && lib_ring_buffer_poll_deliver(config, buf, | |
333 | chan, handle)) { | |
334 | lib_ring_buffer_wakeup(buf, handle); | |
335 | } | |
336 | } | |
337 | } else { | |
338 | struct lttng_ust_lib_ring_buffer *buf = | |
339 | shmp(handle, chan->backend.buf[0].shmp); | |
340 | ||
341 | if (uatomic_read(&buf->active_readers) | |
342 | && lib_ring_buffer_poll_deliver(config, buf, | |
343 | chan, handle)) { | |
344 | lib_ring_buffer_wakeup(buf, handle); | |
345 | } | |
03d2d293 | 346 | } |
cb7378b3 | 347 | pthread_mutex_unlock(&wakeup_fd_mutex); |
34a91bdb MD |
348 | } |
349 | ||
350 | static | |
351 | void lib_ring_buffer_channel_read_timer(int sig, siginfo_t *si, void *uc) | |
352 | { | |
353 | struct channel *chan; | |
354 | ||
355 | assert(CMM_LOAD_SHARED(timer_signal.tid) == pthread_self()); | |
356 | chan = si->si_value.sival_ptr; | |
357 | DBG("Read timer for channel %p\n", chan); | |
358 | lib_ring_buffer_channel_do_read(chan); | |
03d2d293 MD |
359 | return; |
360 | } | |
361 | ||
362 | static | |
363 | void rb_setmask(sigset_t *mask) | |
364 | { | |
365 | int ret; | |
366 | ||
367 | ret = sigemptyset(mask); | |
368 | if (ret) { | |
369 | PERROR("sigemptyset"); | |
370 | } | |
34a91bdb MD |
371 | ret = sigaddset(mask, LTTNG_UST_RB_SIG_FLUSH); |
372 | if (ret) { | |
373 | PERROR("sigaddset"); | |
374 | } | |
375 | ret = sigaddset(mask, LTTNG_UST_RB_SIG_READ); | |
03d2d293 MD |
376 | if (ret) { |
377 | PERROR("sigaddset"); | |
378 | } | |
379 | ret = sigaddset(mask, LTTNG_UST_RB_SIG_TEARDOWN); | |
380 | if (ret) { | |
381 | PERROR("sigaddset"); | |
382 | } | |
383 | } | |
384 | ||
385 | static | |
386 | void *sig_thread(void *arg) | |
387 | { | |
388 | sigset_t mask; | |
389 | siginfo_t info; | |
390 | int signr; | |
391 | ||
392 | /* Only self thread will receive signal mask. */ | |
393 | rb_setmask(&mask); | |
394 | CMM_STORE_SHARED(timer_signal.tid, pthread_self()); | |
395 | ||
396 | for (;;) { | |
397 | signr = sigwaitinfo(&mask, &info); | |
398 | if (signr == -1) { | |
34a91bdb MD |
399 | if (errno != EINTR) |
400 | PERROR("sigwaitinfo"); | |
03d2d293 MD |
401 | continue; |
402 | } | |
34a91bdb | 403 | if (signr == LTTNG_UST_RB_SIG_FLUSH) { |
03d2d293 MD |
404 | lib_ring_buffer_channel_switch_timer(info.si_signo, |
405 | &info, NULL); | |
34a91bdb MD |
406 | } else if (signr == LTTNG_UST_RB_SIG_READ) { |
407 | lib_ring_buffer_channel_read_timer(info.si_signo, | |
408 | &info, NULL); | |
03d2d293 MD |
409 | } else if (signr == LTTNG_UST_RB_SIG_TEARDOWN) { |
410 | cmm_smp_mb(); | |
411 | CMM_STORE_SHARED(timer_signal.qs_done, 1); | |
412 | cmm_smp_mb(); | |
413 | } else { | |
414 | ERR("Unexptected signal %d\n", info.si_signo); | |
415 | } | |
416 | } | |
417 | return NULL; | |
418 | } | |
419 | ||
420 | /* | |
421 | * Called with ust_lock() held. | |
422 | * Ensure only a single thread listens on the timer signal. | |
423 | */ | |
424 | static | |
425 | void lib_ring_buffer_setup_timer_thread(void) | |
426 | { | |
427 | pthread_t thread; | |
428 | int ret; | |
429 | ||
430 | if (timer_signal.setup_done) | |
852c2936 | 431 | return; |
03d2d293 MD |
432 | |
433 | ret = pthread_create(&thread, NULL, &sig_thread, NULL); | |
434 | if (ret) { | |
435 | errno = ret; | |
436 | PERROR("pthread_create"); | |
437 | } | |
438 | ret = pthread_detach(thread); | |
439 | if (ret) { | |
440 | errno = ret; | |
441 | PERROR("pthread_detach"); | |
442 | } | |
443 | timer_signal.setup_done = 1; | |
852c2936 MD |
444 | } |
445 | ||
03d2d293 MD |
446 | /* |
447 | * Called with ust_lock() held. | |
448 | */ | |
449 | static | |
450 | void lib_ring_buffer_channel_switch_timer_start(struct channel *chan) | |
852c2936 | 451 | { |
03d2d293 MD |
452 | struct sigevent sev; |
453 | struct itimerspec its; | |
454 | int ret; | |
852c2936 | 455 | |
03d2d293 | 456 | if (!chan->switch_timer_interval || chan->switch_timer_enabled) |
852c2936 MD |
457 | return; |
458 | ||
03d2d293 MD |
459 | chan->switch_timer_enabled = 1; |
460 | ||
461 | lib_ring_buffer_setup_timer_thread(); | |
462 | ||
463 | sev.sigev_notify = SIGEV_SIGNAL; | |
34a91bdb | 464 | sev.sigev_signo = LTTNG_UST_RB_SIG_FLUSH; |
03d2d293 MD |
465 | sev.sigev_value.sival_ptr = chan; |
466 | ret = timer_create(CLOCKID, &sev, &chan->switch_timer); | |
467 | if (ret == -1) { | |
468 | PERROR("timer_create"); | |
469 | } | |
470 | ||
471 | its.it_value.tv_sec = chan->switch_timer_interval / 1000000; | |
472 | its.it_value.tv_nsec = chan->switch_timer_interval % 1000000; | |
473 | its.it_interval.tv_sec = its.it_value.tv_sec; | |
474 | its.it_interval.tv_nsec = its.it_value.tv_nsec; | |
475 | ||
476 | ret = timer_settime(chan->switch_timer, 0, &its, NULL); | |
477 | if (ret == -1) { | |
478 | PERROR("timer_settime"); | |
479 | } | |
480 | } | |
481 | ||
482 | /* | |
483 | * Called with ust_lock() held. | |
484 | */ | |
485 | static | |
486 | void lib_ring_buffer_channel_switch_timer_stop(struct channel *chan) | |
487 | { | |
488 | sigset_t pending_set; | |
34a91bdb | 489 | int ret; |
03d2d293 MD |
490 | |
491 | if (!chan->switch_timer_interval || !chan->switch_timer_enabled) | |
492 | return; | |
493 | ||
494 | ret = timer_delete(chan->switch_timer); | |
495 | if (ret == -1) { | |
496 | PERROR("timer_delete"); | |
497 | } | |
498 | ||
499 | /* | |
500 | * Ensure we don't have any signal queued for this channel. | |
501 | */ | |
502 | for (;;) { | |
503 | ret = sigemptyset(&pending_set); | |
504 | if (ret == -1) { | |
505 | PERROR("sigemptyset"); | |
506 | } | |
507 | ret = sigpending(&pending_set); | |
508 | if (ret == -1) { | |
509 | PERROR("sigpending"); | |
510 | } | |
34a91bdb | 511 | if (!sigismember(&pending_set, LTTNG_UST_RB_SIG_FLUSH)) |
03d2d293 MD |
512 | break; |
513 | caa_cpu_relax(); | |
514 | } | |
515 | ||
516 | /* | |
517 | * From this point, no new signal handler will be fired that | |
518 | * would try to access "chan". However, we still need to wait | |
519 | * for any currently executing handler to complete. | |
520 | */ | |
521 | cmm_smp_mb(); | |
522 | CMM_STORE_SHARED(timer_signal.qs_done, 0); | |
523 | cmm_smp_mb(); | |
524 | ||
525 | /* | |
526 | * Kill with LTTNG_UST_RB_SIG_TEARDOWN, so signal management | |
527 | * thread wakes up. | |
528 | */ | |
529 | kill(getpid(), LTTNG_UST_RB_SIG_TEARDOWN); | |
530 | ||
531 | while (!CMM_LOAD_SHARED(timer_signal.qs_done)) | |
532 | caa_cpu_relax(); | |
533 | cmm_smp_mb(); | |
534 | ||
535 | chan->switch_timer = 0; | |
536 | chan->switch_timer_enabled = 0; | |
852c2936 MD |
537 | } |
538 | ||
539 | /* | |
34a91bdb | 540 | * Called with ust_lock() held. |
852c2936 | 541 | */ |
34a91bdb MD |
542 | static |
543 | void lib_ring_buffer_channel_read_timer_start(struct channel *chan) | |
852c2936 | 544 | { |
4cfec15c | 545 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
34a91bdb MD |
546 | struct sigevent sev; |
547 | struct itimerspec its; | |
548 | int ret; | |
852c2936 | 549 | |
34a91bdb MD |
550 | if (config->wakeup != RING_BUFFER_WAKEUP_BY_TIMER |
551 | || !chan->read_timer_interval || chan->read_timer_enabled) | |
552 | return; | |
852c2936 | 553 | |
34a91bdb | 554 | chan->read_timer_enabled = 1; |
852c2936 | 555 | |
34a91bdb | 556 | lib_ring_buffer_setup_timer_thread(); |
852c2936 | 557 | |
34a91bdb MD |
558 | sev.sigev_notify = SIGEV_SIGNAL; |
559 | sev.sigev_signo = LTTNG_UST_RB_SIG_READ; | |
560 | sev.sigev_value.sival_ptr = chan; | |
561 | ret = timer_create(CLOCKID, &sev, &chan->read_timer); | |
562 | if (ret == -1) { | |
563 | PERROR("timer_create"); | |
564 | } | |
852c2936 | 565 | |
34a91bdb MD |
566 | its.it_value.tv_sec = chan->read_timer_interval / 1000000; |
567 | its.it_value.tv_nsec = chan->read_timer_interval % 1000000; | |
568 | its.it_interval.tv_sec = its.it_value.tv_sec; | |
569 | its.it_interval.tv_nsec = its.it_value.tv_nsec; | |
852c2936 | 570 | |
34a91bdb MD |
571 | ret = timer_settime(chan->read_timer, 0, &its, NULL); |
572 | if (ret == -1) { | |
573 | PERROR("timer_settime"); | |
574 | } | |
852c2936 MD |
575 | } |
576 | ||
34a91bdb MD |
577 | /* |
578 | * Called with ust_lock() held. | |
579 | */ | |
580 | static | |
581 | void lib_ring_buffer_channel_read_timer_stop(struct channel *chan) | |
852c2936 | 582 | { |
4cfec15c | 583 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
34a91bdb MD |
584 | sigset_t pending_set; |
585 | int ret; | |
852c2936 MD |
586 | |
587 | if (config->wakeup != RING_BUFFER_WAKEUP_BY_TIMER | |
34a91bdb | 588 | || !chan->read_timer_interval || !chan->read_timer_enabled) |
852c2936 MD |
589 | return; |
590 | ||
34a91bdb MD |
591 | ret = timer_delete(chan->read_timer); |
592 | if (ret == -1) { | |
593 | PERROR("timer_delete"); | |
594 | } | |
595 | ||
852c2936 MD |
596 | /* |
597 | * do one more check to catch data that has been written in the last | |
598 | * timer period. | |
599 | */ | |
34a91bdb MD |
600 | lib_ring_buffer_channel_do_read(chan); |
601 | ||
602 | ||
603 | /* | |
604 | * Ensure we don't have any signal queued for this channel. | |
605 | */ | |
606 | for (;;) { | |
607 | ret = sigemptyset(&pending_set); | |
608 | if (ret == -1) { | |
609 | PERROR("sigemptyset"); | |
610 | } | |
611 | ret = sigpending(&pending_set); | |
612 | if (ret == -1) { | |
613 | PERROR("sigpending"); | |
614 | } | |
615 | if (!sigismember(&pending_set, LTTNG_UST_RB_SIG_READ)) | |
616 | break; | |
617 | caa_cpu_relax(); | |
852c2936 | 618 | } |
34a91bdb MD |
619 | |
620 | /* | |
621 | * From this point, no new signal handler will be fired that | |
622 | * would try to access "chan". However, we still need to wait | |
623 | * for any currently executing handler to complete. | |
624 | */ | |
625 | cmm_smp_mb(); | |
626 | CMM_STORE_SHARED(timer_signal.qs_done, 0); | |
627 | cmm_smp_mb(); | |
628 | ||
629 | /* | |
630 | * Kill with LTTNG_UST_RB_SIG_TEARDOWN, so signal management | |
631 | * thread wakes up. | |
632 | */ | |
633 | kill(getpid(), LTTNG_UST_RB_SIG_TEARDOWN); | |
634 | ||
635 | while (!CMM_LOAD_SHARED(timer_signal.qs_done)) | |
636 | caa_cpu_relax(); | |
637 | cmm_smp_mb(); | |
638 | ||
639 | chan->read_timer = 0; | |
640 | chan->read_timer_enabled = 0; | |
852c2936 MD |
641 | } |
642 | ||
1d498196 | 643 | static void channel_unregister_notifiers(struct channel *chan, |
38fae1d3 | 644 | struct lttng_ust_shm_handle *handle) |
852c2936 | 645 | { |
4cfec15c | 646 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
647 | int cpu; |
648 | ||
03d2d293 | 649 | lib_ring_buffer_channel_switch_timer_stop(chan); |
34a91bdb | 650 | lib_ring_buffer_channel_read_timer_stop(chan); |
852c2936 MD |
651 | } |
652 | ||
009769ca MD |
653 | static void channel_print_errors(struct channel *chan, |
654 | struct lttng_ust_shm_handle *handle) | |
655 | { | |
656 | const struct lttng_ust_lib_ring_buffer_config *config = | |
657 | &chan->backend.config; | |
658 | int cpu; | |
659 | ||
660 | if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) { | |
661 | for_each_possible_cpu(cpu) { | |
662 | struct lttng_ust_lib_ring_buffer *buf = | |
663 | shmp(handle, chan->backend.buf[cpu].shmp); | |
664 | lib_ring_buffer_print_errors(chan, buf, cpu, handle); | |
665 | } | |
666 | } else { | |
667 | struct lttng_ust_lib_ring_buffer *buf = | |
668 | shmp(handle, chan->backend.buf[0].shmp); | |
669 | ||
670 | lib_ring_buffer_print_errors(chan, buf, -1, handle); | |
671 | } | |
672 | } | |
673 | ||
674 | static void channel_free(struct channel *chan, | |
675 | struct lttng_ust_shm_handle *handle) | |
852c2936 | 676 | { |
74d81a6c | 677 | channel_backend_free(&chan->backend, handle); |
431d5cf0 | 678 | /* chan is freed by shm teardown */ |
1d498196 MD |
679 | shm_object_table_destroy(handle->table); |
680 | free(handle); | |
852c2936 MD |
681 | } |
682 | ||
683 | /** | |
684 | * channel_create - Create channel. | |
685 | * @config: ring buffer instance configuration | |
686 | * @name: name of the channel | |
a3f61e7f MD |
687 | * @priv_data: ring buffer client private data area pointer (output) |
688 | * @priv_data_size: length, in bytes, of the private data area. | |
d028eddb | 689 | * @priv_data_init: initialization data for private data. |
852c2936 MD |
690 | * @buf_addr: pointer the the beginning of the preallocated buffer contiguous |
691 | * address mapping. It is used only by RING_BUFFER_STATIC | |
692 | * configuration. It can be set to NULL for other backends. | |
693 | * @subbuf_size: subbuffer size | |
694 | * @num_subbuf: number of subbuffers | |
695 | * @switch_timer_interval: Time interval (in us) to fill sub-buffers with | |
696 | * padding to let readers get those sub-buffers. | |
697 | * Used for live streaming. | |
698 | * @read_timer_interval: Time interval (in us) to wake up pending readers. | |
699 | * | |
700 | * Holds cpu hotplug. | |
701 | * Returns NULL on failure. | |
702 | */ | |
4cfec15c | 703 | struct lttng_ust_shm_handle *channel_create(const struct lttng_ust_lib_ring_buffer_config *config, |
a3f61e7f MD |
704 | const char *name, |
705 | void **priv_data, | |
706 | size_t priv_data_align, | |
707 | size_t priv_data_size, | |
d028eddb | 708 | void *priv_data_init, |
a3f61e7f | 709 | void *buf_addr, size_t subbuf_size, |
852c2936 | 710 | size_t num_subbuf, unsigned int switch_timer_interval, |
74d81a6c | 711 | unsigned int read_timer_interval) |
852c2936 | 712 | { |
1d498196 | 713 | int ret, cpu; |
a3f61e7f | 714 | size_t shmsize, chansize; |
852c2936 | 715 | struct channel *chan; |
38fae1d3 | 716 | struct lttng_ust_shm_handle *handle; |
1d498196 | 717 | struct shm_object *shmobj; |
74d81a6c MD |
718 | unsigned int nr_streams; |
719 | ||
720 | if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) | |
721 | nr_streams = num_possible_cpus(); | |
722 | else | |
723 | nr_streams = 1; | |
852c2936 MD |
724 | |
725 | if (lib_ring_buffer_check_config(config, switch_timer_interval, | |
726 | read_timer_interval)) | |
727 | return NULL; | |
728 | ||
38fae1d3 | 729 | handle = zmalloc(sizeof(struct lttng_ust_shm_handle)); |
431d5cf0 MD |
730 | if (!handle) |
731 | return NULL; | |
732 | ||
1d498196 MD |
733 | /* Allocate table for channel + per-cpu buffers */ |
734 | handle->table = shm_object_table_create(1 + num_possible_cpus()); | |
735 | if (!handle->table) | |
736 | goto error_table_alloc; | |
852c2936 | 737 | |
1d498196 MD |
738 | /* Calculate the shm allocation layout */ |
739 | shmsize = sizeof(struct channel); | |
c1fca457 | 740 | shmsize += offset_align(shmsize, __alignof__(struct lttng_ust_lib_ring_buffer_shmp)); |
74d81a6c | 741 | shmsize += sizeof(struct lttng_ust_lib_ring_buffer_shmp) * nr_streams; |
a3f61e7f | 742 | chansize = shmsize; |
74d81a6c MD |
743 | if (priv_data_align) |
744 | shmsize += offset_align(shmsize, priv_data_align); | |
a3f61e7f | 745 | shmsize += priv_data_size; |
a6352fd4 | 746 | |
74d81a6c MD |
747 | /* Allocate normal memory for channel (not shared) */ |
748 | shmobj = shm_object_table_alloc(handle->table, shmsize, SHM_OBJECT_MEM); | |
b5a14697 MD |
749 | if (!shmobj) |
750 | goto error_append; | |
57773204 | 751 | /* struct channel is at object 0, offset 0 (hardcoded) */ |
a3f61e7f | 752 | set_shmp(handle->chan, zalloc_shm(shmobj, chansize)); |
57773204 MD |
753 | assert(handle->chan._ref.index == 0); |
754 | assert(handle->chan._ref.offset == 0); | |
1d498196 | 755 | chan = shmp(handle, handle->chan); |
a6352fd4 | 756 | if (!chan) |
1d498196 | 757 | goto error_append; |
74d81a6c | 758 | chan->nr_streams = nr_streams; |
a6352fd4 | 759 | |
a3f61e7f MD |
760 | /* space for private data */ |
761 | if (priv_data_size) { | |
762 | DECLARE_SHMP(void, priv_data_alloc); | |
763 | ||
764 | align_shm(shmobj, priv_data_align); | |
765 | chan->priv_data_offset = shmobj->allocated_len; | |
766 | set_shmp(priv_data_alloc, zalloc_shm(shmobj, priv_data_size)); | |
767 | if (!shmp(handle, priv_data_alloc)) | |
768 | goto error_append; | |
769 | *priv_data = channel_get_private(chan); | |
d028eddb | 770 | memcpy(*priv_data, priv_data_init, priv_data_size); |
a3f61e7f MD |
771 | } else { |
772 | chan->priv_data_offset = -1; | |
74d81a6c MD |
773 | if (priv_data) |
774 | *priv_data = NULL; | |
a3f61e7f MD |
775 | } |
776 | ||
777 | ret = channel_backend_init(&chan->backend, name, config, | |
1d498196 | 778 | subbuf_size, num_subbuf, handle); |
852c2936 | 779 | if (ret) |
1d498196 | 780 | goto error_backend_init; |
852c2936 | 781 | |
03d2d293 | 782 | chan->handle = handle; |
852c2936 | 783 | chan->commit_count_mask = (~0UL >> chan->backend.num_subbuf_order); |
852c2936 | 784 | |
34a91bdb MD |
785 | chan->switch_timer_interval = switch_timer_interval; |
786 | chan->read_timer_interval = read_timer_interval; | |
03d2d293 | 787 | lib_ring_buffer_channel_switch_timer_start(chan); |
34a91bdb | 788 | lib_ring_buffer_channel_read_timer_start(chan); |
852c2936 | 789 | |
431d5cf0 | 790 | return handle; |
852c2936 | 791 | |
1d498196 MD |
792 | error_backend_init: |
793 | error_append: | |
794 | shm_object_table_destroy(handle->table); | |
795 | error_table_alloc: | |
431d5cf0 | 796 | free(handle); |
852c2936 MD |
797 | return NULL; |
798 | } | |
852c2936 | 799 | |
74d81a6c | 800 | struct lttng_ust_shm_handle *channel_handle_create(void *data, |
ff0f5728 MD |
801 | uint64_t memory_map_size, |
802 | int wakeup_fd) | |
193183fb | 803 | { |
38fae1d3 | 804 | struct lttng_ust_shm_handle *handle; |
193183fb MD |
805 | struct shm_object *object; |
806 | ||
38fae1d3 | 807 | handle = zmalloc(sizeof(struct lttng_ust_shm_handle)); |
193183fb MD |
808 | if (!handle) |
809 | return NULL; | |
810 | ||
811 | /* Allocate table for channel + per-cpu buffers */ | |
812 | handle->table = shm_object_table_create(1 + num_possible_cpus()); | |
813 | if (!handle->table) | |
814 | goto error_table_alloc; | |
815 | /* Add channel object */ | |
74d81a6c | 816 | object = shm_object_table_append_mem(handle->table, data, |
ff0f5728 | 817 | memory_map_size, wakeup_fd); |
193183fb MD |
818 | if (!object) |
819 | goto error_table_object; | |
57773204 MD |
820 | /* struct channel is at object 0, offset 0 (hardcoded) */ |
821 | handle->chan._ref.index = 0; | |
822 | handle->chan._ref.offset = 0; | |
193183fb MD |
823 | return handle; |
824 | ||
825 | error_table_object: | |
826 | shm_object_table_destroy(handle->table); | |
827 | error_table_alloc: | |
828 | free(handle); | |
829 | return NULL; | |
830 | } | |
831 | ||
38fae1d3 | 832 | int channel_handle_add_stream(struct lttng_ust_shm_handle *handle, |
74d81a6c MD |
833 | int shm_fd, int wakeup_fd, uint32_t stream_nr, |
834 | uint64_t memory_map_size) | |
193183fb MD |
835 | { |
836 | struct shm_object *object; | |
837 | ||
838 | /* Add stream object */ | |
74d81a6c MD |
839 | object = shm_object_table_append_shm(handle->table, |
840 | shm_fd, wakeup_fd, stream_nr, | |
841 | memory_map_size); | |
193183fb | 842 | if (!object) |
74d81a6c | 843 | return -EINVAL; |
193183fb MD |
844 | return 0; |
845 | } | |
846 | ||
74d81a6c MD |
847 | unsigned int channel_handle_get_nr_streams(struct lttng_ust_shm_handle *handle) |
848 | { | |
849 | assert(handle->table); | |
850 | return handle->table->allocated_len - 1; | |
851 | } | |
852 | ||
852c2936 | 853 | static |
74d81a6c | 854 | void channel_release(struct channel *chan, struct lttng_ust_shm_handle *handle) |
852c2936 | 855 | { |
74d81a6c | 856 | channel_free(chan, handle); |
852c2936 MD |
857 | } |
858 | ||
859 | /** | |
860 | * channel_destroy - Finalize, wait for q.s. and destroy channel. | |
861 | * @chan: channel to destroy | |
862 | * | |
863 | * Holds cpu hotplug. | |
431d5cf0 MD |
864 | * Call "destroy" callback, finalize channels, decrement the channel |
865 | * reference count. Note that when readers have completed data | |
866 | * consumption of finalized channels, get_subbuf() will return -ENODATA. | |
a3f61e7f | 867 | * They should release their handle at that point. |
852c2936 | 868 | */ |
a3f61e7f | 869 | void channel_destroy(struct channel *chan, struct lttng_ust_shm_handle *handle, |
74d81a6c | 870 | int consumer) |
852c2936 | 871 | { |
74d81a6c MD |
872 | if (consumer) { |
873 | /* | |
874 | * Note: the consumer takes care of finalizing and | |
875 | * switching the buffers. | |
876 | */ | |
877 | channel_unregister_notifiers(chan, handle); | |
3d0ef9f6 MD |
878 | /* |
879 | * The consumer prints errors. | |
880 | */ | |
881 | channel_print_errors(chan, handle); | |
824f40b8 MD |
882 | } |
883 | ||
431d5cf0 MD |
884 | /* |
885 | * sessiond/consumer are keeping a reference on the shm file | |
886 | * descriptor directly. No need to refcount. | |
887 | */ | |
74d81a6c | 888 | channel_release(chan, handle); |
a3f61e7f | 889 | return; |
852c2936 | 890 | } |
852c2936 | 891 | |
4cfec15c MD |
892 | struct lttng_ust_lib_ring_buffer *channel_get_ring_buffer( |
893 | const struct lttng_ust_lib_ring_buffer_config *config, | |
1d498196 | 894 | struct channel *chan, int cpu, |
38fae1d3 | 895 | struct lttng_ust_shm_handle *handle, |
74d81a6c MD |
896 | int *shm_fd, int *wait_fd, |
897 | int *wakeup_fd, | |
898 | uint64_t *memory_map_size) | |
852c2936 | 899 | { |
381c0f1e MD |
900 | struct shm_ref *ref; |
901 | ||
902 | if (config->alloc == RING_BUFFER_ALLOC_GLOBAL) { | |
74d81a6c | 903 | cpu = 0; |
381c0f1e | 904 | } else { |
e095d803 MD |
905 | if (cpu >= num_possible_cpus()) |
906 | return NULL; | |
381c0f1e | 907 | } |
74d81a6c MD |
908 | ref = &chan->backend.buf[cpu].shmp._ref; |
909 | *shm_fd = shm_get_shm_fd(handle, ref); | |
910 | *wait_fd = shm_get_wait_fd(handle, ref); | |
911 | *wakeup_fd = shm_get_wakeup_fd(handle, ref); | |
912 | if (shm_get_shm_size(handle, ref, memory_map_size)) | |
913 | return NULL; | |
914 | return shmp(handle, chan->backend.buf[cpu].shmp); | |
852c2936 | 915 | } |
852c2936 | 916 | |
ff0f5728 MD |
917 | int ring_buffer_channel_close_wait_fd(const struct lttng_ust_lib_ring_buffer_config *config, |
918 | struct channel *chan, | |
919 | struct lttng_ust_shm_handle *handle) | |
920 | { | |
921 | struct shm_ref *ref; | |
922 | ||
923 | ref = &handle->chan._ref; | |
924 | return shm_close_wait_fd(handle, ref); | |
925 | } | |
926 | ||
927 | int ring_buffer_channel_close_wakeup_fd(const struct lttng_ust_lib_ring_buffer_config *config, | |
928 | struct channel *chan, | |
929 | struct lttng_ust_shm_handle *handle) | |
930 | { | |
931 | struct shm_ref *ref; | |
932 | ||
933 | ref = &handle->chan._ref; | |
934 | return shm_close_wakeup_fd(handle, ref); | |
935 | } | |
936 | ||
937 | int ring_buffer_stream_close_wait_fd(const struct lttng_ust_lib_ring_buffer_config *config, | |
74d81a6c MD |
938 | struct channel *chan, |
939 | struct lttng_ust_shm_handle *handle, | |
940 | int cpu) | |
852c2936 | 941 | { |
74d81a6c MD |
942 | struct shm_ref *ref; |
943 | ||
944 | if (config->alloc == RING_BUFFER_ALLOC_GLOBAL) { | |
945 | cpu = 0; | |
946 | } else { | |
947 | if (cpu >= num_possible_cpus()) | |
948 | return -EINVAL; | |
824f40b8 | 949 | } |
74d81a6c MD |
950 | ref = &chan->backend.buf[cpu].shmp._ref; |
951 | return shm_close_wait_fd(handle, ref); | |
952 | } | |
953 | ||
ff0f5728 | 954 | int ring_buffer_stream_close_wakeup_fd(const struct lttng_ust_lib_ring_buffer_config *config, |
74d81a6c MD |
955 | struct channel *chan, |
956 | struct lttng_ust_shm_handle *handle, | |
957 | int cpu) | |
958 | { | |
959 | struct shm_ref *ref; | |
cb7378b3 | 960 | int ret; |
74d81a6c MD |
961 | |
962 | if (config->alloc == RING_BUFFER_ALLOC_GLOBAL) { | |
963 | cpu = 0; | |
964 | } else { | |
965 | if (cpu >= num_possible_cpus()) | |
966 | return -EINVAL; | |
967 | } | |
968 | ref = &chan->backend.buf[cpu].shmp._ref; | |
cb7378b3 MD |
969 | pthread_mutex_lock(&wakeup_fd_mutex); |
970 | ret = shm_close_wakeup_fd(handle, ref); | |
971 | pthread_mutex_unlock(&wakeup_fd_mutex); | |
972 | return ret; | |
74d81a6c MD |
973 | } |
974 | ||
975 | int lib_ring_buffer_open_read(struct lttng_ust_lib_ring_buffer *buf, | |
976 | struct lttng_ust_shm_handle *handle) | |
977 | { | |
a6352fd4 | 978 | if (uatomic_cmpxchg(&buf->active_readers, 0, 1) != 0) |
852c2936 | 979 | return -EBUSY; |
a6352fd4 | 980 | cmm_smp_mb(); |
852c2936 MD |
981 | return 0; |
982 | } | |
852c2936 | 983 | |
4cfec15c | 984 | void lib_ring_buffer_release_read(struct lttng_ust_lib_ring_buffer *buf, |
74d81a6c | 985 | struct lttng_ust_shm_handle *handle) |
852c2936 | 986 | { |
1d498196 | 987 | struct channel *chan = shmp(handle, buf->backend.chan); |
852c2936 | 988 | |
a6352fd4 MD |
989 | CHAN_WARN_ON(chan, uatomic_read(&buf->active_readers) != 1); |
990 | cmm_smp_mb(); | |
991 | uatomic_dec(&buf->active_readers); | |
852c2936 MD |
992 | } |
993 | ||
994 | /** | |
995 | * lib_ring_buffer_snapshot - save subbuffer position snapshot (for read) | |
996 | * @buf: ring buffer | |
997 | * @consumed: consumed count indicating the position where to read | |
998 | * @produced: produced count, indicates position when to stop reading | |
999 | * | |
1000 | * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no | |
1001 | * data to read at consumed position, or 0 if the get operation succeeds. | |
852c2936 MD |
1002 | */ |
1003 | ||
4cfec15c | 1004 | int lib_ring_buffer_snapshot(struct lttng_ust_lib_ring_buffer *buf, |
1d498196 | 1005 | unsigned long *consumed, unsigned long *produced, |
38fae1d3 | 1006 | struct lttng_ust_shm_handle *handle) |
852c2936 | 1007 | { |
1d498196 | 1008 | struct channel *chan = shmp(handle, buf->backend.chan); |
4cfec15c | 1009 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
1010 | unsigned long consumed_cur, write_offset; |
1011 | int finalized; | |
1012 | ||
14641deb | 1013 | finalized = CMM_ACCESS_ONCE(buf->finalized); |
852c2936 MD |
1014 | /* |
1015 | * Read finalized before counters. | |
1016 | */ | |
a6352fd4 MD |
1017 | cmm_smp_rmb(); |
1018 | consumed_cur = uatomic_read(&buf->consumed); | |
852c2936 MD |
1019 | /* |
1020 | * No need to issue a memory barrier between consumed count read and | |
1021 | * write offset read, because consumed count can only change | |
1022 | * concurrently in overwrite mode, and we keep a sequence counter | |
1023 | * identifier derived from the write offset to check we are getting | |
1024 | * the same sub-buffer we are expecting (the sub-buffers are atomically | |
1025 | * "tagged" upon writes, tags are checked upon read). | |
1026 | */ | |
1027 | write_offset = v_read(config, &buf->offset); | |
1028 | ||
1029 | /* | |
1030 | * Check that we are not about to read the same subbuffer in | |
1031 | * which the writer head is. | |
1032 | */ | |
1033 | if (subbuf_trunc(write_offset, chan) - subbuf_trunc(consumed_cur, chan) | |
1034 | == 0) | |
1035 | goto nodata; | |
1036 | ||
1037 | *consumed = consumed_cur; | |
1038 | *produced = subbuf_trunc(write_offset, chan); | |
1039 | ||
1040 | return 0; | |
1041 | ||
1042 | nodata: | |
1043 | /* | |
1044 | * The memory barriers __wait_event()/wake_up_interruptible() take care | |
1045 | * of "raw_spin_is_locked" memory ordering. | |
1046 | */ | |
1047 | if (finalized) | |
1048 | return -ENODATA; | |
852c2936 MD |
1049 | else |
1050 | return -EAGAIN; | |
1051 | } | |
852c2936 MD |
1052 | |
1053 | /** | |
1054 | * lib_ring_buffer_put_snapshot - move consumed counter forward | |
1055 | * @buf: ring buffer | |
1056 | * @consumed_new: new consumed count value | |
1057 | */ | |
4cfec15c | 1058 | void lib_ring_buffer_move_consumer(struct lttng_ust_lib_ring_buffer *buf, |
1d498196 | 1059 | unsigned long consumed_new, |
38fae1d3 | 1060 | struct lttng_ust_shm_handle *handle) |
852c2936 | 1061 | { |
4cfec15c | 1062 | struct lttng_ust_lib_ring_buffer_backend *bufb = &buf->backend; |
1d498196 | 1063 | struct channel *chan = shmp(handle, bufb->chan); |
852c2936 MD |
1064 | unsigned long consumed; |
1065 | ||
74d81a6c | 1066 | CHAN_WARN_ON(chan, uatomic_read(&buf->active_readers) != 1); |
852c2936 MD |
1067 | |
1068 | /* | |
1069 | * Only push the consumed value forward. | |
1070 | * If the consumed cmpxchg fails, this is because we have been pushed by | |
1071 | * the writer in flight recorder mode. | |
1072 | */ | |
a6352fd4 | 1073 | consumed = uatomic_read(&buf->consumed); |
852c2936 | 1074 | while ((long) consumed - (long) consumed_new < 0) |
a6352fd4 MD |
1075 | consumed = uatomic_cmpxchg(&buf->consumed, consumed, |
1076 | consumed_new); | |
852c2936 | 1077 | } |
852c2936 MD |
1078 | |
1079 | /** | |
1080 | * lib_ring_buffer_get_subbuf - get exclusive access to subbuffer for reading | |
1081 | * @buf: ring buffer | |
1082 | * @consumed: consumed count indicating the position where to read | |
1083 | * | |
1084 | * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no | |
1085 | * data to read at consumed position, or 0 if the get operation succeeds. | |
852c2936 | 1086 | */ |
4cfec15c | 1087 | int lib_ring_buffer_get_subbuf(struct lttng_ust_lib_ring_buffer *buf, |
1d498196 | 1088 | unsigned long consumed, |
38fae1d3 | 1089 | struct lttng_ust_shm_handle *handle) |
852c2936 | 1090 | { |
1d498196 | 1091 | struct channel *chan = shmp(handle, buf->backend.chan); |
4cfec15c | 1092 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
1093 | unsigned long consumed_cur, consumed_idx, commit_count, write_offset; |
1094 | int ret; | |
1095 | int finalized; | |
1096 | ||
1097 | retry: | |
14641deb | 1098 | finalized = CMM_ACCESS_ONCE(buf->finalized); |
852c2936 MD |
1099 | /* |
1100 | * Read finalized before counters. | |
1101 | */ | |
a6352fd4 MD |
1102 | cmm_smp_rmb(); |
1103 | consumed_cur = uatomic_read(&buf->consumed); | |
852c2936 | 1104 | consumed_idx = subbuf_index(consumed, chan); |
4746ae29 | 1105 | commit_count = v_read(config, &shmp_index(handle, buf->commit_cold, consumed_idx)->cc_sb); |
852c2936 MD |
1106 | /* |
1107 | * Make sure we read the commit count before reading the buffer | |
1108 | * data and the write offset. Correct consumed offset ordering | |
1109 | * wrt commit count is insured by the use of cmpxchg to update | |
1110 | * the consumed offset. | |
852c2936 | 1111 | */ |
a6352fd4 MD |
1112 | /* |
1113 | * Local rmb to match the remote wmb to read the commit count | |
1114 | * before the buffer data and the write offset. | |
1115 | */ | |
1116 | cmm_smp_rmb(); | |
852c2936 MD |
1117 | |
1118 | write_offset = v_read(config, &buf->offset); | |
1119 | ||
1120 | /* | |
1121 | * Check that the buffer we are getting is after or at consumed_cur | |
1122 | * position. | |
1123 | */ | |
1124 | if ((long) subbuf_trunc(consumed, chan) | |
1125 | - (long) subbuf_trunc(consumed_cur, chan) < 0) | |
1126 | goto nodata; | |
1127 | ||
1128 | /* | |
1129 | * Check that the subbuffer we are trying to consume has been | |
1130 | * already fully committed. | |
1131 | */ | |
1132 | if (((commit_count - chan->backend.subbuf_size) | |
1133 | & chan->commit_count_mask) | |
1134 | - (buf_trunc(consumed_cur, chan) | |
1135 | >> chan->backend.num_subbuf_order) | |
1136 | != 0) | |
1137 | goto nodata; | |
1138 | ||
1139 | /* | |
1140 | * Check that we are not about to read the same subbuffer in | |
1141 | * which the writer head is. | |
1142 | */ | |
1143 | if (subbuf_trunc(write_offset, chan) - subbuf_trunc(consumed_cur, chan) | |
1144 | == 0) | |
1145 | goto nodata; | |
1146 | ||
1147 | /* | |
1148 | * Failure to get the subbuffer causes a busy-loop retry without going | |
1149 | * to a wait queue. These are caused by short-lived race windows where | |
1150 | * the writer is getting access to a subbuffer we were trying to get | |
1151 | * access to. Also checks that the "consumed" buffer count we are | |
1152 | * looking for matches the one contained in the subbuffer id. | |
1153 | */ | |
1154 | ret = update_read_sb_index(config, &buf->backend, &chan->backend, | |
1d498196 MD |
1155 | consumed_idx, buf_trunc_val(consumed, chan), |
1156 | handle); | |
852c2936 MD |
1157 | if (ret) |
1158 | goto retry; | |
1159 | subbuffer_id_clear_noref(config, &buf->backend.buf_rsb.id); | |
1160 | ||
1161 | buf->get_subbuf_consumed = consumed; | |
1162 | buf->get_subbuf = 1; | |
1163 | ||
1164 | return 0; | |
1165 | ||
1166 | nodata: | |
1167 | /* | |
1168 | * The memory barriers __wait_event()/wake_up_interruptible() take care | |
1169 | * of "raw_spin_is_locked" memory ordering. | |
1170 | */ | |
1171 | if (finalized) | |
1172 | return -ENODATA; | |
852c2936 MD |
1173 | else |
1174 | return -EAGAIN; | |
1175 | } | |
852c2936 MD |
1176 | |
1177 | /** | |
1178 | * lib_ring_buffer_put_subbuf - release exclusive subbuffer access | |
1179 | * @buf: ring buffer | |
1180 | */ | |
4cfec15c | 1181 | void lib_ring_buffer_put_subbuf(struct lttng_ust_lib_ring_buffer *buf, |
38fae1d3 | 1182 | struct lttng_ust_shm_handle *handle) |
852c2936 | 1183 | { |
4cfec15c | 1184 | struct lttng_ust_lib_ring_buffer_backend *bufb = &buf->backend; |
1d498196 | 1185 | struct channel *chan = shmp(handle, bufb->chan); |
4cfec15c | 1186 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
1187 | unsigned long read_sb_bindex, consumed_idx, consumed; |
1188 | ||
74d81a6c | 1189 | CHAN_WARN_ON(chan, uatomic_read(&buf->active_readers) != 1); |
852c2936 MD |
1190 | |
1191 | if (!buf->get_subbuf) { | |
1192 | /* | |
1193 | * Reader puts a subbuffer it did not get. | |
1194 | */ | |
1195 | CHAN_WARN_ON(chan, 1); | |
1196 | return; | |
1197 | } | |
1198 | consumed = buf->get_subbuf_consumed; | |
1199 | buf->get_subbuf = 0; | |
1200 | ||
1201 | /* | |
1202 | * Clear the records_unread counter. (overruns counter) | |
1203 | * Can still be non-zero if a file reader simply grabbed the data | |
1204 | * without using iterators. | |
1205 | * Can be below zero if an iterator is used on a snapshot more than | |
1206 | * once. | |
1207 | */ | |
1208 | read_sb_bindex = subbuffer_id_get_index(config, bufb->buf_rsb.id); | |
1209 | v_add(config, v_read(config, | |
4746ae29 | 1210 | &shmp(handle, shmp_index(handle, bufb->array, read_sb_bindex)->shmp)->records_unread), |
852c2936 | 1211 | &bufb->records_read); |
4746ae29 | 1212 | v_set(config, &shmp(handle, shmp_index(handle, bufb->array, read_sb_bindex)->shmp)->records_unread, 0); |
852c2936 MD |
1213 | CHAN_WARN_ON(chan, config->mode == RING_BUFFER_OVERWRITE |
1214 | && subbuffer_id_is_noref(config, bufb->buf_rsb.id)); | |
1215 | subbuffer_id_set_noref(config, &bufb->buf_rsb.id); | |
1216 | ||
1217 | /* | |
1218 | * Exchange the reader subbuffer with the one we put in its place in the | |
1219 | * writer subbuffer table. Expect the original consumed count. If | |
1220 | * update_read_sb_index fails, this is because the writer updated the | |
1221 | * subbuffer concurrently. We should therefore keep the subbuffer we | |
1222 | * currently have: it has become invalid to try reading this sub-buffer | |
1223 | * consumed count value anyway. | |
1224 | */ | |
1225 | consumed_idx = subbuf_index(consumed, chan); | |
1226 | update_read_sb_index(config, &buf->backend, &chan->backend, | |
1d498196 MD |
1227 | consumed_idx, buf_trunc_val(consumed, chan), |
1228 | handle); | |
852c2936 MD |
1229 | /* |
1230 | * update_read_sb_index return value ignored. Don't exchange sub-buffer | |
1231 | * if the writer concurrently updated it. | |
1232 | */ | |
1233 | } | |
852c2936 MD |
1234 | |
1235 | /* | |
1236 | * cons_offset is an iterator on all subbuffer offsets between the reader | |
1237 | * position and the writer position. (inclusive) | |
1238 | */ | |
1239 | static | |
4cfec15c | 1240 | void lib_ring_buffer_print_subbuffer_errors(struct lttng_ust_lib_ring_buffer *buf, |
852c2936 MD |
1241 | struct channel *chan, |
1242 | unsigned long cons_offset, | |
1d498196 | 1243 | int cpu, |
38fae1d3 | 1244 | struct lttng_ust_shm_handle *handle) |
852c2936 | 1245 | { |
4cfec15c | 1246 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
1247 | unsigned long cons_idx, commit_count, commit_count_sb; |
1248 | ||
1249 | cons_idx = subbuf_index(cons_offset, chan); | |
4746ae29 MD |
1250 | commit_count = v_read(config, &shmp_index(handle, buf->commit_hot, cons_idx)->cc); |
1251 | commit_count_sb = v_read(config, &shmp_index(handle, buf->commit_cold, cons_idx)->cc_sb); | |
852c2936 MD |
1252 | |
1253 | if (subbuf_offset(commit_count, chan) != 0) | |
4d3c9523 | 1254 | DBG("ring buffer %s, cpu %d: " |
852c2936 MD |
1255 | "commit count in subbuffer %lu,\n" |
1256 | "expecting multiples of %lu bytes\n" | |
1257 | " [ %lu bytes committed, %lu bytes reader-visible ]\n", | |
1258 | chan->backend.name, cpu, cons_idx, | |
1259 | chan->backend.subbuf_size, | |
1260 | commit_count, commit_count_sb); | |
1261 | ||
4d3c9523 | 1262 | DBG("ring buffer: %s, cpu %d: %lu bytes committed\n", |
852c2936 MD |
1263 | chan->backend.name, cpu, commit_count); |
1264 | } | |
1265 | ||
1266 | static | |
4cfec15c | 1267 | void lib_ring_buffer_print_buffer_errors(struct lttng_ust_lib_ring_buffer *buf, |
852c2936 | 1268 | struct channel *chan, |
1d498196 | 1269 | void *priv, int cpu, |
38fae1d3 | 1270 | struct lttng_ust_shm_handle *handle) |
852c2936 | 1271 | { |
4cfec15c | 1272 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
1273 | unsigned long write_offset, cons_offset; |
1274 | ||
852c2936 MD |
1275 | /* |
1276 | * No need to order commit_count, write_offset and cons_offset reads | |
1277 | * because we execute at teardown when no more writer nor reader | |
1278 | * references are left. | |
1279 | */ | |
1280 | write_offset = v_read(config, &buf->offset); | |
a6352fd4 | 1281 | cons_offset = uatomic_read(&buf->consumed); |
852c2936 | 1282 | if (write_offset != cons_offset) |
4d3c9523 | 1283 | DBG("ring buffer %s, cpu %d: " |
852c2936 MD |
1284 | "non-consumed data\n" |
1285 | " [ %lu bytes written, %lu bytes read ]\n", | |
1286 | chan->backend.name, cpu, write_offset, cons_offset); | |
1287 | ||
a6352fd4 | 1288 | for (cons_offset = uatomic_read(&buf->consumed); |
852c2936 MD |
1289 | (long) (subbuf_trunc((unsigned long) v_read(config, &buf->offset), |
1290 | chan) | |
1291 | - cons_offset) > 0; | |
1292 | cons_offset = subbuf_align(cons_offset, chan)) | |
1293 | lib_ring_buffer_print_subbuffer_errors(buf, chan, cons_offset, | |
1d498196 | 1294 | cpu, handle); |
852c2936 MD |
1295 | } |
1296 | ||
1297 | static | |
1298 | void lib_ring_buffer_print_errors(struct channel *chan, | |
009769ca MD |
1299 | struct lttng_ust_lib_ring_buffer *buf, int cpu, |
1300 | struct lttng_ust_shm_handle *handle) | |
852c2936 | 1301 | { |
4cfec15c | 1302 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
a3f61e7f | 1303 | void *priv = channel_get_private(chan); |
852c2936 | 1304 | |
a1360615 MD |
1305 | if (!strcmp(chan->backend.name, "relay-metadata-mmap")) { |
1306 | DBG("ring buffer %s: %lu records written, " | |
1307 | "%lu records overrun\n", | |
1308 | chan->backend.name, | |
1309 | v_read(config, &buf->records_count), | |
1310 | v_read(config, &buf->records_overrun)); | |
1311 | } else { | |
1312 | DBG("ring buffer %s, cpu %d: %lu records written, " | |
1313 | "%lu records overrun\n", | |
1314 | chan->backend.name, cpu, | |
1315 | v_read(config, &buf->records_count), | |
1316 | v_read(config, &buf->records_overrun)); | |
1317 | ||
1318 | if (v_read(config, &buf->records_lost_full) | |
1319 | || v_read(config, &buf->records_lost_wrap) | |
1320 | || v_read(config, &buf->records_lost_big)) | |
1321 | DBG("ring buffer %s, cpu %d: records were lost. Caused by:\n" | |
1322 | " [ %lu buffer full, %lu nest buffer wrap-around, " | |
1323 | "%lu event too big ]\n", | |
1324 | chan->backend.name, cpu, | |
1325 | v_read(config, &buf->records_lost_full), | |
1326 | v_read(config, &buf->records_lost_wrap), | |
1327 | v_read(config, &buf->records_lost_big)); | |
1328 | } | |
1d498196 | 1329 | lib_ring_buffer_print_buffer_errors(buf, chan, priv, cpu, handle); |
852c2936 MD |
1330 | } |
1331 | ||
1332 | /* | |
1333 | * lib_ring_buffer_switch_old_start: Populate old subbuffer header. | |
1334 | * | |
1335 | * Only executed when the buffer is finalized, in SWITCH_FLUSH. | |
1336 | */ | |
1337 | static | |
4cfec15c | 1338 | void lib_ring_buffer_switch_old_start(struct lttng_ust_lib_ring_buffer *buf, |
852c2936 MD |
1339 | struct channel *chan, |
1340 | struct switch_offsets *offsets, | |
2fed87ae | 1341 | uint64_t tsc, |
38fae1d3 | 1342 | struct lttng_ust_shm_handle *handle) |
852c2936 | 1343 | { |
4cfec15c | 1344 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
1345 | unsigned long oldidx = subbuf_index(offsets->old, chan); |
1346 | unsigned long commit_count; | |
1347 | ||
1d498196 | 1348 | config->cb.buffer_begin(buf, tsc, oldidx, handle); |
852c2936 MD |
1349 | |
1350 | /* | |
1351 | * Order all writes to buffer before the commit count update that will | |
1352 | * determine that the subbuffer is full. | |
1353 | */ | |
a6352fd4 | 1354 | cmm_smp_wmb(); |
852c2936 | 1355 | v_add(config, config->cb.subbuffer_header_size(), |
4746ae29 MD |
1356 | &shmp_index(handle, buf->commit_hot, oldidx)->cc); |
1357 | commit_count = v_read(config, &shmp_index(handle, buf->commit_hot, oldidx)->cc); | |
852c2936 MD |
1358 | /* Check if the written buffer has to be delivered */ |
1359 | lib_ring_buffer_check_deliver(config, buf, chan, offsets->old, | |
1d498196 | 1360 | commit_count, oldidx, handle); |
852c2936 MD |
1361 | lib_ring_buffer_write_commit_counter(config, buf, chan, oldidx, |
1362 | offsets->old, commit_count, | |
1d498196 MD |
1363 | config->cb.subbuffer_header_size(), |
1364 | handle); | |
852c2936 MD |
1365 | } |
1366 | ||
1367 | /* | |
1368 | * lib_ring_buffer_switch_old_end: switch old subbuffer | |
1369 | * | |
1370 | * Note : offset_old should never be 0 here. It is ok, because we never perform | |
1371 | * buffer switch on an empty subbuffer in SWITCH_ACTIVE mode. The caller | |
1372 | * increments the offset_old value when doing a SWITCH_FLUSH on an empty | |
1373 | * subbuffer. | |
1374 | */ | |
1375 | static | |
4cfec15c | 1376 | void lib_ring_buffer_switch_old_end(struct lttng_ust_lib_ring_buffer *buf, |
852c2936 MD |
1377 | struct channel *chan, |
1378 | struct switch_offsets *offsets, | |
2fed87ae | 1379 | uint64_t tsc, |
38fae1d3 | 1380 | struct lttng_ust_shm_handle *handle) |
852c2936 | 1381 | { |
4cfec15c | 1382 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
1383 | unsigned long oldidx = subbuf_index(offsets->old - 1, chan); |
1384 | unsigned long commit_count, padding_size, data_size; | |
1385 | ||
1386 | data_size = subbuf_offset(offsets->old - 1, chan) + 1; | |
1387 | padding_size = chan->backend.subbuf_size - data_size; | |
1d498196 MD |
1388 | subbuffer_set_data_size(config, &buf->backend, oldidx, data_size, |
1389 | handle); | |
852c2936 MD |
1390 | |
1391 | /* | |
1392 | * Order all writes to buffer before the commit count update that will | |
1393 | * determine that the subbuffer is full. | |
1394 | */ | |
a6352fd4 | 1395 | cmm_smp_wmb(); |
4746ae29 MD |
1396 | v_add(config, padding_size, &shmp_index(handle, buf->commit_hot, oldidx)->cc); |
1397 | commit_count = v_read(config, &shmp_index(handle, buf->commit_hot, oldidx)->cc); | |
852c2936 | 1398 | lib_ring_buffer_check_deliver(config, buf, chan, offsets->old - 1, |
1d498196 | 1399 | commit_count, oldidx, handle); |
852c2936 MD |
1400 | lib_ring_buffer_write_commit_counter(config, buf, chan, oldidx, |
1401 | offsets->old, commit_count, | |
1d498196 | 1402 | padding_size, handle); |
852c2936 MD |
1403 | } |
1404 | ||
1405 | /* | |
1406 | * lib_ring_buffer_switch_new_start: Populate new subbuffer. | |
1407 | * | |
1408 | * This code can be executed unordered : writers may already have written to the | |
1409 | * sub-buffer before this code gets executed, caution. The commit makes sure | |
1410 | * that this code is executed before the deliver of this sub-buffer. | |
1411 | */ | |
1412 | static | |
4cfec15c | 1413 | void lib_ring_buffer_switch_new_start(struct lttng_ust_lib_ring_buffer *buf, |
852c2936 MD |
1414 | struct channel *chan, |
1415 | struct switch_offsets *offsets, | |
2fed87ae | 1416 | uint64_t tsc, |
38fae1d3 | 1417 | struct lttng_ust_shm_handle *handle) |
852c2936 | 1418 | { |
4cfec15c | 1419 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
1420 | unsigned long beginidx = subbuf_index(offsets->begin, chan); |
1421 | unsigned long commit_count; | |
1422 | ||
1d498196 | 1423 | config->cb.buffer_begin(buf, tsc, beginidx, handle); |
852c2936 MD |
1424 | |
1425 | /* | |
1426 | * Order all writes to buffer before the commit count update that will | |
1427 | * determine that the subbuffer is full. | |
1428 | */ | |
a6352fd4 | 1429 | cmm_smp_wmb(); |
852c2936 | 1430 | v_add(config, config->cb.subbuffer_header_size(), |
4746ae29 MD |
1431 | &shmp_index(handle, buf->commit_hot, beginidx)->cc); |
1432 | commit_count = v_read(config, &shmp_index(handle, buf->commit_hot, beginidx)->cc); | |
852c2936 MD |
1433 | /* Check if the written buffer has to be delivered */ |
1434 | lib_ring_buffer_check_deliver(config, buf, chan, offsets->begin, | |
1d498196 | 1435 | commit_count, beginidx, handle); |
852c2936 MD |
1436 | lib_ring_buffer_write_commit_counter(config, buf, chan, beginidx, |
1437 | offsets->begin, commit_count, | |
1d498196 MD |
1438 | config->cb.subbuffer_header_size(), |
1439 | handle); | |
852c2936 MD |
1440 | } |
1441 | ||
1442 | /* | |
1443 | * lib_ring_buffer_switch_new_end: finish switching current subbuffer | |
1444 | * | |
1445 | * The only remaining threads could be the ones with pending commits. They will | |
1446 | * have to do the deliver themselves. | |
1447 | */ | |
1448 | static | |
4cfec15c | 1449 | void lib_ring_buffer_switch_new_end(struct lttng_ust_lib_ring_buffer *buf, |
1d498196 MD |
1450 | struct channel *chan, |
1451 | struct switch_offsets *offsets, | |
2fed87ae | 1452 | uint64_t tsc, |
38fae1d3 | 1453 | struct lttng_ust_shm_handle *handle) |
852c2936 | 1454 | { |
4cfec15c | 1455 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
1456 | unsigned long endidx = subbuf_index(offsets->end - 1, chan); |
1457 | unsigned long commit_count, padding_size, data_size; | |
1458 | ||
1459 | data_size = subbuf_offset(offsets->end - 1, chan) + 1; | |
1460 | padding_size = chan->backend.subbuf_size - data_size; | |
1d498196 MD |
1461 | subbuffer_set_data_size(config, &buf->backend, endidx, data_size, |
1462 | handle); | |
852c2936 MD |
1463 | |
1464 | /* | |
1465 | * Order all writes to buffer before the commit count update that will | |
1466 | * determine that the subbuffer is full. | |
1467 | */ | |
a6352fd4 | 1468 | cmm_smp_wmb(); |
4746ae29 MD |
1469 | v_add(config, padding_size, &shmp_index(handle, buf->commit_hot, endidx)->cc); |
1470 | commit_count = v_read(config, &shmp_index(handle, buf->commit_hot, endidx)->cc); | |
852c2936 | 1471 | lib_ring_buffer_check_deliver(config, buf, chan, offsets->end - 1, |
1d498196 | 1472 | commit_count, endidx, handle); |
852c2936 MD |
1473 | lib_ring_buffer_write_commit_counter(config, buf, chan, endidx, |
1474 | offsets->end, commit_count, | |
1d498196 | 1475 | padding_size, handle); |
852c2936 MD |
1476 | } |
1477 | ||
1478 | /* | |
1479 | * Returns : | |
1480 | * 0 if ok | |
1481 | * !0 if execution must be aborted. | |
1482 | */ | |
1483 | static | |
1484 | int lib_ring_buffer_try_switch_slow(enum switch_mode mode, | |
4cfec15c | 1485 | struct lttng_ust_lib_ring_buffer *buf, |
852c2936 MD |
1486 | struct channel *chan, |
1487 | struct switch_offsets *offsets, | |
2fed87ae | 1488 | uint64_t *tsc) |
852c2936 | 1489 | { |
4cfec15c | 1490 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
1491 | unsigned long off; |
1492 | ||
1493 | offsets->begin = v_read(config, &buf->offset); | |
1494 | offsets->old = offsets->begin; | |
1495 | offsets->switch_old_start = 0; | |
1496 | off = subbuf_offset(offsets->begin, chan); | |
1497 | ||
1498 | *tsc = config->cb.ring_buffer_clock_read(chan); | |
1499 | ||
1500 | /* | |
1501 | * Ensure we flush the header of an empty subbuffer when doing the | |
1502 | * finalize (SWITCH_FLUSH). This ensures that we end up knowing the | |
1503 | * total data gathering duration even if there were no records saved | |
1504 | * after the last buffer switch. | |
1505 | * In SWITCH_ACTIVE mode, switch the buffer when it contains events. | |
1506 | * SWITCH_ACTIVE only flushes the current subbuffer, dealing with end of | |
1507 | * subbuffer header as appropriate. | |
1508 | * The next record that reserves space will be responsible for | |
1509 | * populating the following subbuffer header. We choose not to populate | |
1510 | * the next subbuffer header here because we want to be able to use | |
a6352fd4 MD |
1511 | * SWITCH_ACTIVE for periodical buffer flush, which must |
1512 | * guarantee that all the buffer content (records and header | |
1513 | * timestamps) are visible to the reader. This is required for | |
1514 | * quiescence guarantees for the fusion merge. | |
852c2936 MD |
1515 | */ |
1516 | if (mode == SWITCH_FLUSH || off > 0) { | |
b5a3dfa5 | 1517 | if (caa_unlikely(off == 0)) { |
c352d3a4 MD |
1518 | /* |
1519 | * A final flush that encounters an empty | |
1520 | * sub-buffer cannot switch buffer if a | |
1521 | * reader is located within this sub-buffer. | |
1522 | * Anyway, the purpose of final flushing of a | |
1523 | * sub-buffer at offset 0 is to handle the case | |
1524 | * of entirely empty stream. | |
1525 | */ | |
1526 | if (caa_unlikely(subbuf_trunc(offsets->begin, chan) | |
1527 | - subbuf_trunc((unsigned long) | |
1528 | uatomic_read(&buf->consumed), chan) | |
1529 | >= chan->backend.buf_size)) | |
1530 | return -1; | |
852c2936 MD |
1531 | /* |
1532 | * The client does not save any header information. | |
1533 | * Don't switch empty subbuffer on finalize, because it | |
1534 | * is invalid to deliver a completely empty subbuffer. | |
1535 | */ | |
1536 | if (!config->cb.subbuffer_header_size()) | |
1537 | return -1; | |
1538 | /* | |
1539 | * Need to write the subbuffer start header on finalize. | |
1540 | */ | |
1541 | offsets->switch_old_start = 1; | |
1542 | } | |
1543 | offsets->begin = subbuf_align(offsets->begin, chan); | |
1544 | } else | |
1545 | return -1; /* we do not have to switch : buffer is empty */ | |
1546 | /* Note: old points to the next subbuf at offset 0 */ | |
1547 | offsets->end = offsets->begin; | |
1548 | return 0; | |
1549 | } | |
1550 | ||
1551 | /* | |
1552 | * Force a sub-buffer switch. This operation is completely reentrant : can be | |
1553 | * called while tracing is active with absolutely no lock held. | |
1554 | * | |
1555 | * Note, however, that as a v_cmpxchg is used for some atomic | |
1556 | * operations, this function must be called from the CPU which owns the buffer | |
1557 | * for a ACTIVE flush. | |
1558 | */ | |
4cfec15c | 1559 | void lib_ring_buffer_switch_slow(struct lttng_ust_lib_ring_buffer *buf, enum switch_mode mode, |
38fae1d3 | 1560 | struct lttng_ust_shm_handle *handle) |
852c2936 | 1561 | { |
1d498196 | 1562 | struct channel *chan = shmp(handle, buf->backend.chan); |
4cfec15c | 1563 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
1564 | struct switch_offsets offsets; |
1565 | unsigned long oldidx; | |
2fed87ae | 1566 | uint64_t tsc; |
852c2936 MD |
1567 | |
1568 | offsets.size = 0; | |
1569 | ||
1570 | /* | |
1571 | * Perform retryable operations. | |
1572 | */ | |
1573 | do { | |
1574 | if (lib_ring_buffer_try_switch_slow(mode, buf, chan, &offsets, | |
1575 | &tsc)) | |
1576 | return; /* Switch not needed */ | |
1577 | } while (v_cmpxchg(config, &buf->offset, offsets.old, offsets.end) | |
1578 | != offsets.old); | |
1579 | ||
1580 | /* | |
1581 | * Atomically update last_tsc. This update races against concurrent | |
1582 | * atomic updates, but the race will always cause supplementary full TSC | |
1583 | * records, never the opposite (missing a full TSC record when it would | |
1584 | * be needed). | |
1585 | */ | |
1586 | save_last_tsc(config, buf, tsc); | |
1587 | ||
1588 | /* | |
1589 | * Push the reader if necessary | |
1590 | */ | |
1591 | lib_ring_buffer_reserve_push_reader(buf, chan, offsets.old); | |
1592 | ||
1593 | oldidx = subbuf_index(offsets.old, chan); | |
1d498196 | 1594 | lib_ring_buffer_clear_noref(config, &buf->backend, oldidx, handle); |
852c2936 MD |
1595 | |
1596 | /* | |
1597 | * May need to populate header start on SWITCH_FLUSH. | |
1598 | */ | |
1599 | if (offsets.switch_old_start) { | |
1d498196 | 1600 | lib_ring_buffer_switch_old_start(buf, chan, &offsets, tsc, handle); |
852c2936 MD |
1601 | offsets.old += config->cb.subbuffer_header_size(); |
1602 | } | |
1603 | ||
1604 | /* | |
1605 | * Switch old subbuffer. | |
1606 | */ | |
1d498196 | 1607 | lib_ring_buffer_switch_old_end(buf, chan, &offsets, tsc, handle); |
852c2936 | 1608 | } |
852c2936 MD |
1609 | |
1610 | /* | |
1611 | * Returns : | |
1612 | * 0 if ok | |
1613 | * -ENOSPC if event size is too large for packet. | |
1614 | * -ENOBUFS if there is currently not enough space in buffer for the event. | |
1615 | * -EIO if data cannot be written into the buffer for any other reason. | |
1616 | */ | |
1617 | static | |
4cfec15c | 1618 | int lib_ring_buffer_try_reserve_slow(struct lttng_ust_lib_ring_buffer *buf, |
852c2936 MD |
1619 | struct channel *chan, |
1620 | struct switch_offsets *offsets, | |
4cfec15c | 1621 | struct lttng_ust_lib_ring_buffer_ctx *ctx) |
852c2936 | 1622 | { |
4cfec15c | 1623 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
38fae1d3 | 1624 | struct lttng_ust_shm_handle *handle = ctx->handle; |
852c2936 MD |
1625 | unsigned long reserve_commit_diff; |
1626 | ||
1627 | offsets->begin = v_read(config, &buf->offset); | |
1628 | offsets->old = offsets->begin; | |
1629 | offsets->switch_new_start = 0; | |
1630 | offsets->switch_new_end = 0; | |
1631 | offsets->switch_old_end = 0; | |
1632 | offsets->pre_header_padding = 0; | |
1633 | ||
1634 | ctx->tsc = config->cb.ring_buffer_clock_read(chan); | |
1635 | if ((int64_t) ctx->tsc == -EIO) | |
1636 | return -EIO; | |
1637 | ||
1638 | if (last_tsc_overflow(config, buf, ctx->tsc)) | |
1639 | ctx->rflags |= RING_BUFFER_RFLAG_FULL_TSC; | |
1640 | ||
b5a3dfa5 | 1641 | if (caa_unlikely(subbuf_offset(offsets->begin, ctx->chan) == 0)) { |
852c2936 MD |
1642 | offsets->switch_new_start = 1; /* For offsets->begin */ |
1643 | } else { | |
1644 | offsets->size = config->cb.record_header_size(config, chan, | |
1645 | offsets->begin, | |
1646 | &offsets->pre_header_padding, | |
1647 | ctx); | |
1648 | offsets->size += | |
1649 | lib_ring_buffer_align(offsets->begin + offsets->size, | |
1650 | ctx->largest_align) | |
1651 | + ctx->data_size; | |
b5a3dfa5 | 1652 | if (caa_unlikely(subbuf_offset(offsets->begin, chan) + |
852c2936 MD |
1653 | offsets->size > chan->backend.subbuf_size)) { |
1654 | offsets->switch_old_end = 1; /* For offsets->old */ | |
1655 | offsets->switch_new_start = 1; /* For offsets->begin */ | |
1656 | } | |
1657 | } | |
b5a3dfa5 | 1658 | if (caa_unlikely(offsets->switch_new_start)) { |
852c2936 MD |
1659 | unsigned long sb_index; |
1660 | ||
1661 | /* | |
1662 | * We are typically not filling the previous buffer completely. | |
1663 | */ | |
b5a3dfa5 | 1664 | if (caa_likely(offsets->switch_old_end)) |
852c2936 MD |
1665 | offsets->begin = subbuf_align(offsets->begin, chan); |
1666 | offsets->begin = offsets->begin | |
1667 | + config->cb.subbuffer_header_size(); | |
1668 | /* Test new buffer integrity */ | |
1669 | sb_index = subbuf_index(offsets->begin, chan); | |
1670 | reserve_commit_diff = | |
1671 | (buf_trunc(offsets->begin, chan) | |
1672 | >> chan->backend.num_subbuf_order) | |
1673 | - ((unsigned long) v_read(config, | |
4746ae29 | 1674 | &shmp_index(handle, buf->commit_cold, sb_index)->cc_sb) |
852c2936 | 1675 | & chan->commit_count_mask); |
b5a3dfa5 | 1676 | if (caa_likely(reserve_commit_diff == 0)) { |
852c2936 | 1677 | /* Next subbuffer not being written to. */ |
b5a3dfa5 | 1678 | if (caa_unlikely(config->mode != RING_BUFFER_OVERWRITE && |
852c2936 MD |
1679 | subbuf_trunc(offsets->begin, chan) |
1680 | - subbuf_trunc((unsigned long) | |
a6352fd4 | 1681 | uatomic_read(&buf->consumed), chan) |
852c2936 | 1682 | >= chan->backend.buf_size)) { |
64493e4f MD |
1683 | unsigned long nr_lost; |
1684 | ||
852c2936 MD |
1685 | /* |
1686 | * We do not overwrite non consumed buffers | |
1687 | * and we are full : record is lost. | |
1688 | */ | |
64493e4f | 1689 | nr_lost = v_read(config, &buf->records_lost_full); |
852c2936 | 1690 | v_inc(config, &buf->records_lost_full); |
64493e4f MD |
1691 | if ((nr_lost & (DBG_PRINT_NR_LOST - 1)) == 0) { |
1692 | DBG("%lu or more records lost in (%s:%d) (buffer full)\n", | |
1693 | nr_lost + 1, chan->backend.name, | |
1694 | buf->backend.cpu); | |
1695 | } | |
852c2936 MD |
1696 | return -ENOBUFS; |
1697 | } else { | |
1698 | /* | |
1699 | * Next subbuffer not being written to, and we | |
1700 | * are either in overwrite mode or the buffer is | |
1701 | * not full. It's safe to write in this new | |
1702 | * subbuffer. | |
1703 | */ | |
1704 | } | |
1705 | } else { | |
64493e4f MD |
1706 | unsigned long nr_lost; |
1707 | ||
852c2936 MD |
1708 | /* |
1709 | * Next subbuffer reserve offset does not match the | |
1710 | * commit offset. Drop record in producer-consumer and | |
1711 | * overwrite mode. Caused by either a writer OOPS or too | |
1712 | * many nested writes over a reserve/commit pair. | |
1713 | */ | |
64493e4f | 1714 | nr_lost = v_read(config, &buf->records_lost_wrap); |
852c2936 | 1715 | v_inc(config, &buf->records_lost_wrap); |
64493e4f MD |
1716 | if ((nr_lost & (DBG_PRINT_NR_LOST - 1)) == 0) { |
1717 | DBG("%lu or more records lost in (%s:%d) (wrap-around)\n", | |
1718 | nr_lost + 1, chan->backend.name, | |
1719 | buf->backend.cpu); | |
1720 | } | |
852c2936 MD |
1721 | return -EIO; |
1722 | } | |
1723 | offsets->size = | |
1724 | config->cb.record_header_size(config, chan, | |
1725 | offsets->begin, | |
1726 | &offsets->pre_header_padding, | |
1727 | ctx); | |
1728 | offsets->size += | |
1729 | lib_ring_buffer_align(offsets->begin + offsets->size, | |
1730 | ctx->largest_align) | |
1731 | + ctx->data_size; | |
b5a3dfa5 | 1732 | if (caa_unlikely(subbuf_offset(offsets->begin, chan) |
852c2936 | 1733 | + offsets->size > chan->backend.subbuf_size)) { |
64493e4f MD |
1734 | unsigned long nr_lost; |
1735 | ||
852c2936 MD |
1736 | /* |
1737 | * Record too big for subbuffers, report error, don't | |
1738 | * complete the sub-buffer switch. | |
1739 | */ | |
64493e4f | 1740 | nr_lost = v_read(config, &buf->records_lost_big); |
852c2936 | 1741 | v_inc(config, &buf->records_lost_big); |
64493e4f MD |
1742 | if ((nr_lost & (DBG_PRINT_NR_LOST - 1)) == 0) { |
1743 | DBG("%lu or more records lost in (%s:%d) record size " | |
1744 | " of %zu bytes is too large for buffer\n", | |
1745 | nr_lost + 1, chan->backend.name, | |
1746 | buf->backend.cpu, offsets->size); | |
1747 | } | |
852c2936 MD |
1748 | return -ENOSPC; |
1749 | } else { | |
1750 | /* | |
1751 | * We just made a successful buffer switch and the | |
1752 | * record fits in the new subbuffer. Let's write. | |
1753 | */ | |
1754 | } | |
1755 | } else { | |
1756 | /* | |
1757 | * Record fits in the current buffer and we are not on a switch | |
1758 | * boundary. It's safe to write. | |
1759 | */ | |
1760 | } | |
1761 | offsets->end = offsets->begin + offsets->size; | |
1762 | ||
b5a3dfa5 | 1763 | if (caa_unlikely(subbuf_offset(offsets->end, chan) == 0)) { |
852c2936 MD |
1764 | /* |
1765 | * The offset_end will fall at the very beginning of the next | |
1766 | * subbuffer. | |
1767 | */ | |
1768 | offsets->switch_new_end = 1; /* For offsets->begin */ | |
1769 | } | |
1770 | return 0; | |
1771 | } | |
1772 | ||
1773 | /** | |
1774 | * lib_ring_buffer_reserve_slow - Atomic slot reservation in a buffer. | |
1775 | * @ctx: ring buffer context. | |
1776 | * | |
1777 | * Return : -NOBUFS if not enough space, -ENOSPC if event size too large, | |
1778 | * -EIO for other errors, else returns 0. | |
1779 | * It will take care of sub-buffer switching. | |
1780 | */ | |
4cfec15c | 1781 | int lib_ring_buffer_reserve_slow(struct lttng_ust_lib_ring_buffer_ctx *ctx) |
852c2936 MD |
1782 | { |
1783 | struct channel *chan = ctx->chan; | |
38fae1d3 | 1784 | struct lttng_ust_shm_handle *handle = ctx->handle; |
4cfec15c MD |
1785 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
1786 | struct lttng_ust_lib_ring_buffer *buf; | |
852c2936 MD |
1787 | struct switch_offsets offsets; |
1788 | int ret; | |
1789 | ||
1790 | if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) | |
1d498196 | 1791 | buf = shmp(handle, chan->backend.buf[ctx->cpu].shmp); |
852c2936 | 1792 | else |
1d498196 | 1793 | buf = shmp(handle, chan->backend.buf[0].shmp); |
852c2936 MD |
1794 | ctx->buf = buf; |
1795 | ||
1796 | offsets.size = 0; | |
1797 | ||
1798 | do { | |
1799 | ret = lib_ring_buffer_try_reserve_slow(buf, chan, &offsets, | |
1800 | ctx); | |
b5a3dfa5 | 1801 | if (caa_unlikely(ret)) |
852c2936 | 1802 | return ret; |
b5a3dfa5 | 1803 | } while (caa_unlikely(v_cmpxchg(config, &buf->offset, offsets.old, |
852c2936 MD |
1804 | offsets.end) |
1805 | != offsets.old)); | |
1806 | ||
1807 | /* | |
1808 | * Atomically update last_tsc. This update races against concurrent | |
1809 | * atomic updates, but the race will always cause supplementary full TSC | |
1810 | * records, never the opposite (missing a full TSC record when it would | |
1811 | * be needed). | |
1812 | */ | |
1813 | save_last_tsc(config, buf, ctx->tsc); | |
1814 | ||
1815 | /* | |
1816 | * Push the reader if necessary | |
1817 | */ | |
1818 | lib_ring_buffer_reserve_push_reader(buf, chan, offsets.end - 1); | |
1819 | ||
1820 | /* | |
1821 | * Clear noref flag for this subbuffer. | |
1822 | */ | |
1823 | lib_ring_buffer_clear_noref(config, &buf->backend, | |
1d498196 MD |
1824 | subbuf_index(offsets.end - 1, chan), |
1825 | handle); | |
852c2936 MD |
1826 | |
1827 | /* | |
1828 | * Switch old subbuffer if needed. | |
1829 | */ | |
b5a3dfa5 | 1830 | if (caa_unlikely(offsets.switch_old_end)) { |
852c2936 | 1831 | lib_ring_buffer_clear_noref(config, &buf->backend, |
1d498196 MD |
1832 | subbuf_index(offsets.old - 1, chan), |
1833 | handle); | |
1834 | lib_ring_buffer_switch_old_end(buf, chan, &offsets, ctx->tsc, handle); | |
852c2936 MD |
1835 | } |
1836 | ||
1837 | /* | |
1838 | * Populate new subbuffer. | |
1839 | */ | |
b5a3dfa5 | 1840 | if (caa_unlikely(offsets.switch_new_start)) |
1d498196 | 1841 | lib_ring_buffer_switch_new_start(buf, chan, &offsets, ctx->tsc, handle); |
852c2936 | 1842 | |
b5a3dfa5 | 1843 | if (caa_unlikely(offsets.switch_new_end)) |
1d498196 | 1844 | lib_ring_buffer_switch_new_end(buf, chan, &offsets, ctx->tsc, handle); |
852c2936 MD |
1845 | |
1846 | ctx->slot_size = offsets.size; | |
1847 | ctx->pre_offset = offsets.begin; | |
1848 | ctx->buf_offset = offsets.begin + offsets.pre_header_padding; | |
1849 | return 0; | |
1850 | } | |
f645cfa7 MD |
1851 | |
1852 | /* | |
1853 | * Force a read (imply TLS fixup for dlopen) of TLS variables. | |
1854 | */ | |
1855 | void lttng_fixup_ringbuffer_tls(void) | |
1856 | { | |
8c90a710 | 1857 | asm volatile ("" : : "m" (URCU_TLS(lib_ring_buffer_nesting))); |
f645cfa7 | 1858 | } |
03d2d293 MD |
1859 | |
1860 | void lib_ringbuffer_signal_init(void) | |
1861 | { | |
1862 | sigset_t mask; | |
1863 | int ret; | |
1864 | ||
1865 | /* | |
1866 | * Block signal for entire process, so only our thread processes | |
1867 | * it. | |
1868 | */ | |
1869 | rb_setmask(&mask); | |
1870 | ret = pthread_sigmask(SIG_BLOCK, &mask, NULL); | |
1871 | if (ret) { | |
1872 | errno = ret; | |
1873 | PERROR("pthread_sigmask"); | |
1874 | } | |
1875 | } |