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