Add GUI probe support
[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 #define ARRAYSIZE(x) (sizeof((x)) / (sizeof((x)[0])))
32
33 typedef void (*clickfunction)(GtkWidget *widget, gpointer data);
34
35 static void quit_clicked(GtkWidget *widget, gpointer data);
36 static void start_job_clicked(GtkWidget *widget, gpointer data);
37
38 static struct button_spec {
39         const char *buttontext;
40         clickfunction f;
41         const char *tooltiptext;
42 } buttonspeclist[] = {
43 #define START_JOB_BUTTON 0
44         { "Start Job",
45                 start_job_clicked,
46                 "Send current fio job to fio server to be executed" },
47 };
48
49 struct probe_widget {
50         GtkWidget *hostname;
51         GtkWidget *os;
52         GtkWidget *arch;
53         GtkWidget *fio_ver;
54 };
55
56 struct gui {
57         GtkWidget *window;
58         GtkWidget *vbox;
59         GtkWidget *topvbox;
60         GtkWidget *topalign;
61         GtkWidget *bottomalign;
62         GtkWidget *thread_status_pb;
63         GtkWidget *buttonbox;
64         GtkWidget *button[ARRAYSIZE(buttonspeclist)];
65         GtkWidget *hostname_hbox;
66         GtkWidget *hostname_label;
67         GtkWidget *hostname_entry;
68         GtkWidget *port_label;
69         GtkWidget *port_entry;
70         GtkWidget *hostname_combo_box; /* ipv4, ipv6 or socket */
71         GtkWidget *scrolled_window;
72         GtkWidget *textview;
73         GtkWidget *error_info_bar;
74         GtkWidget *error_label;
75         GtkTextBuffer *text;
76         struct probe_widget probe;
77         pthread_t t;
78
79         void *cookie;
80         int nr_job_files;
81         char **job_files;
82 } ui;
83
84 static void gfio_text_op(struct fio_client *client,
85                 FILE *f, __u16 pdu_len, const char *buf)
86 {
87         GtkTextBuffer *buffer;
88         GtkTextIter end;
89
90         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(ui.textview));
91         gdk_threads_enter();
92         gtk_text_buffer_get_end_iter(buffer, &end);
93         gtk_text_buffer_insert(buffer, &end, buf, -1);
94         gdk_threads_leave();
95         gtk_text_view_scroll_to_iter(GTK_TEXT_VIEW(ui.textview),
96                                         &end, 0.0, FALSE, 0.0,0.0);
97 }
98
99 static void gfio_disk_util_op(struct fio_client *client, struct fio_net_cmd *cmd)
100 {
101         printf("gfio_disk_util_op called\n");
102         fio_client_ops.disk_util(client, cmd);
103 }
104
105 static void gfio_thread_status_op(struct fio_net_cmd *cmd)
106 {
107         printf("gfio_thread_status_op called\n");
108         fio_client_ops.thread_status(cmd);
109 }
110
111 static void gfio_group_stats_op(struct fio_net_cmd *cmd)
112 {
113         printf("gfio_group_stats_op called\n");
114         fio_client_ops.group_stats(cmd);
115 }
116
117 static void gfio_eta_op(struct fio_client *client, struct fio_net_cmd *cmd)
118 {
119         fio_client_ops.eta(client, cmd);
120 }
121
122 static void gfio_probe_op(struct fio_client *client, struct fio_net_cmd *cmd)
123 {
124         struct cmd_probe_pdu *probe = (struct cmd_probe_pdu *) cmd->payload;
125         const char *os, *arch;
126         char buf[64];
127
128         os = fio_get_os_string(probe->os);
129         if (!os)
130                 os = "unknown";
131
132         arch = fio_get_arch_string(probe->arch);
133         if (!arch)
134                 os = "unknown";
135
136         if (!client->name)
137                 client->name = strdup((char *) probe->hostname);
138
139         gtk_label_set_text(GTK_LABEL(ui.probe.hostname), (char *) probe->hostname);
140         gtk_label_set_text(GTK_LABEL(ui.probe.os), os);
141         gtk_label_set_text(GTK_LABEL(ui.probe.arch), arch);
142         sprintf(buf, "%u.%u.%u", probe->fio_major, probe->fio_minor, probe->fio_patch);
143         gtk_label_set_text(GTK_LABEL(ui.probe.fio_ver), buf);
144 }
145
146 static void gfio_update_thread_status(char *status_message, double perc)
147 {
148         static char message[100];
149         const char *m = message;
150
151         strncpy(message, status_message, sizeof(message) - 1);
152         gtk_progress_bar_set_text(
153                 GTK_PROGRESS_BAR(ui.thread_status_pb), m);
154         gtk_progress_bar_set_fraction(
155                 GTK_PROGRESS_BAR(ui.thread_status_pb), perc / 100.0);
156         gdk_threads_enter();
157         gtk_widget_queue_draw(ui.window);
158         gdk_threads_leave();
159 }
160
161 struct client_ops gfio_client_ops = {
162         .text_op                = gfio_text_op,
163         .disk_util              = gfio_disk_util_op,
164         .thread_status          = gfio_thread_status_op,
165         .group_stats            = gfio_group_stats_op,
166         .eta                    = gfio_eta_op,
167         .probe                  = gfio_probe_op,
168         .thread_status_display  = gfio_update_thread_status,
169 };
170
171 static void quit_clicked(__attribute__((unused)) GtkWidget *widget,
172                 __attribute__((unused)) gpointer data)
173 {
174         gtk_main_quit();
175 }
176
177 static void *job_thread(void *arg)
178 {
179         struct gui *ui = arg;
180
181         fio_handle_clients(&gfio_client_ops);
182         gtk_widget_set_sensitive(ui->button[START_JOB_BUTTON], 1);
183         return NULL;
184 }
185
186 static int send_job_files(struct gui *ui)
187 {
188         int i, ret;
189
190         for (i = 0; i < ui->nr_job_files; i++) {
191                 ret = fio_clients_send_ini(ui->job_files[i]);
192                 free(ui->job_files[i]);
193                 ui->job_files[i] = NULL;
194                 if (ret)
195                         return ret;
196         }
197
198         return 0;
199 }
200
201 static void start_job_thread(pthread_t *t, struct gui *ui)
202 {
203         fio_clients_connect();
204
205         if (send_job_files(ui)) {
206                 printf("Yeah, I didn't really like those options too much.\n");
207                 gtk_widget_set_sensitive(ui->button[START_JOB_BUTTON], 1);
208                 return;
209         }
210                 
211         pthread_create(t, NULL, job_thread, ui);
212 }
213
214 static void start_job_clicked(__attribute__((unused)) GtkWidget *widget,
215                 gpointer data)
216 {
217         struct gui *ui = data;
218
219         printf("Start job button was clicked.\n");
220         gtk_widget_set_sensitive(ui->button[START_JOB_BUTTON], 0);
221         start_job_thread(&ui->t, ui);
222 }
223
224 static void add_button(struct gui *ui, int i, GtkWidget *buttonbox,
225                         struct button_spec *buttonspec)
226 {
227         ui->button[i] = gtk_button_new_with_label(buttonspec->buttontext);
228         g_signal_connect(ui->button[i], "clicked", G_CALLBACK (buttonspec->f), ui);
229         gtk_box_pack_start(GTK_BOX (ui->buttonbox), ui->button[i], TRUE, TRUE, 0);
230         gtk_widget_set_tooltip_text(ui->button[i], buttonspeclist[i].tooltiptext);
231 }
232
233 static void add_buttons(struct gui *ui,
234                                 struct button_spec *buttonlist,
235                                 int nbuttons)
236 {
237         int i;
238
239         for (i = 0; i < nbuttons; i++)
240                 add_button(ui, i, ui->buttonbox, &buttonlist[i]);
241 }
242
243 static void on_info_bar_response(GtkWidget *widget, gint response,
244                                  gpointer data)
245 {
246         if (response == GTK_RESPONSE_OK) {
247                 gtk_widget_destroy(widget);
248                 ui.error_info_bar = NULL;
249         }
250 }
251
252 void report_error(GError* error)
253 {
254         if (ui.error_info_bar == NULL) {
255                 ui.error_info_bar = gtk_info_bar_new_with_buttons(GTK_STOCK_OK,
256                                                                GTK_RESPONSE_OK,
257                                                                NULL);
258                 g_signal_connect(ui.error_info_bar, "response", G_CALLBACK(on_info_bar_response), NULL);
259                 gtk_info_bar_set_message_type(GTK_INFO_BAR(ui.error_info_bar),
260                                               GTK_MESSAGE_ERROR);
261                 
262                 ui.error_label = gtk_label_new(error->message);
263                 GtkWidget *container = gtk_info_bar_get_content_area(GTK_INFO_BAR(ui.error_info_bar));
264                 gtk_container_add(GTK_CONTAINER(container), ui.error_label);
265                 
266                 gtk_box_pack_start(GTK_BOX(ui.vbox), ui.error_info_bar, FALSE, FALSE, 0);
267                 gtk_widget_show_all(ui.vbox);
268         } else {
269                 char buffer[256];
270                 snprintf(buffer, sizeof(buffer), "Failed to open file.");
271                 gtk_label_set(GTK_LABEL(ui.error_label), buffer);
272         }
273 }
274
275 static void file_open(GtkWidget *w, gpointer data)
276 {
277         GtkWidget *dialog;
278         GSList *filenames, *fn_glist;
279         GtkFileFilter *filter;
280
281         dialog = gtk_file_chooser_dialog_new("Open File",
282                 GTK_WINDOW(ui.window),
283                 GTK_FILE_CHOOSER_ACTION_OPEN,
284                 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
285                 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
286                 NULL);
287         gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), TRUE);
288
289         filter = gtk_file_filter_new();
290         gtk_file_filter_add_pattern(filter, "*.fio");
291         gtk_file_filter_add_pattern(filter, "*.job");
292         gtk_file_filter_add_mime_type(filter, "text/fio");
293         gtk_file_filter_set_name(filter, "Fio job file");
294         gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(dialog), filter);
295
296         if (gtk_dialog_run(GTK_DIALOG(dialog)) != GTK_RESPONSE_ACCEPT) {
297                 gtk_widget_destroy(dialog);
298                 return;
299         }
300
301         fn_glist = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog));
302         filenames = fn_glist;
303         while (filenames != NULL) {
304                 const char *hostname;
305
306                 ui.job_files = realloc(ui.job_files, (ui.nr_job_files + 1) * sizeof(char *));
307                 ui.job_files[ui.nr_job_files] = strdup(filenames->data);
308                 ui.nr_job_files++;
309
310                 hostname = gtk_entry_get_text(GTK_ENTRY(ui.hostname_entry));
311                 fio_client_add(hostname, &ui.cookie);
312 #if 0
313                 if (error) {
314                         report_error(error);
315                         g_error_free(error);
316                         error = NULL;
317                 }
318 #endif
319                         
320                 g_free(filenames->data);
321                 filenames = g_slist_next(filenames);
322         }
323         g_slist_free(fn_glist);
324         gtk_widget_destroy(dialog);
325 }
326
327 static void file_save(GtkWidget *w, gpointer data)
328 {
329         GtkWidget *dialog;
330
331         dialog = gtk_file_chooser_dialog_new("Save File",
332                 GTK_WINDOW(ui.window),
333                 GTK_FILE_CHOOSER_ACTION_SAVE,
334                 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
335                 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
336                 NULL);
337
338         gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(dialog), TRUE);
339         gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), "Untitled document");
340
341         if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
342                 char *filename;
343
344                 filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
345                 // save_job_file(filename);
346                 g_free(filename);
347         }
348         gtk_widget_destroy(dialog);
349 }
350
351 static void about_dialog(GtkWidget *w, gpointer data)
352 {
353         gtk_show_about_dialog(NULL,
354                 "program-name", "gfio",
355                 "comments", "Gtk2 UI for fio",
356                 "license", "GPLv2",
357                 "version", fio_version_string,
358                 "copyright", "Jens Axboe <axboe@kernel.dk> 2012",
359                 "logo-icon-name", "fio",
360                 /* Must be last: */
361                 NULL, NULL,
362                 NULL);
363 }
364
365 static GtkActionEntry menu_items[] = {
366         { "FileMenuAction", GTK_STOCK_FILE, "File", NULL, NULL, NULL},
367         { "HelpMenuAction", GTK_STOCK_HELP, "Help", NULL, NULL, NULL},
368         { "OpenFile",       GTK_STOCK_OPEN, NULL,   "<Control>O", NULL, G_CALLBACK(file_open) },
369         { "SaveFile",       GTK_STOCK_SAVE, NULL,   "<Control>S", NULL, G_CALLBACK(file_save) },
370         { "Quit",           GTK_STOCK_QUIT, NULL,   "<Control>Q", NULL, G_CALLBACK(quit_clicked) },
371         { "About",          GTK_STOCK_ABOUT, NULL,  NULL, NULL, G_CALLBACK(about_dialog) },
372 };
373 static gint nmenu_items = sizeof (menu_items) / sizeof(menu_items[0]);
374
375 static const gchar *ui_string = " \
376         <ui> \
377                 <menubar name=\"MainMenu\"> \
378                         <menu name=\"FileMenu\" action=\"FileMenuAction\"> \
379                                 <menuitem name=\"Open\" action=\"OpenFile\" /> \
380                                 <menuitem name=\"Save\" action=\"SaveFile\" /> \
381                                 <separator name=\"Separator\"/> \
382                                 <menuitem name=\"Quit\" action=\"Quit\" /> \
383                         </menu> \
384                         <menu name=\"Help\" action=\"HelpMenuAction\"> \
385                                 <menuitem name=\"About\" action=\"About\" /> \
386                         </menu> \
387                 </menubar> \
388         </ui> \
389 ";
390
391 static GtkWidget *get_menubar_menu(GtkWidget *window, GtkUIManager *ui_manager)
392 {
393         GtkActionGroup *action_group = gtk_action_group_new("Menu");
394         GError *error = 0;
395
396         action_group = gtk_action_group_new("Menu");
397         gtk_action_group_add_actions(action_group, menu_items, nmenu_items, 0);
398
399         gtk_ui_manager_insert_action_group(ui_manager, action_group, 0);
400         gtk_ui_manager_add_ui_from_string(GTK_UI_MANAGER(ui_manager), ui_string, -1, &error);
401
402         gtk_window_add_accel_group(GTK_WINDOW(window), gtk_ui_manager_get_accel_group(ui_manager));
403         return gtk_ui_manager_get_widget(ui_manager, "/MainMenu");
404 }
405
406 void gfio_ui_setup(GtkSettings *settings, GtkWidget *menubar,
407                    GtkWidget *vbox, GtkUIManager *ui_manager)
408 {
409         gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, FALSE, 0);
410 }
411
412 static GtkWidget *new_info_label_in_frame(GtkWidget *box, const char *label)
413 {
414         GtkWidget *label_widget;
415         GtkWidget *frame;
416
417         frame = gtk_frame_new(label);
418         label_widget = gtk_label_new(NULL);
419         gtk_box_pack_start(GTK_BOX(box), frame, TRUE, TRUE, 3);
420         gtk_container_add(GTK_CONTAINER(frame), label_widget);
421
422         return label_widget;
423 }
424
425 static void init_ui(int *argc, char **argv[], struct gui *ui)
426 {
427         GList *hostname_type_list = NULL;
428         char portnum[20];
429         GtkSettings *settings;
430         GtkUIManager *uimanager;
431         GtkWidget *menu, *probe, *probe_frame, *probe_box;
432
433         memset(ui, 0, sizeof(*ui));
434
435         /* Magical g*thread incantation, you just need this thread stuff.
436          * Without it, the update that happens in gfio_update_thread_status
437          * doesn't really happen in a timely fashion, you need expose events
438          */
439         if (!g_thread_supported ())
440                 g_thread_init(NULL);
441         gdk_threads_init();
442
443         gtk_init(argc, argv);
444         settings = gtk_settings_get_default();
445         gtk_settings_set_long_property(settings, "gtk_tooltip_timeout", 10, "gfio setting");
446         g_type_init();
447         
448         ui->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
449         gtk_window_set_title(GTK_WINDOW(ui->window), "fio");
450         gtk_window_set_default_size(GTK_WINDOW(ui->window), 700, 500);
451
452         g_signal_connect(ui->window, "delete-event", G_CALLBACK(quit_clicked), NULL);
453         g_signal_connect(ui->window, "destroy", G_CALLBACK(quit_clicked), NULL);
454
455         ui->vbox = gtk_vbox_new(FALSE, 0);
456         gtk_container_add(GTK_CONTAINER (ui->window), ui->vbox);
457
458         uimanager = gtk_ui_manager_new();
459         menu = get_menubar_menu(ui->window, uimanager);
460         gfio_ui_setup(settings, menu, ui->vbox, uimanager);
461
462         /*
463          * Set up alignments for widgets at the top of ui, 
464          * align top left, expand horizontally but not vertically
465          */
466         ui->topalign = gtk_alignment_new(0, 0, 1, 0);
467         ui->topvbox = gtk_vbox_new(FALSE, 0);
468         gtk_container_add(GTK_CONTAINER(ui->topalign), ui->topvbox);
469         gtk_box_pack_start(GTK_BOX(ui->vbox), ui->topalign, FALSE, FALSE, 0);
470
471         /*
472          * Set up hostname label + entry, port label + entry,
473          */
474         ui->hostname_hbox = gtk_hbox_new(FALSE, 0);
475         ui->hostname_label = gtk_label_new("Host:");
476         ui->hostname_entry = gtk_entry_new();
477         gtk_entry_set_text(GTK_ENTRY(ui->hostname_entry), "localhost");
478         ui->port_label = gtk_label_new("Port:");
479         ui->port_entry = gtk_entry_new();
480         snprintf(portnum, sizeof(portnum) - 1, "%d", FIO_NET_PORT);
481         gtk_entry_set_text(GTK_ENTRY(ui->port_entry), (gchar *) portnum);
482
483         /*
484          * Set up combo box for address type
485          */
486         ui->hostname_combo_box = gtk_combo_new();
487         gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(ui->hostname_combo_box)->entry), "IPv4");
488         hostname_type_list = g_list_append(hostname_type_list, (gpointer) "IPv4"); 
489         hostname_type_list = g_list_append(hostname_type_list, (gpointer) "local socket"); 
490         hostname_type_list = g_list_append(hostname_type_list, (gpointer) "IPv6"); 
491         gtk_combo_set_popdown_strings(GTK_COMBO(ui->hostname_combo_box), hostname_type_list);
492         g_list_free(hostname_type_list);
493
494         gtk_container_add(GTK_CONTAINER (ui->hostname_hbox), ui->hostname_label);
495         gtk_container_add(GTK_CONTAINER (ui->hostname_hbox), ui->hostname_entry);
496         gtk_container_add(GTK_CONTAINER (ui->hostname_hbox), ui->port_label);
497         gtk_container_add(GTK_CONTAINER (ui->hostname_hbox), ui->port_entry);
498         gtk_container_add(GTK_CONTAINER (ui->hostname_hbox), ui->hostname_combo_box);
499         gtk_container_add(GTK_CONTAINER (ui->topvbox), ui->hostname_hbox);
500
501         probe = gtk_frame_new("Host");
502         gtk_box_pack_start(GTK_BOX(ui->topvbox), probe, TRUE, FALSE, 3);
503         probe_frame = gtk_vbox_new(FALSE, 3);
504         gtk_container_add(GTK_CONTAINER(probe), probe_frame);
505
506         probe_box = gtk_hbox_new(FALSE, 3);
507         gtk_box_pack_start(GTK_BOX(probe_frame), probe_box, TRUE, FALSE, 3);
508
509         ui->probe.hostname = new_info_label_in_frame(probe_box, "Host");
510         ui->probe.os = new_info_label_in_frame(probe_box, "OS");
511         ui->probe.arch = new_info_label_in_frame(probe_box, "Architecture");
512         ui->probe.fio_ver = new_info_label_in_frame(probe_box, "Fio version");
513
514         /*
515          * Set up thread status progress bar
516          */
517         ui->thread_status_pb = gtk_progress_bar_new();
518         gtk_progress_bar_set_fraction(
519                 GTK_PROGRESS_BAR(ui->thread_status_pb), 0.0);
520         gtk_progress_bar_set_text(
521                 GTK_PROGRESS_BAR(ui->thread_status_pb), "No jobs running");
522         gtk_container_add(GTK_CONTAINER (ui->topvbox), ui->thread_status_pb);
523
524         /*
525          * Add a text box for text op messages 
526          */
527         ui->textview = gtk_text_view_new();
528         ui->text = gtk_text_view_get_buffer(GTK_TEXT_VIEW(ui->textview));
529         gtk_text_buffer_set_text(ui->text, "", -1);
530         gtk_text_view_set_editable(GTK_TEXT_VIEW(ui->textview), FALSE);
531         gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(ui->textview), FALSE);
532         ui->scrolled_window = gtk_scrolled_window_new(NULL, NULL);
533         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(ui->scrolled_window),
534                                         GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
535         gtk_container_add(GTK_CONTAINER(ui->scrolled_window), ui->textview);
536         gtk_box_pack_start(GTK_BOX(ui->vbox), ui->scrolled_window,
537                         TRUE, TRUE, 0);
538
539         /*
540          * Set up alignments for widgets at the bottom of ui, 
541          * align bottom left, expand horizontally but not vertically
542          */
543         ui->bottomalign = gtk_alignment_new(0, 1, 1, 0);
544         ui->buttonbox = gtk_hbox_new(FALSE, 0);
545         gtk_container_add(GTK_CONTAINER(ui->bottomalign), ui->buttonbox);
546         gtk_box_pack_start(GTK_BOX(ui->vbox), ui->bottomalign,
547                                         FALSE, FALSE, 0);
548
549         add_buttons(ui, buttonspeclist, ARRAYSIZE(buttonspeclist));
550         gtk_widget_show_all(ui->window);
551 }
552
553 int main(int argc, char *argv[], char *envp[])
554 {
555         if (initialize_fio(envp))
556                 return 1;
557         if (fio_init_options())
558                 return 1;
559
560         init_ui(&argc, &argv, &ui);
561
562         gdk_threads_enter();
563         gtk_main();
564         gdk_threads_leave();
565         return 0;
566 }