2 * ltt-ring-buffer-client.h
4 * Copyright (C) 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 * LTTng lib ring buffer client template.
8 * Dual LGPL v2.1/GPL v2 license.
11 #include <linux/module.h>
12 #include <linux/types.h>
13 #include "wrapper/vmalloc.h" /* for wrapper_vmalloc_sync_all() */
14 #include "ltt-events.h"
15 #include "ltt-tracer.h"
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 unsigned char 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)
52 #include "wrapper/ringbuffer/api.h"
54 static u64 client_ring_buffer_clock_read(struct channel *chan)
60 size_t client_record_header_size(const struct lib_ring_buffer_config *config,
61 struct channel *chan, size_t offset,
62 size_t *pre_header_padding,
63 struct lib_ring_buffer_ctx *ctx)
69 * client_packet_header_size - called on buffer-switch to a new sub-buffer
71 * Return header size without padding after the structure. Don't use packed
72 * structure because gcc generates inefficient code on some architectures
75 static size_t client_packet_header_size(void)
77 return offsetof(struct metadata_packet_header, header_end);
80 static void client_buffer_begin(struct lib_ring_buffer *buf, u64 tsc,
81 unsigned int subbuf_idx)
83 struct channel *chan = buf->backend.chan;
84 struct metadata_packet_header *header =
85 (struct metadata_packet_header *)
86 lib_ring_buffer_offset_address(&buf->backend,
87 subbuf_idx * chan->backend.subbuf_size);
88 struct ltt_channel *ltt_chan = channel_get_private(chan);
89 struct ltt_session *session = ltt_chan->session;
91 header->magic = TSDL_MAGIC_NUMBER;
92 memcpy(header->uuid, session->uuid.b, sizeof(session->uuid));
93 header->checksum = 0; /* 0 if unused */
94 header->content_size = 0xFFFFFFFF; /* in bits, for debugging */
95 header->packet_size = 0xFFFFFFFF; /* in bits, for debugging */
96 header->compression_scheme = 0; /* 0 if unused */
97 header->encryption_scheme = 0; /* 0 if unused */
98 header->checksum_scheme = 0; /* 0 if unused */
99 header->major = CTF_SPEC_MAJOR;
100 header->minor = CTF_SPEC_MINOR;
104 * offset is assumed to never be 0 here : never deliver a completely empty
105 * subbuffer. data_size is between 1 and subbuf_size.
107 static void client_buffer_end(struct lib_ring_buffer *buf, u64 tsc,
108 unsigned int subbuf_idx, unsigned long data_size)
110 struct channel *chan = buf->backend.chan;
111 struct metadata_packet_header *header =
112 (struct metadata_packet_header *)
113 lib_ring_buffer_offset_address(&buf->backend,
114 subbuf_idx * chan->backend.subbuf_size);
115 unsigned long records_lost = 0;
117 header->content_size = data_size * CHAR_BIT; /* in bits */
118 header->packet_size = PAGE_ALIGN(data_size) * CHAR_BIT; /* in bits */
120 * We do not care about the records lost count, because the metadata
121 * channel waits and retry.
123 (void) lib_ring_buffer_get_records_lost_full(&client_config, buf);
124 records_lost += lib_ring_buffer_get_records_lost_wrap(&client_config, buf);
125 records_lost += lib_ring_buffer_get_records_lost_big(&client_config, buf);
126 WARN_ON_ONCE(records_lost != 0);
129 static int client_buffer_create(struct lib_ring_buffer *buf, void *priv,
130 int cpu, const char *name)
135 static void client_buffer_finalize(struct lib_ring_buffer *buf, void *priv, int cpu)
139 static const struct lib_ring_buffer_config client_config = {
140 .cb.ring_buffer_clock_read = client_ring_buffer_clock_read,
141 .cb.record_header_size = client_record_header_size,
142 .cb.subbuffer_header_size = client_packet_header_size,
143 .cb.buffer_begin = client_buffer_begin,
144 .cb.buffer_end = client_buffer_end,
145 .cb.buffer_create = client_buffer_create,
146 .cb.buffer_finalize = client_buffer_finalize,
149 .alloc = RING_BUFFER_ALLOC_GLOBAL,
150 .sync = RING_BUFFER_SYNC_GLOBAL,
151 .mode = RING_BUFFER_MODE_TEMPLATE,
152 .backend = RING_BUFFER_PAGE,
153 .output = RING_BUFFER_OUTPUT_TEMPLATE,
154 .oops = RING_BUFFER_OOPS_CONSISTENCY,
155 .ipi = RING_BUFFER_IPI_BARRIER,
156 .wakeup = RING_BUFFER_WAKEUP_BY_TIMER,
160 struct channel *_channel_create(const char *name,
161 struct ltt_channel *ltt_chan, void *buf_addr,
162 size_t subbuf_size, size_t num_subbuf,
163 unsigned int switch_timer_interval,
164 unsigned int read_timer_interval)
166 return channel_create(&client_config, name, ltt_chan, buf_addr,
167 subbuf_size, num_subbuf, switch_timer_interval,
168 read_timer_interval);
172 void ltt_channel_destroy(struct channel *chan)
174 channel_destroy(chan);
178 struct lib_ring_buffer *ltt_buffer_read_open(struct channel *chan)
180 struct lib_ring_buffer *buf;
182 buf = channel_get_ring_buffer(&client_config, chan, 0);
183 if (!lib_ring_buffer_open_read(buf))
189 int ltt_buffer_has_read_closed_stream(struct channel *chan)
191 struct lib_ring_buffer *buf;
194 for_each_channel_cpu(cpu, chan) {
195 buf = channel_get_ring_buffer(&client_config, chan, cpu);
196 if (!atomic_long_read(&buf->active_readers))
203 void ltt_buffer_read_close(struct lib_ring_buffer *buf)
205 lib_ring_buffer_release_read(buf);
209 int ltt_event_reserve(struct lib_ring_buffer_ctx *ctx, uint32_t event_id)
211 return lib_ring_buffer_reserve(&client_config, ctx);
215 void ltt_event_commit(struct lib_ring_buffer_ctx *ctx)
217 lib_ring_buffer_commit(&client_config, ctx);
221 void ltt_event_write(struct lib_ring_buffer_ctx *ctx, const void *src,
224 lib_ring_buffer_write(&client_config, ctx, src, len);
228 void ltt_event_write_from_user(struct lib_ring_buffer_ctx *ctx,
229 const void __user *src, size_t len)
231 lib_ring_buffer_copy_from_user(&client_config, ctx, src, len);
235 void ltt_event_memset(struct lib_ring_buffer_ctx *ctx,
238 lib_ring_buffer_memset(&client_config, ctx, c, len);
242 size_t ltt_packet_avail_size(struct channel *chan)
245 unsigned long o_begin;
246 struct lib_ring_buffer *buf;
248 buf = chan->backend.buf; /* Only for global buffer ! */
249 o_begin = v_read(&client_config, &buf->offset);
250 if (subbuf_offset(o_begin, chan) != 0) {
251 return chan->backend.subbuf_size - subbuf_offset(o_begin, chan);
253 return chan->backend.subbuf_size - subbuf_offset(o_begin, chan)
254 - sizeof(struct metadata_packet_header);
259 wait_queue_head_t *ltt_get_writer_buf_wait_queue(struct channel *chan, int cpu)
261 struct lib_ring_buffer *buf = channel_get_ring_buffer(&client_config,
263 return &buf->write_wait;
267 wait_queue_head_t *ltt_get_hp_wait_queue(struct channel *chan)
269 return &chan->hp_wait;
273 int ltt_is_finalized(struct channel *chan)
275 return lib_ring_buffer_channel_is_finalized(chan);
279 int ltt_is_disabled(struct channel *chan)
281 return lib_ring_buffer_channel_is_disabled(chan);
284 static struct ltt_transport ltt_relay_transport = {
285 .name = "relay-" RING_BUFFER_MODE_TEMPLATE_STRING,
286 .owner = THIS_MODULE,
288 .channel_create = _channel_create,
289 .channel_destroy = ltt_channel_destroy,
290 .buffer_read_open = ltt_buffer_read_open,
291 .buffer_has_read_closed_stream =
292 ltt_buffer_has_read_closed_stream,
293 .buffer_read_close = ltt_buffer_read_close,
294 .event_reserve = ltt_event_reserve,
295 .event_commit = ltt_event_commit,
296 .event_write_from_user = ltt_event_write_from_user,
297 .event_memset = ltt_event_memset,
298 .event_write = ltt_event_write,
299 .packet_avail_size = ltt_packet_avail_size,
300 .get_writer_buf_wait_queue = ltt_get_writer_buf_wait_queue,
301 .get_hp_wait_queue = ltt_get_hp_wait_queue,
302 .is_finalized = ltt_is_finalized,
303 .is_disabled = ltt_is_disabled,
307 static int __init ltt_ring_buffer_client_init(void)
310 * This vmalloc sync all also takes care of the lib ring buffer
311 * vmalloc'd module pages when it is built as a module into LTTng.
313 wrapper_vmalloc_sync_all();
314 ltt_transport_register(<t_relay_transport);
318 module_init(ltt_ring_buffer_client_init);
320 static void __exit ltt_ring_buffer_client_exit(void)
322 ltt_transport_unregister(<t_relay_transport);
325 module_exit(ltt_ring_buffer_client_exit);
327 MODULE_LICENSE("GPL and additional rights");
328 MODULE_AUTHOR("Mathieu Desnoyers");
329 MODULE_DESCRIPTION("LTTng ring buffer " RING_BUFFER_MODE_TEMPLATE_STRING