Merge branch 'master' into gfio
[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_initialization.h"
30 #include "fio.h"
31
32 #define ARRAYSIZE(x) (sizeof((x)) / (sizeof((x)[0])))
33
34 typedef void (*clickfunction)(GtkWidget *widget, gpointer data);
35
36 static void quit_clicked(GtkWidget *widget, gpointer data);
37 static void start_job_clicked(GtkWidget *widget, gpointer data);
38
39 static struct button_spec {
40         const char *buttontext;
41         clickfunction f;
42         const char *tooltiptext;
43 } buttonspeclist[] = {
44 #define START_JOB_BUTTON 0
45         { "Start Job",
46                 start_job_clicked,
47                 "Send current fio job to fio server to be executed" },
48 #define QUIT_BUTTON 1
49         { "Quit", quit_clicked, "Quit gfio" },
50 };
51
52 struct gui {
53         int argc;
54         char **argv;
55         GtkWidget *window;
56         GtkWidget *vbox;
57         GtkWidget *topvbox;
58         GtkWidget *topalign;
59         GtkWidget *bottomalign;
60         GtkWidget *thread_status_pb;
61         GtkWidget *buttonbox;
62         GtkWidget *button[ARRAYSIZE(buttonspeclist)];
63         GtkWidget *hostname_hbox;
64         GtkWidget *hostname_label;
65         GtkWidget *hostname_entry;
66         GtkWidget *port_label;
67         GtkWidget *port_entry;
68         GtkWidget *hostname_combo_box; /* ipv4, ipv6 or socket */
69         GtkWidget *jobfile_hbox;
70         GtkWidget *jobfile_label;
71         GtkWidget *jobfile_entry;
72         GtkWidget *scrolled_window;
73         GtkWidget *textview;
74         GtkTextBuffer *text;
75         pthread_t t;
76 } ui;
77
78 static void gfio_text_op(struct fio_client *client,
79                 FILE *f, __u16 pdu_len, const char *buf)
80 {
81         GtkTextBuffer *buffer;
82         GtkTextIter end;
83
84         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(ui.textview));
85         gdk_threads_enter();
86         gtk_text_buffer_get_end_iter(buffer, &end);
87         gtk_text_buffer_insert(buffer, &end, buf, -1);
88         gdk_threads_leave();
89         gtk_text_view_scroll_to_iter(GTK_TEXT_VIEW(ui.textview),
90                                         &end, 0.0, FALSE, 0.0,0.0);
91 }
92
93 static void gfio_disk_util_op(struct fio_client *client, struct fio_net_cmd *cmd)
94 {
95         printf("gfio_disk_util_op called\n");
96         fio_client_ops.disk_util(client, cmd);
97 }
98
99 static void gfio_thread_status_op(struct fio_net_cmd *cmd)
100 {
101         printf("gfio_thread_status_op called\n");
102         fio_client_ops.thread_status(cmd);
103 }
104
105 static void gfio_group_stats_op(struct fio_net_cmd *cmd)
106 {
107         printf("gfio_group_stats_op called\n");
108         fio_client_ops.group_stats(cmd);
109 }
110
111 static void gfio_eta_op(struct fio_client *client, struct fio_net_cmd *cmd)
112 {
113         fio_client_ops.eta(client, cmd);
114 }
115
116 static void gfio_probe_op(struct fio_client *client, struct fio_net_cmd *cmd)
117 {
118         printf("gfio_probe_op called\n");
119         fio_client_ops.probe(client, cmd);
120 }
121
122 static void gfio_update_thread_status(char *status_message, double perc)
123 {
124         static char message[100];
125         const char *m = message;
126
127         strncpy(message, status_message, sizeof(message) - 1);
128         gtk_progress_bar_set_text(
129                 GTK_PROGRESS_BAR(ui.thread_status_pb), m);
130         gtk_progress_bar_set_fraction(
131                 GTK_PROGRESS_BAR(ui.thread_status_pb), perc / 100.0);
132         gdk_threads_enter();
133         gtk_widget_queue_draw(ui.window);
134         gdk_threads_leave();
135 }
136
137 struct client_ops gfio_client_ops = {
138         gfio_text_op,
139         gfio_disk_util_op,
140         gfio_thread_status_op,
141         gfio_group_stats_op,
142         gfio_eta_op,
143         gfio_probe_op,
144         gfio_update_thread_status,
145 };
146
147 static void quit_clicked(__attribute__((unused)) GtkWidget *widget,
148                 __attribute__((unused)) gpointer data)
149 {
150         gtk_main_quit();
151 }
152
153 static void add_arg(char **argv, int index, const char *value)
154 {
155         argv[index] = malloc(strlen(value) + 1);
156         strcpy(argv[index], value);
157 }
158
159 static void free_args(int argc, char **argv)
160 {
161         int i;
162
163         for (i = 0; i < argc; i++)
164                 free(argv[i]);
165         free(argv);
166 }
167
168 static void *job_thread(void *arg)
169 {
170         struct gui *ui = arg;
171
172         fio_handle_clients(&gfio_client_ops);
173         gtk_widget_set_sensitive(ui->button[START_JOB_BUTTON], 1);
174         free_args(ui->argc, ui->argv);
175         return NULL;
176 }
177
178 static void construct_options(struct gui *ui, int *argc, char ***argv)
179 {
180         const char *hostname, *hostname_type, *port, *jobfile;
181         char newarg[200];
182         
183         hostname_type = gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(ui->hostname_combo_box)->entry));
184         hostname = gtk_entry_get_text(GTK_ENTRY(ui->hostname_entry));
185         port = gtk_entry_get_text(GTK_ENTRY(ui->port_entry));
186         jobfile = gtk_entry_get_text(GTK_ENTRY(ui->jobfile_entry));
187
188         *argc = 3;
189         *argv = malloc(*argc * sizeof(**argv));         
190         add_arg(*argv, 0,  "gfio");
191         snprintf(newarg, sizeof(newarg) - 1, "--client=%s", hostname);
192         add_arg(*argv, 1, newarg);
193         add_arg(*argv, 2, jobfile);
194 }
195
196 static void start_job_thread(pthread_t *t, struct gui *ui)
197 {
198         construct_options(ui, &ui->argc, &ui->argv);
199         if (parse_options(ui->argc, ui->argv)) {
200                 printf("Yeah, I didn't really like those options too much.\n");
201                 free_args(ui->argc, ui->argv);
202                 gtk_widget_set_sensitive(ui->button[START_JOB_BUTTON], 1);
203                 return;
204         }
205         pthread_create(t, NULL, job_thread, ui);
206 }
207
208 static void start_job_clicked(__attribute__((unused)) GtkWidget *widget,
209                 gpointer data)
210 {
211         struct gui *ui = data;
212
213         printf("Start job button was clicked.\n");
214         gtk_widget_set_sensitive(ui->button[START_JOB_BUTTON], 0);
215         start_job_thread(&ui->t, ui);
216 }
217
218 static void add_button(struct gui *ui, int i, GtkWidget *buttonbox,
219                         struct button_spec *buttonspec)
220 {
221         ui->button[i] = gtk_button_new_with_label(buttonspec->buttontext);
222         g_signal_connect(ui->button[i], "clicked", G_CALLBACK (buttonspec->f), ui);
223         gtk_box_pack_start(GTK_BOX (ui->buttonbox), ui->button[i], TRUE, TRUE, 0);
224         gtk_widget_set_tooltip_text(ui->button[i], buttonspeclist[i].tooltiptext);
225 }
226
227 static void add_buttons(struct gui *ui,
228                                 struct button_spec *buttonlist,
229                                 int nbuttons)
230 {
231         int i;
232
233         for (i = 0; i < nbuttons; i++)
234                 add_button(ui, i, ui->buttonbox, &buttonlist[i]);
235 }
236
237 static void init_ui(int *argc, char **argv[], struct gui *ui)
238 {
239         GList *hostname_type_list = NULL;
240         char portnum[20];
241
242         /* Magical g*thread incantation, you just need this thread stuff.
243          * Without it, the update that happens in gfio_update_thread_status
244          * doesn't really happen in a timely fashion, you need expose events
245          */
246         if (!g_thread_supported ())
247                 g_thread_init(NULL);
248         gdk_threads_init();
249
250         gtk_init(argc, argv);
251         
252         ui->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
253         gtk_window_set_title(GTK_WINDOW(ui->window), "fio");
254         gtk_window_set_default_size(GTK_WINDOW(ui->window), 700, 500);
255
256         g_signal_connect(ui->window, "delete-event", G_CALLBACK (quit_clicked), NULL);
257         g_signal_connect(ui->window, "destroy", G_CALLBACK (quit_clicked), NULL);
258
259         ui->vbox = gtk_vbox_new(FALSE, 0);
260         gtk_container_add(GTK_CONTAINER (ui->window), ui->vbox);
261
262         /*
263          * Set up alignments for widgets at the top of ui, 
264          * align top left, expand horizontally but not vertically
265          */
266         ui->topalign = gtk_alignment_new(0, 0, 1, 0);
267         ui->topvbox = gtk_vbox_new(FALSE, 0);
268         gtk_container_add(GTK_CONTAINER(ui->topalign), ui->topvbox);
269         gtk_box_pack_start(GTK_BOX(ui->vbox), ui->topalign, FALSE, FALSE, 0);
270
271         /*
272          * Set up hostname label + entry, port label + entry,
273          */
274         ui->hostname_hbox = gtk_hbox_new(FALSE, 0);
275         ui->hostname_label = gtk_label_new("Host:");
276         ui->hostname_entry = gtk_entry_new();
277         gtk_entry_set_text(GTK_ENTRY(ui->hostname_entry), "localhost");
278         ui->port_label = gtk_label_new("Port:");
279         ui->port_entry = gtk_entry_new();
280         snprintf(portnum, sizeof(portnum) - 1, "%d", FIO_NET_PORT);
281         gtk_entry_set_text(GTK_ENTRY(ui->port_entry), (gchar *) portnum);
282
283         /*
284          * Set up combo box for address type
285          */
286         ui->hostname_combo_box = gtk_combo_new();
287         gtk_entry_set_text(GTK_ENTRY (GTK_COMBO(ui->hostname_combo_box)->entry), "IPv4");
288         hostname_type_list = g_list_append(hostname_type_list, (gpointer) "IPv4"); 
289         hostname_type_list = g_list_append(hostname_type_list, (gpointer) "local socket"); 
290         hostname_type_list = g_list_append(hostname_type_list, (gpointer) "IPv6"); 
291         gtk_combo_set_popdown_strings (GTK_COMBO (ui->hostname_combo_box), hostname_type_list);
292         g_list_free(hostname_type_list);
293
294         gtk_container_add(GTK_CONTAINER (ui->hostname_hbox), ui->hostname_label);
295         gtk_container_add(GTK_CONTAINER (ui->hostname_hbox), ui->hostname_entry);
296         gtk_container_add(GTK_CONTAINER (ui->hostname_hbox), ui->port_label);
297         gtk_container_add(GTK_CONTAINER (ui->hostname_hbox), ui->port_entry);
298         gtk_container_add(GTK_CONTAINER (ui->hostname_hbox), ui->hostname_combo_box);
299         gtk_container_add(GTK_CONTAINER (ui->topvbox), ui->hostname_hbox);
300
301         /*
302          * Set up jobfile text entry (temporary until gui really works)
303          */
304         ui->jobfile_hbox = gtk_hbox_new(FALSE, 0);
305         ui->jobfile_label = gtk_label_new("Job file:");
306         ui->jobfile_entry = gtk_entry_new();
307         gtk_container_add(GTK_CONTAINER (ui->jobfile_hbox), ui->jobfile_label);
308         gtk_container_add(GTK_CONTAINER (ui->jobfile_hbox), ui->jobfile_entry);
309         gtk_container_add(GTK_CONTAINER (ui->topvbox), ui->jobfile_hbox);
310
311         /*
312          * Set up thread status progress bar
313          */
314         ui->thread_status_pb = gtk_progress_bar_new();
315         gtk_progress_bar_set_fraction(
316                 GTK_PROGRESS_BAR(ui->thread_status_pb), 0.0);
317         gtk_progress_bar_set_text(
318                 GTK_PROGRESS_BAR(ui->thread_status_pb), "No jobs running");
319         gtk_container_add(GTK_CONTAINER (ui->topvbox), ui->thread_status_pb);
320
321         /*
322          * Add a text box for text op messages 
323          */
324         ui->textview = gtk_text_view_new();
325         ui->text = gtk_text_view_get_buffer(GTK_TEXT_VIEW(ui->textview));
326         gtk_text_buffer_set_text(ui->text, "", -1);
327         gtk_text_view_set_editable(GTK_TEXT_VIEW(ui->textview), FALSE);
328         gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(ui->textview), FALSE);
329         ui->scrolled_window = gtk_scrolled_window_new(NULL, NULL);
330         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(ui->scrolled_window),
331                                         GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
332         gtk_container_add(GTK_CONTAINER(ui->scrolled_window), ui->textview);
333         gtk_box_pack_start(GTK_BOX(ui->vbox), ui->scrolled_window,
334                         TRUE, TRUE, 0);
335
336         /*
337          * Set up alignments for widgets at the bottom of ui, 
338          * align bottom left, expand horizontally but not vertically
339          */
340         ui->bottomalign = gtk_alignment_new(0, 1, 1, 0);
341         ui->buttonbox = gtk_hbox_new(FALSE, 0);
342         gtk_container_add(GTK_CONTAINER(ui->bottomalign), ui->buttonbox);
343         gtk_box_pack_start(GTK_BOX(ui->vbox), ui->bottomalign,
344                                         FALSE, FALSE, 0);
345
346         add_buttons(ui, buttonspeclist, ARRAYSIZE(buttonspeclist));
347         gtk_widget_show_all(ui->window);
348 }
349
350 int main(int argc, char *argv[], char *envp[])
351 {
352         if (initialize_fio(envp))
353                 return 1;
354
355         init_ui(&argc, &argv, &ui);
356
357         gdk_threads_enter();
358         gtk_main();
359         gdk_threads_leave();
360         return 0;
361 }