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 * Hardcoded event headers
78 * event header for a trace with active heartbeat : 27 bits timestamps
80 * headers are 32-bits aligned. In order to insure such alignment, a dynamic per
81 * trace alignment value must be done.
83 * Remember that the C compiler does align each member on the boundary
84 * equivalent to their own size.
86 * As relay subbuffers are aligned on pages, we are sure that they are 4 and 8
87 * bytes aligned, so the buffer header and trace header are aligned.
89 * Event headers are aligned depending on the trace alignment option.
91 * Note using C structure bitfields for cross-endianness and portability
95 #define LTT_RESERVED_EVENTS 3
96 #define LTT_EVENT_BITS 5
97 #define LTT_FREE_EVENTS ((1 << LTT_EVENT_BITS) - LTT_RESERVED_EVENTS)
98 #define LTT_TSC_BITS 27
99 #define LTT_TSC_MASK ((1 << LTT_TSC_BITS) - 1)
101 struct event_header
{
102 u32 id_time
; /* 5 bits event id (MSB); 27 bits time (LSB) */
105 /* Reservation flags */
106 #define LTT_RFLAG_ID (1 << 0)
107 #define LTT_RFLAG_ID_SIZE (1 << 1)
108 #define LTT_RFLAG_ID_SIZE_TSC (1 << 2)
110 #define LTT_MAX_SMALL_SIZE 0xFFFFU
113 * We use asm/timex.h : cpu_khz/HZ variable in here : we might have to deal
114 * specifically with CPU frequency scaling someday, so using an interpolation
115 * between the start and end of buffer values is not flexible enough. Using an
116 * immediate frequency value permits to calculate directly the times for parts
117 * of a buffer that would be before a frequency change.
119 * Keep the natural field alignment for _each field_ within this structure if
120 * you ever add/remove a field from this header. Packed attribute is not used
121 * because gcc generates poor code on at least powerpc and mips. Don't ever
122 * let gcc add padding between the structure elements.
124 struct packet_header
{
126 * Trace magic number.
127 * contains endianness information.
129 uint8_t trace_uuid
[16];
131 uint64_t timestamp_begin
; /* Cycle count at subbuffer start */
132 uint64_t timestamp_end
; /* Cycle count at subbuffer end */
133 uint32_t content_size
; /* Size of data in subbuffer */
134 uint32_t packet_size
; /* Subbuffer size (include padding) */
135 uint32_t events_lost
; /*
136 * Events lost in this subbuffer since
137 * the beginning of the trace.
140 /* TODO: move to metadata */
142 uint8_t major_version
;
143 uint8_t minor_version
;
144 uint8_t arch_size
; /* Architecture pointer size */
145 uint8_t alignment
; /* LTT data alignment */
146 uint64_t start_time_sec
; /* NTP-corrected start time */
147 uint64_t start_time_usec
;
148 uint64_t start_freq
; /*
149 * Frequency at trace start,
150 * used all along the trace.
152 uint32_t freq_scale
; /* Frequency scaling (divisor) */
154 uint8_t header_end
[0]; /* End of header */
157 static inline notrace u64
lib_ring_buffer_clock_read(struct channel
*chan
)
159 return trace_clock_read64();
163 * record_header_size - Calculate the header size and padding necessary.
164 * @config: ring buffer instance configuration
166 * @offset: offset in the write buffer
167 * @data_size: size of the payload
168 * @pre_header_padding: padding to add before the header (output)
169 * @rflags: reservation flags
170 * @ctx: reservation context
172 * Returns the event header size (including padding).
175 * The event header must be 32-bits. The total offset calculated here :
177 * Alignment of header struct on 32 bits (min arch size, header size)
178 * + sizeof(header struct) (32-bits)
179 * + (opt) u16 (ext. event id)
180 * + (opt) u16 (event_size)
181 * (if event_size == LTT_MAX_SMALL_SIZE, has ext. event size)
182 * + (opt) u32 (ext. event size)
183 * + (opt) u64 full TSC (aligned on min(64-bits, arch size))
185 * The payload must itself determine its own alignment from the biggest type it
189 unsigned char record_header_size(const struct lib_ring_buffer_config
*config
,
190 struct channel
*chan
, size_t offset
,
191 size_t data_size
, size_t *pre_header_padding
,
193 struct lib_ring_buffer_ctx
*ctx
)
195 size_t orig_offset
= offset
;
198 BUILD_BUG_ON(sizeof(struct event_header
) != sizeof(u32
));
200 padding
= lib_ring_buffer_align(offset
,
201 sizeof(struct event_header
));
203 offset
+= sizeof(struct event_header
);
205 if (unlikely(rflags
)) {
207 case LTT_RFLAG_ID_SIZE_TSC
:
208 offset
+= sizeof(u16
) + sizeof(u16
);
209 if (data_size
>= LTT_MAX_SMALL_SIZE
)
210 offset
+= sizeof(u32
);
211 offset
+= lib_ring_buffer_align(offset
, sizeof(u64
));
212 offset
+= sizeof(u64
);
214 case LTT_RFLAG_ID_SIZE
:
215 offset
+= sizeof(u16
) + sizeof(u16
);
216 if (data_size
>= LTT_MAX_SMALL_SIZE
)
217 offset
+= sizeof(u32
);
220 offset
+= sizeof(u16
);
225 *pre_header_padding
= padding
;
226 return offset
- orig_offset
;
229 #include <linux/ringbuffer/api.h>
232 void ltt_write_event_header_slow(const struct lib_ring_buffer_config
*config
,
233 struct lib_ring_buffer_ctx
*ctx
,
234 u16 eID
, u32 event_size
);
237 * ltt_write_event_header
239 * Writes the event header to the offset (already aligned on 32-bits).
241 * @config: ring buffer instance configuration
242 * @ctx: reservation context
244 * @event_size : size of the event, excluding the event header.
247 void ltt_write_event_header(const struct lib_ring_buffer_config
*config
,
248 struct lib_ring_buffer_ctx
*ctx
,
249 u16 eID
, u32 event_size
)
251 struct event_header header
;
253 if (unlikely(ctx
->rflags
))
256 header
.id_time
= eID
<< LTT_TSC_BITS
;
257 header
.id_time
|= (u32
)ctx
->tsc
& LTT_TSC_MASK
;
258 lib_ring_buffer_write(config
, ctx
, &header
, sizeof(header
));
261 ltt_write_event_header_slow(config
, ctx
, eID
, event_size
);
264 /* Tracer properties */
265 #define CTF_MAGIC_NUMBER 0xC1FC1FC1
266 #define LTT_TRACER_VERSION_MAJOR 3
267 #define LTT_TRACER_VERSION_MINOR 0
270 * ltt_write_trace_header - Write trace header
271 * @priv: Private data (struct trace)
272 * @header: Memory address where the information must be written to
275 void write_trace_header(const struct lib_ring_buffer_config
*config
,
276 struct packet_header
*header
)
278 header
->magic
= CTF_MAGIC_NUMBER
;
280 /* TODO: move start time to metadata */
281 header
->major_version
= LTT_TRACER_VERSION_MAJOR
;
282 header
->minor_version
= LTT_TRACER_VERSION_MINOR
;
283 header
->arch_size
= sizeof(void *);
284 header
->alignment
= lib_ring_buffer_get_alignment(config
);
285 header
->start_time_sec
= ltt_chan
->session
->start_time
.tv_sec
;
286 header
->start_time_usec
= ltt_chan
->session
->start_time
.tv_usec
;
287 header
->start_freq
= ltt_chan
->session
->start_freq
;
288 header
->freq_scale
= ltt_chan
->session
->freq_scale
;
293 * Size reserved for high priority events (interrupts, NMI, BH) at the end of a
294 * nearly full buffer. User space won't use this last amount of space when in
295 * blocking mode. This space also includes the event header that would be
296 * written by this user space event.
298 #define LTT_RESERVE_CRITICAL 4096
300 /* Register and unregister function pointers */
302 enum ltt_module_function
{
303 LTT_FUNCTION_RUN_FILTER
,
304 LTT_FUNCTION_FILTER_CONTROL
,
305 LTT_FUNCTION_STATEDUMP
308 extern int ltt_module_register(enum ltt_module_function name
, void *function
,
309 struct module
*owner
);
310 extern void ltt_module_unregister(enum ltt_module_function name
);
312 /* Exported control function */
314 void ltt_core_register(int (*function
)(u8
, void *));
316 void ltt_core_unregister(void);
319 void ltt_statedump_register_kprobes_dump(void (*callback
)(void *call_data
));
321 void ltt_statedump_unregister_kprobes_dump(void (*callback
)(void *call_data
));
323 extern void ltt_dump_softirq_vec(void *call_data
);
325 #ifdef CONFIG_HAVE_LTT_DUMP_TABLES
326 extern void ltt_dump_sys_call_table(void *call_data
);
327 extern void ltt_dump_idt_table(void *call_data
);
329 static inline void ltt_dump_sys_call_table(void *call_data
)
333 static inline void ltt_dump_idt_table(void *call_data
)
338 #endif /* _LTT_TRACER_H */