Add a packet sequence number
[lttng-modules.git] / lttng-ring-buffer-metadata-client.h
1 /*
2 * lttng-ring-buffer-client.h
3 *
4 * LTTng lib ring buffer client template.
5 *
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
21 */
22
23 #include <linux/module.h>
24 #include <linux/types.h>
25 #include "wrapper/vmalloc.h" /* for wrapper_vmalloc_sync_all() */
26 #include "lttng-events.h"
27 #include "lttng-tracer.h"
28
29 static struct lttng_transport lttng_relay_transport;
30
31 struct metadata_packet_header {
32 uint32_t magic; /* 0x75D11D57 */
33 uint8_t uuid[16]; /* Unique Universal Identifier */
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 */
40 uint8_t major; /* CTF spec major version number */
41 uint8_t minor; /* CTF spec minor version number */
42 uint8_t header_end[0];
43 };
44
45 struct metadata_record_header {
46 uint8_t header_end[0]; /* End of header */
47 };
48
49 static const struct lib_ring_buffer_config client_config;
50
51 static inline
52 u64 lib_ring_buffer_clock_read(struct channel *chan)
53 {
54 return 0;
55 }
56
57 static inline
58 unsigned char record_header_size(const struct lib_ring_buffer_config *config,
59 struct channel *chan, size_t offset,
60 size_t *pre_header_padding,
61 struct lib_ring_buffer_ctx *ctx)
62 {
63 return 0;
64 }
65
66 #include "wrapper/ringbuffer/api.h"
67
68 static u64 client_ring_buffer_clock_read(struct channel *chan)
69 {
70 return 0;
71 }
72
73 static
74 size_t client_record_header_size(const struct lib_ring_buffer_config *config,
75 struct channel *chan, size_t offset,
76 size_t *pre_header_padding,
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 */
89 static size_t client_packet_header_size(void)
90 {
91 return offsetof(struct metadata_packet_header, header_end);
92 }
93
94 static 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);
102 struct lttng_metadata_cache *metadata_cache =
103 channel_get_private(chan);
104
105 header->magic = TSDL_MAGIC_NUMBER;
106 memcpy(header->uuid, metadata_cache->uuid.b,
107 sizeof(metadata_cache->uuid));
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 */
114 header->major = CTF_SPEC_MAJOR;
115 header->minor = CTF_SPEC_MINOR;
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 */
122 static 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;
126 struct metadata_packet_header *header =
127 (struct metadata_packet_header *)
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 */
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);
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
144 static int client_buffer_create(struct lib_ring_buffer *buf, void *priv,
145 int cpu, const char *name)
146 {
147 return 0;
148 }
149
150 static void client_buffer_finalize(struct lib_ring_buffer *buf, void *priv, int cpu)
151 {
152 }
153
154 static 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
160 static 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
167 static 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
174 static 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
181 static 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
188 static 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
195 static 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
202 static 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
209 static const struct lib_ring_buffer_config client_config = {
210 .cb.ring_buffer_clock_read = client_ring_buffer_clock_read,
211 .cb.record_header_size = client_record_header_size,
212 .cb.subbuffer_header_size = client_packet_header_size,
213 .cb.buffer_begin = client_buffer_begin,
214 .cb.buffer_end = client_buffer_end,
215 .cb.buffer_create = client_buffer_create,
216 .cb.buffer_finalize = client_buffer_finalize,
217
218 .tsc_bits = 0,
219 .alloc = RING_BUFFER_ALLOC_GLOBAL,
220 .sync = RING_BUFFER_SYNC_GLOBAL,
221 .mode = RING_BUFFER_MODE_TEMPLATE,
222 .backend = RING_BUFFER_PAGE,
223 .output = RING_BUFFER_OUTPUT_TEMPLATE,
224 .oops = RING_BUFFER_OOPS_CONSISTENCY,
225 .ipi = RING_BUFFER_IPI_BARRIER,
226 .wakeup = RING_BUFFER_WAKEUP_BY_TIMER,
227 };
228
229 static
230 void release_priv_ops(void *priv_ops)
231 {
232 module_put(THIS_MODULE);
233 }
234
235 static
236 void lttng_channel_destroy(struct channel *chan)
237 {
238 channel_destroy(chan);
239 }
240
241 static
242 struct channel *_channel_create(const char *name,
243 struct lttng_channel *lttng_chan, void *buf_addr,
244 size_t subbuf_size, size_t num_subbuf,
245 unsigned int switch_timer_interval,
246 unsigned int read_timer_interval)
247 {
248 struct channel *chan;
249
250 chan = channel_create(&client_config, name,
251 lttng_chan->session->metadata_cache, buf_addr,
252 subbuf_size, num_subbuf, switch_timer_interval,
253 read_timer_interval);
254 if (chan) {
255 /*
256 * Ensure this module is not unloaded before we finish
257 * using lttng_relay_transport.ops.
258 */
259 if (!try_module_get(THIS_MODULE)) {
260 printk(KERN_WARNING "LTT : Can't lock transport module.\n");
261 goto error;
262 }
263 chan->backend.priv_ops = &lttng_relay_transport.ops;
264 chan->backend.release_priv_ops = release_priv_ops;
265 }
266 return chan;
267
268 error:
269 lttng_channel_destroy(chan);
270 return NULL;
271 }
272
273 static
274 struct lib_ring_buffer *lttng_buffer_read_open(struct channel *chan)
275 {
276 struct lib_ring_buffer *buf;
277
278 buf = channel_get_ring_buffer(&client_config, chan, 0);
279 if (!lib_ring_buffer_open_read(buf))
280 return buf;
281 return NULL;
282 }
283
284 static
285 int lttng_buffer_has_read_closed_stream(struct channel *chan)
286 {
287 struct lib_ring_buffer *buf;
288 int cpu;
289
290 for_each_channel_cpu(cpu, chan) {
291 buf = channel_get_ring_buffer(&client_config, chan, cpu);
292 if (!atomic_long_read(&buf->active_readers))
293 return 1;
294 }
295 return 0;
296 }
297
298 static
299 void lttng_buffer_read_close(struct lib_ring_buffer *buf)
300 {
301 lib_ring_buffer_release_read(buf);
302 }
303
304 static
305 int lttng_event_reserve(struct lib_ring_buffer_ctx *ctx, uint32_t event_id)
306 {
307 return lib_ring_buffer_reserve(&client_config, ctx);
308 }
309
310 static
311 void lttng_event_commit(struct lib_ring_buffer_ctx *ctx)
312 {
313 lib_ring_buffer_commit(&client_config, ctx);
314 }
315
316 static
317 void lttng_event_write(struct lib_ring_buffer_ctx *ctx, const void *src,
318 size_t len)
319 {
320 lib_ring_buffer_write(&client_config, ctx, src, len);
321 }
322
323 static
324 void lttng_event_write_from_user(struct lib_ring_buffer_ctx *ctx,
325 const void __user *src, size_t len)
326 {
327 lib_ring_buffer_copy_from_user_inatomic(&client_config, ctx, src, len);
328 }
329
330 static
331 void lttng_event_memset(struct lib_ring_buffer_ctx *ctx,
332 int c, size_t len)
333 {
334 lib_ring_buffer_memset(&client_config, ctx, c, len);
335 }
336
337 static
338 void lttng_event_strcpy(struct lib_ring_buffer_ctx *ctx, const char *src,
339 size_t len)
340 {
341 lib_ring_buffer_strcpy(&client_config, ctx, src, len, '#');
342 }
343
344 static
345 size_t lttng_packet_avail_size(struct channel *chan)
346
347 {
348 unsigned long o_begin;
349 struct lib_ring_buffer *buf;
350
351 buf = chan->backend.buf; /* Only for global buffer ! */
352 o_begin = v_read(&client_config, &buf->offset);
353 if (subbuf_offset(o_begin, chan) != 0) {
354 return chan->backend.subbuf_size - subbuf_offset(o_begin, chan);
355 } else {
356 return chan->backend.subbuf_size - subbuf_offset(o_begin, chan)
357 - sizeof(struct metadata_packet_header);
358 }
359 }
360
361 static
362 wait_queue_head_t *lttng_get_writer_buf_wait_queue(struct channel *chan, int cpu)
363 {
364 struct lib_ring_buffer *buf = channel_get_ring_buffer(&client_config,
365 chan, cpu);
366 return &buf->write_wait;
367 }
368
369 static
370 wait_queue_head_t *lttng_get_hp_wait_queue(struct channel *chan)
371 {
372 return &chan->hp_wait;
373 }
374
375 static
376 int lttng_is_finalized(struct channel *chan)
377 {
378 return lib_ring_buffer_channel_is_finalized(chan);
379 }
380
381 static
382 int lttng_is_disabled(struct channel *chan)
383 {
384 return lib_ring_buffer_channel_is_disabled(chan);
385 }
386
387 static struct lttng_transport lttng_relay_transport = {
388 .name = "relay-" RING_BUFFER_MODE_TEMPLATE_STRING,
389 .owner = THIS_MODULE,
390 .ops = {
391 .channel_create = _channel_create,
392 .channel_destroy = lttng_channel_destroy,
393 .buffer_read_open = lttng_buffer_read_open,
394 .buffer_has_read_closed_stream =
395 lttng_buffer_has_read_closed_stream,
396 .buffer_read_close = lttng_buffer_read_close,
397 .event_reserve = lttng_event_reserve,
398 .event_commit = lttng_event_commit,
399 .event_write_from_user = lttng_event_write_from_user,
400 .event_memset = lttng_event_memset,
401 .event_write = lttng_event_write,
402 .event_strcpy = lttng_event_strcpy,
403 .packet_avail_size = lttng_packet_avail_size,
404 .get_writer_buf_wait_queue = lttng_get_writer_buf_wait_queue,
405 .get_hp_wait_queue = lttng_get_hp_wait_queue,
406 .is_finalized = lttng_is_finalized,
407 .is_disabled = lttng_is_disabled,
408 .timestamp_begin = client_timestamp_begin,
409 .timestamp_end = client_timestamp_end,
410 .events_discarded = client_events_discarded,
411 .content_size = client_content_size,
412 .packet_size = client_packet_size,
413 .stream_id = client_stream_id,
414 .current_timestamp = client_current_timestamp,
415 .sequence_number = client_sequence_number,
416 },
417 };
418
419 static int __init lttng_ring_buffer_client_init(void)
420 {
421 /*
422 * This vmalloc sync all also takes care of the lib ring buffer
423 * vmalloc'd module pages when it is built as a module into LTTng.
424 */
425 wrapper_vmalloc_sync_all();
426 lttng_transport_register(&lttng_relay_transport);
427 return 0;
428 }
429
430 module_init(lttng_ring_buffer_client_init);
431
432 static void __exit lttng_ring_buffer_client_exit(void)
433 {
434 lttng_transport_unregister(&lttng_relay_transport);
435 }
436
437 module_exit(lttng_ring_buffer_client_exit);
438
439 MODULE_LICENSE("GPL and additional rights");
440 MODULE_AUTHOR("Mathieu Desnoyers");
441 MODULE_DESCRIPTION("LTTng ring buffer " RING_BUFFER_MODE_TEMPLATE_STRING
442 " client");
This page took 0.045336 seconds and 5 git commands to generate.