Fix: handle concurrent flush vs get_next_subbuf on metadata cache
[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>
25#include "wrapper/vmalloc.h" /* for wrapper_vmalloc_sync_all() */
a90917c3
MD
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
58unsigned char record_header_size(const struct lib_ring_buffer_config *config,
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
66#include "wrapper/ringbuffer/api.h"
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);
a90917c3
MD
102 struct lttng_channel *lttng_chan = channel_get_private(chan);
103 struct lttng_session *session = lttng_chan->session;
881833e3
MD
104
105 header->magic = TSDL_MAGIC_NUMBER;
1ec3f75a 106 memcpy(header->uuid, session->uuid.b, sizeof(session->uuid));
881833e3
MD
107 header->checksum = 0; /* 0 if unused */
108 header->content_size = 0xFFFFFFFF; /* in bits, for debugging */
109 header->packet_size = 0xFFFFFFFF; /* in bits, for debugging */
110 header->compression_scheme = 0; /* 0 if unused */
111 header->encryption_scheme = 0; /* 0 if unused */
112 header->checksum_scheme = 0; /* 0 if unused */
4173df96
MD
113 header->major = CTF_SPEC_MAJOR;
114 header->minor = CTF_SPEC_MINOR;
881833e3
MD
115}
116
117/*
118 * offset is assumed to never be 0 here : never deliver a completely empty
119 * subbuffer. data_size is between 1 and subbuf_size.
120 */
121static void client_buffer_end(struct lib_ring_buffer *buf, u64 tsc,
122 unsigned int subbuf_idx, unsigned long data_size)
123{
124 struct channel *chan = buf->backend.chan;
d793d5e1 125 struct metadata_packet_header *header =
1ec3f75a 126 (struct metadata_packet_header *)
881833e3
MD
127 lib_ring_buffer_offset_address(&buf->backend,
128 subbuf_idx * chan->backend.subbuf_size);
129 unsigned long records_lost = 0;
130
131 header->content_size = data_size * CHAR_BIT; /* in bits */
132 header->packet_size = PAGE_ALIGN(data_size) * CHAR_BIT; /* in bits */
71c1d843
MD
133 /*
134 * We do not care about the records lost count, because the metadata
135 * channel waits and retry.
136 */
137 (void) lib_ring_buffer_get_records_lost_full(&client_config, buf);
881833e3
MD
138 records_lost += lib_ring_buffer_get_records_lost_wrap(&client_config, buf);
139 records_lost += lib_ring_buffer_get_records_lost_big(&client_config, buf);
140 WARN_ON_ONCE(records_lost != 0);
141}
142
143static int client_buffer_create(struct lib_ring_buffer *buf, void *priv,
144 int cpu, const char *name)
145{
146 return 0;
147}
148
149static void client_buffer_finalize(struct lib_ring_buffer *buf, void *priv, int cpu)
150{
151}
152
3b731ab1
JD
153static int client_timestamp_begin(const struct lib_ring_buffer_config *config,
154 struct lib_ring_buffer *buf, uint64_t *timestamp_begin)
155{
156 return -ENOSYS;
157}
158
159static int client_timestamp_end(const struct lib_ring_buffer_config *config,
160 struct lib_ring_buffer *bufb,
161 uint64_t *timestamp_end)
162{
163 return -ENOSYS;
164}
165
166static int client_events_discarded(const struct lib_ring_buffer_config *config,
167 struct lib_ring_buffer *bufb,
168 uint64_t *events_discarded)
169{
170 return -ENOSYS;
171}
172
2348ca17
JD
173static int client_current_timestamp(const struct lib_ring_buffer_config *config,
174 struct lib_ring_buffer *bufb,
175 uint64_t *ts)
176{
177 return -ENOSYS;
178}
179
3b731ab1
JD
180static int client_content_size(const struct lib_ring_buffer_config *config,
181 struct lib_ring_buffer *bufb,
182 uint64_t *content_size)
183{
184 return -ENOSYS;
185}
186
187static int client_packet_size(const struct lib_ring_buffer_config *config,
188 struct lib_ring_buffer *bufb,
189 uint64_t *packet_size)
190{
191 return -ENOSYS;
192}
193
194static int client_stream_id(const struct lib_ring_buffer_config *config,
195 struct lib_ring_buffer *bufb,
196 uint64_t *stream_id)
197{
198 return -ENOSYS;
199}
200
881833e3
MD
201static const struct lib_ring_buffer_config client_config = {
202 .cb.ring_buffer_clock_read = client_ring_buffer_clock_read,
203 .cb.record_header_size = client_record_header_size,
204 .cb.subbuffer_header_size = client_packet_header_size,
205 .cb.buffer_begin = client_buffer_begin,
206 .cb.buffer_end = client_buffer_end,
207 .cb.buffer_create = client_buffer_create,
208 .cb.buffer_finalize = client_buffer_finalize,
209
210 .tsc_bits = 0,
211 .alloc = RING_BUFFER_ALLOC_GLOBAL,
212 .sync = RING_BUFFER_SYNC_GLOBAL,
213 .mode = RING_BUFFER_MODE_TEMPLATE,
214 .backend = RING_BUFFER_PAGE,
2db1399a 215 .output = RING_BUFFER_OUTPUT_TEMPLATE,
881833e3
MD
216 .oops = RING_BUFFER_OOPS_CONSISTENCY,
217 .ipi = RING_BUFFER_IPI_BARRIER,
218 .wakeup = RING_BUFFER_WAKEUP_BY_TIMER,
219};
220
dd5a0db3
MD
221static
222void release_priv_ops(void *priv_ops)
223{
224 module_put(THIS_MODULE);
225}
226
227static
228void lttng_channel_destroy(struct channel *chan)
229{
230 channel_destroy(chan);
231}
232
881833e3
MD
233static
234struct channel *_channel_create(const char *name,
a90917c3 235 struct lttng_channel *lttng_chan, void *buf_addr,
881833e3
MD
236 size_t subbuf_size, size_t num_subbuf,
237 unsigned int switch_timer_interval,
238 unsigned int read_timer_interval)
239{
dd5a0db3
MD
240 struct channel *chan;
241
242 chan = channel_create(&client_config, name, lttng_chan, buf_addr,
881833e3
MD
243 subbuf_size, num_subbuf, switch_timer_interval,
244 read_timer_interval);
dd5a0db3
MD
245 if (chan) {
246 /*
247 * Ensure this module is not unloaded before we finish
248 * using lttng_relay_transport.ops.
249 */
250 if (!try_module_get(THIS_MODULE)) {
251 printk(KERN_WARNING "LTT : Can't lock transport module.\n");
252 goto error;
253 }
254 chan->backend.priv_ops = &lttng_relay_transport.ops;
255 chan->backend.release_priv_ops = release_priv_ops;
256 }
257 return chan;
881833e3 258
dd5a0db3
MD
259error:
260 lttng_channel_destroy(chan);
261 return NULL;
881833e3
MD
262}
263
264static
a90917c3 265struct lib_ring_buffer *lttng_buffer_read_open(struct channel *chan)
881833e3
MD
266{
267 struct lib_ring_buffer *buf;
881833e3 268
cd4bd11f
MD
269 buf = channel_get_ring_buffer(&client_config, chan, 0);
270 if (!lib_ring_buffer_open_read(buf))
271 return buf;
881833e3
MD
272 return NULL;
273}
274
f71ecafa 275static
a90917c3 276int lttng_buffer_has_read_closed_stream(struct channel *chan)
f71ecafa
MD
277{
278 struct lib_ring_buffer *buf;
279 int cpu;
280
281 for_each_channel_cpu(cpu, chan) {
282 buf = channel_get_ring_buffer(&client_config, chan, cpu);
283 if (!atomic_long_read(&buf->active_readers))
284 return 1;
285 }
286 return 0;
287}
288
881833e3 289static
a90917c3 290void lttng_buffer_read_close(struct lib_ring_buffer *buf)
881833e3
MD
291{
292 lib_ring_buffer_release_read(buf);
881833e3
MD
293}
294
c099397a 295static
a90917c3 296int lttng_event_reserve(struct lib_ring_buffer_ctx *ctx, uint32_t event_id)
881833e3
MD
297{
298 return lib_ring_buffer_reserve(&client_config, ctx);
299}
300
c099397a 301static
a90917c3 302void lttng_event_commit(struct lib_ring_buffer_ctx *ctx)
881833e3
MD
303{
304 lib_ring_buffer_commit(&client_config, ctx);
305}
306
c099397a 307static
a90917c3 308void lttng_event_write(struct lib_ring_buffer_ctx *ctx, const void *src,
881833e3
MD
309 size_t len)
310{
311 lib_ring_buffer_write(&client_config, ctx, src, len);
312}
313
4ea00e4f 314static
a90917c3 315void lttng_event_write_from_user(struct lib_ring_buffer_ctx *ctx,
4ea00e4f
JD
316 const void __user *src, size_t len)
317{
7b8ea3a5 318 lib_ring_buffer_copy_from_user_inatomic(&client_config, ctx, src, len);
4ea00e4f
JD
319}
320
58aa5d24 321static
a90917c3 322void lttng_event_memset(struct lib_ring_buffer_ctx *ctx,
58aa5d24
MD
323 int c, size_t len)
324{
325 lib_ring_buffer_memset(&client_config, ctx, c, len);
326}
327
16f78f3a
MD
328static
329void lttng_event_strcpy(struct lib_ring_buffer_ctx *ctx, const char *src,
330 size_t len)
331{
332 lib_ring_buffer_strcpy(&client_config, ctx, src, len, '#');
333}
334
1ec3f75a 335static
a90917c3 336size_t lttng_packet_avail_size(struct channel *chan)
1ec3f75a
MD
337
338{
339 unsigned long o_begin;
340 struct lib_ring_buffer *buf;
341
342 buf = chan->backend.buf; /* Only for global buffer ! */
343 o_begin = v_read(&client_config, &buf->offset);
344 if (subbuf_offset(o_begin, chan) != 0) {
345 return chan->backend.subbuf_size - subbuf_offset(o_begin, chan);
346 } else {
347 return chan->backend.subbuf_size - subbuf_offset(o_begin, chan)
348 - sizeof(struct metadata_packet_header);
349 }
350}
351
c099397a 352static
a90917c3 353wait_queue_head_t *lttng_get_writer_buf_wait_queue(struct channel *chan, int cpu)
c099397a 354{
71c1d843
MD
355 struct lib_ring_buffer *buf = channel_get_ring_buffer(&client_config,
356 chan, cpu);
357 return &buf->write_wait;
24cedcfe
MD
358}
359
360static
a90917c3 361wait_queue_head_t *lttng_get_hp_wait_queue(struct channel *chan)
24cedcfe
MD
362{
363 return &chan->hp_wait;
364}
365
366static
a90917c3 367int lttng_is_finalized(struct channel *chan)
24cedcfe
MD
368{
369 return lib_ring_buffer_channel_is_finalized(chan);
c099397a
MD
370}
371
254ec7bc 372static
a90917c3 373int lttng_is_disabled(struct channel *chan)
254ec7bc
MD
374{
375 return lib_ring_buffer_channel_is_disabled(chan);
376}
377
a90917c3 378static struct lttng_transport lttng_relay_transport = {
881833e3
MD
379 .name = "relay-" RING_BUFFER_MODE_TEMPLATE_STRING,
380 .owner = THIS_MODULE,
381 .ops = {
382 .channel_create = _channel_create,
a90917c3
MD
383 .channel_destroy = lttng_channel_destroy,
384 .buffer_read_open = lttng_buffer_read_open,
f71ecafa 385 .buffer_has_read_closed_stream =
a90917c3
MD
386 lttng_buffer_has_read_closed_stream,
387 .buffer_read_close = lttng_buffer_read_close,
388 .event_reserve = lttng_event_reserve,
389 .event_commit = lttng_event_commit,
390 .event_write_from_user = lttng_event_write_from_user,
391 .event_memset = lttng_event_memset,
392 .event_write = lttng_event_write,
16f78f3a 393 .event_strcpy = lttng_event_strcpy,
a90917c3
MD
394 .packet_avail_size = lttng_packet_avail_size,
395 .get_writer_buf_wait_queue = lttng_get_writer_buf_wait_queue,
396 .get_hp_wait_queue = lttng_get_hp_wait_queue,
397 .is_finalized = lttng_is_finalized,
398 .is_disabled = lttng_is_disabled,
67a80835
MD
399 .timestamp_begin = client_timestamp_begin,
400 .timestamp_end = client_timestamp_end,
401 .events_discarded = client_events_discarded,
402 .content_size = client_content_size,
403 .packet_size = client_packet_size,
404 .stream_id = client_stream_id,
405 .current_timestamp = client_current_timestamp,
881833e3
MD
406 },
407};
408
a90917c3 409static int __init lttng_ring_buffer_client_init(void)
881833e3
MD
410{
411 /*
412 * This vmalloc sync all also takes care of the lib ring buffer
413 * vmalloc'd module pages when it is built as a module into LTTng.
414 */
415 wrapper_vmalloc_sync_all();
a90917c3 416 lttng_transport_register(&lttng_relay_transport);
881833e3
MD
417 return 0;
418}
419
a90917c3 420module_init(lttng_ring_buffer_client_init);
881833e3 421
a90917c3 422static void __exit lttng_ring_buffer_client_exit(void)
881833e3 423{
a90917c3 424 lttng_transport_unregister(&lttng_relay_transport);
881833e3
MD
425}
426
a90917c3 427module_exit(lttng_ring_buffer_client_exit);
881833e3
MD
428
429MODULE_LICENSE("GPL and additional rights");
430MODULE_AUTHOR("Mathieu Desnoyers");
431MODULE_DESCRIPTION("LTTng ring buffer " RING_BUFFER_MODE_TEMPLATE_STRING
432 " client");
This page took 0.045801 seconds and 4 git commands to generate.