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