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