1 /* SPDX-License-Identifier: (GPL-2.0-only or LGPL-2.1-only)
3 * lttng-ring-buffer-client.h
5 * LTTng lib ring buffer client template.
7 * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
10 #include <linux/module.h>
11 #include <linux/types.h>
12 #include <lttng-events.h>
13 #include <lttng-tracer.h>
15 static struct lttng_transport lttng_relay_transport
;
17 struct metadata_packet_header
{
18 uint32_t magic
; /* 0x75D11D57 */
19 uint8_t uuid
[16]; /* Unique Universal Identifier */
20 uint32_t checksum
; /* 0 if unused */
21 uint32_t content_size
; /* in bits */
22 uint32_t packet_size
; /* in bits */
23 uint8_t compression_scheme
; /* 0 if unused */
24 uint8_t encryption_scheme
; /* 0 if unused */
25 uint8_t checksum_scheme
; /* 0 if unused */
26 uint8_t major
; /* CTF spec major version number */
27 uint8_t minor
; /* CTF spec minor version number */
28 uint8_t header_end
[0];
31 struct metadata_record_header
{
32 uint8_t header_end
[0]; /* End of header */
35 static const struct lib_ring_buffer_config client_config
;
38 u64
lib_ring_buffer_clock_read(struct channel
*chan
)
44 size_t record_header_size(const struct lib_ring_buffer_config
*config
,
45 struct channel
*chan
, size_t offset
,
46 size_t *pre_header_padding
,
47 struct lib_ring_buffer_ctx
*ctx
,
53 #include <wrapper/ringbuffer/api.h>
55 static u64
client_ring_buffer_clock_read(struct channel
*chan
)
61 size_t client_record_header_size(const struct lib_ring_buffer_config
*config
,
62 struct channel
*chan
, size_t offset
,
63 size_t *pre_header_padding
,
64 struct lib_ring_buffer_ctx
*ctx
,
71 * client_packet_header_size - called on buffer-switch to a new sub-buffer
73 * Return header size without padding after the structure. Don't use packed
74 * structure because gcc generates inefficient code on some architectures
77 static size_t client_packet_header_size(void)
79 return offsetof(struct metadata_packet_header
, header_end
);
82 static void client_buffer_begin(struct lib_ring_buffer
*buf
, u64 tsc
,
83 unsigned int subbuf_idx
)
85 struct channel
*chan
= buf
->backend
.chan
;
86 struct metadata_packet_header
*header
=
87 (struct metadata_packet_header
*)
88 lib_ring_buffer_offset_address(&buf
->backend
,
89 subbuf_idx
* chan
->backend
.subbuf_size
);
90 struct lttng_metadata_cache
*metadata_cache
=
91 channel_get_private(chan
);
93 header
->magic
= TSDL_MAGIC_NUMBER
;
94 memcpy(header
->uuid
, metadata_cache
->uuid
.b
,
95 sizeof(metadata_cache
->uuid
));
96 header
->checksum
= 0; /* 0 if unused */
97 header
->content_size
= 0xFFFFFFFF; /* in bits, for debugging */
98 header
->packet_size
= 0xFFFFFFFF; /* in bits, for debugging */
99 header
->compression_scheme
= 0; /* 0 if unused */
100 header
->encryption_scheme
= 0; /* 0 if unused */
101 header
->checksum_scheme
= 0; /* 0 if unused */
102 header
->major
= CTF_SPEC_MAJOR
;
103 header
->minor
= CTF_SPEC_MINOR
;
107 * offset is assumed to never be 0 here : never deliver a completely empty
108 * subbuffer. data_size is between 1 and subbuf_size.
110 static void client_buffer_end(struct lib_ring_buffer
*buf
, u64 tsc
,
111 unsigned int subbuf_idx
, unsigned long data_size
)
113 struct channel
*chan
= buf
->backend
.chan
;
114 struct metadata_packet_header
*header
=
115 (struct metadata_packet_header
*)
116 lib_ring_buffer_offset_address(&buf
->backend
,
117 subbuf_idx
* chan
->backend
.subbuf_size
);
118 unsigned long records_lost
= 0;
120 header
->content_size
= data_size
* CHAR_BIT
; /* in bits */
121 header
->packet_size
= PAGE_ALIGN(data_size
) * CHAR_BIT
; /* in bits */
123 * We do not care about the records lost count, because the metadata
124 * channel waits and retry.
126 (void) lib_ring_buffer_get_records_lost_full(&client_config
, buf
);
127 records_lost
+= lib_ring_buffer_get_records_lost_wrap(&client_config
, buf
);
128 records_lost
+= lib_ring_buffer_get_records_lost_big(&client_config
, buf
);
129 WARN_ON_ONCE(records_lost
!= 0);
132 static int client_buffer_create(struct lib_ring_buffer
*buf
, void *priv
,
133 int cpu
, const char *name
)
138 static void client_buffer_finalize(struct lib_ring_buffer
*buf
, void *priv
, int cpu
)
142 static int client_timestamp_begin(const struct lib_ring_buffer_config
*config
,
143 struct lib_ring_buffer
*buf
, uint64_t *timestamp_begin
)
148 static int client_timestamp_end(const struct lib_ring_buffer_config
*config
,
149 struct lib_ring_buffer
*bufb
,
150 uint64_t *timestamp_end
)
155 static int client_events_discarded(const struct lib_ring_buffer_config
*config
,
156 struct lib_ring_buffer
*bufb
,
157 uint64_t *events_discarded
)
162 static int client_current_timestamp(const struct lib_ring_buffer_config
*config
,
163 struct lib_ring_buffer
*bufb
,
169 static int client_content_size(const struct lib_ring_buffer_config
*config
,
170 struct lib_ring_buffer
*bufb
,
171 uint64_t *content_size
)
176 static int client_packet_size(const struct lib_ring_buffer_config
*config
,
177 struct lib_ring_buffer
*bufb
,
178 uint64_t *packet_size
)
183 static int client_stream_id(const struct lib_ring_buffer_config
*config
,
184 struct lib_ring_buffer
*bufb
,
190 static int client_sequence_number(const struct lib_ring_buffer_config
*config
,
191 struct lib_ring_buffer
*bufb
,
198 int client_instance_id(const struct lib_ring_buffer_config
*config
,
199 struct lib_ring_buffer
*bufb
,
205 static const struct lib_ring_buffer_config client_config
= {
206 .cb
.ring_buffer_clock_read
= client_ring_buffer_clock_read
,
207 .cb
.record_header_size
= client_record_header_size
,
208 .cb
.subbuffer_header_size
= client_packet_header_size
,
209 .cb
.buffer_begin
= client_buffer_begin
,
210 .cb
.buffer_end
= client_buffer_end
,
211 .cb
.buffer_create
= client_buffer_create
,
212 .cb
.buffer_finalize
= client_buffer_finalize
,
215 .alloc
= RING_BUFFER_ALLOC_GLOBAL
,
216 .sync
= RING_BUFFER_SYNC_GLOBAL
,
217 .mode
= RING_BUFFER_MODE_TEMPLATE
,
218 .backend
= RING_BUFFER_PAGE
,
219 .output
= RING_BUFFER_OUTPUT_TEMPLATE
,
220 .oops
= RING_BUFFER_OOPS_CONSISTENCY
,
221 .ipi
= RING_BUFFER_IPI_BARRIER
,
222 .wakeup
= RING_BUFFER_WAKEUP_BY_TIMER
,
226 void release_priv_ops(void *priv_ops
)
228 module_put(THIS_MODULE
);
232 void lttng_channel_destroy(struct channel
*chan
)
234 channel_destroy(chan
);
238 struct channel
*_channel_create(const char *name
,
239 struct lttng_channel
*lttng_chan
, void *buf_addr
,
240 size_t subbuf_size
, size_t num_subbuf
,
241 unsigned int switch_timer_interval
,
242 unsigned int read_timer_interval
)
244 struct channel
*chan
;
246 chan
= channel_create(&client_config
, name
,
247 lttng_chan
->session
->metadata_cache
, buf_addr
,
248 subbuf_size
, num_subbuf
, switch_timer_interval
,
249 read_timer_interval
);
252 * Ensure this module is not unloaded before we finish
253 * using lttng_relay_transport.ops.
255 if (!try_module_get(THIS_MODULE
)) {
256 printk(KERN_WARNING
"LTT : Can't lock transport module.\n");
259 chan
->backend
.priv_ops
= <tng_relay_transport
.ops
;
260 chan
->backend
.release_priv_ops
= release_priv_ops
;
265 lttng_channel_destroy(chan
);
270 struct lib_ring_buffer
*lttng_buffer_read_open(struct channel
*chan
)
272 struct lib_ring_buffer
*buf
;
274 buf
= channel_get_ring_buffer(&client_config
, chan
, 0);
275 if (!lib_ring_buffer_open_read(buf
))
281 int lttng_buffer_has_read_closed_stream(struct channel
*chan
)
283 struct lib_ring_buffer
*buf
;
286 for_each_channel_cpu(cpu
, chan
) {
287 buf
= channel_get_ring_buffer(&client_config
, chan
, cpu
);
288 if (!atomic_long_read(&buf
->active_readers
))
295 void lttng_buffer_read_close(struct lib_ring_buffer
*buf
)
297 lib_ring_buffer_release_read(buf
);
301 int lttng_event_reserve(struct lib_ring_buffer_ctx
*ctx
, uint32_t event_id
)
305 ret
= lib_ring_buffer_reserve(&client_config
, ctx
, NULL
);
308 lib_ring_buffer_backend_get_pages(&client_config
, ctx
,
309 &ctx
->backend_pages
);
315 void lttng_event_commit(struct lib_ring_buffer_ctx
*ctx
)
317 lib_ring_buffer_commit(&client_config
, ctx
);
321 void lttng_event_write(struct lib_ring_buffer_ctx
*ctx
, const void *src
,
324 lib_ring_buffer_write(&client_config
, ctx
, src
, len
);
328 void lttng_event_write_from_user(struct lib_ring_buffer_ctx
*ctx
,
329 const void __user
*src
, size_t len
)
331 lib_ring_buffer_copy_from_user_inatomic(&client_config
, ctx
, src
, len
);
335 void lttng_event_memset(struct lib_ring_buffer_ctx
*ctx
,
338 lib_ring_buffer_memset(&client_config
, ctx
, c
, len
);
342 void lttng_event_strcpy(struct lib_ring_buffer_ctx
*ctx
, const char *src
,
345 lib_ring_buffer_strcpy(&client_config
, ctx
, src
, len
, '#');
349 size_t lttng_packet_avail_size(struct channel
*chan
)
351 unsigned long o_begin
;
352 struct lib_ring_buffer
*buf
;
354 buf
= chan
->backend
.buf
; /* Only for global buffer ! */
355 o_begin
= v_read(&client_config
, &buf
->offset
);
356 if (subbuf_offset(o_begin
, chan
) != 0) {
357 return chan
->backend
.subbuf_size
- subbuf_offset(o_begin
, chan
);
359 return chan
->backend
.subbuf_size
- subbuf_offset(o_begin
, chan
)
360 - sizeof(struct metadata_packet_header
);
365 wait_queue_head_t
*lttng_get_writer_buf_wait_queue(struct channel
*chan
, int cpu
)
367 struct lib_ring_buffer
*buf
= channel_get_ring_buffer(&client_config
,
369 return &buf
->write_wait
;
373 wait_queue_head_t
*lttng_get_hp_wait_queue(struct channel
*chan
)
375 return &chan
->hp_wait
;
379 int lttng_is_finalized(struct channel
*chan
)
381 return lib_ring_buffer_channel_is_finalized(chan
);
385 int lttng_is_disabled(struct channel
*chan
)
387 return lib_ring_buffer_channel_is_disabled(chan
);
390 static struct lttng_transport lttng_relay_transport
= {
391 .name
= "relay-" RING_BUFFER_MODE_TEMPLATE_STRING
,
392 .owner
= THIS_MODULE
,
394 .channel_create
= _channel_create
,
395 .channel_destroy
= lttng_channel_destroy
,
396 .buffer_read_open
= lttng_buffer_read_open
,
397 .buffer_has_read_closed_stream
=
398 lttng_buffer_has_read_closed_stream
,
399 .buffer_read_close
= lttng_buffer_read_close
,
400 .event_reserve
= lttng_event_reserve
,
401 .event_commit
= lttng_event_commit
,
402 .event_write_from_user
= lttng_event_write_from_user
,
403 .event_memset
= lttng_event_memset
,
404 .event_write
= lttng_event_write
,
405 .event_strcpy
= lttng_event_strcpy
,
406 .packet_avail_size
= lttng_packet_avail_size
,
407 .get_writer_buf_wait_queue
= lttng_get_writer_buf_wait_queue
,
408 .get_hp_wait_queue
= lttng_get_hp_wait_queue
,
409 .is_finalized
= lttng_is_finalized
,
410 .is_disabled
= lttng_is_disabled
,
411 .timestamp_begin
= client_timestamp_begin
,
412 .timestamp_end
= client_timestamp_end
,
413 .events_discarded
= client_events_discarded
,
414 .content_size
= client_content_size
,
415 .packet_size
= client_packet_size
,
416 .stream_id
= client_stream_id
,
417 .current_timestamp
= client_current_timestamp
,
418 .sequence_number
= client_sequence_number
,
419 .instance_id
= client_instance_id
,
423 static int __init
lttng_ring_buffer_client_init(void)
425 lttng_transport_register(<tng_relay_transport
);
429 module_init(lttng_ring_buffer_client_init
);
431 static void __exit
lttng_ring_buffer_client_exit(void)
433 lttng_transport_unregister(<tng_relay_transport
);
436 module_exit(lttng_ring_buffer_client_exit
);
438 MODULE_LICENSE("GPL and additional rights");
439 MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>");
440 MODULE_DESCRIPTION("LTTng ring buffer " RING_BUFFER_MODE_TEMPLATE_STRING
442 MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION
) "."
443 __stringify(LTTNG_MODULES_MINOR_VERSION
) "."
444 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
)
445 LTTNG_MODULES_EXTRAVERSION
);