(Trivial) Whitespace cleanup.
[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 #define NANOSECS_PER_SEC 1000000000
43
44 enum
45 {
46         INDEX_COLUMN,
47         PROCEDURE_COLUMN,
48         CALLS_COLUMN,
49         MIN_SRT_COLUMN,
50         MAX_SRT_COLUMN,
51         AVG_SRT_COLUMN,
52         N_COLUMNS
53 };
54
55
56 static void
57 srt_select_filter_cb(GtkWidget *widget _U_, gpointer callback_data, guint callback_action)
58 {
59         srt_stat_table *rst = (srt_stat_table *)callback_data;
60         char *str = NULL;
61         GtkTreeIter iter;
62         GtkTreeModel *model;
63         GtkTreeSelection  *sel;
64         int selection;
65
66         if(rst->filter_string==NULL){
67                 return;
68         }
69
70         sel = gtk_tree_view_get_selection (GTK_TREE_VIEW(rst->table));
71
72         if (!gtk_tree_selection_get_selected(sel, &model, &iter))
73                 return;
74
75         gtk_tree_model_get (model, &iter, INDEX_COLUMN, &selection, -1);
76         if(selection>=(int)rst->num_procs){
77                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "No procedure selected");
78                 return;
79         }
80
81         str = g_strdup_printf("%s==%d", rst->filter_string, selection);
82
83         apply_selected_filter (callback_action, str);
84
85         g_free(str);
86 }
87
88 static gint
89 srt_show_popup_menu_cb(void *widg _U_, GdkEvent *event, srt_stat_table *rst)
90 {
91         GdkEventButton *bevent = (GdkEventButton *)event;
92
93         if(event->type==GDK_BUTTON_PRESS && bevent->button==3){
94                 gtk_menu_popup(GTK_MENU(rst->menu), NULL, NULL, NULL, NULL,
95                         bevent->button, bevent->time);
96         }
97
98         return FALSE;
99 }
100
101 static GtkItemFactoryEntry srt_list_menu_items[] =
102 {
103         /* Match */
104         {"/Apply as Filter", NULL, NULL, 0, "<Branch>", NULL,},
105         {"/Apply as Filter/Selected", NULL,
106                 GTK_MENU_FUNC(srt_select_filter_cb), CALLBACK_MATCH(ACTYPE_SELECTED, 0), NULL, NULL,},
107         {"/Apply as Filter/... not Selected", NULL,
108                 GTK_MENU_FUNC(srt_select_filter_cb), CALLBACK_MATCH(ACTYPE_NOT_SELECTED, 0), NULL, NULL,},
109         {"/Apply as Filter/.. and Selected", NULL,
110                 GTK_MENU_FUNC(srt_select_filter_cb), CALLBACK_MATCH(ACTYPE_AND_SELECTED, 0), NULL, NULL,},
111         {"/Apply as Filter/... or Selected", NULL,
112                 GTK_MENU_FUNC(srt_select_filter_cb), CALLBACK_MATCH(ACTYPE_OR_SELECTED, 0), NULL, NULL,},
113         {"/Apply as Filter/... and not Selected", NULL,
114                 GTK_MENU_FUNC(srt_select_filter_cb), CALLBACK_MATCH(ACTYPE_AND_NOT_SELECTED, 0), NULL, NULL,},
115         {"/Apply as Filter/... or not Selected", NULL,
116                 GTK_MENU_FUNC(srt_select_filter_cb), CALLBACK_MATCH(ACTYPE_OR_NOT_SELECTED, 0), NULL, NULL,},
117
118         /* Prepare */
119         {"/Prepare a Filter", NULL, NULL, 0, "<Branch>", NULL,},
120         {"/Prepare a Filter/Selected", NULL,
121                 GTK_MENU_FUNC(srt_select_filter_cb), CALLBACK_PREPARE(ACTYPE_SELECTED, 0), NULL, NULL,},
122         {"/Prepare a Filter/Not Selected", NULL,
123                 GTK_MENU_FUNC(srt_select_filter_cb), CALLBACK_PREPARE(ACTYPE_NOT_SELECTED, 0), NULL, NULL,},
124         {"/Prepare a Filter/... and Selected", NULL,
125                 GTK_MENU_FUNC(srt_select_filter_cb), CALLBACK_PREPARE(ACTYPE_AND_SELECTED, 0), NULL, NULL,},
126         {"/Prepare a Filter/... or Selected", NULL,
127                 GTK_MENU_FUNC(srt_select_filter_cb), CALLBACK_PREPARE(ACTYPE_OR_SELECTED, 0), NULL, NULL,},
128         {"/Prepare a Filter/... and not Selected", NULL,
129                 GTK_MENU_FUNC(srt_select_filter_cb), CALLBACK_PREPARE(ACTYPE_AND_NOT_SELECTED, 0), NULL, NULL,},
130         {"/Prepare a Filter/... or not Selected", NULL,
131                 GTK_MENU_FUNC(srt_select_filter_cb), CALLBACK_PREPARE(ACTYPE_OR_NOT_SELECTED, 0), NULL, NULL,},
132
133         /* Find Frame */
134         {"/Find Frame", NULL, NULL, 0, "<Branch>", NULL,},
135         {"/Find Frame/Find Frame", NULL, NULL, 0, "<Branch>", NULL,},
136         {"/Find Frame/Find Frame/Selected", NULL,
137                 GTK_MENU_FUNC(srt_select_filter_cb), CALLBACK_FIND_FRAME(ACTYPE_SELECTED, 0), NULL, NULL,},
138         {"/Find Frame/Find Frame/Not Selected", NULL,
139                 GTK_MENU_FUNC(srt_select_filter_cb), CALLBACK_FIND_FRAME(ACTYPE_NOT_SELECTED, 0), NULL, NULL,},
140         /* Find Next */
141         {"/Find Frame/Find Next", NULL, NULL, 0, "<Branch>", NULL,},
142         {"/Find Frame/Find Next/Selected", NULL,
143                 GTK_MENU_FUNC(srt_select_filter_cb), CALLBACK_FIND_NEXT(ACTYPE_SELECTED, 0), NULL, NULL,},
144         {"/Find Frame/Find Next/Not Selected", NULL,
145                 GTK_MENU_FUNC(srt_select_filter_cb), CALLBACK_FIND_NEXT(ACTYPE_NOT_SELECTED, 0), NULL, NULL,},
146
147         /* Find Previous */
148         {"/Find Frame/Find Previous", NULL, NULL, 0, "<Branch>", NULL,},
149         {"/Find Frame/Find Previous/Selected", NULL,
150                 GTK_MENU_FUNC(srt_select_filter_cb), CALLBACK_FIND_PREVIOUS(ACTYPE_SELECTED, 0), NULL, NULL,},
151         {"/Find Frame/Find Previous/Not Selected", NULL,
152                 GTK_MENU_FUNC(srt_select_filter_cb), CALLBACK_FIND_PREVIOUS(ACTYPE_NOT_SELECTED, 0), NULL, NULL,},
153
154         /* Colorize Procedure */
155         {"/Colorize Procedure", NULL, NULL, 0, "<Branch>", NULL,},
156         {"/Colorize Procedure/Selected", NULL,
157                 GTK_MENU_FUNC(srt_select_filter_cb), CALLBACK_COLORIZE(ACTYPE_SELECTED, 0), NULL, NULL,},
158         {"/Colorize Procedure/Not Selected", NULL,
159                 GTK_MENU_FUNC(srt_select_filter_cb), CALLBACK_COLORIZE(ACTYPE_NOT_SELECTED, 0), NULL, NULL,}
160
161 };
162
163 static void
164 srt_create_popup_menu(srt_stat_table *rst)
165 {
166         GtkItemFactory *item_factory;
167
168         item_factory = gtk_item_factory_new(GTK_TYPE_MENU, "<main>", NULL);
169
170         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);
171
172         rst->menu = gtk_item_factory_get_widget(item_factory, "<main>");
173         g_signal_connect(rst->table, "button_press_event", G_CALLBACK(srt_show_popup_menu_cb), rst);
174 }
175
176 /* ---------------- */
177 static void
178 srt_time_func (GtkTreeViewColumn *column _U_,
179                GtkCellRenderer   *renderer,
180                GtkTreeModel      *model,
181                GtkTreeIter       *iter,
182                gpointer           user_data)
183 {
184          gchar *str;
185          nstime_t *data;
186
187          /* The col to get data from is in userdata */
188          gint data_column = GPOINTER_TO_INT(user_data);
189
190          gtk_tree_model_get(model, iter, data_column, &data, -1);
191          if (!data) {
192                  g_object_set(renderer, "text", "", NULL);
193                  return;
194          }
195          str = g_strdup_printf("%3d.%06d", (int)data->secs, (data->nsecs+500)/1000);
196          g_object_set(renderer, "text", str, NULL);
197          g_free(str);
198 }
199
200 static void
201 srt_avg_func (GtkTreeViewColumn *column _U_,
202               GtkCellRenderer   *renderer,
203               GtkTreeModel      *model,
204               GtkTreeIter       *iter,
205               gpointer           user_data)
206 {
207         gchar *str;
208         guint64 td;
209         gint data_column = GPOINTER_TO_INT(user_data);
210
211         gtk_tree_model_get(model, iter, data_column, &td, -1);
212         str=g_strdup_printf("%3d.%06d",
213                             (int)(td/1000000), (int)(td%1000000));
214         g_object_set(renderer, "text", str, NULL);
215         g_free(str);
216 }
217
218 static gint
219 srt_time_sort_func(GtkTreeModel *model,
220                                                         GtkTreeIter *a,
221                                                         GtkTreeIter *b,
222                                                         gpointer user_data)
223 {
224          nstime_t *ns_a;
225          nstime_t *ns_b;
226          gint ret = 0;
227          gint data_column = GPOINTER_TO_INT(user_data);
228
229          gtk_tree_model_get(model, a, data_column, &ns_a, -1);
230          gtk_tree_model_get(model, b, data_column, &ns_b, -1);
231
232         if (ns_a == ns_b) {
233                 ret = 0;
234         }
235         else if (ns_a == NULL || ns_b == NULL) {
236                 ret = (ns_a == NULL) ? -1 : 1;
237         }
238         else {
239                 ret = nstime_cmp(ns_a,ns_b);
240         }
241         return ret;
242 }
243
244 /*
245     XXX Resizable columns are ugly when there's more than on table cf. SMB
246 */
247 void
248 init_srt_table(srt_stat_table *rst, int num_procs, GtkWidget *vbox, const char *filter_string)
249 {
250         int i;
251         GtkListStore *store;
252         GtkWidget *tree;
253         GtkTreeViewColumn *column;
254         GtkCellRenderer *renderer;
255         GtkTreeSortable *sortable;
256         GtkTreeSelection  *sel;
257
258         const char *default_titles[] = { "Index", "Procedure", "Calls", "Min SRT", "Max SRT", "Avg SRT" };
259
260         /* Create the store */
261         store = gtk_list_store_new (N_COLUMNS,  /* Total number of columns */
262                                     G_TYPE_INT,         /* Index     */
263                                     G_TYPE_STRING,   /* Procedure */
264                                     G_TYPE_UINT,        /* Calls     */
265                                     G_TYPE_POINTER,  /* Min SRT   */
266                                     G_TYPE_POINTER,  /* Max SRT   */
267                                     G_TYPE_UINT64);  /* Avg SRT   */
268
269         /* Create a view */
270         tree = gtk_tree_view_new_with_model (GTK_TREE_MODEL (store));
271         rst->table = GTK_TREE_VIEW(tree);
272         sortable = GTK_TREE_SORTABLE(store);
273
274         /* The view now holds a reference.  We can get rid of our own reference */
275         g_object_unref (G_OBJECT (store));
276
277         if(filter_string){
278                 rst->filter_string=g_strdup(filter_string);
279         } else {
280                 rst->filter_string=NULL;
281         }
282         for (i = 0; i < N_COLUMNS; i++) {
283                 renderer = gtk_cell_renderer_text_new ();
284                 if (i != PROCEDURE_COLUMN) {
285                         /* right align numbers */
286                         g_object_set(G_OBJECT(renderer), "xalign", 1.0, NULL);
287                 }
288                 g_object_set(renderer, "ypad", 0, NULL);
289                 switch (i) {
290                 case MIN_SRT_COLUMN:
291                 case MAX_SRT_COLUMN:
292                         column = gtk_tree_view_column_new_with_attributes (default_titles[i], renderer, NULL);
293                         gtk_tree_view_column_set_cell_data_func(column, renderer, srt_time_func,  GINT_TO_POINTER(i), NULL);
294                         gtk_tree_sortable_set_sort_func(sortable, i, srt_time_sort_func, GINT_TO_POINTER(i), NULL);
295                         break;
296                 case AVG_SRT_COLUMN:
297                         column = gtk_tree_view_column_new_with_attributes (default_titles[i], renderer, NULL);
298                         gtk_tree_view_column_set_cell_data_func(column, renderer, srt_avg_func,  GINT_TO_POINTER(i), NULL);
299                         break;
300                 default:
301                         column = gtk_tree_view_column_new_with_attributes (default_titles[i], renderer, "text",
302                                         i, NULL);
303                         break;
304                 }
305
306                 gtk_tree_view_column_set_sort_column_id(column, i);
307                 gtk_tree_view_column_set_resizable(column, TRUE);
308                 gtk_tree_view_append_column (rst->table, column);
309                 if (i == CALLS_COLUMN) {
310                         /* XXX revert order sort */
311                         gtk_tree_view_column_clicked(column);
312                         gtk_tree_view_column_clicked(column);
313                 }
314         }
315
316         rst->scrolled_window=scrolled_window_new(NULL, NULL);
317         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(rst->scrolled_window),
318                                             GTK_SHADOW_IN);
319         gtk_container_add(GTK_CONTAINER(rst->scrolled_window), GTK_WIDGET (rst->table));
320         gtk_box_pack_start(GTK_BOX(vbox), rst->scrolled_window, TRUE, TRUE, 0);
321
322         gtk_tree_view_set_reorderable (rst->table, FALSE);
323         /* Now enable the sorting of each column */
324         gtk_tree_view_set_rules_hint(rst->table, TRUE);
325         gtk_tree_view_set_headers_clickable(rst->table, TRUE);
326
327         gtk_widget_show(rst->scrolled_window);
328
329         rst->num_procs=num_procs;
330         rst->procedures=g_malloc(sizeof(srt_procedure_t)*num_procs);
331         for(i=0;i<num_procs;i++){
332                 time_stat_init(&rst->procedures[i].stats);
333                 rst->procedures[i].index = 0;
334                 rst->procedures[i].procedure = NULL;
335         }
336
337         sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(rst->table));
338         gtk_tree_selection_set_mode(sel, GTK_SELECTION_SINGLE);
339         /* create popup menu for this table */
340         if(rst->filter_string){
341                 srt_create_popup_menu(rst);
342         }
343 }
344
345 void
346 init_srt_table_row(srt_stat_table *rst, int index, const char *procedure)
347 {
348         /* we have discovered a new procedure. Extend the table accordingly */
349         if(index>=rst->num_procs){
350                 int old_num_procs=rst->num_procs;
351                 int i;
352
353                 rst->num_procs=index+1;
354                 rst->procedures=g_realloc(rst->procedures, sizeof(srt_procedure_t)*(rst->num_procs));
355                 for(i=old_num_procs;i<rst->num_procs;i++){
356                         time_stat_init(&rst->procedures[i].stats);
357                         rst->procedures[i].index = i;
358                         rst->procedures[i].procedure=NULL;
359                 }
360         }
361         rst->procedures[index].index = index;
362         rst->procedures[index].procedure=g_strdup(procedure);
363 }
364
365 void
366 add_srt_table_data(srt_stat_table *rst, int index, const nstime_t *req_time, packet_info *pinfo)
367 {
368         srt_procedure_t *rp;
369         nstime_t t, delta;
370
371         g_assert(index >= 0 && index < rst->num_procs);
372         rp=&rst->procedures[index];
373
374         /*
375          * If the count of calls for this procedure is currently zero, it's
376          * going to become non-zero, so add a row for it (we don't want
377          * rows for procedures that have no calls - especially if the
378          * procedure has no calls because the index doesn't correspond
379          * to a procedure, but is an unused/reserved value).
380          *
381          * (Yes, this means that the rows aren't in order by anything
382          * interesting.  That's why we have the table sorted by a column.)
383          */
384
385         if (rp->stats.num==0){
386                 GtkListStore *store = GTK_LIST_STORE(gtk_tree_view_get_model(rst->table));
387                 gtk_list_store_append(store, &rp->iter);
388                 gtk_list_store_set(store, &rp->iter,
389                                    INDEX_COLUMN,     rp->index,
390                                    PROCEDURE_COLUMN, rp->procedure,
391                                    CALLS_COLUMN,     rp->stats.num,
392                                    MIN_SRT_COLUMN,   NULL,
393                                    MAX_SRT_COLUMN,   NULL,
394                                    AVG_SRT_COLUMN,   (guint64)0,
395                                    -1);
396         }
397
398         /* calculate time delta between request and reply */
399         t=pinfo->fd->abs_ts;
400         nstime_delta(&delta, &t, req_time);
401
402         time_stat_update(&rp->stats, &delta, pinfo);
403 }
404
405 void
406 draw_srt_table_data(srt_stat_table *rst)
407 {
408         int i;
409         guint64 td;
410         GtkListStore *store = GTK_LIST_STORE(gtk_tree_view_get_model(rst->table));
411
412         for(i=0;i<rst->num_procs;i++){
413                 /* ignore procedures with no calls (they don't have rows) */
414                 if(rst->procedures[i].stats.num==0){
415                         continue;
416                 }
417                 /* Scale the average SRT in units of 1us and round to the nearest us.
418                    tot.secs is a time_t which may be 32 or 64 bits (or even floating)
419                    depending uon the platform.  After casting tot.secs to 64 bits, it
420                    would take a capture with a duration of over 136 *years* to
421                    overflow the secs portion of td. */
422                 td = ((guint64)(rst->procedures[i].stats.tot.secs))*NANOSECS_PER_SEC + rst->procedures[i].stats.tot.nsecs;
423                 td = ((td / rst->procedures[i].stats.num) + 500) / 1000;
424
425                 gtk_list_store_set(store, &rst->procedures[i].iter,
426                                    CALLS_COLUMN,     rst->procedures[i].stats.num,
427                                    MIN_SRT_COLUMN,   &rst->procedures[i].stats.min,
428                                    MAX_SRT_COLUMN,   &rst->procedures[i].stats.max,
429                                    AVG_SRT_COLUMN,   td,
430                                    -1);
431         }
432 }
433
434
435 void
436 reset_srt_table_data(srt_stat_table *rst)
437 {
438         int i;
439         GtkListStore *store;
440
441         for(i=0;i<rst->num_procs;i++){
442                 time_stat_init(&rst->procedures[i].stats);
443         }
444         store = GTK_LIST_STORE(gtk_tree_view_get_model(rst->table));
445         gtk_list_store_clear(store);
446 }
447
448 void
449 free_srt_table_data(srt_stat_table *rst)
450 {
451         int i;
452
453         for(i=0;i<rst->num_procs;i++){
454                 g_free(rst->procedures[i].procedure);
455                 rst->procedures[i].procedure=NULL;
456         }
457         g_free(rst->filter_string);
458         rst->filter_string=NULL;
459         g_free(rst->procedures);
460         rst->procedures=NULL;
461         rst->num_procs=0;
462 }
463