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