1 /* This file is part of the Linux Trace Toolkit viewer
2 * Copyright (C) 2003-2004 XangXiu Yang
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 <ltt/event.h>
26 #include <ltt/trace.h>
28 #include <lttv/lttv.h>
29 #include <lttv/module.h>
30 #include <lttv/tracecontext.h>
31 #include <lttv/hook.h>
32 #include <lttv/state.h>
33 #include <lttv/stats.h>
35 #include <lttvwindow/lttvwindow.h>
36 #include <lttvwindow/lttvwindowtraces.h>
38 #include "hGuiStatisticInsert.xpm"
40 #define PATH_LENGTH 256 /* CHECK */
41 #define MAX_NUMBER_EVENT "max_number_event"
44 //static GPtrArray * statistic_traceset;
46 /** Array containing instanced objects. Used when module is unloaded */
47 static GSList
*g_statistic_viewer_data_list
= NULL
;
49 typedef struct _StatisticViewerData StatisticViewerData
;
51 static void request_background_data(StatisticViewerData
*svd
);
52 GtkWidget
*guistatistic_get_widget(StatisticViewerData
*svd
);
54 //! Statistic Viewer's constructor hook
55 GtkWidget
*h_gui_statistic(Tab
*tab
, LttvTracesetSelector
* s
, char * key
);
56 //! Statistic Viewer's constructor
57 StatisticViewerData
*gui_statistic(Tab
*tab
);
58 //! Statistic Viewer's destructor
59 void gui_statistic_destructor(StatisticViewerData
*statistic_viewer_data
);
61 static void tree_selection_changed_cb (GtkTreeSelection
*selection
, gpointer data
);
63 void statistic_destroy_hash_key(gpointer key
);
64 void statistic_destroy_hash_data(gpointer data
);
66 void show_traceset_stats(StatisticViewerData
* statistic_viewer_data
);
67 void show_tree(StatisticViewerData
* statistic_viewer_data
,
68 LttvAttribute
* stats
, GtkTreeIter
* parent
);
69 void show_statistic(StatisticViewerData
* statistic_viewer_data
,
70 LttvAttribute
* stats
, GtkTextBuffer
* buf
);
73 gboolean
statistic_traceset_changed(void * hook_data
, void * call_data
);
74 //void statistic_add_context_hooks(StatisticViewerData * statistic_viewer_data,
75 // LttvTracesetContext * tsc);
76 //void statistic_remove_context_hooks(StatisticViewerData *statistic_viewer_data,
77 // LttvTracesetContext * tsc);
79 //gboolean statistic_insert_traceset_stats(void * stats);
87 struct _StatisticViewerData
{
89 //LttvTracesetStats * stats;
92 //gboolean shown; //indicate if the statistic is shown or not
96 GtkTreeStore
* store_m
;
99 //scroll window containing Tree View
100 GtkWidget
* scroll_win_tree
;
103 //scroll window containing Text View
104 GtkWidget
* scroll_win_text
;
107 GtkTreeSelection
*select_c
;
110 GHashTable
*statistic_hash
;
116 /* Action to do when background computation completed.
118 * Eventually, will have to check that every requested traces are finished
119 * before doing the redraw. It will save unnecessary processor usage.
122 static gint
background_ready(void *hook_data
, void *call_data
)
124 StatisticViewerData
*svd
= (StatisticViewerData
*)hook_data
;
126 LttvTrace
*trace
= (LttvTrace
*)call_data
;
128 g_debug("statistics viewer : background computation data ready.");
130 gtk_tree_store_clear (svd
->store_m
);
132 lttv_stats_sum_traceset(lttvwindow_get_traceset_stats(tab
));
133 show_traceset_stats(svd
);
138 /* Request background computation. Verify if it is in progress or ready first.
140 * Right now, for all loaded traces.
142 * Later : must be only for each trace in the tab's traceset.
144 static void request_background_data(StatisticViewerData
*svd
)
146 gint num_traces
= lttvwindowtraces_get_number();
150 LttvHooks
*background_ready_hook
=
152 lttv_hooks_add(background_ready_hook
, background_ready
, svd
,
155 for(i
=0;i
<num_traces
;i
++) {
156 trace
= lttvwindowtraces_get_trace(i
);
158 if(lttvwindowtraces_get_ready(g_quark_from_string("stats"),trace
)==FALSE
) {
160 if(lttvwindowtraces_get_in_progress(g_quark_from_string("stats"),
162 /* We first remove requests that could have been done for the same
163 * information. Happens when two viewers ask for it before servicing
166 lttvwindowtraces_background_request_remove(trace
, "stats");
167 lttvwindowtraces_background_request_queue(trace
,
169 lttvwindowtraces_background_notify_queue(svd
,
173 background_ready_hook
);
174 } else { /* in progress */
176 lttvwindowtraces_background_notify_current(svd
,
180 background_ready_hook
);
185 lttv_hooks_destroy(background_ready_hook
);
189 GtkWidget
*guistatistic_get_widget(StatisticViewerData
*svd
)
191 return svd
->hpaned_v
;
196 gui_statistic_destructor(StatisticViewerData
*statistic_viewer_data
)
198 Tab
*tab
= statistic_viewer_data
->tab
;
200 /* May already been done by GTK window closing */
201 if(GTK_IS_WIDGET(guistatistic_get_widget(statistic_viewer_data
))){
202 g_info("widget still exists");
205 lttvwindow_unregister_traceset_notify(statistic_viewer_data
->tab
,
206 statistic_traceset_changed
,
207 statistic_viewer_data
);
209 lttvwindowtraces_background_notify_remove(statistic_viewer_data
);
211 g_hash_table_destroy(statistic_viewer_data
->statistic_hash
);
212 g_statistic_viewer_data_list
=
213 g_slist_remove(g_statistic_viewer_data_list
, statistic_viewer_data
);
214 g_free(statistic_viewer_data
);
219 * Statistic Viewer's constructor hook
221 * This constructor is given as a parameter to the menuitem and toolbar button
222 * registration. It creates the list.
223 * @param parent_window A pointer to the parent window.
224 * @return The widget created.
227 h_gui_statistic(Tab
*tab
, LttvTracesetSelector
* s
, char * key
)
229 StatisticViewerData
* statistic_viewer_data
= gui_statistic(tab
) ;
231 if(statistic_viewer_data
)
232 return guistatistic_get_widget(statistic_viewer_data
);
238 gboolean
statistic_insert_traceset_stats(void * stats
)
243 len
= statistic_traceset
->len
;
245 s
= g_ptr_array_index(statistic_traceset
, i
);
246 if(s
== stats
) break;
249 g_ptr_array_add(statistic_traceset
, stats
);
257 * Statistic Viewer's constructor
259 * This constructor is used to create StatisticViewerData data structure.
260 * @return The Statistic viewer data created.
262 StatisticViewerData
*
263 gui_statistic(Tab
*tab
)
265 GtkCellRenderer
*renderer
;
266 GtkTreeViewColumn
*column
;
268 StatisticViewerData
* statistic_viewer_data
= g_new(StatisticViewerData
,1);
270 statistic_viewer_data
->tab
= tab
;
271 // statistic_viewer_data->stats =
272 // lttvwindow_get_traceset_stats(statistic_viewer_data->tab);
273 // statistic_viewer_data->calculate_stats =
274 // statistic_insert_traceset_stats((void *)statistic_viewer_data->stats);
276 lttvwindow_register_traceset_notify(statistic_viewer_data
->tab
,
277 statistic_traceset_changed
,
278 statistic_viewer_data
);
279 request_background_data(statistic_viewer_data
);
281 statistic_viewer_data
->statistic_hash
= g_hash_table_new_full(g_str_hash
,
283 statistic_destroy_hash_key
,
284 statistic_destroy_hash_data
);
286 statistic_viewer_data
->hpaned_v
= gtk_hpaned_new();
287 statistic_viewer_data
->store_m
= gtk_tree_store_new (N_COLUMNS
, G_TYPE_STRING
);
288 statistic_viewer_data
->tree_v
=
289 gtk_tree_view_new_with_model (
290 GTK_TREE_MODEL (statistic_viewer_data
->store_m
));
291 g_object_unref (G_OBJECT (statistic_viewer_data
->store_m
));
293 // Setup the selection handler
294 statistic_viewer_data
->select_c
= gtk_tree_view_get_selection (GTK_TREE_VIEW (statistic_viewer_data
->tree_v
));
295 gtk_tree_selection_set_mode (statistic_viewer_data
->select_c
, GTK_SELECTION_SINGLE
);
296 g_signal_connect (G_OBJECT (statistic_viewer_data
->select_c
), "changed",
297 G_CALLBACK (tree_selection_changed_cb
),
298 statistic_viewer_data
);
300 renderer
= gtk_cell_renderer_text_new ();
301 column
= gtk_tree_view_column_new_with_attributes ("Statistic Name",
305 gtk_tree_view_column_set_alignment (column
, 0.0);
306 // gtk_tree_view_column_set_fixed_width (column, 45);
307 gtk_tree_view_append_column (GTK_TREE_VIEW (statistic_viewer_data
->tree_v
), column
);
310 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW (statistic_viewer_data
->tree_v
), FALSE
);
312 statistic_viewer_data
->scroll_win_tree
= gtk_scrolled_window_new (NULL
, NULL
);
313 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(statistic_viewer_data
->scroll_win_tree
),
314 GTK_POLICY_AUTOMATIC
,GTK_POLICY_AUTOMATIC
);
316 gtk_container_add (GTK_CONTAINER (statistic_viewer_data
->scroll_win_tree
), statistic_viewer_data
->tree_v
);
317 gtk_paned_pack1(GTK_PANED(statistic_viewer_data
->hpaned_v
),statistic_viewer_data
->scroll_win_tree
, TRUE
, FALSE
);
318 gtk_paned_set_position(GTK_PANED(statistic_viewer_data
->hpaned_v
), 160);
320 statistic_viewer_data
->scroll_win_text
= gtk_scrolled_window_new (NULL
, NULL
);
321 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(statistic_viewer_data
->scroll_win_text
),
322 GTK_POLICY_AUTOMATIC
,GTK_POLICY_AUTOMATIC
);
324 statistic_viewer_data
->text_v
= gtk_text_view_new ();
326 gtk_text_view_set_editable(GTK_TEXT_VIEW(statistic_viewer_data
->text_v
),FALSE
);
327 gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(statistic_viewer_data
->text_v
),FALSE
);
328 gtk_container_add (GTK_CONTAINER (statistic_viewer_data
->scroll_win_text
), statistic_viewer_data
->text_v
);
329 gtk_paned_pack2(GTK_PANED(statistic_viewer_data
->hpaned_v
), statistic_viewer_data
->scroll_win_text
, TRUE
, FALSE
);
331 gtk_widget_show(statistic_viewer_data
->scroll_win_tree
);
332 gtk_widget_show(statistic_viewer_data
->scroll_win_text
);
333 gtk_widget_show(statistic_viewer_data
->tree_v
);
334 gtk_widget_show(statistic_viewer_data
->text_v
);
335 gtk_widget_show(statistic_viewer_data
->hpaned_v
);
337 g_object_set_data_full(
338 G_OBJECT(guistatistic_get_widget(statistic_viewer_data
)),
339 "statistic_viewer_data",
340 statistic_viewer_data
,
341 (GDestroyNotify
)gui_statistic_destructor
);
343 /* Add the object's information to the module's array */
344 g_statistic_viewer_data_list
= g_slist_append(
345 g_statistic_viewer_data_list
,
346 statistic_viewer_data
);
348 return statistic_viewer_data
;
352 tree_selection_changed_cb (GtkTreeSelection
*selection
, gpointer data
)
354 StatisticViewerData
*statistic_viewer_data
= (StatisticViewerData
*)data
;
356 GtkTreeModel
*model
= GTK_TREE_MODEL(statistic_viewer_data
->store_m
);
361 GtkTextIter text_iter
;
362 LttvAttribute
* stats
;
364 if (gtk_tree_selection_get_selected (selection
, &model
, &iter
))
366 gtk_tree_model_get (model
, &iter
, NAME_COLUMN
, &event
, -1);
368 path
= gtk_tree_model_get_path(GTK_TREE_MODEL(model
),&iter
);
369 str
= gtk_tree_path_to_string (path
);
370 stats
= (LttvAttribute
*)g_hash_table_lookup (statistic_viewer_data
->statistic_hash
,str
);
373 buf
= gtk_text_view_get_buffer((GtkTextView
*)statistic_viewer_data
->text_v
);
374 gtk_text_buffer_set_text(buf
,"Statistic for '", -1);
375 gtk_text_buffer_get_end_iter(buf
, &text_iter
);
376 gtk_text_buffer_insert(buf
, &text_iter
, event
, strlen(event
));
377 gtk_text_buffer_get_end_iter(buf
, &text_iter
);
378 gtk_text_buffer_insert(buf
, &text_iter
, "' :\n\n",5);
380 show_statistic(statistic_viewer_data
, stats
, buf
);
386 void statistic_destroy_hash_key(gpointer key
)
391 void statistic_destroy_hash_data(gpointer data
)
401 void show_traceset_stats(StatisticViewerData
* statistic_viewer_data
)
403 Tab
*tab
= statistic_viewer_data
->tab
;
407 LttSystemDescription
*desc
;
408 LttvTracesetStats
* tscs
= lttvwindow_get_traceset_stats(tab
);
409 gchar
* str
, trace_str
[PATH_LENGTH
];
412 GtkTreeStore
* store
= statistic_viewer_data
->store_m
;
414 if(tscs
->stats
== NULL
) return;
416 lttv_attribute_write_xml(tscs
->stats
, stdout
, 1, 4);
419 ts
= tscs
->parent
.parent
.ts
;
420 nb
= lttv_traceset_number(ts
);
423 gtk_tree_store_append (store
, &iter
, NULL
);
424 gtk_tree_store_set (store
, &iter
,
425 NAME_COLUMN
, "Traceset statistics",
427 path
= gtk_tree_model_get_path(GTK_TREE_MODEL(store
), &iter
);
428 str
= gtk_tree_path_to_string (path
);
429 g_hash_table_insert(statistic_viewer_data
->statistic_hash
,
430 (gpointer
)str
, tscs
->stats
);
431 show_tree(statistic_viewer_data
, tscs
->stats
, &iter
);
433 //show stats for all traces
434 for(i
= 0 ; i
< nb
; i
++) {
435 tcs
= (LttvTraceStats
*)(LTTV_TRACESET_CONTEXT(tscs
)->traces
[i
]);
436 desc
= ltt_trace_system_description(tcs
->parent
.parent
.t
);
437 sprintf(trace_str
, "Trace on system %s at time %d secs",
438 ltt_trace_system_description_node_name(desc
),
439 (ltt_trace_system_description_trace_start_time(desc
)).tv_sec
);
441 gtk_tree_store_append (store
, &iter
, NULL
);
442 gtk_tree_store_set (store
, &iter
,NAME_COLUMN
,trace_str
,-1);
443 path
= gtk_tree_model_get_path(GTK_TREE_MODEL(store
), &iter
);
444 str
= gtk_tree_path_to_string (path
);
445 g_hash_table_insert(statistic_viewer_data
->statistic_hash
,
446 (gpointer
)str
,tcs
->stats
);
447 show_tree(statistic_viewer_data
, tcs
->stats
, &iter
);
449 lttv_attribute_write_xml(tcs
->stats
, stdout
, 3, 4);
454 void show_tree(StatisticViewerData
* statistic_viewer_data
,
455 LttvAttribute
* stats
, GtkTreeIter
* parent
)
458 LttvAttribute
*subtree
;
459 LttvAttributeName name
;
460 LttvAttributeValue value
;
461 LttvAttributeType type
;
462 gchar
* str
, dir_str
[PATH_LENGTH
];
465 GtkTreeStore
* store
= statistic_viewer_data
->store_m
;
467 nb
= lttv_attribute_get_number(stats
);
468 for(i
= 0 ; i
< nb
; i
++) {
469 type
= lttv_attribute_get(stats
, i
, &name
, &value
);
472 if(LTTV_IS_ATTRIBUTE(*(value
.v_gobject
))) {
473 sprintf(dir_str
, "%s", g_quark_to_string(name
));
474 subtree
= (LttvAttribute
*)*(value
.v_gobject
);
475 gtk_tree_store_append (store
, &iter
, parent
);
476 gtk_tree_store_set (store
, &iter
,NAME_COLUMN
,dir_str
,-1);
477 path
= gtk_tree_model_get_path(GTK_TREE_MODEL(store
), &iter
);
478 str
= gtk_tree_path_to_string (path
);
479 g_hash_table_insert(statistic_viewer_data
->statistic_hash
,
480 (gpointer
)str
, subtree
);
481 show_tree(statistic_viewer_data
, subtree
, &iter
);
490 void show_statistic(StatisticViewerData
* statistic_viewer_data
,
491 LttvAttribute
* stats
, GtkTextBuffer
* buf
)
494 LttvAttribute
*subtree
;
495 LttvAttributeName name
;
496 LttvAttributeValue value
;
497 LttvAttributeType type
;
498 gchar type_name
[PATH_LENGTH
], type_value
[PATH_LENGTH
];
499 GtkTextIter text_iter
;
502 nb
= lttv_attribute_get_number(stats
);
503 for(i
= 0 ; i
< nb
; i
++) {
504 type
= lttv_attribute_get(stats
, i
, &name
, &value
);
505 sprintf(type_name
,"%s", g_quark_to_string(name
));
506 type_value
[0] = '\0';
509 sprintf(type_value
, " : %d\n", *value
.v_int
);
512 sprintf(type_value
, " : %u\n", *value
.v_uint
);
515 sprintf(type_value
, " : %ld\n", *value
.v_long
);
518 sprintf(type_value
, " : %lu\n", *value
.v_ulong
);
521 sprintf(type_value
, " : %f\n", (double)*value
.v_float
);
524 sprintf(type_value
, " : %f\n", *value
.v_double
);
527 sprintf(type_value
, " : %10u.%09u\n", value
.v_time
->tv_sec
,
528 value
.v_time
->tv_nsec
);
531 sprintf(type_value
, " : POINTER\n");
534 sprintf(type_value
, " : %s\n", *value
.v_string
);
539 if(strlen(type_value
)){
541 strcat(type_name
,type_value
);
542 gtk_text_buffer_get_end_iter(buf
, &text_iter
);
543 gtk_text_buffer_insert(buf
, &text_iter
, type_name
, strlen(type_name
));
548 sprintf(type_value
, "No statistic information in this directory.\nCheck in subdirectories please.\n");
549 gtk_text_buffer_get_end_iter(buf
, &text_iter
);
550 gtk_text_buffer_insert(buf
, &text_iter
, type_value
, strlen(type_value
));
555 gboolean
statistic_traceset_changed(void * hook_data
, void * call_data
)
557 StatisticViewerData
*statistic_viewer_data
= (StatisticViewerData
*) hook_data
;
559 request_background_data(statistic_viewer_data
);
565 void statistic_add_context_hooks(StatisticViewerData
* statistic_viewer_data
,
566 LttvTracesetContext
* tsc
)
568 gint i
, j
, nbi
, nb_tracefile
;
570 LttvTraceContext
*tc
;
571 LttvTracefileContext
*tfc
;
572 LttvTracesetSelector
* ts_s
;
573 LttvTraceSelector
* t_s
;
574 LttvTracefileSelector
* tf_s
;
577 ts_s
= (LttvTracesetSelector
*)g_object_get_data(G_OBJECT(statistic_viewer_data
->hpaned_v
),
578 statistic_viewer_data
->filter_key
);
580 //if there are hooks for traceset, add them here
582 nbi
= lttv_traceset_number(tsc
->ts
);
583 for(i
= 0 ; i
< nbi
; i
++) {
584 t_s
= lttv_traceset_selector_trace_get(ts_s
,i
);
585 selected
= lttv_trace_selector_get_selected(t_s
);
586 if(!selected
) continue;
589 //if there are hooks for trace, add them here
591 nb_tracefile
= ltt_trace_control_tracefile_number(trace
) +
592 ltt_trace_per_cpu_tracefile_number(trace
);
594 for(j
= 0 ; j
< nb_tracefile
; j
++) {
595 tf_s
= lttv_trace_selector_tracefile_get(t_s
,j
);
596 selected
= lttv_tracefile_selector_get_selected(tf_s
);
597 if(!selected
) continue;
598 tfc
= tc
->tracefiles
[j
];
600 //if there are hooks for tracefile, add them here
601 // lttv_tracefile_context_add_hooks(tfc, NULL,NULL,NULL,NULL,
602 // statistic_viewer_data->before_event_hooks,NULL);
606 lttv_stats_add_event_hooks(LTTV_TRACESET_STATS(tsc
));
610 void statistic_remove_context_hooks(StatisticViewerData
* statistic_viewer_data
,
611 LttvTracesetContext
* tsc
)
613 gint i
, j
, nbi
, nb_tracefile
;
615 LttvTraceContext
*tc
;
616 LttvTracefileContext
*tfc
;
617 LttvTracesetSelector
* ts_s
;
618 LttvTraceSelector
* t_s
;
619 LttvTracefileSelector
* tf_s
;
622 ts_s
= (LttvTracesetSelector
*)g_object_get_data(G_OBJECT(statistic_viewer_data
->hpaned_v
),
623 statistic_viewer_data
->filter_key
);
625 //if there are hooks for traceset, remove them here
627 nbi
= lttv_traceset_number(tsc
->ts
);
628 for(i
= 0 ; i
< nbi
; i
++) {
629 t_s
= lttv_traceset_selector_trace_get(ts_s
,i
);
630 selected
= lttv_trace_selector_get_selected(t_s
);
631 if(!selected
) continue;
634 //if there are hooks for trace, remove them here
636 nb_tracefile
= ltt_trace_control_tracefile_number(trace
) +
637 ltt_trace_per_cpu_tracefile_number(trace
);
639 for(j
= 0 ; j
< nb_tracefile
; j
++) {
640 tf_s
= lttv_trace_selector_tracefile_get(t_s
,j
);
641 selected
= lttv_tracefile_selector_get_selected(tf_s
);
642 if(!selected
) continue;
643 tfc
= tc
->tracefiles
[j
];
645 //if there are hooks for tracefile, remove them here
646 // lttv_tracefile_context_remove_hooks(tfc, NULL,NULL,NULL,NULL,
647 // statistic_viewer_data->before_event_hooks,NULL);
651 lttv_stats_remove_event_hooks(LTTV_TRACESET_STATS(tsc
));
656 * plugin's init function
658 * This function initializes the Statistic Viewer functionnality through the
663 lttvwindow_register_constructor("/",
664 "Insert Statistic Viewer",
665 hGuiStatisticInsert_xpm
,
666 "Insert Statistic Viewer",
670 void statistic_destroy_walk(gpointer data
, gpointer user_data
)
672 StatisticViewerData
*svd
= (StatisticViewerData
*)data
;
674 g_debug("CFV.c : statistic_destroy_walk, %p", svd
);
675 /* May already have been done by GTK window closing */
676 if(GTK_IS_WIDGET(guistatistic_get_widget(svd
)))
677 gtk_widget_destroy(guistatistic_get_widget(svd
));
681 * plugin's destroy function
683 * This function releases the memory reserved by the module and unregisters
684 * everything that has been registered in the gtkTraceSet API.
686 static void destroy() {
689 g_slist_foreach(g_statistic_viewer_data_list
, statistic_destroy_walk
, NULL
);
690 g_slist_free(g_statistic_viewer_data_list
);
692 lttvwindow_unregister_constructor(h_gui_statistic
);
697 LTTV_MODULE("guistatistics", "Statistics viewer", \
698 "Graphical module to view statistics about processes, CPUs and systems", \
699 init
, destroy
, "lttvwindow")