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