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 "ltt-tracer.h"
14 struct ring_buffer_priv
{
15 struct dentry
*dentry
;
19 struct ltt_trace
*trace
;
20 struct ring_buffer_priv
*buf
;
23 static const struct lib_ring_buffer_config client_config
;
25 static u64
client_ring_buffer_clock_read(struct channel
*chan
)
27 return lib_ring_buffer_clock_read(chan
);
31 size_t client_record_header_size(const struct lib_ring_buffer_config
*config
,
32 struct channel
*chan
, size_t offset
,
34 size_t *pre_header_padding
,
36 struct lib_ring_buffer_ctx
*ctx
)
38 return record_header_size(config
, chan
, offset
, data_size
,
39 pre_header_padding
, rflags
, ctx
);
43 * client_subbuffer_header_size - called on buffer-switch to a new sub-buffer
45 * Return header size without padding after the structure. Don't use packed
46 * structure because gcc generates inefficient code on some architectures
49 static size_t client_subbuffer_header_size(void)
51 return offsetof(struct subbuffer_header
, header_end
);
54 static void client_buffer_begin(struct lib_ring_buffer
*buf
, u64 tsc
,
55 unsigned int subbuf_idx
)
57 struct channel
*chan
= buf
->backend
.chan
;
58 struct subbuffer_header
*header
=
59 (struct subbuffer_header
*)
60 lib_ring_buffer_offset_address(&buf
->backend
,
61 subbuf_idx
* chan
->backend
.subbuf_size
);
63 header
->cycle_count_begin
= tsc
;
64 header
->data_size
= 0xFFFFFFFF; /* for debugging */
65 write_trace_header(chan
->backend
.priv
, header
);
69 * offset is assumed to never be 0 here : never deliver a completely empty
70 * subbuffer. data_size is between 1 and subbuf_size.
72 static void client_buffer_end(struct lib_ring_buffer
*buf
, u64 tsc
,
73 unsigned int subbuf_idx
, unsigned long data_size
)
75 struct channel
*chan
= buf
->backend
.chan
;
76 struct subbuffer_header
*header
=
77 (struct subbuffer_header
*)
78 lib_ring_buffer_offset_address(&buf
->backend
,
79 subbuf_idx
* chan
->backend
.subbuf_size
);
80 unsigned long records_lost
= 0;
82 header
->data_size
= data_size
;
83 header
->subbuf_size
= PAGE_ALIGN(data_size
);
84 header
->cycle_count_end
= tsc
;
85 records_lost
+= lib_ring_buffer_get_records_lost_full(&client_config
, buf
);
86 records_lost
+= lib_ring_buffer_get_records_lost_wrap(&client_config
, buf
);
87 records_lost
+= lib_ring_buffer_get_records_lost_big(&client_config
, buf
);
88 header
->events_lost
= records_lost
;
89 header
->subbuf_corrupt
= 0; /* deprecated */
92 static int client_buffer_create(struct lib_ring_buffer
*buf
, void *priv
,
93 int cpu
, const char *name
)
95 struct channel_priv
*chan_priv
= priv
;
96 struct ring_buffer_priv
*buf_priv
;
97 struct dentry
*trace_dentry
;
101 if (client_config
.alloc
== RING_BUFFER_ALLOC_PER_CPU
)
102 buf_priv
= per_cpu_ptr(chan_priv
->buf
, cpu
);
104 buf_priv
= chan_priv
->buf
;
106 tmpname
= kzalloc(NAME_MAX
+ 1, GFP_KERNEL
);
112 snprintf(tmpname
, NAME_MAX
, "%s%s_%d",
113 (client_config
.mode
== RING_BUFFER_OVERWRITE
) ? "flight-" : "",
116 trace_dentry
= chan_priv
->trace
->dentry
.trace_root
;
117 buf_priv
->dentry
= debugfs_create_file(tmpname
, S_IRUSR
, trace_dentry
,
119 &lib_ring_buffer_file_operations
);
120 if (!buf_priv
->dentry
) {
130 static void client_buffer_finalize(struct lib_ring_buffer
*buf
, void *priv
, int cpu
)
132 struct channel_priv
*chan_priv
= priv
;
133 struct lib_ring_buffer_priv
*buf_priv
;
135 if (client_config
.alloc
== RING_BUFFER_ALLOC_PER_CPU
)
136 buf_priv
= per_cpu_ptr(chan_priv
->buf
, cpu
);
138 buf_priv
= chan_priv
->buf
;
140 debugfs_remove(buf_priv
->dentry
);
143 static const struct lib_ring_buffer_config client_config
= {
144 .cb
.ring_buffer_clock_read
= client_ring_buffer_clock_read
,
145 .cb
.record_header_size
= client_record_header_size
,
146 .cb
.subbuffer_header_size
= client_subbuffer_header_size
,
147 .cb
.buffer_begin
= client_buffer_begin
,
148 .cb
.buffer_end
= client_buffer_end
,
149 .cb
.buffer_create
= client_buffer_create
,
150 .cb
.buffer_finalize
= client_buffer_finalize
,
153 .alloc
= RING_BUFFER_ALLOC_PER_CPU
,
154 .sync
= RING_BUFFER_SYNC_PER_CPU
,
155 .mode
= RING_BUFFER_MODE_TEMPLATE
,
156 #ifdef RING_BUFFER_ALIGN
157 .align
= RING_BUFFER_NATURAL
,
159 .align
= RING_BUFFER_PACKED
,
161 .backend
= RING_BUFFER_PAGE
,
162 .output
= RING_BUFFER_SPLICE
,
163 .oops
= RING_BUFFER_OOPS_CONSISTENCY
,
164 .ipi
= RING_BUFFER_IPI_BARRIER
,
165 .wakeup
= RING_BUFFER_WAKEUP_BY_TIMER
,
169 struct channel
*ltt_channel_create(const char *name
, struct ltt_trace
*trace
,
171 size_t subbuf_size
, size_t num_subbuf
,
172 unsigned int switch_timer_interval
,
173 unsigned int read_timer_interval
)
175 struct channel
*chan
;
176 struct chan_priv
*chan_priv
;
178 chan_priv
= kzalloc(sizeof(struct chan_priv
), GFP_KERNEL
);
181 if (client_config
.alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
182 chan_priv
->buf
= alloc_percpu(struct lib_ring_buffer_priv
);
183 memset(chan_priv
->buf
, 0, sizeof(*chan_priv
->buf
));
185 chan_priv
->buf
= kzalloc(sizeof(*chan_priv
->buf
), GFP_KERNEL
)
186 if (!channel_priv
->buf
)
188 chan_priv
->trace
= trace
;
189 chan
= channel_create(&client_config
, name
, chan_priv
, buf_addr
,
190 subbuf_size
, num_subbuf
, switch_timer_interval
,
191 read_timer_interval
);
197 if (client_config
.alloc
== RING_BUFFER_ALLOC_PER_CPU
)
198 free_percpu(chan_priv
->buf
);
200 kfree(chan_priv
->buf
);
207 void ltt_channel_destroy(struct channel
*chan
)
209 struct chan_priv
*chan_priv
= channel_get_private(chan
);
211 channel_destroy(chan
);
212 if (client_config
.alloc
== RING_BUFFER_ALLOC_PER_CPU
)
213 free_percpu(chan_priv
->buf
);
215 kfree(chan_priv
->buf
);
220 struct lib_ring_buffer
*ltt_buffer_read_open(struct channel
*chan
)
222 struct lib_ring_buffer
*buf
;
225 for_each_channel_cpu(cpu
, chan
->chan
) {
226 buf
= channel_get_ring_buffer(&config_config
, chan
, cpu
);
227 if (!lib_ring_buffer_open_read(buf
))
234 struct lib_ring_buffer
*ltt_buffer_read_close(struct lib_ring_buffer
*buf
)
236 lib_ring_buffer_release_read(buf
);
239 static void ltt_relay_remove_dirs(struct ltt_trace
*trace
)
241 debugfs_remove(trace
->dentry
.trace_root
);
244 static int ltt_relay_create_dirs(struct ltt_trace
*new_trace
)
246 struct dentry
*ltt_root_dentry
;
249 ltt_root_dentry
= get_ltt_root();
250 if (!ltt_root_dentry
)
253 new_trace
->dentry
.trace_root
= debugfs_create_dir(new_trace
->trace_name
,
256 if (new_trace
->dentry
.trace_root
== NULL
) {
257 printk(KERN_ERR
"LTT : Trace directory name %s already taken\n",
258 new_trace
->trace_name
);
264 static struct ltt_transport ltt_relay_transport
= {
265 .name
= "relay-" RING_BUFFER_MODE_TEMPLATE_STRING
,
266 .owner
= THIS_MODULE
,
268 .create_dirs
= ltt_relay_create_dirs
,
269 .remove_dirs
= ltt_relay_remove_dirs
,
270 .create_channel
= ltt_channel_create
,
271 .destroy_channel
= ltt_channel_destroy
,
272 .buffer_read_open
= ltt_buffer_read_open
,
273 .buffer_read_close
= ltt_buffer_read_close
,
277 static int __init
ltt_ring_buffer_client_init(void)
279 printk(KERN_INFO
"LTT : ltt ring buffer client init\n");
280 ltt_transport_register(<t_relay_transport
);
284 static void __exit
ltt_ring_buffer_client_exit(void)
286 printk(KERN_INFO
"LTT : ltt ring buffer client exit\n");
287 ltt_transport_unregister(<t_relay_transport
);
290 MODULE_LICENSE("GPL and additional rights");
291 MODULE_AUTHOR("Mathieu Desnoyers");
292 MODULE_DESCRIPTION("LTTng ring buffer " RING_BUFFER_MODE_TEMPLATE_STRING