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