quit menu complete
[lttv.git] / ltt / branches / poly / lttv / modules / guiStatistic / guiStatistic.c
CommitLineData
6b1d3120 1#include <glib.h>
2#include <gmodule.h>
3#include <gtk/gtk.h>
4#include <gdk/gdk.h>
5
6#include <lttv/module.h>
7#include <lttv/gtkTraceSet.h>
8#include <lttv/processTrace.h>
9#include <lttv/hook.h>
10#include <lttv/common.h>
11#include <lttv/state.h>
12#include <lttv/stats.h>
13
14#include <ltt/ltt.h>
15#include <ltt/event.h>
16#include <ltt/type.h>
17#include <ltt/trace.h>
18
19#include <string.h>
20
21#include "../icons/hGuiStatisticInsert.xpm"
22
23#define PATH_LENGTH 256
24
25static LttvModule *Main_Win_Module;
26
27/** Array containing instanced objects. Used when module is unloaded */
28GSList *gStatistic_Viewer_Data_List = NULL ;
29
30typedef struct _StatisticViewerData StatisticViewerData;
31
32//! Statistic Viewer's constructor hook
33GtkWidget *hGuiStatistic(mainWindow *pmParentWindow);
34//! Statistic Viewer's constructor
35StatisticViewerData *GuiStatistic(mainWindow *pmParentWindow);
36//! Statistic Viewer's destructor
37void GuiStatistic_Destructor(StatisticViewerData *Statistic_Viewer_Data);
38void GuiStatistic_free(StatisticViewerData *Statistic_Viewer_Data);
39
40void grab_focus(GtkWidget *widget, gpointer data);
41static void tree_selection_changed_cb (GtkTreeSelection *selection, gpointer data);
42
43void Destroy_hash_key(gpointer key);
44void Destroy_hash_data(gpointer data);
45
46void get_traceset_stats(StatisticViewerData * Statistic_Viewer_Data);
47void show_traceset_stats(StatisticViewerData * Statistic_Viewer_Data);
48void show_tree(StatisticViewerData * Statistic_Viewer_Data,
49 LttvAttribute* stats, GtkTreeIter* parent);
50void show_statistic(StatisticViewerData * Statistic_Viewer_Data,
51 LttvAttribute* stats, GtkTextBuffer* buf);
52
53
54enum
55{
56 NAME_COLUMN,
57 N_COLUMNS
58};
59
60struct _StatisticViewerData{
61 mainWindow * mw;
62 LttvTracesetStats * stats;
63
64 GtkWidget * HPaned_V;
65 GtkTreeStore * Store_M;
66 GtkWidget * Tree_V;
67
68 //scroll window containing Tree View
69 GtkWidget * Scroll_Win_Tree;
70
71 GtkWidget * Text_V;
72 //scroll window containing Text View
73 GtkWidget * Scroll_Win_Text;
74
75 // Selection handler
76 GtkTreeSelection *Select_C;
77
78 //hash
79 GHashTable *Statistic_Hash;
80};
81
82
83/**
84 * plugin's init function
85 *
86 * This function initializes the Statistic Viewer functionnality through the
87 * gtkTraceSet API.
88 */
89G_MODULE_EXPORT void init(LttvModule *self, int argc, char *argv[]) {
90
91 Main_Win_Module = lttv_module_require(self, "mainwin", argc, argv);
92
93 if(Main_Win_Module == NULL){
94 g_critical("Can't load Statistic Viewer : missing mainwin\n");
95 return;
96 }
97
98 g_critical("GUI Statistic Viewer init()");
99
100 /* Register the toolbar insert button */
101 ToolbarItemReg(hGuiStatisticInsert_xpm, "Insert Statistic Viewer", hGuiStatistic);
102
103 /* Register the menu item insert entry */
104 MenuItemReg("/", "Insert Statistic Viewer", hGuiStatistic);
105
106}
107
108void destroy_walk(gpointer data, gpointer user_data)
109{
110 GuiStatistic_Destructor((StatisticViewerData*)data);
111}
112
113/**
114 * plugin's destroy function
115 *
116 * This function releases the memory reserved by the module and unregisters
117 * everything that has been registered in the gtkTraceSet API.
118 */
119G_MODULE_EXPORT void destroy() {
120 int i;
121
122 StatisticViewerData *Statistic_Viewer_Data;
123
124 g_critical("GUI Statistic Viewer destroy()");
125
126 g_slist_foreach(gStatistic_Viewer_Data_List, destroy_walk, NULL );
127
128 g_slist_free(gStatistic_Viewer_Data_List);
129
130 /* Unregister the toolbar insert button */
131 ToolbarItemUnreg(hGuiStatistic);
132
133 /* Unregister the menu item insert entry */
134 MenuItemUnreg(hGuiStatistic);
135}
136
137
138void
139GuiStatistic_free(StatisticViewerData *Statistic_Viewer_Data)
140{
7a859036 141 if(Statistic_Viewer_Data){
142 g_hash_table_destroy(Statistic_Viewer_Data->Statistic_Hash);
143 gStatistic_Viewer_Data_List = g_slist_remove(gStatistic_Viewer_Data_List, Statistic_Viewer_Data);
144 g_warning("Delete Statistic data\n");
145 g_free(Statistic_Viewer_Data);
146 }
6b1d3120 147}
148
149void
150GuiStatistic_Destructor(StatisticViewerData *Statistic_Viewer_Data)
151{
152 /* May already been done by GTK window closing */
7a859036 153 if(GTK_IS_WIDGET(Statistic_Viewer_Data->HPaned_V)){
6b1d3120 154 gtk_widget_destroy(Statistic_Viewer_Data->HPaned_V);
7a859036 155 Statistic_Viewer_Data = NULL;
156 }
6b1d3120 157
158 GuiStatistic_free(Statistic_Viewer_Data);
159}
160
161
162/**
163 * Statistic Viewer's constructor hook
164 *
165 * This constructor is given as a parameter to the menuitem and toolbar button
166 * registration. It creates the list.
167 * @param pmParentWindow A pointer to the parent window.
168 * @return The widget created.
169 */
170GtkWidget *
171hGuiStatistic(mainWindow * pmParentWindow)
172{
173 StatisticViewerData* Statistic_Viewer_Data = GuiStatistic(pmParentWindow) ;
174
175 if(Statistic_Viewer_Data)
176 return Statistic_Viewer_Data->HPaned_V;
177 else return NULL;
178
179}
180
181/**
182 * Statistic Viewer's constructor
183 *
184 * This constructor is used to create StatisticViewerData data structure.
185 * @return The Statistic viewer data created.
186 */
187StatisticViewerData *
188GuiStatistic(mainWindow *pmParentWindow)
189{
190 GtkCellRenderer *renderer;
191 GtkTreeViewColumn *column;
192
193 StatisticViewerData* Statistic_Viewer_Data = g_new(StatisticViewerData,1);
194
195 Statistic_Viewer_Data->mw = pmParentWindow;
196 Statistic_Viewer_Data->stats = getTracesetStats(Statistic_Viewer_Data->mw);
197
198 Statistic_Viewer_Data->Statistic_Hash = g_hash_table_new_full(g_str_hash, g_str_equal,
199 Destroy_hash_key, Destroy_hash_data);
200
201 Statistic_Viewer_Data->HPaned_V = gtk_hpaned_new();
202 Statistic_Viewer_Data->Store_M = gtk_tree_store_new (N_COLUMNS, G_TYPE_STRING);
203 Statistic_Viewer_Data->Tree_V = gtk_tree_view_new_with_model (GTK_TREE_MODEL (Statistic_Viewer_Data->Store_M));
204 g_object_unref (G_OBJECT (Statistic_Viewer_Data->Store_M));
205
206 g_signal_connect (G_OBJECT (Statistic_Viewer_Data->Tree_V), "grab-focus",
207 G_CALLBACK (grab_focus),
208 Statistic_Viewer_Data);
209
210 // Setup the selection handler
211 Statistic_Viewer_Data->Select_C = gtk_tree_view_get_selection (GTK_TREE_VIEW (Statistic_Viewer_Data->Tree_V));
212 gtk_tree_selection_set_mode (Statistic_Viewer_Data->Select_C, GTK_SELECTION_SINGLE);
213 g_signal_connect (G_OBJECT (Statistic_Viewer_Data->Select_C), "changed",
214 G_CALLBACK (tree_selection_changed_cb),
215 Statistic_Viewer_Data);
216
217 renderer = gtk_cell_renderer_text_new ();
218 column = gtk_tree_view_column_new_with_attributes ("Statistic Name",
219 renderer,
220 "text", NAME_COLUMN,
221 NULL);
222 gtk_tree_view_column_set_alignment (column, 0.0);
223 // gtk_tree_view_column_set_fixed_width (column, 45);
224 gtk_tree_view_append_column (GTK_TREE_VIEW (Statistic_Viewer_Data->Tree_V), column);
225
226
227 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW (Statistic_Viewer_Data->Tree_V), FALSE);
228
229 Statistic_Viewer_Data->Scroll_Win_Tree = gtk_scrolled_window_new (NULL, NULL);
230 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(Statistic_Viewer_Data->Scroll_Win_Tree),
231 GTK_POLICY_AUTOMATIC,GTK_POLICY_AUTOMATIC);
232
233 gtk_container_add (GTK_CONTAINER (Statistic_Viewer_Data->Scroll_Win_Tree), Statistic_Viewer_Data->Tree_V);
234 gtk_paned_pack1(GTK_PANED(Statistic_Viewer_Data->HPaned_V),Statistic_Viewer_Data->Scroll_Win_Tree, TRUE, FALSE);
235 gtk_paned_set_position(GTK_PANED(Statistic_Viewer_Data->HPaned_V), 160);
236
237 Statistic_Viewer_Data->Scroll_Win_Text = gtk_scrolled_window_new (NULL, NULL);
238 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(Statistic_Viewer_Data->Scroll_Win_Text),
239 GTK_POLICY_AUTOMATIC,GTK_POLICY_AUTOMATIC);
240
241 Statistic_Viewer_Data->Text_V = gtk_text_view_new ();
242 g_signal_connect (G_OBJECT (Statistic_Viewer_Data->Text_V), "grab-focus",
243 G_CALLBACK (grab_focus),
244 Statistic_Viewer_Data);
245
246 gtk_text_view_set_editable(GTK_TEXT_VIEW(Statistic_Viewer_Data->Text_V),FALSE);
247 gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(Statistic_Viewer_Data->Text_V),FALSE);
248 gtk_container_add (GTK_CONTAINER (Statistic_Viewer_Data->Scroll_Win_Text), Statistic_Viewer_Data->Text_V);
249 gtk_paned_pack2(GTK_PANED(Statistic_Viewer_Data->HPaned_V), Statistic_Viewer_Data->Scroll_Win_Text, TRUE, FALSE);
250
251 gtk_widget_show(Statistic_Viewer_Data->Scroll_Win_Tree);
252 gtk_widget_show(Statistic_Viewer_Data->Scroll_Win_Text);
253 gtk_widget_show(Statistic_Viewer_Data->Tree_V);
254 gtk_widget_show(Statistic_Viewer_Data->Text_V);
255 gtk_widget_show(Statistic_Viewer_Data->HPaned_V);
256
257 g_object_set_data_full(
258 G_OBJECT(Statistic_Viewer_Data->HPaned_V),
259 "Statistic_Viewer_Data",
260 Statistic_Viewer_Data,
261 (GDestroyNotify)GuiStatistic_free);
262
263 /* Add the object's information to the module's array */
264 gStatistic_Viewer_Data_List = g_slist_append(gStatistic_Viewer_Data_List, Statistic_Viewer_Data);
265
266 get_traceset_stats(Statistic_Viewer_Data);
267
268 return Statistic_Viewer_Data;
269}
270
271void grab_focus(GtkWidget *widget, gpointer data)
272{
273 StatisticViewerData *Statistic_Viewer_Data = (StatisticViewerData *)data;
274 mainWindow * mw = Statistic_Viewer_Data->mw;
275 SetFocusedPane(mw, gtk_widget_get_parent(Statistic_Viewer_Data->HPaned_V));
276}
277
278static void
279tree_selection_changed_cb (GtkTreeSelection *selection, gpointer data)
280{
281 StatisticViewerData *Statistic_Viewer_Data = (StatisticViewerData*)data;
282 GtkTreeIter iter;
283 GtkTreeModel *model = GTK_TREE_MODEL(Statistic_Viewer_Data->Store_M);
284 gchar *Event;
285 GtkTextBuffer* buf;
286 gchar * str;
287 GtkTreePath * path;
288 GtkTextIter text_iter;
289 LttvAttribute * stats;
290
291 if (gtk_tree_selection_get_selected (selection, &model, &iter))
292 {
293 gtk_tree_model_get (model, &iter, NAME_COLUMN, &Event, -1);
294
295 path = gtk_tree_model_get_path(GTK_TREE_MODEL(model),&iter);
296 str = gtk_tree_path_to_string (path);
297 stats = (LttvAttribute*)g_hash_table_lookup (Statistic_Viewer_Data->Statistic_Hash,str);
298 g_free(str);
299
300 buf = gtk_text_view_get_buffer((GtkTextView*)Statistic_Viewer_Data->Text_V);
301 gtk_text_buffer_set_text(buf,"Statistic for '", -1);
302 gtk_text_buffer_get_end_iter(buf, &text_iter);
303 gtk_text_buffer_insert(buf, &text_iter, Event, strlen(Event));
304 gtk_text_buffer_get_end_iter(buf, &text_iter);
305 gtk_text_buffer_insert(buf, &text_iter, "' :\n\n",5);
306
307 show_statistic(Statistic_Viewer_Data, stats, buf);
308
309 g_free (Event);
310 }
311}
312
313void Destroy_hash_key(gpointer key)
314{
315 g_free(key);
316}
317
318void Destroy_hash_data(gpointer data)
319{
320 // g_free(data);
321}
322
323void get_traceset_stats(StatisticViewerData * Statistic_Viewer_Data)
324{
325 LttTime start, end;
326
327 start.tv_sec = 0;
328 start.tv_nsec = 0;
329 end.tv_sec = G_MAXULONG;
330 end.tv_nsec = G_MAXULONG;
331
332 stateAddEventHooks(Statistic_Viewer_Data->mw);
333 statsAddEventHooks(Statistic_Viewer_Data->mw);
334
335 processTraceset(Statistic_Viewer_Data->mw, start, end, G_MAXULONG);
336
337 stateRemoveEventHooks(Statistic_Viewer_Data->mw);
338 statsRemoveEventHooks(Statistic_Viewer_Data->mw);
339
340 //establish tree view for stats
341 show_traceset_stats(Statistic_Viewer_Data);
342}
343
344void show_traceset_stats(StatisticViewerData * Statistic_Viewer_Data)
345{
346 int i, nb;
347 LttvTraceset *ts;
348 LttvTraceStats *tcs;
349 LttSystemDescription *desc;
350 LttvTracesetStats * tscs = Statistic_Viewer_Data->stats;
351 gchar * str, trace_str[PATH_LENGTH];
352 GtkTreePath * path;
353 GtkTreeIter iter;
354 GtkTreeStore * store = Statistic_Viewer_Data->Store_M;
355
356 if(tscs->stats == NULL) return;
357
358 gtk_tree_store_append (store, &iter, NULL);
359 gtk_tree_store_set (store, &iter,
360 NAME_COLUMN, "Traceset statistics",
361 -1);
362 path = gtk_tree_model_get_path(GTK_TREE_MODEL(store), &iter);
363 str = gtk_tree_path_to_string (path);
364 g_hash_table_insert(Statistic_Viewer_Data->Statistic_Hash,
365 (gpointer)str, tscs->stats);
366 show_tree(Statistic_Viewer_Data, tscs->stats, &iter);
367
368 //show stats for all traces
369 ts = tscs->parent.parent.ts;
370 nb = lttv_traceset_number(ts);
371
372 for(i = 0 ; i < nb ; i++) {
373 tcs = (LttvTraceStats *)(LTTV_TRACESET_CONTEXT(tscs)->traces[i]);
374 desc = ltt_trace_system_description(tcs->parent.parent.t);
375 sprintf(trace_str, "Trace on system %s at time %d secs",
376 desc->node_name,desc->trace_start.tv_sec);
377
378 gtk_tree_store_append (store, &iter, NULL);
379 gtk_tree_store_set (store, &iter,NAME_COLUMN,trace_str,-1);
380 path = gtk_tree_model_get_path(GTK_TREE_MODEL(store), &iter);
381 str = gtk_tree_path_to_string (path);
382 g_hash_table_insert(Statistic_Viewer_Data->Statistic_Hash,
383 (gpointer)str,tcs->stats);
384 show_tree(Statistic_Viewer_Data, tcs->stats, &iter);
385 }
386}
387
388void show_tree(StatisticViewerData * Statistic_Viewer_Data,
389 LttvAttribute* stats, GtkTreeIter* parent)
390{
391 int i, nb;
392 LttvAttribute *subtree;
393 LttvAttributeName name;
394 LttvAttributeValue value;
395 LttvAttributeType type;
396 gchar * str, dir_str[PATH_LENGTH];
397 GtkTreePath * path;
398 GtkTreeIter iter;
399 GtkTreeStore * store = Statistic_Viewer_Data->Store_M;
400
401 nb = lttv_attribute_get_number(stats);
402 for(i = 0 ; i < nb ; i++) {
403 type = lttv_attribute_get(stats, i, &name, &value);
404 switch(type) {
405 case LTTV_GOBJECT:
406 if(LTTV_IS_ATTRIBUTE(*(value.v_gobject))) {
407 sprintf(dir_str, "%s", g_quark_to_string(name));
408 subtree = (LttvAttribute *)*(value.v_gobject);
409 gtk_tree_store_append (store, &iter, parent);
410 gtk_tree_store_set (store, &iter,NAME_COLUMN,dir_str,-1);
411 path = gtk_tree_model_get_path(GTK_TREE_MODEL(store), &iter);
412 str = gtk_tree_path_to_string (path);
413 g_hash_table_insert(Statistic_Viewer_Data->Statistic_Hash,
414 (gpointer)str, subtree);
415 show_tree(Statistic_Viewer_Data, subtree, &iter);
416 }
417 break;
418 default:
419 break;
420 }
421 }
422}
423
424void show_statistic(StatisticViewerData * Statistic_Viewer_Data,
425 LttvAttribute* stats, GtkTextBuffer* buf)
426{
427 int i, nb , flag;
428 LttvAttribute *subtree;
429 LttvAttributeName name;
430 LttvAttributeValue value;
431 LttvAttributeType type;
432 gchar type_name[PATH_LENGTH], type_value[PATH_LENGTH];
433 GtkTextIter text_iter;
434
435 flag = 0;
436 nb = lttv_attribute_get_number(stats);
437 for(i = 0 ; i < nb ; i++) {
438 type = lttv_attribute_get(stats, i, &name, &value);
439 sprintf(type_name,"%s", g_quark_to_string(name));
440 type_value[0] = '\0';
441 switch(type) {
442 case LTTV_INT:
443 sprintf(type_value, " : %d\n", *value.v_int);
444 break;
445 case LTTV_UINT:
446 sprintf(type_value, " : %u\n", *value.v_uint);
447 break;
448 case LTTV_LONG:
449 sprintf(type_value, " : %ld\n", *value.v_long);
450 break;
451 case LTTV_ULONG:
452 sprintf(type_value, " : %lu\n", *value.v_ulong);
453 break;
454 case LTTV_FLOAT:
455 sprintf(type_value, " : %f\n", (double)*value.v_float);
456 break;
457 case LTTV_DOUBLE:
458 sprintf(type_value, " : %f\n", *value.v_double);
459 break;
460 case LTTV_TIME:
461 sprintf(type_value, " : %10u.%09u\n", value.v_time->tv_sec,
462 value.v_time->tv_nsec);
463 break;
464 case LTTV_POINTER:
465 sprintf(type_value, " : POINTER\n");
466 break;
467 case LTTV_STRING:
468 sprintf(type_value, " : %s\n", *value.v_string);
469 break;
470 default:
471 break;
472 }
473 if(strlen(type_value)){
474 flag = 1;
475 strcat(type_name,type_value);
476 gtk_text_buffer_get_end_iter(buf, &text_iter);
477 gtk_text_buffer_insert(buf, &text_iter, type_name, strlen(type_name));
478 }
479 }
480
481 if(flag == 0){
482 sprintf(type_value, "No statistic information in this directory.\nCheck in subdirectories please.\n");
483 gtk_text_buffer_get_end_iter(buf, &text_iter);
484 gtk_text_buffer_insert(buf, &text_iter, type_value, strlen(type_value));
485
486 }
487}
488
489
This page took 0.040542 seconds and 4 git commands to generate.