2 * Copyright (C) 2005,2006,2008 Mathieu Desnoyers (mathieu.desnoyers@polymtl.ca)
3 * Copyright (C) 2009 Pierre-Marc Fournier
5 * This contains the definitions for the Linux Trace Toolkit tracer.
7 * Ported to userspace by Pierre-Marc Fournier.
9 * This file is released under the GPLv2.
15 #include <sys/types.h>
18 #include "kernelcompat.h"
22 #include "tracercore.h"
25 /* Number of bytes to log with a read/write event */
26 #define LTT_LOG_RW_SIZE 32L
28 /* Interval (in jiffies) at which the LTT per-CPU timer fires */
29 #define LTT_PERCPU_TIMER_INTERVAL 1
32 #define LTT_ARCH_TYPE LTT_ARCH_TYPE_UNDEFINED
35 #ifndef LTT_ARCH_VARIANT
36 #define LTT_ARCH_VARIANT LTT_ARCH_VARIANT_NONE
39 struct ltt_active_marker
;
41 /* Maximum number of callbacks per marker */
42 #define LTT_NR_CALLBACKS 10
44 struct ltt_serialize_closure
;
45 struct ltt_probe_private_data
;
47 /* Serialization callback '%k' */
48 typedef size_t (*ltt_serialize_cb
)(struct rchan_buf
*buf
, size_t buf_offset
,
49 struct ltt_serialize_closure
*closure
,
50 void *serialize_private
, int *largest_align
,
51 const char *fmt
, va_list *args
);
53 struct ltt_serialize_closure
{
54 ltt_serialize_cb
*callbacks
;
55 long cb_args
[LTT_NR_CALLBACKS
];
59 size_t ltt_serialize_data(struct rchan_buf
*buf
, size_t buf_offset
,
60 struct ltt_serialize_closure
*closure
,
61 void *serialize_private
,
62 int *largest_align
, const char *fmt
, va_list *args
);
64 struct ltt_available_probe
{
65 const char *name
; /* probe name */
67 marker_probe_func
*probe_func
;
68 ltt_serialize_cb callbacks
[LTT_NR_CALLBACKS
];
69 struct list_head node
; /* registered probes list */
72 struct ltt_probe_private_data
{
73 struct ltt_trace_struct
*trace
; /*
74 * Target trace, for metadata
77 ltt_serialize_cb serializer
; /*
78 * Serialization function override.
80 void *serialize_private
; /*
81 * Private data for serialization
91 struct ltt_active_marker
{
92 struct list_head node
; /* active markers list */
96 struct ltt_available_probe
*probe
;
99 struct marker
; //ust//
100 extern void ltt_vtrace(const struct marker
*mdata
, void *probe_data
,
101 void *call_data
, const char *fmt
, va_list *args
);
102 extern void ltt_trace(const struct marker
*mdata
, void *probe_data
,
103 void *call_data
, const char *fmt
, ...);
106 * Unique ID assigned to each registered probe.
109 MARKER_ID_SET_MARKER_ID
= 0, /* Static IDs available (range 0-7) */
110 MARKER_ID_SET_MARKER_FORMAT
,
111 MARKER_ID_COMPACT
, /* Compact IDs (range: 8-127) */
112 MARKER_ID_DYNAMIC
, /* Dynamic IDs (range: 128-65535) */
115 /* static ids 0-1 reserved for internal use. */
116 #define MARKER_CORE_IDS 2
117 static inline enum marker_id
marker_id_type(uint16_t id
)
119 if (id
< MARKER_CORE_IDS
)
120 return (enum marker_id
)id
;
122 return MARKER_ID_DYNAMIC
;
125 struct user_dbg_data
{
126 unsigned long avail_size
;
131 struct ltt_trace_ops
{
132 /* First 32 bytes cache-hot cacheline */
133 int (*reserve_slot
) (struct ltt_trace_struct
*trace
,
134 struct ltt_channel_struct
*channel
,
135 void **transport_data
, size_t data_size
,
136 size_t *slot_size
, long *buf_offset
, u64
*tsc
,
137 unsigned int *rflags
,
139 //ust// void (*commit_slot) (struct ltt_channel_struct *channel,
140 //ust// void **transport_data, long buf_offset,
141 //ust// size_t slot_size);
142 void (*wakeup_channel
) (struct ltt_channel_struct
*ltt_channel
);
143 int (*user_blocking
) (struct ltt_trace_struct
*trace
,
144 unsigned int index
, size_t data_size
,
145 struct user_dbg_data
*dbg
);
146 /* End of first 32 bytes cacheline */
147 int (*create_dirs
) (struct ltt_trace_struct
*new_trace
);
148 void (*remove_dirs
) (struct ltt_trace_struct
*new_trace
);
149 int (*create_channel
) (const char *trace_name
,
150 struct ltt_trace_struct
*trace
,
151 struct dentry
*dir
, const char *channel_name
,
152 struct ltt_channel_struct
*ltt_chan
,
153 unsigned int subbuf_size
,
154 unsigned int n_subbufs
, int overwrite
);
155 void (*finish_channel
) (struct ltt_channel_struct
*channel
);
156 void (*remove_channel
) (struct ltt_channel_struct
*channel
);
157 void (*user_errors
) (struct ltt_trace_struct
*trace
,
158 unsigned int index
, size_t data_size
,
159 struct user_dbg_data
*dbg
);
160 } ____cacheline_aligned
;
162 struct ltt_transport
{
164 struct module
*owner
;
165 struct list_head node
;
166 struct ltt_trace_ops ops
;
169 enum trace_mode
{ LTT_TRACE_NORMAL
, LTT_TRACE_FLIGHT
, LTT_TRACE_HYBRID
};
171 #define CHANNEL_FLAG_ENABLE (1U<<0)
172 #define CHANNEL_FLAG_OVERWRITE (1U<<1)
174 /* Per-trace information - each trace/flight recorder represented by one */
175 struct ltt_trace_struct
{
176 /* First 32 bytes cache-hot cacheline */
177 struct list_head list
;
178 struct ltt_trace_ops
*ops
;
180 /* Second 32 bytes cache-hot cacheline */
181 struct ltt_channel_struct
*channels
;
182 unsigned int nr_channels
;
186 unsigned long long start_monotonic
;
187 struct timeval start_time
;
188 struct ltt_channel_setting
*settings
;
190 struct dentry
*trace_root
;
192 struct kref kref
; /* Each channel has a kref of the trace struct */
193 struct ltt_transport
*transport
;
194 struct kref ltt_transport_kref
;
195 char trace_name
[NAME_MAX
];
196 } ____cacheline_aligned
;
198 /* Hardcoded event headers
200 * event header for a trace with active heartbeat : 27 bits timestamps
202 * headers are 32-bits aligned. In order to insure such alignment, a dynamic per
203 * trace alignment value must be done.
205 * Remember that the C compiler does align each member on the boundary
206 * equivalent to their own size.
208 * As relay subbuffers are aligned on pages, we are sure that they are 4 and 8
209 * bytes aligned, so the buffer header and trace header are aligned.
211 * Event headers are aligned depending on the trace alignment option.
213 * Note using C structure bitfields for cross-endianness and portability
217 #define LTT_RESERVED_EVENTS 3
218 #define LTT_EVENT_BITS 5
219 #define LTT_FREE_EVENTS ((1 << LTT_EVENT_BITS) - LTT_RESERVED_EVENTS)
220 #define LTT_TSC_BITS 27
221 #define LTT_TSC_MASK ((1 << LTT_TSC_BITS) - 1)
223 struct ltt_event_header
{
224 u32 id_time
; /* 5 bits event id (MSB); 27 bits time (LSB) */
227 /* Reservation flags */
228 #define LTT_RFLAG_ID (1 << 0)
229 #define LTT_RFLAG_ID_SIZE (1 << 1)
230 #define LTT_RFLAG_ID_SIZE_TSC (1 << 2)
233 * We use asm/timex.h : cpu_khz/HZ variable in here : we might have to deal
234 * specifically with CPU frequency scaling someday, so using an interpolation
235 * between the start and end of buffer values is not flexible enough. Using an
236 * immediate frequency value permits to calculate directly the times for parts
237 * of a buffer that would be before a frequency change.
239 * Keep the natural field alignment for _each field_ within this structure if
240 * you ever add/remove a field from this header. Packed attribute is not used
241 * because gcc generates poor code on at least powerpc and mips. Don't ever
242 * let gcc add padding between the structure elements.
244 struct ltt_subbuffer_header
{
245 uint64_t cycle_count_begin
; /* Cycle count at subbuffer start */
246 uint64_t cycle_count_end
; /* Cycle count at subbuffer end */
247 uint32_t magic_number
; /*
248 * Trace magic number.
249 * contains endianness information.
251 uint8_t major_version
;
252 uint8_t minor_version
;
253 uint8_t arch_size
; /* Architecture pointer size */
254 uint8_t alignment
; /* LTT data alignment */
255 uint64_t start_time_sec
; /* NTP-corrected start time */
256 uint64_t start_time_usec
;
257 uint64_t start_freq
; /*
258 * Frequency at trace start,
259 * used all along the trace.
261 uint32_t freq_scale
; /* Frequency scaling (divisor) */
262 uint32_t lost_size
; /* Size unused at end of subbuffer */
263 uint32_t buf_size
; /* Size of this subbuffer */
264 uint32_t events_lost
; /*
265 * Events lost in this subbuffer since
266 * the beginning of the trace.
269 uint32_t subbuf_corrupt
; /*
270 * Corrupted (lost) subbuffers since
271 * the begginig of the trace.
274 uint8_t header_end
[0]; /* End of header */
278 * ltt_subbuffer_header_size - called on buffer-switch to a new sub-buffer
280 * Return header size without padding after the structure. Don't use packed
281 * structure because gcc generates inefficient code on some architectures
284 static inline size_t ltt_subbuffer_header_size(void)
286 return offsetof(struct ltt_subbuffer_header
, header_end
);
290 * ltt_get_header_size
292 * Calculate alignment offset to 32-bits. This is the alignment offset of the
296 * The event header must be 32-bits. The total offset calculated here :
298 * Alignment of header struct on 32 bits (min arch size, header size)
299 * + sizeof(header struct) (32-bits)
300 * + (opt) u16 (ext. event id)
301 * + (opt) u16 (event_size) (if event_size == 0xFFFFUL, has ext. event size)
302 * + (opt) u32 (ext. event size)
303 * + (opt) u64 full TSC (aligned on min(64-bits, arch size))
305 * The payload must itself determine its own alignment from the biggest type it
308 static inline unsigned char ltt_get_header_size(
309 struct ltt_channel_struct
*channel
,
312 size_t *before_hdr_pad
,
315 size_t orig_offset
= offset
;
318 padding
= ltt_align(offset
, sizeof(struct ltt_event_header
));
320 offset
+= sizeof(struct ltt_event_header
);
323 case LTT_RFLAG_ID_SIZE_TSC
:
324 offset
+= sizeof(u16
) + sizeof(u16
);
325 if (data_size
>= 0xFFFFU
)
326 offset
+= sizeof(u32
);
327 offset
+= ltt_align(offset
, sizeof(u64
));
328 offset
+= sizeof(u64
);
330 case LTT_RFLAG_ID_SIZE
:
331 offset
+= sizeof(u16
) + sizeof(u16
);
332 if (data_size
>= 0xFFFFU
)
333 offset
+= sizeof(u32
);
336 offset
+= sizeof(u16
);
340 *before_hdr_pad
= padding
;
341 return offset
- orig_offset
;
345 * ltt_write_event_header
347 * Writes the event header to the offset (already aligned on 32-bits).
349 * @trace : trace to write to.
350 * @channel : pointer to the channel structure..
351 * @buf : buffer to write to.
352 * @buf_offset : buffer offset to write to (aligned on 32 bits).
354 * @event_size : size of the event, excluding the event header.
355 * @tsc : time stamp counter.
356 * @rflags : reservation flags.
358 * returns : offset where the event data must be written.
360 static inline size_t ltt_write_event_header(struct ltt_trace_struct
*trace
,
361 struct ltt_channel_struct
*channel
,
362 struct rchan_buf
*buf
, long buf_offset
,
363 u16 eID
, size_t event_size
,
364 u64 tsc
, unsigned int rflags
)
366 struct ltt_event_header header
;
370 case LTT_RFLAG_ID_SIZE_TSC
:
371 header
.id_time
= 29 << LTT_TSC_BITS
;
373 case LTT_RFLAG_ID_SIZE
:
374 header
.id_time
= 30 << LTT_TSC_BITS
;
377 header
.id_time
= 31 << LTT_TSC_BITS
;
380 header
.id_time
= eID
<< LTT_TSC_BITS
;
383 header
.id_time
|= (u32
)tsc
& LTT_TSC_MASK
;
384 ltt_relay_write(buf
, buf_offset
, &header
, sizeof(header
));
385 buf_offset
+= sizeof(header
);
388 case LTT_RFLAG_ID_SIZE_TSC
:
389 small_size
= min_t(size_t, event_size
, 0xFFFFU
);
390 ltt_relay_write(buf
, buf_offset
,
391 (u16
[]){ (u16
)eID
}, sizeof(u16
));
392 buf_offset
+= sizeof(u16
);
393 ltt_relay_write(buf
, buf_offset
,
394 (u16
[]){ (u16
)small_size
}, sizeof(u16
));
395 buf_offset
+= sizeof(u16
);
396 if (small_size
== 0xFFFFU
) {
397 ltt_relay_write(buf
, buf_offset
,
398 (u32
[]){ (u32
)event_size
}, sizeof(u32
));
399 buf_offset
+= sizeof(u32
);
401 buf_offset
+= ltt_align(buf_offset
, sizeof(u64
));
402 ltt_relay_write(buf
, buf_offset
,
403 (u64
[]){ (u64
)tsc
}, sizeof(u64
));
404 buf_offset
+= sizeof(u64
);
406 case LTT_RFLAG_ID_SIZE
:
407 small_size
= min_t(size_t, event_size
, 0xFFFFU
);
408 ltt_relay_write(buf
, buf_offset
,
409 (u16
[]){ (u16
)eID
}, sizeof(u16
));
410 buf_offset
+= sizeof(u16
);
411 ltt_relay_write(buf
, buf_offset
,
412 (u16
[]){ (u16
)small_size
}, sizeof(u16
));
413 buf_offset
+= sizeof(u16
);
414 if (small_size
== 0xFFFFU
) {
415 ltt_relay_write(buf
, buf_offset
,
416 (u32
[]){ (u32
)event_size
}, sizeof(u32
));
417 buf_offset
+= sizeof(u32
);
421 ltt_relay_write(buf
, buf_offset
,
422 (u16
[]){ (u16
)eID
}, sizeof(u16
));
423 buf_offset
+= sizeof(u16
);
437 * Atomic slot reservation in a LTTng buffer. It will take care of
438 * sub-buffer switching.
442 * @trace : the trace structure to log to.
443 * @channel : the chanel to reserve space into.
444 * @transport_data : specific transport data.
445 * @data_size : size of the variable length data to log.
446 * @slot_size : pointer to total size of the slot (out)
447 * @buf_offset : pointer to reserve offset (out)
448 * @tsc : pointer to the tsc at the slot reservation (out)
449 * @rflags : reservation flags (header specificity)
452 * Return : -ENOSPC if not enough space, else 0.
454 static inline int ltt_reserve_slot(
455 struct ltt_trace_struct
*trace
,
456 struct ltt_channel_struct
*channel
,
457 void **transport_data
,
462 unsigned int *rflags
,
465 return trace
->ops
->reserve_slot(trace
, channel
, transport_data
,
466 data_size
, slot_size
, buf_offset
, tsc
, rflags
,
474 // * Atomic unordered slot commit. Increments the commit count in the
475 // * specified sub-buffer, and delivers it if necessary.
479 // * @channel : the chanel to reserve space into.
480 // * @transport_data : specific transport data.
481 // * @buf_offset : offset of beginning of reserved slot
482 // * @slot_size : size of the reserved slot.
484 //static inline void ltt_commit_slot(
485 // struct ltt_channel_struct *channel,
486 // void **transport_data,
490 // struct ltt_trace_struct *trace = channel->trace;
492 // trace->ops->commit_slot(channel, transport_data, buf_offset, slot_size);
505 #define LTT_METADATA_CHANNEL "metadata_state"
506 #define LTT_UST_CHANNEL "ust"
508 #define LTT_FLIGHT_PREFIX "flight-"
510 /* Tracer properties */
511 //#define LTT_DEFAULT_SUBBUF_SIZE_LOW 65536
512 #define LTT_DEFAULT_SUBBUF_SIZE_LOW 4096
513 #define LTT_DEFAULT_N_SUBBUFS_LOW 2
514 //#define LTT_DEFAULT_SUBBUF_SIZE_MED 262144
515 #define LTT_DEFAULT_SUBBUF_SIZE_MED 4096
516 #define LTT_DEFAULT_N_SUBBUFS_MED 2
517 //#define LTT_DEFAULT_SUBBUF_SIZE_HIGH 1048576
518 #define LTT_DEFAULT_SUBBUF_SIZE_HIGH 4096
519 #define LTT_DEFAULT_N_SUBBUFS_HIGH 2
520 #define LTT_TRACER_MAGIC_NUMBER 0x00D6B7ED
521 #define LTT_TRACER_VERSION_MAJOR 2
522 #define LTT_TRACER_VERSION_MINOR 3
525 * Size reserved for high priority events (interrupts, NMI, BH) at the end of a
526 * nearly full buffer. User space won't use this last amount of space when in
527 * blocking mode. This space also includes the event header that would be
528 * written by this user space event.
530 #define LTT_RESERVE_CRITICAL 4096
532 /* Register and unregister function pointers */
534 enum ltt_module_function
{
535 LTT_FUNCTION_RUN_FILTER
,
536 LTT_FUNCTION_FILTER_CONTROL
,
537 LTT_FUNCTION_STATEDUMP
540 void ltt_transport_register(struct ltt_transport
*transport
);
541 void ltt_transport_unregister(struct ltt_transport
*transport
);
543 /* Exported control function */
545 union ltt_control_args
{
547 enum trace_mode mode
;
548 unsigned int subbuf_size_low
;
549 unsigned int n_subbufs_low
;
550 unsigned int subbuf_size_med
;
551 unsigned int n_subbufs_med
;
552 unsigned int subbuf_size_high
;
553 unsigned int n_subbufs_high
;
557 int _ltt_trace_setup(const char *trace_name
);
558 int ltt_trace_setup(const char *trace_name
);
559 struct ltt_trace_struct
*_ltt_trace_find_setup(const char *trace_name
);
560 int ltt_trace_set_type(const char *trace_name
, const char *trace_type
);
561 int ltt_trace_set_channel_subbufsize(const char *trace_name
,
562 const char *channel_name
, unsigned int size
);
563 int ltt_trace_set_channel_subbufcount(const char *trace_name
,
564 const char *channel_name
, unsigned int cnt
);
565 int ltt_trace_set_channel_enable(const char *trace_name
,
566 const char *channel_name
, unsigned int enable
);
567 int ltt_trace_set_channel_overwrite(const char *trace_name
,
568 const char *channel_name
, unsigned int overwrite
);
569 int ltt_trace_alloc(const char *trace_name
);
570 int ltt_trace_destroy(const char *trace_name
);
571 int ltt_trace_start(const char *trace_name
);
572 int ltt_trace_stop(const char *trace_name
);
574 enum ltt_filter_control_msg
{
575 LTT_FILTER_DEFAULT_ACCEPT
,
576 LTT_FILTER_DEFAULT_REJECT
579 extern int ltt_filter_control(enum ltt_filter_control_msg msg
,
580 const char *trace_name
);
582 extern struct dentry
*get_filter_root(void);
584 void ltt_write_trace_header(struct ltt_trace_struct
*trace
,
585 struct ltt_subbuffer_header
*header
);
586 extern void ltt_buffer_destroy(struct ltt_channel_struct
*ltt_chan
);
588 void ltt_core_register(int (*function
)(u8
, void *));
590 void ltt_core_unregister(void);
592 void ltt_release_trace(struct kref
*kref
);
593 void ltt_release_transport(struct kref
*kref
);
595 extern int ltt_probe_register(struct ltt_available_probe
*pdata
);
596 extern int ltt_probe_unregister(struct ltt_available_probe
*pdata
);
597 extern int ltt_marker_connect(const char *channel
, const char *mname
,
599 extern int ltt_marker_disconnect(const char *channel
, const char *mname
,
601 extern void ltt_dump_marker_state(struct ltt_trace_struct
*trace
);
603 void ltt_lock_traces(void);
604 void ltt_unlock_traces(void);
606 struct ltt_trace_struct
*_ltt_trace_find(const char *trace_name
);
608 #endif /* _LTT_TRACER_H */