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