Cleanup: Move lib/ringbuffer/ headers to include/ringbuffer/
[lttng-modules.git] / lttng-ring-buffer-metadata-client.h
CommitLineData
b7cdc182 1/* SPDX-License-Identifier: (GPL-2.0-only or LGPL-2.1-only)
9f36eaed 2 *
a90917c3 3 * lttng-ring-buffer-client.h
881833e3 4 *
881833e3
MD
5 * LTTng lib ring buffer client template.
6 *
886d51a3 7 * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
881833e3
MD
8 */
9
10#include <linux/module.h>
11#include <linux/types.h>
263b6c88 12#include <wrapper/vmalloc.h> /* for wrapper_vmalloc_sync_mappings() */
bbd023d6
MD
13#include <lttng-events.h>
14#include <lttng-tracer.h>
881833e3 15
dd5a0db3
MD
16static struct lttng_transport lttng_relay_transport;
17
881833e3
MD
18struct metadata_packet_header {
19 uint32_t magic; /* 0x75D11D57 */
1ec3f75a 20 uint8_t uuid[16]; /* Unique Universal Identifier */
881833e3
MD
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 */
4173df96
MD
27 uint8_t major; /* CTF spec major version number */
28 uint8_t minor; /* CTF spec minor version number */
881833e3
MD
29 uint8_t header_end[0];
30};
31
32struct metadata_record_header {
33 uint8_t header_end[0]; /* End of header */
34};
35
36static const struct lib_ring_buffer_config client_config;
37
38static inline
39u64 lib_ring_buffer_clock_read(struct channel *chan)
40{
41 return 0;
42}
43
44static inline
c1cc82b8 45size_t record_header_size(const struct lib_ring_buffer_config *config,
881833e3 46 struct channel *chan, size_t offset,
64c796d8 47 size_t *pre_header_padding,
cc62f29e
MD
48 struct lib_ring_buffer_ctx *ctx,
49 void *client_ctx)
881833e3
MD
50{
51 return 0;
52}
53
24591303 54#include <ringbuffer/api.h>
881833e3
MD
55
56static u64 client_ring_buffer_clock_read(struct channel *chan)
57{
58 return 0;
59}
60
61static
62size_t client_record_header_size(const struct lib_ring_buffer_config *config,
63 struct channel *chan, size_t offset,
881833e3 64 size_t *pre_header_padding,
cc62f29e
MD
65 struct lib_ring_buffer_ctx *ctx,
66 void *client_ctx)
881833e3
MD
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 */
78static size_t client_packet_header_size(void)
79{
80 return offsetof(struct metadata_packet_header, header_end);
81}
82
83static 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);
a36580d5
MD
91 struct lttng_metadata_cache *metadata_cache =
92 channel_get_private(chan);
881833e3
MD
93
94 header->magic = TSDL_MAGIC_NUMBER;
a36580d5
MD
95 memcpy(header->uuid, metadata_cache->uuid.b,
96 sizeof(metadata_cache->uuid));
881833e3
MD
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 */
4173df96
MD
103 header->major = CTF_SPEC_MAJOR;
104 header->minor = CTF_SPEC_MINOR;
881833e3
MD
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 */
111static 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;
d793d5e1 115 struct metadata_packet_header *header =
1ec3f75a 116 (struct metadata_packet_header *)
881833e3
MD
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 */
71c1d843
MD
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);
881833e3
MD
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
133static int client_buffer_create(struct lib_ring_buffer *buf, void *priv,
134 int cpu, const char *name)
135{
136 return 0;
137}
138
139static void client_buffer_finalize(struct lib_ring_buffer *buf, void *priv, int cpu)
140{
141}
142
3b731ab1
JD
143static 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
149static 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
156static 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
2348ca17
JD
163static 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
3b731ab1
JD
170static 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
177static 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
184static 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
5b3cf4f9
JD
191static 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
5594698f
JD
198static
199int 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
881833e3
MD
206static 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,
2db1399a 220 .output = RING_BUFFER_OUTPUT_TEMPLATE,
881833e3
MD
221 .oops = RING_BUFFER_OOPS_CONSISTENCY,
222 .ipi = RING_BUFFER_IPI_BARRIER,
223 .wakeup = RING_BUFFER_WAKEUP_BY_TIMER,
224};
225
dd5a0db3
MD
226static
227void release_priv_ops(void *priv_ops)
228{
229 module_put(THIS_MODULE);
230}
231
232static
233void lttng_channel_destroy(struct channel *chan)
234{
235 channel_destroy(chan);
236}
237
881833e3
MD
238static
239struct channel *_channel_create(const char *name,
a90917c3 240 struct lttng_channel *lttng_chan, void *buf_addr,
881833e3
MD
241 size_t subbuf_size, size_t num_subbuf,
242 unsigned int switch_timer_interval,
243 unsigned int read_timer_interval)
244{
dd5a0db3
MD
245 struct channel *chan;
246
a36580d5
MD
247 chan = channel_create(&client_config, name,
248 lttng_chan->session->metadata_cache, buf_addr,
881833e3
MD
249 subbuf_size, num_subbuf, switch_timer_interval,
250 read_timer_interval);
dd5a0db3
MD
251 if (chan) {
252 /*
253 * Ensure this module is not unloaded before we finish
254 * using lttng_relay_transport.ops.
255 */
256 if (!try_module_get(THIS_MODULE)) {
257 printk(KERN_WARNING "LTT : Can't lock transport module.\n");
258 goto error;
259 }
260 chan->backend.priv_ops = &lttng_relay_transport.ops;
261 chan->backend.release_priv_ops = release_priv_ops;
262 }
263 return chan;
881833e3 264
dd5a0db3
MD
265error:
266 lttng_channel_destroy(chan);
267 return NULL;
881833e3
MD
268}
269
270static
a90917c3 271struct lib_ring_buffer *lttng_buffer_read_open(struct channel *chan)
881833e3
MD
272{
273 struct lib_ring_buffer *buf;
881833e3 274
cd4bd11f
MD
275 buf = channel_get_ring_buffer(&client_config, chan, 0);
276 if (!lib_ring_buffer_open_read(buf))
277 return buf;
881833e3
MD
278 return NULL;
279}
280
f71ecafa 281static
a90917c3 282int lttng_buffer_has_read_closed_stream(struct channel *chan)
f71ecafa
MD
283{
284 struct lib_ring_buffer *buf;
285 int cpu;
286
287 for_each_channel_cpu(cpu, chan) {
288 buf = channel_get_ring_buffer(&client_config, chan, cpu);
289 if (!atomic_long_read(&buf->active_readers))
290 return 1;
291 }
292 return 0;
293}
294
881833e3 295static
a90917c3 296void lttng_buffer_read_close(struct lib_ring_buffer *buf)
881833e3
MD
297{
298 lib_ring_buffer_release_read(buf);
881833e3
MD
299}
300
c099397a 301static
a90917c3 302int lttng_event_reserve(struct lib_ring_buffer_ctx *ctx, uint32_t event_id)
881833e3 303{
85a07c33
MD
304 int ret;
305
cc62f29e 306 ret = lib_ring_buffer_reserve(&client_config, ctx, NULL);
85a07c33
MD
307 if (ret)
308 return ret;
309 lib_ring_buffer_backend_get_pages(&client_config, ctx,
310 &ctx->backend_pages);
311 return 0;
312
881833e3
MD
313}
314
c099397a 315static
a90917c3 316void lttng_event_commit(struct lib_ring_buffer_ctx *ctx)
881833e3
MD
317{
318 lib_ring_buffer_commit(&client_config, ctx);
319}
320
c099397a 321static
a90917c3 322void lttng_event_write(struct lib_ring_buffer_ctx *ctx, const void *src,
881833e3
MD
323 size_t len)
324{
325 lib_ring_buffer_write(&client_config, ctx, src, len);
326}
327
4ea00e4f 328static
a90917c3 329void lttng_event_write_from_user(struct lib_ring_buffer_ctx *ctx,
4ea00e4f
JD
330 const void __user *src, size_t len)
331{
7b8ea3a5 332 lib_ring_buffer_copy_from_user_inatomic(&client_config, ctx, src, len);
4ea00e4f
JD
333}
334
58aa5d24 335static
a90917c3 336void lttng_event_memset(struct lib_ring_buffer_ctx *ctx,
58aa5d24
MD
337 int c, size_t len)
338{
339 lib_ring_buffer_memset(&client_config, ctx, c, len);
340}
341
16f78f3a
MD
342static
343void lttng_event_strcpy(struct lib_ring_buffer_ctx *ctx, const char *src,
344 size_t len)
345{
346 lib_ring_buffer_strcpy(&client_config, ctx, src, len, '#');
347}
348
1ec3f75a 349static
a90917c3 350size_t lttng_packet_avail_size(struct channel *chan)
1ec3f75a
MD
351{
352 unsigned long o_begin;
353 struct lib_ring_buffer *buf;
354
355 buf = chan->backend.buf; /* Only for global buffer ! */
356 o_begin = v_read(&client_config, &buf->offset);
357 if (subbuf_offset(o_begin, chan) != 0) {
358 return chan->backend.subbuf_size - subbuf_offset(o_begin, chan);
359 } else {
360 return chan->backend.subbuf_size - subbuf_offset(o_begin, chan)
361 - sizeof(struct metadata_packet_header);
362 }
363}
364
c099397a 365static
a90917c3 366wait_queue_head_t *lttng_get_writer_buf_wait_queue(struct channel *chan, int cpu)
c099397a 367{
71c1d843
MD
368 struct lib_ring_buffer *buf = channel_get_ring_buffer(&client_config,
369 chan, cpu);
370 return &buf->write_wait;
24cedcfe
MD
371}
372
373static
a90917c3 374wait_queue_head_t *lttng_get_hp_wait_queue(struct channel *chan)
24cedcfe
MD
375{
376 return &chan->hp_wait;
377}
378
379static
a90917c3 380int lttng_is_finalized(struct channel *chan)
24cedcfe
MD
381{
382 return lib_ring_buffer_channel_is_finalized(chan);
c099397a
MD
383}
384
254ec7bc 385static
a90917c3 386int lttng_is_disabled(struct channel *chan)
254ec7bc
MD
387{
388 return lib_ring_buffer_channel_is_disabled(chan);
389}
390
a90917c3 391static struct lttng_transport lttng_relay_transport = {
881833e3
MD
392 .name = "relay-" RING_BUFFER_MODE_TEMPLATE_STRING,
393 .owner = THIS_MODULE,
394 .ops = {
395 .channel_create = _channel_create,
a90917c3
MD
396 .channel_destroy = lttng_channel_destroy,
397 .buffer_read_open = lttng_buffer_read_open,
f71ecafa 398 .buffer_has_read_closed_stream =
a90917c3
MD
399 lttng_buffer_has_read_closed_stream,
400 .buffer_read_close = lttng_buffer_read_close,
401 .event_reserve = lttng_event_reserve,
402 .event_commit = lttng_event_commit,
403 .event_write_from_user = lttng_event_write_from_user,
404 .event_memset = lttng_event_memset,
405 .event_write = lttng_event_write,
16f78f3a 406 .event_strcpy = lttng_event_strcpy,
a90917c3
MD
407 .packet_avail_size = lttng_packet_avail_size,
408 .get_writer_buf_wait_queue = lttng_get_writer_buf_wait_queue,
409 .get_hp_wait_queue = lttng_get_hp_wait_queue,
410 .is_finalized = lttng_is_finalized,
411 .is_disabled = lttng_is_disabled,
67a80835
MD
412 .timestamp_begin = client_timestamp_begin,
413 .timestamp_end = client_timestamp_end,
414 .events_discarded = client_events_discarded,
415 .content_size = client_content_size,
416 .packet_size = client_packet_size,
417 .stream_id = client_stream_id,
418 .current_timestamp = client_current_timestamp,
5b3cf4f9 419 .sequence_number = client_sequence_number,
5594698f 420 .instance_id = client_instance_id,
881833e3
MD
421 },
422};
423
a90917c3 424static int __init lttng_ring_buffer_client_init(void)
881833e3
MD
425{
426 /*
427 * This vmalloc sync all also takes care of the lib ring buffer
428 * vmalloc'd module pages when it is built as a module into LTTng.
429 */
263b6c88 430 wrapper_vmalloc_sync_mappings();
a90917c3 431 lttng_transport_register(&lttng_relay_transport);
881833e3
MD
432 return 0;
433}
434
a90917c3 435module_init(lttng_ring_buffer_client_init);
881833e3 436
a90917c3 437static void __exit lttng_ring_buffer_client_exit(void)
881833e3 438{
a90917c3 439 lttng_transport_unregister(&lttng_relay_transport);
881833e3
MD
440}
441
a90917c3 442module_exit(lttng_ring_buffer_client_exit);
881833e3
MD
443
444MODULE_LICENSE("GPL and additional rights");
1c124020 445MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>");
881833e3
MD
446MODULE_DESCRIPTION("LTTng ring buffer " RING_BUFFER_MODE_TEMPLATE_STRING
447 " client");
1c124020
MJ
448MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION) "."
449 __stringify(LTTNG_MODULES_MINOR_VERSION) "."
450 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION)
451 LTTNG_MODULES_EXTRAVERSION);
This page took 0.056824 seconds and 4 git commands to generate.