Rewrite last GPL bits in relay.c and relay.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 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.
13 *
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.
18 *
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
22 */
23
24 #ifndef _LTT_TRACER_H
25 #define _LTT_TRACER_H
26
27 #include <sys/types.h>
28 #include <stdarg.h>
29 //#include "list.h"
30 #include <ust/kernelcompat.h>
31 #include "buffers.h"
32 #include "channels.h"
33 #include "tracercore.h"
34 #include <ust/marker.h>
35
36 /* Number of bytes to log with a read/write event */
37 #define LTT_LOG_RW_SIZE 32L
38
39 /* Interval (in jiffies) at which the LTT per-CPU timer fires */
40 #define LTT_PERCPU_TIMER_INTERVAL 1
41
42 #ifndef LTT_ARCH_TYPE
43 #define LTT_ARCH_TYPE LTT_ARCH_TYPE_UNDEFINED
44 #endif
45
46 #ifndef LTT_ARCH_VARIANT
47 #define LTT_ARCH_VARIANT LTT_ARCH_VARIANT_NONE
48 #endif
49
50 struct ltt_active_marker;
51
52 /* Maximum number of callbacks per marker */
53 #define LTT_NR_CALLBACKS 10
54
55 struct ltt_serialize_closure;
56 struct ltt_probe_private_data;
57
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);
63
64 struct ltt_serialize_closure {
65 ltt_serialize_cb *callbacks;
66 long cb_args[LTT_NR_CALLBACKS];
67 unsigned int cb_idx;
68 };
69
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);
74
75 struct ltt_available_probe {
76 const char *name; /* probe name */
77 const char *format;
78 marker_probe_func *probe_func;
79 ltt_serialize_cb callbacks[LTT_NR_CALLBACKS];
80 struct list_head node; /* registered probes list */
81 };
82
83 struct ltt_probe_private_data {
84 struct ltt_trace_struct *trace; /*
85 * Target trace, for metadata
86 * or statedump.
87 */
88 ltt_serialize_cb serializer; /*
89 * Serialization function override.
90 */
91 void *serialize_private; /*
92 * Private data for serialization
93 * functions.
94 */
95 };
96
97 enum ltt_channels {
98 LTT_CHANNEL_METADATA,
99 LTT_CHANNEL_UST,
100 };
101
102 struct ltt_active_marker {
103 struct list_head node; /* active markers list */
104 const char *channel;
105 const char *name;
106 const char *format;
107 struct ltt_available_probe *probe;
108 };
109
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, ...);
115
116 /*
117 * Unique ID assigned to each registered probe.
118 */
119 enum marker_id {
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) */
124 };
125
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)
129 {
130 if (id < MARKER_CORE_IDS)
131 return (enum marker_id)id;
132 else
133 return MARKER_ID_DYNAMIC;
134 }
135
136 struct user_dbg_data {
137 unsigned long avail_size;
138 unsigned long write;
139 unsigned long read;
140 };
141
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,
149 int largest_align);
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;
172
173 struct ltt_transport {
174 char *name;
175 struct module *owner;
176 struct list_head node;
177 struct ltt_trace_ops ops;
178 };
179
180 enum trace_mode { LTT_TRACE_NORMAL, LTT_TRACE_FLIGHT, LTT_TRACE_HYBRID };
181
182 #define CHANNEL_FLAG_ENABLE (1U<<0)
183 #define CHANNEL_FLAG_OVERWRITE (1U<<1)
184
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;
190 int active;
191 /* Second 32 bytes cache-hot cacheline */
192 struct ust_channel *channels;
193 unsigned int nr_channels;
194 u32 freq_scale;
195 u64 start_freq;
196 u64 start_tsc;
197 unsigned long long start_monotonic;
198 struct timeval start_time;
199 struct ltt_channel_setting *settings;
200 struct {
201 struct dentry *trace_root;
202 } dentry;
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;
208
209 /* Hardcoded event headers
210 *
211 * event header for a trace with active heartbeat : 27 bits timestamps
212 *
213 * headers are 32-bits aligned. In order to insure such alignment, a dynamic per
214 * trace alignment value must be done.
215 *
216 * Remember that the C compiler does align each member on the boundary
217 * equivalent to their own size.
218 *
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.
221 *
222 * Event headers are aligned depending on the trace alignment option.
223 *
224 * Note using C structure bitfields for cross-endianness and portability
225 * concerns.
226 */
227
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)
233
234 struct ltt_event_header {
235 u32 id_time; /* 5 bits event id (MSB); 27 bits time (LSB) */
236 };
237
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)
242
243 /*
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.
249 *
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.
254 */
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.
261 */
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.
271 */
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.
278 * (may overflow)
279 */
280 uint32_t subbuf_corrupt; /*
281 * Corrupted (lost) subbuffers since
282 * the begginig of the trace.
283 * (may overflow)
284 */
285 uint8_t header_end[0]; /* End of header */
286 };
287
288 /**
289 * ltt_subbuffer_header_size - called on buffer-switch to a new sub-buffer
290 *
291 * Return header size without padding after the structure. Don't use packed
292 * structure because gcc generates inefficient code on some architectures
293 * (powerpc, mips..)
294 */
295 static inline size_t ltt_subbuffer_header_size(void)
296 {
297 return offsetof(struct ltt_subbuffer_header, header_end);
298 }
299
300 /*
301 * ust_get_header_size
302 *
303 * Calculate alignment offset to 32-bits. This is the alignment offset of the
304 * event header.
305 *
306 * Important note :
307 * The event header must be 32-bits. The total offset calculated here :
308 *
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))
315 *
316 * The payload must itself determine its own alignment from the biggest type it
317 * contains.
318 * */
319 static inline unsigned char ust_get_header_size(
320 struct ust_channel *channel,
321 size_t offset,
322 size_t data_size,
323 size_t *before_hdr_pad,
324 unsigned int rflags)
325 {
326 size_t orig_offset = offset;
327 size_t padding;
328
329 padding = ltt_align(offset, sizeof(struct ltt_event_header));
330 offset += padding;
331 offset += sizeof(struct ltt_event_header);
332
333 switch (rflags) {
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);
340 break;
341 case LTT_RFLAG_ID_SIZE:
342 offset += sizeof(u16) + sizeof(u16);
343 if (data_size >= 0xFFFFU)
344 offset += sizeof(u32);
345 break;
346 case LTT_RFLAG_ID:
347 offset += sizeof(u16);
348 break;
349 }
350
351 *before_hdr_pad = padding;
352 return offset - orig_offset;
353 }
354
355 /*
356 * ltt_write_event_header
357 *
358 * Writes the event header to the offset (already aligned on 32-bits).
359 *
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).
364 * @eID : event ID
365 * @event_size : size of the event, excluding the event header.
366 * @tsc : time stamp counter.
367 * @rflags : reservation flags.
368 *
369 * returns : offset where the event data must be written.
370 */
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)
375 {
376 struct ltt_event_header header;
377 size_t small_size;
378
379 switch (rflags) {
380 case LTT_RFLAG_ID_SIZE_TSC:
381 header.id_time = 29 << LTT_TSC_BITS;
382 break;
383 case LTT_RFLAG_ID_SIZE:
384 header.id_time = 30 << LTT_TSC_BITS;
385 break;
386 case LTT_RFLAG_ID:
387 header.id_time = 31 << LTT_TSC_BITS;
388 break;
389 default:
390 header.id_time = eID << LTT_TSC_BITS;
391 break;
392 }
393 header.id_time |= (u32)tsc & LTT_TSC_MASK;
394 ust_buffers_write(buf, buf_offset, &header, sizeof(header));
395 buf_offset += sizeof(header);
396
397 switch (rflags) {
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);
410 }
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);
415 break;
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);
428 }
429 break;
430 case LTT_RFLAG_ID:
431 ust_buffers_write(buf, buf_offset,
432 (u16[]){ (u16)eID }, sizeof(u16));
433 buf_offset += sizeof(u16);
434 break;
435 default:
436 break;
437 }
438
439 return buf_offset;
440 }
441
442 /* Lockless LTTng */
443
444 /*
445 * ltt_reserve_slot
446 *
447 * Atomic slot reservation in a LTTng buffer. It will take care of
448 * sub-buffer switching.
449 *
450 * Parameters:
451 *
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)
460 * @cpu : cpu id
461 *
462 * Return : -ENOSPC if not enough space, else 0.
463 */
464 static inline int ltt_reserve_slot(
465 struct ltt_trace_struct *trace,
466 struct ust_channel *channel,
467 void **transport_data,
468 size_t data_size,
469 size_t *slot_size,
470 long *buf_offset,
471 u64 *tsc,
472 unsigned int *rflags,
473 int largest_align)
474 {
475 return trace->ops->reserve_slot(trace, channel, transport_data,
476 data_size, slot_size, buf_offset, tsc, rflags,
477 largest_align);
478 }
479
480
481 ///*
482 // * ltt_commit_slot
483 // *
484 // * Atomic unordered slot commit. Increments the commit count in the
485 // * specified sub-buffer, and delivers it if necessary.
486 // *
487 // * Parameters:
488 // *
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.
493 // */
494 //static inline void ltt_commit_slot(
495 // struct ltt_channel_struct *channel,
496 // void **transport_data,
497 // long buf_offset,
498 // size_t slot_size)
499 //{
500 // struct ltt_trace_struct *trace = channel->trace;
501 //
502 // trace->ops->commit_slot(channel, transport_data, buf_offset, slot_size);
503 //}
504
505 /*
506 * Control channels :
507 * control/metadata
508 * control/interrupts
509 * control/...
510 *
511 * cpu channel :
512 * cpu
513 */
514
515 #define LTT_METADATA_CHANNEL "metadata_state"
516 #define LTT_UST_CHANNEL "ust"
517
518 #define LTT_FLIGHT_PREFIX "flight-"
519
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
536
537 /*
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.
542 */
543 #define LTT_RESERVE_CRITICAL 4096
544
545 /* Register and unregister function pointers */
546
547 enum ltt_module_function {
548 LTT_FUNCTION_RUN_FILTER,
549 LTT_FUNCTION_FILTER_CONTROL,
550 LTT_FUNCTION_STATEDUMP
551 };
552
553 extern void ltt_transport_register(struct ltt_transport *transport);
554 extern void ltt_transport_unregister(struct ltt_transport *transport);
555
556 /* Exported control function */
557
558 union ltt_control_args {
559 struct {
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;
567 } new_trace;
568 };
569
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);
586
587 enum ltt_filter_control_msg {
588 LTT_FILTER_DEFAULT_ACCEPT,
589 LTT_FILTER_DEFAULT_REJECT
590 };
591
592 extern int ltt_filter_control(enum ltt_filter_control_msg msg,
593 const char *trace_name);
594
595 extern struct dentry *get_filter_root(void);
596
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);
600
601 extern void ltt_core_register(int (*function)(u8, void *));
602
603 extern void ltt_core_unregister(void);
604
605 extern void ltt_release_trace(struct kref *kref);
606 extern void ltt_release_transport(struct kref *kref);
607
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,
611 const char *pname);
612 extern int ltt_marker_disconnect(const char *channel, const char *mname,
613 const char *pname);
614 extern void ltt_dump_marker_state(struct ltt_trace_struct *trace);
615
616 extern void ltt_lock_traces(void);
617 extern void ltt_unlock_traces(void);
618
619 extern struct ltt_trace_struct *_ltt_trace_find(const char *trace_name);
620
621 #endif /* _LTT_TRACER_H */
This page took 0.055228 seconds and 4 git commands to generate.