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,
25 #include <ltt/event.h>
27 #include <ltt/trace.h>
29 #include <lttv/lttv.h>
30 #include <lttv/module.h>
31 #include <lttv/tracecontext.h>
32 #include <lttv/hook.h>
33 #include <lttv/state.h>
34 #include <lttv/stats.h>
36 #include <lttvwindow/lttvwindow.h>
37 #include <lttvwindow/lttvwindowtraces.h>
39 #include "hGuiStatisticInsert.xpm"
41 #define PATH_LENGTH 256 /* CHECK */
42 #define MAX_NUMBER_EVENT "max_number_event"
45 //static GPtrArray * statistic_traceset;
47 /** Array containing instanced objects. Used when module is unloaded */
48 static GSList
*g_statistic_viewer_data_list
= NULL
;
50 typedef struct _StatisticViewerData StatisticViewerData
;
52 static void request_background_data(StatisticViewerData
*svd
);
53 GtkWidget
*guistatistic_get_widget(StatisticViewerData
*svd
);
55 //! Statistic Viewer's constructor hook
56 GtkWidget
*h_gui_statistic(Tab
*tab
);
57 //! Statistic Viewer's constructor
58 StatisticViewerData
*gui_statistic(Tab
*tab
);
59 //! Statistic Viewer's destructor
60 void gui_statistic_destructor(StatisticViewerData
*statistic_viewer_data
);
62 static void tree_selection_changed_cb (GtkTreeSelection
*selection
, gpointer data
);
64 void statistic_destroy_hash_key(gpointer key
);
65 void statistic_destroy_hash_data(gpointer data
);
67 void show_traceset_stats(StatisticViewerData
* statistic_viewer_data
);
68 void show_tree(StatisticViewerData
* statistic_viewer_data
,
69 LttvAttribute
* stats
, GtkTreeIter
* parent
);
70 void show_statistic(StatisticViewerData
* statistic_viewer_data
,
71 LttvAttribute
* stats
, GtkTextBuffer
* buf
);
74 gboolean
statistic_traceset_changed(void * hook_data
, void * call_data
);
75 //void statistic_add_context_hooks(StatisticViewerData * statistic_viewer_data,
76 // LttvTracesetContext * tsc);
77 //void statistic_remove_context_hooks(StatisticViewerData *statistic_viewer_data,
78 // LttvTracesetContext * tsc);
80 //gboolean statistic_insert_traceset_stats(void * stats);
88 struct _StatisticViewerData
{
90 //LttvTracesetStats * stats;
93 //gboolean shown; //indicate if the statistic is shown or not
97 GtkTreeStore
* store_m
;
100 //scroll window containing Tree View
101 GtkWidget
* scroll_win_tree
;
104 //scroll window containing Text View
105 GtkWidget
* scroll_win_text
;
108 GtkTreeSelection
*select_c
;
111 GHashTable
*statistic_hash
;
117 /* Action to do when background computation completed.
119 * Eventually, will have to check that every requested traces are finished
120 * before doing the redraw. It will save unnecessary processor usage.
123 static gint
background_ready(void *hook_data
, void *call_data
)
125 StatisticViewerData
*svd
= (StatisticViewerData
*)hook_data
;
127 LttvTrace
*trace
= (LttvTrace
*)call_data
;
129 g_debug("statistics viewer : background computation data ready.");
131 gtk_tree_store_clear (svd
->store_m
);
133 lttv_stats_sum_traceset(lttvwindow_get_traceset_stats(tab
));
134 show_traceset_stats(svd
);
139 /* Request background computation. Verify if it is in progress or ready first.
141 * Right now, for all loaded traces.
143 * Later : must be only for each trace in the tab's traceset.
145 static void request_background_data(StatisticViewerData
*svd
)
147 gint num_traces
= lttvwindowtraces_get_number();
151 LttvHooks
*background_ready_hook
=
153 lttv_hooks_add(background_ready_hook
, background_ready
, svd
,
156 for(i
=0;i
<num_traces
;i
++) {
157 trace
= lttvwindowtraces_get_trace(i
);
159 if(lttvwindowtraces_get_ready(g_quark_from_string("stats"),trace
)==FALSE
) {
161 if(lttvwindowtraces_get_in_progress(g_quark_from_string("stats"),
163 /* We first remove requests that could have been done for the same
164 * information. Happens when two viewers ask for it before servicing
167 lttvwindowtraces_background_request_remove(trace
, "stats");
168 lttvwindowtraces_background_request_queue(trace
,
170 lttvwindowtraces_background_notify_queue(svd
,
174 background_ready_hook
);
175 } else { /* in progress */
177 lttvwindowtraces_background_notify_current(svd
,
181 background_ready_hook
);
186 lttv_hooks_call(background_ready_hook
, NULL
);
189 lttv_hooks_destroy(background_ready_hook
);
193 GtkWidget
*guistatistic_get_widget(StatisticViewerData
*svd
)
195 return svd
->hpaned_v
;
200 gui_statistic_destructor(StatisticViewerData
*statistic_viewer_data
)
202 Tab
*tab
= statistic_viewer_data
->tab
;
204 /* May already been done by GTK window closing */
205 if(GTK_IS_WIDGET(guistatistic_get_widget(statistic_viewer_data
))){
206 g_info("widget still exists");
209 lttvwindow_unregister_traceset_notify(statistic_viewer_data
->tab
,
210 statistic_traceset_changed
,
211 statistic_viewer_data
);
213 lttvwindowtraces_background_notify_remove(statistic_viewer_data
);
215 g_hash_table_destroy(statistic_viewer_data
->statistic_hash
);
216 g_statistic_viewer_data_list
=
217 g_slist_remove(g_statistic_viewer_data_list
, statistic_viewer_data
);
218 g_free(statistic_viewer_data
);
223 * Statistic Viewer's constructor hook
225 * This constructor is given as a parameter to the menuitem and toolbar button
226 * registration. It creates the list.
227 * @param parent_window A pointer to the parent window.
228 * @return The widget created.
231 h_gui_statistic(Tab
*tab
)
233 StatisticViewerData
* statistic_viewer_data
= gui_statistic(tab
) ;
235 if(statistic_viewer_data
)
236 return guistatistic_get_widget(statistic_viewer_data
);
242 gboolean
statistic_insert_traceset_stats(void * stats
)
247 len
= statistic_traceset
->len
;
249 s
= g_ptr_array_index(statistic_traceset
, i
);
250 if(s
== stats
) break;
253 g_ptr_array_add(statistic_traceset
, stats
);
261 * Statistic Viewer's constructor
263 * This constructor is used to create StatisticViewerData data structure.
264 * @return The Statistic viewer data created.
266 StatisticViewerData
*
267 gui_statistic(Tab
*tab
)
269 GtkCellRenderer
*renderer
;
270 GtkTreeViewColumn
*column
;
272 StatisticViewerData
* statistic_viewer_data
= g_new(StatisticViewerData
,1);
274 statistic_viewer_data
->tab
= tab
;
275 // statistic_viewer_data->stats =
276 // lttvwindow_get_traceset_stats(statistic_viewer_data->tab);
277 // statistic_viewer_data->calculate_stats =
278 // statistic_insert_traceset_stats((void *)statistic_viewer_data->stats);
280 lttvwindow_register_traceset_notify(statistic_viewer_data
->tab
,
281 statistic_traceset_changed
,
282 statistic_viewer_data
);
283 request_background_data(statistic_viewer_data
);
285 statistic_viewer_data
->statistic_hash
= g_hash_table_new_full(g_str_hash
,
287 statistic_destroy_hash_key
,
290 statistic_viewer_data
->hpaned_v
= gtk_hpaned_new();
291 statistic_viewer_data
->store_m
= gtk_tree_store_new (N_COLUMNS
, G_TYPE_STRING
);
292 statistic_viewer_data
->tree_v
=
293 gtk_tree_view_new_with_model (
294 GTK_TREE_MODEL (statistic_viewer_data
->store_m
));
295 g_object_unref (G_OBJECT (statistic_viewer_data
->store_m
));
297 // Setup the selection handler
298 statistic_viewer_data
->select_c
= gtk_tree_view_get_selection (GTK_TREE_VIEW (statistic_viewer_data
->tree_v
));
299 gtk_tree_selection_set_mode (statistic_viewer_data
->select_c
, GTK_SELECTION_SINGLE
);
300 g_signal_connect (G_OBJECT (statistic_viewer_data
->select_c
), "changed",
301 G_CALLBACK (tree_selection_changed_cb
),
302 statistic_viewer_data
);
304 renderer
= gtk_cell_renderer_text_new ();
305 column
= gtk_tree_view_column_new_with_attributes ("Statistic Name",
309 gtk_tree_view_column_set_alignment (column
, 0.0);
310 // gtk_tree_view_column_set_fixed_width (column, 45);
311 gtk_tree_view_append_column (GTK_TREE_VIEW (statistic_viewer_data
->tree_v
), column
);
314 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW (statistic_viewer_data
->tree_v
), FALSE
);
316 statistic_viewer_data
->scroll_win_tree
= gtk_scrolled_window_new (NULL
, NULL
);
317 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(statistic_viewer_data
->scroll_win_tree
),
318 GTK_POLICY_AUTOMATIC
,GTK_POLICY_AUTOMATIC
);
320 gtk_container_add (GTK_CONTAINER (statistic_viewer_data
->scroll_win_tree
), statistic_viewer_data
->tree_v
);
321 gtk_paned_pack1(GTK_PANED(statistic_viewer_data
->hpaned_v
),statistic_viewer_data
->scroll_win_tree
, TRUE
, FALSE
);
322 gtk_paned_set_position(GTK_PANED(statistic_viewer_data
->hpaned_v
), 160);
324 statistic_viewer_data
->scroll_win_text
= gtk_scrolled_window_new (NULL
, NULL
);
325 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(statistic_viewer_data
->scroll_win_text
),
326 GTK_POLICY_AUTOMATIC
,GTK_POLICY_AUTOMATIC
);
328 statistic_viewer_data
->text_v
= gtk_text_view_new ();
330 gtk_text_view_set_editable(GTK_TEXT_VIEW(statistic_viewer_data
->text_v
),FALSE
);
331 gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(statistic_viewer_data
->text_v
),FALSE
);
332 gtk_container_add (GTK_CONTAINER (statistic_viewer_data
->scroll_win_text
), statistic_viewer_data
->text_v
);
333 gtk_paned_pack2(GTK_PANED(statistic_viewer_data
->hpaned_v
), statistic_viewer_data
->scroll_win_text
, TRUE
, FALSE
);
335 gtk_widget_show(statistic_viewer_data
->scroll_win_tree
);
336 gtk_widget_show(statistic_viewer_data
->scroll_win_text
);
337 gtk_widget_show(statistic_viewer_data
->tree_v
);
338 gtk_widget_show(statistic_viewer_data
->text_v
);
339 gtk_widget_show(statistic_viewer_data
->hpaned_v
);
341 g_object_set_data_full(
342 G_OBJECT(guistatistic_get_widget(statistic_viewer_data
)),
343 "statistic_viewer_data",
344 statistic_viewer_data
,
345 (GDestroyNotify
)gui_statistic_destructor
);
347 /* Add the object's information to the module's array */
348 g_statistic_viewer_data_list
= g_slist_append(
349 g_statistic_viewer_data_list
,
350 statistic_viewer_data
);
352 return statistic_viewer_data
;
356 tree_selection_changed_cb (GtkTreeSelection
*selection
, gpointer data
)
358 StatisticViewerData
*statistic_viewer_data
= (StatisticViewerData
*)data
;
360 GtkTreeModel
*model
= GTK_TREE_MODEL(statistic_viewer_data
->store_m
);
365 GtkTextIter text_iter
;
366 LttvAttribute
* stats
;
368 if (gtk_tree_selection_get_selected (selection
, &model
, &iter
))
370 gtk_tree_model_get (model
, &iter
, NAME_COLUMN
, &event
, -1);
372 path
= gtk_tree_model_get_path(GTK_TREE_MODEL(model
),&iter
);
373 str
= gtk_tree_path_to_string (path
);
374 stats
= (LttvAttribute
*)g_hash_table_lookup (statistic_viewer_data
->statistic_hash
,str
);
377 buf
= gtk_text_view_get_buffer((GtkTextView
*)statistic_viewer_data
->text_v
);
378 gtk_text_buffer_set_text(buf
,"Statistic for '", -1);
379 gtk_text_buffer_get_end_iter(buf
, &text_iter
);
380 gtk_text_buffer_insert(buf
, &text_iter
, event
, strlen(event
));
381 gtk_text_buffer_get_end_iter(buf
, &text_iter
);
382 gtk_text_buffer_insert(buf
, &text_iter
, "' :\n\n",5);
384 show_statistic(statistic_viewer_data
, stats
, buf
);
390 void statistic_destroy_hash_key(gpointer key
)
402 void show_traceset_stats(StatisticViewerData
* statistic_viewer_data
)
404 Tab
*tab
= statistic_viewer_data
->tab
;
408 LttSystemDescription
*desc
;
409 LttvTracesetStats
* tscs
= lttvwindow_get_traceset_stats(tab
);
410 gchar
* str
, trace_str
[PATH_LENGTH
];
413 GtkTreeStore
* store
= statistic_viewer_data
->store_m
;
415 if(tscs
->stats
== NULL
) return;
417 lttv_attribute_write_xml(tscs
->stats
, stdout
, 1, 4);
420 ts
= tscs
->parent
.parent
.ts
;
421 nb
= lttv_traceset_number(ts
);
424 gtk_tree_store_append (store
, &iter
, NULL
);
425 gtk_tree_store_set (store
, &iter
,
426 NAME_COLUMN
, "Traceset statistics",
428 path
= gtk_tree_model_get_path(GTK_TREE_MODEL(store
), &iter
);
429 str
= gtk_tree_path_to_string (path
);
430 g_hash_table_insert(statistic_viewer_data
->statistic_hash
,
431 (gpointer
)str
, tscs
->stats
);
432 show_tree(statistic_viewer_data
, tscs
->stats
, &iter
);
434 //show stats for all traces
435 for(i
= 0 ; i
< nb
; i
++) {
436 tcs
= (LttvTraceStats
*)(LTTV_TRACESET_CONTEXT(tscs
)->traces
[i
]);
437 desc
= ltt_trace_system_description(tcs
->parent
.parent
.t
);
438 LttTime start_time
= ltt_trace_system_description_trace_start_time(desc
);
439 sprintf(trace_str
, "Trace on system %s at time %lu.%09lu",
440 ltt_trace_system_description_node_name(desc
),
444 gtk_tree_store_append (store
, &iter
, NULL
);
445 gtk_tree_store_set (store
, &iter
,NAME_COLUMN
,trace_str
,-1);
446 path
= gtk_tree_model_get_path(GTK_TREE_MODEL(store
), &iter
);
447 str
= gtk_tree_path_to_string (path
);
448 g_hash_table_insert(statistic_viewer_data
->statistic_hash
,
449 (gpointer
)str
,tcs
->stats
);
450 show_tree(statistic_viewer_data
, tcs
->stats
, &iter
);
452 lttv_attribute_write_xml(tcs
->stats
, stdout
, 3, 4);
457 void show_tree(StatisticViewerData
* statistic_viewer_data
,
458 LttvAttribute
* stats
, GtkTreeIter
* parent
)
461 LttvAttribute
*subtree
;
462 LttvAttributeName name
;
463 LttvAttributeValue value
;
464 LttvAttributeType type
;
465 gchar
* str
, dir_str
[PATH_LENGTH
];
468 GtkTreeStore
* store
= statistic_viewer_data
->store_m
;
470 nb
= lttv_attribute_get_number(stats
);
471 for(i
= 0 ; i
< nb
; i
++) {
472 type
= lttv_attribute_get(stats
, i
, &name
, &value
);
475 if(LTTV_IS_ATTRIBUTE(*(value
.v_gobject
))) {
476 sprintf(dir_str
, "%s", g_quark_to_string(name
));
477 subtree
= (LttvAttribute
*)*(value
.v_gobject
);
478 gtk_tree_store_append (store
, &iter
, parent
);
479 gtk_tree_store_set (store
, &iter
,NAME_COLUMN
,dir_str
,-1);
480 path
= gtk_tree_model_get_path(GTK_TREE_MODEL(store
), &iter
);
481 str
= gtk_tree_path_to_string (path
);
482 g_hash_table_insert(statistic_viewer_data
->statistic_hash
,
483 (gpointer
)str
, subtree
);
484 show_tree(statistic_viewer_data
, subtree
, &iter
);
493 void show_statistic(StatisticViewerData
* statistic_viewer_data
,
494 LttvAttribute
* stats
, GtkTextBuffer
* buf
)
497 LttvAttributeName name
;
498 LttvAttributeValue value
;
499 LttvAttributeType type
;
500 gchar type_name
[PATH_LENGTH
], type_value
[PATH_LENGTH
];
501 GtkTextIter text_iter
;
504 nb
= lttv_attribute_get_number(stats
);
505 for(i
= 0 ; i
< nb
; i
++) {
506 type
= lttv_attribute_get(stats
, i
, &name
, &value
);
507 sprintf(type_name
,"%s", g_quark_to_string(name
));
508 type_value
[0] = '\0';
511 sprintf(type_value
, " : %d\n", *value
.v_int
);
514 sprintf(type_value
, " : %u\n", *value
.v_uint
);
517 sprintf(type_value
, " : %ld\n", *value
.v_long
);
520 sprintf(type_value
, " : %lu\n", *value
.v_ulong
);
523 sprintf(type_value
, " : %f\n", (double)*value
.v_float
);
526 sprintf(type_value
, " : %f\n", *value
.v_double
);
529 sprintf(type_value
, " : %10lu.%09lu\n", value
.v_time
->tv_sec
,
530 value
.v_time
->tv_nsec
);
533 sprintf(type_value
, " : POINTER\n");
536 sprintf(type_value
, " : %s\n", *value
.v_string
);
541 if(strlen(type_value
)){
543 strcat(type_name
,type_value
);
544 gtk_text_buffer_get_end_iter(buf
, &text_iter
);
545 gtk_text_buffer_insert(buf
, &text_iter
, type_name
, strlen(type_name
));
550 sprintf(type_value
, "No statistic information in this directory.\nCheck in subdirectories please.\n");
551 gtk_text_buffer_get_end_iter(buf
, &text_iter
);
552 gtk_text_buffer_insert(buf
, &text_iter
, type_value
, strlen(type_value
));
557 gboolean
statistic_traceset_changed(void * hook_data
, void * call_data
)
559 StatisticViewerData
*statistic_viewer_data
= (StatisticViewerData
*) hook_data
;
561 request_background_data(statistic_viewer_data
);
567 void statistic_add_context_hooks(StatisticViewerData
* statistic_viewer_data
,
568 LttvTracesetContext
* tsc
)
570 gint i
, j
, nbi
, nb_tracefile
;
572 LttvTraceContext
*tc
;
573 LttvTracefileContext
*tfc
;
574 LttvTracesetSelector
* ts_s
;
575 LttvTraceSelector
* t_s
;
576 LttvTracefileSelector
* tf_s
;
579 ts_s
= (LttvTracesetSelector
*)g_object_get_data(G_OBJECT(statistic_viewer_data
->hpaned_v
),
580 statistic_viewer_data
->filter_key
);
582 //if there are hooks for traceset, add them here
584 nbi
= lttv_traceset_number(tsc
->ts
);
585 for(i
= 0 ; i
< nbi
; i
++) {
586 t_s
= lttv_traceset_selector_trace_get(ts_s
,i
);
587 selected
= lttv_trace_selector_get_selected(t_s
);
588 if(!selected
) continue;
591 //if there are hooks for trace, add them here
593 nb_tracefile
= ltt_trace_control_tracefile_number(trace
) +
594 ltt_trace_per_cpu_tracefile_number(trace
);
596 for(j
= 0 ; j
< nb_tracefile
; j
++) {
597 tf_s
= lttv_trace_selector_tracefile_get(t_s
,j
);
598 selected
= lttv_tracefile_selector_get_selected(tf_s
);
599 if(!selected
) continue;
600 tfc
= tc
->tracefiles
[j
];
602 //if there are hooks for tracefile, add them here
603 // lttv_tracefile_context_add_hooks(tfc, NULL,NULL,NULL,NULL,
604 // statistic_viewer_data->before_event_hooks,NULL);
608 lttv_stats_add_event_hooks(LTTV_TRACESET_STATS(tsc
));
612 void statistic_remove_context_hooks(StatisticViewerData
* statistic_viewer_data
,
613 LttvTracesetContext
* tsc
)
615 gint i
, j
, nbi
, nb_tracefile
;
617 LttvTraceContext
*tc
;
618 LttvTracefileContext
*tfc
;
619 LttvTracesetSelector
* ts_s
;
620 LttvTraceSelector
* t_s
;
621 LttvTracefileSelector
* tf_s
;
624 ts_s
= (LttvTracesetSelector
*)g_object_get_data(G_OBJECT(statistic_viewer_data
->hpaned_v
),
625 statistic_viewer_data
->filter_key
);
627 //if there are hooks for traceset, remove them here
629 nbi
= lttv_traceset_number(tsc
->ts
);
630 for(i
= 0 ; i
< nbi
; i
++) {
631 t_s
= lttv_traceset_selector_trace_get(ts_s
,i
);
632 selected
= lttv_trace_selector_get_selected(t_s
);
633 if(!selected
) continue;
636 //if there are hooks for trace, remove them here
638 nb_tracefile
= ltt_trace_control_tracefile_number(trace
) +
639 ltt_trace_per_cpu_tracefile_number(trace
);
641 for(j
= 0 ; j
< nb_tracefile
; j
++) {
642 tf_s
= lttv_trace_selector_tracefile_get(t_s
,j
);
643 selected
= lttv_tracefile_selector_get_selected(tf_s
);
644 if(!selected
) continue;
645 tfc
= tc
->tracefiles
[j
];
647 //if there are hooks for tracefile, remove them here
648 // lttv_tracefile_context_remove_hooks(tfc, NULL,NULL,NULL,NULL,
649 // statistic_viewer_data->before_event_hooks,NULL);
653 lttv_stats_remove_event_hooks(LTTV_TRACESET_STATS(tsc
));
658 * plugin's init function
660 * This function initializes the Statistic Viewer functionnality through the
665 lttvwindow_register_constructor("guistatistics",
667 "Insert Statistic Viewer",
668 hGuiStatisticInsert_xpm
,
669 "Insert Statistic Viewer",
673 void statistic_destroy_walk(gpointer data
, gpointer user_data
)
675 StatisticViewerData
*svd
= (StatisticViewerData
*)data
;
677 g_debug("CFV.c : statistic_destroy_walk, %p", svd
);
678 /* May already have been done by GTK window closing */
679 if(GTK_IS_WIDGET(guistatistic_get_widget(svd
)))
680 gtk_widget_destroy(guistatistic_get_widget(svd
));
684 * plugin's destroy function
686 * This function releases the memory reserved by the module and unregisters
687 * everything that has been registered in the gtkTraceSet API.
689 static void destroy() {
691 g_slist_foreach(g_statistic_viewer_data_list
, statistic_destroy_walk
, NULL
);
692 g_slist_free(g_statistic_viewer_data_list
);
694 lttvwindow_unregister_constructor(h_gui_statistic
);
699 LTTV_MODULE("guistatistics", "Statistics viewer", \
700 "Graphical module to view statistics about processes, CPUs and systems", \
701 init
, destroy
, "lttvwindow")