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