1 /* This file is part of the Linux Trace Toolkit viewer
2 * Copyright (C) 2003-2004 Michel Dagenais
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;
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.
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,
23 #include <lttv/tracecontext.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.
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.
36 There is no traceset specific state yet. It may eventually contains such
37 things as clock differences over time.
39 The trace state currently consists in a process table.
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.
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
51 /* Priority of state hooks */
52 #define LTTV_PRIO_STATE 25
54 #define LTTV_STATE_SAVE_INTERVAL 50000
57 #define PREALLOC_NB_SYSCALLS 256
59 * As of 2.6.38, IRQ 239 has been seen (and we have seen higher than
62 #define PREALLOC_NB_IRQS 512
63 /* As of 2.6.38, 255 softirqs are used. */
64 #define PREALLOC_NB_SOFT_IRQS 512
65 #define PREALLOC_NB_TRAPS 256
71 LTT_CHANNEL_GLOBAL_STATE
,
72 LTT_CHANNEL_IRQ_STATE
,
73 LTT_CHANNEL_MODULE_STATE
,
74 LTT_CHANNEL_NETIF_STATE
,
75 LTT_CHANNEL_SOFTIRQ_STATE
,
76 LTT_CHANNEL_SWAP_STATE
,
77 LTT_CHANNEL_SYSCALL_STATE
,
78 LTT_CHANNEL_TASK_STATE
,
80 LTT_CHANNEL_KPROBE_STATE
,
84 LTT_CHANNEL_USERSPACE
,
90 LTT_EVENT_SYSCALL_ENTRY
,
91 LTT_EVENT_SYSCALL_EXIT
,
94 LTT_EVENT_PAGE_FAULT_ENTRY
,
95 LTT_EVENT_PAGE_FAULT_EXIT
,
96 LTT_EVENT_PAGE_FAULT_NOSEM_ENTRY
,
97 LTT_EVENT_PAGE_FAULT_NOSEM_EXIT
,
100 LTT_EVENT_SOFT_IRQ_RAISE
,
101 LTT_EVENT_SOFT_IRQ_ENTRY
,
102 LTT_EVENT_SOFT_IRQ_EXIT
,
103 LTT_EVENT_SCHED_SCHEDULE
,
104 LTT_EVENT_SCHED_TRY_WAKEUP
,
105 LTT_EVENT_PROCESS_FORK
,
106 LTT_EVENT_KTHREAD_CREATE
,
107 LTT_EVENT_PROCESS_EXIT
,
108 LTT_EVENT_PROCESS_FREE
,
110 LTT_EVENT_PROCESS_STATE
,
111 LTT_EVENT_STATEDUMP_END
,
112 LTT_EVENT_FUNCTION_ENTRY
,
113 LTT_EVENT_FUNCTION_EXIT
,
114 LTT_EVENT_THREAD_BRAND
,
115 LTT_EVENT_REQUEST_ISSUE
,
116 LTT_EVENT_REQUEST_COMPLETE
,
117 LTT_EVENT_LIST_INTERRUPT
,
118 LTT_EVENT_SYS_CALL_TABLE
,
119 LTT_EVENT_SOFTIRQ_VEC
,
120 LTT_EVENT_KPROBE_TABLE
,
124 LTT_EVENT_POLL_EVENT
;
129 LTT_FIELD_SYSCALL_ID
,
132 LTT_FIELD_SOFT_IRQ_ID
,
135 LTT_FIELD_PREV_STATE
,
136 LTT_FIELD_PARENT_PID
,
160 typedef struct _LttvTracesetState LttvTracesetState
;
161 typedef struct _LttvTracesetStateClass LttvTracesetStateClass
;
163 typedef struct _LttvTraceState LttvTraceState
;
164 typedef struct _LttvTraceStateClass LttvTraceStateClass
;
166 typedef struct _LttvTracefileState LttvTracefileState
;
167 typedef struct _LttvTracefileStateClass LttvTracefileStateClass
;
169 gint
lttv_state_hook_add_event_hooks(void *hook_data
, void *call_data
);
170 void lttv_state_add_event_hooks(LttvTracesetState
*self
);
172 gint
lttv_state_hook_remove_event_hooks(void *hook_data
, void *call_data
);
173 void lttv_state_remove_event_hooks(LttvTracesetState
*self
);
175 void lttv_state_save_add_event_hooks(LttvTracesetState
*self
);
176 // Hook wrapper. call_data is a trace context.
177 gint
lttv_state_save_hook_add_event_hooks(void *hook_data
, void *call_data
);
179 void lttv_state_save_remove_event_hooks(LttvTracesetState
*self
);
180 // Hook wrapper. call_data is a trace context.
181 gint
lttv_state_save_hook_remove_event_hooks(void *hook_data
, void *call_data
);
183 void lttv_state_traceset_seek_time_closest(LttvTracesetState
*self
, LttTime t
);
185 /* The LttvProcessState structure defines the current state for each process.
186 A process can make system calls (in some rare cases nested) and receive
187 interrupts/faults. For instance, a process may issue a system call,
188 generate a page fault while reading an argument from user space, and
189 get caught by an interrupt. To represent these nested states, an
190 execution mode stack is maintained. The stack bottom is normal user mode
191 and the top of stack is the current execution mode.
193 The execution mode stack tells about the process status, execution mode and
194 submode (interrupt, system call or IRQ number). All these could be
195 defined as enumerations but may need extensions (e.g. new process state).
196 GQuark are thus used. They are as easy to manipulate as integers but have
197 a string associated, just like enumerations.
199 The execution mode is one of "user mode", "kernel thread", "system call",
200 "interrupt request", "fault". */
202 typedef GQuark LttvExecutionMode
;
204 extern LttvExecutionMode
205 LTTV_STATE_USER_MODE
,
206 LTTV_STATE_MAYBE_USER_MODE
,
208 LTTV_STATE_MAYBE_SYSCALL
,
210 LTTV_STATE_MAYBE_TRAP
, /* TODO */
213 LTTV_STATE_MODE_UNKNOWN
;
216 /* The submode number depends on the execution mode. For user mode or kernel
217 thread, which are the normal mode (execution mode stack bottom),
218 it is set to "none". For interrupt requests, faults and system calls,
219 it is set respectively to the interrupt name (e.g. "timer"), fault name
220 (e.g. "page fault"), and system call name (e.g. "select"). */
222 typedef GQuark LttvExecutionSubmode
;
224 extern LttvExecutionSubmode
225 LTTV_STATE_SUBMODE_NONE
,
226 LTTV_STATE_SUBMODE_UNKNOWN
;
228 /* The process status is one of "running", "wait-cpu" (runnable), or "wait-*"
229 where "*" describes the resource waited for (e.g. timer, process,
232 typedef GQuark LttvProcessStatus
;
234 extern LttvProcessStatus
236 LTTV_STATE_WAIT_FORK
,
245 LTTV_STATE_UNBRANDED
;
247 typedef GQuark LttvProcessType
;
249 extern LttvProcessType
250 LTTV_STATE_USER_THREAD
,
251 LTTV_STATE_KERNEL_THREAD
;
253 typedef GQuark LttvCPUMode
;
262 typedef GQuark LttvIRQMode
;
268 typedef GQuark LttvBdevMode
;
272 LTTV_BDEV_BUSY_READING
,
273 LTTV_BDEV_BUSY_WRITING
;
275 typedef struct _LttvExecutionState
{
277 LttvExecutionSubmode n
;
280 LttTime cum_cpu_time
;
282 } LttvExecutionState
;
284 typedef struct _LttvProcessState
{
288 LttTime creation_time
;
289 LttTime insertion_time
;
293 GArray
*execution_stack
; /* Array of LttvExecutionState */
294 LttvExecutionState
*state
; /* Top of interrupt stack */
295 /* WARNING : each time the execution_stack size is modified, the state
296 * must be reget : g_array_set_size can have to move the array.
298 guint cpu
; /* CPU where process is scheduled (being either in
299 the active or inactive runqueue)*/
300 // guint last_tracefile_index; /* index in the trace for cpu tracefile */
301 LttvTracefileState
*usertrace
; /* Associated usertrace */
302 /* opened file descriptors, address map?... */
303 GArray
*user_stack
; /* User space function call stack */
304 guint64 current_function
;
305 LttvProcessType type
; /* kernel thread or user space ? */
306 guint target_pid
; /* target PID of the current event. */
307 guint free_events
; /* 0 : none, 1 : free or exit dead, 2 : should delete */
308 GHashTable
*fds
; /* hash table of int (file descriptor) -> GQuark (file name) */
311 #define ANY_CPU 0 /* For clarity sake : a call to lttv_state_find_process for
312 a PID != 0 will search on any cpu automatically. */
314 LttvProcessState
*lttv_state_find_process(LttvTraceState
*ts
, guint cpu
,
317 LttvProcessState
*lttv_state_find_process_or_create(LttvTraceState
*ts
,
318 guint cpu
, guint pid
, const LttTime
*timestamp
);
320 LttvProcessState
*lttv_state_create_process(LttvTraceState
*tcs
,
321 LttvProcessState
*parent
, guint cpu
, guint pid
,
322 guint tgid
, GQuark name
, const LttTime
*timestamp
);
324 void lttv_state_write(LttvTraceState
*self
, LttTime t
, FILE *fp
);
325 void lttv_state_write_raw(LttvTraceState
*self
, LttTime t
, FILE *fp
);
327 /* The LttvTracesetState, LttvTraceState and LttvTracefileState types
328 inherit from the corresponding Context objects defined in processTrace. */
330 #define LTTV_TRACESET_STATE_TYPE (lttv_traceset_state_get_type ())
331 #define LTTV_TRACESET_STATE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), LTTV_TRACESET_STATE_TYPE, LttvTracesetState))
332 #define LTTV_TRACESET_STATE_CLASS(vtable) (G_TYPE_CHECK_CLASS_CAST ((vtable), LTTV_TRACESET_STATE_TYPE, LttvTracesetStateClass))
333 #define LTTV_IS_TRACESET_STATE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), LTTV_TRACESET_STATE_TYPE))
334 #define LTTV_IS_TRACESET_STATE_CLASS(vtable) (G_TYPE_CHECK_CLASS_TYPE ((vtable), LTTV_TRACESET_STATE_TYPE))
335 #define LTTV_TRACESET_STATE_GET_CLASS(inst) (G_TYPE_INSTANCE_GET_CLASS ((inst), LTTV_TRACESET_STATE_TYPE, LttvTracesetStateClass))
337 struct _LttvTracesetState
{
338 LttvTracesetContext parent
;
341 struct _LttvTracesetStateClass
{
342 LttvTracesetContextClass parent
;
345 GType
lttv_traceset_state_get_type (void);
348 #define LTTV_TRACE_STATE_TYPE (lttv_trace_state_get_type ())
349 #define LTTV_TRACE_STATE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), LTTV_TRACE_STATE_TYPE, LttvTraceState))
350 #define LTTV_TRACE_STATE_CLASS(vtable) (G_TYPE_CHECK_CLASS_CAST ((vtable), LTTV_TRACE_STATE_TYPE, LttvTraceStateClass))
351 #define LTTV_IS_TRACE_STATE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), LTTV_TRACE_STATE_TYPE))
352 #define LTTV_IS_TRACE_STATE_CLASS(vtable) (G_TYPE_CHECK_CLASS_TYPE ((vtable), LTTV_TRACE_STATE_TYPE))
353 #define LTTV_TRACE_STATE_GET_CLASS(inst) (G_TYPE_INSTANCE_GET_CLASS ((inst), LTTV_TRACE_STATE_TYPE, LttvTraceStateClass))
355 typedef struct _LttvCPUState
{
358 GArray
*softirq_stack
;
362 typedef struct _LttvIRQState
{
366 typedef struct _LttvSoftIRQState
{
367 guint pending
; /* number of times it is pending */
368 guint running
; /* number of times it is currently running (on different processors) */
371 typedef struct _LttvTrapState
{
372 guint running
; /* number of times it is currently running (on different processors) */
375 typedef struct _LttvBdevState
{
379 typedef struct _LttvNameTables
{
380 GQuark
*syscall_names
;
386 GQuark
*soft_irq_names
;
388 GHashTable
*kprobe_hash
;
391 struct _LttvTraceState
{
392 LttvTraceContext parent
;
394 GHashTable
*processes
; /* LttvProcessState objects indexed by pid and
396 GHashTable
*usertraces
; /* GPtrArray objects indexed by pid, containing
397 pointers to LttvTracefileState objects. */
398 guint nb_event
, save_interval
;
399 /* Block/char devices, locks, memory pages... */
400 GQuark
*eventtype_names
;
401 LttvNameTables
*name_tables
;
402 LttTime
*max_time_state_recomputed_in_seek
;
403 GHashTable
*kprobe_hash
;
405 /* Array of per cpu running process */
406 LttvProcessState
**running_process
;
407 gboolean has_precomputed_states
;
408 LttvCPUState
*cpu_states
; /* state of each cpu */
409 /* FIXME should be a g_array to deal with resize and copy. */
410 LttvIRQState
*irq_states
; /* state of each irq handler */
411 /* FIXME should be a g_array to deal with resize and copy. */
412 LttvSoftIRQState
*soft_irq_states
; /* state of each softirq */
413 /* FIXME should be a g_array to deal with resize and copy. */
414 LttvTrapState
*trap_states
; /* state of each trap */
415 GHashTable
*bdev_states
; /* state of the block devices */
418 struct _LttvTraceStateClass
{
419 LttvTraceContextClass parent
;
421 void (*state_save
) (LttvTraceState
*self
, LttvAttribute
*container
);
422 void (*state_restore
) (LttvTraceState
*self
, LttvAttribute
*container
);
423 void (*state_saved_free
) (LttvTraceState
*self
, LttvAttribute
*container
);
426 GType
lttv_trace_state_get_type (void);
428 void lttv_state_save(LttvTraceState
*self
, LttvAttribute
*container
);
430 void lttv_state_restore(LttvTraceState
*self
, LttvAttribute
*container
);
432 void lttv_state_state_saved_free(LttvTraceState
*self
,
433 LttvAttribute
*container
);
435 int lttv_state_pop_state_cleanup(LttvProcessState
*process
,
436 LttvTracefileState
*tfs
);
438 #define LTTV_TRACEFILE_STATE_TYPE (lttv_tracefile_state_get_type ())
439 #define LTTV_TRACEFILE_STATE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), LTTV_TRACEFILE_STATE_TYPE, LttvTracefileState))
440 #define LTTV_TRACEFILE_STATE_CLASS(vtable) (G_TYPE_CHECK_CLASS_CAST ((vtable), LTTV_TRACEFILE_STATE_TYPE, LttvTracefileStateClass))
441 #define LTTV_IS_TRACEFILE_STATE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), LTTV_TRACEFILE_STATE_TYPE))
442 #define LTTV_IS_TRACEFILE_STATE_CLASS(vtable) (G_TYPE_CHECK_CLASS_TYPE ((vtable), LTTV_TRACEFILE_STATE_TYPE))
443 #define LTTV_TRACEFILE_STATE_GET_CLASS(inst) (G_TYPE_INSTANCE_GET_CLASS ((inst), LTTV_TRACEFILE_STATE_TYPE, LttvTracefileStateClass))
445 struct _LttvTracefileState
{
446 LttvTracefileContext parent
;
448 GQuark tracefile_name
;
449 guint cpu
; /* Current cpu of the tracefile */ /* perhaps merge in cpu_state */
450 LttvCPUState
*cpu_state
; /* cpu resource state */
453 struct _LttvTracefileStateClass
{
454 LttvTracefileContextClass parent
;
457 GType
lttv_tracefile_state_get_type (void);
459 static inline guint
lttv_state_get_target_pid(LttvTracefileState
*tfs
)
461 LttvTraceState
*ts
= (LttvTraceState
*)tfs
->parent
.t_context
;
462 guint cpu
= tfs
->cpu
;
463 LttvProcessState
*process
= ts
->running_process
[cpu
];
465 if(tfs
->parent
.target_pid
>= 0) return tfs
->parent
.target_pid
;
466 else return process
->pid
;
470 #define HDR_PROCESS 0
472 #define HDR_USER_STACK 2
473 #define HDR_USERTRACE 3
474 #define HDR_PROCESS_STATE 4
476 #define HDR_TRACEFILE 6
477 #define HDR_TRACESET 7
482 /* Device number manipulation macros from kernel source */
484 #define MINORMASK ((1U << MINORBITS) - 1)
485 #define MAJOR(dev) ((unsigned int) ((dev) >> MINORBITS))
486 #define MINOR(dev) ((unsigned int) ((dev) & MINORMASK))
487 #define MKDEV(ma, mi) ((((unsigned int) (ma)) << MINORBITS) | (unsigned int) (mi))