| 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 | #ifndef max |
| 78 | #define max(a, b) ((a) > (b) ? (a) : (b)) |
| 79 | #endif |
| 80 | |
| 81 | /* Print DBG() messages about events lost only every 1048576 hits */ |
| 82 | #define DBG_PRINT_NR_LOST (1UL << 20) |
| 83 | |
| 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 |
| 87 | #define CLOCKID CLOCK_MONOTONIC |
| 88 | #define LTTNG_UST_RING_BUFFER_GET_RETRY 10 |
| 89 | #define LTTNG_UST_RING_BUFFER_RETRY_DELAY_MS 10 |
| 90 | |
| 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 | |
| 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 | |
| 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; |
| 118 | unsigned int switch_new_start:1, switch_new_end:1, switch_old_start:1, |
| 119 | switch_old_end:1; |
| 120 | }; |
| 121 | |
| 122 | DEFINE_URCU_TLS(unsigned int, lib_ring_buffer_nesting); |
| 123 | |
| 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 | |
| 130 | static |
| 131 | void lib_ring_buffer_print_errors(struct channel *chan, |
| 132 | struct lttng_ust_lib_ring_buffer *buf, int cpu, |
| 133 | struct lttng_ust_shm_handle *handle); |
| 134 | |
| 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. |
| 139 | * Protected by the lock within the structure. |
| 140 | */ |
| 141 | struct timer_signal_data { |
| 142 | pthread_t tid; /* thread id managing signals */ |
| 143 | int setup_done; |
| 144 | int qs_done; |
| 145 | pthread_mutex_t lock; |
| 146 | }; |
| 147 | |
| 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 | }; |
| 154 | |
| 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 | */ |
| 164 | void lib_ring_buffer_reset(struct lttng_ust_lib_ring_buffer *buf, |
| 165 | struct lttng_ust_shm_handle *handle) |
| 166 | { |
| 167 | struct channel *chan; |
| 168 | const struct lttng_ust_lib_ring_buffer_config *config; |
| 169 | unsigned int i; |
| 170 | |
| 171 | chan = shmp(handle, buf->backend.chan); |
| 172 | if (!chan) |
| 173 | abort(); |
| 174 | config = &chan->backend.config; |
| 175 | /* |
| 176 | * Reset iterator first. It will put the subbuffer if it currently holds |
| 177 | * it. |
| 178 | */ |
| 179 | v_set(config, &buf->offset, 0); |
| 180 | for (i = 0; i < chan->backend.num_subbuf; i++) { |
| 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); |
| 184 | } |
| 185 | uatomic_set(&buf->consumed, 0); |
| 186 | uatomic_set(&buf->record_disabled, 0); |
| 187 | v_set(config, &buf->last_tsc, 0); |
| 188 | lib_ring_buffer_backend_reset(&buf->backend, handle); |
| 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 | } |
| 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 | */ |
| 212 | uatomic_set(&chan->record_disabled, 0); |
| 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 | } |
| 219 | |
| 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 | |
| 309 | /* |
| 310 | * Must be called under cpu hotplug protection. |
| 311 | */ |
| 312 | int lib_ring_buffer_create(struct lttng_ust_lib_ring_buffer *buf, |
| 313 | struct channel_backend *chanb, int cpu, |
| 314 | struct lttng_ust_shm_handle *handle, |
| 315 | struct shm_object *shmobj) |
| 316 | { |
| 317 | const struct lttng_ust_lib_ring_buffer_config *config = &chanb->config; |
| 318 | struct channel *chan = caa_container_of(chanb, struct channel, backend); |
| 319 | void *priv = channel_get_private(chan); |
| 320 | size_t subbuf_header_size; |
| 321 | uint64_t tsc; |
| 322 | int ret; |
| 323 | |
| 324 | /* Test for cpu hotplug */ |
| 325 | if (buf->backend.allocated) |
| 326 | return 0; |
| 327 | |
| 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)) { |
| 333 | return -ENOMEM; |
| 334 | } |
| 335 | |
| 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)) { |
| 341 | ret = -ENOMEM; |
| 342 | goto free_commit; |
| 343 | } |
| 344 | |
| 345 | ret = lib_ring_buffer_backend_create(&buf->backend, &chan->backend, |
| 346 | cpu, handle, shmobj); |
| 347 | if (ret) { |
| 348 | goto free_init; |
| 349 | } |
| 350 | |
| 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); |
| 357 | subbuffer_id_clear_noref(config, &shmp_index(handle, buf->backend.buf_wsb, 0)->id); |
| 358 | tsc = config->cb.ring_buffer_clock_read(shmp(handle, buf->backend.chan)); |
| 359 | config->cb.buffer_begin(buf, tsc, 0, handle); |
| 360 | v_add(config, subbuf_header_size, &shmp_index(handle, buf->commit_hot, 0)->cc); |
| 361 | v_add(config, subbuf_header_size, &shmp_index(handle, buf->commit_hot, 0)->seq); |
| 362 | |
| 363 | if (config->cb.buffer_create) { |
| 364 | ret = config->cb.buffer_create(buf, priv, cpu, chanb->name, handle); |
| 365 | if (ret) |
| 366 | goto free_chanbuf; |
| 367 | } |
| 368 | |
| 369 | init_crash_abi(config, &buf->crash_abi, buf, chanb, shmobj, handle); |
| 370 | |
| 371 | buf->backend.allocated = 1; |
| 372 | return 0; |
| 373 | |
| 374 | /* Error handling */ |
| 375 | free_init: |
| 376 | /* commit_cold will be freed by shm teardown */ |
| 377 | free_commit: |
| 378 | /* commit_hot will be freed by shm teardown */ |
| 379 | free_chanbuf: |
| 380 | return ret; |
| 381 | } |
| 382 | |
| 383 | static |
| 384 | void lib_ring_buffer_channel_switch_timer(int sig, siginfo_t *si, void *uc) |
| 385 | { |
| 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; |
| 396 | |
| 397 | DBG("Switch timer for channel %p\n", chan); |
| 398 | |
| 399 | /* |
| 400 | * Only flush buffers periodically if readers are active. |
| 401 | */ |
| 402 | pthread_mutex_lock(&wakeup_fd_mutex); |
| 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); |
| 407 | |
| 408 | if (!buf) |
| 409 | abort(); |
| 410 | if (uatomic_read(&buf->active_readers)) |
| 411 | lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE, |
| 412 | chan->handle); |
| 413 | } |
| 414 | } else { |
| 415 | struct lttng_ust_lib_ring_buffer *buf = |
| 416 | shmp(handle, chan->backend.buf[0].shmp); |
| 417 | |
| 418 | if (!buf) |
| 419 | abort(); |
| 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 | |
| 447 | if (!buf) |
| 448 | abort(); |
| 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 | |
| 459 | if (!buf) |
| 460 | abort(); |
| 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 | } |
| 466 | } |
| 467 | pthread_mutex_unlock(&wakeup_fd_mutex); |
| 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); |
| 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 | } |
| 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); |
| 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) { |
| 519 | if (errno != EINTR) |
| 520 | PERROR("sigwaitinfo"); |
| 521 | continue; |
| 522 | } |
| 523 | if (signr == LTTNG_UST_RB_SIG_FLUSH) { |
| 524 | lib_ring_buffer_channel_switch_timer(info.si_signo, |
| 525 | &info, NULL); |
| 526 | } else if (signr == LTTNG_UST_RB_SIG_READ) { |
| 527 | lib_ring_buffer_channel_read_timer(info.si_signo, |
| 528 | &info, NULL); |
| 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 | /* |
| 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 | |
| 549 | pthread_mutex_lock(&timer_signal.lock); |
| 550 | if (timer_signal.setup_done) |
| 551 | goto end; |
| 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; |
| 564 | end: |
| 565 | pthread_mutex_unlock(&timer_signal.lock); |
| 566 | } |
| 567 | |
| 568 | /* |
| 569 | * Wait for signal-handling thread quiescent state. |
| 570 | */ |
| 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 | |
| 622 | static |
| 623 | void lib_ring_buffer_channel_switch_timer_start(struct channel *chan) |
| 624 | { |
| 625 | struct sigevent sev; |
| 626 | struct itimerspec its; |
| 627 | int ret; |
| 628 | |
| 629 | if (!chan->switch_timer_interval || chan->switch_timer_enabled) |
| 630 | return; |
| 631 | |
| 632 | chan->switch_timer_enabled = 1; |
| 633 | |
| 634 | lib_ring_buffer_setup_timer_thread(); |
| 635 | |
| 636 | sev.sigev_notify = SIGEV_SIGNAL; |
| 637 | sev.sigev_signo = LTTNG_UST_RB_SIG_FLUSH; |
| 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; |
| 645 | its.it_value.tv_nsec = (chan->switch_timer_interval % 1000000) * 1000; |
| 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 | |
| 655 | static |
| 656 | void lib_ring_buffer_channel_switch_timer_stop(struct channel *chan) |
| 657 | { |
| 658 | int ret; |
| 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 | |
| 668 | lib_ring_buffer_wait_signal_thread_qs(LTTNG_UST_RB_SIG_FLUSH); |
| 669 | |
| 670 | chan->switch_timer = 0; |
| 671 | chan->switch_timer_enabled = 0; |
| 672 | } |
| 673 | |
| 674 | static |
| 675 | void lib_ring_buffer_channel_read_timer_start(struct channel *chan) |
| 676 | { |
| 677 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
| 678 | struct sigevent sev; |
| 679 | struct itimerspec its; |
| 680 | int ret; |
| 681 | |
| 682 | if (config->wakeup != RING_BUFFER_WAKEUP_BY_TIMER |
| 683 | || !chan->read_timer_interval || chan->read_timer_enabled) |
| 684 | return; |
| 685 | |
| 686 | chan->read_timer_enabled = 1; |
| 687 | |
| 688 | lib_ring_buffer_setup_timer_thread(); |
| 689 | |
| 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 | } |
| 697 | |
| 698 | its.it_value.tv_sec = chan->read_timer_interval / 1000000; |
| 699 | its.it_value.tv_nsec = (chan->read_timer_interval % 1000000) * 1000; |
| 700 | its.it_interval.tv_sec = its.it_value.tv_sec; |
| 701 | its.it_interval.tv_nsec = its.it_value.tv_nsec; |
| 702 | |
| 703 | ret = timer_settime(chan->read_timer, 0, &its, NULL); |
| 704 | if (ret == -1) { |
| 705 | PERROR("timer_settime"); |
| 706 | } |
| 707 | } |
| 708 | |
| 709 | static |
| 710 | void lib_ring_buffer_channel_read_timer_stop(struct channel *chan) |
| 711 | { |
| 712 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
| 713 | int ret; |
| 714 | |
| 715 | if (config->wakeup != RING_BUFFER_WAKEUP_BY_TIMER |
| 716 | || !chan->read_timer_interval || !chan->read_timer_enabled) |
| 717 | return; |
| 718 | |
| 719 | ret = timer_delete(chan->read_timer); |
| 720 | if (ret == -1) { |
| 721 | PERROR("timer_delete"); |
| 722 | } |
| 723 | |
| 724 | /* |
| 725 | * do one more check to catch data that has been written in the last |
| 726 | * timer period. |
| 727 | */ |
| 728 | lib_ring_buffer_channel_do_read(chan); |
| 729 | |
| 730 | lib_ring_buffer_wait_signal_thread_qs(LTTNG_UST_RB_SIG_READ); |
| 731 | |
| 732 | chan->read_timer = 0; |
| 733 | chan->read_timer_enabled = 0; |
| 734 | } |
| 735 | |
| 736 | static void channel_unregister_notifiers(struct channel *chan, |
| 737 | struct lttng_ust_shm_handle *handle) |
| 738 | { |
| 739 | lib_ring_buffer_channel_switch_timer_stop(chan); |
| 740 | lib_ring_buffer_channel_read_timer_stop(chan); |
| 741 | } |
| 742 | |
| 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) |
| 766 | { |
| 767 | channel_backend_free(&chan->backend, handle); |
| 768 | /* chan is freed by shm teardown */ |
| 769 | shm_object_table_destroy(handle->table); |
| 770 | free(handle); |
| 771 | } |
| 772 | |
| 773 | /** |
| 774 | * channel_create - Create channel. |
| 775 | * @config: ring buffer instance configuration |
| 776 | * @name: name of the channel |
| 777 | * @priv_data: ring buffer client private data area pointer (output) |
| 778 | * @priv_data_size: length, in bytes, of the private data area. |
| 779 | * @priv_data_init: initialization data for private data. |
| 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. |
| 789 | * @stream_fds: array of stream file descriptors. |
| 790 | * @nr_stream_fds: number of file descriptors in array. |
| 791 | * |
| 792 | * Holds cpu hotplug. |
| 793 | * Returns NULL on failure. |
| 794 | */ |
| 795 | struct lttng_ust_shm_handle *channel_create(const struct lttng_ust_lib_ring_buffer_config *config, |
| 796 | const char *name, |
| 797 | void **priv_data, |
| 798 | size_t priv_data_align, |
| 799 | size_t priv_data_size, |
| 800 | void *priv_data_init, |
| 801 | void *buf_addr, size_t subbuf_size, |
| 802 | size_t num_subbuf, unsigned int switch_timer_interval, |
| 803 | unsigned int read_timer_interval, |
| 804 | const int *stream_fds, int nr_stream_fds) |
| 805 | { |
| 806 | int ret; |
| 807 | size_t shmsize, chansize; |
| 808 | struct channel *chan; |
| 809 | struct lttng_ust_shm_handle *handle; |
| 810 | struct shm_object *shmobj; |
| 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; |
| 817 | |
| 818 | if (nr_stream_fds != nr_streams) |
| 819 | return NULL; |
| 820 | |
| 821 | if (lib_ring_buffer_check_config(config, switch_timer_interval, |
| 822 | read_timer_interval)) |
| 823 | return NULL; |
| 824 | |
| 825 | handle = zmalloc(sizeof(struct lttng_ust_shm_handle)); |
| 826 | if (!handle) |
| 827 | return NULL; |
| 828 | |
| 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; |
| 833 | |
| 834 | /* Calculate the shm allocation layout */ |
| 835 | shmsize = sizeof(struct channel); |
| 836 | shmsize += offset_align(shmsize, __alignof__(struct lttng_ust_lib_ring_buffer_shmp)); |
| 837 | shmsize += sizeof(struct lttng_ust_lib_ring_buffer_shmp) * nr_streams; |
| 838 | chansize = shmsize; |
| 839 | if (priv_data_align) |
| 840 | shmsize += offset_align(shmsize, priv_data_align); |
| 841 | shmsize += priv_data_size; |
| 842 | |
| 843 | /* Allocate normal memory for channel (not shared) */ |
| 844 | shmobj = shm_object_table_alloc(handle->table, shmsize, SHM_OBJECT_MEM, |
| 845 | -1); |
| 846 | if (!shmobj) |
| 847 | goto error_append; |
| 848 | /* struct channel is at object 0, offset 0 (hardcoded) */ |
| 849 | set_shmp(handle->chan, zalloc_shm(shmobj, chansize)); |
| 850 | assert(handle->chan._ref.index == 0); |
| 851 | assert(handle->chan._ref.offset == 0); |
| 852 | chan = shmp(handle, handle->chan); |
| 853 | if (!chan) |
| 854 | goto error_append; |
| 855 | chan->nr_streams = nr_streams; |
| 856 | |
| 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); |
| 867 | memcpy(*priv_data, priv_data_init, priv_data_size); |
| 868 | } else { |
| 869 | chan->priv_data_offset = -1; |
| 870 | if (priv_data) |
| 871 | *priv_data = NULL; |
| 872 | } |
| 873 | |
| 874 | ret = channel_backend_init(&chan->backend, name, config, |
| 875 | subbuf_size, num_subbuf, handle, |
| 876 | stream_fds); |
| 877 | if (ret) |
| 878 | goto error_backend_init; |
| 879 | |
| 880 | chan->handle = handle; |
| 881 | chan->commit_count_mask = (~0UL >> chan->backend.num_subbuf_order); |
| 882 | |
| 883 | chan->switch_timer_interval = switch_timer_interval; |
| 884 | chan->read_timer_interval = read_timer_interval; |
| 885 | lib_ring_buffer_channel_switch_timer_start(chan); |
| 886 | lib_ring_buffer_channel_read_timer_start(chan); |
| 887 | |
| 888 | return handle; |
| 889 | |
| 890 | error_backend_init: |
| 891 | error_append: |
| 892 | shm_object_table_destroy(handle->table); |
| 893 | error_table_alloc: |
| 894 | free(handle); |
| 895 | return NULL; |
| 896 | } |
| 897 | |
| 898 | struct lttng_ust_shm_handle *channel_handle_create(void *data, |
| 899 | uint64_t memory_map_size, |
| 900 | int wakeup_fd) |
| 901 | { |
| 902 | struct lttng_ust_shm_handle *handle; |
| 903 | struct shm_object *object; |
| 904 | |
| 905 | handle = zmalloc(sizeof(struct lttng_ust_shm_handle)); |
| 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 */ |
| 914 | object = shm_object_table_append_mem(handle->table, data, |
| 915 | memory_map_size, wakeup_fd); |
| 916 | if (!object) |
| 917 | goto error_table_object; |
| 918 | /* struct channel is at object 0, offset 0 (hardcoded) */ |
| 919 | handle->chan._ref.index = 0; |
| 920 | handle->chan._ref.offset = 0; |
| 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 | |
| 930 | int channel_handle_add_stream(struct lttng_ust_shm_handle *handle, |
| 931 | int shm_fd, int wakeup_fd, uint32_t stream_nr, |
| 932 | uint64_t memory_map_size) |
| 933 | { |
| 934 | struct shm_object *object; |
| 935 | |
| 936 | /* Add stream object */ |
| 937 | object = shm_object_table_append_shm(handle->table, |
| 938 | shm_fd, wakeup_fd, stream_nr, |
| 939 | memory_map_size); |
| 940 | if (!object) |
| 941 | return -EINVAL; |
| 942 | return 0; |
| 943 | } |
| 944 | |
| 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 | |
| 951 | static |
| 952 | void channel_release(struct channel *chan, struct lttng_ust_shm_handle *handle) |
| 953 | { |
| 954 | channel_free(chan, handle); |
| 955 | } |
| 956 | |
| 957 | /** |
| 958 | * channel_destroy - Finalize, wait for q.s. and destroy channel. |
| 959 | * @chan: channel to destroy |
| 960 | * |
| 961 | * Holds cpu hotplug. |
| 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. |
| 965 | * They should release their handle at that point. |
| 966 | */ |
| 967 | void channel_destroy(struct channel *chan, struct lttng_ust_shm_handle *handle, |
| 968 | int consumer) |
| 969 | { |
| 970 | if (consumer) { |
| 971 | /* |
| 972 | * Note: the consumer takes care of finalizing and |
| 973 | * switching the buffers. |
| 974 | */ |
| 975 | channel_unregister_notifiers(chan, handle); |
| 976 | /* |
| 977 | * The consumer prints errors. |
| 978 | */ |
| 979 | channel_print_errors(chan, handle); |
| 980 | } |
| 981 | |
| 982 | /* |
| 983 | * sessiond/consumer are keeping a reference on the shm file |
| 984 | * descriptor directly. No need to refcount. |
| 985 | */ |
| 986 | channel_release(chan, handle); |
| 987 | return; |
| 988 | } |
| 989 | |
| 990 | struct lttng_ust_lib_ring_buffer *channel_get_ring_buffer( |
| 991 | const struct lttng_ust_lib_ring_buffer_config *config, |
| 992 | struct channel *chan, int cpu, |
| 993 | struct lttng_ust_shm_handle *handle, |
| 994 | int *shm_fd, int *wait_fd, |
| 995 | int *wakeup_fd, |
| 996 | uint64_t *memory_map_size) |
| 997 | { |
| 998 | struct shm_ref *ref; |
| 999 | |
| 1000 | if (config->alloc == RING_BUFFER_ALLOC_GLOBAL) { |
| 1001 | cpu = 0; |
| 1002 | } else { |
| 1003 | if (cpu >= num_possible_cpus()) |
| 1004 | return NULL; |
| 1005 | } |
| 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); |
| 1013 | } |
| 1014 | |
| 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, |
| 1036 | struct channel *chan, |
| 1037 | struct lttng_ust_shm_handle *handle, |
| 1038 | int cpu) |
| 1039 | { |
| 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; |
| 1047 | } |
| 1048 | ref = &chan->backend.buf[cpu].shmp._ref; |
| 1049 | return shm_close_wait_fd(handle, ref); |
| 1050 | } |
| 1051 | |
| 1052 | int ring_buffer_stream_close_wakeup_fd(const struct lttng_ust_lib_ring_buffer_config *config, |
| 1053 | struct channel *chan, |
| 1054 | struct lttng_ust_shm_handle *handle, |
| 1055 | int cpu) |
| 1056 | { |
| 1057 | struct shm_ref *ref; |
| 1058 | int ret; |
| 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; |
| 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; |
| 1071 | } |
| 1072 | |
| 1073 | int lib_ring_buffer_open_read(struct lttng_ust_lib_ring_buffer *buf, |
| 1074 | struct lttng_ust_shm_handle *handle) |
| 1075 | { |
| 1076 | if (uatomic_cmpxchg(&buf->active_readers, 0, 1) != 0) |
| 1077 | return -EBUSY; |
| 1078 | cmm_smp_mb(); |
| 1079 | return 0; |
| 1080 | } |
| 1081 | |
| 1082 | void lib_ring_buffer_release_read(struct lttng_ust_lib_ring_buffer *buf, |
| 1083 | struct lttng_ust_shm_handle *handle) |
| 1084 | { |
| 1085 | struct channel *chan = shmp(handle, buf->backend.chan); |
| 1086 | |
| 1087 | CHAN_WARN_ON(chan, uatomic_read(&buf->active_readers) != 1); |
| 1088 | cmm_smp_mb(); |
| 1089 | uatomic_dec(&buf->active_readers); |
| 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. |
| 1100 | */ |
| 1101 | |
| 1102 | int lib_ring_buffer_snapshot(struct lttng_ust_lib_ring_buffer *buf, |
| 1103 | unsigned long *consumed, unsigned long *produced, |
| 1104 | struct lttng_ust_shm_handle *handle) |
| 1105 | { |
| 1106 | struct channel *chan = shmp(handle, buf->backend.chan); |
| 1107 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
| 1108 | unsigned long consumed_cur, write_offset; |
| 1109 | int finalized; |
| 1110 | |
| 1111 | finalized = CMM_ACCESS_ONCE(buf->finalized); |
| 1112 | /* |
| 1113 | * Read finalized before counters. |
| 1114 | */ |
| 1115 | cmm_smp_rmb(); |
| 1116 | consumed_cur = uatomic_read(&buf->consumed); |
| 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; |
| 1147 | else |
| 1148 | return -EAGAIN; |
| 1149 | } |
| 1150 | |
| 1151 | /** |
| 1152 | * lib_ring_buffer_move_consumer - move consumed counter forward |
| 1153 | * @buf: ring buffer |
| 1154 | * @consumed_new: new consumed count value |
| 1155 | */ |
| 1156 | void lib_ring_buffer_move_consumer(struct lttng_ust_lib_ring_buffer *buf, |
| 1157 | unsigned long consumed_new, |
| 1158 | struct lttng_ust_shm_handle *handle) |
| 1159 | { |
| 1160 | struct lttng_ust_lib_ring_buffer_backend *bufb = &buf->backend; |
| 1161 | struct channel *chan = shmp(handle, bufb->chan); |
| 1162 | unsigned long consumed; |
| 1163 | |
| 1164 | CHAN_WARN_ON(chan, uatomic_read(&buf->active_readers) != 1); |
| 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 | */ |
| 1171 | consumed = uatomic_read(&buf->consumed); |
| 1172 | while ((long) consumed - (long) consumed_new < 0) |
| 1173 | consumed = uatomic_cmpxchg(&buf->consumed, consumed, |
| 1174 | consumed_new); |
| 1175 | } |
| 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. |
| 1184 | */ |
| 1185 | int lib_ring_buffer_get_subbuf(struct lttng_ust_lib_ring_buffer *buf, |
| 1186 | unsigned long consumed, |
| 1187 | struct lttng_ust_shm_handle *handle) |
| 1188 | { |
| 1189 | struct channel *chan = shmp(handle, buf->backend.chan); |
| 1190 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
| 1191 | unsigned long consumed_cur, consumed_idx, commit_count, write_offset; |
| 1192 | int ret, finalized, nr_retry = LTTNG_UST_RING_BUFFER_GET_RETRY; |
| 1193 | |
| 1194 | retry: |
| 1195 | finalized = CMM_ACCESS_ONCE(buf->finalized); |
| 1196 | /* |
| 1197 | * Read finalized before counters. |
| 1198 | */ |
| 1199 | cmm_smp_rmb(); |
| 1200 | consumed_cur = uatomic_read(&buf->consumed); |
| 1201 | consumed_idx = subbuf_index(consumed, chan); |
| 1202 | commit_count = v_read(config, &shmp_index(handle, buf->commit_cold, consumed_idx)->cc_sb); |
| 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. |
| 1208 | */ |
| 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(); |
| 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 |
| 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. |
| 1273 | */ |
| 1274 | if (((commit_count - chan->backend.subbuf_size) |
| 1275 | & chan->commit_count_mask) |
| 1276 | - (buf_trunc(consumed, chan) |
| 1277 | >> chan->backend.num_subbuf_order) |
| 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 | } |
| 1287 | |
| 1288 | /* |
| 1289 | * Check that we are not about to read the same subbuffer in |
| 1290 | * which the writer head is. |
| 1291 | */ |
| 1292 | if (subbuf_trunc(write_offset, chan) - subbuf_trunc(consumed, chan) |
| 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. |
| 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. |
| 1306 | */ |
| 1307 | ret = update_read_sb_index(config, &buf->backend, &chan->backend, |
| 1308 | consumed_idx, buf_trunc_val(consumed, chan), |
| 1309 | handle); |
| 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 | } |
| 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; |
| 1333 | else |
| 1334 | return -EAGAIN; |
| 1335 | } |
| 1336 | |
| 1337 | /** |
| 1338 | * lib_ring_buffer_put_subbuf - release exclusive subbuffer access |
| 1339 | * @buf: ring buffer |
| 1340 | */ |
| 1341 | void lib_ring_buffer_put_subbuf(struct lttng_ust_lib_ring_buffer *buf, |
| 1342 | struct lttng_ust_shm_handle *handle) |
| 1343 | { |
| 1344 | struct lttng_ust_lib_ring_buffer_backend *bufb = &buf->backend; |
| 1345 | struct channel *chan = shmp(handle, bufb->chan); |
| 1346 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
| 1347 | unsigned long read_sb_bindex, consumed_idx, consumed; |
| 1348 | |
| 1349 | CHAN_WARN_ON(chan, uatomic_read(&buf->active_readers) != 1); |
| 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, |
| 1370 | &shmp(handle, shmp_index(handle, bufb->array, read_sb_bindex)->shmp)->records_unread), |
| 1371 | &bufb->records_read); |
| 1372 | v_set(config, &shmp(handle, shmp_index(handle, bufb->array, read_sb_bindex)->shmp)->records_unread, 0); |
| 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, |
| 1387 | consumed_idx, buf_trunc_val(consumed, chan), |
| 1388 | handle); |
| 1389 | /* |
| 1390 | * update_read_sb_index return value ignored. Don't exchange sub-buffer |
| 1391 | * if the writer concurrently updated it. |
| 1392 | */ |
| 1393 | } |
| 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 |
| 1400 | void lib_ring_buffer_print_subbuffer_errors(struct lttng_ust_lib_ring_buffer *buf, |
| 1401 | struct channel *chan, |
| 1402 | unsigned long cons_offset, |
| 1403 | int cpu, |
| 1404 | struct lttng_ust_shm_handle *handle) |
| 1405 | { |
| 1406 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
| 1407 | unsigned long cons_idx, commit_count, commit_count_sb; |
| 1408 | |
| 1409 | cons_idx = subbuf_index(cons_offset, chan); |
| 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); |
| 1412 | |
| 1413 | if (subbuf_offset(commit_count, chan) != 0) |
| 1414 | DBG("ring buffer %s, cpu %d: " |
| 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 | |
| 1422 | DBG("ring buffer: %s, cpu %d: %lu bytes committed\n", |
| 1423 | chan->backend.name, cpu, commit_count); |
| 1424 | } |
| 1425 | |
| 1426 | static |
| 1427 | void lib_ring_buffer_print_buffer_errors(struct lttng_ust_lib_ring_buffer *buf, |
| 1428 | struct channel *chan, |
| 1429 | void *priv, int cpu, |
| 1430 | struct lttng_ust_shm_handle *handle) |
| 1431 | { |
| 1432 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
| 1433 | unsigned long write_offset, cons_offset; |
| 1434 | |
| 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); |
| 1441 | cons_offset = uatomic_read(&buf->consumed); |
| 1442 | if (write_offset != cons_offset) |
| 1443 | DBG("ring buffer %s, cpu %d: " |
| 1444 | "non-consumed data\n" |
| 1445 | " [ %lu bytes written, %lu bytes read ]\n", |
| 1446 | chan->backend.name, cpu, write_offset, cons_offset); |
| 1447 | |
| 1448 | for (cons_offset = uatomic_read(&buf->consumed); |
| 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, |
| 1454 | cpu, handle); |
| 1455 | } |
| 1456 | |
| 1457 | static |
| 1458 | void lib_ring_buffer_print_errors(struct channel *chan, |
| 1459 | struct lttng_ust_lib_ring_buffer *buf, int cpu, |
| 1460 | struct lttng_ust_shm_handle *handle) |
| 1461 | { |
| 1462 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
| 1463 | void *priv = channel_get_private(chan); |
| 1464 | |
| 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 | } |
| 1489 | lib_ring_buffer_print_buffer_errors(buf, chan, priv, cpu, handle); |
| 1490 | } |
| 1491 | |
| 1492 | /* |
| 1493 | * lib_ring_buffer_switch_old_start: Populate old subbuffer header. |
| 1494 | * |
| 1495 | * Only executed by SWITCH_FLUSH, which can be issued while tracing is |
| 1496 | * active or at buffer finalization (destroy). |
| 1497 | */ |
| 1498 | static |
| 1499 | void lib_ring_buffer_switch_old_start(struct lttng_ust_lib_ring_buffer *buf, |
| 1500 | struct channel *chan, |
| 1501 | struct switch_offsets *offsets, |
| 1502 | uint64_t tsc, |
| 1503 | struct lttng_ust_shm_handle *handle) |
| 1504 | { |
| 1505 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
| 1506 | unsigned long oldidx = subbuf_index(offsets->old, chan); |
| 1507 | unsigned long commit_count; |
| 1508 | |
| 1509 | config->cb.buffer_begin(buf, tsc, oldidx, handle); |
| 1510 | |
| 1511 | /* |
| 1512 | * Order all writes to buffer before the commit count update that will |
| 1513 | * determine that the subbuffer is full. |
| 1514 | */ |
| 1515 | cmm_smp_wmb(); |
| 1516 | v_add(config, config->cb.subbuffer_header_size(), |
| 1517 | &shmp_index(handle, buf->commit_hot, oldidx)->cc); |
| 1518 | commit_count = v_read(config, &shmp_index(handle, buf->commit_hot, oldidx)->cc); |
| 1519 | /* Check if the written buffer has to be delivered */ |
| 1520 | lib_ring_buffer_check_deliver(config, buf, chan, offsets->old, |
| 1521 | commit_count, oldidx, handle, tsc); |
| 1522 | lib_ring_buffer_write_commit_counter(config, buf, chan, oldidx, |
| 1523 | offsets->old + config->cb.subbuffer_header_size(), |
| 1524 | commit_count, handle); |
| 1525 | } |
| 1526 | |
| 1527 | /* |
| 1528 | * lib_ring_buffer_switch_old_end: switch old subbuffer |
| 1529 | * |
| 1530 | * Note : offset_old should never be 0 here. It is ok, because we never perform |
| 1531 | * buffer switch on an empty subbuffer in SWITCH_ACTIVE mode. The caller |
| 1532 | * increments the offset_old value when doing a SWITCH_FLUSH on an empty |
| 1533 | * subbuffer. |
| 1534 | */ |
| 1535 | static |
| 1536 | void lib_ring_buffer_switch_old_end(struct lttng_ust_lib_ring_buffer *buf, |
| 1537 | struct channel *chan, |
| 1538 | struct switch_offsets *offsets, |
| 1539 | uint64_t tsc, |
| 1540 | struct lttng_ust_shm_handle *handle) |
| 1541 | { |
| 1542 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
| 1543 | unsigned long oldidx = subbuf_index(offsets->old - 1, chan); |
| 1544 | unsigned long commit_count, padding_size, data_size; |
| 1545 | |
| 1546 | data_size = subbuf_offset(offsets->old - 1, chan) + 1; |
| 1547 | padding_size = chan->backend.subbuf_size - data_size; |
| 1548 | subbuffer_set_data_size(config, &buf->backend, oldidx, data_size, |
| 1549 | handle); |
| 1550 | |
| 1551 | /* |
| 1552 | * Order all writes to buffer before the commit count update that will |
| 1553 | * determine that the subbuffer is full. |
| 1554 | */ |
| 1555 | cmm_smp_wmb(); |
| 1556 | v_add(config, padding_size, &shmp_index(handle, buf->commit_hot, oldidx)->cc); |
| 1557 | commit_count = v_read(config, &shmp_index(handle, buf->commit_hot, oldidx)->cc); |
| 1558 | lib_ring_buffer_check_deliver(config, buf, chan, offsets->old - 1, |
| 1559 | commit_count, oldidx, handle, tsc); |
| 1560 | lib_ring_buffer_write_commit_counter(config, buf, chan, oldidx, |
| 1561 | offsets->old + padding_size, commit_count, handle); |
| 1562 | } |
| 1563 | |
| 1564 | /* |
| 1565 | * lib_ring_buffer_switch_new_start: Populate new subbuffer. |
| 1566 | * |
| 1567 | * This code can be executed unordered : writers may already have written to the |
| 1568 | * sub-buffer before this code gets executed, caution. The commit makes sure |
| 1569 | * that this code is executed before the deliver of this sub-buffer. |
| 1570 | */ |
| 1571 | static |
| 1572 | void lib_ring_buffer_switch_new_start(struct lttng_ust_lib_ring_buffer *buf, |
| 1573 | struct channel *chan, |
| 1574 | struct switch_offsets *offsets, |
| 1575 | uint64_t tsc, |
| 1576 | struct lttng_ust_shm_handle *handle) |
| 1577 | { |
| 1578 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
| 1579 | unsigned long beginidx = subbuf_index(offsets->begin, chan); |
| 1580 | unsigned long commit_count; |
| 1581 | |
| 1582 | config->cb.buffer_begin(buf, tsc, beginidx, handle); |
| 1583 | |
| 1584 | /* |
| 1585 | * Order all writes to buffer before the commit count update that will |
| 1586 | * determine that the subbuffer is full. |
| 1587 | */ |
| 1588 | cmm_smp_wmb(); |
| 1589 | v_add(config, config->cb.subbuffer_header_size(), |
| 1590 | &shmp_index(handle, buf->commit_hot, beginidx)->cc); |
| 1591 | commit_count = v_read(config, &shmp_index(handle, buf->commit_hot, beginidx)->cc); |
| 1592 | /* Check if the written buffer has to be delivered */ |
| 1593 | lib_ring_buffer_check_deliver(config, buf, chan, offsets->begin, |
| 1594 | commit_count, beginidx, handle, tsc); |
| 1595 | lib_ring_buffer_write_commit_counter(config, buf, chan, beginidx, |
| 1596 | offsets->begin + config->cb.subbuffer_header_size(), |
| 1597 | commit_count, handle); |
| 1598 | } |
| 1599 | |
| 1600 | /* |
| 1601 | * lib_ring_buffer_switch_new_end: finish switching current subbuffer |
| 1602 | * |
| 1603 | * Calls subbuffer_set_data_size() to set the data size of the current |
| 1604 | * sub-buffer. We do not need to perform check_deliver nor commit here, |
| 1605 | * since this task will be done by the "commit" of the event for which |
| 1606 | * we are currently doing the space reservation. |
| 1607 | */ |
| 1608 | static |
| 1609 | void lib_ring_buffer_switch_new_end(struct lttng_ust_lib_ring_buffer *buf, |
| 1610 | struct channel *chan, |
| 1611 | struct switch_offsets *offsets, |
| 1612 | uint64_t tsc, |
| 1613 | struct lttng_ust_shm_handle *handle) |
| 1614 | { |
| 1615 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
| 1616 | unsigned long endidx, data_size; |
| 1617 | |
| 1618 | endidx = subbuf_index(offsets->end - 1, chan); |
| 1619 | data_size = subbuf_offset(offsets->end - 1, chan) + 1; |
| 1620 | subbuffer_set_data_size(config, &buf->backend, endidx, data_size, |
| 1621 | handle); |
| 1622 | } |
| 1623 | |
| 1624 | /* |
| 1625 | * Returns : |
| 1626 | * 0 if ok |
| 1627 | * !0 if execution must be aborted. |
| 1628 | */ |
| 1629 | static |
| 1630 | int lib_ring_buffer_try_switch_slow(enum switch_mode mode, |
| 1631 | struct lttng_ust_lib_ring_buffer *buf, |
| 1632 | struct channel *chan, |
| 1633 | struct switch_offsets *offsets, |
| 1634 | uint64_t *tsc, |
| 1635 | struct lttng_ust_shm_handle *handle) |
| 1636 | { |
| 1637 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
| 1638 | unsigned long off, reserve_commit_diff; |
| 1639 | |
| 1640 | offsets->begin = v_read(config, &buf->offset); |
| 1641 | offsets->old = offsets->begin; |
| 1642 | offsets->switch_old_start = 0; |
| 1643 | off = subbuf_offset(offsets->begin, chan); |
| 1644 | |
| 1645 | *tsc = config->cb.ring_buffer_clock_read(chan); |
| 1646 | |
| 1647 | /* |
| 1648 | * Ensure we flush the header of an empty subbuffer when doing the |
| 1649 | * finalize (SWITCH_FLUSH). This ensures that we end up knowing the |
| 1650 | * total data gathering duration even if there were no records saved |
| 1651 | * after the last buffer switch. |
| 1652 | * In SWITCH_ACTIVE mode, switch the buffer when it contains events. |
| 1653 | * SWITCH_ACTIVE only flushes the current subbuffer, dealing with end of |
| 1654 | * subbuffer header as appropriate. |
| 1655 | * The next record that reserves space will be responsible for |
| 1656 | * populating the following subbuffer header. We choose not to populate |
| 1657 | * the next subbuffer header here because we want to be able to use |
| 1658 | * SWITCH_ACTIVE for periodical buffer flush, which must |
| 1659 | * guarantee that all the buffer content (records and header |
| 1660 | * timestamps) are visible to the reader. This is required for |
| 1661 | * quiescence guarantees for the fusion merge. |
| 1662 | */ |
| 1663 | if (mode != SWITCH_FLUSH && !off) |
| 1664 | return -1; /* we do not have to switch : buffer is empty */ |
| 1665 | |
| 1666 | if (caa_unlikely(off == 0)) { |
| 1667 | unsigned long sb_index, commit_count; |
| 1668 | |
| 1669 | /* |
| 1670 | * We are performing a SWITCH_FLUSH. There may be concurrent |
| 1671 | * writes into the buffer if e.g. invoked while performing a |
| 1672 | * snapshot on an active trace. |
| 1673 | * |
| 1674 | * If the client does not save any header information |
| 1675 | * (sub-buffer header size == 0), don't switch empty subbuffer |
| 1676 | * on finalize, because it is invalid to deliver a completely |
| 1677 | * empty subbuffer. |
| 1678 | */ |
| 1679 | if (!config->cb.subbuffer_header_size()) |
| 1680 | return -1; |
| 1681 | |
| 1682 | /* Test new buffer integrity */ |
| 1683 | sb_index = subbuf_index(offsets->begin, chan); |
| 1684 | commit_count = v_read(config, |
| 1685 | &shmp_index(handle, buf->commit_cold, |
| 1686 | sb_index)->cc_sb); |
| 1687 | reserve_commit_diff = |
| 1688 | (buf_trunc(offsets->begin, chan) |
| 1689 | >> chan->backend.num_subbuf_order) |
| 1690 | - (commit_count & chan->commit_count_mask); |
| 1691 | if (caa_likely(reserve_commit_diff == 0)) { |
| 1692 | /* Next subbuffer not being written to. */ |
| 1693 | if (caa_unlikely(config->mode != RING_BUFFER_OVERWRITE && |
| 1694 | subbuf_trunc(offsets->begin, chan) |
| 1695 | - subbuf_trunc((unsigned long) |
| 1696 | uatomic_read(&buf->consumed), chan) |
| 1697 | >= chan->backend.buf_size)) { |
| 1698 | /* |
| 1699 | * We do not overwrite non consumed buffers |
| 1700 | * and we are full : don't switch. |
| 1701 | */ |
| 1702 | return -1; |
| 1703 | } else { |
| 1704 | /* |
| 1705 | * Next subbuffer not being written to, and we |
| 1706 | * are either in overwrite mode or the buffer is |
| 1707 | * not full. It's safe to write in this new |
| 1708 | * subbuffer. |
| 1709 | */ |
| 1710 | } |
| 1711 | } else { |
| 1712 | /* |
| 1713 | * Next subbuffer reserve offset does not match the |
| 1714 | * commit offset. Don't perform switch in |
| 1715 | * producer-consumer and overwrite mode. Caused by |
| 1716 | * either a writer OOPS or too many nested writes over a |
| 1717 | * reserve/commit pair. |
| 1718 | */ |
| 1719 | return -1; |
| 1720 | } |
| 1721 | |
| 1722 | /* |
| 1723 | * Need to write the subbuffer start header on finalize. |
| 1724 | */ |
| 1725 | offsets->switch_old_start = 1; |
| 1726 | } |
| 1727 | offsets->begin = subbuf_align(offsets->begin, chan); |
| 1728 | /* Note: old points to the next subbuf at offset 0 */ |
| 1729 | offsets->end = offsets->begin; |
| 1730 | return 0; |
| 1731 | } |
| 1732 | |
| 1733 | /* |
| 1734 | * Force a sub-buffer switch. This operation is completely reentrant : can be |
| 1735 | * called while tracing is active with absolutely no lock held. |
| 1736 | * |
| 1737 | * Note, however, that as a v_cmpxchg is used for some atomic |
| 1738 | * operations, this function must be called from the CPU which owns the buffer |
| 1739 | * for a ACTIVE flush. |
| 1740 | */ |
| 1741 | void lib_ring_buffer_switch_slow(struct lttng_ust_lib_ring_buffer *buf, enum switch_mode mode, |
| 1742 | struct lttng_ust_shm_handle *handle) |
| 1743 | { |
| 1744 | struct channel *chan = shmp(handle, buf->backend.chan); |
| 1745 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
| 1746 | struct switch_offsets offsets; |
| 1747 | unsigned long oldidx; |
| 1748 | uint64_t tsc; |
| 1749 | |
| 1750 | offsets.size = 0; |
| 1751 | |
| 1752 | /* |
| 1753 | * Perform retryable operations. |
| 1754 | */ |
| 1755 | do { |
| 1756 | if (lib_ring_buffer_try_switch_slow(mode, buf, chan, &offsets, |
| 1757 | &tsc, handle)) |
| 1758 | return; /* Switch not needed */ |
| 1759 | } while (v_cmpxchg(config, &buf->offset, offsets.old, offsets.end) |
| 1760 | != offsets.old); |
| 1761 | |
| 1762 | /* |
| 1763 | * Atomically update last_tsc. This update races against concurrent |
| 1764 | * atomic updates, but the race will always cause supplementary full TSC |
| 1765 | * records, never the opposite (missing a full TSC record when it would |
| 1766 | * be needed). |
| 1767 | */ |
| 1768 | save_last_tsc(config, buf, tsc); |
| 1769 | |
| 1770 | /* |
| 1771 | * Push the reader if necessary |
| 1772 | */ |
| 1773 | lib_ring_buffer_reserve_push_reader(buf, chan, offsets.old); |
| 1774 | |
| 1775 | oldidx = subbuf_index(offsets.old, chan); |
| 1776 | lib_ring_buffer_clear_noref(config, &buf->backend, oldidx, handle); |
| 1777 | |
| 1778 | /* |
| 1779 | * May need to populate header start on SWITCH_FLUSH. |
| 1780 | */ |
| 1781 | if (offsets.switch_old_start) { |
| 1782 | lib_ring_buffer_switch_old_start(buf, chan, &offsets, tsc, handle); |
| 1783 | offsets.old += config->cb.subbuffer_header_size(); |
| 1784 | } |
| 1785 | |
| 1786 | /* |
| 1787 | * Switch old subbuffer. |
| 1788 | */ |
| 1789 | lib_ring_buffer_switch_old_end(buf, chan, &offsets, tsc, handle); |
| 1790 | } |
| 1791 | |
| 1792 | /* |
| 1793 | * Returns : |
| 1794 | * 0 if ok |
| 1795 | * -ENOSPC if event size is too large for packet. |
| 1796 | * -ENOBUFS if there is currently not enough space in buffer for the event. |
| 1797 | * -EIO if data cannot be written into the buffer for any other reason. |
| 1798 | */ |
| 1799 | static |
| 1800 | int lib_ring_buffer_try_reserve_slow(struct lttng_ust_lib_ring_buffer *buf, |
| 1801 | struct channel *chan, |
| 1802 | struct switch_offsets *offsets, |
| 1803 | struct lttng_ust_lib_ring_buffer_ctx *ctx) |
| 1804 | { |
| 1805 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
| 1806 | struct lttng_ust_shm_handle *handle = ctx->handle; |
| 1807 | unsigned long reserve_commit_diff, offset_cmp; |
| 1808 | |
| 1809 | retry: |
| 1810 | offsets->begin = offset_cmp = v_read(config, &buf->offset); |
| 1811 | offsets->old = offsets->begin; |
| 1812 | offsets->switch_new_start = 0; |
| 1813 | offsets->switch_new_end = 0; |
| 1814 | offsets->switch_old_end = 0; |
| 1815 | offsets->pre_header_padding = 0; |
| 1816 | |
| 1817 | ctx->tsc = config->cb.ring_buffer_clock_read(chan); |
| 1818 | if ((int64_t) ctx->tsc == -EIO) |
| 1819 | return -EIO; |
| 1820 | |
| 1821 | if (last_tsc_overflow(config, buf, ctx->tsc)) |
| 1822 | ctx->rflags |= RING_BUFFER_RFLAG_FULL_TSC; |
| 1823 | |
| 1824 | if (caa_unlikely(subbuf_offset(offsets->begin, ctx->chan) == 0)) { |
| 1825 | offsets->switch_new_start = 1; /* For offsets->begin */ |
| 1826 | } else { |
| 1827 | offsets->size = config->cb.record_header_size(config, chan, |
| 1828 | offsets->begin, |
| 1829 | &offsets->pre_header_padding, |
| 1830 | ctx); |
| 1831 | offsets->size += |
| 1832 | lib_ring_buffer_align(offsets->begin + offsets->size, |
| 1833 | ctx->largest_align) |
| 1834 | + ctx->data_size; |
| 1835 | if (caa_unlikely(subbuf_offset(offsets->begin, chan) + |
| 1836 | offsets->size > chan->backend.subbuf_size)) { |
| 1837 | offsets->switch_old_end = 1; /* For offsets->old */ |
| 1838 | offsets->switch_new_start = 1; /* For offsets->begin */ |
| 1839 | } |
| 1840 | } |
| 1841 | if (caa_unlikely(offsets->switch_new_start)) { |
| 1842 | unsigned long sb_index, commit_count; |
| 1843 | |
| 1844 | /* |
| 1845 | * We are typically not filling the previous buffer completely. |
| 1846 | */ |
| 1847 | if (caa_likely(offsets->switch_old_end)) |
| 1848 | offsets->begin = subbuf_align(offsets->begin, chan); |
| 1849 | offsets->begin = offsets->begin |
| 1850 | + config->cb.subbuffer_header_size(); |
| 1851 | /* Test new buffer integrity */ |
| 1852 | sb_index = subbuf_index(offsets->begin, chan); |
| 1853 | /* |
| 1854 | * Read buf->offset before buf->commit_cold[sb_index].cc_sb. |
| 1855 | * lib_ring_buffer_check_deliver() has the matching |
| 1856 | * memory barriers required around commit_cold cc_sb |
| 1857 | * updates to ensure reserve and commit counter updates |
| 1858 | * are not seen reordered when updated by another CPU. |
| 1859 | */ |
| 1860 | cmm_smp_rmb(); |
| 1861 | commit_count = v_read(config, |
| 1862 | &shmp_index(handle, buf->commit_cold, |
| 1863 | sb_index)->cc_sb); |
| 1864 | /* Read buf->commit_cold[sb_index].cc_sb before buf->offset. */ |
| 1865 | cmm_smp_rmb(); |
| 1866 | if (caa_unlikely(offset_cmp != v_read(config, &buf->offset))) { |
| 1867 | /* |
| 1868 | * The reserve counter have been concurrently updated |
| 1869 | * while we read the commit counter. This means the |
| 1870 | * commit counter we read might not match buf->offset |
| 1871 | * due to concurrent update. We therefore need to retry. |
| 1872 | */ |
| 1873 | goto retry; |
| 1874 | } |
| 1875 | reserve_commit_diff = |
| 1876 | (buf_trunc(offsets->begin, chan) |
| 1877 | >> chan->backend.num_subbuf_order) |
| 1878 | - (commit_count & chan->commit_count_mask); |
| 1879 | if (caa_likely(reserve_commit_diff == 0)) { |
| 1880 | /* Next subbuffer not being written to. */ |
| 1881 | if (caa_unlikely(config->mode != RING_BUFFER_OVERWRITE && |
| 1882 | subbuf_trunc(offsets->begin, chan) |
| 1883 | - subbuf_trunc((unsigned long) |
| 1884 | uatomic_read(&buf->consumed), chan) |
| 1885 | >= chan->backend.buf_size)) { |
| 1886 | unsigned long nr_lost; |
| 1887 | |
| 1888 | /* |
| 1889 | * We do not overwrite non consumed buffers |
| 1890 | * and we are full : record is lost. |
| 1891 | */ |
| 1892 | nr_lost = v_read(config, &buf->records_lost_full); |
| 1893 | v_inc(config, &buf->records_lost_full); |
| 1894 | if ((nr_lost & (DBG_PRINT_NR_LOST - 1)) == 0) { |
| 1895 | DBG("%lu or more records lost in (%s:%d) (buffer full)\n", |
| 1896 | nr_lost + 1, chan->backend.name, |
| 1897 | buf->backend.cpu); |
| 1898 | } |
| 1899 | return -ENOBUFS; |
| 1900 | } else { |
| 1901 | /* |
| 1902 | * Next subbuffer not being written to, and we |
| 1903 | * are either in overwrite mode or the buffer is |
| 1904 | * not full. It's safe to write in this new |
| 1905 | * subbuffer. |
| 1906 | */ |
| 1907 | } |
| 1908 | } else { |
| 1909 | unsigned long nr_lost; |
| 1910 | |
| 1911 | /* |
| 1912 | * Next subbuffer reserve offset does not match the |
| 1913 | * commit offset, and this did not involve update to the |
| 1914 | * reserve counter. Drop record in producer-consumer and |
| 1915 | * overwrite mode. Caused by either a writer OOPS or too |
| 1916 | * many nested writes over a reserve/commit pair. |
| 1917 | */ |
| 1918 | nr_lost = v_read(config, &buf->records_lost_wrap); |
| 1919 | v_inc(config, &buf->records_lost_wrap); |
| 1920 | if ((nr_lost & (DBG_PRINT_NR_LOST - 1)) == 0) { |
| 1921 | DBG("%lu or more records lost in (%s:%d) (wrap-around)\n", |
| 1922 | nr_lost + 1, chan->backend.name, |
| 1923 | buf->backend.cpu); |
| 1924 | } |
| 1925 | return -EIO; |
| 1926 | } |
| 1927 | offsets->size = |
| 1928 | config->cb.record_header_size(config, chan, |
| 1929 | offsets->begin, |
| 1930 | &offsets->pre_header_padding, |
| 1931 | ctx); |
| 1932 | offsets->size += |
| 1933 | lib_ring_buffer_align(offsets->begin + offsets->size, |
| 1934 | ctx->largest_align) |
| 1935 | + ctx->data_size; |
| 1936 | if (caa_unlikely(subbuf_offset(offsets->begin, chan) |
| 1937 | + offsets->size > chan->backend.subbuf_size)) { |
| 1938 | unsigned long nr_lost; |
| 1939 | |
| 1940 | /* |
| 1941 | * Record too big for subbuffers, report error, don't |
| 1942 | * complete the sub-buffer switch. |
| 1943 | */ |
| 1944 | nr_lost = v_read(config, &buf->records_lost_big); |
| 1945 | v_inc(config, &buf->records_lost_big); |
| 1946 | if ((nr_lost & (DBG_PRINT_NR_LOST - 1)) == 0) { |
| 1947 | DBG("%lu or more records lost in (%s:%d) record size " |
| 1948 | " of %zu bytes is too large for buffer\n", |
| 1949 | nr_lost + 1, chan->backend.name, |
| 1950 | buf->backend.cpu, offsets->size); |
| 1951 | } |
| 1952 | return -ENOSPC; |
| 1953 | } else { |
| 1954 | /* |
| 1955 | * We just made a successful buffer switch and the |
| 1956 | * record fits in the new subbuffer. Let's write. |
| 1957 | */ |
| 1958 | } |
| 1959 | } else { |
| 1960 | /* |
| 1961 | * Record fits in the current buffer and we are not on a switch |
| 1962 | * boundary. It's safe to write. |
| 1963 | */ |
| 1964 | } |
| 1965 | offsets->end = offsets->begin + offsets->size; |
| 1966 | |
| 1967 | if (caa_unlikely(subbuf_offset(offsets->end, chan) == 0)) { |
| 1968 | /* |
| 1969 | * The offset_end will fall at the very beginning of the next |
| 1970 | * subbuffer. |
| 1971 | */ |
| 1972 | offsets->switch_new_end = 1; /* For offsets->begin */ |
| 1973 | } |
| 1974 | return 0; |
| 1975 | } |
| 1976 | |
| 1977 | /** |
| 1978 | * lib_ring_buffer_reserve_slow - Atomic slot reservation in a buffer. |
| 1979 | * @ctx: ring buffer context. |
| 1980 | * |
| 1981 | * Return : -NOBUFS if not enough space, -ENOSPC if event size too large, |
| 1982 | * -EIO for other errors, else returns 0. |
| 1983 | * It will take care of sub-buffer switching. |
| 1984 | */ |
| 1985 | int lib_ring_buffer_reserve_slow(struct lttng_ust_lib_ring_buffer_ctx *ctx) |
| 1986 | { |
| 1987 | struct channel *chan = ctx->chan; |
| 1988 | struct lttng_ust_shm_handle *handle = ctx->handle; |
| 1989 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
| 1990 | struct lttng_ust_lib_ring_buffer *buf; |
| 1991 | struct switch_offsets offsets; |
| 1992 | int ret; |
| 1993 | |
| 1994 | if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) |
| 1995 | buf = shmp(handle, chan->backend.buf[ctx->cpu].shmp); |
| 1996 | else |
| 1997 | buf = shmp(handle, chan->backend.buf[0].shmp); |
| 1998 | ctx->buf = buf; |
| 1999 | |
| 2000 | offsets.size = 0; |
| 2001 | |
| 2002 | do { |
| 2003 | ret = lib_ring_buffer_try_reserve_slow(buf, chan, &offsets, |
| 2004 | ctx); |
| 2005 | if (caa_unlikely(ret)) |
| 2006 | return ret; |
| 2007 | } while (caa_unlikely(v_cmpxchg(config, &buf->offset, offsets.old, |
| 2008 | offsets.end) |
| 2009 | != offsets.old)); |
| 2010 | |
| 2011 | /* |
| 2012 | * Atomically update last_tsc. This update races against concurrent |
| 2013 | * atomic updates, but the race will always cause supplementary full TSC |
| 2014 | * records, never the opposite (missing a full TSC record when it would |
| 2015 | * be needed). |
| 2016 | */ |
| 2017 | save_last_tsc(config, buf, ctx->tsc); |
| 2018 | |
| 2019 | /* |
| 2020 | * Push the reader if necessary |
| 2021 | */ |
| 2022 | lib_ring_buffer_reserve_push_reader(buf, chan, offsets.end - 1); |
| 2023 | |
| 2024 | /* |
| 2025 | * Clear noref flag for this subbuffer. |
| 2026 | */ |
| 2027 | lib_ring_buffer_clear_noref(config, &buf->backend, |
| 2028 | subbuf_index(offsets.end - 1, chan), |
| 2029 | handle); |
| 2030 | |
| 2031 | /* |
| 2032 | * Switch old subbuffer if needed. |
| 2033 | */ |
| 2034 | if (caa_unlikely(offsets.switch_old_end)) { |
| 2035 | lib_ring_buffer_clear_noref(config, &buf->backend, |
| 2036 | subbuf_index(offsets.old - 1, chan), |
| 2037 | handle); |
| 2038 | lib_ring_buffer_switch_old_end(buf, chan, &offsets, ctx->tsc, handle); |
| 2039 | } |
| 2040 | |
| 2041 | /* |
| 2042 | * Populate new subbuffer. |
| 2043 | */ |
| 2044 | if (caa_unlikely(offsets.switch_new_start)) |
| 2045 | lib_ring_buffer_switch_new_start(buf, chan, &offsets, ctx->tsc, handle); |
| 2046 | |
| 2047 | if (caa_unlikely(offsets.switch_new_end)) |
| 2048 | lib_ring_buffer_switch_new_end(buf, chan, &offsets, ctx->tsc, handle); |
| 2049 | |
| 2050 | ctx->slot_size = offsets.size; |
| 2051 | ctx->pre_offset = offsets.begin; |
| 2052 | ctx->buf_offset = offsets.begin + offsets.pre_header_padding; |
| 2053 | return 0; |
| 2054 | } |
| 2055 | |
| 2056 | /* |
| 2057 | * Force a read (imply TLS fixup for dlopen) of TLS variables. |
| 2058 | */ |
| 2059 | void lttng_fixup_ringbuffer_tls(void) |
| 2060 | { |
| 2061 | asm volatile ("" : : "m" (URCU_TLS(lib_ring_buffer_nesting))); |
| 2062 | } |
| 2063 | |
| 2064 | void lib_ringbuffer_signal_init(void) |
| 2065 | { |
| 2066 | sigset_t mask; |
| 2067 | int ret; |
| 2068 | |
| 2069 | /* |
| 2070 | * Block signal for entire process, so only our thread processes |
| 2071 | * it. |
| 2072 | */ |
| 2073 | rb_setmask(&mask); |
| 2074 | ret = pthread_sigmask(SIG_BLOCK, &mask, NULL); |
| 2075 | if (ret) { |
| 2076 | errno = ret; |
| 2077 | PERROR("pthread_sigmask"); |
| 2078 | } |
| 2079 | } |