libust: put offset macros in buffer.h
[ust.git] / libust / 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 "list.h"
18 #include "kernelcompat.h"
19 #include "buffer.h"
20 #include "relay.h"
21 #include "channels.h"
22 #include "tracercore.h"
23 #include "marker.h"
24
25 /* Number of bytes to log with a read/write event */
26 #define LTT_LOG_RW_SIZE 32L
27
28 /* Interval (in jiffies) at which the LTT per-CPU timer fires */
29 #define LTT_PERCPU_TIMER_INTERVAL 1
30
31 #ifndef LTT_ARCH_TYPE
32 #define LTT_ARCH_TYPE LTT_ARCH_TYPE_UNDEFINED
33 #endif
34
35 #ifndef LTT_ARCH_VARIANT
36 #define LTT_ARCH_VARIANT LTT_ARCH_VARIANT_NONE
37 #endif
38
39 struct ltt_active_marker;
40
41 /* Maximum number of callbacks per marker */
42 #define LTT_NR_CALLBACKS 10
43
44 struct ltt_serialize_closure;
45 struct ltt_probe_private_data;
46
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);
52
53 struct ltt_serialize_closure {
54 ltt_serialize_cb *callbacks;
55 long cb_args[LTT_NR_CALLBACKS];
56 unsigned int cb_idx;
57 };
58
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);
63
64 struct ltt_available_probe {
65 const char *name; /* probe name */
66 const char *format;
67 marker_probe_func *probe_func;
68 ltt_serialize_cb callbacks[LTT_NR_CALLBACKS];
69 struct list_head node; /* registered probes list */
70 };
71
72 struct ltt_probe_private_data {
73 struct ltt_trace_struct *trace; /*
74 * Target trace, for metadata
75 * or statedump.
76 */
77 ltt_serialize_cb serializer; /*
78 * Serialization function override.
79 */
80 void *serialize_private; /*
81 * Private data for serialization
82 * functions.
83 */
84 };
85
86 enum ltt_channels {
87 LTT_CHANNEL_METADATA,
88 LTT_CHANNEL_UST,
89 };
90
91 struct ltt_active_marker {
92 struct list_head node; /* active markers list */
93 const char *channel;
94 const char *name;
95 const char *format;
96 struct ltt_available_probe *probe;
97 };
98
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, ...);
104
105 /*
106 * Unique ID assigned to each registered probe.
107 */
108 enum marker_id {
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) */
113 };
114
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)
118 {
119 if (id < MARKER_CORE_IDS)
120 return (enum marker_id)id;
121 else
122 return MARKER_ID_DYNAMIC;
123 }
124
125 struct user_dbg_data {
126 unsigned long avail_size;
127 unsigned long write;
128 unsigned long read;
129 };
130
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,
138 int largest_align);
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;
161
162 struct ltt_transport {
163 char *name;
164 struct module *owner;
165 struct list_head node;
166 struct ltt_trace_ops ops;
167 };
168
169 enum trace_mode { LTT_TRACE_NORMAL, LTT_TRACE_FLIGHT, LTT_TRACE_HYBRID };
170
171 #define CHANNEL_FLAG_ENABLE (1U<<0)
172 #define CHANNEL_FLAG_OVERWRITE (1U<<1)
173
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;
179 int active;
180 /* Second 32 bytes cache-hot cacheline */
181 struct ltt_channel_struct *channels;
182 unsigned int nr_channels;
183 u32 freq_scale;
184 u64 start_freq;
185 u64 start_tsc;
186 unsigned long long start_monotonic;
187 struct timeval start_time;
188 struct ltt_channel_setting *settings;
189 struct {
190 struct dentry *trace_root;
191 } dentry;
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;
197
198 /* Hardcoded event headers
199 *
200 * event header for a trace with active heartbeat : 27 bits timestamps
201 *
202 * headers are 32-bits aligned. In order to insure such alignment, a dynamic per
203 * trace alignment value must be done.
204 *
205 * Remember that the C compiler does align each member on the boundary
206 * equivalent to their own size.
207 *
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.
210 *
211 * Event headers are aligned depending on the trace alignment option.
212 *
213 * Note using C structure bitfields for cross-endianness and portability
214 * concerns.
215 */
216
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)
222
223 struct ltt_event_header {
224 u32 id_time; /* 5 bits event id (MSB); 27 bits time (LSB) */
225 };
226
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)
231
232 /*
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.
238 *
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.
243 */
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.
250 */
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.
260 */
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.
267 * (may overflow)
268 */
269 uint32_t subbuf_corrupt; /*
270 * Corrupted (lost) subbuffers since
271 * the begginig of the trace.
272 * (may overflow)
273 */
274 uint8_t header_end[0]; /* End of header */
275 };
276
277 /**
278 * ltt_subbuffer_header_size - called on buffer-switch to a new sub-buffer
279 *
280 * Return header size without padding after the structure. Don't use packed
281 * structure because gcc generates inefficient code on some architectures
282 * (powerpc, mips..)
283 */
284 static inline size_t ltt_subbuffer_header_size(void)
285 {
286 return offsetof(struct ltt_subbuffer_header, header_end);
287 }
288
289 /*
290 * ltt_get_header_size
291 *
292 * Calculate alignment offset to 32-bits. This is the alignment offset of the
293 * event header.
294 *
295 * Important note :
296 * The event header must be 32-bits. The total offset calculated here :
297 *
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))
304 *
305 * The payload must itself determine its own alignment from the biggest type it
306 * contains.
307 * */
308 static inline unsigned char ltt_get_header_size(
309 struct ltt_channel_struct *channel,
310 size_t offset,
311 size_t data_size,
312 size_t *before_hdr_pad,
313 unsigned int rflags)
314 {
315 size_t orig_offset = offset;
316 size_t padding;
317
318 padding = ltt_align(offset, sizeof(struct ltt_event_header));
319 offset += padding;
320 offset += sizeof(struct ltt_event_header);
321
322 switch (rflags) {
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);
329 break;
330 case LTT_RFLAG_ID_SIZE:
331 offset += sizeof(u16) + sizeof(u16);
332 if (data_size >= 0xFFFFU)
333 offset += sizeof(u32);
334 break;
335 case LTT_RFLAG_ID:
336 offset += sizeof(u16);
337 break;
338 }
339
340 *before_hdr_pad = padding;
341 return offset - orig_offset;
342 }
343
344 /*
345 * ltt_write_event_header
346 *
347 * Writes the event header to the offset (already aligned on 32-bits).
348 *
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).
353 * @eID : event ID
354 * @event_size : size of the event, excluding the event header.
355 * @tsc : time stamp counter.
356 * @rflags : reservation flags.
357 *
358 * returns : offset where the event data must be written.
359 */
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)
365 {
366 struct ltt_event_header header;
367 size_t small_size;
368
369 switch (rflags) {
370 case LTT_RFLAG_ID_SIZE_TSC:
371 header.id_time = 29 << LTT_TSC_BITS;
372 break;
373 case LTT_RFLAG_ID_SIZE:
374 header.id_time = 30 << LTT_TSC_BITS;
375 break;
376 case LTT_RFLAG_ID:
377 header.id_time = 31 << LTT_TSC_BITS;
378 break;
379 default:
380 header.id_time = eID << LTT_TSC_BITS;
381 break;
382 }
383 header.id_time |= (u32)tsc & LTT_TSC_MASK;
384 ltt_relay_write(buf, buf_offset, &header, sizeof(header));
385 buf_offset += sizeof(header);
386
387 switch (rflags) {
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);
400 }
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);
405 break;
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);
418 }
419 break;
420 case LTT_RFLAG_ID:
421 ltt_relay_write(buf, buf_offset,
422 (u16[]){ (u16)eID }, sizeof(u16));
423 buf_offset += sizeof(u16);
424 break;
425 default:
426 break;
427 }
428
429 return buf_offset;
430 }
431
432 /* Lockless LTTng */
433
434 /*
435 * ltt_reserve_slot
436 *
437 * Atomic slot reservation in a LTTng buffer. It will take care of
438 * sub-buffer switching.
439 *
440 * Parameters:
441 *
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)
450 * @cpu : cpu id
451 *
452 * Return : -ENOSPC if not enough space, else 0.
453 */
454 static inline int ltt_reserve_slot(
455 struct ltt_trace_struct *trace,
456 struct ltt_channel_struct *channel,
457 void **transport_data,
458 size_t data_size,
459 size_t *slot_size,
460 long *buf_offset,
461 u64 *tsc,
462 unsigned int *rflags,
463 int largest_align)
464 {
465 return trace->ops->reserve_slot(trace, channel, transport_data,
466 data_size, slot_size, buf_offset, tsc, rflags,
467 largest_align);
468 }
469
470
471 ///*
472 // * ltt_commit_slot
473 // *
474 // * Atomic unordered slot commit. Increments the commit count in the
475 // * specified sub-buffer, and delivers it if necessary.
476 // *
477 // * Parameters:
478 // *
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.
483 // */
484 //static inline void ltt_commit_slot(
485 // struct ltt_channel_struct *channel,
486 // void **transport_data,
487 // long buf_offset,
488 // size_t slot_size)
489 //{
490 // struct ltt_trace_struct *trace = channel->trace;
491 //
492 // trace->ops->commit_slot(channel, transport_data, buf_offset, slot_size);
493 //}
494
495 /*
496 * Control channels :
497 * control/metadata
498 * control/interrupts
499 * control/...
500 *
501 * cpu channel :
502 * cpu
503 */
504
505 #define LTT_METADATA_CHANNEL "metadata_state"
506 #define LTT_UST_CHANNEL "ust"
507
508 #define LTT_FLIGHT_PREFIX "flight-"
509
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
523
524 /*
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.
529 */
530 #define LTT_RESERVE_CRITICAL 4096
531
532 /* Register and unregister function pointers */
533
534 enum ltt_module_function {
535 LTT_FUNCTION_RUN_FILTER,
536 LTT_FUNCTION_FILTER_CONTROL,
537 LTT_FUNCTION_STATEDUMP
538 };
539
540 void ltt_transport_register(struct ltt_transport *transport);
541 void ltt_transport_unregister(struct ltt_transport *transport);
542
543 /* Exported control function */
544
545 union ltt_control_args {
546 struct {
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;
554 } new_trace;
555 };
556
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);
573
574 enum ltt_filter_control_msg {
575 LTT_FILTER_DEFAULT_ACCEPT,
576 LTT_FILTER_DEFAULT_REJECT
577 };
578
579 extern int ltt_filter_control(enum ltt_filter_control_msg msg,
580 const char *trace_name);
581
582 extern struct dentry *get_filter_root(void);
583
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);
587
588 void ltt_core_register(int (*function)(u8, void *));
589
590 void ltt_core_unregister(void);
591
592 void ltt_release_trace(struct kref *kref);
593 void ltt_release_transport(struct kref *kref);
594
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,
598 const char *pname);
599 extern int ltt_marker_disconnect(const char *channel, const char *mname,
600 const char *pname);
601 extern void ltt_dump_marker_state(struct ltt_trace_struct *trace);
602
603 void ltt_lock_traces(void);
604 void ltt_unlock_traces(void);
605
606 struct ltt_trace_struct *_ltt_trace_find(const char *trace_name);
607
608 #endif /* _LTT_TRACER_H */
This page took 0.041536 seconds and 5 git commands to generate.