Commit | Line | Data |
---|---|---|
8c108c1c PMF |
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; | |
55cd92ee | 52 | static int a_print_simple_summary = 0; |
8c108c1c PMF |
53 | static LttTime depanalysis_time1, depanalysis_time2; |
54 | static char *arg_t1_str,*arg_t2_str; | |
55 | static int statedump_finished = 0; | |
56 | ||
57 | ||
58 | struct llev_state_info_irq { | |
59 | int irq; | |
60 | }; | |
61 | ||
62 | struct llev_state_info_softirq { | |
63 | int softirq; | |
64 | }; | |
65 | ||
66 | struct llev_state_info_syscall { | |
67 | int syscall_id; | |
68 | ||
69 | int substate; | |
70 | ||
71 | void *private; | |
72 | }; | |
73 | ||
74 | struct llev_state_info_syscall__open { | |
75 | GQuark filename; | |
76 | }; | |
77 | ||
78 | struct llev_state_info_syscall__read { | |
79 | GQuark filename; | |
80 | }; | |
81 | ||
82 | struct llev_state_info_syscall__poll { | |
83 | GQuark filename; | |
84 | }; | |
85 | ||
86 | struct llev_state_info_preempted { | |
87 | int prev_state; | |
88 | }; | |
89 | ||
90 | struct hlev_state_info_blocked { | |
91 | int syscall_id; | |
92 | unsigned char trap; /* flag */ | |
93 | int substate; | |
94 | ||
95 | /* Garray of pointers to struct process_state that reflect the | |
96 | * low-level state stack when respectively entering and exiting the blocked | |
97 | * state. | |
98 | */ | |
99 | GArray *llev_state_entry; | |
100 | GArray *llev_state_exit; | |
101 | ||
102 | int pid_exit; /* FIXME: it's not pretty to have this here; find this info elsewhere */ | |
103 | LttTime time_woken; | |
104 | ||
105 | void *private; | |
106 | }; | |
107 | ||
108 | struct hlev_state_info_blocked__open { | |
109 | GQuark filename; | |
110 | }; | |
111 | ||
112 | struct hlev_state_info_blocked__read { | |
113 | GQuark filename; | |
114 | }; | |
115 | ||
116 | struct hlev_state_info_blocked__poll { | |
117 | GQuark filename; | |
118 | }; | |
119 | ||
120 | struct hlev_state_info_interrupted_irq { | |
121 | int irq; | |
122 | }; | |
123 | ||
124 | struct hlev_state_info_interrupted_softirq { | |
125 | int softirq; | |
126 | }; | |
127 | ||
128 | struct summary_tree_node { | |
129 | char *name; | |
130 | GHashTable *children; | |
131 | LttTime duration; | |
132 | GArray *episodes; | |
133 | int id_for_episodes; | |
134 | }; | |
135 | ||
136 | struct state_info { | |
137 | char name[40]; | |
138 | int size_priv; | |
139 | char *tree_path[6]; | |
140 | }; | |
141 | ||
142 | struct state_info llev_state_infos[] = { | |
143 | { "UNKNOWN", 0, { NULL } }, | |
144 | { "RUNNING", 0, { NULL } }, | |
145 | { "SYSCALL", sizeof(struct llev_state_info_syscall), { NULL } }, | |
146 | { "IRQ", sizeof(struct llev_state_info_irq), { NULL } }, | |
147 | { "SOFTIRQ", sizeof(struct llev_state_info_softirq), { NULL } }, | |
148 | { "TRAP", 0, { NULL } }, | |
149 | { "PREEMPTED", sizeof(struct llev_state_info_preempted), { NULL } }, | |
150 | }; | |
151 | ||
152 | struct state_info hlev_state_infos[] = { | |
153 | { "UNKNOWN", 0, { "Total", "Unknown", NULL } }, | |
154 | { "RUNNING", 0, { "Total", "Working", NULL } }, | |
155 | { "BLOCKED", sizeof(struct hlev_state_info_blocked), { "Total", "Blocked", NULL } }, | |
156 | { "INTERRUPTED_IRQ", sizeof(struct hlev_state_info_interrupted_irq), { "Total", "Interrupted", "IRQ", NULL } }, | |
157 | { "INTERRUPTED_SOFTIRQ", sizeof(struct hlev_state_info_interrupted_softirq), { "Total", "Interrupted", "SoftIRQ", NULL } }, | |
158 | { "INTERRUPTED_CPU", 0, { "Total", "Interrupted", "Preempted", NULL } }, | |
159 | { "INTERRUPTED_POST_BLOCK", 0, { "Total", "Interrupted", "Waiting schedule after blocking", NULL } }, | |
160 | }; | |
161 | ||
162 | enum llev_state { | |
163 | LLEV_UNKNOWN=0, | |
164 | LLEV_RUNNING, | |
165 | LLEV_SYSCALL, | |
166 | LLEV_IRQ, | |
167 | LLEV_SOFTIRQ, | |
168 | LLEV_TRAP, | |
169 | LLEV_PREEMPTED, | |
170 | }; | |
171 | ||
172 | enum llev_syscall_substate { | |
173 | LLEV_SYSCALL__UNDEFINED, | |
174 | LLEV_SYSCALL__OPEN, | |
175 | LLEV_SYSCALL__READ, | |
176 | LLEV_SYSCALL__POLL, | |
177 | }; | |
178 | ||
179 | enum hlev_event { | |
180 | HLEV_EVENT_TRY_WAKEUP=0, | |
181 | }; | |
182 | ||
183 | enum hlev_state { | |
184 | HLEV_UNKNOWN=0, | |
185 | HLEV_RUNNING, | |
186 | HLEV_BLOCKED, | |
187 | HLEV_INTERRUPTED_IRQ, | |
188 | HLEV_INTERRUPTED_SOFTIRQ, | |
189 | HLEV_INTERRUPTED_CPU, | |
190 | HLEV_INTERRUPTED_POST_BLOCK, | |
191 | }; | |
192 | ||
193 | enum hlev_state_blocked { | |
194 | HLEV_BLOCKED__UNDEFINED, | |
195 | HLEV_BLOCKED__OPEN, | |
196 | HLEV_BLOCKED__READ, | |
197 | HLEV_BLOCKED__POLL, | |
198 | }; | |
199 | ||
200 | struct sstack_event { | |
201 | int event_type; | |
202 | void *private; | |
203 | }; | |
204 | ||
205 | struct try_wakeup_event { | |
206 | int pid; /* this sould be more precise avec pid may be reused */ | |
207 | LttTime time; | |
208 | struct process *waker; | |
209 | }; | |
210 | ||
211 | struct process_state { | |
212 | int bstate; | |
213 | int cause_type; | |
214 | void *private; | |
215 | ||
216 | LttTime time_begin; | |
217 | LttTime time_end; | |
218 | }; | |
219 | ||
220 | struct process_with_state { | |
221 | struct process *process; | |
222 | struct process_state state; | |
223 | }; | |
224 | ||
225 | #define PROCESS_STATE_STACK_SIZE 10 | |
226 | struct process { | |
227 | int pid; | |
228 | GQuark name; | |
229 | int parent; | |
230 | ||
231 | struct sstack *stack; | |
232 | struct process_state *llev_state_stack[PROCESS_STATE_STACK_SIZE]; | |
233 | int stack_current; | |
234 | struct process_state *hlev_state; | |
235 | GArray *hlev_history; | |
236 | }; | |
237 | ||
238 | static inline void *old_process_state_private_data(struct process *p) | |
239 | { | |
240 | return p->llev_state_stack[p->stack_current]->private; | |
241 | } | |
242 | ||
243 | static inline struct process_state *process_find_state(struct process *p, enum llev_state st) | |
244 | { | |
245 | int i; | |
246 | ||
247 | for(i=p->stack->array->len-1; i>=0; i--) { | |
248 | struct sstack_item *item = g_array_index(p->stack->array, struct sstack_item *, i); | |
249 | ||
250 | struct process_with_state *pwstate = item->data_val; | |
251 | if(pwstate->state.bstate == st) { | |
252 | return &pwstate->state; | |
253 | } | |
254 | } | |
255 | ||
256 | return NULL; | |
257 | } | |
258 | ||
259 | static int find_pos_in_stack(enum llev_state lls, struct process *p) | |
260 | { | |
261 | int i; | |
262 | for(i=p->stack_current; i>=0; i--) { | |
263 | if(p->llev_state_stack[i]->bstate == lls) | |
264 | return i; | |
265 | } | |
266 | ||
267 | return -1; | |
268 | } | |
269 | ||
270 | static struct process_state *find_in_stack(enum llev_state lls, struct process *p) | |
271 | { | |
272 | int result; | |
273 | ||
274 | result = find_pos_in_stack(lls, p); | |
275 | ||
276 | if(result >= 0) | |
277 | return p->llev_state_stack[result]; | |
278 | else | |
279 | return NULL; | |
280 | ||
281 | } | |
282 | ||
283 | /* called back from sstack on deletion of a data_val which is | |
284 | * a struct process_with_state | |
285 | */ | |
286 | ||
287 | static void delete_data_val(struct process_with_state *pwstate) | |
288 | { | |
289 | // FIXME: Free this also | |
290 | //g_free(pwstate->state.private); | |
291 | ||
292 | // FIXME: this is really ugly. Don't free the pwstate if the state is LLEV_RUNNING. | |
293 | // LLEV_RUNNING is a special case that's being processed and deleted immediately after | |
294 | // being inserted on the sstack, to prevent state begin accumulated because it couldn't | |
295 | // be processed before the end of the trace. If we free the state, we get invalid memory | |
296 | // reads when looking at it on the state_stack. | |
297 | //if(pwstate->state.bstate != LLEV_RUNNING) | |
298 | // g_free(pwstate); | |
299 | } | |
300 | ||
b1d18041 PMF |
301 | inline void print_time(LttTime t) |
302 | { | |
303 | //printf("%lu.%lu", t.tv_sec, t.tv_nsec); | |
304 | double f; | |
305 | f = (double)t.tv_sec + ((double)t.tv_nsec)/1000000000.0; | |
306 | printf("%.9f", f); | |
307 | } | |
308 | ||
8c108c1c PMF |
309 | static struct sstack_item *prepare_push_item(struct process *p, enum llev_state st, LttTime t) |
310 | { | |
311 | struct process_with_state *pwstate = g_malloc(sizeof(struct process_with_state)); | |
312 | struct sstack_item *item; | |
313 | ||
314 | int wait_for_pop = 0; | |
315 | ||
316 | if(st == LLEV_SYSCALL) { | |
317 | /* We need to push LLEV_SYSCALL as wait_for_pop because it depends on some of | |
318 | * its children. If we don't do this, it's going to get processed immediately | |
319 | * by the sstack and we might miss some details about it that will come later. | |
320 | */ | |
321 | wait_for_pop = 1; | |
322 | } | |
323 | ||
324 | item = sstack_item_new_push(wait_for_pop); | |
325 | ||
326 | //printf("pushing in context of %d\n", p->pid); | |
327 | ||
328 | pwstate->process = p; | |
329 | pwstate->state.bstate = st; | |
330 | pwstate->state.time_begin = t; | |
331 | pwstate->state.private = g_malloc(llev_state_infos[st].size_priv); | |
332 | ||
333 | item->data_val = pwstate; | |
b1d18041 PMF |
334 | item->delete_data_val = (void (*)(void*))delete_data_val; |
335 | ||
336 | return item; | |
8c108c1c PMF |
337 | } |
338 | ||
339 | static void *item_private(struct sstack_item *item) | |
340 | { | |
341 | struct process_with_state *pwstate = item->data_val; | |
342 | return pwstate->state.private; | |
343 | } | |
344 | ||
345 | static void commit_item(struct process *p, struct sstack_item *item) | |
346 | { | |
347 | sstack_add_item(p->stack, item); | |
348 | } | |
349 | ||
350 | static void old_process_push_llev_state(struct process *p, struct process_state *pstate) | |
351 | { | |
352 | if(++p->stack_current >= PROCESS_STATE_STACK_SIZE) { | |
353 | fprintf(stderr, "depanalysis: internal process stack overflow\n"); | |
354 | abort(); | |
355 | } | |
356 | ||
357 | p->llev_state_stack[p->stack_current] = pstate; | |
358 | } | |
359 | ||
360 | static void live_complete_process_push_llev_state(struct process *p, enum llev_state st, LttTime t) | |
361 | { | |
362 | struct process_state *pstate = g_malloc(sizeof(struct process_state)); | |
363 | ||
364 | pstate->bstate = st; | |
365 | pstate->time_begin = t; | |
366 | pstate->private = g_malloc(llev_state_infos[st].size_priv); | |
367 | ||
368 | old_process_push_llev_state(p, pstate); | |
369 | } | |
370 | ||
371 | static void prepare_pop_item_commit_nocheck(struct process *p, enum llev_state st, LttTime t) | |
372 | { | |
373 | struct process_with_state *pwstate; | |
374 | struct sstack_item *item = sstack_item_new_pop(); | |
375 | ||
376 | int push_idx; | |
377 | ||
378 | if(p->stack->pushes->len > 0) | |
379 | push_idx = g_array_index(p->stack->pushes, int, p->stack->pushes->len-1); | |
380 | else | |
381 | push_idx = -1; | |
382 | ||
383 | if(push_idx >= 0) { | |
384 | pwstate = g_array_index(p->stack->array, struct sstack_item *, push_idx)->data_val; | |
385 | pwstate->process = p; | |
386 | pwstate->state.time_end = t; | |
387 | item->data_val = pwstate; | |
388 | /* don't set delete_data_val because we use the same pwstate as push, and we don't want to free it twice */ | |
389 | } | |
390 | else { | |
391 | ||
392 | pwstate = g_malloc(sizeof(struct process_with_state)); | |
393 | pwstate->process = p; | |
394 | item->data_val = pwstate; | |
395 | pwstate->state.time_end = t; | |
396 | pwstate->state.bstate = st; | |
397 | } | |
398 | ||
399 | sstack_add_item(p->stack, item); | |
400 | ||
401 | } | |
402 | ||
403 | static void prepare_pop_item_commit(struct process *p, enum llev_state st, LttTime t) | |
404 | { | |
405 | struct process_with_state *pwstate; | |
406 | struct sstack_item *item = sstack_item_new_pop(); | |
407 | ||
408 | int push_idx; | |
409 | ||
410 | if(p->stack->pushes->len > 0) | |
411 | push_idx = g_array_index(p->stack->pushes, int, p->stack->pushes->len-1); | |
412 | else | |
413 | push_idx = -1; | |
414 | ||
415 | if(push_idx >= 0) { | |
416 | /* FIXME: ugly workaround for kernel bug that generates two kernel_arch_syscall_exit on fork. | |
417 | * The bug only occurs upon creation of new processes. But these processes always have | |
418 | * a LLEV_RUNNING at index 0. */ | |
419 | if(push_idx >= p->stack->array->len) | |
420 | return; | |
421 | ||
422 | pwstate = g_array_index(p->stack->array, struct sstack_item *, push_idx)->data_val; | |
423 | ||
424 | if(pwstate->state.bstate != st) { | |
425 | /* FIXME: ugly workaround for kernel bug that generates two kernel_arch_syscall_exit on fork */ | |
426 | if(st != LLEV_SYSCALL) { | |
427 | printf("bad pop! at "); | |
428 | print_time(t); | |
429 | printf("\n"); | |
430 | print_stack(p->stack); | |
431 | abort(); | |
432 | } | |
433 | else { | |
434 | /* case where we have a double syscall_exit */ | |
435 | return; | |
436 | } | |
437 | } | |
438 | } | |
439 | ||
440 | prepare_pop_item_commit_nocheck(p, st, t); | |
441 | } | |
442 | ||
443 | ||
444 | static int try_pop_blocked_llev_preempted(struct process *p, LttTime t) | |
445 | { | |
446 | int push_idx; | |
447 | struct process_with_state *pwstate; | |
448 | ||
449 | if(p->stack->pushes->len > 0) | |
450 | push_idx = g_array_index(p->stack->pushes, int, p->stack->pushes->len-1); | |
451 | else | |
452 | push_idx = -1; | |
453 | ||
454 | if(push_idx >= 0) { | |
455 | pwstate = g_array_index(p->stack->array, struct sstack_item *, push_idx)->data_val; | |
456 | ||
457 | if(!(pwstate->state.bstate == LLEV_PREEMPTED && ((struct llev_state_info_preempted *)pwstate->state.private)->prev_state > 0)) { | |
55cd92ee | 458 | //printf("double try wake up\n"); |
8c108c1c PMF |
459 | return 0; |
460 | } | |
461 | } | |
462 | ||
463 | prepare_pop_item_commit_nocheck(p, LLEV_PREEMPTED, t); | |
464 | return 1; | |
465 | } | |
466 | ||
467 | static void old_process_pop_llev_state(struct process *p, struct process_state *pstate) | |
468 | { | |
469 | /* Ensure we are really popping the current state */ | |
470 | /* FIXME: pstate->bstate is uninitialized? */ | |
471 | // Commenting because it does not work. The way things work now, this check cannot work. | |
472 | //if(p->llev_state_stack[p->stack_current]->bstate != LLEV_UNKNOWN && p->llev_state_stack[p->stack_current]->bstate != pstate->bstate) { | |
473 | // printf("ERROR! bad pop!\n"); | |
474 | // abort(); | |
475 | //} | |
476 | ||
477 | /* Actually change the that position */ | |
478 | if(p->stack_current >= 0) | |
479 | p->stack_current--; | |
480 | ||
481 | /* If stack empty, we must put something in it */ | |
482 | if(p->stack_current == -1) { | |
483 | if(pstate->bstate == LLEV_SYSCALL) { | |
484 | //process_push_llev_state(p, LLEV_RUNNING, pstate->time_end); | |
485 | live_complete_process_push_llev_state(p, LLEV_RUNNING, pstate->time_end); | |
486 | } | |
487 | else { | |
488 | live_complete_process_push_llev_state(p, LLEV_UNKNOWN, pstate->time_end); | |
489 | } | |
490 | } | |
491 | } | |
492 | ||
493 | static GHashTable *process_hash_table; | |
494 | static GHashTable *syscall_table; | |
495 | static GHashTable *irq_table; | |
496 | static GHashTable *softirq_table; | |
497 | ||
498 | /* Insert the hooks before and after each trace and tracefile, and for each | |
499 | event. Print a global header. */ | |
500 | ||
501 | static FILE *a_file; | |
502 | ||
503 | static GString *a_string; | |
504 | ||
505 | static gboolean write_traceset_header(void *hook_data, void *call_data) | |
506 | { | |
507 | LttvTracesetContext *tc = (LttvTracesetContext *)call_data; | |
508 | ||
509 | g_info("Traceset header"); | |
510 | ||
511 | /* Print the trace set header */ | |
55cd92ee | 512 | g_info(a_file,"Trace set contains %d traces\n\n", |
8c108c1c PMF |
513 | lttv_traceset_number(tc->ts)); |
514 | ||
515 | return FALSE; | |
516 | } | |
517 | ||
b1d18041 | 518 | GArray *oldstyle_stack_to_garray(struct process_state **oldstyle_stack, int current) |
8c108c1c PMF |
519 | { |
520 | GArray *retval; | |
521 | int i; | |
522 | ||
b1d18041 | 523 | retval = g_array_new(FALSE, FALSE, sizeof(struct process_state *)); |
8c108c1c PMF |
524 | |
525 | for(i=0; i<current; i++) { | |
526 | g_array_append_val(retval, oldstyle_stack[i]); | |
527 | } | |
528 | ||
529 | return retval; | |
530 | } | |
531 | ||
532 | static void update_hlev_state(struct process *p, LttTime t) | |
533 | { | |
534 | int i; | |
535 | ||
536 | enum hlev_state new_hlev; | |
537 | ||
538 | for(i=p->stack_current; i>=0; i--) { | |
539 | enum llev_state st; | |
540 | st = p->llev_state_stack[i]->bstate; | |
541 | ||
542 | if(st == LLEV_RUNNING || st == LLEV_TRAP || st == LLEV_SYSCALL) { | |
543 | new_hlev = HLEV_RUNNING; | |
544 | break; | |
545 | } | |
546 | else if(st == LLEV_IRQ) { | |
547 | new_hlev = HLEV_INTERRUPTED_IRQ; | |
548 | break; | |
549 | } | |
550 | else if(st == LLEV_SOFTIRQ) { | |
551 | new_hlev = HLEV_INTERRUPTED_SOFTIRQ; | |
552 | break; | |
553 | } | |
554 | else if(st == LLEV_PREEMPTED) { | |
555 | int prev_state = ((struct llev_state_info_preempted *) old_process_state_private_data(p))->prev_state; | |
556 | ||
557 | if(prev_state == 0) { | |
558 | new_hlev = HLEV_INTERRUPTED_CPU; | |
559 | } | |
560 | else if(prev_state == -1) { | |
561 | new_hlev = HLEV_INTERRUPTED_POST_BLOCK; | |
562 | } | |
563 | else { | |
564 | new_hlev = HLEV_BLOCKED; | |
565 | } | |
566 | break; | |
567 | } | |
568 | else if(st == LLEV_UNKNOWN) { | |
569 | new_hlev = HLEV_UNKNOWN; | |
570 | break; | |
571 | } | |
572 | else { | |
573 | abort(); | |
574 | } | |
575 | } | |
576 | ||
577 | /* If no state change, do nothing */ | |
578 | if(p->hlev_state != NULL && new_hlev == p->hlev_state->bstate) { | |
579 | return; | |
580 | } | |
581 | ||
582 | p->hlev_state->time_end = t; | |
583 | /* This check is here because we initially put HLEV_UNKNOWN as hlev state, but in the case | |
584 | * of processes newly created, it is immediately replaced by HLEV_BLOCKED. In order to avoid | |
585 | * having a UNKNOWN state of duration 0 in the summary, we don't add it. This isn't as elegant | |
586 | * as it ought to be. | |
587 | */ | |
588 | if(ltt_time_compare(p->hlev_state->time_begin, p->hlev_state->time_end) != 0) | |
589 | g_array_append_val(p->hlev_history, p->hlev_state); | |
590 | p->hlev_state = g_malloc(sizeof(struct process_state)); | |
591 | p->hlev_state->bstate = new_hlev; | |
592 | p->hlev_state->time_begin = t; | |
593 | p->hlev_state->private = g_malloc(hlev_state_infos[new_hlev].size_priv); | |
594 | ||
595 | //printf("depanalysis: now at hlev state %s\n", hlev_state_infos[new_hlev].name); | |
596 | ||
597 | /* Set private data */ | |
598 | switch(p->hlev_state->bstate) { | |
599 | case HLEV_UNKNOWN: | |
600 | break; | |
601 | case HLEV_RUNNING: | |
602 | break; | |
603 | case HLEV_BLOCKED: { | |
604 | struct hlev_state_info_blocked *hlev_blocked_private = p->hlev_state->private; | |
605 | //struct process_state *ps = find_in_stack(LLEV_SYSCALL, p); | |
606 | int syscall_pos = find_pos_in_stack(LLEV_SYSCALL, p); | |
607 | int trap_pos = find_pos_in_stack(LLEV_TRAP, p); | |
608 | ||
609 | /* init vals */ | |
610 | hlev_blocked_private->syscall_id = 1; | |
611 | hlev_blocked_private->trap = 0; | |
612 | hlev_blocked_private->substate = HLEV_BLOCKED__UNDEFINED; | |
613 | hlev_blocked_private->private = NULL; | |
614 | hlev_blocked_private->llev_state_entry = oldstyle_stack_to_garray(p->llev_state_stack, p->stack_current); | |
615 | hlev_blocked_private->llev_state_exit = NULL; | |
616 | ||
617 | //g_assert(syscall_pos >= 0 || trap_pos >= 0); | |
618 | ||
619 | if(trap_pos > syscall_pos) { | |
620 | hlev_blocked_private->trap = 1; | |
621 | } | |
622 | ||
623 | /* initial value, may be changed below */ | |
624 | hlev_blocked_private->substate = HLEV_BLOCKED__UNDEFINED; | |
625 | ||
626 | if(syscall_pos >= 0) { | |
627 | struct process_state *ps = p->llev_state_stack[syscall_pos]; | |
628 | struct llev_state_info_syscall *llev_syscall_private = (struct llev_state_info_syscall *) ps->private; | |
629 | hlev_blocked_private->syscall_id = llev_syscall_private->syscall_id; | |
630 | ||
631 | if(llev_syscall_private->substate == LLEV_SYSCALL__OPEN) { | |
632 | struct llev_state_info_syscall__open *llev_syscall_open_private; | |
633 | struct hlev_state_info_blocked__open *hlev_blocked_open_private; | |
634 | llev_syscall_open_private = llev_syscall_private->private; | |
635 | hlev_blocked_private->substate = HLEV_BLOCKED__OPEN; | |
636 | hlev_blocked_open_private = g_malloc(sizeof(struct hlev_state_info_blocked__open)); | |
637 | hlev_blocked_private->private = hlev_blocked_open_private; | |
638 | hlev_blocked_open_private->filename = llev_syscall_open_private->filename; | |
639 | ||
640 | //printf("depanalysis: blocked in an open!\n"); | |
641 | } | |
642 | else if(llev_syscall_private->substate == LLEV_SYSCALL__READ) { | |
643 | struct llev_state_info_syscall__read *llev_syscall_read_private; | |
644 | struct hlev_state_info_blocked__read *hlev_blocked_read_private; | |
645 | llev_syscall_read_private = llev_syscall_private->private; | |
646 | hlev_blocked_private->substate = HLEV_BLOCKED__READ; | |
647 | hlev_blocked_read_private = g_malloc(sizeof(struct hlev_state_info_blocked__read)); | |
648 | hlev_blocked_private->private = hlev_blocked_read_private; | |
649 | hlev_blocked_read_private->filename = llev_syscall_read_private->filename; | |
650 | ||
651 | //printf("depanalysis: blocked in a read!\n"); | |
652 | } | |
653 | else if(llev_syscall_private->substate == LLEV_SYSCALL__POLL) { | |
654 | struct llev_state_info_syscall__poll *llev_syscall_poll_private; | |
655 | struct hlev_state_info_blocked__poll *hlev_blocked_poll_private; | |
656 | llev_syscall_poll_private = llev_syscall_private->private; | |
657 | hlev_blocked_private->substate = HLEV_BLOCKED__POLL; | |
658 | hlev_blocked_poll_private = g_malloc(sizeof(struct hlev_state_info_blocked__poll)); | |
659 | hlev_blocked_private->private = hlev_blocked_poll_private; | |
660 | hlev_blocked_poll_private->filename = llev_syscall_poll_private->filename; | |
661 | ||
662 | //printf("depanalysis: blocked in a read!\n"); | |
663 | } | |
664 | } | |
665 | else { | |
666 | hlev_blocked_private->syscall_id = -1; | |
667 | } | |
668 | ||
669 | break; | |
670 | } | |
671 | case HLEV_INTERRUPTED_IRQ: { | |
672 | struct hlev_state_info_interrupted_irq *sinfo = p->hlev_state->private; | |
673 | struct process_state *ps = find_in_stack(LLEV_IRQ, p); | |
674 | if(ps == NULL) | |
675 | abort(); | |
676 | else | |
677 | sinfo->irq = ((struct llev_state_info_irq *) ps->private)->irq; | |
678 | break; | |
679 | } | |
680 | case HLEV_INTERRUPTED_SOFTIRQ: { | |
681 | struct hlev_state_info_interrupted_softirq *sinfo = p->hlev_state->private; | |
682 | struct process_state *ps = find_in_stack(LLEV_SOFTIRQ, p); | |
683 | if(ps == NULL) | |
684 | abort(); | |
685 | else | |
686 | sinfo->softirq = ((struct llev_state_info_softirq *) ps->private)->softirq; | |
687 | break; | |
688 | } | |
689 | default: | |
690 | break; | |
691 | }; | |
692 | } | |
693 | ||
694 | static gint compare_summary_tree_node_times(gconstpointer a, gconstpointer b) | |
695 | { | |
696 | struct summary_tree_node *n1 = (struct summary_tree_node *) a; | |
697 | struct summary_tree_node *n2 = (struct summary_tree_node *) b; | |
698 | ||
699 | return ltt_time_compare(n2->duration, n1->duration); | |
700 | } | |
701 | ||
702 | /* Print an item of the simple summary tree, and recurse, printing its children. | |
703 | * | |
704 | * If depth == -1, this is the root: we don't print a label, we only recurse into | |
705 | * the children. | |
706 | */ | |
707 | ||
708 | static void print_summary_item(struct summary_tree_node *node, int depth) | |
709 | { | |
710 | GList *vals; | |
711 | ||
712 | if(depth >= 0) { | |
713 | printf("\t%*s (", strlen(node->name)+2*depth, node->name); | |
714 | print_time(node->duration); | |
715 | printf(") <%d>\n", node->id_for_episodes); | |
716 | } | |
717 | ||
718 | if(!node->children) | |
719 | return; | |
720 | ||
721 | vals = g_hash_table_get_values(node->children); | |
722 | ||
723 | /* sort the values */ | |
724 | vals = g_list_sort(vals, compare_summary_tree_node_times); | |
725 | ||
726 | while(vals) { | |
727 | print_summary_item((struct summary_tree_node *)vals->data, depth+1); | |
728 | vals = vals->next; | |
729 | } | |
730 | ||
731 | /* we must free the list returned by g_hash_table_get_values() */ | |
732 | g_list_free(vals); | |
733 | } | |
734 | ||
735 | static inline void print_irq(int irq) | |
736 | { | |
737 | printf("IRQ %d [%s]", irq, g_quark_to_string(g_hash_table_lookup(irq_table, &irq))); | |
738 | } | |
739 | ||
740 | static inline void print_softirq(int softirq) | |
741 | { | |
742 | printf("SoftIRQ %d [%s]", softirq, g_quark_to_string(g_hash_table_lookup(softirq_table, &softirq))); | |
743 | } | |
744 | ||
745 | static inline void print_pid(int pid) | |
746 | { | |
747 | struct process *event_process_info = g_hash_table_lookup(process_hash_table, &pid); | |
748 | ||
749 | char *pname; | |
750 | ||
751 | if(event_process_info == NULL) | |
752 | pname = "?"; | |
753 | else | |
754 | pname = g_quark_to_string(event_process_info->name); | |
755 | printf("%d [%s]", pid, pname); | |
756 | } | |
757 | ||
758 | static void modify_path_with_private(GArray *path, struct process_state *pstate) | |
759 | { | |
760 | //GString tmps = g_string_new(""); | |
761 | char *tmps; | |
762 | ||
763 | // FIXME: fix this leak | |
764 | switch(pstate->bstate) { | |
765 | case HLEV_INTERRUPTED_IRQ: | |
766 | 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))); | |
767 | g_array_append_val(path, tmps); | |
768 | break; | |
769 | case HLEV_INTERRUPTED_SOFTIRQ: | |
770 | 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))); | |
771 | g_array_append_val(path, tmps); | |
772 | break; | |
773 | case HLEV_BLOCKED: { | |
774 | struct hlev_state_info_blocked *hlev_blocked_private = (struct hlev_state_info_blocked *)pstate->private; | |
775 | ||
776 | if(hlev_blocked_private->trap) { | |
777 | char *ptr = "Trap"; | |
778 | g_array_append_val(path, ptr); | |
779 | } | |
780 | ||
781 | if(hlev_blocked_private->syscall_id == -1) { | |
782 | char *ptr = "Userspace"; | |
783 | g_array_append_val(path, ptr); | |
784 | } | |
785 | else { | |
786 | 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))); | |
787 | g_array_append_val(path, tmps); | |
788 | } | |
789 | ||
790 | if(((struct hlev_state_info_blocked *)pstate->private)->substate == HLEV_BLOCKED__OPEN) { | |
791 | char *str = g_quark_to_string(((struct hlev_state_info_blocked__open *)((struct hlev_state_info_blocked *)pstate->private)->private)->filename); | |
792 | g_array_append_val(path, str); | |
793 | } | |
794 | else if(((struct hlev_state_info_blocked *)pstate->private)->substate == HLEV_BLOCKED__READ) { | |
795 | char *str; | |
796 | asprintf(&str, "%s", g_quark_to_string(((struct hlev_state_info_blocked__read *)((struct hlev_state_info_blocked *)pstate->private)->private)->filename)); | |
797 | g_array_append_val(path, str); | |
798 | /* FIXME: this must be freed at some point */ | |
799 | //free(str); | |
800 | } | |
801 | else if(((struct hlev_state_info_blocked *)pstate->private)->substate == HLEV_BLOCKED__POLL) { | |
802 | char *str; | |
803 | asprintf(&str, "%s", g_quark_to_string(((struct hlev_state_info_blocked__poll *)((struct hlev_state_info_blocked *)pstate->private)->private)->filename)); | |
804 | g_array_append_val(path, str); | |
805 | /* FIXME: this must be freed at some point */ | |
806 | //free(str); | |
807 | } | |
808 | break; | |
809 | } | |
810 | }; | |
811 | } | |
812 | ||
813 | void print_stack_garray_horizontal(GArray *stack) | |
814 | { | |
815 | /* FIXME: this function doesn't work if we delete the states as we process them because we | |
816 | * try to read those states here to print the low level stack. | |
817 | */ | |
818 | int i; | |
819 | ||
820 | for(i=0; i<stack->len; i++) { | |
821 | struct process_state *pstate = g_array_index(stack, struct process_state *, i); | |
822 | printf("%s", llev_state_infos[pstate->bstate].name); | |
823 | ||
824 | if(pstate->bstate == LLEV_SYSCALL) { | |
825 | struct llev_state_info_syscall *llev_syscall_private = pstate->private; | |
826 | printf(" %d [%s]", llev_syscall_private->syscall_id, g_quark_to_string(g_hash_table_lookup(syscall_table, &llev_syscall_private->syscall_id))); | |
827 | } | |
828 | ||
829 | printf(", "); | |
830 | ||
831 | } | |
832 | } | |
833 | ||
834 | static int dicho_search_state_ending_after(struct process *p, LttTime t) | |
835 | { | |
836 | int under = 0; | |
837 | int over = p->hlev_history->len-1; | |
838 | struct process_state *pstate; | |
839 | int result; | |
840 | ||
841 | if(over < 1) | |
842 | return -1; | |
843 | ||
844 | /* If the last element is smaller or equal than the time we are searching for, | |
845 | * no match | |
846 | */ | |
847 | pstate = g_array_index(p->hlev_history, struct process_state *, over); | |
848 | if(ltt_time_compare(pstate->time_end, t) <= 0) { | |
849 | return -1; | |
850 | } | |
851 | /* no need to check for the equal case */ | |
852 | ||
853 | pstate = g_array_index(p->hlev_history, struct process_state *, under); | |
854 | result = ltt_time_compare(pstate->time_end, t); | |
855 | if(result >= 1) { | |
856 | /* trivial match at the first element if it is greater or equal | |
857 | * than the time we want | |
858 | */ | |
859 | return under; | |
860 | } | |
861 | ||
862 | while(1) { | |
863 | int dicho; | |
864 | ||
865 | dicho = (under+over)/2; | |
866 | pstate = g_array_index(p->hlev_history, struct process_state *, dicho); | |
867 | result = ltt_time_compare(pstate->time_end, t); | |
868 | ||
869 | if(result == -1) { | |
870 | under = dicho; | |
871 | } | |
872 | else if(result == 1) { | |
873 | over = dicho; | |
874 | } | |
875 | else { | |
876 | /* exact match */ | |
877 | return dicho+1; | |
878 | } | |
879 | ||
880 | if(over-under == 1) { | |
881 | /* we have converged */ | |
882 | return over; | |
883 | } | |
884 | } | |
885 | ||
886 | } | |
887 | ||
888 | /* FIXME: this shouldn't be based on pids in case of reuse | |
889 | * FIXME: should add a list of processes used to avoid loops | |
890 | */ | |
891 | ||
892 | static struct process_state *find_state_ending_after(int pid, LttTime t) | |
893 | { | |
894 | struct process *p; | |
895 | int result; | |
896 | ||
897 | ||
898 | p = g_hash_table_lookup(process_hash_table, &pid); | |
899 | if(!p) | |
900 | return NULL; | |
901 | ||
902 | result = dicho_search_state_ending_after(p, t); | |
903 | ||
904 | if(result == -1) | |
905 | return NULL; | |
906 | else | |
907 | return g_array_index(p->hlev_history, struct process_state *, result); | |
908 | } | |
909 | ||
910 | static void print_delay_pid(int pid, LttTime t1, LttTime t2, int offset) | |
911 | { | |
912 | struct process *p; | |
913 | int i; | |
914 | ||
915 | p = g_hash_table_lookup(process_hash_table, &pid); | |
916 | if(!p) | |
917 | return; | |
918 | ||
919 | i = dicho_search_state_ending_after(p, t1); | |
920 | for(; i<p->hlev_history->len; i++) { | |
921 | struct process_state *pstate = g_array_index(p->hlev_history, struct process_state *, i); | |
922 | if(ltt_time_compare(pstate->time_end, t2) > 0) | |
923 | break; | |
924 | ||
925 | if(pstate->bstate == HLEV_BLOCKED) { | |
926 | struct hlev_state_info_blocked *state_private_blocked; | |
927 | state_private_blocked = pstate->private; | |
928 | struct process_state *state_unblocked; | |
929 | ||
930 | printf("%*s", 8*offset, ""); | |
931 | printf("Blocked in "); | |
932 | print_stack_garray_horizontal(state_private_blocked->llev_state_entry); | |
933 | ||
934 | printf("(times: "); | |
935 | print_time(pstate->time_begin); | |
936 | printf("-"); | |
937 | print_time(pstate->time_end); | |
938 | ||
939 | printf(", dur: %f)\n", 1e-9*ltt_time_to_double(ltt_time_sub(pstate->time_end, pstate->time_begin))); | |
940 | ||
941 | state_unblocked = find_state_ending_after(state_private_blocked->pid_exit, state_private_blocked->time_woken); | |
942 | if(state_unblocked) { | |
943 | if(state_unblocked->bstate == HLEV_INTERRUPTED_IRQ) { | |
944 | struct hlev_state_info_interrupted_irq *priv = state_unblocked->private; | |
945 | /* if in irq or softirq, we don't care what the waking process was doing because they are asynchroneous events */ | |
946 | printf("%*s", 8*offset, ""); | |
947 | printf("Woken up by an IRQ: "); | |
948 | print_irq(priv->irq); | |
949 | printf("\n"); | |
950 | } | |
951 | else if(state_unblocked->bstate == HLEV_INTERRUPTED_SOFTIRQ) { | |
952 | struct hlev_state_info_interrupted_softirq *priv = state_unblocked->private; | |
953 | printf("%*s", 8*offset, ""); | |
954 | printf("Woken up by a SoftIRQ: "); | |
955 | print_softirq(priv->softirq); | |
956 | printf("\n"); | |
957 | } | |
958 | else { | |
959 | LttTime t1prime=t1; | |
960 | LttTime t2prime=t2; | |
961 | ||
962 | if(ltt_time_compare(t1prime, pstate->time_begin) < 0) | |
963 | t1prime = pstate->time_begin; | |
964 | if(ltt_time_compare(t2prime, pstate->time_end) > 0) | |
965 | t2prime = pstate->time_end; | |
966 | ||
967 | print_delay_pid(state_private_blocked->pid_exit, t1prime, t2prime, offset+1); | |
968 | printf("%*s", 8*offset, ""); | |
969 | printf("Woken up in context of "); | |
970 | print_pid(state_private_blocked->pid_exit); | |
971 | if(state_private_blocked->llev_state_exit) { | |
972 | print_stack_garray_horizontal(state_private_blocked->llev_state_exit); | |
973 | } | |
974 | else { | |
975 | } | |
976 | printf(" in high-level state %s", hlev_state_infos[state_unblocked->bstate].name); | |
977 | printf("\n"); | |
978 | } | |
979 | } | |
980 | else { | |
981 | printf("%*s", 8*offset, ""); | |
982 | printf("Weird... cannot find in what state the waker (%d) was\n", state_private_blocked->pid_exit); | |
983 | } | |
984 | ||
985 | ||
986 | //print_delay_pid(state_private_blocked->pid_exit, pstate->time_start, pstate->time_end); | |
987 | //printf("\t\t Woken up in context of %d: ", state_private_blocked->pid_exit); | |
988 | //if(state_private_blocked->llev_state_exit) { | |
989 | // print_stack_garray_horizontal(state_private_blocked->llev_state_exit); | |
990 | // printf("here3 (%d)\n", state_private_blocked->llev_state_exit->len); | |
991 | //} | |
992 | //else | |
993 | // printf("the private_blocked %p had a null exit stack\n", state_private_blocked); | |
994 | //printf("\n"); | |
995 | } | |
996 | } | |
997 | } | |
998 | ||
999 | static void print_range_critical_path(int process, LttTime t1, LttTime t2) | |
1000 | { | |
1001 | printf("Critical path for requested range:\n"); | |
1002 | printf("Final process is %d\n", process); | |
1003 | print_delay_pid(process, t1, t2, 2); | |
1004 | } | |
1005 | ||
1006 | static void print_process_critical_path_summary() | |
1007 | { | |
1008 | struct process *pinfo; | |
1009 | GList *pinfos; | |
1010 | int i,j; | |
1011 | ||
1012 | pinfos = g_hash_table_get_values(process_hash_table); | |
1013 | if(pinfos == NULL) { | |
1014 | fprintf(stderr, "error: no process found\n"); | |
1015 | return; | |
1016 | } | |
1017 | ||
1018 | printf("Process Critical Path Summary:\n"); | |
1019 | ||
1020 | for(;;) { | |
1021 | struct summary_tree_node base_node = { children: NULL }; | |
1022 | ||
1023 | struct process_state *hlev_state_cur; | |
1024 | ||
1025 | pinfo = (struct process *)pinfos->data; | |
55cd92ee MD |
1026 | if (depanalysis_range_pid_searching != -1 && pinfo->pid != depanalysis_range_pid_searching) |
1027 | goto next_iter; | |
8c108c1c PMF |
1028 | printf("\tProcess %d [%s]\n", pinfo->pid, g_quark_to_string(pinfo->name)); |
1029 | ||
1030 | if(pinfo->hlev_history->len < 1) | |
1031 | goto next_iter; | |
1032 | ||
1033 | 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); | |
1034 | ||
1035 | next_iter: | |
1036 | ||
1037 | if(pinfos->next) | |
1038 | pinfos = pinfos->next; | |
1039 | else | |
1040 | break; | |
1041 | } | |
1042 | } | |
1043 | ||
1044 | gint compare_states_length(gconstpointer a, gconstpointer b) | |
1045 | { | |
1046 | struct process_state **s1 = (struct process_state **)a; | |
1047 | struct process_state **s2 = (struct process_state **)b; | |
1048 | gint val; | |
1049 | ||
1050 | val = ltt_time_compare(ltt_time_sub((*s2)->time_end, (*s2)->time_begin), ltt_time_sub((*s1)->time_end, (*s1)->time_begin)); | |
1051 | return val; | |
1052 | } | |
1053 | ||
55cd92ee | 1054 | static void print_simple_summary(void) |
8c108c1c PMF |
1055 | { |
1056 | struct process *pinfo; | |
1057 | GList *pinfos; | |
1058 | GList *pinfos_first; | |
1059 | int i,j; | |
1060 | int id_for_episodes = 0; | |
1061 | ||
55cd92ee MD |
1062 | if (!a_print_simple_summary) |
1063 | return; | |
1064 | ||
8c108c1c PMF |
1065 | /* we save all the nodes here to print the episodes table quickly */ |
1066 | GArray *all_nodes = g_array_new(FALSE, FALSE, sizeof(struct summary_tree_node *)); | |
1067 | ||
1068 | pinfos_first = g_hash_table_get_values(process_hash_table); | |
1069 | if(pinfos_first == NULL) { | |
1070 | fprintf(stderr, "error: no processes found\n"); | |
1071 | return; | |
1072 | } | |
1073 | pinfos = pinfos_first; | |
1074 | ||
1075 | printf("Simple summary:\n"); | |
1076 | ||
1077 | /* For each process */ | |
1078 | for(;;) { | |
1079 | struct summary_tree_node base_node = { children: NULL, name: "Root" }; | |
1080 | ||
1081 | struct process_state *hlev_state_cur; | |
1082 | ||
1083 | pinfo = (struct process *)pinfos->data; | |
1084 | printf("\tProcess %d [%s]\n", pinfo->pid, g_quark_to_string(pinfo->name)); | |
1085 | ||
1086 | /* For each state in the process history */ | |
1087 | for(i=0; i<pinfo->hlev_history->len; i++) { | |
1088 | struct process_state *pstate = g_array_index(pinfo->hlev_history, struct process_state *, i); | |
1089 | struct summary_tree_node *node_cur = &base_node; | |
1090 | GArray *tree_path_garray; | |
1091 | ||
1092 | /* Modify the path based on private data */ | |
1093 | tree_path_garray = g_array_new(FALSE, FALSE, sizeof(char *)); | |
1094 | { | |
1095 | int count=0; | |
1096 | char **tree_path_cur2 = hlev_state_infos[pstate->bstate].tree_path; | |
1097 | while(*tree_path_cur2) { | |
1098 | count++; | |
1099 | tree_path_cur2++; | |
1100 | } | |
1101 | g_array_append_vals(tree_path_garray, hlev_state_infos[pstate->bstate].tree_path, count); | |
1102 | } | |
1103 | modify_path_with_private(tree_path_garray, pstate); | |
1104 | ||
1105 | /* Walk the path, adding the nodes to the summary */ | |
1106 | for(j=0; j<tree_path_garray->len; j++) { | |
1107 | struct summary_tree_node *newnode; | |
1108 | GQuark componentquark; | |
1109 | ||
1110 | /* Have a path component we must follow */ | |
1111 | if(!node_cur->children) { | |
1112 | /* must create the hash table for the children */ | |
1113 | node_cur->children = g_hash_table_new(g_int_hash, g_int_equal); | |
1114 | } | |
1115 | ||
1116 | /* try to get the node for the next component */ | |
1117 | componentquark = g_quark_from_string(g_array_index(tree_path_garray, char *, j)); | |
1118 | newnode = g_hash_table_lookup(node_cur->children, &componentquark); | |
1119 | if(newnode == NULL) { | |
1120 | newnode = g_malloc(sizeof(struct summary_tree_node)); | |
1121 | newnode->children = NULL; | |
1122 | newnode->name = g_array_index(tree_path_garray, char *, j); | |
1123 | newnode->duration = ltt_time_zero; | |
1124 | newnode->id_for_episodes = id_for_episodes++; | |
1125 | newnode->episodes = g_array_new(FALSE, FALSE, sizeof(struct process_state *)); | |
1126 | g_hash_table_insert(node_cur->children, &componentquark, newnode); | |
1127 | ||
1128 | g_array_append_val(all_nodes, newnode); | |
1129 | } | |
1130 | node_cur = newnode; | |
1131 | ||
1132 | node_cur->duration = ltt_time_add(node_cur->duration, ltt_time_sub(pstate->time_end, pstate->time_begin)); | |
1133 | g_array_append_val(node_cur->episodes, pstate); | |
1134 | } | |
1135 | } | |
1136 | ||
1137 | /* print the summary */ | |
1138 | print_summary_item(&base_node, -1); | |
1139 | ||
1140 | printf("\n"); | |
1141 | ||
1142 | if(pinfos->next) | |
1143 | pinfos = pinfos->next; | |
1144 | else | |
1145 | break; | |
1146 | } | |
1147 | ||
1148 | printf("\n"); | |
1149 | ||
1150 | printf("Episode list\n"); | |
1151 | pinfos = pinfos_first; | |
1152 | ||
1153 | /* For all the nodes of the Simple summary tree */ | |
1154 | for(i=0; i<all_nodes->len; i++) { | |
1155 | struct summary_tree_node *node = (struct summary_tree_node *)g_array_index(all_nodes, struct summary_tree_node *, i); | |
1156 | ||
1157 | /* Sort the episodes from longest to shortest */ | |
1158 | g_array_sort(node->episodes, compare_states_length); | |
1159 | ||
1160 | printf("\tNode id: <%d>\n", node->id_for_episodes); | |
1161 | /* For each episode of the node */ | |
1162 | for(j=0; j<node->episodes->len; j++) { | |
1163 | struct process_state *st = g_array_index(node->episodes, struct process_state *, j); | |
1164 | ||
1165 | printf("\t\t"); | |
1166 | print_time(st->time_begin); | |
1167 | printf("-"); | |
1168 | print_time(st->time_end); | |
1169 | printf(" (%f)\n", 1e-9*ltt_time_to_double(ltt_time_sub(st->time_end,st->time_begin))); | |
1170 | } | |
1171 | } | |
1172 | } | |
1173 | ||
1174 | static void print_simple_summary_pid_range(int pid, LttTime t1, LttTime t2) | |
1175 | { | |
1176 | struct process *pinfo; | |
1177 | int i,j; | |
1178 | int id_for_episodes = 0; | |
1179 | ||
1180 | /* we save all the nodes here to print the episodes table quickly */ | |
1181 | GArray *all_nodes = g_array_new(FALSE, FALSE, sizeof(struct summary_tree_node *)); | |
1182 | ||
1183 | pinfo = g_hash_table_lookup(process_hash_table, &pid); | |
1184 | ||
1185 | { | |
1186 | struct summary_tree_node base_node = { children: NULL, name: "Root" }; | |
1187 | ||
1188 | struct process_state *hlev_state_cur; | |
1189 | ||
1190 | printf("\tProcess %d [%s]\n", pinfo->pid, g_quark_to_string(pinfo->name)); | |
1191 | ||
1192 | /* For each state in the process history */ | |
1193 | for(i=0; i<pinfo->hlev_history->len; i++) { | |
1194 | struct process_state *pstate = g_array_index(pinfo->hlev_history, struct process_state *, i); | |
1195 | struct summary_tree_node *node_cur = &base_node; | |
1196 | GArray *tree_path_garray; | |
1197 | ||
1198 | if(ltt_time_compare(pstate->time_end, t1) < 0) | |
1199 | continue; | |
1200 | ||
1201 | if(ltt_time_compare(pstate->time_end, t2) > 0) | |
1202 | break; | |
1203 | ||
1204 | /* Modify the path based on private data */ | |
1205 | tree_path_garray = g_array_new(FALSE, FALSE, sizeof(char *)); | |
1206 | { | |
1207 | int count=0; | |
1208 | char **tree_path_cur2 = hlev_state_infos[pstate->bstate].tree_path; | |
1209 | while(*tree_path_cur2) { | |
1210 | count++; | |
1211 | tree_path_cur2++; | |
1212 | } | |
1213 | g_array_append_vals(tree_path_garray, hlev_state_infos[pstate->bstate].tree_path, count); | |
1214 | } | |
1215 | modify_path_with_private(tree_path_garray, pstate); | |
1216 | ||
1217 | /* Walk the path, adding the nodes to the summary */ | |
1218 | for(j=0; j<tree_path_garray->len; j++) { | |
1219 | struct summary_tree_node *newnode; | |
1220 | GQuark componentquark; | |
1221 | ||
1222 | /* Have a path component we must follow */ | |
1223 | if(!node_cur->children) { | |
1224 | /* must create the hash table for the children */ | |
1225 | node_cur->children = g_hash_table_new(g_int_hash, g_int_equal); | |
1226 | } | |
1227 | ||
1228 | /* try to get the node for the next component */ | |
1229 | componentquark = g_quark_from_string(g_array_index(tree_path_garray, char *, j)); | |
1230 | newnode = g_hash_table_lookup(node_cur->children, &componentquark); | |
1231 | if(newnode == NULL) { | |
1232 | newnode = g_malloc(sizeof(struct summary_tree_node)); | |
1233 | newnode->children = NULL; | |
1234 | newnode->name = g_array_index(tree_path_garray, char *, j); | |
1235 | newnode->duration = ltt_time_zero; | |
1236 | newnode->id_for_episodes = id_for_episodes++; | |
1237 | newnode->episodes = g_array_new(FALSE, FALSE, sizeof(struct process_state *)); | |
1238 | g_hash_table_insert(node_cur->children, &componentquark, newnode); | |
1239 | ||
1240 | g_array_append_val(all_nodes, newnode); | |
1241 | } | |
1242 | node_cur = newnode; | |
1243 | ||
1244 | node_cur->duration = ltt_time_add(node_cur->duration, ltt_time_sub(pstate->time_end, pstate->time_begin)); | |
1245 | g_array_append_val(node_cur->episodes, pstate); | |
1246 | } | |
1247 | } | |
1248 | ||
1249 | /* print the summary */ | |
1250 | print_summary_item(&base_node, -1); | |
1251 | ||
1252 | printf("\n"); | |
1253 | } | |
1254 | ||
1255 | printf("\n"); | |
1256 | ||
1257 | printf("Episode list\n"); | |
1258 | ||
1259 | /* For all the nodes of the Simple summary tree */ | |
1260 | for(i=0; i<all_nodes->len; i++) { | |
1261 | struct summary_tree_node *node = (struct summary_tree_node *)g_array_index(all_nodes, struct summary_tree_node *, i); | |
1262 | ||
1263 | /* Sort the episodes from longest to shortest */ | |
1264 | g_array_sort(node->episodes, compare_states_length); | |
1265 | ||
1266 | printf("\tNode id: <%d>\n", node->id_for_episodes); | |
1267 | /* For each episode of the node */ | |
1268 | for(j=0; j<node->episodes->len; j++) { | |
1269 | struct process_state *st = g_array_index(node->episodes, struct process_state *, j); | |
1270 | ||
1271 | printf("\t\t"); | |
1272 | print_time(st->time_begin); | |
1273 | printf("-"); | |
1274 | print_time(st->time_end); | |
1275 | printf(" (%f)\n", 1e-9*ltt_time_to_double(ltt_time_sub(st->time_end,st->time_begin))); | |
1276 | } | |
1277 | } | |
1278 | } | |
1279 | ||
1280 | static void flush_process_sstacks(void) | |
1281 | { | |
1282 | GList *pinfos; | |
1283 | ||
1284 | pinfos = g_hash_table_get_values(process_hash_table); | |
1285 | while(pinfos) { | |
1286 | struct process *pinfo = (struct process *)pinfos->data; | |
1287 | ||
1288 | sstack_force_flush(pinfo->stack); | |
1289 | ||
1290 | pinfos = pinfos->next; | |
1291 | } | |
1292 | ||
1293 | g_list_free(pinfos); | |
1294 | } | |
1295 | ||
1296 | struct family_item { | |
1297 | int pid; | |
1298 | LttTime creation; | |
1299 | }; | |
1300 | ||
1301 | void print_range_reports(int pid, LttTime t1, LttTime t2) | |
1302 | { | |
1303 | GArray *family = g_array_new(FALSE, FALSE, sizeof(struct family_item)); | |
1304 | int i; | |
1305 | ||
1306 | /* reconstruct the parental sequence */ | |
1307 | for(;;) { | |
1308 | struct process *pinfo; | |
1309 | struct family_item fi; | |
1310 | LttTime cur_beg; | |
1311 | ||
1312 | pinfo = g_hash_table_lookup(process_hash_table, &pid); | |
1313 | if(pinfo == NULL) | |
1314 | abort(); | |
1315 | ||
1316 | fi.pid = pid; | |
1317 | cur_beg = g_array_index(pinfo->hlev_history, struct process_state *, 0)->time_begin; | |
1318 | fi.creation = cur_beg; | |
1319 | g_array_append_val(family, fi); | |
1320 | ||
1321 | if(ltt_time_compare(cur_beg, t1) == -1) { | |
1322 | /* current pid starts before the interesting time */ | |
1323 | break; | |
1324 | } | |
1325 | if(pinfo->parent == -1) { | |
1326 | printf("unable to go back, we don't know the parent of %d\n", fi.pid); | |
1327 | abort(); | |
1328 | } | |
1329 | /* else, we go on */ | |
1330 | pid = pinfo->parent; | |
1331 | ||
1332 | } | |
1333 | ||
1334 | printf("Simple summary for range:\n"); | |
1335 | for(i=family->len-1; i>=0; i--) { | |
1336 | LttTime iter_t1, iter_t2; | |
1337 | int iter_pid = g_array_index(family, struct family_item, i).pid; | |
1338 | ||
1339 | if(i == family->len-1) | |
1340 | iter_t1 = t1; | |
1341 | else | |
1342 | iter_t1 = g_array_index(family, struct family_item, i).creation; | |
1343 | ||
1344 | if(i == 0) | |
1345 | iter_t2 = t2; | |
1346 | else | |
1347 | iter_t2 = g_array_index(family, struct family_item, i-1).creation; | |
1348 | ||
1349 | printf("This section of summary concerns pid %d between "); | |
1350 | print_time(iter_t1); | |
1351 | printf(" and "); | |
1352 | print_time(iter_t2); | |
1353 | printf(".\n"); | |
1354 | print_simple_summary_pid_range(iter_pid, iter_t1, iter_t2); | |
1355 | } | |
1356 | print_range_critical_path(depanalysis_range_pid, t1, t2); | |
1357 | } | |
1358 | ||
1359 | static gboolean write_traceset_footer(void *hook_data, void *call_data) | |
1360 | { | |
1361 | LttvTracesetContext *tc = (LttvTracesetContext *)call_data; | |
1362 | ||
1363 | g_info("TextDump traceset footer"); | |
1364 | ||
55cd92ee | 1365 | g_info(a_file,"End trace set\n\n"); |
8c108c1c PMF |
1366 | |
1367 | // if(LTTV_IS_TRACESET_STATS(tc)) { | |
1368 | // lttv_stats_sum_traceset((LttvTracesetStats *)tc, ltt_time_infinite); | |
1369 | // print_stats(a_file, (LttvTracesetStats *)tc); | |
1370 | // } | |
1371 | ||
1372 | /* After processing all the events, we need to flush the sstacks | |
1373 | * because some unfinished states may remain in them. We want them | |
1374 | * event though there are incomplete. | |
1375 | */ | |
1376 | flush_process_sstacks(); | |
1377 | ||
1378 | /* print the reports */ | |
1379 | print_simple_summary(); | |
1380 | print_process_critical_path_summary(); | |
8c108c1c | 1381 | if(depanalysis_use_time == 3) { |
55cd92ee | 1382 | printf("depanalysis_use_time = %d\n", depanalysis_use_time); |
8c108c1c PMF |
1383 | if(depanalysis_range_pid == -1 && depanalysis_range_pid_searching >= 0) |
1384 | depanalysis_range_pid = depanalysis_range_pid_searching; | |
1385 | ||
1386 | if(depanalysis_range_pid >= 0) { | |
1387 | print_range_reports(depanalysis_range_pid, depanalysis_time1, depanalysis_time2); | |
1388 | } | |
1389 | else | |
1390 | printf("range critical path: could not find the end of the range\n"); | |
1391 | } | |
1392 | ||
1393 | return FALSE; | |
1394 | } | |
1395 | ||
1396 | #if 0 | |
1397 | static gboolean write_trace_header(void *hook_data, void *call_data) | |
1398 | { | |
1399 | LttvTraceContext *tc = (LttvTraceContext *)call_data; | |
1400 | #if 0 //FIXME | |
1401 | LttSystemDescription *system = ltt_trace_system_description(tc->t); | |
1402 | ||
1403 | fprintf(a_file," Trace from %s in %s\n%s\n\n", | |
1404 | ltt_trace_system_description_node_name(system), | |
1405 | ltt_trace_system_description_domain_name(system), | |
1406 | ltt_trace_system_description_description(system)); | |
1407 | #endif //0 | |
1408 | return FALSE; | |
1409 | } | |
1410 | #endif | |
1411 | ||
1412 | ||
1413 | static int write_event_content(void *hook_data, void *call_data) | |
1414 | { | |
1415 | gboolean result; | |
1416 | ||
1417 | // LttvIAttribute *attributes = LTTV_IATTRIBUTE(lttv_global_attributes()); | |
1418 | ||
1419 | LttvTracefileContext *tfc = (LttvTracefileContext *)call_data; | |
1420 | ||
1421 | LttvTracefileState *tfs = (LttvTracefileState *)call_data; | |
1422 | ||
1423 | LttEvent *e; | |
1424 | ||
1425 | guint cpu = tfs->cpu; | |
1426 | LttvTraceState *ts = (LttvTraceState*)tfc->t_context; | |
1427 | LttvProcessState *process = ts->running_process[cpu]; | |
1428 | ||
1429 | e = ltt_tracefile_get_event(tfc->tf); | |
1430 | ||
1431 | lttv_event_to_string(e, a_string, TRUE, 1, tfs); | |
1432 | ||
1433 | // if(a_state) { | |
1434 | g_string_append_printf(a_string, " %s ", | |
1435 | g_quark_to_string(process->state->s)); | |
1436 | // } | |
1437 | ||
1438 | g_string_append_printf(a_string,"\n"); | |
1439 | ||
1440 | fputs(a_string->str, a_file); | |
1441 | return FALSE; | |
1442 | } | |
1443 | ||
1444 | static int field_get_value_int(struct LttEvent *e, struct marker_info *info, GQuark f) | |
1445 | { | |
1446 | struct marker_field *marker_field; | |
1447 | int found=0; | |
1448 | ||
1449 | for_each_marker_field(marker_field, info) { | |
1450 | if (marker_field->name == f) { | |
1451 | found = 1; | |
1452 | break; | |
1453 | } | |
1454 | } | |
1455 | g_assert(found); | |
1456 | return ltt_event_get_long_unsigned(e, marker_field); | |
1457 | } | |
1458 | ||
1459 | static char *field_get_value_string(struct LttEvent *e, struct marker_info *info, GQuark f) | |
1460 | { | |
1461 | struct marker_field *marker_field; | |
1462 | int found=0; | |
1463 | ||
1464 | for_each_marker_field(marker_field, info) { | |
1465 | if (marker_field->name == f) { | |
1466 | found = 1; | |
1467 | break; | |
1468 | } | |
1469 | } | |
1470 | g_assert(found); | |
1471 | return ltt_event_get_string(e, marker_field); | |
1472 | } | |
1473 | ||
1474 | void process_delayed_stack_action(struct process *pinfo, struct sstack_item *item) | |
1475 | { | |
1476 | //printf("processing delayed stack action on pid %d at ", pinfo->pid); | |
1477 | //if(((struct process_with_state *) item->data_val)->state.time_begin.tv_nsec == 987799696) | |
1478 | // printf("HERE!!!\n"); | |
1479 | //print_time(((struct process_with_state *) item->data_val)->state.time_begin); | |
1480 | //printf("\n"); | |
1481 | //printf("stack before:\n"); | |
1482 | //print_stack(pinfo->stack); | |
1483 | ||
1484 | if(item->data_type == SSTACK_TYPE_PUSH) { | |
1485 | struct process_with_state *pwstate = item->data_val; | |
1486 | //printf("pushing\n"); | |
1487 | old_process_push_llev_state(pinfo, &pwstate->state); | |
1488 | update_hlev_state(pinfo, pwstate->state.time_begin); | |
1489 | } | |
1490 | else if(item->data_type == SSTACK_TYPE_POP) { | |
1491 | struct process_with_state *pwstate = item->data_val; | |
1492 | //printf("popping\n"); | |
1493 | old_process_pop_llev_state(pinfo, &pwstate->state); | |
1494 | update_hlev_state(pinfo, pwstate->state.time_end); | |
1495 | } | |
1496 | else if(item->data_type == SSTACK_TYPE_EVENT) { | |
1497 | struct sstack_event *se = item->data_val; | |
1498 | if(se->event_type == HLEV_EVENT_TRY_WAKEUP) { | |
1499 | /* FIXME: should change hlev event from BLOCKED to INTERRUPTED CPU when receiving TRY_WAKEUP */ | |
1500 | struct try_wakeup_event *twe = se->private; | |
1501 | ||
1502 | /* FIXME: maybe do some more rigorous checking here */ | |
1503 | if(pinfo->hlev_state->bstate == HLEV_BLOCKED) { | |
1504 | struct hlev_state_info_blocked *hlev_blocked_private = pinfo->hlev_state->private; | |
1505 | ||
1506 | hlev_blocked_private->pid_exit = twe->pid; | |
1507 | hlev_blocked_private->time_woken = twe->time; | |
1508 | hlev_blocked_private->llev_state_exit = oldstyle_stack_to_garray(twe->waker->llev_state_stack, twe->waker->stack_current); | |
1509 | //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); | |
1510 | ||
1511 | /* | |
1512 | if(p->stack_current >= 0 && p->llev_state_stack[p->stack_current]->bstate == LLEV_PREEMPTED) { | |
1513 | old_process_pop_llev_state(pinfo, p->llev_state_stack[p->stack_current]); | |
1514 | update_hlev_state(pinfo | |
1515 | old_process_push_llev_state | |
1516 | }*/ | |
1517 | ||
1518 | } | |
1519 | } | |
1520 | } | |
1521 | ||
1522 | //printf("stack after:\n"); | |
1523 | //print_stack(pinfo->stack); | |
1524 | } | |
1525 | ||
1526 | static struct process *get_or_init_process_info(struct LttEvent *e, GQuark name, int pid, int *new) | |
1527 | { | |
1528 | gconstpointer val; | |
1529 | ||
1530 | val = g_hash_table_lookup(process_hash_table, &pid); | |
1531 | if(val == NULL) { | |
1532 | struct process *pinfo; | |
1533 | int i; | |
1534 | ||
1535 | /* Initialize new pinfo for newly discovered process */ | |
1536 | pinfo = g_malloc(sizeof(struct process)); | |
1537 | pinfo->pid = pid; | |
1538 | pinfo->parent = -1; /* unknown parent */ | |
1539 | pinfo->hlev_history = g_array_new(FALSE, FALSE, sizeof(struct process_state *)); | |
1540 | pinfo->stack = sstack_new(); | |
1541 | pinfo->stack_current=-1; | |
1542 | pinfo->stack->process_func = process_delayed_stack_action; | |
1543 | pinfo->stack->process_func_arg = pinfo; | |
1544 | for(i=0; i<PROCESS_STATE_STACK_SIZE; i++) { | |
1545 | pinfo->llev_state_stack[i] = g_malloc(sizeof(struct process_state)); | |
1546 | } | |
1547 | ||
1548 | pinfo->hlev_state = g_malloc(sizeof(struct process_state)); | |
1549 | pinfo->hlev_state->bstate = HLEV_UNKNOWN; | |
1550 | pinfo->hlev_state->time_begin = e->event_time; | |
1551 | pinfo->hlev_state->private = NULL; | |
1552 | ||
1553 | /* set the name */ | |
1554 | pinfo->name = name; | |
1555 | ||
1556 | g_hash_table_insert(process_hash_table, &pinfo->pid, pinfo); | |
1557 | if(new) | |
1558 | *new = 1; | |
1559 | return pinfo; | |
1560 | } | |
1561 | else { | |
1562 | if(new) | |
1563 | *new = 0; | |
1564 | return val; | |
1565 | ||
1566 | } | |
1567 | } | |
1568 | ||
1569 | static int differentiate_swappers(int pid, LttEvent *e) | |
1570 | { | |
1571 | if(pid == 0) | |
1572 | return pid+e->tracefile->cpu_num+2000000; | |
1573 | else | |
1574 | return pid; | |
1575 | } | |
1576 | ||
1577 | static int process_event(void *hook_data, void *call_data) | |
1578 | { | |
1579 | LttvTracefileContext *tfc = (LttvTracefileContext *)call_data; | |
1580 | LttvTracefileState *tfs = (LttvTracefileState *)call_data; | |
1581 | LttEvent *e; | |
1582 | struct marker_info *info; | |
1583 | ||
1584 | /* Extract data from event structures and state */ | |
1585 | guint cpu = tfs->cpu; | |
1586 | LttvTraceState *ts = (LttvTraceState*)tfc->t_context; | |
1587 | LttvProcessState *process = ts->running_process[cpu]; | |
1588 | LttTrace *trace = ts->parent.t; | |
1589 | struct process *pinfo; | |
1590 | ||
1591 | e = ltt_tracefile_get_event(tfs->parent.tf); | |
1592 | ||
1593 | info = marker_get_info_from_id(tfc->tf->mdata, e->event_id); | |
1594 | ||
1595 | //if(depanalysis_use_time && (ltt_time_compare(e->timestamp, arg_t1) == -1 || ltt_time_compare(e->timestamp, arg_t2) == 1)) { | |
1596 | // return; | |
1597 | //} | |
1598 | /* Set the pid for the dependency analysis at each event, until we are passed the range. */ | |
1599 | if(depanalysis_use_time == 3) { | |
1600 | if(ltt_time_compare(e->event_time, depanalysis_time2) <= 0) { | |
1601 | depanalysis_range_pid = process->pid; | |
1602 | } | |
1603 | else { | |
1604 | /* Should stop processing and print results */ | |
1605 | } | |
1606 | } | |
1607 | ||
1608 | /* Code to limit the event count */ | |
1609 | if(depanalysis_event_limit > 0) { | |
1610 | depanalysis_event_limit--; | |
1611 | } | |
1612 | else if(depanalysis_event_limit == 0) { | |
1613 | write_traceset_footer(hook_data, call_data); | |
1614 | printf("exit due to event limit reached\n"); | |
1615 | exit(0); | |
1616 | } | |
1617 | ||
1618 | /* write event like textDump for now, for debugging purposes */ | |
1619 | //write_event_content(hook_data, call_data); | |
1620 | ||
1621 | if(tfc->tf->name == LTT_CHANNEL_SYSCALL_STATE && info->name == LTT_EVENT_SYS_CALL_TABLE) { | |
1622 | GQuark q; | |
1623 | int *pint = g_malloc(sizeof(int)); | |
1624 | ||
1625 | *pint = field_get_value_int(e, info, LTT_FIELD_ID); | |
1626 | q = g_quark_from_string(field_get_value_string(e, info, LTT_FIELD_SYMBOL)); | |
1627 | g_hash_table_insert(syscall_table, pint, q); | |
1628 | } | |
1629 | else if(tfc->tf->name == LTT_CHANNEL_IRQ_STATE && info->name == LTT_EVENT_LIST_INTERRUPT) { | |
1630 | GQuark q; | |
1631 | int *pint = g_malloc(sizeof(int)); | |
1632 | ||
1633 | *pint = field_get_value_int(e, info, LTT_FIELD_IRQ_ID); | |
1634 | q = g_quark_from_string(field_get_value_string(e, info, LTT_FIELD_ACTION)); | |
1635 | g_hash_table_insert(irq_table, pint, q); | |
1636 | } | |
1637 | else if(tfc->tf->name == LTT_CHANNEL_SOFTIRQ_STATE && info->name == LTT_EVENT_SOFTIRQ_VEC) { | |
1638 | GQuark q; | |
1639 | int *pint = g_malloc(sizeof(int)); | |
1640 | ||
1641 | *pint = field_get_value_int(e, info, LTT_FIELD_ID); | |
1642 | q = g_quark_from_string(field_get_value_string(e, info, LTT_FIELD_SYMBOL)); | |
1643 | g_hash_table_insert(softirq_table, pint, q); | |
1644 | } | |
1645 | ||
1646 | ||
1647 | /* Only look at events after the statedump is finished. | |
1648 | * Before that, the pids in the LttvProcessState are not reliable | |
1649 | */ | |
1650 | if(statedump_finished == 0) { | |
1651 | if(tfc->tf->name == LTT_CHANNEL_GLOBAL_STATE && info->name == LTT_EVENT_STATEDUMP_END) | |
1652 | statedump_finished = 1; | |
1653 | else | |
1654 | return FALSE; | |
1655 | ||
1656 | } | |
1657 | ||
1658 | pinfo = get_or_init_process_info(e, process->name, differentiate_swappers(process->pid, e), NULL); | |
1659 | ||
1660 | /* the state machine | |
1661 | * Process the event in the context of each process | |
1662 | */ | |
1663 | ||
1664 | if(tfc->tf->name == LTT_CHANNEL_KERNEL && info->name == LTT_EVENT_IRQ_ENTRY) { | |
1665 | struct process *event_process_info = pinfo; | |
1666 | struct sstack_item *item; | |
1667 | ||
1668 | item = prepare_push_item(event_process_info, LLEV_IRQ, e->event_time); | |
1669 | ((struct llev_state_info_irq *) item_private(item))->irq = field_get_value_int(e, info, LTT_FIELD_IRQ_ID); | |
1670 | commit_item(event_process_info, item); | |
1671 | } | |
1672 | else if(tfc->tf->name == LTT_CHANNEL_KERNEL && info->name == LTT_EVENT_IRQ_EXIT) { | |
1673 | struct process *event_process_info = pinfo; | |
1674 | ||
1675 | prepare_pop_item_commit(event_process_info, LLEV_IRQ, e->event_time); | |
1676 | } | |
1677 | else if(tfc->tf->name == LTT_CHANNEL_KERNEL && info->name == LTT_EVENT_SCHED_SCHEDULE) { | |
1678 | int next_pid = field_get_value_int(e, info, LTT_FIELD_NEXT_PID); | |
1679 | int prev_pid = field_get_value_int(e, info, LTT_FIELD_PREV_PID); | |
1680 | if(next_pid != 0) { | |
1681 | struct process *event_process_info = get_or_init_process_info(e, process->name, differentiate_swappers(next_pid, e), NULL); | |
1682 | prepare_pop_item_commit(event_process_info, LLEV_PREEMPTED, e->event_time); | |
1683 | } | |
1684 | if(prev_pid != 0) { | |
1685 | struct sstack_item *item; | |
1686 | struct process *event_process_info = get_or_init_process_info(e, process->name, differentiate_swappers(prev_pid, e), NULL); | |
1687 | ||
1688 | item = prepare_push_item(event_process_info, LLEV_PREEMPTED, e->event_time); | |
1689 | ((struct llev_state_info_preempted *) item_private(item))->prev_state = field_get_value_int(e, info, LTT_FIELD_PREV_STATE); | |
1690 | commit_item(event_process_info, item); | |
1691 | } | |
1692 | } | |
1693 | else if(tfc->tf->name == LTT_CHANNEL_KERNEL && info->name == LTT_EVENT_TRAP_ENTRY) { | |
1694 | struct process *event_process_info = pinfo; | |
1695 | struct sstack_item *item; | |
1696 | ||
1697 | item = prepare_push_item(event_process_info, LLEV_TRAP, e->event_time); | |
1698 | commit_item(event_process_info, item); | |
1699 | } | |
1700 | else if(tfc->tf->name == LTT_CHANNEL_KERNEL && info->name == LTT_EVENT_TRAP_EXIT) { | |
1701 | struct process *event_process_info = pinfo; | |
1702 | ||
1703 | prepare_pop_item_commit(event_process_info, LLEV_TRAP, e->event_time); | |
1704 | } | |
1705 | else if(tfc->tf->name == LTT_CHANNEL_KERNEL && info->name == LTT_EVENT_SYSCALL_ENTRY) { | |
1706 | struct process *event_process_info = pinfo; | |
1707 | struct sstack_item *item; | |
1708 | ||
1709 | item = prepare_push_item(event_process_info, LLEV_SYSCALL, e->event_time); | |
1710 | ((struct llev_state_info_syscall *) item_private(item))->syscall_id = field_get_value_int(e, info, LTT_FIELD_SYSCALL_ID); | |
1711 | ((struct llev_state_info_syscall *) item_private(item))->substate = LLEV_SYSCALL__UNDEFINED; | |
1712 | commit_item(event_process_info, item); | |
1713 | } | |
1714 | else if(tfc->tf->name == LTT_CHANNEL_KERNEL && info->name == LTT_EVENT_SYSCALL_EXIT) { | |
1715 | struct process *event_process_info = pinfo; | |
1716 | ||
1717 | prepare_pop_item_commit(event_process_info, LLEV_SYSCALL, e->event_time); | |
1718 | } | |
1719 | else if(tfc->tf->name == LTT_CHANNEL_KERNEL && info->name == LTT_EVENT_SOFT_IRQ_ENTRY) { | |
1720 | struct process *event_process_info = pinfo; | |
1721 | struct sstack_item *item; | |
1722 | ||
1723 | item = prepare_push_item(event_process_info, LLEV_SOFTIRQ, e->event_time); | |
1724 | ((struct llev_state_info_softirq *) item_private(item))->softirq = field_get_value_int(e, info, LTT_FIELD_SOFT_IRQ_ID); | |
1725 | commit_item(event_process_info, item); | |
1726 | } | |
1727 | else if(tfc->tf->name == LTT_CHANNEL_KERNEL && info->name == LTT_EVENT_SOFT_IRQ_EXIT) { | |
1728 | struct process *event_process_info = pinfo; | |
1729 | ||
1730 | prepare_pop_item_commit(event_process_info, LLEV_SOFTIRQ, e->event_time); | |
1731 | } | |
1732 | else if(tfc->tf->name == LTT_CHANNEL_KERNEL && info->name == LTT_EVENT_PROCESS_FORK) { | |
1733 | int pid = differentiate_swappers(field_get_value_int(e, info, LTT_FIELD_CHILD_PID), e); | |
1734 | 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); | |
1735 | struct sstack_item *item; | |
1736 | ||
1737 | event_process_info->parent = process->pid; | |
1738 | //printf("At "); | |
1739 | //print_time(e->event_time); | |
1740 | //printf(", fork in process %d (%s), creating child %d\n", differentiate_swappers(process->pid, e), g_quark_to_string(process->name), pid); | |
1741 | ||
1742 | item = prepare_push_item(event_process_info, LLEV_RUNNING, e->event_time); | |
1743 | commit_item(event_process_info, item); | |
1744 | item = prepare_push_item(event_process_info, LLEV_SYSCALL, e->event_time); | |
1745 | /* FIXME: this sets fork() as syscall, it's pretty inelegant */ | |
1746 | ((struct llev_state_info_syscall *) item_private(item))->syscall_id = 57; | |
1747 | ((struct llev_state_info_syscall *) item_private(item))->substate = LLEV_SYSCALL__UNDEFINED; | |
1748 | commit_item(event_process_info, item); | |
1749 | ||
1750 | item = prepare_push_item(event_process_info, LLEV_PREEMPTED, e->event_time); | |
1751 | /* Consider fork as BLOCKED */ | |
1752 | ((struct llev_state_info_preempted *) item_private(item))->prev_state = 1; | |
1753 | commit_item(event_process_info, item); | |
1754 | ||
1755 | //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); | |
1756 | ||
1757 | } | |
1758 | else if(tfc->tf->name == LTT_CHANNEL_FS && info->name == LTT_EVENT_EXEC) { | |
1759 | struct process *event_process_info = pinfo; | |
1760 | ||
1761 | guint cpu = tfs->cpu; | |
1762 | LttvProcessState *process_state = ts->running_process[cpu]; | |
1763 | event_process_info->name = process_state->name; | |
1764 | } | |
1765 | else if(tfc->tf->name == LTT_CHANNEL_FS && info->name == LTT_EVENT_OPEN) { | |
1766 | struct process_state *pstate = process_find_state(pinfo, LLEV_SYSCALL); | |
1767 | struct llev_state_info_syscall *llev_syscall_private; | |
1768 | struct llev_state_info_syscall__open *llev_syscall_open_private; | |
1769 | ||
1770 | /* TODO: this is too easy */ | |
1771 | if(pstate == NULL) | |
1772 | goto next_iter; | |
1773 | ||
1774 | llev_syscall_private = (struct llev_state_info_syscall *)pstate->private; | |
1775 | ||
1776 | //printf("depanalysis: found an open with state %d in pid %d\n", pstate->bstate, process->pid); | |
1777 | if(pstate->bstate == LLEV_UNKNOWN) | |
1778 | goto next_iter; | |
1779 | ||
1780 | g_assert(pstate->bstate == LLEV_SYSCALL); | |
1781 | g_assert(llev_syscall_private->substate == LLEV_SYSCALL__UNDEFINED); | |
1782 | ||
1783 | llev_syscall_private->substate = LLEV_SYSCALL__OPEN; | |
1784 | //printf("setting substate LLEV_SYSCALL__OPEN on syscall_private %p\n", llev_syscall_private); | |
1785 | llev_syscall_private->private = g_malloc(sizeof(struct llev_state_info_syscall__open)); | |
1786 | llev_syscall_open_private = llev_syscall_private->private; | |
1787 | ||
1788 | llev_syscall_open_private->filename = g_quark_from_string(field_get_value_string(e, info, LTT_FIELD_FILENAME)); | |
1789 | ||
1790 | } | |
1791 | else if(tfc->tf->name == LTT_CHANNEL_FS && info->name == LTT_EVENT_READ) { | |
1792 | struct process_state *pstate = process_find_state(pinfo, LLEV_SYSCALL); | |
1793 | struct llev_state_info_syscall *llev_syscall_private; | |
1794 | struct llev_state_info_syscall__read *llev_syscall_read_private; | |
1795 | GQuark pfileq; | |
1796 | int fd; | |
1797 | ||
1798 | /* TODO: this is too easy */ | |
1799 | if(pstate == NULL) | |
1800 | goto next_iter; | |
1801 | ||
1802 | llev_syscall_private = (struct llev_state_info_syscall *)pstate->private; | |
1803 | ||
1804 | //printf("depanalysis: found an read with state %d in pid %d\n", pstate->bstate, process->pid); | |
1805 | if(pstate->bstate == LLEV_UNKNOWN) | |
1806 | goto next_iter; | |
1807 | ||
1808 | g_assert(pstate->bstate == LLEV_SYSCALL); | |
1809 | g_assert(llev_syscall_private->substate == LLEV_SYSCALL__UNDEFINED); | |
1810 | ||
1811 | llev_syscall_private->substate = LLEV_SYSCALL__READ; | |
1812 | //printf("setting substate LLEV_SYSCALL__READ on syscall_private %p\n", llev_syscall_private); | |
1813 | llev_syscall_private->private = g_malloc(sizeof(struct llev_state_info_syscall__read)); | |
1814 | llev_syscall_read_private = llev_syscall_private->private; | |
1815 | ||
1816 | fd = field_get_value_int(e, info, LTT_FIELD_FD); | |
1817 | pfileq = g_hash_table_lookup(process->fds, fd); | |
1818 | if(pfileq) { | |
1819 | llev_syscall_read_private->filename = pfileq; | |
1820 | } | |
1821 | else { | |
1822 | char *tmp; | |
1823 | asprintf(&tmp, "Unknown filename, fd %d", fd); | |
1824 | llev_syscall_read_private->filename = g_quark_from_string(tmp); | |
1825 | free(tmp); | |
1826 | } | |
1827 | } | |
1828 | else if(tfc->tf->name == LTT_CHANNEL_FS && info->name == LTT_EVENT_POLL_EVENT) { | |
1829 | struct process_state *pstate = process_find_state(pinfo, LLEV_SYSCALL); | |
1830 | struct llev_state_info_syscall *llev_syscall_private; | |
1831 | struct llev_state_info_syscall__poll *llev_syscall_poll_private; | |
1832 | GQuark pfileq; | |
1833 | int fd; | |
1834 | ||
1835 | /* TODO: this is too easy */ | |
1836 | if(pstate == NULL) | |
1837 | goto next_iter; | |
1838 | ||
1839 | llev_syscall_private = (struct llev_state_info_syscall *)pstate->private; | |
1840 | ||
1841 | //printf("depanalysis: found an poll with state %d in pid %d\n", pstate->bstate, process->pid); | |
1842 | if(pstate->bstate == LLEV_UNKNOWN) | |
1843 | goto next_iter; | |
1844 | ||
1845 | /* poll doesn't have a single event that gives the syscall args. instead, there can be an arbitrary | |
1846 | * number of fs_pollfd or fd_poll_event events | |
1847 | * We use the fd_poll_event event, which occurs for each fd that had activity causing a return of the poll() | |
1848 | * For now we only use the first. | |
1849 | * We should do something about this. FIXME | |
1850 | */ | |
1851 | if(llev_syscall_private->substate == LLEV_SYSCALL__POLL) | |
1852 | goto next_iter; | |
1853 | ||
1854 | g_assert(pstate->bstate == LLEV_SYSCALL); | |
1855 | g_assert(llev_syscall_private->substate == LLEV_SYSCALL__UNDEFINED); | |
1856 | ||
1857 | llev_syscall_private->substate = LLEV_SYSCALL__POLL; | |
1858 | //printf("setting substate LLEV_SYSCALL__POLL on syscall_private %p\n", llev_syscall_private); | |
1859 | llev_syscall_private->private = g_malloc(sizeof(struct llev_state_info_syscall__poll)); | |
1860 | llev_syscall_poll_private = llev_syscall_private->private; | |
1861 | ||
1862 | fd = field_get_value_int(e, info, LTT_FIELD_FD); | |
1863 | pfileq = g_hash_table_lookup(process->fds, fd); | |
1864 | if(pfileq) { | |
1865 | llev_syscall_poll_private->filename = pfileq; | |
1866 | } | |
1867 | else { | |
1868 | char *tmp; | |
1869 | asprintf(&tmp, "Unknown filename, fd %d", fd); | |
1870 | llev_syscall_poll_private->filename = g_quark_from_string(tmp); | |
1871 | free(tmp); | |
1872 | } | |
1873 | } | |
1874 | else if(tfc->tf->name == LTT_CHANNEL_KERNEL && info->name == LTT_EVENT_SCHED_TRY_WAKEUP) { | |
1875 | struct sstack_event *se = g_malloc(sizeof(struct sstack_event)); | |
1876 | struct try_wakeup_event *twe = g_malloc(sizeof(struct try_wakeup_event)); | |
1877 | struct sstack_item *item = sstack_item_new_event(); | |
1878 | int target = field_get_value_int(e, info, LTT_FIELD_PID); | |
1879 | struct process *target_pinfo; | |
1880 | int result; | |
1881 | ||
1882 | se->event_type = HLEV_EVENT_TRY_WAKEUP; | |
1883 | se->private = twe; | |
1884 | //printf("pushing try wake up event in context of %d\n", pinfo->pid); | |
1885 | ||
1886 | twe->pid = differentiate_swappers(process->pid, e); | |
1887 | twe->time = e->event_time; | |
1888 | twe->waker = pinfo; | |
1889 | ||
1890 | /* FIXME: the target could not yet have an entry in the hash table, we would then lose data */ | |
1891 | target_pinfo = g_hash_table_lookup(process_hash_table, &target); | |
1892 | if(!target_pinfo) | |
1893 | goto next_iter; | |
1894 | ||
1895 | item->data_val = se; | |
b1d18041 | 1896 | item->delete_data_val = (void (*)(void *))delete_data_val; |
8c108c1c PMF |
1897 | |
1898 | sstack_add_item(target_pinfo->stack, item); | |
1899 | ||
1900 | /* Now pop the blocked schedule out of the target */ | |
1901 | result = try_pop_blocked_llev_preempted(target_pinfo, e->event_time); | |
1902 | ||
1903 | if(result) { | |
1904 | struct sstack_item *item; | |
1905 | struct process *event_process_info = target_pinfo; | |
1906 | ||
1907 | item = prepare_push_item(event_process_info, LLEV_PREEMPTED, e->event_time); | |
1908 | ((struct llev_state_info_preempted *) item_private(item))->prev_state = -1; /* special value meaning post-block sched out */ | |
1909 | commit_item(event_process_info, item); | |
1910 | } | |
1911 | ||
1912 | } | |
1913 | ||
1914 | next_iter: | |
1915 | skip_state_machine: | |
1916 | return FALSE; | |
1917 | } | |
1918 | ||
1919 | void print_sstack_private(struct sstack_item *item) | |
1920 | { | |
1921 | struct process_with_state *pwstate = item->data_val; | |
1922 | ||
1923 | if(pwstate && item->data_type == SSTACK_TYPE_PUSH) | |
1924 | printf("\tstate: %s", llev_state_infos[pwstate->state.bstate].name); | |
1925 | ||
1926 | printf(" ("); | |
1927 | print_time(pwstate->state.time_begin); | |
1928 | printf("-"); | |
1929 | print_time(pwstate->state.time_end); | |
1930 | printf("\n"); | |
1931 | ||
1932 | } | |
1933 | ||
1934 | static LttTime ltt_time_from_string(const char *str) | |
1935 | { | |
1936 | LttTime retval; | |
1937 | ||
1938 | char *decdot = strchr(str, '.'); | |
1939 | ||
1940 | if(decdot) { | |
1941 | *decdot = '\0'; | |
1942 | retval.tv_nsec = atol(decdot+1); | |
1943 | } | |
1944 | else { | |
1945 | retval.tv_nsec = 0; | |
1946 | } | |
1947 | ||
1948 | retval.tv_sec = atol(str); | |
1949 | ||
1950 | return retval; | |
1951 | } | |
1952 | ||
1953 | static void arg_t1(void *hook_data) | |
1954 | { | |
1955 | printf("arg_t1\n"); | |
1956 | depanalysis_use_time |= 1; | |
1957 | depanalysis_time1 = ltt_time_from_string(arg_t1_str); | |
1958 | } | |
1959 | ||
1960 | static void arg_t2(void *hook_data) | |
1961 | { | |
1962 | depanalysis_use_time |= 2; | |
1963 | depanalysis_time2 = ltt_time_from_string(arg_t2_str); | |
1964 | } | |
1965 | ||
1966 | static void arg_pid(void *hook_data) | |
1967 | { | |
1968 | } | |
1969 | ||
1970 | static void arg_limit(void *hook_data) | |
1971 | { | |
1972 | } | |
1973 | ||
55cd92ee MD |
1974 | static void arg_sum(void *hook_data) |
1975 | { | |
1976 | } | |
1977 | ||
8c108c1c PMF |
1978 | static void init() |
1979 | { | |
1980 | gboolean result; | |
1981 | ||
1982 | print_sstack_item_data = print_sstack_private; | |
1983 | ||
1984 | LttvAttributeValue value; | |
1985 | ||
1986 | LttvIAttribute *attributes = LTTV_IATTRIBUTE(lttv_global_attributes()); | |
1987 | ||
1988 | a_file = stdout; | |
1989 | ||
1990 | lttv_option_add("dep-time-start", 0, "dependency analysis time of analysis start", "time", | |
1991 | LTTV_OPT_STRING, &arg_t1_str, arg_t1, NULL); | |
1992 | lttv_option_add("dep-time-end", 0, "dependency analysis time of analysis end", "time", | |
1993 | LTTV_OPT_STRING, &arg_t2_str, arg_t2, NULL); | |
1994 | lttv_option_add("dep-pid", 0, "dependency analysis pid", "pid", | |
1995 | LTTV_OPT_INT, &depanalysis_range_pid_searching, arg_pid, NULL); | |
1996 | lttv_option_add("limit-events", 0, "dependency limit event count", "count", | |
1997 | LTTV_OPT_INT, &depanalysis_event_limit, arg_limit, NULL); | |
55cd92ee MD |
1998 | lttv_option_add("print-summary", 0, "print simple summary", "sum", |
1999 | LTTV_OPT_INT, &a_print_simple_summary, arg_sum, NULL); | |
8c108c1c PMF |
2000 | |
2001 | process_hash_table = g_hash_table_new(g_int_hash, g_int_equal); | |
2002 | syscall_table = g_hash_table_new(g_int_hash, g_int_equal); | |
2003 | irq_table = g_hash_table_new(g_int_hash, g_int_equal); | |
2004 | softirq_table = g_hash_table_new(g_int_hash, g_int_equal); | |
2005 | ||
2006 | a_string = g_string_new(""); | |
2007 | ||
2008 | result = lttv_iattribute_find_by_path(attributes, "hooks/event", | |
2009 | LTTV_POINTER, &value); | |
2010 | g_assert(result); | |
2011 | event_hook = *(value.v_pointer); | |
2012 | g_assert(event_hook); | |
2013 | lttv_hooks_add(event_hook, process_event, NULL, LTTV_PRIO_DEFAULT); | |
2014 | ||
2015 | // result = lttv_iattribute_find_by_path(attributes, "hooks/trace/before", | |
2016 | // LTTV_POINTER, &value); | |
2017 | // g_assert(result); | |
2018 | // before_trace = *(value.v_pointer); | |
2019 | // g_assert(before_trace); | |
2020 | // lttv_hooks_add(before_trace, write_trace_header, NULL, LTTV_PRIO_DEFAULT); | |
2021 | // | |
2022 | result = lttv_iattribute_find_by_path(attributes, "hooks/traceset/before", | |
2023 | LTTV_POINTER, &value); | |
2024 | g_assert(result); | |
2025 | before_traceset = *(value.v_pointer); | |
2026 | g_assert(before_traceset); | |
2027 | lttv_hooks_add(before_traceset, write_traceset_header, NULL, | |
2028 | LTTV_PRIO_DEFAULT); | |
2029 | ||
2030 | result = lttv_iattribute_find_by_path(attributes, "hooks/traceset/after", | |
2031 | LTTV_POINTER, &value); | |
2032 | g_assert(result); | |
2033 | after_traceset = *(value.v_pointer); | |
2034 | g_assert(after_traceset); | |
2035 | lttv_hooks_add(after_traceset, write_traceset_footer, NULL, | |
2036 | LTTV_PRIO_DEFAULT); | |
2037 | } | |
2038 | ||
2039 | static void destroy() | |
2040 | { | |
2041 | lttv_option_remove("dep-time-start"); | |
2042 | lttv_option_remove("dep-time-end"); | |
2043 | lttv_option_remove("dep-pid"); | |
2044 | lttv_option_remove("limit-events"); | |
55cd92ee | 2045 | lttv_option_remove("print-summary"); |
8c108c1c PMF |
2046 | |
2047 | g_hash_table_destroy(process_hash_table); | |
2048 | g_hash_table_destroy(syscall_table); | |
2049 | g_hash_table_destroy(irq_table); | |
2050 | g_hash_table_destroy(softirq_table); | |
2051 | ||
2052 | g_string_free(a_string, TRUE); | |
2053 | ||
2054 | lttv_hooks_remove_data(event_hook, write_event_content, NULL); | |
2055 | // lttv_hooks_remove_data(before_trace, write_trace_header, NULL); | |
2056 | lttv_hooks_remove_data(before_traceset, write_traceset_header, NULL); | |
2057 | lttv_hooks_remove_data(after_traceset, write_traceset_footer, NULL); | |
2058 | } | |
2059 | ||
2060 | LTTV_MODULE("depanalysis", "Dependency analysis test", \ | |
2061 | "Produce a dependency analysis of a trace", \ | |
2062 | init, destroy, "stats", "batchAnalysis", "option", "print") | |
2063 |