blob: 2a9dd6a635c2ec35720953747050b47318e5cb7b [file] [log] [blame]
Stephen M. Cameronff1f3282012-02-24 08:17:30 +01001/*
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 */
Stephen M. Cameron8232e282012-02-24 08:17:31 +010023#include <locale.h>
Stephen M. Cameron60f6b332012-02-24 08:17:32 +010024#include <malloc.h>
Stephen M. Cameron8232e282012-02-24 08:17:31 +010025
Stephen M. Cameron5b7573a2012-02-24 08:17:31 +010026#include <glib.h>
Stephen M. Cameronff1f3282012-02-24 08:17:30 +010027#include <gtk/gtk.h>
28
Stephen M. Cameron8232e282012-02-24 08:17:31 +010029#include "fio_initialization.h"
30#include "fio.h"
31
Stephen M. Cameronf3074002012-02-24 08:17:30 +010032#define ARRAYSIZE(x) (sizeof((x)) / (sizeof((x)[0])))
33
34typedef void (*clickfunction)(GtkWidget *widget, gpointer data);
35
36static void quit_clicked(GtkWidget *widget, gpointer data);
37static void start_job_clicked(GtkWidget *widget, gpointer data);
38
39static 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
Stephen M. Cameronff1f3282012-02-24 08:17:30 +010052struct gui {
Stephen M. Cameron60f6b332012-02-24 08:17:32 +010053 int argc;
54 char **argv;
Stephen M. Cameronff1f3282012-02-24 08:17:30 +010055 GtkWidget *window;
Stephen M. Cameron5b7573a2012-02-24 08:17:31 +010056 GtkWidget *vbox;
Stephen M. Cameron04cc6b72012-02-24 08:17:31 +010057 GtkWidget *thread_status_pb;
Stephen M. Cameronf3074002012-02-24 08:17:30 +010058 GtkWidget *buttonbox;
59 GtkWidget *button[ARRAYSIZE(buttonspeclist)];
Stephen M. Cameron45032dd2012-02-24 08:17:31 +010060 GtkWidget *hostname_hbox;
61 GtkWidget *hostname_label;
62 GtkWidget *hostname_entry;
63 GtkWidget *port_label;
64 GtkWidget *port_entry;
65 GtkWidget *hostname_combo_box; /* ipv4, ipv6 or socket */
Stephen M. Cameron47066342012-02-24 08:17:31 +010066 GtkWidget *jobfile_hbox;
67 GtkWidget *jobfile_label;
68 GtkWidget *jobfile_entry;
Stephen M. Cameron736f2df2012-02-24 08:17:32 +010069 GtkWidget *scrolled_window;
70 GtkWidget *textview;
71 GtkTextBuffer *text;
Stephen M. Cameron25927252012-02-24 08:17:31 +010072 pthread_t t;
Stephen M. Cameron5b7573a2012-02-24 08:17:31 +010073} ui;
Stephen M. Cameronff1f3282012-02-24 08:17:30 +010074
Stephen M. Camerona1820202012-02-24 08:17:31 +010075static void gfio_text_op(struct fio_client *client,
76 FILE *f, __u16 pdu_len, const char *buf)
77{
Stephen M. Cameron736f2df2012-02-24 08:17:32 +010078 GtkTextBuffer *buffer;
79 GtkTextIter end;
80
81 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(ui.textview));
82 gdk_threads_enter();
83 gtk_text_buffer_get_end_iter(buffer, &end);
84 gtk_text_buffer_insert(buffer, &end, buf, -1);
85 gdk_threads_leave();
86 gtk_text_view_scroll_to_iter(GTK_TEXT_VIEW(ui.textview),
87 &end, 0.0, FALSE, 0.0,0.0);
Stephen M. Camerona1820202012-02-24 08:17:31 +010088}
89
90static void gfio_disk_util_op(struct fio_client *client, struct fio_net_cmd *cmd)
91{
92 printf("gfio_disk_util_op called\n");
93 fio_client_ops.disk_util(client, cmd);
94}
95
96static void gfio_thread_status_op(struct fio_net_cmd *cmd)
97{
98 printf("gfio_thread_status_op called\n");
99 fio_client_ops.thread_status(cmd);
100}
101
102static void gfio_group_stats_op(struct fio_net_cmd *cmd)
103{
104 printf("gfio_group_stats_op called\n");
105 fio_client_ops.group_stats(cmd);
106}
107
108static void gfio_eta_op(struct fio_client *client, struct fio_net_cmd *cmd)
109{
Stephen M. Camerona1820202012-02-24 08:17:31 +0100110 fio_client_ops.eta(client, cmd);
111}
112
113static void gfio_probe_op(struct fio_client *client, struct fio_net_cmd *cmd)
114{
115 printf("gfio_probe_op called\n");
116 fio_client_ops.probe(client, cmd);
117}
118
Stephen M. Cameron04cc6b72012-02-24 08:17:31 +0100119static void gfio_update_thread_status(char *status_message, double perc)
Stephen M. Cameron5b7573a2012-02-24 08:17:31 +0100120{
121 static char message[100];
122 const char *m = message;
123
124 strncpy(message, status_message, sizeof(message) - 1);
Stephen M. Cameron04cc6b72012-02-24 08:17:31 +0100125 gtk_progress_bar_set_text(
126 GTK_PROGRESS_BAR(ui.thread_status_pb), m);
127 gtk_progress_bar_set_fraction(
128 GTK_PROGRESS_BAR(ui.thread_status_pb), perc / 100.0);
Stephen M. Cameron5b7573a2012-02-24 08:17:31 +0100129 gdk_threads_enter();
130 gtk_widget_queue_draw(ui.window);
131 gdk_threads_leave();
132}
133
Stephen M. Camerona1820202012-02-24 08:17:31 +0100134struct client_ops gfio_client_ops = {
135 gfio_text_op,
136 gfio_disk_util_op,
137 gfio_thread_status_op,
138 gfio_group_stats_op,
139 gfio_eta_op,
140 gfio_probe_op,
Stephen M. Cameron5b7573a2012-02-24 08:17:31 +0100141 gfio_update_thread_status,
Stephen M. Camerona1820202012-02-24 08:17:31 +0100142};
143
Stephen M. Cameronff1f3282012-02-24 08:17:30 +0100144static void quit_clicked(__attribute__((unused)) GtkWidget *widget,
145 __attribute__((unused)) gpointer data)
146{
147 gtk_main_quit();
148}
149
Stephen M. Cameron60f6b332012-02-24 08:17:32 +0100150static void add_arg(char **argv, int index, const char *value)
151{
152 argv[index] = malloc(strlen(value) + 1);
153 strcpy(argv[index], value);
154}
155
156static void free_args(int argc, char **argv)
157{
158 int i;
159
160 for (i = 0; i < argc; i++)
161 free(argv[i]);
162 free(argv);
163}
164
Stephen M. Cameron25927252012-02-24 08:17:31 +0100165static void *job_thread(void *arg)
Stephen M. Cameronf3074002012-02-24 08:17:30 +0100166{
Stephen M. Cameron25927252012-02-24 08:17:31 +0100167 struct gui *ui = arg;
168
169 fio_handle_clients(&gfio_client_ops);
170 gtk_widget_set_sensitive(ui->button[START_JOB_BUTTON], 1);
Stephen M. Cameron60f6b332012-02-24 08:17:32 +0100171 free_args(ui->argc, ui->argv);
Stephen M. Cameron25927252012-02-24 08:17:31 +0100172 return NULL;
173}
174
Stephen M. Cameron60f6b332012-02-24 08:17:32 +0100175static void construct_options(struct gui *ui, int *argc, char ***argv)
176{
177 const char *hostname, *hostname_type, *port, *jobfile;
178 char newarg[200];
179
180 hostname_type = gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(ui->hostname_combo_box)->entry));
181 hostname = gtk_entry_get_text(GTK_ENTRY(ui->hostname_entry));
182 port = gtk_entry_get_text(GTK_ENTRY(ui->port_entry));
183 jobfile = gtk_entry_get_text(GTK_ENTRY(ui->jobfile_entry));
184
185 *argc = 3;
186 *argv = malloc(*argc * sizeof(**argv));
187 add_arg(*argv, 0, "gfio");
188 snprintf(newarg, sizeof(newarg) - 1, "--client=%s", hostname);
189 add_arg(*argv, 1, newarg);
190 add_arg(*argv, 2, jobfile);
191}
192
Stephen M. Cameron25927252012-02-24 08:17:31 +0100193static void start_job_thread(pthread_t *t, struct gui *ui)
194{
Stephen M. Cameron60f6b332012-02-24 08:17:32 +0100195 construct_options(ui, &ui->argc, &ui->argv);
196 if (parse_options(ui->argc, ui->argv)) {
197 printf("Yeah, I didn't really like those options too much.\n");
198 free_args(ui->argc, ui->argv);
199 gtk_widget_set_sensitive(ui->button[START_JOB_BUTTON], 1);
200 return;
201 }
Stephen M. Cameron25927252012-02-24 08:17:31 +0100202 pthread_create(t, NULL, job_thread, ui);
203}
204
205static void start_job_clicked(__attribute__((unused)) GtkWidget *widget,
206 gpointer data)
207{
208 struct gui *ui = data;
209
Stephen M. Cameronf3074002012-02-24 08:17:30 +0100210 printf("Start job button was clicked.\n");
Stephen M. Cameron25927252012-02-24 08:17:31 +0100211 gtk_widget_set_sensitive(ui->button[START_JOB_BUTTON], 0);
212 start_job_thread(&ui->t, ui);
Stephen M. Cameronf3074002012-02-24 08:17:30 +0100213}
214
215static void add_button(struct gui *ui, int i, GtkWidget *buttonbox,
216 struct button_spec *buttonspec)
217{
218 ui->button[i] = gtk_button_new_with_label(buttonspec->buttontext);
219 g_signal_connect(ui->button[i], "clicked", G_CALLBACK (buttonspec->f), ui);
220 gtk_box_pack_start(GTK_BOX (ui->buttonbox), ui->button[i], TRUE, TRUE, 0);
221 gtk_widget_set_tooltip_text(ui->button[i], buttonspeclist[i].tooltiptext);
222}
223
224static void add_buttons(struct gui *ui,
225 struct button_spec *buttonlist,
226 int nbuttons)
227{
228 int i;
229
230 ui->buttonbox = gtk_hbox_new(FALSE, 0);
Stephen M. Cameron5b7573a2012-02-24 08:17:31 +0100231 gtk_container_add(GTK_CONTAINER (ui->vbox), ui->buttonbox);
Stephen M. Cameronf3074002012-02-24 08:17:30 +0100232 for (i = 0; i < nbuttons; i++)
233 add_button(ui, i, ui->buttonbox, &buttonlist[i]);
234}
235
Stephen M. Cameronff1f3282012-02-24 08:17:30 +0100236static void init_ui(int *argc, char **argv[], struct gui *ui)
237{
Stephen M. Cameron45032dd2012-02-24 08:17:31 +0100238 GList *hostname_type_list = NULL;
239 char portnum[20];
240
Stephen M. Cameron2839f0c2012-02-24 08:17:31 +0100241 /* Magical g*thread incantation, you just need this thread stuff.
Stephen M. Cameron04cc6b72012-02-24 08:17:31 +0100242 * Without it, the update that happens in gfio_update_thread_status
Stephen M. Cameron2839f0c2012-02-24 08:17:31 +0100243 * doesn't really happen in a timely fashion, you need expose events
244 */
245 if (!g_thread_supported ())
246 g_thread_init(NULL);
247 gdk_threads_init();
248
Stephen M. Cameronff1f3282012-02-24 08:17:30 +0100249 gtk_init(argc, argv);
250
251 ui->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
252 gtk_window_set_title(GTK_WINDOW(ui->window), "fio");
253 gtk_window_set_default_size(GTK_WINDOW(ui->window), 700, 500);
254
255 g_signal_connect(ui->window, "delete-event", G_CALLBACK (quit_clicked), NULL);
256 g_signal_connect(ui->window, "destroy", G_CALLBACK (quit_clicked), NULL);
257
Stephen M. Cameron5b7573a2012-02-24 08:17:31 +0100258 ui->vbox = gtk_vbox_new(FALSE, 0);
259 gtk_container_add(GTK_CONTAINER (ui->window), ui->vbox);
Stephen M. Cameron04cc6b72012-02-24 08:17:31 +0100260
261 /*
Stephen M. Cameron45032dd2012-02-24 08:17:31 +0100262 * Set up hostname label + entry, port label + entry,
263 */
264 ui->hostname_hbox = gtk_hbox_new(FALSE, 0);
265 ui->hostname_label = gtk_label_new("Host:");
266 ui->hostname_entry = gtk_entry_new();
267 gtk_entry_set_text(GTK_ENTRY(ui->hostname_entry), "localhost");
268 ui->port_label = gtk_label_new("Port:");
269 ui->port_entry = gtk_entry_new();
270 snprintf(portnum, sizeof(portnum) - 1, "%d", FIO_NET_PORT);
271 gtk_entry_set_text(GTK_ENTRY(ui->port_entry), (gchar *) portnum);
272
273 /*
274 * Set up combo box for address type
275 */
276 ui->hostname_combo_box = gtk_combo_new();
277 gtk_entry_set_text(GTK_ENTRY (GTK_COMBO(ui->hostname_combo_box)->entry), "IPv4");
278 hostname_type_list = g_list_append(hostname_type_list, (gpointer) "IPv4");
279 hostname_type_list = g_list_append(hostname_type_list, (gpointer) "local socket");
280 hostname_type_list = g_list_append(hostname_type_list, (gpointer) "IPv6");
281 gtk_combo_set_popdown_strings (GTK_COMBO (ui->hostname_combo_box), hostname_type_list);
282 g_list_free(hostname_type_list);
283
284 gtk_container_add(GTK_CONTAINER (ui->hostname_hbox), ui->hostname_label);
285 gtk_container_add(GTK_CONTAINER (ui->hostname_hbox), ui->hostname_entry);
286 gtk_container_add(GTK_CONTAINER (ui->hostname_hbox), ui->port_label);
287 gtk_container_add(GTK_CONTAINER (ui->hostname_hbox), ui->port_entry);
288 gtk_container_add(GTK_CONTAINER (ui->hostname_hbox), ui->hostname_combo_box);
289 gtk_container_add(GTK_CONTAINER (ui->vbox), ui->hostname_hbox);
290
291 /*
Stephen M. Cameron47066342012-02-24 08:17:31 +0100292 * Set up jobfile text entry (temporary until gui really works)
293 */
294 ui->jobfile_hbox = gtk_hbox_new(FALSE, 0);
295 ui->jobfile_label = gtk_label_new("Job file:");
296 ui->jobfile_entry = gtk_entry_new();
297 gtk_container_add(GTK_CONTAINER (ui->jobfile_hbox), ui->jobfile_label);
298 gtk_container_add(GTK_CONTAINER (ui->jobfile_hbox), ui->jobfile_entry);
299 gtk_container_add(GTK_CONTAINER (ui->vbox), ui->jobfile_hbox);
300
301 /*
Stephen M. Cameron04cc6b72012-02-24 08:17:31 +0100302 * Set up thread status progress bar
303 */
304 ui->thread_status_pb = gtk_progress_bar_new();
305 gtk_progress_bar_set_fraction(
306 GTK_PROGRESS_BAR(ui->thread_status_pb), 0.0);
307 gtk_progress_bar_set_text(
308 GTK_PROGRESS_BAR(ui->thread_status_pb), "No jobs running");
309 gtk_container_add(GTK_CONTAINER (ui->vbox), ui->thread_status_pb);
Stephen M. Cameron5b7573a2012-02-24 08:17:31 +0100310
Stephen M. Cameron736f2df2012-02-24 08:17:32 +0100311 /*
312 * Add a text box for text op messages
313 */
314 ui->textview = gtk_text_view_new();
315 ui->text = gtk_text_view_get_buffer(GTK_TEXT_VIEW(ui->textview));
316 gtk_text_buffer_set_text(ui->text, "", -1);
317 gtk_text_view_set_editable(GTK_TEXT_VIEW(ui->textview), FALSE);
318 gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(ui->textview), FALSE);
319 ui->scrolled_window = gtk_scrolled_window_new(NULL, NULL);
320 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(ui->scrolled_window),
321 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
322 gtk_container_add(GTK_CONTAINER(ui->scrolled_window), ui->textview);
323 gtk_container_add(GTK_CONTAINER(ui->vbox), ui->scrolled_window);
324
Stephen M. Cameronf3074002012-02-24 08:17:30 +0100325 add_buttons(ui, buttonspeclist, ARRAYSIZE(buttonspeclist));
Stephen M. Cameronff1f3282012-02-24 08:17:30 +0100326 gtk_widget_show_all(ui->window);
327}
328
Stephen M. Cameron8232e282012-02-24 08:17:31 +0100329int main(int argc, char *argv[], char *envp[])
Stephen M. Cameronff1f3282012-02-24 08:17:30 +0100330{
Stephen M. Cameron8232e282012-02-24 08:17:31 +0100331 if (initialize_fio(envp))
332 return 1;
Stephen M. Camerona1820202012-02-24 08:17:31 +0100333
Stephen M. Cameronff1f3282012-02-24 08:17:30 +0100334 init_ui(&argc, &argv, &ui);
Stephen M. Cameron5b7573a2012-02-24 08:17:31 +0100335
Stephen M. Cameron2839f0c2012-02-24 08:17:31 +0100336 gdk_threads_enter();
Stephen M. Cameronff1f3282012-02-24 08:17:30 +0100337 gtk_main();
Stephen M. Cameron2839f0c2012-02-24 08:17:31 +0100338 gdk_threads_leave();
Stephen M. Cameronff1f3282012-02-24 08:17:30 +0100339 return 0;
340}