Ongoing transition to the generic ring buffer
[lttng-modules.git] / ltt-tracer.h
1 /*
2 * Copyright (C) 2005,2006,2008 Mathieu Desnoyers (mathieu.desnoyers@polymtl.ca)
3 *
4 * This contains the definitions for the Linux Trace Toolkit tracer.
5 *
6 * Dual LGPL v2.1/GPL v2 license.
7 */
8
9 #ifndef _LTT_TRACER_H
10 #define _LTT_TRACER_H
11
12 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
13 /* Align data on its natural alignment */
14 #define RING_BUFFER_ALIGN
15 #endif
16
17 #include <linux/ringbuffer/config.h>
18
19 #include <stdarg.h>
20 #include <linux/types.h>
21 #include <linux/limits.h>
22 #include <linux/list.h>
23 #include <linux/cache.h>
24 #include <linux/kernel.h>
25 #include <linux/timex.h>
26 #include <linux/wait.h>
27 #include <linux/marker.h>
28 #include <linux/trace-clock.h>
29 #include <asm/atomic.h>
30 #include <asm/local.h>
31
32 #include "ltt-tracer-core.h"
33 #include "ltt-channels.h"
34
35 /* Number of bytes to log with a read/write event */
36 #define LTT_LOG_RW_SIZE 32L
37
38 struct ltt_active_marker;
39
40 /* Maximum number of callbacks per marker */
41 #define LTT_NR_CALLBACKS 10
42
43 struct ltt_serialize_closure {
44 ltt_serialize_cb *callbacks;
45 long cb_args[LTT_NR_CALLBACKS];
46 unsigned int cb_idx;
47 };
48
49 size_t ltt_serialize_data(struct ltt_chanbuf *buf, size_t buf_offset,
50 struct ltt_serialize_closure *closure,
51 void *serialize_private, unsigned int stack_pos_ctx,
52 int *largest_align, const char *fmt, va_list *args);
53
54 struct ltt_available_probe {
55 const char *name; /* probe name */
56 const char *format;
57 marker_probe_func *probe_func;
58 ltt_serialize_cb callbacks[LTT_NR_CALLBACKS];
59 struct list_head node; /* registered probes list */
60 };
61
62 enum ltt_channels {
63 LTT_CHANNEL_METADATA,
64 LTT_CHANNEL_FD_STATE,
65 LTT_CHANNEL_GLOBAL_STATE,
66 LTT_CHANNEL_IRQ_STATE,
67 LTT_CHANNEL_MODULE_STATE,
68 LTT_CHANNEL_NETIF_STATE,
69 LTT_CHANNEL_SOFTIRQ_STATE,
70 LTT_CHANNEL_SWAP_STATE,
71 LTT_CHANNEL_SYSCALL_STATE,
72 LTT_CHANNEL_TASK_STATE,
73 LTT_CHANNEL_VM_STATE,
74 LTT_CHANNEL_FS,
75 LTT_CHANNEL_INPUT,
76 LTT_CHANNEL_IPC,
77 LTT_CHANNEL_KERNEL,
78 LTT_CHANNEL_MM,
79 LTT_CHANNEL_RCU,
80 LTT_CHANNEL_DEFAULT,
81 };
82
83 struct ltt_active_marker {
84 struct list_head node; /* active markers list */
85 const char *channel;
86 const char *name;
87 const char *format;
88 struct ltt_available_probe *probe;
89 };
90
91 extern void ltt_vtrace(const struct marker *mdata, void *probe_data,
92 void *call_data, const char *fmt, va_list *args);
93 extern void ltt_trace(const struct marker *mdata, void *probe_data,
94 void *call_data, const char *fmt, ...);
95
96 size_t ltt_serialize_printf(struct ltt_chanbuf *buf, unsigned long buf_offset,
97 size_t *msg_size, char *output, size_t outlen,
98 const char *fmt);
99
100 /*
101 * Unique ID assigned to each registered probe.
102 */
103 enum marker_id {
104 MARKER_ID_SET_MARKER_ID = 0, /* Static IDs available (range 0-7) */
105 MARKER_ID_SET_MARKER_FORMAT,
106 MARKER_ID_COMPACT, /* Compact IDs (range: 8-127) */
107 MARKER_ID_DYNAMIC, /* Dynamic IDs (range: 128-65535) */
108 };
109
110 /* static ids 0-1 reserved for internal use. */
111 #define MARKER_CORE_IDS 2
112 static __inline__ enum marker_id marker_id_type(uint16_t id)
113 {
114 if (id < MARKER_CORE_IDS)
115 return (enum marker_id)id;
116 else
117 return MARKER_ID_DYNAMIC;
118 }
119
120 struct user_dbg_data {
121 unsigned long avail_size;
122 unsigned long write;
123 unsigned long read;
124 };
125
126 struct ltt_trace_ops {
127 int (*create_dirs) (struct ltt_trace *new_trace);
128 void (*remove_dirs) (struct ltt_trace *new_trace);
129 };
130
131 struct ltt_transport {
132 char *name;
133 struct module *owner;
134 struct list_head node;
135 struct ltt_trace_ops ops;
136 };
137
138 enum trace_mode { LTT_TRACE_NORMAL, LTT_TRACE_FLIGHT, LTT_TRACE_HYBRID };
139
140 #define CHANNEL_FLAG_ENABLE (1U<<0)
141 #define CHANNEL_FLAG_OVERWRITE (1U<<1)
142
143 /* Per-trace information - each trace/flight recorder represented by one */
144 struct ltt_trace {
145 /* First 32 bytes cache-hot cacheline */
146 struct list_head list;
147 struct ltt_chan **channels;
148 unsigned int nr_channels;
149 int active;
150 /* Second 32 bytes cache-hot cacheline */
151 struct ltt_trace_ops *ops;
152 u32 freq_scale;
153 u64 start_freq;
154 u64 start_tsc;
155 unsigned long long start_monotonic;
156 struct timeval start_time;
157 struct ltt_channel_setting *settings;
158 struct {
159 struct dentry *trace_root;
160 struct dentry *ascii_root;
161 } dentry;
162 struct kref kref; /* Each channel has a kref of the trace struct */
163 struct ltt_transport *transport;
164 struct kref ltt_transport_kref;
165 wait_queue_head_t kref_wq; /* Place for ltt_trace_destroy to sleep */
166 char trace_name[NAME_MAX];
167 } ____cacheline_aligned;
168
169 /*
170 * Hardcoded event headers
171 *
172 * event header for a trace with active heartbeat : 27 bits timestamps
173 *
174 * headers are 32-bits aligned. In order to insure such alignment, a dynamic per
175 * trace alignment value must be done.
176 *
177 * Remember that the C compiler does align each member on the boundary
178 * equivalent to their own size.
179 *
180 * As relay subbuffers are aligned on pages, we are sure that they are 4 and 8
181 * bytes aligned, so the buffer header and trace header are aligned.
182 *
183 * Event headers are aligned depending on the trace alignment option.
184 *
185 * Note using C structure bitfields for cross-endianness and portability
186 * concerns.
187 */
188
189 #define LTT_RESERVED_EVENTS 3
190 #define LTT_EVENT_BITS 5
191 #define LTT_FREE_EVENTS ((1 << LTT_EVENT_BITS) - LTT_RESERVED_EVENTS)
192 #define LTT_TSC_BITS 27
193 #define LTT_TSC_MASK ((1 << LTT_TSC_BITS) - 1)
194
195 struct event_header {
196 u32 id_time; /* 5 bits event id (MSB); 27 bits time (LSB) */
197 };
198
199 /* Reservation flags */
200 #define LTT_RFLAG_ID (1 << 0)
201 #define LTT_RFLAG_ID_SIZE (1 << 1)
202 #define LTT_RFLAG_ID_SIZE_TSC (1 << 2)
203
204 #define LTT_MAX_SMALL_SIZE 0xFFFFU
205
206 /*
207 * We use asm/timex.h : cpu_khz/HZ variable in here : we might have to deal
208 * specifically with CPU frequency scaling someday, so using an interpolation
209 * between the start and end of buffer values is not flexible enough. Using an
210 * immediate frequency value permits to calculate directly the times for parts
211 * of a buffer that would be before a frequency change.
212 *
213 * Keep the natural field alignment for _each field_ within this structure if
214 * you ever add/remove a field from this header. Packed attribute is not used
215 * because gcc generates poor code on at least powerpc and mips. Don't ever
216 * let gcc add padding between the structure elements.
217 */
218 struct subbuffer_header {
219 uint64_t cycle_count_begin; /* Cycle count at subbuffer start */
220 uint64_t cycle_count_end; /* Cycle count at subbuffer end */
221 uint32_t magic_number; /*
222 * Trace magic number.
223 * contains endianness information.
224 */
225 uint8_t major_version;
226 uint8_t minor_version;
227 uint8_t arch_size; /* Architecture pointer size */
228 uint8_t alignment; /* LTT data alignment */
229 uint64_t start_time_sec; /* NTP-corrected start time */
230 uint64_t start_time_usec;
231 uint64_t start_freq; /*
232 * Frequency at trace start,
233 * used all along the trace.
234 */
235 uint32_t freq_scale; /* Frequency scaling (divisor) */
236 uint32_t data_size; /* Size of data in subbuffer */
237 uint32_t sb_size; /* Subbuffer size (include padding) */
238 uint32_t events_lost; /*
239 * Events lost in this subbuffer since
240 * the beginning of the trace.
241 * (may overflow)
242 */
243 uint32_t subbuf_corrupt; /*
244 * Corrupted (lost) subbuffers since
245 * the begginig of the trace.
246 * (may overflow)
247 */
248 uint8_t header_end[0]; /* End of header */
249 };
250
251 static inline notrace u64 lib_ring_buffer_clock_read(struct channel *chan)
252 {
253 return trace_clock_read64();
254 }
255
256 /*
257 * record_header_size - Calculate the header size and padding necessary.
258 * @config: ring buffer instance configuration
259 * @chan: channel
260 * @offset: offset in the write buffer
261 * @data_size: size of the payload
262 * @pre_header_padding: padding to add before the header (output)
263 * @rflags: reservation flags
264 * @ctx: reservation context
265 *
266 * Returns the event header size (including padding).
267 *
268 * Important note :
269 * The event header must be 32-bits. The total offset calculated here :
270 *
271 * Alignment of header struct on 32 bits (min arch size, header size)
272 * + sizeof(header struct) (32-bits)
273 * + (opt) u16 (ext. event id)
274 * + (opt) u16 (event_size)
275 * (if event_size == LTT_MAX_SMALL_SIZE, has ext. event size)
276 * + (opt) u32 (ext. event size)
277 * + (opt) u64 full TSC (aligned on min(64-bits, arch size))
278 *
279 * The payload must itself determine its own alignment from the biggest type it
280 * contains.
281 */
282 static __inline__
283 unsigned char record_header_size(const struct lib_ring_buffer_config *config,
284 struct channel *chan, size_t offset,
285 size_t data_size, size_t *pre_header_padding,
286 unsigned int rflags,
287 struct lib_ring_buffer_ctx *ctx)
288 {
289 size_t orig_offset = offset;
290 size_t padding;
291
292 BUILD_BUG_ON(sizeof(struct event_header) != sizeof(u32));
293
294 padding = lib_ring_buffer_align(config, offset,
295 sizeof(struct event_header));
296 offset += padding;
297 offset += sizeof(struct event_header);
298
299 if (unlikely(rflags)) {
300 switch (rflags) {
301 case LTT_RFLAG_ID_SIZE_TSC:
302 offset += sizeof(u16) + sizeof(u16);
303 if (data_size >= LTT_MAX_SMALL_SIZE)
304 offset += sizeof(u32);
305 offset += ltt_align(offset, sizeof(u64));
306 offset += sizeof(u64);
307 break;
308 case LTT_RFLAG_ID_SIZE:
309 offset += sizeof(u16) + sizeof(u16);
310 if (data_size >= LTT_MAX_SMALL_SIZE)
311 offset += sizeof(u32);
312 break;
313 case LTT_RFLAG_ID:
314 offset += sizeof(u16);
315 break;
316 }
317 }
318
319 *pre_header_padding = padding;
320 return offset - orig_offset;
321 }
322
323 #include <linux/ringbuffer/api.h>
324
325 extern
326 size_t ltt_write_event_header_slow(struct ltt_chanbuf_alloc *bufa,
327 struct ltt_chan_alloc *chana,
328 long buf_offset, u16 eID, u32 event_size,
329 u64 tsc, unsigned int rflags);
330
331 /*
332 * ltt_write_event_header
333 *
334 * Writes the event header to the offset (already aligned on 32-bits).
335 *
336 * @config: ring buffer instance configuration
337 * @ctx: reservation context
338 * @eID : event ID
339 * @event_size : size of the event, excluding the event header.
340 */
341 static __inline__
342 void ltt_write_event_header(const struct lib_ring_buffer_config *config,
343 struct lib_ring_buffer_ctx *ctx,
344 u16 eID, u32 event_size)
345 {
346 struct event_header header;
347
348 if (unlikely(ctx->rflags))
349 goto slow_path;
350
351 header.id_time = eID << LTT_TSC_BITS;
352 header.id_time |= (u32)ctx->tsc & LTT_TSC_MASK;
353 lib_ring_buffer_write(config, ctx, &header, sizeof(header));
354
355 slow_path:
356 ltt_write_event_header_slow(config, ctx, eID, event_size);
357 }
358
359 #if 0
360 /*
361 * ltt_read_event_header
362 * buf_offset must aligned on 32 bits
363 */
364 static __inline__
365 size_t ltt_read_event_header(struct ltt_chanbuf_alloc *bufa, long buf_offset,
366 u64 *tsc, u32 *event_size, u16 *eID,
367 unsigned int *rflags)
368 {
369 struct ltt_event_header header;
370 u16 small_size;
371
372 ltt_relay_read(bufa, buf_offset, &header, sizeof(header));
373 buf_offset += sizeof(header);
374
375 *event_size = INT_MAX;
376 *eID = header.id_time >> LTT_TSC_BITS;
377 *tsc = header.id_time & LTT_TSC_MASK;
378
379 switch (*eID) {
380 case 29:
381 *rflags = LTT_RFLAG_ID_SIZE_TSC;
382 ltt_relay_read(bufa, buf_offset, eID, sizeof(u16));
383 buf_offset += sizeof(u16);
384 ltt_relay_read(bufa, buf_offset, &small_size, sizeof(u16));
385 buf_offset += sizeof(u16);
386 if (small_size == LTT_MAX_SMALL_SIZE) {
387 ltt_relay_read(bufa, buf_offset, event_size,
388 sizeof(u32));
389 buf_offset += sizeof(u32);
390 } else
391 *event_size = small_size;
392 buf_offset += ltt_align(buf_offset, sizeof(u64));
393 ltt_relay_read(bufa, buf_offset, tsc, sizeof(u64));
394 buf_offset += sizeof(u64);
395 break;
396 case 30:
397 *rflags = LTT_RFLAG_ID_SIZE;
398 ltt_relay_read(bufa, buf_offset, eID, sizeof(u16));
399 buf_offset += sizeof(u16);
400 ltt_relay_read(bufa, buf_offset, &small_size, sizeof(u16));
401 buf_offset += sizeof(u16);
402 if (small_size == LTT_MAX_SMALL_SIZE) {
403 ltt_relay_read(bufa, buf_offset, event_size,
404 sizeof(u32));
405 buf_offset += sizeof(u32);
406 } else
407 *event_size = small_size;
408 break;
409 case 31:
410 *rflags = LTT_RFLAG_ID;
411 ltt_relay_read(bufa, buf_offset, eID, sizeof(u16));
412 buf_offset += sizeof(u16);
413 break;
414 default:
415 *rflags = 0;
416 break;
417 }
418
419 return buf_offset;
420 }
421 #endif //0
422
423 /*
424 * Control channels :
425 * control/metadata
426 * control/interrupts
427 * control/...
428 *
429 * cpu channel :
430 * cpu
431 */
432 #define LTT_RELAY_ROOT "ltt"
433
434 #define LTT_METADATA_CHANNEL "metadata_state"
435 #define LTT_FD_STATE_CHANNEL "fd_state"
436 #define LTT_GLOBAL_STATE_CHANNEL "global_state"
437 #define LTT_IRQ_STATE_CHANNEL "irq_state"
438 #define LTT_MODULE_STATE_CHANNEL "module_state"
439 #define LTT_NETIF_STATE_CHANNEL "netif_state"
440 #define LTT_SOFTIRQ_STATE_CHANNEL "softirq_state"
441 #define LTT_SWAP_STATE_CHANNEL "swap_state"
442 #define LTT_SYSCALL_STATE_CHANNEL "syscall_state"
443 #define LTT_TASK_STATE_CHANNEL "task_state"
444 #define LTT_VM_STATE_CHANNEL "vm_state"
445 #define LTT_FS_CHANNEL "fs"
446 #define LTT_INPUT_CHANNEL "input"
447 #define LTT_IPC_CHANNEL "ipc"
448 #define LTT_KERNEL_CHANNEL "kernel"
449 #define LTT_MM_CHANNEL "mm"
450 #define LTT_RCU_CHANNEL "rcu"
451
452 #define LTT_FLIGHT_PREFIX "flight-"
453
454 #define LTT_ASCII "ascii"
455
456 /* Tracer properties */
457 #define LTT_DEFAULT_SUBBUF_SIZE_LOW 65536
458 #define LTT_DEFAULT_N_SUBBUFS_LOW 2
459 #define LTT_DEFAULT_SUBBUF_SIZE_MED 262144
460 #define LTT_DEFAULT_N_SUBBUFS_MED 2
461 #define LTT_DEFAULT_SUBBUF_SIZE_HIGH 1048576
462 #define LTT_DEFAULT_N_SUBBUFS_HIGH 2
463 #define LTT_TRACER_MAGIC_NUMBER 0x00D6B7ED
464 #define LTT_TRACER_VERSION_MAJOR 2
465 #define LTT_TRACER_VERSION_MINOR 6
466
467 /**
468 * ltt_write_trace_header - Write trace header
469 * @priv: Private data (struct trace)
470 * @header: Memory address where the information must be written to
471 */
472 static __inline__
473 void ltt_write_trace_header(void *priv,
474 struct subbuffer_header *header)
475 {
476 struct ltt_trace *trace = priv;
477
478 header->magic_number = LTT_TRACER_MAGIC_NUMBER;
479 header->major_version = LTT_TRACER_VERSION_MAJOR;
480 header->minor_version = LTT_TRACER_VERSION_MINOR;
481 header->arch_size = sizeof(void *);
482 header->alignment = lib_ring_buffer_get_alignment();
483 header->start_time_sec = trace->start_time.tv_sec;
484 header->start_time_usec = trace->start_time.tv_usec;
485 header->start_freq = trace->start_freq;
486 header->freq_scale = trace->freq_scale;
487 }
488
489 /*
490 * Size reserved for high priority events (interrupts, NMI, BH) at the end of a
491 * nearly full buffer. User space won't use this last amount of space when in
492 * blocking mode. This space also includes the event header that would be
493 * written by this user space event.
494 */
495 #define LTT_RESERVE_CRITICAL 4096
496
497 /* Register and unregister function pointers */
498
499 enum ltt_module_function {
500 LTT_FUNCTION_RUN_FILTER,
501 LTT_FUNCTION_FILTER_CONTROL,
502 LTT_FUNCTION_STATEDUMP
503 };
504
505 extern int ltt_module_register(enum ltt_module_function name, void *function,
506 struct module *owner);
507 extern void ltt_module_unregister(enum ltt_module_function name);
508
509 void ltt_transport_register(struct ltt_transport *transport);
510 void ltt_transport_unregister(struct ltt_transport *transport);
511
512 /* Exported control function */
513
514 enum ltt_control_msg {
515 LTT_CONTROL_START,
516 LTT_CONTROL_STOP,
517 LTT_CONTROL_CREATE_TRACE,
518 LTT_CONTROL_DESTROY_TRACE
519 };
520
521 union ltt_control_args {
522 struct {
523 enum trace_mode mode;
524 unsigned int subbuf_size_low;
525 unsigned int n_subbufs_low;
526 unsigned int subbuf_size_med;
527 unsigned int n_subbufs_med;
528 unsigned int subbuf_size_high;
529 unsigned int n_subbufs_high;
530 } new_trace;
531 };
532
533 int _ltt_trace_setup(const char *trace_name);
534 int ltt_trace_setup(const char *trace_name);
535 struct ltt_trace *_ltt_trace_find_setup(const char *trace_name);
536 int ltt_trace_set_type(const char *trace_name, const char *trace_type);
537 int ltt_trace_set_channel_subbufsize(const char *trace_name,
538 const char *channel_name,
539 unsigned int size);
540 int ltt_trace_set_channel_subbufcount(const char *trace_name,
541 const char *channel_name,
542 unsigned int cnt);
543 int ltt_trace_set_channel_switch_timer(const char *trace_name,
544 const char *channel_name,
545 unsigned long interval);
546 int ltt_trace_set_channel_overwrite(const char *trace_name,
547 const char *channel_name,
548 unsigned int overwrite);
549 int ltt_trace_alloc(const char *trace_name);
550 int ltt_trace_destroy(const char *trace_name);
551 int ltt_trace_start(const char *trace_name);
552 int ltt_trace_stop(const char *trace_name);
553
554 extern int ltt_control(enum ltt_control_msg msg, const char *trace_name,
555 const char *trace_type, union ltt_control_args args);
556
557 enum ltt_filter_control_msg {
558 LTT_FILTER_DEFAULT_ACCEPT,
559 LTT_FILTER_DEFAULT_REJECT
560 };
561
562 extern int ltt_filter_control(enum ltt_filter_control_msg msg,
563 const char *trace_name);
564
565 extern struct dentry *get_filter_root(void);
566
567 void ltt_core_register(int (*function)(u8, void *));
568
569 void ltt_core_unregister(void);
570
571 void ltt_release_trace(struct kref *kref);
572 void ltt_release_transport(struct kref *kref);
573
574 extern int ltt_probe_register(struct ltt_available_probe *pdata);
575 extern int ltt_probe_unregister(struct ltt_available_probe *pdata);
576 extern int ltt_marker_connect(const char *channel, const char *mname,
577 const char *pname);
578 extern int ltt_marker_disconnect(const char *channel, const char *mname,
579 const char *pname);
580 extern void ltt_dump_marker_state(struct ltt_trace *trace);
581
582 void ltt_lock_traces(void);
583 void ltt_unlock_traces(void);
584
585 extern int ltt_ascii_create_dir(struct ltt_trace *new_trace);
586 extern void ltt_ascii_remove_dir(struct ltt_trace *trace);
587 extern int ltt_ascii_create(struct ltt_chan *chan);
588 extern void ltt_ascii_remove(struct ltt_chan *chan);
589
590 extern
591 void ltt_statedump_register_kprobes_dump(void (*callback)(void *call_data));
592 extern
593 void ltt_statedump_unregister_kprobes_dump(void (*callback)(void *call_data));
594
595 extern void ltt_dump_softirq_vec(void *call_data);
596
597 #ifdef CONFIG_HAVE_LTT_DUMP_TABLES
598 extern void ltt_dump_sys_call_table(void *call_data);
599 extern void ltt_dump_idt_table(void *call_data);
600 #else
601 static inline void ltt_dump_sys_call_table(void *call_data)
602 {
603 }
604
605 static inline void ltt_dump_idt_table(void *call_data)
606 {
607 }
608 #endif
609
610 #endif /* _LTT_TRACER_H */
This page took 0.041043 seconds and 4 git commands to generate.