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