Commit | Line | Data |
---|---|---|
881833e3 | 1 | /* |
a90917c3 | 2 | * lttng-ring-buffer-client.h |
881833e3 | 3 | * |
881833e3 MD |
4 | * LTTng lib ring buffer client template. |
5 | * | |
886d51a3 MD |
6 | * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
7 | * | |
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. | |
12 | * | |
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. | |
17 | * | |
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 | |
881833e3 MD |
21 | */ |
22 | ||
23 | #include <linux/module.h> | |
24 | #include <linux/types.h> | |
bbd023d6 MD |
25 | #include <wrapper/vmalloc.h> /* for wrapper_vmalloc_sync_all() */ |
26 | #include <lttng-events.h> | |
27 | #include <lttng-tracer.h> | |
881833e3 | 28 | |
dd5a0db3 MD |
29 | static struct lttng_transport lttng_relay_transport; |
30 | ||
881833e3 MD |
31 | struct metadata_packet_header { |
32 | uint32_t magic; /* 0x75D11D57 */ | |
1ec3f75a | 33 | uint8_t uuid[16]; /* Unique Universal Identifier */ |
881833e3 MD |
34 | uint32_t checksum; /* 0 if unused */ |
35 | uint32_t content_size; /* in bits */ | |
36 | uint32_t packet_size; /* in bits */ | |
37 | uint8_t compression_scheme; /* 0 if unused */ | |
38 | uint8_t encryption_scheme; /* 0 if unused */ | |
39 | uint8_t checksum_scheme; /* 0 if unused */ | |
4173df96 MD |
40 | uint8_t major; /* CTF spec major version number */ |
41 | uint8_t minor; /* CTF spec minor version number */ | |
881833e3 MD |
42 | uint8_t header_end[0]; |
43 | }; | |
44 | ||
45 | struct metadata_record_header { | |
46 | uint8_t header_end[0]; /* End of header */ | |
47 | }; | |
48 | ||
49 | static const struct lib_ring_buffer_config client_config; | |
50 | ||
51 | static inline | |
52 | u64 lib_ring_buffer_clock_read(struct channel *chan) | |
53 | { | |
54 | return 0; | |
55 | } | |
56 | ||
57 | static inline | |
c1cc82b8 | 58 | size_t record_header_size(const struct lib_ring_buffer_config *config, |
881833e3 | 59 | struct channel *chan, size_t offset, |
64c796d8 | 60 | size_t *pre_header_padding, |
cc62f29e MD |
61 | struct lib_ring_buffer_ctx *ctx, |
62 | void *client_ctx) | |
881833e3 MD |
63 | { |
64 | return 0; | |
65 | } | |
66 | ||
bbd023d6 | 67 | #include <wrapper/ringbuffer/api.h> |
881833e3 MD |
68 | |
69 | static u64 client_ring_buffer_clock_read(struct channel *chan) | |
70 | { | |
71 | return 0; | |
72 | } | |
73 | ||
74 | static | |
75 | size_t client_record_header_size(const struct lib_ring_buffer_config *config, | |
76 | struct channel *chan, size_t offset, | |
881833e3 | 77 | size_t *pre_header_padding, |
cc62f29e MD |
78 | struct lib_ring_buffer_ctx *ctx, |
79 | void *client_ctx) | |
881833e3 MD |
80 | { |
81 | return 0; | |
82 | } | |
83 | ||
84 | /** | |
85 | * client_packet_header_size - called on buffer-switch to a new sub-buffer | |
86 | * | |
87 | * Return header size without padding after the structure. Don't use packed | |
88 | * structure because gcc generates inefficient code on some architectures | |
89 | * (powerpc, mips..) | |
90 | */ | |
91 | static size_t client_packet_header_size(void) | |
92 | { | |
93 | return offsetof(struct metadata_packet_header, header_end); | |
94 | } | |
95 | ||
96 | static void client_buffer_begin(struct lib_ring_buffer *buf, u64 tsc, | |
97 | unsigned int subbuf_idx) | |
98 | { | |
99 | struct channel *chan = buf->backend.chan; | |
100 | struct metadata_packet_header *header = | |
101 | (struct metadata_packet_header *) | |
102 | lib_ring_buffer_offset_address(&buf->backend, | |
103 | subbuf_idx * chan->backend.subbuf_size); | |
a36580d5 MD |
104 | struct lttng_metadata_cache *metadata_cache = |
105 | channel_get_private(chan); | |
881833e3 MD |
106 | |
107 | header->magic = TSDL_MAGIC_NUMBER; | |
a36580d5 MD |
108 | memcpy(header->uuid, metadata_cache->uuid.b, |
109 | sizeof(metadata_cache->uuid)); | |
881833e3 MD |
110 | header->checksum = 0; /* 0 if unused */ |
111 | header->content_size = 0xFFFFFFFF; /* in bits, for debugging */ | |
112 | header->packet_size = 0xFFFFFFFF; /* in bits, for debugging */ | |
113 | header->compression_scheme = 0; /* 0 if unused */ | |
114 | header->encryption_scheme = 0; /* 0 if unused */ | |
115 | header->checksum_scheme = 0; /* 0 if unused */ | |
4173df96 MD |
116 | header->major = CTF_SPEC_MAJOR; |
117 | header->minor = CTF_SPEC_MINOR; | |
881833e3 MD |
118 | } |
119 | ||
120 | /* | |
121 | * offset is assumed to never be 0 here : never deliver a completely empty | |
122 | * subbuffer. data_size is between 1 and subbuf_size. | |
123 | */ | |
124 | static void client_buffer_end(struct lib_ring_buffer *buf, u64 tsc, | |
125 | unsigned int subbuf_idx, unsigned long data_size) | |
126 | { | |
127 | struct channel *chan = buf->backend.chan; | |
d793d5e1 | 128 | struct metadata_packet_header *header = |
1ec3f75a | 129 | (struct metadata_packet_header *) |
881833e3 MD |
130 | lib_ring_buffer_offset_address(&buf->backend, |
131 | subbuf_idx * chan->backend.subbuf_size); | |
132 | unsigned long records_lost = 0; | |
133 | ||
134 | header->content_size = data_size * CHAR_BIT; /* in bits */ | |
135 | header->packet_size = PAGE_ALIGN(data_size) * CHAR_BIT; /* in bits */ | |
71c1d843 MD |
136 | /* |
137 | * We do not care about the records lost count, because the metadata | |
138 | * channel waits and retry. | |
139 | */ | |
140 | (void) lib_ring_buffer_get_records_lost_full(&client_config, buf); | |
881833e3 MD |
141 | records_lost += lib_ring_buffer_get_records_lost_wrap(&client_config, buf); |
142 | records_lost += lib_ring_buffer_get_records_lost_big(&client_config, buf); | |
143 | WARN_ON_ONCE(records_lost != 0); | |
144 | } | |
145 | ||
146 | static int client_buffer_create(struct lib_ring_buffer *buf, void *priv, | |
147 | int cpu, const char *name) | |
148 | { | |
149 | return 0; | |
150 | } | |
151 | ||
152 | static void client_buffer_finalize(struct lib_ring_buffer *buf, void *priv, int cpu) | |
153 | { | |
154 | } | |
155 | ||
3b731ab1 JD |
156 | static int client_timestamp_begin(const struct lib_ring_buffer_config *config, |
157 | struct lib_ring_buffer *buf, uint64_t *timestamp_begin) | |
158 | { | |
159 | return -ENOSYS; | |
160 | } | |
161 | ||
162 | static int client_timestamp_end(const struct lib_ring_buffer_config *config, | |
163 | struct lib_ring_buffer *bufb, | |
164 | uint64_t *timestamp_end) | |
165 | { | |
166 | return -ENOSYS; | |
167 | } | |
168 | ||
169 | static int client_events_discarded(const struct lib_ring_buffer_config *config, | |
170 | struct lib_ring_buffer *bufb, | |
171 | uint64_t *events_discarded) | |
172 | { | |
173 | return -ENOSYS; | |
174 | } | |
175 | ||
2348ca17 JD |
176 | static int client_current_timestamp(const struct lib_ring_buffer_config *config, |
177 | struct lib_ring_buffer *bufb, | |
178 | uint64_t *ts) | |
179 | { | |
180 | return -ENOSYS; | |
181 | } | |
182 | ||
3b731ab1 JD |
183 | static int client_content_size(const struct lib_ring_buffer_config *config, |
184 | struct lib_ring_buffer *bufb, | |
185 | uint64_t *content_size) | |
186 | { | |
187 | return -ENOSYS; | |
188 | } | |
189 | ||
190 | static int client_packet_size(const struct lib_ring_buffer_config *config, | |
191 | struct lib_ring_buffer *bufb, | |
192 | uint64_t *packet_size) | |
193 | { | |
194 | return -ENOSYS; | |
195 | } | |
196 | ||
197 | static int client_stream_id(const struct lib_ring_buffer_config *config, | |
198 | struct lib_ring_buffer *bufb, | |
199 | uint64_t *stream_id) | |
200 | { | |
201 | return -ENOSYS; | |
202 | } | |
203 | ||
5b3cf4f9 JD |
204 | static int client_sequence_number(const struct lib_ring_buffer_config *config, |
205 | struct lib_ring_buffer *bufb, | |
206 | uint64_t *seq) | |
207 | { | |
208 | return -ENOSYS; | |
209 | } | |
210 | ||
5594698f JD |
211 | static |
212 | int client_instance_id(const struct lib_ring_buffer_config *config, | |
213 | struct lib_ring_buffer *bufb, | |
214 | uint64_t *id) | |
215 | { | |
216 | return -ENOSYS; | |
217 | } | |
218 | ||
881833e3 MD |
219 | static const struct lib_ring_buffer_config client_config = { |
220 | .cb.ring_buffer_clock_read = client_ring_buffer_clock_read, | |
221 | .cb.record_header_size = client_record_header_size, | |
222 | .cb.subbuffer_header_size = client_packet_header_size, | |
223 | .cb.buffer_begin = client_buffer_begin, | |
224 | .cb.buffer_end = client_buffer_end, | |
225 | .cb.buffer_create = client_buffer_create, | |
226 | .cb.buffer_finalize = client_buffer_finalize, | |
227 | ||
228 | .tsc_bits = 0, | |
229 | .alloc = RING_BUFFER_ALLOC_GLOBAL, | |
230 | .sync = RING_BUFFER_SYNC_GLOBAL, | |
231 | .mode = RING_BUFFER_MODE_TEMPLATE, | |
232 | .backend = RING_BUFFER_PAGE, | |
2db1399a | 233 | .output = RING_BUFFER_OUTPUT_TEMPLATE, |
881833e3 MD |
234 | .oops = RING_BUFFER_OOPS_CONSISTENCY, |
235 | .ipi = RING_BUFFER_IPI_BARRIER, | |
236 | .wakeup = RING_BUFFER_WAKEUP_BY_TIMER, | |
237 | }; | |
238 | ||
dd5a0db3 MD |
239 | static |
240 | void release_priv_ops(void *priv_ops) | |
241 | { | |
242 | module_put(THIS_MODULE); | |
243 | } | |
244 | ||
245 | static | |
246 | void lttng_channel_destroy(struct channel *chan) | |
247 | { | |
248 | channel_destroy(chan); | |
249 | } | |
250 | ||
881833e3 MD |
251 | static |
252 | struct channel *_channel_create(const char *name, | |
a90917c3 | 253 | struct lttng_channel *lttng_chan, void *buf_addr, |
881833e3 MD |
254 | size_t subbuf_size, size_t num_subbuf, |
255 | unsigned int switch_timer_interval, | |
256 | unsigned int read_timer_interval) | |
257 | { | |
dd5a0db3 MD |
258 | struct channel *chan; |
259 | ||
a36580d5 MD |
260 | chan = channel_create(&client_config, name, |
261 | lttng_chan->session->metadata_cache, buf_addr, | |
881833e3 MD |
262 | subbuf_size, num_subbuf, switch_timer_interval, |
263 | read_timer_interval); | |
dd5a0db3 MD |
264 | if (chan) { |
265 | /* | |
266 | * Ensure this module is not unloaded before we finish | |
267 | * using lttng_relay_transport.ops. | |
268 | */ | |
269 | if (!try_module_get(THIS_MODULE)) { | |
270 | printk(KERN_WARNING "LTT : Can't lock transport module.\n"); | |
271 | goto error; | |
272 | } | |
273 | chan->backend.priv_ops = <tng_relay_transport.ops; | |
274 | chan->backend.release_priv_ops = release_priv_ops; | |
275 | } | |
276 | return chan; | |
881833e3 | 277 | |
dd5a0db3 MD |
278 | error: |
279 | lttng_channel_destroy(chan); | |
280 | return NULL; | |
881833e3 MD |
281 | } |
282 | ||
283 | static | |
a90917c3 | 284 | struct lib_ring_buffer *lttng_buffer_read_open(struct channel *chan) |
881833e3 MD |
285 | { |
286 | struct lib_ring_buffer *buf; | |
881833e3 | 287 | |
cd4bd11f MD |
288 | buf = channel_get_ring_buffer(&client_config, chan, 0); |
289 | if (!lib_ring_buffer_open_read(buf)) | |
290 | return buf; | |
881833e3 MD |
291 | return NULL; |
292 | } | |
293 | ||
f71ecafa | 294 | static |
a90917c3 | 295 | int lttng_buffer_has_read_closed_stream(struct channel *chan) |
f71ecafa MD |
296 | { |
297 | struct lib_ring_buffer *buf; | |
298 | int cpu; | |
299 | ||
300 | for_each_channel_cpu(cpu, chan) { | |
301 | buf = channel_get_ring_buffer(&client_config, chan, cpu); | |
302 | if (!atomic_long_read(&buf->active_readers)) | |
303 | return 1; | |
304 | } | |
305 | return 0; | |
306 | } | |
307 | ||
881833e3 | 308 | static |
a90917c3 | 309 | void lttng_buffer_read_close(struct lib_ring_buffer *buf) |
881833e3 MD |
310 | { |
311 | lib_ring_buffer_release_read(buf); | |
881833e3 MD |
312 | } |
313 | ||
c099397a | 314 | static |
a90917c3 | 315 | int lttng_event_reserve(struct lib_ring_buffer_ctx *ctx, uint32_t event_id) |
881833e3 | 316 | { |
85a07c33 MD |
317 | int ret; |
318 | ||
cc62f29e | 319 | ret = lib_ring_buffer_reserve(&client_config, ctx, NULL); |
85a07c33 MD |
320 | if (ret) |
321 | return ret; | |
322 | lib_ring_buffer_backend_get_pages(&client_config, ctx, | |
323 | &ctx->backend_pages); | |
324 | return 0; | |
325 | ||
881833e3 MD |
326 | } |
327 | ||
c099397a | 328 | static |
a90917c3 | 329 | void lttng_event_commit(struct lib_ring_buffer_ctx *ctx) |
881833e3 MD |
330 | { |
331 | lib_ring_buffer_commit(&client_config, ctx); | |
332 | } | |
333 | ||
c099397a | 334 | static |
a90917c3 | 335 | void lttng_event_write(struct lib_ring_buffer_ctx *ctx, const void *src, |
881833e3 MD |
336 | size_t len) |
337 | { | |
338 | lib_ring_buffer_write(&client_config, ctx, src, len); | |
339 | } | |
340 | ||
4ea00e4f | 341 | static |
a90917c3 | 342 | void lttng_event_write_from_user(struct lib_ring_buffer_ctx *ctx, |
4ea00e4f JD |
343 | const void __user *src, size_t len) |
344 | { | |
7b8ea3a5 | 345 | lib_ring_buffer_copy_from_user_inatomic(&client_config, ctx, src, len); |
4ea00e4f JD |
346 | } |
347 | ||
58aa5d24 | 348 | static |
a90917c3 | 349 | void lttng_event_memset(struct lib_ring_buffer_ctx *ctx, |
58aa5d24 MD |
350 | int c, size_t len) |
351 | { | |
352 | lib_ring_buffer_memset(&client_config, ctx, c, len); | |
353 | } | |
354 | ||
16f78f3a MD |
355 | static |
356 | void lttng_event_strcpy(struct lib_ring_buffer_ctx *ctx, const char *src, | |
357 | size_t len) | |
358 | { | |
359 | lib_ring_buffer_strcpy(&client_config, ctx, src, len, '#'); | |
360 | } | |
361 | ||
1ec3f75a | 362 | static |
a90917c3 | 363 | size_t lttng_packet_avail_size(struct channel *chan) |
1ec3f75a MD |
364 | |
365 | { | |
366 | unsigned long o_begin; | |
367 | struct lib_ring_buffer *buf; | |
368 | ||
369 | buf = chan->backend.buf; /* Only for global buffer ! */ | |
370 | o_begin = v_read(&client_config, &buf->offset); | |
371 | if (subbuf_offset(o_begin, chan) != 0) { | |
372 | return chan->backend.subbuf_size - subbuf_offset(o_begin, chan); | |
373 | } else { | |
374 | return chan->backend.subbuf_size - subbuf_offset(o_begin, chan) | |
375 | - sizeof(struct metadata_packet_header); | |
376 | } | |
377 | } | |
378 | ||
c099397a | 379 | static |
a90917c3 | 380 | wait_queue_head_t *lttng_get_writer_buf_wait_queue(struct channel *chan, int cpu) |
c099397a | 381 | { |
71c1d843 MD |
382 | struct lib_ring_buffer *buf = channel_get_ring_buffer(&client_config, |
383 | chan, cpu); | |
384 | return &buf->write_wait; | |
24cedcfe MD |
385 | } |
386 | ||
387 | static | |
a90917c3 | 388 | wait_queue_head_t *lttng_get_hp_wait_queue(struct channel *chan) |
24cedcfe MD |
389 | { |
390 | return &chan->hp_wait; | |
391 | } | |
392 | ||
393 | static | |
a90917c3 | 394 | int lttng_is_finalized(struct channel *chan) |
24cedcfe MD |
395 | { |
396 | return lib_ring_buffer_channel_is_finalized(chan); | |
c099397a MD |
397 | } |
398 | ||
254ec7bc | 399 | static |
a90917c3 | 400 | int lttng_is_disabled(struct channel *chan) |
254ec7bc MD |
401 | { |
402 | return lib_ring_buffer_channel_is_disabled(chan); | |
403 | } | |
404 | ||
a90917c3 | 405 | static struct lttng_transport lttng_relay_transport = { |
881833e3 MD |
406 | .name = "relay-" RING_BUFFER_MODE_TEMPLATE_STRING, |
407 | .owner = THIS_MODULE, | |
408 | .ops = { | |
409 | .channel_create = _channel_create, | |
a90917c3 MD |
410 | .channel_destroy = lttng_channel_destroy, |
411 | .buffer_read_open = lttng_buffer_read_open, | |
f71ecafa | 412 | .buffer_has_read_closed_stream = |
a90917c3 MD |
413 | lttng_buffer_has_read_closed_stream, |
414 | .buffer_read_close = lttng_buffer_read_close, | |
415 | .event_reserve = lttng_event_reserve, | |
416 | .event_commit = lttng_event_commit, | |
417 | .event_write_from_user = lttng_event_write_from_user, | |
418 | .event_memset = lttng_event_memset, | |
419 | .event_write = lttng_event_write, | |
16f78f3a | 420 | .event_strcpy = lttng_event_strcpy, |
a90917c3 MD |
421 | .packet_avail_size = lttng_packet_avail_size, |
422 | .get_writer_buf_wait_queue = lttng_get_writer_buf_wait_queue, | |
423 | .get_hp_wait_queue = lttng_get_hp_wait_queue, | |
424 | .is_finalized = lttng_is_finalized, | |
425 | .is_disabled = lttng_is_disabled, | |
67a80835 MD |
426 | .timestamp_begin = client_timestamp_begin, |
427 | .timestamp_end = client_timestamp_end, | |
428 | .events_discarded = client_events_discarded, | |
429 | .content_size = client_content_size, | |
430 | .packet_size = client_packet_size, | |
431 | .stream_id = client_stream_id, | |
432 | .current_timestamp = client_current_timestamp, | |
5b3cf4f9 | 433 | .sequence_number = client_sequence_number, |
5594698f | 434 | .instance_id = client_instance_id, |
881833e3 MD |
435 | }, |
436 | }; | |
437 | ||
a90917c3 | 438 | static int __init lttng_ring_buffer_client_init(void) |
881833e3 MD |
439 | { |
440 | /* | |
441 | * This vmalloc sync all also takes care of the lib ring buffer | |
442 | * vmalloc'd module pages when it is built as a module into LTTng. | |
443 | */ | |
444 | wrapper_vmalloc_sync_all(); | |
a90917c3 | 445 | lttng_transport_register(<tng_relay_transport); |
881833e3 MD |
446 | return 0; |
447 | } | |
448 | ||
a90917c3 | 449 | module_init(lttng_ring_buffer_client_init); |
881833e3 | 450 | |
a90917c3 | 451 | static void __exit lttng_ring_buffer_client_exit(void) |
881833e3 | 452 | { |
a90917c3 | 453 | lttng_transport_unregister(<tng_relay_transport); |
881833e3 MD |
454 | } |
455 | ||
a90917c3 | 456 | module_exit(lttng_ring_buffer_client_exit); |
881833e3 MD |
457 | |
458 | MODULE_LICENSE("GPL and additional rights"); | |
459 | MODULE_AUTHOR("Mathieu Desnoyers"); | |
460 | MODULE_DESCRIPTION("LTTng ring buffer " RING_BUFFER_MODE_TEMPLATE_STRING | |
461 | " client"); |