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