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"
21 struct metadata_packet_header
{
22 uint32_t magic
; /* 0x75D11D57 */
23 uint8_t trace_uuid
[16]; /* Unique Universal Identifier */
24 uint32_t checksum
; /* 0 if unused */
25 uint32_t content_size
; /* in bits */
26 uint32_t packet_size
; /* in bits */
27 uint8_t compression_scheme
; /* 0 if unused */
28 uint8_t encryption_scheme
; /* 0 if unused */
29 uint8_t checksum_scheme
; /* 0 if unused */
30 uint8_t header_end
[0];
33 struct metadata_record_header
{
34 uint8_t header_end
[0]; /* End of header */
37 static const struct lib_ring_buffer_config client_config
;
40 u64
lib_ring_buffer_clock_read(struct channel
*chan
)
46 unsigned char record_header_size(const struct lib_ring_buffer_config
*config
,
47 struct channel
*chan
, size_t offset
,
48 size_t data_size
, size_t *pre_header_padding
,
50 struct lib_ring_buffer_ctx
*ctx
)
55 #include "wrapper/ringbuffer/api.h"
57 static u64
client_ring_buffer_clock_read(struct channel
*chan
)
63 size_t client_record_header_size(const struct lib_ring_buffer_config
*config
,
64 struct channel
*chan
, size_t offset
,
66 size_t *pre_header_padding
,
68 struct lib_ring_buffer_ctx
*ctx
)
74 * client_packet_header_size - called on buffer-switch to a new sub-buffer
76 * Return header size without padding after the structure. Don't use packed
77 * structure because gcc generates inefficient code on some architectures
80 static size_t client_packet_header_size(void)
82 return offsetof(struct metadata_packet_header
, header_end
);
85 static void client_buffer_begin(struct lib_ring_buffer
*buf
, u64 tsc
,
86 unsigned int subbuf_idx
)
88 struct channel
*chan
= buf
->backend
.chan
;
89 struct metadata_packet_header
*header
=
90 (struct metadata_packet_header
*)
91 lib_ring_buffer_offset_address(&buf
->backend
,
92 subbuf_idx
* chan
->backend
.subbuf_size
);
94 header
->magic
= TSDL_MAGIC_NUMBER
;
96 //header->trace_uuid = ; /* Unique Universal Identifier */
97 header
->checksum
= 0; /* 0 if unused */
98 header
->content_size
= 0xFFFFFFFF; /* in bits, for debugging */
99 header
->packet_size
= 0xFFFFFFFF; /* in bits, for debugging */
100 header
->compression_scheme
= 0; /* 0 if unused */
101 header
->encryption_scheme
= 0; /* 0 if unused */
102 header
->checksum_scheme
= 0; /* 0 if unused */
106 * offset is assumed to never be 0 here : never deliver a completely empty
107 * subbuffer. data_size is between 1 and subbuf_size.
109 static void client_buffer_end(struct lib_ring_buffer
*buf
, u64 tsc
,
110 unsigned int subbuf_idx
, unsigned long data_size
)
112 struct channel
*chan
= buf
->backend
.chan
;
113 struct packet_header
*header
=
114 (struct packet_header
*)
115 lib_ring_buffer_offset_address(&buf
->backend
,
116 subbuf_idx
* chan
->backend
.subbuf_size
);
117 unsigned long records_lost
= 0;
119 header
->content_size
= data_size
* CHAR_BIT
; /* in bits */
120 header
->packet_size
= PAGE_ALIGN(data_size
) * CHAR_BIT
; /* in bits */
121 records_lost
+= lib_ring_buffer_get_records_lost_full(&client_config
, buf
);
122 records_lost
+= lib_ring_buffer_get_records_lost_wrap(&client_config
, buf
);
123 records_lost
+= lib_ring_buffer_get_records_lost_big(&client_config
, buf
);
124 WARN_ON_ONCE(records_lost
!= 0);
127 static int client_buffer_create(struct lib_ring_buffer
*buf
, void *priv
,
128 int cpu
, const char *name
)
133 static void client_buffer_finalize(struct lib_ring_buffer
*buf
, void *priv
, int cpu
)
137 static const struct lib_ring_buffer_config client_config
= {
138 .cb
.ring_buffer_clock_read
= client_ring_buffer_clock_read
,
139 .cb
.record_header_size
= client_record_header_size
,
140 .cb
.subbuffer_header_size
= client_packet_header_size
,
141 .cb
.buffer_begin
= client_buffer_begin
,
142 .cb
.buffer_end
= client_buffer_end
,
143 .cb
.buffer_create
= client_buffer_create
,
144 .cb
.buffer_finalize
= client_buffer_finalize
,
147 .alloc
= RING_BUFFER_ALLOC_GLOBAL
,
148 .sync
= RING_BUFFER_SYNC_GLOBAL
,
149 .mode
= RING_BUFFER_MODE_TEMPLATE
,
150 .backend
= RING_BUFFER_PAGE
,
151 .output
= RING_BUFFER_SPLICE
,
152 .oops
= RING_BUFFER_OOPS_CONSISTENCY
,
153 .ipi
= RING_BUFFER_IPI_BARRIER
,
154 .wakeup
= RING_BUFFER_WAKEUP_BY_TIMER
,
158 struct channel
*_channel_create(const char *name
,
159 struct ltt_session
*session
, void *buf_addr
,
160 size_t subbuf_size
, size_t num_subbuf
,
161 unsigned int switch_timer_interval
,
162 unsigned int read_timer_interval
)
164 return channel_create(&client_config
, name
, session
, buf_addr
,
165 subbuf_size
, num_subbuf
, switch_timer_interval
,
166 read_timer_interval
);
170 void ltt_channel_destroy(struct channel
*chan
)
172 channel_destroy(chan
);
176 struct lib_ring_buffer
*ltt_buffer_read_open(struct channel
*chan
)
178 struct lib_ring_buffer
*buf
;
181 for_each_channel_cpu(cpu
, chan
) {
182 buf
= channel_get_ring_buffer(&client_config
, chan
, cpu
);
183 if (!lib_ring_buffer_open_read(buf
))
190 void ltt_buffer_read_close(struct lib_ring_buffer
*buf
)
192 lib_ring_buffer_release_read(buf
);
196 int ltt_event_reserve(struct lib_ring_buffer_ctx
*ctx
)
198 return lib_ring_buffer_reserve(&client_config
, ctx
);
201 void ltt_event_commit(struct lib_ring_buffer_ctx
*ctx
)
203 lib_ring_buffer_commit(&client_config
, ctx
);
206 void ltt_event_write(struct lib_ring_buffer_ctx
*ctx
, const void *src
,
209 lib_ring_buffer_write(&client_config
, ctx
, src
, len
);
212 static struct ltt_transport ltt_relay_transport
= {
213 .name
= "relay-" RING_BUFFER_MODE_TEMPLATE_STRING
,
214 .owner
= THIS_MODULE
,
216 .channel_create
= _channel_create
,
217 .channel_destroy
= ltt_channel_destroy
,
218 .buffer_read_open
= ltt_buffer_read_open
,
219 .buffer_read_close
= ltt_buffer_read_close
,
220 .event_reserve
= ltt_event_reserve
,
221 .event_commit
= ltt_event_commit
,
222 .event_write
= ltt_event_write
,
226 static int __init
ltt_ring_buffer_client_init(void)
229 * This vmalloc sync all also takes care of the lib ring buffer
230 * vmalloc'd module pages when it is built as a module into LTTng.
232 wrapper_vmalloc_sync_all();
233 printk(KERN_INFO
"LTT : ltt ring buffer metadata client init\n");
234 ltt_transport_register(<t_relay_transport
);
238 module_init(ltt_ring_buffer_client_init
);
240 static void __exit
ltt_ring_buffer_client_exit(void)
242 printk(KERN_INFO
"LTT : ltt ring buffer metadata client exit\n");
243 ltt_transport_unregister(<t_relay_transport
);
246 module_exit(ltt_ring_buffer_client_exit
);
248 MODULE_LICENSE("GPL and additional rights");
249 MODULE_AUTHOR("Mathieu Desnoyers");
250 MODULE_DESCRIPTION("LTTng ring buffer " RING_BUFFER_MODE_TEMPLATE_STRING