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 struct metadata_packet_header
{
30 uint32_t magic
; /* 0x75D11D57 */
31 uint8_t uuid
[16]; /* Unique Universal Identifier */
32 uint32_t checksum
; /* 0 if unused */
33 uint32_t content_size
; /* in bits */
34 uint32_t packet_size
; /* in bits */
35 uint8_t compression_scheme
; /* 0 if unused */
36 uint8_t encryption_scheme
; /* 0 if unused */
37 uint8_t checksum_scheme
; /* 0 if unused */
38 uint8_t major
; /* CTF spec major version number */
39 uint8_t minor
; /* CTF spec minor version number */
40 uint8_t header_end
[0];
43 struct metadata_record_header
{
44 uint8_t header_end
[0]; /* End of header */
47 static const struct lib_ring_buffer_config client_config
;
50 u64
lib_ring_buffer_clock_read(struct channel
*chan
)
56 unsigned char record_header_size(const struct lib_ring_buffer_config
*config
,
57 struct channel
*chan
, size_t offset
,
58 size_t *pre_header_padding
,
59 struct lib_ring_buffer_ctx
*ctx
)
64 #include "wrapper/ringbuffer/api.h"
66 static u64
client_ring_buffer_clock_read(struct channel
*chan
)
72 size_t client_record_header_size(const struct lib_ring_buffer_config
*config
,
73 struct channel
*chan
, size_t offset
,
74 size_t *pre_header_padding
,
75 struct lib_ring_buffer_ctx
*ctx
)
81 * client_packet_header_size - called on buffer-switch to a new sub-buffer
83 * Return header size without padding after the structure. Don't use packed
84 * structure because gcc generates inefficient code on some architectures
87 static size_t client_packet_header_size(void)
89 return offsetof(struct metadata_packet_header
, header_end
);
92 static void client_buffer_begin(struct lib_ring_buffer
*buf
, u64 tsc
,
93 unsigned int subbuf_idx
)
95 struct channel
*chan
= buf
->backend
.chan
;
96 struct metadata_packet_header
*header
=
97 (struct metadata_packet_header
*)
98 lib_ring_buffer_offset_address(&buf
->backend
,
99 subbuf_idx
* chan
->backend
.subbuf_size
);
100 struct lttng_channel
*lttng_chan
= channel_get_private(chan
);
101 struct lttng_session
*session
= lttng_chan
->session
;
103 header
->magic
= TSDL_MAGIC_NUMBER
;
104 memcpy(header
->uuid
, session
->uuid
.b
, sizeof(session
->uuid
));
105 header
->checksum
= 0; /* 0 if unused */
106 header
->content_size
= 0xFFFFFFFF; /* in bits, for debugging */
107 header
->packet_size
= 0xFFFFFFFF; /* in bits, for debugging */
108 header
->compression_scheme
= 0; /* 0 if unused */
109 header
->encryption_scheme
= 0; /* 0 if unused */
110 header
->checksum_scheme
= 0; /* 0 if unused */
111 header
->major
= CTF_SPEC_MAJOR
;
112 header
->minor
= CTF_SPEC_MINOR
;
116 * offset is assumed to never be 0 here : never deliver a completely empty
117 * subbuffer. data_size is between 1 and subbuf_size.
119 static void client_buffer_end(struct lib_ring_buffer
*buf
, u64 tsc
,
120 unsigned int subbuf_idx
, unsigned long data_size
)
122 struct channel
*chan
= buf
->backend
.chan
;
123 struct metadata_packet_header
*header
=
124 (struct metadata_packet_header
*)
125 lib_ring_buffer_offset_address(&buf
->backend
,
126 subbuf_idx
* chan
->backend
.subbuf_size
);
127 unsigned long records_lost
= 0;
129 header
->content_size
= data_size
* CHAR_BIT
; /* in bits */
130 header
->packet_size
= PAGE_ALIGN(data_size
) * CHAR_BIT
; /* in bits */
132 * We do not care about the records lost count, because the metadata
133 * channel waits and retry.
135 (void) lib_ring_buffer_get_records_lost_full(&client_config
, buf
);
136 records_lost
+= lib_ring_buffer_get_records_lost_wrap(&client_config
, buf
);
137 records_lost
+= lib_ring_buffer_get_records_lost_big(&client_config
, buf
);
138 WARN_ON_ONCE(records_lost
!= 0);
141 static int client_buffer_create(struct lib_ring_buffer
*buf
, void *priv
,
142 int cpu
, const char *name
)
147 static void client_buffer_finalize(struct lib_ring_buffer
*buf
, void *priv
, int cpu
)
151 static const struct lib_ring_buffer_config client_config
= {
152 .cb
.ring_buffer_clock_read
= client_ring_buffer_clock_read
,
153 .cb
.record_header_size
= client_record_header_size
,
154 .cb
.subbuffer_header_size
= client_packet_header_size
,
155 .cb
.buffer_begin
= client_buffer_begin
,
156 .cb
.buffer_end
= client_buffer_end
,
157 .cb
.buffer_create
= client_buffer_create
,
158 .cb
.buffer_finalize
= client_buffer_finalize
,
161 .alloc
= RING_BUFFER_ALLOC_GLOBAL
,
162 .sync
= RING_BUFFER_SYNC_GLOBAL
,
163 .mode
= RING_BUFFER_MODE_TEMPLATE
,
164 .backend
= RING_BUFFER_PAGE
,
165 .output
= RING_BUFFER_OUTPUT_TEMPLATE
,
166 .oops
= RING_BUFFER_OOPS_CONSISTENCY
,
167 .ipi
= RING_BUFFER_IPI_BARRIER
,
168 .wakeup
= RING_BUFFER_WAKEUP_BY_TIMER
,
172 struct channel
*_channel_create(const char *name
,
173 struct lttng_channel
*lttng_chan
, void *buf_addr
,
174 size_t subbuf_size
, size_t num_subbuf
,
175 unsigned int switch_timer_interval
,
176 unsigned int read_timer_interval
)
178 return channel_create(&client_config
, name
, lttng_chan
, buf_addr
,
179 subbuf_size
, num_subbuf
, switch_timer_interval
,
180 read_timer_interval
);
184 void lttng_channel_destroy(struct channel
*chan
)
186 channel_destroy(chan
);
190 struct lib_ring_buffer
*lttng_buffer_read_open(struct channel
*chan
)
192 struct lib_ring_buffer
*buf
;
194 buf
= channel_get_ring_buffer(&client_config
, chan
, 0);
195 if (!lib_ring_buffer_open_read(buf
))
201 int lttng_buffer_has_read_closed_stream(struct channel
*chan
)
203 struct lib_ring_buffer
*buf
;
206 for_each_channel_cpu(cpu
, chan
) {
207 buf
= channel_get_ring_buffer(&client_config
, chan
, cpu
);
208 if (!atomic_long_read(&buf
->active_readers
))
215 void lttng_buffer_read_close(struct lib_ring_buffer
*buf
)
217 lib_ring_buffer_release_read(buf
);
221 int lttng_event_reserve(struct lib_ring_buffer_ctx
*ctx
, uint32_t event_id
)
223 return lib_ring_buffer_reserve(&client_config
, ctx
);
227 void lttng_event_commit(struct lib_ring_buffer_ctx
*ctx
)
229 lib_ring_buffer_commit(&client_config
, ctx
);
233 void lttng_event_write(struct lib_ring_buffer_ctx
*ctx
, const void *src
,
236 lib_ring_buffer_write(&client_config
, ctx
, src
, len
);
240 void lttng_event_write_from_user(struct lib_ring_buffer_ctx
*ctx
,
241 const void __user
*src
, size_t len
)
243 lib_ring_buffer_copy_from_user(&client_config
, ctx
, src
, len
);
247 void lttng_event_memset(struct lib_ring_buffer_ctx
*ctx
,
250 lib_ring_buffer_memset(&client_config
, ctx
, c
, len
);
254 size_t lttng_packet_avail_size(struct channel
*chan
)
257 unsigned long o_begin
;
258 struct lib_ring_buffer
*buf
;
260 buf
= chan
->backend
.buf
; /* Only for global buffer ! */
261 o_begin
= v_read(&client_config
, &buf
->offset
);
262 if (subbuf_offset(o_begin
, chan
) != 0) {
263 return chan
->backend
.subbuf_size
- subbuf_offset(o_begin
, chan
);
265 return chan
->backend
.subbuf_size
- subbuf_offset(o_begin
, chan
)
266 - sizeof(struct metadata_packet_header
);
271 wait_queue_head_t
*lttng_get_writer_buf_wait_queue(struct channel
*chan
, int cpu
)
273 struct lib_ring_buffer
*buf
= channel_get_ring_buffer(&client_config
,
275 return &buf
->write_wait
;
279 wait_queue_head_t
*lttng_get_hp_wait_queue(struct channel
*chan
)
281 return &chan
->hp_wait
;
285 int lttng_is_finalized(struct channel
*chan
)
287 return lib_ring_buffer_channel_is_finalized(chan
);
291 int lttng_is_disabled(struct channel
*chan
)
293 return lib_ring_buffer_channel_is_disabled(chan
);
296 static struct lttng_transport lttng_relay_transport
= {
297 .name
= "relay-" RING_BUFFER_MODE_TEMPLATE_STRING
,
298 .owner
= THIS_MODULE
,
300 .channel_create
= _channel_create
,
301 .channel_destroy
= lttng_channel_destroy
,
302 .buffer_read_open
= lttng_buffer_read_open
,
303 .buffer_has_read_closed_stream
=
304 lttng_buffer_has_read_closed_stream
,
305 .buffer_read_close
= lttng_buffer_read_close
,
306 .event_reserve
= lttng_event_reserve
,
307 .event_commit
= lttng_event_commit
,
308 .event_write_from_user
= lttng_event_write_from_user
,
309 .event_memset
= lttng_event_memset
,
310 .event_write
= lttng_event_write
,
311 .packet_avail_size
= lttng_packet_avail_size
,
312 .get_writer_buf_wait_queue
= lttng_get_writer_buf_wait_queue
,
313 .get_hp_wait_queue
= lttng_get_hp_wait_queue
,
314 .is_finalized
= lttng_is_finalized
,
315 .is_disabled
= lttng_is_disabled
,
319 static int __init
lttng_ring_buffer_client_init(void)
322 * This vmalloc sync all also takes care of the lib ring buffer
323 * vmalloc'd module pages when it is built as a module into LTTng.
325 wrapper_vmalloc_sync_all();
326 lttng_transport_register(<tng_relay_transport
);
330 module_init(lttng_ring_buffer_client_init
);
332 static void __exit
lttng_ring_buffer_client_exit(void)
334 lttng_transport_unregister(<tng_relay_transport
);
337 module_exit(lttng_ring_buffer_client_exit
);
339 MODULE_LICENSE("GPL and additional rights");
340 MODULE_AUTHOR("Mathieu Desnoyers");
341 MODULE_DESCRIPTION("LTTng ring buffer " RING_BUFFER_MODE_TEMPLATE_STRING