2 * ltt-ring-buffer-client.h
4 * Copyright (C) 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 * LTTng lib ring buffer client template.
8 * Dual LGPL v2.1/GPL v2 license.
11 #include <linux/module.h>
12 #include <linux/types.h>
13 #include "wrapper/vmalloc.h" /* for wrapper_vmalloc_sync_all() */
14 #include "ltt-events.h"
15 #include "ltt-tracer.h"
17 struct metadata_packet_header
{
18 uint32_t magic
; /* 0x75D11D57 */
19 uint8_t trace_uuid
[16]; /* Unique Universal Identifier */
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 */
26 uint8_t header_end
[0];
29 struct metadata_record_header
{
30 uint8_t header_end
[0]; /* End of header */
33 static const struct lib_ring_buffer_config client_config
;
36 u64
lib_ring_buffer_clock_read(struct channel
*chan
)
42 unsigned char record_header_size(const struct lib_ring_buffer_config
*config
,
43 struct channel
*chan
, size_t offset
,
44 size_t data_size
, size_t *pre_header_padding
,
46 struct lib_ring_buffer_ctx
*ctx
)
51 #include "wrapper/ringbuffer/api.h"
53 static u64
client_ring_buffer_clock_read(struct channel
*chan
)
59 size_t client_record_header_size(const struct lib_ring_buffer_config
*config
,
60 struct channel
*chan
, size_t offset
,
62 size_t *pre_header_padding
,
64 struct lib_ring_buffer_ctx
*ctx
)
70 * client_packet_header_size - called on buffer-switch to a new sub-buffer
72 * Return header size without padding after the structure. Don't use packed
73 * structure because gcc generates inefficient code on some architectures
76 static size_t client_packet_header_size(void)
78 return offsetof(struct metadata_packet_header
, header_end
);
81 static void client_buffer_begin(struct lib_ring_buffer
*buf
, u64 tsc
,
82 unsigned int subbuf_idx
)
84 struct channel
*chan
= buf
->backend
.chan
;
85 struct metadata_packet_header
*header
=
86 (struct metadata_packet_header
*)
87 lib_ring_buffer_offset_address(&buf
->backend
,
88 subbuf_idx
* chan
->backend
.subbuf_size
);
90 header
->magic
= TSDL_MAGIC_NUMBER
;
92 //header->trace_uuid = ; /* Unique Universal Identifier */
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 */
102 * offset is assumed to never be 0 here : never deliver a completely empty
103 * subbuffer. data_size is between 1 and subbuf_size.
105 static void client_buffer_end(struct lib_ring_buffer
*buf
, u64 tsc
,
106 unsigned int subbuf_idx
, unsigned long data_size
)
108 struct channel
*chan
= buf
->backend
.chan
;
109 struct packet_header
*header
=
110 (struct packet_header
*)
111 lib_ring_buffer_offset_address(&buf
->backend
,
112 subbuf_idx
* chan
->backend
.subbuf_size
);
113 unsigned long records_lost
= 0;
115 header
->content_size
= data_size
* CHAR_BIT
; /* in bits */
116 header
->packet_size
= PAGE_ALIGN(data_size
) * CHAR_BIT
; /* in bits */
117 records_lost
+= lib_ring_buffer_get_records_lost_full(&client_config
, buf
);
118 records_lost
+= lib_ring_buffer_get_records_lost_wrap(&client_config
, buf
);
119 records_lost
+= lib_ring_buffer_get_records_lost_big(&client_config
, buf
);
120 WARN_ON_ONCE(records_lost
!= 0);
123 static int client_buffer_create(struct lib_ring_buffer
*buf
, void *priv
,
124 int cpu
, const char *name
)
129 static void client_buffer_finalize(struct lib_ring_buffer
*buf
, void *priv
, int cpu
)
133 static const struct lib_ring_buffer_config client_config
= {
134 .cb
.ring_buffer_clock_read
= client_ring_buffer_clock_read
,
135 .cb
.record_header_size
= client_record_header_size
,
136 .cb
.subbuffer_header_size
= client_packet_header_size
,
137 .cb
.buffer_begin
= client_buffer_begin
,
138 .cb
.buffer_end
= client_buffer_end
,
139 .cb
.buffer_create
= client_buffer_create
,
140 .cb
.buffer_finalize
= client_buffer_finalize
,
143 .alloc
= RING_BUFFER_ALLOC_GLOBAL
,
144 .sync
= RING_BUFFER_SYNC_GLOBAL
,
145 .mode
= RING_BUFFER_MODE_TEMPLATE
,
146 .backend
= RING_BUFFER_PAGE
,
147 .output
= RING_BUFFER_SPLICE
,
148 .oops
= RING_BUFFER_OOPS_CONSISTENCY
,
149 .ipi
= RING_BUFFER_IPI_BARRIER
,
150 .wakeup
= RING_BUFFER_WAKEUP_BY_TIMER
,
154 struct channel
*_channel_create(const char *name
,
155 struct ltt_session
*session
, void *buf_addr
,
156 size_t subbuf_size
, size_t num_subbuf
,
157 unsigned int switch_timer_interval
,
158 unsigned int read_timer_interval
)
160 return channel_create(&client_config
, name
, session
, buf_addr
,
161 subbuf_size
, num_subbuf
, switch_timer_interval
,
162 read_timer_interval
);
166 void ltt_channel_destroy(struct channel
*chan
)
168 channel_destroy(chan
);
172 struct lib_ring_buffer
*ltt_buffer_read_open(struct channel
*chan
)
174 struct lib_ring_buffer
*buf
;
177 for_each_channel_cpu(cpu
, chan
) {
178 buf
= channel_get_ring_buffer(&client_config
, chan
, cpu
);
179 if (!lib_ring_buffer_open_read(buf
))
186 void ltt_buffer_read_close(struct lib_ring_buffer
*buf
)
188 lib_ring_buffer_release_read(buf
);
193 int ltt_event_reserve(struct lib_ring_buffer_ctx
*ctx
)
195 return lib_ring_buffer_reserve(&client_config
, ctx
);
199 void ltt_event_commit(struct lib_ring_buffer_ctx
*ctx
)
201 lib_ring_buffer_commit(&client_config
, ctx
);
205 void ltt_event_write(struct lib_ring_buffer_ctx
*ctx
, const void *src
,
208 lib_ring_buffer_write(&client_config
, ctx
, src
, len
);
212 wait_queue_head_t
*ltt_get_reader_wait_queue(struct ltt_channel
*chan
)
214 return &chan
->chan
->read_wait
;
217 static struct ltt_transport ltt_relay_transport
= {
218 .name
= "relay-" RING_BUFFER_MODE_TEMPLATE_STRING
,
219 .owner
= THIS_MODULE
,
221 .channel_create
= _channel_create
,
222 .channel_destroy
= ltt_channel_destroy
,
223 .buffer_read_open
= ltt_buffer_read_open
,
224 .buffer_read_close
= ltt_buffer_read_close
,
225 .event_reserve
= ltt_event_reserve
,
226 .event_commit
= ltt_event_commit
,
227 .event_write
= ltt_event_write
,
228 .get_reader_wait_queue
= ltt_get_reader_wait_queue
,
232 static int __init
ltt_ring_buffer_client_init(void)
235 * This vmalloc sync all also takes care of the lib ring buffer
236 * vmalloc'd module pages when it is built as a module into LTTng.
238 wrapper_vmalloc_sync_all();
239 printk(KERN_INFO
"LTT : ltt ring buffer metadata client init\n");
240 ltt_transport_register(<t_relay_transport
);
244 module_init(ltt_ring_buffer_client_init
);
246 static void __exit
ltt_ring_buffer_client_exit(void)
248 printk(KERN_INFO
"LTT : ltt ring buffer metadata client exit\n");
249 ltt_transport_unregister(<t_relay_transport
);
252 module_exit(ltt_ring_buffer_client_exit
);
254 MODULE_LICENSE("GPL and additional rights");
255 MODULE_AUTHOR("Mathieu Desnoyers");
256 MODULE_DESCRIPTION("LTTng ring buffer " RING_BUFFER_MODE_TEMPLATE_STRING