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