Move to kernel style SPDX license identifiers
[lttng-ust.git] / liblttng-ust / lttng-ring-buffer-client.h
CommitLineData
3d1fc7fd 1/*
c0c0989a 2 * SPDX-License-Identifier: LGPL-2.1-only
3d1fc7fd 3 *
e92f3e28
MD
4 * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 *
c0c0989a 6 * LTTng lib ring buffer client template.
3d1fc7fd
MD
7 */
8
b4051ad8 9#include <stddef.h>
9f3fdbc6 10#include <stdint.h>
4318ae1b
MD
11#include <lttng/ust-events.h>
12#include "lttng/bitfield.h"
b728d87e 13#include "clock.h"
7dd08bec 14#include "lttng-tracer.h"
9f3fdbc6 15#include "../libringbuffer/frontend_types.h"
3d1fc7fd 16
79dfbf42
MD
17#define LTTNG_COMPACT_EVENT_BITS 5
18#define LTTNG_COMPACT_TSC_BITS 27
19
ce7352a2
MD
20enum app_ctx_mode {
21 APP_CTX_DISABLED,
22 APP_CTX_ENABLED,
23};
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;
62};
3d1fc7fd 63
23c8854a 64static inline uint64_t lib_ring_buffer_clock_read(struct channel *chan)
3d1fc7fd
MD
65{
66 return trace_clock_read64();
67}
68
69static inline
e56bb47c
MD
70size_t ctx_get_aligned_size(size_t offset, struct lttng_ctx *ctx,
71 size_t ctx_len)
3d1fc7fd 72{
3d1fc7fd
MD
73 size_t orig_offset = offset;
74
b5a3dfa5 75 if (caa_likely(!ctx))
3d1fc7fd 76 return 0;
b2cc986a 77 offset += lib_ring_buffer_align(offset, ctx->largest_align);
e56bb47c
MD
78 offset += ctx_len;
79 return offset - orig_offset;
80}
81
82static inline
83void ctx_get_struct_size(struct lttng_ctx *ctx, size_t *ctx_len,
84 enum app_ctx_mode mode)
85{
86 int i;
87 size_t offset = 0;
88
89 if (caa_likely(!ctx)) {
90 *ctx_len = 0;
91 return;
92 }
ce7352a2
MD
93 for (i = 0; i < ctx->nr_fields; i++) {
94 if (mode == APP_CTX_ENABLED) {
95 offset += ctx->fields[i].get_size(&ctx->fields[i], offset);
96 } else {
97 if (lttng_context_is_app(ctx->fields[i].event_field.name)) {
98 /*
99 * Before UST 2.8, we cannot use the
100 * application context, because we
101 * cannot trust that the handler used
102 * for get_size is the same used for
103 * ctx_record, which would result in
104 * corrupted traces when tracing
105 * concurrently with application context
106 * register/unregister.
107 */
108 offset += lttng_ust_dummy_get_size(&ctx->fields[i], offset);
109 } else {
110 offset += ctx->fields[i].get_size(&ctx->fields[i], offset);
111 }
112 }
113 }
e56bb47c 114 *ctx_len = offset;
3d1fc7fd
MD
115}
116
117static inline
4cfec15c 118void ctx_record(struct lttng_ust_lib_ring_buffer_ctx *bufctx,
7dd08bec 119 struct lttng_channel *chan,
ce7352a2
MD
120 struct lttng_ctx *ctx,
121 enum app_ctx_mode mode)
3d1fc7fd
MD
122{
123 int i;
124
b5a3dfa5 125 if (caa_likely(!ctx))
3d1fc7fd 126 return;
b2cc986a 127 lib_ring_buffer_align_ctx(bufctx, ctx->largest_align);
ce7352a2
MD
128 for (i = 0; i < ctx->nr_fields; i++) {
129 if (mode == APP_CTX_ENABLED) {
130 ctx->fields[i].record(&ctx->fields[i], bufctx, chan);
131 } else {
132 if (lttng_context_is_app(ctx->fields[i].event_field.name)) {
133 /*
134 * Before UST 2.8, we cannot use the
135 * application context, because we
136 * cannot trust that the handler used
137 * for get_size is the same used for
138 * ctx_record, which would result in
139 * corrupted traces when tracing
140 * concurrently with application context
141 * register/unregister.
142 */
143 lttng_ust_dummy_record(&ctx->fields[i], bufctx, chan);
144 } else {
145 ctx->fields[i].record(&ctx->fields[i], bufctx, chan);
146 }
147 }
148 }
3d1fc7fd
MD
149}
150
151/*
152 * record_header_size - Calculate the header size and padding necessary.
153 * @config: ring buffer instance configuration
154 * @chan: channel
155 * @offset: offset in the write buffer
156 * @pre_header_padding: padding to add before the header (output)
157 * @ctx: reservation context
158 *
159 * Returns the event header size (including padding).
160 *
161 * The payload must itself determine its own alignment from the biggest type it
162 * contains.
163 */
164static __inline__
8ca68914 165size_t record_header_size(const struct lttng_ust_lib_ring_buffer_config *config,
3d1fc7fd
MD
166 struct channel *chan, size_t offset,
167 size_t *pre_header_padding,
e56bb47c
MD
168 struct lttng_ust_lib_ring_buffer_ctx *ctx,
169 struct lttng_client_ctx *client_ctx)
3d1fc7fd 170{
7dd08bec 171 struct lttng_channel *lttng_chan = channel_get_private(chan);
ce7352a2 172 struct lttng_event *event = ctx->priv;
ba1c40c6 173 struct lttng_stack_ctx *lttng_ctx = ctx->priv2;
3d1fc7fd
MD
174 size_t orig_offset = offset;
175 size_t padding;
176
7dd08bec 177 switch (lttng_chan->header_type) {
3d1fc7fd 178 case 1: /* compact */
1dbfff0c 179 padding = lib_ring_buffer_align(offset, lttng_alignof(uint32_t));
3d1fc7fd 180 offset += padding;
7dd08bec 181 if (!(ctx->rflags & (RING_BUFFER_RFLAG_FULL_TSC | LTTNG_RFLAG_EXTENDED))) {
3d1fc7fd
MD
182 offset += sizeof(uint32_t); /* id and timestamp */
183 } else {
79dfbf42
MD
184 /* Minimum space taken by LTTNG_COMPACT_EVENT_BITS id */
185 offset += (LTTNG_COMPACT_EVENT_BITS + CHAR_BIT - 1) / CHAR_BIT;
3d1fc7fd 186 /* Align extended struct on largest member */
1dbfff0c 187 offset += lib_ring_buffer_align(offset, lttng_alignof(uint64_t));
3d1fc7fd 188 offset += sizeof(uint32_t); /* id */
1dbfff0c 189 offset += lib_ring_buffer_align(offset, lttng_alignof(uint64_t));
3d1fc7fd
MD
190 offset += sizeof(uint64_t); /* timestamp */
191 }
192 break;
193 case 2: /* large */
1dbfff0c 194 padding = lib_ring_buffer_align(offset, lttng_alignof(uint16_t));
3d1fc7fd
MD
195 offset += padding;
196 offset += sizeof(uint16_t);
7dd08bec 197 if (!(ctx->rflags & (RING_BUFFER_RFLAG_FULL_TSC | LTTNG_RFLAG_EXTENDED))) {
1dbfff0c 198 offset += lib_ring_buffer_align(offset, lttng_alignof(uint32_t));
3d1fc7fd
MD
199 offset += sizeof(uint32_t); /* timestamp */
200 } else {
201 /* Align extended struct on largest member */
1dbfff0c 202 offset += lib_ring_buffer_align(offset, lttng_alignof(uint64_t));
3d1fc7fd 203 offset += sizeof(uint32_t); /* id */
1dbfff0c 204 offset += lib_ring_buffer_align(offset, lttng_alignof(uint64_t));
3d1fc7fd
MD
205 offset += sizeof(uint64_t); /* timestamp */
206 }
207 break;
208 default:
9f3fdbc6 209 padding = 0;
3d1fc7fd
MD
210 WARN_ON_ONCE(1);
211 }
ce7352a2
MD
212 if (lttng_ctx) {
213 /* 2.8+ probe ABI. */
e56bb47c
MD
214 offset += ctx_get_aligned_size(offset, lttng_ctx->chan_ctx,
215 client_ctx->packet_context_len);
216 offset += ctx_get_aligned_size(offset, lttng_ctx->event_ctx,
217 client_ctx->event_context_len);
ce7352a2
MD
218 } else {
219 /* Pre 2.8 probe ABI. */
e56bb47c
MD
220 offset += ctx_get_aligned_size(offset, lttng_chan->ctx,
221 client_ctx->packet_context_len);
222 offset += ctx_get_aligned_size(offset, event->ctx,
223 client_ctx->event_context_len);
ce7352a2 224 }
3d1fc7fd
MD
225 *pre_header_padding = padding;
226 return offset - orig_offset;
227}
228
9f3fdbc6 229#include "../libringbuffer/api.h"
82b9bde8 230#include "lttng-rb-clients.h"
3d1fc7fd 231
9f3fdbc6 232static
7dd08bec 233void lttng_write_event_header_slow(const struct lttng_ust_lib_ring_buffer_config *config,
4cfec15c 234 struct lttng_ust_lib_ring_buffer_ctx *ctx,
3d1fc7fd
MD
235 uint32_t event_id);
236
237/*
7dd08bec 238 * lttng_write_event_header
3d1fc7fd
MD
239 *
240 * Writes the event header to the offset (already aligned on 32-bits).
241 *
242 * @config: ring buffer instance configuration
243 * @ctx: reservation context
244 * @event_id: event ID
245 */
246static __inline__
7dd08bec 247void lttng_write_event_header(const struct lttng_ust_lib_ring_buffer_config *config,
4cfec15c 248 struct lttng_ust_lib_ring_buffer_ctx *ctx,
3d1fc7fd
MD
249 uint32_t event_id)
250{
7dd08bec 251 struct lttng_channel *lttng_chan = channel_get_private(ctx->chan);
ce7352a2 252 struct lttng_event *event = ctx->priv;
53569322 253 struct lttng_stack_ctx *lttng_ctx = ctx->priv2;
3d1fc7fd 254
b5a3dfa5 255 if (caa_unlikely(ctx->rflags))
3d1fc7fd
MD
256 goto slow_path;
257
7dd08bec 258 switch (lttng_chan->header_type) {
3d1fc7fd
MD
259 case 1: /* compact */
260 {
261 uint32_t id_time = 0;
262
79dfbf42
MD
263 bt_bitfield_write(&id_time, uint32_t,
264 0,
265 LTTNG_COMPACT_EVENT_BITS,
266 event_id);
267 bt_bitfield_write(&id_time, uint32_t,
268 LTTNG_COMPACT_EVENT_BITS,
269 LTTNG_COMPACT_TSC_BITS,
270 ctx->tsc);
3d1fc7fd
MD
271 lib_ring_buffer_write(config, ctx, &id_time, sizeof(id_time));
272 break;
273 }
274 case 2: /* large */
275 {
276 uint32_t timestamp = (uint32_t) ctx->tsc;
277 uint16_t id = event_id;
278
279 lib_ring_buffer_write(config, ctx, &id, sizeof(id));
1dbfff0c 280 lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint32_t));
3d1fc7fd
MD
281 lib_ring_buffer_write(config, ctx, &timestamp, sizeof(timestamp));
282 break;
283 }
284 default:
285 WARN_ON_ONCE(1);
286 }
287
ce7352a2
MD
288 if (lttng_ctx) {
289 /* 2.8+ probe ABI. */
290 ctx_record(ctx, lttng_chan, lttng_ctx->chan_ctx, APP_CTX_ENABLED);
291 ctx_record(ctx, lttng_chan, lttng_ctx->event_ctx, APP_CTX_ENABLED);
292 } else {
293 /* Pre 2.8 probe ABI. */
294 ctx_record(ctx, lttng_chan, lttng_chan->ctx, APP_CTX_DISABLED);
295 ctx_record(ctx, lttng_chan, event->ctx, APP_CTX_DISABLED);
296 }
d3b2c3e0 297 lib_ring_buffer_align_ctx(ctx, ctx->largest_align);
3d1fc7fd
MD
298
299 return;
300
301slow_path:
7dd08bec 302 lttng_write_event_header_slow(config, ctx, event_id);
3d1fc7fd
MD
303}
304
9f3fdbc6 305static
7dd08bec 306void lttng_write_event_header_slow(const struct lttng_ust_lib_ring_buffer_config *config,
4cfec15c 307 struct lttng_ust_lib_ring_buffer_ctx *ctx,
3d1fc7fd
MD
308 uint32_t event_id)
309{
7dd08bec 310 struct lttng_channel *lttng_chan = channel_get_private(ctx->chan);
ce7352a2 311 struct lttng_event *event = ctx->priv;
53569322 312 struct lttng_stack_ctx *lttng_ctx = ctx->priv2;
3d1fc7fd 313
7dd08bec 314 switch (lttng_chan->header_type) {
3d1fc7fd 315 case 1: /* compact */
7dd08bec 316 if (!(ctx->rflags & (RING_BUFFER_RFLAG_FULL_TSC | LTTNG_RFLAG_EXTENDED))) {
3d1fc7fd
MD
317 uint32_t id_time = 0;
318
79dfbf42
MD
319 bt_bitfield_write(&id_time, uint32_t,
320 0,
321 LTTNG_COMPACT_EVENT_BITS,
322 event_id);
323 bt_bitfield_write(&id_time, uint32_t,
324 LTTNG_COMPACT_EVENT_BITS,
325 LTTNG_COMPACT_TSC_BITS,
326 ctx->tsc);
3d1fc7fd
MD
327 lib_ring_buffer_write(config, ctx, &id_time, sizeof(id_time));
328 } else {
329 uint8_t id = 0;
330 uint64_t timestamp = ctx->tsc;
331
79dfbf42
MD
332 bt_bitfield_write(&id, uint8_t,
333 0,
334 LTTNG_COMPACT_EVENT_BITS,
335 31);
3d1fc7fd
MD
336 lib_ring_buffer_write(config, ctx, &id, sizeof(id));
337 /* Align extended struct on largest member */
1dbfff0c 338 lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint64_t));
3d1fc7fd 339 lib_ring_buffer_write(config, ctx, &event_id, sizeof(event_id));
1dbfff0c 340 lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint64_t));
3d1fc7fd
MD
341 lib_ring_buffer_write(config, ctx, &timestamp, sizeof(timestamp));
342 }
343 break;
344 case 2: /* large */
345 {
7dd08bec 346 if (!(ctx->rflags & (RING_BUFFER_RFLAG_FULL_TSC | LTTNG_RFLAG_EXTENDED))) {
3d1fc7fd
MD
347 uint32_t timestamp = (uint32_t) ctx->tsc;
348 uint16_t id = event_id;
349
350 lib_ring_buffer_write(config, ctx, &id, sizeof(id));
1dbfff0c 351 lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint32_t));
3d1fc7fd
MD
352 lib_ring_buffer_write(config, ctx, &timestamp, sizeof(timestamp));
353 } else {
354 uint16_t id = 65535;
355 uint64_t timestamp = ctx->tsc;
356
357 lib_ring_buffer_write(config, ctx, &id, sizeof(id));
358 /* Align extended struct on largest member */
1dbfff0c 359 lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint64_t));
3d1fc7fd 360 lib_ring_buffer_write(config, ctx, &event_id, sizeof(event_id));
1dbfff0c 361 lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint64_t));
3d1fc7fd
MD
362 lib_ring_buffer_write(config, ctx, &timestamp, sizeof(timestamp));
363 }
364 break;
365 }
366 default:
367 WARN_ON_ONCE(1);
368 }
ce7352a2
MD
369 if (lttng_ctx) {
370 /* 2.8+ probe ABI. */
371 ctx_record(ctx, lttng_chan, lttng_ctx->chan_ctx, APP_CTX_ENABLED);
372 ctx_record(ctx, lttng_chan, lttng_ctx->event_ctx, APP_CTX_ENABLED);
373 } else {
374 /* Pre 2.8 probe ABI. */
375 ctx_record(ctx, lttng_chan, lttng_chan->ctx, APP_CTX_DISABLED);
376 ctx_record(ctx, lttng_chan, event->ctx, APP_CTX_DISABLED);
377 }
d3b2c3e0 378 lib_ring_buffer_align_ctx(ctx, ctx->largest_align);
3d1fc7fd
MD
379}
380
4cfec15c 381static const struct lttng_ust_lib_ring_buffer_config client_config;
3d1fc7fd 382
23c8854a 383static uint64_t client_ring_buffer_clock_read(struct channel *chan)
3d1fc7fd
MD
384{
385 return lib_ring_buffer_clock_read(chan);
386}
387
388static
4cfec15c 389size_t client_record_header_size(const struct lttng_ust_lib_ring_buffer_config *config,
3d1fc7fd
MD
390 struct channel *chan, size_t offset,
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{
1d498196 415 struct 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);
7dd08bec 421 struct lttng_channel *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;
7dd08bec
MD
428 memcpy(header->uuid, lttng_chan->uuid, sizeof(lttng_chan->uuid));
429 header->stream_id = lttng_chan->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{
1d498196 448 struct 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,
502 struct lttng_ust_shm_handle *handle,
503 uint64_t *timestamp_begin)
504{
505 struct packet_header *header;
506
507 header = client_packet_header(buf, handle);
34daae3e
MD
508 if (!header)
509 return -1;
b2f3252a
JD
510 *timestamp_begin = header->ctx.timestamp_begin;
511 return 0;
512}
513
514static int client_timestamp_end(struct lttng_ust_lib_ring_buffer *buf,
515 struct lttng_ust_shm_handle *handle,
516 uint64_t *timestamp_end)
517{
518 struct packet_header *header;
519
520 header = client_packet_header(buf, handle);
34daae3e
MD
521 if (!header)
522 return -1;
b2f3252a
JD
523 *timestamp_end = header->ctx.timestamp_end;
524 return 0;
525}
526
527static int client_events_discarded(struct lttng_ust_lib_ring_buffer *buf,
528 struct lttng_ust_shm_handle *handle,
529 uint64_t *events_discarded)
530{
531 struct packet_header *header;
532
533 header = client_packet_header(buf, handle);
34daae3e
MD
534 if (!header)
535 return -1;
b2f3252a
JD
536 *events_discarded = header->ctx.events_discarded;
537 return 0;
538}
539
540static int client_content_size(struct lttng_ust_lib_ring_buffer *buf,
541 struct lttng_ust_shm_handle *handle,
542 uint64_t *content_size)
543{
544 struct packet_header *header;
545
546 header = client_packet_header(buf, handle);
34daae3e
MD
547 if (!header)
548 return -1;
b2f3252a
JD
549 *content_size = header->ctx.content_size;
550 return 0;
551}
552
553static int client_packet_size(struct lttng_ust_lib_ring_buffer *buf,
554 struct lttng_ust_shm_handle *handle,
555 uint64_t *packet_size)
556{
557 struct packet_header *header;
558
559 header = client_packet_header(buf, handle);
34daae3e
MD
560 if (!header)
561 return -1;
b2f3252a
JD
562 *packet_size = header->ctx.packet_size;
563 return 0;
564}
565
566static int client_stream_id(struct lttng_ust_lib_ring_buffer *buf,
567 struct lttng_ust_shm_handle *handle,
568 uint64_t *stream_id)
569{
db95cf8b
MD
570 struct channel *chan = shmp(handle, buf->backend.chan);
571 struct lttng_channel *lttng_chan = channel_get_private(chan);
572
573 *stream_id = lttng_chan->id;
b2f3252a 574
b2f3252a
JD
575 return 0;
576}
577
fca361e8
JD
578static int client_current_timestamp(struct lttng_ust_lib_ring_buffer *buf,
579 struct lttng_ust_shm_handle *handle,
580 uint64_t *ts)
581{
582 struct channel *chan;
583
584 chan = shmp(handle, handle->chan);
585 *ts = client_ring_buffer_clock_read(chan);
586
587 return 0;
588}
589
1ff31389
JD
590static int client_sequence_number(struct lttng_ust_lib_ring_buffer *buf,
591 struct lttng_ust_shm_handle *handle,
592 uint64_t *seq)
593{
594 struct packet_header *header;
595
596 header = client_packet_header(buf, handle);
3e1db88d
MD
597 if (!header)
598 return -1;
1ff31389
JD
599 *seq = header->ctx.packet_seq_num;
600 return 0;
601}
602
45a00b05
JD
603static int client_instance_id(struct lttng_ust_lib_ring_buffer *buf,
604 struct lttng_ust_shm_handle *handle,
605 uint64_t *id)
606{
db95cf8b 607 *id = buf->backend.cpu;
45a00b05 608
45a00b05
JD
609 return 0;
610}
611
82b9bde8
JD
612static const
613struct lttng_ust_client_lib_ring_buffer_client_cb client_cb = {
614 .parent = {
615 .ring_buffer_clock_read = client_ring_buffer_clock_read,
616 .record_header_size = client_record_header_size,
617 .subbuffer_header_size = client_packet_header_size,
618 .buffer_begin = client_buffer_begin,
619 .buffer_end = client_buffer_end,
620 .buffer_create = client_buffer_create,
621 .buffer_finalize = client_buffer_finalize,
a9ff648c
MD
622 .content_size_field = client_content_size_field,
623 .packet_size_field = client_packet_size_field,
82b9bde8 624 },
b2f3252a
JD
625 .timestamp_begin = client_timestamp_begin,
626 .timestamp_end = client_timestamp_end,
627 .events_discarded = client_events_discarded,
628 .content_size = client_content_size,
629 .packet_size = client_packet_size,
630 .stream_id = client_stream_id,
fca361e8 631 .current_timestamp = client_current_timestamp,
1ff31389 632 .sequence_number = client_sequence_number,
45a00b05 633 .instance_id = client_instance_id,
82b9bde8
JD
634};
635
4cfec15c 636static const struct lttng_ust_lib_ring_buffer_config client_config = {
3d1fc7fd
MD
637 .cb.ring_buffer_clock_read = client_ring_buffer_clock_read,
638 .cb.record_header_size = client_record_header_size,
639 .cb.subbuffer_header_size = client_packet_header_size,
640 .cb.buffer_begin = client_buffer_begin,
641 .cb.buffer_end = client_buffer_end,
642 .cb.buffer_create = client_buffer_create,
643 .cb.buffer_finalize = client_buffer_finalize,
a9ff648c
MD
644 .cb.content_size_field = client_content_size_field,
645 .cb.packet_size_field = client_packet_size_field,
3d1fc7fd 646
79dfbf42 647 .tsc_bits = LTTNG_COMPACT_TSC_BITS,
3d1fc7fd 648 .alloc = RING_BUFFER_ALLOC_PER_CPU,
5d61a504 649 .sync = RING_BUFFER_SYNC_GLOBAL,
3d1fc7fd
MD
650 .mode = RING_BUFFER_MODE_TEMPLATE,
651 .backend = RING_BUFFER_PAGE,
5d61a504 652 .output = RING_BUFFER_MMAP,
3d1fc7fd 653 .oops = RING_BUFFER_OOPS_CONSISTENCY,
5d61a504 654 .ipi = RING_BUFFER_NO_IPI_BARRIER,
34a91bdb 655 .wakeup = LTTNG_CLIENT_WAKEUP,
c1fca457 656 .client_type = LTTNG_CLIENT_TYPE,
82b9bde8
JD
657
658 .cb_ptr = &client_cb.parent,
3d1fc7fd
MD
659};
660
82b9bde8 661const struct lttng_ust_client_lib_ring_buffer_client_cb *LTTNG_CLIENT_CALLBACKS = &client_cb;
c1fca457 662
3d1fc7fd 663static
7dd08bec 664struct lttng_channel *_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{
74d81a6c 674 struct lttng_channel chan_priv_init;
a3f61e7f 675 struct lttng_ust_shm_handle *handle;
74d81a6c
MD
676 struct lttng_channel *lttng_chan;
677 void *priv;
a3f61e7f 678
74d81a6c
MD
679 memset(&chan_priv_init, 0, sizeof(chan_priv_init));
680 memcpy(chan_priv_init.uuid, uuid, LTTNG_UST_UUID_LEN);
6ca18e66 681 chan_priv_init.id = chan_id;
a3f61e7f 682 handle = channel_create(&client_config, name,
8824f307
MD
683 &priv, __alignof__(struct lttng_channel),
684 sizeof(struct lttng_channel),
74d81a6c 685 &chan_priv_init,
a3f61e7f 686 buf_addr, subbuf_size, num_subbuf,
a9ff648c 687 switch_timer_interval, read_timer_interval,
b2c5f61a 688 stream_fds, nr_stream_fds, blocking_timeout);
a3f61e7f 689 if (!handle)
afdf9825 690 return NULL;
7dd08bec
MD
691 lttng_chan = priv;
692 lttng_chan->handle = handle;
74d81a6c 693 lttng_chan->chan = shmp(handle, handle->chan);
7dd08bec 694 return lttng_chan;
3d1fc7fd
MD
695}
696
697static
74d81a6c 698void lttng_channel_destroy(struct lttng_channel *chan)
3d1fc7fd 699{
74d81a6c 700 channel_destroy(chan->chan, chan->handle, 1);
3d1fc7fd
MD
701}
702
703static
7dd08bec 704int lttng_event_reserve(struct lttng_ust_lib_ring_buffer_ctx *ctx,
3d1fc7fd
MD
705 uint32_t event_id)
706{
7dd08bec 707 struct lttng_channel *lttng_chan = channel_get_private(ctx->chan);
e56bb47c
MD
708 struct lttng_event *event = ctx->priv;
709 struct lttng_stack_ctx *lttng_ctx = ctx->priv2;
710 struct lttng_client_ctx client_ctx;
3d1fc7fd
MD
711 int ret, cpu;
712
e56bb47c
MD
713 /* Compute internal size of context structures. */
714
715 if (lttng_ctx) {
716 /* 2.8+ probe ABI. */
717 ctx_get_struct_size(lttng_ctx->chan_ctx, &client_ctx.packet_context_len,
718 APP_CTX_ENABLED);
719 ctx_get_struct_size(lttng_ctx->event_ctx, &client_ctx.event_context_len,
720 APP_CTX_ENABLED);
721 } else {
722 /* Pre 2.8 probe ABI. */
723 ctx_get_struct_size(lttng_chan->ctx, &client_ctx.packet_context_len,
724 APP_CTX_DISABLED);
725 ctx_get_struct_size(event->ctx, &client_ctx.event_context_len,
726 APP_CTX_DISABLED);
727 }
728
3d1fc7fd
MD
729 cpu = lib_ring_buffer_get_cpu(&client_config);
730 if (cpu < 0)
731 return -EPERM;
732 ctx->cpu = cpu;
733
7dd08bec 734 switch (lttng_chan->header_type) {
3d1fc7fd
MD
735 case 1: /* compact */
736 if (event_id > 30)
7dd08bec 737 ctx->rflags |= LTTNG_RFLAG_EXTENDED;
3d1fc7fd
MD
738 break;
739 case 2: /* large */
740 if (event_id > 65534)
7dd08bec 741 ctx->rflags |= LTTNG_RFLAG_EXTENDED;
3d1fc7fd
MD
742 break;
743 default:
744 WARN_ON_ONCE(1);
745 }
746
e56bb47c 747 ret = lib_ring_buffer_reserve(&client_config, ctx, &client_ctx);
630d6836 748 if (caa_unlikely(ret))
3d1fc7fd 749 goto put;
a3492932
MD
750 if (caa_likely(ctx->ctx_len
751 >= sizeof(struct lttng_ust_lib_ring_buffer_ctx))) {
752 if (lib_ring_buffer_backend_get_pages(&client_config, ctx,
753 &ctx->backend_pages)) {
754 ret = -EPERM;
755 goto put;
756 }
757 }
7dd08bec 758 lttng_write_event_header(&client_config, ctx, event_id);
3d1fc7fd
MD
759 return 0;
760put:
761 lib_ring_buffer_put_cpu(&client_config);
762 return ret;
763}
764
765static
7dd08bec 766void lttng_event_commit(struct lttng_ust_lib_ring_buffer_ctx *ctx)
3d1fc7fd
MD
767{
768 lib_ring_buffer_commit(&client_config, ctx);
769 lib_ring_buffer_put_cpu(&client_config);
770}
771
772static
7dd08bec 773void lttng_event_write(struct lttng_ust_lib_ring_buffer_ctx *ctx, const void *src,
3d1fc7fd
MD
774 size_t len)
775{
776 lib_ring_buffer_write(&client_config, ctx, src, len);
777}
778
a44c74d9
MD
779static
780void lttng_event_strcpy(struct lttng_ust_lib_ring_buffer_ctx *ctx, const char *src,
781 size_t len)
782{
783 lib_ring_buffer_strcpy(&client_config, ctx, src, len, '#');
784}
785
9f3fdbc6 786#if 0
3d1fc7fd 787static
7dd08bec 788wait_queue_head_t *lttng_get_reader_wait_queue(struct channel *chan)
3d1fc7fd
MD
789{
790 return &chan->read_wait;
791}
792
793static
7dd08bec 794wait_queue_head_t *lttng_get_hp_wait_queue(struct channel *chan)
3d1fc7fd
MD
795{
796 return &chan->hp_wait;
797}
9f3fdbc6 798#endif //0
3d1fc7fd
MD
799
800static
7dd08bec 801int lttng_is_finalized(struct channel *chan)
3d1fc7fd
MD
802{
803 return lib_ring_buffer_channel_is_finalized(chan);
804}
805
806static
7dd08bec 807int lttng_is_disabled(struct channel *chan)
3d1fc7fd
MD
808{
809 return lib_ring_buffer_channel_is_disabled(chan);
810}
811
43861eab 812static
7dd08bec 813int lttng_flush_buffer(struct channel *chan, struct lttng_ust_shm_handle *handle)
43861eab 814{
4cfec15c 815 struct lttng_ust_lib_ring_buffer *buf;
43861eab
MD
816 int cpu;
817
818 for_each_channel_cpu(cpu, chan) {
74d81a6c
MD
819 int shm_fd, wait_fd, wakeup_fd;
820 uint64_t memory_map_size;
43861eab
MD
821
822 buf = channel_get_ring_buffer(&client_config, chan,
823 cpu, handle, &shm_fd, &wait_fd,
74d81a6c 824 &wakeup_fd, &memory_map_size);
43861eab
MD
825 lib_ring_buffer_switch(&client_config, buf,
826 SWITCH_ACTIVE, handle);
827 }
828 return 0;
829}
830
7dd08bec 831static struct lttng_transport lttng_relay_transport = {
818173b9 832 .name = "relay-" RING_BUFFER_MODE_TEMPLATE_STRING "-mmap",
3d1fc7fd
MD
833 .ops = {
834 .channel_create = _channel_create,
7dd08bec 835 .channel_destroy = lttng_channel_destroy,
7dd08bec
MD
836 .event_reserve = lttng_event_reserve,
837 .event_commit = lttng_event_commit,
838 .event_write = lttng_event_write,
3d1fc7fd 839 .packet_avail_size = NULL, /* Would be racy anyway */
7dd08bec
MD
840 .is_finalized = lttng_is_finalized,
841 .is_disabled = lttng_is_disabled,
842 .flush_buffer = lttng_flush_buffer,
a44c74d9 843 .event_strcpy = lttng_event_strcpy,
3d1fc7fd 844 },
74d81a6c 845 .client_config = &client_config,
3d1fc7fd
MD
846};
847
edaa1431 848void RING_BUFFER_MODE_TEMPLATE_INIT(void)
3d1fc7fd 849{
34a91bdb
MD
850 DBG("LTT : ltt ring buffer client \"%s\" init\n",
851 "relay-" RING_BUFFER_MODE_TEMPLATE_STRING "-mmap");
7dd08bec 852 lttng_transport_register(&lttng_relay_transport);
3d1fc7fd
MD
853}
854
edaa1431 855void RING_BUFFER_MODE_TEMPLATE_EXIT(void)
3d1fc7fd 856{
34a91bdb
MD
857 DBG("LTT : ltt ring buffer client \"%s\" exit\n",
858 "relay-" RING_BUFFER_MODE_TEMPLATE_STRING "-mmap");
7dd08bec 859 lttng_transport_unregister(&lttng_relay_transport);
3d1fc7fd 860}
This page took 0.075625 seconds and 4 git commands to generate.