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