License text standardization, add missing licenses
[lttng-ust.git] / liblttng-ust / ltt-ring-buffer-client.h
1 /*
2 * ltt-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 "ltt-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 /*
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
40 struct packet_header {
41 /* Trace packet header */
42 uint32_t magic; /*
43 * Trace magic number.
44 * contains endianness information.
45 */
46 uint8_t uuid[16];
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 */
53 uint32_t events_discarded; /*
54 * Events lost in this subbuffer since
55 * the beginning of the trace.
56 * (may overflow)
57 */
58 uint32_t content_size; /* Size of data in subbuffer */
59 uint32_t packet_size; /* Subbuffer size (include padding) */
60 uint32_t cpu_id; /* CPU id associated with stream */
61 uint8_t header_end; /* End of header */
62 } ctx;
63 };
64
65
66 static inline uint64_t lib_ring_buffer_clock_read(struct channel *chan)
67 {
68 return trace_clock_read64();
69 }
70
71 static inline
72 size_t ctx_get_size(size_t offset, struct lttng_ctx *ctx)
73 {
74 int i;
75 size_t orig_offset = offset;
76
77 if (caa_likely(!ctx))
78 return 0;
79 for (i = 0; i < ctx->nr_fields; i++)
80 offset += ctx->fields[i].get_size(offset);
81 return offset - orig_offset;
82 }
83
84 static inline
85 void ctx_record(struct lttng_ust_lib_ring_buffer_ctx *bufctx,
86 struct ltt_channel *chan,
87 struct lttng_ctx *ctx)
88 {
89 int i;
90
91 if (caa_likely(!ctx))
92 return;
93 for (i = 0; i < ctx->nr_fields; i++)
94 ctx->fields[i].record(&ctx->fields[i], bufctx, chan);
95 }
96
97 /*
98 * record_header_size - Calculate the header size and padding necessary.
99 * @config: ring buffer instance configuration
100 * @chan: channel
101 * @offset: offset in the write buffer
102 * @pre_header_padding: padding to add before the header (output)
103 * @ctx: reservation context
104 *
105 * Returns the event header size (including padding).
106 *
107 * The payload must itself determine its own alignment from the biggest type it
108 * contains.
109 */
110 static __inline__
111 unsigned char record_header_size(const struct lttng_ust_lib_ring_buffer_config *config,
112 struct channel *chan, size_t offset,
113 size_t *pre_header_padding,
114 struct lttng_ust_lib_ring_buffer_ctx *ctx)
115 {
116 struct ltt_channel *ltt_chan = channel_get_private(chan);
117 struct ltt_event *event = ctx->priv;
118 size_t orig_offset = offset;
119 size_t padding;
120
121 switch (ltt_chan->header_type) {
122 case 1: /* compact */
123 padding = lib_ring_buffer_align(offset, lttng_alignof(uint32_t));
124 offset += padding;
125 if (!(ctx->rflags & (RING_BUFFER_RFLAG_FULL_TSC | LTT_RFLAG_EXTENDED))) {
126 offset += sizeof(uint32_t); /* id and timestamp */
127 } else {
128 /* Minimum space taken by LTTNG_COMPACT_EVENT_BITS id */
129 offset += (LTTNG_COMPACT_EVENT_BITS + CHAR_BIT - 1) / CHAR_BIT;
130 /* Align extended struct on largest member */
131 offset += lib_ring_buffer_align(offset, lttng_alignof(uint64_t));
132 offset += sizeof(uint32_t); /* id */
133 offset += lib_ring_buffer_align(offset, lttng_alignof(uint64_t));
134 offset += sizeof(uint64_t); /* timestamp */
135 }
136 break;
137 case 2: /* large */
138 padding = lib_ring_buffer_align(offset, lttng_alignof(uint16_t));
139 offset += padding;
140 offset += sizeof(uint16_t);
141 if (!(ctx->rflags & (RING_BUFFER_RFLAG_FULL_TSC | LTT_RFLAG_EXTENDED))) {
142 offset += lib_ring_buffer_align(offset, lttng_alignof(uint32_t));
143 offset += sizeof(uint32_t); /* timestamp */
144 } else {
145 /* Align extended struct on largest member */
146 offset += lib_ring_buffer_align(offset, lttng_alignof(uint64_t));
147 offset += sizeof(uint32_t); /* id */
148 offset += lib_ring_buffer_align(offset, lttng_alignof(uint64_t));
149 offset += sizeof(uint64_t); /* timestamp */
150 }
151 break;
152 default:
153 padding = 0;
154 WARN_ON_ONCE(1);
155 }
156 offset += ctx_get_size(offset, event->ctx);
157 offset += ctx_get_size(offset, ltt_chan->ctx);
158
159 *pre_header_padding = padding;
160 return offset - orig_offset;
161 }
162
163 #include "../libringbuffer/api.h"
164
165 static
166 void ltt_write_event_header_slow(const struct lttng_ust_lib_ring_buffer_config *config,
167 struct lttng_ust_lib_ring_buffer_ctx *ctx,
168 uint32_t event_id);
169
170 /*
171 * ltt_write_event_header
172 *
173 * Writes the event header to the offset (already aligned on 32-bits).
174 *
175 * @config: ring buffer instance configuration
176 * @ctx: reservation context
177 * @event_id: event ID
178 */
179 static __inline__
180 void ltt_write_event_header(const struct lttng_ust_lib_ring_buffer_config *config,
181 struct lttng_ust_lib_ring_buffer_ctx *ctx,
182 uint32_t event_id)
183 {
184 struct ltt_channel *ltt_chan = channel_get_private(ctx->chan);
185 struct ltt_event *event = ctx->priv;
186
187 if (caa_unlikely(ctx->rflags))
188 goto slow_path;
189
190 switch (ltt_chan->header_type) {
191 case 1: /* compact */
192 {
193 uint32_t id_time = 0;
194
195 bt_bitfield_write(&id_time, uint32_t,
196 0,
197 LTTNG_COMPACT_EVENT_BITS,
198 event_id);
199 bt_bitfield_write(&id_time, uint32_t,
200 LTTNG_COMPACT_EVENT_BITS,
201 LTTNG_COMPACT_TSC_BITS,
202 ctx->tsc);
203 lib_ring_buffer_write(config, ctx, &id_time, sizeof(id_time));
204 break;
205 }
206 case 2: /* large */
207 {
208 uint32_t timestamp = (uint32_t) ctx->tsc;
209 uint16_t id = event_id;
210
211 lib_ring_buffer_write(config, ctx, &id, sizeof(id));
212 lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint32_t));
213 lib_ring_buffer_write(config, ctx, &timestamp, sizeof(timestamp));
214 break;
215 }
216 default:
217 WARN_ON_ONCE(1);
218 }
219
220 ctx_record(ctx, ltt_chan, ltt_chan->ctx);
221 ctx_record(ctx, ltt_chan, event->ctx);
222 lib_ring_buffer_align_ctx(ctx, ctx->largest_align);
223
224 return;
225
226 slow_path:
227 ltt_write_event_header_slow(config, ctx, event_id);
228 }
229
230 static
231 void ltt_write_event_header_slow(const struct lttng_ust_lib_ring_buffer_config *config,
232 struct lttng_ust_lib_ring_buffer_ctx *ctx,
233 uint32_t event_id)
234 {
235 struct ltt_channel *ltt_chan = channel_get_private(ctx->chan);
236 struct ltt_event *event = ctx->priv;
237
238 switch (ltt_chan->header_type) {
239 case 1: /* compact */
240 if (!(ctx->rflags & (RING_BUFFER_RFLAG_FULL_TSC | LTT_RFLAG_EXTENDED))) {
241 uint32_t id_time = 0;
242
243 bt_bitfield_write(&id_time, uint32_t,
244 0,
245 LTTNG_COMPACT_EVENT_BITS,
246 event_id);
247 bt_bitfield_write(&id_time, uint32_t,
248 LTTNG_COMPACT_EVENT_BITS,
249 LTTNG_COMPACT_TSC_BITS,
250 ctx->tsc);
251 lib_ring_buffer_write(config, ctx, &id_time, sizeof(id_time));
252 } else {
253 uint8_t id = 0;
254 uint64_t timestamp = ctx->tsc;
255
256 bt_bitfield_write(&id, uint8_t,
257 0,
258 LTTNG_COMPACT_EVENT_BITS,
259 31);
260 lib_ring_buffer_write(config, ctx, &id, sizeof(id));
261 /* Align extended struct on largest member */
262 lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint64_t));
263 lib_ring_buffer_write(config, ctx, &event_id, sizeof(event_id));
264 lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint64_t));
265 lib_ring_buffer_write(config, ctx, &timestamp, sizeof(timestamp));
266 }
267 break;
268 case 2: /* large */
269 {
270 if (!(ctx->rflags & (RING_BUFFER_RFLAG_FULL_TSC | LTT_RFLAG_EXTENDED))) {
271 uint32_t timestamp = (uint32_t) ctx->tsc;
272 uint16_t id = event_id;
273
274 lib_ring_buffer_write(config, ctx, &id, sizeof(id));
275 lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint32_t));
276 lib_ring_buffer_write(config, ctx, &timestamp, sizeof(timestamp));
277 } else {
278 uint16_t id = 65535;
279 uint64_t timestamp = ctx->tsc;
280
281 lib_ring_buffer_write(config, ctx, &id, sizeof(id));
282 /* Align extended struct on largest member */
283 lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint64_t));
284 lib_ring_buffer_write(config, ctx, &event_id, sizeof(event_id));
285 lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint64_t));
286 lib_ring_buffer_write(config, ctx, &timestamp, sizeof(timestamp));
287 }
288 break;
289 }
290 default:
291 WARN_ON_ONCE(1);
292 }
293 ctx_record(ctx, ltt_chan, ltt_chan->ctx);
294 ctx_record(ctx, ltt_chan, event->ctx);
295 lib_ring_buffer_align_ctx(ctx, ctx->largest_align);
296 }
297
298 static const struct lttng_ust_lib_ring_buffer_config client_config;
299
300 static uint64_t client_ring_buffer_clock_read(struct channel *chan)
301 {
302 return lib_ring_buffer_clock_read(chan);
303 }
304
305 static
306 size_t client_record_header_size(const struct lttng_ust_lib_ring_buffer_config *config,
307 struct channel *chan, size_t offset,
308 size_t *pre_header_padding,
309 struct lttng_ust_lib_ring_buffer_ctx *ctx)
310 {
311 return record_header_size(config, chan, offset,
312 pre_header_padding, ctx);
313 }
314
315 /**
316 * client_packet_header_size - called on buffer-switch to a new sub-buffer
317 *
318 * Return header size without padding after the structure. Don't use packed
319 * structure because gcc generates inefficient code on some architectures
320 * (powerpc, mips..)
321 */
322 static size_t client_packet_header_size(void)
323 {
324 return offsetof(struct packet_header, ctx.header_end);
325 }
326
327 static void client_buffer_begin(struct lttng_ust_lib_ring_buffer *buf, uint64_t tsc,
328 unsigned int subbuf_idx,
329 struct lttng_ust_shm_handle *handle)
330 {
331 struct channel *chan = shmp(handle, buf->backend.chan);
332 struct packet_header *header =
333 (struct packet_header *)
334 lib_ring_buffer_offset_address(&buf->backend,
335 subbuf_idx * chan->backend.subbuf_size,
336 handle);
337 struct ltt_channel *ltt_chan = channel_get_private(chan);
338
339 header->magic = CTF_MAGIC_NUMBER;
340 memcpy(header->uuid, ltt_chan->uuid, sizeof(ltt_chan->uuid));
341 header->stream_id = ltt_chan->id;
342 header->ctx.timestamp_begin = tsc;
343 header->ctx.timestamp_end = 0;
344 header->ctx.events_discarded = 0;
345 header->ctx.content_size = 0xFFFFFFFF; /* for debugging */
346 header->ctx.packet_size = 0xFFFFFFFF;
347 header->ctx.cpu_id = buf->backend.cpu;
348 }
349
350 /*
351 * offset is assumed to never be 0 here : never deliver a completely empty
352 * subbuffer. data_size is between 1 and subbuf_size.
353 */
354 static void client_buffer_end(struct lttng_ust_lib_ring_buffer *buf, uint64_t tsc,
355 unsigned int subbuf_idx, unsigned long data_size,
356 struct lttng_ust_shm_handle *handle)
357 {
358 struct channel *chan = shmp(handle, buf->backend.chan);
359 struct packet_header *header =
360 (struct packet_header *)
361 lib_ring_buffer_offset_address(&buf->backend,
362 subbuf_idx * chan->backend.subbuf_size,
363 handle);
364 unsigned long records_lost = 0;
365
366 header->ctx.timestamp_end = tsc;
367 header->ctx.content_size = data_size * CHAR_BIT; /* in bits */
368 header->ctx.packet_size = PAGE_ALIGN(data_size) * CHAR_BIT; /* in bits */
369 /*
370 * We do not care about the records lost count, because the metadata
371 * channel waits and retry.
372 */
373 (void) lib_ring_buffer_get_records_lost_full(&client_config, buf);
374 records_lost += lib_ring_buffer_get_records_lost_wrap(&client_config, buf);
375 records_lost += lib_ring_buffer_get_records_lost_big(&client_config, buf);
376 header->ctx.events_discarded = records_lost;
377 }
378
379 static int client_buffer_create(struct lttng_ust_lib_ring_buffer *buf, void *priv,
380 int cpu, const char *name, struct lttng_ust_shm_handle *handle)
381 {
382 return 0;
383 }
384
385 static void client_buffer_finalize(struct lttng_ust_lib_ring_buffer *buf, void *priv, int cpu, struct lttng_ust_shm_handle *handle)
386 {
387 }
388
389 static const struct lttng_ust_lib_ring_buffer_config client_config = {
390 .cb.ring_buffer_clock_read = client_ring_buffer_clock_read,
391 .cb.record_header_size = client_record_header_size,
392 .cb.subbuffer_header_size = client_packet_header_size,
393 .cb.buffer_begin = client_buffer_begin,
394 .cb.buffer_end = client_buffer_end,
395 .cb.buffer_create = client_buffer_create,
396 .cb.buffer_finalize = client_buffer_finalize,
397
398 .tsc_bits = LTTNG_COMPACT_TSC_BITS,
399 .alloc = RING_BUFFER_ALLOC_PER_CPU,
400 .sync = RING_BUFFER_SYNC_GLOBAL,
401 .mode = RING_BUFFER_MODE_TEMPLATE,
402 .backend = RING_BUFFER_PAGE,
403 .output = RING_BUFFER_MMAP,
404 .oops = RING_BUFFER_OOPS_CONSISTENCY,
405 .ipi = RING_BUFFER_NO_IPI_BARRIER,
406 .wakeup = RING_BUFFER_WAKEUP_BY_WRITER,
407 .client_type = LTTNG_CLIENT_TYPE,
408 };
409
410 const struct lttng_ust_lib_ring_buffer_client_cb *LTTNG_CLIENT_CALLBACKS = &client_config.cb;
411
412 static
413 struct ltt_channel *_channel_create(const char *name,
414 void *buf_addr,
415 size_t subbuf_size, size_t num_subbuf,
416 unsigned int switch_timer_interval,
417 unsigned int read_timer_interval,
418 int **shm_fd, int **wait_fd,
419 uint64_t **memory_map_size,
420 struct ltt_channel *chan_priv_init)
421 {
422 void *priv;
423 struct ltt_channel *ltt_chan = NULL;
424 struct lttng_ust_shm_handle *handle;
425
426 handle = channel_create(&client_config, name,
427 &priv, __alignof__(*ltt_chan), sizeof(*ltt_chan),
428 chan_priv_init,
429 buf_addr, subbuf_size, num_subbuf,
430 switch_timer_interval, read_timer_interval,
431 shm_fd, wait_fd, memory_map_size);
432 if (!handle)
433 return NULL;
434 ltt_chan = priv;
435 ltt_chan->handle = handle;
436 ltt_chan->chan = shmp(ltt_chan->handle, ltt_chan->handle->chan);
437 return ltt_chan;
438 }
439
440 static
441 void ltt_channel_destroy(struct ltt_channel *ltt_chan)
442 {
443 channel_destroy(ltt_chan->chan, ltt_chan->handle, 0);
444 }
445
446 static
447 struct lttng_ust_lib_ring_buffer *ltt_buffer_read_open(struct channel *chan,
448 struct lttng_ust_shm_handle *handle,
449 int **shm_fd, int **wait_fd,
450 uint64_t **memory_map_size)
451 {
452 struct lttng_ust_lib_ring_buffer *buf;
453 int cpu;
454
455 for_each_channel_cpu(cpu, chan) {
456 buf = channel_get_ring_buffer(&client_config, chan,
457 cpu, handle, shm_fd, wait_fd,
458 memory_map_size);
459 if (!lib_ring_buffer_open_read(buf, handle, 0))
460 return buf;
461 }
462 return NULL;
463 }
464
465 static
466 void ltt_buffer_read_close(struct lttng_ust_lib_ring_buffer *buf,
467 struct lttng_ust_shm_handle *handle)
468 {
469 lib_ring_buffer_release_read(buf, handle, 0);
470 }
471
472 static
473 int ltt_event_reserve(struct lttng_ust_lib_ring_buffer_ctx *ctx,
474 uint32_t event_id)
475 {
476 struct ltt_channel *ltt_chan = channel_get_private(ctx->chan);
477 int ret, cpu;
478
479 cpu = lib_ring_buffer_get_cpu(&client_config);
480 if (cpu < 0)
481 return -EPERM;
482 ctx->cpu = cpu;
483
484 switch (ltt_chan->header_type) {
485 case 1: /* compact */
486 if (event_id > 30)
487 ctx->rflags |= LTT_RFLAG_EXTENDED;
488 break;
489 case 2: /* large */
490 if (event_id > 65534)
491 ctx->rflags |= LTT_RFLAG_EXTENDED;
492 break;
493 default:
494 WARN_ON_ONCE(1);
495 }
496
497 ret = lib_ring_buffer_reserve(&client_config, ctx);
498 if (ret)
499 goto put;
500 ltt_write_event_header(&client_config, ctx, event_id);
501 return 0;
502 put:
503 lib_ring_buffer_put_cpu(&client_config);
504 return ret;
505 }
506
507 static
508 void ltt_event_commit(struct lttng_ust_lib_ring_buffer_ctx *ctx)
509 {
510 lib_ring_buffer_commit(&client_config, ctx);
511 lib_ring_buffer_put_cpu(&client_config);
512 }
513
514 static
515 void ltt_event_write(struct lttng_ust_lib_ring_buffer_ctx *ctx, const void *src,
516 size_t len)
517 {
518 lib_ring_buffer_write(&client_config, ctx, src, len);
519 }
520
521 #if 0
522 static
523 wait_queue_head_t *ltt_get_reader_wait_queue(struct channel *chan)
524 {
525 return &chan->read_wait;
526 }
527
528 static
529 wait_queue_head_t *ltt_get_hp_wait_queue(struct channel *chan)
530 {
531 return &chan->hp_wait;
532 }
533 #endif //0
534
535 static
536 int ltt_is_finalized(struct channel *chan)
537 {
538 return lib_ring_buffer_channel_is_finalized(chan);
539 }
540
541 static
542 int ltt_is_disabled(struct channel *chan)
543 {
544 return lib_ring_buffer_channel_is_disabled(chan);
545 }
546
547 static
548 int ltt_flush_buffer(struct channel *chan, struct lttng_ust_shm_handle *handle)
549 {
550 struct lttng_ust_lib_ring_buffer *buf;
551 int cpu;
552
553 for_each_channel_cpu(cpu, chan) {
554 int *shm_fd, *wait_fd;
555 uint64_t *memory_map_size;
556
557 buf = channel_get_ring_buffer(&client_config, chan,
558 cpu, handle, &shm_fd, &wait_fd,
559 &memory_map_size);
560 lib_ring_buffer_switch(&client_config, buf,
561 SWITCH_ACTIVE, handle);
562 }
563 return 0;
564 }
565
566 static struct ltt_transport ltt_relay_transport = {
567 .name = "relay-" RING_BUFFER_MODE_TEMPLATE_STRING "-mmap",
568 .ops = {
569 .channel_create = _channel_create,
570 .channel_destroy = ltt_channel_destroy,
571 .buffer_read_open = ltt_buffer_read_open,
572 .buffer_read_close = ltt_buffer_read_close,
573 .event_reserve = ltt_event_reserve,
574 .event_commit = ltt_event_commit,
575 .event_write = ltt_event_write,
576 .packet_avail_size = NULL, /* Would be racy anyway */
577 //.get_reader_wait_queue = ltt_get_reader_wait_queue,
578 //.get_hp_wait_queue = ltt_get_hp_wait_queue,
579 .is_finalized = ltt_is_finalized,
580 .is_disabled = ltt_is_disabled,
581 .flush_buffer = ltt_flush_buffer,
582 },
583 };
584
585 void RING_BUFFER_MODE_TEMPLATE_INIT(void)
586 {
587 DBG("LTT : ltt ring buffer client init\n");
588 ltt_transport_register(&ltt_relay_transport);
589 }
590
591 void RING_BUFFER_MODE_TEMPLATE_EXIT(void)
592 {
593 DBG("LTT : ltt ring buffer client exit\n");
594 ltt_transport_unregister(&ltt_relay_transport);
595 }
This page took 0.041101 seconds and 5 git commands to generate.