From : Didier Gautheron
[obnox/wireshark/wip.git] / gtk / service_response_time_table.c
1 /* service_response_time_table.c
2  * service_response_time_table   2003 Ronnie Sahlberg
3  * Helper routines common to all service response time statistics
4  * tap.
5  *
6  * $Id$
7  *
8  * Wireshark - Network traffic analyzer
9  * By Gerald Combs <gerald@wireshark.org>
10  * Copyright 1998 Gerald Combs
11  * 
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  * 
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  * 
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25  */
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <gtk/gtk.h>
32
33 #include "epan/packet_info.h"
34
35 #include "../simple_dialog.h"
36 #include "../globals.h"
37
38 #include "gtk/service_response_time_table.h"
39 #include "gtk/filter_utils.h"
40 #include "gtk/gui_utils.h"
41
42 enum
43 {
44    INDEX_COLUMN,
45    PROCEDURE_COLUMN,
46    CALLS_COLUMN,
47    MIN_SRT_COLUMN,
48    MAX_SRT_COLUMN,
49    AVG_SRT_COLUMN,
50    N_COLUMNS
51 };
52
53
54 static void
55 srt_select_filter_cb(GtkWidget *widget _U_, gpointer callback_data, guint callback_action)
56 {
57         srt_stat_table *rst = (srt_stat_table *)callback_data;
58         char *str = NULL;
59         GtkTreeIter iter;
60         GtkTreeModel *model;
61         GtkTreeSelection  *sel;
62         int selection;
63
64         if(rst->filter_string==NULL){
65                 return;
66         }
67
68         sel = gtk_tree_view_get_selection (GTK_TREE_VIEW(rst->table));
69
70         if (!gtk_tree_selection_get_selected(sel, &model, &iter))
71                 return;
72
73         gtk_tree_model_get (model, &iter, INDEX_COLUMN, &selection, -1);
74         if(selection>=(int)rst->num_procs){
75                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "No procedure selected");
76                 return;
77         }
78
79         str = g_strdup_printf("%s==%d", rst->filter_string, selection);
80
81         apply_selected_filter (callback_action, str);
82
83         g_free(str);
84 }
85
86 static gint
87 srt_show_popup_menu_cb(void *widg _U_, GdkEvent *event, srt_stat_table *rst)
88 {
89         GdkEventButton *bevent = (GdkEventButton *)event;
90
91         if(event->type==GDK_BUTTON_PRESS && bevent->button==3){
92                 gtk_menu_popup(GTK_MENU(rst->menu), NULL, NULL, NULL, NULL, 
93                         bevent->button, bevent->time);
94         }
95
96         return FALSE;
97 }
98
99 static GtkItemFactoryEntry srt_list_menu_items[] =
100 {
101         /* Match */
102         {"/Apply as Filter", NULL, NULL, 0, "<Branch>", NULL,},
103         {"/Apply as Filter/Selected", NULL,
104                 GTK_MENU_FUNC(srt_select_filter_cb), CALLBACK_MATCH(ACTYPE_SELECTED, 0), NULL, NULL,},
105         {"/Apply as Filter/... not Selected", NULL,
106                 GTK_MENU_FUNC(srt_select_filter_cb), CALLBACK_MATCH(ACTYPE_NOT_SELECTED, 0), NULL, NULL,},
107         {"/Apply as Filter/.. and Selected", NULL,
108                 GTK_MENU_FUNC(srt_select_filter_cb), CALLBACK_MATCH(ACTYPE_AND_SELECTED, 0), NULL, NULL,},
109         {"/Apply as Filter/... or Selected", NULL,
110                 GTK_MENU_FUNC(srt_select_filter_cb), CALLBACK_MATCH(ACTYPE_OR_SELECTED, 0), NULL, NULL,},
111         {"/Apply as Filter/... and not Selected", NULL,
112                 GTK_MENU_FUNC(srt_select_filter_cb), CALLBACK_MATCH(ACTYPE_AND_NOT_SELECTED, 0), NULL, NULL,},
113         {"/Apply as Filter/... or not Selected", NULL,
114                 GTK_MENU_FUNC(srt_select_filter_cb), CALLBACK_MATCH(ACTYPE_OR_NOT_SELECTED, 0), NULL, NULL,},
115
116         /* Prepare */
117         {"/Prepare a Filter", NULL, NULL, 0, "<Branch>", NULL,},
118         {"/Prepare a Filter/Selected", NULL,
119                 GTK_MENU_FUNC(srt_select_filter_cb), CALLBACK_PREPARE(ACTYPE_SELECTED, 0), NULL, NULL,},
120         {"/Prepare a Filter/Not Selected", NULL,
121                 GTK_MENU_FUNC(srt_select_filter_cb), CALLBACK_PREPARE(ACTYPE_NOT_SELECTED, 0), NULL, NULL,},
122         {"/Prepare a Filter/... and Selected", NULL,
123                 GTK_MENU_FUNC(srt_select_filter_cb), CALLBACK_PREPARE(ACTYPE_AND_SELECTED, 0), NULL, NULL,},
124         {"/Prepare a Filter/... or Selected", NULL,
125                 GTK_MENU_FUNC(srt_select_filter_cb), CALLBACK_PREPARE(ACTYPE_OR_SELECTED, 0), NULL, NULL,},
126         {"/Prepare a Filter/... and not Selected", NULL,
127                 GTK_MENU_FUNC(srt_select_filter_cb), CALLBACK_PREPARE(ACTYPE_AND_NOT_SELECTED, 0), NULL, NULL,},
128         {"/Prepare a Filter/... or not Selected", NULL,
129                 GTK_MENU_FUNC(srt_select_filter_cb), CALLBACK_PREPARE(ACTYPE_OR_NOT_SELECTED, 0), NULL, NULL,},
130
131         /* Find Frame */
132         {"/Find Frame", NULL, NULL, 0, "<Branch>", NULL,},
133         {"/Find Frame/Find Frame", NULL, NULL, 0, "<Branch>", NULL,},
134         {"/Find Frame/Find Frame/Selected", NULL,
135                 GTK_MENU_FUNC(srt_select_filter_cb), CALLBACK_FIND_FRAME(ACTYPE_SELECTED, 0), NULL, NULL,},
136         {"/Find Frame/Find Frame/Not Selected", NULL,
137                 GTK_MENU_FUNC(srt_select_filter_cb), CALLBACK_FIND_FRAME(ACTYPE_NOT_SELECTED, 0), NULL, NULL,},
138         /* Find Next */
139         {"/Find Frame/Find Next", NULL, NULL, 0, "<Branch>", NULL,},
140         {"/Find Frame/Find Next/Selected", NULL,
141                 GTK_MENU_FUNC(srt_select_filter_cb), CALLBACK_FIND_NEXT(ACTYPE_SELECTED, 0), NULL, NULL,},
142         {"/Find Frame/Find Next/Not Selected", NULL,
143                 GTK_MENU_FUNC(srt_select_filter_cb), CALLBACK_FIND_NEXT(ACTYPE_NOT_SELECTED, 0), NULL, NULL,},
144
145         /* Find Previous */
146         {"/Find Frame/Find Previous", NULL, NULL, 0, "<Branch>", NULL,},
147         {"/Find Frame/Find Previous/Selected", NULL,
148                 GTK_MENU_FUNC(srt_select_filter_cb), CALLBACK_FIND_PREVIOUS(ACTYPE_SELECTED, 0), NULL, NULL,},
149         {"/Find Frame/Find Previous/Not Selected", NULL,
150                 GTK_MENU_FUNC(srt_select_filter_cb), CALLBACK_FIND_PREVIOUS(ACTYPE_NOT_SELECTED, 0), NULL, NULL,},
151
152         /* Colorize Procedure */
153         {"/Colorize Procedure", NULL, NULL, 0, "<Branch>", NULL,},
154         {"/Colorize Procedure/Selected", NULL,
155                 GTK_MENU_FUNC(srt_select_filter_cb), CALLBACK_COLORIZE(ACTYPE_SELECTED, 0), NULL, NULL,},
156         {"/Colorize Procedure/Not Selected", NULL,
157                 GTK_MENU_FUNC(srt_select_filter_cb), CALLBACK_COLORIZE(ACTYPE_NOT_SELECTED, 0), NULL, NULL,}
158
159 };
160
161 static void
162 srt_create_popup_menu(srt_stat_table *rst)
163 {
164         GtkItemFactory *item_factory;
165
166         item_factory = gtk_item_factory_new(GTK_TYPE_MENU, "<main>", NULL);
167
168         gtk_item_factory_create_items_ac(item_factory, sizeof(srt_list_menu_items)/sizeof(srt_list_menu_items[0]), srt_list_menu_items, rst, 2);
169
170         rst->menu = gtk_item_factory_get_widget(item_factory, "<main>");
171         g_signal_connect(rst->table, "button_press_event", G_CALLBACK(srt_show_popup_menu_cb), rst);
172 }
173
174 /*
175     XXX Resizable columns are ugly when there's more than on table cf. SMB
176 */
177 void
178 init_srt_table(srt_stat_table *rst, int num_procs, GtkWidget *vbox, const char *filter_string)
179 {
180         int i;
181         GtkListStore *store;
182         GtkWidget *tree;
183         GtkTreeViewColumn *column;
184         GtkCellRenderer *renderer;
185         GtkTreeSortable *sortable;
186         GtkTreeSelection  *sel;
187
188         const char *default_titles[] = { "Index", "Procedure", "Calls", "Min SRT", "Max SRT", "Avg SRT" };
189
190         /* Create the store */
191         store = gtk_list_store_new (N_COLUMNS,  /* Total number of columns */
192                                G_TYPE_INT,      /* Index     */
193                                G_TYPE_STRING,   /* Procedure */
194                                G_TYPE_INT,      /* Calls     */
195                                G_TYPE_STRING,   /* Min SRT   */
196                                G_TYPE_STRING,   /* Max SRT   */
197                                G_TYPE_STRING);  /* Avg SRT   */
198
199       /* Create a view */
200     tree = gtk_tree_view_new_with_model (GTK_TREE_MODEL (store));
201     rst->table = GTK_TREE_VIEW(tree);
202     sortable = GTK_TREE_SORTABLE(store);
203
204     /* The view now holds a reference.  We can get rid of our own reference */
205         g_object_unref (G_OBJECT (store));
206
207         if(filter_string){
208                 rst->filter_string=g_strdup(filter_string);
209         } else {
210                 rst->filter_string=NULL;
211         }
212         for (i = 0; i < N_COLUMNS; i++) {
213                 renderer = gtk_cell_renderer_text_new ();
214                 if (i != PROCEDURE_COLUMN) {
215                         /* right align numbers */
216                         g_object_set(G_OBJECT(renderer), "xalign", 1.0, NULL);
217                 }
218                 g_object_set(renderer, "ypad", 0, NULL);
219                 column = gtk_tree_view_column_new_with_attributes (default_titles[i], renderer, "text", 
220                                 i, NULL);
221                                 
222                 gtk_tree_view_column_set_sort_column_id(column, i);
223                 gtk_tree_view_column_set_resizable(column, TRUE);
224                 gtk_tree_view_append_column (rst->table, column);
225                 if (i == CALLS_COLUMN) {
226                         /* XXX revert order sort */
227                         gtk_tree_view_column_clicked(column);
228                         gtk_tree_view_column_clicked(column);
229                 }
230         }
231
232         rst->scrolled_window=scrolled_window_new(NULL, NULL);
233         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(rst->scrolled_window),
234                                             GTK_SHADOW_IN);
235         gtk_container_add(GTK_CONTAINER(rst->scrolled_window), GTK_WIDGET (rst->table));
236         gtk_box_pack_start(GTK_BOX(vbox), rst->scrolled_window, TRUE, TRUE, 0);
237
238     gtk_tree_view_set_reorderable (rst->table, FALSE);   
239         /* Now enable the sorting of each column */
240         gtk_tree_view_set_rules_hint(rst->table, TRUE);
241         gtk_tree_view_set_headers_clickable(rst->table, TRUE);
242
243         gtk_widget_show(rst->scrolled_window);
244
245         rst->num_procs=num_procs;
246         rst->procedures=g_malloc(sizeof(srt_procedure_t)*num_procs);
247         for(i=0;i<num_procs;i++){
248                 time_stat_init(&rst->procedures[i].stats);
249                 rst->procedures[i].index = 0;
250                 rst->procedures[i].procedure = NULL;
251         }
252
253         sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(rst->table));
254         gtk_tree_selection_set_mode(sel, GTK_SELECTION_SINGLE);
255         /* create popup menu for this table */
256         if(rst->filter_string){
257                 srt_create_popup_menu(rst);
258         }
259 }
260
261 void
262 init_srt_table_row(srt_stat_table *rst, int index, const char *procedure)
263 {
264         /* we have discovered a new procedure. Extend the table accordingly */
265         if(index>=rst->num_procs){
266                 int old_num_procs=rst->num_procs;
267                 int i;
268
269                 rst->num_procs=index+1;
270                 rst->procedures=g_realloc(rst->procedures, sizeof(srt_procedure_t)*(rst->num_procs));
271                 for(i=old_num_procs;i<rst->num_procs;i++){
272                         time_stat_init(&rst->procedures[i].stats);
273                         rst->procedures[i].index = i;
274                         rst->procedures[i].procedure=NULL;
275                 }
276         }
277         rst->procedures[index].index = index;
278         rst->procedures[index].procedure=g_strdup(procedure);
279 }
280
281 void
282 add_srt_table_data(srt_stat_table *rst, int index, const nstime_t *req_time, packet_info *pinfo)
283 {
284         srt_procedure_t *rp;
285         nstime_t t, delta;
286
287         g_assert(index >= 0 && index < rst->num_procs);
288         rp=&rst->procedures[index];
289
290         /*
291          * If the count of calls for this procedure is currently zero, it's
292          * going to become non-zero, so add a row for it (we don't want
293          * rows for procedures that have no calls - especially if the
294          * procedure has no calls because the index doesn't correspond
295          * to a procedure, but is an unused/reserved value).
296          *
297          * (Yes, this means that the rows aren't in order by anything
298          * interesting.  That's why we have the table sorted by a column.)
299          */
300
301         if (rp->stats.num==0){
302                 GtkListStore *store = GTK_LIST_STORE(gtk_tree_view_get_model(rst->table));
303                 gtk_list_store_append(store, &rp->iter);
304                 gtk_list_store_set(store, &rp->iter,
305                                    INDEX_COLUMN,     rp->index,
306                                    PROCEDURE_COLUMN, rp->procedure,
307                                    CALLS_COLUMN,     rp->stats.num,
308                                    MIN_SRT_COLUMN,   "",
309                                    MAX_SRT_COLUMN,   "",
310                                    AVG_SRT_COLUMN,   "",
311                                    -1);
312         }
313
314         /* calculate time delta between request and reply */
315         t=pinfo->fd->abs_ts;
316         nstime_delta(&delta, &t, req_time);
317
318         time_stat_update(&rp->stats, &delta, pinfo);
319 }
320
321 void
322 draw_srt_table_data(srt_stat_table *rst)
323 {
324         int i;
325         guint64 td;
326         char *min, *max, *avg;
327         GtkListStore *store = GTK_LIST_STORE(gtk_tree_view_get_model(rst->table));
328
329         for(i=0;i<rst->num_procs;i++){
330                 /* ignore procedures with no calls (they don't have rows) */
331                 if(rst->procedures[i].stats.num==0){
332                         continue;
333                 }
334
335                 /* scale it to units of 10us.*/
336                 /* for long captures with a large tot time, this can overflow on 32bit */
337                 td=(int)rst->procedures[i].stats.tot.secs;
338                 td=td*100000+(int)rst->procedures[i].stats.tot.nsecs/10000;
339                 td/=rst->procedures[i].stats.num;
340
341                 min=g_strdup_printf("%3d.%05d",
342                     (int)rst->procedures[i].stats.min.secs,
343                     rst->procedures[i].stats.min.nsecs/10000);
344
345                 max=g_strdup_printf("%3d.%05d",
346                     (int)rst->procedures[i].stats.max.secs,
347                     rst->procedures[i].stats.max.nsecs/10000);
348                 avg=g_strdup_printf("%3" G_GINT64_MODIFIER "d.%05" G_GINT64_MODIFIER "d",
349                     td/100000, td%100000);
350                     
351                 gtk_list_store_set(store, &rst->procedures[i].iter,
352                                    CALLS_COLUMN,     rst->procedures[i].stats.num,
353                                    MIN_SRT_COLUMN,   min,
354                                    MAX_SRT_COLUMN,   max,
355                                    AVG_SRT_COLUMN,   avg,
356                                    -1);
357                 g_free(min);
358                 g_free(max);
359                 g_free(avg);
360         }
361 }
362
363
364 void
365 reset_srt_table_data(srt_stat_table *rst)
366 {
367         int i;
368         GtkListStore *store;
369
370         for(i=0;i<rst->num_procs;i++){
371                 time_stat_init(&rst->procedures[i].stats);
372         }
373         store = GTK_LIST_STORE(gtk_tree_view_get_model(rst->table));
374         gtk_list_store_clear(store);
375 }
376
377 void
378 free_srt_table_data(srt_stat_table *rst)
379 {
380         int i;
381
382         for(i=0;i<rst->num_procs;i++){
383                 g_free(rst->procedures[i].procedure);
384                 rst->procedures[i].procedure=NULL;
385         }
386         g_free(rst->filter_string);
387         rst->filter_string=NULL;
388         g_free(rst->procedures);
389         rst->procedures=NULL;
390         rst->num_procs=0;
391 }
392