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