add experimental depanalysis module
[lttv.git] / lttv / modules / text / depanalysis.c
1 /* This file is part of the Linux Trace Toolkit viewer
2 * Copyright (C) 2008 Pierre-Marc Fournier
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License Version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
16 * MA 02111-1307, USA.
17 */
18
19 #ifdef HAVE_CONFIG_H
20 #include <config.h>
21 #endif
22
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>
32 #include <ltt/ltt.h>
33 #include <ltt/event.h>
34 #include <ltt/trace.h>
35 #define _GNU_SOURCE
36 #include <stdio.h>
37 #include <glib.h>
38 #include <stdlib.h>
39
40 #include "sstack.h"
41
42 static LttvHooks
43 *before_traceset,
44 *after_traceset,
45 // *before_trace,
46 *event_hook;
47
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;
55
56
57 struct llev_state_info_irq {
58 int irq;
59 };
60
61 struct llev_state_info_softirq {
62 int softirq;
63 };
64
65 struct llev_state_info_syscall {
66 int syscall_id;
67
68 int substate;
69
70 void *private;
71 };
72
73 struct llev_state_info_syscall__open {
74 GQuark filename;
75 };
76
77 struct llev_state_info_syscall__read {
78 GQuark filename;
79 };
80
81 struct llev_state_info_syscall__poll {
82 GQuark filename;
83 };
84
85 struct llev_state_info_preempted {
86 int prev_state;
87 };
88
89 struct hlev_state_info_blocked {
90 int syscall_id;
91 unsigned char trap; /* flag */
92 int substate;
93
94 /* Garray of pointers to struct process_state that reflect the
95 * low-level state stack when respectively entering and exiting the blocked
96 * state.
97 */
98 GArray *llev_state_entry;
99 GArray *llev_state_exit;
100
101 int pid_exit; /* FIXME: it's not pretty to have this here; find this info elsewhere */
102 LttTime time_woken;
103
104 void *private;
105 };
106
107 struct hlev_state_info_blocked__open {
108 GQuark filename;
109 };
110
111 struct hlev_state_info_blocked__read {
112 GQuark filename;
113 };
114
115 struct hlev_state_info_blocked__poll {
116 GQuark filename;
117 };
118
119 struct hlev_state_info_interrupted_irq {
120 int irq;
121 };
122
123 struct hlev_state_info_interrupted_softirq {
124 int softirq;
125 };
126
127 struct summary_tree_node {
128 char *name;
129 GHashTable *children;
130 LttTime duration;
131 GArray *episodes;
132 int id_for_episodes;
133 };
134
135 struct state_info {
136 char name[40];
137 int size_priv;
138 char *tree_path[6];
139 };
140
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 } },
149 };
150
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 } },
159 };
160
161 enum llev_state {
162 LLEV_UNKNOWN=0,
163 LLEV_RUNNING,
164 LLEV_SYSCALL,
165 LLEV_IRQ,
166 LLEV_SOFTIRQ,
167 LLEV_TRAP,
168 LLEV_PREEMPTED,
169 };
170
171 enum llev_syscall_substate {
172 LLEV_SYSCALL__UNDEFINED,
173 LLEV_SYSCALL__OPEN,
174 LLEV_SYSCALL__READ,
175 LLEV_SYSCALL__POLL,
176 };
177
178 enum hlev_event {
179 HLEV_EVENT_TRY_WAKEUP=0,
180 };
181
182 enum hlev_state {
183 HLEV_UNKNOWN=0,
184 HLEV_RUNNING,
185 HLEV_BLOCKED,
186 HLEV_INTERRUPTED_IRQ,
187 HLEV_INTERRUPTED_SOFTIRQ,
188 HLEV_INTERRUPTED_CPU,
189 HLEV_INTERRUPTED_POST_BLOCK,
190 };
191
192 enum hlev_state_blocked {
193 HLEV_BLOCKED__UNDEFINED,
194 HLEV_BLOCKED__OPEN,
195 HLEV_BLOCKED__READ,
196 HLEV_BLOCKED__POLL,
197 };
198
199 struct sstack_event {
200 int event_type;
201 void *private;
202 };
203
204 struct try_wakeup_event {
205 int pid; /* this sould be more precise avec pid may be reused */
206 LttTime time;
207 struct process *waker;
208 };
209
210 struct process_state {
211 int bstate;
212 int cause_type;
213 void *private;
214
215 LttTime time_begin;
216 LttTime time_end;
217 };
218
219 struct process_with_state {
220 struct process *process;
221 struct process_state state;
222 };
223
224 #define PROCESS_STATE_STACK_SIZE 10
225 struct process {
226 int pid;
227 GQuark name;
228 int parent;
229
230 struct sstack *stack;
231 struct process_state *llev_state_stack[PROCESS_STATE_STACK_SIZE];
232 int stack_current;
233 struct process_state *hlev_state;
234 GArray *hlev_history;
235 };
236
237 static inline void *old_process_state_private_data(struct process *p)
238 {
239 return p->llev_state_stack[p->stack_current]->private;
240 }
241
242 static inline struct process_state *process_find_state(struct process *p, enum llev_state st)
243 {
244 int i;
245
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);
248
249 struct process_with_state *pwstate = item->data_val;
250 if(pwstate->state.bstate == st) {
251 return &pwstate->state;
252 }
253 }
254
255 return NULL;
256 }
257
258 static int find_pos_in_stack(enum llev_state lls, struct process *p)
259 {
260 int i;
261 for(i=p->stack_current; i>=0; i--) {
262 if(p->llev_state_stack[i]->bstate == lls)
263 return i;
264 }
265
266 return -1;
267 }
268
269 static struct process_state *find_in_stack(enum llev_state lls, struct process *p)
270 {
271 int result;
272
273 result = find_pos_in_stack(lls, p);
274
275 if(result >= 0)
276 return p->llev_state_stack[result];
277 else
278 return NULL;
279
280 }
281
282 /* called back from sstack on deletion of a data_val which is
283 * a struct process_with_state
284 */
285
286 static void delete_data_val(struct process_with_state *pwstate)
287 {
288 // FIXME: Free this also
289 //g_free(pwstate->state.private);
290
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)
297 // g_free(pwstate);
298 }
299
300 static struct sstack_item *prepare_push_item(struct process *p, enum llev_state st, LttTime t)
301 {
302 struct process_with_state *pwstate = g_malloc(sizeof(struct process_with_state));
303 struct sstack_item *item;
304
305 int wait_for_pop = 0;
306
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.
311 */
312 wait_for_pop = 1;
313 }
314
315 item = sstack_item_new_push(wait_for_pop);
316
317 //printf("pushing in context of %d\n", p->pid);
318
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);
323
324 item->data_val = pwstate;
325 item->delete_data_val = delete_data_val;
326 }
327
328 static void *item_private(struct sstack_item *item)
329 {
330 struct process_with_state *pwstate = item->data_val;
331 return pwstate->state.private;
332 }
333
334 static void commit_item(struct process *p, struct sstack_item *item)
335 {
336 sstack_add_item(p->stack, item);
337 }
338
339 static void old_process_push_llev_state(struct process *p, struct process_state *pstate)
340 {
341 if(++p->stack_current >= PROCESS_STATE_STACK_SIZE) {
342 fprintf(stderr, "depanalysis: internal process stack overflow\n");
343 abort();
344 }
345
346 p->llev_state_stack[p->stack_current] = pstate;
347 }
348
349 static void live_complete_process_push_llev_state(struct process *p, enum llev_state st, LttTime t)
350 {
351 struct process_state *pstate = g_malloc(sizeof(struct process_state));
352
353 pstate->bstate = st;
354 pstate->time_begin = t;
355 pstate->private = g_malloc(llev_state_infos[st].size_priv);
356
357 old_process_push_llev_state(p, pstate);
358 }
359
360 static void prepare_pop_item_commit_nocheck(struct process *p, enum llev_state st, LttTime t)
361 {
362 struct process_with_state *pwstate;
363 struct sstack_item *item = sstack_item_new_pop();
364
365 int push_idx;
366
367 if(p->stack->pushes->len > 0)
368 push_idx = g_array_index(p->stack->pushes, int, p->stack->pushes->len-1);
369 else
370 push_idx = -1;
371
372 if(push_idx >= 0) {
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 */
378 }
379 else {
380
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;
386 }
387
388 sstack_add_item(p->stack, item);
389
390 }
391
392 static void prepare_pop_item_commit(struct process *p, enum llev_state st, LttTime t)
393 {
394 struct process_with_state *pwstate;
395 struct sstack_item *item = sstack_item_new_pop();
396
397 int push_idx;
398
399 if(p->stack->pushes->len > 0)
400 push_idx = g_array_index(p->stack->pushes, int, p->stack->pushes->len-1);
401 else
402 push_idx = -1;
403
404 if(push_idx >= 0) {
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)
409 return;
410
411 pwstate = g_array_index(p->stack->array, struct sstack_item *, push_idx)->data_val;
412
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 ");
417 print_time(t);
418 printf("\n");
419 print_stack(p->stack);
420 abort();
421 }
422 else {
423 /* case where we have a double syscall_exit */
424 return;
425 }
426 }
427 }
428
429 prepare_pop_item_commit_nocheck(p, st, t);
430 }
431
432
433 static int try_pop_blocked_llev_preempted(struct process *p, LttTime t)
434 {
435 int push_idx;
436 struct process_with_state *pwstate;
437
438 if(p->stack->pushes->len > 0)
439 push_idx = g_array_index(p->stack->pushes, int, p->stack->pushes->len-1);
440 else
441 push_idx = -1;
442
443 if(push_idx >= 0) {
444 pwstate = g_array_index(p->stack->array, struct sstack_item *, push_idx)->data_val;
445
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");
448 return 0;
449 }
450 }
451
452 prepare_pop_item_commit_nocheck(p, LLEV_PREEMPTED, t);
453 return 1;
454 }
455
456 static void old_process_pop_llev_state(struct process *p, struct process_state *pstate)
457 {
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");
463 // abort();
464 //}
465
466 /* Actually change the that position */
467 if(p->stack_current >= 0)
468 p->stack_current--;
469
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);
475 }
476 else {
477 live_complete_process_push_llev_state(p, LLEV_UNKNOWN, pstate->time_end);
478 }
479 }
480 }
481
482 static GHashTable *process_hash_table;
483 static GHashTable *syscall_table;
484 static GHashTable *irq_table;
485 static GHashTable *softirq_table;
486
487 /* Insert the hooks before and after each trace and tracefile, and for each
488 event. Print a global header. */
489
490 static FILE *a_file;
491
492 static GString *a_string;
493
494 static gboolean write_traceset_header(void *hook_data, void *call_data)
495 {
496 LttvTracesetContext *tc = (LttvTracesetContext *)call_data;
497
498 g_info("Traceset header");
499
500 /* Print the trace set header */
501 fprintf(a_file,"Trace set contains %d traces\n\n",
502 lttv_traceset_number(tc->ts));
503
504 return FALSE;
505 }
506
507 inline void print_time(LttTime t)
508 {
509 //printf("%lu.%lu", t.tv_sec, t.tv_nsec);
510 double f;
511 f = (double)t.tv_sec + ((double)t.tv_nsec)/1000000000.0;
512 printf("%.9f", f);
513 }
514
515 GArray *oldstyle_stack_to_garray(struct process_state_stack **oldstyle_stack, int current)
516 {
517 GArray *retval;
518 int i;
519
520 retval = g_array_new(FALSE, FALSE, sizeof(struct process_state_stack *));
521
522 for(i=0; i<current; i++) {
523 g_array_append_val(retval, oldstyle_stack[i]);
524 }
525
526 return retval;
527 }
528
529 static void update_hlev_state(struct process *p, LttTime t)
530 {
531 int i;
532
533 enum hlev_state new_hlev;
534
535 for(i=p->stack_current; i>=0; i--) {
536 enum llev_state st;
537 st = p->llev_state_stack[i]->bstate;
538
539 if(st == LLEV_RUNNING || st == LLEV_TRAP || st == LLEV_SYSCALL) {
540 new_hlev = HLEV_RUNNING;
541 break;
542 }
543 else if(st == LLEV_IRQ) {
544 new_hlev = HLEV_INTERRUPTED_IRQ;
545 break;
546 }
547 else if(st == LLEV_SOFTIRQ) {
548 new_hlev = HLEV_INTERRUPTED_SOFTIRQ;
549 break;
550 }
551 else if(st == LLEV_PREEMPTED) {
552 int prev_state = ((struct llev_state_info_preempted *) old_process_state_private_data(p))->prev_state;
553
554 if(prev_state == 0) {
555 new_hlev = HLEV_INTERRUPTED_CPU;
556 }
557 else if(prev_state == -1) {
558 new_hlev = HLEV_INTERRUPTED_POST_BLOCK;
559 }
560 else {
561 new_hlev = HLEV_BLOCKED;
562 }
563 break;
564 }
565 else if(st == LLEV_UNKNOWN) {
566 new_hlev = HLEV_UNKNOWN;
567 break;
568 }
569 else {
570 abort();
571 }
572 }
573
574 /* If no state change, do nothing */
575 if(p->hlev_state != NULL && new_hlev == p->hlev_state->bstate) {
576 return;
577 }
578
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
583 * as it ought to be.
584 */
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);
591
592 //printf("depanalysis: now at hlev state %s\n", hlev_state_infos[new_hlev].name);
593
594 /* Set private data */
595 switch(p->hlev_state->bstate) {
596 case HLEV_UNKNOWN:
597 break;
598 case HLEV_RUNNING:
599 break;
600 case HLEV_BLOCKED: {
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);
605
606 /* init vals */
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;
613
614 //g_assert(syscall_pos >= 0 || trap_pos >= 0);
615
616 if(trap_pos > syscall_pos) {
617 hlev_blocked_private->trap = 1;
618 }
619
620 /* initial value, may be changed below */
621 hlev_blocked_private->substate = HLEV_BLOCKED__UNDEFINED;
622
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;
627
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;
636
637 //printf("depanalysis: blocked in an open!\n");
638 }
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;
647
648 //printf("depanalysis: blocked in a read!\n");
649 }
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;
658
659 //printf("depanalysis: blocked in a read!\n");
660 }
661 }
662 else {
663 hlev_blocked_private->syscall_id = -1;
664 }
665
666 break;
667 }
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);
671 if(ps == NULL)
672 abort();
673 else
674 sinfo->irq = ((struct llev_state_info_irq *) ps->private)->irq;
675 break;
676 }
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);
680 if(ps == NULL)
681 abort();
682 else
683 sinfo->softirq = ((struct llev_state_info_softirq *) ps->private)->softirq;
684 break;
685 }
686 default:
687 break;
688 };
689 }
690
691 static gint compare_summary_tree_node_times(gconstpointer a, gconstpointer b)
692 {
693 struct summary_tree_node *n1 = (struct summary_tree_node *) a;
694 struct summary_tree_node *n2 = (struct summary_tree_node *) b;
695
696 return ltt_time_compare(n2->duration, n1->duration);
697 }
698
699 /* Print an item of the simple summary tree, and recurse, printing its children.
700 *
701 * If depth == -1, this is the root: we don't print a label, we only recurse into
702 * the children.
703 */
704
705 static void print_summary_item(struct summary_tree_node *node, int depth)
706 {
707 GList *vals;
708
709 if(depth >= 0) {
710 printf("\t%*s (", strlen(node->name)+2*depth, node->name);
711 print_time(node->duration);
712 printf(") <%d>\n", node->id_for_episodes);
713 }
714
715 if(!node->children)
716 return;
717
718 vals = g_hash_table_get_values(node->children);
719
720 /* sort the values */
721 vals = g_list_sort(vals, compare_summary_tree_node_times);
722
723 while(vals) {
724 print_summary_item((struct summary_tree_node *)vals->data, depth+1);
725 vals = vals->next;
726 }
727
728 /* we must free the list returned by g_hash_table_get_values() */
729 g_list_free(vals);
730 }
731
732 static inline void print_irq(int irq)
733 {
734 printf("IRQ %d [%s]", irq, g_quark_to_string(g_hash_table_lookup(irq_table, &irq)));
735 }
736
737 static inline void print_softirq(int softirq)
738 {
739 printf("SoftIRQ %d [%s]", softirq, g_quark_to_string(g_hash_table_lookup(softirq_table, &softirq)));
740 }
741
742 static inline void print_pid(int pid)
743 {
744 struct process *event_process_info = g_hash_table_lookup(process_hash_table, &pid);
745
746 char *pname;
747
748 if(event_process_info == NULL)
749 pname = "?";
750 else
751 pname = g_quark_to_string(event_process_info->name);
752 printf("%d [%s]", pid, pname);
753 }
754
755 static void modify_path_with_private(GArray *path, struct process_state *pstate)
756 {
757 //GString tmps = g_string_new("");
758 char *tmps;
759
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);
765 break;
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);
769 break;
770 case HLEV_BLOCKED: {
771 struct hlev_state_info_blocked *hlev_blocked_private = (struct hlev_state_info_blocked *)pstate->private;
772
773 if(hlev_blocked_private->trap) {
774 char *ptr = "Trap";
775 g_array_append_val(path, ptr);
776 }
777
778 if(hlev_blocked_private->syscall_id == -1) {
779 char *ptr = "Userspace";
780 g_array_append_val(path, ptr);
781 }
782 else {
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);
785 }
786
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);
790 }
791 else if(((struct hlev_state_info_blocked *)pstate->private)->substate == HLEV_BLOCKED__READ) {
792 char *str;
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 */
796 //free(str);
797 }
798 else if(((struct hlev_state_info_blocked *)pstate->private)->substate == HLEV_BLOCKED__POLL) {
799 char *str;
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 */
803 //free(str);
804 }
805 break;
806 }
807 };
808 }
809
810 void print_stack_garray_horizontal(GArray *stack)
811 {
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.
814 */
815 int i;
816
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);
820
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)));
824 }
825
826 printf(", ");
827
828 }
829 }
830
831 static int dicho_search_state_ending_after(struct process *p, LttTime t)
832 {
833 int under = 0;
834 int over = p->hlev_history->len-1;
835 struct process_state *pstate;
836 int result;
837
838 if(over < 1)
839 return -1;
840
841 /* If the last element is smaller or equal than the time we are searching for,
842 * no match
843 */
844 pstate = g_array_index(p->hlev_history, struct process_state *, over);
845 if(ltt_time_compare(pstate->time_end, t) <= 0) {
846 return -1;
847 }
848 /* no need to check for the equal case */
849
850 pstate = g_array_index(p->hlev_history, struct process_state *, under);
851 result = ltt_time_compare(pstate->time_end, t);
852 if(result >= 1) {
853 /* trivial match at the first element if it is greater or equal
854 * than the time we want
855 */
856 return under;
857 }
858
859 while(1) {
860 int dicho;
861
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);
865
866 if(result == -1) {
867 under = dicho;
868 }
869 else if(result == 1) {
870 over = dicho;
871 }
872 else {
873 /* exact match */
874 return dicho+1;
875 }
876
877 if(over-under == 1) {
878 /* we have converged */
879 return over;
880 }
881 }
882
883 }
884
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
887 */
888
889 static struct process_state *find_state_ending_after(int pid, LttTime t)
890 {
891 struct process *p;
892 int result;
893
894
895 p = g_hash_table_lookup(process_hash_table, &pid);
896 if(!p)
897 return NULL;
898
899 result = dicho_search_state_ending_after(p, t);
900
901 if(result == -1)
902 return NULL;
903 else
904 return g_array_index(p->hlev_history, struct process_state *, result);
905 }
906
907 static void print_delay_pid(int pid, LttTime t1, LttTime t2, int offset)
908 {
909 struct process *p;
910 int i;
911
912 p = g_hash_table_lookup(process_hash_table, &pid);
913 if(!p)
914 return;
915
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)
920 break;
921
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;
926
927 printf("%*s", 8*offset, "");
928 printf("Blocked in ");
929 print_stack_garray_horizontal(state_private_blocked->llev_state_entry);
930
931 printf("(times: ");
932 print_time(pstate->time_begin);
933 printf("-");
934 print_time(pstate->time_end);
935
936 printf(", dur: %f)\n", 1e-9*ltt_time_to_double(ltt_time_sub(pstate->time_end, pstate->time_begin)));
937
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);
946 printf("\n");
947 }
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);
953 printf("\n");
954 }
955 else {
956 LttTime t1prime=t1;
957 LttTime t2prime=t2;
958
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;
963
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);
970 }
971 else {
972 }
973 printf(" in high-level state %s", hlev_state_infos[state_unblocked->bstate].name);
974 printf("\n");
975 }
976 }
977 else {
978 printf("%*s", 8*offset, "");
979 printf("Weird... cannot find in what state the waker (%d) was\n", state_private_blocked->pid_exit);
980 }
981
982
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);
988 //}
989 //else
990 // printf("the private_blocked %p had a null exit stack\n", state_private_blocked);
991 //printf("\n");
992 }
993 }
994 }
995
996 static void print_range_critical_path(int process, LttTime t1, LttTime t2)
997 {
998 printf("Critical path for requested range:\n");
999 printf("Final process is %d\n", process);
1000 print_delay_pid(process, t1, t2, 2);
1001 }
1002
1003 static void print_process_critical_path_summary()
1004 {
1005 struct process *pinfo;
1006 GList *pinfos;
1007 int i,j;
1008
1009 pinfos = g_hash_table_get_values(process_hash_table);
1010 if(pinfos == NULL) {
1011 fprintf(stderr, "error: no process found\n");
1012 return;
1013 }
1014
1015 printf("Process Critical Path Summary:\n");
1016
1017 for(;;) {
1018 struct summary_tree_node base_node = { children: NULL };
1019
1020 struct process_state *hlev_state_cur;
1021
1022 pinfo = (struct process *)pinfos->data;
1023 printf("\tProcess %d [%s]\n", pinfo->pid, g_quark_to_string(pinfo->name));
1024
1025 if(pinfo->hlev_history->len < 1)
1026 goto next_iter;
1027
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);
1029
1030 next_iter:
1031
1032 if(pinfos->next)
1033 pinfos = pinfos->next;
1034 else
1035 break;
1036 }
1037 }
1038
1039 gint compare_states_length(gconstpointer a, gconstpointer b)
1040 {
1041 struct process_state **s1 = (struct process_state **)a;
1042 struct process_state **s2 = (struct process_state **)b;
1043 gint val;
1044
1045 val = ltt_time_compare(ltt_time_sub((*s2)->time_end, (*s2)->time_begin), ltt_time_sub((*s1)->time_end, (*s1)->time_begin));
1046 return val;
1047 }
1048
1049 static void print_simple_summary()
1050 {
1051 struct process *pinfo;
1052 GList *pinfos;
1053 GList *pinfos_first;
1054 int i,j;
1055 int id_for_episodes = 0;
1056
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 *));
1059
1060 pinfos_first = g_hash_table_get_values(process_hash_table);
1061 if(pinfos_first == NULL) {
1062 fprintf(stderr, "error: no processes found\n");
1063 return;
1064 }
1065 pinfos = pinfos_first;
1066
1067 printf("Simple summary:\n");
1068
1069 /* For each process */
1070 for(;;) {
1071 struct summary_tree_node base_node = { children: NULL, name: "Root" };
1072
1073 struct process_state *hlev_state_cur;
1074
1075 pinfo = (struct process *)pinfos->data;
1076 printf("\tProcess %d [%s]\n", pinfo->pid, g_quark_to_string(pinfo->name));
1077
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;
1083
1084 /* Modify the path based on private data */
1085 tree_path_garray = g_array_new(FALSE, FALSE, sizeof(char *));
1086 {
1087 int count=0;
1088 char **tree_path_cur2 = hlev_state_infos[pstate->bstate].tree_path;
1089 while(*tree_path_cur2) {
1090 count++;
1091 tree_path_cur2++;
1092 }
1093 g_array_append_vals(tree_path_garray, hlev_state_infos[pstate->bstate].tree_path, count);
1094 }
1095 modify_path_with_private(tree_path_garray, pstate);
1096
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;
1101
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);
1106 }
1107
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);
1119
1120 g_array_append_val(all_nodes, newnode);
1121 }
1122 node_cur = newnode;
1123
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);
1126 }
1127 }
1128
1129 /* print the summary */
1130 print_summary_item(&base_node, -1);
1131
1132 printf("\n");
1133
1134 if(pinfos->next)
1135 pinfos = pinfos->next;
1136 else
1137 break;
1138 }
1139
1140 printf("\n");
1141
1142 printf("Episode list\n");
1143 pinfos = pinfos_first;
1144
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);
1148
1149 /* Sort the episodes from longest to shortest */
1150 g_array_sort(node->episodes, compare_states_length);
1151
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);
1156
1157 printf("\t\t");
1158 print_time(st->time_begin);
1159 printf("-");
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)));
1162 }
1163 }
1164 }
1165
1166 static void print_simple_summary_pid_range(int pid, LttTime t1, LttTime t2)
1167 {
1168 struct process *pinfo;
1169 int i,j;
1170 int id_for_episodes = 0;
1171
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 *));
1174
1175 pinfo = g_hash_table_lookup(process_hash_table, &pid);
1176
1177 {
1178 struct summary_tree_node base_node = { children: NULL, name: "Root" };
1179
1180 struct process_state *hlev_state_cur;
1181
1182 printf("\tProcess %d [%s]\n", pinfo->pid, g_quark_to_string(pinfo->name));
1183
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;
1189
1190 if(ltt_time_compare(pstate->time_end, t1) < 0)
1191 continue;
1192
1193 if(ltt_time_compare(pstate->time_end, t2) > 0)
1194 break;
1195
1196 /* Modify the path based on private data */
1197 tree_path_garray = g_array_new(FALSE, FALSE, sizeof(char *));
1198 {
1199 int count=0;
1200 char **tree_path_cur2 = hlev_state_infos[pstate->bstate].tree_path;
1201 while(*tree_path_cur2) {
1202 count++;
1203 tree_path_cur2++;
1204 }
1205 g_array_append_vals(tree_path_garray, hlev_state_infos[pstate->bstate].tree_path, count);
1206 }
1207 modify_path_with_private(tree_path_garray, pstate);
1208
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;
1213
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);
1218 }
1219
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);
1231
1232 g_array_append_val(all_nodes, newnode);
1233 }
1234 node_cur = newnode;
1235
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);
1238 }
1239 }
1240
1241 /* print the summary */
1242 print_summary_item(&base_node, -1);
1243
1244 printf("\n");
1245 }
1246
1247 printf("\n");
1248
1249 printf("Episode list\n");
1250
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);
1254
1255 /* Sort the episodes from longest to shortest */
1256 g_array_sort(node->episodes, compare_states_length);
1257
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);
1262
1263 printf("\t\t");
1264 print_time(st->time_begin);
1265 printf("-");
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)));
1268 }
1269 }
1270 }
1271
1272 static void flush_process_sstacks(void)
1273 {
1274 GList *pinfos;
1275
1276 pinfos = g_hash_table_get_values(process_hash_table);
1277 while(pinfos) {
1278 struct process *pinfo = (struct process *)pinfos->data;
1279
1280 sstack_force_flush(pinfo->stack);
1281
1282 pinfos = pinfos->next;
1283 }
1284
1285 g_list_free(pinfos);
1286 }
1287
1288 struct family_item {
1289 int pid;
1290 LttTime creation;
1291 };
1292
1293 void print_range_reports(int pid, LttTime t1, LttTime t2)
1294 {
1295 GArray *family = g_array_new(FALSE, FALSE, sizeof(struct family_item));
1296 int i;
1297
1298 /* reconstruct the parental sequence */
1299 for(;;) {
1300 struct process *pinfo;
1301 struct family_item fi;
1302 LttTime cur_beg;
1303
1304 pinfo = g_hash_table_lookup(process_hash_table, &pid);
1305 if(pinfo == NULL)
1306 abort();
1307
1308 fi.pid = 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);
1312
1313 if(ltt_time_compare(cur_beg, t1) == -1) {
1314 /* current pid starts before the interesting time */
1315 break;
1316 }
1317 if(pinfo->parent == -1) {
1318 printf("unable to go back, we don't know the parent of %d\n", fi.pid);
1319 abort();
1320 }
1321 /* else, we go on */
1322 pid = pinfo->parent;
1323
1324 }
1325
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;
1330
1331 if(i == family->len-1)
1332 iter_t1 = t1;
1333 else
1334 iter_t1 = g_array_index(family, struct family_item, i).creation;
1335
1336 if(i == 0)
1337 iter_t2 = t2;
1338 else
1339 iter_t2 = g_array_index(family, struct family_item, i-1).creation;
1340
1341 printf("This section of summary concerns pid %d between ");
1342 print_time(iter_t1);
1343 printf(" and ");
1344 print_time(iter_t2);
1345 printf(".\n");
1346 print_simple_summary_pid_range(iter_pid, iter_t1, iter_t2);
1347 }
1348 print_range_critical_path(depanalysis_range_pid, t1, t2);
1349 }
1350
1351 static gboolean write_traceset_footer(void *hook_data, void *call_data)
1352 {
1353 LttvTracesetContext *tc = (LttvTracesetContext *)call_data;
1354
1355 g_info("TextDump traceset footer");
1356
1357 fprintf(a_file,"End trace set\n\n");
1358
1359 // if(LTTV_IS_TRACESET_STATS(tc)) {
1360 // lttv_stats_sum_traceset((LttvTracesetStats *)tc, ltt_time_infinite);
1361 // print_stats(a_file, (LttvTracesetStats *)tc);
1362 // }
1363
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.
1367 */
1368 flush_process_sstacks();
1369
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;
1377
1378 if(depanalysis_range_pid >= 0) {
1379 print_range_reports(depanalysis_range_pid, depanalysis_time1, depanalysis_time2);
1380 }
1381 else
1382 printf("range critical path: could not find the end of the range\n");
1383 }
1384
1385 return FALSE;
1386 }
1387
1388 #if 0
1389 static gboolean write_trace_header(void *hook_data, void *call_data)
1390 {
1391 LttvTraceContext *tc = (LttvTraceContext *)call_data;
1392 #if 0 //FIXME
1393 LttSystemDescription *system = ltt_trace_system_description(tc->t);
1394
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));
1399 #endif //0
1400 return FALSE;
1401 }
1402 #endif
1403
1404
1405 static int write_event_content(void *hook_data, void *call_data)
1406 {
1407 gboolean result;
1408
1409 // LttvIAttribute *attributes = LTTV_IATTRIBUTE(lttv_global_attributes());
1410
1411 LttvTracefileContext *tfc = (LttvTracefileContext *)call_data;
1412
1413 LttvTracefileState *tfs = (LttvTracefileState *)call_data;
1414
1415 LttEvent *e;
1416
1417 guint cpu = tfs->cpu;
1418 LttvTraceState *ts = (LttvTraceState*)tfc->t_context;
1419 LttvProcessState *process = ts->running_process[cpu];
1420
1421 e = ltt_tracefile_get_event(tfc->tf);
1422
1423 lttv_event_to_string(e, a_string, TRUE, 1, tfs);
1424
1425 // if(a_state) {
1426 g_string_append_printf(a_string, " %s ",
1427 g_quark_to_string(process->state->s));
1428 // }
1429
1430 g_string_append_printf(a_string,"\n");
1431
1432 fputs(a_string->str, a_file);
1433 return FALSE;
1434 }
1435
1436 static int field_get_value_int(struct LttEvent *e, struct marker_info *info, GQuark f)
1437 {
1438 struct marker_field *marker_field;
1439 int found=0;
1440
1441 for_each_marker_field(marker_field, info) {
1442 if (marker_field->name == f) {
1443 found = 1;
1444 break;
1445 }
1446 }
1447 g_assert(found);
1448 return ltt_event_get_long_unsigned(e, marker_field);
1449 }
1450
1451 static char *field_get_value_string(struct LttEvent *e, struct marker_info *info, GQuark f)
1452 {
1453 struct marker_field *marker_field;
1454 int found=0;
1455
1456 for_each_marker_field(marker_field, info) {
1457 if (marker_field->name == f) {
1458 found = 1;
1459 break;
1460 }
1461 }
1462 g_assert(found);
1463 return ltt_event_get_string(e, marker_field);
1464 }
1465
1466 void process_delayed_stack_action(struct process *pinfo, struct sstack_item *item)
1467 {
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);
1472 //printf("\n");
1473 //printf("stack before:\n");
1474 //print_stack(pinfo->stack);
1475
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);
1481 }
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);
1487 }
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;
1493
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;
1497
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);
1502
1503 /*
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
1508 }*/
1509
1510 }
1511 }
1512 }
1513
1514 //printf("stack after:\n");
1515 //print_stack(pinfo->stack);
1516 }
1517
1518 static struct process *get_or_init_process_info(struct LttEvent *e, GQuark name, int pid, int *new)
1519 {
1520 gconstpointer val;
1521
1522 val = g_hash_table_lookup(process_hash_table, &pid);
1523 if(val == NULL) {
1524 struct process *pinfo;
1525 int i;
1526
1527 /* Initialize new pinfo for newly discovered process */
1528 pinfo = g_malloc(sizeof(struct process));
1529 pinfo->pid = pid;
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));
1538 }
1539
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;
1544
1545 /* set the name */
1546 pinfo->name = name;
1547
1548 g_hash_table_insert(process_hash_table, &pinfo->pid, pinfo);
1549 if(new)
1550 *new = 1;
1551 return pinfo;
1552 }
1553 else {
1554 if(new)
1555 *new = 0;
1556 return val;
1557
1558 }
1559 }
1560
1561 static int differentiate_swappers(int pid, LttEvent *e)
1562 {
1563 if(pid == 0)
1564 return pid+e->tracefile->cpu_num+2000000;
1565 else
1566 return pid;
1567 }
1568
1569 static int process_event(void *hook_data, void *call_data)
1570 {
1571 LttvTracefileContext *tfc = (LttvTracefileContext *)call_data;
1572 LttvTracefileState *tfs = (LttvTracefileState *)call_data;
1573 LttEvent *e;
1574 struct marker_info *info;
1575
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;
1582
1583 e = ltt_tracefile_get_event(tfs->parent.tf);
1584
1585 info = marker_get_info_from_id(tfc->tf->mdata, e->event_id);
1586
1587 //if(depanalysis_use_time && (ltt_time_compare(e->timestamp, arg_t1) == -1 || ltt_time_compare(e->timestamp, arg_t2) == 1)) {
1588 // return;
1589 //}
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;
1594 }
1595 else {
1596 /* Should stop processing and print results */
1597 }
1598 }
1599
1600 /* Code to limit the event count */
1601 if(depanalysis_event_limit > 0) {
1602 depanalysis_event_limit--;
1603 }
1604 else if(depanalysis_event_limit == 0) {
1605 write_traceset_footer(hook_data, call_data);
1606 printf("exit due to event limit reached\n");
1607 exit(0);
1608 }
1609
1610 /* write event like textDump for now, for debugging purposes */
1611 //write_event_content(hook_data, call_data);
1612
1613 if(tfc->tf->name == LTT_CHANNEL_SYSCALL_STATE && info->name == LTT_EVENT_SYS_CALL_TABLE) {
1614 GQuark q;
1615 int *pint = g_malloc(sizeof(int));
1616
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);
1620 }
1621 else if(tfc->tf->name == LTT_CHANNEL_IRQ_STATE && info->name == LTT_EVENT_LIST_INTERRUPT) {
1622 GQuark q;
1623 int *pint = g_malloc(sizeof(int));
1624
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);
1628 }
1629 else if(tfc->tf->name == LTT_CHANNEL_SOFTIRQ_STATE && info->name == LTT_EVENT_SOFTIRQ_VEC) {
1630 GQuark q;
1631 int *pint = g_malloc(sizeof(int));
1632
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);
1636 }
1637
1638
1639 /* Only look at events after the statedump is finished.
1640 * Before that, the pids in the LttvProcessState are not reliable
1641 */
1642 if(statedump_finished == 0) {
1643 if(tfc->tf->name == LTT_CHANNEL_GLOBAL_STATE && info->name == LTT_EVENT_STATEDUMP_END)
1644 statedump_finished = 1;
1645 else
1646 return FALSE;
1647
1648 }
1649
1650 pinfo = get_or_init_process_info(e, process->name, differentiate_swappers(process->pid, e), NULL);
1651
1652 /* the state machine
1653 * Process the event in the context of each process
1654 */
1655
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;
1659
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);
1663 }
1664 else if(tfc->tf->name == LTT_CHANNEL_KERNEL && info->name == LTT_EVENT_IRQ_EXIT) {
1665 struct process *event_process_info = pinfo;
1666
1667 prepare_pop_item_commit(event_process_info, LLEV_IRQ, e->event_time);
1668 }
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);
1672 if(next_pid != 0) {
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);
1675 }
1676 if(prev_pid != 0) {
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);
1679
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);
1683 }
1684 }
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;
1688
1689 item = prepare_push_item(event_process_info, LLEV_TRAP, e->event_time);
1690 commit_item(event_process_info, item);
1691 }
1692 else if(tfc->tf->name == LTT_CHANNEL_KERNEL && info->name == LTT_EVENT_TRAP_EXIT) {
1693 struct process *event_process_info = pinfo;
1694
1695 prepare_pop_item_commit(event_process_info, LLEV_TRAP, e->event_time);
1696 }
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;
1700
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);
1705 }
1706 else if(tfc->tf->name == LTT_CHANNEL_KERNEL && info->name == LTT_EVENT_SYSCALL_EXIT) {
1707 struct process *event_process_info = pinfo;
1708
1709 prepare_pop_item_commit(event_process_info, LLEV_SYSCALL, e->event_time);
1710 }
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;
1714
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);
1718 }
1719 else if(tfc->tf->name == LTT_CHANNEL_KERNEL && info->name == LTT_EVENT_SOFT_IRQ_EXIT) {
1720 struct process *event_process_info = pinfo;
1721
1722 prepare_pop_item_commit(event_process_info, LLEV_SOFTIRQ, e->event_time);
1723 }
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;
1728
1729 event_process_info->parent = process->pid;
1730 //printf("At ");
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);
1733
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);
1741
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);
1746
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);
1748
1749 }
1750 else if(tfc->tf->name == LTT_CHANNEL_FS && info->name == LTT_EVENT_EXEC) {
1751 struct process *event_process_info = pinfo;
1752
1753 guint cpu = tfs->cpu;
1754 LttvProcessState *process_state = ts->running_process[cpu];
1755 event_process_info->name = process_state->name;
1756 }
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;
1761
1762 /* TODO: this is too easy */
1763 if(pstate == NULL)
1764 goto next_iter;
1765
1766 llev_syscall_private = (struct llev_state_info_syscall *)pstate->private;
1767
1768 //printf("depanalysis: found an open with state %d in pid %d\n", pstate->bstate, process->pid);
1769 if(pstate->bstate == LLEV_UNKNOWN)
1770 goto next_iter;
1771
1772 g_assert(pstate->bstate == LLEV_SYSCALL);
1773 g_assert(llev_syscall_private->substate == LLEV_SYSCALL__UNDEFINED);
1774
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;
1779
1780 llev_syscall_open_private->filename = g_quark_from_string(field_get_value_string(e, info, LTT_FIELD_FILENAME));
1781
1782 }
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;
1787 GQuark pfileq;
1788 int fd;
1789
1790 /* TODO: this is too easy */
1791 if(pstate == NULL)
1792 goto next_iter;
1793
1794 llev_syscall_private = (struct llev_state_info_syscall *)pstate->private;
1795
1796 //printf("depanalysis: found an read with state %d in pid %d\n", pstate->bstate, process->pid);
1797 if(pstate->bstate == LLEV_UNKNOWN)
1798 goto next_iter;
1799
1800 g_assert(pstate->bstate == LLEV_SYSCALL);
1801 g_assert(llev_syscall_private->substate == LLEV_SYSCALL__UNDEFINED);
1802
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;
1807
1808 fd = field_get_value_int(e, info, LTT_FIELD_FD);
1809 pfileq = g_hash_table_lookup(process->fds, fd);
1810 if(pfileq) {
1811 llev_syscall_read_private->filename = pfileq;
1812 }
1813 else {
1814 char *tmp;
1815 asprintf(&tmp, "Unknown filename, fd %d", fd);
1816 llev_syscall_read_private->filename = g_quark_from_string(tmp);
1817 free(tmp);
1818 }
1819 }
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;
1824 GQuark pfileq;
1825 int fd;
1826
1827 /* TODO: this is too easy */
1828 if(pstate == NULL)
1829 goto next_iter;
1830
1831 llev_syscall_private = (struct llev_state_info_syscall *)pstate->private;
1832
1833 //printf("depanalysis: found an poll with state %d in pid %d\n", pstate->bstate, process->pid);
1834 if(pstate->bstate == LLEV_UNKNOWN)
1835 goto next_iter;
1836
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
1842 */
1843 if(llev_syscall_private->substate == LLEV_SYSCALL__POLL)
1844 goto next_iter;
1845
1846 g_assert(pstate->bstate == LLEV_SYSCALL);
1847 g_assert(llev_syscall_private->substate == LLEV_SYSCALL__UNDEFINED);
1848
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;
1853
1854 fd = field_get_value_int(e, info, LTT_FIELD_FD);
1855 pfileq = g_hash_table_lookup(process->fds, fd);
1856 if(pfileq) {
1857 llev_syscall_poll_private->filename = pfileq;
1858 }
1859 else {
1860 char *tmp;
1861 asprintf(&tmp, "Unknown filename, fd %d", fd);
1862 llev_syscall_poll_private->filename = g_quark_from_string(tmp);
1863 free(tmp);
1864 }
1865 }
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;
1872 int result;
1873
1874 se->event_type = HLEV_EVENT_TRY_WAKEUP;
1875 se->private = twe;
1876 //printf("pushing try wake up event in context of %d\n", pinfo->pid);
1877
1878 twe->pid = differentiate_swappers(process->pid, e);
1879 twe->time = e->event_time;
1880 twe->waker = pinfo;
1881
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);
1884 if(!target_pinfo)
1885 goto next_iter;
1886
1887 item->data_val = se;
1888 item->delete_data_val = delete_data_val;
1889
1890 sstack_add_item(target_pinfo->stack, item);
1891
1892 /* Now pop the blocked schedule out of the target */
1893 result = try_pop_blocked_llev_preempted(target_pinfo, e->event_time);
1894
1895 if(result) {
1896 struct sstack_item *item;
1897 struct process *event_process_info = target_pinfo;
1898
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);
1902 }
1903
1904 }
1905
1906 next_iter:
1907 skip_state_machine:
1908 return FALSE;
1909 }
1910
1911 void print_sstack_private(struct sstack_item *item)
1912 {
1913 struct process_with_state *pwstate = item->data_val;
1914
1915 if(pwstate && item->data_type == SSTACK_TYPE_PUSH)
1916 printf("\tstate: %s", llev_state_infos[pwstate->state.bstate].name);
1917
1918 printf(" (");
1919 print_time(pwstate->state.time_begin);
1920 printf("-");
1921 print_time(pwstate->state.time_end);
1922 printf("\n");
1923
1924 }
1925
1926 static LttTime ltt_time_from_string(const char *str)
1927 {
1928 LttTime retval;
1929
1930 char *decdot = strchr(str, '.');
1931
1932 if(decdot) {
1933 *decdot = '\0';
1934 retval.tv_nsec = atol(decdot+1);
1935 }
1936 else {
1937 retval.tv_nsec = 0;
1938 }
1939
1940 retval.tv_sec = atol(str);
1941
1942 return retval;
1943 }
1944
1945 static void arg_t1(void *hook_data)
1946 {
1947 printf("arg_t1\n");
1948 depanalysis_use_time |= 1;
1949 depanalysis_time1 = ltt_time_from_string(arg_t1_str);
1950 }
1951
1952 static void arg_t2(void *hook_data)
1953 {
1954 depanalysis_use_time |= 2;
1955 depanalysis_time2 = ltt_time_from_string(arg_t2_str);
1956 }
1957
1958 static void arg_pid(void *hook_data)
1959 {
1960 }
1961
1962 static void arg_limit(void *hook_data)
1963 {
1964 }
1965
1966 static void init()
1967 {
1968 gboolean result;
1969
1970 print_sstack_item_data = print_sstack_private;
1971
1972 LttvAttributeValue value;
1973
1974 LttvIAttribute *attributes = LTTV_IATTRIBUTE(lttv_global_attributes());
1975
1976 a_file = stdout;
1977
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);
1986
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);
1991
1992 a_string = g_string_new("");
1993
1994 result = lttv_iattribute_find_by_path(attributes, "hooks/event",
1995 LTTV_POINTER, &value);
1996 g_assert(result);
1997 event_hook = *(value.v_pointer);
1998 g_assert(event_hook);
1999 lttv_hooks_add(event_hook, process_event, NULL, LTTV_PRIO_DEFAULT);
2000
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);
2007 //
2008 result = lttv_iattribute_find_by_path(attributes, "hooks/traceset/before",
2009 LTTV_POINTER, &value);
2010 g_assert(result);
2011 before_traceset = *(value.v_pointer);
2012 g_assert(before_traceset);
2013 lttv_hooks_add(before_traceset, write_traceset_header, NULL,
2014 LTTV_PRIO_DEFAULT);
2015
2016 result = lttv_iattribute_find_by_path(attributes, "hooks/traceset/after",
2017 LTTV_POINTER, &value);
2018 g_assert(result);
2019 after_traceset = *(value.v_pointer);
2020 g_assert(after_traceset);
2021 lttv_hooks_add(after_traceset, write_traceset_footer, NULL,
2022 LTTV_PRIO_DEFAULT);
2023 }
2024
2025 static void destroy()
2026 {
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");
2031
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);
2036
2037 g_string_free(a_string, TRUE);
2038
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);
2043 }
2044
2045 LTTV_MODULE("depanalysis", "Dependency analysis test", \
2046 "Produce a dependency analysis of a trace", \
2047 init, destroy, "stats", "batchAnalysis", "option", "print")
2048
This page took 0.12115 seconds and 4 git commands to generate.