fio: factor out FIO_NET_PORT
[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
25 #include <glib.h>
26 #include <gtk/gtk.h>
27
28 #include "fio_initialization.h"
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 #define QUIT_BUTTON 1
48         { "Quit", quit_clicked, "Quit gfio" },
49 };
50
51 struct gui {
52         GtkWidget *window;
53         GtkWidget *vbox;
54         GtkWidget *thread_status_pb;
55         GtkWidget *buttonbox;
56         GtkWidget *button[ARRAYSIZE(buttonspeclist)];
57         pthread_t t;
58 } ui;
59
60 static void gfio_text_op(struct fio_client *client,
61                 FILE *f, __u16 pdu_len, const char *buf)
62 {
63         printf("gfio_text_op called\n");
64         fio_client_ops.text_op(client, f, pdu_len, buf);
65 }
66
67 static void gfio_disk_util_op(struct fio_client *client, struct fio_net_cmd *cmd)
68 {
69         printf("gfio_disk_util_op called\n");
70         fio_client_ops.disk_util(client, cmd);
71 }
72
73 static void gfio_thread_status_op(struct fio_net_cmd *cmd)
74 {
75         printf("gfio_thread_status_op called\n");
76         fio_client_ops.thread_status(cmd);
77 }
78
79 static void gfio_group_stats_op(struct fio_net_cmd *cmd)
80 {
81         printf("gfio_group_stats_op called\n");
82         fio_client_ops.group_stats(cmd);
83 }
84
85 static void gfio_eta_op(struct fio_client *client, struct fio_net_cmd *cmd)
86 {
87         fio_client_ops.eta(client, cmd);
88 }
89
90 static void gfio_probe_op(struct fio_client *client, struct fio_net_cmd *cmd)
91 {
92         printf("gfio_probe_op called\n");
93         fio_client_ops.probe(client, cmd);
94 }
95
96 static void gfio_update_thread_status(char *status_message, double perc)
97 {
98         static char message[100];
99         const char *m = message;
100
101         strncpy(message, status_message, sizeof(message) - 1);
102         gtk_progress_bar_set_text(
103                 GTK_PROGRESS_BAR(ui.thread_status_pb), m);
104         gtk_progress_bar_set_fraction(
105                 GTK_PROGRESS_BAR(ui.thread_status_pb), perc / 100.0);
106         gdk_threads_enter();
107         gtk_widget_queue_draw(ui.window);
108         gdk_threads_leave();
109 }
110
111 struct client_ops gfio_client_ops = {
112         gfio_text_op,
113         gfio_disk_util_op,
114         gfio_thread_status_op,
115         gfio_group_stats_op,
116         gfio_eta_op,
117         gfio_probe_op,
118         gfio_update_thread_status,
119 };
120
121 static void quit_clicked(__attribute__((unused)) GtkWidget *widget,
122                 __attribute__((unused)) gpointer data)
123 {
124         gtk_main_quit();
125 }
126
127 static void *job_thread(void *arg)
128 {
129         struct gui *ui = arg;
130
131         fio_handle_clients(&gfio_client_ops);
132         gtk_widget_set_sensitive(ui->button[START_JOB_BUTTON], 1);
133         return NULL;
134 }
135
136 static void start_job_thread(pthread_t *t, struct gui *ui)
137 {
138         pthread_create(t, NULL, job_thread, ui);
139 }
140
141 static void start_job_clicked(__attribute__((unused)) GtkWidget *widget,
142                 gpointer data)
143 {
144         struct gui *ui = data;
145
146         printf("Start job button was clicked.\n");
147         gtk_widget_set_sensitive(ui->button[START_JOB_BUTTON], 0);
148         start_job_thread(&ui->t, ui);
149 }
150
151 static void add_button(struct gui *ui, int i, GtkWidget *buttonbox,
152                         struct button_spec *buttonspec)
153 {
154         ui->button[i] = gtk_button_new_with_label(buttonspec->buttontext);
155         g_signal_connect(ui->button[i], "clicked", G_CALLBACK (buttonspec->f), ui);
156         gtk_box_pack_start(GTK_BOX (ui->buttonbox), ui->button[i], TRUE, TRUE, 0);
157         gtk_widget_set_tooltip_text(ui->button[i], buttonspeclist[i].tooltiptext);
158 }
159
160 static void add_buttons(struct gui *ui,
161                                 struct button_spec *buttonlist,
162                                 int nbuttons)
163 {
164         int i;
165
166         ui->buttonbox = gtk_hbox_new(FALSE, 0);
167         gtk_container_add(GTK_CONTAINER (ui->vbox), ui->buttonbox);
168         for (i = 0; i < nbuttons; i++)
169                 add_button(ui, i, ui->buttonbox, &buttonlist[i]);
170 }
171
172 static void init_ui(int *argc, char **argv[], struct gui *ui)
173 {
174         /* Magical g*thread incantation, you just need this thread stuff.
175          * Without it, the update that happens in gfio_update_thread_status
176          * doesn't really happen in a timely fashion, you need expose events
177          */
178         if (!g_thread_supported ())
179                 g_thread_init(NULL);
180         gdk_threads_init();
181
182         gtk_init(argc, argv);
183         
184         ui->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
185         gtk_window_set_title(GTK_WINDOW(ui->window), "fio");
186         gtk_window_set_default_size(GTK_WINDOW(ui->window), 700, 500);
187
188         g_signal_connect(ui->window, "delete-event", G_CALLBACK (quit_clicked), NULL);
189         g_signal_connect(ui->window, "destroy", G_CALLBACK (quit_clicked), NULL);
190
191         ui->vbox = gtk_vbox_new(FALSE, 0);
192         gtk_container_add(GTK_CONTAINER (ui->window), ui->vbox);
193
194         /*
195          * Set up thread status progress bar
196          */
197         ui->thread_status_pb = gtk_progress_bar_new();
198         gtk_progress_bar_set_fraction(
199                 GTK_PROGRESS_BAR(ui->thread_status_pb), 0.0);
200         gtk_progress_bar_set_text(
201                 GTK_PROGRESS_BAR(ui->thread_status_pb), "No jobs running");
202         gtk_container_add(GTK_CONTAINER (ui->vbox), ui->thread_status_pb);
203
204         add_buttons(ui, buttonspeclist, ARRAYSIZE(buttonspeclist));
205         gtk_widget_show_all(ui->window);
206 }
207
208 int main(int argc, char *argv[], char *envp[])
209 {
210         if (initialize_fio(envp))
211                 return 1;
212
213         if (parse_options(argc, argv))
214                 return 1;
215
216         init_ui(&argc, &argv, &ui);
217
218         gdk_threads_enter();
219         gtk_main();
220         gdk_threads_leave();
221         return 0;
222 }