Fix: ensure power of 2 check handles 64-bit size_t entirely
[lttng-modules.git] / lttng-ring-buffer-metadata-client.h
CommitLineData
881833e3 1/*
a90917c3 2 * lttng-ring-buffer-client.h
881833e3
MD
3 *
4 * Copyright (C) 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 *
6 * LTTng lib ring buffer client template.
7 *
8 * Dual LGPL v2.1/GPL v2 license.
9 */
10
11#include <linux/module.h>
12#include <linux/types.h>
13#include "wrapper/vmalloc.h" /* for wrapper_vmalloc_sync_all() */
a90917c3
MD
14#include "lttng-events.h"
15#include "lttng-tracer.h"
881833e3 16
881833e3
MD
17struct metadata_packet_header {
18 uint32_t magic; /* 0x75D11D57 */
1ec3f75a 19 uint8_t uuid[16]; /* Unique Universal Identifier */
881833e3
MD
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 */
4173df96
MD
26 uint8_t major; /* CTF spec major version number */
27 uint8_t minor; /* CTF spec minor version number */
881833e3
MD
28 uint8_t header_end[0];
29};
30
31struct metadata_record_header {
32 uint8_t header_end[0]; /* End of header */
33};
34
35static const struct lib_ring_buffer_config client_config;
36
37static inline
38u64 lib_ring_buffer_clock_read(struct channel *chan)
39{
40 return 0;
41}
42
43static inline
44unsigned char record_header_size(const struct lib_ring_buffer_config *config,
45 struct channel *chan, size_t offset,
64c796d8 46 size_t *pre_header_padding,
881833e3
MD
47 struct lib_ring_buffer_ctx *ctx)
48{
49 return 0;
50}
51
52#include "wrapper/ringbuffer/api.h"
53
54static u64 client_ring_buffer_clock_read(struct channel *chan)
55{
56 return 0;
57}
58
59static
60size_t client_record_header_size(const struct lib_ring_buffer_config *config,
61 struct channel *chan, size_t offset,
881833e3 62 size_t *pre_header_padding,
881833e3
MD
63 struct lib_ring_buffer_ctx *ctx)
64{
65 return 0;
66}
67
68/**
69 * client_packet_header_size - called on buffer-switch to a new sub-buffer
70 *
71 * Return header size without padding after the structure. Don't use packed
72 * structure because gcc generates inefficient code on some architectures
73 * (powerpc, mips..)
74 */
75static size_t client_packet_header_size(void)
76{
77 return offsetof(struct metadata_packet_header, header_end);
78}
79
80static void client_buffer_begin(struct lib_ring_buffer *buf, u64 tsc,
81 unsigned int subbuf_idx)
82{
83 struct channel *chan = buf->backend.chan;
84 struct metadata_packet_header *header =
85 (struct metadata_packet_header *)
86 lib_ring_buffer_offset_address(&buf->backend,
87 subbuf_idx * chan->backend.subbuf_size);
a90917c3
MD
88 struct lttng_channel *lttng_chan = channel_get_private(chan);
89 struct lttng_session *session = lttng_chan->session;
881833e3
MD
90
91 header->magic = TSDL_MAGIC_NUMBER;
1ec3f75a 92 memcpy(header->uuid, session->uuid.b, sizeof(session->uuid));
881833e3
MD
93 header->checksum = 0; /* 0 if unused */
94 header->content_size = 0xFFFFFFFF; /* in bits, for debugging */
95 header->packet_size = 0xFFFFFFFF; /* in bits, for debugging */
96 header->compression_scheme = 0; /* 0 if unused */
97 header->encryption_scheme = 0; /* 0 if unused */
98 header->checksum_scheme = 0; /* 0 if unused */
4173df96
MD
99 header->major = CTF_SPEC_MAJOR;
100 header->minor = CTF_SPEC_MINOR;
881833e3
MD
101}
102
103/*
104 * offset is assumed to never be 0 here : never deliver a completely empty
105 * subbuffer. data_size is between 1 and subbuf_size.
106 */
107static void client_buffer_end(struct lib_ring_buffer *buf, u64 tsc,
108 unsigned int subbuf_idx, unsigned long data_size)
109{
110 struct channel *chan = buf->backend.chan;
d793d5e1 111 struct metadata_packet_header *header =
1ec3f75a 112 (struct metadata_packet_header *)
881833e3
MD
113 lib_ring_buffer_offset_address(&buf->backend,
114 subbuf_idx * chan->backend.subbuf_size);
115 unsigned long records_lost = 0;
116
117 header->content_size = data_size * CHAR_BIT; /* in bits */
118 header->packet_size = PAGE_ALIGN(data_size) * CHAR_BIT; /* in bits */
71c1d843
MD
119 /*
120 * We do not care about the records lost count, because the metadata
121 * channel waits and retry.
122 */
123 (void) lib_ring_buffer_get_records_lost_full(&client_config, buf);
881833e3
MD
124 records_lost += lib_ring_buffer_get_records_lost_wrap(&client_config, buf);
125 records_lost += lib_ring_buffer_get_records_lost_big(&client_config, buf);
126 WARN_ON_ONCE(records_lost != 0);
127}
128
129static int client_buffer_create(struct lib_ring_buffer *buf, void *priv,
130 int cpu, const char *name)
131{
132 return 0;
133}
134
135static void client_buffer_finalize(struct lib_ring_buffer *buf, void *priv, int cpu)
136{
137}
138
139static const struct lib_ring_buffer_config client_config = {
140 .cb.ring_buffer_clock_read = client_ring_buffer_clock_read,
141 .cb.record_header_size = client_record_header_size,
142 .cb.subbuffer_header_size = client_packet_header_size,
143 .cb.buffer_begin = client_buffer_begin,
144 .cb.buffer_end = client_buffer_end,
145 .cb.buffer_create = client_buffer_create,
146 .cb.buffer_finalize = client_buffer_finalize,
147
148 .tsc_bits = 0,
149 .alloc = RING_BUFFER_ALLOC_GLOBAL,
150 .sync = RING_BUFFER_SYNC_GLOBAL,
151 .mode = RING_BUFFER_MODE_TEMPLATE,
152 .backend = RING_BUFFER_PAGE,
2db1399a 153 .output = RING_BUFFER_OUTPUT_TEMPLATE,
881833e3
MD
154 .oops = RING_BUFFER_OOPS_CONSISTENCY,
155 .ipi = RING_BUFFER_IPI_BARRIER,
156 .wakeup = RING_BUFFER_WAKEUP_BY_TIMER,
157};
158
159static
160struct channel *_channel_create(const char *name,
a90917c3 161 struct lttng_channel *lttng_chan, void *buf_addr,
881833e3
MD
162 size_t subbuf_size, size_t num_subbuf,
163 unsigned int switch_timer_interval,
164 unsigned int read_timer_interval)
165{
a90917c3 166 return channel_create(&client_config, name, lttng_chan, buf_addr,
881833e3
MD
167 subbuf_size, num_subbuf, switch_timer_interval,
168 read_timer_interval);
169}
170
171static
a90917c3 172void lttng_channel_destroy(struct channel *chan)
881833e3
MD
173{
174 channel_destroy(chan);
175}
176
177static
a90917c3 178struct lib_ring_buffer *lttng_buffer_read_open(struct channel *chan)
881833e3
MD
179{
180 struct lib_ring_buffer *buf;
881833e3 181
cd4bd11f
MD
182 buf = channel_get_ring_buffer(&client_config, chan, 0);
183 if (!lib_ring_buffer_open_read(buf))
184 return buf;
881833e3
MD
185 return NULL;
186}
187
f71ecafa 188static
a90917c3 189int lttng_buffer_has_read_closed_stream(struct channel *chan)
f71ecafa
MD
190{
191 struct lib_ring_buffer *buf;
192 int cpu;
193
194 for_each_channel_cpu(cpu, chan) {
195 buf = channel_get_ring_buffer(&client_config, chan, cpu);
196 if (!atomic_long_read(&buf->active_readers))
197 return 1;
198 }
199 return 0;
200}
201
881833e3 202static
a90917c3 203void lttng_buffer_read_close(struct lib_ring_buffer *buf)
881833e3
MD
204{
205 lib_ring_buffer_release_read(buf);
881833e3
MD
206}
207
c099397a 208static
a90917c3 209int lttng_event_reserve(struct lib_ring_buffer_ctx *ctx, uint32_t event_id)
881833e3
MD
210{
211 return lib_ring_buffer_reserve(&client_config, ctx);
212}
213
c099397a 214static
a90917c3 215void lttng_event_commit(struct lib_ring_buffer_ctx *ctx)
881833e3
MD
216{
217 lib_ring_buffer_commit(&client_config, ctx);
218}
219
c099397a 220static
a90917c3 221void lttng_event_write(struct lib_ring_buffer_ctx *ctx, const void *src,
881833e3
MD
222 size_t len)
223{
224 lib_ring_buffer_write(&client_config, ctx, src, len);
225}
226
4ea00e4f 227static
a90917c3 228void lttng_event_write_from_user(struct lib_ring_buffer_ctx *ctx,
4ea00e4f
JD
229 const void __user *src, size_t len)
230{
231 lib_ring_buffer_copy_from_user(&client_config, ctx, src, len);
232}
233
58aa5d24 234static
a90917c3 235void lttng_event_memset(struct lib_ring_buffer_ctx *ctx,
58aa5d24
MD
236 int c, size_t len)
237{
238 lib_ring_buffer_memset(&client_config, ctx, c, len);
239}
240
1ec3f75a 241static
a90917c3 242size_t lttng_packet_avail_size(struct channel *chan)
1ec3f75a
MD
243
244{
245 unsigned long o_begin;
246 struct lib_ring_buffer *buf;
247
248 buf = chan->backend.buf; /* Only for global buffer ! */
249 o_begin = v_read(&client_config, &buf->offset);
250 if (subbuf_offset(o_begin, chan) != 0) {
251 return chan->backend.subbuf_size - subbuf_offset(o_begin, chan);
252 } else {
253 return chan->backend.subbuf_size - subbuf_offset(o_begin, chan)
254 - sizeof(struct metadata_packet_header);
255 }
256}
257
c099397a 258static
a90917c3 259wait_queue_head_t *lttng_get_writer_buf_wait_queue(struct channel *chan, int cpu)
c099397a 260{
71c1d843
MD
261 struct lib_ring_buffer *buf = channel_get_ring_buffer(&client_config,
262 chan, cpu);
263 return &buf->write_wait;
24cedcfe
MD
264}
265
266static
a90917c3 267wait_queue_head_t *lttng_get_hp_wait_queue(struct channel *chan)
24cedcfe
MD
268{
269 return &chan->hp_wait;
270}
271
272static
a90917c3 273int lttng_is_finalized(struct channel *chan)
24cedcfe
MD
274{
275 return lib_ring_buffer_channel_is_finalized(chan);
c099397a
MD
276}
277
254ec7bc 278static
a90917c3 279int lttng_is_disabled(struct channel *chan)
254ec7bc
MD
280{
281 return lib_ring_buffer_channel_is_disabled(chan);
282}
283
a90917c3 284static struct lttng_transport lttng_relay_transport = {
881833e3
MD
285 .name = "relay-" RING_BUFFER_MODE_TEMPLATE_STRING,
286 .owner = THIS_MODULE,
287 .ops = {
288 .channel_create = _channel_create,
a90917c3
MD
289 .channel_destroy = lttng_channel_destroy,
290 .buffer_read_open = lttng_buffer_read_open,
f71ecafa 291 .buffer_has_read_closed_stream =
a90917c3
MD
292 lttng_buffer_has_read_closed_stream,
293 .buffer_read_close = lttng_buffer_read_close,
294 .event_reserve = lttng_event_reserve,
295 .event_commit = lttng_event_commit,
296 .event_write_from_user = lttng_event_write_from_user,
297 .event_memset = lttng_event_memset,
298 .event_write = lttng_event_write,
299 .packet_avail_size = lttng_packet_avail_size,
300 .get_writer_buf_wait_queue = lttng_get_writer_buf_wait_queue,
301 .get_hp_wait_queue = lttng_get_hp_wait_queue,
302 .is_finalized = lttng_is_finalized,
303 .is_disabled = lttng_is_disabled,
881833e3
MD
304 },
305};
306
a90917c3 307static int __init lttng_ring_buffer_client_init(void)
881833e3
MD
308{
309 /*
310 * This vmalloc sync all also takes care of the lib ring buffer
311 * vmalloc'd module pages when it is built as a module into LTTng.
312 */
313 wrapper_vmalloc_sync_all();
a90917c3 314 lttng_transport_register(&lttng_relay_transport);
881833e3
MD
315 return 0;
316}
317
a90917c3 318module_init(lttng_ring_buffer_client_init);
881833e3 319
a90917c3 320static void __exit lttng_ring_buffer_client_exit(void)
881833e3 321{
a90917c3 322 lttng_transport_unregister(&lttng_relay_transport);
881833e3
MD
323}
324
a90917c3 325module_exit(lttng_ring_buffer_client_exit);
881833e3
MD
326
327MODULE_LICENSE("GPL and additional rights");
328MODULE_AUTHOR("Mathieu Desnoyers");
329MODULE_DESCRIPTION("LTTng ring buffer " RING_BUFFER_MODE_TEMPLATE_STRING
330 " client");
This page took 0.038884 seconds and 4 git commands to generate.