Move channel context to private structures
[lttng-ust.git] / liblttng-ust / lttng-ring-buffer-client.h
1 /*
2 * SPDX-License-Identifier: LGPL-2.1-only
3 *
4 * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 *
6 * LTTng lib ring buffer client template.
7 */
8
9 #include <stddef.h>
10 #include <stdint.h>
11
12 #include <ust-events-internal.h>
13 #include <lttng/urcu/pointer.h>
14 #include "ust-bitfield.h"
15 #include "ust-compat.h"
16 #include "clock.h"
17 #include "context-internal.h"
18 #include "lttng-tracer.h"
19 #include "../libringbuffer/frontend_types.h"
20
21 #define LTTNG_COMPACT_EVENT_BITS 5
22 #define LTTNG_COMPACT_TSC_BITS 27
23
24 enum app_ctx_mode {
25 APP_CTX_DISABLED,
26 APP_CTX_ENABLED,
27 };
28
29 /*
30 * Keep the natural field alignment for _each field_ within this structure if
31 * you ever add/remove a field from this header. Packed attribute is not used
32 * because gcc generates poor code on at least powerpc and mips. Don't ever
33 * let gcc add padding between the structure elements.
34 */
35
36 struct packet_header {
37 /* Trace packet header */
38 uint32_t magic; /*
39 * Trace magic number.
40 * contains endianness information.
41 */
42 uint8_t uuid[LTTNG_UST_UUID_LEN];
43 uint32_t stream_id;
44 uint64_t stream_instance_id;
45
46 struct {
47 /* Stream packet context */
48 uint64_t timestamp_begin; /* Cycle count at subbuffer start */
49 uint64_t timestamp_end; /* Cycle count at subbuffer end */
50 uint64_t content_size; /* Size of data in subbuffer */
51 uint64_t packet_size; /* Subbuffer size (include padding) */
52 uint64_t packet_seq_num; /* Packet sequence number */
53 unsigned long events_discarded; /*
54 * Events lost in this subbuffer since
55 * the beginning of the trace.
56 * (may overflow)
57 */
58 uint32_t cpu_id; /* CPU id associated with stream */
59 uint8_t header_end; /* End of header */
60 } ctx;
61 };
62
63 struct lttng_client_ctx {
64 size_t packet_context_len;
65 size_t event_context_len;
66 struct lttng_ust_ctx *chan_ctx;
67 };
68
69 static inline uint64_t lib_ring_buffer_clock_read(struct lttng_ust_lib_ring_buffer_channel *chan)
70 {
71 return trace_clock_read64();
72 }
73
74 static inline
75 size_t ctx_get_aligned_size(size_t offset, struct lttng_ust_ctx *ctx,
76 size_t ctx_len)
77 {
78 size_t orig_offset = offset;
79
80 if (caa_likely(!ctx))
81 return 0;
82 offset += lib_ring_buffer_align(offset, ctx->largest_align);
83 offset += ctx_len;
84 return offset - orig_offset;
85 }
86
87 static inline
88 void ctx_get_struct_size(struct lttng_ust_ctx *ctx, size_t *ctx_len,
89 enum app_ctx_mode mode)
90 {
91 int i;
92 size_t offset = 0;
93
94 if (caa_likely(!ctx)) {
95 *ctx_len = 0;
96 return;
97 }
98 for (i = 0; i < ctx->nr_fields; i++) {
99 if (mode == APP_CTX_ENABLED) {
100 offset += ctx->fields[i]->get_size(ctx->fields[i], offset);
101 } else {
102 if (lttng_context_is_app(ctx->fields[i]->event_field->name)) {
103 /*
104 * Before UST 2.8, we cannot use the
105 * application context, because we
106 * cannot trust that the handler used
107 * for get_size is the same used for
108 * ctx_record, which would result in
109 * corrupted traces when tracing
110 * concurrently with application context
111 * register/unregister.
112 */
113 offset += lttng_ust_dummy_get_size(ctx->fields[i], offset);
114 } else {
115 offset += ctx->fields[i]->get_size(ctx->fields[i], offset);
116 }
117 }
118 }
119 *ctx_len = offset;
120 }
121
122 static inline
123 void ctx_record(struct lttng_ust_lib_ring_buffer_ctx *bufctx,
124 struct lttng_ust_channel_buffer *chan,
125 struct lttng_ust_ctx *ctx,
126 enum app_ctx_mode mode)
127 {
128 int i;
129
130 if (caa_likely(!ctx))
131 return;
132 lib_ring_buffer_align_ctx(bufctx, ctx->largest_align);
133 for (i = 0; i < ctx->nr_fields; i++) {
134 if (mode == APP_CTX_ENABLED) {
135 ctx->fields[i]->record(ctx->fields[i], bufctx, chan);
136 } else {
137 if (lttng_context_is_app(ctx->fields[i]->event_field->name)) {
138 /*
139 * Before UST 2.8, we cannot use the
140 * application context, because we
141 * cannot trust that the handler used
142 * for get_size is the same used for
143 * ctx_record, which would result in
144 * corrupted traces when tracing
145 * concurrently with application context
146 * register/unregister.
147 */
148 lttng_ust_dummy_record(ctx->fields[i], bufctx, chan);
149 } else {
150 ctx->fields[i]->record(ctx->fields[i], bufctx, chan);
151 }
152 }
153 }
154 }
155
156 /*
157 * record_header_size - Calculate the header size and padding necessary.
158 * @config: ring buffer instance configuration
159 * @chan: channel
160 * @offset: offset in the write buffer
161 * @pre_header_padding: padding to add before the header (output)
162 * @ctx: reservation context
163 *
164 * Returns the event header size (including padding).
165 *
166 * The payload must itself determine its own alignment from the biggest type it
167 * contains.
168 */
169 static __inline__
170 size_t record_header_size(const struct lttng_ust_lib_ring_buffer_config *config,
171 struct lttng_ust_lib_ring_buffer_channel *chan,
172 size_t offset,
173 size_t *pre_header_padding,
174 struct lttng_ust_lib_ring_buffer_ctx *ctx,
175 struct lttng_client_ctx *client_ctx)
176 {
177 struct lttng_ust_channel_buffer *lttng_chan = channel_get_private(chan);
178 struct lttng_ust_stack_ctx *lttng_ctx = ctx->priv;
179 size_t orig_offset = offset;
180 size_t padding;
181
182 switch (lttng_chan->priv->header_type) {
183 case 1: /* compact */
184 padding = lib_ring_buffer_align(offset, lttng_alignof(uint32_t));
185 offset += padding;
186 if (!(ctx->rflags & (RING_BUFFER_RFLAG_FULL_TSC | LTTNG_RFLAG_EXTENDED))) {
187 offset += sizeof(uint32_t); /* id and timestamp */
188 } else {
189 /* Minimum space taken by LTTNG_COMPACT_EVENT_BITS id */
190 offset += (LTTNG_COMPACT_EVENT_BITS + CHAR_BIT - 1) / CHAR_BIT;
191 /* Align extended struct on largest member */
192 offset += lib_ring_buffer_align(offset, lttng_alignof(uint64_t));
193 offset += sizeof(uint32_t); /* id */
194 offset += lib_ring_buffer_align(offset, lttng_alignof(uint64_t));
195 offset += sizeof(uint64_t); /* timestamp */
196 }
197 break;
198 case 2: /* large */
199 padding = lib_ring_buffer_align(offset, lttng_alignof(uint16_t));
200 offset += padding;
201 offset += sizeof(uint16_t);
202 if (!(ctx->rflags & (RING_BUFFER_RFLAG_FULL_TSC | LTTNG_RFLAG_EXTENDED))) {
203 offset += lib_ring_buffer_align(offset, lttng_alignof(uint32_t));
204 offset += sizeof(uint32_t); /* timestamp */
205 } else {
206 /* Align extended struct on largest member */
207 offset += lib_ring_buffer_align(offset, lttng_alignof(uint64_t));
208 offset += sizeof(uint32_t); /* id */
209 offset += lib_ring_buffer_align(offset, lttng_alignof(uint64_t));
210 offset += sizeof(uint64_t); /* timestamp */
211 }
212 break;
213 default:
214 padding = 0;
215 WARN_ON_ONCE(1);
216 }
217 offset += ctx_get_aligned_size(offset, client_ctx->chan_ctx,
218 client_ctx->packet_context_len);
219 offset += ctx_get_aligned_size(offset, lttng_ctx->event_ctx,
220 client_ctx->event_context_len);
221 *pre_header_padding = padding;
222 return offset - orig_offset;
223 }
224
225 #include "../libringbuffer/api.h"
226 #include "lttng-rb-clients.h"
227
228 static
229 void lttng_write_event_header_slow(const struct lttng_ust_lib_ring_buffer_config *config,
230 struct lttng_ust_lib_ring_buffer_ctx *ctx,
231 struct lttng_client_ctx *client_ctx,
232 uint32_t event_id);
233
234 /*
235 * lttng_write_event_header
236 *
237 * Writes the event header to the offset (already aligned on 32-bits).
238 *
239 * @config: ring buffer instance configuration
240 * @ctx: reservation context
241 * @event_id: event ID
242 */
243 static __inline__
244 void lttng_write_event_header(const struct lttng_ust_lib_ring_buffer_config *config,
245 struct lttng_ust_lib_ring_buffer_ctx *ctx,
246 struct lttng_client_ctx *client_ctx,
247 uint32_t event_id)
248 {
249 struct lttng_ust_channel_buffer *lttng_chan = channel_get_private(ctx->chan);
250 struct lttng_ust_stack_ctx *lttng_ctx = ctx->priv;
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 lib_ring_buffer_align_ctx(ctx, lttng_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, lttng_ctx->event_ctx, APP_CTX_ENABLED);
287 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 struct lttng_ust_stack_ctx *lttng_ctx = ctx->priv;
303
304 switch (lttng_chan->priv->header_type) {
305 case 1: /* compact */
306 if (!(ctx->rflags & (RING_BUFFER_RFLAG_FULL_TSC | LTTNG_RFLAG_EXTENDED))) {
307 uint32_t id_time = 0;
308
309 bt_bitfield_write(&id_time, uint32_t,
310 0,
311 LTTNG_COMPACT_EVENT_BITS,
312 event_id);
313 bt_bitfield_write(&id_time, uint32_t,
314 LTTNG_COMPACT_EVENT_BITS,
315 LTTNG_COMPACT_TSC_BITS,
316 ctx->tsc);
317 lib_ring_buffer_write(config, ctx, &id_time, sizeof(id_time));
318 } else {
319 uint8_t id = 0;
320 uint64_t timestamp = ctx->tsc;
321
322 bt_bitfield_write(&id, uint8_t,
323 0,
324 LTTNG_COMPACT_EVENT_BITS,
325 31);
326 lib_ring_buffer_write(config, ctx, &id, sizeof(id));
327 /* Align extended struct on largest member */
328 lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint64_t));
329 lib_ring_buffer_write(config, ctx, &event_id, sizeof(event_id));
330 lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint64_t));
331 lib_ring_buffer_write(config, ctx, &timestamp, sizeof(timestamp));
332 }
333 break;
334 case 2: /* large */
335 {
336 if (!(ctx->rflags & (RING_BUFFER_RFLAG_FULL_TSC | LTTNG_RFLAG_EXTENDED))) {
337 uint32_t timestamp = (uint32_t) ctx->tsc;
338 uint16_t id = event_id;
339
340 lib_ring_buffer_write(config, ctx, &id, sizeof(id));
341 lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint32_t));
342 lib_ring_buffer_write(config, ctx, &timestamp, sizeof(timestamp));
343 } else {
344 uint16_t id = 65535;
345 uint64_t timestamp = ctx->tsc;
346
347 lib_ring_buffer_write(config, ctx, &id, sizeof(id));
348 /* Align extended struct on largest member */
349 lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint64_t));
350 lib_ring_buffer_write(config, ctx, &event_id, sizeof(event_id));
351 lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint64_t));
352 lib_ring_buffer_write(config, ctx, &timestamp, sizeof(timestamp));
353 }
354 break;
355 }
356 default:
357 WARN_ON_ONCE(1);
358 }
359 ctx_record(ctx, lttng_chan, client_ctx->chan_ctx, APP_CTX_ENABLED);
360 ctx_record(ctx, lttng_chan, lttng_ctx->event_ctx, APP_CTX_ENABLED);
361 lib_ring_buffer_align_ctx(ctx, ctx->largest_align);
362 }
363
364 static const struct lttng_ust_lib_ring_buffer_config client_config;
365
366 static uint64_t client_ring_buffer_clock_read(struct lttng_ust_lib_ring_buffer_channel *chan)
367 {
368 return lib_ring_buffer_clock_read(chan);
369 }
370
371 static
372 size_t client_record_header_size(const struct lttng_ust_lib_ring_buffer_config *config,
373 struct lttng_ust_lib_ring_buffer_channel *chan,
374 size_t offset,
375 size_t *pre_header_padding,
376 struct lttng_ust_lib_ring_buffer_ctx *ctx,
377 void *client_ctx)
378 {
379 return record_header_size(config, chan, offset,
380 pre_header_padding, ctx, client_ctx);
381 }
382
383 /**
384 * client_packet_header_size - called on buffer-switch to a new sub-buffer
385 *
386 * Return header size without padding after the structure. Don't use packed
387 * structure because gcc generates inefficient code on some architectures
388 * (powerpc, mips..)
389 */
390 static size_t client_packet_header_size(void)
391 {
392 return offsetof(struct packet_header, ctx.header_end);
393 }
394
395 static void client_buffer_begin(struct lttng_ust_lib_ring_buffer *buf, uint64_t tsc,
396 unsigned int subbuf_idx,
397 struct lttng_ust_shm_handle *handle)
398 {
399 struct lttng_ust_lib_ring_buffer_channel *chan = shmp(handle, buf->backend.chan);
400 struct packet_header *header =
401 (struct packet_header *)
402 lib_ring_buffer_offset_address(&buf->backend,
403 subbuf_idx * chan->backend.subbuf_size,
404 handle);
405 struct lttng_ust_channel_buffer *lttng_chan = channel_get_private(chan);
406 uint64_t cnt = shmp_index(handle, buf->backend.buf_cnt, subbuf_idx)->seq_cnt;
407
408 assert(header);
409 if (!header)
410 return;
411 header->magic = CTF_MAGIC_NUMBER;
412 memcpy(header->uuid, lttng_chan->priv->uuid, sizeof(lttng_chan->priv->uuid));
413 header->stream_id = lttng_chan->priv->id;
414 header->stream_instance_id = buf->backend.cpu;
415 header->ctx.timestamp_begin = tsc;
416 header->ctx.timestamp_end = 0;
417 header->ctx.content_size = ~0ULL; /* for debugging */
418 header->ctx.packet_size = ~0ULL;
419 header->ctx.packet_seq_num = chan->backend.num_subbuf * cnt + subbuf_idx;
420 header->ctx.events_discarded = 0;
421 header->ctx.cpu_id = buf->backend.cpu;
422 }
423
424 /*
425 * offset is assumed to never be 0 here : never deliver a completely empty
426 * subbuffer. data_size is between 1 and subbuf_size.
427 */
428 static void client_buffer_end(struct lttng_ust_lib_ring_buffer *buf, uint64_t tsc,
429 unsigned int subbuf_idx, unsigned long data_size,
430 struct lttng_ust_shm_handle *handle)
431 {
432 struct lttng_ust_lib_ring_buffer_channel *chan = shmp(handle, buf->backend.chan);
433 struct packet_header *header =
434 (struct packet_header *)
435 lib_ring_buffer_offset_address(&buf->backend,
436 subbuf_idx * chan->backend.subbuf_size,
437 handle);
438 unsigned long records_lost = 0;
439
440 assert(header);
441 if (!header)
442 return;
443 header->ctx.timestamp_end = tsc;
444 header->ctx.content_size =
445 (uint64_t) data_size * CHAR_BIT; /* in bits */
446 header->ctx.packet_size =
447 (uint64_t) LTTNG_UST_PAGE_ALIGN(data_size) * CHAR_BIT; /* in bits */
448
449 records_lost += lib_ring_buffer_get_records_lost_full(&client_config, buf);
450 records_lost += lib_ring_buffer_get_records_lost_wrap(&client_config, buf);
451 records_lost += lib_ring_buffer_get_records_lost_big(&client_config, buf);
452 header->ctx.events_discarded = records_lost;
453 }
454
455 static int client_buffer_create(struct lttng_ust_lib_ring_buffer *buf, void *priv,
456 int cpu, const char *name, struct lttng_ust_shm_handle *handle)
457 {
458 return 0;
459 }
460
461 static void client_buffer_finalize(struct lttng_ust_lib_ring_buffer *buf, void *priv, int cpu, struct lttng_ust_shm_handle *handle)
462 {
463 }
464
465 static void client_content_size_field(const struct lttng_ust_lib_ring_buffer_config *config,
466 size_t *offset, size_t *length)
467 {
468 *offset = offsetof(struct packet_header, ctx.content_size);
469 *length = sizeof(((struct packet_header *) NULL)->ctx.content_size);
470 }
471
472 static void client_packet_size_field(const struct lttng_ust_lib_ring_buffer_config *config,
473 size_t *offset, size_t *length)
474 {
475 *offset = offsetof(struct packet_header, ctx.packet_size);
476 *length = sizeof(((struct packet_header *) NULL)->ctx.packet_size);
477 }
478
479 static struct packet_header *client_packet_header(struct lttng_ust_lib_ring_buffer *buf,
480 struct lttng_ust_shm_handle *handle)
481 {
482 return lib_ring_buffer_read_offset_address(&buf->backend, 0, handle);
483 }
484
485 static int client_timestamp_begin(struct lttng_ust_lib_ring_buffer *buf,
486 struct lttng_ust_shm_handle *handle,
487 uint64_t *timestamp_begin)
488 {
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_shm_handle *handle,
500 uint64_t *timestamp_end)
501 {
502 struct packet_header *header;
503
504 header = client_packet_header(buf, handle);
505 if (!header)
506 return -1;
507 *timestamp_end = header->ctx.timestamp_end;
508 return 0;
509 }
510
511 static int client_events_discarded(struct lttng_ust_lib_ring_buffer *buf,
512 struct lttng_ust_shm_handle *handle,
513 uint64_t *events_discarded)
514 {
515 struct packet_header *header;
516
517 header = client_packet_header(buf, handle);
518 if (!header)
519 return -1;
520 *events_discarded = header->ctx.events_discarded;
521 return 0;
522 }
523
524 static int client_content_size(struct lttng_ust_lib_ring_buffer *buf,
525 struct lttng_ust_shm_handle *handle,
526 uint64_t *content_size)
527 {
528 struct packet_header *header;
529
530 header = client_packet_header(buf, handle);
531 if (!header)
532 return -1;
533 *content_size = header->ctx.content_size;
534 return 0;
535 }
536
537 static int client_packet_size(struct lttng_ust_lib_ring_buffer *buf,
538 struct lttng_ust_shm_handle *handle,
539 uint64_t *packet_size)
540 {
541 struct packet_header *header;
542
543 header = client_packet_header(buf, handle);
544 if (!header)
545 return -1;
546 *packet_size = header->ctx.packet_size;
547 return 0;
548 }
549
550 static int client_stream_id(struct lttng_ust_lib_ring_buffer *buf,
551 struct lttng_ust_shm_handle *handle,
552 uint64_t *stream_id)
553 {
554 struct lttng_ust_lib_ring_buffer_channel *chan = shmp(handle,
555 buf->backend.chan);
556 struct lttng_ust_channel_buffer *lttng_chan = channel_get_private(chan);
557
558 *stream_id = lttng_chan->priv->id;
559
560 return 0;
561 }
562
563 static int client_current_timestamp(struct lttng_ust_lib_ring_buffer *buf,
564 struct lttng_ust_shm_handle *handle,
565 uint64_t *ts)
566 {
567 struct lttng_ust_lib_ring_buffer_channel *chan;
568
569 chan = shmp(handle, handle->chan);
570 *ts = client_ring_buffer_clock_read(chan);
571
572 return 0;
573 }
574
575 static int client_sequence_number(struct lttng_ust_lib_ring_buffer *buf,
576 struct lttng_ust_shm_handle *handle,
577 uint64_t *seq)
578 {
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_shm_handle *handle,
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->handle = handle;
681 lttng_chan_buf->chan = shmp(handle, handle->chan);
682 return lttng_chan_buf;
683
684 error:
685 lttng_ust_free_channel_common(lttng_chan_buf->parent);
686 return NULL;
687 }
688
689 static
690 void lttng_channel_destroy(struct lttng_ust_channel_buffer *lttng_chan_buf)
691 {
692 channel_destroy(lttng_chan_buf->chan, lttng_chan_buf->handle, 1);
693 lttng_ust_free_channel_common(lttng_chan_buf->parent);
694 }
695
696 static
697 int lttng_event_reserve(struct lttng_ust_lib_ring_buffer_ctx *ctx,
698 uint32_t event_id)
699 {
700 struct lttng_ust_channel_buffer *lttng_chan = channel_get_private(ctx->chan);
701 struct lttng_ust_stack_ctx *lttng_ctx = ctx->priv;
702 struct lttng_client_ctx client_ctx;
703 int ret, cpu;
704
705 client_ctx.chan_ctx = lttng_ust_rcu_dereference(lttng_chan->priv->ctx);
706 /* Compute internal size of context structures. */
707 ctx_get_struct_size(client_ctx.chan_ctx, &client_ctx.packet_context_len,
708 APP_CTX_ENABLED);
709 ctx_get_struct_size(lttng_ctx->event_ctx, &client_ctx.event_context_len,
710 APP_CTX_ENABLED);
711
712 cpu = lib_ring_buffer_get_cpu(&client_config);
713 if (cpu < 0)
714 return -EPERM;
715 ctx->cpu = cpu;
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_put_cpu(&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_put_cpu(&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 #if 0
767 static
768 wait_queue_head_t *lttng_get_reader_wait_queue(struct lttng_ust_lib_ring_buffer_channel *chan)
769 {
770 return &chan->read_wait;
771 }
772
773 static
774 wait_queue_head_t *lttng_get_hp_wait_queue(struct lttng_ust_lib_ring_buffer_channel *chan)
775 {
776 return &chan->hp_wait;
777 }
778 #endif //0
779
780 static
781 int lttng_is_finalized(struct lttng_ust_lib_ring_buffer_channel *chan)
782 {
783 return lib_ring_buffer_channel_is_finalized(chan);
784 }
785
786 static
787 int lttng_is_disabled(struct lttng_ust_lib_ring_buffer_channel *chan)
788 {
789 return lib_ring_buffer_channel_is_disabled(chan);
790 }
791
792 static
793 int lttng_flush_buffer(struct lttng_ust_lib_ring_buffer_channel *chan,
794 struct lttng_ust_shm_handle *handle)
795 {
796 struct lttng_ust_lib_ring_buffer *buf;
797 int cpu;
798
799 for_each_channel_cpu(cpu, chan) {
800 int shm_fd, wait_fd, wakeup_fd;
801 uint64_t memory_map_size;
802
803 buf = channel_get_ring_buffer(&client_config, chan,
804 cpu, handle, &shm_fd, &wait_fd,
805 &wakeup_fd, &memory_map_size);
806 lib_ring_buffer_switch(&client_config, buf,
807 SWITCH_ACTIVE, handle);
808 }
809 return 0;
810 }
811
812 static struct lttng_transport lttng_relay_transport = {
813 .name = "relay-" RING_BUFFER_MODE_TEMPLATE_STRING "-mmap",
814 .ops = {
815 .struct_size = sizeof(struct lttng_ust_channel_ops),
816 .priv = __LTTNG_COMPOUND_LITERAL(struct lttng_ust_channel_ops_private, {
817 .pub = &lttng_relay_transport.ops,
818 .channel_create = _channel_create,
819 .channel_destroy = lttng_channel_destroy,
820 .packet_avail_size = NULL, /* Would be racy anyway */
821 .is_finalized = lttng_is_finalized,
822 .is_disabled = lttng_is_disabled,
823 .flush_buffer = lttng_flush_buffer,
824 }),
825 .event_reserve = lttng_event_reserve,
826 .event_commit = lttng_event_commit,
827 .event_write = lttng_event_write,
828 .event_strcpy = lttng_event_strcpy,
829 },
830 .client_config = &client_config,
831 };
832
833 void RING_BUFFER_MODE_TEMPLATE_INIT(void)
834 {
835 DBG("LTT : ltt ring buffer client \"%s\" init\n",
836 "relay-" RING_BUFFER_MODE_TEMPLATE_STRING "-mmap");
837 lttng_transport_register(&lttng_relay_transport);
838 }
839
840 void RING_BUFFER_MODE_TEMPLATE_EXIT(void)
841 {
842 DBG("LTT : ltt ring buffer client \"%s\" exit\n",
843 "relay-" RING_BUFFER_MODE_TEMPLATE_STRING "-mmap");
844 lttng_transport_unregister(&lttng_relay_transport);
845 }
This page took 0.053454 seconds and 5 git commands to generate.