summaryrefslogtreecommitdiff
path: root/ui/gtk/memory_dlg.c
blob: d11122c2aee4fa4ea0f008ee5da780c61b8e3dc3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
/* memory_dlg.c
 *
 * Based on
 * io_stat   2002 Ronnie Sahlberg
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */


#include "config.h"

#include <math.h>
#include <gtk/gtk.h>

#include "ui/gtk/dlg_utils.h"
#include "ui/simple_dialog.h"
#include "ui/gtk/gui_utils.h"
#include "ui/gtk/old-gtk-compat.h"

#include "wsutil/str_util.h"
#include "epan/app_mem_usage.h"

enum {
    MAX_GRAPHS = 10
};

#define MAX_YSCALE          28
static guint32 yscale_max[MAX_YSCALE] = {0, 1, 10, 20,
                                         50, 100, 200, 500, 1000, 2000, 5000, 10000, 20000,
                                         50000, 100000, 200000, 500000, 1000000, 2000000,
                                         5000000, 10000000, 20000000, 50000000, 100000000,
                                         200000000, 500000000, 1000000000, 2000000000};

#define DEFAULT_PIXELS_PER_TICK 5

#define NUM_IO_ITEMS 100000
typedef struct _io_item_t {
    gsize bytes;
} io_item_t;

typedef struct _io_stat_graph_t {
    struct _io_stat_t *io;

    io_item_t         *items[NUM_IO_ITEMS];
    gboolean           display;
    GtkWidget         *display_button;
} io_stat_graph_t;

typedef struct _io_stat_t {
    gboolean       needs_redraw;
    guint32        num_items;   /* total number of items in all intervals (zero relative) */
    guint32        left_x_border;
    guint32        right_x_border;
    nstime_t       start_time;

    struct _io_stat_graph_t graphs[MAX_GRAPHS];
    GtkWidget     *window;
    GtkWidget     *draw_area;
#if GTK_CHECK_VERSION(2,22,0)
    cairo_surface_t *surface;
#else
    GdkPixmap       *pixmap;
#endif
    int            surface_width;
    int            surface_height;
    int            pixels_per_tick;

    guint timer_id;
} io_stat_t;

#define INTERVAL 1000

static void
io_stat_reset(io_stat_t *io)
{
    int i, j;

    io->needs_redraw = TRUE;
    for (i=0; i<MAX_GRAPHS; i++) {
        for (j=0; j<NUM_IO_ITEMS; j++) {
            io_item_t *ioi;
            ioi = (io_item_t *)io->graphs[i].items[j];

            ioi->bytes      = 0;
        }
    }
    io->num_items        = 0;
    io->start_time.secs  = time(NULL);
    io->start_time.nsecs = 0;
}

static guint64
get_it_value(io_stat_t *io, int graph, int idx)
{
    io_item_t *it;

    g_assert(graph < MAX_GRAPHS);
    g_assert(idx < NUM_IO_ITEMS);

    it = (io_item_t *)io->graphs[graph].items[idx];

        return it->bytes;
}

static void
print_interval_string(char *buf, int buf_len, guint32 interval, io_stat_t *io)
{
    struct tm *tmp;
    time_t sec_val = interval/1000 + io->start_time.secs;
    gint32 nsec_val = interval%1000 + io->start_time.nsecs/1000000;

    if (nsec_val >= 1000) {
        sec_val++;
        nsec_val -= 1000;
    }
    tmp = localtime (&sec_val);
    if (tmp != NULL) {
        if (INTERVAL >= 1000) {
            g_snprintf(buf, buf_len, "%02d:%02d:%02d", tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
        } else if (INTERVAL >= 100) {
            g_snprintf(buf, buf_len, "%02d:%02d:%02d.%1d", tmp->tm_hour, tmp->tm_min, tmp->tm_sec, nsec_val/100);
        } else if (INTERVAL >= 10) {
            g_snprintf(buf, buf_len, "%02d:%02d:%02d.%02d", tmp->tm_hour, tmp->tm_min, tmp->tm_sec, nsec_val/10);
        } else {
            g_snprintf(buf, buf_len, "%02d:%02d:%02d.%03d", tmp->tm_hour, tmp->tm_min, tmp->tm_sec, nsec_val);
        }
    } else
        g_snprintf(buf, buf_len, "XX:XX:XX");
}

static void
io_stat_draw(io_stat_t *io)
{
    int            i;
    guint32        last_interval, first_interval, interval_delta;
    gint32         current_interval;
    guint32        top_y_border;
    guint32        bottom_y_border;
    PangoLayout   *layout;
    int            label_width, label_height;
    guint32        draw_width, draw_height;
    GtkAllocation  widget_alloc;

    /* new variables */
    guint32        num_time_intervals; /* number of intervals relative to 1 */
    guint64        max_value;   /* max value of seen data */
    guint32        max_y;       /* max value of the Y scale */
    cairo_t       *cr;

    if (!io->needs_redraw) {
        return;
    }
    io->needs_redraw = FALSE;
    /*
    * Find the length of the intervals we have data for
    * so we know how large arrays we need to malloc()
    */
    num_time_intervals = io->num_items+1;

    /* XXX move this check to _packet() */
    if (num_time_intervals > NUM_IO_ITEMS) {
        simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "IO-Stat error. There are too many entries, bailing out");
        return;
    }

    /*
    * find the max value so we can autoscale the y axis
    */
    max_value = 0;
    for (i=0; i<MAX_GRAPHS; i++) {
        int idx;

        if (!io->graphs[i].display) {
            continue;
        }
        for (idx=0; (guint32)(idx) < num_time_intervals; idx++) {
            guint64 val;

            val = get_it_value(io, i, idx);

            /* keep track of the max value we have encountered */
            if (val>max_value) {
                max_value = val;
            }
        }
    }

    /*
    * Clear out old plot
    */
#if GTK_CHECK_VERSION(2,22,0)
    cr = cairo_create (io->surface);
#else
    cr = gdk_cairo_create (io->pixmap);
#endif
    cairo_set_source_rgb (cr, 1, 1, 1);
    gtk_widget_get_allocation(io->draw_area, &widget_alloc);
    cairo_rectangle (cr, 0, 0, widget_alloc.width,widget_alloc.height);
    cairo_fill (cr);
    cairo_destroy (cr);
    /*
    * Calculate the y scale we should use
    */
    max_y = yscale_max[MAX_YSCALE-1];
    for (i=MAX_YSCALE-1; i>1; i--) {
        if (max_value < yscale_max[i]) {
            max_y = yscale_max[i];
        }
    }

    layout = gtk_widget_create_pango_layout(io->draw_area, "99999 T bytes");
    pango_layout_get_pixel_size(layout, &label_width, &label_height);

    io->left_x_border = 10;
    io->right_x_border = label_width + 20;
    top_y_border = 10;
    bottom_y_border = label_height + 20;

    /*
    * Calculate the size of the drawing area for the actual plot
    */
    draw_width = io->surface_width-io->right_x_border - io->left_x_border;
    draw_height = io->surface_height-top_y_border - bottom_y_border;

    /* Draw the y axis and labels
    * (we always draw the y scale with 11 ticks along the axis)
    */
#if GTK_CHECK_VERSION(2,22,0)
    cr = cairo_create(io->surface);
#else
    cr = gdk_cairo_create(io->pixmap);
#endif
    cairo_set_line_width(cr, 1.0);
    cairo_move_to(cr, io->surface_width-io->right_x_border+1.5, top_y_border + 0.5);
    cairo_line_to(cr, io->surface_width-io->right_x_border+1.5, io->surface_height-bottom_y_border + 0.5);
    cairo_stroke(cr);

    for (i=0; i<=10; i++) {
        int xwidth, lwidth, ypos;

        xwidth = 5;
            if (!(i%5)) {
                /* first, middle and last tick are slightly longer */
                xwidth = 10;
            }
            ypos = io->surface_height-bottom_y_border-draw_height*i/10;
        /* draw the tick */
        cairo_move_to(cr, io->surface_width-io->right_x_border+1.5, ypos+0.5);
        cairo_line_to(cr, io->surface_width-io->right_x_border+1.5+xwidth,ypos+0.5);
        cairo_stroke(cr);
        /* draw the labels */
        if (xwidth == 10) {
            guint32 value = (max_y/10)*i;
            char *label_tmp;

            label_tmp = format_size(value, format_size_unit_bytes);

            pango_layout_set_text(layout, label_tmp, -1);
            pango_layout_get_pixel_size(layout, &lwidth, NULL);

            cairo_move_to (cr, io->surface_width-io->right_x_border+15+label_width-lwidth, ypos-label_height/2);
            pango_cairo_show_layout (cr, layout);

            g_free(label_tmp);
        }
    }

    last_interval = (io->num_items) * INTERVAL;

    /*XXX*/
    /* plot the x-scale */
    cairo_move_to(cr, io->left_x_border+0.5, io->surface_height-bottom_y_border+1.5);
    cairo_line_to(cr, io->surface_width-io->right_x_border+1.5,io->surface_height-bottom_y_border+1.5);
    cairo_stroke(cr);
    if ((last_interval/INTERVAL) >= draw_width/io->pixels_per_tick) {
        first_interval  = (last_interval/INTERVAL)-draw_width/io->pixels_per_tick+1;
        first_interval *= INTERVAL;
    } else {
        first_interval = 0;
    }

    interval_delta = (100/io->pixels_per_tick)*INTERVAL;
    for (current_interval = last_interval;
         current_interval >= (gint32)first_interval;
         current_interval = current_interval-INTERVAL) {
        int x, xlen;

        /* if pixels_per_tick is 1 or 2, only draw every 10 ticks */
        /* if pixels_per_tick is 5, only draw every 5 ticks */
        if (((io->pixels_per_tick < 5) && (current_interval % (10*INTERVAL))) ||
            ((io->pixels_per_tick == 5) && (current_interval % (5*INTERVAL)))) {
                continue;
        }

        if (!(current_interval%interval_delta)) {
            xlen = 10;
        } else if (!(current_interval%(interval_delta/2))) {
            xlen = 8;
        } else {
            xlen = 5;
        }
        x = draw_width+io->left_x_border-((last_interval-current_interval)/INTERVAL)*io->pixels_per_tick;
        cairo_move_to(cr, x-1-io->pixels_per_tick/2+0.5, io->surface_height-bottom_y_border+1.5);
        cairo_line_to(cr, x-1-io->pixels_per_tick/2+0.5, io->surface_height-bottom_y_border+xlen+1.5);
        cairo_stroke(cr);
        if (xlen == 10) {
            char label_string[64];
            int lwidth, x_pos;
            print_interval_string (label_string, sizeof(label_string), current_interval, io);
            pango_layout_set_text(layout, label_string, -1);
            pango_layout_get_pixel_size(layout, &lwidth, NULL);

            if ((x-1-io->pixels_per_tick/2-lwidth/2) < 5) {
                x_pos = 5;
            } else if ((x-1-io->pixels_per_tick/2+lwidth/2) > (io->surface_width-5)) {
                x_pos = io->surface_width-lwidth-5;
            } else {
                x_pos = x-1-io->pixels_per_tick/2-lwidth/2;
            }
            cairo_move_to (cr, x_pos, io->surface_height-bottom_y_border+15);
            pango_cairo_show_layout (cr, layout);
        }

    }
    cairo_destroy (cr);
    cr = NULL;
    g_object_unref(G_OBJECT(layout));

    /*
    * Loop over all graphs and draw them
    */
#if GTK_CHECK_VERSION(2,22,0)
    cr = cairo_create (io->surface);
#else
    cr = gdk_cairo_create (io->pixmap);
#endif
    cairo_set_line_width (cr, 1.0);

    for (i=MAX_GRAPHS-1; i>=0; i--) {
        guint64 val;
        guint32 interval, x_pos, y_pos, prev_x_pos, prev_y_pos;

        if (!io->graphs[i].display) {
            continue;
        }

        /* initialize prev x/y to the value of the first interval */
        prev_x_pos = draw_width-1 -
            io->pixels_per_tick * ((last_interval - first_interval) / INTERVAL) +
            io->left_x_border;
        val = get_it_value(io, i, first_interval / INTERVAL);

        if (val>max_y) {
            prev_y_pos = 0;
        } else {
            prev_y_pos = (guint32)(draw_height-1-(val*draw_height)/max_y+top_y_border);
        }

        for (interval = first_interval;
             interval < last_interval;
             interval += INTERVAL) {
                x_pos = draw_width-1-io->pixels_per_tick*((last_interval-interval)/INTERVAL)+io->left_x_border;

                val = get_it_value(io, i, interval/INTERVAL);
                /* Moving average calculation */

                if (val>max_y) {
                    y_pos = 0;
                } else {
                    y_pos = (guint32)(draw_height - 1 -
                        ((val * draw_height) / max_y) +
                        top_y_border);
                }

                    /* Don't draw anything if the segment entirely above the top of the graph
                    */
                    if ( (prev_y_pos != 0) || (y_pos != 0) ) {
                        static GdkRGBA red_color = {1.0, 0.0, 0.1, 1.0};

                        cairo_move_to(cr, prev_x_pos+0.5, prev_y_pos+0.5);
                        cairo_line_to(cr, x_pos+0.5, y_pos+0.5);
                        gdk_cairo_set_source_rgba(cr, &red_color);
                        cairo_stroke(cr);
                    }

                prev_y_pos = y_pos;
                prev_x_pos = x_pos;
        }
    }
    cairo_destroy(cr);

    cr = gdk_cairo_create(gtk_widget_get_window(io->draw_area));

#if GTK_CHECK_VERSION(2,22,0)
    cairo_set_source_surface(cr, io->surface, 0, 0);
#else
    ws_gdk_cairo_set_source_pixmap(cr, io->pixmap, 0, 0);
#endif
    cairo_rectangle(cr, 0, 0, io->surface_width, io->surface_height);
    cairo_fill (cr);

    cairo_destroy (cr);
}

static void
io_stat_redraw(io_stat_t *io)
{
    io->needs_redraw = TRUE;
    io_stat_draw(io);
}


static void
draw_area_destroy_cb(GtkWidget *widget _U_, gpointer user_data)
{
    io_stat_t      *io           = (io_stat_t *)user_data;
    int             i,j;

    for (i=0; i<MAX_GRAPHS; i++) {
        if (io->graphs[i].display) {

            for (j=0; j<NUM_IO_ITEMS; j++) {
                g_free(io->graphs[i].items[j]);
                io->graphs[i].items[j] = NULL;
            }
        }
    }

    g_source_remove(io->timer_id);

    g_free(io);
}

/* create a new backing pixmap of the appropriate size */
static gboolean
draw_area_configure_event(GtkWidget *widget, GdkEventConfigure *event _U_, gpointer user_data)
{
    io_stat_t      *io           = (io_stat_t *)user_data;
    GtkAllocation   widget_alloc;
    cairo_t        *cr;

#if GTK_CHECK_VERSION(2,22,0)
    if (io->surface) {
         cairo_surface_destroy (io->surface);
        io->surface = NULL;
    }
#else
    if (io->pixmap) {
        g_object_unref(io->pixmap);
        io->pixmap = NULL;
    }
#endif

    gtk_widget_get_allocation(widget, &widget_alloc);
#if GTK_CHECK_VERSION(2,22,0)
    io->surface = gdk_window_create_similar_surface (gtk_widget_get_window(widget),
            CAIRO_CONTENT_COLOR,
            widget_alloc.width,
            widget_alloc.height);

#else
    io->pixmap = gdk_pixmap_new(gtk_widget_get_window(widget),
            widget_alloc.width,
            widget_alloc.height,
            -1);
#endif
    io->surface_width = widget_alloc.width;
    io->surface_height = widget_alloc.height;

#if GTK_CHECK_VERSION(2,22,0)
    cr = cairo_create(io->surface);
#else
    cr = gdk_cairo_create(io->pixmap);
#endif
    cairo_rectangle(cr, 0, 0, widget_alloc.width, widget_alloc.height);
    cairo_set_source_rgb(cr, 1, 1, 1);
    cairo_fill(cr);
    cairo_destroy(cr);

    io_stat_redraw(io);
    return TRUE;
}

#if GTK_CHECK_VERSION(3,0,0)
static gboolean
draw_area_draw(GtkWidget *widget, cairo_t *cr, gpointer user_data)
{
    io_stat_t     *io = (io_stat_t *)user_data;
    GtkAllocation  allocation;

    gtk_widget_get_allocation(widget, &allocation);
    cairo_set_source_surface(cr, io->surface, 0, 0);
    cairo_rectangle(cr, 0, 0, allocation.width, allocation.width);
    cairo_fill (cr);

    return FALSE;
}
#else
/* redraw the screen from the backing pixmap */
static gboolean
draw_area_expose_event(GtkWidget *widget, GdkEventExpose *event, gpointer user_data)
{
    io_stat_t *io = (io_stat_t *)user_data;
    cairo_t   *cr = gdk_cairo_create (gtk_widget_get_window(widget));

#if GTK_CHECK_VERSION(2,22,0)
    cairo_set_source_surface (cr, io->surface, 0, 0);
#else
    ws_gdk_cairo_set_source_pixmap (cr, io->pixmap, 0, 0);
#endif
    cairo_rectangle (cr, event->area.x, event->area.y, event->area.width, event->area.height);
    cairo_fill (cr);

    cairo_destroy (cr);

    return FALSE;
}
#endif
static void
create_draw_area(io_stat_t *io, GtkWidget *box)
{
    io->draw_area = gtk_drawing_area_new();
    g_signal_connect(io->draw_area, "destroy", G_CALLBACK(draw_area_destroy_cb), io);

    gtk_widget_set_size_request(io->draw_area, io->surface_width, io->surface_height);

    /* signals needed to handle backing pixmap */
#if GTK_CHECK_VERSION(3,0,0)
    g_signal_connect(io->draw_area, "draw", G_CALLBACK(draw_area_draw), io);
#else
    g_signal_connect(io->draw_area, "expose-event", G_CALLBACK(draw_area_expose_event), io);
#endif
    g_signal_connect(io->draw_area, "configure-event", G_CALLBACK(draw_area_configure_event), io);
    gtk_widget_add_events (io->draw_area, GDK_BUTTON_PRESS_MASK);

    gtk_widget_show(io->draw_area);
    gtk_box_pack_start(GTK_BOX(box), io->draw_area, TRUE, TRUE, 0);
}

static void
filter_callback(GtkWidget *widget _U_, gpointer user_data)
{
    io_stat_graph_t   *gio   = (io_stat_graph_t *)user_data;

    /* this graph is not active, just update display and redraw */
    if (!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(gio->display_button))) {
        gio->display = FALSE;
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(gio->display_button), FALSE);
    } else {
        gio->display = TRUE;
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(gio->display_button), TRUE);
    }

    gdk_window_raise(gtk_widget_get_window(gio->io->window));
    io_stat_redraw(gio->io);
}

static void
create_filter_area(io_stat_t *io, GtkWidget *box)
{
    GtkWidget *frame;
    GtkWidget *hbox;
    int i;

    frame = gtk_frame_new("Memory Graphs");
    gtk_box_pack_start(GTK_BOX(box), frame, TRUE, TRUE, 0);
    gtk_widget_show(frame);

    hbox = ws_gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 1, FALSE);
    gtk_container_add(GTK_CONTAINER(frame), hbox);
    gtk_container_set_border_width(GTK_CONTAINER(hbox), 3);
    gtk_widget_show(hbox);

    for (i=0; i<MAX_GRAPHS; i++) {
        const char *label = memory_usage_get(i, NULL);
        GtkWidget *display_button;

        if (!label)
            break;

        display_button = gtk_toggle_button_new_with_label(label);
        gtk_box_pack_start(GTK_BOX(hbox), display_button, FALSE, FALSE, 0);
        g_signal_connect(display_button, "toggled", G_CALLBACK(filter_callback), &io->graphs[i]);
        gtk_widget_show(display_button);

        io->graphs[i].display_button = display_button;
    }
}

static void
init_io_stat_window(io_stat_t *io)
{
    GtkWidget *vbox;
    GtkWidget *hbox;
    GtkWidget *bbox;
    GtkWidget *close_bt;

    /* create the main window, transient_for top_level */
    io->window = dlg_window_new("Wireshark memory usage");
    gtk_window_set_destroy_with_parent (GTK_WINDOW(io->window), TRUE);

    vbox = ws_gtk_box_new(GTK_ORIENTATION_VERTICAL, 0, FALSE);
    gtk_container_add(GTK_CONTAINER(io->window), vbox);
    gtk_widget_show(vbox);

    create_draw_area(io, vbox);

    hbox = ws_gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 3, FALSE);
    gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 5);
    gtk_container_set_border_width(GTK_CONTAINER(hbox), 3);
    gtk_widget_show(hbox);

    create_filter_area(io, hbox);

    bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
    gtk_box_pack_start(GTK_BOX(vbox), bbox, FALSE, FALSE, 0);
    gtk_widget_show(bbox);

    close_bt = (GtkWidget *)g_object_get_data(G_OBJECT(bbox), GTK_STOCK_CLOSE);
    window_set_cancel_button(io->window, close_bt, window_cancel_button_cb);
    gtk_widget_set_tooltip_text(close_bt,  "Close this dialog");

    gtk_widget_show(io->window);
    window_present(io->window);
}


static gboolean
call_it(gpointer user_data)
{
    io_stat_t *io = (io_stat_t *) user_data;
    char buf[64];
    char *tmp;
    int idx, i;

    io->needs_redraw = TRUE;

    idx = io->num_items++;

    /* some sanity checks */
    if ((idx < 0) || (idx >= NUM_IO_ITEMS)) {
        io->num_items = NUM_IO_ITEMS-1;
        return FALSE;
    }


    for (i = 0; i < MAX_GRAPHS; i++) {
        const char *label;

        label = memory_usage_get(i, &io->graphs[i].items[idx]->bytes);

        if (!label)
           break;

        tmp = format_size(io->graphs[i].items[idx]->bytes, format_size_unit_bytes);
        g_snprintf(buf, sizeof(buf), "%s [%s]", label, tmp);
        gtk_button_set_label(GTK_BUTTON(io->graphs[i].display_button), buf);
        g_free(tmp);
    }

    io_stat_draw(io);

    return TRUE;
}

void
memory_stat_init(void)
{
    io_stat_t *io;
    int i = 0, j = 0;

    io = g_new(io_stat_t,1);
    io->needs_redraw         = TRUE;
    io->window               = NULL;
    io->draw_area            = NULL;
#if GTK_CHECK_VERSION(2,22,0)
    io->surface              = NULL;
#else
    io->pixmap               = NULL;
#endif
    io->surface_width        = 500;
    io->surface_height       = 200;
    io->pixels_per_tick      = DEFAULT_PIXELS_PER_TICK;
    io->num_items            = 0;
    io->left_x_border        = 0;
    io->right_x_border       = 500;
    io->start_time.secs      = time(NULL);
    io->start_time.nsecs     = 0;

    for (i=0; i<MAX_GRAPHS; i++) {
        io->graphs[i].display                   = 0;
        io->graphs[i].display_button            = NULL;
        io->graphs[i].io                        = io;

        for (j=0; j<NUM_IO_ITEMS; j++) {
            io->graphs[i].items[j] = g_new(io_item_t,1);
        }
    }
    io_stat_reset(io);

    /* build the GUI */
    init_io_stat_window(io);
    io->graphs[0].display = TRUE;
    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(io->graphs[0].display_button), TRUE);

    gdk_window_raise(gtk_widget_get_window(io->window));
    io_stat_redraw(io);

    io->timer_id = g_timeout_add(INTERVAL, call_it, io);
}


/*
 * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
 *
 * Local variables:
 * c-basic-offset: 4
 * tab-width: 8
 * indent-tabs-mode: nil
 * End:
 *
 * vi: set shiftwidth=4 tabstop=8 expandtab:
 * :indentSize=4:tabSize=8:noTabs=true:
 */