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