1 /* This file is part of the Linux Trace Toolkit viewer
2 * Copyright (C) 2003-2004 Michel Dagenais
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License Version 2 as
6 * published by the Free Software Foundation;
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
24 #include <lttv/module.h>
25 #include <lttv/stats.h>
26 #include <lttv/lttv.h>
27 #include <lttv/attribute.h>
28 #include <ltt/facility.h>
29 #include <ltt/trace.h>
30 #include <ltt/event.h>
34 #define MAX_64_HEX_STRING_LEN 19
37 LTTV_STATS_PROCESS_UNKNOWN
,
40 LTTV_STATS_MODE_TYPES
,
44 LTTV_STATS_EVENT_TYPES
,
46 LTTV_STATS_CUMULATIVE_CPU_TIME
,
47 LTTV_STATS_ELAPSED_TIME
,
49 LTTV_STATS_EVENTS_COUNT
,
52 LTTV_STATS_TRACEFILES
,
54 LTTV_STATS_BEFORE_HOOKS
,
55 LTTV_STATS_AFTER_HOOKS
;
58 find_event_tree(LttvTracefileStats
*tfcs
, GQuark pid_time
, guint cpu
,
60 GQuark mode
, GQuark sub_mode
, LttvAttribute
**events_tree
,
61 LttvAttribute
**event_types_tree
);
64 static void lttv_stats_init(LttvTracesetStats
*self
)
66 guint i
, j
, nb_trace
, nb_tracefile
;
72 LttvTracefileContext
*tfc
;
74 LttvTracefileContext
**tfs
;
75 LttvTracefileStats
*tfcs
;
77 LttTime timestamp
= {0,0};
85 LttvTraceset
*ts
= self
->parent
.parent
.ts
;
87 self
->stats
= lttv_attribute_find_subdir(
88 lttv_traceset_attribute(self
->parent
.parent
.ts
),
90 lttv_attribute_find(lttv_traceset_attribute(self
->parent
.parent
.ts
),
95 if(*(v
.v_uint
) == 1) {
96 g_assert(lttv_attribute_get_number(self
->stats
) == 0);
99 nb_trace
= lttv_traceset_number(ts
);
101 for(i
= 0 ; i
< nb_trace
; i
++) {
102 tc
= self
->parent
.parent
.traces
[i
];
103 tcs
= LTTV_TRACE_STATS(tc
);
105 tcs
->stats
= lttv_attribute_find_subdir(tcs
->parent
.parent
.t_a
,LTTV_STATS
);
106 tracefiles_stats
= lttv_attribute_find_subdir(tcs
->parent
.parent
.t_a
,
107 LTTV_STATS_TRACEFILES
);
108 lttv_attribute_find(tcs
->parent
.parent
.t_a
, LTTV_STATS_USE_COUNT
,
112 if(*(v
.v_uint
) == 1) {
113 g_assert(lttv_attribute_get_number(tcs
->stats
) == 0);
116 nb_tracefile
= tc
->tracefiles
->len
;
118 for(j
= 0 ; j
< nb_tracefile
; j
++) {
119 tfs
= &g_array_index(tc
->tracefiles
,
120 LttvTracefileContext
*, j
);
121 tfcs
= LTTV_TRACEFILE_STATS(*tfs
);
122 tfcs
->stats
= lttv_attribute_find_subdir(tracefiles_stats
,
123 ltt_tracefile_long_name(tfcs
->parent
.parent
.tf
));
124 guint cpu
= tfcs
->parent
.cpu
;
125 find_event_tree(tfcs
, LTTV_STATS_PROCESS_UNKNOWN
,
128 LTTV_STATE_MODE_UNKNOWN
,
129 LTTV_STATE_SUBMODE_UNKNOWN
, &tfcs
->current_events_tree
,
130 &tfcs
->current_event_types_tree
);
136 static void lttv_stats_fini(LttvTracesetStats
*self
)
138 guint i
, j
, nb_trace
, nb_tracefile
;
142 LttvTraceContext
*tc
;
146 LttvTracefileContext
*tfc
;
148 LttvTracefileStats
*tfcs
;
150 LttTime timestamp
= {0,0};
152 LttvAttributeValue v
;
154 LttvAttribute
*tracefiles_stats
;
156 lttv_attribute_find(self
->parent
.parent
.ts_a
, LTTV_STATS_USE_COUNT
,
160 if(*(v
.v_uint
) == 0) {
161 lttv_attribute_remove_by_name(self
->parent
.parent
.ts_a
, LTTV_STATS
);
165 ts
= self
->parent
.parent
.ts
;
166 nb_trace
= lttv_traceset_number(ts
);
168 for(i
= 0 ; i
< nb_trace
; i
++) {
169 tcs
= (LttvTraceStats
*)(tc
= (LTTV_TRACESET_CONTEXT(self
)->traces
[i
]));
171 lttv_attribute_find(tcs
->parent
.parent
.t_a
, LTTV_STATS_USE_COUNT
,
175 if(*(v
.v_uint
) == 0) {
176 lttv_attribute_remove_by_name(tcs
->parent
.parent
.t_a
,LTTV_STATS
);
177 tracefiles_stats
= lttv_attribute_find_subdir(tcs
->parent
.parent
.t_a
,
178 LTTV_STATS_TRACEFILES
);
179 lttv_attribute_remove_by_name(tcs
->parent
.parent
.t_a
,
180 LTTV_STATS_TRACEFILES
);
184 nb_tracefile
= tc
->tracefiles
->len
;
186 for(j
= 0 ; j
< nb_tracefile
; j
++) {
187 tfc
= g_array_index(tc
->tracefiles
,
188 LttvTracefileContext
*, j
);
189 tfcs
= (LttvTracefileStats
*)tfc
;
191 tfcs
->current_events_tree
= NULL
;
192 tfcs
->current_event_types_tree
= NULL
;
198 void lttv_stats_reset(LttvTracesetStats
*self
)
200 lttv_stats_fini(self
);
201 lttv_stats_init(self
);
207 init(LttvTracesetStats
*self
, LttvTraceset
*ts
)
209 LTTV_TRACESET_CONTEXT_CLASS(g_type_class_peek(LTTV_TRACESET_STATE_TYPE
))->
210 init((LttvTracesetContext
*)self
, ts
);
212 lttv_stats_init(self
);
217 fini(LttvTracesetStats
*self
)
219 lttv_stats_fini(self
);
221 LTTV_TRACESET_CONTEXT_CLASS(g_type_class_peek(LTTV_TRACESET_STATE_TYPE
))->
222 fini((LttvTracesetContext
*)self
);
226 static LttvTracesetContext
*
227 new_traceset_context(LttvTracesetContext
*self
)
229 return LTTV_TRACESET_CONTEXT(g_object_new(LTTV_TRACESET_STATS_TYPE
, NULL
));
233 static LttvTraceContext
*
234 new_trace_context(LttvTracesetContext
*self
)
236 return LTTV_TRACE_CONTEXT(g_object_new(LTTV_TRACE_STATS_TYPE
, NULL
));
240 static LttvTracefileContext
*
241 new_tracefile_context(LttvTracesetContext
*self
)
243 return LTTV_TRACEFILE_CONTEXT(g_object_new(LTTV_TRACEFILE_STATS_TYPE
, NULL
));
248 traceset_stats_instance_init (GTypeInstance
*instance
, gpointer g_class
)
254 traceset_stats_finalize (LttvTracesetStats
*self
)
256 G_OBJECT_CLASS(g_type_class_peek(LTTV_TRACESET_STATE_TYPE
))->
257 finalize(G_OBJECT(self
));
262 traceset_stats_class_init (LttvTracesetContextClass
*klass
)
264 GObjectClass
*gobject_class
= G_OBJECT_CLASS(klass
);
266 gobject_class
->finalize
= (void (*)(GObject
*self
)) traceset_stats_finalize
;
267 klass
->init
= (void (*)(LttvTracesetContext
*self
, LttvTraceset
*ts
))init
;
268 klass
->fini
= (void (*)(LttvTracesetContext
*self
))fini
;
269 klass
->new_traceset_context
= new_traceset_context
;
270 klass
->new_trace_context
= new_trace_context
;
271 klass
->new_tracefile_context
= new_tracefile_context
;
276 lttv_traceset_stats_get_type(void)
278 static GType type
= 0;
280 static const GTypeInfo info
= {
281 sizeof (LttvTracesetStatsClass
),
282 NULL
, /* base_init */
283 NULL
, /* base_finalize */
284 (GClassInitFunc
) traceset_stats_class_init
, /* class_init */
285 NULL
, /* class_finalize */
286 NULL
, /* class_data */
287 sizeof (LttvTracesetStats
),
289 (GInstanceInitFunc
) traceset_stats_instance_init
, /* instance_init */
290 NULL
/* Value handling */
293 type
= g_type_register_static (LTTV_TRACESET_STATE_TYPE
,
294 "LttvTracesetStatsType",
302 trace_stats_instance_init (GTypeInstance
*instance
, gpointer g_class
)
308 trace_stats_finalize (LttvTraceStats
*self
)
310 G_OBJECT_CLASS(g_type_class_peek(LTTV_TRACE_STATE_TYPE
))->
311 finalize(G_OBJECT(self
));
316 trace_stats_class_init (LttvTraceContextClass
*klass
)
318 GObjectClass
*gobject_class
= G_OBJECT_CLASS(klass
);
320 gobject_class
->finalize
= (void (*)(GObject
*self
)) trace_stats_finalize
;
325 lttv_trace_stats_get_type(void)
327 static GType type
= 0;
329 static const GTypeInfo info
= {
330 sizeof (LttvTraceStatsClass
),
331 NULL
, /* base_init */
332 NULL
, /* base_finalize */
333 (GClassInitFunc
) trace_stats_class_init
, /* class_init */
334 NULL
, /* class_finalize */
335 NULL
, /* class_data */
336 sizeof (LttvTraceStats
),
338 (GInstanceInitFunc
) trace_stats_instance_init
, /* instance_init */
339 NULL
/* Value handling */
342 type
= g_type_register_static (LTTV_TRACE_STATE_TYPE
,
343 "LttvTraceStatsType", &info
, 0);
350 tracefile_stats_instance_init (GTypeInstance
*instance
, gpointer g_class
)
356 tracefile_stats_finalize (LttvTracefileStats
*self
)
358 G_OBJECT_CLASS(g_type_class_peek(LTTV_TRACEFILE_STATE_TYPE
))->
359 finalize(G_OBJECT(self
));
364 tracefile_stats_class_init (LttvTracefileStatsClass
*klass
)
366 GObjectClass
*gobject_class
= G_OBJECT_CLASS(klass
);
368 gobject_class
->finalize
= (void (*)(GObject
*self
)) tracefile_stats_finalize
;
373 lttv_tracefile_stats_get_type(void)
375 static GType type
= 0;
377 static const GTypeInfo info
= {
378 sizeof (LttvTracefileStatsClass
),
379 NULL
, /* base_init */
380 NULL
, /* base_finalize */
381 (GClassInitFunc
) tracefile_stats_class_init
, /* class_init */
382 NULL
, /* class_finalize */
383 NULL
, /* class_data */
384 sizeof (LttvTracefileStats
),
386 (GInstanceInitFunc
) tracefile_stats_instance_init
, /* instance_init */
387 NULL
/* Value handling */
390 type
= g_type_register_static (LTTV_TRACEFILE_STATE_TYPE
,
391 "LttvTracefileStatsType", &info
, 0);
398 find_event_tree(LttvTracefileStats
*tfcs
,
404 LttvAttribute
**events_tree
,
405 LttvAttribute
**event_types_tree
)
407 LttvAttribute
*a
, *prev_a
;
408 gchar fstring
[MAX_64_HEX_STRING_LEN
];
411 ret
= snprintf(fstring
, MAX_64_HEX_STRING_LEN
-1,
412 "0x%llX", function
) > 0;
414 fstring
[MAX_64_HEX_STRING_LEN
-1] = '\0';
416 LttvTraceStats
*tcs
= (LttvTraceStats
*)tfcs
->parent
.parent
.t_context
;
417 a
= lttv_attribute_find_subdir(tcs
->stats
, LTTV_STATS_PROCESSES
);
418 a
= lttv_attribute_find_subdir(a
, pid_time
);
419 a
= lttv_attribute_find_subdir(a
, LTTV_STATS_CPU
);
420 a
= lttv_attribute_find_subdir_unnamed(a
, cpu
);
421 a
= lttv_attribute_find_subdir(a
, LTTV_STATS_FUNCTIONS
);
422 a
= lttv_attribute_find_subdir(a
, g_quark_from_string(fstring
));
423 a
= lttv_attribute_find_subdir(a
, LTTV_STATS_MODE_TYPES
);
424 a
= lttv_attribute_find_subdir(a
, mode
);
425 a
= lttv_attribute_find_subdir(a
, LTTV_STATS_SUBMODES
);
426 a
= lttv_attribute_find_subdir(a
, sub_mode
);
428 a
= lttv_attribute_find_subdir(a
, LTTV_STATS_EVENT_TYPES
);
429 *event_types_tree
= a
;
433 static void update_event_tree(LttvTracefileStats
*tfcs
)
435 LttvTraceState
*ts
= (LttvTraceState
*)tfcs
->parent
.parent
.t_context
;
436 guint cpu
= tfcs
->parent
.cpu
;
437 LttvProcessState
*process
= ts
->running_process
[cpu
];
438 LttvExecutionState
*es
= process
->state
;
440 find_event_tree(tfcs
, process
->pid_time
,
442 process
->current_function
,
443 es
->t
, es
->n
, &(tfcs
->current_events_tree
),
444 &(tfcs
->current_event_types_tree
));
448 static void mode_change(LttvTracefileStats
*tfcs
)
450 LttvTraceState
*ts
= (LttvTraceState
*)tfcs
->parent
.parent
.t_context
;
451 guint cpu
= tfcs
->parent
.cpu
;
452 LttvProcessState
*process
= ts
->running_process
[cpu
];
453 LttvAttributeValue cpu_time
, cum_cpu_time
;
457 delta
= ltt_time_sub(tfcs
->parent
.parent
.timestamp
,
458 process
->state
->change
);
460 lttv_attribute_find(tfcs
->current_events_tree
, LTTV_STATS_CPU_TIME
,
461 LTTV_TIME
, &cpu_time
);
462 *(cpu_time
.v_time
) = ltt_time_add(*(cpu_time
.v_time
), delta
);
464 process
->state
->cum_cpu_time
= ltt_time_add(process
->state
->cum_cpu_time
,
468 /* Note : every mode_end must come with a cumulative cpu time update in the
470 static void mode_end(LttvTracefileStats
*tfcs
)
472 LttvTraceState
*ts
= (LttvTraceState
*)tfcs
->parent
.parent
.t_context
;
473 guint cpu
= tfcs
->parent
.cpu
;
474 LttvProcessState
*process
= ts
->running_process
[cpu
];
475 LttvAttributeValue elapsed_time
, cpu_time
, cum_cpu_time
;
479 lttv_attribute_find(tfcs
->current_events_tree
, LTTV_STATS_ELAPSED_TIME
,
480 LTTV_TIME
, &elapsed_time
);
481 delta
= ltt_time_sub(tfcs
->parent
.parent
.timestamp
,
482 process
->state
->entry
);
483 *(elapsed_time
.v_time
) = ltt_time_add(*(elapsed_time
.v_time
), delta
);
485 lttv_attribute_find(tfcs
->current_events_tree
, LTTV_STATS_CPU_TIME
,
486 LTTV_TIME
, &cpu_time
);
487 delta
= ltt_time_sub(tfcs
->parent
.parent
.timestamp
,
488 process
->state
->change
);
489 *(cpu_time
.v_time
) = ltt_time_add(*(cpu_time
.v_time
), delta
);
490 process
->state
->cum_cpu_time
= ltt_time_add(process
->state
->cum_cpu_time
,
493 lttv_attribute_find(tfcs
->current_events_tree
, LTTV_STATS_CUMULATIVE_CPU_TIME
,
494 LTTV_TIME
, &cum_cpu_time
);
495 *(cum_cpu_time
.v_time
) = ltt_time_add(*(cum_cpu_time
.v_time
),
496 process
->state
->cum_cpu_time
);
500 static void after_mode_end(LttvTracefileStats
*tfcs
)
502 LttvTraceState
*ts
= (LttvTraceState
*)tfcs
->parent
.parent
.t_context
;
503 guint cpu
= tfcs
->parent
.cpu
;
504 LttvProcessState
*process
= ts
->running_process
[cpu
];
505 LttvAttributeValue cum_cpu_time
;
507 LttTime nested_delta
;
509 nested_delta
= process
->state
->cum_cpu_time
;
510 process
->state
->cum_cpu_time
= ltt_time_zero
; /* For after traceset hook */
512 update_event_tree(tfcs
);
514 process
->state
->cum_cpu_time
= ltt_time_add(process
->state
->cum_cpu_time
,
518 static gboolean
before_syscall_entry(void *hook_data
, void *call_data
)
520 mode_change((LttvTracefileStats
*)call_data
);
525 static gboolean
after_syscall_entry(void *hook_data
, void *call_data
)
527 update_event_tree((LttvTracefileStats
*)call_data
);
532 gboolean
before_syscall_exit(void *hook_data
, void *call_data
)
534 mode_end((LttvTracefileStats
*)call_data
);
539 static gboolean
after_syscall_exit(void *hook_data
, void *call_data
)
541 after_mode_end((LttvTracefileStats
*)call_data
);
546 gboolean
before_trap_entry(void *hook_data
, void *call_data
)
548 mode_change((LttvTracefileStats
*)call_data
);
553 static gboolean
after_trap_entry(void *hook_data
, void *call_data
)
555 update_event_tree((LttvTracefileStats
*)call_data
);
560 gboolean
before_trap_exit(void *hook_data
, void *call_data
)
562 mode_end((LttvTracefileStats
*)call_data
);
567 gboolean
after_trap_exit(void *hook_data
, void *call_data
)
569 after_mode_end((LttvTracefileStats
*)call_data
);
574 gboolean
before_irq_entry(void *hook_data
, void *call_data
)
576 mode_change((LttvTracefileStats
*)call_data
);
580 gboolean
after_irq_entry(void *hook_data
, void *call_data
)
582 update_event_tree((LttvTracefileStats
*)call_data
);
587 gboolean
before_irq_exit(void *hook_data
, void *call_data
)
589 mode_end((LttvTracefileStats
*)call_data
);
594 gboolean
after_irq_exit(void *hook_data
, void *call_data
)
596 after_mode_end((LttvTracefileStats
*)call_data
);
601 gboolean
before_soft_irq_entry(void *hook_data
, void *call_data
)
603 mode_change((LttvTracefileStats
*)call_data
);
607 gboolean
after_soft_irq_entry(void *hook_data
, void *call_data
)
609 update_event_tree((LttvTracefileStats
*)call_data
);
614 gboolean
before_soft_irq_exit(void *hook_data
, void *call_data
)
616 mode_end((LttvTracefileStats
*)call_data
);
621 gboolean
after_soft_irq_exit(void *hook_data
, void *call_data
)
623 after_mode_end((LttvTracefileStats
*)call_data
);
627 gboolean
before_function_entry(void *hook_data
, void *call_data
)
629 mode_change((LttvTracefileStats
*)call_data
);
633 gboolean
after_function_entry(void *hook_data
, void *call_data
)
635 update_event_tree((LttvTracefileStats
*)call_data
);
639 gboolean
before_function_exit(void *hook_data
, void *call_data
)
641 mode_end((LttvTracefileStats
*)call_data
);
645 gboolean
after_function_exit(void *hook_data
, void *call_data
)
647 after_mode_end((LttvTracefileStats
*)call_data
);
652 gboolean
before_schedchange(void *hook_data
, void *call_data
)
654 LttvTracefileStats
*tfcs
= (LttvTracefileStats
*)call_data
;
656 LttvTraceState
*ts
= (LttvTraceState
*)tfcs
->parent
.parent
.t_context
;
658 LttEvent
*e
= ltt_tracefile_get_event(tfcs
->parent
.parent
.tf
);
660 LttvTraceHookByFacility
*thf
= (LttvTraceHookByFacility
*)hook_data
;
662 guint pid_in
, pid_out
;
666 pid_out
= ltt_event_get_unsigned(e
, thf
->f1
);
667 pid_in
= ltt_event_get_unsigned(e
, thf
->f2
);
668 state_out
= ltt_event_get_int(e
, thf
->f3
);
670 /* compute the time for the process to schedule out */
677 gboolean
after_schedchange(void *hook_data
, void *call_data
)
679 LttvTracefileStats
*tfcs
= (LttvTracefileStats
*)call_data
;
681 LttvTraceState
*ts
= (LttvTraceState
*)tfcs
->parent
.parent
.t_context
;
683 LttEvent
*e
= ltt_tracefile_get_event(tfcs
->parent
.parent
.tf
);
685 LttvTraceHookByFacility
*thf
= (LttvTraceHookByFacility
*)hook_data
;
687 guint pid_in
, pid_out
;
691 LttvProcessState
*process
;
693 pid_out
= ltt_event_get_unsigned(e
, thf
->f1
);
694 pid_in
= ltt_event_get_unsigned(e
, thf
->f2
);
695 state_out
= ltt_event_get_int(e
, thf
->f3
);
697 /* get the information for the process scheduled in */
698 guint cpu
= tfcs
->parent
.cpu
;
699 process
= ts
->running_process
[cpu
];
701 find_event_tree(tfcs
, process
->pid_time
,
703 process
->current_function
,
704 process
->state
->t
, process
->state
->n
, &(tfcs
->current_events_tree
),
705 &(tfcs
->current_event_types_tree
));
707 /* compute the time waiting for the process to schedule in */
713 gboolean
process_fork(void *hook_data
, void *call_data
)
715 /* nothing to do for now */
720 gboolean
process_exit(void *hook_data
, void *call_data
)
722 /* We should probably exit all modes here or we could do that at
727 gboolean
process_free(void *hook_data
, void *call_data
)
732 gboolean
every_event(void *hook_data
, void *call_data
)
734 LttvTracefileStats
*tfcs
= (LttvTracefileStats
*)call_data
;
736 LttEvent
*e
= ltt_tracefile_get_event(tfcs
->parent
.parent
.tf
);
738 LttvAttributeValue v
;
740 /* The current branch corresponds to the tracefile/process/interrupt state.
741 Statistics are added within it, to count the number of events of this
742 type occuring in this context. A quark has been pre-allocated for each
743 event type and is used as name. */
745 lttv_attribute_find(tfcs
->current_event_types_tree
,
746 ltt_eventtype_name(ltt_event_eventtype(e
)),
752 static void lttv_stats_cleanup_process_state(gpointer key
, gpointer value
,
755 LttvTraceStats
*tcs
= (LttvTraceStats
*)user_data
;
756 LttvTraceState
*ts
= (LttvTraceState
*)user_data
;
757 LttvTracesetContext
*tsc
= ts
->parent
.ts_context
;
758 LttvProcessState
*process
= (LttvProcessState
*)value
;
760 LttvTracefileStats
**tfs
= (LttvTracefileStats
**)
761 &g_array_index(ts
->parent
.tracefiles
, LttvTracefileContext
*,
763 int cleanup_empty
= 0;
764 LttTime nested_delta
= ltt_time_zero
;
765 /* FIXME : ok, this is a hack. The time is infinite here :( */
766 LttTime save_time
= (*tfs
)->parent
.parent
.timestamp
;
768 ltt_trace_time_span_get(ts
->parent
.t
, &start
, &end
);
769 (*tfs
)->parent
.parent
.timestamp
= end
;
772 if(ltt_time_compare(process
->state
->cum_cpu_time
, ltt_time_zero
) != 0) {
773 find_event_tree(*tfs
, process
->pid_time
,
775 process
->current_function
,
776 process
->state
->t
, process
->state
->n
, &((*tfs
)->current_events_tree
),
777 &((*tfs
)->current_event_types_tree
));
779 nested_delta
= process
->state
->cum_cpu_time
;
781 cleanup_empty
= lttv_state_pop_state_cleanup(process
,
782 (LttvTracefileState
*)*tfs
);
783 process
->state
->cum_cpu_time
= ltt_time_add(process
->state
->cum_cpu_time
,
786 } while(cleanup_empty
!= 1);
788 (*tfs
)->parent
.parent
.timestamp
= save_time
;
791 /* For each process in the state, for each of their stacked states,
792 * perform sum of needed values. */
793 static void lttv_stats_cleanup_state(LttvTraceStats
*tcs
)
795 LttvTraceState
*ts
= (LttvTraceState
*)tcs
;
797 /* Does not work correctly FIXME. */
798 g_hash_table_foreach(ts
->processes
, lttv_stats_cleanup_process_state
,
803 lttv_stats_sum_trace(LttvTraceStats
*self
, LttvAttribute
*ts_stats
)
805 LttvAttribute
*sum_container
= self
->stats
;
807 LttvAttributeType type
;
809 LttvAttributeValue value
;
811 LttvAttributeName name
;
819 int i
, j
, k
, l
, m
, nb_process
, nb_cpu
, nb_mode_type
, nb_submode
,
820 nb_event_type
, nf
, nb_functions
;
822 LttvAttribute
*main_tree
, *processes_tree
, *process_tree
, *cpus_tree
,
823 *cpu_tree
, *mode_tree
, *mode_types_tree
, *submodes_tree
,
824 *submode_tree
, *event_types_tree
, *mode_events_tree
,
827 *function_mode_types_tree
,
831 main_tree
= sum_container
;
833 lttv_attribute_find(sum_container
,
836 trace_is_summed
= *(value
.v_uint
);
839 /* First cleanup the state : sum all stalled information (never ending
842 lttv_stats_cleanup_state(self
);
844 processes_tree
= lttv_attribute_find_subdir(main_tree
,
845 LTTV_STATS_PROCESSES
);
846 nb_process
= lttv_attribute_get_number(processes_tree
);
848 for(i
= 0 ; i
< nb_process
; i
++) {
849 type
= lttv_attribute_get(processes_tree
, i
, &name
, &value
, &is_named
);
850 process_tree
= LTTV_ATTRIBUTE(*(value
.v_gobject
));
852 cpus_tree
= lttv_attribute_find_subdir(process_tree
, LTTV_STATS_CPU
);
853 nb_cpu
= lttv_attribute_get_number(cpus_tree
);
855 for(j
= 0 ; j
< nb_cpu
; j
++) {
856 type
= lttv_attribute_get(cpus_tree
, j
, &name
, &value
, &is_named
);
857 cpu_tree
= LTTV_ATTRIBUTE(*(value
.v_gobject
));
859 trace_cpu_tree
= lttv_attribute_find_subdir(main_tree
, LTTV_STATS_CPU
);
860 trace_cpu_tree
= lttv_attribute_find_subdir_unnamed(trace_cpu_tree
, name
);
861 cpu_functions_tree
= lttv_attribute_find_subdir(cpu_tree
,
862 LTTV_STATS_FUNCTIONS
);
863 nb_functions
= lttv_attribute_get_number(cpu_functions_tree
);
865 for(nf
=0; nf
< nb_functions
; nf
++) {
866 type
= lttv_attribute_get(cpu_functions_tree
, nf
, &name
, &value
,
868 function_tree
= LTTV_ATTRIBUTE(*(value
.v_gobject
));
869 function_mode_types_tree
= lttv_attribute_find_subdir(function_tree
,
870 LTTV_STATS_MODE_TYPES
);
871 nb_mode_type
= lttv_attribute_get_number(function_mode_types_tree
);
872 for(k
= 0 ; k
< nb_mode_type
; k
++) {
873 type
= lttv_attribute_get(function_mode_types_tree
, k
, &name
, &value
,
875 mode_tree
= LTTV_ATTRIBUTE(*(value
.v_gobject
));
877 submodes_tree
= lttv_attribute_find_subdir(mode_tree
,
878 LTTV_STATS_SUBMODES
);
879 mode_events_tree
= lttv_attribute_find_subdir(mode_tree
,
881 mode_types_tree
= lttv_attribute_find_subdir(mode_tree
,
882 LTTV_STATS_MODE_TYPES
);
884 nb_submode
= lttv_attribute_get_number(submodes_tree
);
886 for(l
= 0 ; l
< nb_submode
; l
++) {
887 type
= lttv_attribute_get(submodes_tree
, l
, &name
, &value
,
889 submode_tree
= LTTV_ATTRIBUTE(*(value
.v_gobject
));
891 event_types_tree
= lttv_attribute_find_subdir(submode_tree
,
892 LTTV_STATS_EVENT_TYPES
);
893 nb_event_type
= lttv_attribute_get_number(event_types_tree
);
896 for(m
= 0 ; m
< nb_event_type
; m
++) {
897 type
= lttv_attribute_get(event_types_tree
, m
, &name
, &value
,
899 sum
+= *(value
.v_uint
);
901 lttv_attribute_find(submode_tree
, LTTV_STATS_EVENTS_COUNT
,
903 *(value
.v_uint
) = sum
;
905 type
= lttv_attribute_get(submodes_tree
, l
, &name
, &value
,
907 submode_tree
= LTTV_ATTRIBUTE(*(value
.v_gobject
));
908 if(!trace_is_summed
) {
909 lttv_attribute_recursive_add(mode_events_tree
, event_types_tree
);
910 lttv_attribute_recursive_add(mode_types_tree
, submode_tree
);
913 if(!trace_is_summed
) {
914 lttv_attribute_recursive_add(function_tree
, mode_types_tree
);
917 if(!trace_is_summed
) {
918 lttv_attribute_recursive_add(cpu_tree
, function_tree
);
919 lttv_attribute_recursive_add(process_tree
, function_tree
);
920 lttv_attribute_recursive_add(trace_cpu_tree
, function_tree
);
921 lttv_attribute_recursive_add(main_tree
, function_tree
);
923 lttv_attribute_recursive_add(ts_stats
, function_tree
);
930 gboolean
lttv_stats_sum_traceset_hook(void *hook_data
, void *call_data
)
932 lttv_stats_sum_traceset((LttvTracesetStats
*)call_data
);
937 lttv_stats_sum_traceset(LttvTracesetStats
*self
)
939 LttvTraceset
*traceset
= self
->parent
.parent
.ts
;
940 LttvAttribute
*sum_container
= self
->stats
;
946 LttvAttribute
*main_tree
;
948 LttvAttributeValue value
;
950 lttv_attribute_find(sum_container
, LTTV_STATS_SUMMED
,
952 if(*(value
.v_uint
) != 0) return;
955 nb_trace
= lttv_traceset_number(traceset
);
957 for(i
= 0 ; i
< nb_trace
; i
++) {
958 tcs
= (LttvTraceStats
*)(self
->parent
.parent
.traces
[i
]);
959 lttv_stats_sum_trace(tcs
, self
->stats
);
960 // lttv_attribute_recursive_add(sum_container, tcs->stats);
965 // Hook wrapper. call_data is a traceset context.
966 gboolean
lttv_stats_hook_add_event_hooks(void *hook_data
, void *call_data
)
968 LttvTracesetStats
*tss
= (LttvTracesetStats
*)call_data
;
970 lttv_stats_add_event_hooks(tss
);
975 void lttv_stats_add_event_hooks(LttvTracesetStats
*self
)
977 LttvTraceset
*traceset
= self
->parent
.parent
.ts
;
979 guint i
, j
, k
, l
, nb_trace
, nb_tracefile
;
983 LttvTracefileStats
*tfs
;
985 GArray
*hooks
, *before_hooks
, *after_hooks
;
989 LttvTraceHookByFacility
*thf
;
991 LttvAttributeValue val
;
996 nb_trace
= lttv_traceset_number(traceset
);
997 for(i
= 0 ; i
< nb_trace
; i
++) {
998 ts
= (LttvTraceStats
*)self
->parent
.parent
.traces
[i
];
1000 /* Find the eventtype id for the following events and register the
1001 associated by id hooks. */
1003 hooks
= g_array_sized_new(FALSE
, FALSE
, sizeof(LttvTraceHook
), 11);
1004 g_array_set_size(hooks
, 11);
1007 ret
= lttv_trace_find_hook(ts
->parent
.parent
.t
,
1008 LTT_FACILITY_KERNEL_ARCH
, LTT_EVENT_SYSCALL_ENTRY
,
1009 LTT_FIELD_SYSCALL_ID
, 0, 0,
1010 before_syscall_entry
, NULL
,
1011 &g_array_index(hooks
, LttvTraceHook
, hn
++));
1014 ret
= lttv_trace_find_hook(ts
->parent
.parent
.t
,
1015 LTT_FACILITY_KERNEL_ARCH
, LTT_EVENT_SYSCALL_EXIT
,
1017 before_syscall_exit
, NULL
,
1018 &g_array_index(hooks
, LttvTraceHook
, hn
++));
1021 ret
= lttv_trace_find_hook(ts
->parent
.parent
.t
,
1022 LTT_FACILITY_KERNEL
, LTT_EVENT_TRAP_ENTRY
,
1023 LTT_FIELD_TRAP_ID
, 0, 0,
1024 before_trap_entry
, NULL
,
1025 &g_array_index(hooks
, LttvTraceHook
, hn
++));
1028 ret
= lttv_trace_find_hook(ts
->parent
.parent
.t
,
1029 LTT_FACILITY_KERNEL
, LTT_EVENT_TRAP_EXIT
,
1031 before_trap_exit
, NULL
,
1032 &g_array_index(hooks
, LttvTraceHook
, hn
++));
1035 ret
= lttv_trace_find_hook(ts
->parent
.parent
.t
,
1036 LTT_FACILITY_KERNEL
, LTT_EVENT_IRQ_ENTRY
,
1037 LTT_FIELD_IRQ_ID
, 0, 0,
1038 before_irq_entry
, NULL
,
1039 &g_array_index(hooks
, LttvTraceHook
, hn
++));
1042 ret
= lttv_trace_find_hook(ts
->parent
.parent
.t
,
1043 LTT_FACILITY_KERNEL
, LTT_EVENT_IRQ_EXIT
,
1045 before_irq_exit
, NULL
,
1046 &g_array_index(hooks
, LttvTraceHook
, hn
++));
1049 ret
= lttv_trace_find_hook(ts
->parent
.parent
.t
,
1050 LTT_FACILITY_KERNEL
, LTT_EVENT_SOFT_IRQ_ENTRY
,
1051 LTT_FIELD_SOFT_IRQ_ID
, 0, 0,
1052 before_soft_irq_entry
, NULL
,
1053 &g_array_index(hooks
, LttvTraceHook
, hn
++));
1056 ret
= lttv_trace_find_hook(ts
->parent
.parent
.t
,
1057 LTT_FACILITY_KERNEL
, LTT_EVENT_SOFT_IRQ_EXIT
,
1059 before_soft_irq_exit
, NULL
,
1060 &g_array_index(hooks
, LttvTraceHook
, hn
++));
1063 ret
= lttv_trace_find_hook(ts
->parent
.parent
.t
,
1064 LTT_FACILITY_PROCESS
, LTT_EVENT_SCHEDCHANGE
,
1065 LTT_FIELD_OUT
, LTT_FIELD_IN
, LTT_FIELD_OUT_STATE
,
1066 before_schedchange
, NULL
,
1067 &g_array_index(hooks
, LttvTraceHook
, hn
++));
1070 ret
= lttv_trace_find_hook(ts
->parent
.parent
.t
,
1071 LTT_FACILITY_USER_GENERIC
, LTT_EVENT_FUNCTION_ENTRY
,
1072 LTT_FIELD_THIS_FN
, LTT_FIELD_CALL_SITE
, 0,
1073 before_function_entry
, NULL
,
1074 &g_array_index(hooks
, LttvTraceHook
, hn
++));
1077 ret
= lttv_trace_find_hook(ts
->parent
.parent
.t
,
1078 LTT_FACILITY_USER_GENERIC
, LTT_EVENT_FUNCTION_EXIT
,
1079 LTT_FIELD_THIS_FN
, LTT_FIELD_CALL_SITE
, 0,
1080 before_function_exit
, NULL
,
1081 &g_array_index(hooks
, LttvTraceHook
, hn
++));
1084 g_array_set_size(hooks
, hn
);
1086 before_hooks
= hooks
;
1088 hooks
= g_array_sized_new(FALSE
, FALSE
, sizeof(LttvTraceHook
), 14);
1089 g_array_set_size(hooks
, 14);
1092 ret
= lttv_trace_find_hook(ts
->parent
.parent
.t
,
1093 LTT_FACILITY_KERNEL_ARCH
, LTT_EVENT_SYSCALL_ENTRY
,
1094 LTT_FIELD_SYSCALL_ID
, 0, 0,
1095 after_syscall_entry
, NULL
,
1096 &g_array_index(hooks
, LttvTraceHook
, hn
++));
1099 ret
= lttv_trace_find_hook(ts
->parent
.parent
.t
,
1100 LTT_FACILITY_KERNEL_ARCH
, LTT_EVENT_SYSCALL_EXIT
,
1102 after_syscall_exit
, NULL
,
1103 &g_array_index(hooks
, LttvTraceHook
, hn
++));
1106 ret
= lttv_trace_find_hook(ts
->parent
.parent
.t
,
1107 LTT_FACILITY_KERNEL
, LTT_EVENT_TRAP_ENTRY
,
1108 LTT_FIELD_TRAP_ID
, 0, 0,
1109 after_trap_entry
, NULL
,
1110 &g_array_index(hooks
, LttvTraceHook
, hn
++));
1113 ret
= lttv_trace_find_hook(ts
->parent
.parent
.t
,
1114 LTT_FACILITY_KERNEL
, LTT_EVENT_TRAP_EXIT
,
1116 after_trap_exit
, NULL
,
1117 &g_array_index(hooks
, LttvTraceHook
, hn
++));
1120 ret
= lttv_trace_find_hook(ts
->parent
.parent
.t
,
1121 LTT_FACILITY_KERNEL
, LTT_EVENT_IRQ_ENTRY
,
1122 LTT_FIELD_IRQ_ID
, 0, 0,
1123 after_irq_entry
, NULL
,
1124 &g_array_index(hooks
, LttvTraceHook
, hn
++));
1127 ret
= lttv_trace_find_hook(ts
->parent
.parent
.t
,
1128 LTT_FACILITY_KERNEL
, LTT_EVENT_IRQ_EXIT
,
1130 after_irq_exit
, NULL
,
1131 &g_array_index(hooks
, LttvTraceHook
, hn
++));
1134 ret
= lttv_trace_find_hook(ts
->parent
.parent
.t
,
1135 LTT_FACILITY_KERNEL
, LTT_EVENT_SOFT_IRQ_ENTRY
,
1136 LTT_FIELD_SOFT_IRQ_ID
, 0, 0,
1137 after_irq_entry
, NULL
,
1138 &g_array_index(hooks
, LttvTraceHook
, hn
++));
1141 ret
= lttv_trace_find_hook(ts
->parent
.parent
.t
,
1142 LTT_FACILITY_KERNEL
, LTT_EVENT_SOFT_IRQ_EXIT
,
1144 after_soft_irq_exit
, NULL
,
1145 &g_array_index(hooks
, LttvTraceHook
, hn
++));
1148 ret
= lttv_trace_find_hook(ts
->parent
.parent
.t
,
1149 LTT_FACILITY_PROCESS
, LTT_EVENT_SCHEDCHANGE
,
1150 LTT_FIELD_OUT
, LTT_FIELD_IN
, LTT_FIELD_OUT_STATE
,
1151 after_schedchange
, NULL
,
1152 &g_array_index(hooks
, LttvTraceHook
, hn
++));
1155 ret
= lttv_trace_find_hook(ts
->parent
.parent
.t
,
1156 LTT_FACILITY_PROCESS
, LTT_EVENT_FORK
,
1157 LTT_FIELD_PARENT_PID
, LTT_FIELD_CHILD_PID
, 0,
1159 &g_array_index(hooks
, LttvTraceHook
, hn
++));
1162 ret
= lttv_trace_find_hook(ts
->parent
.parent
.t
,
1163 LTT_FACILITY_PROCESS
, LTT_EVENT_EXIT
,
1164 LTT_FIELD_PID
, 0, 0,
1166 &g_array_index(hooks
, LttvTraceHook
, hn
++));
1169 ret
= lttv_trace_find_hook(ts
->parent
.parent
.t
,
1170 LTT_FACILITY_PROCESS
, LTT_EVENT_FREE
,
1171 LTT_FIELD_PID
, 0, 0,
1173 &g_array_index(hooks
, LttvTraceHook
, hn
++));
1176 ret
= lttv_trace_find_hook(ts
->parent
.parent
.t
,
1177 LTT_FACILITY_USER_GENERIC
, LTT_EVENT_FUNCTION_ENTRY
,
1178 LTT_FIELD_THIS_FN
, LTT_FIELD_CALL_SITE
, 0,
1179 after_function_entry
, NULL
,
1180 &g_array_index(hooks
, LttvTraceHook
, hn
++));
1183 ret
= lttv_trace_find_hook(ts
->parent
.parent
.t
,
1184 LTT_FACILITY_USER_GENERIC
, LTT_EVENT_FUNCTION_EXIT
,
1185 LTT_FIELD_THIS_FN
, LTT_FIELD_CALL_SITE
, 0,
1186 after_function_exit
, NULL
,
1187 &g_array_index(hooks
, LttvTraceHook
, hn
++));
1190 g_array_set_size(hooks
, hn
);
1192 after_hooks
= hooks
;
1194 /* Add these hooks to each event_by_id hooks list */
1196 nb_tracefile
= ts
->parent
.parent
.tracefiles
->len
;
1198 for(j
= 0 ; j
< nb_tracefile
; j
++) {
1199 tfs
= LTTV_TRACEFILE_STATS(g_array_index(ts
->parent
.parent
.tracefiles
,
1200 LttvTracefileContext
*, j
));
1201 lttv_hooks_add(tfs
->parent
.parent
.event
, every_event
, NULL
,
1204 for(k
= 0 ; k
< before_hooks
->len
; k
++) {
1205 hook
= &g_array_index(before_hooks
, LttvTraceHook
, k
);
1206 for(l
= 0; l
<hook
->fac_list
->len
;l
++) {
1207 thf
= g_array_index(hook
->fac_list
, LttvTraceHookByFacility
*, l
);
1209 lttv_hooks_by_id_find(tfs
->parent
.parent
.event_by_id
, thf
->id
),
1212 LTTV_PRIO_STATS_BEFORE_STATE
);
1215 for(k
= 0 ; k
< after_hooks
->len
; k
++) {
1216 hook
= &g_array_index(after_hooks
, LttvTraceHook
, k
);
1217 for(l
= 0; l
<hook
->fac_list
->len
;l
++) {
1218 thf
= g_array_index(hook
->fac_list
, LttvTraceHookByFacility
*, l
);
1220 lttv_hooks_by_id_find(tfs
->parent
.parent
.event_by_id
, thf
->id
),
1223 LTTV_PRIO_STATS_AFTER_STATE
);
1227 lttv_attribute_find(self
->parent
.parent
.a
, LTTV_STATS_BEFORE_HOOKS
,
1228 LTTV_POINTER
, &val
);
1229 *(val
.v_pointer
) = before_hooks
;
1230 lttv_attribute_find(self
->parent
.parent
.a
, LTTV_STATS_AFTER_HOOKS
,
1231 LTTV_POINTER
, &val
);
1232 *(val
.v_pointer
) = after_hooks
;
1236 // Hook wrapper. call_data is a traceset context.
1237 gboolean
lttv_stats_hook_remove_event_hooks(void *hook_data
, void *call_data
)
1239 LttvTracesetStats
*tss
= (LttvTracesetStats
*)call_data
;
1241 lttv_stats_remove_event_hooks(tss
);
1246 void lttv_stats_remove_event_hooks(LttvTracesetStats
*self
)
1248 LttvTraceset
*traceset
= self
->parent
.parent
.ts
;
1250 guint i
, j
, k
, l
, nb_trace
, nb_tracefile
;
1254 LttvTracefileStats
*tfs
;
1258 GArray
*before_hooks
, *after_hooks
;
1260 LttvTraceHook
*hook
;
1262 LttvTraceHookByFacility
*thf
;
1264 LttvAttributeValue val
;
1266 nb_trace
= lttv_traceset_number(traceset
);
1267 for(i
= 0 ; i
< nb_trace
; i
++) {
1268 ts
= (LttvTraceStats
*)self
->parent
.parent
.traces
[i
];
1269 lttv_attribute_find(self
->parent
.parent
.a
, LTTV_STATS_BEFORE_HOOKS
,
1270 LTTV_POINTER
, &val
);
1271 before_hooks
= *(val
.v_pointer
);
1272 lttv_attribute_find(self
->parent
.parent
.a
, LTTV_STATS_AFTER_HOOKS
,
1273 LTTV_POINTER
, &val
);
1274 after_hooks
= *(val
.v_pointer
);
1276 /* Remove these hooks from each event_by_id hooks list */
1278 nb_tracefile
= ts
->parent
.parent
.tracefiles
->len
;
1280 for(j
= 0 ; j
< nb_tracefile
; j
++) {
1281 tfs
= LTTV_TRACEFILE_STATS(g_array_index(ts
->parent
.parent
.tracefiles
,
1282 LttvTracefileContext
*, j
));
1283 lttv_hooks_remove_data(tfs
->parent
.parent
.event
, every_event
,
1286 for(k
= 0 ; k
< before_hooks
->len
; k
++) {
1287 hook
= &g_array_index(before_hooks
, LttvTraceHook
, k
);
1288 for(l
= 0 ; l
< hook
->fac_list
->len
; l
++) {
1289 thf
= g_array_index(hook
->fac_list
, LttvTraceHookByFacility
*, l
);
1290 lttv_hooks_remove_data(
1291 lttv_hooks_by_id_find(tfs
->parent
.parent
.event_by_id
, thf
->id
),
1296 for(k
= 0 ; k
< after_hooks
->len
; k
++) {
1297 hook
= &g_array_index(after_hooks
, LttvTraceHook
, k
);
1298 for(l
= 0 ; l
< hook
->fac_list
->len
; l
++) {
1299 thf
= g_array_index(hook
->fac_list
, LttvTraceHookByFacility
*, l
);
1300 lttv_hooks_remove_data(
1301 lttv_hooks_by_id_find(tfs
->parent
.parent
.event_by_id
, thf
->id
),
1307 g_debug("lttv_stats_remove_event_hooks()");
1308 g_array_free(before_hooks
, TRUE
);
1309 g_array_free(after_hooks
, TRUE
);
1314 static void module_init()
1316 LTTV_STATS_PROCESS_UNKNOWN
= g_quark_from_string("unknown process");
1317 LTTV_STATS_PROCESSES
= g_quark_from_string("processes");
1318 LTTV_STATS_CPU
= g_quark_from_string("cpu");
1319 LTTV_STATS_MODE_TYPES
= g_quark_from_string("mode_types");
1320 LTTV_STATS_MODES
= g_quark_from_string("modes");
1321 LTTV_STATS_SUBMODES
= g_quark_from_string("submodes");
1322 LTTV_STATS_FUNCTIONS
= g_quark_from_string("functions");
1323 LTTV_STATS_EVENT_TYPES
= g_quark_from_string("event_types");
1324 LTTV_STATS_CPU_TIME
= g_quark_from_string("cpu time");
1325 LTTV_STATS_CUMULATIVE_CPU_TIME
= g_quark_from_string("cumulative cpu time (includes nested routines and modes)");
1326 LTTV_STATS_ELAPSED_TIME
= g_quark_from_string("elapsed time (includes per process waiting time)");
1327 LTTV_STATS_EVENTS
= g_quark_from_string("events");
1328 LTTV_STATS_EVENTS_COUNT
= g_quark_from_string("events count");
1329 LTTV_STATS_BEFORE_HOOKS
= g_quark_from_string("saved stats before hooks");
1330 LTTV_STATS_AFTER_HOOKS
= g_quark_from_string("saved stats after hooks");
1331 LTTV_STATS_USE_COUNT
= g_quark_from_string("stats_use_count");
1332 LTTV_STATS
= g_quark_from_string("statistics");
1333 LTTV_STATS_TRACEFILES
= g_quark_from_string("tracefiles statistics");
1334 LTTV_STATS_SUMMED
= g_quark_from_string("statistics summed");
1337 static void module_destroy()
1342 LTTV_MODULE("stats", "Compute processes statistics", \
1343 "Accumulate statistics for event types, processes and CPUs", \
1344 module_init
, module_destroy
, "state");
1346 /* Change the places where stats are called (create/read/write stats)
1348 Check for options in batchtest.c to reduce writing and see what tests are
1349 best candidates for performance analysis. Once OK, commit, move to main
1350 and run tests. Update the gui for statistics. */