| 1 | /* SPDX-License-Identifier: (GPL-2.0 or LGPL-2.1) |
| 2 | * |
| 3 | * lttng-ring-buffer-client.h |
| 4 | * |
| 5 | * LTTng lib ring buffer client template. |
| 6 | * |
| 7 | * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 8 | */ |
| 9 | |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/types.h> |
| 12 | #include <lib/bitfield.h> |
| 13 | #include <wrapper/vmalloc.h> /* for wrapper_vmalloc_sync_mappings() */ |
| 14 | #include <wrapper/trace-clock.h> |
| 15 | #include <lttng-events.h> |
| 16 | #include <lttng-tracer.h> |
| 17 | #include <wrapper/ringbuffer/frontend_types.h> |
| 18 | |
| 19 | #define LTTNG_COMPACT_EVENT_BITS 5 |
| 20 | #define LTTNG_COMPACT_TSC_BITS 27 |
| 21 | |
| 22 | static struct lttng_transport lttng_relay_transport; |
| 23 | |
| 24 | /* |
| 25 | * Keep the natural field alignment for _each field_ within this structure if |
| 26 | * you ever add/remove a field from this header. Packed attribute is not used |
| 27 | * because gcc generates poor code on at least powerpc and mips. Don't ever |
| 28 | * let gcc add padding between the structure elements. |
| 29 | * |
| 30 | * The guarantee we have with timestamps is that all the events in a |
| 31 | * packet are included (inclusive) within the begin/end timestamps of |
| 32 | * the packet. Another guarantee we have is that the "timestamp begin", |
| 33 | * as well as the event timestamps, are monotonically increasing (never |
| 34 | * decrease) when moving forward in a stream (physically). But this |
| 35 | * guarantee does not apply to "timestamp end", because it is sampled at |
| 36 | * commit time, which is not ordered with respect to space reservation. |
| 37 | */ |
| 38 | |
| 39 | struct packet_header { |
| 40 | /* Trace packet header */ |
| 41 | uint32_t magic; /* |
| 42 | * Trace magic number. |
| 43 | * contains endianness information. |
| 44 | */ |
| 45 | uint8_t uuid[16]; |
| 46 | uint32_t stream_id; |
| 47 | uint64_t stream_instance_id; |
| 48 | |
| 49 | struct { |
| 50 | /* Stream packet context */ |
| 51 | uint64_t timestamp_begin; /* Cycle count at subbuffer start */ |
| 52 | uint64_t timestamp_end; /* Cycle count at subbuffer end */ |
| 53 | uint64_t content_size; /* Size of data in subbuffer */ |
| 54 | uint64_t packet_size; /* Subbuffer size (include padding) */ |
| 55 | uint64_t packet_seq_num; /* Packet sequence number */ |
| 56 | unsigned long events_discarded; /* |
| 57 | * Events lost in this subbuffer since |
| 58 | * the beginning of the trace. |
| 59 | * (may overflow) |
| 60 | */ |
| 61 | uint32_t cpu_id; /* CPU id associated with stream */ |
| 62 | uint8_t header_end; /* End of header */ |
| 63 | } ctx; |
| 64 | }; |
| 65 | |
| 66 | struct lttng_client_ctx { |
| 67 | size_t packet_context_len; |
| 68 | size_t event_context_len; |
| 69 | }; |
| 70 | |
| 71 | static inline notrace u64 lib_ring_buffer_clock_read(struct channel *chan) |
| 72 | { |
| 73 | return trace_clock_read64(); |
| 74 | } |
| 75 | |
| 76 | static inline |
| 77 | size_t ctx_get_aligned_size(size_t offset, struct lttng_ctx *ctx, |
| 78 | size_t ctx_len) |
| 79 | { |
| 80 | size_t orig_offset = offset; |
| 81 | |
| 82 | if (likely(!ctx)) |
| 83 | return 0; |
| 84 | offset += lib_ring_buffer_align(offset, ctx->largest_align); |
| 85 | offset += ctx_len; |
| 86 | return offset - orig_offset; |
| 87 | } |
| 88 | |
| 89 | static inline |
| 90 | void ctx_get_struct_size(struct lttng_ctx *ctx, size_t *ctx_len, |
| 91 | struct lttng_channel *chan, struct lib_ring_buffer_ctx *bufctx) |
| 92 | { |
| 93 | int i; |
| 94 | size_t offset = 0; |
| 95 | |
| 96 | if (likely(!ctx)) { |
| 97 | *ctx_len = 0; |
| 98 | return; |
| 99 | } |
| 100 | for (i = 0; i < ctx->nr_fields; i++) { |
| 101 | if (ctx->fields[i].get_size) |
| 102 | offset += ctx->fields[i].get_size(offset); |
| 103 | if (ctx->fields[i].get_size_arg) |
| 104 | offset += ctx->fields[i].get_size_arg(offset, |
| 105 | &ctx->fields[i], bufctx, chan); |
| 106 | } |
| 107 | *ctx_len = offset; |
| 108 | } |
| 109 | |
| 110 | static inline |
| 111 | void ctx_record(struct lib_ring_buffer_ctx *bufctx, |
| 112 | struct lttng_channel *chan, |
| 113 | struct lttng_ctx *ctx) |
| 114 | { |
| 115 | int i; |
| 116 | |
| 117 | if (likely(!ctx)) |
| 118 | return; |
| 119 | lib_ring_buffer_align_ctx(bufctx, ctx->largest_align); |
| 120 | for (i = 0; i < ctx->nr_fields; i++) |
| 121 | ctx->fields[i].record(&ctx->fields[i], bufctx, chan); |
| 122 | } |
| 123 | |
| 124 | /* |
| 125 | * record_header_size - Calculate the header size and padding necessary. |
| 126 | * @config: ring buffer instance configuration |
| 127 | * @chan: channel |
| 128 | * @offset: offset in the write buffer |
| 129 | * @pre_header_padding: padding to add before the header (output) |
| 130 | * @ctx: reservation context |
| 131 | * |
| 132 | * Returns the event header size (including padding). |
| 133 | * |
| 134 | * The payload must itself determine its own alignment from the biggest type it |
| 135 | * contains. |
| 136 | */ |
| 137 | static __inline__ |
| 138 | size_t record_header_size(const struct lib_ring_buffer_config *config, |
| 139 | struct channel *chan, size_t offset, |
| 140 | size_t *pre_header_padding, |
| 141 | struct lib_ring_buffer_ctx *ctx, |
| 142 | struct lttng_client_ctx *client_ctx) |
| 143 | { |
| 144 | struct lttng_channel *lttng_chan = channel_get_private(chan); |
| 145 | struct lttng_probe_ctx *lttng_probe_ctx = ctx->priv; |
| 146 | struct lttng_event *event = lttng_probe_ctx->event; |
| 147 | size_t orig_offset = offset; |
| 148 | size_t padding; |
| 149 | |
| 150 | switch (lttng_chan->header_type) { |
| 151 | case 1: /* compact */ |
| 152 | padding = lib_ring_buffer_align(offset, lttng_alignof(uint32_t)); |
| 153 | offset += padding; |
| 154 | if (!(ctx->rflags & (RING_BUFFER_RFLAG_FULL_TSC | LTTNG_RFLAG_EXTENDED))) { |
| 155 | offset += sizeof(uint32_t); /* id and timestamp */ |
| 156 | } else { |
| 157 | /* Minimum space taken by LTTNG_COMPACT_EVENT_BITS id */ |
| 158 | offset += (LTTNG_COMPACT_EVENT_BITS + CHAR_BIT - 1) / CHAR_BIT; |
| 159 | /* Align extended struct on largest member */ |
| 160 | offset += lib_ring_buffer_align(offset, lttng_alignof(uint64_t)); |
| 161 | offset += sizeof(uint32_t); /* id */ |
| 162 | offset += lib_ring_buffer_align(offset, lttng_alignof(uint64_t)); |
| 163 | offset += sizeof(uint64_t); /* timestamp */ |
| 164 | } |
| 165 | break; |
| 166 | case 2: /* large */ |
| 167 | padding = lib_ring_buffer_align(offset, lttng_alignof(uint16_t)); |
| 168 | offset += padding; |
| 169 | offset += sizeof(uint16_t); |
| 170 | if (!(ctx->rflags & (RING_BUFFER_RFLAG_FULL_TSC | LTTNG_RFLAG_EXTENDED))) { |
| 171 | offset += lib_ring_buffer_align(offset, lttng_alignof(uint32_t)); |
| 172 | offset += sizeof(uint32_t); /* timestamp */ |
| 173 | } else { |
| 174 | /* Align extended struct on largest member */ |
| 175 | offset += lib_ring_buffer_align(offset, lttng_alignof(uint64_t)); |
| 176 | offset += sizeof(uint32_t); /* id */ |
| 177 | offset += lib_ring_buffer_align(offset, lttng_alignof(uint64_t)); |
| 178 | offset += sizeof(uint64_t); /* timestamp */ |
| 179 | } |
| 180 | break; |
| 181 | default: |
| 182 | padding = 0; |
| 183 | WARN_ON_ONCE(1); |
| 184 | } |
| 185 | offset += ctx_get_aligned_size(offset, lttng_chan->ctx, |
| 186 | client_ctx->packet_context_len); |
| 187 | offset += ctx_get_aligned_size(offset, event->ctx, |
| 188 | client_ctx->event_context_len); |
| 189 | |
| 190 | *pre_header_padding = padding; |
| 191 | return offset - orig_offset; |
| 192 | } |
| 193 | |
| 194 | #include <wrapper/ringbuffer/api.h> |
| 195 | |
| 196 | static |
| 197 | void lttng_write_event_header_slow(const struct lib_ring_buffer_config *config, |
| 198 | struct lib_ring_buffer_ctx *ctx, |
| 199 | uint32_t event_id); |
| 200 | |
| 201 | /* |
| 202 | * lttng_write_event_header |
| 203 | * |
| 204 | * Writes the event header to the offset (already aligned on 32-bits). |
| 205 | * |
| 206 | * @config: ring buffer instance configuration |
| 207 | * @ctx: reservation context |
| 208 | * @event_id: event ID |
| 209 | */ |
| 210 | static __inline__ |
| 211 | void lttng_write_event_header(const struct lib_ring_buffer_config *config, |
| 212 | struct lib_ring_buffer_ctx *ctx, |
| 213 | uint32_t event_id) |
| 214 | { |
| 215 | struct lttng_channel *lttng_chan = channel_get_private(ctx->chan); |
| 216 | struct lttng_probe_ctx *lttng_probe_ctx = ctx->priv; |
| 217 | struct lttng_event *event = lttng_probe_ctx->event; |
| 218 | |
| 219 | if (unlikely(ctx->rflags)) |
| 220 | goto slow_path; |
| 221 | |
| 222 | switch (lttng_chan->header_type) { |
| 223 | case 1: /* compact */ |
| 224 | { |
| 225 | uint32_t id_time = 0; |
| 226 | |
| 227 | bt_bitfield_write(&id_time, uint32_t, |
| 228 | 0, |
| 229 | LTTNG_COMPACT_EVENT_BITS, |
| 230 | event_id); |
| 231 | bt_bitfield_write(&id_time, uint32_t, |
| 232 | LTTNG_COMPACT_EVENT_BITS, |
| 233 | LTTNG_COMPACT_TSC_BITS, |
| 234 | ctx->tsc); |
| 235 | lib_ring_buffer_write(config, ctx, &id_time, sizeof(id_time)); |
| 236 | break; |
| 237 | } |
| 238 | case 2: /* large */ |
| 239 | { |
| 240 | uint32_t timestamp = (uint32_t) ctx->tsc; |
| 241 | uint16_t id = event_id; |
| 242 | |
| 243 | lib_ring_buffer_write(config, ctx, &id, sizeof(id)); |
| 244 | lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint32_t)); |
| 245 | lib_ring_buffer_write(config, ctx, ×tamp, sizeof(timestamp)); |
| 246 | break; |
| 247 | } |
| 248 | default: |
| 249 | WARN_ON_ONCE(1); |
| 250 | } |
| 251 | |
| 252 | ctx_record(ctx, lttng_chan, lttng_chan->ctx); |
| 253 | ctx_record(ctx, lttng_chan, event->ctx); |
| 254 | lib_ring_buffer_align_ctx(ctx, ctx->largest_align); |
| 255 | |
| 256 | return; |
| 257 | |
| 258 | slow_path: |
| 259 | lttng_write_event_header_slow(config, ctx, event_id); |
| 260 | } |
| 261 | |
| 262 | static |
| 263 | void lttng_write_event_header_slow(const struct lib_ring_buffer_config *config, |
| 264 | struct lib_ring_buffer_ctx *ctx, |
| 265 | uint32_t event_id) |
| 266 | { |
| 267 | struct lttng_channel *lttng_chan = channel_get_private(ctx->chan); |
| 268 | struct lttng_probe_ctx *lttng_probe_ctx = ctx->priv; |
| 269 | struct lttng_event *event = lttng_probe_ctx->event; |
| 270 | |
| 271 | switch (lttng_chan->header_type) { |
| 272 | case 1: /* compact */ |
| 273 | if (!(ctx->rflags & (RING_BUFFER_RFLAG_FULL_TSC | LTTNG_RFLAG_EXTENDED))) { |
| 274 | uint32_t id_time = 0; |
| 275 | |
| 276 | bt_bitfield_write(&id_time, uint32_t, |
| 277 | 0, |
| 278 | LTTNG_COMPACT_EVENT_BITS, |
| 279 | event_id); |
| 280 | bt_bitfield_write(&id_time, uint32_t, |
| 281 | LTTNG_COMPACT_EVENT_BITS, |
| 282 | LTTNG_COMPACT_TSC_BITS, ctx->tsc); |
| 283 | lib_ring_buffer_write(config, ctx, &id_time, sizeof(id_time)); |
| 284 | } else { |
| 285 | uint8_t id = 0; |
| 286 | uint64_t timestamp = ctx->tsc; |
| 287 | |
| 288 | bt_bitfield_write(&id, uint8_t, |
| 289 | 0, |
| 290 | LTTNG_COMPACT_EVENT_BITS, |
| 291 | 31); |
| 292 | lib_ring_buffer_write(config, ctx, &id, sizeof(id)); |
| 293 | /* Align extended struct on largest member */ |
| 294 | lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint64_t)); |
| 295 | lib_ring_buffer_write(config, ctx, &event_id, sizeof(event_id)); |
| 296 | lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint64_t)); |
| 297 | lib_ring_buffer_write(config, ctx, ×tamp, sizeof(timestamp)); |
| 298 | } |
| 299 | break; |
| 300 | case 2: /* large */ |
| 301 | { |
| 302 | if (!(ctx->rflags & (RING_BUFFER_RFLAG_FULL_TSC | LTTNG_RFLAG_EXTENDED))) { |
| 303 | uint32_t timestamp = (uint32_t) ctx->tsc; |
| 304 | uint16_t id = event_id; |
| 305 | |
| 306 | lib_ring_buffer_write(config, ctx, &id, sizeof(id)); |
| 307 | lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint32_t)); |
| 308 | lib_ring_buffer_write(config, ctx, ×tamp, sizeof(timestamp)); |
| 309 | } else { |
| 310 | uint16_t id = 65535; |
| 311 | uint64_t timestamp = ctx->tsc; |
| 312 | |
| 313 | lib_ring_buffer_write(config, ctx, &id, sizeof(id)); |
| 314 | /* Align extended struct on largest member */ |
| 315 | lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint64_t)); |
| 316 | lib_ring_buffer_write(config, ctx, &event_id, sizeof(event_id)); |
| 317 | lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint64_t)); |
| 318 | lib_ring_buffer_write(config, ctx, ×tamp, sizeof(timestamp)); |
| 319 | } |
| 320 | break; |
| 321 | } |
| 322 | default: |
| 323 | WARN_ON_ONCE(1); |
| 324 | } |
| 325 | ctx_record(ctx, lttng_chan, lttng_chan->ctx); |
| 326 | ctx_record(ctx, lttng_chan, event->ctx); |
| 327 | lib_ring_buffer_align_ctx(ctx, ctx->largest_align); |
| 328 | } |
| 329 | |
| 330 | static const struct lib_ring_buffer_config client_config; |
| 331 | |
| 332 | static u64 client_ring_buffer_clock_read(struct channel *chan) |
| 333 | { |
| 334 | return lib_ring_buffer_clock_read(chan); |
| 335 | } |
| 336 | |
| 337 | static |
| 338 | size_t client_record_header_size(const struct lib_ring_buffer_config *config, |
| 339 | struct channel *chan, size_t offset, |
| 340 | size_t *pre_header_padding, |
| 341 | struct lib_ring_buffer_ctx *ctx, |
| 342 | void *client_ctx) |
| 343 | { |
| 344 | return record_header_size(config, chan, offset, |
| 345 | pre_header_padding, ctx, client_ctx); |
| 346 | } |
| 347 | |
| 348 | /** |
| 349 | * client_packet_header_size - called on buffer-switch to a new sub-buffer |
| 350 | * |
| 351 | * Return header size without padding after the structure. Don't use packed |
| 352 | * structure because gcc generates inefficient code on some architectures |
| 353 | * (powerpc, mips..) |
| 354 | */ |
| 355 | static size_t client_packet_header_size(void) |
| 356 | { |
| 357 | return offsetof(struct packet_header, ctx.header_end); |
| 358 | } |
| 359 | |
| 360 | static void client_buffer_begin(struct lib_ring_buffer *buf, u64 tsc, |
| 361 | unsigned int subbuf_idx) |
| 362 | { |
| 363 | struct channel *chan = buf->backend.chan; |
| 364 | struct packet_header *header = |
| 365 | (struct packet_header *) |
| 366 | lib_ring_buffer_offset_address(&buf->backend, |
| 367 | subbuf_idx * chan->backend.subbuf_size); |
| 368 | struct lttng_channel *lttng_chan = channel_get_private(chan); |
| 369 | struct lttng_session *session = lttng_chan->session; |
| 370 | |
| 371 | header->magic = CTF_MAGIC_NUMBER; |
| 372 | memcpy(header->uuid, session->uuid.b, sizeof(session->uuid)); |
| 373 | header->stream_id = lttng_chan->id; |
| 374 | header->stream_instance_id = buf->backend.cpu; |
| 375 | header->ctx.timestamp_begin = tsc; |
| 376 | header->ctx.timestamp_end = 0; |
| 377 | header->ctx.content_size = ~0ULL; /* for debugging */ |
| 378 | header->ctx.packet_size = ~0ULL; |
| 379 | header->ctx.packet_seq_num = chan->backend.num_subbuf * \ |
| 380 | buf->backend.buf_cnt[subbuf_idx].seq_cnt + \ |
| 381 | subbuf_idx; |
| 382 | header->ctx.events_discarded = 0; |
| 383 | header->ctx.cpu_id = buf->backend.cpu; |
| 384 | } |
| 385 | |
| 386 | /* |
| 387 | * offset is assumed to never be 0 here : never deliver a completely empty |
| 388 | * subbuffer. data_size is between 1 and subbuf_size. |
| 389 | */ |
| 390 | static void client_buffer_end(struct lib_ring_buffer *buf, u64 tsc, |
| 391 | unsigned int subbuf_idx, unsigned long data_size) |
| 392 | { |
| 393 | struct channel *chan = buf->backend.chan; |
| 394 | struct packet_header *header = |
| 395 | (struct packet_header *) |
| 396 | lib_ring_buffer_offset_address(&buf->backend, |
| 397 | subbuf_idx * chan->backend.subbuf_size); |
| 398 | unsigned long records_lost = 0; |
| 399 | |
| 400 | header->ctx.timestamp_end = tsc; |
| 401 | header->ctx.content_size = |
| 402 | (uint64_t) data_size * CHAR_BIT; /* in bits */ |
| 403 | header->ctx.packet_size = |
| 404 | (uint64_t) PAGE_ALIGN(data_size) * CHAR_BIT; /* in bits */ |
| 405 | records_lost += lib_ring_buffer_get_records_lost_full(&client_config, buf); |
| 406 | records_lost += lib_ring_buffer_get_records_lost_wrap(&client_config, buf); |
| 407 | records_lost += lib_ring_buffer_get_records_lost_big(&client_config, buf); |
| 408 | header->ctx.events_discarded = records_lost; |
| 409 | } |
| 410 | |
| 411 | static int client_buffer_create(struct lib_ring_buffer *buf, void *priv, |
| 412 | int cpu, const char *name) |
| 413 | { |
| 414 | return 0; |
| 415 | } |
| 416 | |
| 417 | static void client_buffer_finalize(struct lib_ring_buffer *buf, void *priv, int cpu) |
| 418 | { |
| 419 | } |
| 420 | |
| 421 | static struct packet_header *client_packet_header( |
| 422 | const struct lib_ring_buffer_config *config, |
| 423 | struct lib_ring_buffer *buf) |
| 424 | { |
| 425 | return lib_ring_buffer_read_offset_address(&buf->backend, 0); |
| 426 | } |
| 427 | |
| 428 | static int client_timestamp_begin(const struct lib_ring_buffer_config *config, |
| 429 | struct lib_ring_buffer *buf, |
| 430 | uint64_t *timestamp_begin) |
| 431 | { |
| 432 | struct packet_header *header = client_packet_header(config, buf); |
| 433 | *timestamp_begin = header->ctx.timestamp_begin; |
| 434 | |
| 435 | return 0; |
| 436 | } |
| 437 | |
| 438 | static int client_timestamp_end(const struct lib_ring_buffer_config *config, |
| 439 | struct lib_ring_buffer *buf, |
| 440 | uint64_t *timestamp_end) |
| 441 | { |
| 442 | struct packet_header *header = client_packet_header(config, buf); |
| 443 | *timestamp_end = header->ctx.timestamp_end; |
| 444 | |
| 445 | return 0; |
| 446 | } |
| 447 | |
| 448 | static int client_events_discarded(const struct lib_ring_buffer_config *config, |
| 449 | struct lib_ring_buffer *buf, |
| 450 | uint64_t *events_discarded) |
| 451 | { |
| 452 | struct packet_header *header = client_packet_header(config, buf); |
| 453 | *events_discarded = header->ctx.events_discarded; |
| 454 | |
| 455 | return 0; |
| 456 | } |
| 457 | |
| 458 | static int client_content_size(const struct lib_ring_buffer_config *config, |
| 459 | struct lib_ring_buffer *buf, |
| 460 | uint64_t *content_size) |
| 461 | { |
| 462 | struct packet_header *header = client_packet_header(config, buf); |
| 463 | *content_size = header->ctx.content_size; |
| 464 | |
| 465 | return 0; |
| 466 | } |
| 467 | |
| 468 | static int client_packet_size(const struct lib_ring_buffer_config *config, |
| 469 | struct lib_ring_buffer *buf, |
| 470 | uint64_t *packet_size) |
| 471 | { |
| 472 | struct packet_header *header = client_packet_header(config, buf); |
| 473 | *packet_size = header->ctx.packet_size; |
| 474 | |
| 475 | return 0; |
| 476 | } |
| 477 | |
| 478 | static int client_stream_id(const struct lib_ring_buffer_config *config, |
| 479 | struct lib_ring_buffer *buf, |
| 480 | uint64_t *stream_id) |
| 481 | { |
| 482 | struct channel *chan = buf->backend.chan; |
| 483 | struct lttng_channel *lttng_chan = channel_get_private(chan); |
| 484 | |
| 485 | *stream_id = lttng_chan->id; |
| 486 | return 0; |
| 487 | } |
| 488 | |
| 489 | static int client_current_timestamp(const struct lib_ring_buffer_config *config, |
| 490 | struct lib_ring_buffer *bufb, |
| 491 | uint64_t *ts) |
| 492 | { |
| 493 | *ts = config->cb.ring_buffer_clock_read(bufb->backend.chan); |
| 494 | |
| 495 | return 0; |
| 496 | } |
| 497 | |
| 498 | static int client_sequence_number(const struct lib_ring_buffer_config *config, |
| 499 | struct lib_ring_buffer *buf, |
| 500 | uint64_t *seq) |
| 501 | { |
| 502 | struct packet_header *header = client_packet_header(config, buf); |
| 503 | |
| 504 | *seq = header->ctx.packet_seq_num; |
| 505 | |
| 506 | return 0; |
| 507 | } |
| 508 | |
| 509 | static |
| 510 | int client_instance_id(const struct lib_ring_buffer_config *config, |
| 511 | struct lib_ring_buffer *buf, |
| 512 | uint64_t *id) |
| 513 | { |
| 514 | *id = buf->backend.cpu; |
| 515 | |
| 516 | return 0; |
| 517 | } |
| 518 | |
| 519 | static const struct lib_ring_buffer_config client_config = { |
| 520 | .cb.ring_buffer_clock_read = client_ring_buffer_clock_read, |
| 521 | .cb.record_header_size = client_record_header_size, |
| 522 | .cb.subbuffer_header_size = client_packet_header_size, |
| 523 | .cb.buffer_begin = client_buffer_begin, |
| 524 | .cb.buffer_end = client_buffer_end, |
| 525 | .cb.buffer_create = client_buffer_create, |
| 526 | .cb.buffer_finalize = client_buffer_finalize, |
| 527 | |
| 528 | .tsc_bits = LTTNG_COMPACT_TSC_BITS, |
| 529 | .alloc = RING_BUFFER_ALLOC_PER_CPU, |
| 530 | .sync = RING_BUFFER_SYNC_PER_CPU, |
| 531 | .mode = RING_BUFFER_MODE_TEMPLATE, |
| 532 | .backend = RING_BUFFER_PAGE, |
| 533 | .output = RING_BUFFER_OUTPUT_TEMPLATE, |
| 534 | .oops = RING_BUFFER_OOPS_CONSISTENCY, |
| 535 | .ipi = RING_BUFFER_IPI_BARRIER, |
| 536 | .wakeup = RING_BUFFER_WAKEUP_BY_TIMER, |
| 537 | }; |
| 538 | |
| 539 | static |
| 540 | void release_priv_ops(void *priv_ops) |
| 541 | { |
| 542 | module_put(THIS_MODULE); |
| 543 | } |
| 544 | |
| 545 | static |
| 546 | void lttng_channel_destroy(struct channel *chan) |
| 547 | { |
| 548 | channel_destroy(chan); |
| 549 | } |
| 550 | |
| 551 | static |
| 552 | struct channel *_channel_create(const char *name, |
| 553 | struct lttng_channel *lttng_chan, void *buf_addr, |
| 554 | size_t subbuf_size, size_t num_subbuf, |
| 555 | unsigned int switch_timer_interval, |
| 556 | unsigned int read_timer_interval) |
| 557 | { |
| 558 | struct channel *chan; |
| 559 | |
| 560 | chan = channel_create(&client_config, name, lttng_chan, buf_addr, |
| 561 | subbuf_size, num_subbuf, switch_timer_interval, |
| 562 | read_timer_interval); |
| 563 | if (chan) { |
| 564 | /* |
| 565 | * Ensure this module is not unloaded before we finish |
| 566 | * using lttng_relay_transport.ops. |
| 567 | */ |
| 568 | if (!try_module_get(THIS_MODULE)) { |
| 569 | printk(KERN_WARNING "LTT : Can't lock transport module.\n"); |
| 570 | goto error; |
| 571 | } |
| 572 | chan->backend.priv_ops = <tng_relay_transport.ops; |
| 573 | chan->backend.release_priv_ops = release_priv_ops; |
| 574 | } |
| 575 | return chan; |
| 576 | |
| 577 | error: |
| 578 | lttng_channel_destroy(chan); |
| 579 | return NULL; |
| 580 | } |
| 581 | |
| 582 | static |
| 583 | struct lib_ring_buffer *lttng_buffer_read_open(struct channel *chan) |
| 584 | { |
| 585 | struct lib_ring_buffer *buf; |
| 586 | int cpu; |
| 587 | |
| 588 | for_each_channel_cpu(cpu, chan) { |
| 589 | buf = channel_get_ring_buffer(&client_config, chan, cpu); |
| 590 | if (!lib_ring_buffer_open_read(buf)) |
| 591 | return buf; |
| 592 | } |
| 593 | return NULL; |
| 594 | } |
| 595 | |
| 596 | static |
| 597 | int lttng_buffer_has_read_closed_stream(struct channel *chan) |
| 598 | { |
| 599 | struct lib_ring_buffer *buf; |
| 600 | int cpu; |
| 601 | |
| 602 | for_each_channel_cpu(cpu, chan) { |
| 603 | buf = channel_get_ring_buffer(&client_config, chan, cpu); |
| 604 | if (!atomic_long_read(&buf->active_readers)) |
| 605 | return 1; |
| 606 | } |
| 607 | return 0; |
| 608 | } |
| 609 | |
| 610 | static |
| 611 | void lttng_buffer_read_close(struct lib_ring_buffer *buf) |
| 612 | { |
| 613 | lib_ring_buffer_release_read(buf); |
| 614 | } |
| 615 | |
| 616 | static |
| 617 | int lttng_event_reserve(struct lib_ring_buffer_ctx *ctx, |
| 618 | uint32_t event_id) |
| 619 | { |
| 620 | struct lttng_channel *lttng_chan = channel_get_private(ctx->chan); |
| 621 | struct lttng_probe_ctx *lttng_probe_ctx = ctx->priv; |
| 622 | struct lttng_event *event = lttng_probe_ctx->event; |
| 623 | struct lttng_client_ctx client_ctx; |
| 624 | int ret, cpu; |
| 625 | |
| 626 | cpu = lib_ring_buffer_get_cpu(&client_config); |
| 627 | if (unlikely(cpu < 0)) |
| 628 | return -EPERM; |
| 629 | ctx->cpu = cpu; |
| 630 | |
| 631 | /* Compute internal size of context structures. */ |
| 632 | ctx_get_struct_size(lttng_chan->ctx, &client_ctx.packet_context_len, lttng_chan, ctx); |
| 633 | ctx_get_struct_size(event->ctx, &client_ctx.event_context_len, lttng_chan, ctx); |
| 634 | |
| 635 | switch (lttng_chan->header_type) { |
| 636 | case 1: /* compact */ |
| 637 | if (event_id > 30) |
| 638 | ctx->rflags |= LTTNG_RFLAG_EXTENDED; |
| 639 | break; |
| 640 | case 2: /* large */ |
| 641 | if (event_id > 65534) |
| 642 | ctx->rflags |= LTTNG_RFLAG_EXTENDED; |
| 643 | break; |
| 644 | default: |
| 645 | WARN_ON_ONCE(1); |
| 646 | } |
| 647 | |
| 648 | ret = lib_ring_buffer_reserve(&client_config, ctx, &client_ctx); |
| 649 | if (unlikely(ret)) |
| 650 | goto put; |
| 651 | lib_ring_buffer_backend_get_pages(&client_config, ctx, |
| 652 | &ctx->backend_pages); |
| 653 | lttng_write_event_header(&client_config, ctx, event_id); |
| 654 | return 0; |
| 655 | put: |
| 656 | lib_ring_buffer_put_cpu(&client_config); |
| 657 | return ret; |
| 658 | } |
| 659 | |
| 660 | static |
| 661 | void lttng_event_commit(struct lib_ring_buffer_ctx *ctx) |
| 662 | { |
| 663 | lib_ring_buffer_commit(&client_config, ctx); |
| 664 | lib_ring_buffer_put_cpu(&client_config); |
| 665 | } |
| 666 | |
| 667 | static |
| 668 | void lttng_event_write(struct lib_ring_buffer_ctx *ctx, const void *src, |
| 669 | size_t len) |
| 670 | { |
| 671 | lib_ring_buffer_write(&client_config, ctx, src, len); |
| 672 | } |
| 673 | |
| 674 | static |
| 675 | void lttng_event_write_from_user(struct lib_ring_buffer_ctx *ctx, |
| 676 | const void __user *src, size_t len) |
| 677 | { |
| 678 | lib_ring_buffer_copy_from_user_inatomic(&client_config, ctx, src, len); |
| 679 | } |
| 680 | |
| 681 | static |
| 682 | void lttng_event_memset(struct lib_ring_buffer_ctx *ctx, |
| 683 | int c, size_t len) |
| 684 | { |
| 685 | lib_ring_buffer_memset(&client_config, ctx, c, len); |
| 686 | } |
| 687 | |
| 688 | static |
| 689 | void lttng_event_strcpy(struct lib_ring_buffer_ctx *ctx, const char *src, |
| 690 | size_t len) |
| 691 | { |
| 692 | lib_ring_buffer_strcpy(&client_config, ctx, src, len, '#'); |
| 693 | } |
| 694 | |
| 695 | static |
| 696 | void lttng_event_strcpy_from_user(struct lib_ring_buffer_ctx *ctx, |
| 697 | const char __user *src, size_t len) |
| 698 | { |
| 699 | lib_ring_buffer_strcpy_from_user_inatomic(&client_config, ctx, src, |
| 700 | len, '#'); |
| 701 | } |
| 702 | |
| 703 | static |
| 704 | wait_queue_head_t *lttng_get_writer_buf_wait_queue(struct channel *chan, int cpu) |
| 705 | { |
| 706 | struct lib_ring_buffer *buf = channel_get_ring_buffer(&client_config, |
| 707 | chan, cpu); |
| 708 | return &buf->write_wait; |
| 709 | } |
| 710 | |
| 711 | static |
| 712 | wait_queue_head_t *lttng_get_hp_wait_queue(struct channel *chan) |
| 713 | { |
| 714 | return &chan->hp_wait; |
| 715 | } |
| 716 | |
| 717 | static |
| 718 | int lttng_is_finalized(struct channel *chan) |
| 719 | { |
| 720 | return lib_ring_buffer_channel_is_finalized(chan); |
| 721 | } |
| 722 | |
| 723 | static |
| 724 | int lttng_is_disabled(struct channel *chan) |
| 725 | { |
| 726 | return lib_ring_buffer_channel_is_disabled(chan); |
| 727 | } |
| 728 | |
| 729 | static struct lttng_transport lttng_relay_transport = { |
| 730 | .name = "relay-" RING_BUFFER_MODE_TEMPLATE_STRING, |
| 731 | .owner = THIS_MODULE, |
| 732 | .ops = { |
| 733 | .channel_create = _channel_create, |
| 734 | .channel_destroy = lttng_channel_destroy, |
| 735 | .buffer_read_open = lttng_buffer_read_open, |
| 736 | .buffer_has_read_closed_stream = |
| 737 | lttng_buffer_has_read_closed_stream, |
| 738 | .buffer_read_close = lttng_buffer_read_close, |
| 739 | .event_reserve = lttng_event_reserve, |
| 740 | .event_commit = lttng_event_commit, |
| 741 | .event_write = lttng_event_write, |
| 742 | .event_write_from_user = lttng_event_write_from_user, |
| 743 | .event_memset = lttng_event_memset, |
| 744 | .event_strcpy = lttng_event_strcpy, |
| 745 | .event_strcpy_from_user = lttng_event_strcpy_from_user, |
| 746 | .packet_avail_size = NULL, /* Would be racy anyway */ |
| 747 | .get_writer_buf_wait_queue = lttng_get_writer_buf_wait_queue, |
| 748 | .get_hp_wait_queue = lttng_get_hp_wait_queue, |
| 749 | .is_finalized = lttng_is_finalized, |
| 750 | .is_disabled = lttng_is_disabled, |
| 751 | .timestamp_begin = client_timestamp_begin, |
| 752 | .timestamp_end = client_timestamp_end, |
| 753 | .events_discarded = client_events_discarded, |
| 754 | .content_size = client_content_size, |
| 755 | .packet_size = client_packet_size, |
| 756 | .stream_id = client_stream_id, |
| 757 | .current_timestamp = client_current_timestamp, |
| 758 | .sequence_number = client_sequence_number, |
| 759 | .instance_id = client_instance_id, |
| 760 | }, |
| 761 | }; |
| 762 | |
| 763 | static int __init lttng_ring_buffer_client_init(void) |
| 764 | { |
| 765 | /* |
| 766 | * This vmalloc sync all also takes care of the lib ring buffer |
| 767 | * vmalloc'd module pages when it is built as a module into LTTng. |
| 768 | */ |
| 769 | wrapper_vmalloc_sync_mappings(); |
| 770 | lttng_transport_register(<tng_relay_transport); |
| 771 | return 0; |
| 772 | } |
| 773 | |
| 774 | module_init(lttng_ring_buffer_client_init); |
| 775 | |
| 776 | static void __exit lttng_ring_buffer_client_exit(void) |
| 777 | { |
| 778 | lttng_transport_unregister(<tng_relay_transport); |
| 779 | } |
| 780 | |
| 781 | module_exit(lttng_ring_buffer_client_exit); |
| 782 | |
| 783 | MODULE_LICENSE("GPL and additional rights"); |
| 784 | MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>"); |
| 785 | MODULE_DESCRIPTION("LTTng ring buffer " RING_BUFFER_MODE_TEMPLATE_STRING |
| 786 | " client"); |
| 787 | MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION) "." |
| 788 | __stringify(LTTNG_MODULES_MINOR_VERSION) "." |
| 789 | __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION) |
| 790 | LTTNG_MODULES_EXTRAVERSION); |