fio: construct options from gui
[fio.git] / gfio.c
CommitLineData
ff1f3280
SC
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 */
8232e285 23#include <locale.h>
60f6b330 24#include <malloc.h>
8232e285 25
5b7573ab 26#include <glib.h>
ff1f3280
SC
27#include <gtk/gtk.h>
28
8232e285
SC
29#include "fio_initialization.h"
30#include "fio.h"
31
f3074008
SC
32#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
ff1f3280 52struct gui {
60f6b330
SC
53 int argc;
54 char **argv;
ff1f3280 55 GtkWidget *window;
5b7573ab 56 GtkWidget *vbox;
04cc6b77 57 GtkWidget *thread_status_pb;
f3074008
SC
58 GtkWidget *buttonbox;
59 GtkWidget *button[ARRAYSIZE(buttonspeclist)];
45032dd8
SC
60 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 */
47066341
SC
66 GtkWidget *jobfile_hbox;
67 GtkWidget *jobfile_label;
68 GtkWidget *jobfile_entry;
25927259 69 pthread_t t;
5b7573ab 70} ui;
ff1f3280 71
a1820207
SC
72static void gfio_text_op(struct fio_client *client,
73 FILE *f, __u16 pdu_len, const char *buf)
74{
75 printf("gfio_text_op called\n");
76 fio_client_ops.text_op(client, f, pdu_len, buf);
77}
78
79static void gfio_disk_util_op(struct fio_client *client, struct fio_net_cmd *cmd)
80{
81 printf("gfio_disk_util_op called\n");
82 fio_client_ops.disk_util(client, cmd);
83}
84
85static void gfio_thread_status_op(struct fio_net_cmd *cmd)
86{
87 printf("gfio_thread_status_op called\n");
88 fio_client_ops.thread_status(cmd);
89}
90
91static void gfio_group_stats_op(struct fio_net_cmd *cmd)
92{
93 printf("gfio_group_stats_op called\n");
94 fio_client_ops.group_stats(cmd);
95}
96
97static void gfio_eta_op(struct fio_client *client, struct fio_net_cmd *cmd)
98{
a1820207
SC
99 fio_client_ops.eta(client, cmd);
100}
101
102static void gfio_probe_op(struct fio_client *client, struct fio_net_cmd *cmd)
103{
104 printf("gfio_probe_op called\n");
105 fio_client_ops.probe(client, cmd);
106}
107
04cc6b77 108static void gfio_update_thread_status(char *status_message, double perc)
5b7573ab
SC
109{
110 static char message[100];
111 const char *m = message;
112
113 strncpy(message, status_message, sizeof(message) - 1);
04cc6b77
SC
114 gtk_progress_bar_set_text(
115 GTK_PROGRESS_BAR(ui.thread_status_pb), m);
116 gtk_progress_bar_set_fraction(
117 GTK_PROGRESS_BAR(ui.thread_status_pb), perc / 100.0);
5b7573ab
SC
118 gdk_threads_enter();
119 gtk_widget_queue_draw(ui.window);
120 gdk_threads_leave();
121}
122
a1820207
SC
123struct client_ops gfio_client_ops = {
124 gfio_text_op,
125 gfio_disk_util_op,
126 gfio_thread_status_op,
127 gfio_group_stats_op,
128 gfio_eta_op,
129 gfio_probe_op,
5b7573ab 130 gfio_update_thread_status,
a1820207
SC
131};
132
ff1f3280
SC
133static void quit_clicked(__attribute__((unused)) GtkWidget *widget,
134 __attribute__((unused)) gpointer data)
135{
136 gtk_main_quit();
137}
138
60f6b330
SC
139static void add_arg(char **argv, int index, const char *value)
140{
141 argv[index] = malloc(strlen(value) + 1);
142 strcpy(argv[index], value);
143}
144
145static void free_args(int argc, char **argv)
146{
147 int i;
148
149 for (i = 0; i < argc; i++)
150 free(argv[i]);
151 free(argv);
152}
153
25927259
SC
154static void *job_thread(void *arg)
155{
156 struct gui *ui = arg;
157
158 fio_handle_clients(&gfio_client_ops);
159 gtk_widget_set_sensitive(ui->button[START_JOB_BUTTON], 1);
60f6b330 160 free_args(ui->argc, ui->argv);
25927259
SC
161 return NULL;
162}
163
60f6b330
SC
164static void construct_options(struct gui *ui, int *argc, char ***argv)
165{
166 const char *hostname, *hostname_type, *port, *jobfile;
167 char newarg[200];
168
169 hostname_type = gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(ui->hostname_combo_box)->entry));
170 hostname = gtk_entry_get_text(GTK_ENTRY(ui->hostname_entry));
171 port = gtk_entry_get_text(GTK_ENTRY(ui->port_entry));
172 jobfile = gtk_entry_get_text(GTK_ENTRY(ui->jobfile_entry));
173
174 *argc = 3;
175 *argv = malloc(*argc * sizeof(**argv));
176 add_arg(*argv, 0, "gfio");
177 snprintf(newarg, sizeof(newarg) - 1, "--client=%s", hostname);
178 add_arg(*argv, 1, newarg);
179 add_arg(*argv, 2, jobfile);
180}
181
25927259
SC
182static void start_job_thread(pthread_t *t, struct gui *ui)
183{
60f6b330
SC
184 construct_options(ui, &ui->argc, &ui->argv);
185 if (parse_options(ui->argc, ui->argv)) {
186 printf("Yeah, I didn't really like those options too much.\n");
187 free_args(ui->argc, ui->argv);
188 gtk_widget_set_sensitive(ui->button[START_JOB_BUTTON], 1);
189 return;
190 }
25927259
SC
191 pthread_create(t, NULL, job_thread, ui);
192}
193
f3074008 194static void start_job_clicked(__attribute__((unused)) GtkWidget *widget,
25927259 195 gpointer data)
f3074008 196{
25927259
SC
197 struct gui *ui = data;
198
f3074008 199 printf("Start job button was clicked.\n");
25927259
SC
200 gtk_widget_set_sensitive(ui->button[START_JOB_BUTTON], 0);
201 start_job_thread(&ui->t, ui);
f3074008
SC
202}
203
204static void add_button(struct gui *ui, int i, GtkWidget *buttonbox,
205 struct button_spec *buttonspec)
206{
207 ui->button[i] = gtk_button_new_with_label(buttonspec->buttontext);
208 g_signal_connect(ui->button[i], "clicked", G_CALLBACK (buttonspec->f), ui);
209 gtk_box_pack_start(GTK_BOX (ui->buttonbox), ui->button[i], TRUE, TRUE, 0);
210 gtk_widget_set_tooltip_text(ui->button[i], buttonspeclist[i].tooltiptext);
211}
212
213static void add_buttons(struct gui *ui,
214 struct button_spec *buttonlist,
215 int nbuttons)
216{
217 int i;
218
219 ui->buttonbox = gtk_hbox_new(FALSE, 0);
5b7573ab 220 gtk_container_add(GTK_CONTAINER (ui->vbox), ui->buttonbox);
f3074008
SC
221 for (i = 0; i < nbuttons; i++)
222 add_button(ui, i, ui->buttonbox, &buttonlist[i]);
223}
224
ff1f3280
SC
225static void init_ui(int *argc, char **argv[], struct gui *ui)
226{
45032dd8
SC
227 GList *hostname_type_list = NULL;
228 char portnum[20];
229
2839f0c6 230 /* Magical g*thread incantation, you just need this thread stuff.
04cc6b77 231 * Without it, the update that happens in gfio_update_thread_status
2839f0c6
SC
232 * doesn't really happen in a timely fashion, you need expose events
233 */
234 if (!g_thread_supported ())
235 g_thread_init(NULL);
236 gdk_threads_init();
237
ff1f3280
SC
238 gtk_init(argc, argv);
239
240 ui->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
241 gtk_window_set_title(GTK_WINDOW(ui->window), "fio");
242 gtk_window_set_default_size(GTK_WINDOW(ui->window), 700, 500);
243
244 g_signal_connect(ui->window, "delete-event", G_CALLBACK (quit_clicked), NULL);
245 g_signal_connect(ui->window, "destroy", G_CALLBACK (quit_clicked), NULL);
246
5b7573ab
SC
247 ui->vbox = gtk_vbox_new(FALSE, 0);
248 gtk_container_add(GTK_CONTAINER (ui->window), ui->vbox);
04cc6b77 249
45032dd8
SC
250 /*
251 * Set up hostname label + entry, port label + entry,
252 */
253 ui->hostname_hbox = gtk_hbox_new(FALSE, 0);
254 ui->hostname_label = gtk_label_new("Host:");
255 ui->hostname_entry = gtk_entry_new();
256 gtk_entry_set_text(GTK_ENTRY(ui->hostname_entry), "localhost");
257 ui->port_label = gtk_label_new("Port:");
258 ui->port_entry = gtk_entry_new();
259 snprintf(portnum, sizeof(portnum) - 1, "%d", FIO_NET_PORT);
260 gtk_entry_set_text(GTK_ENTRY(ui->port_entry), (gchar *) portnum);
261
262 /*
263 * Set up combo box for address type
264 */
265 ui->hostname_combo_box = gtk_combo_new();
266 gtk_entry_set_text(GTK_ENTRY (GTK_COMBO(ui->hostname_combo_box)->entry), "IPv4");
267 hostname_type_list = g_list_append(hostname_type_list, (gpointer) "IPv4");
268 hostname_type_list = g_list_append(hostname_type_list, (gpointer) "local socket");
269 hostname_type_list = g_list_append(hostname_type_list, (gpointer) "IPv6");
270 gtk_combo_set_popdown_strings (GTK_COMBO (ui->hostname_combo_box), hostname_type_list);
271 g_list_free(hostname_type_list);
272
273 gtk_container_add(GTK_CONTAINER (ui->hostname_hbox), ui->hostname_label);
274 gtk_container_add(GTK_CONTAINER (ui->hostname_hbox), ui->hostname_entry);
275 gtk_container_add(GTK_CONTAINER (ui->hostname_hbox), ui->port_label);
276 gtk_container_add(GTK_CONTAINER (ui->hostname_hbox), ui->port_entry);
277 gtk_container_add(GTK_CONTAINER (ui->hostname_hbox), ui->hostname_combo_box);
278 gtk_container_add(GTK_CONTAINER (ui->vbox), ui->hostname_hbox);
279
47066341
SC
280 /*
281 * Set up jobfile text entry (temporary until gui really works)
282 */
283 ui->jobfile_hbox = gtk_hbox_new(FALSE, 0);
284 ui->jobfile_label = gtk_label_new("Job file:");
285 ui->jobfile_entry = gtk_entry_new();
286 gtk_container_add(GTK_CONTAINER (ui->jobfile_hbox), ui->jobfile_label);
287 gtk_container_add(GTK_CONTAINER (ui->jobfile_hbox), ui->jobfile_entry);
288 gtk_container_add(GTK_CONTAINER (ui->vbox), ui->jobfile_hbox);
289
04cc6b77
SC
290 /*
291 * Set up thread status progress bar
292 */
293 ui->thread_status_pb = gtk_progress_bar_new();
294 gtk_progress_bar_set_fraction(
295 GTK_PROGRESS_BAR(ui->thread_status_pb), 0.0);
296 gtk_progress_bar_set_text(
297 GTK_PROGRESS_BAR(ui->thread_status_pb), "No jobs running");
298 gtk_container_add(GTK_CONTAINER (ui->vbox), ui->thread_status_pb);
5b7573ab 299
f3074008 300 add_buttons(ui, buttonspeclist, ARRAYSIZE(buttonspeclist));
ff1f3280
SC
301 gtk_widget_show_all(ui->window);
302}
303
8232e285 304int main(int argc, char *argv[], char *envp[])
ff1f3280 305{
8232e285
SC
306 if (initialize_fio(envp))
307 return 1;
a1820207 308
ff1f3280 309 init_ui(&argc, &argv, &ui);
5b7573ab 310
2839f0c6 311 gdk_threads_enter();
ff1f3280 312 gtk_main();
2839f0c6 313 gdk_threads_leave();
ff1f3280
SC
314 return 0;
315}