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