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,
23 #include <lttv/lttv.h>
24 #include <lttv/option.h>
25 #include <lttv/module.h>
26 #include <lttv/hook.h>
27 #include <lttv/attribute.h>
28 #include <lttv/iattribute.h>
29 #include <lttv/stats.h>
30 #include <lttv/filter.h>
31 #include <lttv/print.h>
33 #include <ltt/event.h>
34 #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 LttTime depanalysis_time1
, depanalysis_time2
;
53 static char *arg_t1_str
,*arg_t2_str
;
54 static int statedump_finished
= 0;
57 struct llev_state_info_irq
{
61 struct llev_state_info_softirq
{
65 struct llev_state_info_syscall
{
73 struct llev_state_info_syscall__open
{
77 struct llev_state_info_syscall__read
{
81 struct llev_state_info_syscall__poll
{
85 struct llev_state_info_preempted
{
89 struct hlev_state_info_blocked
{
91 unsigned char trap
; /* flag */
94 /* Garray of pointers to struct process_state that reflect the
95 * low-level state stack when respectively entering and exiting the blocked
98 GArray
*llev_state_entry
;
99 GArray
*llev_state_exit
;
101 int pid_exit
; /* FIXME: it's not pretty to have this here; find this info elsewhere */
107 struct hlev_state_info_blocked__open
{
111 struct hlev_state_info_blocked__read
{
115 struct hlev_state_info_blocked__poll
{
119 struct hlev_state_info_interrupted_irq
{
123 struct hlev_state_info_interrupted_softirq
{
127 struct summary_tree_node
{
129 GHashTable
*children
;
141 struct state_info llev_state_infos
[] = {
142 { "UNKNOWN", 0, { NULL
} },
143 { "RUNNING", 0, { NULL
} },
144 { "SYSCALL", sizeof(struct llev_state_info_syscall
), { NULL
} },
145 { "IRQ", sizeof(struct llev_state_info_irq
), { NULL
} },
146 { "SOFTIRQ", sizeof(struct llev_state_info_softirq
), { NULL
} },
147 { "TRAP", 0, { NULL
} },
148 { "PREEMPTED", sizeof(struct llev_state_info_preempted
), { NULL
} },
151 struct state_info hlev_state_infos
[] = {
152 { "UNKNOWN", 0, { "Total", "Unknown", NULL
} },
153 { "RUNNING", 0, { "Total", "Working", NULL
} },
154 { "BLOCKED", sizeof(struct hlev_state_info_blocked
), { "Total", "Blocked", NULL
} },
155 { "INTERRUPTED_IRQ", sizeof(struct hlev_state_info_interrupted_irq
), { "Total", "Interrupted", "IRQ", NULL
} },
156 { "INTERRUPTED_SOFTIRQ", sizeof(struct hlev_state_info_interrupted_softirq
), { "Total", "Interrupted", "SoftIRQ", NULL
} },
157 { "INTERRUPTED_CPU", 0, { "Total", "Interrupted", "Preempted", NULL
} },
158 { "INTERRUPTED_POST_BLOCK", 0, { "Total", "Interrupted", "Waiting schedule after blocking", NULL
} },
171 enum llev_syscall_substate
{
172 LLEV_SYSCALL__UNDEFINED
,
179 HLEV_EVENT_TRY_WAKEUP
=0,
186 HLEV_INTERRUPTED_IRQ
,
187 HLEV_INTERRUPTED_SOFTIRQ
,
188 HLEV_INTERRUPTED_CPU
,
189 HLEV_INTERRUPTED_POST_BLOCK
,
192 enum hlev_state_blocked
{
193 HLEV_BLOCKED__UNDEFINED
,
199 struct sstack_event
{
204 struct try_wakeup_event
{
205 int pid
; /* this sould be more precise avec pid may be reused */
207 struct process
*waker
;
210 struct process_state
{
219 struct process_with_state
{
220 struct process
*process
;
221 struct process_state state
;
224 #define PROCESS_STATE_STACK_SIZE 10
230 struct sstack
*stack
;
231 struct process_state
*llev_state_stack
[PROCESS_STATE_STACK_SIZE
];
233 struct process_state
*hlev_state
;
234 GArray
*hlev_history
;
237 static inline void *old_process_state_private_data(struct process
*p
)
239 return p
->llev_state_stack
[p
->stack_current
]->private;
242 static inline struct process_state
*process_find_state(struct process
*p
, enum llev_state st
)
246 for(i
=p
->stack
->array
->len
-1; i
>=0; i
--) {
247 struct sstack_item
*item
= g_array_index(p
->stack
->array
, struct sstack_item
*, i
);
249 struct process_with_state
*pwstate
= item
->data_val
;
250 if(pwstate
->state
.bstate
== st
) {
251 return &pwstate
->state
;
258 static int find_pos_in_stack(enum llev_state lls
, struct process
*p
)
261 for(i
=p
->stack_current
; i
>=0; i
--) {
262 if(p
->llev_state_stack
[i
]->bstate
== lls
)
269 static struct process_state
*find_in_stack(enum llev_state lls
, struct process
*p
)
273 result
= find_pos_in_stack(lls
, p
);
276 return p
->llev_state_stack
[result
];
282 /* called back from sstack on deletion of a data_val which is
283 * a struct process_with_state
286 static void delete_data_val(struct process_with_state
*pwstate
)
288 // FIXME: Free this also
289 //g_free(pwstate->state.private);
291 // FIXME: this is really ugly. Don't free the pwstate if the state is LLEV_RUNNING.
292 // LLEV_RUNNING is a special case that's being processed and deleted immediately after
293 // being inserted on the sstack, to prevent state begin accumulated because it couldn't
294 // be processed before the end of the trace. If we free the state, we get invalid memory
295 // reads when looking at it on the state_stack.
296 //if(pwstate->state.bstate != LLEV_RUNNING)
300 static struct sstack_item
*prepare_push_item(struct process
*p
, enum llev_state st
, LttTime t
)
302 struct process_with_state
*pwstate
= g_malloc(sizeof(struct process_with_state
));
303 struct sstack_item
*item
;
305 int wait_for_pop
= 0;
307 if(st
== LLEV_SYSCALL
) {
308 /* We need to push LLEV_SYSCALL as wait_for_pop because it depends on some of
309 * its children. If we don't do this, it's going to get processed immediately
310 * by the sstack and we might miss some details about it that will come later.
315 item
= sstack_item_new_push(wait_for_pop
);
317 //printf("pushing in context of %d\n", p->pid);
319 pwstate
->process
= p
;
320 pwstate
->state
.bstate
= st
;
321 pwstate
->state
.time_begin
= t
;
322 pwstate
->state
.private = g_malloc(llev_state_infos
[st
].size_priv
);
324 item
->data_val
= pwstate
;
325 item
->delete_data_val
= delete_data_val
;
328 static void *item_private(struct sstack_item
*item
)
330 struct process_with_state
*pwstate
= item
->data_val
;
331 return pwstate
->state
.private;
334 static void commit_item(struct process
*p
, struct sstack_item
*item
)
336 sstack_add_item(p
->stack
, item
);
339 static void old_process_push_llev_state(struct process
*p
, struct process_state
*pstate
)
341 if(++p
->stack_current
>= PROCESS_STATE_STACK_SIZE
) {
342 fprintf(stderr
, "depanalysis: internal process stack overflow\n");
346 p
->llev_state_stack
[p
->stack_current
] = pstate
;
349 static void live_complete_process_push_llev_state(struct process
*p
, enum llev_state st
, LttTime t
)
351 struct process_state
*pstate
= g_malloc(sizeof(struct process_state
));
354 pstate
->time_begin
= t
;
355 pstate
->private = g_malloc(llev_state_infos
[st
].size_priv
);
357 old_process_push_llev_state(p
, pstate
);
360 static void prepare_pop_item_commit_nocheck(struct process
*p
, enum llev_state st
, LttTime t
)
362 struct process_with_state
*pwstate
;
363 struct sstack_item
*item
= sstack_item_new_pop();
367 if(p
->stack
->pushes
->len
> 0)
368 push_idx
= g_array_index(p
->stack
->pushes
, int, p
->stack
->pushes
->len
-1);
373 pwstate
= g_array_index(p
->stack
->array
, struct sstack_item
*, push_idx
)->data_val
;
374 pwstate
->process
= p
;
375 pwstate
->state
.time_end
= t
;
376 item
->data_val
= pwstate
;
377 /* don't set delete_data_val because we use the same pwstate as push, and we don't want to free it twice */
381 pwstate
= g_malloc(sizeof(struct process_with_state
));
382 pwstate
->process
= p
;
383 item
->data_val
= pwstate
;
384 pwstate
->state
.time_end
= t
;
385 pwstate
->state
.bstate
= st
;
388 sstack_add_item(p
->stack
, item
);
392 static void prepare_pop_item_commit(struct process
*p
, enum llev_state st
, LttTime t
)
394 struct process_with_state
*pwstate
;
395 struct sstack_item
*item
= sstack_item_new_pop();
399 if(p
->stack
->pushes
->len
> 0)
400 push_idx
= g_array_index(p
->stack
->pushes
, int, p
->stack
->pushes
->len
-1);
405 /* FIXME: ugly workaround for kernel bug that generates two kernel_arch_syscall_exit on fork.
406 * The bug only occurs upon creation of new processes. But these processes always have
407 * a LLEV_RUNNING at index 0. */
408 if(push_idx
>= p
->stack
->array
->len
)
411 pwstate
= g_array_index(p
->stack
->array
, struct sstack_item
*, push_idx
)->data_val
;
413 if(pwstate
->state
.bstate
!= st
) {
414 /* FIXME: ugly workaround for kernel bug that generates two kernel_arch_syscall_exit on fork */
415 if(st
!= LLEV_SYSCALL
) {
416 printf("bad pop! at ");
419 print_stack(p
->stack
);
423 /* case where we have a double syscall_exit */
429 prepare_pop_item_commit_nocheck(p
, st
, t
);
433 static int try_pop_blocked_llev_preempted(struct process
*p
, LttTime t
)
436 struct process_with_state
*pwstate
;
438 if(p
->stack
->pushes
->len
> 0)
439 push_idx
= g_array_index(p
->stack
->pushes
, int, p
->stack
->pushes
->len
-1);
444 pwstate
= g_array_index(p
->stack
->array
, struct sstack_item
*, push_idx
)->data_val
;
446 if(!(pwstate
->state
.bstate
== LLEV_PREEMPTED
&& ((struct llev_state_info_preempted
*)pwstate
->state
.private)->prev_state
> 0)) {
447 printf("double try wake up\n");
452 prepare_pop_item_commit_nocheck(p
, LLEV_PREEMPTED
, t
);
456 static void old_process_pop_llev_state(struct process
*p
, struct process_state
*pstate
)
458 /* Ensure we are really popping the current state */
459 /* FIXME: pstate->bstate is uninitialized? */
460 // Commenting because it does not work. The way things work now, this check cannot work.
461 //if(p->llev_state_stack[p->stack_current]->bstate != LLEV_UNKNOWN && p->llev_state_stack[p->stack_current]->bstate != pstate->bstate) {
462 // printf("ERROR! bad pop!\n");
466 /* Actually change the that position */
467 if(p
->stack_current
>= 0)
470 /* If stack empty, we must put something in it */
471 if(p
->stack_current
== -1) {
472 if(pstate
->bstate
== LLEV_SYSCALL
) {
473 //process_push_llev_state(p, LLEV_RUNNING, pstate->time_end);
474 live_complete_process_push_llev_state(p
, LLEV_RUNNING
, pstate
->time_end
);
477 live_complete_process_push_llev_state(p
, LLEV_UNKNOWN
, pstate
->time_end
);
482 static GHashTable
*process_hash_table
;
483 static GHashTable
*syscall_table
;
484 static GHashTable
*irq_table
;
485 static GHashTable
*softirq_table
;
487 /* Insert the hooks before and after each trace and tracefile, and for each
488 event. Print a global header. */
492 static GString
*a_string
;
494 static gboolean
write_traceset_header(void *hook_data
, void *call_data
)
496 LttvTracesetContext
*tc
= (LttvTracesetContext
*)call_data
;
498 g_info("Traceset header");
500 /* Print the trace set header */
501 fprintf(a_file
,"Trace set contains %d traces\n\n",
502 lttv_traceset_number(tc
->ts
));
507 inline void print_time(LttTime t
)
509 //printf("%lu.%lu", t.tv_sec, t.tv_nsec);
511 f
= (double)t
.tv_sec
+ ((double)t
.tv_nsec
)/1000000000.0;
515 GArray
*oldstyle_stack_to_garray(struct process_state_stack
**oldstyle_stack
, int current
)
520 retval
= g_array_new(FALSE
, FALSE
, sizeof(struct process_state_stack
*));
522 for(i
=0; i
<current
; i
++) {
523 g_array_append_val(retval
, oldstyle_stack
[i
]);
529 static void update_hlev_state(struct process
*p
, LttTime t
)
533 enum hlev_state new_hlev
;
535 for(i
=p
->stack_current
; i
>=0; i
--) {
537 st
= p
->llev_state_stack
[i
]->bstate
;
539 if(st
== LLEV_RUNNING
|| st
== LLEV_TRAP
|| st
== LLEV_SYSCALL
) {
540 new_hlev
= HLEV_RUNNING
;
543 else if(st
== LLEV_IRQ
) {
544 new_hlev
= HLEV_INTERRUPTED_IRQ
;
547 else if(st
== LLEV_SOFTIRQ
) {
548 new_hlev
= HLEV_INTERRUPTED_SOFTIRQ
;
551 else if(st
== LLEV_PREEMPTED
) {
552 int prev_state
= ((struct llev_state_info_preempted
*) old_process_state_private_data(p
))->prev_state
;
554 if(prev_state
== 0) {
555 new_hlev
= HLEV_INTERRUPTED_CPU
;
557 else if(prev_state
== -1) {
558 new_hlev
= HLEV_INTERRUPTED_POST_BLOCK
;
561 new_hlev
= HLEV_BLOCKED
;
565 else if(st
== LLEV_UNKNOWN
) {
566 new_hlev
= HLEV_UNKNOWN
;
574 /* If no state change, do nothing */
575 if(p
->hlev_state
!= NULL
&& new_hlev
== p
->hlev_state
->bstate
) {
579 p
->hlev_state
->time_end
= t
;
580 /* This check is here because we initially put HLEV_UNKNOWN as hlev state, but in the case
581 * of processes newly created, it is immediately replaced by HLEV_BLOCKED. In order to avoid
582 * having a UNKNOWN state of duration 0 in the summary, we don't add it. This isn't as elegant
585 if(ltt_time_compare(p
->hlev_state
->time_begin
, p
->hlev_state
->time_end
) != 0)
586 g_array_append_val(p
->hlev_history
, p
->hlev_state
);
587 p
->hlev_state
= g_malloc(sizeof(struct process_state
));
588 p
->hlev_state
->bstate
= new_hlev
;
589 p
->hlev_state
->time_begin
= t
;
590 p
->hlev_state
->private = g_malloc(hlev_state_infos
[new_hlev
].size_priv
);
592 //printf("depanalysis: now at hlev state %s\n", hlev_state_infos[new_hlev].name);
594 /* Set private data */
595 switch(p
->hlev_state
->bstate
) {
601 struct hlev_state_info_blocked
*hlev_blocked_private
= p
->hlev_state
->private;
602 //struct process_state *ps = find_in_stack(LLEV_SYSCALL, p);
603 int syscall_pos
= find_pos_in_stack(LLEV_SYSCALL
, p
);
604 int trap_pos
= find_pos_in_stack(LLEV_TRAP
, p
);
607 hlev_blocked_private
->syscall_id
= 1;
608 hlev_blocked_private
->trap
= 0;
609 hlev_blocked_private
->substate
= HLEV_BLOCKED__UNDEFINED
;
610 hlev_blocked_private
->private = NULL
;
611 hlev_blocked_private
->llev_state_entry
= oldstyle_stack_to_garray(p
->llev_state_stack
, p
->stack_current
);
612 hlev_blocked_private
->llev_state_exit
= NULL
;
614 //g_assert(syscall_pos >= 0 || trap_pos >= 0);
616 if(trap_pos
> syscall_pos
) {
617 hlev_blocked_private
->trap
= 1;
620 /* initial value, may be changed below */
621 hlev_blocked_private
->substate
= HLEV_BLOCKED__UNDEFINED
;
623 if(syscall_pos
>= 0) {
624 struct process_state
*ps
= p
->llev_state_stack
[syscall_pos
];
625 struct llev_state_info_syscall
*llev_syscall_private
= (struct llev_state_info_syscall
*) ps
->private;
626 hlev_blocked_private
->syscall_id
= llev_syscall_private
->syscall_id
;
628 if(llev_syscall_private
->substate
== LLEV_SYSCALL__OPEN
) {
629 struct llev_state_info_syscall__open
*llev_syscall_open_private
;
630 struct hlev_state_info_blocked__open
*hlev_blocked_open_private
;
631 llev_syscall_open_private
= llev_syscall_private
->private;
632 hlev_blocked_private
->substate
= HLEV_BLOCKED__OPEN
;
633 hlev_blocked_open_private
= g_malloc(sizeof(struct hlev_state_info_blocked__open
));
634 hlev_blocked_private
->private = hlev_blocked_open_private
;
635 hlev_blocked_open_private
->filename
= llev_syscall_open_private
->filename
;
637 //printf("depanalysis: blocked in an open!\n");
639 else if(llev_syscall_private
->substate
== LLEV_SYSCALL__READ
) {
640 struct llev_state_info_syscall__read
*llev_syscall_read_private
;
641 struct hlev_state_info_blocked__read
*hlev_blocked_read_private
;
642 llev_syscall_read_private
= llev_syscall_private
->private;
643 hlev_blocked_private
->substate
= HLEV_BLOCKED__READ
;
644 hlev_blocked_read_private
= g_malloc(sizeof(struct hlev_state_info_blocked__read
));
645 hlev_blocked_private
->private = hlev_blocked_read_private
;
646 hlev_blocked_read_private
->filename
= llev_syscall_read_private
->filename
;
648 //printf("depanalysis: blocked in a read!\n");
650 else if(llev_syscall_private
->substate
== LLEV_SYSCALL__POLL
) {
651 struct llev_state_info_syscall__poll
*llev_syscall_poll_private
;
652 struct hlev_state_info_blocked__poll
*hlev_blocked_poll_private
;
653 llev_syscall_poll_private
= llev_syscall_private
->private;
654 hlev_blocked_private
->substate
= HLEV_BLOCKED__POLL
;
655 hlev_blocked_poll_private
= g_malloc(sizeof(struct hlev_state_info_blocked__poll
));
656 hlev_blocked_private
->private = hlev_blocked_poll_private
;
657 hlev_blocked_poll_private
->filename
= llev_syscall_poll_private
->filename
;
659 //printf("depanalysis: blocked in a read!\n");
663 hlev_blocked_private
->syscall_id
= -1;
668 case HLEV_INTERRUPTED_IRQ
: {
669 struct hlev_state_info_interrupted_irq
*sinfo
= p
->hlev_state
->private;
670 struct process_state
*ps
= find_in_stack(LLEV_IRQ
, p
);
674 sinfo
->irq
= ((struct llev_state_info_irq
*) ps
->private)->irq
;
677 case HLEV_INTERRUPTED_SOFTIRQ
: {
678 struct hlev_state_info_interrupted_softirq
*sinfo
= p
->hlev_state
->private;
679 struct process_state
*ps
= find_in_stack(LLEV_SOFTIRQ
, p
);
683 sinfo
->softirq
= ((struct llev_state_info_softirq
*) ps
->private)->softirq
;
691 static gint
compare_summary_tree_node_times(gconstpointer a
, gconstpointer b
)
693 struct summary_tree_node
*n1
= (struct summary_tree_node
*) a
;
694 struct summary_tree_node
*n2
= (struct summary_tree_node
*) b
;
696 return ltt_time_compare(n2
->duration
, n1
->duration
);
699 /* Print an item of the simple summary tree, and recurse, printing its children.
701 * If depth == -1, this is the root: we don't print a label, we only recurse into
705 static void print_summary_item(struct summary_tree_node
*node
, int depth
)
710 printf("\t%*s (", strlen(node
->name
)+2*depth
, node
->name
);
711 print_time(node
->duration
);
712 printf(") <%d>\n", node
->id_for_episodes
);
718 vals
= g_hash_table_get_values(node
->children
);
720 /* sort the values */
721 vals
= g_list_sort(vals
, compare_summary_tree_node_times
);
724 print_summary_item((struct summary_tree_node
*)vals
->data
, depth
+1);
728 /* we must free the list returned by g_hash_table_get_values() */
732 static inline void print_irq(int irq
)
734 printf("IRQ %d [%s]", irq
, g_quark_to_string(g_hash_table_lookup(irq_table
, &irq
)));
737 static inline void print_softirq(int softirq
)
739 printf("SoftIRQ %d [%s]", softirq
, g_quark_to_string(g_hash_table_lookup(softirq_table
, &softirq
)));
742 static inline void print_pid(int pid
)
744 struct process
*event_process_info
= g_hash_table_lookup(process_hash_table
, &pid
);
748 if(event_process_info
== NULL
)
751 pname
= g_quark_to_string(event_process_info
->name
);
752 printf("%d [%s]", pid
, pname
);
755 static void modify_path_with_private(GArray
*path
, struct process_state
*pstate
)
757 //GString tmps = g_string_new("");
760 // FIXME: fix this leak
761 switch(pstate
->bstate
) {
762 case HLEV_INTERRUPTED_IRQ
:
763 asprintf(&tmps
, "IRQ %d [%s]", ((struct hlev_state_info_interrupted_irq
*)pstate
->private)->irq
, g_quark_to_string(g_hash_table_lookup(irq_table
, &((struct hlev_state_info_interrupted_irq
*)pstate
->private)->irq
)));
764 g_array_append_val(path
, tmps
);
766 case HLEV_INTERRUPTED_SOFTIRQ
:
767 asprintf(&tmps
, "SoftIRQ %d [%s]", ((struct hlev_state_info_interrupted_softirq
*)pstate
->private)->softirq
, g_quark_to_string(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 asprintf(&tmps
, "Syscall %d [%s]", hlev_blocked_private
->syscall_id
, g_quark_to_string(g_hash_table_lookup(syscall_table
, &hlev_blocked_private
->syscall_id
)));
784 g_array_append_val(path
, tmps
);
787 if(((struct hlev_state_info_blocked
*)pstate
->private)->substate
== HLEV_BLOCKED__OPEN
) {
788 char *str
= g_quark_to_string(((struct hlev_state_info_blocked__open
*)((struct hlev_state_info_blocked
*)pstate
->private)->private)->filename
);
789 g_array_append_val(path
, str
);
791 else if(((struct hlev_state_info_blocked
*)pstate
->private)->substate
== HLEV_BLOCKED__READ
) {
793 asprintf(&str
, "%s", g_quark_to_string(((struct hlev_state_info_blocked__read
*)((struct hlev_state_info_blocked
*)pstate
->private)->private)->filename
));
794 g_array_append_val(path
, str
);
795 /* FIXME: this must be freed at some point */
798 else if(((struct hlev_state_info_blocked
*)pstate
->private)->substate
== HLEV_BLOCKED__POLL
) {
800 asprintf(&str
, "%s", g_quark_to_string(((struct hlev_state_info_blocked__poll
*)((struct hlev_state_info_blocked
*)pstate
->private)->private)->filename
));
801 g_array_append_val(path
, str
);
802 /* FIXME: this must be freed at some point */
810 void print_stack_garray_horizontal(GArray
*stack
)
812 /* FIXME: this function doesn't work if we delete the states as we process them because we
813 * try to read those states here to print the low level stack.
817 for(i
=0; i
<stack
->len
; i
++) {
818 struct process_state
*pstate
= g_array_index(stack
, struct process_state
*, i
);
819 printf("%s", llev_state_infos
[pstate
->bstate
].name
);
821 if(pstate
->bstate
== LLEV_SYSCALL
) {
822 struct llev_state_info_syscall
*llev_syscall_private
= pstate
->private;
823 printf(" %d [%s]", llev_syscall_private
->syscall_id
, g_quark_to_string(g_hash_table_lookup(syscall_table
, &llev_syscall_private
->syscall_id
)));
831 static int dicho_search_state_ending_after(struct process
*p
, LttTime t
)
834 int over
= p
->hlev_history
->len
-1;
835 struct process_state
*pstate
;
841 /* If the last element is smaller or equal than the time we are searching for,
844 pstate
= g_array_index(p
->hlev_history
, struct process_state
*, over
);
845 if(ltt_time_compare(pstate
->time_end
, t
) <= 0) {
848 /* no need to check for the equal case */
850 pstate
= g_array_index(p
->hlev_history
, struct process_state
*, under
);
851 result
= ltt_time_compare(pstate
->time_end
, t
);
853 /* trivial match at the first element if it is greater or equal
854 * than the time we want
862 dicho
= (under
+over
)/2;
863 pstate
= g_array_index(p
->hlev_history
, struct process_state
*, dicho
);
864 result
= ltt_time_compare(pstate
->time_end
, t
);
869 else if(result
== 1) {
877 if(over
-under
== 1) {
878 /* we have converged */
885 /* FIXME: this shouldn't be based on pids in case of reuse
886 * FIXME: should add a list of processes used to avoid loops
889 static struct process_state
*find_state_ending_after(int pid
, LttTime t
)
895 p
= g_hash_table_lookup(process_hash_table
, &pid
);
899 result
= dicho_search_state_ending_after(p
, t
);
904 return g_array_index(p
->hlev_history
, struct process_state
*, result
);
907 static void print_delay_pid(int pid
, LttTime t1
, LttTime t2
, int offset
)
912 p
= g_hash_table_lookup(process_hash_table
, &pid
);
916 i
= dicho_search_state_ending_after(p
, t1
);
917 for(; i
<p
->hlev_history
->len
; i
++) {
918 struct process_state
*pstate
= g_array_index(p
->hlev_history
, struct process_state
*, i
);
919 if(ltt_time_compare(pstate
->time_end
, t2
) > 0)
922 if(pstate
->bstate
== HLEV_BLOCKED
) {
923 struct hlev_state_info_blocked
*state_private_blocked
;
924 state_private_blocked
= pstate
->private;
925 struct process_state
*state_unblocked
;
927 printf("%*s", 8*offset
, "");
928 printf("Blocked in ");
929 print_stack_garray_horizontal(state_private_blocked
->llev_state_entry
);
932 print_time(pstate
->time_begin
);
934 print_time(pstate
->time_end
);
936 printf(", dur: %f)\n", 1e-9*ltt_time_to_double(ltt_time_sub(pstate
->time_end
, pstate
->time_begin
)));
938 state_unblocked
= find_state_ending_after(state_private_blocked
->pid_exit
, state_private_blocked
->time_woken
);
939 if(state_unblocked
) {
940 if(state_unblocked
->bstate
== HLEV_INTERRUPTED_IRQ
) {
941 struct hlev_state_info_interrupted_irq
*priv
= state_unblocked
->private;
942 /* if in irq or softirq, we don't care what the waking process was doing because they are asynchroneous events */
943 printf("%*s", 8*offset
, "");
944 printf("Woken up by an IRQ: ");
945 print_irq(priv
->irq
);
948 else if(state_unblocked
->bstate
== HLEV_INTERRUPTED_SOFTIRQ
) {
949 struct hlev_state_info_interrupted_softirq
*priv
= state_unblocked
->private;
950 printf("%*s", 8*offset
, "");
951 printf("Woken up by a SoftIRQ: ");
952 print_softirq(priv
->softirq
);
959 if(ltt_time_compare(t1prime
, pstate
->time_begin
) < 0)
960 t1prime
= pstate
->time_begin
;
961 if(ltt_time_compare(t2prime
, pstate
->time_end
) > 0)
962 t2prime
= pstate
->time_end
;
964 print_delay_pid(state_private_blocked
->pid_exit
, t1prime
, t2prime
, offset
+1);
965 printf("%*s", 8*offset
, "");
966 printf("Woken up in context of ");
967 print_pid(state_private_blocked
->pid_exit
);
968 if(state_private_blocked
->llev_state_exit
) {
969 print_stack_garray_horizontal(state_private_blocked
->llev_state_exit
);
973 printf(" in high-level state %s", hlev_state_infos
[state_unblocked
->bstate
].name
);
978 printf("%*s", 8*offset
, "");
979 printf("Weird... cannot find in what state the waker (%d) was\n", state_private_blocked
->pid_exit
);
983 //print_delay_pid(state_private_blocked->pid_exit, pstate->time_start, pstate->time_end);
984 //printf("\t\t Woken up in context of %d: ", state_private_blocked->pid_exit);
985 //if(state_private_blocked->llev_state_exit) {
986 // print_stack_garray_horizontal(state_private_blocked->llev_state_exit);
987 // printf("here3 (%d)\n", state_private_blocked->llev_state_exit->len);
990 // printf("the private_blocked %p had a null exit stack\n", state_private_blocked);
996 static void print_range_critical_path(int process
, LttTime t1
, LttTime t2
)
998 printf("Critical path for requested range:\n");
999 printf("Final process is %d\n", process
);
1000 print_delay_pid(process
, t1
, t2
, 2);
1003 static void print_process_critical_path_summary()
1005 struct process
*pinfo
;
1009 pinfos
= g_hash_table_get_values(process_hash_table
);
1010 if(pinfos
== NULL
) {
1011 fprintf(stderr
, "error: no process found\n");
1015 printf("Process Critical Path Summary:\n");
1018 struct summary_tree_node base_node
= { children
: NULL
};
1020 struct process_state
*hlev_state_cur
;
1022 pinfo
= (struct process
*)pinfos
->data
;
1023 printf("\tProcess %d [%s]\n", pinfo
->pid
, g_quark_to_string(pinfo
->name
));
1025 if(pinfo
->hlev_history
->len
< 1)
1028 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);
1033 pinfos
= pinfos
->next
;
1039 gint
compare_states_length(gconstpointer a
, gconstpointer b
)
1041 struct process_state
**s1
= (struct process_state
**)a
;
1042 struct process_state
**s2
= (struct process_state
**)b
;
1045 val
= ltt_time_compare(ltt_time_sub((*s2
)->time_end
, (*s2
)->time_begin
), ltt_time_sub((*s1
)->time_end
, (*s1
)->time_begin
));
1049 static void print_simple_summary()
1051 struct process
*pinfo
;
1053 GList
*pinfos_first
;
1055 int id_for_episodes
= 0;
1057 /* we save all the nodes here to print the episodes table quickly */
1058 GArray
*all_nodes
= g_array_new(FALSE
, FALSE
, sizeof(struct summary_tree_node
*));
1060 pinfos_first
= g_hash_table_get_values(process_hash_table
);
1061 if(pinfos_first
== NULL
) {
1062 fprintf(stderr
, "error: no processes found\n");
1065 pinfos
= pinfos_first
;
1067 printf("Simple summary:\n");
1069 /* For each process */
1071 struct summary_tree_node base_node
= { children
: NULL
, name
: "Root" };
1073 struct process_state
*hlev_state_cur
;
1075 pinfo
= (struct process
*)pinfos
->data
;
1076 printf("\tProcess %d [%s]\n", pinfo
->pid
, g_quark_to_string(pinfo
->name
));
1078 /* For each state in the process history */
1079 for(i
=0; i
<pinfo
->hlev_history
->len
; i
++) {
1080 struct process_state
*pstate
= g_array_index(pinfo
->hlev_history
, struct process_state
*, i
);
1081 struct summary_tree_node
*node_cur
= &base_node
;
1082 GArray
*tree_path_garray
;
1084 /* Modify the path based on private data */
1085 tree_path_garray
= g_array_new(FALSE
, FALSE
, sizeof(char *));
1088 char **tree_path_cur2
= hlev_state_infos
[pstate
->bstate
].tree_path
;
1089 while(*tree_path_cur2
) {
1093 g_array_append_vals(tree_path_garray
, hlev_state_infos
[pstate
->bstate
].tree_path
, count
);
1095 modify_path_with_private(tree_path_garray
, pstate
);
1097 /* Walk the path, adding the nodes to the summary */
1098 for(j
=0; j
<tree_path_garray
->len
; j
++) {
1099 struct summary_tree_node
*newnode
;
1100 GQuark componentquark
;
1102 /* Have a path component we must follow */
1103 if(!node_cur
->children
) {
1104 /* must create the hash table for the children */
1105 node_cur
->children
= g_hash_table_new(g_int_hash
, g_int_equal
);
1108 /* try to get the node for the next component */
1109 componentquark
= g_quark_from_string(g_array_index(tree_path_garray
, char *, j
));
1110 newnode
= g_hash_table_lookup(node_cur
->children
, &componentquark
);
1111 if(newnode
== NULL
) {
1112 newnode
= g_malloc(sizeof(struct summary_tree_node
));
1113 newnode
->children
= NULL
;
1114 newnode
->name
= g_array_index(tree_path_garray
, char *, j
);
1115 newnode
->duration
= ltt_time_zero
;
1116 newnode
->id_for_episodes
= id_for_episodes
++;
1117 newnode
->episodes
= g_array_new(FALSE
, FALSE
, sizeof(struct process_state
*));
1118 g_hash_table_insert(node_cur
->children
, &componentquark
, newnode
);
1120 g_array_append_val(all_nodes
, newnode
);
1124 node_cur
->duration
= ltt_time_add(node_cur
->duration
, ltt_time_sub(pstate
->time_end
, pstate
->time_begin
));
1125 g_array_append_val(node_cur
->episodes
, pstate
);
1129 /* print the summary */
1130 print_summary_item(&base_node
, -1);
1135 pinfos
= pinfos
->next
;
1142 printf("Episode list\n");
1143 pinfos
= pinfos_first
;
1145 /* For all the nodes of the Simple summary tree */
1146 for(i
=0; i
<all_nodes
->len
; i
++) {
1147 struct summary_tree_node
*node
= (struct summary_tree_node
*)g_array_index(all_nodes
, struct summary_tree_node
*, i
);
1149 /* Sort the episodes from longest to shortest */
1150 g_array_sort(node
->episodes
, compare_states_length
);
1152 printf("\tNode id: <%d>\n", node
->id_for_episodes
);
1153 /* For each episode of the node */
1154 for(j
=0; j
<node
->episodes
->len
; j
++) {
1155 struct process_state
*st
= g_array_index(node
->episodes
, struct process_state
*, j
);
1158 print_time(st
->time_begin
);
1160 print_time(st
->time_end
);
1161 printf(" (%f)\n", 1e-9*ltt_time_to_double(ltt_time_sub(st
->time_end
,st
->time_begin
)));
1166 static void print_simple_summary_pid_range(int pid
, LttTime t1
, LttTime t2
)
1168 struct process
*pinfo
;
1170 int id_for_episodes
= 0;
1172 /* we save all the nodes here to print the episodes table quickly */
1173 GArray
*all_nodes
= g_array_new(FALSE
, FALSE
, sizeof(struct summary_tree_node
*));
1175 pinfo
= g_hash_table_lookup(process_hash_table
, &pid
);
1178 struct summary_tree_node base_node
= { children
: NULL
, name
: "Root" };
1180 struct process_state
*hlev_state_cur
;
1182 printf("\tProcess %d [%s]\n", pinfo
->pid
, g_quark_to_string(pinfo
->name
));
1184 /* For each state in the process history */
1185 for(i
=0; i
<pinfo
->hlev_history
->len
; i
++) {
1186 struct process_state
*pstate
= g_array_index(pinfo
->hlev_history
, struct process_state
*, i
);
1187 struct summary_tree_node
*node_cur
= &base_node
;
1188 GArray
*tree_path_garray
;
1190 if(ltt_time_compare(pstate
->time_end
, t1
) < 0)
1193 if(ltt_time_compare(pstate
->time_end
, t2
) > 0)
1196 /* Modify the path based on private data */
1197 tree_path_garray
= g_array_new(FALSE
, FALSE
, sizeof(char *));
1200 char **tree_path_cur2
= hlev_state_infos
[pstate
->bstate
].tree_path
;
1201 while(*tree_path_cur2
) {
1205 g_array_append_vals(tree_path_garray
, hlev_state_infos
[pstate
->bstate
].tree_path
, count
);
1207 modify_path_with_private(tree_path_garray
, pstate
);
1209 /* Walk the path, adding the nodes to the summary */
1210 for(j
=0; j
<tree_path_garray
->len
; j
++) {
1211 struct summary_tree_node
*newnode
;
1212 GQuark componentquark
;
1214 /* Have a path component we must follow */
1215 if(!node_cur
->children
) {
1216 /* must create the hash table for the children */
1217 node_cur
->children
= g_hash_table_new(g_int_hash
, g_int_equal
);
1220 /* try to get the node for the next component */
1221 componentquark
= g_quark_from_string(g_array_index(tree_path_garray
, char *, j
));
1222 newnode
= g_hash_table_lookup(node_cur
->children
, &componentquark
);
1223 if(newnode
== NULL
) {
1224 newnode
= g_malloc(sizeof(struct summary_tree_node
));
1225 newnode
->children
= NULL
;
1226 newnode
->name
= g_array_index(tree_path_garray
, char *, j
);
1227 newnode
->duration
= ltt_time_zero
;
1228 newnode
->id_for_episodes
= id_for_episodes
++;
1229 newnode
->episodes
= g_array_new(FALSE
, FALSE
, sizeof(struct process_state
*));
1230 g_hash_table_insert(node_cur
->children
, &componentquark
, newnode
);
1232 g_array_append_val(all_nodes
, newnode
);
1236 node_cur
->duration
= ltt_time_add(node_cur
->duration
, ltt_time_sub(pstate
->time_end
, pstate
->time_begin
));
1237 g_array_append_val(node_cur
->episodes
, pstate
);
1241 /* print the summary */
1242 print_summary_item(&base_node
, -1);
1249 printf("Episode list\n");
1251 /* For all the nodes of the Simple summary tree */
1252 for(i
=0; i
<all_nodes
->len
; i
++) {
1253 struct summary_tree_node
*node
= (struct summary_tree_node
*)g_array_index(all_nodes
, struct summary_tree_node
*, i
);
1255 /* Sort the episodes from longest to shortest */
1256 g_array_sort(node
->episodes
, compare_states_length
);
1258 printf("\tNode id: <%d>\n", node
->id_for_episodes
);
1259 /* For each episode of the node */
1260 for(j
=0; j
<node
->episodes
->len
; j
++) {
1261 struct process_state
*st
= g_array_index(node
->episodes
, struct process_state
*, j
);
1264 print_time(st
->time_begin
);
1266 print_time(st
->time_end
);
1267 printf(" (%f)\n", 1e-9*ltt_time_to_double(ltt_time_sub(st
->time_end
,st
->time_begin
)));
1272 static void flush_process_sstacks(void)
1276 pinfos
= g_hash_table_get_values(process_hash_table
);
1278 struct process
*pinfo
= (struct process
*)pinfos
->data
;
1280 sstack_force_flush(pinfo
->stack
);
1282 pinfos
= pinfos
->next
;
1285 g_list_free(pinfos
);
1288 struct family_item
{
1293 void print_range_reports(int pid
, LttTime t1
, LttTime t2
)
1295 GArray
*family
= g_array_new(FALSE
, FALSE
, sizeof(struct family_item
));
1298 /* reconstruct the parental sequence */
1300 struct process
*pinfo
;
1301 struct family_item fi
;
1304 pinfo
= g_hash_table_lookup(process_hash_table
, &pid
);
1309 cur_beg
= g_array_index(pinfo
->hlev_history
, struct process_state
*, 0)->time_begin
;
1310 fi
.creation
= cur_beg
;
1311 g_array_append_val(family
, fi
);
1313 if(ltt_time_compare(cur_beg
, t1
) == -1) {
1314 /* current pid starts before the interesting time */
1317 if(pinfo
->parent
== -1) {
1318 printf("unable to go back, we don't know the parent of %d\n", fi
.pid
);
1321 /* else, we go on */
1322 pid
= pinfo
->parent
;
1326 printf("Simple summary for range:\n");
1327 for(i
=family
->len
-1; i
>=0; i
--) {
1328 LttTime iter_t1
, iter_t2
;
1329 int iter_pid
= g_array_index(family
, struct family_item
, i
).pid
;
1331 if(i
== family
->len
-1)
1334 iter_t1
= g_array_index(family
, struct family_item
, i
).creation
;
1339 iter_t2
= g_array_index(family
, struct family_item
, i
-1).creation
;
1341 printf("This section of summary concerns pid %d between ");
1342 print_time(iter_t1
);
1344 print_time(iter_t2
);
1346 print_simple_summary_pid_range(iter_pid
, iter_t1
, iter_t2
);
1348 print_range_critical_path(depanalysis_range_pid
, t1
, t2
);
1351 static gboolean
write_traceset_footer(void *hook_data
, void *call_data
)
1353 LttvTracesetContext
*tc
= (LttvTracesetContext
*)call_data
;
1355 g_info("TextDump traceset footer");
1357 fprintf(a_file
,"End trace set\n\n");
1359 // if(LTTV_IS_TRACESET_STATS(tc)) {
1360 // lttv_stats_sum_traceset((LttvTracesetStats *)tc, ltt_time_infinite);
1361 // print_stats(a_file, (LttvTracesetStats *)tc);
1364 /* After processing all the events, we need to flush the sstacks
1365 * because some unfinished states may remain in them. We want them
1366 * event though there are incomplete.
1368 flush_process_sstacks();
1370 /* print the reports */
1371 print_simple_summary();
1372 print_process_critical_path_summary();
1373 printf("depanalysis_use_time = %d\n", depanalysis_use_time
);
1374 if(depanalysis_use_time
== 3) {
1375 if(depanalysis_range_pid
== -1 && depanalysis_range_pid_searching
>= 0)
1376 depanalysis_range_pid
= depanalysis_range_pid_searching
;
1378 if(depanalysis_range_pid
>= 0) {
1379 print_range_reports(depanalysis_range_pid
, depanalysis_time1
, depanalysis_time2
);
1382 printf("range critical path: could not find the end of the range\n");
1389 static gboolean
write_trace_header(void *hook_data
, void *call_data
)
1391 LttvTraceContext
*tc
= (LttvTraceContext
*)call_data
;
1393 LttSystemDescription
*system
= ltt_trace_system_description(tc
->t
);
1395 fprintf(a_file
," Trace from %s in %s\n%s\n\n",
1396 ltt_trace_system_description_node_name(system
),
1397 ltt_trace_system_description_domain_name(system
),
1398 ltt_trace_system_description_description(system
));
1405 static int write_event_content(void *hook_data
, void *call_data
)
1409 // LttvIAttribute *attributes = LTTV_IATTRIBUTE(lttv_global_attributes());
1411 LttvTracefileContext
*tfc
= (LttvTracefileContext
*)call_data
;
1413 LttvTracefileState
*tfs
= (LttvTracefileState
*)call_data
;
1417 guint cpu
= tfs
->cpu
;
1418 LttvTraceState
*ts
= (LttvTraceState
*)tfc
->t_context
;
1419 LttvProcessState
*process
= ts
->running_process
[cpu
];
1421 e
= ltt_tracefile_get_event(tfc
->tf
);
1423 lttv_event_to_string(e
, a_string
, TRUE
, 1, tfs
);
1426 g_string_append_printf(a_string
, " %s ",
1427 g_quark_to_string(process
->state
->s
));
1430 g_string_append_printf(a_string
,"\n");
1432 fputs(a_string
->str
, a_file
);
1436 static int field_get_value_int(struct LttEvent
*e
, struct marker_info
*info
, GQuark f
)
1438 struct marker_field
*marker_field
;
1441 for_each_marker_field(marker_field
, info
) {
1442 if (marker_field
->name
== f
) {
1448 return ltt_event_get_long_unsigned(e
, marker_field
);
1451 static char *field_get_value_string(struct LttEvent
*e
, struct marker_info
*info
, GQuark f
)
1453 struct marker_field
*marker_field
;
1456 for_each_marker_field(marker_field
, info
) {
1457 if (marker_field
->name
== f
) {
1463 return ltt_event_get_string(e
, marker_field
);
1466 void process_delayed_stack_action(struct process
*pinfo
, struct sstack_item
*item
)
1468 //printf("processing delayed stack action on pid %d at ", pinfo->pid);
1469 //if(((struct process_with_state *) item->data_val)->state.time_begin.tv_nsec == 987799696)
1470 // printf("HERE!!!\n");
1471 //print_time(((struct process_with_state *) item->data_val)->state.time_begin);
1473 //printf("stack before:\n");
1474 //print_stack(pinfo->stack);
1476 if(item
->data_type
== SSTACK_TYPE_PUSH
) {
1477 struct process_with_state
*pwstate
= item
->data_val
;
1478 //printf("pushing\n");
1479 old_process_push_llev_state(pinfo
, &pwstate
->state
);
1480 update_hlev_state(pinfo
, pwstate
->state
.time_begin
);
1482 else if(item
->data_type
== SSTACK_TYPE_POP
) {
1483 struct process_with_state
*pwstate
= item
->data_val
;
1484 //printf("popping\n");
1485 old_process_pop_llev_state(pinfo
, &pwstate
->state
);
1486 update_hlev_state(pinfo
, pwstate
->state
.time_end
);
1488 else if(item
->data_type
== SSTACK_TYPE_EVENT
) {
1489 struct sstack_event
*se
= item
->data_val
;
1490 if(se
->event_type
== HLEV_EVENT_TRY_WAKEUP
) {
1491 /* FIXME: should change hlev event from BLOCKED to INTERRUPTED CPU when receiving TRY_WAKEUP */
1492 struct try_wakeup_event
*twe
= se
->private;
1494 /* FIXME: maybe do some more rigorous checking here */
1495 if(pinfo
->hlev_state
->bstate
== HLEV_BLOCKED
) {
1496 struct hlev_state_info_blocked
*hlev_blocked_private
= pinfo
->hlev_state
->private;
1498 hlev_blocked_private
->pid_exit
= twe
->pid
;
1499 hlev_blocked_private
->time_woken
= twe
->time
;
1500 hlev_blocked_private
->llev_state_exit
= oldstyle_stack_to_garray(twe
->waker
->llev_state_stack
, twe
->waker
->stack_current
);
1501 //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);
1504 if(p->stack_current >= 0 && p->llev_state_stack[p->stack_current]->bstate == LLEV_PREEMPTED) {
1505 old_process_pop_llev_state(pinfo, p->llev_state_stack[p->stack_current]);
1506 update_hlev_state(pinfo
1507 old_process_push_llev_state
1514 //printf("stack after:\n");
1515 //print_stack(pinfo->stack);
1518 static struct process
*get_or_init_process_info(struct LttEvent
*e
, GQuark name
, int pid
, int *new)
1522 val
= g_hash_table_lookup(process_hash_table
, &pid
);
1524 struct process
*pinfo
;
1527 /* Initialize new pinfo for newly discovered process */
1528 pinfo
= g_malloc(sizeof(struct process
));
1530 pinfo
->parent
= -1; /* unknown parent */
1531 pinfo
->hlev_history
= g_array_new(FALSE
, FALSE
, sizeof(struct process_state
*));
1532 pinfo
->stack
= sstack_new();
1533 pinfo
->stack_current
=-1;
1534 pinfo
->stack
->process_func
= process_delayed_stack_action
;
1535 pinfo
->stack
->process_func_arg
= pinfo
;
1536 for(i
=0; i
<PROCESS_STATE_STACK_SIZE
; i
++) {
1537 pinfo
->llev_state_stack
[i
] = g_malloc(sizeof(struct process_state
));
1540 pinfo
->hlev_state
= g_malloc(sizeof(struct process_state
));
1541 pinfo
->hlev_state
->bstate
= HLEV_UNKNOWN
;
1542 pinfo
->hlev_state
->time_begin
= e
->event_time
;
1543 pinfo
->hlev_state
->private = NULL
;
1548 g_hash_table_insert(process_hash_table
, &pinfo
->pid
, pinfo
);
1561 static int differentiate_swappers(int pid
, LttEvent
*e
)
1564 return pid
+e
->tracefile
->cpu_num
+2000000;
1569 static int process_event(void *hook_data
, void *call_data
)
1571 LttvTracefileContext
*tfc
= (LttvTracefileContext
*)call_data
;
1572 LttvTracefileState
*tfs
= (LttvTracefileState
*)call_data
;
1574 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 LttTrace
*trace
= ts
->parent
.t
;
1581 struct process
*pinfo
;
1583 e
= ltt_tracefile_get_event(tfs
->parent
.tf
);
1585 info
= marker_get_info_from_id(tfc
->tf
->mdata
, e
->event_id
);
1587 //if(depanalysis_use_time && (ltt_time_compare(e->timestamp, arg_t1) == -1 || ltt_time_compare(e->timestamp, arg_t2) == 1)) {
1590 /* Set the pid for the dependency analysis at each event, until we are passed the range. */
1591 if(depanalysis_use_time
== 3) {
1592 if(ltt_time_compare(e
->event_time
, depanalysis_time2
) <= 0) {
1593 depanalysis_range_pid
= process
->pid
;
1596 /* Should stop processing and print results */
1600 /* Code to limit the event count */
1601 if(depanalysis_event_limit
> 0) {
1602 depanalysis_event_limit
--;
1604 else if(depanalysis_event_limit
== 0) {
1605 write_traceset_footer(hook_data
, call_data
);
1606 printf("exit due to event limit reached\n");
1610 /* write event like textDump for now, for debugging purposes */
1611 //write_event_content(hook_data, call_data);
1613 if(tfc
->tf
->name
== LTT_CHANNEL_SYSCALL_STATE
&& info
->name
== LTT_EVENT_SYS_CALL_TABLE
) {
1615 int *pint
= g_malloc(sizeof(int));
1617 *pint
= field_get_value_int(e
, info
, LTT_FIELD_ID
);
1618 q
= g_quark_from_string(field_get_value_string(e
, info
, LTT_FIELD_SYMBOL
));
1619 g_hash_table_insert(syscall_table
, pint
, q
);
1621 else if(tfc
->tf
->name
== LTT_CHANNEL_IRQ_STATE
&& info
->name
== LTT_EVENT_LIST_INTERRUPT
) {
1623 int *pint
= g_malloc(sizeof(int));
1625 *pint
= field_get_value_int(e
, info
, LTT_FIELD_IRQ_ID
);
1626 q
= g_quark_from_string(field_get_value_string(e
, info
, LTT_FIELD_ACTION
));
1627 g_hash_table_insert(irq_table
, pint
, q
);
1629 else if(tfc
->tf
->name
== LTT_CHANNEL_SOFTIRQ_STATE
&& info
->name
== LTT_EVENT_SOFTIRQ_VEC
) {
1631 int *pint
= g_malloc(sizeof(int));
1633 *pint
= field_get_value_int(e
, info
, LTT_FIELD_ID
);
1634 q
= g_quark_from_string(field_get_value_string(e
, info
, LTT_FIELD_SYMBOL
));
1635 g_hash_table_insert(softirq_table
, pint
, q
);
1639 /* Only look at events after the statedump is finished.
1640 * Before that, the pids in the LttvProcessState are not reliable
1642 if(statedump_finished
== 0) {
1643 if(tfc
->tf
->name
== LTT_CHANNEL_GLOBAL_STATE
&& info
->name
== LTT_EVENT_STATEDUMP_END
)
1644 statedump_finished
= 1;
1650 pinfo
= get_or_init_process_info(e
, process
->name
, differentiate_swappers(process
->pid
, e
), NULL
);
1652 /* the state machine
1653 * Process the event in the context of each process
1656 if(tfc
->tf
->name
== LTT_CHANNEL_KERNEL
&& info
->name
== LTT_EVENT_IRQ_ENTRY
) {
1657 struct process
*event_process_info
= pinfo
;
1658 struct sstack_item
*item
;
1660 item
= prepare_push_item(event_process_info
, LLEV_IRQ
, e
->event_time
);
1661 ((struct llev_state_info_irq
*) item_private(item
))->irq
= field_get_value_int(e
, info
, LTT_FIELD_IRQ_ID
);
1662 commit_item(event_process_info
, item
);
1664 else if(tfc
->tf
->name
== LTT_CHANNEL_KERNEL
&& info
->name
== LTT_EVENT_IRQ_EXIT
) {
1665 struct process
*event_process_info
= pinfo
;
1667 prepare_pop_item_commit(event_process_info
, LLEV_IRQ
, e
->event_time
);
1669 else if(tfc
->tf
->name
== LTT_CHANNEL_KERNEL
&& info
->name
== LTT_EVENT_SCHED_SCHEDULE
) {
1670 int next_pid
= field_get_value_int(e
, info
, LTT_FIELD_NEXT_PID
);
1671 int prev_pid
= field_get_value_int(e
, info
, LTT_FIELD_PREV_PID
);
1673 struct process
*event_process_info
= get_or_init_process_info(e
, process
->name
, differentiate_swappers(next_pid
, e
), NULL
);
1674 prepare_pop_item_commit(event_process_info
, LLEV_PREEMPTED
, e
->event_time
);
1677 struct sstack_item
*item
;
1678 struct process
*event_process_info
= get_or_init_process_info(e
, process
->name
, differentiate_swappers(prev_pid
, e
), NULL
);
1680 item
= prepare_push_item(event_process_info
, LLEV_PREEMPTED
, e
->event_time
);
1681 ((struct llev_state_info_preempted
*) item_private(item
))->prev_state
= field_get_value_int(e
, info
, LTT_FIELD_PREV_STATE
);
1682 commit_item(event_process_info
, item
);
1685 else if(tfc
->tf
->name
== LTT_CHANNEL_KERNEL
&& info
->name
== LTT_EVENT_TRAP_ENTRY
) {
1686 struct process
*event_process_info
= pinfo
;
1687 struct sstack_item
*item
;
1689 item
= prepare_push_item(event_process_info
, LLEV_TRAP
, e
->event_time
);
1690 commit_item(event_process_info
, item
);
1692 else if(tfc
->tf
->name
== LTT_CHANNEL_KERNEL
&& info
->name
== LTT_EVENT_TRAP_EXIT
) {
1693 struct process
*event_process_info
= pinfo
;
1695 prepare_pop_item_commit(event_process_info
, LLEV_TRAP
, e
->event_time
);
1697 else if(tfc
->tf
->name
== LTT_CHANNEL_KERNEL
&& info
->name
== LTT_EVENT_SYSCALL_ENTRY
) {
1698 struct process
*event_process_info
= pinfo
;
1699 struct sstack_item
*item
;
1701 item
= prepare_push_item(event_process_info
, LLEV_SYSCALL
, e
->event_time
);
1702 ((struct llev_state_info_syscall
*) item_private(item
))->syscall_id
= field_get_value_int(e
, info
, LTT_FIELD_SYSCALL_ID
);
1703 ((struct llev_state_info_syscall
*) item_private(item
))->substate
= LLEV_SYSCALL__UNDEFINED
;
1704 commit_item(event_process_info
, item
);
1706 else if(tfc
->tf
->name
== LTT_CHANNEL_KERNEL
&& info
->name
== LTT_EVENT_SYSCALL_EXIT
) {
1707 struct process
*event_process_info
= pinfo
;
1709 prepare_pop_item_commit(event_process_info
, LLEV_SYSCALL
, e
->event_time
);
1711 else if(tfc
->tf
->name
== LTT_CHANNEL_KERNEL
&& info
->name
== LTT_EVENT_SOFT_IRQ_ENTRY
) {
1712 struct process
*event_process_info
= pinfo
;
1713 struct sstack_item
*item
;
1715 item
= prepare_push_item(event_process_info
, LLEV_SOFTIRQ
, e
->event_time
);
1716 ((struct llev_state_info_softirq
*) item_private(item
))->softirq
= field_get_value_int(e
, info
, LTT_FIELD_SOFT_IRQ_ID
);
1717 commit_item(event_process_info
, item
);
1719 else if(tfc
->tf
->name
== LTT_CHANNEL_KERNEL
&& info
->name
== LTT_EVENT_SOFT_IRQ_EXIT
) {
1720 struct process
*event_process_info
= pinfo
;
1722 prepare_pop_item_commit(event_process_info
, LLEV_SOFTIRQ
, e
->event_time
);
1724 else if(tfc
->tf
->name
== LTT_CHANNEL_KERNEL
&& info
->name
== LTT_EVENT_PROCESS_FORK
) {
1725 int pid
= differentiate_swappers(field_get_value_int(e
, info
, LTT_FIELD_CHILD_PID
), e
);
1726 struct process
*event_process_info
= get_or_init_process_info(e
, process
->name
, differentiate_swappers(field_get_value_int(e
, info
, LTT_FIELD_CHILD_PID
), e
), NULL
);
1727 struct sstack_item
*item
;
1729 event_process_info
->parent
= process
->pid
;
1731 //print_time(e->event_time);
1732 //printf(", fork in process %d (%s), creating child %d\n", differentiate_swappers(process->pid, e), g_quark_to_string(process->name), pid);
1734 item
= prepare_push_item(event_process_info
, LLEV_RUNNING
, e
->event_time
);
1735 commit_item(event_process_info
, item
);
1736 item
= prepare_push_item(event_process_info
, LLEV_SYSCALL
, e
->event_time
);
1737 /* FIXME: this sets fork() as syscall, it's pretty inelegant */
1738 ((struct llev_state_info_syscall
*) item_private(item
))->syscall_id
= 57;
1739 ((struct llev_state_info_syscall
*) item_private(item
))->substate
= LLEV_SYSCALL__UNDEFINED
;
1740 commit_item(event_process_info
, item
);
1742 item
= prepare_push_item(event_process_info
, LLEV_PREEMPTED
, e
->event_time
);
1743 /* Consider fork as BLOCKED */
1744 ((struct llev_state_info_preempted
*) item_private(item
))->prev_state
= 1;
1745 commit_item(event_process_info
, item
);
1747 //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);
1750 else if(tfc
->tf
->name
== LTT_CHANNEL_FS
&& info
->name
== LTT_EVENT_EXEC
) {
1751 struct process
*event_process_info
= pinfo
;
1753 guint cpu
= tfs
->cpu
;
1754 LttvProcessState
*process_state
= ts
->running_process
[cpu
];
1755 event_process_info
->name
= process_state
->name
;
1757 else if(tfc
->tf
->name
== LTT_CHANNEL_FS
&& info
->name
== LTT_EVENT_OPEN
) {
1758 struct process_state
*pstate
= process_find_state(pinfo
, LLEV_SYSCALL
);
1759 struct llev_state_info_syscall
*llev_syscall_private
;
1760 struct llev_state_info_syscall__open
*llev_syscall_open_private
;
1762 /* TODO: this is too easy */
1766 llev_syscall_private
= (struct llev_state_info_syscall
*)pstate
->private;
1768 //printf("depanalysis: found an open with state %d in pid %d\n", pstate->bstate, process->pid);
1769 if(pstate
->bstate
== LLEV_UNKNOWN
)
1772 g_assert(pstate
->bstate
== LLEV_SYSCALL
);
1773 g_assert(llev_syscall_private
->substate
== LLEV_SYSCALL__UNDEFINED
);
1775 llev_syscall_private
->substate
= LLEV_SYSCALL__OPEN
;
1776 //printf("setting substate LLEV_SYSCALL__OPEN on syscall_private %p\n", llev_syscall_private);
1777 llev_syscall_private
->private = g_malloc(sizeof(struct llev_state_info_syscall__open
));
1778 llev_syscall_open_private
= llev_syscall_private
->private;
1780 llev_syscall_open_private
->filename
= g_quark_from_string(field_get_value_string(e
, info
, LTT_FIELD_FILENAME
));
1783 else if(tfc
->tf
->name
== LTT_CHANNEL_FS
&& info
->name
== LTT_EVENT_READ
) {
1784 struct process_state
*pstate
= process_find_state(pinfo
, LLEV_SYSCALL
);
1785 struct llev_state_info_syscall
*llev_syscall_private
;
1786 struct llev_state_info_syscall__read
*llev_syscall_read_private
;
1790 /* TODO: this is too easy */
1794 llev_syscall_private
= (struct llev_state_info_syscall
*)pstate
->private;
1796 //printf("depanalysis: found an read with state %d in pid %d\n", pstate->bstate, process->pid);
1797 if(pstate
->bstate
== LLEV_UNKNOWN
)
1800 g_assert(pstate
->bstate
== LLEV_SYSCALL
);
1801 g_assert(llev_syscall_private
->substate
== LLEV_SYSCALL__UNDEFINED
);
1803 llev_syscall_private
->substate
= LLEV_SYSCALL__READ
;
1804 //printf("setting substate LLEV_SYSCALL__READ on syscall_private %p\n", llev_syscall_private);
1805 llev_syscall_private
->private = g_malloc(sizeof(struct llev_state_info_syscall__read
));
1806 llev_syscall_read_private
= llev_syscall_private
->private;
1808 fd
= field_get_value_int(e
, info
, LTT_FIELD_FD
);
1809 pfileq
= g_hash_table_lookup(process
->fds
, fd
);
1811 llev_syscall_read_private
->filename
= pfileq
;
1815 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
= g_hash_table_lookup(process
->fds
, fd
);
1857 llev_syscall_poll_private
->filename
= pfileq
;
1861 asprintf(&tmp
, "Unknown filename, fd %d", fd
);
1862 llev_syscall_poll_private
->filename
= g_quark_from_string(tmp
);
1866 else if(tfc
->tf
->name
== LTT_CHANNEL_KERNEL
&& info
->name
== LTT_EVENT_SCHED_TRY_WAKEUP
) {
1867 struct sstack_event
*se
= g_malloc(sizeof(struct sstack_event
));
1868 struct try_wakeup_event
*twe
= g_malloc(sizeof(struct try_wakeup_event
));
1869 struct sstack_item
*item
= sstack_item_new_event();
1870 int target
= field_get_value_int(e
, info
, LTT_FIELD_PID
);
1871 struct process
*target_pinfo
;
1874 se
->event_type
= HLEV_EVENT_TRY_WAKEUP
;
1876 //printf("pushing try wake up event in context of %d\n", pinfo->pid);
1878 twe
->pid
= differentiate_swappers(process
->pid
, e
);
1879 twe
->time
= e
->event_time
;
1882 /* FIXME: the target could not yet have an entry in the hash table, we would then lose data */
1883 target_pinfo
= g_hash_table_lookup(process_hash_table
, &target
);
1887 item
->data_val
= se
;
1888 item
->delete_data_val
= delete_data_val
;
1890 sstack_add_item(target_pinfo
->stack
, item
);
1892 /* Now pop the blocked schedule out of the target */
1893 result
= try_pop_blocked_llev_preempted(target_pinfo
, e
->event_time
);
1896 struct sstack_item
*item
;
1897 struct process
*event_process_info
= target_pinfo
;
1899 item
= prepare_push_item(event_process_info
, LLEV_PREEMPTED
, e
->event_time
);
1900 ((struct llev_state_info_preempted
*) item_private(item
))->prev_state
= -1; /* special value meaning post-block sched out */
1901 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
)
1970 print_sstack_item_data
= print_sstack_private
;
1972 LttvAttributeValue value
;
1974 LttvIAttribute
*attributes
= LTTV_IATTRIBUTE(lttv_global_attributes());
1978 lttv_option_add("dep-time-start", 0, "dependency analysis time of analysis start", "time",
1979 LTTV_OPT_STRING
, &arg_t1_str
, arg_t1
, NULL
);
1980 lttv_option_add("dep-time-end", 0, "dependency analysis time of analysis end", "time",
1981 LTTV_OPT_STRING
, &arg_t2_str
, arg_t2
, NULL
);
1982 lttv_option_add("dep-pid", 0, "dependency analysis pid", "pid",
1983 LTTV_OPT_INT
, &depanalysis_range_pid_searching
, arg_pid
, NULL
);
1984 lttv_option_add("limit-events", 0, "dependency limit event count", "count",
1985 LTTV_OPT_INT
, &depanalysis_event_limit
, arg_limit
, NULL
);
1987 process_hash_table
= g_hash_table_new(g_int_hash
, g_int_equal
);
1988 syscall_table
= g_hash_table_new(g_int_hash
, g_int_equal
);
1989 irq_table
= g_hash_table_new(g_int_hash
, g_int_equal
);
1990 softirq_table
= g_hash_table_new(g_int_hash
, g_int_equal
);
1992 a_string
= g_string_new("");
1994 result
= lttv_iattribute_find_by_path(attributes
, "hooks/event",
1995 LTTV_POINTER
, &value
);
1997 event_hook
= *(value
.v_pointer
);
1998 g_assert(event_hook
);
1999 lttv_hooks_add(event_hook
, process_event
, NULL
, LTTV_PRIO_DEFAULT
);
2001 // result = lttv_iattribute_find_by_path(attributes, "hooks/trace/before",
2002 // LTTV_POINTER, &value);
2003 // g_assert(result);
2004 // before_trace = *(value.v_pointer);
2005 // g_assert(before_trace);
2006 // lttv_hooks_add(before_trace, write_trace_header, NULL, LTTV_PRIO_DEFAULT);
2008 result
= lttv_iattribute_find_by_path(attributes
, "hooks/traceset/before",
2009 LTTV_POINTER
, &value
);
2011 before_traceset
= *(value
.v_pointer
);
2012 g_assert(before_traceset
);
2013 lttv_hooks_add(before_traceset
, write_traceset_header
, NULL
,
2016 result
= lttv_iattribute_find_by_path(attributes
, "hooks/traceset/after",
2017 LTTV_POINTER
, &value
);
2019 after_traceset
= *(value
.v_pointer
);
2020 g_assert(after_traceset
);
2021 lttv_hooks_add(after_traceset
, write_traceset_footer
, NULL
,
2025 static void destroy()
2027 lttv_option_remove("dep-time-start");
2028 lttv_option_remove("dep-time-end");
2029 lttv_option_remove("dep-pid");
2030 lttv_option_remove("limit-events");
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
);
2040 // lttv_hooks_remove_data(before_trace, write_trace_header, 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")