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/symbols.h" /* for wrapper_vmalloc_sync_all() */
14 #include "wrapper/trace-clock.h"
15 #include "ltt-events.h"
16 #include "ltt-tracer.h"
18 static const struct lib_ring_buffer_config client_config
;
20 static u64
client_ring_buffer_clock_read(struct channel
*chan
)
22 return lib_ring_buffer_clock_read(chan
);
26 size_t client_record_header_size(const struct lib_ring_buffer_config
*config
,
27 struct channel
*chan
, size_t offset
,
29 size_t *pre_header_padding
,
31 struct lib_ring_buffer_ctx
*ctx
)
33 return record_header_size(config
, chan
, offset
, data_size
,
34 pre_header_padding
, rflags
, ctx
);
38 * client_packet_header_size - called on buffer-switch to a new sub-buffer
40 * Return header size without padding after the structure. Don't use packed
41 * structure because gcc generates inefficient code on some architectures
44 static size_t client_packet_header_size(void)
46 return offsetof(struct packet_header
, header_end
);
49 static void client_buffer_begin(struct lib_ring_buffer
*buf
, u64 tsc
,
50 unsigned int subbuf_idx
)
52 struct channel
*chan
= buf
->backend
.chan
;
53 struct packet_header
*header
=
54 (struct packet_header
*)
55 lib_ring_buffer_offset_address(&buf
->backend
,
56 subbuf_idx
* chan
->backend
.subbuf_size
);
58 header
->timestamp_begin
= tsc
;
59 header
->content_size
= 0xFFFFFFFF; /* for debugging */
60 write_trace_header(&client_config
, header
);
64 * offset is assumed to never be 0 here : never deliver a completely empty
65 * subbuffer. data_size is between 1 and subbuf_size.
67 static void client_buffer_end(struct lib_ring_buffer
*buf
, u64 tsc
,
68 unsigned int subbuf_idx
, unsigned long data_size
)
70 struct channel
*chan
= buf
->backend
.chan
;
71 struct packet_header
*header
=
72 (struct packet_header
*)
73 lib_ring_buffer_offset_address(&buf
->backend
,
74 subbuf_idx
* chan
->backend
.subbuf_size
);
75 unsigned long records_lost
= 0;
77 header
->content_size
= data_size
;
78 header
->packet_size
= PAGE_ALIGN(data_size
);
79 header
->timestamp_end
= tsc
;
80 records_lost
+= lib_ring_buffer_get_records_lost_full(&client_config
, buf
);
81 records_lost
+= lib_ring_buffer_get_records_lost_wrap(&client_config
, buf
);
82 records_lost
+= lib_ring_buffer_get_records_lost_big(&client_config
, buf
);
83 header
->events_lost
= records_lost
;
86 static int client_buffer_create(struct lib_ring_buffer
*buf
, void *priv
,
87 int cpu
, const char *name
)
92 static void client_buffer_finalize(struct lib_ring_buffer
*buf
, void *priv
, int cpu
)
96 static const struct lib_ring_buffer_config client_config
= {
97 .cb
.ring_buffer_clock_read
= client_ring_buffer_clock_read
,
98 .cb
.record_header_size
= client_record_header_size
,
99 .cb
.subbuffer_header_size
= client_packet_header_size
,
100 .cb
.buffer_begin
= client_buffer_begin
,
101 .cb
.buffer_end
= client_buffer_end
,
102 .cb
.buffer_create
= client_buffer_create
,
103 .cb
.buffer_finalize
= client_buffer_finalize
,
106 .alloc
= RING_BUFFER_ALLOC_PER_CPU
,
107 .sync
= RING_BUFFER_SYNC_PER_CPU
,
108 .mode
= RING_BUFFER_MODE_TEMPLATE
,
109 .backend
= RING_BUFFER_PAGE
,
110 .output
= RING_BUFFER_SPLICE
,
111 .oops
= RING_BUFFER_OOPS_CONSISTENCY
,
112 .ipi
= RING_BUFFER_IPI_BARRIER
,
113 .wakeup
= RING_BUFFER_WAKEUP_BY_TIMER
,
117 struct channel
*_channel_create(const char *name
,
118 struct ltt_session
*session
, void *buf_addr
,
119 size_t subbuf_size
, size_t num_subbuf
,
120 unsigned int switch_timer_interval
,
121 unsigned int read_timer_interval
)
123 return channel_create(&client_config
, name
, session
, buf_addr
,
124 subbuf_size
, num_subbuf
, switch_timer_interval
,
125 read_timer_interval
);
129 void ltt_channel_destroy(struct channel
*chan
)
131 channel_destroy(chan
);
135 struct lib_ring_buffer
*ltt_buffer_read_open(struct channel
*chan
)
137 struct lib_ring_buffer
*buf
;
140 for_each_channel_cpu(cpu
, chan
) {
141 buf
= channel_get_ring_buffer(&client_config
, chan
, cpu
);
142 if (!lib_ring_buffer_open_read(buf
))
149 void ltt_buffer_read_close(struct lib_ring_buffer
*buf
)
151 lib_ring_buffer_release_read(buf
);
155 int ltt_event_reserve(struct lib_ring_buffer_ctx
*ctx
)
159 cpu
= lib_ring_buffer_get_cpu(&client_config
);
164 ret
= lib_ring_buffer_reserve(&client_config
, ctx
);
170 lib_ring_buffer_put_cpu(&client_config
);
174 void ltt_event_commit(struct lib_ring_buffer_ctx
*ctx
)
176 lib_ring_buffer_commit(&client_config
, ctx
);
177 lib_ring_buffer_put_cpu(&client_config
);
180 void ltt_event_write(struct lib_ring_buffer_ctx
*ctx
, const void *src
,
183 lib_ring_buffer_write(&client_config
, ctx
, src
, len
);
186 static struct ltt_transport ltt_relay_transport
= {
187 .name
= "relay-" RING_BUFFER_MODE_TEMPLATE_STRING
,
188 .owner
= THIS_MODULE
,
190 .channel_create
= _channel_create
,
191 .channel_destroy
= ltt_channel_destroy
,
192 .buffer_read_open
= ltt_buffer_read_open
,
193 .buffer_read_close
= ltt_buffer_read_close
,
194 .event_reserve
= ltt_event_reserve
,
195 .event_commit
= ltt_event_commit
,
196 .event_write
= ltt_event_write
,
200 static int __init
ltt_ring_buffer_client_init(void)
203 * This vmalloc sync all also takes care of the lib ring buffer
204 * vmalloc'd module pages when it is built as a module into LTTng.
206 wrapper_vmalloc_sync_all();
207 printk(KERN_INFO
"LTT : ltt ring buffer client init\n");
208 ltt_transport_register(<t_relay_transport
);
212 module_init(ltt_ring_buffer_client_init
);
214 static void __exit
ltt_ring_buffer_client_exit(void)
216 printk(KERN_INFO
"LTT : ltt ring buffer client exit\n");
217 ltt_transport_unregister(<t_relay_transport
);
220 module_exit(ltt_ring_buffer_client_exit
);
222 MODULE_LICENSE("GPL and additional rights");
223 MODULE_AUTHOR("Mathieu Desnoyers");
224 MODULE_DESCRIPTION("LTTng ring buffer " RING_BUFFER_MODE_TEMPLATE_STRING