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