ca6187bfa6e1d0f2fa607efa16b384f861e07a93
[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 enum trace_mode { LTT_TRACE_NORMAL, LTT_TRACE_FLIGHT, LTT_TRACE_HYBRID };
127
128 #define CHANNEL_FLAG_ENABLE (1U<<0)
129 #define CHANNEL_FLAG_OVERWRITE (1U<<1)
130
131 #if 0
132 /* Per-trace information - each trace/flight recorder represented by one */
133 struct ltt_trace {
134 /* First 32 bytes cache-hot cacheline */
135 struct list_head list;
136 struct ltt_chan **channels;
137 unsigned int nr_channels;
138 int active;
139 /* Second 32 bytes cache-hot cacheline */
140 struct ltt_trace_ops *ops;
141 u32 freq_scale;
142 u64 start_freq;
143 u64 start_tsc;
144 unsigned long long start_monotonic;
145 struct timeval start_time;
146 struct ltt_channel_setting *settings;
147 struct {
148 struct dentry *trace_root;
149 struct dentry *ascii_root;
150 } dentry;
151 struct kref kref; /* Each channel has a kref of the trace struct */
152 struct ltt_transport *transport;
153 struct kref ltt_transport_kref;
154 wait_queue_head_t kref_wq; /* Place for ltt_trace_destroy to sleep */
155 char trace_name[NAME_MAX];
156 } ____cacheline_aligned;
157 #endif //0
158
159 /*
160 * Hardcoded event headers
161 *
162 * event header for a trace with active heartbeat : 27 bits timestamps
163 *
164 * headers are 32-bits aligned. In order to insure such alignment, a dynamic per
165 * trace alignment value must be done.
166 *
167 * Remember that the C compiler does align each member on the boundary
168 * equivalent to their own size.
169 *
170 * As relay subbuffers are aligned on pages, we are sure that they are 4 and 8
171 * bytes aligned, so the buffer header and trace header are aligned.
172 *
173 * Event headers are aligned depending on the trace alignment option.
174 *
175 * Note using C structure bitfields for cross-endianness and portability
176 * concerns.
177 */
178
179 #define LTT_RESERVED_EVENTS 3
180 #define LTT_EVENT_BITS 5
181 #define LTT_FREE_EVENTS ((1 << LTT_EVENT_BITS) - LTT_RESERVED_EVENTS)
182 #define LTT_TSC_BITS 27
183 #define LTT_TSC_MASK ((1 << LTT_TSC_BITS) - 1)
184
185 struct event_header {
186 u32 id_time; /* 5 bits event id (MSB); 27 bits time (LSB) */
187 };
188
189 /* Reservation flags */
190 #define LTT_RFLAG_ID (1 << 0)
191 #define LTT_RFLAG_ID_SIZE (1 << 1)
192 #define LTT_RFLAG_ID_SIZE_TSC (1 << 2)
193
194 #define LTT_MAX_SMALL_SIZE 0xFFFFU
195
196 /*
197 * We use asm/timex.h : cpu_khz/HZ variable in here : we might have to deal
198 * specifically with CPU frequency scaling someday, so using an interpolation
199 * between the start and end of buffer values is not flexible enough. Using an
200 * immediate frequency value permits to calculate directly the times for parts
201 * of a buffer that would be before a frequency change.
202 *
203 * Keep the natural field alignment for _each field_ within this structure if
204 * you ever add/remove a field from this header. Packed attribute is not used
205 * because gcc generates poor code on at least powerpc and mips. Don't ever
206 * let gcc add padding between the structure elements.
207 */
208 struct subbuffer_header {
209 uint64_t cycle_count_begin; /* Cycle count at subbuffer start */
210 uint64_t cycle_count_end; /* Cycle count at subbuffer end */
211 uint32_t magic_number; /*
212 * Trace magic number.
213 * contains endianness information.
214 */
215 uint8_t major_version;
216 uint8_t minor_version;
217 uint8_t arch_size; /* Architecture pointer size */
218 uint8_t alignment; /* LTT data alignment */
219 uint64_t start_time_sec; /* NTP-corrected start time */
220 uint64_t start_time_usec;
221 uint64_t start_freq; /*
222 * Frequency at trace start,
223 * used all along the trace.
224 */
225 uint32_t freq_scale; /* Frequency scaling (divisor) */
226 uint32_t data_size; /* Size of data in subbuffer */
227 uint32_t sb_size; /* Subbuffer size (include padding) */
228 uint32_t events_lost; /*
229 * Events lost in this subbuffer since
230 * the beginning of the trace.
231 * (may overflow)
232 */
233 uint32_t subbuf_corrupt; /*
234 * Corrupted (lost) subbuffers since
235 * the begginig of the trace.
236 * (may overflow)
237 */
238 uint8_t header_end[0]; /* End of header */
239 };
240
241 static inline notrace u64 lib_ring_buffer_clock_read(struct channel *chan)
242 {
243 return trace_clock_read64();
244 }
245
246 /*
247 * record_header_size - Calculate the header size and padding necessary.
248 * @config: ring buffer instance configuration
249 * @chan: channel
250 * @offset: offset in the write buffer
251 * @data_size: size of the payload
252 * @pre_header_padding: padding to add before the header (output)
253 * @rflags: reservation flags
254 * @ctx: reservation context
255 *
256 * Returns the event header size (including padding).
257 *
258 * Important note :
259 * The event header must be 32-bits. The total offset calculated here :
260 *
261 * Alignment of header struct on 32 bits (min arch size, header size)
262 * + sizeof(header struct) (32-bits)
263 * + (opt) u16 (ext. event id)
264 * + (opt) u16 (event_size)
265 * (if event_size == LTT_MAX_SMALL_SIZE, has ext. event size)
266 * + (opt) u32 (ext. event size)
267 * + (opt) u64 full TSC (aligned on min(64-bits, arch size))
268 *
269 * The payload must itself determine its own alignment from the biggest type it
270 * contains.
271 */
272 static __inline__
273 unsigned char record_header_size(const struct lib_ring_buffer_config *config,
274 struct channel *chan, size_t offset,
275 size_t data_size, size_t *pre_header_padding,
276 unsigned int rflags,
277 struct lib_ring_buffer_ctx *ctx)
278 {
279 size_t orig_offset = offset;
280 size_t padding;
281
282 BUILD_BUG_ON(sizeof(struct event_header) != sizeof(u32));
283
284 padding = lib_ring_buffer_align(config, offset,
285 sizeof(struct event_header));
286 offset += padding;
287 offset += sizeof(struct event_header);
288
289 if (unlikely(rflags)) {
290 switch (rflags) {
291 case LTT_RFLAG_ID_SIZE_TSC:
292 offset += sizeof(u16) + sizeof(u16);
293 if (data_size >= LTT_MAX_SMALL_SIZE)
294 offset += sizeof(u32);
295 offset += ltt_align(offset, sizeof(u64));
296 offset += sizeof(u64);
297 break;
298 case LTT_RFLAG_ID_SIZE:
299 offset += sizeof(u16) + sizeof(u16);
300 if (data_size >= LTT_MAX_SMALL_SIZE)
301 offset += sizeof(u32);
302 break;
303 case LTT_RFLAG_ID:
304 offset += sizeof(u16);
305 break;
306 }
307 }
308
309 *pre_header_padding = padding;
310 return offset - orig_offset;
311 }
312
313 #include <linux/ringbuffer/api.h>
314
315 extern
316 size_t ltt_write_event_header_slow(struct ltt_chanbuf_alloc *bufa,
317 struct ltt_chan_alloc *chana,
318 long buf_offset, u16 eID, u32 event_size,
319 u64 tsc, unsigned int rflags);
320
321 /*
322 * ltt_write_event_header
323 *
324 * Writes the event header to the offset (already aligned on 32-bits).
325 *
326 * @config: ring buffer instance configuration
327 * @ctx: reservation context
328 * @eID : event ID
329 * @event_size : size of the event, excluding the event header.
330 */
331 static __inline__
332 void ltt_write_event_header(const struct lib_ring_buffer_config *config,
333 struct lib_ring_buffer_ctx *ctx,
334 u16 eID, u32 event_size)
335 {
336 struct event_header header;
337
338 if (unlikely(ctx->rflags))
339 goto slow_path;
340
341 header.id_time = eID << LTT_TSC_BITS;
342 header.id_time |= (u32)ctx->tsc & LTT_TSC_MASK;
343 lib_ring_buffer_write(config, ctx, &header, sizeof(header));
344
345 slow_path:
346 ltt_write_event_header_slow(config, ctx, eID, event_size);
347 }
348
349 #if 0
350 /*
351 * ltt_read_event_header
352 * buf_offset must aligned on 32 bits
353 */
354 static __inline__
355 size_t ltt_read_event_header(struct ltt_chanbuf_alloc *bufa, long buf_offset,
356 u64 *tsc, u32 *event_size, u16 *eID,
357 unsigned int *rflags)
358 {
359 struct ltt_event_header header;
360 u16 small_size;
361
362 ltt_relay_read(bufa, buf_offset, &header, sizeof(header));
363 buf_offset += sizeof(header);
364
365 *event_size = INT_MAX;
366 *eID = header.id_time >> LTT_TSC_BITS;
367 *tsc = header.id_time & LTT_TSC_MASK;
368
369 switch (*eID) {
370 case 29:
371 *rflags = LTT_RFLAG_ID_SIZE_TSC;
372 ltt_relay_read(bufa, buf_offset, eID, sizeof(u16));
373 buf_offset += sizeof(u16);
374 ltt_relay_read(bufa, buf_offset, &small_size, sizeof(u16));
375 buf_offset += sizeof(u16);
376 if (small_size == LTT_MAX_SMALL_SIZE) {
377 ltt_relay_read(bufa, buf_offset, event_size,
378 sizeof(u32));
379 buf_offset += sizeof(u32);
380 } else
381 *event_size = small_size;
382 buf_offset += ltt_align(buf_offset, sizeof(u64));
383 ltt_relay_read(bufa, buf_offset, tsc, sizeof(u64));
384 buf_offset += sizeof(u64);
385 break;
386 case 30:
387 *rflags = LTT_RFLAG_ID_SIZE;
388 ltt_relay_read(bufa, buf_offset, eID, sizeof(u16));
389 buf_offset += sizeof(u16);
390 ltt_relay_read(bufa, buf_offset, &small_size, sizeof(u16));
391 buf_offset += sizeof(u16);
392 if (small_size == LTT_MAX_SMALL_SIZE) {
393 ltt_relay_read(bufa, buf_offset, event_size,
394 sizeof(u32));
395 buf_offset += sizeof(u32);
396 } else
397 *event_size = small_size;
398 break;
399 case 31:
400 *rflags = LTT_RFLAG_ID;
401 ltt_relay_read(bufa, buf_offset, eID, sizeof(u16));
402 buf_offset += sizeof(u16);
403 break;
404 default:
405 *rflags = 0;
406 break;
407 }
408
409 return buf_offset;
410 }
411 #endif //0
412
413 /*
414 * Control channels :
415 * control/metadata
416 * control/interrupts
417 * control/...
418 *
419 * cpu channel :
420 * cpu
421 */
422 #define LTT_RELAY_ROOT "ltt"
423
424 #define LTT_METADATA_CHANNEL "metadata_state"
425 #define LTT_FD_STATE_CHANNEL "fd_state"
426 #define LTT_GLOBAL_STATE_CHANNEL "global_state"
427 #define LTT_IRQ_STATE_CHANNEL "irq_state"
428 #define LTT_MODULE_STATE_CHANNEL "module_state"
429 #define LTT_NETIF_STATE_CHANNEL "netif_state"
430 #define LTT_SOFTIRQ_STATE_CHANNEL "softirq_state"
431 #define LTT_SWAP_STATE_CHANNEL "swap_state"
432 #define LTT_SYSCALL_STATE_CHANNEL "syscall_state"
433 #define LTT_TASK_STATE_CHANNEL "task_state"
434 #define LTT_VM_STATE_CHANNEL "vm_state"
435 #define LTT_FS_CHANNEL "fs"
436 #define LTT_INPUT_CHANNEL "input"
437 #define LTT_IPC_CHANNEL "ipc"
438 #define LTT_KERNEL_CHANNEL "kernel"
439 #define LTT_MM_CHANNEL "mm"
440 #define LTT_RCU_CHANNEL "rcu"
441
442 #define LTT_FLIGHT_PREFIX "flight-"
443
444 #define LTT_ASCII "ascii"
445
446 /* Tracer properties */
447 #define LTT_DEFAULT_SUBBUF_SIZE_LOW 65536
448 #define LTT_DEFAULT_N_SUBBUFS_LOW 2
449 #define LTT_DEFAULT_SUBBUF_SIZE_MED 262144
450 #define LTT_DEFAULT_N_SUBBUFS_MED 2
451 #define LTT_DEFAULT_SUBBUF_SIZE_HIGH 1048576
452 #define LTT_DEFAULT_N_SUBBUFS_HIGH 2
453 #define LTT_TRACER_MAGIC_NUMBER 0x00D6B7ED
454 #define LTT_TRACER_VERSION_MAJOR 2
455 #define LTT_TRACER_VERSION_MINOR 6
456
457 /**
458 * ltt_write_trace_header - Write trace header
459 * @priv: Private data (struct trace)
460 * @header: Memory address where the information must be written to
461 */
462 static __inline__
463 void ltt_write_trace_header(void *priv,
464 struct subbuffer_header *header)
465 {
466 struct ltt_trace *trace = priv;
467
468 header->magic_number = LTT_TRACER_MAGIC_NUMBER;
469 header->major_version = LTT_TRACER_VERSION_MAJOR;
470 header->minor_version = LTT_TRACER_VERSION_MINOR;
471 header->arch_size = sizeof(void *);
472 header->alignment = lib_ring_buffer_get_alignment();
473 header->start_time_sec = trace->start_time.tv_sec;
474 header->start_time_usec = trace->start_time.tv_usec;
475 header->start_freq = trace->start_freq;
476 header->freq_scale = trace->freq_scale;
477 }
478
479 /*
480 * Size reserved for high priority events (interrupts, NMI, BH) at the end of a
481 * nearly full buffer. User space won't use this last amount of space when in
482 * blocking mode. This space also includes the event header that would be
483 * written by this user space event.
484 */
485 #define LTT_RESERVE_CRITICAL 4096
486
487 /* Register and unregister function pointers */
488
489 enum ltt_module_function {
490 LTT_FUNCTION_RUN_FILTER,
491 LTT_FUNCTION_FILTER_CONTROL,
492 LTT_FUNCTION_STATEDUMP
493 };
494
495 extern int ltt_module_register(enum ltt_module_function name, void *function,
496 struct module *owner);
497 extern void ltt_module_unregister(enum ltt_module_function name);
498
499 /* Exported control function */
500
501 enum ltt_control_msg {
502 LTT_CONTROL_START,
503 LTT_CONTROL_STOP,
504 LTT_CONTROL_CREATE_TRACE,
505 LTT_CONTROL_DESTROY_TRACE
506 };
507
508 union ltt_control_args {
509 struct {
510 enum trace_mode mode;
511 unsigned int subbuf_size_low;
512 unsigned int n_subbufs_low;
513 unsigned int subbuf_size_med;
514 unsigned int n_subbufs_med;
515 unsigned int subbuf_size_high;
516 unsigned int n_subbufs_high;
517 } new_trace;
518 };
519
520 void ltt_core_register(int (*function)(u8, void *));
521
522 void ltt_core_unregister(void);
523
524 extern int ltt_probe_register(struct ltt_available_probe *pdata);
525 extern int ltt_probe_unregister(struct ltt_available_probe *pdata);
526 extern int ltt_marker_connect(const char *channel, const char *mname,
527 const char *pname);
528 extern int ltt_marker_disconnect(const char *channel, const char *mname,
529 const char *pname);
530 extern void ltt_dump_marker_state(struct ltt_trace *trace);
531
532 extern
533 void ltt_statedump_register_kprobes_dump(void (*callback)(void *call_data));
534 extern
535 void ltt_statedump_unregister_kprobes_dump(void (*callback)(void *call_data));
536
537 extern void ltt_dump_softirq_vec(void *call_data);
538
539 #ifdef CONFIG_HAVE_LTT_DUMP_TABLES
540 extern void ltt_dump_sys_call_table(void *call_data);
541 extern void ltt_dump_idt_table(void *call_data);
542 #else
543 static inline void ltt_dump_sys_call_table(void *call_data)
544 {
545 }
546
547 static inline void ltt_dump_idt_table(void *call_data)
548 {
549 }
550 #endif
551
552 #endif /* _LTT_TRACER_H */
This page took 0.039362 seconds and 3 git commands to generate.