Refactoring: combine event recorder and notifier callback functions
[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
23c8854a 67static inline uint64_t lib_ring_buffer_clock_read(struct channel *chan)
3d1fc7fd
MD
68{
69 return trace_clock_read64();
70}
71
72static inline
e56bb47c
MD
73size_t ctx_get_aligned_size(size_t offset, struct lttng_ctx *ctx,
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
86void ctx_get_struct_size(struct lttng_ctx *ctx, size_t *ctx_len,
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) {
98 offset += ctx->fields[i].get_size(&ctx->fields[i], offset);
99 } else {
100 if (lttng_context_is_app(ctx->fields[i].event_field.name)) {
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 */
111 offset += lttng_ust_dummy_get_size(&ctx->fields[i], offset);
112 } else {
113 offset += ctx->fields[i].get_size(&ctx->fields[i], offset);
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,
ce7352a2
MD
123 struct lttng_ctx *ctx,
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) {
133 ctx->fields[i].record(&ctx->fields[i], bufctx, chan);
134 } else {
135 if (lttng_context_is_app(ctx->fields[i].event_field.name)) {
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 */
146 lttng_ust_dummy_record(&ctx->fields[i], bufctx, chan);
147 } else {
148 ctx->fields[i].record(&ctx->fields[i], bufctx, chan);
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,
3d1fc7fd
MD
169 struct channel *chan, size_t offset,
170 size_t *pre_header_padding,
e56bb47c
MD
171 struct lttng_ust_lib_ring_buffer_ctx *ctx,
172 struct lttng_client_ctx *client_ctx)
3d1fc7fd 173{
7dd08bec 174 struct lttng_channel *lttng_chan = channel_get_private(chan);
2b7080aa 175 struct lttng_stack_ctx *lttng_ctx = ctx->priv;
3d1fc7fd
MD
176 size_t orig_offset = offset;
177 size_t padding;
178
7dd08bec 179 switch (lttng_chan->header_type) {
3d1fc7fd 180 case 1: /* compact */
1dbfff0c 181 padding = lib_ring_buffer_align(offset, lttng_alignof(uint32_t));
3d1fc7fd 182 offset += padding;
7dd08bec 183 if (!(ctx->rflags & (RING_BUFFER_RFLAG_FULL_TSC | LTTNG_RFLAG_EXTENDED))) {
3d1fc7fd
MD
184 offset += sizeof(uint32_t); /* id and timestamp */
185 } else {
79dfbf42
MD
186 /* Minimum space taken by LTTNG_COMPACT_EVENT_BITS id */
187 offset += (LTTNG_COMPACT_EVENT_BITS + CHAR_BIT - 1) / CHAR_BIT;
3d1fc7fd 188 /* Align extended struct on largest member */
1dbfff0c 189 offset += lib_ring_buffer_align(offset, lttng_alignof(uint64_t));
3d1fc7fd 190 offset += sizeof(uint32_t); /* id */
1dbfff0c 191 offset += lib_ring_buffer_align(offset, lttng_alignof(uint64_t));
3d1fc7fd
MD
192 offset += sizeof(uint64_t); /* timestamp */
193 }
194 break;
195 case 2: /* large */
1dbfff0c 196 padding = lib_ring_buffer_align(offset, lttng_alignof(uint16_t));
3d1fc7fd
MD
197 offset += padding;
198 offset += sizeof(uint16_t);
7dd08bec 199 if (!(ctx->rflags & (RING_BUFFER_RFLAG_FULL_TSC | LTTNG_RFLAG_EXTENDED))) {
1dbfff0c 200 offset += lib_ring_buffer_align(offset, lttng_alignof(uint32_t));
3d1fc7fd
MD
201 offset += sizeof(uint32_t); /* timestamp */
202 } else {
203 /* Align extended struct on largest member */
1dbfff0c 204 offset += lib_ring_buffer_align(offset, lttng_alignof(uint64_t));
3d1fc7fd 205 offset += sizeof(uint32_t); /* id */
1dbfff0c 206 offset += lib_ring_buffer_align(offset, lttng_alignof(uint64_t));
3d1fc7fd
MD
207 offset += sizeof(uint64_t); /* timestamp */
208 }
209 break;
210 default:
9f3fdbc6 211 padding = 0;
3d1fc7fd
MD
212 WARN_ON_ONCE(1);
213 }
2b7080aa
MD
214 offset += ctx_get_aligned_size(offset, lttng_ctx->chan_ctx,
215 client_ctx->packet_context_len);
216 offset += ctx_get_aligned_size(offset, lttng_ctx->event_ctx,
217 client_ctx->event_context_len);
3d1fc7fd
MD
218 *pre_header_padding = padding;
219 return offset - orig_offset;
220}
221
9f3fdbc6 222#include "../libringbuffer/api.h"
82b9bde8 223#include "lttng-rb-clients.h"
3d1fc7fd 224
9f3fdbc6 225static
7dd08bec 226void lttng_write_event_header_slow(const struct lttng_ust_lib_ring_buffer_config *config,
4cfec15c 227 struct lttng_ust_lib_ring_buffer_ctx *ctx,
3d1fc7fd
MD
228 uint32_t event_id);
229
230/*
7dd08bec 231 * lttng_write_event_header
3d1fc7fd
MD
232 *
233 * Writes the event header to the offset (already aligned on 32-bits).
234 *
235 * @config: ring buffer instance configuration
236 * @ctx: reservation context
237 * @event_id: event ID
238 */
239static __inline__
7dd08bec 240void lttng_write_event_header(const struct lttng_ust_lib_ring_buffer_config *config,
4cfec15c 241 struct lttng_ust_lib_ring_buffer_ctx *ctx,
3d1fc7fd
MD
242 uint32_t event_id)
243{
7dd08bec 244 struct lttng_channel *lttng_chan = channel_get_private(ctx->chan);
2b7080aa 245 struct lttng_stack_ctx *lttng_ctx = ctx->priv;
3d1fc7fd 246
b5a3dfa5 247 if (caa_unlikely(ctx->rflags))
3d1fc7fd
MD
248 goto slow_path;
249
7dd08bec 250 switch (lttng_chan->header_type) {
3d1fc7fd
MD
251 case 1: /* compact */
252 {
253 uint32_t id_time = 0;
254
79dfbf42
MD
255 bt_bitfield_write(&id_time, uint32_t,
256 0,
257 LTTNG_COMPACT_EVENT_BITS,
258 event_id);
259 bt_bitfield_write(&id_time, uint32_t,
260 LTTNG_COMPACT_EVENT_BITS,
261 LTTNG_COMPACT_TSC_BITS,
262 ctx->tsc);
3d1fc7fd
MD
263 lib_ring_buffer_write(config, ctx, &id_time, sizeof(id_time));
264 break;
265 }
266 case 2: /* large */
267 {
268 uint32_t timestamp = (uint32_t) ctx->tsc;
269 uint16_t id = event_id;
270
271 lib_ring_buffer_write(config, ctx, &id, sizeof(id));
1dbfff0c 272 lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint32_t));
3d1fc7fd
MD
273 lib_ring_buffer_write(config, ctx, &timestamp, sizeof(timestamp));
274 break;
275 }
276 default:
277 WARN_ON_ONCE(1);
278 }
279
2b7080aa
MD
280 ctx_record(ctx, lttng_chan, lttng_ctx->chan_ctx, APP_CTX_ENABLED);
281 ctx_record(ctx, lttng_chan, lttng_ctx->event_ctx, APP_CTX_ENABLED);
d3b2c3e0 282 lib_ring_buffer_align_ctx(ctx, ctx->largest_align);
3d1fc7fd
MD
283
284 return;
285
286slow_path:
7dd08bec 287 lttng_write_event_header_slow(config, ctx, event_id);
3d1fc7fd
MD
288}
289
9f3fdbc6 290static
7dd08bec 291void lttng_write_event_header_slow(const struct lttng_ust_lib_ring_buffer_config *config,
4cfec15c 292 struct lttng_ust_lib_ring_buffer_ctx *ctx,
3d1fc7fd
MD
293 uint32_t event_id)
294{
7dd08bec 295 struct lttng_channel *lttng_chan = channel_get_private(ctx->chan);
2b7080aa 296 struct lttng_stack_ctx *lttng_ctx = ctx->priv;
3d1fc7fd 297
7dd08bec 298 switch (lttng_chan->header_type) {
3d1fc7fd 299 case 1: /* compact */
7dd08bec 300 if (!(ctx->rflags & (RING_BUFFER_RFLAG_FULL_TSC | LTTNG_RFLAG_EXTENDED))) {
3d1fc7fd
MD
301 uint32_t id_time = 0;
302
79dfbf42
MD
303 bt_bitfield_write(&id_time, uint32_t,
304 0,
305 LTTNG_COMPACT_EVENT_BITS,
306 event_id);
307 bt_bitfield_write(&id_time, uint32_t,
308 LTTNG_COMPACT_EVENT_BITS,
309 LTTNG_COMPACT_TSC_BITS,
310 ctx->tsc);
3d1fc7fd
MD
311 lib_ring_buffer_write(config, ctx, &id_time, sizeof(id_time));
312 } else {
313 uint8_t id = 0;
314 uint64_t timestamp = ctx->tsc;
315
79dfbf42
MD
316 bt_bitfield_write(&id, uint8_t,
317 0,
318 LTTNG_COMPACT_EVENT_BITS,
319 31);
3d1fc7fd
MD
320 lib_ring_buffer_write(config, ctx, &id, sizeof(id));
321 /* Align extended struct on largest member */
1dbfff0c 322 lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint64_t));
3d1fc7fd 323 lib_ring_buffer_write(config, ctx, &event_id, sizeof(event_id));
1dbfff0c 324 lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint64_t));
3d1fc7fd
MD
325 lib_ring_buffer_write(config, ctx, &timestamp, sizeof(timestamp));
326 }
327 break;
328 case 2: /* large */
329 {
7dd08bec 330 if (!(ctx->rflags & (RING_BUFFER_RFLAG_FULL_TSC | LTTNG_RFLAG_EXTENDED))) {
3d1fc7fd
MD
331 uint32_t timestamp = (uint32_t) ctx->tsc;
332 uint16_t id = event_id;
333
334 lib_ring_buffer_write(config, ctx, &id, sizeof(id));
1dbfff0c 335 lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint32_t));
3d1fc7fd
MD
336 lib_ring_buffer_write(config, ctx, &timestamp, sizeof(timestamp));
337 } else {
338 uint16_t id = 65535;
339 uint64_t timestamp = ctx->tsc;
340
341 lib_ring_buffer_write(config, ctx, &id, sizeof(id));
342 /* Align extended struct on largest member */
1dbfff0c 343 lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint64_t));
3d1fc7fd 344 lib_ring_buffer_write(config, ctx, &event_id, sizeof(event_id));
1dbfff0c 345 lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint64_t));
3d1fc7fd
MD
346 lib_ring_buffer_write(config, ctx, &timestamp, sizeof(timestamp));
347 }
348 break;
349 }
350 default:
351 WARN_ON_ONCE(1);
352 }
2b7080aa
MD
353 ctx_record(ctx, lttng_chan, lttng_ctx->chan_ctx, APP_CTX_ENABLED);
354 ctx_record(ctx, lttng_chan, lttng_ctx->event_ctx, APP_CTX_ENABLED);
d3b2c3e0 355 lib_ring_buffer_align_ctx(ctx, ctx->largest_align);
3d1fc7fd
MD
356}
357
4cfec15c 358static const struct lttng_ust_lib_ring_buffer_config client_config;
3d1fc7fd 359
23c8854a 360static uint64_t client_ring_buffer_clock_read(struct channel *chan)
3d1fc7fd
MD
361{
362 return lib_ring_buffer_clock_read(chan);
363}
364
365static
4cfec15c 366size_t client_record_header_size(const struct lttng_ust_lib_ring_buffer_config *config,
3d1fc7fd
MD
367 struct channel *chan, size_t offset,
368 size_t *pre_header_padding,
e56bb47c
MD
369 struct lttng_ust_lib_ring_buffer_ctx *ctx,
370 void *client_ctx)
3d1fc7fd
MD
371{
372 return record_header_size(config, chan, offset,
e56bb47c 373 pre_header_padding, ctx, client_ctx);
3d1fc7fd
MD
374}
375
376/**
377 * client_packet_header_size - called on buffer-switch to a new sub-buffer
378 *
379 * Return header size without padding after the structure. Don't use packed
380 * structure because gcc generates inefficient code on some architectures
381 * (powerpc, mips..)
382 */
383static size_t client_packet_header_size(void)
384{
385 return offsetof(struct packet_header, ctx.header_end);
386}
387
23c8854a 388static void client_buffer_begin(struct lttng_ust_lib_ring_buffer *buf, uint64_t tsc,
1d498196 389 unsigned int subbuf_idx,
38fae1d3 390 struct lttng_ust_shm_handle *handle)
3d1fc7fd 391{
1d498196 392 struct channel *chan = shmp(handle, buf->backend.chan);
3d1fc7fd
MD
393 struct packet_header *header =
394 (struct packet_header *)
395 lib_ring_buffer_offset_address(&buf->backend,
1d498196
MD
396 subbuf_idx * chan->backend.subbuf_size,
397 handle);
7dd08bec 398 struct lttng_channel *lttng_chan = channel_get_private(chan);
1ff31389 399 uint64_t cnt = shmp_index(handle, buf->backend.buf_cnt, subbuf_idx)->seq_cnt;
3d1fc7fd 400
34daae3e
MD
401 assert(header);
402 if (!header)
403 return;
3d1fc7fd 404 header->magic = CTF_MAGIC_NUMBER;
7dd08bec
MD
405 memcpy(header->uuid, lttng_chan->uuid, sizeof(lttng_chan->uuid));
406 header->stream_id = lttng_chan->id;
2d94adc5 407 header->stream_instance_id = buf->backend.cpu;
3d1fc7fd
MD
408 header->ctx.timestamp_begin = tsc;
409 header->ctx.timestamp_end = 0;
0c00a107
MD
410 header->ctx.content_size = ~0ULL; /* for debugging */
411 header->ctx.packet_size = ~0ULL;
1ff31389 412 header->ctx.packet_seq_num = chan->backend.num_subbuf * cnt + subbuf_idx;
3d1fc7fd 413 header->ctx.events_discarded = 0;
3d1fc7fd
MD
414 header->ctx.cpu_id = buf->backend.cpu;
415}
416
417/*
418 * offset is assumed to never be 0 here : never deliver a completely empty
419 * subbuffer. data_size is between 1 and subbuf_size.
420 */
23c8854a 421static void client_buffer_end(struct lttng_ust_lib_ring_buffer *buf, uint64_t tsc,
1d498196 422 unsigned int subbuf_idx, unsigned long data_size,
38fae1d3 423 struct lttng_ust_shm_handle *handle)
3d1fc7fd 424{
1d498196 425 struct channel *chan = shmp(handle, buf->backend.chan);
3d1fc7fd
MD
426 struct packet_header *header =
427 (struct packet_header *)
428 lib_ring_buffer_offset_address(&buf->backend,
1d498196
MD
429 subbuf_idx * chan->backend.subbuf_size,
430 handle);
3d1fc7fd
MD
431 unsigned long records_lost = 0;
432
34daae3e
MD
433 assert(header);
434 if (!header)
435 return;
3d1fc7fd 436 header->ctx.timestamp_end = tsc;
b1830883
MD
437 header->ctx.content_size =
438 (uint64_t) data_size * CHAR_BIT; /* in bits */
439 header->ctx.packet_size =
b72687b8 440 (uint64_t) LTTNG_UST_PAGE_ALIGN(data_size) * CHAR_BIT; /* in bits */
04489ab4
MD
441
442 records_lost += lib_ring_buffer_get_records_lost_full(&client_config, buf);
3d1fc7fd
MD
443 records_lost += lib_ring_buffer_get_records_lost_wrap(&client_config, buf);
444 records_lost += lib_ring_buffer_get_records_lost_big(&client_config, buf);
445 header->ctx.events_discarded = records_lost;
446}
447
4cfec15c 448static int client_buffer_create(struct lttng_ust_lib_ring_buffer *buf, void *priv,
38fae1d3 449 int cpu, const char *name, struct lttng_ust_shm_handle *handle)
3d1fc7fd
MD
450{
451 return 0;
452}
453
4cfec15c 454static void client_buffer_finalize(struct lttng_ust_lib_ring_buffer *buf, void *priv, int cpu, struct lttng_ust_shm_handle *handle)
3d1fc7fd
MD
455{
456}
457
a9ff648c
MD
458static void client_content_size_field(const struct lttng_ust_lib_ring_buffer_config *config,
459 size_t *offset, size_t *length)
460{
461 *offset = offsetof(struct packet_header, ctx.content_size);
462 *length = sizeof(((struct packet_header *) NULL)->ctx.content_size);
463}
464
465static void client_packet_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.packet_size);
469 *length = sizeof(((struct packet_header *) NULL)->ctx.packet_size);
470}
471
b2f3252a
JD
472static struct packet_header *client_packet_header(struct lttng_ust_lib_ring_buffer *buf,
473 struct lttng_ust_shm_handle *handle)
474{
c4d6bd10 475 return lib_ring_buffer_read_offset_address(&buf->backend, 0, handle);
b2f3252a
JD
476}
477
478static int client_timestamp_begin(struct lttng_ust_lib_ring_buffer *buf,
479 struct lttng_ust_shm_handle *handle,
480 uint64_t *timestamp_begin)
481{
482 struct packet_header *header;
483
484 header = client_packet_header(buf, handle);
34daae3e
MD
485 if (!header)
486 return -1;
b2f3252a
JD
487 *timestamp_begin = header->ctx.timestamp_begin;
488 return 0;
489}
490
491static int client_timestamp_end(struct lttng_ust_lib_ring_buffer *buf,
492 struct lttng_ust_shm_handle *handle,
493 uint64_t *timestamp_end)
494{
495 struct packet_header *header;
496
497 header = client_packet_header(buf, handle);
34daae3e
MD
498 if (!header)
499 return -1;
b2f3252a
JD
500 *timestamp_end = header->ctx.timestamp_end;
501 return 0;
502}
503
504static int client_events_discarded(struct lttng_ust_lib_ring_buffer *buf,
505 struct lttng_ust_shm_handle *handle,
506 uint64_t *events_discarded)
507{
508 struct packet_header *header;
509
510 header = client_packet_header(buf, handle);
34daae3e
MD
511 if (!header)
512 return -1;
b2f3252a
JD
513 *events_discarded = header->ctx.events_discarded;
514 return 0;
515}
516
517static int client_content_size(struct lttng_ust_lib_ring_buffer *buf,
518 struct lttng_ust_shm_handle *handle,
519 uint64_t *content_size)
520{
521 struct packet_header *header;
522
523 header = client_packet_header(buf, handle);
34daae3e
MD
524 if (!header)
525 return -1;
b2f3252a
JD
526 *content_size = header->ctx.content_size;
527 return 0;
528}
529
530static int client_packet_size(struct lttng_ust_lib_ring_buffer *buf,
531 struct lttng_ust_shm_handle *handle,
532 uint64_t *packet_size)
533{
534 struct packet_header *header;
535
536 header = client_packet_header(buf, handle);
34daae3e
MD
537 if (!header)
538 return -1;
b2f3252a
JD
539 *packet_size = header->ctx.packet_size;
540 return 0;
541}
542
543static int client_stream_id(struct lttng_ust_lib_ring_buffer *buf,
544 struct lttng_ust_shm_handle *handle,
545 uint64_t *stream_id)
546{
db95cf8b
MD
547 struct channel *chan = shmp(handle, buf->backend.chan);
548 struct lttng_channel *lttng_chan = channel_get_private(chan);
549
550 *stream_id = lttng_chan->id;
b2f3252a 551
b2f3252a
JD
552 return 0;
553}
554
fca361e8
JD
555static int client_current_timestamp(struct lttng_ust_lib_ring_buffer *buf,
556 struct lttng_ust_shm_handle *handle,
557 uint64_t *ts)
558{
559 struct channel *chan;
560
561 chan = shmp(handle, handle->chan);
562 *ts = client_ring_buffer_clock_read(chan);
563
564 return 0;
565}
566
1ff31389
JD
567static int client_sequence_number(struct lttng_ust_lib_ring_buffer *buf,
568 struct lttng_ust_shm_handle *handle,
569 uint64_t *seq)
570{
571 struct packet_header *header;
572
573 header = client_packet_header(buf, handle);
3e1db88d
MD
574 if (!header)
575 return -1;
1ff31389
JD
576 *seq = header->ctx.packet_seq_num;
577 return 0;
578}
579
45a00b05
JD
580static int client_instance_id(struct lttng_ust_lib_ring_buffer *buf,
581 struct lttng_ust_shm_handle *handle,
582 uint64_t *id)
583{
db95cf8b 584 *id = buf->backend.cpu;
45a00b05 585
45a00b05
JD
586 return 0;
587}
588
82b9bde8
JD
589static const
590struct lttng_ust_client_lib_ring_buffer_client_cb client_cb = {
591 .parent = {
592 .ring_buffer_clock_read = client_ring_buffer_clock_read,
593 .record_header_size = client_record_header_size,
594 .subbuffer_header_size = client_packet_header_size,
595 .buffer_begin = client_buffer_begin,
596 .buffer_end = client_buffer_end,
597 .buffer_create = client_buffer_create,
598 .buffer_finalize = client_buffer_finalize,
a9ff648c
MD
599 .content_size_field = client_content_size_field,
600 .packet_size_field = client_packet_size_field,
82b9bde8 601 },
b2f3252a
JD
602 .timestamp_begin = client_timestamp_begin,
603 .timestamp_end = client_timestamp_end,
604 .events_discarded = client_events_discarded,
605 .content_size = client_content_size,
606 .packet_size = client_packet_size,
607 .stream_id = client_stream_id,
fca361e8 608 .current_timestamp = client_current_timestamp,
1ff31389 609 .sequence_number = client_sequence_number,
45a00b05 610 .instance_id = client_instance_id,
82b9bde8
JD
611};
612
4cfec15c 613static const struct lttng_ust_lib_ring_buffer_config client_config = {
3d1fc7fd
MD
614 .cb.ring_buffer_clock_read = client_ring_buffer_clock_read,
615 .cb.record_header_size = client_record_header_size,
616 .cb.subbuffer_header_size = client_packet_header_size,
617 .cb.buffer_begin = client_buffer_begin,
618 .cb.buffer_end = client_buffer_end,
619 .cb.buffer_create = client_buffer_create,
620 .cb.buffer_finalize = client_buffer_finalize,
a9ff648c
MD
621 .cb.content_size_field = client_content_size_field,
622 .cb.packet_size_field = client_packet_size_field,
3d1fc7fd 623
79dfbf42 624 .tsc_bits = LTTNG_COMPACT_TSC_BITS,
3d1fc7fd 625 .alloc = RING_BUFFER_ALLOC_PER_CPU,
5d61a504 626 .sync = RING_BUFFER_SYNC_GLOBAL,
3d1fc7fd
MD
627 .mode = RING_BUFFER_MODE_TEMPLATE,
628 .backend = RING_BUFFER_PAGE,
5d61a504 629 .output = RING_BUFFER_MMAP,
3d1fc7fd 630 .oops = RING_BUFFER_OOPS_CONSISTENCY,
5d61a504 631 .ipi = RING_BUFFER_NO_IPI_BARRIER,
34a91bdb 632 .wakeup = LTTNG_CLIENT_WAKEUP,
c1fca457 633 .client_type = LTTNG_CLIENT_TYPE,
82b9bde8
JD
634
635 .cb_ptr = &client_cb.parent,
3d1fc7fd
MD
636};
637
638static
7dd08bec 639struct lttng_channel *_channel_create(const char *name,
a3f61e7f 640 void *buf_addr,
3d1fc7fd
MD
641 size_t subbuf_size, size_t num_subbuf,
642 unsigned int switch_timer_interval,
193183fb 643 unsigned int read_timer_interval,
6ca18e66 644 unsigned char *uuid,
a9ff648c 645 uint32_t chan_id,
b2c5f61a
MD
646 const int *stream_fds, int nr_stream_fds,
647 int64_t blocking_timeout)
3d1fc7fd 648{
74d81a6c 649 struct lttng_channel chan_priv_init;
a3f61e7f 650 struct lttng_ust_shm_handle *handle;
74d81a6c
MD
651 struct lttng_channel *lttng_chan;
652 void *priv;
a3f61e7f 653
74d81a6c
MD
654 memset(&chan_priv_init, 0, sizeof(chan_priv_init));
655 memcpy(chan_priv_init.uuid, uuid, LTTNG_UST_UUID_LEN);
6ca18e66 656 chan_priv_init.id = chan_id;
a3f61e7f 657 handle = channel_create(&client_config, name,
8824f307
MD
658 &priv, __alignof__(struct lttng_channel),
659 sizeof(struct lttng_channel),
74d81a6c 660 &chan_priv_init,
a3f61e7f 661 buf_addr, subbuf_size, num_subbuf,
a9ff648c 662 switch_timer_interval, read_timer_interval,
b2c5f61a 663 stream_fds, nr_stream_fds, blocking_timeout);
a3f61e7f 664 if (!handle)
afdf9825 665 return NULL;
7dd08bec
MD
666 lttng_chan = priv;
667 lttng_chan->handle = handle;
74d81a6c 668 lttng_chan->chan = shmp(handle, handle->chan);
7dd08bec 669 return lttng_chan;
3d1fc7fd
MD
670}
671
672static
74d81a6c 673void lttng_channel_destroy(struct lttng_channel *chan)
3d1fc7fd 674{
74d81a6c 675 channel_destroy(chan->chan, chan->handle, 1);
3d1fc7fd
MD
676}
677
678static
7dd08bec 679int lttng_event_reserve(struct lttng_ust_lib_ring_buffer_ctx *ctx,
3d1fc7fd
MD
680 uint32_t event_id)
681{
7dd08bec 682 struct lttng_channel *lttng_chan = channel_get_private(ctx->chan);
2b7080aa 683 struct lttng_stack_ctx *lttng_ctx = ctx->priv;
e56bb47c 684 struct lttng_client_ctx client_ctx;
3d1fc7fd
MD
685 int ret, cpu;
686
e56bb47c 687 /* Compute internal size of context structures. */
2b7080aa
MD
688 ctx_get_struct_size(lttng_ctx->chan_ctx, &client_ctx.packet_context_len,
689 APP_CTX_ENABLED);
690 ctx_get_struct_size(lttng_ctx->event_ctx, &client_ctx.event_context_len,
691 APP_CTX_ENABLED);
e56bb47c 692
3d1fc7fd
MD
693 cpu = lib_ring_buffer_get_cpu(&client_config);
694 if (cpu < 0)
695 return -EPERM;
696 ctx->cpu = cpu;
697
7dd08bec 698 switch (lttng_chan->header_type) {
3d1fc7fd
MD
699 case 1: /* compact */
700 if (event_id > 30)
7dd08bec 701 ctx->rflags |= LTTNG_RFLAG_EXTENDED;
3d1fc7fd
MD
702 break;
703 case 2: /* large */
704 if (event_id > 65534)
7dd08bec 705 ctx->rflags |= LTTNG_RFLAG_EXTENDED;
3d1fc7fd
MD
706 break;
707 default:
708 WARN_ON_ONCE(1);
709 }
710
e56bb47c 711 ret = lib_ring_buffer_reserve(&client_config, ctx, &client_ctx);
630d6836 712 if (caa_unlikely(ret))
3d1fc7fd 713 goto put;
2b7080aa
MD
714 if (lib_ring_buffer_backend_get_pages(&client_config, ctx,
715 &ctx->backend_pages)) {
716 ret = -EPERM;
717 goto put;
a3492932 718 }
7dd08bec 719 lttng_write_event_header(&client_config, ctx, event_id);
3d1fc7fd
MD
720 return 0;
721put:
722 lib_ring_buffer_put_cpu(&client_config);
723 return ret;
724}
725
726static
7dd08bec 727void lttng_event_commit(struct lttng_ust_lib_ring_buffer_ctx *ctx)
3d1fc7fd
MD
728{
729 lib_ring_buffer_commit(&client_config, ctx);
730 lib_ring_buffer_put_cpu(&client_config);
731}
732
733static
7dd08bec 734void lttng_event_write(struct lttng_ust_lib_ring_buffer_ctx *ctx, const void *src,
3d1fc7fd
MD
735 size_t len)
736{
737 lib_ring_buffer_write(&client_config, ctx, src, len);
738}
739
a44c74d9
MD
740static
741void lttng_event_strcpy(struct lttng_ust_lib_ring_buffer_ctx *ctx, const char *src,
742 size_t len)
743{
744 lib_ring_buffer_strcpy(&client_config, ctx, src, len, '#');
745}
746
9f3fdbc6 747#if 0
3d1fc7fd 748static
7dd08bec 749wait_queue_head_t *lttng_get_reader_wait_queue(struct channel *chan)
3d1fc7fd
MD
750{
751 return &chan->read_wait;
752}
753
754static
7dd08bec 755wait_queue_head_t *lttng_get_hp_wait_queue(struct channel *chan)
3d1fc7fd
MD
756{
757 return &chan->hp_wait;
758}
9f3fdbc6 759#endif //0
3d1fc7fd
MD
760
761static
7dd08bec 762int lttng_is_finalized(struct channel *chan)
3d1fc7fd
MD
763{
764 return lib_ring_buffer_channel_is_finalized(chan);
765}
766
767static
7dd08bec 768int lttng_is_disabled(struct channel *chan)
3d1fc7fd
MD
769{
770 return lib_ring_buffer_channel_is_disabled(chan);
771}
772
43861eab 773static
7dd08bec 774int lttng_flush_buffer(struct channel *chan, struct lttng_ust_shm_handle *handle)
43861eab 775{
4cfec15c 776 struct lttng_ust_lib_ring_buffer *buf;
43861eab
MD
777 int cpu;
778
779 for_each_channel_cpu(cpu, chan) {
74d81a6c
MD
780 int shm_fd, wait_fd, wakeup_fd;
781 uint64_t memory_map_size;
43861eab
MD
782
783 buf = channel_get_ring_buffer(&client_config, chan,
784 cpu, handle, &shm_fd, &wait_fd,
74d81a6c 785 &wakeup_fd, &memory_map_size);
43861eab
MD
786 lib_ring_buffer_switch(&client_config, buf,
787 SWITCH_ACTIVE, handle);
788 }
789 return 0;
790}
791
7dd08bec 792static struct lttng_transport lttng_relay_transport = {
818173b9 793 .name = "relay-" RING_BUFFER_MODE_TEMPLATE_STRING "-mmap",
3d1fc7fd
MD
794 .ops = {
795 .channel_create = _channel_create,
7dd08bec 796 .channel_destroy = lttng_channel_destroy,
7dd08bec
MD
797 .event_reserve = lttng_event_reserve,
798 .event_commit = lttng_event_commit,
799 .event_write = lttng_event_write,
3d1fc7fd 800 .packet_avail_size = NULL, /* Would be racy anyway */
7dd08bec
MD
801 .is_finalized = lttng_is_finalized,
802 .is_disabled = lttng_is_disabled,
803 .flush_buffer = lttng_flush_buffer,
a44c74d9 804 .event_strcpy = lttng_event_strcpy,
3d1fc7fd 805 },
74d81a6c 806 .client_config = &client_config,
3d1fc7fd
MD
807};
808
edaa1431 809void RING_BUFFER_MODE_TEMPLATE_INIT(void)
3d1fc7fd 810{
34a91bdb
MD
811 DBG("LTT : ltt ring buffer client \"%s\" init\n",
812 "relay-" RING_BUFFER_MODE_TEMPLATE_STRING "-mmap");
7dd08bec 813 lttng_transport_register(&lttng_relay_transport);
3d1fc7fd
MD
814}
815
edaa1431 816void RING_BUFFER_MODE_TEMPLATE_EXIT(void)
3d1fc7fd 817{
34a91bdb
MD
818 DBG("LTT : ltt ring buffer client \"%s\" exit\n",
819 "relay-" RING_BUFFER_MODE_TEMPLATE_STRING "-mmap");
7dd08bec 820 lttng_transport_unregister(&lttng_relay_transport);
3d1fc7fd 821}
This page took 0.076225 seconds and 4 git commands to generate.