Don't update CR fields
[fio.git] / gfio.c
1 /*
2  * gfio - gui front end for fio - the flexible io tester
3  *
4  * Copyright (C) 2012 Stephen M. Cameron <stephenmcameron@gmail.com> 
5  *
6  * The license below covers all files distributed with fio unless otherwise
7  * noted in the file itself.
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License version 2 as
11  *  published by the Free Software Foundation.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  */
23 #include <locale.h>
24 #include <malloc.h>
25
26 #include <glib.h>
27 #include <gtk/gtk.h>
28
29 #include "fio.h"
30
31 static void gfio_update_thread_status(char *status_message, double perc);
32
33 #define ARRAYSIZE(x) (sizeof((x)) / (sizeof((x)[0])))
34
35 typedef void (*clickfunction)(GtkWidget *widget, gpointer data);
36
37 static void connect_clicked(GtkWidget *widget, gpointer data);
38 static void start_job_clicked(GtkWidget *widget, gpointer data);
39
40 static struct button_spec {
41         const char *buttontext;
42         clickfunction f;
43         const char *tooltiptext;
44         const int start_insensitive;
45 } buttonspeclist[] = {
46 #define CONNECT_BUTTON 0
47 #define START_JOB_BUTTON 1
48         { "Connect", connect_clicked, "Connect to host", 0 },
49         { "Start Job",
50                 start_job_clicked,
51                 "Send current fio job to fio server to be executed", 1 },
52 };
53
54 struct probe_widget {
55         GtkWidget *hostname;
56         GtkWidget *os;
57         GtkWidget *arch;
58         GtkWidget *fio_ver;
59 };
60
61 struct eta_widget {
62         GtkWidget *name;
63         GtkWidget *iotype;
64         GtkWidget *ioengine;
65         GtkWidget *iodepth;
66         GtkWidget *jobs;
67         GtkWidget *files;
68         GtkWidget *read_bw;
69         GtkWidget *read_iops;
70         GtkWidget *cr_bw;
71         GtkWidget *cr_iops;
72         GtkWidget *write_bw;
73         GtkWidget *write_iops;
74         GtkWidget *cw_bw;
75         GtkWidget *cw_iops;
76 };
77
78 struct gui {
79         GtkWidget *window;
80         GtkWidget *vbox;
81         GtkWidget *topvbox;
82         GtkWidget *topalign;
83         GtkWidget *bottomalign;
84         GtkWidget *thread_status_pb;
85         GtkWidget *buttonbox;
86         GtkWidget *button[ARRAYSIZE(buttonspeclist)];
87         GtkWidget *hostname_hbox;
88         GtkWidget *hostname_label;
89         GtkWidget *hostname_entry;
90         GtkWidget *port_button;
91         GtkWidget *port_label;
92         GtkWidget *hostname_combo_box; /* ipv4, ipv6 or socket */
93         GtkWidget *scrolled_window;
94         GtkWidget *textview;
95         GtkWidget *error_info_bar;
96         GtkWidget *error_label;
97         GtkTextBuffer *text;
98         struct probe_widget probe;
99         struct eta_widget eta;
100         int connected;
101         pthread_t t;
102
103         struct fio_client *client;
104         int nr_job_files;
105         char **job_files;
106 } ui;
107
108 static void gfio_set_connected(struct gui *ui, int connected)
109 {
110         if (connected) {
111                 gtk_widget_set_sensitive(ui->button[START_JOB_BUTTON], 1);
112                 ui->connected = 1;
113                 gtk_button_set_label(GTK_BUTTON(ui->button[CONNECT_BUTTON]), "Disconnect");
114         } else {
115                 ui->connected = 0;
116                 gtk_button_set_label(GTK_BUTTON(ui->button[CONNECT_BUTTON]), "Connect");
117                 gtk_widget_set_sensitive(ui->button[START_JOB_BUTTON], 0);
118         }
119 }
120
121 static void gfio_text_op(struct fio_client *client,
122                 FILE *f, __u16 pdu_len, const char *buf)
123 {
124 #if 0
125         GtkTextBuffer *buffer;
126         GtkTextIter end;
127
128         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(ui.textview));
129         gdk_threads_enter();
130         gtk_text_buffer_get_end_iter(buffer, &end);
131         gtk_text_buffer_insert(buffer, &end, buf, -1);
132         gdk_threads_leave();
133         gtk_text_view_scroll_to_iter(GTK_TEXT_VIEW(ui.textview),
134                                         &end, 0.0, FALSE, 0.0,0.0);
135 #else
136         fio_client_ops.text_op(client, f, pdu_len, buf);
137 #endif
138 }
139
140 static void gfio_disk_util_op(struct fio_client *client, struct fio_net_cmd *cmd)
141 {
142         printf("gfio_disk_util_op called\n");
143         fio_client_ops.disk_util(client, cmd);
144 }
145
146 static void gfio_thread_status_op(struct fio_net_cmd *cmd)
147 {
148         printf("gfio_thread_status_op called\n");
149         fio_client_ops.thread_status(cmd);
150 }
151
152 static void gfio_group_stats_op(struct fio_net_cmd *cmd)
153 {
154         printf("gfio_group_stats_op called\n");
155         fio_client_ops.group_stats(cmd);
156 }
157
158 static void gfio_update_eta(struct jobs_eta *je)
159 {
160         static int eta_good;
161         char eta_str[128];
162         char output[256];
163         char tmp[32];
164         double perc = 0.0;
165         int i2p = 0;
166
167         eta_str[0] = '\0';
168         output[0] = '\0';
169
170         if (je->eta_sec != INT_MAX && je->elapsed_sec) {
171                 perc = (double) je->elapsed_sec / (double) (je->elapsed_sec + je->eta_sec);
172                 eta_to_str(eta_str, je->eta_sec);
173         }
174
175         sprintf(tmp, "%u", je->nr_running);
176         gtk_label_set_text(GTK_LABEL(ui.eta.jobs), tmp);
177         sprintf(tmp, "%u", je->files_open);
178         gtk_label_set_text(GTK_LABEL(ui.eta.files), tmp);
179
180 #if 0
181         if (je->m_rate[0] || je->m_rate[1] || je->t_rate[0] || je->t_rate[1]) {
182         if (je->m_rate || je->t_rate) {
183                 char *tr, *mr;
184
185                 mr = num2str(je->m_rate, 4, 0, i2p);
186                 tr = num2str(je->t_rate, 4, 0, i2p);
187                 gtk_label_set_text(GTK_LABEL(ui.eta.
188                 p += sprintf(p, ", CR=%s/%s KB/s", tr, mr);
189                 free(tr);
190                 free(mr);
191         } else if (je->m_iops || je->t_iops)
192                 p += sprintf(p, ", CR=%d/%d IOPS", je->t_iops, je->m_iops);
193
194         gtk_label_set_text(GTK_LABEL(ui.eta.cr_bw), "---");
195         gtk_label_set_text(GTK_LABEL(ui.eta.cr_iops), "---");
196         gtk_label_set_text(GTK_LABEL(ui.eta.cw_bw), "---");
197         gtk_label_set_text(GTK_LABEL(ui.eta.cw_iops), "---");
198 #endif
199
200         if (je->eta_sec != INT_MAX && je->nr_running) {
201                 char *iops_str[2];
202                 char *rate_str[2];
203
204                 if ((!je->eta_sec && !eta_good) || je->nr_ramp == je->nr_running)
205                         strcpy(output, "-.-% done");
206                 else {
207                         eta_good = 1;
208                         perc *= 100.0;
209                         sprintf(output, "%3.1f%% done", perc);
210                 }
211
212                 rate_str[0] = num2str(je->rate[0], 5, 10, i2p);
213                 rate_str[1] = num2str(je->rate[1], 5, 10, i2p);
214
215                 iops_str[0] = num2str(je->iops[0], 4, 1, 0);
216                 iops_str[1] = num2str(je->iops[1], 4, 1, 0);
217
218                 gtk_label_set_text(GTK_LABEL(ui.eta.read_bw), rate_str[0]);
219                 gtk_label_set_text(GTK_LABEL(ui.eta.read_iops), iops_str[0]);
220                 gtk_label_set_text(GTK_LABEL(ui.eta.write_bw), rate_str[1]);
221                 gtk_label_set_text(GTK_LABEL(ui.eta.write_iops), iops_str[1]);
222
223                 free(rate_str[0]);
224                 free(rate_str[1]);
225                 free(iops_str[0]);
226                 free(iops_str[1]);
227         }
228
229         if (eta_str[0]) {
230                 char *dst = output + strlen(output);
231
232                 sprintf(dst, " - %s", eta_str);
233         }
234                 
235         gfio_update_thread_status(output, perc);
236 }
237
238 static void gfio_eta_op(struct fio_client *client, struct fio_net_cmd *cmd)
239 {
240         struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
241         struct client_eta *eta = (struct client_eta *) (uintptr_t) cmd->tag;
242
243         client->eta_in_flight = NULL;
244         flist_del_init(&client->eta_list);
245
246         fio_client_convert_jobs_eta(je);
247         fio_client_sum_jobs_eta(&eta->eta, je);
248         fio_client_dec_jobs_eta(eta, gfio_update_eta);
249 }
250
251 static void gfio_probe_op(struct fio_client *client, struct fio_net_cmd *cmd)
252 {
253         struct cmd_probe_pdu *probe = (struct cmd_probe_pdu *) cmd->payload;
254         const char *os, *arch;
255         char buf[64];
256
257         os = fio_get_os_string(probe->os);
258         if (!os)
259                 os = "unknown";
260
261         arch = fio_get_arch_string(probe->arch);
262         if (!arch)
263                 os = "unknown";
264
265         if (!client->name)
266                 client->name = strdup((char *) probe->hostname);
267
268         gtk_label_set_text(GTK_LABEL(ui.probe.hostname), (char *) probe->hostname);
269         gtk_label_set_text(GTK_LABEL(ui.probe.os), os);
270         gtk_label_set_text(GTK_LABEL(ui.probe.arch), arch);
271         sprintf(buf, "%u.%u.%u", probe->fio_major, probe->fio_minor, probe->fio_patch);
272         gtk_label_set_text(GTK_LABEL(ui.probe.fio_ver), buf);
273 }
274
275 static void gfio_update_thread_status(char *status_message, double perc)
276 {
277         static char message[100];
278         const char *m = message;
279
280         strncpy(message, status_message, sizeof(message) - 1);
281         gtk_progress_bar_set_text(
282                 GTK_PROGRESS_BAR(ui.thread_status_pb), m);
283         gtk_progress_bar_set_fraction(
284                 GTK_PROGRESS_BAR(ui.thread_status_pb), perc / 100.0);
285         gdk_threads_enter();
286         gtk_widget_queue_draw(ui.window);
287         gdk_threads_leave();
288 }
289
290 static void gfio_quit_op(struct fio_client *client)
291 {
292         struct gui *ui = client->client_data;
293
294         gfio_set_connected(ui, 0);
295 }
296
297 static void gfio_add_job_op(struct fio_client *client, struct fio_net_cmd *cmd)
298 {
299         struct cmd_add_job_pdu *p = (struct cmd_add_job_pdu *) cmd->payload;
300         struct gui *ui = client->client_data;
301         char tmp[8];
302         int i;
303
304         p->iodepth              = le32_to_cpu(p->iodepth);
305         p->rw                   = le32_to_cpu(p->rw);
306
307         for (i = 0; i < 2; i++) {
308                 p->min_bs[i]    = le32_to_cpu(p->min_bs[i]);
309                 p->max_bs[i]    = le32_to_cpu(p->max_bs[i]);
310         }
311
312         p->numjobs              = le32_to_cpu(p->numjobs);
313         p->group_reporting      = le32_to_cpu(p->group_reporting);
314
315         gtk_label_set_text(GTK_LABEL(ui->eta.name), (gchar *) p->jobname);
316         gtk_label_set_text(GTK_LABEL(ui->eta.iotype), ddir_str(p->rw));
317         gtk_label_set_text(GTK_LABEL(ui->eta.ioengine), (gchar *) p->ioengine);
318
319         sprintf(tmp, "%u", p->iodepth);
320         gtk_label_set_text(GTK_LABEL(ui->eta.iodepth), tmp);
321 }
322
323 struct client_ops gfio_client_ops = {
324         .text_op                = gfio_text_op,
325         .disk_util              = gfio_disk_util_op,
326         .thread_status          = gfio_thread_status_op,
327         .group_stats            = gfio_group_stats_op,
328         .eta                    = gfio_eta_op,
329         .probe                  = gfio_probe_op,
330         .quit                   = gfio_quit_op,
331         .add_job                = gfio_add_job_op,
332         .stay_connected         = 1,
333 };
334
335 static void quit_clicked(__attribute__((unused)) GtkWidget *widget,
336                 __attribute__((unused)) gpointer data)
337 {
338         gtk_main_quit();
339 }
340
341 static void *job_thread(void *arg)
342 {
343         printf("job thread starts\n");
344         fio_handle_clients(&gfio_client_ops);
345         printf("job thread exits\n");
346         return NULL;
347 }
348
349 static int send_job_files(struct gui *ui)
350 {
351         int i, ret = 0;
352
353         for (i = 0; i < ui->nr_job_files; i++) {
354                 ret = fio_clients_send_ini(ui->job_files[i]);
355                 if (ret)
356                         break;
357
358                 free(ui->job_files[i]);
359                 ui->job_files[i] = NULL;
360         }
361         while (i < ui->nr_job_files) {
362                 free(ui->job_files[i]);
363                 ui->job_files[i] = NULL;
364                 i++;
365         }
366
367         return ret;
368 }
369
370 static void start_job_thread(struct gui *ui)
371 {
372         if (send_job_files(ui)) {
373                 printf("Yeah, I didn't really like those options too much.\n");
374                 gtk_widget_set_sensitive(ui->button[START_JOB_BUTTON], 1);
375                 return;
376         }
377 }
378
379 static void start_job_clicked(__attribute__((unused)) GtkWidget *widget,
380                 gpointer data)
381 {
382         struct gui *ui = data;
383
384         gtk_widget_set_sensitive(ui->button[START_JOB_BUTTON], 0);
385         start_job_thread(ui);
386 }
387
388 static void connect_clicked(__attribute__((unused)) GtkWidget *widget,
389                 gpointer data)
390 {
391         struct gui *ui = data;
392
393         if (!ui->connected) {
394                 fio_clients_connect();
395                 pthread_create(&ui->t, NULL, job_thread, NULL);
396                 gfio_set_connected(ui, 1);
397         } else
398                 gfio_set_connected(ui, 0);
399 }
400
401 static void add_button(struct gui *ui, int i, GtkWidget *buttonbox,
402                         struct button_spec *buttonspec)
403 {
404         ui->button[i] = gtk_button_new_with_label(buttonspec->buttontext);
405         g_signal_connect(ui->button[i], "clicked", G_CALLBACK (buttonspec->f), ui);
406         gtk_box_pack_start(GTK_BOX (ui->buttonbox), ui->button[i], FALSE, FALSE, 3);
407         gtk_widget_set_tooltip_text(ui->button[i], buttonspeclist[i].tooltiptext);
408         gtk_widget_set_sensitive(ui->button[i], !buttonspec->start_insensitive);
409 }
410
411 static void add_buttons(struct gui *ui,
412                                 struct button_spec *buttonlist,
413                                 int nbuttons)
414 {
415         int i;
416
417         for (i = 0; i < nbuttons; i++)
418                 add_button(ui, i, ui->buttonbox, &buttonlist[i]);
419 }
420
421 static void on_info_bar_response(GtkWidget *widget, gint response,
422                                  gpointer data)
423 {
424         if (response == GTK_RESPONSE_OK) {
425                 gtk_widget_destroy(widget);
426                 ui.error_info_bar = NULL;
427         }
428 }
429
430 void report_error(GError* error)
431 {
432         if (ui.error_info_bar == NULL) {
433                 ui.error_info_bar = gtk_info_bar_new_with_buttons(GTK_STOCK_OK,
434                                                                GTK_RESPONSE_OK,
435                                                                NULL);
436                 g_signal_connect(ui.error_info_bar, "response", G_CALLBACK(on_info_bar_response), NULL);
437                 gtk_info_bar_set_message_type(GTK_INFO_BAR(ui.error_info_bar),
438                                               GTK_MESSAGE_ERROR);
439                 
440                 ui.error_label = gtk_label_new(error->message);
441                 GtkWidget *container = gtk_info_bar_get_content_area(GTK_INFO_BAR(ui.error_info_bar));
442                 gtk_container_add(GTK_CONTAINER(container), ui.error_label);
443                 
444                 gtk_box_pack_start(GTK_BOX(ui.vbox), ui.error_info_bar, FALSE, FALSE, 0);
445                 gtk_widget_show_all(ui.vbox);
446         } else {
447                 char buffer[256];
448                 snprintf(buffer, sizeof(buffer), "Failed to open file.");
449                 gtk_label_set(GTK_LABEL(ui.error_label), buffer);
450         }
451 }
452
453 static void file_open(GtkWidget *w, gpointer data)
454 {
455         GtkWidget *dialog;
456         GSList *filenames, *fn_glist;
457         GtkFileFilter *filter;
458
459         dialog = gtk_file_chooser_dialog_new("Open File",
460                 GTK_WINDOW(ui.window),
461                 GTK_FILE_CHOOSER_ACTION_OPEN,
462                 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
463                 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
464                 NULL);
465         gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), TRUE);
466
467         filter = gtk_file_filter_new();
468         gtk_file_filter_add_pattern(filter, "*.fio");
469         gtk_file_filter_add_pattern(filter, "*.job");
470         gtk_file_filter_add_mime_type(filter, "text/fio");
471         gtk_file_filter_set_name(filter, "Fio job file");
472         gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(dialog), filter);
473
474         if (gtk_dialog_run(GTK_DIALOG(dialog)) != GTK_RESPONSE_ACCEPT) {
475                 gtk_widget_destroy(dialog);
476                 return;
477         }
478
479         fn_glist = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog));
480         filenames = fn_glist;
481         while (filenames != NULL) {
482                 const char *hostname;
483                 char *typeentry;
484                 gint port;
485                 int type;
486
487                 ui.job_files = realloc(ui.job_files, (ui.nr_job_files + 1) * sizeof(char *));
488                 ui.job_files[ui.nr_job_files] = strdup(filenames->data);
489                 ui.nr_job_files++;
490
491                 hostname = gtk_entry_get_text(GTK_ENTRY(ui.hostname_entry));
492                 port = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(ui.port_button));
493                 typeentry = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(ui.hostname_combo_box));
494                 if (!typeentry || !strncmp(typeentry, "IPv4", 4))
495                         type = Fio_client_ipv4;
496                 else if (!strncmp(typeentry, "IPv6", 4))
497                         type = Fio_client_ipv6;
498                 else
499                         type = Fio_client_socket;
500                 g_free(typeentry);
501
502                 ui.client = fio_client_add_explicit(hostname, type, port);
503                 ui.client->client_data = &ui;
504 #if 0
505                 if (error) {
506                         report_error(error);
507                         g_error_free(error);
508                         error = NULL;
509                 }
510 #endif
511                         
512                 g_free(filenames->data);
513                 filenames = g_slist_next(filenames);
514         }
515         g_slist_free(fn_glist);
516         gtk_widget_destroy(dialog);
517 }
518
519 static void file_save(GtkWidget *w, gpointer data)
520 {
521         GtkWidget *dialog;
522
523         dialog = gtk_file_chooser_dialog_new("Save File",
524                 GTK_WINDOW(ui.window),
525                 GTK_FILE_CHOOSER_ACTION_SAVE,
526                 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
527                 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
528                 NULL);
529
530         gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(dialog), TRUE);
531         gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), "Untitled document");
532
533         if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
534                 char *filename;
535
536                 filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
537                 // save_job_file(filename);
538                 g_free(filename);
539         }
540         gtk_widget_destroy(dialog);
541 }
542
543 static void about_dialog(GtkWidget *w, gpointer data)
544 {
545         gtk_show_about_dialog(NULL,
546                 "program-name", "gfio",
547                 "comments", "Gtk2 UI for fio",
548                 "license", "GPLv2",
549                 "version", fio_version_string,
550                 "copyright", "Jens Axboe <axboe@kernel.dk> 2012",
551                 "logo-icon-name", "fio",
552                 /* Must be last: */
553                 NULL, NULL,
554                 NULL);
555 }
556
557 static GtkActionEntry menu_items[] = {
558         { "FileMenuAction", GTK_STOCK_FILE, "File", NULL, NULL, NULL},
559         { "HelpMenuAction", GTK_STOCK_HELP, "Help", NULL, NULL, NULL},
560         { "OpenFile",       GTK_STOCK_OPEN, NULL,   "<Control>O", NULL, G_CALLBACK(file_open) },
561         { "SaveFile",       GTK_STOCK_SAVE, NULL,   "<Control>S", NULL, G_CALLBACK(file_save) },
562         { "Quit",           GTK_STOCK_QUIT, NULL,   "<Control>Q", NULL, G_CALLBACK(quit_clicked) },
563         { "About",          GTK_STOCK_ABOUT, NULL,  NULL, NULL, G_CALLBACK(about_dialog) },
564 };
565 static gint nmenu_items = sizeof(menu_items) / sizeof(menu_items[0]);
566
567 static const gchar *ui_string = " \
568         <ui> \
569                 <menubar name=\"MainMenu\"> \
570                         <menu name=\"FileMenu\" action=\"FileMenuAction\"> \
571                                 <menuitem name=\"Open\" action=\"OpenFile\" /> \
572                                 <menuitem name=\"Save\" action=\"SaveFile\" /> \
573                                 <separator name=\"Separator\"/> \
574                                 <menuitem name=\"Quit\" action=\"Quit\" /> \
575                         </menu> \
576                         <menu name=\"Help\" action=\"HelpMenuAction\"> \
577                                 <menuitem name=\"About\" action=\"About\" /> \
578                         </menu> \
579                 </menubar> \
580         </ui> \
581 ";
582
583 static GtkWidget *get_menubar_menu(GtkWidget *window, GtkUIManager *ui_manager)
584 {
585         GtkActionGroup *action_group = gtk_action_group_new("Menu");
586         GError *error = 0;
587
588         action_group = gtk_action_group_new("Menu");
589         gtk_action_group_add_actions(action_group, menu_items, nmenu_items, 0);
590
591         gtk_ui_manager_insert_action_group(ui_manager, action_group, 0);
592         gtk_ui_manager_add_ui_from_string(GTK_UI_MANAGER(ui_manager), ui_string, -1, &error);
593
594         gtk_window_add_accel_group(GTK_WINDOW(window), gtk_ui_manager_get_accel_group(ui_manager));
595         return gtk_ui_manager_get_widget(ui_manager, "/MainMenu");
596 }
597
598 void gfio_ui_setup(GtkSettings *settings, GtkWidget *menubar,
599                    GtkWidget *vbox, GtkUIManager *ui_manager)
600 {
601         gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, FALSE, 0);
602 }
603
604 static GtkWidget *new_info_label_in_frame(GtkWidget *box, const char *label)
605 {
606         GtkWidget *label_widget;
607         GtkWidget *frame;
608
609         frame = gtk_frame_new(label);
610         label_widget = gtk_label_new(NULL);
611         gtk_box_pack_start(GTK_BOX(box), frame, TRUE, TRUE, 3);
612         gtk_container_add(GTK_CONTAINER(frame), label_widget);
613
614         return label_widget;
615 }
616
617 static GtkWidget *create_text_entry(GtkWidget *hbox, GtkWidget *label, const char *defval)
618 {
619         GtkWidget *text, *box;
620
621         gtk_container_add(GTK_CONTAINER(hbox), label);
622
623         box = gtk_hbox_new(FALSE, 3);
624         gtk_container_add(GTK_CONTAINER(hbox), box);
625
626         text = gtk_entry_new();
627         gtk_box_pack_start(GTK_BOX(box), text, TRUE, TRUE, 0);
628         gtk_entry_set_text(GTK_ENTRY(text), "localhost");
629
630         return text;
631 }
632
633 static GtkWidget *create_spinbutton(GtkWidget *hbox, GtkWidget *label, double min, double max, double defval)
634 {
635         GtkWidget *button, *box;
636
637         gtk_container_add(GTK_CONTAINER(hbox), label);
638
639         box = gtk_hbox_new(FALSE, 3);
640         gtk_container_add(GTK_CONTAINER(hbox), box);
641
642         button = gtk_spin_button_new_with_range(min, max, 1.0);
643         gtk_box_pack_start(GTK_BOX(box), button, TRUE, TRUE, 0);
644
645         gtk_spin_button_set_update_policy(GTK_SPIN_BUTTON(button), GTK_UPDATE_IF_VALID);
646         gtk_spin_button_set_value(GTK_SPIN_BUTTON(button), defval);
647
648         return button;
649 }
650
651 static void init_ui(int *argc, char **argv[], struct gui *ui)
652 {
653         GtkSettings *settings;
654         GtkUIManager *uimanager;
655         GtkWidget *menu, *probe, *probe_frame, *probe_box;
656
657         memset(ui, 0, sizeof(*ui));
658
659         /* Magical g*thread incantation, you just need this thread stuff.
660          * Without it, the update that happens in gfio_update_thread_status
661          * doesn't really happen in a timely fashion, you need expose events
662          */
663         if (!g_thread_supported ())
664                 g_thread_init(NULL);
665         gdk_threads_init();
666
667         gtk_init(argc, argv);
668         settings = gtk_settings_get_default();
669         gtk_settings_set_long_property(settings, "gtk_tooltip_timeout", 10, "gfio setting");
670         g_type_init();
671         
672         ui->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
673         gtk_window_set_title(GTK_WINDOW(ui->window), "fio");
674         gtk_window_set_default_size(GTK_WINDOW(ui->window), 700, 500);
675
676         g_signal_connect(ui->window, "delete-event", G_CALLBACK(quit_clicked), NULL);
677         g_signal_connect(ui->window, "destroy", G_CALLBACK(quit_clicked), NULL);
678
679         ui->vbox = gtk_vbox_new(FALSE, 0);
680         gtk_container_add(GTK_CONTAINER (ui->window), ui->vbox);
681
682         uimanager = gtk_ui_manager_new();
683         menu = get_menubar_menu(ui->window, uimanager);
684         gfio_ui_setup(settings, menu, ui->vbox, uimanager);
685
686         /*
687          * Set up alignments for widgets at the top of ui, 
688          * align top left, expand horizontally but not vertically
689          */
690         ui->topalign = gtk_alignment_new(0, 0, 1, 0);
691         ui->topvbox = gtk_vbox_new(FALSE, 3);
692         gtk_container_add(GTK_CONTAINER(ui->topalign), ui->topvbox);
693         gtk_box_pack_start(GTK_BOX(ui->vbox), ui->topalign, FALSE, FALSE, 0);
694
695         /*
696          * Set up hostname label + entry, port label + entry,
697          */
698         ui->hostname_hbox = gtk_hbox_new(FALSE, 0);
699
700         ui->hostname_label = gtk_label_new("Hostname:");
701         ui->hostname_entry = create_text_entry(ui->hostname_hbox, ui->hostname_label, "localhost");
702
703         ui->port_label = gtk_label_new("Port:");
704         ui->port_button = create_spinbutton(ui->hostname_hbox, ui->port_label, 1, 65535, FIO_NET_PORT);
705
706         /*
707          * Set up combo box for address type
708          */
709         ui->hostname_combo_box = gtk_combo_box_text_new();
710         gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(ui->hostname_combo_box), "IPv4");
711         gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(ui->hostname_combo_box), "IPv6");
712         gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(ui->hostname_combo_box), "local socket");
713         gtk_combo_box_set_active(GTK_COMBO_BOX(ui->hostname_combo_box), 0);
714
715         gtk_container_add(GTK_CONTAINER(ui->hostname_hbox), ui->hostname_combo_box);
716         gtk_container_add(GTK_CONTAINER(ui->topvbox), ui->hostname_hbox);
717
718         probe = gtk_frame_new("Job");
719         gtk_box_pack_start(GTK_BOX(ui->topvbox), probe, TRUE, FALSE, 3);
720         probe_frame = gtk_vbox_new(FALSE, 3);
721         gtk_container_add(GTK_CONTAINER(probe), probe_frame);
722
723         probe_box = gtk_hbox_new(FALSE, 3);
724         gtk_box_pack_start(GTK_BOX(probe_frame), probe_box, TRUE, FALSE, 3);
725         ui->probe.hostname = new_info_label_in_frame(probe_box, "Host");
726         ui->probe.os = new_info_label_in_frame(probe_box, "OS");
727         ui->probe.arch = new_info_label_in_frame(probe_box, "Architecture");
728         ui->probe.fio_ver = new_info_label_in_frame(probe_box, "Fio version");
729
730         probe_box = gtk_hbox_new(FALSE, 3);
731         gtk_box_pack_start(GTK_BOX(probe_frame), probe_box, TRUE, FALSE, 3);
732
733         ui->eta.name = new_info_label_in_frame(probe_box, "Name");
734         ui->eta.iotype = new_info_label_in_frame(probe_box, "IO");
735         ui->eta.ioengine = new_info_label_in_frame(probe_box, "IO Engine");
736         ui->eta.iodepth = new_info_label_in_frame(probe_box, "IO Depth");
737         ui->eta.jobs = new_info_label_in_frame(probe_box, "Jobs");
738         ui->eta.files = new_info_label_in_frame(probe_box, "Open files");
739
740         probe_box = gtk_hbox_new(FALSE, 3);
741         gtk_box_pack_start(GTK_BOX(probe_frame), probe_box, TRUE, FALSE, 3);
742         ui->eta.read_bw = new_info_label_in_frame(probe_box, "Read BW");
743         ui->eta.read_iops = new_info_label_in_frame(probe_box, "IOPS");
744         ui->eta.write_bw = new_info_label_in_frame(probe_box, "Write BW");
745         ui->eta.write_iops = new_info_label_in_frame(probe_box, "IOPS");
746
747         /*
748          * Only add this if we have a commit rate
749          */
750 #if 0
751         probe_box = gtk_hbox_new(FALSE, 3);
752         gtk_box_pack_start(GTK_BOX(probe_frame), probe_box, TRUE, FALSE, 3);
753
754         ui->eta.cr_bw = new_info_label_in_frame(probe_box, "Commit BW");
755         ui->eta.cr_iops = new_info_label_in_frame(probe_box, "Commit IOPS");
756
757         ui->eta.cw_bw = new_info_label_in_frame(probe_box, "Commit BW");
758         ui->eta.cw_iops = new_info_label_in_frame(probe_box, "Commit IOPS");
759 #endif
760
761         /*
762          * Add a text box for text op messages 
763          */
764         ui->textview = gtk_text_view_new();
765         ui->text = gtk_text_view_get_buffer(GTK_TEXT_VIEW(ui->textview));
766         gtk_text_buffer_set_text(ui->text, "", -1);
767         gtk_text_view_set_editable(GTK_TEXT_VIEW(ui->textview), FALSE);
768         gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(ui->textview), FALSE);
769         ui->scrolled_window = gtk_scrolled_window_new(NULL, NULL);
770         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(ui->scrolled_window),
771                                         GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
772         gtk_container_add(GTK_CONTAINER(ui->scrolled_window), ui->textview);
773         gtk_box_pack_start(GTK_BOX(ui->vbox), ui->scrolled_window,
774                         TRUE, TRUE, 0);
775
776         /*
777          * Set up alignments for widgets at the bottom of ui, 
778          * align bottom left, expand horizontally but not vertically
779          */
780         ui->bottomalign = gtk_alignment_new(0, 1, 1, 0);
781         ui->buttonbox = gtk_hbox_new(FALSE, 0);
782         gtk_container_add(GTK_CONTAINER(ui->bottomalign), ui->buttonbox);
783         gtk_box_pack_start(GTK_BOX(ui->vbox), ui->bottomalign,
784                                         FALSE, FALSE, 0);
785
786         add_buttons(ui, buttonspeclist, ARRAYSIZE(buttonspeclist));
787
788         /*
789          * Set up thread status progress bar
790          */
791         ui->thread_status_pb = gtk_progress_bar_new();
792         gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(ui->thread_status_pb), 0.0);
793         gtk_progress_bar_set_text(GTK_PROGRESS_BAR(ui->thread_status_pb), "No jobs running");
794         gtk_container_add(GTK_CONTAINER(ui->buttonbox), ui->thread_status_pb);
795
796
797         gtk_widget_show_all(ui->window);
798 }
799
800 int main(int argc, char *argv[], char *envp[])
801 {
802         if (initialize_fio(envp))
803                 return 1;
804         if (fio_init_options())
805                 return 1;
806
807         fio_debug = ~0UL;
808         init_ui(&argc, &argv, &ui);
809
810         gdk_threads_enter();
811         gtk_main();
812         gdk_threads_leave();
813         return 0;
814 }