1 /* service_response_time_table.c
2 * service_response_time_table 2003 Ronnie Sahlberg
3 * Helper routines common to all service response time statistics
8 * Wireshark - Network traffic analyzer
9 * By Gerald Combs <gerald@wireshark.org>
10 * Copyright 1998 Gerald Combs
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.
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.
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.
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"
43 #include "gtk/find_dlg.h"
45 #include "gtk/color_dlg.h"
47 #include "gui_utils.h"
48 #include "gtkglobals.h"
51 #define GTK_MENU_FUNC(a) ((GtkItemFactoryCallback)(a))
54 typedef struct column_arrows {
57 GtkWidget *descend_pm;
62 srt_click_column_cb(GtkCList *clist, gint column, gpointer data)
64 column_arrows *col_arrows = (column_arrows *) data;
67 gtk_clist_freeze(clist);
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);
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);
79 clist->sort_type = GTK_SORT_ASCENDING;
80 gtk_widget_show(col_arrows[column].ascend_pm);
83 /* Columns 2-5 Count, Min, Max, Avg are sorted in descending
85 Columns 0 and 1 sort by ascending order by default
88 clist->sort_type = GTK_SORT_DESCENDING;
89 gtk_widget_show(col_arrows[column].descend_pm);
91 clist->sort_type = GTK_SORT_ASCENDING;
92 gtk_widget_show(col_arrows[column].ascend_pm);
94 gtk_clist_set_sort_column(clist, column);
96 gtk_clist_thaw(clist);
98 gtk_clist_sort(clist);
102 srt_sort_column(GtkCList *clist, gconstpointer ptr1, gconstpointer ptr2)
109 const GtkCListRow *row1 = ptr1;
110 const GtkCListRow *row2 = ptr2;
112 text1 = GTK_CELL_TEXT (row1->cell[clist->sort_column])->text;
113 text2 = GTK_CELL_TEXT (row2->cell[clist->sort_column])->text;
115 switch(clist->sort_column){
117 return strcmp (text1, text2);
126 sscanf(text1,"%f",&f1);
127 sscanf(text2,"%f",&f2);
128 if(fabs(f1-f2)<0.000005)
134 g_assert_not_reached();
141 #define ACTION_MATCH 0
142 #define ACTION_PREPARE 1
143 #define ACTION_FIND_FRAME 2
144 #define ACTION_FIND_NEXT 3
145 #define ACTION_FIND_PREVIOUS 4
146 #define ACTION_COLORIZE 5
148 /* Action type - says what to do with the filter */
149 #define ACTYPE_SELECTED 0
150 #define ACTYPE_NOT_SELECTED 1
151 #define ACTYPE_AND_SELECTED 2
152 #define ACTYPE_OR_SELECTED 3
153 #define ACTYPE_AND_NOT_SELECTED 4
154 #define ACTYPE_OR_NOT_SELECTED 5
156 /* Encoded callback arguments */
157 #define CALLBACK_MATCH(type) ((ACTION_MATCH<<8) | (type))
158 #define CALLBACK_PREPARE(type) ((ACTION_PREPARE<<8) | (type))
159 #define CALLBACK_FIND_FRAME(type) ((ACTION_FIND_FRAME<<8) | (type))
160 #define CALLBACK_FIND_NEXT(type) ((ACTION_FIND_NEXT<<8) | (type))
161 #define CALLBACK_FIND_PREVIOUS(type) ((ACTION_FIND_PREVIOUS<<8) | (type))
162 #define CALLBACK_COLORIZE(type) ((ACTION_COLORIZE<<8) | (type))
164 /* Extract components of callback argument */
165 #define FILTER_ACTION(cb_arg) (((cb_arg)>>8) & 0xff)
166 #define FILTER_ACTYPE(cb_arg) ((cb_arg) & 0xff)
169 srt_select_filter_cb(GtkWidget *widget _U_, gpointer callback_data, guint callback_action)
171 int action, type, selection;
172 srt_stat_table *rst = (srt_stat_table *)callback_data;
174 const char *current_filter;
177 if(rst->filter_string==NULL){
181 action = FILTER_ACTION(callback_action);
182 type = FILTER_ACTYPE(callback_action);
184 selection=GPOINTER_TO_INT(g_list_nth_data(GTK_CLIST(rst->table)->selection, 0));
185 if(selection>=(int)rst->num_procs){
186 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "No procedure selected");
189 /* translate it back from row index to index in procedures array */
190 selection=GPOINTER_TO_INT(gtk_clist_get_row_data(rst->table, selection));
192 current_filter=gtk_entry_get_text(GTK_ENTRY(main_display_filter_widget));
195 case ACTYPE_SELECTED:
196 str = g_strdup_printf("%s==%d", rst->filter_string, selection);
198 case ACTYPE_NOT_SELECTED:
199 str = g_strdup_printf("!(%s==%d)", rst->filter_string,
202 case ACTYPE_AND_SELECTED:
203 if ((!current_filter) || (0 == strlen(current_filter)))
204 str = g_strdup_printf("%s==%d", rst->filter_string, selection);
206 str = g_strdup_printf("(%s) && (%s==%d)", current_filter,
207 rst->filter_string, selection);
209 case ACTYPE_OR_SELECTED:
210 if ((!current_filter) || (0 == strlen(current_filter)))
211 str = g_strdup_printf("%s==%d", rst->filter_string, selection);
213 str = g_strdup_printf("(%s) || (%s==%d)", current_filter,
214 rst->filter_string, selection);
216 case ACTYPE_AND_NOT_SELECTED:
217 if ((!current_filter) || (0 == strlen(current_filter)))
218 str = g_strdup_printf("!(%s==%d)", rst->filter_string, selection);
220 str = g_strdup_printf("(%s) && !(%s==%d)", current_filter,
221 rst->filter_string, selection);
223 case ACTYPE_OR_NOT_SELECTED:
224 if ((!current_filter) || (0 == strlen(current_filter)))
225 str = g_strdup_printf("!(%s==%d)", rst->filter_string, selection);
227 str = g_strdup_printf("(%s) || !(%s==%d)", current_filter,
228 rst->filter_string, selection);
232 gtk_entry_set_text(GTK_ENTRY(main_display_filter_widget), str);
236 main_filter_packets(&cfile, str, FALSE);
241 case ACTION_FIND_FRAME:
242 find_frame_with_filter(str);
244 case ACTION_FIND_NEXT:
245 find_previous_next_frame_with_filter(str, FALSE);
247 case ACTION_FIND_PREVIOUS:
248 find_previous_next_frame_with_filter(str, TRUE);
250 case ACTION_COLORIZE:
251 color_display_with_filter(str);
259 srt_show_popup_menu_cb(void *widg _U_, GdkEvent *event, srt_stat_table *rst)
261 GdkEventButton *bevent = (GdkEventButton *)event;
263 if(event->type==GDK_BUTTON_PRESS && bevent->button==3){
264 gtk_menu_popup(GTK_MENU(rst->menu), NULL, NULL, NULL, NULL,
265 bevent->button, bevent->time);
271 static GtkItemFactoryEntry srt_list_menu_items[] =
274 {"/Apply as Filter", NULL, NULL, 0, "<Branch>", NULL,},
275 {"/Apply as Filter/Selected", NULL,
276 GTK_MENU_FUNC(srt_select_filter_cb), CALLBACK_MATCH(ACTYPE_SELECTED), NULL, NULL,},
277 {"/Apply as Filter/... not Selected", NULL,
278 GTK_MENU_FUNC(srt_select_filter_cb), CALLBACK_MATCH(ACTYPE_NOT_SELECTED), NULL, NULL,},
279 {"/Apply as Filter/.. and Selected", NULL,
280 GTK_MENU_FUNC(srt_select_filter_cb), CALLBACK_MATCH(ACTYPE_AND_SELECTED), NULL, NULL,},
281 {"/Apply as Filter/... or Selected", NULL,
282 GTK_MENU_FUNC(srt_select_filter_cb), CALLBACK_MATCH(ACTYPE_OR_SELECTED), NULL, NULL,},
283 {"/Apply as Filter/... and not Selected", NULL,
284 GTK_MENU_FUNC(srt_select_filter_cb), CALLBACK_MATCH(ACTYPE_AND_NOT_SELECTED), NULL, NULL,},
285 {"/Apply as Filter/... or not Selected", NULL,
286 GTK_MENU_FUNC(srt_select_filter_cb), CALLBACK_MATCH(ACTYPE_OR_NOT_SELECTED), NULL, NULL,},
289 {"/Prepare a Filter", NULL, NULL, 0, "<Branch>", NULL,},
290 {"/Prepare a Filter/Selected", NULL,
291 GTK_MENU_FUNC(srt_select_filter_cb), CALLBACK_PREPARE(ACTYPE_SELECTED), NULL, NULL,},
292 {"/Prepare a Filter/Not Selected", NULL,
293 GTK_MENU_FUNC(srt_select_filter_cb), CALLBACK_PREPARE(ACTYPE_NOT_SELECTED), NULL, NULL,},
294 {"/Prepare a Filter/... and Selected", NULL,
295 GTK_MENU_FUNC(srt_select_filter_cb), CALLBACK_PREPARE(ACTYPE_AND_SELECTED), NULL, NULL,},
296 {"/Prepare a Filter/... or Selected", NULL,
297 GTK_MENU_FUNC(srt_select_filter_cb), CALLBACK_PREPARE(ACTYPE_OR_SELECTED), NULL, NULL,},
298 {"/Prepare a Filter/... and not Selected", NULL,
299 GTK_MENU_FUNC(srt_select_filter_cb), CALLBACK_PREPARE(ACTYPE_AND_NOT_SELECTED), NULL, NULL,},
300 {"/Prepare a Filter/... or not Selected", NULL,
301 GTK_MENU_FUNC(srt_select_filter_cb), CALLBACK_PREPARE(ACTYPE_OR_NOT_SELECTED), NULL, NULL,},
304 {"/Find Frame", NULL, NULL, 0, "<Branch>", NULL,},
305 {"/Find Frame/Find Frame", NULL, NULL, 0, "<Branch>", NULL,},
306 {"/Find Frame/Find Frame/Selected", NULL,
307 GTK_MENU_FUNC(srt_select_filter_cb), CALLBACK_FIND_FRAME(ACTYPE_SELECTED), NULL, NULL,},
308 {"/Find Frame/Find Frame/Not Selected", NULL,
309 GTK_MENU_FUNC(srt_select_filter_cb), CALLBACK_FIND_FRAME(ACTYPE_NOT_SELECTED), NULL, NULL,},
311 {"/Find Frame/Find Next", NULL, NULL, 0, "<Branch>", NULL,},
312 {"/Find Frame/Find Next/Selected", NULL,
313 GTK_MENU_FUNC(srt_select_filter_cb), CALLBACK_FIND_NEXT(ACTYPE_SELECTED), NULL, NULL,},
314 {"/Find Frame/Find Next/Not Selected", NULL,
315 GTK_MENU_FUNC(srt_select_filter_cb), CALLBACK_FIND_NEXT(ACTYPE_NOT_SELECTED), NULL, NULL,},
318 {"/Find Frame/Find Previous", NULL, NULL, 0, "<Branch>", NULL,},
319 {"/Find Frame/Find Previous/Selected", NULL,
320 GTK_MENU_FUNC(srt_select_filter_cb), CALLBACK_FIND_PREVIOUS(ACTYPE_SELECTED), NULL, NULL,},
321 {"/Find Frame/Find Previous/Not Selected", NULL,
322 GTK_MENU_FUNC(srt_select_filter_cb), CALLBACK_FIND_PREVIOUS(ACTYPE_NOT_SELECTED), NULL, NULL,},
324 /* Colorize Procedure */
325 {"/Colorize Procedure", NULL, NULL, 0, "<Branch>", NULL,},
326 {"/Colorize Procedure/Selected", NULL,
327 GTK_MENU_FUNC(srt_select_filter_cb), CALLBACK_COLORIZE(ACTYPE_SELECTED), NULL, NULL,},
328 {"/Colorize Procedure/Not Selected", NULL,
329 GTK_MENU_FUNC(srt_select_filter_cb), CALLBACK_COLORIZE(ACTYPE_NOT_SELECTED), NULL, NULL,}
334 srt_create_popup_menu(srt_stat_table *rst)
336 GtkItemFactory *item_factory;
338 item_factory = gtk_item_factory_new(GTK_TYPE_MENU, "<main>", NULL);
340 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);
342 rst->menu = gtk_item_factory_get_widget(item_factory, "<main>");
343 SIGNAL_CONNECT(rst->table, "button_press_event", srt_show_popup_menu_cb, rst);
348 init_srt_table(srt_stat_table *rst, int num_procs, GtkWidget *vbox, const char *filter_string)
351 column_arrows *col_arrows;
352 GdkBitmap *ascend_bm, *descend_bm;
353 GdkPixmap *ascend_pm, *descend_pm;
355 GtkWidget *column_lb;
356 const char *default_titles[] = { "Index", "Procedure", "Calls", "Min SRT", "Max SRT", "Avg SRT" };
360 rst->filter_string=g_strdup(filter_string);
362 rst->filter_string=NULL;
364 rst->scrolled_window=scrolled_window_new(NULL, NULL);
365 gtk_box_pack_start(GTK_BOX(vbox), rst->scrolled_window, TRUE, TRUE, 0);
367 rst->table=(GtkCList *)gtk_clist_new(6);
369 gtk_widget_show(GTK_WIDGET(rst->table));
370 gtk_widget_show(rst->scrolled_window);
372 col_arrows = (column_arrows *) g_malloc(sizeof(column_arrows) * 6);
373 win_style = gtk_widget_get_style(rst->scrolled_window);
374 ascend_pm = gdk_pixmap_create_from_xpm_d(rst->scrolled_window->window,
376 &win_style->bg[GTK_STATE_NORMAL],
377 (gchar **)clist_ascend_xpm);
378 descend_pm = gdk_pixmap_create_from_xpm_d(rst->scrolled_window->window,
380 &win_style->bg[GTK_STATE_NORMAL],
381 (gchar **)clist_descend_xpm);
382 for (i = 0; i < 6; i++) {
383 col_arrows[i].table = gtk_table_new(2, 2, FALSE);
384 gtk_table_set_col_spacings(GTK_TABLE(col_arrows[i].table), 5);
385 column_lb = gtk_label_new(default_titles[i]);
386 gtk_table_attach(GTK_TABLE(col_arrows[i].table), column_lb, 0, 1, 0, 2, GTK_SHRINK, GTK_SHRINK, 0, 0);
387 gtk_widget_show(column_lb);
389 col_arrows[i].ascend_pm = gtk_pixmap_new(ascend_pm, ascend_bm);
390 gtk_table_attach(GTK_TABLE(col_arrows[i].table), col_arrows[i].ascend_pm, 1, 2, 1, 2, GTK_SHRINK, GTK_SHRINK, 0, 0);
391 col_arrows[i].descend_pm = gtk_pixmap_new(descend_pm, descend_bm);
392 gtk_table_attach(GTK_TABLE(col_arrows[i].table), col_arrows[i].descend_pm, 1, 2, 0, 1, GTK_SHRINK, GTK_SHRINK, 0, 0);
394 gtk_widget_show(col_arrows[i].descend_pm);
396 gtk_clist_set_column_widget(GTK_CLIST(rst->table), i, col_arrows[i].table);
397 gtk_widget_show(col_arrows[i].table);
399 gtk_clist_column_titles_show(GTK_CLIST(rst->table));
401 gtk_clist_set_compare_func(rst->table, srt_sort_column);
402 gtk_clist_set_sort_column(rst->table, 2);
403 gtk_clist_set_sort_type(rst->table, GTK_SORT_DESCENDING);
406 /*XXX instead of this we should probably have some code to
407 dynamically adjust the width of the columns */
408 gtk_clist_set_column_width(rst->table, 0, 32);
409 gtk_clist_set_column_width(rst->table, 1, 160);
410 gtk_clist_set_column_width(rst->table, 2, 50);
411 gtk_clist_set_column_width(rst->table, 3, 60);
412 gtk_clist_set_column_width(rst->table, 4, 60);
413 gtk_clist_set_column_width(rst->table, 5, 60);
415 gtk_clist_set_shadow_type(rst->table, GTK_SHADOW_IN);
416 gtk_clist_column_titles_show(rst->table);
417 gtk_container_add(GTK_CONTAINER(rst->scrolled_window), (GtkWidget *)rst->table);
419 SIGNAL_CONNECT(rst->table, "click-column", srt_click_column_cb, col_arrows);
421 gtk_widget_show(GTK_WIDGET(rst->table));
422 gtk_widget_show(rst->scrolled_window);
425 rst->num_procs=num_procs;
426 rst->procedures=g_malloc(sizeof(srt_procedure_t)*num_procs);
427 for(i=0;i<num_procs;i++){
428 rst->procedures[i].stats.num=0;
429 rst->procedures[i].stats.min.secs=0;
430 rst->procedures[i].stats.min.nsecs=0;
431 rst->procedures[i].stats.max.secs=0;
432 rst->procedures[i].stats.max.nsecs=0;
433 rst->procedures[i].stats.tot.secs=0;
434 rst->procedures[i].stats.tot.nsecs=0;
436 rst->procedures[i].entries[j]=NULL;
440 /* create popup menu for this table */
441 if(rst->filter_string){
442 srt_create_popup_menu(rst);
447 init_srt_table_row(srt_stat_table *rst, int index, const char *procedure)
449 /* we have discovered a new procedure. Extend the table accordingly */
450 if(index>=rst->num_procs){
451 int old_num_procs=rst->num_procs;
453 rst->num_procs=index+1;
454 rst->procedures=g_realloc(rst->procedures, sizeof(srt_procedure_t)*(rst->num_procs));
455 for(i=old_num_procs;i<rst->num_procs;i++){
456 rst->procedures[i].stats.num=0;
457 rst->procedures[i].stats.min.secs=0;
458 rst->procedures[i].stats.min.nsecs=0;
459 rst->procedures[i].stats.max.secs=0;
460 rst->procedures[i].stats.max.nsecs=0;
461 rst->procedures[i].stats.tot.secs=0;
462 rst->procedures[i].stats.tot.nsecs=0;
464 rst->procedures[i].entries[j]=NULL;
468 rst->procedures[index].entries[0]=g_strdup_printf("%d", index);
470 rst->procedures[index].entries[1]=g_strdup(procedure);
472 rst->procedures[index].entries[2]=g_strdup("0");
473 rst->procedures[index].entries[3]=g_strdup("0");
474 rst->procedures[index].entries[4]=g_strdup("0");
475 rst->procedures[index].entries[5]=g_strdup("0");
479 add_srt_table_data(srt_stat_table *rst, int index, const nstime_t *req_time, packet_info *pinfo)
485 rp=&rst->procedures[index];
488 * If the count of calls for this procedure is currently zero, it's
489 * going to become non-zero, so add a row for it (we don't want
490 * rows for procedures that have no calls - especially if the
491 * procedure has no calls because the index doesn't correspond
492 * to a procedure, but is an unused/reserved value).
494 * (Yes, this means that the rows aren't in order by anything
495 * interesting. That's why we have the table sorted by a column.)
497 if (rp->stats.num==0){
498 row=gtk_clist_append(rst->table, rst->procedures[index].entries);
499 gtk_clist_set_row_data(rst->table, row, (gpointer)(long) index);
502 /* calculate time delta between request and reply */
504 nstime_delta(&delta, &t, req_time);
506 time_stat_update(&rp->stats, &delta, pinfo);
510 draw_srt_table_data(srt_stat_table *rst)
516 for(i=0;i<rst->num_procs;i++){
517 /* ignore procedures with no calls (they don't have CList rows) */
518 if(rst->procedures[i].stats.num==0){
522 /* scale it to units of 10us.*/
523 /* for long captures with a large tot time, this can overflow on 32bit */
524 td=(int)rst->procedures[i].stats.tot.secs;
525 td=td*100000+(int)rst->procedures[i].stats.tot.nsecs/10000;
526 td/=rst->procedures[i].stats.num;
528 j=gtk_clist_find_row_from_data(rst->table, (gpointer)(long)i);
529 strp=g_strdup_printf("%d", rst->procedures[i].stats.num);
530 gtk_clist_set_text(rst->table, j, 2, strp);
531 g_free(rst->procedures[i].entries[2]);
532 rst->procedures[i].entries[2]=strp;
535 strp=g_strdup_printf("%3d.%05d",
536 (int)rst->procedures[i].stats.min.secs,
537 rst->procedures[i].stats.min.nsecs/10000);
538 gtk_clist_set_text(rst->table, j, 3, strp);
539 g_free(rst->procedures[i].entries[3]);
540 rst->procedures[i].entries[3]=strp;
543 strp=g_strdup_printf("%3d.%05d",
544 (int)rst->procedures[i].stats.max.secs,
545 rst->procedures[i].stats.max.nsecs/10000);
546 gtk_clist_set_text(rst->table, j, 4, strp);
547 g_free(rst->procedures[i].entries[4]);
548 rst->procedures[i].entries[4]=strp;
550 strp=g_strdup_printf("%3" G_GINT64_MODIFIER "d.%05" G_GINT64_MODIFIER "d",
551 td/100000, td%100000);
552 gtk_clist_set_text(rst->table, j, 5, strp);
553 g_free(rst->procedures[i].entries[5]);
554 rst->procedures[i].entries[5]=strp;
557 gtk_clist_sort(rst->table);
562 reset_srt_table_data(srt_stat_table *rst)
566 for(i=0;i<rst->num_procs;i++){
567 time_stat_init(&rst->procedures[i].stats);
569 gtk_clist_clear(rst->table);
573 free_srt_table_data(srt_stat_table *rst)
577 for(i=0;i<rst->num_procs;i++){
579 if(rst->procedures[i].entries[j]){
580 g_free(rst->procedures[i].entries[j]);
581 rst->procedures[i].entries[j]=NULL;
585 g_free(rst->filter_string);
586 rst->filter_string=NULL;
587 g_free(rst->procedures);
588 rst->procedures=NULL;