blob: 93c7103f1a3f40f7a42fe2afa5ecea4c162147fc [file] [log] [blame]
Jens Axboe132159a2011-09-30 15:01:32 -06001#include <stdio.h>
2#include <stdlib.h>
3#include <unistd.h>
4#include <limits.h>
5#include <errno.h>
6#include <fcntl.h>
7#include <sys/poll.h>
8#include <sys/types.h>
9#include <sys/stat.h>
10#include <sys/wait.h>
Jens Axboed05c4a02011-10-05 00:16:11 +020011#include <sys/socket.h>
Jens Axboe87aa8f12011-10-06 21:24:13 +020012#include <sys/un.h>
Jens Axboe132159a2011-09-30 15:01:32 -060013#include <netinet/in.h>
14#include <arpa/inet.h>
15#include <netdb.h>
Jens Axboe9e22ecb2011-10-04 14:50:21 +020016#include <signal.h>
Jens Axboe132159a2011-09-30 15:01:32 -060017
18#include "fio.h"
19#include "server.h"
Jens Axboeb66570d2011-10-01 11:11:35 -060020#include "flist.h"
Jens Axboe3c5f57e2011-10-06 12:37:50 +020021#include "hash.h"
Jens Axboe132159a2011-09-30 15:01:32 -060022
Jens Axboe82c1ed32011-10-10 08:56:18 +020023struct client_eta {
24 struct jobs_eta eta;
25 unsigned int pending;
26};
27
Jens Axboeb66570d2011-10-01 11:11:35 -060028struct fio_client {
29 struct flist_head list;
Jens Axboebebe6392011-10-07 10:00:51 +020030 struct flist_head hash_list;
Jens Axboe3f3a4542011-10-10 21:11:09 +020031 struct flist_head arg_list;
Jens Axboe811826b2011-10-24 09:11:50 +020032 union {
33 struct sockaddr_in addr;
34 struct sockaddr_in6 addr6;
35 struct sockaddr_un addr_un;
36 };
Jens Axboeb66570d2011-10-01 11:11:35 -060037 char *hostname;
Jens Axboebebe6392011-10-07 10:00:51 +020038 int port;
Jens Axboeb66570d2011-10-01 11:11:35 -060039 int fd;
Jens Axboee55f8f32012-03-06 15:42:31 +010040 unsigned int refs;
Jens Axboe81179ee2011-10-04 12:42:06 +020041
Jens Axboeb5296dd2011-10-07 16:35:56 +020042 char *name;
43
Jens Axboe81179ee2011-10-04 12:42:06 +020044 int state;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +020045
Jens Axboe17dd1762011-10-04 13:27:34 +020046 int skip_newline;
Jens Axboe87aa8f12011-10-06 21:24:13 +020047 int is_sock;
Jens Axboed09a64a2011-10-13 11:38:56 +020048 int disk_stats_shown;
Jens Axboe11e950b2011-10-16 21:34:14 +020049 unsigned int jobs;
50 int error;
Jens Axboe811826b2011-10-24 09:11:50 +020051 int ipv6;
Jens Axboec2cb6862012-02-23 20:56:12 +010052 int sent_job;
Jens Axboe82c1ed32011-10-10 08:56:18 +020053
54 struct flist_head eta_list;
55 struct client_eta *eta_in_flight;
Jens Axboe81179ee2011-10-04 12:42:06 +020056
Jens Axboe89c17072011-10-11 10:15:51 +020057 struct flist_head cmd_list;
58
Jens Axboe81179ee2011-10-04 12:42:06 +020059 uint16_t argc;
60 char **argv;
Jens Axboebd023602012-08-26 17:30:45 +020061
62 char **ini_file;
63 unsigned int nr_ini_file;
Jens Axboe81179ee2011-10-04 12:42:06 +020064};
65
Jens Axboeaf9c9fb2011-10-09 21:54:10 +020066static struct timeval eta_tv;
Jens Axboe48fbb462011-10-09 12:19:08 +020067
Jens Axboe81179ee2011-10-04 12:42:06 +020068enum {
Jens Axboe5c2857f2011-10-04 13:14:32 +020069 Client_created = 0,
Jens Axboe81179ee2011-10-04 12:42:06 +020070 Client_connected = 1,
71 Client_started = 2,
Jens Axboe01be0382011-10-15 14:43:41 +020072 Client_running = 3,
73 Client_stopped = 4,
74 Client_exited = 5,
Jens Axboeb66570d2011-10-01 11:11:35 -060075};
76
77static FLIST_HEAD(client_list);
Jens Axboe82c1ed32011-10-10 08:56:18 +020078static FLIST_HEAD(eta_list);
Jens Axboeb66570d2011-10-01 11:11:35 -060079
Jens Axboe3f3a4542011-10-10 21:11:09 +020080static FLIST_HEAD(arg_list);
81
Jens Axboe37f0c1a2011-10-11 14:08:33 +020082static struct thread_stat client_ts;
83static struct group_run_stats client_gs;
84static int sum_stat_clients;
85static int sum_stat_nr;
86
Jens Axboe3c5f57e2011-10-06 12:37:50 +020087#define FIO_CLIENT_HASH_BITS 7
88#define FIO_CLIENT_HASH_SZ (1 << FIO_CLIENT_HASH_BITS)
89#define FIO_CLIENT_HASH_MASK (FIO_CLIENT_HASH_SZ - 1)
Jens Axboebebe6392011-10-07 10:00:51 +020090static struct flist_head client_hash[FIO_CLIENT_HASH_SZ];
Jens Axboe3c5f57e2011-10-06 12:37:50 +020091
Jens Axboee951bdc2011-10-05 21:58:45 +020092static int handle_client(struct fio_client *client);
Jens Axboe82c1ed32011-10-10 08:56:18 +020093static void dec_jobs_eta(struct client_eta *eta);
Jens Axboe0b8f30a2011-10-04 10:31:53 +020094
Jens Axboebebe6392011-10-07 10:00:51 +020095static void fio_client_add_hash(struct fio_client *client)
Jens Axboe3c5f57e2011-10-06 12:37:50 +020096{
97 int bucket = hash_long(client->fd, FIO_CLIENT_HASH_BITS);
98
99 bucket &= FIO_CLIENT_HASH_MASK;
Jens Axboebebe6392011-10-07 10:00:51 +0200100 flist_add(&client->hash_list, &client_hash[bucket]);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200101}
102
Jens Axboebebe6392011-10-07 10:00:51 +0200103static void fio_client_remove_hash(struct fio_client *client)
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200104{
Jens Axboebebe6392011-10-07 10:00:51 +0200105 if (!flist_empty(&client->hash_list))
106 flist_del_init(&client->hash_list);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200107}
108
109static void fio_init fio_client_hash_init(void)
110{
111 int i;
112
Jens Axboebebe6392011-10-07 10:00:51 +0200113 for (i = 0; i < FIO_CLIENT_HASH_SZ; i++)
114 INIT_FLIST_HEAD(&client_hash[i]);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200115}
116
Jens Axboeb66570d2011-10-01 11:11:35 -0600117static struct fio_client *find_client_by_fd(int fd)
118{
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200119 int bucket = hash_long(fd, FIO_CLIENT_HASH_BITS) & FIO_CLIENT_HASH_MASK;
Jens Axboeb66570d2011-10-01 11:11:35 -0600120 struct fio_client *client;
121 struct flist_head *entry;
122
Jens Axboebebe6392011-10-07 10:00:51 +0200123 flist_for_each(entry, &client_hash[bucket]) {
124 client = flist_entry(entry, struct fio_client, hash_list);
Jens Axboeb66570d2011-10-01 11:11:35 -0600125
Jens Axboee55f8f32012-03-06 15:42:31 +0100126 if (client->fd == fd) {
127 client->refs++;
Jens Axboeb66570d2011-10-01 11:11:35 -0600128 return client;
Jens Axboee55f8f32012-03-06 15:42:31 +0100129 }
Jens Axboeb66570d2011-10-01 11:11:35 -0600130 }
131
132 return NULL;
133}
134
Jens Axboeb66570d2011-10-01 11:11:35 -0600135static void remove_client(struct fio_client *client)
136{
Jens Axboee55f8f32012-03-06 15:42:31 +0100137 assert(client->refs);
138
139 if (--client->refs)
140 return;
141
Jens Axboe39e8e012011-10-04 13:46:08 +0200142 dprint(FD_NET, "client: removed <%s>\n", client->hostname);
Jens Axboeb66570d2011-10-01 11:11:35 -0600143 flist_del(&client->list);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200144
Jens Axboebebe6392011-10-07 10:00:51 +0200145 fio_client_remove_hash(client);
Jens Axboe81179ee2011-10-04 12:42:06 +0200146
Jens Axboe82c1ed32011-10-10 08:56:18 +0200147 if (!flist_empty(&client->eta_list)) {
148 flist_del_init(&client->eta_list);
149 dec_jobs_eta(client->eta_in_flight);
150 }
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200151
Jens Axboeb66570d2011-10-01 11:11:35 -0600152 free(client->hostname);
Jens Axboe81179ee2011-10-04 12:42:06 +0200153 if (client->argv)
154 free(client->argv);
Jens Axboeb5296dd2011-10-07 16:35:56 +0200155 if (client->name)
156 free(client->name);
Jens Axboebd023602012-08-26 17:30:45 +0200157 while (client->nr_ini_file)
158 free(client->ini_file[--client->nr_ini_file]);
159 if (client->ini_file)
160 free(client->ini_file);
Jens Axboe81179ee2011-10-04 12:42:06 +0200161
Jens Axboeb66570d2011-10-01 11:11:35 -0600162 free(client);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200163 nr_clients--;
Jens Axboe5fd0acb2011-10-11 14:20:22 +0200164 sum_stat_clients--;
Jens Axboeb66570d2011-10-01 11:11:35 -0600165}
Jens Axboe132159a2011-09-30 15:01:32 -0600166
Jens Axboee55f8f32012-03-06 15:42:31 +0100167static void put_client(struct fio_client *client)
168{
169 remove_client(client);
170}
171
Jens Axboefa2ea802011-10-08 21:07:29 +0200172static void __fio_client_add_cmd_option(struct fio_client *client,
173 const char *opt)
Jens Axboe81179ee2011-10-04 12:42:06 +0200174{
Jens Axboe39e8e012011-10-04 13:46:08 +0200175 int index;
176
177 index = client->argc++;
Jens Axboe81179ee2011-10-04 12:42:06 +0200178 client->argv = realloc(client->argv, sizeof(char *) * client->argc);
Jens Axboe39e8e012011-10-04 13:46:08 +0200179 client->argv[index] = strdup(opt);
180 dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
Jens Axboe81179ee2011-10-04 12:42:06 +0200181}
182
Jens Axboefa2ea802011-10-08 21:07:29 +0200183void fio_client_add_cmd_option(void *cookie, const char *opt)
Jens Axboe81179ee2011-10-04 12:42:06 +0200184{
Jens Axboebebe6392011-10-07 10:00:51 +0200185 struct fio_client *client = cookie;
Jens Axboe3f3a4542011-10-10 21:11:09 +0200186 struct flist_head *entry;
Jens Axboe81179ee2011-10-04 12:42:06 +0200187
Jens Axboebebe6392011-10-07 10:00:51 +0200188 if (!client || !opt)
Jens Axboefa2ea802011-10-08 21:07:29 +0200189 return;
Jens Axboe81179ee2011-10-04 12:42:06 +0200190
Jens Axboefa2ea802011-10-08 21:07:29 +0200191 __fio_client_add_cmd_option(client, opt);
Jens Axboe3f3a4542011-10-10 21:11:09 +0200192
193 /*
194 * Duplicate arguments to shared client group
195 */
196 flist_for_each(entry, &arg_list) {
197 client = flist_entry(entry, struct fio_client, arg_list);
198
199 __fio_client_add_cmd_option(client, opt);
200 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200201}
202
Jens Axboebd023602012-08-26 17:30:45 +0200203void fio_client_add_ini_file(void *cookie, const char *ini_file)
204{
205 struct fio_client *client = cookie;
206 size_t new_size;
207
208 dprint(FD_NET, "client <%s>: add ini %s\n", client->hostname, ini_file);
209
210 new_size = (client->nr_ini_file + 1) * sizeof(char *);
211 client->ini_file = realloc(client->ini_file, new_size);
212 client->ini_file[client->nr_ini_file] = strdup(ini_file);
213 client->nr_ini_file++;
214}
215
Jens Axboebebe6392011-10-07 10:00:51 +0200216int fio_client_add(const char *hostname, void **cookie)
Jens Axboe132159a2011-09-30 15:01:32 -0600217{
Jens Axboe3f3a4542011-10-10 21:11:09 +0200218 struct fio_client *existing = *cookie;
Jens Axboeb66570d2011-10-01 11:11:35 -0600219 struct fio_client *client;
Jens Axboe132159a2011-09-30 15:01:32 -0600220
Jens Axboe3f3a4542011-10-10 21:11:09 +0200221 if (existing) {
222 /*
223 * We always add our "exec" name as the option, hence 1
224 * means empty.
225 */
226 if (existing->argc == 1)
227 flist_add_tail(&existing->arg_list, &arg_list);
228 else {
229 while (!flist_empty(&arg_list))
230 flist_del_init(arg_list.next);
231 }
232 }
233
Jens Axboeb66570d2011-10-01 11:11:35 -0600234 client = malloc(sizeof(*client));
Jens Axboea37f69b2011-10-01 12:24:21 -0600235 memset(client, 0, sizeof(*client));
Jens Axboe81179ee2011-10-04 12:42:06 +0200236
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200237 INIT_FLIST_HEAD(&client->list);
Jens Axboebebe6392011-10-07 10:00:51 +0200238 INIT_FLIST_HEAD(&client->hash_list);
Jens Axboe3f3a4542011-10-10 21:11:09 +0200239 INIT_FLIST_HEAD(&client->arg_list);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200240 INIT_FLIST_HEAD(&client->eta_list);
Jens Axboe89c17072011-10-11 10:15:51 +0200241 INIT_FLIST_HEAD(&client->cmd_list);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200242
Jens Axboebebe6392011-10-07 10:00:51 +0200243 if (fio_server_parse_string(hostname, &client->hostname,
244 &client->is_sock, &client->port,
Jens Axboe811826b2011-10-24 09:11:50 +0200245 &client->addr.sin_addr,
246 &client->addr6.sin6_addr,
247 &client->ipv6))
Jens Axboebebe6392011-10-07 10:00:51 +0200248 return -1;
249
Jens Axboea37f69b2011-10-01 12:24:21 -0600250 client->fd = -1;
Jens Axboee55f8f32012-03-06 15:42:31 +0100251 client->refs = 1;
Jens Axboe81179ee2011-10-04 12:42:06 +0200252
253 __fio_client_add_cmd_option(client, "fio");
254
Jens Axboea37f69b2011-10-01 12:24:21 -0600255 flist_add(&client->list, &client_list);
256 nr_clients++;
Jens Axboebebe6392011-10-07 10:00:51 +0200257 dprint(FD_NET, "client: added <%s>\n", client->hostname);
258 *cookie = client;
259 return 0;
Jens Axboea37f69b2011-10-01 12:24:21 -0600260}
261
Jens Axboe87aa8f12011-10-06 21:24:13 +0200262static int fio_client_connect_ip(struct fio_client *client)
Jens Axboea37f69b2011-10-01 12:24:21 -0600263{
Jens Axboe811826b2011-10-24 09:11:50 +0200264 struct sockaddr *addr;
265 fio_socklen_t socklen;
266 int fd, domain;
Jens Axboe132159a2011-09-30 15:01:32 -0600267
Jens Axboe811826b2011-10-24 09:11:50 +0200268 if (client->ipv6) {
269 client->addr6.sin6_family = AF_INET6;
270 client->addr6.sin6_port = htons(client->port);
271 domain = AF_INET6;
272 addr = (struct sockaddr *) &client->addr6;
273 socklen = sizeof(client->addr6);
274 } else {
275 client->addr.sin_family = AF_INET;
276 client->addr.sin_port = htons(client->port);
277 domain = AF_INET;
278 addr = (struct sockaddr *) &client->addr;
279 socklen = sizeof(client->addr);
280 }
Jens Axboe132159a2011-09-30 15:01:32 -0600281
Jens Axboe811826b2011-10-24 09:11:50 +0200282 fd = socket(domain, SOCK_STREAM, 0);
Jens Axboe132159a2011-09-30 15:01:32 -0600283 if (fd < 0) {
284 log_err("fio: socket: %s\n", strerror(errno));
Jens Axboe87aa8f12011-10-06 21:24:13 +0200285 return -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600286 }
287
Jens Axboe811826b2011-10-24 09:11:50 +0200288 if (connect(fd, addr, socklen) < 0) {
Jens Axboe132159a2011-09-30 15:01:32 -0600289 log_err("fio: connect: %s\n", strerror(errno));
Jens Axboea7de0a12011-10-07 12:55:14 +0200290 log_err("fio: failed to connect to %s:%u\n", client->hostname,
291 client->port);
Jens Axboeb94cba42011-10-06 21:27:10 +0200292 close(fd);
Jens Axboe87aa8f12011-10-06 21:24:13 +0200293 return -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600294 }
295
Jens Axboe87aa8f12011-10-06 21:24:13 +0200296 return fd;
297}
298
299static int fio_client_connect_sock(struct fio_client *client)
300{
301 struct sockaddr_un *addr = &client->addr_un;
302 fio_socklen_t len;
303 int fd;
304
305 memset(addr, 0, sizeof(*addr));
306 addr->sun_family = AF_UNIX;
307 strcpy(addr->sun_path, client->hostname);
308
309 fd = socket(AF_UNIX, SOCK_STREAM, 0);
310 if (fd < 0) {
311 log_err("fio: socket: %s\n", strerror(errno));
312 return -1;
313 }
314
315 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
316 if (connect(fd, (struct sockaddr *) addr, len) < 0) {
317 log_err("fio: connect; %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200318 close(fd);
Jens Axboe87aa8f12011-10-06 21:24:13 +0200319 return -1;
320 }
321
322 return fd;
323}
324
325static int fio_client_connect(struct fio_client *client)
326{
327 int fd;
328
329 dprint(FD_NET, "client: connect to host %s\n", client->hostname);
330
Jens Axboe87aa8f12011-10-06 21:24:13 +0200331 if (client->is_sock)
332 fd = fio_client_connect_sock(client);
333 else
334 fd = fio_client_connect_ip(client);
335
Jens Axboe89c17072011-10-11 10:15:51 +0200336 dprint(FD_NET, "client: %s connected %d\n", client->hostname, fd);
337
Jens Axboe87aa8f12011-10-06 21:24:13 +0200338 if (fd < 0)
339 return 1;
340
Jens Axboeb66570d2011-10-01 11:11:35 -0600341 client->fd = fd;
Jens Axboebebe6392011-10-07 10:00:51 +0200342 fio_client_add_hash(client);
Jens Axboe81179ee2011-10-04 12:42:06 +0200343 client->state = Client_connected;
Jens Axboe132159a2011-09-30 15:01:32 -0600344 return 0;
345}
346
Jens Axboecc0df002011-10-03 20:53:32 +0200347void fio_clients_terminate(void)
348{
349 struct flist_head *entry;
350 struct fio_client *client;
351
Jens Axboe60efd142011-10-04 13:30:11 +0200352 dprint(FD_NET, "client: terminate clients\n");
353
Jens Axboecc0df002011-10-03 20:53:32 +0200354 flist_for_each(entry, &client_list) {
355 client = flist_entry(entry, struct fio_client, list);
356
Jens Axboe89c17072011-10-11 10:15:51 +0200357 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_QUIT, 0, NULL);
Jens Axboecc0df002011-10-03 20:53:32 +0200358 }
359}
360
361static void sig_int(int sig)
362{
Jens Axboebebe6392011-10-07 10:00:51 +0200363 dprint(FD_NET, "client: got signal %d\n", sig);
Jens Axboecc0df002011-10-03 20:53:32 +0200364 fio_clients_terminate();
365}
366
Jens Axboe4c6d91e2012-03-30 10:30:35 +0200367static void sig_show_status(int sig)
368{
369 show_running_run_stats();
370}
371
Jens Axboecc0df002011-10-03 20:53:32 +0200372static void client_signal_handler(void)
373{
374 struct sigaction act;
375
376 memset(&act, 0, sizeof(act));
377 act.sa_handler = sig_int;
378 act.sa_flags = SA_RESTART;
379 sigaction(SIGINT, &act, NULL);
380
381 memset(&act, 0, sizeof(act));
382 act.sa_handler = sig_int;
383 act.sa_flags = SA_RESTART;
384 sigaction(SIGTERM, &act, NULL);
Jens Axboe4c6d91e2012-03-30 10:30:35 +0200385
386 memset(&act, 0, sizeof(act));
387 act.sa_handler = sig_show_status;
388 act.sa_flags = SA_RESTART;
389 sigaction(SIGUSR1, &act, NULL);
Jens Axboecc0df002011-10-03 20:53:32 +0200390}
391
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200392static void probe_client(struct fio_client *client)
393{
Jens Axboe60efd142011-10-04 13:30:11 +0200394 dprint(FD_NET, "client: send probe\n");
395
Jens Axboe89c17072011-10-11 10:15:51 +0200396 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_PROBE, 0, &client->cmd_list);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200397}
398
Jens Axboe81179ee2011-10-04 12:42:06 +0200399static int send_client_cmd_line(struct fio_client *client)
400{
Jens Axboefa2ea802011-10-08 21:07:29 +0200401 struct cmd_single_line_pdu *cslp;
402 struct cmd_line_pdu *clp;
403 unsigned long offset;
Jens Axboe7f868312011-10-10 09:55:21 +0200404 unsigned int *lens;
Jens Axboefa2ea802011-10-08 21:07:29 +0200405 void *pdu;
406 size_t mem;
Jens Axboe81179ee2011-10-04 12:42:06 +0200407 int i, ret;
408
Jens Axboe39e8e012011-10-04 13:46:08 +0200409 dprint(FD_NET, "client: send cmdline %d\n", client->argc);
Jens Axboe60efd142011-10-04 13:30:11 +0200410
Jens Axboe7f868312011-10-10 09:55:21 +0200411 lens = malloc(client->argc * sizeof(unsigned int));
412
Jens Axboefa2ea802011-10-08 21:07:29 +0200413 /*
414 * Find out how much mem we need
415 */
Jens Axboe7f868312011-10-10 09:55:21 +0200416 for (i = 0, mem = 0; i < client->argc; i++) {
417 lens[i] = strlen(client->argv[i]) + 1;
418 mem += lens[i];
419 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200420
Jens Axboefa2ea802011-10-08 21:07:29 +0200421 /*
422 * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu
423 */
424 mem += sizeof(*clp) + (client->argc * sizeof(*cslp));
425
426 pdu = malloc(mem);
427 clp = pdu;
428 offset = sizeof(*clp);
429
430 for (i = 0; i < client->argc; i++) {
Jens Axboe7f868312011-10-10 09:55:21 +0200431 uint16_t arg_len = lens[i];
Jens Axboefa2ea802011-10-08 21:07:29 +0200432
433 cslp = pdu + offset;
434 strcpy((char *) cslp->text, client->argv[i]);
435 cslp->len = cpu_to_le16(arg_len);
436 offset += sizeof(*cslp) + arg_len;
437 }
438
Jens Axboe7f868312011-10-10 09:55:21 +0200439 free(lens);
Jens Axboefa2ea802011-10-08 21:07:29 +0200440 clp->lines = cpu_to_le16(client->argc);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200441 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, 0);
Jens Axboe81179ee2011-10-04 12:42:06 +0200442 free(pdu);
443 return ret;
444}
445
Jens Axboea37f69b2011-10-01 12:24:21 -0600446int fio_clients_connect(void)
447{
448 struct fio_client *client;
449 struct flist_head *entry, *tmp;
450 int ret;
451
Bruce Cran93bcfd22012-02-20 20:18:19 +0100452#ifdef WIN32
453 WSADATA wsd;
454 WSAStartup(MAKEWORD(2,2), &wsd);
455#endif
456
Jens Axboe60efd142011-10-04 13:30:11 +0200457 dprint(FD_NET, "client: connect all\n");
458
Jens Axboecc0df002011-10-03 20:53:32 +0200459 client_signal_handler();
460
Jens Axboea37f69b2011-10-01 12:24:21 -0600461 flist_for_each_safe(entry, tmp, &client_list) {
462 client = flist_entry(entry, struct fio_client, list);
463
464 ret = fio_client_connect(client);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200465 if (ret) {
Jens Axboea37f69b2011-10-01 12:24:21 -0600466 remove_client(client);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200467 continue;
468 }
469
470 probe_client(client);
Jens Axboe81179ee2011-10-04 12:42:06 +0200471
472 if (client->argc > 1)
473 send_client_cmd_line(client);
Jens Axboea37f69b2011-10-01 12:24:21 -0600474 }
475
476 return !nr_clients;
477}
478
Jens Axboe132159a2011-09-30 15:01:32 -0600479/*
480 * Send file contents to server backend. We could use sendfile(), but to remain
481 * more portable lets just read/write the darn thing.
482 */
Jens Axboea37f69b2011-10-01 12:24:21 -0600483static int fio_client_send_ini(struct fio_client *client, const char *filename)
Jens Axboe132159a2011-09-30 15:01:32 -0600484{
485 struct stat sb;
486 char *p, *buf;
487 off_t len;
488 int fd, ret;
489
Jens Axboe46c48f12011-10-01 12:50:51 -0600490 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
491
Jens Axboe132159a2011-09-30 15:01:32 -0600492 fd = open(filename, O_RDONLY);
493 if (fd < 0) {
Jens Axboee951bdc2011-10-05 21:58:45 +0200494 log_err("fio: job file <%s> open: %s\n", filename, strerror(errno));
Jens Axboe132159a2011-09-30 15:01:32 -0600495 return 1;
496 }
497
498 if (fstat(fd, &sb) < 0) {
499 log_err("fio: job file stat: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200500 close(fd);
Jens Axboe132159a2011-09-30 15:01:32 -0600501 return 1;
502 }
503
504 buf = malloc(sb.st_size);
505
506 len = sb.st_size;
507 p = buf;
508 do {
509 ret = read(fd, p, len);
510 if (ret > 0) {
511 len -= ret;
512 if (!len)
513 break;
514 p += ret;
515 continue;
516 } else if (!ret)
517 break;
518 else if (errno == EAGAIN || errno == EINTR)
519 continue;
520 } while (1);
521
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200522 if (len) {
523 log_err("fio: failed reading job file %s\n", filename);
Jens Axboeb94cba42011-10-06 21:27:10 +0200524 close(fd);
Jens Axboec524ef72011-10-07 12:23:34 +0200525 free(buf);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200526 return 1;
527 }
528
Jens Axboec2cb6862012-02-23 20:56:12 +0100529 client->sent_job = 1;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200530 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, buf, sb.st_size, 0);
Jens Axboe132159a2011-09-30 15:01:32 -0600531 free(buf);
Jens Axboeb94cba42011-10-06 21:27:10 +0200532 close(fd);
Jens Axboe132159a2011-09-30 15:01:32 -0600533 return ret;
534}
Jens Axboe37db14f2011-09-30 17:00:42 -0600535
Jens Axboea37f69b2011-10-01 12:24:21 -0600536int fio_clients_send_ini(const char *filename)
537{
538 struct fio_client *client;
539 struct flist_head *entry, *tmp;
540
541 flist_for_each_safe(entry, tmp, &client_list) {
542 client = flist_entry(entry, struct fio_client, list);
543
Jens Axboebd023602012-08-26 17:30:45 +0200544 if (client->nr_ini_file) {
545 int i;
546
547 for (i = 0; i < client->nr_ini_file; i++) {
548 const char *ini = client->ini_file[i];
549
550 if (fio_client_send_ini(client, ini)) {
551 remove_client(client);
552 break;
553 }
554 }
555 } else if (!filename || fio_client_send_ini(client, filename))
Jens Axboea37f69b2011-10-01 12:24:21 -0600556 remove_client(client);
Jens Axboec2cb6862012-02-23 20:56:12 +0100557
558 client->sent_job = 1;
Jens Axboea37f69b2011-10-01 12:24:21 -0600559 }
560
561 return !nr_clients;
562}
563
Jens Axboea64e88d2011-10-03 14:20:01 +0200564static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
565{
566 dst->max_val = le64_to_cpu(src->max_val);
567 dst->min_val = le64_to_cpu(src->min_val);
568 dst->samples = le64_to_cpu(src->samples);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200569
570 /*
571 * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
572 */
573 dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
574 dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
Jens Axboea64e88d2011-10-03 14:20:01 +0200575}
576
577static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
578{
579 int i, j;
580
581 dst->error = le32_to_cpu(src->error);
582 dst->groupid = le32_to_cpu(src->groupid);
583 dst->pid = le32_to_cpu(src->pid);
584 dst->members = le32_to_cpu(src->members);
585
586 for (i = 0; i < 2; i++) {
587 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
588 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
589 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
590 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
591 }
592
593 dst->usr_time = le64_to_cpu(src->usr_time);
594 dst->sys_time = le64_to_cpu(src->sys_time);
595 dst->ctx = le64_to_cpu(src->ctx);
596 dst->minf = le64_to_cpu(src->minf);
597 dst->majf = le64_to_cpu(src->majf);
598 dst->clat_percentiles = le64_to_cpu(src->clat_percentiles);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200599
600 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
601 fio_fp64_t *fps = &src->percentile_list[i];
602 fio_fp64_t *fpd = &dst->percentile_list[i];
603
604 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
605 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200606
607 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
608 dst->io_u_map[i] = le32_to_cpu(src->io_u_map[i]);
609 dst->io_u_submit[i] = le32_to_cpu(src->io_u_submit[i]);
610 dst->io_u_complete[i] = le32_to_cpu(src->io_u_complete[i]);
611 }
612
613 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
614 dst->io_u_lat_u[i] = le32_to_cpu(src->io_u_lat_u[i]);
615 dst->io_u_lat_m[i] = le32_to_cpu(src->io_u_lat_m[i]);
616 }
617
618 for (i = 0; i < 2; i++)
619 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
620 dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]);
621
622 for (i = 0; i < 3; i++) {
623 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
Jens Axboe93eee042011-10-03 21:08:48 +0200624 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
Jens Axboea64e88d2011-10-03 14:20:01 +0200625 }
626
627 dst->total_submit = le64_to_cpu(src->total_submit);
628 dst->total_complete = le64_to_cpu(src->total_complete);
629
630 for (i = 0; i < 2; i++) {
631 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
632 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
633 }
634
635 dst->total_run_time = le64_to_cpu(src->total_run_time);
636 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
637 dst->total_err_count = le64_to_cpu(src->total_err_count);
Jens Axboeddcc0b62011-10-03 14:45:27 +0200638 dst->first_error = le32_to_cpu(src->first_error);
639 dst->kb_base = le32_to_cpu(src->kb_base);
Jens Axboea64e88d2011-10-03 14:20:01 +0200640}
641
642static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
643{
644 int i;
645
646 for (i = 0; i < 2; i++) {
647 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
648 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
649 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
650 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
651 dst->io_kb[i] = le64_to_cpu(src->io_kb[i]);
652 dst->agg[i] = le64_to_cpu(src->agg[i]);
653 }
654
655 dst->kb_base = le32_to_cpu(src->kb_base);
656 dst->groupid = le32_to_cpu(src->groupid);
657}
658
659static void handle_ts(struct fio_net_cmd *cmd)
660{
661 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
662
663 convert_ts(&p->ts, &p->ts);
664 convert_gs(&p->rs, &p->rs);
665
666 show_thread_status(&p->ts, &p->rs);
Jens Axboe37f0c1a2011-10-11 14:08:33 +0200667
668 if (sum_stat_clients == 1)
669 return;
670
671 sum_thread_stats(&client_ts, &p->ts, sum_stat_nr);
672 sum_group_stats(&client_gs, &p->rs);
673
674 client_ts.members++;
675 client_ts.groupid = p->ts.groupid;
676
677 if (++sum_stat_nr == sum_stat_clients) {
678 strcpy(client_ts.name, "All clients");
679 show_thread_status(&client_ts, &client_gs);
680 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200681}
682
683static void handle_gs(struct fio_net_cmd *cmd)
684{
685 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
686
687 convert_gs(gs, gs);
688 show_group_stats(gs);
689}
690
Jens Axboed09a64a2011-10-13 11:38:56 +0200691static void convert_agg(struct disk_util_agg *agg)
692{
693 int i;
694
695 for (i = 0; i < 2; i++) {
696 agg->ios[i] = le32_to_cpu(agg->ios[i]);
697 agg->merges[i] = le32_to_cpu(agg->merges[i]);
698 agg->sectors[i] = le64_to_cpu(agg->sectors[i]);
699 agg->ticks[i] = le32_to_cpu(agg->ticks[i]);
700 }
701
702 agg->io_ticks = le32_to_cpu(agg->io_ticks);
703 agg->time_in_queue = le32_to_cpu(agg->time_in_queue);
704 agg->slavecount = le32_to_cpu(agg->slavecount);
Anton Blanchard823ba542011-11-07 14:16:26 +0100705 agg->max_util.u.f = fio_uint64_to_double(__le64_to_cpu(agg->max_util.u.i));
Jens Axboed09a64a2011-10-13 11:38:56 +0200706}
707
708static void convert_dus(struct disk_util_stat *dus)
709{
710 int i;
711
712 for (i = 0; i < 2; i++) {
713 dus->ios[i] = le32_to_cpu(dus->ios[i]);
714 dus->merges[i] = le32_to_cpu(dus->merges[i]);
715 dus->sectors[i] = le64_to_cpu(dus->sectors[i]);
716 dus->ticks[i] = le32_to_cpu(dus->ticks[i]);
717 }
718
719 dus->io_ticks = le32_to_cpu(dus->io_ticks);
720 dus->time_in_queue = le32_to_cpu(dus->time_in_queue);
721 dus->msec = le64_to_cpu(dus->msec);
722}
723
724static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd)
725{
726 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
727
728 convert_dus(&du->dus);
729 convert_agg(&du->agg);
730
731 if (!client->disk_stats_shown) {
732 client->disk_stats_shown = 1;
733 log_info("\nDisk stats (read/write):\n");
734 }
735
Jens Axboef3afa572012-09-17 13:34:16 +0200736 print_disk_util(&du->dus, &du->agg, output_format == FIO_OUTPUT_TERSE);
Jens Axboed09a64a2011-10-13 11:38:56 +0200737}
738
Jens Axboe48fbb462011-10-09 12:19:08 +0200739static void convert_jobs_eta(struct jobs_eta *je)
Jens Axboecf451d12011-10-03 16:48:30 +0200740{
Jens Axboecf451d12011-10-03 16:48:30 +0200741 int i;
742
743 je->nr_running = le32_to_cpu(je->nr_running);
744 je->nr_ramp = le32_to_cpu(je->nr_ramp);
745 je->nr_pending = le32_to_cpu(je->nr_pending);
746 je->files_open = le32_to_cpu(je->files_open);
747 je->m_rate = le32_to_cpu(je->m_rate);
748 je->t_rate = le32_to_cpu(je->t_rate);
749 je->m_iops = le32_to_cpu(je->m_iops);
750 je->t_iops = le32_to_cpu(je->t_iops);
751
752 for (i = 0; i < 2; i++) {
753 je->rate[i] = le32_to_cpu(je->rate[i]);
754 je->iops[i] = le32_to_cpu(je->iops[i]);
755 }
756
Jens Axboeb51eedb2011-10-09 12:13:39 +0200757 je->elapsed_sec = le64_to_cpu(je->elapsed_sec);
Jens Axboecf451d12011-10-03 16:48:30 +0200758 je->eta_sec = le64_to_cpu(je->eta_sec);
Jens Axboeb7f05eb2012-05-11 20:33:02 +0200759 je->is_pow2 = le32_to_cpu(je->is_pow2);
Jens Axboe48fbb462011-10-09 12:19:08 +0200760}
Jens Axboecf451d12011-10-03 16:48:30 +0200761
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200762static void sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je)
Jens Axboe48fbb462011-10-09 12:19:08 +0200763{
Jens Axboe48fbb462011-10-09 12:19:08 +0200764 int i;
765
766 dst->nr_running += je->nr_running;
767 dst->nr_ramp += je->nr_ramp;
768 dst->nr_pending += je->nr_pending;
769 dst->files_open += je->files_open;
770 dst->m_rate += je->m_rate;
771 dst->t_rate += je->t_rate;
772 dst->m_iops += je->m_iops;
773 dst->t_iops += je->t_iops;
774
775 for (i = 0; i < 2; i++) {
776 dst->rate[i] += je->rate[i];
777 dst->iops[i] += je->iops[i];
778 }
779
780 dst->elapsed_sec += je->elapsed_sec;
781
782 if (je->eta_sec > dst->eta_sec)
783 dst->eta_sec = je->eta_sec;
784}
785
Jens Axboe82c1ed32011-10-10 08:56:18 +0200786static void dec_jobs_eta(struct client_eta *eta)
787{
788 if (!--eta->pending) {
789 display_thread_status(&eta->eta);
790 free(eta);
791 }
792}
793
Jens Axboe89c17072011-10-11 10:15:51 +0200794static void remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd)
795{
796 struct fio_net_int_cmd *icmd = NULL;
797 struct flist_head *entry;
798
799 flist_for_each(entry, &client->cmd_list) {
800 icmd = flist_entry(entry, struct fio_net_int_cmd, list);
801
Jens Axboedf380932011-10-11 14:25:08 +0200802 if (cmd->tag == (uintptr_t) icmd)
Jens Axboe89c17072011-10-11 10:15:51 +0200803 break;
804
805 icmd = NULL;
806 }
807
808 if (!icmd) {
809 log_err("fio: client: unable to find matching tag\n");
810 return;
811 }
812
813 flist_del(&icmd->list);
814 cmd->tag = icmd->saved_tag;
815 free(icmd);
816}
817
Jens Axboe82c1ed32011-10-10 08:56:18 +0200818static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboe48fbb462011-10-09 12:19:08 +0200819{
820 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
Jens Axboedf380932011-10-11 14:25:08 +0200821 struct client_eta *eta = (struct client_eta *) (uintptr_t) cmd->tag;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200822
823 dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending);
Jens Axboe48fbb462011-10-09 12:19:08 +0200824
Jens Axboef77d2672011-10-10 14:36:07 +0200825 assert(client->eta_in_flight == eta);
826
827 client->eta_in_flight = NULL;
Jens Axboe82c1ed32011-10-10 08:56:18 +0200828 flist_del_init(&client->eta_list);
829
Jens Axboe48fbb462011-10-09 12:19:08 +0200830 convert_jobs_eta(je);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200831 sum_jobs_eta(&eta->eta, je);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200832 dec_jobs_eta(eta);
Jens Axboecf451d12011-10-03 16:48:30 +0200833}
834
Jens Axboeb5296dd2011-10-07 16:35:56 +0200835static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200836{
837 struct cmd_probe_pdu *probe = (struct cmd_probe_pdu *) cmd->payload;
Jens Axboed2333352011-10-11 15:07:23 +0200838 const char *os, *arch;
839 char bit[16];
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200840
Jens Axboecca84642011-10-07 12:47:57 +0200841 os = fio_get_os_string(probe->os);
842 if (!os)
843 os = "unknown";
844
845 arch = fio_get_arch_string(probe->arch);
846 if (!arch)
847 os = "unknown";
848
Jens Axboed2333352011-10-11 15:07:23 +0200849 sprintf(bit, "%d-bit", probe->bpp * 8);
Jens Axboe38fdef22011-10-11 14:30:06 +0200850
Jens Axboe0922d612012-04-16 09:52:22 +0200851 log_info("hostname=%s, be=%u, %s, os=%s, arch=%s, fio=%s\n",
Jens Axboe38fdef22011-10-11 14:30:06 +0200852 probe->hostname, probe->bigendian, bit, os, arch,
Jens Axboe0922d612012-04-16 09:52:22 +0200853 probe->fio_version);
Jens Axboeb5296dd2011-10-07 16:35:56 +0200854
855 if (!client->name)
856 client->name = strdup((char *) probe->hostname);
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200857}
858
Jens Axboe11e950b2011-10-16 21:34:14 +0200859static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd)
860{
861 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
862
863 client->state = Client_started;
864 client->jobs = le32_to_cpu(pdu->jobs);
865}
866
867static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd)
868{
869 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
870
871 client->state = Client_stopped;
872 client->error = le32_to_cpu(pdu->error);
Jens Axboe498c92c2011-10-17 09:14:42 +0200873
874 if (client->error)
875 log_info("client <%s>: exited with error %d\n", client->hostname, client->error);
Jens Axboe11e950b2011-10-16 21:34:14 +0200876}
877
Jens Axboee951bdc2011-10-05 21:58:45 +0200878static int handle_client(struct fio_client *client)
Jens Axboe37db14f2011-09-30 17:00:42 -0600879{
880 struct fio_net_cmd *cmd;
881
Jens Axboe60efd142011-10-04 13:30:11 +0200882 dprint(FD_NET, "client: handle %s\n", client->hostname);
883
Jens Axboee951bdc2011-10-05 21:58:45 +0200884 cmd = fio_net_recv_cmd(client->fd);
885 if (!cmd)
886 return 0;
Jens Axboec2c94582011-10-05 20:31:30 +0200887
Jens Axboe89c17072011-10-11 10:15:51 +0200888 dprint(FD_NET, "client: got cmd op %s from %s\n",
889 fio_server_op(cmd->opcode), client->hostname);
Jens Axboe46c48f12011-10-01 12:50:51 -0600890
Jens Axboee951bdc2011-10-05 21:58:45 +0200891 switch (cmd->opcode) {
892 case FIO_NET_CMD_QUIT:
893 remove_client(client);
894 free(cmd);
895 break;
896 case FIO_NET_CMD_TEXT: {
897 const char *buf = (const char *) cmd->payload;
Jens Axboeb5296dd2011-10-07 16:35:56 +0200898 const char *name;
Jens Axboee951bdc2011-10-05 21:58:45 +0200899 int fio_unused ret;
Jens Axboe17dd1762011-10-04 13:27:34 +0200900
Jens Axboeb5296dd2011-10-07 16:35:56 +0200901 name = client->name ? client->name : client->hostname;
902
Jens Axboee951bdc2011-10-05 21:58:45 +0200903 if (!client->skip_newline)
Jens Axboeb5296dd2011-10-07 16:35:56 +0200904 fprintf(f_out, "<%s> ", name);
Jens Axboee951bdc2011-10-05 21:58:45 +0200905 ret = fwrite(buf, cmd->pdu_len, 1, f_out);
906 fflush(f_out);
907 client->skip_newline = strchr(buf, '\n') == NULL;
908 free(cmd);
909 break;
Jens Axboe37db14f2011-09-30 17:00:42 -0600910 }
Jens Axboed09a64a2011-10-13 11:38:56 +0200911 case FIO_NET_CMD_DU:
912 handle_du(client, cmd);
913 free(cmd);
914 break;
Jens Axboee951bdc2011-10-05 21:58:45 +0200915 case FIO_NET_CMD_TS:
916 handle_ts(cmd);
917 free(cmd);
918 break;
919 case FIO_NET_CMD_GS:
920 handle_gs(cmd);
921 free(cmd);
922 break;
923 case FIO_NET_CMD_ETA:
Jens Axboe89c17072011-10-11 10:15:51 +0200924 remove_reply_cmd(client, cmd);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200925 handle_eta(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200926 free(cmd);
927 break;
928 case FIO_NET_CMD_PROBE:
Jens Axboe89c17072011-10-11 10:15:51 +0200929 remove_reply_cmd(client, cmd);
Jens Axboeb5296dd2011-10-07 16:35:56 +0200930 handle_probe(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200931 free(cmd);
932 break;
Jens Axboe01be0382011-10-15 14:43:41 +0200933 case FIO_NET_CMD_RUN:
934 client->state = Client_running;
935 free(cmd);
936 break;
Jens Axboee951bdc2011-10-05 21:58:45 +0200937 case FIO_NET_CMD_START:
Jens Axboe11e950b2011-10-16 21:34:14 +0200938 handle_start(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200939 free(cmd);
940 break;
941 case FIO_NET_CMD_STOP:
Jens Axboe11e950b2011-10-16 21:34:14 +0200942 handle_stop(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200943 free(cmd);
944 break;
945 default:
Jens Axboe89c17072011-10-11 10:15:51 +0200946 log_err("fio: unknown client op: %s\n", fio_server_op(cmd->opcode));
Jens Axboee951bdc2011-10-05 21:58:45 +0200947 free(cmd);
948 break;
Jens Axboe37db14f2011-09-30 17:00:42 -0600949 }
950
Jens Axboee951bdc2011-10-05 21:58:45 +0200951 return 1;
Jens Axboe37db14f2011-09-30 17:00:42 -0600952}
Jens Axboeb66570d2011-10-01 11:11:35 -0600953
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200954static void request_client_etas(void)
955{
956 struct fio_client *client;
957 struct flist_head *entry;
958 struct client_eta *eta;
Jens Axboe82c1ed32011-10-10 08:56:18 +0200959 int skipped = 0;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200960
961 dprint(FD_NET, "client: request eta (%d)\n", nr_clients);
962
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200963 eta = malloc(sizeof(*eta));
964 memset(&eta->eta, 0, sizeof(eta->eta));
965 eta->pending = nr_clients;
966
967 flist_for_each(entry, &client_list) {
968 client = flist_entry(entry, struct fio_client, list);
969
Jens Axboe82c1ed32011-10-10 08:56:18 +0200970 if (!flist_empty(&client->eta_list)) {
971 skipped++;
972 continue;
973 }
Jens Axboe01be0382011-10-15 14:43:41 +0200974 if (client->state != Client_running)
975 continue;
Jens Axboe82c1ed32011-10-10 08:56:18 +0200976
Jens Axboef77d2672011-10-10 14:36:07 +0200977 assert(!client->eta_in_flight);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200978 flist_add_tail(&client->eta_list, &eta_list);
Jens Axboef77d2672011-10-10 14:36:07 +0200979 client->eta_in_flight = eta;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200980 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA,
Jens Axboedf380932011-10-11 14:25:08 +0200981 (uintptr_t) eta, &client->cmd_list);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200982 }
983
Jens Axboe82c1ed32011-10-10 08:56:18 +0200984 while (skipped--)
985 dec_jobs_eta(eta);
986
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200987 dprint(FD_NET, "client: requested eta tag %p\n", eta);
988}
989
Jens Axboe89c17072011-10-11 10:15:51 +0200990static int client_check_cmd_timeout(struct fio_client *client,
991 struct timeval *now)
992{
993 struct fio_net_int_cmd *cmd;
994 struct flist_head *entry, *tmp;
995 int ret = 0;
996
997 flist_for_each_safe(entry, tmp, &client->cmd_list) {
998 cmd = flist_entry(entry, struct fio_net_int_cmd, list);
999
1000 if (mtime_since(&cmd->tv, now) < FIO_NET_CLIENT_TIMEOUT)
1001 continue;
1002
1003 log_err("fio: client %s, timeout on cmd %s\n", client->hostname,
1004 fio_server_op(cmd->cmd.opcode));
1005 flist_del(&cmd->list);
1006 free(cmd);
1007 ret = 1;
1008 }
1009
1010 return flist_empty(&client->cmd_list) && ret;
1011}
1012
1013static int fio_client_timed_out(void)
1014{
1015 struct fio_client *client;
1016 struct flist_head *entry, *tmp;
1017 struct timeval tv;
1018 int ret = 0;
1019
1020 gettimeofday(&tv, NULL);
1021
1022 flist_for_each_safe(entry, tmp, &client_list) {
1023 client = flist_entry(entry, struct fio_client, list);
1024
1025 if (flist_empty(&client->cmd_list))
1026 continue;
1027
1028 if (!client_check_cmd_timeout(client, &tv))
1029 continue;
1030
1031 log_err("fio: client %s timed out\n", client->hostname);
1032 remove_client(client);
1033 ret = 1;
1034 }
1035
1036 return ret;
1037}
1038
Jens Axboeb66570d2011-10-01 11:11:35 -06001039int fio_handle_clients(void)
1040{
Jens Axboeb66570d2011-10-01 11:11:35 -06001041 struct pollfd *pfds;
Jens Axboe498c92c2011-10-17 09:14:42 +02001042 int i, ret = 0, retval = 0;
Jens Axboeb66570d2011-10-01 11:11:35 -06001043
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001044 gettimeofday(&eta_tv, NULL);
1045
Jens Axboeb66570d2011-10-01 11:11:35 -06001046 pfds = malloc(nr_clients * sizeof(struct pollfd));
1047
Jens Axboe37f0c1a2011-10-11 14:08:33 +02001048 sum_stat_clients = nr_clients;
1049 init_thread_stat(&client_ts);
1050 init_group_run_stat(&client_gs);
1051
Jens Axboeb66570d2011-10-01 11:11:35 -06001052 while (!exit_backend && nr_clients) {
Jens Axboec2cb6862012-02-23 20:56:12 +01001053 struct flist_head *entry, *tmp;
1054 struct fio_client *client;
1055
Jens Axboe82a4be12011-10-01 12:40:32 -06001056 i = 0;
Jens Axboec2cb6862012-02-23 20:56:12 +01001057 flist_for_each_safe(entry, tmp, &client_list) {
Jens Axboe82a4be12011-10-01 12:40:32 -06001058 client = flist_entry(entry, struct fio_client, list);
1059
Jens Axboec2cb6862012-02-23 20:56:12 +01001060 if (!client->sent_job &&
1061 flist_empty(&client->cmd_list)) {
1062 remove_client(client);
1063 continue;
1064 }
1065
Jens Axboe82a4be12011-10-01 12:40:32 -06001066 pfds[i].fd = client->fd;
1067 pfds[i].events = POLLIN;
1068 i++;
1069 }
1070
Jens Axboec2cb6862012-02-23 20:56:12 +01001071 if (!nr_clients)
1072 break;
1073
Jens Axboe82a4be12011-10-01 12:40:32 -06001074 assert(i == nr_clients);
1075
Jens Axboe5c2857f2011-10-04 13:14:32 +02001076 do {
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001077 struct timeval tv;
1078
1079 gettimeofday(&tv, NULL);
1080 if (mtime_since(&eta_tv, &tv) >= 900) {
1081 request_client_etas();
1082 memcpy(&eta_tv, &tv, sizeof(tv));
Jens Axboe89c17072011-10-11 10:15:51 +02001083
1084 if (fio_client_timed_out())
1085 break;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +02001086 }
1087
Jens Axboe5c2857f2011-10-04 13:14:32 +02001088 ret = poll(pfds, nr_clients, 100);
1089 if (ret < 0) {
1090 if (errno == EINTR)
1091 continue;
1092 log_err("fio: poll clients: %s\n", strerror(errno));
1093 break;
1094 } else if (!ret)
Jens Axboeb66570d2011-10-01 11:11:35 -06001095 continue;
Jens Axboe5c2857f2011-10-04 13:14:32 +02001096 } while (ret <= 0);
Jens Axboeb66570d2011-10-01 11:11:35 -06001097
1098 for (i = 0; i < nr_clients; i++) {
1099 if (!(pfds[i].revents & POLLIN))
1100 continue;
1101
1102 client = find_client_by_fd(pfds[i].fd);
1103 if (!client) {
Jens Axboe3c5f57e2011-10-06 12:37:50 +02001104 log_err("fio: unknown client fd %d\n", pfds[i].fd);
Jens Axboeb66570d2011-10-01 11:11:35 -06001105 continue;
1106 }
Jens Axboee951bdc2011-10-05 21:58:45 +02001107 if (!handle_client(client)) {
Jens Axboe28d3ab02011-10-05 20:45:37 +02001108 log_info("client: host=%s disconnected\n",
1109 client->hostname);
1110 remove_client(client);
Jens Axboe498c92c2011-10-17 09:14:42 +02001111 retval = 1;
Jens Axboe38990762011-10-17 13:31:33 +02001112 } else if (client->error)
Jens Axboe498c92c2011-10-17 09:14:42 +02001113 retval = 1;
Jens Axboee55f8f32012-03-06 15:42:31 +01001114 put_client(client);
Jens Axboeb66570d2011-10-01 11:11:35 -06001115 }
1116 }
1117
1118 free(pfds);
Jens Axboe498c92c2011-10-17 09:14:42 +02001119 return retval;
Jens Axboeb66570d2011-10-01 11:11:35 -06001120}