| 1 | /* |
| 2 | * lttng-ring-buffer-client.h |
| 3 | * |
| 4 | * LTTng lib ring buffer client template. |
| 5 | * |
| 6 | * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 7 | * |
| 8 | * This library is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU Lesser General Public |
| 10 | * License as published by the Free Software Foundation; only |
| 11 | * version 2.1 of the License. |
| 12 | * |
| 13 | * This library is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | * Lesser General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU Lesser General Public |
| 19 | * License along with this library; if not, write to the Free Software |
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 21 | */ |
| 22 | |
| 23 | #include <stddef.h> |
| 24 | #include <stdint.h> |
| 25 | #include <lttng/ust-events.h> |
| 26 | #include "lttng/bitfield.h" |
| 27 | #include "clock.h" |
| 28 | #include "lttng-tracer.h" |
| 29 | #include "../libringbuffer/frontend_types.h" |
| 30 | |
| 31 | #define LTTNG_COMPACT_EVENT_BITS 5 |
| 32 | #define LTTNG_COMPACT_TSC_BITS 27 |
| 33 | |
| 34 | enum app_ctx_mode { |
| 35 | APP_CTX_DISABLED, |
| 36 | APP_CTX_ENABLED, |
| 37 | }; |
| 38 | |
| 39 | /* |
| 40 | * Keep the natural field alignment for _each field_ within this structure if |
| 41 | * you ever add/remove a field from this header. Packed attribute is not used |
| 42 | * because gcc generates poor code on at least powerpc and mips. Don't ever |
| 43 | * let gcc add padding between the structure elements. |
| 44 | */ |
| 45 | |
| 46 | struct packet_header { |
| 47 | /* Trace packet header */ |
| 48 | uint32_t magic; /* |
| 49 | * Trace magic number. |
| 50 | * contains endianness information. |
| 51 | */ |
| 52 | uint8_t uuid[LTTNG_UST_UUID_LEN]; |
| 53 | uint32_t stream_id; |
| 54 | uint64_t stream_instance_id; |
| 55 | |
| 56 | struct { |
| 57 | /* Stream packet context */ |
| 58 | uint64_t timestamp_begin; /* Cycle count at subbuffer start */ |
| 59 | uint64_t timestamp_end; /* Cycle count at subbuffer end */ |
| 60 | uint64_t content_size; /* Size of data in subbuffer */ |
| 61 | uint64_t packet_size; /* Subbuffer size (include padding) */ |
| 62 | uint64_t packet_seq_num; /* Packet sequence number */ |
| 63 | unsigned long events_discarded; /* |
| 64 | * Events lost in this subbuffer since |
| 65 | * the beginning of the trace. |
| 66 | * (may overflow) |
| 67 | */ |
| 68 | uint32_t cpu_id; /* CPU id associated with stream */ |
| 69 | uint8_t header_end; /* End of header */ |
| 70 | } ctx; |
| 71 | }; |
| 72 | |
| 73 | struct lttng_client_ctx { |
| 74 | size_t packet_context_len; |
| 75 | size_t event_context_len; |
| 76 | }; |
| 77 | |
| 78 | static inline uint64_t lib_ring_buffer_clock_read(struct channel *chan) |
| 79 | { |
| 80 | return trace_clock_read64(); |
| 81 | } |
| 82 | |
| 83 | static inline |
| 84 | size_t ctx_get_aligned_size(size_t offset, struct lttng_ctx *ctx, |
| 85 | size_t ctx_len) |
| 86 | { |
| 87 | size_t orig_offset = offset; |
| 88 | |
| 89 | if (caa_likely(!ctx)) |
| 90 | return 0; |
| 91 | offset += lib_ring_buffer_align(offset, ctx->largest_align); |
| 92 | offset += ctx_len; |
| 93 | return offset - orig_offset; |
| 94 | } |
| 95 | |
| 96 | static inline |
| 97 | void ctx_get_struct_size(struct lttng_ctx *ctx, size_t *ctx_len, |
| 98 | enum app_ctx_mode mode) |
| 99 | { |
| 100 | int i; |
| 101 | size_t offset = 0; |
| 102 | |
| 103 | if (caa_likely(!ctx)) { |
| 104 | *ctx_len = 0; |
| 105 | return; |
| 106 | } |
| 107 | for (i = 0; i < ctx->nr_fields; i++) { |
| 108 | if (mode == APP_CTX_ENABLED) { |
| 109 | offset += ctx->fields[i].get_size(&ctx->fields[i], offset); |
| 110 | } else { |
| 111 | if (lttng_context_is_app(ctx->fields[i].event_field.name)) { |
| 112 | /* |
| 113 | * Before UST 2.8, we cannot use the |
| 114 | * application context, because we |
| 115 | * cannot trust that the handler used |
| 116 | * for get_size is the same used for |
| 117 | * ctx_record, which would result in |
| 118 | * corrupted traces when tracing |
| 119 | * concurrently with application context |
| 120 | * register/unregister. |
| 121 | */ |
| 122 | offset += lttng_ust_dummy_get_size(&ctx->fields[i], offset); |
| 123 | } else { |
| 124 | offset += ctx->fields[i].get_size(&ctx->fields[i], offset); |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | *ctx_len = offset; |
| 129 | } |
| 130 | |
| 131 | static inline |
| 132 | void ctx_record(struct lttng_ust_lib_ring_buffer_ctx *bufctx, |
| 133 | struct lttng_channel *chan, |
| 134 | struct lttng_ctx *ctx, |
| 135 | enum app_ctx_mode mode) |
| 136 | { |
| 137 | int i; |
| 138 | |
| 139 | if (caa_likely(!ctx)) |
| 140 | return; |
| 141 | lib_ring_buffer_align_ctx(bufctx, ctx->largest_align); |
| 142 | for (i = 0; i < ctx->nr_fields; i++) { |
| 143 | if (mode == APP_CTX_ENABLED) { |
| 144 | ctx->fields[i].record(&ctx->fields[i], bufctx, chan); |
| 145 | } else { |
| 146 | if (lttng_context_is_app(ctx->fields[i].event_field.name)) { |
| 147 | /* |
| 148 | * Before UST 2.8, we cannot use the |
| 149 | * application context, because we |
| 150 | * cannot trust that the handler used |
| 151 | * for get_size is the same used for |
| 152 | * ctx_record, which would result in |
| 153 | * corrupted traces when tracing |
| 154 | * concurrently with application context |
| 155 | * register/unregister. |
| 156 | */ |
| 157 | lttng_ust_dummy_record(&ctx->fields[i], bufctx, chan); |
| 158 | } else { |
| 159 | ctx->fields[i].record(&ctx->fields[i], bufctx, chan); |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | /* |
| 166 | * record_header_size - Calculate the header size and padding necessary. |
| 167 | * @config: ring buffer instance configuration |
| 168 | * @chan: channel |
| 169 | * @offset: offset in the write buffer |
| 170 | * @pre_header_padding: padding to add before the header (output) |
| 171 | * @ctx: reservation context |
| 172 | * |
| 173 | * Returns the event header size (including padding). |
| 174 | * |
| 175 | * The payload must itself determine its own alignment from the biggest type it |
| 176 | * contains. |
| 177 | */ |
| 178 | static __inline__ |
| 179 | size_t record_header_size(const struct lttng_ust_lib_ring_buffer_config *config, |
| 180 | struct channel *chan, size_t offset, |
| 181 | size_t *pre_header_padding, |
| 182 | struct lttng_ust_lib_ring_buffer_ctx *ctx, |
| 183 | struct lttng_client_ctx *client_ctx) |
| 184 | { |
| 185 | struct lttng_channel *lttng_chan = channel_get_private(chan); |
| 186 | struct lttng_event *event = ctx->priv; |
| 187 | struct lttng_stack_ctx *lttng_ctx = ctx->priv2; |
| 188 | size_t orig_offset = offset; |
| 189 | size_t padding; |
| 190 | |
| 191 | switch (lttng_chan->header_type) { |
| 192 | case 1: /* compact */ |
| 193 | padding = lib_ring_buffer_align(offset, lttng_alignof(uint32_t)); |
| 194 | offset += padding; |
| 195 | if (!(ctx->rflags & (RING_BUFFER_RFLAG_FULL_TSC | LTTNG_RFLAG_EXTENDED))) { |
| 196 | offset += sizeof(uint32_t); /* id and timestamp */ |
| 197 | } else { |
| 198 | /* Minimum space taken by LTTNG_COMPACT_EVENT_BITS id */ |
| 199 | offset += (LTTNG_COMPACT_EVENT_BITS + CHAR_BIT - 1) / CHAR_BIT; |
| 200 | /* Align extended struct on largest member */ |
| 201 | offset += lib_ring_buffer_align(offset, lttng_alignof(uint64_t)); |
| 202 | offset += sizeof(uint32_t); /* id */ |
| 203 | offset += lib_ring_buffer_align(offset, lttng_alignof(uint64_t)); |
| 204 | offset += sizeof(uint64_t); /* timestamp */ |
| 205 | } |
| 206 | break; |
| 207 | case 2: /* large */ |
| 208 | padding = lib_ring_buffer_align(offset, lttng_alignof(uint16_t)); |
| 209 | offset += padding; |
| 210 | offset += sizeof(uint16_t); |
| 211 | if (!(ctx->rflags & (RING_BUFFER_RFLAG_FULL_TSC | LTTNG_RFLAG_EXTENDED))) { |
| 212 | offset += lib_ring_buffer_align(offset, lttng_alignof(uint32_t)); |
| 213 | offset += sizeof(uint32_t); /* timestamp */ |
| 214 | } else { |
| 215 | /* Align extended struct on largest member */ |
| 216 | offset += lib_ring_buffer_align(offset, lttng_alignof(uint64_t)); |
| 217 | offset += sizeof(uint32_t); /* id */ |
| 218 | offset += lib_ring_buffer_align(offset, lttng_alignof(uint64_t)); |
| 219 | offset += sizeof(uint64_t); /* timestamp */ |
| 220 | } |
| 221 | break; |
| 222 | default: |
| 223 | padding = 0; |
| 224 | WARN_ON_ONCE(1); |
| 225 | } |
| 226 | if (lttng_ctx) { |
| 227 | /* 2.8+ probe ABI. */ |
| 228 | offset += ctx_get_aligned_size(offset, lttng_ctx->chan_ctx, |
| 229 | client_ctx->packet_context_len); |
| 230 | offset += ctx_get_aligned_size(offset, lttng_ctx->event_ctx, |
| 231 | client_ctx->event_context_len); |
| 232 | } else { |
| 233 | /* Pre 2.8 probe ABI. */ |
| 234 | offset += ctx_get_aligned_size(offset, lttng_chan->ctx, |
| 235 | client_ctx->packet_context_len); |
| 236 | offset += ctx_get_aligned_size(offset, event->ctx, |
| 237 | client_ctx->event_context_len); |
| 238 | } |
| 239 | *pre_header_padding = padding; |
| 240 | return offset - orig_offset; |
| 241 | } |
| 242 | |
| 243 | #include "../libringbuffer/api.h" |
| 244 | #include "lttng-rb-clients.h" |
| 245 | |
| 246 | static |
| 247 | void lttng_write_event_header_slow(const struct lttng_ust_lib_ring_buffer_config *config, |
| 248 | struct lttng_ust_lib_ring_buffer_ctx *ctx, |
| 249 | uint32_t event_id); |
| 250 | |
| 251 | /* |
| 252 | * lttng_write_event_header |
| 253 | * |
| 254 | * Writes the event header to the offset (already aligned on 32-bits). |
| 255 | * |
| 256 | * @config: ring buffer instance configuration |
| 257 | * @ctx: reservation context |
| 258 | * @event_id: event ID |
| 259 | */ |
| 260 | static __inline__ |
| 261 | void lttng_write_event_header(const struct lttng_ust_lib_ring_buffer_config *config, |
| 262 | struct lttng_ust_lib_ring_buffer_ctx *ctx, |
| 263 | uint32_t event_id) |
| 264 | { |
| 265 | struct lttng_channel *lttng_chan = channel_get_private(ctx->chan); |
| 266 | struct lttng_event *event = ctx->priv; |
| 267 | struct lttng_stack_ctx *lttng_ctx = ctx->priv2; |
| 268 | |
| 269 | if (caa_unlikely(ctx->rflags)) |
| 270 | goto slow_path; |
| 271 | |
| 272 | switch (lttng_chan->header_type) { |
| 273 | case 1: /* compact */ |
| 274 | { |
| 275 | uint32_t id_time = 0; |
| 276 | |
| 277 | bt_bitfield_write(&id_time, uint32_t, |
| 278 | 0, |
| 279 | LTTNG_COMPACT_EVENT_BITS, |
| 280 | event_id); |
| 281 | bt_bitfield_write(&id_time, uint32_t, |
| 282 | LTTNG_COMPACT_EVENT_BITS, |
| 283 | LTTNG_COMPACT_TSC_BITS, |
| 284 | ctx->tsc); |
| 285 | lib_ring_buffer_write(config, ctx, &id_time, sizeof(id_time)); |
| 286 | break; |
| 287 | } |
| 288 | case 2: /* large */ |
| 289 | { |
| 290 | uint32_t timestamp = (uint32_t) ctx->tsc; |
| 291 | uint16_t id = event_id; |
| 292 | |
| 293 | lib_ring_buffer_write(config, ctx, &id, sizeof(id)); |
| 294 | lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint32_t)); |
| 295 | lib_ring_buffer_write(config, ctx, ×tamp, sizeof(timestamp)); |
| 296 | break; |
| 297 | } |
| 298 | default: |
| 299 | WARN_ON_ONCE(1); |
| 300 | } |
| 301 | |
| 302 | if (lttng_ctx) { |
| 303 | /* 2.8+ probe ABI. */ |
| 304 | ctx_record(ctx, lttng_chan, lttng_ctx->chan_ctx, APP_CTX_ENABLED); |
| 305 | ctx_record(ctx, lttng_chan, lttng_ctx->event_ctx, APP_CTX_ENABLED); |
| 306 | } else { |
| 307 | /* Pre 2.8 probe ABI. */ |
| 308 | ctx_record(ctx, lttng_chan, lttng_chan->ctx, APP_CTX_DISABLED); |
| 309 | ctx_record(ctx, lttng_chan, event->ctx, APP_CTX_DISABLED); |
| 310 | } |
| 311 | lib_ring_buffer_align_ctx(ctx, ctx->largest_align); |
| 312 | |
| 313 | return; |
| 314 | |
| 315 | slow_path: |
| 316 | lttng_write_event_header_slow(config, ctx, event_id); |
| 317 | } |
| 318 | |
| 319 | static |
| 320 | void lttng_write_event_header_slow(const struct lttng_ust_lib_ring_buffer_config *config, |
| 321 | struct lttng_ust_lib_ring_buffer_ctx *ctx, |
| 322 | uint32_t event_id) |
| 323 | { |
| 324 | struct lttng_channel *lttng_chan = channel_get_private(ctx->chan); |
| 325 | struct lttng_event *event = ctx->priv; |
| 326 | struct lttng_stack_ctx *lttng_ctx = ctx->priv2; |
| 327 | |
| 328 | switch (lttng_chan->header_type) { |
| 329 | case 1: /* compact */ |
| 330 | if (!(ctx->rflags & (RING_BUFFER_RFLAG_FULL_TSC | LTTNG_RFLAG_EXTENDED))) { |
| 331 | uint32_t id_time = 0; |
| 332 | |
| 333 | bt_bitfield_write(&id_time, uint32_t, |
| 334 | 0, |
| 335 | LTTNG_COMPACT_EVENT_BITS, |
| 336 | event_id); |
| 337 | bt_bitfield_write(&id_time, uint32_t, |
| 338 | LTTNG_COMPACT_EVENT_BITS, |
| 339 | LTTNG_COMPACT_TSC_BITS, |
| 340 | ctx->tsc); |
| 341 | lib_ring_buffer_write(config, ctx, &id_time, sizeof(id_time)); |
| 342 | } else { |
| 343 | uint8_t id = 0; |
| 344 | uint64_t timestamp = ctx->tsc; |
| 345 | |
| 346 | bt_bitfield_write(&id, uint8_t, |
| 347 | 0, |
| 348 | LTTNG_COMPACT_EVENT_BITS, |
| 349 | 31); |
| 350 | lib_ring_buffer_write(config, ctx, &id, sizeof(id)); |
| 351 | /* Align extended struct on largest member */ |
| 352 | lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint64_t)); |
| 353 | lib_ring_buffer_write(config, ctx, &event_id, sizeof(event_id)); |
| 354 | lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint64_t)); |
| 355 | lib_ring_buffer_write(config, ctx, ×tamp, sizeof(timestamp)); |
| 356 | } |
| 357 | break; |
| 358 | case 2: /* large */ |
| 359 | { |
| 360 | if (!(ctx->rflags & (RING_BUFFER_RFLAG_FULL_TSC | LTTNG_RFLAG_EXTENDED))) { |
| 361 | uint32_t timestamp = (uint32_t) ctx->tsc; |
| 362 | uint16_t id = event_id; |
| 363 | |
| 364 | lib_ring_buffer_write(config, ctx, &id, sizeof(id)); |
| 365 | lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint32_t)); |
| 366 | lib_ring_buffer_write(config, ctx, ×tamp, sizeof(timestamp)); |
| 367 | } else { |
| 368 | uint16_t id = 65535; |
| 369 | uint64_t timestamp = ctx->tsc; |
| 370 | |
| 371 | lib_ring_buffer_write(config, ctx, &id, sizeof(id)); |
| 372 | /* Align extended struct on largest member */ |
| 373 | lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint64_t)); |
| 374 | lib_ring_buffer_write(config, ctx, &event_id, sizeof(event_id)); |
| 375 | lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint64_t)); |
| 376 | lib_ring_buffer_write(config, ctx, ×tamp, sizeof(timestamp)); |
| 377 | } |
| 378 | break; |
| 379 | } |
| 380 | default: |
| 381 | WARN_ON_ONCE(1); |
| 382 | } |
| 383 | if (lttng_ctx) { |
| 384 | /* 2.8+ probe ABI. */ |
| 385 | ctx_record(ctx, lttng_chan, lttng_ctx->chan_ctx, APP_CTX_ENABLED); |
| 386 | ctx_record(ctx, lttng_chan, lttng_ctx->event_ctx, APP_CTX_ENABLED); |
| 387 | } else { |
| 388 | /* Pre 2.8 probe ABI. */ |
| 389 | ctx_record(ctx, lttng_chan, lttng_chan->ctx, APP_CTX_DISABLED); |
| 390 | ctx_record(ctx, lttng_chan, event->ctx, APP_CTX_DISABLED); |
| 391 | } |
| 392 | lib_ring_buffer_align_ctx(ctx, ctx->largest_align); |
| 393 | } |
| 394 | |
| 395 | static const struct lttng_ust_lib_ring_buffer_config client_config; |
| 396 | |
| 397 | static uint64_t client_ring_buffer_clock_read(struct channel *chan) |
| 398 | { |
| 399 | return lib_ring_buffer_clock_read(chan); |
| 400 | } |
| 401 | |
| 402 | static |
| 403 | size_t client_record_header_size(const struct lttng_ust_lib_ring_buffer_config *config, |
| 404 | struct channel *chan, size_t offset, |
| 405 | size_t *pre_header_padding, |
| 406 | struct lttng_ust_lib_ring_buffer_ctx *ctx, |
| 407 | void *client_ctx) |
| 408 | { |
| 409 | return record_header_size(config, chan, offset, |
| 410 | pre_header_padding, ctx, client_ctx); |
| 411 | } |
| 412 | |
| 413 | /** |
| 414 | * client_packet_header_size - called on buffer-switch to a new sub-buffer |
| 415 | * |
| 416 | * Return header size without padding after the structure. Don't use packed |
| 417 | * structure because gcc generates inefficient code on some architectures |
| 418 | * (powerpc, mips..) |
| 419 | */ |
| 420 | static size_t client_packet_header_size(void) |
| 421 | { |
| 422 | return offsetof(struct packet_header, ctx.header_end); |
| 423 | } |
| 424 | |
| 425 | static void client_buffer_begin(struct lttng_ust_lib_ring_buffer *buf, uint64_t tsc, |
| 426 | unsigned int subbuf_idx, |
| 427 | struct lttng_ust_shm_handle *handle) |
| 428 | { |
| 429 | struct channel *chan = shmp(handle, buf->backend.chan); |
| 430 | struct packet_header *header = |
| 431 | (struct packet_header *) |
| 432 | lib_ring_buffer_offset_address(&buf->backend, |
| 433 | subbuf_idx * chan->backend.subbuf_size, |
| 434 | handle); |
| 435 | struct lttng_channel *lttng_chan = channel_get_private(chan); |
| 436 | uint64_t cnt = shmp_index(handle, buf->backend.buf_cnt, subbuf_idx)->seq_cnt; |
| 437 | |
| 438 | assert(header); |
| 439 | if (!header) |
| 440 | return; |
| 441 | header->magic = CTF_MAGIC_NUMBER; |
| 442 | memcpy(header->uuid, lttng_chan->uuid, sizeof(lttng_chan->uuid)); |
| 443 | header->stream_id = lttng_chan->id; |
| 444 | header->stream_instance_id = buf->backend.cpu; |
| 445 | header->ctx.timestamp_begin = tsc; |
| 446 | header->ctx.timestamp_end = 0; |
| 447 | header->ctx.content_size = ~0ULL; /* for debugging */ |
| 448 | header->ctx.packet_size = ~0ULL; |
| 449 | header->ctx.packet_seq_num = chan->backend.num_subbuf * cnt + subbuf_idx; |
| 450 | header->ctx.events_discarded = 0; |
| 451 | header->ctx.cpu_id = buf->backend.cpu; |
| 452 | } |
| 453 | |
| 454 | /* |
| 455 | * offset is assumed to never be 0 here : never deliver a completely empty |
| 456 | * subbuffer. data_size is between 1 and subbuf_size. |
| 457 | */ |
| 458 | static void client_buffer_end(struct lttng_ust_lib_ring_buffer *buf, uint64_t tsc, |
| 459 | unsigned int subbuf_idx, unsigned long data_size, |
| 460 | struct lttng_ust_shm_handle *handle) |
| 461 | { |
| 462 | struct channel *chan = shmp(handle, buf->backend.chan); |
| 463 | struct packet_header *header = |
| 464 | (struct packet_header *) |
| 465 | lib_ring_buffer_offset_address(&buf->backend, |
| 466 | subbuf_idx * chan->backend.subbuf_size, |
| 467 | handle); |
| 468 | unsigned long records_lost = 0; |
| 469 | |
| 470 | assert(header); |
| 471 | if (!header) |
| 472 | return; |
| 473 | header->ctx.timestamp_end = tsc; |
| 474 | header->ctx.content_size = |
| 475 | (uint64_t) data_size * CHAR_BIT; /* in bits */ |
| 476 | header->ctx.packet_size = |
| 477 | (uint64_t) PAGE_ALIGN(data_size) * CHAR_BIT; /* in bits */ |
| 478 | |
| 479 | records_lost += lib_ring_buffer_get_records_lost_full(&client_config, buf); |
| 480 | records_lost += lib_ring_buffer_get_records_lost_wrap(&client_config, buf); |
| 481 | records_lost += lib_ring_buffer_get_records_lost_big(&client_config, buf); |
| 482 | header->ctx.events_discarded = records_lost; |
| 483 | } |
| 484 | |
| 485 | static int client_buffer_create(struct lttng_ust_lib_ring_buffer *buf, void *priv, |
| 486 | int cpu, const char *name, struct lttng_ust_shm_handle *handle) |
| 487 | { |
| 488 | return 0; |
| 489 | } |
| 490 | |
| 491 | static void client_buffer_finalize(struct lttng_ust_lib_ring_buffer *buf, void *priv, int cpu, struct lttng_ust_shm_handle *handle) |
| 492 | { |
| 493 | } |
| 494 | |
| 495 | static void client_content_size_field(const struct lttng_ust_lib_ring_buffer_config *config, |
| 496 | size_t *offset, size_t *length) |
| 497 | { |
| 498 | *offset = offsetof(struct packet_header, ctx.content_size); |
| 499 | *length = sizeof(((struct packet_header *) NULL)->ctx.content_size); |
| 500 | } |
| 501 | |
| 502 | static void client_packet_size_field(const struct lttng_ust_lib_ring_buffer_config *config, |
| 503 | size_t *offset, size_t *length) |
| 504 | { |
| 505 | *offset = offsetof(struct packet_header, ctx.packet_size); |
| 506 | *length = sizeof(((struct packet_header *) NULL)->ctx.packet_size); |
| 507 | } |
| 508 | |
| 509 | static struct packet_header *client_packet_header(struct lttng_ust_lib_ring_buffer *buf, |
| 510 | struct lttng_ust_shm_handle *handle) |
| 511 | { |
| 512 | return lib_ring_buffer_read_offset_address(&buf->backend, 0, handle); |
| 513 | } |
| 514 | |
| 515 | static int client_timestamp_begin(struct lttng_ust_lib_ring_buffer *buf, |
| 516 | struct lttng_ust_shm_handle *handle, |
| 517 | uint64_t *timestamp_begin) |
| 518 | { |
| 519 | struct packet_header *header; |
| 520 | |
| 521 | header = client_packet_header(buf, handle); |
| 522 | if (!header) |
| 523 | return -1; |
| 524 | *timestamp_begin = header->ctx.timestamp_begin; |
| 525 | return 0; |
| 526 | } |
| 527 | |
| 528 | static int client_timestamp_end(struct lttng_ust_lib_ring_buffer *buf, |
| 529 | struct lttng_ust_shm_handle *handle, |
| 530 | uint64_t *timestamp_end) |
| 531 | { |
| 532 | struct packet_header *header; |
| 533 | |
| 534 | header = client_packet_header(buf, handle); |
| 535 | if (!header) |
| 536 | return -1; |
| 537 | *timestamp_end = header->ctx.timestamp_end; |
| 538 | return 0; |
| 539 | } |
| 540 | |
| 541 | static int client_events_discarded(struct lttng_ust_lib_ring_buffer *buf, |
| 542 | struct lttng_ust_shm_handle *handle, |
| 543 | uint64_t *events_discarded) |
| 544 | { |
| 545 | struct packet_header *header; |
| 546 | |
| 547 | header = client_packet_header(buf, handle); |
| 548 | if (!header) |
| 549 | return -1; |
| 550 | *events_discarded = header->ctx.events_discarded; |
| 551 | return 0; |
| 552 | } |
| 553 | |
| 554 | static int client_content_size(struct lttng_ust_lib_ring_buffer *buf, |
| 555 | struct lttng_ust_shm_handle *handle, |
| 556 | uint64_t *content_size) |
| 557 | { |
| 558 | struct packet_header *header; |
| 559 | |
| 560 | header = client_packet_header(buf, handle); |
| 561 | if (!header) |
| 562 | return -1; |
| 563 | *content_size = header->ctx.content_size; |
| 564 | return 0; |
| 565 | } |
| 566 | |
| 567 | static int client_packet_size(struct lttng_ust_lib_ring_buffer *buf, |
| 568 | struct lttng_ust_shm_handle *handle, |
| 569 | uint64_t *packet_size) |
| 570 | { |
| 571 | struct packet_header *header; |
| 572 | |
| 573 | header = client_packet_header(buf, handle); |
| 574 | if (!header) |
| 575 | return -1; |
| 576 | *packet_size = header->ctx.packet_size; |
| 577 | return 0; |
| 578 | } |
| 579 | |
| 580 | static int client_stream_id(struct lttng_ust_lib_ring_buffer *buf, |
| 581 | struct lttng_ust_shm_handle *handle, |
| 582 | uint64_t *stream_id) |
| 583 | { |
| 584 | struct channel *chan = shmp(handle, buf->backend.chan); |
| 585 | struct lttng_channel *lttng_chan = channel_get_private(chan); |
| 586 | |
| 587 | *stream_id = lttng_chan->id; |
| 588 | |
| 589 | return 0; |
| 590 | } |
| 591 | |
| 592 | static int client_current_timestamp(struct lttng_ust_lib_ring_buffer *buf, |
| 593 | struct lttng_ust_shm_handle *handle, |
| 594 | uint64_t *ts) |
| 595 | { |
| 596 | struct channel *chan; |
| 597 | |
| 598 | chan = shmp(handle, handle->chan); |
| 599 | *ts = client_ring_buffer_clock_read(chan); |
| 600 | |
| 601 | return 0; |
| 602 | } |
| 603 | |
| 604 | static int client_sequence_number(struct lttng_ust_lib_ring_buffer *buf, |
| 605 | struct lttng_ust_shm_handle *handle, |
| 606 | uint64_t *seq) |
| 607 | { |
| 608 | struct packet_header *header; |
| 609 | |
| 610 | header = client_packet_header(buf, handle); |
| 611 | if (!header) |
| 612 | return -1; |
| 613 | *seq = header->ctx.packet_seq_num; |
| 614 | return 0; |
| 615 | } |
| 616 | |
| 617 | static int client_instance_id(struct lttng_ust_lib_ring_buffer *buf, |
| 618 | struct lttng_ust_shm_handle *handle, |
| 619 | uint64_t *id) |
| 620 | { |
| 621 | *id = buf->backend.cpu; |
| 622 | |
| 623 | return 0; |
| 624 | } |
| 625 | |
| 626 | static const |
| 627 | struct lttng_ust_client_lib_ring_buffer_client_cb client_cb = { |
| 628 | .parent = { |
| 629 | .ring_buffer_clock_read = client_ring_buffer_clock_read, |
| 630 | .record_header_size = client_record_header_size, |
| 631 | .subbuffer_header_size = client_packet_header_size, |
| 632 | .buffer_begin = client_buffer_begin, |
| 633 | .buffer_end = client_buffer_end, |
| 634 | .buffer_create = client_buffer_create, |
| 635 | .buffer_finalize = client_buffer_finalize, |
| 636 | .content_size_field = client_content_size_field, |
| 637 | .packet_size_field = client_packet_size_field, |
| 638 | }, |
| 639 | .timestamp_begin = client_timestamp_begin, |
| 640 | .timestamp_end = client_timestamp_end, |
| 641 | .events_discarded = client_events_discarded, |
| 642 | .content_size = client_content_size, |
| 643 | .packet_size = client_packet_size, |
| 644 | .stream_id = client_stream_id, |
| 645 | .current_timestamp = client_current_timestamp, |
| 646 | .sequence_number = client_sequence_number, |
| 647 | .instance_id = client_instance_id, |
| 648 | }; |
| 649 | |
| 650 | static const struct lttng_ust_lib_ring_buffer_config client_config = { |
| 651 | .cb.ring_buffer_clock_read = client_ring_buffer_clock_read, |
| 652 | .cb.record_header_size = client_record_header_size, |
| 653 | .cb.subbuffer_header_size = client_packet_header_size, |
| 654 | .cb.buffer_begin = client_buffer_begin, |
| 655 | .cb.buffer_end = client_buffer_end, |
| 656 | .cb.buffer_create = client_buffer_create, |
| 657 | .cb.buffer_finalize = client_buffer_finalize, |
| 658 | .cb.content_size_field = client_content_size_field, |
| 659 | .cb.packet_size_field = client_packet_size_field, |
| 660 | |
| 661 | .tsc_bits = LTTNG_COMPACT_TSC_BITS, |
| 662 | .alloc = RING_BUFFER_ALLOC_PER_CPU, |
| 663 | .sync = RING_BUFFER_SYNC_GLOBAL, |
| 664 | .mode = RING_BUFFER_MODE_TEMPLATE, |
| 665 | .backend = RING_BUFFER_PAGE, |
| 666 | .output = RING_BUFFER_MMAP, |
| 667 | .oops = RING_BUFFER_OOPS_CONSISTENCY, |
| 668 | .ipi = RING_BUFFER_NO_IPI_BARRIER, |
| 669 | .wakeup = LTTNG_CLIENT_WAKEUP, |
| 670 | .client_type = LTTNG_CLIENT_TYPE, |
| 671 | |
| 672 | .cb_ptr = &client_cb.parent, |
| 673 | }; |
| 674 | |
| 675 | const struct lttng_ust_client_lib_ring_buffer_client_cb *LTTNG_CLIENT_CALLBACKS = &client_cb; |
| 676 | |
| 677 | static |
| 678 | struct lttng_channel *_channel_create(const char *name, |
| 679 | void *buf_addr, |
| 680 | size_t subbuf_size, size_t num_subbuf, |
| 681 | unsigned int switch_timer_interval, |
| 682 | unsigned int read_timer_interval, |
| 683 | unsigned char *uuid, |
| 684 | uint32_t chan_id, |
| 685 | const int *stream_fds, int nr_stream_fds, |
| 686 | int64_t blocking_timeout) |
| 687 | { |
| 688 | struct lttng_channel chan_priv_init; |
| 689 | struct lttng_ust_shm_handle *handle; |
| 690 | struct lttng_channel *lttng_chan; |
| 691 | void *priv; |
| 692 | |
| 693 | memset(&chan_priv_init, 0, sizeof(chan_priv_init)); |
| 694 | memcpy(chan_priv_init.uuid, uuid, LTTNG_UST_UUID_LEN); |
| 695 | chan_priv_init.id = chan_id; |
| 696 | handle = channel_create(&client_config, name, |
| 697 | &priv, __alignof__(struct lttng_channel), |
| 698 | sizeof(struct lttng_channel), |
| 699 | &chan_priv_init, |
| 700 | buf_addr, subbuf_size, num_subbuf, |
| 701 | switch_timer_interval, read_timer_interval, |
| 702 | stream_fds, nr_stream_fds, blocking_timeout); |
| 703 | if (!handle) |
| 704 | return NULL; |
| 705 | lttng_chan = priv; |
| 706 | lttng_chan->handle = handle; |
| 707 | lttng_chan->chan = shmp(handle, handle->chan); |
| 708 | return lttng_chan; |
| 709 | } |
| 710 | |
| 711 | static |
| 712 | void lttng_channel_destroy(struct lttng_channel *chan) |
| 713 | { |
| 714 | channel_destroy(chan->chan, chan->handle, 1); |
| 715 | } |
| 716 | |
| 717 | static |
| 718 | int lttng_event_reserve(struct lttng_ust_lib_ring_buffer_ctx *ctx, |
| 719 | uint32_t event_id) |
| 720 | { |
| 721 | struct lttng_channel *lttng_chan = channel_get_private(ctx->chan); |
| 722 | struct lttng_event *event = ctx->priv; |
| 723 | struct lttng_stack_ctx *lttng_ctx = ctx->priv2; |
| 724 | struct lttng_client_ctx client_ctx; |
| 725 | int ret, cpu; |
| 726 | |
| 727 | /* Compute internal size of context structures. */ |
| 728 | |
| 729 | if (lttng_ctx) { |
| 730 | /* 2.8+ probe ABI. */ |
| 731 | ctx_get_struct_size(lttng_ctx->chan_ctx, &client_ctx.packet_context_len, |
| 732 | APP_CTX_ENABLED); |
| 733 | ctx_get_struct_size(lttng_ctx->event_ctx, &client_ctx.event_context_len, |
| 734 | APP_CTX_ENABLED); |
| 735 | } else { |
| 736 | /* Pre 2.8 probe ABI. */ |
| 737 | ctx_get_struct_size(lttng_chan->ctx, &client_ctx.packet_context_len, |
| 738 | APP_CTX_DISABLED); |
| 739 | ctx_get_struct_size(event->ctx, &client_ctx.event_context_len, |
| 740 | APP_CTX_DISABLED); |
| 741 | } |
| 742 | |
| 743 | cpu = lib_ring_buffer_get_cpu(&client_config); |
| 744 | if (cpu < 0) |
| 745 | return -EPERM; |
| 746 | ctx->cpu = cpu; |
| 747 | |
| 748 | switch (lttng_chan->header_type) { |
| 749 | case 1: /* compact */ |
| 750 | if (event_id > 30) |
| 751 | ctx->rflags |= LTTNG_RFLAG_EXTENDED; |
| 752 | break; |
| 753 | case 2: /* large */ |
| 754 | if (event_id > 65534) |
| 755 | ctx->rflags |= LTTNG_RFLAG_EXTENDED; |
| 756 | break; |
| 757 | default: |
| 758 | WARN_ON_ONCE(1); |
| 759 | } |
| 760 | |
| 761 | ret = lib_ring_buffer_reserve(&client_config, ctx, &client_ctx); |
| 762 | if (caa_unlikely(ret)) |
| 763 | goto put; |
| 764 | if (caa_likely(ctx->ctx_len |
| 765 | >= sizeof(struct lttng_ust_lib_ring_buffer_ctx))) { |
| 766 | if (lib_ring_buffer_backend_get_pages(&client_config, ctx, |
| 767 | &ctx->backend_pages)) { |
| 768 | ret = -EPERM; |
| 769 | goto put; |
| 770 | } |
| 771 | } |
| 772 | lttng_write_event_header(&client_config, ctx, event_id); |
| 773 | return 0; |
| 774 | put: |
| 775 | lib_ring_buffer_put_cpu(&client_config); |
| 776 | return ret; |
| 777 | } |
| 778 | |
| 779 | static |
| 780 | void lttng_event_commit(struct lttng_ust_lib_ring_buffer_ctx *ctx) |
| 781 | { |
| 782 | lib_ring_buffer_commit(&client_config, ctx); |
| 783 | lib_ring_buffer_put_cpu(&client_config); |
| 784 | } |
| 785 | |
| 786 | static |
| 787 | void lttng_event_write(struct lttng_ust_lib_ring_buffer_ctx *ctx, const void *src, |
| 788 | size_t len) |
| 789 | { |
| 790 | lib_ring_buffer_write(&client_config, ctx, src, len); |
| 791 | } |
| 792 | |
| 793 | static |
| 794 | void lttng_event_strcpy(struct lttng_ust_lib_ring_buffer_ctx *ctx, const char *src, |
| 795 | size_t len) |
| 796 | { |
| 797 | lib_ring_buffer_strcpy(&client_config, ctx, src, len, '#'); |
| 798 | } |
| 799 | |
| 800 | #if 0 |
| 801 | static |
| 802 | wait_queue_head_t *lttng_get_reader_wait_queue(struct channel *chan) |
| 803 | { |
| 804 | return &chan->read_wait; |
| 805 | } |
| 806 | |
| 807 | static |
| 808 | wait_queue_head_t *lttng_get_hp_wait_queue(struct channel *chan) |
| 809 | { |
| 810 | return &chan->hp_wait; |
| 811 | } |
| 812 | #endif //0 |
| 813 | |
| 814 | static |
| 815 | int lttng_is_finalized(struct channel *chan) |
| 816 | { |
| 817 | return lib_ring_buffer_channel_is_finalized(chan); |
| 818 | } |
| 819 | |
| 820 | static |
| 821 | int lttng_is_disabled(struct channel *chan) |
| 822 | { |
| 823 | return lib_ring_buffer_channel_is_disabled(chan); |
| 824 | } |
| 825 | |
| 826 | static |
| 827 | int lttng_flush_buffer(struct channel *chan, struct lttng_ust_shm_handle *handle) |
| 828 | { |
| 829 | struct lttng_ust_lib_ring_buffer *buf; |
| 830 | int cpu; |
| 831 | |
| 832 | for_each_channel_cpu(cpu, chan) { |
| 833 | int shm_fd, wait_fd, wakeup_fd; |
| 834 | uint64_t memory_map_size; |
| 835 | |
| 836 | buf = channel_get_ring_buffer(&client_config, chan, |
| 837 | cpu, handle, &shm_fd, &wait_fd, |
| 838 | &wakeup_fd, &memory_map_size); |
| 839 | lib_ring_buffer_switch(&client_config, buf, |
| 840 | SWITCH_ACTIVE, handle); |
| 841 | } |
| 842 | return 0; |
| 843 | } |
| 844 | |
| 845 | static struct lttng_transport lttng_relay_transport = { |
| 846 | .name = "relay-" RING_BUFFER_MODE_TEMPLATE_STRING "-mmap", |
| 847 | .ops = { |
| 848 | .channel_create = _channel_create, |
| 849 | .channel_destroy = lttng_channel_destroy, |
| 850 | .u.has_strcpy = 1, |
| 851 | .event_reserve = lttng_event_reserve, |
| 852 | .event_commit = lttng_event_commit, |
| 853 | .event_write = lttng_event_write, |
| 854 | .packet_avail_size = NULL, /* Would be racy anyway */ |
| 855 | //.get_reader_wait_queue = lttng_get_reader_wait_queue, |
| 856 | //.get_hp_wait_queue = lttng_get_hp_wait_queue, |
| 857 | .is_finalized = lttng_is_finalized, |
| 858 | .is_disabled = lttng_is_disabled, |
| 859 | .flush_buffer = lttng_flush_buffer, |
| 860 | .event_strcpy = lttng_event_strcpy, |
| 861 | }, |
| 862 | .client_config = &client_config, |
| 863 | }; |
| 864 | |
| 865 | void RING_BUFFER_MODE_TEMPLATE_INIT(void) |
| 866 | { |
| 867 | DBG("LTT : ltt ring buffer client \"%s\" init\n", |
| 868 | "relay-" RING_BUFFER_MODE_TEMPLATE_STRING "-mmap"); |
| 869 | lttng_transport_register(<tng_relay_transport); |
| 870 | } |
| 871 | |
| 872 | void RING_BUFFER_MODE_TEMPLATE_EXIT(void) |
| 873 | { |
| 874 | DBG("LTT : ltt ring buffer client \"%s\" exit\n", |
| 875 | "relay-" RING_BUFFER_MODE_TEMPLATE_STRING "-mmap"); |
| 876 | lttng_transport_unregister(<tng_relay_transport); |
| 877 | } |