2 * lttng-ring-buffer-client.h
4 * LTTng lib ring buffer client template.
6 * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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.
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.
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
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"
29 static struct lttng_transport lttng_relay_transport
;
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];
45 struct metadata_record_header
{
46 uint8_t header_end
[0]; /* End of header */
49 static const struct lib_ring_buffer_config client_config
;
52 u64
lib_ring_buffer_clock_read(struct channel
*chan
)
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
)
66 #include "wrapper/ringbuffer/api.h"
68 static u64
client_ring_buffer_clock_read(struct channel
*chan
)
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
)
83 * client_packet_header_size - called on buffer-switch to a new sub-buffer
85 * Return header size without padding after the structure. Don't use packed
86 * structure because gcc generates inefficient code on some architectures
89 static size_t client_packet_header_size(void)
91 return offsetof(struct metadata_packet_header
, header_end
);
94 static void client_buffer_begin(struct lib_ring_buffer
*buf
, u64 tsc
,
95 unsigned int subbuf_idx
)
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
);
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
;
119 * offset is assumed to never be 0 here : never deliver a completely empty
120 * subbuffer. data_size is between 1 and subbuf_size.
122 static void client_buffer_end(struct lib_ring_buffer
*buf
, u64 tsc
,
123 unsigned int subbuf_idx
, unsigned long data_size
)
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;
132 header
->content_size
= data_size
* CHAR_BIT
; /* in bits */
133 header
->packet_size
= PAGE_ALIGN(data_size
) * CHAR_BIT
; /* in bits */
135 * We do not care about the records lost count, because the metadata
136 * channel waits and retry.
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);
144 static int client_buffer_create(struct lib_ring_buffer
*buf
, void *priv
,
145 int cpu
, const char *name
)
150 static void client_buffer_finalize(struct lib_ring_buffer
*buf
, void *priv
, int cpu
)
154 static int client_timestamp_begin(const struct lib_ring_buffer_config
*config
,
155 struct lib_ring_buffer
*buf
, uint64_t *timestamp_begin
)
160 static int client_timestamp_end(const struct lib_ring_buffer_config
*config
,
161 struct lib_ring_buffer
*bufb
,
162 uint64_t *timestamp_end
)
167 static int client_events_discarded(const struct lib_ring_buffer_config
*config
,
168 struct lib_ring_buffer
*bufb
,
169 uint64_t *events_discarded
)
174 static int client_current_timestamp(const struct lib_ring_buffer_config
*config
,
175 struct lib_ring_buffer
*bufb
,
181 static int client_content_size(const struct lib_ring_buffer_config
*config
,
182 struct lib_ring_buffer
*bufb
,
183 uint64_t *content_size
)
188 static int client_packet_size(const struct lib_ring_buffer_config
*config
,
189 struct lib_ring_buffer
*bufb
,
190 uint64_t *packet_size
)
195 static int client_stream_id(const struct lib_ring_buffer_config
*config
,
196 struct lib_ring_buffer
*bufb
,
202 static const struct lib_ring_buffer_config client_config
= {
203 .cb
.ring_buffer_clock_read
= client_ring_buffer_clock_read
,
204 .cb
.record_header_size
= client_record_header_size
,
205 .cb
.subbuffer_header_size
= client_packet_header_size
,
206 .cb
.buffer_begin
= client_buffer_begin
,
207 .cb
.buffer_end
= client_buffer_end
,
208 .cb
.buffer_create
= client_buffer_create
,
209 .cb
.buffer_finalize
= client_buffer_finalize
,
212 .alloc
= RING_BUFFER_ALLOC_GLOBAL
,
213 .sync
= RING_BUFFER_SYNC_GLOBAL
,
214 .mode
= RING_BUFFER_MODE_TEMPLATE
,
215 .backend
= RING_BUFFER_PAGE
,
216 .output
= RING_BUFFER_OUTPUT_TEMPLATE
,
217 .oops
= RING_BUFFER_OOPS_CONSISTENCY
,
218 .ipi
= RING_BUFFER_IPI_BARRIER
,
219 .wakeup
= RING_BUFFER_WAKEUP_BY_TIMER
,
223 void release_priv_ops(void *priv_ops
)
225 module_put(THIS_MODULE
);
229 void lttng_channel_destroy(struct channel
*chan
)
231 channel_destroy(chan
);
235 struct channel
*_channel_create(const char *name
,
236 struct lttng_channel
*lttng_chan
, void *buf_addr
,
237 size_t subbuf_size
, size_t num_subbuf
,
238 unsigned int switch_timer_interval
,
239 unsigned int read_timer_interval
)
241 struct channel
*chan
;
243 chan
= channel_create(&client_config
, name
,
244 lttng_chan
->session
->metadata_cache
, buf_addr
,
245 subbuf_size
, num_subbuf
, switch_timer_interval
,
246 read_timer_interval
);
249 * Ensure this module is not unloaded before we finish
250 * using lttng_relay_transport.ops.
252 if (!try_module_get(THIS_MODULE
)) {
253 printk(KERN_WARNING
"LTT : Can't lock transport module.\n");
256 chan
->backend
.priv_ops
= <tng_relay_transport
.ops
;
257 chan
->backend
.release_priv_ops
= release_priv_ops
;
262 lttng_channel_destroy(chan
);
267 struct lib_ring_buffer
*lttng_buffer_read_open(struct channel
*chan
)
269 struct lib_ring_buffer
*buf
;
271 buf
= channel_get_ring_buffer(&client_config
, chan
, 0);
272 if (!lib_ring_buffer_open_read(buf
))
278 int lttng_buffer_has_read_closed_stream(struct channel
*chan
)
280 struct lib_ring_buffer
*buf
;
283 for_each_channel_cpu(cpu
, chan
) {
284 buf
= channel_get_ring_buffer(&client_config
, chan
, cpu
);
285 if (!atomic_long_read(&buf
->active_readers
))
292 void lttng_buffer_read_close(struct lib_ring_buffer
*buf
)
294 lib_ring_buffer_release_read(buf
);
298 int lttng_event_reserve(struct lib_ring_buffer_ctx
*ctx
, uint32_t event_id
)
300 return lib_ring_buffer_reserve(&client_config
, ctx
);
304 void lttng_event_commit(struct lib_ring_buffer_ctx
*ctx
)
306 lib_ring_buffer_commit(&client_config
, ctx
);
310 void lttng_event_write(struct lib_ring_buffer_ctx
*ctx
, const void *src
,
313 lib_ring_buffer_write(&client_config
, ctx
, src
, len
);
317 void lttng_event_write_from_user(struct lib_ring_buffer_ctx
*ctx
,
318 const void __user
*src
, size_t len
)
320 lib_ring_buffer_copy_from_user_inatomic(&client_config
, ctx
, src
, len
);
324 void lttng_event_memset(struct lib_ring_buffer_ctx
*ctx
,
327 lib_ring_buffer_memset(&client_config
, ctx
, c
, len
);
331 void lttng_event_strcpy(struct lib_ring_buffer_ctx
*ctx
, const char *src
,
334 lib_ring_buffer_strcpy(&client_config
, ctx
, src
, len
, '#');
338 size_t lttng_packet_avail_size(struct channel
*chan
)
341 unsigned long o_begin
;
342 struct lib_ring_buffer
*buf
;
344 buf
= chan
->backend
.buf
; /* Only for global buffer ! */
345 o_begin
= v_read(&client_config
, &buf
->offset
);
346 if (subbuf_offset(o_begin
, chan
) != 0) {
347 return chan
->backend
.subbuf_size
- subbuf_offset(o_begin
, chan
);
349 return chan
->backend
.subbuf_size
- subbuf_offset(o_begin
, chan
)
350 - sizeof(struct metadata_packet_header
);
355 wait_queue_head_t
*lttng_get_writer_buf_wait_queue(struct channel
*chan
, int cpu
)
357 struct lib_ring_buffer
*buf
= channel_get_ring_buffer(&client_config
,
359 return &buf
->write_wait
;
363 wait_queue_head_t
*lttng_get_hp_wait_queue(struct channel
*chan
)
365 return &chan
->hp_wait
;
369 int lttng_is_finalized(struct channel
*chan
)
371 return lib_ring_buffer_channel_is_finalized(chan
);
375 int lttng_is_disabled(struct channel
*chan
)
377 return lib_ring_buffer_channel_is_disabled(chan
);
380 static struct lttng_transport lttng_relay_transport
= {
381 .name
= "relay-" RING_BUFFER_MODE_TEMPLATE_STRING
,
382 .owner
= THIS_MODULE
,
384 .channel_create
= _channel_create
,
385 .channel_destroy
= lttng_channel_destroy
,
386 .buffer_read_open
= lttng_buffer_read_open
,
387 .buffer_has_read_closed_stream
=
388 lttng_buffer_has_read_closed_stream
,
389 .buffer_read_close
= lttng_buffer_read_close
,
390 .event_reserve
= lttng_event_reserve
,
391 .event_commit
= lttng_event_commit
,
392 .event_write_from_user
= lttng_event_write_from_user
,
393 .event_memset
= lttng_event_memset
,
394 .event_write
= lttng_event_write
,
395 .event_strcpy
= lttng_event_strcpy
,
396 .packet_avail_size
= lttng_packet_avail_size
,
397 .get_writer_buf_wait_queue
= lttng_get_writer_buf_wait_queue
,
398 .get_hp_wait_queue
= lttng_get_hp_wait_queue
,
399 .is_finalized
= lttng_is_finalized
,
400 .is_disabled
= lttng_is_disabled
,
401 .timestamp_begin
= client_timestamp_begin
,
402 .timestamp_end
= client_timestamp_end
,
403 .events_discarded
= client_events_discarded
,
404 .content_size
= client_content_size
,
405 .packet_size
= client_packet_size
,
406 .stream_id
= client_stream_id
,
407 .current_timestamp
= client_current_timestamp
,
411 static int __init
lttng_ring_buffer_client_init(void)
414 * This vmalloc sync all also takes care of the lib ring buffer
415 * vmalloc'd module pages when it is built as a module into LTTng.
417 wrapper_vmalloc_sync_all();
418 lttng_transport_register(<tng_relay_transport
);
422 module_init(lttng_ring_buffer_client_init
);
424 static void __exit
lttng_ring_buffer_client_exit(void)
426 lttng_transport_unregister(<tng_relay_transport
);
429 module_exit(lttng_ring_buffer_client_exit
);
431 MODULE_LICENSE("GPL and additional rights");
432 MODULE_AUTHOR("Mathieu Desnoyers");
433 MODULE_DESCRIPTION("LTTng ring buffer " RING_BUFFER_MODE_TEMPLATE_STRING