6ed067105d8e7fd0f8a280e3f09caa0375186836
[lttng-ust.git] / liblttng-ust / lttng-ring-buffer-client.h
1 /*
2 * lttng-ring-buffer-client.h
3 *
4 * LTTng lib ring buffer client template.
5 *
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
21 */
22
23 #include <stdint.h>
24 #include <lttng/ust-events.h>
25 #include "lttng/bitfield.h"
26 #include "clock.h"
27 #include "lttng-tracer.h"
28 #include "../libringbuffer/frontend_types.h"
29
30 #define LTTNG_COMPACT_EVENT_BITS 5
31 #define LTTNG_COMPACT_TSC_BITS 27
32
33 enum app_ctx_mode {
34 APP_CTX_DISABLED,
35 APP_CTX_ENABLED,
36 };
37
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
45 struct packet_header {
46 /* Trace packet header */
47 uint32_t magic; /*
48 * Trace magic number.
49 * contains endianness information.
50 */
51 uint8_t uuid[LTTNG_UST_UUID_LEN];
52 uint32_t stream_id;
53 uint64_t stream_instance_id;
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 */
59 uint64_t content_size; /* Size of data in subbuffer */
60 uint64_t packet_size; /* Subbuffer size (include padding) */
61 uint64_t packet_seq_num; /* Packet sequence number */
62 unsigned long events_discarded; /*
63 * Events lost in this subbuffer since
64 * the beginning of the trace.
65 * (may overflow)
66 */
67 uint32_t cpu_id; /* CPU id associated with stream */
68 uint8_t header_end; /* End of header */
69 } ctx;
70 };
71
72
73 static inline uint64_t lib_ring_buffer_clock_read(struct channel *chan)
74 {
75 return trace_clock_read64();
76 }
77
78 static inline
79 size_t ctx_get_size(size_t offset, struct lttng_ctx *ctx,
80 enum app_ctx_mode mode)
81 {
82 int i;
83 size_t orig_offset = offset;
84
85 if (caa_likely(!ctx))
86 return 0;
87 offset += lib_ring_buffer_align(offset, ctx->largest_align);
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 }
109 return offset - orig_offset;
110 }
111
112 static inline
113 void ctx_record(struct lttng_ust_lib_ring_buffer_ctx *bufctx,
114 struct lttng_channel *chan,
115 struct lttng_ctx *ctx,
116 enum app_ctx_mode mode)
117 {
118 int i;
119
120 if (caa_likely(!ctx))
121 return;
122 lib_ring_buffer_align_ctx(bufctx, ctx->largest_align);
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 }
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 */
159 static __inline__
160 size_t record_header_size(const struct lttng_ust_lib_ring_buffer_config *config,
161 struct channel *chan, size_t offset,
162 size_t *pre_header_padding,
163 struct lttng_ust_lib_ring_buffer_ctx *ctx)
164 {
165 struct lttng_channel *lttng_chan = channel_get_private(chan);
166 struct lttng_event *event = ctx->priv;
167 struct lttng_stack_ctx *lttng_ctx = ctx->priv2;
168 size_t orig_offset = offset;
169 size_t padding;
170
171 switch (lttng_chan->header_type) {
172 case 1: /* compact */
173 padding = lib_ring_buffer_align(offset, lttng_alignof(uint32_t));
174 offset += padding;
175 if (!(ctx->rflags & (RING_BUFFER_RFLAG_FULL_TSC | LTTNG_RFLAG_EXTENDED))) {
176 offset += sizeof(uint32_t); /* id and timestamp */
177 } else {
178 /* Minimum space taken by LTTNG_COMPACT_EVENT_BITS id */
179 offset += (LTTNG_COMPACT_EVENT_BITS + CHAR_BIT - 1) / CHAR_BIT;
180 /* Align extended struct on largest member */
181 offset += lib_ring_buffer_align(offset, lttng_alignof(uint64_t));
182 offset += sizeof(uint32_t); /* id */
183 offset += lib_ring_buffer_align(offset, lttng_alignof(uint64_t));
184 offset += sizeof(uint64_t); /* timestamp */
185 }
186 break;
187 case 2: /* large */
188 padding = lib_ring_buffer_align(offset, lttng_alignof(uint16_t));
189 offset += padding;
190 offset += sizeof(uint16_t);
191 if (!(ctx->rflags & (RING_BUFFER_RFLAG_FULL_TSC | LTTNG_RFLAG_EXTENDED))) {
192 offset += lib_ring_buffer_align(offset, lttng_alignof(uint32_t));
193 offset += sizeof(uint32_t); /* timestamp */
194 } else {
195 /* Align extended struct on largest member */
196 offset += lib_ring_buffer_align(offset, lttng_alignof(uint64_t));
197 offset += sizeof(uint32_t); /* id */
198 offset += lib_ring_buffer_align(offset, lttng_alignof(uint64_t));
199 offset += sizeof(uint64_t); /* timestamp */
200 }
201 break;
202 default:
203 padding = 0;
204 WARN_ON_ONCE(1);
205 }
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 }
215 *pre_header_padding = padding;
216 return offset - orig_offset;
217 }
218
219 #include "../libringbuffer/api.h"
220 #include "lttng-rb-clients.h"
221
222 static
223 void lttng_write_event_header_slow(const struct lttng_ust_lib_ring_buffer_config *config,
224 struct lttng_ust_lib_ring_buffer_ctx *ctx,
225 uint32_t event_id);
226
227 /*
228 * lttng_write_event_header
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 */
236 static __inline__
237 void lttng_write_event_header(const struct lttng_ust_lib_ring_buffer_config *config,
238 struct lttng_ust_lib_ring_buffer_ctx *ctx,
239 uint32_t event_id)
240 {
241 struct lttng_channel *lttng_chan = channel_get_private(ctx->chan);
242 struct lttng_event *event = ctx->priv;
243 struct lttng_stack_ctx *lttng_ctx = ctx->priv2;
244
245 if (caa_unlikely(ctx->rflags))
246 goto slow_path;
247
248 switch (lttng_chan->header_type) {
249 case 1: /* compact */
250 {
251 uint32_t id_time = 0;
252
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);
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));
270 lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint32_t));
271 lib_ring_buffer_write(config, ctx, &timestamp, sizeof(timestamp));
272 break;
273 }
274 default:
275 WARN_ON_ONCE(1);
276 }
277
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 }
287 lib_ring_buffer_align_ctx(ctx, ctx->largest_align);
288
289 return;
290
291 slow_path:
292 lttng_write_event_header_slow(config, ctx, event_id);
293 }
294
295 static
296 void lttng_write_event_header_slow(const struct lttng_ust_lib_ring_buffer_config *config,
297 struct lttng_ust_lib_ring_buffer_ctx *ctx,
298 uint32_t event_id)
299 {
300 struct lttng_channel *lttng_chan = channel_get_private(ctx->chan);
301 struct lttng_event *event = ctx->priv;
302 struct lttng_stack_ctx *lttng_ctx = ctx->priv2;
303
304 switch (lttng_chan->header_type) {
305 case 1: /* compact */
306 if (!(ctx->rflags & (RING_BUFFER_RFLAG_FULL_TSC | LTTNG_RFLAG_EXTENDED))) {
307 uint32_t id_time = 0;
308
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);
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
322 bt_bitfield_write(&id, uint8_t,
323 0,
324 LTTNG_COMPACT_EVENT_BITS,
325 31);
326 lib_ring_buffer_write(config, ctx, &id, sizeof(id));
327 /* Align extended struct on largest member */
328 lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint64_t));
329 lib_ring_buffer_write(config, ctx, &event_id, sizeof(event_id));
330 lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint64_t));
331 lib_ring_buffer_write(config, ctx, &timestamp, sizeof(timestamp));
332 }
333 break;
334 case 2: /* large */
335 {
336 if (!(ctx->rflags & (RING_BUFFER_RFLAG_FULL_TSC | LTTNG_RFLAG_EXTENDED))) {
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));
341 lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint32_t));
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 */
349 lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint64_t));
350 lib_ring_buffer_write(config, ctx, &event_id, sizeof(event_id));
351 lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint64_t));
352 lib_ring_buffer_write(config, ctx, &timestamp, sizeof(timestamp));
353 }
354 break;
355 }
356 default:
357 WARN_ON_ONCE(1);
358 }
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 }
368 lib_ring_buffer_align_ctx(ctx, ctx->largest_align);
369 }
370
371 static const struct lttng_ust_lib_ring_buffer_config client_config;
372
373 static uint64_t client_ring_buffer_clock_read(struct channel *chan)
374 {
375 return lib_ring_buffer_clock_read(chan);
376 }
377
378 static
379 size_t client_record_header_size(const struct lttng_ust_lib_ring_buffer_config *config,
380 struct channel *chan, size_t offset,
381 size_t *pre_header_padding,
382 struct lttng_ust_lib_ring_buffer_ctx *ctx)
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 */
395 static size_t client_packet_header_size(void)
396 {
397 return offsetof(struct packet_header, ctx.header_end);
398 }
399
400 static void client_buffer_begin(struct lttng_ust_lib_ring_buffer *buf, uint64_t tsc,
401 unsigned int subbuf_idx,
402 struct lttng_ust_shm_handle *handle)
403 {
404 struct channel *chan = shmp(handle, buf->backend.chan);
405 struct packet_header *header =
406 (struct packet_header *)
407 lib_ring_buffer_offset_address(&buf->backend,
408 subbuf_idx * chan->backend.subbuf_size,
409 handle);
410 struct lttng_channel *lttng_chan = channel_get_private(chan);
411 uint64_t cnt = shmp_index(handle, buf->backend.buf_cnt, subbuf_idx)->seq_cnt;
412
413 assert(header);
414 if (!header)
415 return;
416 header->magic = CTF_MAGIC_NUMBER;
417 memcpy(header->uuid, lttng_chan->uuid, sizeof(lttng_chan->uuid));
418 header->stream_id = lttng_chan->id;
419 header->stream_instance_id = buf->backend.cpu;
420 header->ctx.timestamp_begin = tsc;
421 header->ctx.timestamp_end = 0;
422 header->ctx.content_size = ~0ULL; /* for debugging */
423 header->ctx.packet_size = ~0ULL;
424 header->ctx.packet_seq_num = chan->backend.num_subbuf * cnt + subbuf_idx;
425 header->ctx.events_discarded = 0;
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 */
433 static void client_buffer_end(struct lttng_ust_lib_ring_buffer *buf, uint64_t tsc,
434 unsigned int subbuf_idx, unsigned long data_size,
435 struct lttng_ust_shm_handle *handle)
436 {
437 struct channel *chan = shmp(handle, buf->backend.chan);
438 struct packet_header *header =
439 (struct packet_header *)
440 lib_ring_buffer_offset_address(&buf->backend,
441 subbuf_idx * chan->backend.subbuf_size,
442 handle);
443 unsigned long records_lost = 0;
444
445 assert(header);
446 if (!header)
447 return;
448 header->ctx.timestamp_end = tsc;
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 */
453
454 records_lost += lib_ring_buffer_get_records_lost_full(&client_config, buf);
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
460 static int client_buffer_create(struct lttng_ust_lib_ring_buffer *buf, void *priv,
461 int cpu, const char *name, struct lttng_ust_shm_handle *handle)
462 {
463 return 0;
464 }
465
466 static void client_buffer_finalize(struct lttng_ust_lib_ring_buffer *buf, void *priv, int cpu, struct lttng_ust_shm_handle *handle)
467 {
468 }
469
470 static 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
477 static 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
484 static struct packet_header *client_packet_header(struct lttng_ust_lib_ring_buffer *buf,
485 struct lttng_ust_shm_handle *handle)
486 {
487 return lib_ring_buffer_read_offset_address(&buf->backend, 0, handle);
488 }
489
490 static 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);
497 if (!header)
498 return -1;
499 *timestamp_begin = header->ctx.timestamp_begin;
500 return 0;
501 }
502
503 static 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);
510 if (!header)
511 return -1;
512 *timestamp_end = header->ctx.timestamp_end;
513 return 0;
514 }
515
516 static 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);
523 if (!header)
524 return -1;
525 *events_discarded = header->ctx.events_discarded;
526 return 0;
527 }
528
529 static 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);
536 if (!header)
537 return -1;
538 *content_size = header->ctx.content_size;
539 return 0;
540 }
541
542 static 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);
549 if (!header)
550 return -1;
551 *packet_size = header->ctx.packet_size;
552 return 0;
553 }
554
555 static int client_stream_id(struct lttng_ust_lib_ring_buffer *buf,
556 struct lttng_ust_shm_handle *handle,
557 uint64_t *stream_id)
558 {
559 struct packet_header *header;
560
561 header = client_packet_header(buf, handle);
562 if (!header)
563 return -1;
564 *stream_id = header->stream_id;
565 return 0;
566 }
567
568 static int client_current_timestamp(struct lttng_ust_lib_ring_buffer *buf,
569 struct lttng_ust_shm_handle *handle,
570 uint64_t *ts)
571 {
572 struct channel *chan;
573
574 chan = shmp(handle, handle->chan);
575 *ts = client_ring_buffer_clock_read(chan);
576
577 return 0;
578 }
579
580 static int client_sequence_number(struct lttng_ust_lib_ring_buffer *buf,
581 struct lttng_ust_shm_handle *handle,
582 uint64_t *seq)
583 {
584 struct packet_header *header;
585
586 header = client_packet_header(buf, handle);
587 *seq = header->ctx.packet_seq_num;
588 return 0;
589 }
590
591 static int client_instance_id(struct lttng_ust_lib_ring_buffer *buf,
592 struct lttng_ust_shm_handle *handle,
593 uint64_t *id)
594 {
595 struct packet_header *header;
596
597 header = client_packet_header(buf, handle);
598 *id = header->stream_instance_id;
599 return 0;
600 }
601
602 static const
603 struct lttng_ust_client_lib_ring_buffer_client_cb client_cb = {
604 .parent = {
605 .ring_buffer_clock_read = client_ring_buffer_clock_read,
606 .record_header_size = client_record_header_size,
607 .subbuffer_header_size = client_packet_header_size,
608 .buffer_begin = client_buffer_begin,
609 .buffer_end = client_buffer_end,
610 .buffer_create = client_buffer_create,
611 .buffer_finalize = client_buffer_finalize,
612 .content_size_field = client_content_size_field,
613 .packet_size_field = client_packet_size_field,
614 },
615 .timestamp_begin = client_timestamp_begin,
616 .timestamp_end = client_timestamp_end,
617 .events_discarded = client_events_discarded,
618 .content_size = client_content_size,
619 .packet_size = client_packet_size,
620 .stream_id = client_stream_id,
621 .current_timestamp = client_current_timestamp,
622 .sequence_number = client_sequence_number,
623 .instance_id = client_instance_id,
624 };
625
626 static const struct lttng_ust_lib_ring_buffer_config client_config = {
627 .cb.ring_buffer_clock_read = client_ring_buffer_clock_read,
628 .cb.record_header_size = client_record_header_size,
629 .cb.subbuffer_header_size = client_packet_header_size,
630 .cb.buffer_begin = client_buffer_begin,
631 .cb.buffer_end = client_buffer_end,
632 .cb.buffer_create = client_buffer_create,
633 .cb.buffer_finalize = client_buffer_finalize,
634 .cb.content_size_field = client_content_size_field,
635 .cb.packet_size_field = client_packet_size_field,
636
637 .tsc_bits = LTTNG_COMPACT_TSC_BITS,
638 .alloc = RING_BUFFER_ALLOC_PER_CPU,
639 .sync = RING_BUFFER_SYNC_GLOBAL,
640 .mode = RING_BUFFER_MODE_TEMPLATE,
641 .backend = RING_BUFFER_PAGE,
642 .output = RING_BUFFER_MMAP,
643 .oops = RING_BUFFER_OOPS_CONSISTENCY,
644 .ipi = RING_BUFFER_NO_IPI_BARRIER,
645 .wakeup = LTTNG_CLIENT_WAKEUP,
646 .client_type = LTTNG_CLIENT_TYPE,
647
648 .cb_ptr = &client_cb.parent,
649 };
650
651 const struct lttng_ust_client_lib_ring_buffer_client_cb *LTTNG_CLIENT_CALLBACKS = &client_cb;
652
653 static
654 struct lttng_channel *_channel_create(const char *name,
655 void *buf_addr,
656 size_t subbuf_size, size_t num_subbuf,
657 unsigned int switch_timer_interval,
658 unsigned int read_timer_interval,
659 unsigned char *uuid,
660 uint32_t chan_id,
661 const int *stream_fds, int nr_stream_fds)
662 {
663 struct lttng_channel chan_priv_init;
664 struct lttng_ust_shm_handle *handle;
665 struct lttng_channel *lttng_chan;
666 void *priv;
667
668 memset(&chan_priv_init, 0, sizeof(chan_priv_init));
669 memcpy(chan_priv_init.uuid, uuid, LTTNG_UST_UUID_LEN);
670 chan_priv_init.id = chan_id;
671 handle = channel_create(&client_config, name,
672 &priv, __alignof__(struct lttng_channel),
673 sizeof(struct lttng_channel),
674 &chan_priv_init,
675 buf_addr, subbuf_size, num_subbuf,
676 switch_timer_interval, read_timer_interval,
677 stream_fds, nr_stream_fds);
678 if (!handle)
679 return NULL;
680 lttng_chan = priv;
681 lttng_chan->handle = handle;
682 lttng_chan->chan = shmp(handle, handle->chan);
683 return lttng_chan;
684 }
685
686 static
687 void lttng_channel_destroy(struct lttng_channel *chan)
688 {
689 channel_destroy(chan->chan, chan->handle, 1);
690 }
691
692 static
693 int lttng_event_reserve(struct lttng_ust_lib_ring_buffer_ctx *ctx,
694 uint32_t event_id)
695 {
696 struct lttng_channel *lttng_chan = channel_get_private(ctx->chan);
697 int ret, cpu;
698
699 cpu = lib_ring_buffer_get_cpu(&client_config);
700 if (cpu < 0)
701 return -EPERM;
702 ctx->cpu = cpu;
703
704 switch (lttng_chan->header_type) {
705 case 1: /* compact */
706 if (event_id > 30)
707 ctx->rflags |= LTTNG_RFLAG_EXTENDED;
708 break;
709 case 2: /* large */
710 if (event_id > 65534)
711 ctx->rflags |= LTTNG_RFLAG_EXTENDED;
712 break;
713 default:
714 WARN_ON_ONCE(1);
715 }
716
717 ret = lib_ring_buffer_reserve(&client_config, ctx);
718 if (caa_unlikely(ret))
719 goto put;
720 if (caa_likely(ctx->ctx_len
721 >= sizeof(struct lttng_ust_lib_ring_buffer_ctx))) {
722 if (lib_ring_buffer_backend_get_pages(&client_config, ctx,
723 &ctx->backend_pages)) {
724 ret = -EPERM;
725 goto put;
726 }
727 }
728 lttng_write_event_header(&client_config, ctx, event_id);
729 return 0;
730 put:
731 lib_ring_buffer_put_cpu(&client_config);
732 return ret;
733 }
734
735 static
736 void lttng_event_commit(struct lttng_ust_lib_ring_buffer_ctx *ctx)
737 {
738 lib_ring_buffer_commit(&client_config, ctx);
739 lib_ring_buffer_put_cpu(&client_config);
740 }
741
742 static
743 void lttng_event_write(struct lttng_ust_lib_ring_buffer_ctx *ctx, const void *src,
744 size_t len)
745 {
746 lib_ring_buffer_write(&client_config, ctx, src, len);
747 }
748
749 static
750 void lttng_event_strcpy(struct lttng_ust_lib_ring_buffer_ctx *ctx, const char *src,
751 size_t len)
752 {
753 lib_ring_buffer_strcpy(&client_config, ctx, src, len, '#');
754 }
755
756 #if 0
757 static
758 wait_queue_head_t *lttng_get_reader_wait_queue(struct channel *chan)
759 {
760 return &chan->read_wait;
761 }
762
763 static
764 wait_queue_head_t *lttng_get_hp_wait_queue(struct channel *chan)
765 {
766 return &chan->hp_wait;
767 }
768 #endif //0
769
770 static
771 int lttng_is_finalized(struct channel *chan)
772 {
773 return lib_ring_buffer_channel_is_finalized(chan);
774 }
775
776 static
777 int lttng_is_disabled(struct channel *chan)
778 {
779 return lib_ring_buffer_channel_is_disabled(chan);
780 }
781
782 static
783 int lttng_flush_buffer(struct channel *chan, struct lttng_ust_shm_handle *handle)
784 {
785 struct lttng_ust_lib_ring_buffer *buf;
786 int cpu;
787
788 for_each_channel_cpu(cpu, chan) {
789 int shm_fd, wait_fd, wakeup_fd;
790 uint64_t memory_map_size;
791
792 buf = channel_get_ring_buffer(&client_config, chan,
793 cpu, handle, &shm_fd, &wait_fd,
794 &wakeup_fd, &memory_map_size);
795 lib_ring_buffer_switch(&client_config, buf,
796 SWITCH_ACTIVE, handle);
797 }
798 return 0;
799 }
800
801 static struct lttng_transport lttng_relay_transport = {
802 .name = "relay-" RING_BUFFER_MODE_TEMPLATE_STRING "-mmap",
803 .ops = {
804 .channel_create = _channel_create,
805 .channel_destroy = lttng_channel_destroy,
806 .u.has_strcpy = 1,
807 .event_reserve = lttng_event_reserve,
808 .event_commit = lttng_event_commit,
809 .event_write = lttng_event_write,
810 .packet_avail_size = NULL, /* Would be racy anyway */
811 //.get_reader_wait_queue = lttng_get_reader_wait_queue,
812 //.get_hp_wait_queue = lttng_get_hp_wait_queue,
813 .is_finalized = lttng_is_finalized,
814 .is_disabled = lttng_is_disabled,
815 .flush_buffer = lttng_flush_buffer,
816 .event_strcpy = lttng_event_strcpy,
817 },
818 .client_config = &client_config,
819 };
820
821 void RING_BUFFER_MODE_TEMPLATE_INIT(void)
822 {
823 DBG("LTT : ltt ring buffer client \"%s\" init\n",
824 "relay-" RING_BUFFER_MODE_TEMPLATE_STRING "-mmap");
825 lttng_transport_register(&lttng_relay_transport);
826 }
827
828 void RING_BUFFER_MODE_TEMPLATE_EXIT(void)
829 {
830 DBG("LTT : ltt ring buffer client \"%s\" exit\n",
831 "relay-" RING_BUFFER_MODE_TEMPLATE_STRING "-mmap");
832 lttng_transport_unregister(&lttng_relay_transport);
833 }
This page took 0.05184 seconds and 3 git commands to generate.