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