SoW-2019-0002: Dynamic Snapshot
[deliverable/lttng-modules.git] / lttng-ring-buffer-metadata-client.h
1 /* SPDX-License-Identifier: (GPL-2.0 or LGPL-2.1)
2 *
3 * lttng-ring-buffer-client.h
4 *
5 * LTTng lib ring buffer client template.
6 *
7 * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 */
9
10 #include <linux/module.h>
11 #include <linux/types.h>
12 #include <wrapper/vmalloc.h> /* for wrapper_vmalloc_sync_all() */
13 #include <lttng-events.h>
14 #include <lttng-tracer.h>
15
16 static struct lttng_transport lttng_relay_transport;
17
18 struct metadata_packet_header {
19 uint32_t magic; /* 0x75D11D57 */
20 uint8_t uuid[16]; /* Unique Universal Identifier */
21 uint32_t checksum; /* 0 if unused */
22 uint32_t content_size; /* in bits */
23 uint32_t packet_size; /* in bits */
24 uint8_t compression_scheme; /* 0 if unused */
25 uint8_t encryption_scheme; /* 0 if unused */
26 uint8_t checksum_scheme; /* 0 if unused */
27 uint8_t major; /* CTF spec major version number */
28 uint8_t minor; /* CTF spec minor version number */
29 uint8_t header_end[0];
30 };
31
32 struct metadata_record_header {
33 uint8_t header_end[0]; /* End of header */
34 };
35
36 static const struct lib_ring_buffer_config client_config;
37
38 static inline
39 u64 lib_ring_buffer_clock_read(struct channel *chan)
40 {
41 return 0;
42 }
43
44 static inline
45 size_t record_header_size(const struct lib_ring_buffer_config *config,
46 struct channel *chan, size_t offset,
47 size_t *pre_header_padding,
48 struct lib_ring_buffer_ctx *ctx,
49 void *client_ctx)
50 {
51 return 0;
52 }
53
54 #include <wrapper/ringbuffer/api.h>
55
56 static u64 client_ring_buffer_clock_read(struct channel *chan)
57 {
58 return 0;
59 }
60
61 static
62 size_t client_record_header_size(const struct lib_ring_buffer_config *config,
63 struct channel *chan, size_t offset,
64 size_t *pre_header_padding,
65 struct lib_ring_buffer_ctx *ctx,
66 void *client_ctx)
67 {
68 return 0;
69 }
70
71 /**
72 * client_packet_header_size - called on buffer-switch to a new sub-buffer
73 *
74 * Return header size without padding after the structure. Don't use packed
75 * structure because gcc generates inefficient code on some architectures
76 * (powerpc, mips..)
77 */
78 static size_t client_packet_header_size(void)
79 {
80 return offsetof(struct metadata_packet_header, header_end);
81 }
82
83 static void client_buffer_begin(struct lib_ring_buffer *buf, u64 tsc,
84 unsigned int subbuf_idx)
85 {
86 struct channel *chan = buf->backend.chan;
87 struct metadata_packet_header *header =
88 (struct metadata_packet_header *)
89 lib_ring_buffer_offset_address(&buf->backend,
90 subbuf_idx * chan->backend.subbuf_size);
91 struct lttng_metadata_cache *metadata_cache =
92 channel_get_private(chan);
93
94 header->magic = TSDL_MAGIC_NUMBER;
95 memcpy(header->uuid, metadata_cache->uuid.b,
96 sizeof(metadata_cache->uuid));
97 header->checksum = 0; /* 0 if unused */
98 header->content_size = 0xFFFFFFFF; /* in bits, for debugging */
99 header->packet_size = 0xFFFFFFFF; /* in bits, for debugging */
100 header->compression_scheme = 0; /* 0 if unused */
101 header->encryption_scheme = 0; /* 0 if unused */
102 header->checksum_scheme = 0; /* 0 if unused */
103 header->major = CTF_SPEC_MAJOR;
104 header->minor = CTF_SPEC_MINOR;
105 }
106
107 /*
108 * offset is assumed to never be 0 here : never deliver a completely empty
109 * subbuffer. data_size is between 1 and subbuf_size.
110 */
111 static void client_buffer_end(struct lib_ring_buffer *buf, u64 tsc,
112 unsigned int subbuf_idx, unsigned long data_size)
113 {
114 struct channel *chan = buf->backend.chan;
115 struct metadata_packet_header *header =
116 (struct metadata_packet_header *)
117 lib_ring_buffer_offset_address(&buf->backend,
118 subbuf_idx * chan->backend.subbuf_size);
119 unsigned long records_lost = 0;
120
121 header->content_size = data_size * CHAR_BIT; /* in bits */
122 header->packet_size = PAGE_ALIGN(data_size) * CHAR_BIT; /* in bits */
123 /*
124 * We do not care about the records lost count, because the metadata
125 * channel waits and retry.
126 */
127 (void) lib_ring_buffer_get_records_lost_full(&client_config, buf);
128 records_lost += lib_ring_buffer_get_records_lost_wrap(&client_config, buf);
129 records_lost += lib_ring_buffer_get_records_lost_big(&client_config, buf);
130 WARN_ON_ONCE(records_lost != 0);
131 }
132
133 static int client_buffer_create(struct lib_ring_buffer *buf, void *priv,
134 int cpu, const char *name)
135 {
136 return 0;
137 }
138
139 static void client_buffer_finalize(struct lib_ring_buffer *buf, void *priv, int cpu)
140 {
141 }
142
143 static int client_timestamp_begin(const struct lib_ring_buffer_config *config,
144 struct lib_ring_buffer *buf, uint64_t *timestamp_begin)
145 {
146 return -ENOSYS;
147 }
148
149 static int client_timestamp_end(const struct lib_ring_buffer_config *config,
150 struct lib_ring_buffer *bufb,
151 uint64_t *timestamp_end)
152 {
153 return -ENOSYS;
154 }
155
156 static int client_events_discarded(const struct lib_ring_buffer_config *config,
157 struct lib_ring_buffer *bufb,
158 uint64_t *events_discarded)
159 {
160 return -ENOSYS;
161 }
162
163 static int client_current_timestamp(const struct lib_ring_buffer_config *config,
164 struct lib_ring_buffer *bufb,
165 uint64_t *ts)
166 {
167 return -ENOSYS;
168 }
169
170 static int client_content_size(const struct lib_ring_buffer_config *config,
171 struct lib_ring_buffer *bufb,
172 uint64_t *content_size)
173 {
174 return -ENOSYS;
175 }
176
177 static int client_packet_size(const struct lib_ring_buffer_config *config,
178 struct lib_ring_buffer *bufb,
179 uint64_t *packet_size)
180 {
181 return -ENOSYS;
182 }
183
184 static int client_stream_id(const struct lib_ring_buffer_config *config,
185 struct lib_ring_buffer *bufb,
186 uint64_t *stream_id)
187 {
188 return -ENOSYS;
189 }
190
191 static int client_sequence_number(const struct lib_ring_buffer_config *config,
192 struct lib_ring_buffer *bufb,
193 uint64_t *seq)
194 {
195 return -ENOSYS;
196 }
197
198 static
199 int client_instance_id(const struct lib_ring_buffer_config *config,
200 struct lib_ring_buffer *bufb,
201 uint64_t *id)
202 {
203 return -ENOSYS;
204 }
205
206 static const struct lib_ring_buffer_config client_config = {
207 .cb.ring_buffer_clock_read = client_ring_buffer_clock_read,
208 .cb.record_header_size = client_record_header_size,
209 .cb.subbuffer_header_size = client_packet_header_size,
210 .cb.buffer_begin = client_buffer_begin,
211 .cb.buffer_end = client_buffer_end,
212 .cb.buffer_create = client_buffer_create,
213 .cb.buffer_finalize = client_buffer_finalize,
214
215 .tsc_bits = 0,
216 .alloc = RING_BUFFER_ALLOC_GLOBAL,
217 .sync = RING_BUFFER_SYNC_GLOBAL,
218 .mode = RING_BUFFER_MODE_TEMPLATE,
219 .backend = RING_BUFFER_PAGE,
220 .output = RING_BUFFER_OUTPUT_TEMPLATE,
221 .oops = RING_BUFFER_OOPS_CONSISTENCY,
222 .ipi = RING_BUFFER_IPI_BARRIER,
223 .wakeup = RING_BUFFER_WAKEUP_BY_TIMER,
224 };
225
226 static
227 void release_priv_ops(void *priv_ops)
228 {
229 module_put(THIS_MODULE);
230 }
231
232 static
233 void lttng_channel_destroy(struct channel *chan)
234 {
235 channel_destroy(chan);
236 }
237
238 static
239 struct channel *_channel_create(const char *name,
240 void *priv, void *buf_addr,
241 size_t subbuf_size, size_t num_subbuf,
242 unsigned int switch_timer_interval,
243 unsigned int read_timer_interval)
244 {
245 struct lttng_channel *lttng_chan = priv;
246 struct channel *chan;
247
248 chan = channel_create(&client_config, name,
249 lttng_chan->session->metadata_cache, buf_addr,
250 subbuf_size, num_subbuf, switch_timer_interval,
251 read_timer_interval);
252 if (chan) {
253 /*
254 * Ensure this module is not unloaded before we finish
255 * using lttng_relay_transport.ops.
256 */
257 if (!try_module_get(THIS_MODULE)) {
258 printk(KERN_WARNING "LTT : Can't lock transport module.\n");
259 goto error;
260 }
261 chan->backend.priv_ops = &lttng_relay_transport.ops;
262 chan->backend.release_priv_ops = release_priv_ops;
263 }
264 return chan;
265
266 error:
267 lttng_channel_destroy(chan);
268 return NULL;
269 }
270
271 static
272 struct lib_ring_buffer *lttng_buffer_read_open(struct channel *chan)
273 {
274 struct lib_ring_buffer *buf;
275
276 buf = channel_get_ring_buffer(&client_config, chan, 0);
277 if (!lib_ring_buffer_open_read(buf))
278 return buf;
279 return NULL;
280 }
281
282 static
283 int lttng_buffer_has_read_closed_stream(struct channel *chan)
284 {
285 struct lib_ring_buffer *buf;
286 int cpu;
287
288 for_each_channel_cpu(cpu, chan) {
289 buf = channel_get_ring_buffer(&client_config, chan, cpu);
290 if (!atomic_long_read(&buf->active_readers))
291 return 1;
292 }
293 return 0;
294 }
295
296 static
297 void lttng_buffer_read_close(struct lib_ring_buffer *buf)
298 {
299 lib_ring_buffer_release_read(buf);
300 }
301
302 static
303 int lttng_event_reserve(struct lib_ring_buffer_ctx *ctx, uint32_t event_id)
304 {
305 int ret;
306
307 ret = lib_ring_buffer_reserve(&client_config, ctx, NULL);
308 if (ret)
309 return ret;
310 lib_ring_buffer_backend_get_pages(&client_config, ctx,
311 &ctx->backend_pages);
312 return 0;
313
314 }
315
316 static
317 void lttng_event_commit(struct lib_ring_buffer_ctx *ctx)
318 {
319 lib_ring_buffer_commit(&client_config, ctx);
320 }
321
322 static
323 void lttng_event_write(struct lib_ring_buffer_ctx *ctx, const void *src,
324 size_t len)
325 {
326 lib_ring_buffer_write(&client_config, ctx, src, len);
327 }
328
329 static
330 void lttng_event_write_from_user(struct lib_ring_buffer_ctx *ctx,
331 const void __user *src, size_t len)
332 {
333 lib_ring_buffer_copy_from_user_inatomic(&client_config, ctx, src, len);
334 }
335
336 static
337 void lttng_event_memset(struct lib_ring_buffer_ctx *ctx,
338 int c, size_t len)
339 {
340 lib_ring_buffer_memset(&client_config, ctx, c, len);
341 }
342
343 static
344 void lttng_event_strcpy(struct lib_ring_buffer_ctx *ctx, const char *src,
345 size_t len)
346 {
347 lib_ring_buffer_strcpy(&client_config, ctx, src, len, '#');
348 }
349
350 static
351 size_t lttng_packet_avail_size(struct channel *chan)
352
353 {
354 unsigned long o_begin;
355 struct lib_ring_buffer *buf;
356
357 buf = chan->backend.buf; /* Only for global buffer ! */
358 o_begin = v_read(&client_config, &buf->offset);
359 if (subbuf_offset(o_begin, chan) != 0) {
360 return chan->backend.subbuf_size - subbuf_offset(o_begin, chan);
361 } else {
362 return chan->backend.subbuf_size - subbuf_offset(o_begin, chan)
363 - sizeof(struct metadata_packet_header);
364 }
365 }
366
367 static
368 wait_queue_head_t *lttng_get_writer_buf_wait_queue(struct channel *chan, int cpu)
369 {
370 struct lib_ring_buffer *buf = channel_get_ring_buffer(&client_config,
371 chan, cpu);
372 return &buf->write_wait;
373 }
374
375 static
376 wait_queue_head_t *lttng_get_hp_wait_queue(struct channel *chan)
377 {
378 return &chan->hp_wait;
379 }
380
381 static
382 int lttng_is_finalized(struct channel *chan)
383 {
384 return lib_ring_buffer_channel_is_finalized(chan);
385 }
386
387 static
388 int lttng_is_disabled(struct channel *chan)
389 {
390 return lib_ring_buffer_channel_is_disabled(chan);
391 }
392
393 static struct lttng_transport lttng_relay_transport = {
394 .name = "relay-" RING_BUFFER_MODE_TEMPLATE_STRING,
395 .owner = THIS_MODULE,
396 .ops = {
397 .channel_create = _channel_create,
398 .channel_destroy = lttng_channel_destroy,
399 .buffer_read_open = lttng_buffer_read_open,
400 .buffer_has_read_closed_stream =
401 lttng_buffer_has_read_closed_stream,
402 .buffer_read_close = lttng_buffer_read_close,
403 .event_reserve = lttng_event_reserve,
404 .event_commit = lttng_event_commit,
405 .event_write_from_user = lttng_event_write_from_user,
406 .event_memset = lttng_event_memset,
407 .event_write = lttng_event_write,
408 .event_strcpy = lttng_event_strcpy,
409 .packet_avail_size = lttng_packet_avail_size,
410 .get_writer_buf_wait_queue = lttng_get_writer_buf_wait_queue,
411 .get_hp_wait_queue = lttng_get_hp_wait_queue,
412 .is_finalized = lttng_is_finalized,
413 .is_disabled = lttng_is_disabled,
414 .timestamp_begin = client_timestamp_begin,
415 .timestamp_end = client_timestamp_end,
416 .events_discarded = client_events_discarded,
417 .content_size = client_content_size,
418 .packet_size = client_packet_size,
419 .stream_id = client_stream_id,
420 .current_timestamp = client_current_timestamp,
421 .sequence_number = client_sequence_number,
422 .instance_id = client_instance_id,
423 },
424 };
425
426 static int __init lttng_ring_buffer_client_init(void)
427 {
428 /*
429 * This vmalloc sync all also takes care of the lib ring buffer
430 * vmalloc'd module pages when it is built as a module into LTTng.
431 */
432 wrapper_vmalloc_sync_all();
433 lttng_transport_register(&lttng_relay_transport);
434 return 0;
435 }
436
437 module_init(lttng_ring_buffer_client_init);
438
439 static void __exit lttng_ring_buffer_client_exit(void)
440 {
441 lttng_transport_unregister(&lttng_relay_transport);
442 }
443
444 module_exit(lttng_ring_buffer_client_exit);
445
446 MODULE_LICENSE("GPL and additional rights");
447 MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>");
448 MODULE_DESCRIPTION("LTTng ring buffer " RING_BUFFER_MODE_TEMPLATE_STRING
449 " client");
450 MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION) "."
451 __stringify(LTTNG_MODULES_MINOR_VERSION) "."
452 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION)
453 LTTNG_MODULES_EXTRAVERSION);
This page took 0.040279 seconds and 5 git commands to generate.