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