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