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