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