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