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