add environment variables to set subbuffer options
[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 <ust/marker.h>
30 #include <ust/probe.h>
31 #include <ust/core.h>
32 #include "channels.h"
33 #include "tracercore.h"
34 #include "tracerconst.h"
35 #include "buffers.h"
36
37 /* Number of bytes to log with a read/write event */
38 #define LTT_LOG_RW_SIZE 32L
39
40 /* Interval (in jiffies) at which the LTT per-CPU timer fires */
41 #define LTT_PERCPU_TIMER_INTERVAL 1
42
43 #ifndef LTT_ARCH_TYPE
44 #define LTT_ARCH_TYPE LTT_ARCH_TYPE_UNDEFINED
45 #endif
46
47 #ifndef LTT_ARCH_VARIANT
48 #define LTT_ARCH_VARIANT LTT_ARCH_VARIANT_NONE
49 #endif
50
51 struct ltt_active_marker;
52
53 /* Maximum number of callbacks per marker */
54 #define LTT_NR_CALLBACKS 10
55
56 struct ltt_serialize_closure;
57 struct ltt_probe_private_data;
58
59 struct ltt_serialize_closure {
60 ltt_serialize_cb *callbacks;
61 long cb_args[LTT_NR_CALLBACKS];
62 unsigned int cb_idx;
63 };
64
65 extern size_t ltt_serialize_data(struct ust_buffer *buf, size_t buf_offset,
66 struct ltt_serialize_closure *closure,
67 void *serialize_private,
68 int *largest_align, const char *fmt, va_list *args);
69
70 struct ltt_probe_private_data {
71 struct ust_trace *trace; /*
72 * Target trace, for metadata
73 * or statedump.
74 */
75 ltt_serialize_cb serializer; /*
76 * Serialization function override.
77 */
78 void *serialize_private; /*
79 * Private data for serialization
80 * functions.
81 */
82 };
83
84 enum ltt_channels {
85 LTT_CHANNEL_METADATA,
86 LTT_CHANNEL_UST,
87 };
88
89 struct chan_info_struct {
90 const char *name;
91 unsigned int def_subbufsize;
92 unsigned int def_subbufcount;
93 };
94
95 struct ltt_active_marker {
96 struct list_head node; /* active markers list */
97 const char *channel;
98 const char *name;
99 const char *format;
100 struct ltt_available_probe *probe;
101 };
102
103 struct marker; //ust//
104 extern void ltt_vtrace(const struct marker *mdata, void *probe_data,
105 struct registers *regs, void *call_data, const char *fmt, va_list *args);
106 extern void ltt_trace(const struct marker *mdata, void *probe_data,
107 struct registers *regs, void *call_data, const char *fmt, ...);
108
109 /*
110 * Unique ID assigned to each registered probe.
111 */
112 enum marker_id {
113 MARKER_ID_SET_MARKER_ID = 0, /* Static IDs available (range 0-7) */
114 MARKER_ID_SET_MARKER_FORMAT,
115 MARKER_ID_COMPACT, /* Compact IDs (range: 8-127) */
116 MARKER_ID_DYNAMIC, /* Dynamic IDs (range: 128-65535) */
117 };
118
119 /* static ids 0-1 reserved for internal use. */
120 #define MARKER_CORE_IDS 2
121 static __inline__ enum marker_id marker_id_type(uint16_t id)
122 {
123 if (id < MARKER_CORE_IDS)
124 return (enum marker_id)id;
125 else
126 return MARKER_ID_DYNAMIC;
127 }
128
129 struct user_dbg_data {
130 unsigned long avail_size;
131 unsigned long write;
132 unsigned long read;
133 };
134
135 struct ltt_trace_ops {
136 /* First 32 bytes cache-hot cacheline */
137 void (*wakeup_channel) (struct ust_channel *channel);
138 int (*user_blocking) (struct ust_trace *trace,
139 unsigned int index, size_t data_size,
140 struct user_dbg_data *dbg);
141 /* End of first 32 bytes cacheline */
142 int (*create_dirs) (struct ust_trace *new_trace);
143 void (*remove_dirs) (struct ust_trace *new_trace);
144 int (*create_channel) (const char *trace_name,
145 struct ust_trace *trace,
146 const char *channel_name,
147 struct ust_channel *channel,
148 unsigned int subbuf_size,
149 unsigned int n_subbufs, int overwrite);
150 void (*finish_channel) (struct ust_channel *channel);
151 void (*remove_channel) (struct ust_channel *channel);
152 void (*user_errors) (struct ust_trace *trace,
153 unsigned int index, size_t data_size,
154 struct user_dbg_data *dbg, unsigned int cpu);
155 };
156
157 struct ltt_transport {
158 char *name;
159 struct module *owner;
160 struct list_head node;
161 struct ltt_trace_ops ops;
162 };
163
164 enum trace_mode { LTT_TRACE_NORMAL, LTT_TRACE_FLIGHT, LTT_TRACE_HYBRID };
165
166 #define CHANNEL_FLAG_ENABLE (1U<<0)
167 #define CHANNEL_FLAG_OVERWRITE (1U<<1)
168
169 /* Per-trace information - each trace/flight recorder represented by one */
170 struct ust_trace {
171 /* First 32 bytes cache-hot cacheline */
172 struct list_head list;
173 struct ltt_trace_ops *ops;
174 int active;
175 /* Second 32 bytes cache-hot cacheline */
176 struct ust_channel *channels;
177 unsigned int nr_channels;
178 u32 freq_scale;
179 u64 start_freq;
180 u64 start_tsc;
181 unsigned long long start_monotonic;
182 struct timeval start_time;
183 struct ltt_channel_setting *settings;
184 struct {
185 struct dentry *trace_root;
186 } dentry;
187 struct kref kref; /* Each channel has a kref of the trace struct */
188 struct ltt_transport *transport;
189 struct kref ltt_transport_kref;
190 char trace_name[NAME_MAX];
191 } ____cacheline_aligned;
192
193 /*
194 * We use asm/timex.h : cpu_khz/HZ variable in here : we might have to deal
195 * specifically with CPU frequency scaling someday, so using an interpolation
196 * between the start and end of buffer values is not flexible enough. Using an
197 * immediate frequency value permits to calculate directly the times for parts
198 * of a buffer that would be before a frequency change.
199 *
200 * Keep the natural field alignment for _each field_ within this structure if
201 * you ever add/remove a field from this header. Packed attribute is not used
202 * because gcc generates poor code on at least powerpc and mips. Don't ever
203 * let gcc add padding between the structure elements.
204 */
205 struct ltt_subbuffer_header {
206 uint64_t cycle_count_begin; /* Cycle count at subbuffer start */
207 uint64_t cycle_count_end; /* Cycle count at subbuffer end */
208 uint32_t magic_number; /*
209 * Trace magic number.
210 * contains endianness information.
211 */
212 uint8_t major_version;
213 uint8_t minor_version;
214 uint8_t arch_size; /* Architecture pointer size */
215 uint8_t alignment; /* LTT data alignment */
216 uint64_t start_time_sec; /* NTP-corrected start time */
217 uint64_t start_time_usec;
218 uint64_t start_freq; /*
219 * Frequency at trace start,
220 * used all along the trace.
221 */
222 uint32_t freq_scale; /* Frequency scaling (divisor) */
223 uint32_t data_size; /* Size of data in subbuffer */
224 uint32_t sb_size; /* Subbuffer size (including padding) */
225 uint32_t events_lost; /*
226 * Events lost in this subbuffer since
227 * the beginning of the trace.
228 * (may overflow)
229 */
230 uint32_t subbuf_corrupt; /*
231 * Corrupted (lost) subbuffers since
232 * the begginig of the trace.
233 * (may overflow)
234 */
235 uint8_t header_end[0]; /* End of header */
236 };
237
238 /**
239 * ltt_subbuffer_header_size - called on buffer-switch to a new sub-buffer
240 *
241 * Return header size without padding after the structure. Don't use packed
242 * structure because gcc generates inefficient code on some architectures
243 * (powerpc, mips..)
244 */
245 static __inline__ size_t ltt_subbuffer_header_size(void)
246 {
247 return offsetof(struct ltt_subbuffer_header, header_end);
248 }
249
250 extern size_t ltt_write_event_header_slow(struct ust_channel *channel,
251 struct ust_buffer *buf, long buf_offset,
252 u16 eID, u32 event_size,
253 u64 tsc, unsigned int rflags);
254
255
256 /*
257 * ltt_write_event_header
258 *
259 * Writes the event header to the offset (already aligned on 32-bits).
260 *
261 * @trace : trace to write to.
262 * @channel : pointer to the channel structure..
263 * @buf : buffer to write to.
264 * @buf_offset : buffer offset to write to (aligned on 32 bits).
265 * @eID : event ID
266 * @event_size : size of the event, excluding the event header.
267 * @tsc : time stamp counter.
268 * @rflags : reservation flags.
269 *
270 * returns : offset where the event data must be written.
271 */
272 static __inline__ size_t ltt_write_event_header(struct ust_channel *chan,
273 struct ust_buffer *buf, long buf_offset,
274 u16 eID, u32 event_size,
275 u64 tsc, unsigned int rflags)
276 {
277 struct ltt_event_header header;
278
279 if (unlikely(rflags))
280 goto slow_path;
281
282 header.id_time = eID << LTT_TSC_BITS;
283 header.id_time |= (u32)tsc & LTT_TSC_MASK;
284 ust_buffers_write(buf, buf_offset, &header, sizeof(header));
285 buf_offset += sizeof(header);
286
287 return buf_offset;
288
289 slow_path:
290 return ltt_write_event_header_slow(chan, buf, buf_offset,
291 eID, event_size, tsc, rflags);
292 }
293
294 /*
295 * Control channels :
296 * control/metadata
297 * control/interrupts
298 * control/...
299 *
300 * cpu channel :
301 * cpu
302 */
303
304 #define LTT_METADATA_CHANNEL "metadata_state"
305 #define LTT_UST_CHANNEL "ust"
306
307 #define LTT_FLIGHT_PREFIX "flight-"
308
309 /* Tracer properties */
310 //#define LTT_DEFAULT_SUBBUF_SIZE_LOW 134217728
311 #define LTT_DEFAULT_SUBBUF_SIZE_LOW 65536
312 //#define LTT_DEFAULT_SUBBUF_SIZE_LOW 4096
313 #define LTT_DEFAULT_N_SUBBUFS_LOW 2
314 //#define LTT_DEFAULT_SUBBUF_SIZE_MED 134217728
315 #define LTT_DEFAULT_SUBBUF_SIZE_MED 262144
316 //#define LTT_DEFAULT_SUBBUF_SIZE_MED 4096
317 #define LTT_DEFAULT_N_SUBBUFS_MED 2
318 //#define LTT_DEFAULT_SUBBUF_SIZE_HIGH 134217728
319 #define LTT_DEFAULT_SUBBUF_SIZE_HIGH 1048576
320 //#define LTT_DEFAULT_SUBBUF_SIZE_HIGH 4096
321 #define LTT_DEFAULT_N_SUBBUFS_HIGH 2
322 #define LTT_TRACER_MAGIC_NUMBER 0x00D6B7ED
323 #define LTT_TRACER_VERSION_MAJOR 2
324 #define LTT_TRACER_VERSION_MINOR 6
325
326 /**
327 * ust_write_trace_header - Write trace header
328 * @trace: Trace information
329 * @header: Memory address where the information must be written to
330 */
331 static __inline__ void ltt_write_trace_header(struct ust_trace *trace,
332 struct ltt_subbuffer_header *header)
333 {
334 header->magic_number = LTT_TRACER_MAGIC_NUMBER;
335 header->major_version = LTT_TRACER_VERSION_MAJOR;
336 header->minor_version = LTT_TRACER_VERSION_MINOR;
337 header->arch_size = sizeof(void *);
338 header->alignment = ltt_get_alignment();
339 header->start_time_sec = trace->start_time.tv_sec;
340 header->start_time_usec = trace->start_time.tv_usec;
341 header->start_freq = trace->start_freq;
342 header->freq_scale = trace->freq_scale;
343 }
344
345 static __inline__ int ust_get_cpu(void)
346 {
347 #ifndef UST_VALGRIND
348 return sched_getcpu();
349 #else
350 /* Valgrind does not support the sched_getcpu() vsyscall.
351 * It causes it to detect a segfault in the program and stop it.
352 * So if we want to check libust with valgrind, we have to refrain
353 * from using this call. TODO: it would probably be better to return
354 * other values too, to better test it.
355 */
356 return 0;
357 #endif
358 }
359
360
361 /*
362 * Size reserved for high priority events (interrupts, NMI, BH) at the end of a
363 * nearly full buffer. User space won't use this last amount of space when in
364 * blocking mode. This space also includes the event header that would be
365 * written by this user space event.
366 */
367 #define LTT_RESERVE_CRITICAL 4096
368
369 /* Register and unregister function pointers */
370
371 enum ltt_module_function {
372 LTT_FUNCTION_RUN_FILTER,
373 LTT_FUNCTION_FILTER_CONTROL,
374 LTT_FUNCTION_STATEDUMP
375 };
376
377 extern void ltt_transport_register(struct ltt_transport *transport);
378 extern void ltt_transport_unregister(struct ltt_transport *transport);
379
380 /* Exported control function */
381
382 union ltt_control_args {
383 struct {
384 enum trace_mode mode;
385 unsigned int subbuf_size_low;
386 unsigned int n_subbufs_low;
387 unsigned int subbuf_size_med;
388 unsigned int n_subbufs_med;
389 unsigned int subbuf_size_high;
390 unsigned int n_subbufs_high;
391 } new_trace;
392 };
393
394 extern int _ltt_trace_setup(const char *trace_name);
395 extern int ltt_trace_setup(const char *trace_name);
396 extern struct ust_trace *_ltt_trace_find_setup(const char *trace_name);
397 extern int ltt_trace_set_type(const char *trace_name, const char *trace_type);
398 extern int ltt_trace_set_channel_subbufsize(const char *trace_name,
399 const char *channel_name, unsigned int size);
400 extern int ltt_trace_set_channel_subbufcount(const char *trace_name,
401 const char *channel_name, unsigned int cnt);
402 extern int ltt_trace_set_channel_enable(const char *trace_name,
403 const char *channel_name, unsigned int enable);
404 extern int ltt_trace_set_channel_overwrite(const char *trace_name,
405 const char *channel_name, unsigned int overwrite);
406 extern int ltt_trace_alloc(const char *trace_name);
407 extern int ltt_trace_destroy(const char *trace_name, int drop);
408 extern int ltt_trace_start(const char *trace_name);
409 extern int ltt_trace_stop(const char *trace_name);
410
411 enum ltt_filter_control_msg {
412 LTT_FILTER_DEFAULT_ACCEPT,
413 LTT_FILTER_DEFAULT_REJECT
414 };
415
416 extern int ltt_filter_control(enum ltt_filter_control_msg msg,
417 const char *trace_name);
418
419 extern struct dentry *get_filter_root(void);
420
421 extern void ltt_write_trace_header(struct ust_trace *trace,
422 struct ltt_subbuffer_header *header);
423 extern void ltt_buffer_destroy(struct ust_channel *ltt_chan);
424
425 extern void ltt_core_register(int (*function)(u8, void *));
426
427 extern void ltt_core_unregister(void);
428
429 extern void ltt_release_trace(struct kref *kref);
430 extern void ltt_release_transport(struct kref *kref);
431
432 extern void ltt_dump_marker_state(struct ust_trace *trace);
433
434 extern void ltt_lock_traces(void);
435 extern void ltt_unlock_traces(void);
436
437 extern struct ust_trace *_ltt_trace_find(const char *trace_name);
438
439 #endif /* _LTT_TRACER_H */
This page took 0.03739 seconds and 4 git commands to generate.