From Greg Morris:
[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  * Ethereal - Network traffic analyzer
9  * By Gerald Combs <gerald@ethereal.com>
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 <string.h>
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <math.h>
35 #include <gtk/gtk.h>
36 #include "compat_macros.h"
37 #include "epan/packet_info.h"
38 #include "service_response_time_table.h"
39 #include "image/clist_ascend.xpm"
40 #include "image/clist_descend.xpm"
41 #include "simple_dialog.h"
42 #include "globals.h"
43 #include "gtk/find_dlg.h"
44 #include "color.h"
45 #include "gtk/color_dlg.h"
46 #include "main.h"
47 #include "gui_utils.h"
48 #include "gtkglobals.h"
49
50
51 #define GTK_MENU_FUNC(a) ((GtkItemFactoryCallback)(a))
52
53
54 typedef struct column_arrows {
55         GtkWidget *table;
56         GtkWidget *ascend_pm;
57         GtkWidget *descend_pm;
58 } column_arrows;
59
60
61 static void
62 srt_click_column_cb(GtkCList *clist, gint column, gpointer data)
63 {
64         column_arrows *col_arrows = (column_arrows *) data;
65         int i;
66
67         gtk_clist_freeze(clist);
68
69         for (i = 0; i < 6; i++) {
70                 gtk_widget_hide(col_arrows[i].ascend_pm);
71                 gtk_widget_hide(col_arrows[i].descend_pm);
72         }
73
74         if (column == clist->sort_column) {
75                 if (clist->sort_type == GTK_SORT_ASCENDING) {
76                         clist->sort_type = GTK_SORT_DESCENDING;
77                         gtk_widget_show(col_arrows[column].descend_pm);
78                 } else {
79                         clist->sort_type = GTK_SORT_ASCENDING;
80                         gtk_widget_show(col_arrows[column].ascend_pm);
81                 }
82         } else {
83                 /* Columns 2-5   Count, Min, Max, Avg are sorted in descending
84                         order by default.
85                    Columns 0 and 1 sort by ascending order by default
86                 */
87                 if(column>=2){
88                         clist->sort_type = GTK_SORT_DESCENDING;
89                         gtk_widget_show(col_arrows[column].descend_pm);
90                 } else {
91                         clist->sort_type = GTK_SORT_ASCENDING;
92                         gtk_widget_show(col_arrows[column].ascend_pm);
93                 }
94                 gtk_clist_set_sort_column(clist, column);
95         }
96         gtk_clist_thaw(clist);
97
98         gtk_clist_sort(clist);
99 }
100
101 static gint
102 srt_sort_column(GtkCList *clist, gconstpointer ptr1, gconstpointer ptr2)
103 {
104         char *text1 = NULL;
105         char *text2 = NULL;
106         int i1, i2;
107         float f1,f2;
108
109         const GtkCListRow *row1 = ptr1;
110         const GtkCListRow *row2 = ptr2;
111
112         text1 = GTK_CELL_TEXT (row1->cell[clist->sort_column])->text;
113         text2 = GTK_CELL_TEXT (row2->cell[clist->sort_column])->text;
114
115         switch(clist->sort_column){
116         case 1:
117                 return strcmp (text1, text2);
118         case 0:
119         case 2:
120                 i1=atoi(text1);
121                 i2=atoi(text2);
122                 return i1-i2;
123         case 3:
124         case 4:
125         case 5:
126                 sscanf(text1,"%f",&f1);
127                 sscanf(text2,"%f",&f2);
128                 if(fabs(f1-f2)<0.000005)
129                         return 0;
130                 if(f1>f2)
131                         return 1;
132                 return -1;
133         }
134         g_assert_not_reached();
135         return 0;
136 }
137
138
139
140 /* action is encoded as 
141    filter_action*256+filter_type
142
143    filter_action:
144         0: Match
145         1: Prepare
146         2: Find Frame
147         3:   Find Next
148         4:   Find Previous
149         5: Colorize Procedure
150    filter_type:
151         0: Selected
152         1: Not Selected
153         2: And Selected
154         3: Or Selected
155         4: And Not Selected
156         5: Or Not Selected
157 */
158 static void
159 srt_select_filter_cb(GtkWidget *widget _U_, gpointer callback_data, guint callback_action)
160 {
161         int action, type, selection;
162         srt_stat_table *rst = (srt_stat_table *)callback_data;
163         char str[256];
164         const char *current_filter;
165
166
167         if(rst->filter_string==NULL){
168                 return;
169         }
170
171         action=(callback_action>>8)&0xff;
172         type=callback_action&0xff;
173
174         selection=GPOINTER_TO_INT(g_list_nth_data(GTK_CLIST(rst->table)->selection, 0));
175         if(selection>=(int)rst->num_procs){
176                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "No procedure selected");
177                 return;
178         }
179         /* translate it back from row index to index in procedures array */
180         selection=GPOINTER_TO_INT(gtk_clist_get_row_data(rst->table, selection));
181
182         current_filter=gtk_entry_get_text(GTK_ENTRY(main_display_filter_widget));
183
184         switch(type){
185         case 0:
186                 /* selected */
187                 g_snprintf(str, 255, "%s==%d", rst->filter_string, selection);
188                 break;
189         case 1:
190                 /* not selected */
191                 g_snprintf(str, 255, "!(%s==%d)", rst->filter_string, selection);
192                 break;
193         case 2:
194                 /* and selected */
195                 g_snprintf(str, 255, "(%s) && (%s==%d)", current_filter, rst->filter_string, selection);
196                 break;
197         case 3:
198                 /* or selected */
199                 g_snprintf(str, 255, "(%s) || (%s==%d)", current_filter, rst->filter_string, selection);
200                 break;
201         case 4:
202                 /* and not selected */
203                 g_snprintf(str, 255, "(%s) && !(%s==%d)", current_filter, rst->filter_string, selection);
204                 break;
205         case 5:
206                 /* or not selected */
207                 g_snprintf(str, 255, "(%s) || !(%s==%d)", current_filter, rst->filter_string, selection);
208                 break;
209         }
210
211         gtk_entry_set_text(GTK_ENTRY(main_display_filter_widget), str);
212
213         switch(action){
214         case 0:
215                 /* match */
216                 main_filter_packets(&cfile, str, FALSE);
217         case 1:
218                 /* prepare */
219                 /* do nothing */
220                 break;
221         case 2:
222                 /* find frame */
223                 find_frame_with_filter(str);
224                 break;
225         case 3:
226                 /* find next */
227                 find_previous_next_frame_with_filter(str, FALSE);
228                 break;
229         case 4:
230                 /* find previous */
231                 find_previous_next_frame_with_filter(str, TRUE);
232                 break;
233         case 5:
234                 /* colorize procedure */
235                 color_display_with_filter(str);
236                 break;
237         }
238
239 }
240
241 static gint
242 srt_show_popup_menu_cb(void *widg _U_, GdkEvent *event, srt_stat_table *rst)
243 {
244         GdkEventButton *bevent = (GdkEventButton *)event;
245
246         if(event->type==GDK_BUTTON_PRESS && bevent->button==3){
247                 gtk_menu_popup(GTK_MENU(rst->menu), NULL, NULL, NULL, NULL, 
248                         bevent->button, bevent->time);
249         }
250
251         return FALSE;
252 }
253
254 static GtkItemFactoryEntry srt_list_menu_items[] =
255 {
256         /* Match */
257         ITEM_FACTORY_ENTRY("/Apply as Filter", NULL, NULL, 0, "<Branch>", NULL),
258         ITEM_FACTORY_ENTRY("/Apply as Filter/Selected", NULL,
259                 srt_select_filter_cb, 0*256+0, NULL, NULL),
260         ITEM_FACTORY_ENTRY("/Apply as Filter/... not Selected", NULL,
261                 srt_select_filter_cb, 0*256+1, NULL, NULL),
262         ITEM_FACTORY_ENTRY("/Apply as Filter/.. and Selected", NULL,
263                 srt_select_filter_cb, 0*256+2, NULL, NULL),
264         ITEM_FACTORY_ENTRY("/Apply as Filter/... or Selected", NULL,
265                 srt_select_filter_cb, 0*256+3, NULL, NULL),
266         ITEM_FACTORY_ENTRY("/Apply as Filter/... and not Selected", NULL,
267                 srt_select_filter_cb, 0*256+4, NULL, NULL),
268         ITEM_FACTORY_ENTRY("/Apply as Filter/... or not Selected", NULL,
269                 srt_select_filter_cb, 0*256+5, NULL, NULL),
270
271         /* Prepare */
272         ITEM_FACTORY_ENTRY("/Prepare a Filter", NULL, NULL, 0, "<Branch>", NULL),
273         ITEM_FACTORY_ENTRY("/Prepare a Filter/Selected", NULL,
274                 srt_select_filter_cb, 1*256+0, NULL, NULL),
275         ITEM_FACTORY_ENTRY("/Prepare a Filter/Not Selected", NULL,
276                 srt_select_filter_cb, 1*256+1, NULL, NULL),
277         ITEM_FACTORY_ENTRY("/Prepare a Filter/... and Selected", NULL,
278                 srt_select_filter_cb, 1*256+2, NULL, NULL),
279         ITEM_FACTORY_ENTRY("/Prepare a Filter/... or Selected", NULL,
280                 srt_select_filter_cb, 1*256+3, NULL, NULL),
281         ITEM_FACTORY_ENTRY("/Prepare a Filter/... and not Selected", NULL,
282                 srt_select_filter_cb, 1*256+4, NULL, NULL),
283         ITEM_FACTORY_ENTRY("/Prepare a Filter/... or not Selected", NULL,
284                 srt_select_filter_cb, 1*256+5, NULL, NULL),
285
286         /* Find Frame */
287         ITEM_FACTORY_ENTRY("/Find Frame", NULL, NULL, 0, "<Branch>", NULL),
288         ITEM_FACTORY_ENTRY("/Find Frame/Find Frame", NULL, NULL, 0, "<Branch>", NULL),
289         ITEM_FACTORY_ENTRY("/Find Frame/Find Frame/Selected", NULL,
290                 srt_select_filter_cb, 2*256+0, NULL, NULL),
291         ITEM_FACTORY_ENTRY("/Find Frame/Find Frame/Not Selected", NULL,
292                 srt_select_filter_cb, 2*256+1, NULL, NULL),
293         /* Find Next */
294         ITEM_FACTORY_ENTRY("/Find Frame/Find Next", NULL, NULL, 0, "<Branch>", NULL),
295         ITEM_FACTORY_ENTRY("/Find Frame/Find Next/Selected", NULL,
296                 srt_select_filter_cb, 3*256+0, NULL, NULL),
297         ITEM_FACTORY_ENTRY("/Find Frame/Find Next/Not Selected", NULL,
298                 srt_select_filter_cb, 3*256+1, NULL, NULL),
299
300         /* Find Previous */
301         ITEM_FACTORY_ENTRY("/Find Frame/Find Previous", NULL, NULL, 0, "<Branch>", NULL),
302         ITEM_FACTORY_ENTRY("/Find Frame/Find Previous/Selected", NULL,
303                 srt_select_filter_cb, 4*256+0, NULL, NULL),
304         ITEM_FACTORY_ENTRY("/Find Frame/Find Previous/Not Selected", NULL,
305                 srt_select_filter_cb, 4*256+1, NULL, NULL),
306
307         /* Colorize Procedure */
308         ITEM_FACTORY_ENTRY("/Colorize Procedure", NULL, NULL, 0, "<Branch>", NULL),
309         ITEM_FACTORY_ENTRY("/Colorize Procedure/Selected", NULL,
310                 srt_select_filter_cb, 5*256+0, NULL, NULL),
311         ITEM_FACTORY_ENTRY("/Colorize Procedure/Not Selected", NULL,
312                 srt_select_filter_cb, 5*256+1, NULL, NULL),
313
314 };
315
316 static void
317 srt_create_popup_menu(srt_stat_table *rst)
318 {
319         GtkItemFactory *item_factory;
320
321         item_factory = gtk_item_factory_new(GTK_TYPE_MENU, "<main>", NULL);
322
323         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);
324
325         rst->menu = gtk_item_factory_get_widget(item_factory, "<main>");
326         SIGNAL_CONNECT(rst->table, "button_press_event", srt_show_popup_menu_cb, rst);
327 }
328
329
330 void
331 init_srt_table(srt_stat_table *rst, int num_procs, GtkWidget *vbox, const char *filter_string)
332 {
333         int i, j;
334         column_arrows *col_arrows;
335         GdkBitmap *ascend_bm, *descend_bm;
336         GdkPixmap *ascend_pm, *descend_pm;
337         GtkStyle *win_style;
338         GtkWidget *column_lb;
339         const char *default_titles[] = { "Index", "Procedure", "Calls", "Min SRT", "Max SRT", "Avg SRT" };
340
341
342         if(filter_string){
343                 rst->filter_string=g_strdup(filter_string);
344         } else {
345                 rst->filter_string=NULL;
346         }
347         rst->scrolled_window=scrolled_window_new(NULL, NULL);
348         gtk_box_pack_start(GTK_BOX(vbox), rst->scrolled_window, TRUE, TRUE, 0);
349
350         rst->table=(GtkCList *)gtk_clist_new(6);
351
352         gtk_widget_show(GTK_WIDGET(rst->table));
353         gtk_widget_show(rst->scrolled_window);
354
355         col_arrows = (column_arrows *) g_malloc(sizeof(column_arrows) * 6);
356         win_style = gtk_widget_get_style(rst->scrolled_window);
357         ascend_pm = gdk_pixmap_create_from_xpm_d(rst->scrolled_window->window,
358                         &ascend_bm,
359                         &win_style->bg[GTK_STATE_NORMAL],
360                         (gchar **)clist_ascend_xpm);
361         descend_pm = gdk_pixmap_create_from_xpm_d(rst->scrolled_window->window,
362                         &descend_bm,
363                         &win_style->bg[GTK_STATE_NORMAL],
364                         (gchar **)clist_descend_xpm);
365         for (i = 0; i < 6; i++) {
366                 col_arrows[i].table = gtk_table_new(2, 2, FALSE);
367                 gtk_table_set_col_spacings(GTK_TABLE(col_arrows[i].table), 5);
368                 column_lb = gtk_label_new(default_titles[i]);
369                 gtk_table_attach(GTK_TABLE(col_arrows[i].table), column_lb, 0, 1, 0, 2, GTK_SHRINK, GTK_SHRINK, 0, 0);
370                 gtk_widget_show(column_lb);
371
372                 col_arrows[i].ascend_pm = gtk_pixmap_new(ascend_pm, ascend_bm);
373                 gtk_table_attach(GTK_TABLE(col_arrows[i].table), col_arrows[i].ascend_pm, 1, 2, 1, 2, GTK_SHRINK, GTK_SHRINK, 0, 0);
374                 col_arrows[i].descend_pm = gtk_pixmap_new(descend_pm, descend_bm);
375                 gtk_table_attach(GTK_TABLE(col_arrows[i].table), col_arrows[i].descend_pm, 1, 2, 0, 1, GTK_SHRINK, GTK_SHRINK, 0, 0);
376                 if (i == 2) {
377                         gtk_widget_show(col_arrows[i].descend_pm);
378                 }
379                 gtk_clist_set_column_widget(GTK_CLIST(rst->table), i, col_arrows[i].table);
380                 gtk_widget_show(col_arrows[i].table);
381         }
382         gtk_clist_column_titles_show(GTK_CLIST(rst->table));
383
384         gtk_clist_set_compare_func(rst->table, srt_sort_column);
385         gtk_clist_set_sort_column(rst->table, 2);
386         gtk_clist_set_sort_type(rst->table, GTK_SORT_DESCENDING);
387
388
389         /*XXX instead of this we should probably have some code to
390                 dynamically adjust the width of the columns */
391         gtk_clist_set_column_width(rst->table, 0, 32);
392         gtk_clist_set_column_width(rst->table, 1, 160);
393         gtk_clist_set_column_width(rst->table, 2, 50);
394         gtk_clist_set_column_width(rst->table, 3, 60);
395         gtk_clist_set_column_width(rst->table, 4, 60);
396         gtk_clist_set_column_width(rst->table, 5, 60);
397
398         gtk_clist_set_shadow_type(rst->table, GTK_SHADOW_IN);
399         gtk_clist_column_titles_show(rst->table);
400         gtk_container_add(GTK_CONTAINER(rst->scrolled_window), (GtkWidget *)rst->table);
401
402         SIGNAL_CONNECT(rst->table, "click-column", srt_click_column_cb, col_arrows);
403
404         gtk_widget_show(GTK_WIDGET(rst->table));
405         gtk_widget_show(rst->scrolled_window);
406
407
408         rst->num_procs=num_procs;
409         rst->procedures=g_malloc(sizeof(srt_procedure_t)*num_procs);
410         for(i=0;i<num_procs;i++){
411                 rst->procedures[i].stats.num=0;
412                 rst->procedures[i].stats.min.secs=0;
413                 rst->procedures[i].stats.min.nsecs=0;
414                 rst->procedures[i].stats.max.secs=0;
415                 rst->procedures[i].stats.max.nsecs=0;
416                 rst->procedures[i].stats.tot.secs=0;
417                 rst->procedures[i].stats.tot.nsecs=0;
418                 for(j=0;j<6;j++){
419                         rst->procedures[i].entries[j]=NULL;
420                 }
421         }
422
423         /* create popup menu for this table */
424         if(rst->filter_string){
425                 srt_create_popup_menu(rst);
426         }
427 }
428
429 void
430 init_srt_table_row(srt_stat_table *rst, int index, const char *procedure)
431 {
432         /* we have discovered a new procedure. Extend the table accordingly */
433         if(index>=rst->num_procs){
434                 int old_num_procs=rst->num_procs;
435                 int i,j;
436                 rst->num_procs=index+1;
437                 rst->procedures=g_realloc(rst->procedures, sizeof(srt_procedure_t)*(rst->num_procs));
438                 for(i=old_num_procs;i<rst->num_procs;i++){
439                         rst->procedures[i].stats.num=0;
440                         rst->procedures[i].stats.min.secs=0;
441                         rst->procedures[i].stats.min.nsecs=0;
442                         rst->procedures[i].stats.max.secs=0;
443                         rst->procedures[i].stats.max.nsecs=0;
444                         rst->procedures[i].stats.tot.secs=0;
445                         rst->procedures[i].stats.tot.nsecs=0;
446                         for(j=0;j<6;j++){
447                                 rst->procedures[i].entries[j]=NULL;
448                         }
449                 }
450         }
451         rst->procedures[index].entries[0]=g_strdup_printf("%d", index);
452
453         rst->procedures[index].entries[1]=g_strdup(procedure);
454
455         rst->procedures[index].entries[2]=g_strdup("0");
456         rst->procedures[index].entries[3]=g_strdup("0");
457         rst->procedures[index].entries[4]=g_strdup("0");
458         rst->procedures[index].entries[5]=g_strdup("0");
459 }
460
461 void
462 add_srt_table_data(srt_stat_table *rst, int index, const nstime_t *req_time, packet_info *pinfo)
463 {
464         srt_procedure_t *rp;
465         nstime_t t, delta;
466         gint row;
467
468         rp=&rst->procedures[index];
469
470         /*
471          * If the count of calls for this procedure is currently zero, it's
472          * going to become non-zero, so add a row for it (we don't want
473          * rows for procedures that have no calls - especially if the
474          * procedure has no calls because the index doesn't correspond
475          * to a procedure, but is an unused/reserved value).
476          *
477          * (Yes, this means that the rows aren't in order by anything
478          * interesting.  That's why we have the table sorted by a column.)
479          */
480         if (rp->stats.num==0){
481                 row=gtk_clist_append(rst->table, rst->procedures[index].entries);
482                 gtk_clist_set_row_data(rst->table, row, (gpointer) index);
483         }
484
485         /* calculate time delta between request and reply */
486         t=pinfo->fd->abs_ts;
487         nstime_delta(&delta, &t, req_time);
488
489         time_stat_update(&rp->stats, &delta, pinfo);
490 }
491
492 void
493 draw_srt_table_data(srt_stat_table *rst)
494 {
495         int i,j;
496         guint64 td;
497         char *strp;
498
499         for(i=0;i<rst->num_procs;i++){
500                 /* ignore procedures with no calls (they don't have CList rows) */
501                 if(rst->procedures[i].stats.num==0){
502                         continue;
503                 }
504
505                 /* scale it to units of 10us.*/
506                 /* for long captures with a large tot time, this can overflow on 32bit */
507                 td=(int)rst->procedures[i].stats.tot.secs;
508                 td=td*100000+(int)rst->procedures[i].stats.tot.nsecs/10000;
509                 td/=rst->procedures[i].stats.num;
510
511                 j=gtk_clist_find_row_from_data(rst->table, (gpointer)i);
512                 strp=g_strdup_printf("%d", rst->procedures[i].stats.num);
513                 gtk_clist_set_text(rst->table, j, 2, strp);
514                 g_free(rst->procedures[i].entries[2]);
515                 rst->procedures[i].entries[2]=strp;
516
517
518                 strp=g_strdup_printf("%3d.%05d",
519                     (int)rst->procedures[i].stats.min.secs,
520                     rst->procedures[i].stats.min.nsecs/10000);
521                 gtk_clist_set_text(rst->table, j, 3, strp);
522                 g_free(rst->procedures[i].entries[3]);
523                 rst->procedures[i].entries[3]=strp;
524
525
526                 strp=g_strdup_printf("%3d.%05d",
527                     (int)rst->procedures[i].stats.max.secs,
528                     rst->procedures[i].stats.max.nsecs/10000);
529                 gtk_clist_set_text(rst->table, j, 4, strp);
530                 g_free(rst->procedures[i].entries[4]);
531                 rst->procedures[i].entries[4]=strp;
532
533                 strp=g_strdup_printf("%3" PRId64 ".%05" PRId64,
534                     td/100000, td%100000);
535                 gtk_clist_set_text(rst->table, j, 5, strp);
536                 g_free(rst->procedures[i].entries[5]);
537                 rst->procedures[i].entries[5]=strp;
538         }
539
540         gtk_clist_sort(rst->table);
541 }
542
543
544 void
545 reset_srt_table_data(srt_stat_table *rst)
546 {
547         int i;
548
549         for(i=0;i<rst->num_procs;i++){
550                 time_stat_init(&rst->procedures[i].stats);
551         }
552         gtk_clist_clear(rst->table);
553 }
554
555 void
556 free_srt_table_data(srt_stat_table *rst)
557 {
558         int i,j;
559
560         for(i=0;i<rst->num_procs;i++){
561                 for(j=0;j<6;j++){
562                         if(rst->procedures[i].entries[j]){
563                                 g_free(rst->procedures[i].entries[j]);
564                                 rst->procedures[i].entries[j]=NULL;
565                         }
566                 }
567         }
568         g_free(rst->filter_string);
569         rst->filter_string=NULL;
570         g_free(rst->procedures);
571         rst->procedures=NULL;
572         rst->num_procs=0;
573 }
574