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