Move ust-events.h private functions to internal
[lttng-ust.git] / liblttng-ust / lttng-ring-buffer-client.h
CommitLineData
3d1fc7fd 1/*
c0c0989a 2 * SPDX-License-Identifier: LGPL-2.1-only
3d1fc7fd 3 *
e92f3e28
MD
4 * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 *
c0c0989a 6 * LTTng lib ring buffer client template.
3d1fc7fd
MD
7 */
8
b4051ad8 9#include <stddef.h>
9f3fdbc6 10#include <stdint.h>
7753d283
MJ
11
12#include <ust-events-internal.h>
ae4b659d 13#include "ust-bitfield.h"
cd61d9bf 14#include "ust-compat.h"
b728d87e 15#include "clock.h"
7dd08bec 16#include "lttng-tracer.h"
9f3fdbc6 17#include "../libringbuffer/frontend_types.h"
3d1fc7fd 18
79dfbf42
MD
19#define LTTNG_COMPACT_EVENT_BITS 5
20#define LTTNG_COMPACT_TSC_BITS 27
21
ce7352a2
MD
22enum app_ctx_mode {
23 APP_CTX_DISABLED,
24 APP_CTX_ENABLED,
25};
26
3d1fc7fd
MD
27/*
28 * Keep the natural field alignment for _each field_ within this structure if
29 * you ever add/remove a field from this header. Packed attribute is not used
30 * because gcc generates poor code on at least powerpc and mips. Don't ever
31 * let gcc add padding between the structure elements.
32 */
33
34struct packet_header {
35 /* Trace packet header */
36 uint32_t magic; /*
37 * Trace magic number.
38 * contains endianness information.
39 */
19d8b1b3 40 uint8_t uuid[LTTNG_UST_UUID_LEN];
3d1fc7fd 41 uint32_t stream_id;
2d94adc5 42 uint64_t stream_instance_id;
3d1fc7fd
MD
43
44 struct {
45 /* Stream packet context */
46 uint64_t timestamp_begin; /* Cycle count at subbuffer start */
47 uint64_t timestamp_end; /* Cycle count at subbuffer end */
0c00a107
MD
48 uint64_t content_size; /* Size of data in subbuffer */
49 uint64_t packet_size; /* Subbuffer size (include padding) */
1ff31389 50 uint64_t packet_seq_num; /* Packet sequence number */
18fc8d71 51 unsigned long events_discarded; /*
3d1fc7fd
MD
52 * Events lost in this subbuffer since
53 * the beginning of the trace.
54 * (may overflow)
55 */
3d1fc7fd
MD
56 uint32_t cpu_id; /* CPU id associated with stream */
57 uint8_t header_end; /* End of header */
58 } ctx;
59};
60
e56bb47c
MD
61struct lttng_client_ctx {
62 size_t packet_context_len;
63 size_t event_context_len;
64};
3d1fc7fd 65
23c8854a 66static inline uint64_t lib_ring_buffer_clock_read(struct channel *chan)
3d1fc7fd
MD
67{
68 return trace_clock_read64();
69}
70
71static inline
e56bb47c
MD
72size_t ctx_get_aligned_size(size_t offset, struct lttng_ctx *ctx,
73 size_t ctx_len)
3d1fc7fd 74{
3d1fc7fd
MD
75 size_t orig_offset = offset;
76
b5a3dfa5 77 if (caa_likely(!ctx))
3d1fc7fd 78 return 0;
b2cc986a 79 offset += lib_ring_buffer_align(offset, ctx->largest_align);
e56bb47c
MD
80 offset += ctx_len;
81 return offset - orig_offset;
82}
83
84static inline
85void ctx_get_struct_size(struct lttng_ctx *ctx, size_t *ctx_len,
86 enum app_ctx_mode mode)
87{
88 int i;
89 size_t offset = 0;
90
91 if (caa_likely(!ctx)) {
92 *ctx_len = 0;
93 return;
94 }
ce7352a2
MD
95 for (i = 0; i < ctx->nr_fields; i++) {
96 if (mode == APP_CTX_ENABLED) {
97 offset += ctx->fields[i].get_size(&ctx->fields[i], offset);
98 } else {
99 if (lttng_context_is_app(ctx->fields[i].event_field.name)) {
100 /*
101 * Before UST 2.8, we cannot use the
102 * application context, because we
103 * cannot trust that the handler used
104 * for get_size is the same used for
105 * ctx_record, which would result in
106 * corrupted traces when tracing
107 * concurrently with application context
108 * register/unregister.
109 */
110 offset += lttng_ust_dummy_get_size(&ctx->fields[i], offset);
111 } else {
112 offset += ctx->fields[i].get_size(&ctx->fields[i], offset);
113 }
114 }
115 }
e56bb47c 116 *ctx_len = offset;
3d1fc7fd
MD
117}
118
119static inline
4cfec15c 120void ctx_record(struct lttng_ust_lib_ring_buffer_ctx *bufctx,
7dd08bec 121 struct lttng_channel *chan,
ce7352a2
MD
122 struct lttng_ctx *ctx,
123 enum app_ctx_mode mode)
3d1fc7fd
MD
124{
125 int i;
126
b5a3dfa5 127 if (caa_likely(!ctx))
3d1fc7fd 128 return;
b2cc986a 129 lib_ring_buffer_align_ctx(bufctx, ctx->largest_align);
ce7352a2
MD
130 for (i = 0; i < ctx->nr_fields; i++) {
131 if (mode == APP_CTX_ENABLED) {
132 ctx->fields[i].record(&ctx->fields[i], bufctx, chan);
133 } else {
134 if (lttng_context_is_app(ctx->fields[i].event_field.name)) {
135 /*
136 * Before UST 2.8, we cannot use the
137 * application context, because we
138 * cannot trust that the handler used
139 * for get_size is the same used for
140 * ctx_record, which would result in
141 * corrupted traces when tracing
142 * concurrently with application context
143 * register/unregister.
144 */
145 lttng_ust_dummy_record(&ctx->fields[i], bufctx, chan);
146 } else {
147 ctx->fields[i].record(&ctx->fields[i], bufctx, chan);
148 }
149 }
150 }
3d1fc7fd
MD
151}
152
153/*
154 * record_header_size - Calculate the header size and padding necessary.
155 * @config: ring buffer instance configuration
156 * @chan: channel
157 * @offset: offset in the write buffer
158 * @pre_header_padding: padding to add before the header (output)
159 * @ctx: reservation context
160 *
161 * Returns the event header size (including padding).
162 *
163 * The payload must itself determine its own alignment from the biggest type it
164 * contains.
165 */
166static __inline__
8ca68914 167size_t record_header_size(const struct lttng_ust_lib_ring_buffer_config *config,
3d1fc7fd
MD
168 struct channel *chan, size_t offset,
169 size_t *pre_header_padding,
e56bb47c
MD
170 struct lttng_ust_lib_ring_buffer_ctx *ctx,
171 struct lttng_client_ctx *client_ctx)
3d1fc7fd 172{
7dd08bec 173 struct lttng_channel *lttng_chan = channel_get_private(chan);
ce7352a2 174 struct lttng_event *event = ctx->priv;
ba1c40c6 175 struct lttng_stack_ctx *lttng_ctx = ctx->priv2;
3d1fc7fd
MD
176 size_t orig_offset = offset;
177 size_t padding;
178
7dd08bec 179 switch (lttng_chan->header_type) {
3d1fc7fd 180 case 1: /* compact */
1dbfff0c 181 padding = lib_ring_buffer_align(offset, lttng_alignof(uint32_t));
3d1fc7fd 182 offset += padding;
7dd08bec 183 if (!(ctx->rflags & (RING_BUFFER_RFLAG_FULL_TSC | LTTNG_RFLAG_EXTENDED))) {
3d1fc7fd
MD
184 offset += sizeof(uint32_t); /* id and timestamp */
185 } else {
79dfbf42
MD
186 /* Minimum space taken by LTTNG_COMPACT_EVENT_BITS id */
187 offset += (LTTNG_COMPACT_EVENT_BITS + CHAR_BIT - 1) / CHAR_BIT;
3d1fc7fd 188 /* Align extended struct on largest member */
1dbfff0c 189 offset += lib_ring_buffer_align(offset, lttng_alignof(uint64_t));
3d1fc7fd 190 offset += sizeof(uint32_t); /* id */
1dbfff0c 191 offset += lib_ring_buffer_align(offset, lttng_alignof(uint64_t));
3d1fc7fd
MD
192 offset += sizeof(uint64_t); /* timestamp */
193 }
194 break;
195 case 2: /* large */
1dbfff0c 196 padding = lib_ring_buffer_align(offset, lttng_alignof(uint16_t));
3d1fc7fd
MD
197 offset += padding;
198 offset += sizeof(uint16_t);
7dd08bec 199 if (!(ctx->rflags & (RING_BUFFER_RFLAG_FULL_TSC | LTTNG_RFLAG_EXTENDED))) {
1dbfff0c 200 offset += lib_ring_buffer_align(offset, lttng_alignof(uint32_t));
3d1fc7fd
MD
201 offset += sizeof(uint32_t); /* timestamp */
202 } else {
203 /* Align extended struct on largest member */
1dbfff0c 204 offset += lib_ring_buffer_align(offset, lttng_alignof(uint64_t));
3d1fc7fd 205 offset += sizeof(uint32_t); /* id */
1dbfff0c 206 offset += lib_ring_buffer_align(offset, lttng_alignof(uint64_t));
3d1fc7fd
MD
207 offset += sizeof(uint64_t); /* timestamp */
208 }
209 break;
210 default:
9f3fdbc6 211 padding = 0;
3d1fc7fd
MD
212 WARN_ON_ONCE(1);
213 }
ce7352a2
MD
214 if (lttng_ctx) {
215 /* 2.8+ probe ABI. */
e56bb47c
MD
216 offset += ctx_get_aligned_size(offset, lttng_ctx->chan_ctx,
217 client_ctx->packet_context_len);
218 offset += ctx_get_aligned_size(offset, lttng_ctx->event_ctx,
219 client_ctx->event_context_len);
ce7352a2
MD
220 } else {
221 /* Pre 2.8 probe ABI. */
e56bb47c
MD
222 offset += ctx_get_aligned_size(offset, lttng_chan->ctx,
223 client_ctx->packet_context_len);
224 offset += ctx_get_aligned_size(offset, event->ctx,
225 client_ctx->event_context_len);
ce7352a2 226 }
3d1fc7fd
MD
227 *pre_header_padding = padding;
228 return offset - orig_offset;
229}
230
9f3fdbc6 231#include "../libringbuffer/api.h"
82b9bde8 232#include "lttng-rb-clients.h"
3d1fc7fd 233
9f3fdbc6 234static
7dd08bec 235void lttng_write_event_header_slow(const struct lttng_ust_lib_ring_buffer_config *config,
4cfec15c 236 struct lttng_ust_lib_ring_buffer_ctx *ctx,
3d1fc7fd
MD
237 uint32_t event_id);
238
239/*
7dd08bec 240 * lttng_write_event_header
3d1fc7fd
MD
241 *
242 * Writes the event header to the offset (already aligned on 32-bits).
243 *
244 * @config: ring buffer instance configuration
245 * @ctx: reservation context
246 * @event_id: event ID
247 */
248static __inline__
7dd08bec 249void lttng_write_event_header(const struct lttng_ust_lib_ring_buffer_config *config,
4cfec15c 250 struct lttng_ust_lib_ring_buffer_ctx *ctx,
3d1fc7fd
MD
251 uint32_t event_id)
252{
7dd08bec 253 struct lttng_channel *lttng_chan = channel_get_private(ctx->chan);
ce7352a2 254 struct lttng_event *event = ctx->priv;
53569322 255 struct lttng_stack_ctx *lttng_ctx = ctx->priv2;
3d1fc7fd 256
b5a3dfa5 257 if (caa_unlikely(ctx->rflags))
3d1fc7fd
MD
258 goto slow_path;
259
7dd08bec 260 switch (lttng_chan->header_type) {
3d1fc7fd
MD
261 case 1: /* compact */
262 {
263 uint32_t id_time = 0;
264
79dfbf42
MD
265 bt_bitfield_write(&id_time, uint32_t,
266 0,
267 LTTNG_COMPACT_EVENT_BITS,
268 event_id);
269 bt_bitfield_write(&id_time, uint32_t,
270 LTTNG_COMPACT_EVENT_BITS,
271 LTTNG_COMPACT_TSC_BITS,
272 ctx->tsc);
3d1fc7fd
MD
273 lib_ring_buffer_write(config, ctx, &id_time, sizeof(id_time));
274 break;
275 }
276 case 2: /* large */
277 {
278 uint32_t timestamp = (uint32_t) ctx->tsc;
279 uint16_t id = event_id;
280
281 lib_ring_buffer_write(config, ctx, &id, sizeof(id));
1dbfff0c 282 lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint32_t));
3d1fc7fd
MD
283 lib_ring_buffer_write(config, ctx, &timestamp, sizeof(timestamp));
284 break;
285 }
286 default:
287 WARN_ON_ONCE(1);
288 }
289
ce7352a2
MD
290 if (lttng_ctx) {
291 /* 2.8+ probe ABI. */
292 ctx_record(ctx, lttng_chan, lttng_ctx->chan_ctx, APP_CTX_ENABLED);
293 ctx_record(ctx, lttng_chan, lttng_ctx->event_ctx, APP_CTX_ENABLED);
294 } else {
295 /* Pre 2.8 probe ABI. */
296 ctx_record(ctx, lttng_chan, lttng_chan->ctx, APP_CTX_DISABLED);
297 ctx_record(ctx, lttng_chan, event->ctx, APP_CTX_DISABLED);
298 }
d3b2c3e0 299 lib_ring_buffer_align_ctx(ctx, ctx->largest_align);
3d1fc7fd
MD
300
301 return;
302
303slow_path:
7dd08bec 304 lttng_write_event_header_slow(config, ctx, event_id);
3d1fc7fd
MD
305}
306
9f3fdbc6 307static
7dd08bec 308void lttng_write_event_header_slow(const struct lttng_ust_lib_ring_buffer_config *config,
4cfec15c 309 struct lttng_ust_lib_ring_buffer_ctx *ctx,
3d1fc7fd
MD
310 uint32_t event_id)
311{
7dd08bec 312 struct lttng_channel *lttng_chan = channel_get_private(ctx->chan);
ce7352a2 313 struct lttng_event *event = ctx->priv;
53569322 314 struct lttng_stack_ctx *lttng_ctx = ctx->priv2;
3d1fc7fd 315
7dd08bec 316 switch (lttng_chan->header_type) {
3d1fc7fd 317 case 1: /* compact */
7dd08bec 318 if (!(ctx->rflags & (RING_BUFFER_RFLAG_FULL_TSC | LTTNG_RFLAG_EXTENDED))) {
3d1fc7fd
MD
319 uint32_t id_time = 0;
320
79dfbf42
MD
321 bt_bitfield_write(&id_time, uint32_t,
322 0,
323 LTTNG_COMPACT_EVENT_BITS,
324 event_id);
325 bt_bitfield_write(&id_time, uint32_t,
326 LTTNG_COMPACT_EVENT_BITS,
327 LTTNG_COMPACT_TSC_BITS,
328 ctx->tsc);
3d1fc7fd
MD
329 lib_ring_buffer_write(config, ctx, &id_time, sizeof(id_time));
330 } else {
331 uint8_t id = 0;
332 uint64_t timestamp = ctx->tsc;
333
79dfbf42
MD
334 bt_bitfield_write(&id, uint8_t,
335 0,
336 LTTNG_COMPACT_EVENT_BITS,
337 31);
3d1fc7fd
MD
338 lib_ring_buffer_write(config, ctx, &id, sizeof(id));
339 /* Align extended struct on largest member */
1dbfff0c 340 lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint64_t));
3d1fc7fd 341 lib_ring_buffer_write(config, ctx, &event_id, sizeof(event_id));
1dbfff0c 342 lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint64_t));
3d1fc7fd
MD
343 lib_ring_buffer_write(config, ctx, &timestamp, sizeof(timestamp));
344 }
345 break;
346 case 2: /* large */
347 {
7dd08bec 348 if (!(ctx->rflags & (RING_BUFFER_RFLAG_FULL_TSC | LTTNG_RFLAG_EXTENDED))) {
3d1fc7fd
MD
349 uint32_t timestamp = (uint32_t) ctx->tsc;
350 uint16_t id = event_id;
351
352 lib_ring_buffer_write(config, ctx, &id, sizeof(id));
1dbfff0c 353 lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint32_t));
3d1fc7fd
MD
354 lib_ring_buffer_write(config, ctx, &timestamp, sizeof(timestamp));
355 } else {
356 uint16_t id = 65535;
357 uint64_t timestamp = ctx->tsc;
358
359 lib_ring_buffer_write(config, ctx, &id, sizeof(id));
360 /* Align extended struct on largest member */
1dbfff0c 361 lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint64_t));
3d1fc7fd 362 lib_ring_buffer_write(config, ctx, &event_id, sizeof(event_id));
1dbfff0c 363 lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint64_t));
3d1fc7fd
MD
364 lib_ring_buffer_write(config, ctx, &timestamp, sizeof(timestamp));
365 }
366 break;
367 }
368 default:
369 WARN_ON_ONCE(1);
370 }
ce7352a2
MD
371 if (lttng_ctx) {
372 /* 2.8+ probe ABI. */
373 ctx_record(ctx, lttng_chan, lttng_ctx->chan_ctx, APP_CTX_ENABLED);
374 ctx_record(ctx, lttng_chan, lttng_ctx->event_ctx, APP_CTX_ENABLED);
375 } else {
376 /* Pre 2.8 probe ABI. */
377 ctx_record(ctx, lttng_chan, lttng_chan->ctx, APP_CTX_DISABLED);
378 ctx_record(ctx, lttng_chan, event->ctx, APP_CTX_DISABLED);
379 }
d3b2c3e0 380 lib_ring_buffer_align_ctx(ctx, ctx->largest_align);
3d1fc7fd
MD
381}
382
4cfec15c 383static const struct lttng_ust_lib_ring_buffer_config client_config;
3d1fc7fd 384
23c8854a 385static uint64_t client_ring_buffer_clock_read(struct channel *chan)
3d1fc7fd
MD
386{
387 return lib_ring_buffer_clock_read(chan);
388}
389
390static
4cfec15c 391size_t client_record_header_size(const struct lttng_ust_lib_ring_buffer_config *config,
3d1fc7fd
MD
392 struct channel *chan, size_t offset,
393 size_t *pre_header_padding,
e56bb47c
MD
394 struct lttng_ust_lib_ring_buffer_ctx *ctx,
395 void *client_ctx)
3d1fc7fd
MD
396{
397 return record_header_size(config, chan, offset,
e56bb47c 398 pre_header_padding, ctx, client_ctx);
3d1fc7fd
MD
399}
400
401/**
402 * client_packet_header_size - called on buffer-switch to a new sub-buffer
403 *
404 * Return header size without padding after the structure. Don't use packed
405 * structure because gcc generates inefficient code on some architectures
406 * (powerpc, mips..)
407 */
408static size_t client_packet_header_size(void)
409{
410 return offsetof(struct packet_header, ctx.header_end);
411}
412
23c8854a 413static void client_buffer_begin(struct lttng_ust_lib_ring_buffer *buf, uint64_t tsc,
1d498196 414 unsigned int subbuf_idx,
38fae1d3 415 struct lttng_ust_shm_handle *handle)
3d1fc7fd 416{
1d498196 417 struct channel *chan = shmp(handle, buf->backend.chan);
3d1fc7fd
MD
418 struct packet_header *header =
419 (struct packet_header *)
420 lib_ring_buffer_offset_address(&buf->backend,
1d498196
MD
421 subbuf_idx * chan->backend.subbuf_size,
422 handle);
7dd08bec 423 struct lttng_channel *lttng_chan = channel_get_private(chan);
1ff31389 424 uint64_t cnt = shmp_index(handle, buf->backend.buf_cnt, subbuf_idx)->seq_cnt;
3d1fc7fd 425
34daae3e
MD
426 assert(header);
427 if (!header)
428 return;
3d1fc7fd 429 header->magic = CTF_MAGIC_NUMBER;
7dd08bec
MD
430 memcpy(header->uuid, lttng_chan->uuid, sizeof(lttng_chan->uuid));
431 header->stream_id = lttng_chan->id;
2d94adc5 432 header->stream_instance_id = buf->backend.cpu;
3d1fc7fd
MD
433 header->ctx.timestamp_begin = tsc;
434 header->ctx.timestamp_end = 0;
0c00a107
MD
435 header->ctx.content_size = ~0ULL; /* for debugging */
436 header->ctx.packet_size = ~0ULL;
1ff31389 437 header->ctx.packet_seq_num = chan->backend.num_subbuf * cnt + subbuf_idx;
3d1fc7fd 438 header->ctx.events_discarded = 0;
3d1fc7fd
MD
439 header->ctx.cpu_id = buf->backend.cpu;
440}
441
442/*
443 * offset is assumed to never be 0 here : never deliver a completely empty
444 * subbuffer. data_size is between 1 and subbuf_size.
445 */
23c8854a 446static void client_buffer_end(struct lttng_ust_lib_ring_buffer *buf, uint64_t tsc,
1d498196 447 unsigned int subbuf_idx, unsigned long data_size,
38fae1d3 448 struct lttng_ust_shm_handle *handle)
3d1fc7fd 449{
1d498196 450 struct channel *chan = shmp(handle, buf->backend.chan);
3d1fc7fd
MD
451 struct packet_header *header =
452 (struct packet_header *)
453 lib_ring_buffer_offset_address(&buf->backend,
1d498196
MD
454 subbuf_idx * chan->backend.subbuf_size,
455 handle);
3d1fc7fd
MD
456 unsigned long records_lost = 0;
457
34daae3e
MD
458 assert(header);
459 if (!header)
460 return;
3d1fc7fd 461 header->ctx.timestamp_end = tsc;
b1830883
MD
462 header->ctx.content_size =
463 (uint64_t) data_size * CHAR_BIT; /* in bits */
464 header->ctx.packet_size =
b72687b8 465 (uint64_t) LTTNG_UST_PAGE_ALIGN(data_size) * CHAR_BIT; /* in bits */
04489ab4
MD
466
467 records_lost += lib_ring_buffer_get_records_lost_full(&client_config, buf);
3d1fc7fd
MD
468 records_lost += lib_ring_buffer_get_records_lost_wrap(&client_config, buf);
469 records_lost += lib_ring_buffer_get_records_lost_big(&client_config, buf);
470 header->ctx.events_discarded = records_lost;
471}
472
4cfec15c 473static int client_buffer_create(struct lttng_ust_lib_ring_buffer *buf, void *priv,
38fae1d3 474 int cpu, const char *name, struct lttng_ust_shm_handle *handle)
3d1fc7fd
MD
475{
476 return 0;
477}
478
4cfec15c 479static void client_buffer_finalize(struct lttng_ust_lib_ring_buffer *buf, void *priv, int cpu, struct lttng_ust_shm_handle *handle)
3d1fc7fd
MD
480{
481}
482
a9ff648c
MD
483static void client_content_size_field(const struct lttng_ust_lib_ring_buffer_config *config,
484 size_t *offset, size_t *length)
485{
486 *offset = offsetof(struct packet_header, ctx.content_size);
487 *length = sizeof(((struct packet_header *) NULL)->ctx.content_size);
488}
489
490static void client_packet_size_field(const struct lttng_ust_lib_ring_buffer_config *config,
491 size_t *offset, size_t *length)
492{
493 *offset = offsetof(struct packet_header, ctx.packet_size);
494 *length = sizeof(((struct packet_header *) NULL)->ctx.packet_size);
495}
496
b2f3252a
JD
497static struct packet_header *client_packet_header(struct lttng_ust_lib_ring_buffer *buf,
498 struct lttng_ust_shm_handle *handle)
499{
c4d6bd10 500 return lib_ring_buffer_read_offset_address(&buf->backend, 0, handle);
b2f3252a
JD
501}
502
503static int client_timestamp_begin(struct lttng_ust_lib_ring_buffer *buf,
504 struct lttng_ust_shm_handle *handle,
505 uint64_t *timestamp_begin)
506{
507 struct packet_header *header;
508
509 header = client_packet_header(buf, handle);
34daae3e
MD
510 if (!header)
511 return -1;
b2f3252a
JD
512 *timestamp_begin = header->ctx.timestamp_begin;
513 return 0;
514}
515
516static int client_timestamp_end(struct lttng_ust_lib_ring_buffer *buf,
517 struct lttng_ust_shm_handle *handle,
518 uint64_t *timestamp_end)
519{
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,
530 struct lttng_ust_shm_handle *handle,
531 uint64_t *events_discarded)
532{
533 struct packet_header *header;
534
535 header = client_packet_header(buf, handle);
34daae3e
MD
536 if (!header)
537 return -1;
b2f3252a
JD
538 *events_discarded = header->ctx.events_discarded;
539 return 0;
540}
541
542static int client_content_size(struct lttng_ust_lib_ring_buffer *buf,
543 struct lttng_ust_shm_handle *handle,
544 uint64_t *content_size)
545{
546 struct packet_header *header;
547
548 header = client_packet_header(buf, handle);
34daae3e
MD
549 if (!header)
550 return -1;
b2f3252a
JD
551 *content_size = header->ctx.content_size;
552 return 0;
553}
554
555static int client_packet_size(struct lttng_ust_lib_ring_buffer *buf,
556 struct lttng_ust_shm_handle *handle,
557 uint64_t *packet_size)
558{
559 struct packet_header *header;
560
561 header = client_packet_header(buf, handle);
34daae3e
MD
562 if (!header)
563 return -1;
b2f3252a
JD
564 *packet_size = header->ctx.packet_size;
565 return 0;
566}
567
568static int client_stream_id(struct lttng_ust_lib_ring_buffer *buf,
569 struct lttng_ust_shm_handle *handle,
570 uint64_t *stream_id)
571{
db95cf8b
MD
572 struct channel *chan = shmp(handle, buf->backend.chan);
573 struct lttng_channel *lttng_chan = channel_get_private(chan);
574
575 *stream_id = lttng_chan->id;
b2f3252a 576
b2f3252a
JD
577 return 0;
578}
579
fca361e8
JD
580static int client_current_timestamp(struct lttng_ust_lib_ring_buffer *buf,
581 struct lttng_ust_shm_handle *handle,
582 uint64_t *ts)
583{
584 struct channel *chan;
585
586 chan = shmp(handle, handle->chan);
587 *ts = client_ring_buffer_clock_read(chan);
588
589 return 0;
590}
591
1ff31389
JD
592static int client_sequence_number(struct lttng_ust_lib_ring_buffer *buf,
593 struct lttng_ust_shm_handle *handle,
594 uint64_t *seq)
595{
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
JD
605static int client_instance_id(struct lttng_ust_lib_ring_buffer *buf,
606 struct lttng_ust_shm_handle *handle,
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
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.077789 seconds and 4 git commands to generate.