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>
19 #include "kernelcompat.h"
21 #include "tracercore.h"
24 /* Number of bytes to log with a read/write event */
25 #define LTT_LOG_RW_SIZE 32L
27 /* Interval (in jiffies) at which the LTT per-CPU timer fires */
28 #define LTT_PERCPU_TIMER_INTERVAL 1
31 #define LTT_ARCH_TYPE LTT_ARCH_TYPE_UNDEFINED
34 #ifndef LTT_ARCH_VARIANT
35 #define LTT_ARCH_VARIANT LTT_ARCH_VARIANT_NONE
38 struct ltt_active_marker
;
40 /* Maximum number of callbacks per marker */
41 #define LTT_NR_CALLBACKS 10
43 struct ltt_serialize_closure
;
44 struct ltt_probe_private_data
;
46 /* Serialization callback '%k' */
47 typedef size_t (*ltt_serialize_cb
)(struct rchan_buf
*buf
, size_t buf_offset
,
48 struct ltt_serialize_closure
*closure
,
49 void *serialize_private
, int *largest_align
,
50 const char *fmt
, va_list *args
);
52 struct ltt_serialize_closure
{
53 ltt_serialize_cb
*callbacks
;
54 long cb_args
[LTT_NR_CALLBACKS
];
58 size_t ltt_serialize_data(struct rchan_buf
*buf
, size_t buf_offset
,
59 struct ltt_serialize_closure
*closure
,
60 void *serialize_private
,
61 int *largest_align
, const char *fmt
, va_list *args
);
63 struct ltt_available_probe
{
64 const char *name
; /* probe name */
66 marker_probe_func
*probe_func
;
67 ltt_serialize_cb callbacks
[LTT_NR_CALLBACKS
];
68 struct list_head node
; /* registered probes list */
71 struct ltt_probe_private_data
{
72 struct ltt_trace_struct
*trace
; /*
73 * Target trace, for metadata
76 ltt_serialize_cb serializer
; /*
77 * Serialization function override.
79 void *serialize_private
; /*
80 * Private data for serialization
90 struct ltt_active_marker
{
91 struct list_head node
; /* active markers list */
95 struct ltt_available_probe
*probe
;
98 struct marker
; //ust//
99 extern void ltt_vtrace(const struct marker
*mdata
, void *probe_data
,
100 void *call_data
, const char *fmt
, va_list *args
);
101 extern void ltt_trace(const struct marker
*mdata
, void *probe_data
,
102 void *call_data
, const char *fmt
, ...);
105 * Unique ID assigned to each registered probe.
108 MARKER_ID_SET_MARKER_ID
= 0, /* Static IDs available (range 0-7) */
109 MARKER_ID_SET_MARKER_FORMAT
,
110 MARKER_ID_COMPACT
, /* Compact IDs (range: 8-127) */
111 MARKER_ID_DYNAMIC
, /* Dynamic IDs (range: 128-65535) */
114 /* static ids 0-1 reserved for internal use. */
115 #define MARKER_CORE_IDS 2
116 static inline enum marker_id
marker_id_type(uint16_t id
)
118 if (id
< MARKER_CORE_IDS
)
119 return (enum marker_id
)id
;
121 return MARKER_ID_DYNAMIC
;
124 struct user_dbg_data
{
125 unsigned long avail_size
;
130 struct ltt_trace_ops
{
131 /* First 32 bytes cache-hot cacheline */
132 int (*reserve_slot
) (struct ltt_trace_struct
*trace
,
133 struct ltt_channel_struct
*channel
,
134 void **transport_data
, size_t data_size
,
135 size_t *slot_size
, long *buf_offset
, u64
*tsc
,
136 unsigned int *rflags
,
138 void (*commit_slot
) (struct ltt_channel_struct
*channel
,
139 void **transport_data
, long buf_offset
,
141 void (*wakeup_channel
) (struct ltt_channel_struct
*ltt_channel
);
142 int (*user_blocking
) (struct ltt_trace_struct
*trace
,
143 unsigned int index
, size_t data_size
,
144 struct user_dbg_data
*dbg
);
145 /* End of first 32 bytes cacheline */
146 int (*create_dirs
) (struct ltt_trace_struct
*new_trace
);
147 void (*remove_dirs
) (struct ltt_trace_struct
*new_trace
);
148 int (*create_channel
) (const char *trace_name
,
149 struct ltt_trace_struct
*trace
,
150 struct dentry
*dir
, const char *channel_name
,
151 struct ltt_channel_struct
*ltt_chan
,
152 unsigned int subbuf_size
,
153 unsigned int n_subbufs
, int overwrite
);
154 void (*finish_channel
) (struct ltt_channel_struct
*channel
);
155 void (*remove_channel
) (struct ltt_channel_struct
*channel
);
156 void (*user_errors
) (struct ltt_trace_struct
*trace
,
157 unsigned int index
, size_t data_size
,
158 struct user_dbg_data
*dbg
);
159 } ____cacheline_aligned
;
161 struct ltt_transport
{
163 struct module
*owner
;
164 struct list_head node
;
165 struct ltt_trace_ops ops
;
168 enum trace_mode
{ LTT_TRACE_NORMAL
, LTT_TRACE_FLIGHT
, LTT_TRACE_HYBRID
};
170 #define CHANNEL_FLAG_ENABLE (1U<<0)
171 #define CHANNEL_FLAG_OVERWRITE (1U<<1)
173 /* Per-trace information - each trace/flight recorder represented by one */
174 struct ltt_trace_struct
{
175 /* First 32 bytes cache-hot cacheline */
176 struct list_head list
;
177 struct ltt_trace_ops
*ops
;
179 /* Second 32 bytes cache-hot cacheline */
180 struct ltt_channel_struct
*channels
;
181 unsigned int nr_channels
;
185 unsigned long long start_monotonic
;
186 struct timeval start_time
;
187 struct ltt_channel_setting
*settings
;
189 struct dentry
*trace_root
;
191 struct kref kref
; /* Each channel has a kref of the trace struct */
192 struct ltt_transport
*transport
;
193 struct kref ltt_transport_kref
;
194 char trace_name
[NAME_MAX
];
195 } ____cacheline_aligned
;
197 /* Hardcoded event headers
199 * event header for a trace with active heartbeat : 27 bits timestamps
201 * headers are 32-bits aligned. In order to insure such alignment, a dynamic per
202 * trace alignment value must be done.
204 * Remember that the C compiler does align each member on the boundary
205 * equivalent to their own size.
207 * As relay subbuffers are aligned on pages, we are sure that they are 4 and 8
208 * bytes aligned, so the buffer header and trace header are aligned.
210 * Event headers are aligned depending on the trace alignment option.
212 * Note using C structure bitfields for cross-endianness and portability
216 #define LTT_RESERVED_EVENTS 3
217 #define LTT_EVENT_BITS 5
218 #define LTT_FREE_EVENTS ((1 << LTT_EVENT_BITS) - LTT_RESERVED_EVENTS)
219 #define LTT_TSC_BITS 27
220 #define LTT_TSC_MASK ((1 << LTT_TSC_BITS) - 1)
222 struct ltt_event_header
{
223 u32 id_time
; /* 5 bits event id (MSB); 27 bits time (LSB) */
226 /* Reservation flags */
227 #define LTT_RFLAG_ID (1 << 0)
228 #define LTT_RFLAG_ID_SIZE (1 << 1)
229 #define LTT_RFLAG_ID_SIZE_TSC (1 << 2)
232 * We use asm/timex.h : cpu_khz/HZ variable in here : we might have to deal
233 * specifically with CPU frequency scaling someday, so using an interpolation
234 * between the start and end of buffer values is not flexible enough. Using an
235 * immediate frequency value permits to calculate directly the times for parts
236 * of a buffer that would be before a frequency change.
238 * Keep the natural field alignment for _each field_ within this structure if
239 * you ever add/remove a field from this header. Packed attribute is not used
240 * because gcc generates poor code on at least powerpc and mips. Don't ever
241 * let gcc add padding between the structure elements.
243 struct ltt_subbuffer_header
{
244 uint64_t cycle_count_begin
; /* Cycle count at subbuffer start */
245 uint64_t cycle_count_end
; /* Cycle count at subbuffer end */
246 uint32_t magic_number
; /*
247 * Trace magic number.
248 * contains endianness information.
250 uint8_t major_version
;
251 uint8_t minor_version
;
252 uint8_t arch_size
; /* Architecture pointer size */
253 uint8_t alignment
; /* LTT data alignment */
254 uint64_t start_time_sec
; /* NTP-corrected start time */
255 uint64_t start_time_usec
;
256 uint64_t start_freq
; /*
257 * Frequency at trace start,
258 * used all along the trace.
260 uint32_t freq_scale
; /* Frequency scaling (divisor) */
261 uint32_t lost_size
; /* Size unused at end of subbuffer */
262 uint32_t buf_size
; /* Size of this subbuffer */
263 uint32_t events_lost
; /*
264 * Events lost in this subbuffer since
265 * the beginning of the trace.
268 uint32_t subbuf_corrupt
; /*
269 * Corrupted (lost) subbuffers since
270 * the begginig of the trace.
273 uint8_t header_end
[0]; /* End of header */
277 * ltt_subbuffer_header_size - called on buffer-switch to a new sub-buffer
279 * Return header size without padding after the structure. Don't use packed
280 * structure because gcc generates inefficient code on some architectures
283 static inline size_t ltt_subbuffer_header_size(void)
285 return offsetof(struct ltt_subbuffer_header
, header_end
);
289 * ltt_get_header_size
291 * Calculate alignment offset to 32-bits. This is the alignment offset of the
295 * The event header must be 32-bits. The total offset calculated here :
297 * Alignment of header struct on 32 bits (min arch size, header size)
298 * + sizeof(header struct) (32-bits)
299 * + (opt) u16 (ext. event id)
300 * + (opt) u16 (event_size) (if event_size == 0xFFFFUL, has ext. event size)
301 * + (opt) u32 (ext. event size)
302 * + (opt) u64 full TSC (aligned on min(64-bits, arch size))
304 * The payload must itself determine its own alignment from the biggest type it
307 static inline unsigned char ltt_get_header_size(
308 struct ltt_channel_struct
*channel
,
311 size_t *before_hdr_pad
,
314 size_t orig_offset
= offset
;
317 padding
= ltt_align(offset
, sizeof(struct ltt_event_header
));
319 offset
+= sizeof(struct ltt_event_header
);
322 case LTT_RFLAG_ID_SIZE_TSC
:
323 offset
+= sizeof(u16
) + sizeof(u16
);
324 if (data_size
>= 0xFFFFU
)
325 offset
+= sizeof(u32
);
326 offset
+= ltt_align(offset
, sizeof(u64
));
327 offset
+= sizeof(u64
);
329 case LTT_RFLAG_ID_SIZE
:
330 offset
+= sizeof(u16
) + sizeof(u16
);
331 if (data_size
>= 0xFFFFU
)
332 offset
+= sizeof(u32
);
335 offset
+= sizeof(u16
);
339 *before_hdr_pad
= padding
;
340 return offset
- orig_offset
;
344 * ltt_write_event_header
346 * Writes the event header to the offset (already aligned on 32-bits).
348 * @trace : trace to write to.
349 * @channel : pointer to the channel structure..
350 * @buf : buffer to write to.
351 * @buf_offset : buffer offset to write to (aligned on 32 bits).
353 * @event_size : size of the event, excluding the event header.
354 * @tsc : time stamp counter.
355 * @rflags : reservation flags.
357 * returns : offset where the event data must be written.
359 static inline size_t ltt_write_event_header(struct ltt_trace_struct
*trace
,
360 struct ltt_channel_struct
*channel
,
361 struct rchan_buf
*buf
, long buf_offset
,
362 u16 eID
, size_t event_size
,
363 u64 tsc
, unsigned int rflags
)
365 struct ltt_event_header header
;
369 case LTT_RFLAG_ID_SIZE_TSC
:
370 header
.id_time
= 29 << LTT_TSC_BITS
;
372 case LTT_RFLAG_ID_SIZE
:
373 header
.id_time
= 30 << LTT_TSC_BITS
;
376 header
.id_time
= 31 << LTT_TSC_BITS
;
379 header
.id_time
= eID
<< LTT_TSC_BITS
;
382 header
.id_time
|= (u32
)tsc
& LTT_TSC_MASK
;
383 ltt_relay_write(buf
, buf_offset
, &header
, sizeof(header
));
384 buf_offset
+= sizeof(header
);
387 case LTT_RFLAG_ID_SIZE_TSC
:
388 small_size
= min_t(size_t, event_size
, 0xFFFFU
);
389 ltt_relay_write(buf
, buf_offset
,
390 (u16
[]){ (u16
)eID
}, sizeof(u16
));
391 buf_offset
+= sizeof(u16
);
392 ltt_relay_write(buf
, buf_offset
,
393 (u16
[]){ (u16
)small_size
}, sizeof(u16
));
394 buf_offset
+= sizeof(u16
);
395 if (small_size
== 0xFFFFU
) {
396 ltt_relay_write(buf
, buf_offset
,
397 (u32
[]){ (u32
)event_size
}, sizeof(u32
));
398 buf_offset
+= sizeof(u32
);
400 buf_offset
+= ltt_align(buf_offset
, sizeof(u64
));
401 ltt_relay_write(buf
, buf_offset
,
402 (u64
[]){ (u64
)tsc
}, sizeof(u64
));
403 buf_offset
+= sizeof(u64
);
405 case LTT_RFLAG_ID_SIZE
:
406 small_size
= min_t(size_t, event_size
, 0xFFFFU
);
407 ltt_relay_write(buf
, buf_offset
,
408 (u16
[]){ (u16
)eID
}, sizeof(u16
));
409 buf_offset
+= sizeof(u16
);
410 ltt_relay_write(buf
, buf_offset
,
411 (u16
[]){ (u16
)small_size
}, sizeof(u16
));
412 buf_offset
+= sizeof(u16
);
413 if (small_size
== 0xFFFFU
) {
414 ltt_relay_write(buf
, buf_offset
,
415 (u32
[]){ (u32
)event_size
}, sizeof(u32
));
416 buf_offset
+= sizeof(u32
);
420 ltt_relay_write(buf
, buf_offset
,
421 (u16
[]){ (u16
)eID
}, sizeof(u16
));
422 buf_offset
+= sizeof(u16
);
433 /* Buffer offset macros */
436 * BUFFER_TRUNC zeroes the subbuffer offset and the subbuffer number parts of
437 * the offset, which leaves only the buffer number.
439 #define BUFFER_TRUNC(offset, chan) \
440 ((offset) & (~((chan)->alloc_size-1)))
441 #define BUFFER_OFFSET(offset, chan) ((offset) & ((chan)->alloc_size - 1))
442 #define SUBBUF_OFFSET(offset, chan) ((offset) & ((chan)->subbuf_size - 1))
443 #define SUBBUF_ALIGN(offset, chan) \
444 (((offset) + (chan)->subbuf_size) & (~((chan)->subbuf_size - 1)))
445 #define SUBBUF_TRUNC(offset, chan) \
446 ((offset) & (~((chan)->subbuf_size - 1)))
447 #define SUBBUF_INDEX(offset, chan) \
448 (BUFFER_OFFSET((offset), chan) >> (chan)->subbuf_size_order)
453 * Atomic slot reservation in a LTTng buffer. It will take care of
454 * sub-buffer switching.
458 * @trace : the trace structure to log to.
459 * @channel : the chanel to reserve space into.
460 * @transport_data : specific transport data.
461 * @data_size : size of the variable length data to log.
462 * @slot_size : pointer to total size of the slot (out)
463 * @buf_offset : pointer to reserve offset (out)
464 * @tsc : pointer to the tsc at the slot reservation (out)
465 * @rflags : reservation flags (header specificity)
468 * Return : -ENOSPC if not enough space, else 0.
470 static inline int ltt_reserve_slot(
471 struct ltt_trace_struct
*trace
,
472 struct ltt_channel_struct
*channel
,
473 void **transport_data
,
478 unsigned int *rflags
,
481 return trace
->ops
->reserve_slot(trace
, channel
, transport_data
,
482 data_size
, slot_size
, buf_offset
, tsc
, rflags
,
490 * Atomic unordered slot commit. Increments the commit count in the
491 * specified sub-buffer, and delivers it if necessary.
495 * @channel : the chanel to reserve space into.
496 * @transport_data : specific transport data.
497 * @buf_offset : offset of beginning of reserved slot
498 * @slot_size : size of the reserved slot.
500 static inline void ltt_commit_slot(
501 struct ltt_channel_struct
*channel
,
502 void **transport_data
,
506 struct ltt_trace_struct
*trace
= channel
->trace
;
508 trace
->ops
->commit_slot(channel
, transport_data
, buf_offset
, slot_size
);
521 #define LTT_METADATA_CHANNEL "metadata_state"
522 #define LTT_UST_CHANNEL "ust"
524 #define LTT_FLIGHT_PREFIX "flight-"
526 /* Tracer properties */
527 //#define LTT_DEFAULT_SUBBUF_SIZE_LOW 65536
528 #define LTT_DEFAULT_SUBBUF_SIZE_LOW 4096
529 #define LTT_DEFAULT_N_SUBBUFS_LOW 2
530 //#define LTT_DEFAULT_SUBBUF_SIZE_MED 262144
531 #define LTT_DEFAULT_SUBBUF_SIZE_MED 4096
532 #define LTT_DEFAULT_N_SUBBUFS_MED 2
533 //#define LTT_DEFAULT_SUBBUF_SIZE_HIGH 1048576
534 #define LTT_DEFAULT_SUBBUF_SIZE_HIGH 4096
535 #define LTT_DEFAULT_N_SUBBUFS_HIGH 2
536 #define LTT_TRACER_MAGIC_NUMBER 0x00D6B7ED
537 #define LTT_TRACER_VERSION_MAJOR 2
538 #define LTT_TRACER_VERSION_MINOR 3
541 * Size reserved for high priority events (interrupts, NMI, BH) at the end of a
542 * nearly full buffer. User space won't use this last amount of space when in
543 * blocking mode. This space also includes the event header that would be
544 * written by this user space event.
546 #define LTT_RESERVE_CRITICAL 4096
548 /* Register and unregister function pointers */
550 enum ltt_module_function
{
551 LTT_FUNCTION_RUN_FILTER
,
552 LTT_FUNCTION_FILTER_CONTROL
,
553 LTT_FUNCTION_STATEDUMP
556 void ltt_transport_register(struct ltt_transport
*transport
);
557 void ltt_transport_unregister(struct ltt_transport
*transport
);
559 /* Exported control function */
561 union ltt_control_args
{
563 enum trace_mode mode
;
564 unsigned int subbuf_size_low
;
565 unsigned int n_subbufs_low
;
566 unsigned int subbuf_size_med
;
567 unsigned int n_subbufs_med
;
568 unsigned int subbuf_size_high
;
569 unsigned int n_subbufs_high
;
573 int _ltt_trace_setup(const char *trace_name
);
574 int ltt_trace_setup(const char *trace_name
);
575 struct ltt_trace_struct
*_ltt_trace_find_setup(const char *trace_name
);
576 int ltt_trace_set_type(const char *trace_name
, const char *trace_type
);
577 int ltt_trace_set_channel_subbufsize(const char *trace_name
,
578 const char *channel_name
, unsigned int size
);
579 int ltt_trace_set_channel_subbufcount(const char *trace_name
,
580 const char *channel_name
, unsigned int cnt
);
581 int ltt_trace_set_channel_enable(const char *trace_name
,
582 const char *channel_name
, unsigned int enable
);
583 int ltt_trace_set_channel_overwrite(const char *trace_name
,
584 const char *channel_name
, unsigned int overwrite
);
585 int ltt_trace_alloc(const char *trace_name
);
586 int ltt_trace_destroy(const char *trace_name
);
587 int ltt_trace_start(const char *trace_name
);
588 int ltt_trace_stop(const char *trace_name
);
590 enum ltt_filter_control_msg
{
591 LTT_FILTER_DEFAULT_ACCEPT
,
592 LTT_FILTER_DEFAULT_REJECT
595 extern int ltt_filter_control(enum ltt_filter_control_msg msg
,
596 const char *trace_name
);
598 extern struct dentry
*get_filter_root(void);
600 void ltt_write_trace_header(struct ltt_trace_struct
*trace
,
601 struct ltt_subbuffer_header
*header
);
602 extern void ltt_buffer_destroy(struct ltt_channel_struct
*ltt_chan
);
604 void ltt_core_register(int (*function
)(u8
, void *));
606 void ltt_core_unregister(void);
608 void ltt_release_trace(struct kref
*kref
);
609 void ltt_release_transport(struct kref
*kref
);
611 extern int ltt_probe_register(struct ltt_available_probe
*pdata
);
612 extern int ltt_probe_unregister(struct ltt_available_probe
*pdata
);
613 extern int ltt_marker_connect(const char *channel
, const char *mname
,
615 extern int ltt_marker_disconnect(const char *channel
, const char *mname
,
617 extern void ltt_dump_marker_state(struct ltt_trace_struct
*trace
);
619 void ltt_lock_traces(void);
620 void ltt_unlock_traces(void);
622 struct ltt_trace_struct
*_ltt_trace_find(const char *trace_name
);
624 #endif /* _LTT_TRACER_H */