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