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