Rename "tsc" to "timestamp"
[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 "ust-bitfield.h"
13 #include "ust-compat.h"
14 #include "clock.h"
15 #include "lttng-tracer.h"
16 #include "../libringbuffer/frontend_types.h"
17
18 #define LTTNG_COMPACT_EVENT_BITS 5
19 #define LTTNG_COMPACT_TSC_BITS 27
20
21 enum app_ctx_mode {
22 APP_CTX_DISABLED,
23 APP_CTX_ENABLED,
24 };
25
26 /*
27 * Keep the natural field alignment for _each field_ within this structure if
28 * you ever add/remove a field from this header. Packed attribute is not used
29 * because gcc generates poor code on at least powerpc and mips. Don't ever
30 * let gcc add padding between the structure elements.
31 */
32
33 struct packet_header {
34 /* Trace packet header */
35 uint32_t magic; /*
36 * Trace magic number.
37 * contains endianness information.
38 */
39 uint8_t uuid[LTTNG_UST_UUID_LEN];
40 uint32_t stream_id;
41 uint64_t stream_instance_id;
42
43 struct {
44 /* Stream packet context */
45 uint64_t timestamp_begin; /* Cycle count at subbuffer start */
46 uint64_t timestamp_end; /* Cycle count at subbuffer end */
47 uint64_t content_size; /* Size of data in subbuffer */
48 uint64_t packet_size; /* Subbuffer size (include padding) */
49 uint64_t packet_seq_num; /* Packet sequence number */
50 unsigned long events_discarded; /*
51 * Events lost in this subbuffer since
52 * the beginning of the trace.
53 * (may overflow)
54 */
55 uint32_t cpu_id; /* CPU id associated with stream */
56 uint8_t header_end; /* End of header */
57 } ctx;
58 };
59
60 struct lttng_client_ctx {
61 size_t packet_context_len;
62 size_t event_context_len;
63 };
64
65 static inline uint64_t lib_ring_buffer_clock_read(struct channel *chan)
66 {
67 return trace_clock_read64();
68 }
69
70 static inline
71 size_t ctx_get_aligned_size(size_t offset, struct lttng_ctx *ctx,
72 size_t ctx_len)
73 {
74 size_t orig_offset = offset;
75
76 if (caa_likely(!ctx))
77 return 0;
78 offset += lib_ring_buffer_align(offset, ctx->largest_align);
79 offset += ctx_len;
80 return offset - orig_offset;
81 }
82
83 static inline
84 void ctx_get_struct_size(struct lttng_ctx *ctx, size_t *ctx_len,
85 enum app_ctx_mode mode)
86 {
87 int i;
88 size_t offset = 0;
89
90 if (caa_likely(!ctx)) {
91 *ctx_len = 0;
92 return;
93 }
94 for (i = 0; i < ctx->nr_fields; i++) {
95 if (mode == APP_CTX_ENABLED) {
96 offset += ctx->fields[i].get_size(&ctx->fields[i], offset);
97 } else {
98 if (lttng_context_is_app(ctx->fields[i].event_field.name)) {
99 /*
100 * Before UST 2.8, we cannot use the
101 * application context, because we
102 * cannot trust that the handler used
103 * for get_size is the same used for
104 * ctx_record, which would result in
105 * corrupted traces when tracing
106 * concurrently with application context
107 * register/unregister.
108 */
109 offset += lttng_ust_dummy_get_size(&ctx->fields[i], offset);
110 } else {
111 offset += ctx->fields[i].get_size(&ctx->fields[i], offset);
112 }
113 }
114 }
115 *ctx_len = offset;
116 }
117
118 static inline
119 void ctx_record(struct lttng_ust_lib_ring_buffer_ctx *bufctx,
120 struct lttng_channel *chan,
121 struct lttng_ctx *ctx,
122 enum app_ctx_mode mode)
123 {
124 int i;
125
126 if (caa_likely(!ctx))
127 return;
128 lib_ring_buffer_align_ctx(bufctx, ctx->largest_align);
129 for (i = 0; i < ctx->nr_fields; i++) {
130 if (mode == APP_CTX_ENABLED) {
131 ctx->fields[i].record(&ctx->fields[i], bufctx, chan);
132 } else {
133 if (lttng_context_is_app(ctx->fields[i].event_field.name)) {
134 /*
135 * Before UST 2.8, we cannot use the
136 * application context, because we
137 * cannot trust that the handler used
138 * for get_size is the same used for
139 * ctx_record, which would result in
140 * corrupted traces when tracing
141 * concurrently with application context
142 * register/unregister.
143 */
144 lttng_ust_dummy_record(&ctx->fields[i], bufctx, chan);
145 } else {
146 ctx->fields[i].record(&ctx->fields[i], bufctx, chan);
147 }
148 }
149 }
150 }
151
152 /*
153 * record_header_size - Calculate the header size and padding necessary.
154 * @config: ring buffer instance configuration
155 * @chan: channel
156 * @offset: offset in the write buffer
157 * @pre_header_padding: padding to add before the header (output)
158 * @ctx: reservation context
159 *
160 * Returns the event header size (including padding).
161 *
162 * The payload must itself determine its own alignment from the biggest type it
163 * contains.
164 */
165 static __inline__
166 size_t record_header_size(const struct lttng_ust_lib_ring_buffer_config *config,
167 struct channel *chan, size_t offset,
168 size_t *pre_header_padding,
169 struct lttng_ust_lib_ring_buffer_ctx *ctx,
170 struct lttng_client_ctx *client_ctx)
171 {
172 struct lttng_channel *lttng_chan = channel_get_private(chan);
173 struct lttng_event *event = ctx->priv;
174 struct lttng_stack_ctx *lttng_ctx = ctx->priv2;
175 size_t orig_offset = offset;
176 size_t padding;
177
178 switch (lttng_chan->header_type) {
179 case 1: /* compact */
180 padding = lib_ring_buffer_align(offset, lttng_alignof(uint32_t));
181 offset += padding;
182 if (!(ctx->rflags & (RING_BUFFER_RFLAG_FULL_TSC | LTTNG_RFLAG_EXTENDED))) {
183 offset += sizeof(uint32_t); /* id and timestamp */
184 } else {
185 /* Minimum space taken by LTTNG_COMPACT_EVENT_BITS id */
186 offset += (LTTNG_COMPACT_EVENT_BITS + CHAR_BIT - 1) / CHAR_BIT;
187 /* Align extended struct on largest member */
188 offset += lib_ring_buffer_align(offset, lttng_alignof(uint64_t));
189 offset += sizeof(uint32_t); /* id */
190 offset += lib_ring_buffer_align(offset, lttng_alignof(uint64_t));
191 offset += sizeof(uint64_t); /* timestamp */
192 }
193 break;
194 case 2: /* large */
195 padding = lib_ring_buffer_align(offset, lttng_alignof(uint16_t));
196 offset += padding;
197 offset += sizeof(uint16_t);
198 if (!(ctx->rflags & (RING_BUFFER_RFLAG_FULL_TSC | LTTNG_RFLAG_EXTENDED))) {
199 offset += lib_ring_buffer_align(offset, lttng_alignof(uint32_t));
200 offset += sizeof(uint32_t); /* timestamp */
201 } else {
202 /* Align extended struct on largest member */
203 offset += lib_ring_buffer_align(offset, lttng_alignof(uint64_t));
204 offset += sizeof(uint32_t); /* id */
205 offset += lib_ring_buffer_align(offset, lttng_alignof(uint64_t));
206 offset += sizeof(uint64_t); /* timestamp */
207 }
208 break;
209 default:
210 padding = 0;
211 WARN_ON_ONCE(1);
212 }
213 if (lttng_ctx) {
214 /* 2.8+ probe ABI. */
215 offset += ctx_get_aligned_size(offset, lttng_ctx->chan_ctx,
216 client_ctx->packet_context_len);
217 offset += ctx_get_aligned_size(offset, lttng_ctx->event_ctx,
218 client_ctx->event_context_len);
219 } else {
220 /* Pre 2.8 probe ABI. */
221 offset += ctx_get_aligned_size(offset, lttng_chan->ctx,
222 client_ctx->packet_context_len);
223 offset += ctx_get_aligned_size(offset, event->ctx,
224 client_ctx->event_context_len);
225 }
226 *pre_header_padding = padding;
227 return offset - orig_offset;
228 }
229
230 #include "../libringbuffer/api.h"
231 #include "lttng-rb-clients.h"
232
233 static
234 void lttng_write_event_header_slow(const struct lttng_ust_lib_ring_buffer_config *config,
235 struct lttng_ust_lib_ring_buffer_ctx *ctx,
236 uint32_t event_id);
237
238 /*
239 * lttng_write_event_header
240 *
241 * Writes the event header to the offset (already aligned on 32-bits).
242 *
243 * @config: ring buffer instance configuration
244 * @ctx: reservation context
245 * @event_id: event ID
246 */
247 static __inline__
248 void lttng_write_event_header(const struct lttng_ust_lib_ring_buffer_config *config,
249 struct lttng_ust_lib_ring_buffer_ctx *ctx,
250 uint32_t event_id)
251 {
252 struct lttng_channel *lttng_chan = channel_get_private(ctx->chan);
253 struct lttng_event *event = ctx->priv;
254 struct lttng_stack_ctx *lttng_ctx = ctx->priv2;
255
256 if (caa_unlikely(ctx->rflags))
257 goto slow_path;
258
259 switch (lttng_chan->header_type) {
260 case 1: /* compact */
261 {
262 uint32_t id_time = 0;
263
264 bt_bitfield_write(&id_time, uint32_t,
265 0,
266 LTTNG_COMPACT_EVENT_BITS,
267 event_id);
268 bt_bitfield_write(&id_time, uint32_t,
269 LTTNG_COMPACT_EVENT_BITS,
270 LTTNG_COMPACT_TSC_BITS,
271 ctx->tsc);
272 lib_ring_buffer_write(config, ctx, &id_time, sizeof(id_time));
273 break;
274 }
275 case 2: /* large */
276 {
277 uint32_t timestamp = (uint32_t) ctx->tsc;
278 uint16_t id = event_id;
279
280 lib_ring_buffer_write(config, ctx, &id, sizeof(id));
281 lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint32_t));
282 lib_ring_buffer_write(config, ctx, &timestamp, sizeof(timestamp));
283 break;
284 }
285 default:
286 WARN_ON_ONCE(1);
287 }
288
289 if (lttng_ctx) {
290 /* 2.8+ probe ABI. */
291 ctx_record(ctx, lttng_chan, lttng_ctx->chan_ctx, APP_CTX_ENABLED);
292 ctx_record(ctx, lttng_chan, lttng_ctx->event_ctx, APP_CTX_ENABLED);
293 } else {
294 /* Pre 2.8 probe ABI. */
295 ctx_record(ctx, lttng_chan, lttng_chan->ctx, APP_CTX_DISABLED);
296 ctx_record(ctx, lttng_chan, event->ctx, APP_CTX_DISABLED);
297 }
298 lib_ring_buffer_align_ctx(ctx, ctx->largest_align);
299
300 return;
301
302 slow_path:
303 lttng_write_event_header_slow(config, ctx, event_id);
304 }
305
306 static
307 void lttng_write_event_header_slow(const struct lttng_ust_lib_ring_buffer_config *config,
308 struct lttng_ust_lib_ring_buffer_ctx *ctx,
309 uint32_t event_id)
310 {
311 struct lttng_channel *lttng_chan = channel_get_private(ctx->chan);
312 struct lttng_event *event = ctx->priv;
313 struct lttng_stack_ctx *lttng_ctx = ctx->priv2;
314
315 switch (lttng_chan->header_type) {
316 case 1: /* compact */
317 if (!(ctx->rflags & (RING_BUFFER_RFLAG_FULL_TSC | LTTNG_RFLAG_EXTENDED))) {
318 uint32_t id_time = 0;
319
320 bt_bitfield_write(&id_time, uint32_t,
321 0,
322 LTTNG_COMPACT_EVENT_BITS,
323 event_id);
324 bt_bitfield_write(&id_time, uint32_t,
325 LTTNG_COMPACT_EVENT_BITS,
326 LTTNG_COMPACT_TSC_BITS,
327 ctx->tsc);
328 lib_ring_buffer_write(config, ctx, &id_time, sizeof(id_time));
329 } else {
330 uint8_t id = 0;
331 uint64_t timestamp = ctx->tsc;
332
333 bt_bitfield_write(&id, uint8_t,
334 0,
335 LTTNG_COMPACT_EVENT_BITS,
336 31);
337 lib_ring_buffer_write(config, ctx, &id, sizeof(id));
338 /* Align extended struct on largest member */
339 lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint64_t));
340 lib_ring_buffer_write(config, ctx, &event_id, sizeof(event_id));
341 lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint64_t));
342 lib_ring_buffer_write(config, ctx, &timestamp, sizeof(timestamp));
343 }
344 break;
345 case 2: /* large */
346 {
347 if (!(ctx->rflags & (RING_BUFFER_RFLAG_FULL_TSC | LTTNG_RFLAG_EXTENDED))) {
348 uint32_t timestamp = (uint32_t) ctx->tsc;
349 uint16_t id = event_id;
350
351 lib_ring_buffer_write(config, ctx, &id, sizeof(id));
352 lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint32_t));
353 lib_ring_buffer_write(config, ctx, &timestamp, sizeof(timestamp));
354 } else {
355 uint16_t id = 65535;
356 uint64_t timestamp = ctx->tsc;
357
358 lib_ring_buffer_write(config, ctx, &id, sizeof(id));
359 /* Align extended struct on largest member */
360 lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint64_t));
361 lib_ring_buffer_write(config, ctx, &event_id, sizeof(event_id));
362 lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint64_t));
363 lib_ring_buffer_write(config, ctx, &timestamp, sizeof(timestamp));
364 }
365 break;
366 }
367 default:
368 WARN_ON_ONCE(1);
369 }
370 if (lttng_ctx) {
371 /* 2.8+ probe ABI. */
372 ctx_record(ctx, lttng_chan, lttng_ctx->chan_ctx, APP_CTX_ENABLED);
373 ctx_record(ctx, lttng_chan, lttng_ctx->event_ctx, APP_CTX_ENABLED);
374 } else {
375 /* Pre 2.8 probe ABI. */
376 ctx_record(ctx, lttng_chan, lttng_chan->ctx, APP_CTX_DISABLED);
377 ctx_record(ctx, lttng_chan, event->ctx, APP_CTX_DISABLED);
378 }
379 lib_ring_buffer_align_ctx(ctx, ctx->largest_align);
380 }
381
382 static const struct lttng_ust_lib_ring_buffer_config client_config;
383
384 static uint64_t client_ring_buffer_clock_read(struct channel *chan)
385 {
386 return lib_ring_buffer_clock_read(chan);
387 }
388
389 static
390 size_t client_record_header_size(const struct lttng_ust_lib_ring_buffer_config *config,
391 struct channel *chan, size_t offset,
392 size_t *pre_header_padding,
393 struct lttng_ust_lib_ring_buffer_ctx *ctx,
394 void *client_ctx)
395 {
396 return record_header_size(config, chan, offset,
397 pre_header_padding, ctx, client_ctx);
398 }
399
400 /**
401 * client_packet_header_size - called on buffer-switch to a new sub-buffer
402 *
403 * Return header size without padding after the structure. Don't use packed
404 * structure because gcc generates inefficient code on some architectures
405 * (powerpc, mips..)
406 */
407 static size_t client_packet_header_size(void)
408 {
409 return offsetof(struct packet_header, ctx.header_end);
410 }
411
412 static void client_buffer_begin(struct lttng_ust_lib_ring_buffer *buf, uint64_t tsc,
413 unsigned int subbuf_idx,
414 struct lttng_ust_shm_handle *handle)
415 {
416 struct channel *chan = shmp(handle, buf->backend.chan);
417 struct packet_header *header =
418 (struct packet_header *)
419 lib_ring_buffer_offset_address(&buf->backend,
420 subbuf_idx * chan->backend.subbuf_size,
421 handle);
422 struct lttng_channel *lttng_chan = channel_get_private(chan);
423 uint64_t cnt = shmp_index(handle, buf->backend.buf_cnt, subbuf_idx)->seq_cnt;
424
425 assert(header);
426 if (!header)
427 return;
428 header->magic = CTF_MAGIC_NUMBER;
429 memcpy(header->uuid, lttng_chan->uuid, sizeof(lttng_chan->uuid));
430 header->stream_id = lttng_chan->id;
431 header->stream_instance_id = buf->backend.cpu;
432 header->ctx.timestamp_begin = tsc;
433 header->ctx.timestamp_end = 0;
434 header->ctx.content_size = ~0ULL; /* for debugging */
435 header->ctx.packet_size = ~0ULL;
436 header->ctx.packet_seq_num = chan->backend.num_subbuf * cnt + subbuf_idx;
437 header->ctx.events_discarded = 0;
438 header->ctx.cpu_id = buf->backend.cpu;
439 }
440
441 /*
442 * offset is assumed to never be 0 here : never deliver a completely empty
443 * subbuffer. data_size is between 1 and subbuf_size.
444 */
445 static void client_buffer_end(struct lttng_ust_lib_ring_buffer *buf, uint64_t tsc,
446 unsigned int subbuf_idx, unsigned long data_size,
447 struct lttng_ust_shm_handle *handle)
448 {
449 struct channel *chan = shmp(handle, buf->backend.chan);
450 struct packet_header *header =
451 (struct packet_header *)
452 lib_ring_buffer_offset_address(&buf->backend,
453 subbuf_idx * chan->backend.subbuf_size,
454 handle);
455 unsigned long records_lost = 0;
456
457 assert(header);
458 if (!header)
459 return;
460 header->ctx.timestamp_end = tsc;
461 header->ctx.content_size =
462 (uint64_t) data_size * CHAR_BIT; /* in bits */
463 header->ctx.packet_size =
464 (uint64_t) LTTNG_UST_PAGE_ALIGN(data_size) * CHAR_BIT; /* in bits */
465
466 records_lost += lib_ring_buffer_get_records_lost_full(&client_config, buf);
467 records_lost += lib_ring_buffer_get_records_lost_wrap(&client_config, buf);
468 records_lost += lib_ring_buffer_get_records_lost_big(&client_config, buf);
469 header->ctx.events_discarded = records_lost;
470 }
471
472 static int client_buffer_create(struct lttng_ust_lib_ring_buffer *buf, void *priv,
473 int cpu, const char *name, struct lttng_ust_shm_handle *handle)
474 {
475 return 0;
476 }
477
478 static void client_buffer_finalize(struct lttng_ust_lib_ring_buffer *buf, void *priv, int cpu, struct lttng_ust_shm_handle *handle)
479 {
480 }
481
482 static void client_content_size_field(const struct lttng_ust_lib_ring_buffer_config *config,
483 size_t *offset, size_t *length)
484 {
485 *offset = offsetof(struct packet_header, ctx.content_size);
486 *length = sizeof(((struct packet_header *) NULL)->ctx.content_size);
487 }
488
489 static void client_packet_size_field(const struct lttng_ust_lib_ring_buffer_config *config,
490 size_t *offset, size_t *length)
491 {
492 *offset = offsetof(struct packet_header, ctx.packet_size);
493 *length = sizeof(((struct packet_header *) NULL)->ctx.packet_size);
494 }
495
496 static struct packet_header *client_packet_header(struct lttng_ust_lib_ring_buffer *buf,
497 struct lttng_ust_shm_handle *handle)
498 {
499 return lib_ring_buffer_read_offset_address(&buf->backend, 0, handle);
500 }
501
502 static int client_timestamp_begin(struct lttng_ust_lib_ring_buffer *buf,
503 struct lttng_ust_shm_handle *handle,
504 uint64_t *timestamp_begin)
505 {
506 struct packet_header *header;
507
508 header = client_packet_header(buf, handle);
509 if (!header)
510 return -1;
511 *timestamp_begin = header->ctx.timestamp_begin;
512 return 0;
513 }
514
515 static int client_timestamp_end(struct lttng_ust_lib_ring_buffer *buf,
516 struct lttng_ust_shm_handle *handle,
517 uint64_t *timestamp_end)
518 {
519 struct packet_header *header;
520
521 header = client_packet_header(buf, handle);
522 if (!header)
523 return -1;
524 *timestamp_end = header->ctx.timestamp_end;
525 return 0;
526 }
527
528 static int client_events_discarded(struct lttng_ust_lib_ring_buffer *buf,
529 struct lttng_ust_shm_handle *handle,
530 uint64_t *events_discarded)
531 {
532 struct packet_header *header;
533
534 header = client_packet_header(buf, handle);
535 if (!header)
536 return -1;
537 *events_discarded = header->ctx.events_discarded;
538 return 0;
539 }
540
541 static int client_content_size(struct lttng_ust_lib_ring_buffer *buf,
542 struct lttng_ust_shm_handle *handle,
543 uint64_t *content_size)
544 {
545 struct packet_header *header;
546
547 header = client_packet_header(buf, handle);
548 if (!header)
549 return -1;
550 *content_size = header->ctx.content_size;
551 return 0;
552 }
553
554 static int client_packet_size(struct lttng_ust_lib_ring_buffer *buf,
555 struct lttng_ust_shm_handle *handle,
556 uint64_t *packet_size)
557 {
558 struct packet_header *header;
559
560 header = client_packet_header(buf, handle);
561 if (!header)
562 return -1;
563 *packet_size = header->ctx.packet_size;
564 return 0;
565 }
566
567 static int client_stream_id(struct lttng_ust_lib_ring_buffer *buf,
568 struct lttng_ust_shm_handle *handle,
569 uint64_t *stream_id)
570 {
571 struct channel *chan = shmp(handle, buf->backend.chan);
572 struct lttng_channel *lttng_chan = channel_get_private(chan);
573
574 *stream_id = lttng_chan->id;
575
576 return 0;
577 }
578
579 static int client_current_timestamp(struct lttng_ust_lib_ring_buffer *buf,
580 struct lttng_ust_shm_handle *handle,
581 uint64_t *ts)
582 {
583 struct channel *chan;
584
585 chan = shmp(handle, handle->chan);
586 *ts = client_ring_buffer_clock_read(chan);
587
588 return 0;
589 }
590
591 static int client_sequence_number(struct lttng_ust_lib_ring_buffer *buf,
592 struct lttng_ust_shm_handle *handle,
593 uint64_t *seq)
594 {
595 struct packet_header *header;
596
597 header = client_packet_header(buf, handle);
598 if (!header)
599 return -1;
600 *seq = header->ctx.packet_seq_num;
601 return 0;
602 }
603
604 static int client_instance_id(struct lttng_ust_lib_ring_buffer *buf,
605 struct lttng_ust_shm_handle *handle,
606 uint64_t *id)
607 {
608 *id = buf->backend.cpu;
609
610 return 0;
611 }
612
613 static const
614 struct lttng_ust_client_lib_ring_buffer_client_cb client_cb = {
615 .parent = {
616 .ring_buffer_clock_read = client_ring_buffer_clock_read,
617 .record_header_size = client_record_header_size,
618 .subbuffer_header_size = client_packet_header_size,
619 .buffer_begin = client_buffer_begin,
620 .buffer_end = client_buffer_end,
621 .buffer_create = client_buffer_create,
622 .buffer_finalize = client_buffer_finalize,
623 .content_size_field = client_content_size_field,
624 .packet_size_field = client_packet_size_field,
625 },
626 .timestamp_begin = client_timestamp_begin,
627 .timestamp_end = client_timestamp_end,
628 .events_discarded = client_events_discarded,
629 .content_size = client_content_size,
630 .packet_size = client_packet_size,
631 .stream_id = client_stream_id,
632 .current_timestamp = client_current_timestamp,
633 .sequence_number = client_sequence_number,
634 .instance_id = client_instance_id,
635 };
636
637 static const struct lttng_ust_lib_ring_buffer_config client_config = {
638 .cb.ring_buffer_clock_read = client_ring_buffer_clock_read,
639 .cb.record_header_size = client_record_header_size,
640 .cb.subbuffer_header_size = client_packet_header_size,
641 .cb.buffer_begin = client_buffer_begin,
642 .cb.buffer_end = client_buffer_end,
643 .cb.buffer_create = client_buffer_create,
644 .cb.buffer_finalize = client_buffer_finalize,
645 .cb.content_size_field = client_content_size_field,
646 .cb.packet_size_field = client_packet_size_field,
647
648 .tsc_bits = LTTNG_COMPACT_TSC_BITS,
649 .alloc = RING_BUFFER_ALLOC_PER_CPU,
650 .sync = RING_BUFFER_SYNC_GLOBAL,
651 .mode = RING_BUFFER_MODE_TEMPLATE,
652 .backend = RING_BUFFER_PAGE,
653 .output = RING_BUFFER_MMAP,
654 .oops = RING_BUFFER_OOPS_CONSISTENCY,
655 .ipi = RING_BUFFER_NO_IPI_BARRIER,
656 .wakeup = LTTNG_CLIENT_WAKEUP,
657 .client_type = LTTNG_CLIENT_TYPE,
658
659 .cb_ptr = &client_cb.parent,
660 };
661
662 const struct lttng_ust_client_lib_ring_buffer_client_cb *LTTNG_CLIENT_CALLBACKS = &client_cb;
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.045649 seconds and 4 git commands to generate.