Initial port of the state system to the LTTng 2.0 trace format
[lttv.git] / lttv / lttv / state.h
1 /* This file is part of the Linux Trace Toolkit viewer
2 * Copyright (C) 2003-2004 Michel Dagenais
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License Version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
16 * MA 02111-1307, USA.
17 */
18
19 #ifndef STATE_H
20 #define STATE_H
21
22 #include <glib.h>
23 #include <stdio.h>
24 #include <babeltrace/context.h>
25 #include <lttv/attribute.h>
26 /* The operating system state, kept during the trace analysis,
27 contains a subset of the real operating system state,
28 sufficient for the analysis, and possibly organized quite differently.
29
30 The state information is added to LttvTracesetContext, LttvTraceContext
31 and LttvTracefileContext objects, used by process_traceset, through
32 subtyping. The context objects already reflect the multiple tracefiles
33 (one per cpu) per trace and multiple traces per trace set. The state
34 objects defined here simply add fields to the relevant context objects.
35
36 There is no traceset specific state yet. It may eventually contains such
37 things as clock differences over time.
38
39 The trace state currently consists in a process table.
40
41 The tracefile level state relates to the associated cpu. It contains the
42 position of the current event in the tracefile (since the state depends on
43 which events have been processed) and a pointer to the current process,
44 in the process table, being run on that cpu.
45
46 For each process in the process table, various information such as exec
47 file name, pid, ppid and creation time are stored. Each process state also
48 contains an execution mode stack (e.g. irq within system call, called
49 from user mode). */
50
51 typedef struct _LttvTraceset LttvTraceset;
52
53 typedef struct _LttvTrace LttvTrace;
54
55 typedef struct _LttvTracesetPosition LttvTracesetPosition;
56
57 struct bt_context;
58
59 /* Priority of state hooks */
60 #define LTTV_PRIO_STATE 25
61
62 #define LTTV_STATE_SAVE_INTERVAL 50000
63
64
65 #define PREALLOC_NB_SYSCALLS 256
66 /*
67 * As of 2.6.38, IRQ 239 has been seen (and we have seen higher than
68 * 256 too.
69 */
70 #define PREALLOC_NB_IRQS 512
71 /* As of 2.6.38, 255 softirqs are used. */
72 #define PREALLOC_NB_SOFT_IRQS 512
73 #define PREALLOC_NB_TRAPS 256
74
75 /* Channel Quarks */
76
77 extern GQuark
78 LTT_CHANNEL_FD_STATE,
79 LTT_CHANNEL_GLOBAL_STATE,
80 LTT_CHANNEL_IRQ_STATE,
81 LTT_CHANNEL_MODULE_STATE,
82 LTT_CHANNEL_NETIF_STATE,
83 LTT_CHANNEL_SOFTIRQ_STATE,
84 LTT_CHANNEL_SWAP_STATE,
85 LTT_CHANNEL_SYSCALL_STATE,
86 LTT_CHANNEL_TASK_STATE,
87 LTT_CHANNEL_VM_STATE,
88 LTT_CHANNEL_KPROBE_STATE,
89 LTT_CHANNEL_FS,
90 LTT_CHANNEL_KERNEL,
91 LTT_CHANNEL_MM,
92 LTT_CHANNEL_USERSPACE,
93 LTT_CHANNEL_BLOCK;
94
95 /* Events Quarks */
96
97 extern GQuark
98 LTT_EVENT_SYSCALL_ENTRY,
99 LTT_EVENT_SYSCALL_EXIT,
100 LTT_EVENT_TRAP_ENTRY,
101 LTT_EVENT_TRAP_EXIT,
102 LTT_EVENT_PAGE_FAULT_ENTRY,
103 LTT_EVENT_PAGE_FAULT_EXIT,
104 LTT_EVENT_PAGE_FAULT_NOSEM_ENTRY,
105 LTT_EVENT_PAGE_FAULT_NOSEM_EXIT,
106 LTT_EVENT_IRQ_ENTRY,
107 LTT_EVENT_IRQ_EXIT,
108 LTT_EVENT_SOFT_IRQ_RAISE,
109 LTT_EVENT_SOFT_IRQ_ENTRY,
110 LTT_EVENT_SOFT_IRQ_EXIT,
111 LTT_EVENT_SCHED_SCHEDULE,
112 LTT_EVENT_SCHED_TRY_WAKEUP,
113 LTT_EVENT_PROCESS_FORK,
114 LTT_EVENT_KTHREAD_CREATE,
115 LTT_EVENT_PROCESS_EXIT,
116 LTT_EVENT_PROCESS_FREE,
117 LTT_EVENT_EXEC,
118 LTT_EVENT_PROCESS_STATE,
119 LTT_EVENT_STATEDUMP_END,
120 //LTT_EVENT_FUNCTION_ENTRY,
121 //LTT_EVENT_FUNCTION_EXIT,
122 LTT_EVENT_THREAD_BRAND,
123 LTT_EVENT_REQUEST_ISSUE,
124 LTT_EVENT_REQUEST_COMPLETE,
125 LTT_EVENT_LIST_INTERRUPT,
126 LTT_EVENT_SYS_CALL_TABLE,
127 LTT_EVENT_SOFTIRQ_VEC,
128 LTT_EVENT_KPROBE_TABLE,
129 LTT_EVENT_KPROBE,
130 LTT_EVENT_OPEN,
131 LTT_EVENT_READ,
132 LTT_EVENT_POLL_EVENT;
133
134 /* Fields Quarks */
135
136 extern GQuark
137 LTT_FIELD_SYSCALL_ID,
138 LTT_FIELD_TRAP_ID,
139 LTT_FIELD_IRQ_ID,
140 LTT_FIELD_SOFT_IRQ_ID,
141 LTT_FIELD_PREV_PID,
142 LTT_FIELD_NEXT_PID,
143 LTT_FIELD_PREV_STATE,
144 LTT_FIELD_PARENT_PID,
145 LTT_FIELD_CHILD_PID,
146 LTT_FIELD_PID,
147 LTT_FIELD_TGID,
148 LTT_FIELD_FILENAME,
149 LTT_FIELD_NAME,
150 LTT_FIELD_TYPE,
151 LTT_FIELD_MODE,
152 LTT_FIELD_SUBMODE,
153 LTT_FIELD_STATUS,
154 LTT_FIELD_THIS_FN,
155 LTT_FIELD_CALL_SITE,
156 LTT_FIELD_MINOR,
157 LTT_FIELD_MAJOR,
158 LTT_FIELD_OPERATION,
159 LTT_FIELD_ACTION,
160 LTT_FIELD_ID,
161 LTT_FIELD_ADDRESS,
162 LTT_FIELD_SYMBOL,
163 LTT_FIELD_IP,
164 LTT_FIELD_FD,
165 LTT_FIELD_STATE,
166 LTT_FIELD_CPU_ID;
167
168 typedef struct _LttvTraceState LttvTraceState;
169 typedef struct _LttvTraceStateClass LttvTraceStateClass;
170
171 typedef struct _LttvTracefileState LttvTracefileState;
172 typedef struct _LttvTracefileStateClass LttvTracefileStateClass;
173
174 void lttv_traceset_add_state_event_hooks(LttvTraceset *traceset);
175
176 void lttv_traceset_remove_state_event_hooks(LttvTraceset *traceset);
177
178 void lttv_traceset_seek_time_closest_prior_state(LttvTraceset *traceset, LttTime t);
179
180 /* The LttvProcessState structure defines the current state for each process.
181 A process can make system calls (in some rare cases nested) and receive
182 interrupts/faults. For instance, a process may issue a system call,
183 generate a page fault while reading an argument from user space, and
184 get caught by an interrupt. To represent these nested states, an
185 execution mode stack is maintained. The stack bottom is normal user mode
186 and the top of stack is the current execution mode.
187
188 The execution mode stack tells about the process status, execution mode and
189 submode (interrupt, system call or IRQ number). All these could be
190 defined as enumerations but may need extensions (e.g. new process state).
191 GQuark are thus used. They are as easy to manipulate as integers but have
192 a string associated, just like enumerations.
193
194 The execution mode is one of "user mode", "kernel thread", "system call",
195 "interrupt request", "fault". */
196
197 typedef GQuark LttvExecutionMode;
198
199 extern LttvExecutionMode
200 LTTV_STATE_USER_MODE,
201 LTTV_STATE_MAYBE_USER_MODE,
202 LTTV_STATE_SYSCALL,
203 LTTV_STATE_MAYBE_SYSCALL,
204 LTTV_STATE_TRAP,
205 LTTV_STATE_MAYBE_TRAP, /* TODO */
206 LTTV_STATE_IRQ,
207 LTTV_STATE_SOFT_IRQ,
208 LTTV_STATE_MODE_UNKNOWN;
209
210
211 /* The submode number depends on the execution mode. For user mode or kernel
212 thread, which are the normal mode (execution mode stack bottom),
213 it is set to "none". For interrupt requests, faults and system calls,
214 it is set respectively to the interrupt name (e.g. "timer"), fault name
215 (e.g. "page fault"), and system call name (e.g. "select"). */
216
217 typedef GQuark LttvExecutionSubmode;
218
219 extern LttvExecutionSubmode
220 LTTV_STATE_SUBMODE_NONE,
221 LTTV_STATE_SUBMODE_UNKNOWN;
222
223 /* The process status is one of "running", "wait-cpu" (runnable), or "wait-*"
224 where "*" describes the resource waited for (e.g. timer, process,
225 disk...). */
226
227 typedef GQuark LttvProcessStatus;
228
229 extern LttvProcessStatus
230 LTTV_STATE_UNNAMED,
231 LTTV_STATE_WAIT_FORK,
232 LTTV_STATE_WAIT_CPU,
233 LTTV_STATE_EXIT,
234 LTTV_STATE_ZOMBIE,
235 LTTV_STATE_WAIT,
236 LTTV_STATE_RUN,
237 LTTV_STATE_DEAD;
238
239 extern GQuark
240 LTTV_STATE_UNBRANDED;
241
242 typedef GQuark LttvProcessType;
243
244 extern LttvProcessType
245 LTTV_STATE_USER_THREAD,
246 LTTV_STATE_KERNEL_THREAD;
247
248 typedef GQuark LttvCPUMode;
249 extern LttvCPUMode
250 LTTV_CPU_UNKNOWN,
251 LTTV_CPU_IDLE,
252 LTTV_CPU_BUSY,
253 LTTV_CPU_IRQ,
254 LTTV_CPU_SOFT_IRQ,
255 LTTV_CPU_TRAP;
256
257 typedef GQuark LttvIRQMode;
258 extern LttvIRQMode
259 LTTV_IRQ_UNKNOWN,
260 LTTV_IRQ_IDLE,
261 LTTV_IRQ_BUSY;
262
263 typedef GQuark LttvBdevMode;
264 extern LttvBdevMode
265 LTTV_BDEV_UNKNOWN,
266 LTTV_BDEV_IDLE,
267 LTTV_BDEV_BUSY_READING,
268 LTTV_BDEV_BUSY_WRITING;
269
270 typedef struct _LttvExecutionState {
271 LttvExecutionMode t;
272 LttvExecutionSubmode n;
273 LttTime entry;
274 LttTime change;
275 LttTime cum_cpu_time;
276 LttvProcessStatus s;
277 } LttvExecutionState;
278
279 typedef struct _LttvProcessState {
280 guint pid;
281 guint tgid;
282 guint ppid;
283 LttTime creation_time;
284 LttTime insertion_time;
285 GQuark name;
286 GQuark brand;
287 GQuark pid_time;
288 GArray *execution_stack; /* Array of LttvExecutionState */
289 LttvExecutionState *state; /* Top of interrupt stack */
290 /* WARNING : each time the execution_stack size is modified, the state
291 * must be reget : g_array_set_size can have to move the array.
292 * (Mathieu) */
293 guint cpu; /* CPU where process is scheduled (being either in
294 the active or inactive runqueue)*/
295 // guint last_tracefile_index; /* index in the trace for cpu tracefile */
296 /* opened file descriptors, address map?... */
297 LttvProcessType type; /* kernel thread or user space ? */
298 guint free_events; /* 0 : none, 1 : free or exit dead, 2 : should delete */
299 GHashTable *fds; /* hash table of int (file descriptor) -> GQuark (file name) */
300 } LttvProcessState;
301
302 #define ANY_CPU 0 /* For clarity sake : a call to lttv_state_find_process for
303 a PID != 0 will search on any cpu automatically. */
304
305 LttvProcessState *lttv_state_find_process(LttvTraceState *ts, guint cpu,
306 guint pid);
307
308 LttvProcessState *lttv_state_find_process_or_create(LttvTraceState *ts,
309 guint cpu, guint pid, const LttTime *timestamp);
310
311 LttvProcessState *lttv_state_create_process(LttvTraceState *tcs,
312 LttvProcessState *parent, guint cpu, guint pid,
313 guint tgid, GQuark name, const LttTime *timestamp);
314
315 //void lttv_state_write(LttvTraceState *trace_state, LttTime t, FILE *fp);
316 //void lttv_state_write_raw(LttvTraceState *trace_state, LttTime t, FILE *fp);
317
318 typedef struct _LttvCPUState {
319 GArray *mode_stack;
320 GArray *irq_stack;
321 GArray *softirq_stack;
322 GArray *trap_stack;
323 } LttvCPUState;
324
325 typedef struct _LttvIRQState {
326 GArray *mode_stack;
327 } LttvIRQState;
328
329 typedef struct _LttvSoftIRQState {
330 guint pending; /* number of times it is pending */
331 guint running; /* number of times it is currently running (on different processors) */
332 } LttvSoftIRQState;
333
334 typedef struct _LttvTrapState {
335 guint running; /* number of times it is currently running (on different processors) */
336 } LttvTrapState;
337
338 typedef struct _LttvBdevState {
339 GArray *mode_stack;
340 } LttvBdevState;
341
342 typedef struct _LttvNameTables {
343 GQuark *syscall_names;
344 guint nb_syscalls;
345 GQuark *trap_names;
346 guint nb_traps;
347 GQuark *irq_names;
348 guint nb_irqs;
349 GQuark *soft_irq_names;
350 guint nb_soft_irqs;
351 GHashTable *kprobe_hash;
352 } LttvNameTables;
353
354 struct _LttvTraceState {
355 LttvTrace *trace; /* LttvTrace this state belongs to */
356 GHashTable *processes; /* LttvProcessState objects indexed by pid and
357 last_cpu */
358 guint nb_event, save_interval;
359 /* Block/char devices, locks, memory pages... */
360 GQuark *eventtype_names;
361 LttvNameTables *name_tables;
362 LttTime *max_time_state_recomputed_in_seek;
363 GHashTable *kprobe_hash;
364
365 /* Array of per cpu running process */
366 LttvProcessState **running_process;
367 gboolean has_precomputed_states;
368 LttvCPUState *cpu_states; /* state of each cpu */
369 /* FIXME should be a g_array to deal with resize and copy. */
370 LttvIRQState *irq_states; /* state of each irq handler */
371 /* FIXME should be a g_array to deal with resize and copy. */
372 LttvSoftIRQState *soft_irq_states; /* state of each softirq */
373 /* FIXME should be a g_array to deal with resize and copy. */
374 LttvTrapState *trap_states; /* state of each trap */
375 GHashTable *bdev_states; /* state of the block devices */
376 };
377
378 void lttv_trace_state_init(LttvTraceState *self, LttvTrace *trace);
379 void lttv_trace_state_fini(LttvTraceState *self);
380
381 void lttv_state_save(LttvTraceState *self, LttvAttribute *container);
382 void lttv_state_restore(LttvTraceState *self, LttvAttribute *container);
383 LttvTracesetPosition *lttv_trace_state_get_position(LttvAttribute *container);
384 void lttv_state_saved_free(LttvTraceState *self, LttvAttribute *container);
385
386 //TODO ybrosseau Need to export that cleanly
387 //int lttv_state_pop_state_cleanup(LttvProcessState *process,
388 // LttvEvent *event);
389
390 #define HDR_PROCESS 0
391 #define HDR_ES 1
392 #define HDR_USER_STACK 2
393 //#define HDR_USERTRACE 3
394 #define HDR_PROCESS_STATE 4
395 #define HDR_CPU 5
396 #define HDR_TRACEFILE 6
397 #define HDR_TRACESET 7
398 #define HDR_TRACE 8
399 #define HDR_QUARKS 9
400 #define HDR_QUARK 10
401
402 /* Device number manipulation macros from kernel source */
403 #define MINORBITS 20
404 #define MINORMASK ((1U << MINORBITS) - 1)
405 #define MAJOR(dev) ((unsigned int) ((dev) >> MINORBITS))
406 #define MINOR(dev) ((unsigned int) ((dev) & MINORMASK))
407 #define MKDEV(ma, mi) ((((unsigned int) (ma)) << MINORBITS) | (unsigned int) (mi))
408
409 #endif // STATE_H
This page took 0.038 seconds and 4 git commands to generate.