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