Update nohz
[lttng-modules.git] / ltt-tracer.h
CommitLineData
1c8284eb
MD
1/*
2 * Copyright (C) 2005,2006,2008 Mathieu Desnoyers (mathieu.desnoyers@polymtl.ca)
3 *
4 * This contains the definitions for the Linux Trace Toolkit tracer.
5 *
6 * Dual LGPL v2.1/GPL v2 license.
7 */
8
9#ifndef _LTT_TRACER_H
10#define _LTT_TRACER_H
11
12#include <stdarg.h>
13#include <linux/types.h>
14#include <linux/limits.h>
15#include <linux/list.h>
16#include <linux/cache.h>
1c8284eb
MD
17#include <linux/timex.h>
18#include <linux/wait.h>
1c8284eb
MD
19#include <asm/atomic.h>
20#include <asm/local.h>
21
f3bc08c5 22#include "wrapper/trace-clock.h"
1c8284eb 23#include "ltt-tracer-core.h"
1c25284c 24#include "ltt-events.h"
1c8284eb
MD
25
26/* Number of bytes to log with a read/write event */
27#define LTT_LOG_RW_SIZE 32L
28
1c8284eb
MD
29/* Maximum number of callbacks per marker */
30#define LTT_NR_CALLBACKS 10
31
11b5a3c2
MD
32struct ltt_serialize_closure;
33
34/* Serialization callback */
35typedef size_t (*ltt_serialize_cb)(struct lib_ring_buffer *buf,
36 size_t buf_offset,
37 struct ltt_serialize_closure *closure,
38 void *serialize_private,
39 unsigned int stack_pos_ctx,
40 int *largest_align,
41 const char *fmt, va_list *args);
42
1c8284eb
MD
43struct ltt_serialize_closure {
44 ltt_serialize_cb *callbacks;
45 long cb_args[LTT_NR_CALLBACKS];
46 unsigned int cb_idx;
47};
48
11b5a3c2 49size_t ltt_serialize_data(struct lib_ring_buffer *buf, size_t buf_offset,
1c8284eb
MD
50 struct ltt_serialize_closure *closure,
51 void *serialize_private, unsigned int stack_pos_ctx,
52 int *largest_align, const char *fmt, va_list *args);
53
1c8284eb
MD
54enum ltt_channels {
55 LTT_CHANNEL_METADATA,
56 LTT_CHANNEL_FD_STATE,
57 LTT_CHANNEL_GLOBAL_STATE,
58 LTT_CHANNEL_IRQ_STATE,
59 LTT_CHANNEL_MODULE_STATE,
60 LTT_CHANNEL_NETIF_STATE,
61 LTT_CHANNEL_SOFTIRQ_STATE,
62 LTT_CHANNEL_SWAP_STATE,
63 LTT_CHANNEL_SYSCALL_STATE,
64 LTT_CHANNEL_TASK_STATE,
65 LTT_CHANNEL_VM_STATE,
66 LTT_CHANNEL_FS,
67 LTT_CHANNEL_INPUT,
68 LTT_CHANNEL_IPC,
69 LTT_CHANNEL_KERNEL,
70 LTT_CHANNEL_MM,
71 LTT_CHANNEL_RCU,
72 LTT_CHANNEL_DEFAULT,
73};
74
7514523f
MD
75/*
76 * Hardcoded event headers
1c8284eb
MD
77 *
78 * event header for a trace with active heartbeat : 27 bits timestamps
79 *
80 * headers are 32-bits aligned. In order to insure such alignment, a dynamic per
81 * trace alignment value must be done.
82 *
83 * Remember that the C compiler does align each member on the boundary
84 * equivalent to their own size.
85 *
86 * As relay subbuffers are aligned on pages, we are sure that they are 4 and 8
87 * bytes aligned, so the buffer header and trace header are aligned.
88 *
89 * Event headers are aligned depending on the trace alignment option.
90 *
91 * Note using C structure bitfields for cross-endianness and portability
92 * concerns.
93 */
94
95#define LTT_RESERVED_EVENTS 3
96#define LTT_EVENT_BITS 5
97#define LTT_FREE_EVENTS ((1 << LTT_EVENT_BITS) - LTT_RESERVED_EVENTS)
98#define LTT_TSC_BITS 27
99#define LTT_TSC_MASK ((1 << LTT_TSC_BITS) - 1)
100
7514523f 101struct event_header {
1c8284eb
MD
102 u32 id_time; /* 5 bits event id (MSB); 27 bits time (LSB) */
103};
104
105/* Reservation flags */
106#define LTT_RFLAG_ID (1 << 0)
107#define LTT_RFLAG_ID_SIZE (1 << 1)
108#define LTT_RFLAG_ID_SIZE_TSC (1 << 2)
109
110#define LTT_MAX_SMALL_SIZE 0xFFFFU
111
112/*
113 * We use asm/timex.h : cpu_khz/HZ variable in here : we might have to deal
114 * specifically with CPU frequency scaling someday, so using an interpolation
115 * between the start and end of buffer values is not flexible enough. Using an
116 * immediate frequency value permits to calculate directly the times for parts
117 * of a buffer that would be before a frequency change.
118 *
119 * Keep the natural field alignment for _each field_ within this structure if
120 * you ever add/remove a field from this header. Packed attribute is not used
121 * because gcc generates poor code on at least powerpc and mips. Don't ever
122 * let gcc add padding between the structure elements.
123 */
1c25284c
MD
124struct packet_header {
125 uint32_t magic; /*
1c8284eb
MD
126 * Trace magic number.
127 * contains endianness information.
128 */
1c25284c
MD
129 uint8_t trace_uuid[16];
130 uint32_t stream_id;
131 uint64_t timestamp_begin; /* Cycle count at subbuffer start */
132 uint64_t timestamp_end; /* Cycle count at subbuffer end */
133 uint32_t content_size; /* Size of data in subbuffer */
134 uint32_t packet_size; /* Subbuffer size (include padding) */
135 uint32_t events_lost; /*
136 * Events lost in this subbuffer since
137 * the beginning of the trace.
138 * (may overflow)
139 */
140 /* TODO: move to metadata */
141#if 0
1c8284eb
MD
142 uint8_t major_version;
143 uint8_t minor_version;
144 uint8_t arch_size; /* Architecture pointer size */
145 uint8_t alignment; /* LTT data alignment */
146 uint64_t start_time_sec; /* NTP-corrected start time */
147 uint64_t start_time_usec;
148 uint64_t start_freq; /*
149 * Frequency at trace start,
150 * used all along the trace.
151 */
152 uint32_t freq_scale; /* Frequency scaling (divisor) */
1c25284c 153#endif //0
1c8284eb
MD
154 uint8_t header_end[0]; /* End of header */
155};
156
7514523f 157static inline notrace u64 lib_ring_buffer_clock_read(struct channel *chan)
1c8284eb 158{
7514523f 159 return trace_clock_read64();
1c8284eb
MD
160}
161
162/*
7514523f
MD
163 * record_header_size - Calculate the header size and padding necessary.
164 * @config: ring buffer instance configuration
165 * @chan: channel
166 * @offset: offset in the write buffer
167 * @data_size: size of the payload
168 * @pre_header_padding: padding to add before the header (output)
169 * @rflags: reservation flags
170 * @ctx: reservation context
1c8284eb 171 *
7514523f 172 * Returns the event header size (including padding).
1c8284eb
MD
173 *
174 * Important note :
175 * The event header must be 32-bits. The total offset calculated here :
176 *
177 * Alignment of header struct on 32 bits (min arch size, header size)
178 * + sizeof(header struct) (32-bits)
179 * + (opt) u16 (ext. event id)
180 * + (opt) u16 (event_size)
181 * (if event_size == LTT_MAX_SMALL_SIZE, has ext. event size)
182 * + (opt) u32 (ext. event size)
183 * + (opt) u64 full TSC (aligned on min(64-bits, arch size))
184 *
185 * The payload must itself determine its own alignment from the biggest type it
186 * contains.
7514523f 187 */
1c8284eb 188static __inline__
7514523f
MD
189unsigned char record_header_size(const struct lib_ring_buffer_config *config,
190 struct channel *chan, size_t offset,
191 size_t data_size, size_t *pre_header_padding,
192 unsigned int rflags,
193 struct lib_ring_buffer_ctx *ctx)
1c8284eb
MD
194{
195 size_t orig_offset = offset;
196 size_t padding;
197
7514523f 198 BUILD_BUG_ON(sizeof(struct event_header) != sizeof(u32));
1c8284eb 199
6db3d13b 200 padding = lib_ring_buffer_align(offset,
7514523f 201 sizeof(struct event_header));
1c8284eb 202 offset += padding;
7514523f 203 offset += sizeof(struct event_header);
1c8284eb
MD
204
205 if (unlikely(rflags)) {
206 switch (rflags) {
207 case LTT_RFLAG_ID_SIZE_TSC:
208 offset += sizeof(u16) + sizeof(u16);
209 if (data_size >= LTT_MAX_SMALL_SIZE)
210 offset += sizeof(u32);
9c5653d5 211 offset += lib_ring_buffer_align(offset, sizeof(u64));
1c8284eb
MD
212 offset += sizeof(u64);
213 break;
214 case LTT_RFLAG_ID_SIZE:
215 offset += sizeof(u16) + sizeof(u16);
216 if (data_size >= LTT_MAX_SMALL_SIZE)
217 offset += sizeof(u32);
218 break;
219 case LTT_RFLAG_ID:
220 offset += sizeof(u16);
221 break;
222 }
223 }
224
7514523f 225 *pre_header_padding = padding;
1c8284eb
MD
226 return offset - orig_offset;
227}
228
f3bc08c5 229#include "wrapper/ringbuffer/api.h"
7514523f 230
1c8284eb 231extern
1c25284c
MD
232void ltt_write_event_header_slow(const struct lib_ring_buffer_config *config,
233 struct lib_ring_buffer_ctx *ctx,
234 u16 eID, u32 event_size);
1c8284eb
MD
235
236/*
237 * ltt_write_event_header
238 *
239 * Writes the event header to the offset (already aligned on 32-bits).
240 *
7514523f
MD
241 * @config: ring buffer instance configuration
242 * @ctx: reservation context
1c8284eb
MD
243 * @eID : event ID
244 * @event_size : size of the event, excluding the event header.
1c8284eb
MD
245 */
246static __inline__
7514523f
MD
247void ltt_write_event_header(const struct lib_ring_buffer_config *config,
248 struct lib_ring_buffer_ctx *ctx,
249 u16 eID, u32 event_size)
1c8284eb 250{
7514523f 251 struct event_header header;
1c8284eb 252
7514523f 253 if (unlikely(ctx->rflags))
1c8284eb
MD
254 goto slow_path;
255
256 header.id_time = eID << LTT_TSC_BITS;
7514523f
MD
257 header.id_time |= (u32)ctx->tsc & LTT_TSC_MASK;
258 lib_ring_buffer_write(config, ctx, &header, sizeof(header));
1c8284eb
MD
259
260slow_path:
7514523f 261 ltt_write_event_header_slow(config, ctx, eID, event_size);
1c8284eb
MD
262}
263
1c8284eb 264/* Tracer properties */
1c25284c
MD
265#define CTF_MAGIC_NUMBER 0xC1FC1FC1
266#define LTT_TRACER_VERSION_MAJOR 3
267#define LTT_TRACER_VERSION_MINOR 0
1c8284eb
MD
268
269/**
270 * ltt_write_trace_header - Write trace header
7514523f 271 * @priv: Private data (struct trace)
1c8284eb
MD
272 * @header: Memory address where the information must be written to
273 */
274static __inline__
1c25284c
MD
275void write_trace_header(const struct lib_ring_buffer_config *config,
276 struct packet_header *header)
1c8284eb 277{
1c25284c
MD
278 header->magic = CTF_MAGIC_NUMBER;
279#if 0
280 /* TODO: move start time to metadata */
1c8284eb
MD
281 header->major_version = LTT_TRACER_VERSION_MAJOR;
282 header->minor_version = LTT_TRACER_VERSION_MINOR;
283 header->arch_size = sizeof(void *);
1c25284c
MD
284 header->alignment = lib_ring_buffer_get_alignment(config);
285 header->start_time_sec = ltt_chan->session->start_time.tv_sec;
286 header->start_time_usec = ltt_chan->session->start_time.tv_usec;
287 header->start_freq = ltt_chan->session->start_freq;
288 header->freq_scale = ltt_chan->session->freq_scale;
289#endif //0
1c8284eb
MD
290}
291
292/*
293 * Size reserved for high priority events (interrupts, NMI, BH) at the end of a
294 * nearly full buffer. User space won't use this last amount of space when in
295 * blocking mode. This space also includes the event header that would be
296 * written by this user space event.
297 */
298#define LTT_RESERVE_CRITICAL 4096
299
300/* Register and unregister function pointers */
301
302enum ltt_module_function {
303 LTT_FUNCTION_RUN_FILTER,
304 LTT_FUNCTION_FILTER_CONTROL,
305 LTT_FUNCTION_STATEDUMP
306};
307
308extern int ltt_module_register(enum ltt_module_function name, void *function,
309 struct module *owner);
310extern void ltt_module_unregister(enum ltt_module_function name);
311
1c8284eb
MD
312/* Exported control function */
313
1c8284eb
MD
314void ltt_core_register(int (*function)(u8, void *));
315
316void ltt_core_unregister(void);
317
1c8284eb
MD
318extern
319void ltt_statedump_register_kprobes_dump(void (*callback)(void *call_data));
320extern
321void ltt_statedump_unregister_kprobes_dump(void (*callback)(void *call_data));
322
323extern void ltt_dump_softirq_vec(void *call_data);
324
f277b4c1 325#ifdef CONFIG_HAVE_LTT_DUMP_TABLES
1c8284eb
MD
326extern void ltt_dump_sys_call_table(void *call_data);
327extern void ltt_dump_idt_table(void *call_data);
f277b4c1
MD
328#else
329static inline void ltt_dump_sys_call_table(void *call_data)
330{
331}
332
333static inline void ltt_dump_idt_table(void *call_data)
334{
335}
336#endif
1c8284eb 337
1c8284eb 338#endif /* _LTT_TRACER_H */
This page took 0.036634 seconds and 4 git commands to generate.