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 library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 #include <sys/types.h>
30 #include <ust/kernelcompat.h>
33 #include "tracercore.h"
34 #include <ust/marker.h>
36 /* Number of bytes to log with a read/write event */
37 #define LTT_LOG_RW_SIZE 32L
39 /* Interval (in jiffies) at which the LTT per-CPU timer fires */
40 #define LTT_PERCPU_TIMER_INTERVAL 1
43 #define LTT_ARCH_TYPE LTT_ARCH_TYPE_UNDEFINED
46 #ifndef LTT_ARCH_VARIANT
47 #define LTT_ARCH_VARIANT LTT_ARCH_VARIANT_NONE
50 struct ltt_active_marker
;
52 /* Maximum number of callbacks per marker */
53 #define LTT_NR_CALLBACKS 10
55 struct ltt_serialize_closure
;
56 struct ltt_probe_private_data
;
58 /* Serialization callback '%k' */
59 typedef size_t (*ltt_serialize_cb
)(struct ust_buffer
*buf
, size_t buf_offset
,
60 struct ltt_serialize_closure
*closure
,
61 void *serialize_private
, int *largest_align
,
62 const char *fmt
, va_list *args
);
64 struct ltt_serialize_closure
{
65 ltt_serialize_cb
*callbacks
;
66 long cb_args
[LTT_NR_CALLBACKS
];
70 extern size_t ltt_serialize_data(struct ust_buffer
*buf
, size_t buf_offset
,
71 struct ltt_serialize_closure
*closure
,
72 void *serialize_private
,
73 int *largest_align
, const char *fmt
, va_list *args
);
75 struct ltt_available_probe
{
76 const char *name
; /* probe name */
78 marker_probe_func
*probe_func
;
79 ltt_serialize_cb callbacks
[LTT_NR_CALLBACKS
];
80 struct list_head node
; /* registered probes list */
83 struct ltt_probe_private_data
{
84 struct ltt_trace_struct
*trace
; /*
85 * Target trace, for metadata
88 ltt_serialize_cb serializer
; /*
89 * Serialization function override.
91 void *serialize_private
; /*
92 * Private data for serialization
102 struct ltt_active_marker
{
103 struct list_head node
; /* active markers list */
107 struct ltt_available_probe
*probe
;
110 struct marker
; //ust//
111 extern void ltt_vtrace(const struct marker
*mdata
, void *probe_data
,
112 struct registers
*regs
, void *call_data
, const char *fmt
, va_list *args
);
113 extern void ltt_trace(const struct marker
*mdata
, void *probe_data
,
114 struct registers
*regs
, void *call_data
, const char *fmt
, ...);
117 * Unique ID assigned to each registered probe.
120 MARKER_ID_SET_MARKER_ID
= 0, /* Static IDs available (range 0-7) */
121 MARKER_ID_SET_MARKER_FORMAT
,
122 MARKER_ID_COMPACT
, /* Compact IDs (range: 8-127) */
123 MARKER_ID_DYNAMIC
, /* Dynamic IDs (range: 128-65535) */
126 /* static ids 0-1 reserved for internal use. */
127 #define MARKER_CORE_IDS 2
128 static inline enum marker_id
marker_id_type(uint16_t id
)
130 if (id
< MARKER_CORE_IDS
)
131 return (enum marker_id
)id
;
133 return MARKER_ID_DYNAMIC
;
136 struct user_dbg_data
{
137 unsigned long avail_size
;
142 struct ltt_trace_ops
{
143 /* First 32 bytes cache-hot cacheline */
144 int (*reserve_slot
) (struct ltt_trace_struct
*trace
,
145 struct ust_channel
*channel
,
146 void **transport_data
, size_t data_size
,
147 size_t *slot_size
, long *buf_offset
, u64
*tsc
,
148 unsigned int *rflags
,
150 //ust// void (*commit_slot) (struct ltt_channel_struct *channel,
151 //ust// void **transport_data, long buf_offset,
152 //ust// size_t slot_size);
153 void (*wakeup_channel
) (struct ust_channel
*channel
);
154 int (*user_blocking
) (struct ltt_trace_struct
*trace
,
155 unsigned int index
, size_t data_size
,
156 struct user_dbg_data
*dbg
);
157 /* End of first 32 bytes cacheline */
158 int (*create_dirs
) (struct ltt_trace_struct
*new_trace
);
159 void (*remove_dirs
) (struct ltt_trace_struct
*new_trace
);
160 int (*create_channel
) (const char *trace_name
,
161 struct ltt_trace_struct
*trace
,
162 const char *channel_name
,
163 struct ust_channel
*channel
,
164 unsigned int subbuf_size
,
165 unsigned int n_subbufs
, int overwrite
);
166 void (*finish_channel
) (struct ust_channel
*channel
);
167 void (*remove_channel
) (struct ust_channel
*channel
);
168 void (*user_errors
) (struct ltt_trace_struct
*trace
,
169 unsigned int index
, size_t data_size
,
170 struct user_dbg_data
*dbg
);
171 } ____cacheline_aligned
;
173 struct ltt_transport
{
175 struct module
*owner
;
176 struct list_head node
;
177 struct ltt_trace_ops ops
;
180 enum trace_mode
{ LTT_TRACE_NORMAL
, LTT_TRACE_FLIGHT
, LTT_TRACE_HYBRID
};
182 #define CHANNEL_FLAG_ENABLE (1U<<0)
183 #define CHANNEL_FLAG_OVERWRITE (1U<<1)
185 /* Per-trace information - each trace/flight recorder represented by one */
186 struct ltt_trace_struct
{
187 /* First 32 bytes cache-hot cacheline */
188 struct list_head list
;
189 struct ltt_trace_ops
*ops
;
191 /* Second 32 bytes cache-hot cacheline */
192 struct ust_channel
*channels
;
193 unsigned int nr_channels
;
197 unsigned long long start_monotonic
;
198 struct timeval start_time
;
199 struct ltt_channel_setting
*settings
;
201 struct dentry
*trace_root
;
203 struct kref kref
; /* Each channel has a kref of the trace struct */
204 struct ltt_transport
*transport
;
205 struct kref ltt_transport_kref
;
206 char trace_name
[NAME_MAX
];
207 } ____cacheline_aligned
;
209 /* Hardcoded event headers
211 * event header for a trace with active heartbeat : 27 bits timestamps
213 * headers are 32-bits aligned. In order to insure such alignment, a dynamic per
214 * trace alignment value must be done.
216 * Remember that the C compiler does align each member on the boundary
217 * equivalent to their own size.
219 * As relay subbuffers are aligned on pages, we are sure that they are 4 and 8
220 * bytes aligned, so the buffer header and trace header are aligned.
222 * Event headers are aligned depending on the trace alignment option.
224 * Note using C structure bitfields for cross-endianness and portability
228 #define LTT_RESERVED_EVENTS 3
229 #define LTT_EVENT_BITS 5
230 #define LTT_FREE_EVENTS ((1 << LTT_EVENT_BITS) - LTT_RESERVED_EVENTS)
231 #define LTT_TSC_BITS 27
232 #define LTT_TSC_MASK ((1 << LTT_TSC_BITS) - 1)
234 struct ltt_event_header
{
235 u32 id_time
; /* 5 bits event id (MSB); 27 bits time (LSB) */
238 /* Reservation flags */
239 #define LTT_RFLAG_ID (1 << 0)
240 #define LTT_RFLAG_ID_SIZE (1 << 1)
241 #define LTT_RFLAG_ID_SIZE_TSC (1 << 2)
244 * We use asm/timex.h : cpu_khz/HZ variable in here : we might have to deal
245 * specifically with CPU frequency scaling someday, so using an interpolation
246 * between the start and end of buffer values is not flexible enough. Using an
247 * immediate frequency value permits to calculate directly the times for parts
248 * of a buffer that would be before a frequency change.
250 * Keep the natural field alignment for _each field_ within this structure if
251 * you ever add/remove a field from this header. Packed attribute is not used
252 * because gcc generates poor code on at least powerpc and mips. Don't ever
253 * let gcc add padding between the structure elements.
255 struct ltt_subbuffer_header
{
256 uint64_t cycle_count_begin
; /* Cycle count at subbuffer start */
257 uint64_t cycle_count_end
; /* Cycle count at subbuffer end */
258 uint32_t magic_number
; /*
259 * Trace magic number.
260 * contains endianness information.
262 uint8_t major_version
;
263 uint8_t minor_version
;
264 uint8_t arch_size
; /* Architecture pointer size */
265 uint8_t alignment
; /* LTT data alignment */
266 uint64_t start_time_sec
; /* NTP-corrected start time */
267 uint64_t start_time_usec
;
268 uint64_t start_freq
; /*
269 * Frequency at trace start,
270 * used all along the trace.
272 uint32_t freq_scale
; /* Frequency scaling (divisor) */
273 uint32_t lost_size
; /* Size unused at end of subbuffer */
274 uint32_t buf_size
; /* Size of this subbuffer */
275 uint32_t events_lost
; /*
276 * Events lost in this subbuffer since
277 * the beginning of the trace.
280 uint32_t subbuf_corrupt
; /*
281 * Corrupted (lost) subbuffers since
282 * the begginig of the trace.
285 uint8_t header_end
[0]; /* End of header */
289 * ltt_subbuffer_header_size - called on buffer-switch to a new sub-buffer
291 * Return header size without padding after the structure. Don't use packed
292 * structure because gcc generates inefficient code on some architectures
295 static inline size_t ltt_subbuffer_header_size(void)
297 return offsetof(struct ltt_subbuffer_header
, header_end
);
301 * ust_get_header_size
303 * Calculate alignment offset to 32-bits. This is the alignment offset of the
307 * The event header must be 32-bits. The total offset calculated here :
309 * Alignment of header struct on 32 bits (min arch size, header size)
310 * + sizeof(header struct) (32-bits)
311 * + (opt) u16 (ext. event id)
312 * + (opt) u16 (event_size) (if event_size == 0xFFFFUL, has ext. event size)
313 * + (opt) u32 (ext. event size)
314 * + (opt) u64 full TSC (aligned on min(64-bits, arch size))
316 * The payload must itself determine its own alignment from the biggest type it
319 static inline unsigned char ust_get_header_size(
320 struct ust_channel
*channel
,
323 size_t *before_hdr_pad
,
326 size_t orig_offset
= offset
;
329 padding
= ltt_align(offset
, sizeof(struct ltt_event_header
));
331 offset
+= sizeof(struct ltt_event_header
);
334 case LTT_RFLAG_ID_SIZE_TSC
:
335 offset
+= sizeof(u16
) + sizeof(u16
);
336 if (data_size
>= 0xFFFFU
)
337 offset
+= sizeof(u32
);
338 offset
+= ltt_align(offset
, sizeof(u64
));
339 offset
+= sizeof(u64
);
341 case LTT_RFLAG_ID_SIZE
:
342 offset
+= sizeof(u16
) + sizeof(u16
);
343 if (data_size
>= 0xFFFFU
)
344 offset
+= sizeof(u32
);
347 offset
+= sizeof(u16
);
351 *before_hdr_pad
= padding
;
352 return offset
- orig_offset
;
356 * ltt_write_event_header
358 * Writes the event header to the offset (already aligned on 32-bits).
360 * @trace : trace to write to.
361 * @channel : pointer to the channel structure..
362 * @buf : buffer to write to.
363 * @buf_offset : buffer offset to write to (aligned on 32 bits).
365 * @event_size : size of the event, excluding the event header.
366 * @tsc : time stamp counter.
367 * @rflags : reservation flags.
369 * returns : offset where the event data must be written.
371 static inline size_t ltt_write_event_header(struct ltt_trace_struct
*trace
,
372 struct ust_buffer
*buf
, long buf_offset
,
373 u16 eID
, size_t event_size
,
374 u64 tsc
, unsigned int rflags
)
376 struct ltt_event_header header
;
380 case LTT_RFLAG_ID_SIZE_TSC
:
381 header
.id_time
= 29 << LTT_TSC_BITS
;
383 case LTT_RFLAG_ID_SIZE
:
384 header
.id_time
= 30 << LTT_TSC_BITS
;
387 header
.id_time
= 31 << LTT_TSC_BITS
;
390 header
.id_time
= eID
<< LTT_TSC_BITS
;
393 header
.id_time
|= (u32
)tsc
& LTT_TSC_MASK
;
394 ust_buffers_write(buf
, buf_offset
, &header
, sizeof(header
));
395 buf_offset
+= sizeof(header
);
398 case LTT_RFLAG_ID_SIZE_TSC
:
399 small_size
= min_t(size_t, event_size
, 0xFFFFU
);
400 ust_buffers_write(buf
, buf_offset
,
401 (u16
[]){ (u16
)eID
}, sizeof(u16
));
402 buf_offset
+= sizeof(u16
);
403 ust_buffers_write(buf
, buf_offset
,
404 (u16
[]){ (u16
)small_size
}, sizeof(u16
));
405 buf_offset
+= sizeof(u16
);
406 if (small_size
== 0xFFFFU
) {
407 ust_buffers_write(buf
, buf_offset
,
408 (u32
[]){ (u32
)event_size
}, sizeof(u32
));
409 buf_offset
+= sizeof(u32
);
411 buf_offset
+= ltt_align(buf_offset
, sizeof(u64
));
412 ust_buffers_write(buf
, buf_offset
,
413 (u64
[]){ (u64
)tsc
}, sizeof(u64
));
414 buf_offset
+= sizeof(u64
);
416 case LTT_RFLAG_ID_SIZE
:
417 small_size
= min_t(size_t, event_size
, 0xFFFFU
);
418 ust_buffers_write(buf
, buf_offset
,
419 (u16
[]){ (u16
)eID
}, sizeof(u16
));
420 buf_offset
+= sizeof(u16
);
421 ust_buffers_write(buf
, buf_offset
,
422 (u16
[]){ (u16
)small_size
}, sizeof(u16
));
423 buf_offset
+= sizeof(u16
);
424 if (small_size
== 0xFFFFU
) {
425 ust_buffers_write(buf
, buf_offset
,
426 (u32
[]){ (u32
)event_size
}, sizeof(u32
));
427 buf_offset
+= sizeof(u32
);
431 ust_buffers_write(buf
, buf_offset
,
432 (u16
[]){ (u16
)eID
}, sizeof(u16
));
433 buf_offset
+= sizeof(u16
);
447 * Atomic slot reservation in a LTTng buffer. It will take care of
448 * sub-buffer switching.
452 * @trace : the trace structure to log to.
453 * @channel : the chanel to reserve space into.
454 * @transport_data : specific transport data.
455 * @data_size : size of the variable length data to log.
456 * @slot_size : pointer to total size of the slot (out)
457 * @buf_offset : pointer to reserve offset (out)
458 * @tsc : pointer to the tsc at the slot reservation (out)
459 * @rflags : reservation flags (header specificity)
462 * Return : -ENOSPC if not enough space, else 0.
464 static inline int ltt_reserve_slot(
465 struct ltt_trace_struct
*trace
,
466 struct ust_channel
*channel
,
467 void **transport_data
,
472 unsigned int *rflags
,
475 return trace
->ops
->reserve_slot(trace
, channel
, transport_data
,
476 data_size
, slot_size
, buf_offset
, tsc
, rflags
,
484 // * Atomic unordered slot commit. Increments the commit count in the
485 // * specified sub-buffer, and delivers it if necessary.
489 // * @channel : the chanel to reserve space into.
490 // * @transport_data : specific transport data.
491 // * @buf_offset : offset of beginning of reserved slot
492 // * @slot_size : size of the reserved slot.
494 //static inline void ltt_commit_slot(
495 // struct ltt_channel_struct *channel,
496 // void **transport_data,
500 // struct ltt_trace_struct *trace = channel->trace;
502 // trace->ops->commit_slot(channel, transport_data, buf_offset, slot_size);
515 #define LTT_METADATA_CHANNEL "metadata_state"
516 #define LTT_UST_CHANNEL "ust"
518 #define LTT_FLIGHT_PREFIX "flight-"
520 /* Tracer properties */
521 //#define LTT_DEFAULT_SUBBUF_SIZE_LOW 134217728
522 #define LTT_DEFAULT_SUBBUF_SIZE_LOW 65536
523 //#define LTT_DEFAULT_SUBBUF_SIZE_LOW 4096
524 #define LTT_DEFAULT_N_SUBBUFS_LOW 2
525 //#define LTT_DEFAULT_SUBBUF_SIZE_MED 134217728
526 #define LTT_DEFAULT_SUBBUF_SIZE_MED 262144
527 //#define LTT_DEFAULT_SUBBUF_SIZE_MED 4096
528 #define LTT_DEFAULT_N_SUBBUFS_MED 2
529 //#define LTT_DEFAULT_SUBBUF_SIZE_HIGH 134217728
530 #define LTT_DEFAULT_SUBBUF_SIZE_HIGH 1048576
531 //#define LTT_DEFAULT_SUBBUF_SIZE_HIGH 4096
532 #define LTT_DEFAULT_N_SUBBUFS_HIGH 2
533 #define LTT_TRACER_MAGIC_NUMBER 0x00D6B7ED
534 #define LTT_TRACER_VERSION_MAJOR 2
535 #define LTT_TRACER_VERSION_MINOR 3
538 * Size reserved for high priority events (interrupts, NMI, BH) at the end of a
539 * nearly full buffer. User space won't use this last amount of space when in
540 * blocking mode. This space also includes the event header that would be
541 * written by this user space event.
543 #define LTT_RESERVE_CRITICAL 4096
545 /* Register and unregister function pointers */
547 enum ltt_module_function
{
548 LTT_FUNCTION_RUN_FILTER
,
549 LTT_FUNCTION_FILTER_CONTROL
,
550 LTT_FUNCTION_STATEDUMP
553 extern void ltt_transport_register(struct ltt_transport
*transport
);
554 extern void ltt_transport_unregister(struct ltt_transport
*transport
);
556 /* Exported control function */
558 union ltt_control_args
{
560 enum trace_mode mode
;
561 unsigned int subbuf_size_low
;
562 unsigned int n_subbufs_low
;
563 unsigned int subbuf_size_med
;
564 unsigned int n_subbufs_med
;
565 unsigned int subbuf_size_high
;
566 unsigned int n_subbufs_high
;
570 extern int _ltt_trace_setup(const char *trace_name
);
571 extern int ltt_trace_setup(const char *trace_name
);
572 extern struct ltt_trace_struct
*_ltt_trace_find_setup(const char *trace_name
);
573 extern int ltt_trace_set_type(const char *trace_name
, const char *trace_type
);
574 extern int ltt_trace_set_channel_subbufsize(const char *trace_name
,
575 const char *channel_name
, unsigned int size
);
576 extern int ltt_trace_set_channel_subbufcount(const char *trace_name
,
577 const char *channel_name
, unsigned int cnt
);
578 extern int ltt_trace_set_channel_enable(const char *trace_name
,
579 const char *channel_name
, unsigned int enable
);
580 extern int ltt_trace_set_channel_overwrite(const char *trace_name
,
581 const char *channel_name
, unsigned int overwrite
);
582 extern int ltt_trace_alloc(const char *trace_name
);
583 extern int ltt_trace_destroy(const char *trace_name
);
584 extern int ltt_trace_start(const char *trace_name
);
585 extern int ltt_trace_stop(const char *trace_name
);
587 enum ltt_filter_control_msg
{
588 LTT_FILTER_DEFAULT_ACCEPT
,
589 LTT_FILTER_DEFAULT_REJECT
592 extern int ltt_filter_control(enum ltt_filter_control_msg msg
,
593 const char *trace_name
);
595 extern struct dentry
*get_filter_root(void);
597 extern void ltt_write_trace_header(struct ltt_trace_struct
*trace
,
598 struct ltt_subbuffer_header
*header
);
599 extern void ltt_buffer_destroy(struct ust_channel
*ltt_chan
);
601 extern void ltt_core_register(int (*function
)(u8
, void *));
603 extern void ltt_core_unregister(void);
605 extern void ltt_release_trace(struct kref
*kref
);
606 extern void ltt_release_transport(struct kref
*kref
);
608 extern int ltt_probe_register(struct ltt_available_probe
*pdata
);
609 extern int ltt_probe_unregister(struct ltt_available_probe
*pdata
);
610 extern int ltt_marker_connect(const char *channel
, const char *mname
,
612 extern int ltt_marker_disconnect(const char *channel
, const char *mname
,
614 extern void ltt_dump_marker_state(struct ltt_trace_struct
*trace
);
616 extern void ltt_lock_traces(void);
617 extern void ltt_unlock_traces(void);
619 extern struct ltt_trace_struct
*_ltt_trace_find(const char *trace_name
);
621 #endif /* _LTT_TRACER_H */