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