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