Commit | Line | Data |
---|---|---|
7514523f | 1 | /* |
3d084699 | 2 | * ltt-ring-buffer-client.h |
7514523f MD |
3 | * |
4 | * Copyright (C) 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
5 | * | |
3d084699 | 6 | * LTTng lib ring buffer client template. |
7514523f MD |
7 | * |
8 | * Dual LGPL v2.1/GPL v2 license. | |
9 | */ | |
10 | ||
11 | #include <linux/module.h> | |
c0e31d2e | 12 | #include <linux/types.h> |
9115fbdc | 13 | #include "lib/bitfield.h" |
b13f3ebe | 14 | #include "wrapper/vmalloc.h" /* for wrapper_vmalloc_sync_all() */ |
f3bc08c5 | 15 | #include "wrapper/trace-clock.h" |
c0e31d2e | 16 | #include "ltt-events.h" |
7514523f | 17 | #include "ltt-tracer.h" |
9115fbdc | 18 | #include "wrapper/ringbuffer/frontend_types.h" |
7514523f | 19 | |
d793d5e1 MD |
20 | /* |
21 | * Keep the natural field alignment for _each field_ within this structure if | |
22 | * you ever add/remove a field from this header. Packed attribute is not used | |
23 | * because gcc generates poor code on at least powerpc and mips. Don't ever | |
24 | * let gcc add padding between the structure elements. | |
25 | */ | |
9115fbdc | 26 | |
d793d5e1 | 27 | struct packet_header { |
9115fbdc | 28 | /* Trace packet header */ |
d793d5e1 MD |
29 | uint32_t magic; /* |
30 | * Trace magic number. | |
31 | * contains endianness information. | |
32 | */ | |
1ec3f75a | 33 | uint8_t uuid[16]; |
d793d5e1 | 34 | uint32_t stream_id; |
9115fbdc MD |
35 | |
36 | struct { | |
37 | /* Stream packet context */ | |
38 | uint64_t timestamp_begin; /* Cycle count at subbuffer start */ | |
39 | uint64_t timestamp_end; /* Cycle count at subbuffer end */ | |
40 | uint32_t events_discarded; /* | |
41 | * Events lost in this subbuffer since | |
42 | * the beginning of the trace. | |
43 | * (may overflow) | |
44 | */ | |
45 | uint32_t content_size; /* Size of data in subbuffer */ | |
46 | uint32_t packet_size; /* Subbuffer size (include padding) */ | |
47 | uint32_t cpu_id; /* CPU id associated with stream */ | |
48 | uint8_t header_end; /* End of header */ | |
49 | } ctx; | |
d793d5e1 MD |
50 | }; |
51 | ||
52 | ||
881833e3 MD |
53 | static inline notrace u64 lib_ring_buffer_clock_read(struct channel *chan) |
54 | { | |
55 | return trace_clock_read64(); | |
56 | } | |
57 | ||
58 | /* | |
59 | * record_header_size - Calculate the header size and padding necessary. | |
60 | * @config: ring buffer instance configuration | |
61 | * @chan: channel | |
62 | * @offset: offset in the write buffer | |
881833e3 | 63 | * @pre_header_padding: padding to add before the header (output) |
881833e3 MD |
64 | * @ctx: reservation context |
65 | * | |
66 | * Returns the event header size (including padding). | |
67 | * | |
881833e3 MD |
68 | * The payload must itself determine its own alignment from the biggest type it |
69 | * contains. | |
70 | */ | |
71 | static __inline__ | |
72 | unsigned char record_header_size(const struct lib_ring_buffer_config *config, | |
73 | struct channel *chan, size_t offset, | |
64c796d8 | 74 | size_t *pre_header_padding, |
881833e3 MD |
75 | struct lib_ring_buffer_ctx *ctx) |
76 | { | |
9115fbdc | 77 | struct ltt_channel *ltt_chan = channel_get_private(chan); |
881833e3 MD |
78 | size_t orig_offset = offset; |
79 | size_t padding; | |
80 | ||
9115fbdc MD |
81 | switch (ltt_chan->header_type) { |
82 | case 1: /* compact */ | |
83 | padding = lib_ring_buffer_align(offset, ltt_alignof(uint32_t)); | |
84 | offset += padding; | |
64c796d8 | 85 | if (!(ctx->rflags & (RING_BUFFER_RFLAG_FULL_TSC | LTT_RFLAG_EXTENDED))) { |
9115fbdc MD |
86 | offset += sizeof(uint32_t); /* id and timestamp */ |
87 | } else { | |
88 | /* Minimum space taken by 5-bit id */ | |
89 | offset += sizeof(uint8_t); | |
90 | /* Align extended struct on largest member */ | |
91 | offset += lib_ring_buffer_align(offset, ltt_alignof(uint64_t)); | |
92 | offset += sizeof(uint32_t); /* id */ | |
93 | offset += lib_ring_buffer_align(offset, ltt_alignof(uint64_t)); | |
94 | offset += sizeof(uint64_t); /* timestamp */ | |
95 | } | |
96 | break; | |
97 | case 2: /* large */ | |
98 | padding = lib_ring_buffer_align(offset, ltt_alignof(uint16_t)); | |
99 | offset += padding; | |
100 | offset += sizeof(uint16_t); | |
64c796d8 | 101 | if (!(ctx->rflags & (RING_BUFFER_RFLAG_FULL_TSC | LTT_RFLAG_EXTENDED))) { |
9115fbdc MD |
102 | offset += lib_ring_buffer_align(offset, ltt_alignof(uint32_t)); |
103 | offset += sizeof(uint32_t); /* timestamp */ | |
104 | } else { | |
105 | /* Align extended struct on largest member */ | |
106 | offset += lib_ring_buffer_align(offset, ltt_alignof(uint64_t)); | |
107 | offset += sizeof(uint32_t); /* id */ | |
108 | offset += lib_ring_buffer_align(offset, ltt_alignof(uint64_t)); | |
109 | offset += sizeof(uint64_t); /* timestamp */ | |
110 | ||
881833e3 | 111 | } |
9115fbdc MD |
112 | break; |
113 | default: | |
64c796d8 | 114 | WARN_ON_ONCE(1); |
881833e3 MD |
115 | } |
116 | ||
117 | *pre_header_padding = padding; | |
118 | return offset - orig_offset; | |
119 | } | |
120 | ||
121 | #include "wrapper/ringbuffer/api.h" | |
122 | ||
123 | extern | |
124 | void ltt_write_event_header_slow(const struct lib_ring_buffer_config *config, | |
125 | struct lib_ring_buffer_ctx *ctx, | |
64c796d8 | 126 | uint32_t event_id); |
881833e3 MD |
127 | |
128 | /* | |
129 | * ltt_write_event_header | |
130 | * | |
131 | * Writes the event header to the offset (already aligned on 32-bits). | |
132 | * | |
133 | * @config: ring buffer instance configuration | |
134 | * @ctx: reservation context | |
4e1f08f4 | 135 | * @event_id: event ID |
881833e3 MD |
136 | */ |
137 | static __inline__ | |
138 | void ltt_write_event_header(const struct lib_ring_buffer_config *config, | |
139 | struct lib_ring_buffer_ctx *ctx, | |
64c796d8 | 140 | uint32_t event_id) |
881833e3 | 141 | { |
9115fbdc | 142 | struct ltt_channel *ltt_chan = channel_get_private(ctx->chan); |
881833e3 MD |
143 | |
144 | if (unlikely(ctx->rflags)) | |
145 | goto slow_path; | |
146 | ||
9115fbdc MD |
147 | switch (ltt_chan->header_type) { |
148 | case 1: /* compact */ | |
149 | { | |
150 | uint32_t id_time = 0; | |
151 | ||
4e1f08f4 | 152 | bt_bitfield_write(&id_time, uint32_t, 0, 5, event_id); |
9115fbdc MD |
153 | bt_bitfield_write(&id_time, uint32_t, 5, 27, ctx->tsc); |
154 | lib_ring_buffer_write(config, ctx, &id_time, sizeof(id_time)); | |
155 | break; | |
156 | } | |
157 | case 2: /* large */ | |
158 | { | |
9115fbdc MD |
159 | uint32_t timestamp = (uint32_t) ctx->tsc; |
160 | ||
161 | lib_ring_buffer_write(config, ctx, &event_id, sizeof(event_id)); | |
162 | lib_ring_buffer_align_ctx(ctx, ltt_alignof(uint32_t)); | |
163 | lib_ring_buffer_write(config, ctx, ×tamp, sizeof(timestamp)); | |
164 | break; | |
165 | } | |
166 | default: | |
64c796d8 | 167 | WARN_ON_ONCE(1); |
9115fbdc MD |
168 | } |
169 | return; | |
881833e3 MD |
170 | |
171 | slow_path: | |
4e1f08f4 | 172 | ltt_write_event_header_slow(config, ctx, event_id); |
881833e3 MD |
173 | } |
174 | ||
881833e3 | 175 | void ltt_write_event_header_slow(const struct lib_ring_buffer_config *config, |
64c796d8 MD |
176 | struct lib_ring_buffer_ctx *ctx, |
177 | uint32_t event_id) | |
881833e3 | 178 | { |
9115fbdc MD |
179 | struct ltt_channel *ltt_chan = channel_get_private(ctx->chan); |
180 | ||
181 | switch (ltt_chan->header_type) { | |
182 | case 1: /* compact */ | |
64c796d8 | 183 | if (!(ctx->rflags & (RING_BUFFER_RFLAG_FULL_TSC | LTT_RFLAG_EXTENDED))) { |
9115fbdc MD |
184 | uint32_t id_time = 0; |
185 | ||
4e1f08f4 | 186 | bt_bitfield_write(&id_time, uint32_t, 0, 5, event_id); |
9115fbdc MD |
187 | bt_bitfield_write(&id_time, uint32_t, 5, 27, ctx->tsc); |
188 | lib_ring_buffer_write(config, ctx, &id_time, sizeof(id_time)); | |
189 | } else { | |
190 | uint8_t id = 0; | |
9115fbdc MD |
191 | uint64_t timestamp = ctx->tsc; |
192 | ||
193 | bt_bitfield_write(&id, uint8_t, 0, 5, 31); | |
194 | lib_ring_buffer_write(config, ctx, &id, sizeof(id)); | |
195 | /* Align extended struct on largest member */ | |
196 | lib_ring_buffer_align_ctx(ctx, ltt_alignof(uint64_t)); | |
197 | lib_ring_buffer_write(config, ctx, &event_id, sizeof(event_id)); | |
198 | lib_ring_buffer_align_ctx(ctx, ltt_alignof(uint64_t)); | |
199 | lib_ring_buffer_write(config, ctx, ×tamp, sizeof(timestamp)); | |
200 | } | |
881833e3 | 201 | break; |
9115fbdc MD |
202 | case 2: /* large */ |
203 | { | |
64c796d8 | 204 | if (!(ctx->rflags & (RING_BUFFER_RFLAG_FULL_TSC | LTT_RFLAG_EXTENDED))) { |
9115fbdc MD |
205 | uint32_t timestamp = (uint32_t) ctx->tsc; |
206 | ||
207 | lib_ring_buffer_write(config, ctx, &event_id, sizeof(event_id)); | |
208 | lib_ring_buffer_align_ctx(ctx, ltt_alignof(uint32_t)); | |
209 | lib_ring_buffer_write(config, ctx, ×tamp, sizeof(timestamp)); | |
210 | } else { | |
64c796d8 | 211 | uint16_t id = 65535; |
9115fbdc MD |
212 | uint64_t timestamp = ctx->tsc; |
213 | ||
64c796d8 | 214 | lib_ring_buffer_write(config, ctx, &id, sizeof(id)); |
9115fbdc MD |
215 | /* Align extended struct on largest member */ |
216 | lib_ring_buffer_align_ctx(ctx, ltt_alignof(uint64_t)); | |
64c796d8 | 217 | lib_ring_buffer_write(config, ctx, &event_id, sizeof(event_id)); |
9115fbdc MD |
218 | lib_ring_buffer_align_ctx(ctx, ltt_alignof(uint64_t)); |
219 | lib_ring_buffer_write(config, ctx, ×tamp, sizeof(timestamp)); | |
220 | } | |
881833e3 | 221 | break; |
881833e3 | 222 | } |
9115fbdc | 223 | default: |
64c796d8 | 224 | WARN_ON_ONCE(1); |
881833e3 MD |
225 | } |
226 | } | |
227 | ||
7514523f MD |
228 | static const struct lib_ring_buffer_config client_config; |
229 | ||
230 | static u64 client_ring_buffer_clock_read(struct channel *chan) | |
231 | { | |
232 | return lib_ring_buffer_clock_read(chan); | |
233 | } | |
234 | ||
1e2015dc | 235 | static |
7514523f MD |
236 | size_t client_record_header_size(const struct lib_ring_buffer_config *config, |
237 | struct channel *chan, size_t offset, | |
7514523f | 238 | size_t *pre_header_padding, |
7514523f MD |
239 | struct lib_ring_buffer_ctx *ctx) |
240 | { | |
64c796d8 MD |
241 | return record_header_size(config, chan, offset, |
242 | pre_header_padding, ctx); | |
7514523f MD |
243 | } |
244 | ||
245 | /** | |
1c25284c | 246 | * client_packet_header_size - called on buffer-switch to a new sub-buffer |
7514523f MD |
247 | * |
248 | * Return header size without padding after the structure. Don't use packed | |
249 | * structure because gcc generates inefficient code on some architectures | |
250 | * (powerpc, mips..) | |
251 | */ | |
1c25284c | 252 | static size_t client_packet_header_size(void) |
7514523f | 253 | { |
9115fbdc | 254 | return offsetof(struct packet_header, ctx.header_end); |
7514523f MD |
255 | } |
256 | ||
257 | static void client_buffer_begin(struct lib_ring_buffer *buf, u64 tsc, | |
258 | unsigned int subbuf_idx) | |
259 | { | |
260 | struct channel *chan = buf->backend.chan; | |
1c25284c MD |
261 | struct packet_header *header = |
262 | (struct packet_header *) | |
7514523f MD |
263 | lib_ring_buffer_offset_address(&buf->backend, |
264 | subbuf_idx * chan->backend.subbuf_size); | |
9115fbdc MD |
265 | struct ltt_channel *ltt_chan = channel_get_private(chan); |
266 | struct ltt_session *session = ltt_chan->session; | |
7514523f | 267 | |
d793d5e1 | 268 | header->magic = CTF_MAGIC_NUMBER; |
1ec3f75a | 269 | memcpy(header->uuid, session->uuid.b, sizeof(session->uuid)); |
9115fbdc MD |
270 | header->stream_id = ltt_chan->id; |
271 | header->ctx.timestamp_begin = tsc; | |
272 | header->ctx.timestamp_end = 0; | |
273 | header->ctx.events_discarded = 0; | |
274 | header->ctx.content_size = 0xFFFFFFFF; /* for debugging */ | |
275 | header->ctx.packet_size = 0xFFFFFFFF; | |
276 | header->ctx.cpu_id = buf->backend.cpu; | |
7514523f MD |
277 | } |
278 | ||
279 | /* | |
280 | * offset is assumed to never be 0 here : never deliver a completely empty | |
281 | * subbuffer. data_size is between 1 and subbuf_size. | |
282 | */ | |
283 | static void client_buffer_end(struct lib_ring_buffer *buf, u64 tsc, | |
284 | unsigned int subbuf_idx, unsigned long data_size) | |
285 | { | |
286 | struct channel *chan = buf->backend.chan; | |
1c25284c MD |
287 | struct packet_header *header = |
288 | (struct packet_header *) | |
7514523f MD |
289 | lib_ring_buffer_offset_address(&buf->backend, |
290 | subbuf_idx * chan->backend.subbuf_size); | |
291 | unsigned long records_lost = 0; | |
292 | ||
9115fbdc | 293 | header->ctx.timestamp_end = tsc; |
05d32c64 MD |
294 | header->ctx.content_size = data_size * CHAR_BIT; /* in bits */ |
295 | header->ctx.packet_size = PAGE_ALIGN(data_size) * CHAR_BIT; /* in bits */ | |
7514523f MD |
296 | records_lost += lib_ring_buffer_get_records_lost_full(&client_config, buf); |
297 | records_lost += lib_ring_buffer_get_records_lost_wrap(&client_config, buf); | |
298 | records_lost += lib_ring_buffer_get_records_lost_big(&client_config, buf); | |
9115fbdc | 299 | header->ctx.events_discarded = records_lost; |
7514523f MD |
300 | } |
301 | ||
302 | static int client_buffer_create(struct lib_ring_buffer *buf, void *priv, | |
303 | int cpu, const char *name) | |
304 | { | |
1c25284c | 305 | return 0; |
7514523f MD |
306 | } |
307 | ||
308 | static void client_buffer_finalize(struct lib_ring_buffer *buf, void *priv, int cpu) | |
309 | { | |
7514523f MD |
310 | } |
311 | ||
312 | static const struct lib_ring_buffer_config client_config = { | |
313 | .cb.ring_buffer_clock_read = client_ring_buffer_clock_read, | |
314 | .cb.record_header_size = client_record_header_size, | |
1c25284c | 315 | .cb.subbuffer_header_size = client_packet_header_size, |
7514523f MD |
316 | .cb.buffer_begin = client_buffer_begin, |
317 | .cb.buffer_end = client_buffer_end, | |
318 | .cb.buffer_create = client_buffer_create, | |
319 | .cb.buffer_finalize = client_buffer_finalize, | |
320 | ||
321 | .tsc_bits = 32, | |
322 | .alloc = RING_BUFFER_ALLOC_PER_CPU, | |
323 | .sync = RING_BUFFER_SYNC_PER_CPU, | |
3d084699 | 324 | .mode = RING_BUFFER_MODE_TEMPLATE, |
7514523f MD |
325 | .backend = RING_BUFFER_PAGE, |
326 | .output = RING_BUFFER_SPLICE, | |
327 | .oops = RING_BUFFER_OOPS_CONSISTENCY, | |
328 | .ipi = RING_BUFFER_IPI_BARRIER, | |
329 | .wakeup = RING_BUFFER_WAKEUP_BY_TIMER, | |
330 | }; | |
331 | ||
1e2015dc | 332 | static |
1c25284c | 333 | struct channel *_channel_create(const char *name, |
9115fbdc | 334 | struct ltt_channel *ltt_chan, void *buf_addr, |
1c25284c MD |
335 | size_t subbuf_size, size_t num_subbuf, |
336 | unsigned int switch_timer_interval, | |
337 | unsigned int read_timer_interval) | |
7514523f | 338 | { |
9115fbdc | 339 | return channel_create(&client_config, name, ltt_chan, buf_addr, |
7514523f MD |
340 | subbuf_size, num_subbuf, switch_timer_interval, |
341 | read_timer_interval); | |
7514523f MD |
342 | } |
343 | ||
1e2015dc | 344 | static |
7514523f MD |
345 | void ltt_channel_destroy(struct channel *chan) |
346 | { | |
7514523f | 347 | channel_destroy(chan); |
7514523f MD |
348 | } |
349 | ||
ad1c05e1 MD |
350 | static |
351 | struct lib_ring_buffer *ltt_buffer_read_open(struct channel *chan) | |
352 | { | |
353 | struct lib_ring_buffer *buf; | |
354 | int cpu; | |
355 | ||
1c25284c MD |
356 | for_each_channel_cpu(cpu, chan) { |
357 | buf = channel_get_ring_buffer(&client_config, chan, cpu); | |
ad1c05e1 MD |
358 | if (!lib_ring_buffer_open_read(buf)) |
359 | return buf; | |
360 | } | |
361 | return NULL; | |
362 | } | |
363 | ||
364 | static | |
1c25284c | 365 | void ltt_buffer_read_close(struct lib_ring_buffer *buf) |
ad1c05e1 MD |
366 | { |
367 | lib_ring_buffer_release_read(buf); | |
1c25284c MD |
368 | |
369 | } | |
370 | ||
c099397a | 371 | static |
4e1f08f4 | 372 | int ltt_event_reserve(struct lib_ring_buffer_ctx *ctx, |
64c796d8 | 373 | uint32_t event_id) |
1c25284c | 374 | { |
64c796d8 | 375 | struct ltt_channel *ltt_chan = channel_get_private(ctx->chan); |
1c25284c MD |
376 | int ret, cpu; |
377 | ||
378 | cpu = lib_ring_buffer_get_cpu(&client_config); | |
379 | if (cpu < 0) | |
380 | return -EPERM; | |
381 | ctx->cpu = cpu; | |
382 | ||
64c796d8 MD |
383 | switch (ltt_chan->header_type) { |
384 | case 1: /* compact */ | |
385 | if (event_id > 30) | |
386 | ctx->rflags |= LTT_RFLAG_EXTENDED; | |
387 | break; | |
388 | case 2: /* large */ | |
389 | if (event_id > 65534) | |
390 | ctx->rflags |= LTT_RFLAG_EXTENDED; | |
391 | break; | |
392 | default: | |
393 | WARN_ON_ONCE(1); | |
394 | } | |
395 | ||
1c25284c MD |
396 | ret = lib_ring_buffer_reserve(&client_config, ctx); |
397 | if (ret) | |
398 | goto put; | |
4e1f08f4 MD |
399 | ltt_write_event_header(&client_config, ctx, event_id); |
400 | return 0; | |
1c25284c MD |
401 | put: |
402 | lib_ring_buffer_put_cpu(&client_config); | |
403 | return ret; | |
ad1c05e1 MD |
404 | } |
405 | ||
c099397a | 406 | static |
1c25284c MD |
407 | void ltt_event_commit(struct lib_ring_buffer_ctx *ctx) |
408 | { | |
409 | lib_ring_buffer_commit(&client_config, ctx); | |
410 | lib_ring_buffer_put_cpu(&client_config); | |
411 | } | |
412 | ||
c099397a | 413 | static |
e763dbf5 MD |
414 | void ltt_event_write(struct lib_ring_buffer_ctx *ctx, const void *src, |
415 | size_t len) | |
416 | { | |
417 | lib_ring_buffer_write(&client_config, ctx, src, len); | |
418 | } | |
1c25284c | 419 | |
c099397a MD |
420 | static |
421 | wait_queue_head_t *ltt_get_reader_wait_queue(struct ltt_channel *chan) | |
422 | { | |
423 | return &chan->chan->read_wait; | |
424 | } | |
425 | ||
7514523f | 426 | static struct ltt_transport ltt_relay_transport = { |
3d084699 | 427 | .name = "relay-" RING_BUFFER_MODE_TEMPLATE_STRING, |
7514523f MD |
428 | .owner = THIS_MODULE, |
429 | .ops = { | |
1c25284c MD |
430 | .channel_create = _channel_create, |
431 | .channel_destroy = ltt_channel_destroy, | |
ad1c05e1 MD |
432 | .buffer_read_open = ltt_buffer_read_open, |
433 | .buffer_read_close = ltt_buffer_read_close, | |
1c25284c MD |
434 | .event_reserve = ltt_event_reserve, |
435 | .event_commit = ltt_event_commit, | |
e763dbf5 | 436 | .event_write = ltt_event_write, |
1ec3f75a | 437 | .packet_avail_size = NULL, /* Would be racy anyway */ |
c099397a | 438 | .get_reader_wait_queue = ltt_get_reader_wait_queue, |
7514523f MD |
439 | }, |
440 | }; | |
441 | ||
3d084699 | 442 | static int __init ltt_ring_buffer_client_init(void) |
7514523f | 443 | { |
a509e133 MD |
444 | /* |
445 | * This vmalloc sync all also takes care of the lib ring buffer | |
446 | * vmalloc'd module pages when it is built as a module into LTTng. | |
447 | */ | |
6d2a620c | 448 | wrapper_vmalloc_sync_all(); |
7514523f MD |
449 | printk(KERN_INFO "LTT : ltt ring buffer client init\n"); |
450 | ltt_transport_register(<t_relay_transport); | |
451 | return 0; | |
452 | } | |
453 | ||
1c25284c MD |
454 | module_init(ltt_ring_buffer_client_init); |
455 | ||
3d084699 | 456 | static void __exit ltt_ring_buffer_client_exit(void) |
7514523f MD |
457 | { |
458 | printk(KERN_INFO "LTT : ltt ring buffer client exit\n"); | |
459 | ltt_transport_unregister(<t_relay_transport); | |
460 | } | |
461 | ||
1c25284c MD |
462 | module_exit(ltt_ring_buffer_client_exit); |
463 | ||
7514523f MD |
464 | MODULE_LICENSE("GPL and additional rights"); |
465 | MODULE_AUTHOR("Mathieu Desnoyers"); | |
3d084699 MD |
466 | MODULE_DESCRIPTION("LTTng ring buffer " RING_BUFFER_MODE_TEMPLATE_STRING |
467 | " client"); |