cc86d548ecfb39e6a4af5429289e0ee55de67da2
[ust.git] / libtracing / tracer.h
1 /*
2 * Copyright (C) 2005,2006,2008 Mathieu Desnoyers (mathieu.desnoyers@polymtl.ca)
3 * Copyright (C) 2009 Pierre-Marc Fournier
4 *
5 * This contains the definitions for the Linux Trace Toolkit tracer.
6 *
7 * Ported to userspace by Pierre-Marc Fournier.
8 *
9 * This file is released under the GPLv2.
10 */
11
12 #ifndef _LTT_TRACER_H
13 #define _LTT_TRACER_H
14
15 #include <sys/types.h>
16 #include <stdarg.h>
17 #include "relay.h"
18 #include "list.h"
19 #include "kernelcompat.h"
20 #include "channels.h"
21 #include "tracercore.h"
22 #include "marker.h"
23
24 /* Number of bytes to log with a read/write event */
25 #define LTT_LOG_RW_SIZE 32L
26
27 /* Interval (in jiffies) at which the LTT per-CPU timer fires */
28 #define LTT_PERCPU_TIMER_INTERVAL 1
29
30 #ifndef LTT_ARCH_TYPE
31 #define LTT_ARCH_TYPE LTT_ARCH_TYPE_UNDEFINED
32 #endif
33
34 #ifndef LTT_ARCH_VARIANT
35 #define LTT_ARCH_VARIANT LTT_ARCH_VARIANT_NONE
36 #endif
37
38 struct ltt_active_marker;
39
40 /* Maximum number of callbacks per marker */
41 #define LTT_NR_CALLBACKS 10
42
43 struct ltt_serialize_closure;
44 struct ltt_probe_private_data;
45
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);
51
52 struct ltt_serialize_closure {
53 ltt_serialize_cb *callbacks;
54 long cb_args[LTT_NR_CALLBACKS];
55 unsigned int cb_idx;
56 };
57
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);
62
63 struct ltt_available_probe {
64 const char *name; /* probe name */
65 const char *format;
66 marker_probe_func *probe_func;
67 ltt_serialize_cb callbacks[LTT_NR_CALLBACKS];
68 struct list_head node; /* registered probes list */
69 };
70
71 struct ltt_probe_private_data {
72 struct ltt_trace_struct *trace; /*
73 * Target trace, for metadata
74 * or statedump.
75 */
76 ltt_serialize_cb serializer; /*
77 * Serialization function override.
78 */
79 void *serialize_private; /*
80 * Private data for serialization
81 * functions.
82 */
83 };
84
85 enum ltt_channels {
86 LTT_CHANNEL_METADATA,
87 LTT_CHANNEL_UST,
88 };
89
90 struct ltt_active_marker {
91 struct list_head node; /* active markers list */
92 const char *channel;
93 const char *name;
94 const char *format;
95 struct ltt_available_probe *probe;
96 };
97
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, ...);
103
104 /*
105 * Unique ID assigned to each registered probe.
106 */
107 enum marker_id {
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) */
112 };
113
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)
117 {
118 if (id < MARKER_CORE_IDS)
119 return (enum marker_id)id;
120 else
121 return MARKER_ID_DYNAMIC;
122 }
123
124 struct user_dbg_data {
125 unsigned long avail_size;
126 unsigned long write;
127 unsigned long read;
128 };
129
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,
137 int largest_align);
138 void (*commit_slot) (struct ltt_channel_struct *channel,
139 void **transport_data, long buf_offset,
140 size_t slot_size);
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;
160
161 struct ltt_transport {
162 char *name;
163 struct module *owner;
164 struct list_head node;
165 struct ltt_trace_ops ops;
166 };
167
168 enum trace_mode { LTT_TRACE_NORMAL, LTT_TRACE_FLIGHT, LTT_TRACE_HYBRID };
169
170 #define CHANNEL_FLAG_ENABLE (1U<<0)
171 #define CHANNEL_FLAG_OVERWRITE (1U<<1)
172
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;
178 int active;
179 /* Second 32 bytes cache-hot cacheline */
180 struct ltt_channel_struct *channels;
181 unsigned int nr_channels;
182 u32 freq_scale;
183 u64 start_freq;
184 u64 start_tsc;
185 unsigned long long start_monotonic;
186 struct timeval start_time;
187 struct ltt_channel_setting *settings;
188 struct {
189 struct dentry *trace_root;
190 } dentry;
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;
196
197 /* Hardcoded event headers
198 *
199 * event header for a trace with active heartbeat : 27 bits timestamps
200 *
201 * headers are 32-bits aligned. In order to insure such alignment, a dynamic per
202 * trace alignment value must be done.
203 *
204 * Remember that the C compiler does align each member on the boundary
205 * equivalent to their own size.
206 *
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.
209 *
210 * Event headers are aligned depending on the trace alignment option.
211 *
212 * Note using C structure bitfields for cross-endianness and portability
213 * concerns.
214 */
215
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)
221
222 struct ltt_event_header {
223 u32 id_time; /* 5 bits event id (MSB); 27 bits time (LSB) */
224 };
225
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)
230
231 /*
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.
237 *
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.
242 */
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.
249 */
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.
259 */
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.
266 * (may overflow)
267 */
268 uint32_t subbuf_corrupt; /*
269 * Corrupted (lost) subbuffers since
270 * the begginig of the trace.
271 * (may overflow)
272 */
273 uint8_t header_end[0]; /* End of header */
274 };
275
276 /**
277 * ltt_subbuffer_header_size - called on buffer-switch to a new sub-buffer
278 *
279 * Return header size without padding after the structure. Don't use packed
280 * structure because gcc generates inefficient code on some architectures
281 * (powerpc, mips..)
282 */
283 static inline size_t ltt_subbuffer_header_size(void)
284 {
285 return offsetof(struct ltt_subbuffer_header, header_end);
286 }
287
288 /*
289 * ltt_get_header_size
290 *
291 * Calculate alignment offset to 32-bits. This is the alignment offset of the
292 * event header.
293 *
294 * Important note :
295 * The event header must be 32-bits. The total offset calculated here :
296 *
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))
303 *
304 * The payload must itself determine its own alignment from the biggest type it
305 * contains.
306 * */
307 static inline unsigned char ltt_get_header_size(
308 struct ltt_channel_struct *channel,
309 size_t offset,
310 size_t data_size,
311 size_t *before_hdr_pad,
312 unsigned int rflags)
313 {
314 size_t orig_offset = offset;
315 size_t padding;
316
317 padding = ltt_align(offset, sizeof(struct ltt_event_header));
318 offset += padding;
319 offset += sizeof(struct ltt_event_header);
320
321 switch (rflags) {
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);
328 break;
329 case LTT_RFLAG_ID_SIZE:
330 offset += sizeof(u16) + sizeof(u16);
331 if (data_size >= 0xFFFFU)
332 offset += sizeof(u32);
333 break;
334 case LTT_RFLAG_ID:
335 offset += sizeof(u16);
336 break;
337 }
338
339 *before_hdr_pad = padding;
340 return offset - orig_offset;
341 }
342
343 /*
344 * ltt_write_event_header
345 *
346 * Writes the event header to the offset (already aligned on 32-bits).
347 *
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).
352 * @eID : event ID
353 * @event_size : size of the event, excluding the event header.
354 * @tsc : time stamp counter.
355 * @rflags : reservation flags.
356 *
357 * returns : offset where the event data must be written.
358 */
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)
364 {
365 struct ltt_event_header header;
366 size_t small_size;
367
368 switch (rflags) {
369 case LTT_RFLAG_ID_SIZE_TSC:
370 header.id_time = 29 << LTT_TSC_BITS;
371 break;
372 case LTT_RFLAG_ID_SIZE:
373 header.id_time = 30 << LTT_TSC_BITS;
374 break;
375 case LTT_RFLAG_ID:
376 header.id_time = 31 << LTT_TSC_BITS;
377 break;
378 default:
379 header.id_time = eID << LTT_TSC_BITS;
380 break;
381 }
382 header.id_time |= (u32)tsc & LTT_TSC_MASK;
383 ltt_relay_write(buf, buf_offset, &header, sizeof(header));
384 buf_offset += sizeof(header);
385
386 switch (rflags) {
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);
399 }
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);
404 break;
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);
417 }
418 break;
419 case LTT_RFLAG_ID:
420 ltt_relay_write(buf, buf_offset,
421 (u16[]){ (u16)eID }, sizeof(u16));
422 buf_offset += sizeof(u16);
423 break;
424 default:
425 break;
426 }
427
428 return buf_offset;
429 }
430
431 /* Lockless LTTng */
432
433 /* Buffer offset macros */
434
435 /*
436 * BUFFER_TRUNC zeroes the subbuffer offset and the subbuffer number parts of
437 * the offset, which leaves only the buffer number.
438 */
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)
449
450 /*
451 * ltt_reserve_slot
452 *
453 * Atomic slot reservation in a LTTng buffer. It will take care of
454 * sub-buffer switching.
455 *
456 * Parameters:
457 *
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)
466 * @cpu : cpu id
467 *
468 * Return : -ENOSPC if not enough space, else 0.
469 */
470 static inline int ltt_reserve_slot(
471 struct ltt_trace_struct *trace,
472 struct ltt_channel_struct *channel,
473 void **transport_data,
474 size_t data_size,
475 size_t *slot_size,
476 long *buf_offset,
477 u64 *tsc,
478 unsigned int *rflags,
479 int largest_align)
480 {
481 return trace->ops->reserve_slot(trace, channel, transport_data,
482 data_size, slot_size, buf_offset, tsc, rflags,
483 largest_align);
484 }
485
486
487 /*
488 * ltt_commit_slot
489 *
490 * Atomic unordered slot commit. Increments the commit count in the
491 * specified sub-buffer, and delivers it if necessary.
492 *
493 * Parameters:
494 *
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.
499 */
500 static inline void ltt_commit_slot(
501 struct ltt_channel_struct *channel,
502 void **transport_data,
503 long buf_offset,
504 size_t slot_size)
505 {
506 struct ltt_trace_struct *trace = channel->trace;
507
508 trace->ops->commit_slot(channel, transport_data, buf_offset, slot_size);
509 }
510
511 /*
512 * Control channels :
513 * control/metadata
514 * control/interrupts
515 * control/...
516 *
517 * cpu channel :
518 * cpu
519 */
520
521 #define LTT_METADATA_CHANNEL "metadata_state"
522 #define LTT_UST_CHANNEL "ust"
523
524 #define LTT_FLIGHT_PREFIX "flight-"
525
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
539
540 /*
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.
545 */
546 #define LTT_RESERVE_CRITICAL 4096
547
548 /* Register and unregister function pointers */
549
550 enum ltt_module_function {
551 LTT_FUNCTION_RUN_FILTER,
552 LTT_FUNCTION_FILTER_CONTROL,
553 LTT_FUNCTION_STATEDUMP
554 };
555
556 void ltt_transport_register(struct ltt_transport *transport);
557 void ltt_transport_unregister(struct ltt_transport *transport);
558
559 /* Exported control function */
560
561 union ltt_control_args {
562 struct {
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;
570 } new_trace;
571 };
572
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);
589
590 enum ltt_filter_control_msg {
591 LTT_FILTER_DEFAULT_ACCEPT,
592 LTT_FILTER_DEFAULT_REJECT
593 };
594
595 extern int ltt_filter_control(enum ltt_filter_control_msg msg,
596 const char *trace_name);
597
598 extern struct dentry *get_filter_root(void);
599
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);
603
604 void ltt_core_register(int (*function)(u8, void *));
605
606 void ltt_core_unregister(void);
607
608 void ltt_release_trace(struct kref *kref);
609 void ltt_release_transport(struct kref *kref);
610
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,
614 const char *pname);
615 extern int ltt_marker_disconnect(const char *channel, const char *mname,
616 const char *pname);
617 extern void ltt_dump_marker_state(struct ltt_trace_struct *trace);
618
619 void ltt_lock_traces(void);
620 void ltt_unlock_traces(void);
621
622 struct ltt_trace_struct *_ltt_trace_find(const char *trace_name);
623
624 #endif /* _LTT_TRACER_H */
This page took 0.039848 seconds and 3 git commands to generate.