| 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 <fcntl.h> |
| 59 | #include <urcu/compiler.h> |
| 60 | #include <urcu/ref.h> |
| 61 | #include <helper.h> |
| 62 | |
| 63 | #include "smp.h" |
| 64 | #include <lttng/ringbuffer-config.h> |
| 65 | #include "vatomic.h" |
| 66 | #include "backend.h" |
| 67 | #include "frontend.h" |
| 68 | #include "shm.h" |
| 69 | #include "tlsfixup.h" |
| 70 | #include "../liblttng-ust/compat.h" /* For ENODATA */ |
| 71 | |
| 72 | #ifndef max |
| 73 | #define max(a, b) ((a) > (b) ? (a) : (b)) |
| 74 | #endif |
| 75 | |
| 76 | /* Print DBG() messages about events lost only every 1048576 hits */ |
| 77 | #define DBG_PRINT_NR_LOST (1UL << 20) |
| 78 | |
| 79 | /* |
| 80 | * Use POSIX SHM: shm_open(3) and shm_unlink(3). |
| 81 | * close(2) to close the fd returned by shm_open. |
| 82 | * shm_unlink releases the shared memory object name. |
| 83 | * ftruncate(2) sets the size of the memory object. |
| 84 | * mmap/munmap maps the shared memory obj to a virtual address in the |
| 85 | * calling proceess (should be done both in libust and consumer). |
| 86 | * See shm_overview(7) for details. |
| 87 | * Pass file descriptor returned by shm_open(3) to ltt-sessiond through |
| 88 | * a UNIX socket. |
| 89 | * |
| 90 | * Since we don't need to access the object using its name, we can |
| 91 | * immediately shm_unlink(3) it, and only keep the handle with its file |
| 92 | * descriptor. |
| 93 | */ |
| 94 | |
| 95 | /* |
| 96 | * Internal structure representing offsets to use at a sub-buffer switch. |
| 97 | */ |
| 98 | struct switch_offsets { |
| 99 | unsigned long begin, end, old; |
| 100 | size_t pre_header_padding, size; |
| 101 | unsigned int switch_new_start:1, switch_new_end:1, switch_old_start:1, |
| 102 | switch_old_end:1; |
| 103 | }; |
| 104 | |
| 105 | __thread unsigned int lib_ring_buffer_nesting; |
| 106 | |
| 107 | /* |
| 108 | * TODO: this is unused. Errors are saved within the ring buffer. |
| 109 | * Eventually, allow consumerd to print these errors. |
| 110 | */ |
| 111 | static |
| 112 | void lib_ring_buffer_print_errors(struct channel *chan, |
| 113 | struct lttng_ust_lib_ring_buffer *buf, int cpu, |
| 114 | struct lttng_ust_shm_handle *handle) |
| 115 | __attribute__((unused)); |
| 116 | |
| 117 | /** |
| 118 | * lib_ring_buffer_reset - Reset ring buffer to initial values. |
| 119 | * @buf: Ring buffer. |
| 120 | * |
| 121 | * Effectively empty the ring buffer. Should be called when the buffer is not |
| 122 | * used for writing. The ring buffer can be opened for reading, but the reader |
| 123 | * should not be using the iterator concurrently with reset. The previous |
| 124 | * current iterator record is reset. |
| 125 | */ |
| 126 | void lib_ring_buffer_reset(struct lttng_ust_lib_ring_buffer *buf, |
| 127 | struct lttng_ust_shm_handle *handle) |
| 128 | { |
| 129 | struct channel *chan = shmp(handle, buf->backend.chan); |
| 130 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
| 131 | unsigned int i; |
| 132 | |
| 133 | /* |
| 134 | * Reset iterator first. It will put the subbuffer if it currently holds |
| 135 | * it. |
| 136 | */ |
| 137 | v_set(config, &buf->offset, 0); |
| 138 | for (i = 0; i < chan->backend.num_subbuf; i++) { |
| 139 | v_set(config, &shmp_index(handle, buf->commit_hot, i)->cc, 0); |
| 140 | v_set(config, &shmp_index(handle, buf->commit_hot, i)->seq, 0); |
| 141 | v_set(config, &shmp_index(handle, buf->commit_cold, i)->cc_sb, 0); |
| 142 | } |
| 143 | uatomic_set(&buf->consumed, 0); |
| 144 | uatomic_set(&buf->record_disabled, 0); |
| 145 | v_set(config, &buf->last_tsc, 0); |
| 146 | lib_ring_buffer_backend_reset(&buf->backend, handle); |
| 147 | /* Don't reset number of active readers */ |
| 148 | v_set(config, &buf->records_lost_full, 0); |
| 149 | v_set(config, &buf->records_lost_wrap, 0); |
| 150 | v_set(config, &buf->records_lost_big, 0); |
| 151 | v_set(config, &buf->records_count, 0); |
| 152 | v_set(config, &buf->records_overrun, 0); |
| 153 | buf->finalized = 0; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * channel_reset - Reset channel to initial values. |
| 158 | * @chan: Channel. |
| 159 | * |
| 160 | * Effectively empty the channel. Should be called when the channel is not used |
| 161 | * for writing. The channel can be opened for reading, but the reader should not |
| 162 | * be using the iterator concurrently with reset. The previous current iterator |
| 163 | * record is reset. |
| 164 | */ |
| 165 | void channel_reset(struct channel *chan) |
| 166 | { |
| 167 | /* |
| 168 | * Reset iterators first. Will put the subbuffer if held for reading. |
| 169 | */ |
| 170 | uatomic_set(&chan->record_disabled, 0); |
| 171 | /* Don't reset commit_count_mask, still valid */ |
| 172 | channel_backend_reset(&chan->backend); |
| 173 | /* Don't reset switch/read timer interval */ |
| 174 | /* Don't reset notifiers and notifier enable bits */ |
| 175 | /* Don't reset reader reference count */ |
| 176 | } |
| 177 | |
| 178 | /* |
| 179 | * Must be called under cpu hotplug protection. |
| 180 | */ |
| 181 | int lib_ring_buffer_create(struct lttng_ust_lib_ring_buffer *buf, |
| 182 | struct channel_backend *chanb, int cpu, |
| 183 | struct lttng_ust_shm_handle *handle, |
| 184 | struct shm_object *shmobj) |
| 185 | { |
| 186 | const struct lttng_ust_lib_ring_buffer_config *config = &chanb->config; |
| 187 | struct channel *chan = caa_container_of(chanb, struct channel, backend); |
| 188 | void *priv = channel_get_private(chan); |
| 189 | size_t subbuf_header_size; |
| 190 | uint64_t tsc; |
| 191 | int ret; |
| 192 | |
| 193 | /* Test for cpu hotplug */ |
| 194 | if (buf->backend.allocated) |
| 195 | return 0; |
| 196 | |
| 197 | ret = lib_ring_buffer_backend_create(&buf->backend, &chan->backend, |
| 198 | cpu, handle, shmobj); |
| 199 | if (ret) |
| 200 | return ret; |
| 201 | |
| 202 | align_shm(shmobj, __alignof__(struct commit_counters_hot)); |
| 203 | set_shmp(buf->commit_hot, |
| 204 | zalloc_shm(shmobj, |
| 205 | sizeof(struct commit_counters_hot) * chan->backend.num_subbuf)); |
| 206 | if (!shmp(handle, buf->commit_hot)) { |
| 207 | ret = -ENOMEM; |
| 208 | goto free_chanbuf; |
| 209 | } |
| 210 | |
| 211 | align_shm(shmobj, __alignof__(struct commit_counters_cold)); |
| 212 | set_shmp(buf->commit_cold, |
| 213 | zalloc_shm(shmobj, |
| 214 | sizeof(struct commit_counters_cold) * chan->backend.num_subbuf)); |
| 215 | if (!shmp(handle, buf->commit_cold)) { |
| 216 | ret = -ENOMEM; |
| 217 | goto free_commit; |
| 218 | } |
| 219 | |
| 220 | /* |
| 221 | * Write the subbuffer header for first subbuffer so we know the total |
| 222 | * duration of data gathering. |
| 223 | */ |
| 224 | subbuf_header_size = config->cb.subbuffer_header_size(); |
| 225 | v_set(config, &buf->offset, subbuf_header_size); |
| 226 | subbuffer_id_clear_noref(config, &shmp_index(handle, buf->backend.buf_wsb, 0)->id); |
| 227 | tsc = config->cb.ring_buffer_clock_read(shmp(handle, buf->backend.chan)); |
| 228 | config->cb.buffer_begin(buf, tsc, 0, handle); |
| 229 | v_add(config, subbuf_header_size, &shmp_index(handle, buf->commit_hot, 0)->cc); |
| 230 | |
| 231 | if (config->cb.buffer_create) { |
| 232 | ret = config->cb.buffer_create(buf, priv, cpu, chanb->name, handle); |
| 233 | if (ret) |
| 234 | goto free_init; |
| 235 | } |
| 236 | buf->backend.allocated = 1; |
| 237 | return 0; |
| 238 | |
| 239 | /* Error handling */ |
| 240 | free_init: |
| 241 | /* commit_cold will be freed by shm teardown */ |
| 242 | free_commit: |
| 243 | /* commit_hot will be freed by shm teardown */ |
| 244 | free_chanbuf: |
| 245 | return ret; |
| 246 | } |
| 247 | |
| 248 | #if 0 |
| 249 | static void switch_buffer_timer(unsigned long data) |
| 250 | { |
| 251 | struct lttng_ust_lib_ring_buffer *buf = (struct lttng_ust_lib_ring_buffer *)data; |
| 252 | struct channel *chan = shmp(handle, buf->backend.chan); |
| 253 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
| 254 | |
| 255 | /* |
| 256 | * Only flush buffers periodically if readers are active. |
| 257 | */ |
| 258 | if (uatomic_read(&buf->active_readers) || uatomic_read(&buf->active_shadow_readers)) |
| 259 | lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE, handle); |
| 260 | |
| 261 | //TODO timers |
| 262 | //if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) |
| 263 | // mod_timer_pinned(&buf->switch_timer, |
| 264 | // jiffies + chan->switch_timer_interval); |
| 265 | //else |
| 266 | // mod_timer(&buf->switch_timer, |
| 267 | // jiffies + chan->switch_timer_interval); |
| 268 | } |
| 269 | #endif //0 |
| 270 | |
| 271 | static void lib_ring_buffer_start_switch_timer(struct lttng_ust_lib_ring_buffer *buf, |
| 272 | struct lttng_ust_shm_handle *handle) |
| 273 | { |
| 274 | struct channel *chan = shmp(handle, buf->backend.chan); |
| 275 | //const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
| 276 | |
| 277 | if (!chan->switch_timer_interval || buf->switch_timer_enabled) |
| 278 | return; |
| 279 | //TODO |
| 280 | //init_timer(&buf->switch_timer); |
| 281 | //buf->switch_timer.function = switch_buffer_timer; |
| 282 | //buf->switch_timer.expires = jiffies + chan->switch_timer_interval; |
| 283 | //buf->switch_timer.data = (unsigned long)buf; |
| 284 | //if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) |
| 285 | // add_timer_on(&buf->switch_timer, buf->backend.cpu); |
| 286 | //else |
| 287 | // add_timer(&buf->switch_timer); |
| 288 | buf->switch_timer_enabled = 1; |
| 289 | } |
| 290 | |
| 291 | static void lib_ring_buffer_stop_switch_timer(struct lttng_ust_lib_ring_buffer *buf, |
| 292 | struct lttng_ust_shm_handle *handle) |
| 293 | { |
| 294 | struct channel *chan = shmp(handle, buf->backend.chan); |
| 295 | |
| 296 | if (!chan->switch_timer_interval || !buf->switch_timer_enabled) |
| 297 | return; |
| 298 | |
| 299 | //TODO |
| 300 | //del_timer_sync(&buf->switch_timer); |
| 301 | buf->switch_timer_enabled = 0; |
| 302 | } |
| 303 | |
| 304 | #if 0 |
| 305 | /* |
| 306 | * Polling timer to check the channels for data. |
| 307 | */ |
| 308 | static void read_buffer_timer(unsigned long data) |
| 309 | { |
| 310 | struct lttng_ust_lib_ring_buffer *buf = (struct lttng_ust_lib_ring_buffer *)data; |
| 311 | struct channel *chan = shmp(handle, buf->backend.chan); |
| 312 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
| 313 | |
| 314 | CHAN_WARN_ON(chan, !buf->backend.allocated); |
| 315 | |
| 316 | if (uatomic_read(&buf->active_readers) || uatomic_read(&buf->active_shadow_readers)) |
| 317 | && lib_ring_buffer_poll_deliver(config, buf, chan)) { |
| 318 | //TODO |
| 319 | //wake_up_interruptible(&buf->read_wait); |
| 320 | //wake_up_interruptible(&chan->read_wait); |
| 321 | } |
| 322 | |
| 323 | //TODO |
| 324 | //if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) |
| 325 | // mod_timer_pinned(&buf->read_timer, |
| 326 | // jiffies + chan->read_timer_interval); |
| 327 | //else |
| 328 | // mod_timer(&buf->read_timer, |
| 329 | // jiffies + chan->read_timer_interval); |
| 330 | } |
| 331 | #endif //0 |
| 332 | |
| 333 | static void lib_ring_buffer_start_read_timer(struct lttng_ust_lib_ring_buffer *buf, |
| 334 | struct lttng_ust_shm_handle *handle) |
| 335 | { |
| 336 | struct channel *chan = shmp(handle, buf->backend.chan); |
| 337 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
| 338 | |
| 339 | if (config->wakeup != RING_BUFFER_WAKEUP_BY_TIMER |
| 340 | || !chan->read_timer_interval |
| 341 | || buf->read_timer_enabled) |
| 342 | return; |
| 343 | |
| 344 | //TODO |
| 345 | //init_timer(&buf->read_timer); |
| 346 | //buf->read_timer.function = read_buffer_timer; |
| 347 | //buf->read_timer.expires = jiffies + chan->read_timer_interval; |
| 348 | //buf->read_timer.data = (unsigned long)buf; |
| 349 | |
| 350 | //if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) |
| 351 | // add_timer_on(&buf->read_timer, buf->backend.cpu); |
| 352 | //else |
| 353 | // add_timer(&buf->read_timer); |
| 354 | buf->read_timer_enabled = 1; |
| 355 | } |
| 356 | |
| 357 | static void lib_ring_buffer_stop_read_timer(struct lttng_ust_lib_ring_buffer *buf, |
| 358 | struct lttng_ust_shm_handle *handle) |
| 359 | { |
| 360 | struct channel *chan = shmp(handle, buf->backend.chan); |
| 361 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
| 362 | |
| 363 | if (config->wakeup != RING_BUFFER_WAKEUP_BY_TIMER |
| 364 | || !chan->read_timer_interval |
| 365 | || !buf->read_timer_enabled) |
| 366 | return; |
| 367 | |
| 368 | //TODO |
| 369 | //del_timer_sync(&buf->read_timer); |
| 370 | /* |
| 371 | * do one more check to catch data that has been written in the last |
| 372 | * timer period. |
| 373 | */ |
| 374 | if (lib_ring_buffer_poll_deliver(config, buf, chan, handle)) { |
| 375 | //TODO |
| 376 | //wake_up_interruptible(&buf->read_wait); |
| 377 | //wake_up_interruptible(&chan->read_wait); |
| 378 | } |
| 379 | buf->read_timer_enabled = 0; |
| 380 | } |
| 381 | |
| 382 | static void channel_unregister_notifiers(struct channel *chan, |
| 383 | struct lttng_ust_shm_handle *handle) |
| 384 | { |
| 385 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
| 386 | int cpu; |
| 387 | |
| 388 | if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) { |
| 389 | for_each_possible_cpu(cpu) { |
| 390 | struct lttng_ust_lib_ring_buffer *buf = shmp(handle, chan->backend.buf[cpu].shmp); |
| 391 | |
| 392 | lib_ring_buffer_stop_switch_timer(buf, handle); |
| 393 | lib_ring_buffer_stop_read_timer(buf, handle); |
| 394 | } |
| 395 | } else { |
| 396 | struct lttng_ust_lib_ring_buffer *buf = shmp(handle, chan->backend.buf[0].shmp); |
| 397 | |
| 398 | lib_ring_buffer_stop_switch_timer(buf, handle); |
| 399 | lib_ring_buffer_stop_read_timer(buf, handle); |
| 400 | } |
| 401 | //channel_backend_unregister_notifiers(&chan->backend); |
| 402 | } |
| 403 | |
| 404 | static void channel_free(struct channel *chan, struct lttng_ust_shm_handle *handle, |
| 405 | int shadow) |
| 406 | { |
| 407 | if (!shadow) |
| 408 | channel_backend_free(&chan->backend, handle); |
| 409 | /* chan is freed by shm teardown */ |
| 410 | shm_object_table_destroy(handle->table); |
| 411 | free(handle); |
| 412 | } |
| 413 | |
| 414 | /** |
| 415 | * channel_create - Create channel. |
| 416 | * @config: ring buffer instance configuration |
| 417 | * @name: name of the channel |
| 418 | * @priv_data: ring buffer client private data area pointer (output) |
| 419 | * @priv_data_size: length, in bytes, of the private data area. |
| 420 | * @priv_data_init: initialization data for private data. |
| 421 | * @buf_addr: pointer the the beginning of the preallocated buffer contiguous |
| 422 | * address mapping. It is used only by RING_BUFFER_STATIC |
| 423 | * configuration. It can be set to NULL for other backends. |
| 424 | * @subbuf_size: subbuffer size |
| 425 | * @num_subbuf: number of subbuffers |
| 426 | * @switch_timer_interval: Time interval (in us) to fill sub-buffers with |
| 427 | * padding to let readers get those sub-buffers. |
| 428 | * Used for live streaming. |
| 429 | * @read_timer_interval: Time interval (in us) to wake up pending readers. |
| 430 | * |
| 431 | * Holds cpu hotplug. |
| 432 | * Returns NULL on failure. |
| 433 | */ |
| 434 | struct lttng_ust_shm_handle *channel_create(const struct lttng_ust_lib_ring_buffer_config *config, |
| 435 | const char *name, |
| 436 | void **priv_data, |
| 437 | size_t priv_data_align, |
| 438 | size_t priv_data_size, |
| 439 | void *priv_data_init, |
| 440 | void *buf_addr, size_t subbuf_size, |
| 441 | size_t num_subbuf, unsigned int switch_timer_interval, |
| 442 | unsigned int read_timer_interval, |
| 443 | int **shm_fd, int **wait_fd, uint64_t **memory_map_size) |
| 444 | { |
| 445 | int ret, cpu; |
| 446 | size_t shmsize, chansize; |
| 447 | struct channel *chan; |
| 448 | struct lttng_ust_shm_handle *handle; |
| 449 | struct shm_object *shmobj; |
| 450 | struct shm_ref *ref; |
| 451 | |
| 452 | if (lib_ring_buffer_check_config(config, switch_timer_interval, |
| 453 | read_timer_interval)) |
| 454 | return NULL; |
| 455 | |
| 456 | handle = zmalloc(sizeof(struct lttng_ust_shm_handle)); |
| 457 | if (!handle) |
| 458 | return NULL; |
| 459 | |
| 460 | /* Allocate table for channel + per-cpu buffers */ |
| 461 | handle->table = shm_object_table_create(1 + num_possible_cpus()); |
| 462 | if (!handle->table) |
| 463 | goto error_table_alloc; |
| 464 | |
| 465 | /* Calculate the shm allocation layout */ |
| 466 | shmsize = sizeof(struct channel); |
| 467 | shmsize += offset_align(shmsize, __alignof__(struct lttng_ust_lib_ring_buffer_shmp)); |
| 468 | if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) |
| 469 | shmsize += sizeof(struct lttng_ust_lib_ring_buffer_shmp) * num_possible_cpus(); |
| 470 | else |
| 471 | shmsize += sizeof(struct lttng_ust_lib_ring_buffer_shmp); |
| 472 | chansize = shmsize; |
| 473 | shmsize += offset_align(shmsize, priv_data_align); |
| 474 | shmsize += priv_data_size; |
| 475 | |
| 476 | shmobj = shm_object_table_append(handle->table, shmsize); |
| 477 | if (!shmobj) |
| 478 | goto error_append; |
| 479 | /* struct channel is at object 0, offset 0 (hardcoded) */ |
| 480 | set_shmp(handle->chan, zalloc_shm(shmobj, chansize)); |
| 481 | assert(handle->chan._ref.index == 0); |
| 482 | assert(handle->chan._ref.offset == 0); |
| 483 | chan = shmp(handle, handle->chan); |
| 484 | if (!chan) |
| 485 | goto error_append; |
| 486 | |
| 487 | /* space for private data */ |
| 488 | if (priv_data_size) { |
| 489 | DECLARE_SHMP(void, priv_data_alloc); |
| 490 | |
| 491 | align_shm(shmobj, priv_data_align); |
| 492 | chan->priv_data_offset = shmobj->allocated_len; |
| 493 | set_shmp(priv_data_alloc, zalloc_shm(shmobj, priv_data_size)); |
| 494 | if (!shmp(handle, priv_data_alloc)) |
| 495 | goto error_append; |
| 496 | *priv_data = channel_get_private(chan); |
| 497 | memcpy(*priv_data, priv_data_init, priv_data_size); |
| 498 | } else { |
| 499 | chan->priv_data_offset = -1; |
| 500 | *priv_data = NULL; |
| 501 | } |
| 502 | |
| 503 | ret = channel_backend_init(&chan->backend, name, config, |
| 504 | subbuf_size, num_subbuf, handle); |
| 505 | if (ret) |
| 506 | goto error_backend_init; |
| 507 | |
| 508 | chan->commit_count_mask = (~0UL >> chan->backend.num_subbuf_order); |
| 509 | //TODO |
| 510 | //chan->switch_timer_interval = usecs_to_jiffies(switch_timer_interval); |
| 511 | //chan->read_timer_interval = usecs_to_jiffies(read_timer_interval); |
| 512 | //TODO |
| 513 | //init_waitqueue_head(&chan->read_wait); |
| 514 | //init_waitqueue_head(&chan->hp_wait); |
| 515 | |
| 516 | if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) { |
| 517 | /* |
| 518 | * In case of non-hotplug cpu, if the ring-buffer is allocated |
| 519 | * in early initcall, it will not be notified of secondary cpus. |
| 520 | * In that off case, we need to allocate for all possible cpus. |
| 521 | */ |
| 522 | for_each_possible_cpu(cpu) { |
| 523 | struct lttng_ust_lib_ring_buffer *buf = shmp(handle, chan->backend.buf[cpu].shmp); |
| 524 | lib_ring_buffer_start_switch_timer(buf, handle); |
| 525 | lib_ring_buffer_start_read_timer(buf, handle); |
| 526 | } |
| 527 | } else { |
| 528 | struct lttng_ust_lib_ring_buffer *buf = shmp(handle, chan->backend.buf[0].shmp); |
| 529 | |
| 530 | lib_ring_buffer_start_switch_timer(buf, handle); |
| 531 | lib_ring_buffer_start_read_timer(buf, handle); |
| 532 | } |
| 533 | ref = &handle->chan._ref; |
| 534 | shm_get_object_data(handle, ref, shm_fd, wait_fd, memory_map_size); |
| 535 | return handle; |
| 536 | |
| 537 | error_backend_init: |
| 538 | error_append: |
| 539 | shm_object_table_destroy(handle->table); |
| 540 | error_table_alloc: |
| 541 | free(handle); |
| 542 | return NULL; |
| 543 | } |
| 544 | |
| 545 | struct lttng_ust_shm_handle *channel_handle_create(int shm_fd, int wait_fd, |
| 546 | uint64_t memory_map_size) |
| 547 | { |
| 548 | struct lttng_ust_shm_handle *handle; |
| 549 | struct shm_object *object; |
| 550 | |
| 551 | handle = zmalloc(sizeof(struct lttng_ust_shm_handle)); |
| 552 | if (!handle) |
| 553 | return NULL; |
| 554 | |
| 555 | /* Allocate table for channel + per-cpu buffers */ |
| 556 | handle->table = shm_object_table_create(1 + num_possible_cpus()); |
| 557 | if (!handle->table) |
| 558 | goto error_table_alloc; |
| 559 | /* Add channel object */ |
| 560 | object = shm_object_table_append_shadow(handle->table, |
| 561 | shm_fd, wait_fd, memory_map_size); |
| 562 | if (!object) |
| 563 | goto error_table_object; |
| 564 | /* struct channel is at object 0, offset 0 (hardcoded) */ |
| 565 | handle->chan._ref.index = 0; |
| 566 | handle->chan._ref.offset = 0; |
| 567 | return handle; |
| 568 | |
| 569 | error_table_object: |
| 570 | shm_object_table_destroy(handle->table); |
| 571 | error_table_alloc: |
| 572 | free(handle); |
| 573 | return NULL; |
| 574 | } |
| 575 | |
| 576 | int channel_handle_add_stream(struct lttng_ust_shm_handle *handle, |
| 577 | int shm_fd, int wait_fd, uint64_t memory_map_size) |
| 578 | { |
| 579 | struct shm_object *object; |
| 580 | |
| 581 | /* Add stream object */ |
| 582 | object = shm_object_table_append_shadow(handle->table, |
| 583 | shm_fd, wait_fd, memory_map_size); |
| 584 | if (!object) |
| 585 | return -1; |
| 586 | return 0; |
| 587 | } |
| 588 | |
| 589 | static |
| 590 | void channel_release(struct channel *chan, struct lttng_ust_shm_handle *handle, |
| 591 | int shadow) |
| 592 | { |
| 593 | channel_free(chan, handle, shadow); |
| 594 | } |
| 595 | |
| 596 | /** |
| 597 | * channel_destroy - Finalize, wait for q.s. and destroy channel. |
| 598 | * @chan: channel to destroy |
| 599 | * |
| 600 | * Holds cpu hotplug. |
| 601 | * Call "destroy" callback, finalize channels, decrement the channel |
| 602 | * reference count. Note that when readers have completed data |
| 603 | * consumption of finalized channels, get_subbuf() will return -ENODATA. |
| 604 | * They should release their handle at that point. |
| 605 | */ |
| 606 | void channel_destroy(struct channel *chan, struct lttng_ust_shm_handle *handle, |
| 607 | int shadow) |
| 608 | { |
| 609 | if (shadow) { |
| 610 | channel_release(chan, handle, shadow); |
| 611 | return; |
| 612 | } |
| 613 | |
| 614 | channel_unregister_notifiers(chan, handle); |
| 615 | |
| 616 | /* |
| 617 | * Note: the consumer takes care of finalizing and switching the |
| 618 | * buffers. |
| 619 | */ |
| 620 | |
| 621 | /* |
| 622 | * sessiond/consumer are keeping a reference on the shm file |
| 623 | * descriptor directly. No need to refcount. |
| 624 | */ |
| 625 | channel_release(chan, handle, shadow); |
| 626 | return; |
| 627 | } |
| 628 | |
| 629 | struct lttng_ust_lib_ring_buffer *channel_get_ring_buffer( |
| 630 | const struct lttng_ust_lib_ring_buffer_config *config, |
| 631 | struct channel *chan, int cpu, |
| 632 | struct lttng_ust_shm_handle *handle, |
| 633 | int **shm_fd, int **wait_fd, |
| 634 | uint64_t **memory_map_size) |
| 635 | { |
| 636 | struct shm_ref *ref; |
| 637 | |
| 638 | if (config->alloc == RING_BUFFER_ALLOC_GLOBAL) { |
| 639 | ref = &chan->backend.buf[0].shmp._ref; |
| 640 | shm_get_object_data(handle, ref, shm_fd, wait_fd, |
| 641 | memory_map_size); |
| 642 | return shmp(handle, chan->backend.buf[0].shmp); |
| 643 | } else { |
| 644 | if (cpu >= num_possible_cpus()) |
| 645 | return NULL; |
| 646 | ref = &chan->backend.buf[cpu].shmp._ref; |
| 647 | shm_get_object_data(handle, ref, shm_fd, wait_fd, |
| 648 | memory_map_size); |
| 649 | return shmp(handle, chan->backend.buf[cpu].shmp); |
| 650 | } |
| 651 | } |
| 652 | |
| 653 | int lib_ring_buffer_open_read(struct lttng_ust_lib_ring_buffer *buf, |
| 654 | struct lttng_ust_shm_handle *handle, |
| 655 | int shadow) |
| 656 | { |
| 657 | if (shadow) { |
| 658 | if (uatomic_cmpxchg(&buf->active_shadow_readers, 0, 1) != 0) |
| 659 | return -EBUSY; |
| 660 | cmm_smp_mb(); |
| 661 | return 0; |
| 662 | } |
| 663 | if (uatomic_cmpxchg(&buf->active_readers, 0, 1) != 0) |
| 664 | return -EBUSY; |
| 665 | cmm_smp_mb(); |
| 666 | return 0; |
| 667 | } |
| 668 | |
| 669 | void lib_ring_buffer_release_read(struct lttng_ust_lib_ring_buffer *buf, |
| 670 | struct lttng_ust_shm_handle *handle, |
| 671 | int shadow) |
| 672 | { |
| 673 | struct channel *chan = shmp(handle, buf->backend.chan); |
| 674 | |
| 675 | if (shadow) { |
| 676 | CHAN_WARN_ON(chan, uatomic_read(&buf->active_shadow_readers) != 1); |
| 677 | cmm_smp_mb(); |
| 678 | uatomic_dec(&buf->active_shadow_readers); |
| 679 | return; |
| 680 | } |
| 681 | CHAN_WARN_ON(chan, uatomic_read(&buf->active_readers) != 1); |
| 682 | cmm_smp_mb(); |
| 683 | uatomic_dec(&buf->active_readers); |
| 684 | } |
| 685 | |
| 686 | /** |
| 687 | * lib_ring_buffer_snapshot - save subbuffer position snapshot (for read) |
| 688 | * @buf: ring buffer |
| 689 | * @consumed: consumed count indicating the position where to read |
| 690 | * @produced: produced count, indicates position when to stop reading |
| 691 | * |
| 692 | * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no |
| 693 | * data to read at consumed position, or 0 if the get operation succeeds. |
| 694 | */ |
| 695 | |
| 696 | int lib_ring_buffer_snapshot(struct lttng_ust_lib_ring_buffer *buf, |
| 697 | unsigned long *consumed, unsigned long *produced, |
| 698 | struct lttng_ust_shm_handle *handle) |
| 699 | { |
| 700 | struct channel *chan = shmp(handle, buf->backend.chan); |
| 701 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
| 702 | unsigned long consumed_cur, write_offset; |
| 703 | int finalized; |
| 704 | |
| 705 | finalized = CMM_ACCESS_ONCE(buf->finalized); |
| 706 | /* |
| 707 | * Read finalized before counters. |
| 708 | */ |
| 709 | cmm_smp_rmb(); |
| 710 | consumed_cur = uatomic_read(&buf->consumed); |
| 711 | /* |
| 712 | * No need to issue a memory barrier between consumed count read and |
| 713 | * write offset read, because consumed count can only change |
| 714 | * concurrently in overwrite mode, and we keep a sequence counter |
| 715 | * identifier derived from the write offset to check we are getting |
| 716 | * the same sub-buffer we are expecting (the sub-buffers are atomically |
| 717 | * "tagged" upon writes, tags are checked upon read). |
| 718 | */ |
| 719 | write_offset = v_read(config, &buf->offset); |
| 720 | |
| 721 | /* |
| 722 | * Check that we are not about to read the same subbuffer in |
| 723 | * which the writer head is. |
| 724 | */ |
| 725 | if (subbuf_trunc(write_offset, chan) - subbuf_trunc(consumed_cur, chan) |
| 726 | == 0) |
| 727 | goto nodata; |
| 728 | |
| 729 | *consumed = consumed_cur; |
| 730 | *produced = subbuf_trunc(write_offset, chan); |
| 731 | |
| 732 | return 0; |
| 733 | |
| 734 | nodata: |
| 735 | /* |
| 736 | * The memory barriers __wait_event()/wake_up_interruptible() take care |
| 737 | * of "raw_spin_is_locked" memory ordering. |
| 738 | */ |
| 739 | if (finalized) |
| 740 | return -ENODATA; |
| 741 | else |
| 742 | return -EAGAIN; |
| 743 | } |
| 744 | |
| 745 | /** |
| 746 | * lib_ring_buffer_put_snapshot - move consumed counter forward |
| 747 | * @buf: ring buffer |
| 748 | * @consumed_new: new consumed count value |
| 749 | */ |
| 750 | void lib_ring_buffer_move_consumer(struct lttng_ust_lib_ring_buffer *buf, |
| 751 | unsigned long consumed_new, |
| 752 | struct lttng_ust_shm_handle *handle) |
| 753 | { |
| 754 | struct lttng_ust_lib_ring_buffer_backend *bufb = &buf->backend; |
| 755 | struct channel *chan = shmp(handle, bufb->chan); |
| 756 | unsigned long consumed; |
| 757 | |
| 758 | CHAN_WARN_ON(chan, uatomic_read(&buf->active_readers) != 1 |
| 759 | && uatomic_read(&buf->active_shadow_readers) != 1); |
| 760 | |
| 761 | /* |
| 762 | * Only push the consumed value forward. |
| 763 | * If the consumed cmpxchg fails, this is because we have been pushed by |
| 764 | * the writer in flight recorder mode. |
| 765 | */ |
| 766 | consumed = uatomic_read(&buf->consumed); |
| 767 | while ((long) consumed - (long) consumed_new < 0) |
| 768 | consumed = uatomic_cmpxchg(&buf->consumed, consumed, |
| 769 | consumed_new); |
| 770 | } |
| 771 | |
| 772 | /** |
| 773 | * lib_ring_buffer_get_subbuf - get exclusive access to subbuffer for reading |
| 774 | * @buf: ring buffer |
| 775 | * @consumed: consumed count indicating the position where to read |
| 776 | * |
| 777 | * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no |
| 778 | * data to read at consumed position, or 0 if the get operation succeeds. |
| 779 | */ |
| 780 | int lib_ring_buffer_get_subbuf(struct lttng_ust_lib_ring_buffer *buf, |
| 781 | unsigned long consumed, |
| 782 | struct lttng_ust_shm_handle *handle) |
| 783 | { |
| 784 | struct channel *chan = shmp(handle, buf->backend.chan); |
| 785 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
| 786 | unsigned long consumed_cur, consumed_idx, commit_count, write_offset; |
| 787 | int ret; |
| 788 | int finalized; |
| 789 | |
| 790 | retry: |
| 791 | finalized = CMM_ACCESS_ONCE(buf->finalized); |
| 792 | /* |
| 793 | * Read finalized before counters. |
| 794 | */ |
| 795 | cmm_smp_rmb(); |
| 796 | consumed_cur = uatomic_read(&buf->consumed); |
| 797 | consumed_idx = subbuf_index(consumed, chan); |
| 798 | commit_count = v_read(config, &shmp_index(handle, buf->commit_cold, consumed_idx)->cc_sb); |
| 799 | /* |
| 800 | * Make sure we read the commit count before reading the buffer |
| 801 | * data and the write offset. Correct consumed offset ordering |
| 802 | * wrt commit count is insured by the use of cmpxchg to update |
| 803 | * the consumed offset. |
| 804 | */ |
| 805 | /* |
| 806 | * Local rmb to match the remote wmb to read the commit count |
| 807 | * before the buffer data and the write offset. |
| 808 | */ |
| 809 | cmm_smp_rmb(); |
| 810 | |
| 811 | write_offset = v_read(config, &buf->offset); |
| 812 | |
| 813 | /* |
| 814 | * Check that the buffer we are getting is after or at consumed_cur |
| 815 | * position. |
| 816 | */ |
| 817 | if ((long) subbuf_trunc(consumed, chan) |
| 818 | - (long) subbuf_trunc(consumed_cur, chan) < 0) |
| 819 | goto nodata; |
| 820 | |
| 821 | /* |
| 822 | * Check that the subbuffer we are trying to consume has been |
| 823 | * already fully committed. |
| 824 | */ |
| 825 | if (((commit_count - chan->backend.subbuf_size) |
| 826 | & chan->commit_count_mask) |
| 827 | - (buf_trunc(consumed_cur, chan) |
| 828 | >> chan->backend.num_subbuf_order) |
| 829 | != 0) |
| 830 | goto nodata; |
| 831 | |
| 832 | /* |
| 833 | * Check that we are not about to read the same subbuffer in |
| 834 | * which the writer head is. |
| 835 | */ |
| 836 | if (subbuf_trunc(write_offset, chan) - subbuf_trunc(consumed_cur, chan) |
| 837 | == 0) |
| 838 | goto nodata; |
| 839 | |
| 840 | /* |
| 841 | * Failure to get the subbuffer causes a busy-loop retry without going |
| 842 | * to a wait queue. These are caused by short-lived race windows where |
| 843 | * the writer is getting access to a subbuffer we were trying to get |
| 844 | * access to. Also checks that the "consumed" buffer count we are |
| 845 | * looking for matches the one contained in the subbuffer id. |
| 846 | */ |
| 847 | ret = update_read_sb_index(config, &buf->backend, &chan->backend, |
| 848 | consumed_idx, buf_trunc_val(consumed, chan), |
| 849 | handle); |
| 850 | if (ret) |
| 851 | goto retry; |
| 852 | subbuffer_id_clear_noref(config, &buf->backend.buf_rsb.id); |
| 853 | |
| 854 | buf->get_subbuf_consumed = consumed; |
| 855 | buf->get_subbuf = 1; |
| 856 | |
| 857 | return 0; |
| 858 | |
| 859 | nodata: |
| 860 | /* |
| 861 | * The memory barriers __wait_event()/wake_up_interruptible() take care |
| 862 | * of "raw_spin_is_locked" memory ordering. |
| 863 | */ |
| 864 | if (finalized) |
| 865 | return -ENODATA; |
| 866 | else |
| 867 | return -EAGAIN; |
| 868 | } |
| 869 | |
| 870 | /** |
| 871 | * lib_ring_buffer_put_subbuf - release exclusive subbuffer access |
| 872 | * @buf: ring buffer |
| 873 | */ |
| 874 | void lib_ring_buffer_put_subbuf(struct lttng_ust_lib_ring_buffer *buf, |
| 875 | struct lttng_ust_shm_handle *handle) |
| 876 | { |
| 877 | struct lttng_ust_lib_ring_buffer_backend *bufb = &buf->backend; |
| 878 | struct channel *chan = shmp(handle, bufb->chan); |
| 879 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
| 880 | unsigned long read_sb_bindex, consumed_idx, consumed; |
| 881 | |
| 882 | CHAN_WARN_ON(chan, uatomic_read(&buf->active_readers) != 1 |
| 883 | && uatomic_read(&buf->active_shadow_readers) != 1); |
| 884 | |
| 885 | if (!buf->get_subbuf) { |
| 886 | /* |
| 887 | * Reader puts a subbuffer it did not get. |
| 888 | */ |
| 889 | CHAN_WARN_ON(chan, 1); |
| 890 | return; |
| 891 | } |
| 892 | consumed = buf->get_subbuf_consumed; |
| 893 | buf->get_subbuf = 0; |
| 894 | |
| 895 | /* |
| 896 | * Clear the records_unread counter. (overruns counter) |
| 897 | * Can still be non-zero if a file reader simply grabbed the data |
| 898 | * without using iterators. |
| 899 | * Can be below zero if an iterator is used on a snapshot more than |
| 900 | * once. |
| 901 | */ |
| 902 | read_sb_bindex = subbuffer_id_get_index(config, bufb->buf_rsb.id); |
| 903 | v_add(config, v_read(config, |
| 904 | &shmp(handle, shmp_index(handle, bufb->array, read_sb_bindex)->shmp)->records_unread), |
| 905 | &bufb->records_read); |
| 906 | v_set(config, &shmp(handle, shmp_index(handle, bufb->array, read_sb_bindex)->shmp)->records_unread, 0); |
| 907 | CHAN_WARN_ON(chan, config->mode == RING_BUFFER_OVERWRITE |
| 908 | && subbuffer_id_is_noref(config, bufb->buf_rsb.id)); |
| 909 | subbuffer_id_set_noref(config, &bufb->buf_rsb.id); |
| 910 | |
| 911 | /* |
| 912 | * Exchange the reader subbuffer with the one we put in its place in the |
| 913 | * writer subbuffer table. Expect the original consumed count. If |
| 914 | * update_read_sb_index fails, this is because the writer updated the |
| 915 | * subbuffer concurrently. We should therefore keep the subbuffer we |
| 916 | * currently have: it has become invalid to try reading this sub-buffer |
| 917 | * consumed count value anyway. |
| 918 | */ |
| 919 | consumed_idx = subbuf_index(consumed, chan); |
| 920 | update_read_sb_index(config, &buf->backend, &chan->backend, |
| 921 | consumed_idx, buf_trunc_val(consumed, chan), |
| 922 | handle); |
| 923 | /* |
| 924 | * update_read_sb_index return value ignored. Don't exchange sub-buffer |
| 925 | * if the writer concurrently updated it. |
| 926 | */ |
| 927 | } |
| 928 | |
| 929 | /* |
| 930 | * cons_offset is an iterator on all subbuffer offsets between the reader |
| 931 | * position and the writer position. (inclusive) |
| 932 | */ |
| 933 | static |
| 934 | void lib_ring_buffer_print_subbuffer_errors(struct lttng_ust_lib_ring_buffer *buf, |
| 935 | struct channel *chan, |
| 936 | unsigned long cons_offset, |
| 937 | int cpu, |
| 938 | struct lttng_ust_shm_handle *handle) |
| 939 | { |
| 940 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
| 941 | unsigned long cons_idx, commit_count, commit_count_sb; |
| 942 | |
| 943 | cons_idx = subbuf_index(cons_offset, chan); |
| 944 | commit_count = v_read(config, &shmp_index(handle, buf->commit_hot, cons_idx)->cc); |
| 945 | commit_count_sb = v_read(config, &shmp_index(handle, buf->commit_cold, cons_idx)->cc_sb); |
| 946 | |
| 947 | if (subbuf_offset(commit_count, chan) != 0) |
| 948 | DBG("ring buffer %s, cpu %d: " |
| 949 | "commit count in subbuffer %lu,\n" |
| 950 | "expecting multiples of %lu bytes\n" |
| 951 | " [ %lu bytes committed, %lu bytes reader-visible ]\n", |
| 952 | chan->backend.name, cpu, cons_idx, |
| 953 | chan->backend.subbuf_size, |
| 954 | commit_count, commit_count_sb); |
| 955 | |
| 956 | DBG("ring buffer: %s, cpu %d: %lu bytes committed\n", |
| 957 | chan->backend.name, cpu, commit_count); |
| 958 | } |
| 959 | |
| 960 | static |
| 961 | void lib_ring_buffer_print_buffer_errors(struct lttng_ust_lib_ring_buffer *buf, |
| 962 | struct channel *chan, |
| 963 | void *priv, int cpu, |
| 964 | struct lttng_ust_shm_handle *handle) |
| 965 | { |
| 966 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
| 967 | unsigned long write_offset, cons_offset; |
| 968 | |
| 969 | /* |
| 970 | * No need to order commit_count, write_offset and cons_offset reads |
| 971 | * because we execute at teardown when no more writer nor reader |
| 972 | * references are left. |
| 973 | */ |
| 974 | write_offset = v_read(config, &buf->offset); |
| 975 | cons_offset = uatomic_read(&buf->consumed); |
| 976 | if (write_offset != cons_offset) |
| 977 | DBG("ring buffer %s, cpu %d: " |
| 978 | "non-consumed data\n" |
| 979 | " [ %lu bytes written, %lu bytes read ]\n", |
| 980 | chan->backend.name, cpu, write_offset, cons_offset); |
| 981 | |
| 982 | for (cons_offset = uatomic_read(&buf->consumed); |
| 983 | (long) (subbuf_trunc((unsigned long) v_read(config, &buf->offset), |
| 984 | chan) |
| 985 | - cons_offset) > 0; |
| 986 | cons_offset = subbuf_align(cons_offset, chan)) |
| 987 | lib_ring_buffer_print_subbuffer_errors(buf, chan, cons_offset, |
| 988 | cpu, handle); |
| 989 | } |
| 990 | |
| 991 | static |
| 992 | void lib_ring_buffer_print_errors(struct channel *chan, |
| 993 | struct lttng_ust_lib_ring_buffer *buf, int cpu, |
| 994 | struct lttng_ust_shm_handle *handle) |
| 995 | { |
| 996 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
| 997 | void *priv = channel_get_private(chan); |
| 998 | |
| 999 | if (!strcmp(chan->backend.name, "relay-metadata-mmap")) { |
| 1000 | DBG("ring buffer %s: %lu records written, " |
| 1001 | "%lu records overrun\n", |
| 1002 | chan->backend.name, |
| 1003 | v_read(config, &buf->records_count), |
| 1004 | v_read(config, &buf->records_overrun)); |
| 1005 | } else { |
| 1006 | DBG("ring buffer %s, cpu %d: %lu records written, " |
| 1007 | "%lu records overrun\n", |
| 1008 | chan->backend.name, cpu, |
| 1009 | v_read(config, &buf->records_count), |
| 1010 | v_read(config, &buf->records_overrun)); |
| 1011 | |
| 1012 | if (v_read(config, &buf->records_lost_full) |
| 1013 | || v_read(config, &buf->records_lost_wrap) |
| 1014 | || v_read(config, &buf->records_lost_big)) |
| 1015 | DBG("ring buffer %s, cpu %d: records were lost. Caused by:\n" |
| 1016 | " [ %lu buffer full, %lu nest buffer wrap-around, " |
| 1017 | "%lu event too big ]\n", |
| 1018 | chan->backend.name, cpu, |
| 1019 | v_read(config, &buf->records_lost_full), |
| 1020 | v_read(config, &buf->records_lost_wrap), |
| 1021 | v_read(config, &buf->records_lost_big)); |
| 1022 | } |
| 1023 | lib_ring_buffer_print_buffer_errors(buf, chan, priv, cpu, handle); |
| 1024 | } |
| 1025 | |
| 1026 | /* |
| 1027 | * lib_ring_buffer_switch_old_start: Populate old subbuffer header. |
| 1028 | * |
| 1029 | * Only executed when the buffer is finalized, in SWITCH_FLUSH. |
| 1030 | */ |
| 1031 | static |
| 1032 | void lib_ring_buffer_switch_old_start(struct lttng_ust_lib_ring_buffer *buf, |
| 1033 | struct channel *chan, |
| 1034 | struct switch_offsets *offsets, |
| 1035 | uint64_t tsc, |
| 1036 | struct lttng_ust_shm_handle *handle) |
| 1037 | { |
| 1038 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
| 1039 | unsigned long oldidx = subbuf_index(offsets->old, chan); |
| 1040 | unsigned long commit_count; |
| 1041 | |
| 1042 | config->cb.buffer_begin(buf, tsc, oldidx, handle); |
| 1043 | |
| 1044 | /* |
| 1045 | * Order all writes to buffer before the commit count update that will |
| 1046 | * determine that the subbuffer is full. |
| 1047 | */ |
| 1048 | cmm_smp_wmb(); |
| 1049 | v_add(config, config->cb.subbuffer_header_size(), |
| 1050 | &shmp_index(handle, buf->commit_hot, oldidx)->cc); |
| 1051 | commit_count = v_read(config, &shmp_index(handle, buf->commit_hot, oldidx)->cc); |
| 1052 | /* Check if the written buffer has to be delivered */ |
| 1053 | lib_ring_buffer_check_deliver(config, buf, chan, offsets->old, |
| 1054 | commit_count, oldidx, handle); |
| 1055 | lib_ring_buffer_write_commit_counter(config, buf, chan, oldidx, |
| 1056 | offsets->old, commit_count, |
| 1057 | config->cb.subbuffer_header_size(), |
| 1058 | handle); |
| 1059 | } |
| 1060 | |
| 1061 | /* |
| 1062 | * lib_ring_buffer_switch_old_end: switch old subbuffer |
| 1063 | * |
| 1064 | * Note : offset_old should never be 0 here. It is ok, because we never perform |
| 1065 | * buffer switch on an empty subbuffer in SWITCH_ACTIVE mode. The caller |
| 1066 | * increments the offset_old value when doing a SWITCH_FLUSH on an empty |
| 1067 | * subbuffer. |
| 1068 | */ |
| 1069 | static |
| 1070 | void lib_ring_buffer_switch_old_end(struct lttng_ust_lib_ring_buffer *buf, |
| 1071 | struct channel *chan, |
| 1072 | struct switch_offsets *offsets, |
| 1073 | uint64_t tsc, |
| 1074 | struct lttng_ust_shm_handle *handle) |
| 1075 | { |
| 1076 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
| 1077 | unsigned long oldidx = subbuf_index(offsets->old - 1, chan); |
| 1078 | unsigned long commit_count, padding_size, data_size; |
| 1079 | |
| 1080 | data_size = subbuf_offset(offsets->old - 1, chan) + 1; |
| 1081 | padding_size = chan->backend.subbuf_size - data_size; |
| 1082 | subbuffer_set_data_size(config, &buf->backend, oldidx, data_size, |
| 1083 | handle); |
| 1084 | |
| 1085 | /* |
| 1086 | * Order all writes to buffer before the commit count update that will |
| 1087 | * determine that the subbuffer is full. |
| 1088 | */ |
| 1089 | cmm_smp_wmb(); |
| 1090 | v_add(config, padding_size, &shmp_index(handle, buf->commit_hot, oldidx)->cc); |
| 1091 | commit_count = v_read(config, &shmp_index(handle, buf->commit_hot, oldidx)->cc); |
| 1092 | lib_ring_buffer_check_deliver(config, buf, chan, offsets->old - 1, |
| 1093 | commit_count, oldidx, handle); |
| 1094 | lib_ring_buffer_write_commit_counter(config, buf, chan, oldidx, |
| 1095 | offsets->old, commit_count, |
| 1096 | padding_size, handle); |
| 1097 | } |
| 1098 | |
| 1099 | /* |
| 1100 | * lib_ring_buffer_switch_new_start: Populate new subbuffer. |
| 1101 | * |
| 1102 | * This code can be executed unordered : writers may already have written to the |
| 1103 | * sub-buffer before this code gets executed, caution. The commit makes sure |
| 1104 | * that this code is executed before the deliver of this sub-buffer. |
| 1105 | */ |
| 1106 | static |
| 1107 | void lib_ring_buffer_switch_new_start(struct lttng_ust_lib_ring_buffer *buf, |
| 1108 | struct channel *chan, |
| 1109 | struct switch_offsets *offsets, |
| 1110 | uint64_t tsc, |
| 1111 | struct lttng_ust_shm_handle *handle) |
| 1112 | { |
| 1113 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
| 1114 | unsigned long beginidx = subbuf_index(offsets->begin, chan); |
| 1115 | unsigned long commit_count; |
| 1116 | |
| 1117 | config->cb.buffer_begin(buf, tsc, beginidx, handle); |
| 1118 | |
| 1119 | /* |
| 1120 | * Order all writes to buffer before the commit count update that will |
| 1121 | * determine that the subbuffer is full. |
| 1122 | */ |
| 1123 | cmm_smp_wmb(); |
| 1124 | v_add(config, config->cb.subbuffer_header_size(), |
| 1125 | &shmp_index(handle, buf->commit_hot, beginidx)->cc); |
| 1126 | commit_count = v_read(config, &shmp_index(handle, buf->commit_hot, beginidx)->cc); |
| 1127 | /* Check if the written buffer has to be delivered */ |
| 1128 | lib_ring_buffer_check_deliver(config, buf, chan, offsets->begin, |
| 1129 | commit_count, beginidx, handle); |
| 1130 | lib_ring_buffer_write_commit_counter(config, buf, chan, beginidx, |
| 1131 | offsets->begin, commit_count, |
| 1132 | config->cb.subbuffer_header_size(), |
| 1133 | handle); |
| 1134 | } |
| 1135 | |
| 1136 | /* |
| 1137 | * lib_ring_buffer_switch_new_end: finish switching current subbuffer |
| 1138 | * |
| 1139 | * The only remaining threads could be the ones with pending commits. They will |
| 1140 | * have to do the deliver themselves. |
| 1141 | */ |
| 1142 | static |
| 1143 | void lib_ring_buffer_switch_new_end(struct lttng_ust_lib_ring_buffer *buf, |
| 1144 | struct channel *chan, |
| 1145 | struct switch_offsets *offsets, |
| 1146 | uint64_t tsc, |
| 1147 | struct lttng_ust_shm_handle *handle) |
| 1148 | { |
| 1149 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
| 1150 | unsigned long endidx = subbuf_index(offsets->end - 1, chan); |
| 1151 | unsigned long commit_count, padding_size, data_size; |
| 1152 | |
| 1153 | data_size = subbuf_offset(offsets->end - 1, chan) + 1; |
| 1154 | padding_size = chan->backend.subbuf_size - data_size; |
| 1155 | subbuffer_set_data_size(config, &buf->backend, endidx, data_size, |
| 1156 | handle); |
| 1157 | |
| 1158 | /* |
| 1159 | * Order all writes to buffer before the commit count update that will |
| 1160 | * determine that the subbuffer is full. |
| 1161 | */ |
| 1162 | cmm_smp_wmb(); |
| 1163 | v_add(config, padding_size, &shmp_index(handle, buf->commit_hot, endidx)->cc); |
| 1164 | commit_count = v_read(config, &shmp_index(handle, buf->commit_hot, endidx)->cc); |
| 1165 | lib_ring_buffer_check_deliver(config, buf, chan, offsets->end - 1, |
| 1166 | commit_count, endidx, handle); |
| 1167 | lib_ring_buffer_write_commit_counter(config, buf, chan, endidx, |
| 1168 | offsets->end, commit_count, |
| 1169 | padding_size, handle); |
| 1170 | } |
| 1171 | |
| 1172 | /* |
| 1173 | * Returns : |
| 1174 | * 0 if ok |
| 1175 | * !0 if execution must be aborted. |
| 1176 | */ |
| 1177 | static |
| 1178 | int lib_ring_buffer_try_switch_slow(enum switch_mode mode, |
| 1179 | struct lttng_ust_lib_ring_buffer *buf, |
| 1180 | struct channel *chan, |
| 1181 | struct switch_offsets *offsets, |
| 1182 | uint64_t *tsc) |
| 1183 | { |
| 1184 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
| 1185 | unsigned long off; |
| 1186 | |
| 1187 | offsets->begin = v_read(config, &buf->offset); |
| 1188 | offsets->old = offsets->begin; |
| 1189 | offsets->switch_old_start = 0; |
| 1190 | off = subbuf_offset(offsets->begin, chan); |
| 1191 | |
| 1192 | *tsc = config->cb.ring_buffer_clock_read(chan); |
| 1193 | |
| 1194 | /* |
| 1195 | * Ensure we flush the header of an empty subbuffer when doing the |
| 1196 | * finalize (SWITCH_FLUSH). This ensures that we end up knowing the |
| 1197 | * total data gathering duration even if there were no records saved |
| 1198 | * after the last buffer switch. |
| 1199 | * In SWITCH_ACTIVE mode, switch the buffer when it contains events. |
| 1200 | * SWITCH_ACTIVE only flushes the current subbuffer, dealing with end of |
| 1201 | * subbuffer header as appropriate. |
| 1202 | * The next record that reserves space will be responsible for |
| 1203 | * populating the following subbuffer header. We choose not to populate |
| 1204 | * the next subbuffer header here because we want to be able to use |
| 1205 | * SWITCH_ACTIVE for periodical buffer flush, which must |
| 1206 | * guarantee that all the buffer content (records and header |
| 1207 | * timestamps) are visible to the reader. This is required for |
| 1208 | * quiescence guarantees for the fusion merge. |
| 1209 | */ |
| 1210 | if (mode == SWITCH_FLUSH || off > 0) { |
| 1211 | if (caa_unlikely(off == 0)) { |
| 1212 | /* |
| 1213 | * The client does not save any header information. |
| 1214 | * Don't switch empty subbuffer on finalize, because it |
| 1215 | * is invalid to deliver a completely empty subbuffer. |
| 1216 | */ |
| 1217 | if (!config->cb.subbuffer_header_size()) |
| 1218 | return -1; |
| 1219 | /* |
| 1220 | * Need to write the subbuffer start header on finalize. |
| 1221 | */ |
| 1222 | offsets->switch_old_start = 1; |
| 1223 | } |
| 1224 | offsets->begin = subbuf_align(offsets->begin, chan); |
| 1225 | } else |
| 1226 | return -1; /* we do not have to switch : buffer is empty */ |
| 1227 | /* Note: old points to the next subbuf at offset 0 */ |
| 1228 | offsets->end = offsets->begin; |
| 1229 | return 0; |
| 1230 | } |
| 1231 | |
| 1232 | /* |
| 1233 | * Force a sub-buffer switch. This operation is completely reentrant : can be |
| 1234 | * called while tracing is active with absolutely no lock held. |
| 1235 | * |
| 1236 | * Note, however, that as a v_cmpxchg is used for some atomic |
| 1237 | * operations, this function must be called from the CPU which owns the buffer |
| 1238 | * for a ACTIVE flush. |
| 1239 | */ |
| 1240 | void lib_ring_buffer_switch_slow(struct lttng_ust_lib_ring_buffer *buf, enum switch_mode mode, |
| 1241 | struct lttng_ust_shm_handle *handle) |
| 1242 | { |
| 1243 | struct channel *chan = shmp(handle, buf->backend.chan); |
| 1244 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
| 1245 | struct switch_offsets offsets; |
| 1246 | unsigned long oldidx; |
| 1247 | uint64_t tsc; |
| 1248 | |
| 1249 | offsets.size = 0; |
| 1250 | |
| 1251 | /* |
| 1252 | * Perform retryable operations. |
| 1253 | */ |
| 1254 | do { |
| 1255 | if (lib_ring_buffer_try_switch_slow(mode, buf, chan, &offsets, |
| 1256 | &tsc)) |
| 1257 | return; /* Switch not needed */ |
| 1258 | } while (v_cmpxchg(config, &buf->offset, offsets.old, offsets.end) |
| 1259 | != offsets.old); |
| 1260 | |
| 1261 | /* |
| 1262 | * Atomically update last_tsc. This update races against concurrent |
| 1263 | * atomic updates, but the race will always cause supplementary full TSC |
| 1264 | * records, never the opposite (missing a full TSC record when it would |
| 1265 | * be needed). |
| 1266 | */ |
| 1267 | save_last_tsc(config, buf, tsc); |
| 1268 | |
| 1269 | /* |
| 1270 | * Push the reader if necessary |
| 1271 | */ |
| 1272 | lib_ring_buffer_reserve_push_reader(buf, chan, offsets.old); |
| 1273 | |
| 1274 | oldidx = subbuf_index(offsets.old, chan); |
| 1275 | lib_ring_buffer_clear_noref(config, &buf->backend, oldidx, handle); |
| 1276 | |
| 1277 | /* |
| 1278 | * May need to populate header start on SWITCH_FLUSH. |
| 1279 | */ |
| 1280 | if (offsets.switch_old_start) { |
| 1281 | lib_ring_buffer_switch_old_start(buf, chan, &offsets, tsc, handle); |
| 1282 | offsets.old += config->cb.subbuffer_header_size(); |
| 1283 | } |
| 1284 | |
| 1285 | /* |
| 1286 | * Switch old subbuffer. |
| 1287 | */ |
| 1288 | lib_ring_buffer_switch_old_end(buf, chan, &offsets, tsc, handle); |
| 1289 | } |
| 1290 | |
| 1291 | /* |
| 1292 | * Returns : |
| 1293 | * 0 if ok |
| 1294 | * -ENOSPC if event size is too large for packet. |
| 1295 | * -ENOBUFS if there is currently not enough space in buffer for the event. |
| 1296 | * -EIO if data cannot be written into the buffer for any other reason. |
| 1297 | */ |
| 1298 | static |
| 1299 | int lib_ring_buffer_try_reserve_slow(struct lttng_ust_lib_ring_buffer *buf, |
| 1300 | struct channel *chan, |
| 1301 | struct switch_offsets *offsets, |
| 1302 | struct lttng_ust_lib_ring_buffer_ctx *ctx) |
| 1303 | { |
| 1304 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
| 1305 | struct lttng_ust_shm_handle *handle = ctx->handle; |
| 1306 | unsigned long reserve_commit_diff; |
| 1307 | |
| 1308 | offsets->begin = v_read(config, &buf->offset); |
| 1309 | offsets->old = offsets->begin; |
| 1310 | offsets->switch_new_start = 0; |
| 1311 | offsets->switch_new_end = 0; |
| 1312 | offsets->switch_old_end = 0; |
| 1313 | offsets->pre_header_padding = 0; |
| 1314 | |
| 1315 | ctx->tsc = config->cb.ring_buffer_clock_read(chan); |
| 1316 | if ((int64_t) ctx->tsc == -EIO) |
| 1317 | return -EIO; |
| 1318 | |
| 1319 | if (last_tsc_overflow(config, buf, ctx->tsc)) |
| 1320 | ctx->rflags |= RING_BUFFER_RFLAG_FULL_TSC; |
| 1321 | |
| 1322 | if (caa_unlikely(subbuf_offset(offsets->begin, ctx->chan) == 0)) { |
| 1323 | offsets->switch_new_start = 1; /* For offsets->begin */ |
| 1324 | } else { |
| 1325 | offsets->size = config->cb.record_header_size(config, chan, |
| 1326 | offsets->begin, |
| 1327 | &offsets->pre_header_padding, |
| 1328 | ctx); |
| 1329 | offsets->size += |
| 1330 | lib_ring_buffer_align(offsets->begin + offsets->size, |
| 1331 | ctx->largest_align) |
| 1332 | + ctx->data_size; |
| 1333 | if (caa_unlikely(subbuf_offset(offsets->begin, chan) + |
| 1334 | offsets->size > chan->backend.subbuf_size)) { |
| 1335 | offsets->switch_old_end = 1; /* For offsets->old */ |
| 1336 | offsets->switch_new_start = 1; /* For offsets->begin */ |
| 1337 | } |
| 1338 | } |
| 1339 | if (caa_unlikely(offsets->switch_new_start)) { |
| 1340 | unsigned long sb_index; |
| 1341 | |
| 1342 | /* |
| 1343 | * We are typically not filling the previous buffer completely. |
| 1344 | */ |
| 1345 | if (caa_likely(offsets->switch_old_end)) |
| 1346 | offsets->begin = subbuf_align(offsets->begin, chan); |
| 1347 | offsets->begin = offsets->begin |
| 1348 | + config->cb.subbuffer_header_size(); |
| 1349 | /* Test new buffer integrity */ |
| 1350 | sb_index = subbuf_index(offsets->begin, chan); |
| 1351 | reserve_commit_diff = |
| 1352 | (buf_trunc(offsets->begin, chan) |
| 1353 | >> chan->backend.num_subbuf_order) |
| 1354 | - ((unsigned long) v_read(config, |
| 1355 | &shmp_index(handle, buf->commit_cold, sb_index)->cc_sb) |
| 1356 | & chan->commit_count_mask); |
| 1357 | if (caa_likely(reserve_commit_diff == 0)) { |
| 1358 | /* Next subbuffer not being written to. */ |
| 1359 | if (caa_unlikely(config->mode != RING_BUFFER_OVERWRITE && |
| 1360 | subbuf_trunc(offsets->begin, chan) |
| 1361 | - subbuf_trunc((unsigned long) |
| 1362 | uatomic_read(&buf->consumed), chan) |
| 1363 | >= chan->backend.buf_size)) { |
| 1364 | unsigned long nr_lost; |
| 1365 | |
| 1366 | /* |
| 1367 | * We do not overwrite non consumed buffers |
| 1368 | * and we are full : record is lost. |
| 1369 | */ |
| 1370 | nr_lost = v_read(config, &buf->records_lost_full); |
| 1371 | v_inc(config, &buf->records_lost_full); |
| 1372 | if ((nr_lost & (DBG_PRINT_NR_LOST - 1)) == 0) { |
| 1373 | DBG("%lu or more records lost in (%s:%d) (buffer full)\n", |
| 1374 | nr_lost + 1, chan->backend.name, |
| 1375 | buf->backend.cpu); |
| 1376 | } |
| 1377 | return -ENOBUFS; |
| 1378 | } else { |
| 1379 | /* |
| 1380 | * Next subbuffer not being written to, and we |
| 1381 | * are either in overwrite mode or the buffer is |
| 1382 | * not full. It's safe to write in this new |
| 1383 | * subbuffer. |
| 1384 | */ |
| 1385 | } |
| 1386 | } else { |
| 1387 | unsigned long nr_lost; |
| 1388 | |
| 1389 | /* |
| 1390 | * Next subbuffer reserve offset does not match the |
| 1391 | * commit offset. Drop record in producer-consumer and |
| 1392 | * overwrite mode. Caused by either a writer OOPS or too |
| 1393 | * many nested writes over a reserve/commit pair. |
| 1394 | */ |
| 1395 | nr_lost = v_read(config, &buf->records_lost_wrap); |
| 1396 | v_inc(config, &buf->records_lost_wrap); |
| 1397 | if ((nr_lost & (DBG_PRINT_NR_LOST - 1)) == 0) { |
| 1398 | DBG("%lu or more records lost in (%s:%d) (wrap-around)\n", |
| 1399 | nr_lost + 1, chan->backend.name, |
| 1400 | buf->backend.cpu); |
| 1401 | } |
| 1402 | return -EIO; |
| 1403 | } |
| 1404 | offsets->size = |
| 1405 | config->cb.record_header_size(config, chan, |
| 1406 | offsets->begin, |
| 1407 | &offsets->pre_header_padding, |
| 1408 | ctx); |
| 1409 | offsets->size += |
| 1410 | lib_ring_buffer_align(offsets->begin + offsets->size, |
| 1411 | ctx->largest_align) |
| 1412 | + ctx->data_size; |
| 1413 | if (caa_unlikely(subbuf_offset(offsets->begin, chan) |
| 1414 | + offsets->size > chan->backend.subbuf_size)) { |
| 1415 | unsigned long nr_lost; |
| 1416 | |
| 1417 | /* |
| 1418 | * Record too big for subbuffers, report error, don't |
| 1419 | * complete the sub-buffer switch. |
| 1420 | */ |
| 1421 | nr_lost = v_read(config, &buf->records_lost_big); |
| 1422 | v_inc(config, &buf->records_lost_big); |
| 1423 | if ((nr_lost & (DBG_PRINT_NR_LOST - 1)) == 0) { |
| 1424 | DBG("%lu or more records lost in (%s:%d) record size " |
| 1425 | " of %zu bytes is too large for buffer\n", |
| 1426 | nr_lost + 1, chan->backend.name, |
| 1427 | buf->backend.cpu, offsets->size); |
| 1428 | } |
| 1429 | return -ENOSPC; |
| 1430 | } else { |
| 1431 | /* |
| 1432 | * We just made a successful buffer switch and the |
| 1433 | * record fits in the new subbuffer. Let's write. |
| 1434 | */ |
| 1435 | } |
| 1436 | } else { |
| 1437 | /* |
| 1438 | * Record fits in the current buffer and we are not on a switch |
| 1439 | * boundary. It's safe to write. |
| 1440 | */ |
| 1441 | } |
| 1442 | offsets->end = offsets->begin + offsets->size; |
| 1443 | |
| 1444 | if (caa_unlikely(subbuf_offset(offsets->end, chan) == 0)) { |
| 1445 | /* |
| 1446 | * The offset_end will fall at the very beginning of the next |
| 1447 | * subbuffer. |
| 1448 | */ |
| 1449 | offsets->switch_new_end = 1; /* For offsets->begin */ |
| 1450 | } |
| 1451 | return 0; |
| 1452 | } |
| 1453 | |
| 1454 | /** |
| 1455 | * lib_ring_buffer_reserve_slow - Atomic slot reservation in a buffer. |
| 1456 | * @ctx: ring buffer context. |
| 1457 | * |
| 1458 | * Return : -NOBUFS if not enough space, -ENOSPC if event size too large, |
| 1459 | * -EIO for other errors, else returns 0. |
| 1460 | * It will take care of sub-buffer switching. |
| 1461 | */ |
| 1462 | int lib_ring_buffer_reserve_slow(struct lttng_ust_lib_ring_buffer_ctx *ctx) |
| 1463 | { |
| 1464 | struct channel *chan = ctx->chan; |
| 1465 | struct lttng_ust_shm_handle *handle = ctx->handle; |
| 1466 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
| 1467 | struct lttng_ust_lib_ring_buffer *buf; |
| 1468 | struct switch_offsets offsets; |
| 1469 | int ret; |
| 1470 | |
| 1471 | if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) |
| 1472 | buf = shmp(handle, chan->backend.buf[ctx->cpu].shmp); |
| 1473 | else |
| 1474 | buf = shmp(handle, chan->backend.buf[0].shmp); |
| 1475 | ctx->buf = buf; |
| 1476 | |
| 1477 | offsets.size = 0; |
| 1478 | |
| 1479 | do { |
| 1480 | ret = lib_ring_buffer_try_reserve_slow(buf, chan, &offsets, |
| 1481 | ctx); |
| 1482 | if (caa_unlikely(ret)) |
| 1483 | return ret; |
| 1484 | } while (caa_unlikely(v_cmpxchg(config, &buf->offset, offsets.old, |
| 1485 | offsets.end) |
| 1486 | != offsets.old)); |
| 1487 | |
| 1488 | /* |
| 1489 | * Atomically update last_tsc. This update races against concurrent |
| 1490 | * atomic updates, but the race will always cause supplementary full TSC |
| 1491 | * records, never the opposite (missing a full TSC record when it would |
| 1492 | * be needed). |
| 1493 | */ |
| 1494 | save_last_tsc(config, buf, ctx->tsc); |
| 1495 | |
| 1496 | /* |
| 1497 | * Push the reader if necessary |
| 1498 | */ |
| 1499 | lib_ring_buffer_reserve_push_reader(buf, chan, offsets.end - 1); |
| 1500 | |
| 1501 | /* |
| 1502 | * Clear noref flag for this subbuffer. |
| 1503 | */ |
| 1504 | lib_ring_buffer_clear_noref(config, &buf->backend, |
| 1505 | subbuf_index(offsets.end - 1, chan), |
| 1506 | handle); |
| 1507 | |
| 1508 | /* |
| 1509 | * Switch old subbuffer if needed. |
| 1510 | */ |
| 1511 | if (caa_unlikely(offsets.switch_old_end)) { |
| 1512 | lib_ring_buffer_clear_noref(config, &buf->backend, |
| 1513 | subbuf_index(offsets.old - 1, chan), |
| 1514 | handle); |
| 1515 | lib_ring_buffer_switch_old_end(buf, chan, &offsets, ctx->tsc, handle); |
| 1516 | } |
| 1517 | |
| 1518 | /* |
| 1519 | * Populate new subbuffer. |
| 1520 | */ |
| 1521 | if (caa_unlikely(offsets.switch_new_start)) |
| 1522 | lib_ring_buffer_switch_new_start(buf, chan, &offsets, ctx->tsc, handle); |
| 1523 | |
| 1524 | if (caa_unlikely(offsets.switch_new_end)) |
| 1525 | lib_ring_buffer_switch_new_end(buf, chan, &offsets, ctx->tsc, handle); |
| 1526 | |
| 1527 | ctx->slot_size = offsets.size; |
| 1528 | ctx->pre_offset = offsets.begin; |
| 1529 | ctx->buf_offset = offsets.begin + offsets.pre_header_padding; |
| 1530 | return 0; |
| 1531 | } |
| 1532 | |
| 1533 | /* |
| 1534 | * Force a read (imply TLS fixup for dlopen) of TLS variables. |
| 1535 | */ |
| 1536 | void lttng_fixup_ringbuffer_tls(void) |
| 1537 | { |
| 1538 | asm volatile ("" : : "m" (lib_ring_buffer_nesting)); |
| 1539 | } |