1 /* This file is part of the Linux Trace Toolkit viewer
2 * Copyright (C) 2008 Pierre-Marc Fournier
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,
25 #include <lttv/lttv.h>
26 #include <lttv/option.h>
27 #include <lttv/module.h>
28 #include <lttv/hook.h>
29 #include <lttv/attribute.h>
30 #include <lttv/iattribute.h>
31 #include <lttv/stats.h>
32 #include <lttv/filter.h>
33 #include <lttv/print.h>
35 #include <ltt/event.h>
36 #include <ltt/trace.h>
48 static int depanalysis_range_pid
= -1;
49 static int depanalysis_range_pid_searching
= -1;
50 static int depanalysis_use_time
=0;
51 static int depanalysis_event_limit
= -1;
52 static int a_print_simple_summary
= 0;
53 static LttTime depanalysis_time1
, depanalysis_time2
;
54 static char *arg_t1_str
,*arg_t2_str
;
55 static int statedump_finished
= 0;
58 struct llev_state_info_irq
{
62 struct llev_state_info_softirq
{
66 struct llev_state_info_syscall
{
74 struct llev_state_info_syscall__open
{
78 struct llev_state_info_syscall__read
{
82 struct llev_state_info_syscall__poll
{
86 struct llev_state_info_preempted
{
90 struct hlev_state_info_blocked
{
92 unsigned char trap
; /* flag */
95 /* Garray of pointers to struct process_state that reflect the
96 * low-level state stack when respectively entering and exiting the blocked
99 GArray
*llev_state_entry
;
100 GArray
*llev_state_exit
;
102 int pid_exit
; /* FIXME: it's not pretty to have this here; find this info elsewhere */
108 struct hlev_state_info_blocked__open
{
112 struct hlev_state_info_blocked__read
{
116 struct hlev_state_info_blocked__poll
{
120 struct hlev_state_info_interrupted_irq
{
124 struct hlev_state_info_interrupted_softirq
{
128 struct summary_tree_node
{
130 GHashTable
*children
;
142 struct state_info llev_state_infos
[] = {
143 { "UNKNOWN", 0, { NULL
} },
144 { "RUNNING", 0, { NULL
} },
145 { "SYSCALL", sizeof(struct llev_state_info_syscall
), { NULL
} },
146 { "IRQ", sizeof(struct llev_state_info_irq
), { NULL
} },
147 { "SOFTIRQ", sizeof(struct llev_state_info_softirq
), { NULL
} },
148 { "TRAP", 0, { NULL
} },
149 { "PREEMPTED", sizeof(struct llev_state_info_preempted
), { NULL
} },
152 struct state_info hlev_state_infos
[] = {
153 { "UNKNOWN", 0, { "Total", "Unknown", NULL
} },
154 { "RUNNING", 0, { "Total", "Working", NULL
} },
155 { "BLOCKED", sizeof(struct hlev_state_info_blocked
), { "Total", "Blocked", NULL
} },
156 { "INTERRUPTED_IRQ", sizeof(struct hlev_state_info_interrupted_irq
), { "Total", "Interrupted", "IRQ", NULL
} },
157 { "INTERRUPTED_SOFTIRQ", sizeof(struct hlev_state_info_interrupted_softirq
), { "Total", "Interrupted", "SoftIRQ", NULL
} },
158 { "INTERRUPTED_CPU", 0, { "Total", "Interrupted", "Preempted", NULL
} },
159 { "INTERRUPTED_POST_BLOCK", 0, { "Total", "Interrupted", "Waiting schedule after blocking", NULL
} },
172 enum llev_syscall_substate
{
173 LLEV_SYSCALL__UNDEFINED
,
180 HLEV_EVENT_TRY_WAKEUP
=0,
187 HLEV_INTERRUPTED_IRQ
,
188 HLEV_INTERRUPTED_SOFTIRQ
,
189 HLEV_INTERRUPTED_CPU
,
190 HLEV_INTERRUPTED_POST_BLOCK
,
193 enum hlev_state_blocked
{
194 HLEV_BLOCKED__UNDEFINED
,
200 struct sstack_event
{
205 struct try_wakeup_event
{
206 int pid
; /* this sould be more precise avec pid may be reused */
208 struct process
*waker
;
211 struct process_state
{
220 struct process_with_state
{
221 struct process
*process
;
222 struct process_state state
;
225 #define PROCESS_STATE_STACK_SIZE 10
231 struct sstack
*stack
;
232 struct process_state
*llev_state_stack
[PROCESS_STATE_STACK_SIZE
];
234 struct process_state
*hlev_state
;
235 GArray
*hlev_history
;
238 static inline void *old_process_state_private_data(struct process
*p
)
240 return p
->llev_state_stack
[p
->stack_current
]->private;
243 static inline struct process_state
*process_find_state(struct process
*p
, enum llev_state st
)
247 for(i
=p
->stack
->array
->len
-1; i
>=0; i
--) {
248 struct sstack_item
*item
= g_array_index(p
->stack
->array
, struct sstack_item
*, i
);
250 struct process_with_state
*pwstate
= item
->data_val
;
251 if(pwstate
->state
.bstate
== st
) {
252 return &pwstate
->state
;
259 static int find_pos_in_stack(enum llev_state lls
, struct process
*p
)
262 for(i
=p
->stack_current
; i
>=0; i
--) {
263 if(p
->llev_state_stack
[i
]->bstate
== lls
)
270 static struct process_state
*find_in_stack(enum llev_state lls
, struct process
*p
)
274 result
= find_pos_in_stack(lls
, p
);
277 return p
->llev_state_stack
[result
];
283 /* called back from sstack on deletion of a data_val which is
284 * a struct process_with_state
287 static void delete_data_val(struct process_with_state
*pwstate
)
289 // FIXME: Free this also
290 //g_free(pwstate->state.private);
292 // FIXME: this is really ugly. Don't free the pwstate if the state is LLEV_RUNNING.
293 // LLEV_RUNNING is a special case that's being processed and deleted immediately after
294 // being inserted on the sstack, to prevent state begin accumulated because it couldn't
295 // be processed before the end of the trace. If we free the state, we get invalid memory
296 // reads when looking at it on the state_stack.
297 //if(pwstate->state.bstate != LLEV_RUNNING)
301 inline void print_time(LttTime t
)
303 //printf("%lu.%lu", t.tv_sec, t.tv_nsec);
305 f
= (double)t
.tv_sec
+ ((double)t
.tv_nsec
)/1000000000.0;
309 static struct sstack_item
*prepare_push_item(struct process
*p
, enum llev_state st
, LttTime t
)
311 struct process_with_state
*pwstate
= g_malloc(sizeof(struct process_with_state
));
312 struct sstack_item
*item
;
314 int wait_for_pop
= 0;
316 if(st
== LLEV_SYSCALL
) {
317 /* We need to push LLEV_SYSCALL as wait_for_pop because it depends on some of
318 * its children. If we don't do this, it's going to get processed immediately
319 * by the sstack and we might miss some details about it that will come later.
324 item
= sstack_item_new_push(wait_for_pop
);
326 //printf("pushing in context of %d\n", p->pid);
328 pwstate
->process
= p
;
329 pwstate
->state
.bstate
= st
;
330 pwstate
->state
.time_begin
= t
;
331 pwstate
->state
.private = g_malloc(llev_state_infos
[st
].size_priv
);
333 item
->data_val
= pwstate
;
334 item
->delete_data_val
= (void (*)(void*))delete_data_val
;
339 static void *item_private(struct sstack_item
*item
)
341 struct process_with_state
*pwstate
= item
->data_val
;
342 return pwstate
->state
.private;
345 static void commit_item(struct process
*p
, struct sstack_item
*item
)
347 sstack_add_item(p
->stack
, item
);
350 static void old_process_push_llev_state(struct process
*p
, struct process_state
*pstate
)
352 if(++p
->stack_current
>= PROCESS_STATE_STACK_SIZE
) {
353 fprintf(stderr
, "depanalysis: internal process stack overflow\n");
357 p
->llev_state_stack
[p
->stack_current
] = pstate
;
360 static void live_complete_process_push_llev_state(struct process
*p
, enum llev_state st
, LttTime t
)
362 struct process_state
*pstate
= g_malloc(sizeof(struct process_state
));
365 pstate
->time_begin
= t
;
366 pstate
->private = g_malloc(llev_state_infos
[st
].size_priv
);
368 old_process_push_llev_state(p
, pstate
);
371 static void prepare_pop_item_commit_nocheck(struct process
*p
, enum llev_state st
, LttTime t
)
373 struct process_with_state
*pwstate
;
374 struct sstack_item
*item
= sstack_item_new_pop();
378 if(p
->stack
->pushes
->len
> 0)
379 push_idx
= g_array_index(p
->stack
->pushes
, int, p
->stack
->pushes
->len
-1);
384 pwstate
= g_array_index(p
->stack
->array
, struct sstack_item
*, push_idx
)->data_val
;
385 pwstate
->process
= p
;
386 pwstate
->state
.time_end
= t
;
387 item
->data_val
= pwstate
;
388 /* don't set delete_data_val because we use the same pwstate as push, and we don't want to free it twice */
392 pwstate
= g_malloc(sizeof(struct process_with_state
));
393 pwstate
->process
= p
;
394 item
->data_val
= pwstate
;
395 pwstate
->state
.time_end
= t
;
396 pwstate
->state
.bstate
= st
;
399 sstack_add_item(p
->stack
, item
);
403 static void prepare_pop_item_commit(struct process
*p
, enum llev_state st
, LttTime t
)
405 struct process_with_state
*pwstate
;
409 if(p
->stack
->pushes
->len
> 0)
410 push_idx
= g_array_index(p
->stack
->pushes
, int, p
->stack
->pushes
->len
-1);
415 /* FIXME: ugly workaround for kernel bug that generates two kernel_arch_syscall_exit on fork.
416 * The bug only occurs upon creation of new processes. But these processes always have
417 * a LLEV_RUNNING at index 0. */
418 if(push_idx
>= p
->stack
->array
->len
)
421 pwstate
= g_array_index(p
->stack
->array
, struct sstack_item
*, push_idx
)->data_val
;
423 if(pwstate
->state
.bstate
!= st
) {
424 /* FIXME: ugly workaround for kernel bug that generates two kernel_arch_syscall_exit on fork */
425 if(st
!= LLEV_SYSCALL
) {
426 printf("bad pop! at ");
429 print_stack(p
->stack
);
433 /* case where we have a double syscall_exit */
439 prepare_pop_item_commit_nocheck(p
, st
, t
);
443 static int try_pop_blocked_llev_preempted(struct process
*p
, LttTime t
)
446 struct process_with_state
*pwstate
;
448 if(p
->stack
->pushes
->len
> 0)
449 push_idx
= g_array_index(p
->stack
->pushes
, int, p
->stack
->pushes
->len
-1);
454 pwstate
= g_array_index(p
->stack
->array
, struct sstack_item
*, push_idx
)->data_val
;
456 if(!(pwstate
->state
.bstate
== LLEV_PREEMPTED
&& ((struct llev_state_info_preempted
*)pwstate
->state
.private)->prev_state
> 0)) {
457 //printf("double try wake up\n");
462 prepare_pop_item_commit_nocheck(p
, LLEV_PREEMPTED
, t
);
466 static void old_process_pop_llev_state(struct process
*p
, struct process_state
*pstate
)
468 /* Ensure we are really popping the current state */
469 /* FIXME: pstate->bstate is uninitialized? */
470 // Commenting because it does not work. The way things work now, this check cannot work.
471 //if(p->llev_state_stack[p->stack_current]->bstate != LLEV_UNKNOWN && p->llev_state_stack[p->stack_current]->bstate != pstate->bstate) {
472 // printf("ERROR! bad pop!\n");
476 /* Actually change the that position */
477 if(p
->stack_current
>= 0)
480 /* If stack empty, we must put something in it */
481 if(p
->stack_current
== -1) {
482 if(pstate
->bstate
== LLEV_SYSCALL
) {
483 //process_push_llev_state(p, LLEV_RUNNING, pstate->time_end);
484 live_complete_process_push_llev_state(p
, LLEV_RUNNING
, pstate
->time_end
);
487 live_complete_process_push_llev_state(p
, LLEV_UNKNOWN
, pstate
->time_end
);
492 static GHashTable
*process_hash_table
;
493 static GHashTable
*syscall_table
;
494 static GHashTable
*irq_table
;
495 static GHashTable
*softirq_table
;
497 /* Insert the hooks before and after each trace and tracefile, and for each
498 event. Print a global header. */
502 static GString
*a_string
;
504 static gboolean
write_traceset_header(void *hook_data
, void *call_data
)
506 g_info("Traceset header");
511 GArray
*oldstyle_stack_to_garray(struct process_state
**oldstyle_stack
, int current
)
516 retval
= g_array_new(FALSE
, FALSE
, sizeof(struct process_state
*));
518 for(i
=0; i
<current
; i
++) {
519 g_array_append_val(retval
, oldstyle_stack
[i
]);
525 static void update_hlev_state(struct process
*p
, LttTime t
)
529 enum hlev_state new_hlev
= 0;
531 for(i
=p
->stack_current
; i
>=0; i
--) {
533 st
= p
->llev_state_stack
[i
]->bstate
;
535 if(st
== LLEV_RUNNING
|| st
== LLEV_TRAP
|| st
== LLEV_SYSCALL
) {
536 new_hlev
= HLEV_RUNNING
;
539 else if(st
== LLEV_IRQ
) {
540 new_hlev
= HLEV_INTERRUPTED_IRQ
;
543 else if(st
== LLEV_SOFTIRQ
) {
544 new_hlev
= HLEV_INTERRUPTED_SOFTIRQ
;
547 else if(st
== LLEV_PREEMPTED
) {
548 int prev_state
= ((struct llev_state_info_preempted
*) old_process_state_private_data(p
))->prev_state
;
550 if(prev_state
== 0) {
551 new_hlev
= HLEV_INTERRUPTED_CPU
;
553 else if(prev_state
== -1) {
554 new_hlev
= HLEV_INTERRUPTED_POST_BLOCK
;
557 new_hlev
= HLEV_BLOCKED
;
561 else if(st
== LLEV_UNKNOWN
) {
562 new_hlev
= HLEV_UNKNOWN
;
570 /* If no state change, do nothing */
571 if(p
->hlev_state
!= NULL
&& new_hlev
== p
->hlev_state
->bstate
) {
575 p
->hlev_state
->time_end
= t
;
576 /* This check is here because we initially put HLEV_UNKNOWN as hlev state, but in the case
577 * of processes newly created, it is immediately replaced by HLEV_BLOCKED. In order to avoid
578 * having a UNKNOWN state of duration 0 in the summary, we don't add it. This isn't as elegant
581 if(ltt_time_compare(p
->hlev_state
->time_begin
, p
->hlev_state
->time_end
) != 0)
582 g_array_append_val(p
->hlev_history
, p
->hlev_state
);
583 p
->hlev_state
= g_malloc(sizeof(struct process_state
));
584 p
->hlev_state
->bstate
= new_hlev
;
585 p
->hlev_state
->time_begin
= t
;
586 p
->hlev_state
->private = g_malloc(hlev_state_infos
[new_hlev
].size_priv
);
588 //printf("depanalysis: now at hlev state %s\n", hlev_state_infos[new_hlev].name);
590 /* Set private data */
591 switch(p
->hlev_state
->bstate
) {
597 struct hlev_state_info_blocked
*hlev_blocked_private
= p
->hlev_state
->private;
598 int syscall_pos
= find_pos_in_stack(LLEV_SYSCALL
, p
);
599 int trap_pos
= find_pos_in_stack(LLEV_TRAP
, p
);
602 hlev_blocked_private
->syscall_id
= 1;
603 hlev_blocked_private
->trap
= 0;
604 hlev_blocked_private
->pid_exit
= 0;
605 hlev_blocked_private
->substate
= HLEV_BLOCKED__UNDEFINED
;
606 hlev_blocked_private
->private = NULL
;
607 hlev_blocked_private
->llev_state_entry
= oldstyle_stack_to_garray(p
->llev_state_stack
, p
->stack_current
);
608 hlev_blocked_private
->llev_state_exit
= NULL
;
610 //g_assert(syscall_pos >= 0 || trap_pos >= 0);
612 if(trap_pos
> syscall_pos
) {
613 hlev_blocked_private
->trap
= 1;
616 /* initial value, may be changed below */
617 hlev_blocked_private
->substate
= HLEV_BLOCKED__UNDEFINED
;
619 if(syscall_pos
>= 0) {
620 struct process_state
*ps
= p
->llev_state_stack
[syscall_pos
];
621 struct llev_state_info_syscall
*llev_syscall_private
= (struct llev_state_info_syscall
*) ps
->private;
622 hlev_blocked_private
->syscall_id
= llev_syscall_private
->syscall_id
;
624 if(llev_syscall_private
->substate
== LLEV_SYSCALL__OPEN
) {
625 struct llev_state_info_syscall__open
*llev_syscall_open_private
;
626 struct hlev_state_info_blocked__open
*hlev_blocked_open_private
;
627 llev_syscall_open_private
= llev_syscall_private
->private;
628 hlev_blocked_private
->substate
= HLEV_BLOCKED__OPEN
;
629 hlev_blocked_open_private
= g_malloc(sizeof(struct hlev_state_info_blocked__open
));
630 hlev_blocked_private
->private = hlev_blocked_open_private
;
631 hlev_blocked_open_private
->filename
= llev_syscall_open_private
->filename
;
633 //printf("depanalysis: blocked in an open!\n");
635 else if(llev_syscall_private
->substate
== LLEV_SYSCALL__READ
) {
636 struct llev_state_info_syscall__read
*llev_syscall_read_private
;
637 struct hlev_state_info_blocked__read
*hlev_blocked_read_private
;
638 llev_syscall_read_private
= llev_syscall_private
->private;
639 hlev_blocked_private
->substate
= HLEV_BLOCKED__READ
;
640 hlev_blocked_read_private
= g_malloc(sizeof(struct hlev_state_info_blocked__read
));
641 hlev_blocked_private
->private = hlev_blocked_read_private
;
642 hlev_blocked_read_private
->filename
= llev_syscall_read_private
->filename
;
644 //printf("depanalysis: blocked in a read!\n");
646 else if(llev_syscall_private
->substate
== LLEV_SYSCALL__POLL
) {
647 struct llev_state_info_syscall__poll
*llev_syscall_poll_private
;
648 struct hlev_state_info_blocked__poll
*hlev_blocked_poll_private
;
649 llev_syscall_poll_private
= llev_syscall_private
->private;
650 hlev_blocked_private
->substate
= HLEV_BLOCKED__POLL
;
651 hlev_blocked_poll_private
= g_malloc(sizeof(struct hlev_state_info_blocked__poll
));
652 hlev_blocked_private
->private = hlev_blocked_poll_private
;
653 hlev_blocked_poll_private
->filename
= llev_syscall_poll_private
->filename
;
655 //printf("depanalysis: blocked in a read!\n");
659 hlev_blocked_private
->syscall_id
= -1;
664 case HLEV_INTERRUPTED_IRQ
: {
665 struct hlev_state_info_interrupted_irq
*sinfo
= p
->hlev_state
->private;
666 struct process_state
*ps
= find_in_stack(LLEV_IRQ
, p
);
670 sinfo
->irq
= ((struct llev_state_info_irq
*) ps
->private)->irq
;
673 case HLEV_INTERRUPTED_SOFTIRQ
: {
674 struct hlev_state_info_interrupted_softirq
*sinfo
= p
->hlev_state
->private;
675 struct process_state
*ps
= find_in_stack(LLEV_SOFTIRQ
, p
);
679 sinfo
->softirq
= ((struct llev_state_info_softirq
*) ps
->private)->softirq
;
687 static gint
compare_summary_tree_node_times(gconstpointer a
, gconstpointer b
)
689 struct summary_tree_node
*n1
= (struct summary_tree_node
*) a
;
690 struct summary_tree_node
*n2
= (struct summary_tree_node
*) b
;
692 return ltt_time_compare(n2
->duration
, n1
->duration
);
695 /* Print an item of the simple summary tree, and recurse, printing its children.
697 * If depth == -1, this is the root: we don't print a label, we only recurse into
701 static void print_summary_item(struct summary_tree_node
*node
, int depth
)
707 (unsigned int)strlen(node
->name
)+2*depth
,
709 print_time(node
->duration
);
710 printf(") <%d>\n", node
->id_for_episodes
);
716 vals
= g_hash_table_get_values(node
->children
);
718 /* sort the values */
719 vals
= g_list_sort(vals
, compare_summary_tree_node_times
);
722 print_summary_item((struct summary_tree_node
*)vals
->data
, depth
+1);
726 /* we must free the list returned by g_hash_table_get_values() */
730 static inline void print_irq(int irq
)
732 printf("IRQ %d [%s]", irq
, g_quark_to_string((GQuark
)(unsigned long)g_hash_table_lookup(irq_table
, &irq
)));
735 static inline void print_softirq(int softirq
)
737 printf("SoftIRQ %d [%s]", softirq
, g_quark_to_string((GQuark
)(unsigned long)g_hash_table_lookup(softirq_table
, &softirq
)));
740 static inline void print_pid(int pid
)
742 struct process
*event_process_info
= g_hash_table_lookup(process_hash_table
, &pid
);
746 if(event_process_info
== NULL
)
749 pname
= g_quark_to_string(event_process_info
->name
);
750 printf("%d [%s]", pid
, pname
);
753 static void modify_path_with_private(GArray
*path
, struct process_state
*pstate
)
758 // FIXME: fix this leak
759 switch(pstate
->bstate
) {
760 case HLEV_INTERRUPTED_IRQ
:
761 res
= asprintf(&tmps
, "IRQ %d [%s]", ((struct hlev_state_info_interrupted_irq
*)pstate
->private)->irq
, g_quark_to_string((GQuark
)(unsigned long)g_hash_table_lookup(irq_table
, &((struct hlev_state_info_interrupted_irq
*)pstate
->private)->irq
)));
763 g_array_append_val(path
, tmps
);
765 case HLEV_INTERRUPTED_SOFTIRQ
:
766 res
= asprintf(&tmps
, "SoftIRQ %d [%s]", ((struct hlev_state_info_interrupted_softirq
*)pstate
->private)->softirq
, g_quark_to_string((GQuark
)(unsigned long)g_hash_table_lookup(softirq_table
, &((struct hlev_state_info_interrupted_softirq
*)pstate
->private)->softirq
)));
768 g_array_append_val(path
, tmps
);
771 struct hlev_state_info_blocked
*hlev_blocked_private
= (struct hlev_state_info_blocked
*)pstate
->private;
773 if(hlev_blocked_private
->trap
) {
775 g_array_append_val(path
, ptr
);
778 if(hlev_blocked_private
->syscall_id
== -1) {
779 char *ptr
= "Userspace";
780 g_array_append_val(path
, ptr
);
783 res
= asprintf(&tmps
, "Syscall %d [%s]", hlev_blocked_private
->syscall_id
, g_quark_to_string((GQuark
)(unsigned long)g_hash_table_lookup(syscall_table
, &hlev_blocked_private
->syscall_id
)));
785 g_array_append_val(path
, tmps
);
788 if(((struct hlev_state_info_blocked
*)pstate
->private)->substate
== HLEV_BLOCKED__OPEN
) {
789 const char *str
= g_quark_to_string(((struct hlev_state_info_blocked__open
*)((struct hlev_state_info_blocked
*)pstate
->private)->private)->filename
);
790 g_array_append_val(path
, str
);
792 else if(((struct hlev_state_info_blocked
*)pstate
->private)->substate
== HLEV_BLOCKED__READ
) {
794 res
= asprintf(&str
, "%s", g_quark_to_string(((struct hlev_state_info_blocked__read
*)((struct hlev_state_info_blocked
*)pstate
->private)->private)->filename
));
796 g_array_append_val(path
, str
);
797 /* FIXME: this must be freed at some point */
800 else if(((struct hlev_state_info_blocked
*)pstate
->private)->substate
== HLEV_BLOCKED__POLL
) {
802 res
= asprintf(&str
, "%s", g_quark_to_string(((struct hlev_state_info_blocked__poll
*)((struct hlev_state_info_blocked
*)pstate
->private)->private)->filename
));
804 g_array_append_val(path
, str
);
805 /* FIXME: this must be freed at some point */
813 void print_stack_garray_horizontal(GArray
*stack
)
815 /* FIXME: this function doesn't work if we delete the states as we process them because we
816 * try to read those states here to print the low level stack.
820 for(i
=0; i
<stack
->len
; i
++) {
821 struct process_state
*pstate
= g_array_index(stack
, struct process_state
*, i
);
822 printf("%s", llev_state_infos
[pstate
->bstate
].name
);
824 if(pstate
->bstate
== LLEV_SYSCALL
) {
825 struct llev_state_info_syscall
*llev_syscall_private
= pstate
->private;
826 printf(" %d [%s]", llev_syscall_private
->syscall_id
, g_quark_to_string((GQuark
)(unsigned long)g_hash_table_lookup(syscall_table
, &llev_syscall_private
->syscall_id
)));
834 static int dicho_search_state_ending_after(struct process
*p
, LttTime t
)
837 int over
= p
->hlev_history
->len
-1;
838 struct process_state
*pstate
;
844 /* If the last element is smaller or equal than the time we are searching for,
847 pstate
= g_array_index(p
->hlev_history
, struct process_state
*, over
);
848 if(ltt_time_compare(pstate
->time_end
, t
) <= 0) {
851 /* no need to check for the equal case */
853 pstate
= g_array_index(p
->hlev_history
, struct process_state
*, under
);
854 result
= ltt_time_compare(pstate
->time_end
, t
);
856 /* trivial match at the first element if it is greater or equal
857 * than the time we want
865 dicho
= (under
+over
)/2;
866 pstate
= g_array_index(p
->hlev_history
, struct process_state
*, dicho
);
867 result
= ltt_time_compare(pstate
->time_end
, t
);
872 else if(result
== 1) {
880 if(over
-under
== 1) {
881 /* we have converged */
888 /* FIXME: this shouldn't be based on pids in case of reuse
889 * FIXME: should add a list of processes used to avoid loops
892 static struct process_state
*find_state_ending_after(int pid
, LttTime t
)
898 p
= g_hash_table_lookup(process_hash_table
, &pid
);
902 result
= dicho_search_state_ending_after(p
, t
);
907 return g_array_index(p
->hlev_history
, struct process_state
*, result
);
910 static void print_indent(int offset
)
915 printf("%*s", 8, "");
916 for (i
= 3; i
< offset
; i
++) {
918 printf("%*s", 4, "");
921 printf("%*s", 4*offset
, "");
924 static void print_delay_pid(int pid
, LttTime t1
, LttTime t2
, int offset
)
929 p
= g_hash_table_lookup(process_hash_table
, &pid
);
933 i
= dicho_search_state_ending_after(p
, t1
);
934 for(; i
<p
->hlev_history
->len
; i
++) {
935 struct process_state
*pstate
= g_array_index(p
->hlev_history
, struct process_state
*, i
);
936 if(ltt_time_compare(pstate
->time_end
, t2
) > 0)
939 if(pstate
->bstate
== HLEV_BLOCKED
) {
940 struct hlev_state_info_blocked
*state_private_blocked
;
941 state_private_blocked
= pstate
->private;
942 struct process_state
*state_unblocked
;
944 print_indent(offset
);
945 printf("--> Blocked in ");
946 print_stack_garray_horizontal(state_private_blocked
->llev_state_entry
);
949 print_time(pstate
->time_begin
);
951 print_time(pstate
->time_end
);
953 printf(", dur: %f)\n", 1e-9*ltt_time_to_double(ltt_time_sub(pstate
->time_end
, pstate
->time_begin
)));
955 state_unblocked
= find_state_ending_after(state_private_blocked
->pid_exit
, state_private_blocked
->time_woken
);
956 if(state_unblocked
) {
957 if(state_unblocked
->bstate
== HLEV_INTERRUPTED_IRQ
) {
958 struct hlev_state_info_interrupted_irq
*priv
= state_unblocked
->private;
959 /* if in irq or softirq, we don't care what the waking process was doing because they are asynchroneous events */
960 print_indent(offset
);
961 printf("--- Woken up by an IRQ: ");
962 print_irq(priv
->irq
);
965 else if(state_unblocked
->bstate
== HLEV_INTERRUPTED_SOFTIRQ
) {
966 struct hlev_state_info_interrupted_softirq
*priv
= state_unblocked
->private;
967 print_indent(offset
);
968 printf("--- Woken up by a SoftIRQ: ");
969 print_softirq(priv
->softirq
);
976 if(ltt_time_compare(t1prime
, pstate
->time_begin
) < 0)
977 t1prime
= pstate
->time_begin
;
978 if(ltt_time_compare(t2prime
, pstate
->time_end
) > 0)
979 t2prime
= pstate
->time_end
;
981 print_delay_pid(state_private_blocked
->pid_exit
, t1prime
, t2prime
, offset
+1);
982 print_indent(offset
);
983 printf("--- Woken up in context of ");
984 print_pid(state_private_blocked
->pid_exit
);
985 if(state_private_blocked
->llev_state_exit
) {
986 print_stack_garray_horizontal(state_private_blocked
->llev_state_exit
);
990 printf(" in high-level state %s", hlev_state_infos
[state_unblocked
->bstate
].name
);
995 print_indent(offset
);
996 printf("Weird... cannot find in what state the waker (%d) was\n", state_private_blocked
->pid_exit
);
1000 //print_delay_pid(state_private_blocked->pid_exit, pstate->time_start, pstate->time_end);
1001 //printf("\t\t Woken up in context of %d: ", state_private_blocked->pid_exit);
1002 //if(state_private_blocked->llev_state_exit) {
1003 // print_stack_garray_horizontal(state_private_blocked->llev_state_exit);
1004 // printf("here3 (%d)\n", state_private_blocked->llev_state_exit->len);
1007 // printf("the private_blocked %p had a null exit stack\n", state_private_blocked);
1013 static void print_range_critical_path(int process
, LttTime t1
, LttTime t2
)
1015 printf("Critical path for requested range:\n");
1016 printf("Final process is %d\n", process
);
1017 print_delay_pid(process
, t1
, t2
, 2);
1021 * output legend example:
1023 * --> Blocked in RUNNING, SYSCALL NNN [syscall_name]
1024 * | ---> Blocked in RUNNING, SYSCALL NNN [syscall_name]
1025 * | | --> Blocked in RUNNING, SYSCALL [syscall_name]
1026 * | | --- Woken up by an IRQ: IRQ 0 [timer]
1027 * | --- Woken up in context of PID [appname] in high-level state RUNNING
1028 * --- Woken up in context of PID [appname] in high-level state RUNNING
1031 static void print_process_critical_path_summary()
1033 struct process
*pinfo
;
1036 pinfos
= g_hash_table_get_values(process_hash_table
);
1037 if(pinfos
== NULL
) {
1038 fprintf(stderr
, "error: no process found\n");
1042 printf("Process Critical Path Summary:\n");
1046 pinfo
= (struct process
*)pinfos
->data
;
1047 if (depanalysis_range_pid_searching
!= -1 && pinfo
->pid
!= depanalysis_range_pid_searching
)
1049 printf("\tProcess %d [%s]\n", pinfo
->pid
, g_quark_to_string(pinfo
->name
));
1051 if(pinfo
->hlev_history
->len
< 1)
1054 print_delay_pid(pinfo
->pid
, g_array_index(pinfo
->hlev_history
, struct process_state
*, 0)->time_begin
, g_array_index(pinfo
->hlev_history
, struct process_state
*, pinfo
->hlev_history
->len
- 1)->time_end
, 2);
1059 pinfos
= pinfos
->next
;
1065 gint
compare_states_length(gconstpointer a
, gconstpointer b
)
1067 struct process_state
**s1
= (struct process_state
**)a
;
1068 struct process_state
**s2
= (struct process_state
**)b
;
1071 val
= ltt_time_compare(ltt_time_sub((*s2
)->time_end
, (*s2
)->time_begin
), ltt_time_sub((*s1
)->time_end
, (*s1
)->time_begin
));
1075 static void print_simple_summary(void)
1077 struct process
*pinfo
;
1079 GList
*pinfos_first
;
1081 int id_for_episodes
= 0;
1083 if (!a_print_simple_summary
)
1086 /* we save all the nodes here to print the episodes table quickly */
1087 GArray
*all_nodes
= g_array_new(FALSE
, FALSE
, sizeof(struct summary_tree_node
*));
1089 pinfos_first
= g_hash_table_get_values(process_hash_table
);
1090 if(pinfos_first
== NULL
) {
1091 fprintf(stderr
, "error: no processes found\n");
1094 pinfos
= pinfos_first
;
1096 printf("Simple summary:\n");
1098 /* For each process */
1100 struct summary_tree_node base_node
= { children
: NULL
, name
: "Root" };
1102 pinfo
= (struct process
*)pinfos
->data
;
1103 printf("\tProcess %d [%s]\n", pinfo
->pid
, g_quark_to_string(pinfo
->name
));
1105 /* For each state in the process history */
1106 for(i
=0; i
<pinfo
->hlev_history
->len
; i
++) {
1107 struct process_state
*pstate
= g_array_index(pinfo
->hlev_history
, struct process_state
*, i
);
1108 struct summary_tree_node
*node_cur
= &base_node
;
1109 GArray
*tree_path_garray
;
1111 /* Modify the path based on private data */
1112 tree_path_garray
= g_array_new(FALSE
, FALSE
, sizeof(char *));
1115 char **tree_path_cur2
= hlev_state_infos
[pstate
->bstate
].tree_path
;
1116 while(*tree_path_cur2
) {
1120 g_array_append_vals(tree_path_garray
, hlev_state_infos
[pstate
->bstate
].tree_path
, count
);
1122 modify_path_with_private(tree_path_garray
, pstate
);
1124 /* Walk the path, adding the nodes to the summary */
1125 for(j
=0; j
<tree_path_garray
->len
; j
++) {
1126 struct summary_tree_node
*newnode
;
1127 GQuark componentquark
;
1129 /* Have a path component we must follow */
1130 if(!node_cur
->children
) {
1131 /* must create the hash table for the children */
1132 node_cur
->children
= g_hash_table_new(g_int_hash
, g_int_equal
);
1135 /* try to get the node for the next component */
1136 componentquark
= g_quark_from_string(g_array_index(tree_path_garray
, char *, j
));
1137 newnode
= g_hash_table_lookup(node_cur
->children
, &componentquark
);
1138 if(newnode
== NULL
) {
1139 newnode
= g_malloc(sizeof(struct summary_tree_node
));
1140 newnode
->children
= NULL
;
1141 newnode
->name
= g_array_index(tree_path_garray
, char *, j
);
1142 newnode
->duration
= ltt_time_zero
;
1143 newnode
->id_for_episodes
= id_for_episodes
++;
1144 newnode
->episodes
= g_array_new(FALSE
, FALSE
, sizeof(struct process_state
*));
1145 g_hash_table_insert(node_cur
->children
, &componentquark
, newnode
);
1147 g_array_append_val(all_nodes
, newnode
);
1151 node_cur
->duration
= ltt_time_add(node_cur
->duration
, ltt_time_sub(pstate
->time_end
, pstate
->time_begin
));
1152 g_array_append_val(node_cur
->episodes
, pstate
);
1156 /* print the summary */
1157 print_summary_item(&base_node
, -1);
1162 pinfos
= pinfos
->next
;
1169 printf("Episode list\n");
1170 pinfos
= pinfos_first
;
1172 /* For all the nodes of the Simple summary tree */
1173 for(i
=0; i
<all_nodes
->len
; i
++) {
1174 struct summary_tree_node
*node
= (struct summary_tree_node
*)g_array_index(all_nodes
, struct summary_tree_node
*, i
);
1176 /* Sort the episodes from longest to shortest */
1177 g_array_sort(node
->episodes
, compare_states_length
);
1179 printf("\tNode id: <%d>\n", node
->id_for_episodes
);
1180 /* For each episode of the node */
1181 for(j
=0; j
<node
->episodes
->len
; j
++) {
1182 struct process_state
*st
= g_array_index(node
->episodes
, struct process_state
*, j
);
1185 print_time(st
->time_begin
);
1187 print_time(st
->time_end
);
1188 printf(" (%f)\n", 1e-9*ltt_time_to_double(ltt_time_sub(st
->time_end
,st
->time_begin
)));
1193 static void print_simple_summary_pid_range(int pid
, LttTime t1
, LttTime t2
)
1195 struct process
*pinfo
;
1197 int id_for_episodes
= 0;
1199 /* we save all the nodes here to print the episodes table quickly */
1200 GArray
*all_nodes
= g_array_new(FALSE
, FALSE
, sizeof(struct summary_tree_node
*));
1202 pinfo
= g_hash_table_lookup(process_hash_table
, &pid
);
1205 struct summary_tree_node base_node
= { children
: NULL
, name
: "Root" };
1207 printf("\tProcess %d [%s]\n", pinfo
->pid
, g_quark_to_string(pinfo
->name
));
1209 /* For each state in the process history */
1210 for(i
=0; i
<pinfo
->hlev_history
->len
; i
++) {
1211 struct process_state
*pstate
= g_array_index(pinfo
->hlev_history
, struct process_state
*, i
);
1212 struct summary_tree_node
*node_cur
= &base_node
;
1213 GArray
*tree_path_garray
;
1215 if(ltt_time_compare(pstate
->time_end
, t1
) < 0)
1218 if(ltt_time_compare(pstate
->time_end
, t2
) > 0)
1221 /* Modify the path based on private data */
1222 tree_path_garray
= g_array_new(FALSE
, FALSE
, sizeof(char *));
1225 char **tree_path_cur2
= hlev_state_infos
[pstate
->bstate
].tree_path
;
1226 while(*tree_path_cur2
) {
1230 g_array_append_vals(tree_path_garray
, hlev_state_infos
[pstate
->bstate
].tree_path
, count
);
1232 modify_path_with_private(tree_path_garray
, pstate
);
1234 /* Walk the path, adding the nodes to the summary */
1235 for(j
=0; j
<tree_path_garray
->len
; j
++) {
1236 struct summary_tree_node
*newnode
;
1237 GQuark componentquark
;
1239 /* Have a path component we must follow */
1240 if(!node_cur
->children
) {
1241 /* must create the hash table for the children */
1242 node_cur
->children
= g_hash_table_new(g_int_hash
, g_int_equal
);
1245 /* try to get the node for the next component */
1246 componentquark
= g_quark_from_string(g_array_index(tree_path_garray
, char *, j
));
1247 newnode
= g_hash_table_lookup(node_cur
->children
, &componentquark
);
1248 if(newnode
== NULL
) {
1249 newnode
= g_malloc(sizeof(struct summary_tree_node
));
1250 newnode
->children
= NULL
;
1251 newnode
->name
= g_array_index(tree_path_garray
, char *, j
);
1252 newnode
->duration
= ltt_time_zero
;
1253 newnode
->id_for_episodes
= id_for_episodes
++;
1254 newnode
->episodes
= g_array_new(FALSE
, FALSE
, sizeof(struct process_state
*));
1255 g_hash_table_insert(node_cur
->children
, &componentquark
, newnode
);
1257 g_array_append_val(all_nodes
, newnode
);
1261 node_cur
->duration
= ltt_time_add(node_cur
->duration
, ltt_time_sub(pstate
->time_end
, pstate
->time_begin
));
1262 g_array_append_val(node_cur
->episodes
, pstate
);
1266 /* print the summary */
1267 print_summary_item(&base_node
, -1);
1274 printf("Episode list\n");
1276 /* For all the nodes of the Simple summary tree */
1277 for(i
=0; i
<all_nodes
->len
; i
++) {
1278 struct summary_tree_node
*node
= (struct summary_tree_node
*)g_array_index(all_nodes
, struct summary_tree_node
*, i
);
1280 /* Sort the episodes from longest to shortest */
1281 g_array_sort(node
->episodes
, compare_states_length
);
1283 printf("\tNode id: <%d>\n", node
->id_for_episodes
);
1284 /* For each episode of the node */
1285 for(j
=0; j
<node
->episodes
->len
; j
++) {
1286 struct process_state
*st
= g_array_index(node
->episodes
, struct process_state
*, j
);
1289 print_time(st
->time_begin
);
1291 print_time(st
->time_end
);
1292 printf(" (%f)\n", 1e-9*ltt_time_to_double(ltt_time_sub(st
->time_end
,st
->time_begin
)));
1297 static void flush_process_sstacks(void)
1301 pinfos
= g_hash_table_get_values(process_hash_table
);
1303 struct process
*pinfo
= (struct process
*)pinfos
->data
;
1305 sstack_force_flush(pinfo
->stack
);
1307 pinfos
= pinfos
->next
;
1310 g_list_free(pinfos
);
1313 struct family_item
{
1318 void print_range_reports(int pid
, LttTime t1
, LttTime t2
)
1320 GArray
*family
= g_array_new(FALSE
, FALSE
, sizeof(struct family_item
));
1323 /* reconstruct the parental sequence */
1325 struct process
*pinfo
;
1326 struct family_item fi
;
1329 pinfo
= g_hash_table_lookup(process_hash_table
, &pid
);
1334 cur_beg
= g_array_index(pinfo
->hlev_history
, struct process_state
*, 0)->time_begin
;
1335 fi
.creation
= cur_beg
;
1336 g_array_append_val(family
, fi
);
1338 if(ltt_time_compare(cur_beg
, t1
) == -1) {
1339 /* current pid starts before the interesting time */
1342 if(pinfo
->parent
== -1) {
1343 printf("unable to go back, we don't know the parent of %d\n", fi
.pid
);
1346 /* else, we go on */
1347 pid
= pinfo
->parent
;
1351 printf("Simple summary for range:\n");
1352 for(i
=family
->len
-1; i
>=0; i
--) {
1353 LttTime iter_t1
, iter_t2
;
1354 int iter_pid
= g_array_index(family
, struct family_item
, i
).pid
;
1356 if(i
== family
->len
-1)
1359 iter_t1
= g_array_index(family
, struct family_item
, i
).creation
;
1364 iter_t2
= g_array_index(family
, struct family_item
, i
-1).creation
;
1366 printf("This section of summary concerns pid %d between ", iter_pid
);
1367 print_time(iter_t1
);
1369 print_time(iter_t2
);
1371 print_simple_summary_pid_range(iter_pid
, iter_t1
, iter_t2
);
1373 print_range_critical_path(depanalysis_range_pid
, t1
, t2
);
1376 static gboolean
write_traceset_footer(void *hook_data
, void *call_data
)
1378 g_info("depanalysis traceset footer");
1380 /* After processing all the events, we need to flush the sstacks
1381 * because some unfinished states may remain in them. We want them
1382 * event though there are incomplete.
1384 flush_process_sstacks();
1386 /* print the reports */
1387 print_simple_summary();
1388 print_process_critical_path_summary();
1389 if(depanalysis_use_time
== 3) {
1390 printf("depanalysis_use_time = %d\n", depanalysis_use_time
);
1391 if(depanalysis_range_pid
== -1 && depanalysis_range_pid_searching
>= 0)
1392 depanalysis_range_pid
= depanalysis_range_pid_searching
;
1394 if(depanalysis_range_pid
>= 0) {
1395 print_range_reports(depanalysis_range_pid
, depanalysis_time1
, depanalysis_time2
);
1398 printf("range critical path: could not find the end of the range\n");
1405 static int write_event_content(void *hook_data
, void *call_data
)
1408 LttvTracefileContext
*tfc
= (LttvTracefileContext
*)call_data
;
1410 LttvTracefileState
*tfs
= (LttvTracefileState
*)call_data
;
1414 guint cpu
= tfs
->cpu
;
1415 LttvTraceState
*ts
= (LttvTraceState
*)tfc
->t_context
;
1416 LttvProcessState
*process
= ts
->running_process
[cpu
];
1418 e
= ltt_tracefile_get_event(tfc
->tf
);
1420 lttv_event_to_string(e
, a_string
, TRUE
, 1, tfs
);
1423 g_string_append_printf(a_string
, " %s ",
1424 g_quark_to_string(process
->state
->s
));
1427 g_string_append_printf(a_string
,"\n");
1429 fputs(a_string
->str
, a_file
);
1433 static int field_get_value_int(struct LttEvent
*e
, struct marker_info
*info
, GQuark f
)
1435 struct marker_field
*marker_field
;
1438 for_each_marker_field(marker_field
, info
) {
1439 if (marker_field
->name
== f
) {
1445 return ltt_event_get_long_unsigned(e
, marker_field
);
1448 static char *field_get_value_string(struct LttEvent
*e
, struct marker_info
*info
, GQuark f
)
1450 struct marker_field
*marker_field
;
1453 for_each_marker_field(marker_field
, info
) {
1454 if (marker_field
->name
== f
) {
1460 return ltt_event_get_string(e
, marker_field
);
1463 void process_delayed_stack_action(void *arg
, struct sstack_item
*item
)
1465 struct process
*pinfo
= (struct process
*)arg
;
1466 //printf("processing delayed stack action on pid %d at ", pinfo->pid);
1467 //if(((struct process_with_state *) item->data_val)->state.time_begin.tv_nsec == 987799696)
1468 // printf("HERE!!!\n");
1469 //print_time(((struct process_with_state *) item->data_val)->state.time_begin);
1471 //printf("stack before:\n");
1472 //print_stack(pinfo->stack);
1474 if(item
->data_type
== SSTACK_TYPE_PUSH
) {
1475 struct process_with_state
*pwstate
= item
->data_val
;
1476 //printf("pushing\n");
1477 old_process_push_llev_state(pinfo
, &pwstate
->state
);
1478 update_hlev_state(pinfo
, pwstate
->state
.time_begin
);
1480 else if(item
->data_type
== SSTACK_TYPE_POP
) {
1481 struct process_with_state
*pwstate
= item
->data_val
;
1482 //printf("popping\n");
1483 old_process_pop_llev_state(pinfo
, &pwstate
->state
);
1484 update_hlev_state(pinfo
, pwstate
->state
.time_end
);
1486 else if(item
->data_type
== SSTACK_TYPE_EVENT
) {
1487 struct sstack_event
*se
= item
->data_val
;
1488 if(se
->event_type
== HLEV_EVENT_TRY_WAKEUP
) {
1489 /* FIXME: should change hlev event from BLOCKED to INTERRUPTED CPU when receiving TRY_WAKEUP */
1490 struct try_wakeup_event
*twe
= se
->private;
1492 /* FIXME: maybe do some more rigorous checking here */
1493 if(pinfo
->hlev_state
->bstate
== HLEV_BLOCKED
) {
1494 struct hlev_state_info_blocked
*hlev_blocked_private
= pinfo
->hlev_state
->private;
1496 hlev_blocked_private
->pid_exit
= twe
->pid
;
1497 hlev_blocked_private
->time_woken
= twe
->time
;
1498 hlev_blocked_private
->llev_state_exit
= oldstyle_stack_to_garray(twe
->waker
->llev_state_stack
, twe
->waker
->stack_current
);
1499 //printf("set a non null exit stack on %p, and stack size is %d\n", hlev_blocked_private, hlev_blocked_private->llev_state_exit->len);
1502 if(p->stack_current >= 0 && p->llev_state_stack[p->stack_current]->bstate == LLEV_PREEMPTED) {
1503 old_process_pop_llev_state(pinfo, p->llev_state_stack[p->stack_current]);
1504 update_hlev_state(pinfo
1505 old_process_push_llev_state
1512 //printf("stack after:\n");
1513 //print_stack(pinfo->stack);
1516 static struct process
*get_or_init_process_info(struct LttEvent
*e
, GQuark name
, int pid
, int *new)
1518 //gconstpointer val;
1521 val
= g_hash_table_lookup(process_hash_table
, &pid
);
1523 struct process
*pinfo
;
1526 /* Initialize new pinfo for newly discovered process */
1527 pinfo
= g_malloc(sizeof(struct process
));
1529 pinfo
->parent
= -1; /* unknown parent */
1530 pinfo
->hlev_history
= g_array_new(FALSE
, FALSE
, sizeof(struct process_state
*));
1531 pinfo
->stack
= sstack_new();
1532 pinfo
->stack_current
=-1;
1533 pinfo
->stack
->process_func
= process_delayed_stack_action
;
1534 pinfo
->stack
->process_func_arg
= pinfo
;
1535 for(i
=0; i
<PROCESS_STATE_STACK_SIZE
; i
++) {
1536 pinfo
->llev_state_stack
[i
] = g_malloc(sizeof(struct process_state
));
1539 pinfo
->hlev_state
= g_malloc(sizeof(struct process_state
));
1540 pinfo
->hlev_state
->bstate
= HLEV_UNKNOWN
;
1541 pinfo
->hlev_state
->time_begin
= e
->event_time
;
1542 pinfo
->hlev_state
->private = NULL
;
1547 g_hash_table_insert(process_hash_table
, &pinfo
->pid
, pinfo
);
1560 static int differentiate_swappers(int pid
, LttEvent
*e
)
1563 return pid
+e
->tracefile
->cpu_num
+2000000;
1568 static int process_event(void *hook_data
, void *call_data
)
1570 LttvTracefileContext
*tfc
= (LttvTracefileContext
*)call_data
;
1571 LttvTracefileState
*tfs
= (LttvTracefileState
*)call_data
;
1573 struct marker_info
*info
;
1576 /* Extract data from event structures and state */
1577 guint cpu
= tfs
->cpu
;
1578 LttvTraceState
*ts
= (LttvTraceState
*)tfc
->t_context
;
1579 LttvProcessState
*process
= ts
->running_process
[cpu
];
1580 struct process
*pinfo
;
1582 e
= ltt_tracefile_get_event(tfs
->parent
.tf
);
1584 info
= marker_get_info_from_id(tfc
->tf
->mdata
, e
->event_id
);
1586 //if(depanalysis_use_time && (ltt_time_compare(e->timestamp, arg_t1) == -1 || ltt_time_compare(e->timestamp, arg_t2) == 1)) {
1589 /* Set the pid for the dependency analysis at each event, until we are passed the range. */
1590 if(depanalysis_use_time
== 3) {
1591 if(ltt_time_compare(e
->event_time
, depanalysis_time2
) <= 0) {
1592 depanalysis_range_pid
= process
->pid
;
1595 /* Should stop processing and print results */
1599 /* Code to limit the event count */
1600 if(depanalysis_event_limit
> 0) {
1601 depanalysis_event_limit
--;
1603 else if(depanalysis_event_limit
== 0) {
1604 write_traceset_footer(hook_data
, call_data
);
1605 printf("exit due to event limit reached\n");
1609 /* write event like textDump for now, for debugging purposes */
1610 //write_event_content(hook_data, call_data);
1612 if(tfc
->tf
->name
== LTT_CHANNEL_SYSCALL_STATE
&& info
->name
== LTT_EVENT_SYS_CALL_TABLE
) {
1614 int *pint
= g_malloc(sizeof(int));
1616 *pint
= field_get_value_int(e
, info
, LTT_FIELD_ID
);
1617 q
= g_quark_from_string(field_get_value_string(e
, info
, LTT_FIELD_SYMBOL
));
1618 g_hash_table_insert(syscall_table
, pint
, (gpointer
)(unsigned long)q
);
1620 else if(tfc
->tf
->name
== LTT_CHANNEL_IRQ_STATE
&& info
->name
== LTT_EVENT_LIST_INTERRUPT
) {
1622 int *pint
= g_malloc(sizeof(int));
1624 *pint
= field_get_value_int(e
, info
, LTT_FIELD_IRQ_ID
);
1625 q
= g_quark_from_string(field_get_value_string(e
, info
, LTT_FIELD_ACTION
));
1626 g_hash_table_insert(irq_table
, pint
, (gpointer
)(unsigned long)q
);
1628 else if(tfc
->tf
->name
== LTT_CHANNEL_SOFTIRQ_STATE
&& info
->name
== LTT_EVENT_SOFTIRQ_VEC
) {
1630 int *pint
= g_malloc(sizeof(int));
1632 *pint
= field_get_value_int(e
, info
, LTT_FIELD_ID
);
1633 q
= g_quark_from_string(field_get_value_string(e
, info
, LTT_FIELD_SYMBOL
));
1634 g_hash_table_insert(softirq_table
, pint
, (gpointer
)(unsigned long)q
);
1638 /* Only look at events after the statedump is finished.
1639 * Before that, the pids in the LttvProcessState are not reliable
1641 if(statedump_finished
== 0) {
1642 if(tfc
->tf
->name
== LTT_CHANNEL_GLOBAL_STATE
&& info
->name
== LTT_EVENT_STATEDUMP_END
)
1643 statedump_finished
= 1;
1649 pinfo
= get_or_init_process_info(e
, process
->name
, differentiate_swappers(process
->pid
, e
), NULL
);
1651 /* the state machine
1652 * Process the event in the context of each process
1655 if(tfc
->tf
->name
== LTT_CHANNEL_KERNEL
&& info
->name
== LTT_EVENT_IRQ_ENTRY
) {
1656 struct process
*event_process_info
= pinfo
;
1657 struct sstack_item
*item
;
1659 item
= prepare_push_item(event_process_info
, LLEV_IRQ
, e
->event_time
);
1660 ((struct llev_state_info_irq
*) item_private(item
))->irq
= field_get_value_int(e
, info
, LTT_FIELD_IRQ_ID
);
1661 commit_item(event_process_info
, item
);
1663 else if(tfc
->tf
->name
== LTT_CHANNEL_KERNEL
&& info
->name
== LTT_EVENT_IRQ_EXIT
) {
1664 struct process
*event_process_info
= pinfo
;
1666 prepare_pop_item_commit(event_process_info
, LLEV_IRQ
, e
->event_time
);
1668 else if(tfc
->tf
->name
== LTT_CHANNEL_KERNEL
&& info
->name
== LTT_EVENT_SCHED_SCHEDULE
) {
1669 int next_pid
= field_get_value_int(e
, info
, LTT_FIELD_NEXT_PID
);
1670 int prev_pid
= field_get_value_int(e
, info
, LTT_FIELD_PREV_PID
);
1672 struct process
*event_process_info
= get_or_init_process_info(e
, process
->name
, differentiate_swappers(next_pid
, e
), NULL
);
1673 prepare_pop_item_commit(event_process_info
, LLEV_PREEMPTED
, e
->event_time
);
1676 struct sstack_item
*item
;
1677 struct process
*event_process_info
= get_or_init_process_info(e
, process
->name
, differentiate_swappers(prev_pid
, e
), NULL
);
1679 item
= prepare_push_item(event_process_info
, LLEV_PREEMPTED
, e
->event_time
);
1680 ((struct llev_state_info_preempted
*) item_private(item
))->prev_state
= field_get_value_int(e
, info
, LTT_FIELD_PREV_STATE
);
1681 commit_item(event_process_info
, item
);
1684 else if(tfc
->tf
->name
== LTT_CHANNEL_KERNEL
&& info
->name
== LTT_EVENT_TRAP_ENTRY
) {
1685 struct process
*event_process_info
= pinfo
;
1686 struct sstack_item
*item
;
1688 item
= prepare_push_item(event_process_info
, LLEV_TRAP
, e
->event_time
);
1689 commit_item(event_process_info
, item
);
1691 else if(tfc
->tf
->name
== LTT_CHANNEL_KERNEL
&& info
->name
== LTT_EVENT_TRAP_EXIT
) {
1692 struct process
*event_process_info
= pinfo
;
1694 prepare_pop_item_commit(event_process_info
, LLEV_TRAP
, e
->event_time
);
1696 else if(tfc
->tf
->name
== LTT_CHANNEL_KERNEL
&& info
->name
== LTT_EVENT_SYSCALL_ENTRY
) {
1697 struct process
*event_process_info
= pinfo
;
1698 struct sstack_item
*item
;
1700 item
= prepare_push_item(event_process_info
, LLEV_SYSCALL
, e
->event_time
);
1701 ((struct llev_state_info_syscall
*) item_private(item
))->syscall_id
= field_get_value_int(e
, info
, LTT_FIELD_SYSCALL_ID
);
1702 ((struct llev_state_info_syscall
*) item_private(item
))->substate
= LLEV_SYSCALL__UNDEFINED
;
1703 commit_item(event_process_info
, item
);
1705 else if(tfc
->tf
->name
== LTT_CHANNEL_KERNEL
&& info
->name
== LTT_EVENT_SYSCALL_EXIT
) {
1706 struct process
*event_process_info
= pinfo
;
1708 prepare_pop_item_commit(event_process_info
, LLEV_SYSCALL
, e
->event_time
);
1710 else if(tfc
->tf
->name
== LTT_CHANNEL_KERNEL
&& info
->name
== LTT_EVENT_SOFT_IRQ_ENTRY
) {
1711 struct process
*event_process_info
= pinfo
;
1712 struct sstack_item
*item
;
1714 item
= prepare_push_item(event_process_info
, LLEV_SOFTIRQ
, e
->event_time
);
1715 ((struct llev_state_info_softirq
*) item_private(item
))->softirq
= field_get_value_int(e
, info
, LTT_FIELD_SOFT_IRQ_ID
);
1716 commit_item(event_process_info
, item
);
1718 else if(tfc
->tf
->name
== LTT_CHANNEL_KERNEL
&& info
->name
== LTT_EVENT_SOFT_IRQ_EXIT
) {
1719 struct process
*event_process_info
= pinfo
;
1721 prepare_pop_item_commit(event_process_info
, LLEV_SOFTIRQ
, e
->event_time
);
1723 else if(tfc
->tf
->name
== LTT_CHANNEL_KERNEL
&& info
->name
== LTT_EVENT_PROCESS_FORK
) {
1724 int pid
= differentiate_swappers(field_get_value_int(e
, info
, LTT_FIELD_CHILD_PID
), e
);
1725 struct process
*event_process_info
= get_or_init_process_info(e
, process
->name
, pid
, NULL
);
1726 struct sstack_item
*item
;
1728 event_process_info
->parent
= process
->pid
;
1730 //print_time(e->event_time);
1731 //printf(", fork in process %d (%s), creating child %d\n", differentiate_swappers(process->pid, e), g_quark_to_string(process->name), pid);
1733 item
= prepare_push_item(event_process_info
, LLEV_RUNNING
, e
->event_time
);
1734 commit_item(event_process_info
, item
);
1735 item
= prepare_push_item(event_process_info
, LLEV_SYSCALL
, e
->event_time
);
1736 /* FIXME: this sets fork() as syscall, it's pretty inelegant */
1737 ((struct llev_state_info_syscall
*) item_private(item
))->syscall_id
= 57;
1738 ((struct llev_state_info_syscall
*) item_private(item
))->substate
= LLEV_SYSCALL__UNDEFINED
;
1739 commit_item(event_process_info
, item
);
1741 item
= prepare_push_item(event_process_info
, LLEV_PREEMPTED
, e
->event_time
);
1742 /* Consider fork as BLOCKED */
1743 ((struct llev_state_info_preempted
*) item_private(item
))->prev_state
= 1;
1744 commit_item(event_process_info
, item
);
1746 //printf("process %d now has a stack of height %d\n", differentiate_swappers(process->pid, e), get_or_init_process_info(e, process->name, differentiate_swappers(process->pid, cpu), NULL)->stack_current-1);
1749 else if(tfc
->tf
->name
== LTT_CHANNEL_FS
&& info
->name
== LTT_EVENT_EXEC
) {
1750 struct process
*event_process_info
= pinfo
;
1752 guint cpu
= tfs
->cpu
;
1753 LttvProcessState
*process_state
= ts
->running_process
[cpu
];
1754 event_process_info
->name
= process_state
->name
;
1756 else if(tfc
->tf
->name
== LTT_CHANNEL_FS
&& info
->name
== LTT_EVENT_OPEN
) {
1757 struct process_state
*pstate
= process_find_state(pinfo
, LLEV_SYSCALL
);
1758 struct llev_state_info_syscall
*llev_syscall_private
;
1759 struct llev_state_info_syscall__open
*llev_syscall_open_private
;
1761 /* TODO: this is too easy */
1765 llev_syscall_private
= (struct llev_state_info_syscall
*)pstate
->private;
1767 //printf("depanalysis: found an open with state %d in pid %d\n", pstate->bstate, process->pid);
1768 if(pstate
->bstate
== LLEV_UNKNOWN
)
1771 g_assert(pstate
->bstate
== LLEV_SYSCALL
);
1772 g_assert(llev_syscall_private
->substate
== LLEV_SYSCALL__UNDEFINED
);
1774 llev_syscall_private
->substate
= LLEV_SYSCALL__OPEN
;
1775 //printf("setting substate LLEV_SYSCALL__OPEN on syscall_private %p\n", llev_syscall_private);
1776 llev_syscall_private
->private = g_malloc(sizeof(struct llev_state_info_syscall__open
));
1777 llev_syscall_open_private
= llev_syscall_private
->private;
1779 llev_syscall_open_private
->filename
= g_quark_from_string(field_get_value_string(e
, info
, LTT_FIELD_FILENAME
));
1782 else if(tfc
->tf
->name
== LTT_CHANNEL_FS
&& info
->name
== LTT_EVENT_READ
) {
1783 struct process_state
*pstate
= process_find_state(pinfo
, LLEV_SYSCALL
);
1784 struct llev_state_info_syscall
*llev_syscall_private
;
1785 struct llev_state_info_syscall__read
*llev_syscall_read_private
;
1789 /* TODO: this is too easy */
1793 llev_syscall_private
= (struct llev_state_info_syscall
*)pstate
->private;
1795 //printf("depanalysis: found an read with state %d in pid %d\n", pstate->bstate, process->pid);
1796 if(pstate
->bstate
== LLEV_UNKNOWN
)
1799 g_assert(pstate
->bstate
== LLEV_SYSCALL
);
1800 g_assert(llev_syscall_private
->substate
== LLEV_SYSCALL__UNDEFINED
);
1802 llev_syscall_private
->substate
= LLEV_SYSCALL__READ
;
1803 //printf("setting substate LLEV_SYSCALL__READ on syscall_private %p\n", llev_syscall_private);
1804 llev_syscall_private
->private = g_malloc(sizeof(struct llev_state_info_syscall__read
));
1805 llev_syscall_read_private
= llev_syscall_private
->private;
1807 fd
= field_get_value_int(e
, info
, LTT_FIELD_FD
);
1808 pfileq
= (GQuark
)(unsigned long)g_hash_table_lookup(process
->fds
, &fd
);
1810 llev_syscall_read_private
->filename
= pfileq
;
1814 res
= asprintf(&tmp
, "Unknown filename, fd %d", fd
);
1816 llev_syscall_read_private
->filename
= g_quark_from_string(tmp
);
1820 else if(tfc
->tf
->name
== LTT_CHANNEL_FS
&& info
->name
== LTT_EVENT_POLL_EVENT
) {
1821 struct process_state
*pstate
= process_find_state(pinfo
, LLEV_SYSCALL
);
1822 struct llev_state_info_syscall
*llev_syscall_private
;
1823 struct llev_state_info_syscall__poll
*llev_syscall_poll_private
;
1827 /* TODO: this is too easy */
1831 llev_syscall_private
= (struct llev_state_info_syscall
*)pstate
->private;
1833 //printf("depanalysis: found an poll with state %d in pid %d\n", pstate->bstate, process->pid);
1834 if(pstate
->bstate
== LLEV_UNKNOWN
)
1837 /* poll doesn't have a single event that gives the syscall args. instead, there can be an arbitrary
1838 * number of fs_pollfd or fd_poll_event events
1839 * We use the fd_poll_event event, which occurs for each fd that had activity causing a return of the poll()
1840 * For now we only use the first.
1841 * We should do something about this. FIXME
1843 if(llev_syscall_private
->substate
== LLEV_SYSCALL__POLL
)
1846 g_assert(pstate
->bstate
== LLEV_SYSCALL
);
1847 g_assert(llev_syscall_private
->substate
== LLEV_SYSCALL__UNDEFINED
);
1849 llev_syscall_private
->substate
= LLEV_SYSCALL__POLL
;
1850 //printf("setting substate LLEV_SYSCALL__POLL on syscall_private %p\n", llev_syscall_private);
1851 llev_syscall_private
->private = g_malloc(sizeof(struct llev_state_info_syscall__poll
));
1852 llev_syscall_poll_private
= llev_syscall_private
->private;
1854 fd
= field_get_value_int(e
, info
, LTT_FIELD_FD
);
1855 pfileq
= (GQuark
)(unsigned long)g_hash_table_lookup(process
->fds
, &fd
);
1857 llev_syscall_poll_private
->filename
= pfileq
;
1861 res
= asprintf(&tmp
, "Unknown filename, fd %d", fd
);
1863 llev_syscall_poll_private
->filename
= g_quark_from_string(tmp
);
1867 else if(tfc
->tf
->name
== LTT_CHANNEL_KERNEL
&& info
->name
== LTT_EVENT_SCHED_TRY_WAKEUP
) {
1868 struct sstack_event
*se
= g_malloc(sizeof(struct sstack_event
));
1869 struct try_wakeup_event
*twe
= g_malloc(sizeof(struct try_wakeup_event
));
1870 struct sstack_item
*item
= sstack_item_new_event();
1871 int target
= field_get_value_int(e
, info
, LTT_FIELD_PID
);
1872 struct process
*target_pinfo
;
1875 se
->event_type
= HLEV_EVENT_TRY_WAKEUP
;
1877 //printf("pushing try wake up event in context of %d\n", pinfo->pid);
1879 twe
->pid
= differentiate_swappers(process
->pid
, e
);
1880 twe
->time
= e
->event_time
;
1883 /* FIXME: the target could not yet have an entry in the hash table, we would then lose data */
1884 target_pinfo
= g_hash_table_lookup(process_hash_table
, &target
);
1888 item
->data_val
= se
;
1889 item
->delete_data_val
= (void (*)(void *))delete_data_val
;
1891 sstack_add_item(target_pinfo
->stack
, item
);
1893 /* Now pop the blocked schedule out of the target */
1894 result
= try_pop_blocked_llev_preempted(target_pinfo
, e
->event_time
);
1897 struct sstack_item
*item
;
1898 struct process
*event_process_info
= target_pinfo
;
1900 item
= prepare_push_item(event_process_info
, LLEV_PREEMPTED
, e
->event_time
);
1901 ((struct llev_state_info_preempted
*) item_private(item
))->prev_state
= -1; /* special value meaning post-block sched out */
1902 commit_item(event_process_info
, item
);
1911 void print_sstack_private(struct sstack_item
*item
)
1913 struct process_with_state
*pwstate
= item
->data_val
;
1915 if(pwstate
&& item
->data_type
== SSTACK_TYPE_PUSH
)
1916 printf("\tstate: %s", llev_state_infos
[pwstate
->state
.bstate
].name
);
1919 print_time(pwstate
->state
.time_begin
);
1921 print_time(pwstate
->state
.time_end
);
1926 static LttTime
ltt_time_from_string(const char *str
)
1930 char *decdot
= strchr(str
, '.');
1934 retval
.tv_nsec
= atol(decdot
+1);
1940 retval
.tv_sec
= atol(str
);
1945 static void arg_t1(void *hook_data
)
1948 depanalysis_use_time
|= 1;
1949 depanalysis_time1
= ltt_time_from_string(arg_t1_str
);
1952 static void arg_t2(void *hook_data
)
1954 depanalysis_use_time
|= 2;
1955 depanalysis_time2
= ltt_time_from_string(arg_t2_str
);
1958 static void arg_pid(void *hook_data
)
1962 static void arg_limit(void *hook_data
)
1966 static void arg_sum(void *hook_data
)
1974 print_sstack_item_data
= print_sstack_private
;
1976 LttvAttributeValue value
;
1978 LttvIAttribute
*attributes
= LTTV_IATTRIBUTE(lttv_global_attributes());
1982 lttv_option_add("dep-time-start", 0, "dependency analysis time of analysis start", "time",
1983 LTTV_OPT_STRING
, &arg_t1_str
, arg_t1
, NULL
);
1984 lttv_option_add("dep-time-end", 0, "dependency analysis time of analysis end", "time",
1985 LTTV_OPT_STRING
, &arg_t2_str
, arg_t2
, NULL
);
1986 lttv_option_add("dep-pid", 0, "dependency analysis pid", "pid",
1987 LTTV_OPT_INT
, &depanalysis_range_pid_searching
, arg_pid
, NULL
);
1988 lttv_option_add("limit-events", 0, "dependency limit event count", "count",
1989 LTTV_OPT_INT
, &depanalysis_event_limit
, arg_limit
, NULL
);
1990 lttv_option_add("print-summary", 0, "print simple summary", "sum",
1991 LTTV_OPT_INT
, &a_print_simple_summary
, arg_sum
, NULL
);
1993 process_hash_table
= g_hash_table_new(g_int_hash
, g_int_equal
);
1994 syscall_table
= g_hash_table_new(g_int_hash
, g_int_equal
);
1995 irq_table
= g_hash_table_new(g_int_hash
, g_int_equal
);
1996 softirq_table
= g_hash_table_new(g_int_hash
, g_int_equal
);
1998 a_string
= g_string_new("");
2000 result
= lttv_iattribute_find_by_path(attributes
, "hooks/event",
2001 LTTV_POINTER
, &value
);
2003 event_hook
= *(value
.v_pointer
);
2004 g_assert(event_hook
);
2005 lttv_hooks_add(event_hook
, process_event
, NULL
, LTTV_PRIO_DEFAULT
);
2007 result
= lttv_iattribute_find_by_path(attributes
, "hooks/traceset/before",
2008 LTTV_POINTER
, &value
);
2010 before_traceset
= *(value
.v_pointer
);
2011 g_assert(before_traceset
);
2012 lttv_hooks_add(before_traceset
, write_traceset_header
, NULL
,
2015 result
= lttv_iattribute_find_by_path(attributes
, "hooks/traceset/after",
2016 LTTV_POINTER
, &value
);
2018 after_traceset
= *(value
.v_pointer
);
2019 g_assert(after_traceset
);
2020 lttv_hooks_add(after_traceset
, write_traceset_footer
, NULL
,
2024 static void destroy()
2026 lttv_option_remove("dep-time-start");
2027 lttv_option_remove("dep-time-end");
2028 lttv_option_remove("dep-pid");
2029 lttv_option_remove("limit-events");
2030 lttv_option_remove("print-summary");
2032 g_hash_table_destroy(process_hash_table
);
2033 g_hash_table_destroy(syscall_table
);
2034 g_hash_table_destroy(irq_table
);
2035 g_hash_table_destroy(softirq_table
);
2037 g_string_free(a_string
, TRUE
);
2039 lttv_hooks_remove_data(event_hook
, write_event_content
, NULL
);
2041 lttv_hooks_remove_data(before_traceset
, write_traceset_header
, NULL
);
2042 lttv_hooks_remove_data(after_traceset
, write_traceset_footer
, NULL
);
2045 LTTV_MODULE("depanalysis", "Dependency analysis test", \
2046 "Produce a dependency analysis of a trace", \
2047 init
, destroy
, "stats", "batchAnalysis", "option", "print")