Move alignment into event write callback
[lttng-modules.git] / src / lttng-ring-buffer-metadata-client.h
... / ...
CommitLineData
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
17static struct lttng_transport lttng_relay_transport;
18
19struct 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
33struct metadata_record_header {
34 uint8_t header_end[0]; /* End of header */
35};
36
37static const struct lttng_kernel_ring_buffer_config client_config;
38
39static inline
40u64 lib_ring_buffer_clock_read(struct lttng_kernel_ring_buffer_channel *chan)
41{
42 return 0;
43}
44
45static inline
46size_t record_header_size(const struct lttng_kernel_ring_buffer_config *config,
47 struct lttng_kernel_ring_buffer_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
57static u64 client_ring_buffer_clock_read(struct lttng_kernel_ring_buffer_channel *chan)
58{
59 return 0;
60}
61
62static
63size_t client_record_header_size(const struct lttng_kernel_ring_buffer_config *config,
64 struct lttng_kernel_ring_buffer_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 */
79static size_t client_packet_header_size(void)
80{
81 return offsetof(struct metadata_packet_header, header_end);
82}
83
84static void client_buffer_begin(struct lttng_kernel_ring_buffer *buf, u64 tsc,
85 unsigned int subbuf_idx)
86{
87 struct lttng_kernel_ring_buffer_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 */
112static void client_buffer_end(struct lttng_kernel_ring_buffer *buf, u64 tsc,
113 unsigned int subbuf_idx, unsigned long data_size)
114{
115 struct lttng_kernel_ring_buffer_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
134static int client_buffer_create(struct lttng_kernel_ring_buffer *buf, void *priv,
135 int cpu, const char *name)
136{
137 return 0;
138}
139
140static void client_buffer_finalize(struct lttng_kernel_ring_buffer *buf, void *priv, int cpu)
141{
142}
143
144static int client_timestamp_begin(const struct lttng_kernel_ring_buffer_config *config,
145 struct lttng_kernel_ring_buffer *buf, uint64_t *timestamp_begin)
146{
147 return -ENOSYS;
148}
149
150static int client_timestamp_end(const struct lttng_kernel_ring_buffer_config *config,
151 struct lttng_kernel_ring_buffer *bufb,
152 uint64_t *timestamp_end)
153{
154 return -ENOSYS;
155}
156
157static int client_events_discarded(const struct lttng_kernel_ring_buffer_config *config,
158 struct lttng_kernel_ring_buffer *bufb,
159 uint64_t *events_discarded)
160{
161 return -ENOSYS;
162}
163
164static int client_current_timestamp(const struct lttng_kernel_ring_buffer_config *config,
165 struct lttng_kernel_ring_buffer *bufb,
166 uint64_t *ts)
167{
168 return -ENOSYS;
169}
170
171static int client_content_size(const struct lttng_kernel_ring_buffer_config *config,
172 struct lttng_kernel_ring_buffer *bufb,
173 uint64_t *content_size)
174{
175 return -ENOSYS;
176}
177
178static int client_packet_size(const struct lttng_kernel_ring_buffer_config *config,
179 struct lttng_kernel_ring_buffer *bufb,
180 uint64_t *packet_size)
181{
182 return -ENOSYS;
183}
184
185static int client_stream_id(const struct lttng_kernel_ring_buffer_config *config,
186 struct lttng_kernel_ring_buffer *bufb,
187 uint64_t *stream_id)
188{
189 return -ENOSYS;
190}
191
192static int client_sequence_number(const struct lttng_kernel_ring_buffer_config *config,
193 struct lttng_kernel_ring_buffer *bufb,
194 uint64_t *seq)
195{
196 return -ENOSYS;
197}
198
199static
200int client_instance_id(const struct lttng_kernel_ring_buffer_config *config,
201 struct lttng_kernel_ring_buffer *bufb,
202 uint64_t *id)
203{
204 return -ENOSYS;
205}
206
207static const struct lttng_kernel_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
227static
228void release_priv_ops(void *priv_ops)
229{
230 module_put(THIS_MODULE);
231}
232
233static
234void lttng_channel_destroy(struct lttng_kernel_ring_buffer_channel *chan)
235{
236 channel_destroy(chan);
237}
238
239static
240struct lttng_kernel_ring_buffer_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_kernel_channel_buffer *lttng_chan = priv;
247 struct lttng_kernel_ring_buffer_channel *chan;
248
249 chan = channel_create(&client_config, name,
250 lttng_chan->parent.session->priv->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
267error:
268 lttng_channel_destroy(chan);
269 return NULL;
270}
271
272static
273struct lttng_kernel_ring_buffer *lttng_buffer_read_open(struct lttng_kernel_ring_buffer_channel *chan)
274{
275 struct lttng_kernel_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
283static
284int lttng_buffer_has_read_closed_stream(struct lttng_kernel_ring_buffer_channel *chan)
285{
286 struct lttng_kernel_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
297static
298void lttng_buffer_read_close(struct lttng_kernel_ring_buffer *buf)
299{
300 lib_ring_buffer_release_read(buf);
301}
302
303static
304int lttng_event_reserve(struct lttng_kernel_ring_buffer_ctx *ctx)
305{
306 struct lttng_kernel_ring_buffer_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
321static
322void lttng_event_commit(struct lttng_kernel_ring_buffer_ctx *ctx)
323{
324 lib_ring_buffer_commit(&client_config, ctx);
325}
326
327static
328void lttng_event_write(struct lttng_kernel_ring_buffer_ctx *ctx, const void *src,
329 size_t len, size_t alignment)
330{
331 lib_ring_buffer_align_ctx(ctx, alignment);
332 lib_ring_buffer_write(&client_config, ctx, src, len);
333}
334
335static
336void lttng_event_write_from_user(struct lttng_kernel_ring_buffer_ctx *ctx,
337 const void __user *src, size_t len, size_t alignment)
338{
339 lib_ring_buffer_align_ctx(ctx, alignment);
340 lib_ring_buffer_copy_from_user_inatomic(&client_config, ctx, src, len);
341}
342
343static
344void lttng_event_memset(struct lttng_kernel_ring_buffer_ctx *ctx,
345 int c, size_t len)
346{
347 lib_ring_buffer_memset(&client_config, ctx, c, len);
348}
349
350static
351void lttng_event_strcpy(struct lttng_kernel_ring_buffer_ctx *ctx, const char *src,
352 size_t len)
353{
354 lib_ring_buffer_strcpy(&client_config, ctx, src, len, '#');
355}
356
357static
358size_t lttng_packet_avail_size(struct lttng_kernel_ring_buffer_channel *chan)
359{
360 unsigned long o_begin;
361 struct lttng_kernel_ring_buffer *buf;
362
363 buf = chan->backend.buf; /* Only for global buffer ! */
364 o_begin = v_read(&client_config, &buf->offset);
365 if (subbuf_offset(o_begin, chan) != 0) {
366 return chan->backend.subbuf_size - subbuf_offset(o_begin, chan);
367 } else {
368 return chan->backend.subbuf_size - subbuf_offset(o_begin, chan)
369 - sizeof(struct metadata_packet_header);
370 }
371}
372
373static
374wait_queue_head_t *lttng_get_writer_buf_wait_queue(struct lttng_kernel_ring_buffer_channel *chan, int cpu)
375{
376 struct lttng_kernel_ring_buffer *buf = channel_get_ring_buffer(&client_config,
377 chan, cpu);
378 return &buf->write_wait;
379}
380
381static
382wait_queue_head_t *lttng_get_hp_wait_queue(struct lttng_kernel_ring_buffer_channel *chan)
383{
384 return &chan->hp_wait;
385}
386
387static
388int lttng_is_finalized(struct lttng_kernel_ring_buffer_channel *chan)
389{
390 return lib_ring_buffer_channel_is_finalized(chan);
391}
392
393static
394int lttng_is_disabled(struct lttng_kernel_ring_buffer_channel *chan)
395{
396 return lib_ring_buffer_channel_is_disabled(chan);
397}
398
399static struct lttng_transport lttng_relay_transport = {
400 .name = "relay-" RING_BUFFER_MODE_TEMPLATE_STRING,
401 .owner = THIS_MODULE,
402 .ops = {
403 .priv = __LTTNG_COMPOUND_LITERAL(struct lttng_kernel_channel_buffer_ops_private, {
404 .pub = &lttng_relay_transport.ops,
405 .channel_create = _channel_create,
406 .channel_destroy = lttng_channel_destroy,
407 .buffer_read_open = lttng_buffer_read_open,
408 .buffer_has_read_closed_stream =
409 lttng_buffer_has_read_closed_stream,
410 .buffer_read_close = lttng_buffer_read_close,
411 .packet_avail_size = lttng_packet_avail_size,
412 .get_writer_buf_wait_queue = lttng_get_writer_buf_wait_queue,
413 .get_hp_wait_queue = lttng_get_hp_wait_queue,
414 .is_finalized = lttng_is_finalized,
415 .is_disabled = lttng_is_disabled,
416 .timestamp_begin = client_timestamp_begin,
417 .timestamp_end = client_timestamp_end,
418 .events_discarded = client_events_discarded,
419 .content_size = client_content_size,
420 .packet_size = client_packet_size,
421 .stream_id = client_stream_id,
422 .current_timestamp = client_current_timestamp,
423 .sequence_number = client_sequence_number,
424 .instance_id = client_instance_id,
425 }),
426 .event_reserve = lttng_event_reserve,
427 .event_commit = lttng_event_commit,
428 .event_write_from_user = lttng_event_write_from_user,
429 .event_memset = lttng_event_memset,
430 .event_write = lttng_event_write,
431 .event_strcpy = lttng_event_strcpy,
432 },
433};
434
435static int __init lttng_ring_buffer_client_init(void)
436{
437 /*
438 * This vmalloc sync all also takes care of the lib ring buffer
439 * vmalloc'd module pages when it is built as a module into LTTng.
440 */
441 wrapper_vmalloc_sync_mappings();
442 lttng_transport_register(&lttng_relay_transport);
443 return 0;
444}
445
446module_init(lttng_ring_buffer_client_init);
447
448static void __exit lttng_ring_buffer_client_exit(void)
449{
450 lttng_transport_unregister(&lttng_relay_transport);
451}
452
453module_exit(lttng_ring_buffer_client_exit);
454
455MODULE_LICENSE("GPL and additional rights");
456MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>");
457MODULE_DESCRIPTION("LTTng ring buffer " RING_BUFFER_MODE_TEMPLATE_STRING
458 " client");
459MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION) "."
460 __stringify(LTTNG_MODULES_MINOR_VERSION) "."
461 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION)
462 LTTNG_MODULES_EXTRAVERSION);
This page took 0.023252 seconds and 4 git commands to generate.