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.
12 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
13 /* Align data on its natural alignment */
14 #define RING_BUFFER_ALIGN
17 #include <linux/ringbuffer/config.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/trace-clock.h>
28 #include <asm/atomic.h>
29 #include <asm/local.h>
31 #include "ltt-tracer-core.h"
32 #include "ltt-events.h"
34 /* Number of bytes to log with a read/write event */
35 #define LTT_LOG_RW_SIZE 32L
37 /* Maximum number of callbacks per marker */
38 #define LTT_NR_CALLBACKS 10
40 struct ltt_serialize_closure
;
42 /* Serialization callback */
43 typedef size_t (*ltt_serialize_cb
)(struct lib_ring_buffer
*buf
,
45 struct ltt_serialize_closure
*closure
,
46 void *serialize_private
,
47 unsigned int stack_pos_ctx
,
49 const char *fmt
, va_list *args
);
51 struct ltt_serialize_closure
{
52 ltt_serialize_cb
*callbacks
;
53 long cb_args
[LTT_NR_CALLBACKS
];
57 size_t ltt_serialize_data(struct lib_ring_buffer
*buf
, size_t buf_offset
,
58 struct ltt_serialize_closure
*closure
,
59 void *serialize_private
, unsigned int stack_pos_ctx
,
60 int *largest_align
, const char *fmt
, va_list *args
);
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
,
84 size_t ltt_serialize_printf(struct lib_ring_buffer
*buf
, unsigned long buf_offset
,
85 size_t *msg_size
, char *output
, size_t outlen
,
89 * Unique ID assigned to each registered probe.
92 MARKER_ID_SET_MARKER_ID
= 0, /* Static IDs available (range 0-7) */
93 MARKER_ID_SET_MARKER_FORMAT
,
94 MARKER_ID_COMPACT
, /* Compact IDs (range: 8-127) */
95 MARKER_ID_DYNAMIC
, /* Dynamic IDs (range: 128-65535) */
98 /* static ids 0-1 reserved for internal use. */
99 #define MARKER_CORE_IDS 2
100 static __inline__
enum marker_id
marker_id_type(uint16_t id
)
102 if (id
< MARKER_CORE_IDS
)
103 return (enum marker_id
)id
;
105 return MARKER_ID_DYNAMIC
;
108 struct user_dbg_data
{
109 unsigned long avail_size
;
114 enum trace_mode
{ LTT_TRACE_NORMAL
, LTT_TRACE_FLIGHT
, LTT_TRACE_HYBRID
};
116 #define CHANNEL_FLAG_ENABLE (1U<<0)
117 #define CHANNEL_FLAG_OVERWRITE (1U<<1)
121 /* Per-trace information - each trace/flight recorder represented by one */
123 /* First 32 bytes cache-hot cacheline */
124 struct list_head list
;
125 struct ltt_chan
**channels
;
126 unsigned int nr_channels
;
128 /* Second 32 bytes cache-hot cacheline */
129 struct ltt_trace_ops
*ops
;
133 unsigned long long start_monotonic
;
134 struct timeval start_time
;
135 struct ltt_channel_setting
*settings
;
137 struct dentry
*trace_root
;
138 struct dentry
*ascii_root
;
140 struct kref kref
; /* Each channel has a kref of the trace struct */
141 struct ltt_transport
*transport
;
142 struct kref ltt_transport_kref
;
143 wait_queue_head_t kref_wq
; /* Place for ltt_trace_destroy to sleep */
144 char trace_name
[NAME_MAX
];
145 } ____cacheline_aligned
;
149 * Hardcoded event headers
151 * event header for a trace with active heartbeat : 27 bits timestamps
153 * headers are 32-bits aligned. In order to insure such alignment, a dynamic per
154 * trace alignment value must be done.
156 * Remember that the C compiler does align each member on the boundary
157 * equivalent to their own size.
159 * As relay subbuffers are aligned on pages, we are sure that they are 4 and 8
160 * bytes aligned, so the buffer header and trace header are aligned.
162 * Event headers are aligned depending on the trace alignment option.
164 * Note using C structure bitfields for cross-endianness and portability
168 #define LTT_RESERVED_EVENTS 3
169 #define LTT_EVENT_BITS 5
170 #define LTT_FREE_EVENTS ((1 << LTT_EVENT_BITS) - LTT_RESERVED_EVENTS)
171 #define LTT_TSC_BITS 27
172 #define LTT_TSC_MASK ((1 << LTT_TSC_BITS) - 1)
174 struct event_header
{
175 u32 id_time
; /* 5 bits event id (MSB); 27 bits time (LSB) */
178 /* Reservation flags */
179 #define LTT_RFLAG_ID (1 << 0)
180 #define LTT_RFLAG_ID_SIZE (1 << 1)
181 #define LTT_RFLAG_ID_SIZE_TSC (1 << 2)
183 #define LTT_MAX_SMALL_SIZE 0xFFFFU
186 * We use asm/timex.h : cpu_khz/HZ variable in here : we might have to deal
187 * specifically with CPU frequency scaling someday, so using an interpolation
188 * between the start and end of buffer values is not flexible enough. Using an
189 * immediate frequency value permits to calculate directly the times for parts
190 * of a buffer that would be before a frequency change.
192 * Keep the natural field alignment for _each field_ within this structure if
193 * you ever add/remove a field from this header. Packed attribute is not used
194 * because gcc generates poor code on at least powerpc and mips. Don't ever
195 * let gcc add padding between the structure elements.
197 struct packet_header
{
199 * Trace magic number.
200 * contains endianness information.
202 uint8_t trace_uuid
[16];
204 uint64_t timestamp_begin
; /* Cycle count at subbuffer start */
205 uint64_t timestamp_end
; /* Cycle count at subbuffer end */
206 uint32_t content_size
; /* Size of data in subbuffer */
207 uint32_t packet_size
; /* Subbuffer size (include padding) */
208 uint32_t events_lost
; /*
209 * Events lost in this subbuffer since
210 * the beginning of the trace.
213 /* TODO: move to metadata */
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.
225 uint32_t freq_scale
; /* Frequency scaling (divisor) */
227 uint8_t header_end
[0]; /* End of header */
230 static inline notrace u64
lib_ring_buffer_clock_read(struct channel
*chan
)
232 return trace_clock_read64();
236 * record_header_size - Calculate the header size and padding necessary.
237 * @config: ring buffer instance configuration
239 * @offset: offset in the write buffer
240 * @data_size: size of the payload
241 * @pre_header_padding: padding to add before the header (output)
242 * @rflags: reservation flags
243 * @ctx: reservation context
245 * Returns the event header size (including padding).
248 * The event header must be 32-bits. The total offset calculated here :
250 * Alignment of header struct on 32 bits (min arch size, header size)
251 * + sizeof(header struct) (32-bits)
252 * + (opt) u16 (ext. event id)
253 * + (opt) u16 (event_size)
254 * (if event_size == LTT_MAX_SMALL_SIZE, has ext. event size)
255 * + (opt) u32 (ext. event size)
256 * + (opt) u64 full TSC (aligned on min(64-bits, arch size))
258 * The payload must itself determine its own alignment from the biggest type it
262 unsigned char record_header_size(const struct lib_ring_buffer_config
*config
,
263 struct channel
*chan
, size_t offset
,
264 size_t data_size
, size_t *pre_header_padding
,
266 struct lib_ring_buffer_ctx
*ctx
)
268 size_t orig_offset
= offset
;
271 BUILD_BUG_ON(sizeof(struct event_header
) != sizeof(u32
));
273 padding
= lib_ring_buffer_align(config
, offset
,
274 sizeof(struct event_header
));
276 offset
+= sizeof(struct event_header
);
278 if (unlikely(rflags
)) {
280 case LTT_RFLAG_ID_SIZE_TSC
:
281 offset
+= sizeof(u16
) + sizeof(u16
);
282 if (data_size
>= LTT_MAX_SMALL_SIZE
)
283 offset
+= sizeof(u32
);
284 offset
+= ltt_align(offset
, sizeof(u64
));
285 offset
+= sizeof(u64
);
287 case LTT_RFLAG_ID_SIZE
:
288 offset
+= sizeof(u16
) + sizeof(u16
);
289 if (data_size
>= LTT_MAX_SMALL_SIZE
)
290 offset
+= sizeof(u32
);
293 offset
+= sizeof(u16
);
298 *pre_header_padding
= padding
;
299 return offset
- orig_offset
;
302 #include <linux/ringbuffer/api.h>
305 void ltt_write_event_header_slow(const struct lib_ring_buffer_config
*config
,
306 struct lib_ring_buffer_ctx
*ctx
,
307 u16 eID
, u32 event_size
);
310 * ltt_write_event_header
312 * Writes the event header to the offset (already aligned on 32-bits).
314 * @config: ring buffer instance configuration
315 * @ctx: reservation context
317 * @event_size : size of the event, excluding the event header.
320 void ltt_write_event_header(const struct lib_ring_buffer_config
*config
,
321 struct lib_ring_buffer_ctx
*ctx
,
322 u16 eID
, u32 event_size
)
324 struct event_header header
;
326 if (unlikely(ctx
->rflags
))
329 header
.id_time
= eID
<< LTT_TSC_BITS
;
330 header
.id_time
|= (u32
)ctx
->tsc
& LTT_TSC_MASK
;
331 lib_ring_buffer_write(config
, ctx
, &header
, sizeof(header
));
334 ltt_write_event_header_slow(config
, ctx
, eID
, event_size
);
339 * ltt_read_event_header
340 * buf_offset must aligned on 32 bits
343 size_t ltt_read_event_header(struct ltt_chanbuf_alloc
*bufa
, long buf_offset
,
344 u64
*tsc
, u32
*event_size
, u16
*eID
,
345 unsigned int *rflags
)
347 struct ltt_event_header header
;
350 ltt_relay_read(bufa
, buf_offset
, &header
, sizeof(header
));
351 buf_offset
+= sizeof(header
);
353 *event_size
= INT_MAX
;
354 *eID
= header
.id_time
>> LTT_TSC_BITS
;
355 *tsc
= header
.id_time
& LTT_TSC_MASK
;
359 *rflags
= LTT_RFLAG_ID_SIZE_TSC
;
360 ltt_relay_read(bufa
, buf_offset
, eID
, sizeof(u16
));
361 buf_offset
+= sizeof(u16
);
362 ltt_relay_read(bufa
, buf_offset
, &small_size
, sizeof(u16
));
363 buf_offset
+= sizeof(u16
);
364 if (small_size
== LTT_MAX_SMALL_SIZE
) {
365 ltt_relay_read(bufa
, buf_offset
, event_size
,
367 buf_offset
+= sizeof(u32
);
369 *event_size
= small_size
;
370 buf_offset
+= ltt_align(buf_offset
, sizeof(u64
));
371 ltt_relay_read(bufa
, buf_offset
, tsc
, sizeof(u64
));
372 buf_offset
+= sizeof(u64
);
375 *rflags
= LTT_RFLAG_ID_SIZE
;
376 ltt_relay_read(bufa
, buf_offset
, eID
, sizeof(u16
));
377 buf_offset
+= sizeof(u16
);
378 ltt_relay_read(bufa
, buf_offset
, &small_size
, sizeof(u16
));
379 buf_offset
+= sizeof(u16
);
380 if (small_size
== LTT_MAX_SMALL_SIZE
) {
381 ltt_relay_read(bufa
, buf_offset
, event_size
,
383 buf_offset
+= sizeof(u32
);
385 *event_size
= small_size
;
388 *rflags
= LTT_RFLAG_ID
;
389 ltt_relay_read(bufa
, buf_offset
, eID
, sizeof(u16
));
390 buf_offset
+= sizeof(u16
);
401 /* Tracer properties */
402 #define CTF_MAGIC_NUMBER 0xC1FC1FC1
403 #define LTT_TRACER_VERSION_MAJOR 3
404 #define LTT_TRACER_VERSION_MINOR 0
407 * ltt_write_trace_header - Write trace header
408 * @priv: Private data (struct trace)
409 * @header: Memory address where the information must be written to
412 void write_trace_header(const struct lib_ring_buffer_config
*config
,
413 struct packet_header
*header
)
415 header
->magic
= CTF_MAGIC_NUMBER
;
417 /* TODO: move start time to metadata */
418 header
->major_version
= LTT_TRACER_VERSION_MAJOR
;
419 header
->minor_version
= LTT_TRACER_VERSION_MINOR
;
420 header
->arch_size
= sizeof(void *);
421 header
->alignment
= lib_ring_buffer_get_alignment(config
);
422 header
->start_time_sec
= ltt_chan
->session
->start_time
.tv_sec
;
423 header
->start_time_usec
= ltt_chan
->session
->start_time
.tv_usec
;
424 header
->start_freq
= ltt_chan
->session
->start_freq
;
425 header
->freq_scale
= ltt_chan
->session
->freq_scale
;
430 * Size reserved for high priority events (interrupts, NMI, BH) at the end of a
431 * nearly full buffer. User space won't use this last amount of space when in
432 * blocking mode. This space also includes the event header that would be
433 * written by this user space event.
435 #define LTT_RESERVE_CRITICAL 4096
437 /* Register and unregister function pointers */
439 enum ltt_module_function
{
440 LTT_FUNCTION_RUN_FILTER
,
441 LTT_FUNCTION_FILTER_CONTROL
,
442 LTT_FUNCTION_STATEDUMP
445 extern int ltt_module_register(enum ltt_module_function name
, void *function
,
446 struct module
*owner
);
447 extern void ltt_module_unregister(enum ltt_module_function name
);
449 /* Exported control function */
451 void ltt_core_register(int (*function
)(u8
, void *));
453 void ltt_core_unregister(void);
456 void ltt_statedump_register_kprobes_dump(void (*callback
)(void *call_data
));
458 void ltt_statedump_unregister_kprobes_dump(void (*callback
)(void *call_data
));
460 extern void ltt_dump_softirq_vec(void *call_data
);
462 #ifdef CONFIG_HAVE_LTT_DUMP_TABLES
463 extern void ltt_dump_sys_call_table(void *call_data
);
464 extern void ltt_dump_idt_table(void *call_data
);
466 static inline void ltt_dump_sys_call_table(void *call_data
)
470 static inline void ltt_dump_idt_table(void *call_data
)
475 #endif /* _LTT_TRACER_H */