2 * Copyright (C) 2005,2006,2008 Mathieu Desnoyers (mathieu.desnoyers@polymtl.ca)
4 * This contains the definitions for the Linux Trace Toolkit tracer.
6 * Dual LGPL v2.1/GPL v2 license.
13 #include <linux/types.h>
14 #include <linux/limits.h>
15 #include <linux/list.h>
16 #include <linux/cache.h>
17 #include <linux/timex.h>
18 #include <linux/wait.h>
19 #include <linux/trace-clock.h>
20 #include <asm/atomic.h>
21 #include <asm/local.h>
23 #include "ltt-tracer-core.h"
24 #include "ltt-events.h"
26 /* Number of bytes to log with a read/write event */
27 #define LTT_LOG_RW_SIZE 32L
29 /* Maximum number of callbacks per marker */
30 #define LTT_NR_CALLBACKS 10
32 struct ltt_serialize_closure
;
34 /* Serialization callback */
35 typedef size_t (*ltt_serialize_cb
)(struct lib_ring_buffer
*buf
,
37 struct ltt_serialize_closure
*closure
,
38 void *serialize_private
,
39 unsigned int stack_pos_ctx
,
41 const char *fmt
, va_list *args
);
43 struct ltt_serialize_closure
{
44 ltt_serialize_cb
*callbacks
;
45 long cb_args
[LTT_NR_CALLBACKS
];
49 size_t ltt_serialize_data(struct lib_ring_buffer
*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
);
57 LTT_CHANNEL_GLOBAL_STATE
,
58 LTT_CHANNEL_IRQ_STATE
,
59 LTT_CHANNEL_MODULE_STATE
,
60 LTT_CHANNEL_NETIF_STATE
,
61 LTT_CHANNEL_SOFTIRQ_STATE
,
62 LTT_CHANNEL_SWAP_STATE
,
63 LTT_CHANNEL_SYSCALL_STATE
,
64 LTT_CHANNEL_TASK_STATE
,
76 size_t ltt_serialize_printf(struct lib_ring_buffer
*buf
, unsigned long buf_offset
,
77 size_t *msg_size
, char *output
, size_t outlen
,
81 * Unique ID assigned to each registered probe.
84 MARKER_ID_SET_MARKER_ID
= 0, /* Static IDs available (range 0-7) */
85 MARKER_ID_SET_MARKER_FORMAT
,
86 MARKER_ID_COMPACT
, /* Compact IDs (range: 8-127) */
87 MARKER_ID_DYNAMIC
, /* Dynamic IDs (range: 128-65535) */
90 /* static ids 0-1 reserved for internal use. */
91 #define MARKER_CORE_IDS 2
92 static __inline__
enum marker_id
marker_id_type(uint16_t id
)
94 if (id
< MARKER_CORE_IDS
)
95 return (enum marker_id
)id
;
97 return MARKER_ID_DYNAMIC
;
100 struct user_dbg_data
{
101 unsigned long avail_size
;
106 enum trace_mode
{ LTT_TRACE_NORMAL
, LTT_TRACE_FLIGHT
, LTT_TRACE_HYBRID
};
108 #define CHANNEL_FLAG_ENABLE (1U<<0)
109 #define CHANNEL_FLAG_OVERWRITE (1U<<1)
113 /* Per-trace information - each trace/flight recorder represented by one */
115 /* First 32 bytes cache-hot cacheline */
116 struct list_head list
;
117 struct ltt_chan
**channels
;
118 unsigned int nr_channels
;
120 /* Second 32 bytes cache-hot cacheline */
121 struct ltt_trace_ops
*ops
;
125 unsigned long long start_monotonic
;
126 struct timeval start_time
;
127 struct ltt_channel_setting
*settings
;
129 struct dentry
*trace_root
;
130 struct dentry
*ascii_root
;
132 struct kref kref
; /* Each channel has a kref of the trace struct */
133 struct ltt_transport
*transport
;
134 struct kref ltt_transport_kref
;
135 wait_queue_head_t kref_wq
; /* Place for ltt_trace_destroy to sleep */
136 char trace_name
[NAME_MAX
];
137 } ____cacheline_aligned
;
141 * Hardcoded event headers
143 * event header for a trace with active heartbeat : 27 bits timestamps
145 * headers are 32-bits aligned. In order to insure such alignment, a dynamic per
146 * trace alignment value must be done.
148 * Remember that the C compiler does align each member on the boundary
149 * equivalent to their own size.
151 * As relay subbuffers are aligned on pages, we are sure that they are 4 and 8
152 * bytes aligned, so the buffer header and trace header are aligned.
154 * Event headers are aligned depending on the trace alignment option.
156 * Note using C structure bitfields for cross-endianness and portability
160 #define LTT_RESERVED_EVENTS 3
161 #define LTT_EVENT_BITS 5
162 #define LTT_FREE_EVENTS ((1 << LTT_EVENT_BITS) - LTT_RESERVED_EVENTS)
163 #define LTT_TSC_BITS 27
164 #define LTT_TSC_MASK ((1 << LTT_TSC_BITS) - 1)
166 struct event_header
{
167 u32 id_time
; /* 5 bits event id (MSB); 27 bits time (LSB) */
170 /* Reservation flags */
171 #define LTT_RFLAG_ID (1 << 0)
172 #define LTT_RFLAG_ID_SIZE (1 << 1)
173 #define LTT_RFLAG_ID_SIZE_TSC (1 << 2)
175 #define LTT_MAX_SMALL_SIZE 0xFFFFU
178 * We use asm/timex.h : cpu_khz/HZ variable in here : we might have to deal
179 * specifically with CPU frequency scaling someday, so using an interpolation
180 * between the start and end of buffer values is not flexible enough. Using an
181 * immediate frequency value permits to calculate directly the times for parts
182 * of a buffer that would be before a frequency change.
184 * Keep the natural field alignment for _each field_ within this structure if
185 * you ever add/remove a field from this header. Packed attribute is not used
186 * because gcc generates poor code on at least powerpc and mips. Don't ever
187 * let gcc add padding between the structure elements.
189 struct packet_header
{
191 * Trace magic number.
192 * contains endianness information.
194 uint8_t trace_uuid
[16];
196 uint64_t timestamp_begin
; /* Cycle count at subbuffer start */
197 uint64_t timestamp_end
; /* Cycle count at subbuffer end */
198 uint32_t content_size
; /* Size of data in subbuffer */
199 uint32_t packet_size
; /* Subbuffer size (include padding) */
200 uint32_t events_lost
; /*
201 * Events lost in this subbuffer since
202 * the beginning of the trace.
205 /* TODO: move to metadata */
207 uint8_t major_version
;
208 uint8_t minor_version
;
209 uint8_t arch_size
; /* Architecture pointer size */
210 uint8_t alignment
; /* LTT data alignment */
211 uint64_t start_time_sec
; /* NTP-corrected start time */
212 uint64_t start_time_usec
;
213 uint64_t start_freq
; /*
214 * Frequency at trace start,
215 * used all along the trace.
217 uint32_t freq_scale
; /* Frequency scaling (divisor) */
219 uint8_t header_end
[0]; /* End of header */
222 static inline notrace u64
lib_ring_buffer_clock_read(struct channel
*chan
)
224 return trace_clock_read64();
228 * record_header_size - Calculate the header size and padding necessary.
229 * @config: ring buffer instance configuration
231 * @offset: offset in the write buffer
232 * @data_size: size of the payload
233 * @pre_header_padding: padding to add before the header (output)
234 * @rflags: reservation flags
235 * @ctx: reservation context
237 * Returns the event header size (including padding).
240 * The event header must be 32-bits. The total offset calculated here :
242 * Alignment of header struct on 32 bits (min arch size, header size)
243 * + sizeof(header struct) (32-bits)
244 * + (opt) u16 (ext. event id)
245 * + (opt) u16 (event_size)
246 * (if event_size == LTT_MAX_SMALL_SIZE, has ext. event size)
247 * + (opt) u32 (ext. event size)
248 * + (opt) u64 full TSC (aligned on min(64-bits, arch size))
250 * The payload must itself determine its own alignment from the biggest type it
254 unsigned char record_header_size(const struct lib_ring_buffer_config
*config
,
255 struct channel
*chan
, size_t offset
,
256 size_t data_size
, size_t *pre_header_padding
,
258 struct lib_ring_buffer_ctx
*ctx
)
260 size_t orig_offset
= offset
;
263 BUILD_BUG_ON(sizeof(struct event_header
) != sizeof(u32
));
265 padding
= lib_ring_buffer_align(offset
,
266 sizeof(struct event_header
));
268 offset
+= sizeof(struct event_header
);
270 if (unlikely(rflags
)) {
272 case LTT_RFLAG_ID_SIZE_TSC
:
273 offset
+= sizeof(u16
) + sizeof(u16
);
274 if (data_size
>= LTT_MAX_SMALL_SIZE
)
275 offset
+= sizeof(u32
);
276 offset
+= ltt_align(offset
, sizeof(u64
));
277 offset
+= sizeof(u64
);
279 case LTT_RFLAG_ID_SIZE
:
280 offset
+= sizeof(u16
) + sizeof(u16
);
281 if (data_size
>= LTT_MAX_SMALL_SIZE
)
282 offset
+= sizeof(u32
);
285 offset
+= sizeof(u16
);
290 *pre_header_padding
= padding
;
291 return offset
- orig_offset
;
294 #include <linux/ringbuffer/api.h>
297 void ltt_write_event_header_slow(const struct lib_ring_buffer_config
*config
,
298 struct lib_ring_buffer_ctx
*ctx
,
299 u16 eID
, u32 event_size
);
302 * ltt_write_event_header
304 * Writes the event header to the offset (already aligned on 32-bits).
306 * @config: ring buffer instance configuration
307 * @ctx: reservation context
309 * @event_size : size of the event, excluding the event header.
312 void ltt_write_event_header(const struct lib_ring_buffer_config
*config
,
313 struct lib_ring_buffer_ctx
*ctx
,
314 u16 eID
, u32 event_size
)
316 struct event_header header
;
318 if (unlikely(ctx
->rflags
))
321 header
.id_time
= eID
<< LTT_TSC_BITS
;
322 header
.id_time
|= (u32
)ctx
->tsc
& LTT_TSC_MASK
;
323 lib_ring_buffer_write(config
, ctx
, &header
, sizeof(header
));
326 ltt_write_event_header_slow(config
, ctx
, eID
, event_size
);
331 * ltt_read_event_header
332 * buf_offset must aligned on 32 bits
335 size_t ltt_read_event_header(struct ltt_chanbuf_alloc
*bufa
, long buf_offset
,
336 u64
*tsc
, u32
*event_size
, u16
*eID
,
337 unsigned int *rflags
)
339 struct ltt_event_header header
;
342 ltt_relay_read(bufa
, buf_offset
, &header
, sizeof(header
));
343 buf_offset
+= sizeof(header
);
345 *event_size
= INT_MAX
;
346 *eID
= header
.id_time
>> LTT_TSC_BITS
;
347 *tsc
= header
.id_time
& LTT_TSC_MASK
;
351 *rflags
= LTT_RFLAG_ID_SIZE_TSC
;
352 ltt_relay_read(bufa
, buf_offset
, eID
, sizeof(u16
));
353 buf_offset
+= sizeof(u16
);
354 ltt_relay_read(bufa
, buf_offset
, &small_size
, sizeof(u16
));
355 buf_offset
+= sizeof(u16
);
356 if (small_size
== LTT_MAX_SMALL_SIZE
) {
357 ltt_relay_read(bufa
, buf_offset
, event_size
,
359 buf_offset
+= sizeof(u32
);
361 *event_size
= small_size
;
362 buf_offset
+= ltt_align(buf_offset
, sizeof(u64
));
363 ltt_relay_read(bufa
, buf_offset
, tsc
, sizeof(u64
));
364 buf_offset
+= sizeof(u64
);
367 *rflags
= LTT_RFLAG_ID_SIZE
;
368 ltt_relay_read(bufa
, buf_offset
, eID
, sizeof(u16
));
369 buf_offset
+= sizeof(u16
);
370 ltt_relay_read(bufa
, buf_offset
, &small_size
, sizeof(u16
));
371 buf_offset
+= sizeof(u16
);
372 if (small_size
== LTT_MAX_SMALL_SIZE
) {
373 ltt_relay_read(bufa
, buf_offset
, event_size
,
375 buf_offset
+= sizeof(u32
);
377 *event_size
= small_size
;
380 *rflags
= LTT_RFLAG_ID
;
381 ltt_relay_read(bufa
, buf_offset
, eID
, sizeof(u16
));
382 buf_offset
+= sizeof(u16
);
393 /* Tracer properties */
394 #define CTF_MAGIC_NUMBER 0xC1FC1FC1
395 #define LTT_TRACER_VERSION_MAJOR 3
396 #define LTT_TRACER_VERSION_MINOR 0
399 * ltt_write_trace_header - Write trace header
400 * @priv: Private data (struct trace)
401 * @header: Memory address where the information must be written to
404 void write_trace_header(const struct lib_ring_buffer_config
*config
,
405 struct packet_header
*header
)
407 header
->magic
= CTF_MAGIC_NUMBER
;
409 /* TODO: move start time to metadata */
410 header
->major_version
= LTT_TRACER_VERSION_MAJOR
;
411 header
->minor_version
= LTT_TRACER_VERSION_MINOR
;
412 header
->arch_size
= sizeof(void *);
413 header
->alignment
= lib_ring_buffer_get_alignment(config
);
414 header
->start_time_sec
= ltt_chan
->session
->start_time
.tv_sec
;
415 header
->start_time_usec
= ltt_chan
->session
->start_time
.tv_usec
;
416 header
->start_freq
= ltt_chan
->session
->start_freq
;
417 header
->freq_scale
= ltt_chan
->session
->freq_scale
;
422 * Size reserved for high priority events (interrupts, NMI, BH) at the end of a
423 * nearly full buffer. User space won't use this last amount of space when in
424 * blocking mode. This space also includes the event header that would be
425 * written by this user space event.
427 #define LTT_RESERVE_CRITICAL 4096
429 /* Register and unregister function pointers */
431 enum ltt_module_function
{
432 LTT_FUNCTION_RUN_FILTER
,
433 LTT_FUNCTION_FILTER_CONTROL
,
434 LTT_FUNCTION_STATEDUMP
437 extern int ltt_module_register(enum ltt_module_function name
, void *function
,
438 struct module
*owner
);
439 extern void ltt_module_unregister(enum ltt_module_function name
);
441 /* Exported control function */
443 void ltt_core_register(int (*function
)(u8
, void *));
445 void ltt_core_unregister(void);
448 void ltt_statedump_register_kprobes_dump(void (*callback
)(void *call_data
));
450 void ltt_statedump_unregister_kprobes_dump(void (*callback
)(void *call_data
));
452 extern void ltt_dump_softirq_vec(void *call_data
);
454 #ifdef CONFIG_HAVE_LTT_DUMP_TABLES
455 extern void ltt_dump_sys_call_table(void *call_data
);
456 extern void ltt_dump_idt_table(void *call_data
);
458 static inline void ltt_dump_sys_call_table(void *call_data
)
462 static inline void ltt_dump_idt_table(void *call_data
)
467 #endif /* _LTT_TRACER_H */