blob: e6e4291b31d7715ed7f26426f7dee3f834a57fc0 [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 Axboeb66570d2011-10-01 11:11:35 -060032 struct sockaddr_in addr;
Jens Axboe87aa8f12011-10-06 21:24:13 +020033 struct sockaddr_un addr_un;
Jens Axboeb66570d2011-10-01 11:11:35 -060034 char *hostname;
Jens Axboebebe6392011-10-07 10:00:51 +020035 int port;
Jens Axboeb66570d2011-10-01 11:11:35 -060036 int fd;
Jens Axboe81179ee2011-10-04 12:42:06 +020037
Jens Axboeb5296dd2011-10-07 16:35:56 +020038 char *name;
39
Jens Axboe81179ee2011-10-04 12:42:06 +020040 int state;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +020041
Jens Axboe17dd1762011-10-04 13:27:34 +020042 int skip_newline;
Jens Axboe87aa8f12011-10-06 21:24:13 +020043 int is_sock;
Jens Axboed09a64a2011-10-13 11:38:56 +020044 int disk_stats_shown;
Jens Axboe11e950b2011-10-16 21:34:14 +020045 unsigned int jobs;
46 int error;
Jens Axboe82c1ed32011-10-10 08:56:18 +020047
48 struct flist_head eta_list;
49 struct client_eta *eta_in_flight;
Jens Axboe81179ee2011-10-04 12:42:06 +020050
Jens Axboe89c17072011-10-11 10:15:51 +020051 struct flist_head cmd_list;
52
Jens Axboe81179ee2011-10-04 12:42:06 +020053 uint16_t argc;
54 char **argv;
55};
56
Jens Axboeaf9c9fb2011-10-09 21:54:10 +020057static struct timeval eta_tv;
Jens Axboe48fbb462011-10-09 12:19:08 +020058
Jens Axboe81179ee2011-10-04 12:42:06 +020059enum {
Jens Axboe5c2857f2011-10-04 13:14:32 +020060 Client_created = 0,
Jens Axboe81179ee2011-10-04 12:42:06 +020061 Client_connected = 1,
62 Client_started = 2,
Jens Axboe01be0382011-10-15 14:43:41 +020063 Client_running = 3,
64 Client_stopped = 4,
65 Client_exited = 5,
Jens Axboeb66570d2011-10-01 11:11:35 -060066};
67
68static FLIST_HEAD(client_list);
Jens Axboe82c1ed32011-10-10 08:56:18 +020069static FLIST_HEAD(eta_list);
Jens Axboeb66570d2011-10-01 11:11:35 -060070
Jens Axboe3f3a4542011-10-10 21:11:09 +020071static FLIST_HEAD(arg_list);
72
Jens Axboe37f0c1a2011-10-11 14:08:33 +020073static struct thread_stat client_ts;
74static struct group_run_stats client_gs;
75static int sum_stat_clients;
76static int sum_stat_nr;
77
Jens Axboe3c5f57e2011-10-06 12:37:50 +020078#define FIO_CLIENT_HASH_BITS 7
79#define FIO_CLIENT_HASH_SZ (1 << FIO_CLIENT_HASH_BITS)
80#define FIO_CLIENT_HASH_MASK (FIO_CLIENT_HASH_SZ - 1)
Jens Axboebebe6392011-10-07 10:00:51 +020081static struct flist_head client_hash[FIO_CLIENT_HASH_SZ];
Jens Axboe3c5f57e2011-10-06 12:37:50 +020082
Jens Axboee951bdc2011-10-05 21:58:45 +020083static int handle_client(struct fio_client *client);
Jens Axboe82c1ed32011-10-10 08:56:18 +020084static void dec_jobs_eta(struct client_eta *eta);
Jens Axboe0b8f30a2011-10-04 10:31:53 +020085
Jens Axboebebe6392011-10-07 10:00:51 +020086static void fio_client_add_hash(struct fio_client *client)
Jens Axboe3c5f57e2011-10-06 12:37:50 +020087{
88 int bucket = hash_long(client->fd, FIO_CLIENT_HASH_BITS);
89
90 bucket &= FIO_CLIENT_HASH_MASK;
Jens Axboebebe6392011-10-07 10:00:51 +020091 flist_add(&client->hash_list, &client_hash[bucket]);
Jens Axboe3c5f57e2011-10-06 12:37:50 +020092}
93
Jens Axboebebe6392011-10-07 10:00:51 +020094static void fio_client_remove_hash(struct fio_client *client)
Jens Axboe3c5f57e2011-10-06 12:37:50 +020095{
Jens Axboebebe6392011-10-07 10:00:51 +020096 if (!flist_empty(&client->hash_list))
97 flist_del_init(&client->hash_list);
Jens Axboe3c5f57e2011-10-06 12:37:50 +020098}
99
100static void fio_init fio_client_hash_init(void)
101{
102 int i;
103
Jens Axboebebe6392011-10-07 10:00:51 +0200104 for (i = 0; i < FIO_CLIENT_HASH_SZ; i++)
105 INIT_FLIST_HEAD(&client_hash[i]);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200106}
107
Jens Axboeb66570d2011-10-01 11:11:35 -0600108static struct fio_client *find_client_by_fd(int fd)
109{
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200110 int bucket = hash_long(fd, FIO_CLIENT_HASH_BITS) & FIO_CLIENT_HASH_MASK;
Jens Axboeb66570d2011-10-01 11:11:35 -0600111 struct fio_client *client;
112 struct flist_head *entry;
113
Jens Axboebebe6392011-10-07 10:00:51 +0200114 flist_for_each(entry, &client_hash[bucket]) {
115 client = flist_entry(entry, struct fio_client, hash_list);
Jens Axboeb66570d2011-10-01 11:11:35 -0600116
117 if (client->fd == fd)
118 return client;
119 }
120
121 return NULL;
122}
123
Jens Axboeb66570d2011-10-01 11:11:35 -0600124static void remove_client(struct fio_client *client)
125{
Jens Axboe39e8e012011-10-04 13:46:08 +0200126 dprint(FD_NET, "client: removed <%s>\n", client->hostname);
Jens Axboeb66570d2011-10-01 11:11:35 -0600127 flist_del(&client->list);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200128
Jens Axboebebe6392011-10-07 10:00:51 +0200129 fio_client_remove_hash(client);
Jens Axboe81179ee2011-10-04 12:42:06 +0200130
Jens Axboe82c1ed32011-10-10 08:56:18 +0200131 if (!flist_empty(&client->eta_list)) {
132 flist_del_init(&client->eta_list);
133 dec_jobs_eta(client->eta_in_flight);
134 }
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200135
Jens Axboeb66570d2011-10-01 11:11:35 -0600136 free(client->hostname);
Jens Axboe81179ee2011-10-04 12:42:06 +0200137 if (client->argv)
138 free(client->argv);
Jens Axboeb5296dd2011-10-07 16:35:56 +0200139 if (client->name)
140 free(client->name);
Jens Axboe81179ee2011-10-04 12:42:06 +0200141
Jens Axboeb66570d2011-10-01 11:11:35 -0600142 free(client);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200143 nr_clients--;
Jens Axboe5fd0acb2011-10-11 14:20:22 +0200144 sum_stat_clients--;
Jens Axboeb66570d2011-10-01 11:11:35 -0600145}
Jens Axboe132159a2011-09-30 15:01:32 -0600146
Jens Axboefa2ea802011-10-08 21:07:29 +0200147static void __fio_client_add_cmd_option(struct fio_client *client,
148 const char *opt)
Jens Axboe81179ee2011-10-04 12:42:06 +0200149{
Jens Axboe39e8e012011-10-04 13:46:08 +0200150 int index;
151
152 index = client->argc++;
Jens Axboe81179ee2011-10-04 12:42:06 +0200153 client->argv = realloc(client->argv, sizeof(char *) * client->argc);
Jens Axboe39e8e012011-10-04 13:46:08 +0200154 client->argv[index] = strdup(opt);
155 dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
Jens Axboe81179ee2011-10-04 12:42:06 +0200156}
157
Jens Axboefa2ea802011-10-08 21:07:29 +0200158void fio_client_add_cmd_option(void *cookie, const char *opt)
Jens Axboe81179ee2011-10-04 12:42:06 +0200159{
Jens Axboebebe6392011-10-07 10:00:51 +0200160 struct fio_client *client = cookie;
Jens Axboe3f3a4542011-10-10 21:11:09 +0200161 struct flist_head *entry;
Jens Axboe81179ee2011-10-04 12:42:06 +0200162
Jens Axboebebe6392011-10-07 10:00:51 +0200163 if (!client || !opt)
Jens Axboefa2ea802011-10-08 21:07:29 +0200164 return;
Jens Axboe81179ee2011-10-04 12:42:06 +0200165
Jens Axboefa2ea802011-10-08 21:07:29 +0200166 __fio_client_add_cmd_option(client, opt);
Jens Axboe3f3a4542011-10-10 21:11:09 +0200167
168 /*
169 * Duplicate arguments to shared client group
170 */
171 flist_for_each(entry, &arg_list) {
172 client = flist_entry(entry, struct fio_client, arg_list);
173
174 __fio_client_add_cmd_option(client, opt);
175 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200176}
177
Jens Axboebebe6392011-10-07 10:00:51 +0200178int fio_client_add(const char *hostname, void **cookie)
Jens Axboe132159a2011-09-30 15:01:32 -0600179{
Jens Axboe3f3a4542011-10-10 21:11:09 +0200180 struct fio_client *existing = *cookie;
Jens Axboeb66570d2011-10-01 11:11:35 -0600181 struct fio_client *client;
Jens Axboe132159a2011-09-30 15:01:32 -0600182
Jens Axboe3f3a4542011-10-10 21:11:09 +0200183 if (existing) {
184 /*
185 * We always add our "exec" name as the option, hence 1
186 * means empty.
187 */
188 if (existing->argc == 1)
189 flist_add_tail(&existing->arg_list, &arg_list);
190 else {
191 while (!flist_empty(&arg_list))
192 flist_del_init(arg_list.next);
193 }
194 }
195
Jens Axboeb66570d2011-10-01 11:11:35 -0600196 client = malloc(sizeof(*client));
Jens Axboea37f69b2011-10-01 12:24:21 -0600197 memset(client, 0, sizeof(*client));
Jens Axboe81179ee2011-10-04 12:42:06 +0200198
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200199 INIT_FLIST_HEAD(&client->list);
Jens Axboebebe6392011-10-07 10:00:51 +0200200 INIT_FLIST_HEAD(&client->hash_list);
Jens Axboe3f3a4542011-10-10 21:11:09 +0200201 INIT_FLIST_HEAD(&client->arg_list);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200202 INIT_FLIST_HEAD(&client->eta_list);
Jens Axboe89c17072011-10-11 10:15:51 +0200203 INIT_FLIST_HEAD(&client->cmd_list);
Jens Axboe3c5f57e2011-10-06 12:37:50 +0200204
Jens Axboebebe6392011-10-07 10:00:51 +0200205 if (fio_server_parse_string(hostname, &client->hostname,
206 &client->is_sock, &client->port,
207 &client->addr.sin_addr))
208 return -1;
209
Jens Axboea37f69b2011-10-01 12:24:21 -0600210 client->fd = -1;
Jens Axboe81179ee2011-10-04 12:42:06 +0200211
212 __fio_client_add_cmd_option(client, "fio");
213
Jens Axboea37f69b2011-10-01 12:24:21 -0600214 flist_add(&client->list, &client_list);
215 nr_clients++;
Jens Axboebebe6392011-10-07 10:00:51 +0200216 dprint(FD_NET, "client: added <%s>\n", client->hostname);
217 *cookie = client;
218 return 0;
Jens Axboea37f69b2011-10-01 12:24:21 -0600219}
220
Jens Axboe87aa8f12011-10-06 21:24:13 +0200221static int fio_client_connect_ip(struct fio_client *client)
Jens Axboea37f69b2011-10-01 12:24:21 -0600222{
223 int fd;
Jens Axboe132159a2011-09-30 15:01:32 -0600224
Jens Axboeb66570d2011-10-01 11:11:35 -0600225 client->addr.sin_family = AF_INET;
Jens Axboebebe6392011-10-07 10:00:51 +0200226 client->addr.sin_port = htons(client->port);
Jens Axboe132159a2011-09-30 15:01:32 -0600227
228 fd = socket(AF_INET, SOCK_STREAM, 0);
229 if (fd < 0) {
230 log_err("fio: socket: %s\n", strerror(errno));
Jens Axboe87aa8f12011-10-06 21:24:13 +0200231 return -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600232 }
233
Jens Axboeb66570d2011-10-01 11:11:35 -0600234 if (connect(fd, (struct sockaddr *) &client->addr, sizeof(client->addr)) < 0) {
Jens Axboe132159a2011-09-30 15:01:32 -0600235 log_err("fio: connect: %s\n", strerror(errno));
Jens Axboea7de0a12011-10-07 12:55:14 +0200236 log_err("fio: failed to connect to %s:%u\n", client->hostname,
237 client->port);
Jens Axboeb94cba42011-10-06 21:27:10 +0200238 close(fd);
Jens Axboe87aa8f12011-10-06 21:24:13 +0200239 return -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600240 }
241
Jens Axboe87aa8f12011-10-06 21:24:13 +0200242 return fd;
243}
244
245static int fio_client_connect_sock(struct fio_client *client)
246{
247 struct sockaddr_un *addr = &client->addr_un;
248 fio_socklen_t len;
249 int fd;
250
251 memset(addr, 0, sizeof(*addr));
252 addr->sun_family = AF_UNIX;
253 strcpy(addr->sun_path, client->hostname);
254
255 fd = socket(AF_UNIX, SOCK_STREAM, 0);
256 if (fd < 0) {
257 log_err("fio: socket: %s\n", strerror(errno));
258 return -1;
259 }
260
261 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
262 if (connect(fd, (struct sockaddr *) addr, len) < 0) {
263 log_err("fio: connect; %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200264 close(fd);
Jens Axboe87aa8f12011-10-06 21:24:13 +0200265 return -1;
266 }
267
268 return fd;
269}
270
271static int fio_client_connect(struct fio_client *client)
272{
273 int fd;
274
275 dprint(FD_NET, "client: connect to host %s\n", client->hostname);
276
Jens Axboe87aa8f12011-10-06 21:24:13 +0200277 if (client->is_sock)
278 fd = fio_client_connect_sock(client);
279 else
280 fd = fio_client_connect_ip(client);
281
Jens Axboe89c17072011-10-11 10:15:51 +0200282 dprint(FD_NET, "client: %s connected %d\n", client->hostname, fd);
283
Jens Axboe87aa8f12011-10-06 21:24:13 +0200284 if (fd < 0)
285 return 1;
286
Jens Axboeb66570d2011-10-01 11:11:35 -0600287 client->fd = fd;
Jens Axboebebe6392011-10-07 10:00:51 +0200288 fio_client_add_hash(client);
Jens Axboe81179ee2011-10-04 12:42:06 +0200289 client->state = Client_connected;
Jens Axboe132159a2011-09-30 15:01:32 -0600290 return 0;
291}
292
Jens Axboecc0df002011-10-03 20:53:32 +0200293void fio_clients_terminate(void)
294{
295 struct flist_head *entry;
296 struct fio_client *client;
297
Jens Axboe60efd142011-10-04 13:30:11 +0200298 dprint(FD_NET, "client: terminate clients\n");
299
Jens Axboecc0df002011-10-03 20:53:32 +0200300 flist_for_each(entry, &client_list) {
301 client = flist_entry(entry, struct fio_client, list);
302
Jens Axboe89c17072011-10-11 10:15:51 +0200303 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_QUIT, 0, NULL);
Jens Axboecc0df002011-10-03 20:53:32 +0200304 }
305}
306
307static void sig_int(int sig)
308{
Jens Axboebebe6392011-10-07 10:00:51 +0200309 dprint(FD_NET, "client: got signal %d\n", sig);
Jens Axboecc0df002011-10-03 20:53:32 +0200310 fio_clients_terminate();
311}
312
313static void client_signal_handler(void)
314{
315 struct sigaction act;
316
317 memset(&act, 0, sizeof(act));
318 act.sa_handler = sig_int;
319 act.sa_flags = SA_RESTART;
320 sigaction(SIGINT, &act, NULL);
321
322 memset(&act, 0, sizeof(act));
323 act.sa_handler = sig_int;
324 act.sa_flags = SA_RESTART;
325 sigaction(SIGTERM, &act, NULL);
326}
327
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200328static void probe_client(struct fio_client *client)
329{
Jens Axboe60efd142011-10-04 13:30:11 +0200330 dprint(FD_NET, "client: send probe\n");
331
Jens Axboe89c17072011-10-11 10:15:51 +0200332 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_PROBE, 0, &client->cmd_list);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200333}
334
Jens Axboe81179ee2011-10-04 12:42:06 +0200335static int send_client_cmd_line(struct fio_client *client)
336{
Jens Axboefa2ea802011-10-08 21:07:29 +0200337 struct cmd_single_line_pdu *cslp;
338 struct cmd_line_pdu *clp;
339 unsigned long offset;
Jens Axboe7f868312011-10-10 09:55:21 +0200340 unsigned int *lens;
Jens Axboefa2ea802011-10-08 21:07:29 +0200341 void *pdu;
342 size_t mem;
Jens Axboe81179ee2011-10-04 12:42:06 +0200343 int i, ret;
344
Jens Axboe39e8e012011-10-04 13:46:08 +0200345 dprint(FD_NET, "client: send cmdline %d\n", client->argc);
Jens Axboe60efd142011-10-04 13:30:11 +0200346
Jens Axboe7f868312011-10-10 09:55:21 +0200347 lens = malloc(client->argc * sizeof(unsigned int));
348
Jens Axboefa2ea802011-10-08 21:07:29 +0200349 /*
350 * Find out how much mem we need
351 */
Jens Axboe7f868312011-10-10 09:55:21 +0200352 for (i = 0, mem = 0; i < client->argc; i++) {
353 lens[i] = strlen(client->argv[i]) + 1;
354 mem += lens[i];
355 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200356
Jens Axboefa2ea802011-10-08 21:07:29 +0200357 /*
358 * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu
359 */
360 mem += sizeof(*clp) + (client->argc * sizeof(*cslp));
361
362 pdu = malloc(mem);
363 clp = pdu;
364 offset = sizeof(*clp);
365
366 for (i = 0; i < client->argc; i++) {
Jens Axboe7f868312011-10-10 09:55:21 +0200367 uint16_t arg_len = lens[i];
Jens Axboefa2ea802011-10-08 21:07:29 +0200368
369 cslp = pdu + offset;
370 strcpy((char *) cslp->text, client->argv[i]);
371 cslp->len = cpu_to_le16(arg_len);
372 offset += sizeof(*cslp) + arg_len;
373 }
374
Jens Axboe7f868312011-10-10 09:55:21 +0200375 free(lens);
Jens Axboefa2ea802011-10-08 21:07:29 +0200376 clp->lines = cpu_to_le16(client->argc);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200377 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, 0);
Jens Axboe81179ee2011-10-04 12:42:06 +0200378 free(pdu);
379 return ret;
380}
381
Jens Axboea37f69b2011-10-01 12:24:21 -0600382int fio_clients_connect(void)
383{
384 struct fio_client *client;
385 struct flist_head *entry, *tmp;
386 int ret;
387
Jens Axboe60efd142011-10-04 13:30:11 +0200388 dprint(FD_NET, "client: connect all\n");
389
Jens Axboecc0df002011-10-03 20:53:32 +0200390 client_signal_handler();
391
Jens Axboea37f69b2011-10-01 12:24:21 -0600392 flist_for_each_safe(entry, tmp, &client_list) {
393 client = flist_entry(entry, struct fio_client, list);
394
395 ret = fio_client_connect(client);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200396 if (ret) {
Jens Axboea37f69b2011-10-01 12:24:21 -0600397 remove_client(client);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200398 continue;
399 }
400
401 probe_client(client);
Jens Axboe81179ee2011-10-04 12:42:06 +0200402
403 if (client->argc > 1)
404 send_client_cmd_line(client);
Jens Axboea37f69b2011-10-01 12:24:21 -0600405 }
406
407 return !nr_clients;
408}
409
Jens Axboe132159a2011-09-30 15:01:32 -0600410/*
411 * Send file contents to server backend. We could use sendfile(), but to remain
412 * more portable lets just read/write the darn thing.
413 */
Jens Axboea37f69b2011-10-01 12:24:21 -0600414static int fio_client_send_ini(struct fio_client *client, const char *filename)
Jens Axboe132159a2011-09-30 15:01:32 -0600415{
416 struct stat sb;
417 char *p, *buf;
418 off_t len;
419 int fd, ret;
420
Jens Axboe46c48f12011-10-01 12:50:51 -0600421 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
422
Jens Axboe132159a2011-09-30 15:01:32 -0600423 fd = open(filename, O_RDONLY);
424 if (fd < 0) {
Jens Axboee951bdc2011-10-05 21:58:45 +0200425 log_err("fio: job file <%s> open: %s\n", filename, strerror(errno));
Jens Axboe132159a2011-09-30 15:01:32 -0600426 return 1;
427 }
428
429 if (fstat(fd, &sb) < 0) {
430 log_err("fio: job file stat: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200431 close(fd);
Jens Axboe132159a2011-09-30 15:01:32 -0600432 return 1;
433 }
434
435 buf = malloc(sb.st_size);
436
437 len = sb.st_size;
438 p = buf;
439 do {
440 ret = read(fd, p, len);
441 if (ret > 0) {
442 len -= ret;
443 if (!len)
444 break;
445 p += ret;
446 continue;
447 } else if (!ret)
448 break;
449 else if (errno == EAGAIN || errno == EINTR)
450 continue;
451 } while (1);
452
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200453 if (len) {
454 log_err("fio: failed reading job file %s\n", filename);
Jens Axboeb94cba42011-10-06 21:27:10 +0200455 close(fd);
Jens Axboec524ef72011-10-07 12:23:34 +0200456 free(buf);
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200457 return 1;
458 }
459
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200460 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, buf, sb.st_size, 0);
Jens Axboe132159a2011-09-30 15:01:32 -0600461 free(buf);
Jens Axboeb94cba42011-10-06 21:27:10 +0200462 close(fd);
Jens Axboe132159a2011-09-30 15:01:32 -0600463 return ret;
464}
Jens Axboe37db14f2011-09-30 17:00:42 -0600465
Jens Axboea37f69b2011-10-01 12:24:21 -0600466int fio_clients_send_ini(const char *filename)
467{
468 struct fio_client *client;
469 struct flist_head *entry, *tmp;
470
471 flist_for_each_safe(entry, tmp, &client_list) {
472 client = flist_entry(entry, struct fio_client, list);
473
474 if (fio_client_send_ini(client, filename))
475 remove_client(client);
476 }
477
478 return !nr_clients;
479}
480
Jens Axboea64e88d2011-10-03 14:20:01 +0200481static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
482{
483 dst->max_val = le64_to_cpu(src->max_val);
484 dst->min_val = le64_to_cpu(src->min_val);
485 dst->samples = le64_to_cpu(src->samples);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200486
487 /*
488 * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
489 */
490 dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
491 dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
Jens Axboea64e88d2011-10-03 14:20:01 +0200492}
493
494static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
495{
496 int i, j;
497
498 dst->error = le32_to_cpu(src->error);
499 dst->groupid = le32_to_cpu(src->groupid);
500 dst->pid = le32_to_cpu(src->pid);
501 dst->members = le32_to_cpu(src->members);
502
503 for (i = 0; i < 2; i++) {
504 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
505 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
506 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
507 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
508 }
509
510 dst->usr_time = le64_to_cpu(src->usr_time);
511 dst->sys_time = le64_to_cpu(src->sys_time);
512 dst->ctx = le64_to_cpu(src->ctx);
513 dst->minf = le64_to_cpu(src->minf);
514 dst->majf = le64_to_cpu(src->majf);
515 dst->clat_percentiles = le64_to_cpu(src->clat_percentiles);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200516
517 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
518 fio_fp64_t *fps = &src->percentile_list[i];
519 fio_fp64_t *fpd = &dst->percentile_list[i];
520
521 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
522 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200523
524 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
525 dst->io_u_map[i] = le32_to_cpu(src->io_u_map[i]);
526 dst->io_u_submit[i] = le32_to_cpu(src->io_u_submit[i]);
527 dst->io_u_complete[i] = le32_to_cpu(src->io_u_complete[i]);
528 }
529
530 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
531 dst->io_u_lat_u[i] = le32_to_cpu(src->io_u_lat_u[i]);
532 dst->io_u_lat_m[i] = le32_to_cpu(src->io_u_lat_m[i]);
533 }
534
535 for (i = 0; i < 2; i++)
536 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
537 dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]);
538
539 for (i = 0; i < 3; i++) {
540 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
Jens Axboe93eee042011-10-03 21:08:48 +0200541 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
Jens Axboea64e88d2011-10-03 14:20:01 +0200542 }
543
544 dst->total_submit = le64_to_cpu(src->total_submit);
545 dst->total_complete = le64_to_cpu(src->total_complete);
546
547 for (i = 0; i < 2; i++) {
548 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
549 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
550 }
551
552 dst->total_run_time = le64_to_cpu(src->total_run_time);
553 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
554 dst->total_err_count = le64_to_cpu(src->total_err_count);
Jens Axboeddcc0b62011-10-03 14:45:27 +0200555 dst->first_error = le32_to_cpu(src->first_error);
556 dst->kb_base = le32_to_cpu(src->kb_base);
Jens Axboea64e88d2011-10-03 14:20:01 +0200557}
558
559static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
560{
561 int i;
562
563 for (i = 0; i < 2; i++) {
564 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
565 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
566 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
567 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
568 dst->io_kb[i] = le64_to_cpu(src->io_kb[i]);
569 dst->agg[i] = le64_to_cpu(src->agg[i]);
570 }
571
572 dst->kb_base = le32_to_cpu(src->kb_base);
573 dst->groupid = le32_to_cpu(src->groupid);
574}
575
576static void handle_ts(struct fio_net_cmd *cmd)
577{
578 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
579
580 convert_ts(&p->ts, &p->ts);
581 convert_gs(&p->rs, &p->rs);
582
583 show_thread_status(&p->ts, &p->rs);
Jens Axboe37f0c1a2011-10-11 14:08:33 +0200584
585 if (sum_stat_clients == 1)
586 return;
587
588 sum_thread_stats(&client_ts, &p->ts, sum_stat_nr);
589 sum_group_stats(&client_gs, &p->rs);
590
591 client_ts.members++;
592 client_ts.groupid = p->ts.groupid;
593
594 if (++sum_stat_nr == sum_stat_clients) {
595 strcpy(client_ts.name, "All clients");
596 show_thread_status(&client_ts, &client_gs);
597 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200598}
599
600static void handle_gs(struct fio_net_cmd *cmd)
601{
602 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
603
604 convert_gs(gs, gs);
605 show_group_stats(gs);
606}
607
Jens Axboed09a64a2011-10-13 11:38:56 +0200608static void convert_agg(struct disk_util_agg *agg)
609{
610 int i;
611
612 for (i = 0; i < 2; i++) {
613 agg->ios[i] = le32_to_cpu(agg->ios[i]);
614 agg->merges[i] = le32_to_cpu(agg->merges[i]);
615 agg->sectors[i] = le64_to_cpu(agg->sectors[i]);
616 agg->ticks[i] = le32_to_cpu(agg->ticks[i]);
617 }
618
619 agg->io_ticks = le32_to_cpu(agg->io_ticks);
620 agg->time_in_queue = le32_to_cpu(agg->time_in_queue);
621 agg->slavecount = le32_to_cpu(agg->slavecount);
622 agg->max_util.u.f = __le64_to_cpu(fio_uint64_to_double(agg->max_util.u.i));
623}
624
625static void convert_dus(struct disk_util_stat *dus)
626{
627 int i;
628
629 for (i = 0; i < 2; i++) {
630 dus->ios[i] = le32_to_cpu(dus->ios[i]);
631 dus->merges[i] = le32_to_cpu(dus->merges[i]);
632 dus->sectors[i] = le64_to_cpu(dus->sectors[i]);
633 dus->ticks[i] = le32_to_cpu(dus->ticks[i]);
634 }
635
636 dus->io_ticks = le32_to_cpu(dus->io_ticks);
637 dus->time_in_queue = le32_to_cpu(dus->time_in_queue);
638 dus->msec = le64_to_cpu(dus->msec);
639}
640
641static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd)
642{
643 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
644
645 convert_dus(&du->dus);
646 convert_agg(&du->agg);
647
648 if (!client->disk_stats_shown) {
649 client->disk_stats_shown = 1;
650 log_info("\nDisk stats (read/write):\n");
651 }
652
Jens Axboef2f788d2011-10-13 14:03:52 +0200653 print_disk_util(&du->dus, &du->agg, terse_output);
Jens Axboed09a64a2011-10-13 11:38:56 +0200654}
655
Jens Axboe48fbb462011-10-09 12:19:08 +0200656static void convert_jobs_eta(struct jobs_eta *je)
Jens Axboecf451d12011-10-03 16:48:30 +0200657{
Jens Axboecf451d12011-10-03 16:48:30 +0200658 int i;
659
660 je->nr_running = le32_to_cpu(je->nr_running);
661 je->nr_ramp = le32_to_cpu(je->nr_ramp);
662 je->nr_pending = le32_to_cpu(je->nr_pending);
663 je->files_open = le32_to_cpu(je->files_open);
664 je->m_rate = le32_to_cpu(je->m_rate);
665 je->t_rate = le32_to_cpu(je->t_rate);
666 je->m_iops = le32_to_cpu(je->m_iops);
667 je->t_iops = le32_to_cpu(je->t_iops);
668
669 for (i = 0; i < 2; i++) {
670 je->rate[i] = le32_to_cpu(je->rate[i]);
671 je->iops[i] = le32_to_cpu(je->iops[i]);
672 }
673
Jens Axboeb51eedb2011-10-09 12:13:39 +0200674 je->elapsed_sec = le64_to_cpu(je->elapsed_sec);
Jens Axboecf451d12011-10-03 16:48:30 +0200675 je->eta_sec = le64_to_cpu(je->eta_sec);
Jens Axboe48fbb462011-10-09 12:19:08 +0200676}
Jens Axboecf451d12011-10-03 16:48:30 +0200677
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200678static void sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je)
Jens Axboe48fbb462011-10-09 12:19:08 +0200679{
Jens Axboe48fbb462011-10-09 12:19:08 +0200680 int i;
681
682 dst->nr_running += je->nr_running;
683 dst->nr_ramp += je->nr_ramp;
684 dst->nr_pending += je->nr_pending;
685 dst->files_open += je->files_open;
686 dst->m_rate += je->m_rate;
687 dst->t_rate += je->t_rate;
688 dst->m_iops += je->m_iops;
689 dst->t_iops += je->t_iops;
690
691 for (i = 0; i < 2; i++) {
692 dst->rate[i] += je->rate[i];
693 dst->iops[i] += je->iops[i];
694 }
695
696 dst->elapsed_sec += je->elapsed_sec;
697
698 if (je->eta_sec > dst->eta_sec)
699 dst->eta_sec = je->eta_sec;
700}
701
Jens Axboe82c1ed32011-10-10 08:56:18 +0200702static void dec_jobs_eta(struct client_eta *eta)
703{
704 if (!--eta->pending) {
705 display_thread_status(&eta->eta);
706 free(eta);
707 }
708}
709
Jens Axboe89c17072011-10-11 10:15:51 +0200710static void remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd)
711{
712 struct fio_net_int_cmd *icmd = NULL;
713 struct flist_head *entry;
714
715 flist_for_each(entry, &client->cmd_list) {
716 icmd = flist_entry(entry, struct fio_net_int_cmd, list);
717
Jens Axboedf380932011-10-11 14:25:08 +0200718 if (cmd->tag == (uintptr_t) icmd)
Jens Axboe89c17072011-10-11 10:15:51 +0200719 break;
720
721 icmd = NULL;
722 }
723
724 if (!icmd) {
725 log_err("fio: client: unable to find matching tag\n");
726 return;
727 }
728
729 flist_del(&icmd->list);
730 cmd->tag = icmd->saved_tag;
731 free(icmd);
732}
733
Jens Axboe82c1ed32011-10-10 08:56:18 +0200734static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboe48fbb462011-10-09 12:19:08 +0200735{
736 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
Jens Axboedf380932011-10-11 14:25:08 +0200737 struct client_eta *eta = (struct client_eta *) (uintptr_t) cmd->tag;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200738
739 dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending);
Jens Axboe48fbb462011-10-09 12:19:08 +0200740
Jens Axboef77d2672011-10-10 14:36:07 +0200741 assert(client->eta_in_flight == eta);
742
743 client->eta_in_flight = NULL;
Jens Axboe82c1ed32011-10-10 08:56:18 +0200744 flist_del_init(&client->eta_list);
745
Jens Axboe48fbb462011-10-09 12:19:08 +0200746 convert_jobs_eta(je);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200747 sum_jobs_eta(&eta->eta, je);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200748 dec_jobs_eta(eta);
Jens Axboecf451d12011-10-03 16:48:30 +0200749}
750
Jens Axboeb5296dd2011-10-07 16:35:56 +0200751static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200752{
753 struct cmd_probe_pdu *probe = (struct cmd_probe_pdu *) cmd->payload;
Jens Axboed2333352011-10-11 15:07:23 +0200754 const char *os, *arch;
755 char bit[16];
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200756
Jens Axboecca84642011-10-07 12:47:57 +0200757 os = fio_get_os_string(probe->os);
758 if (!os)
759 os = "unknown";
760
761 arch = fio_get_arch_string(probe->arch);
762 if (!arch)
763 os = "unknown";
764
Jens Axboed2333352011-10-11 15:07:23 +0200765 sprintf(bit, "%d-bit", probe->bpp * 8);
Jens Axboe38fdef22011-10-11 14:30:06 +0200766
767 log_info("hostname=%s, be=%u, %s, os=%s, arch=%s, fio=%u.%u.%u\n",
768 probe->hostname, probe->bigendian, bit, os, arch,
769 probe->fio_major, probe->fio_minor, probe->fio_patch);
Jens Axboeb5296dd2011-10-07 16:35:56 +0200770
771 if (!client->name)
772 client->name = strdup((char *) probe->hostname);
Jens Axboe2e03b4b2011-10-04 09:00:40 +0200773}
774
Jens Axboe11e950b2011-10-16 21:34:14 +0200775static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd)
776{
777 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
778
779 client->state = Client_started;
780 client->jobs = le32_to_cpu(pdu->jobs);
781}
782
783static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd)
784{
785 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
786
787 client->state = Client_stopped;
788 client->error = le32_to_cpu(pdu->error);
789}
790
Jens Axboee951bdc2011-10-05 21:58:45 +0200791static int handle_client(struct fio_client *client)
Jens Axboe37db14f2011-09-30 17:00:42 -0600792{
793 struct fio_net_cmd *cmd;
794
Jens Axboe60efd142011-10-04 13:30:11 +0200795 dprint(FD_NET, "client: handle %s\n", client->hostname);
796
Jens Axboee951bdc2011-10-05 21:58:45 +0200797 cmd = fio_net_recv_cmd(client->fd);
798 if (!cmd)
799 return 0;
Jens Axboec2c94582011-10-05 20:31:30 +0200800
Jens Axboe89c17072011-10-11 10:15:51 +0200801 dprint(FD_NET, "client: got cmd op %s from %s\n",
802 fio_server_op(cmd->opcode), client->hostname);
Jens Axboe46c48f12011-10-01 12:50:51 -0600803
Jens Axboee951bdc2011-10-05 21:58:45 +0200804 switch (cmd->opcode) {
805 case FIO_NET_CMD_QUIT:
806 remove_client(client);
807 free(cmd);
808 break;
809 case FIO_NET_CMD_TEXT: {
810 const char *buf = (const char *) cmd->payload;
Jens Axboeb5296dd2011-10-07 16:35:56 +0200811 const char *name;
Jens Axboee951bdc2011-10-05 21:58:45 +0200812 int fio_unused ret;
Jens Axboe17dd1762011-10-04 13:27:34 +0200813
Jens Axboeb5296dd2011-10-07 16:35:56 +0200814 name = client->name ? client->name : client->hostname;
815
Jens Axboee951bdc2011-10-05 21:58:45 +0200816 if (!client->skip_newline)
Jens Axboeb5296dd2011-10-07 16:35:56 +0200817 fprintf(f_out, "<%s> ", name);
Jens Axboee951bdc2011-10-05 21:58:45 +0200818 ret = fwrite(buf, cmd->pdu_len, 1, f_out);
819 fflush(f_out);
820 client->skip_newline = strchr(buf, '\n') == NULL;
821 free(cmd);
822 break;
Jens Axboe37db14f2011-09-30 17:00:42 -0600823 }
Jens Axboed09a64a2011-10-13 11:38:56 +0200824 case FIO_NET_CMD_DU:
825 handle_du(client, cmd);
826 free(cmd);
827 break;
Jens Axboee951bdc2011-10-05 21:58:45 +0200828 case FIO_NET_CMD_TS:
829 handle_ts(cmd);
830 free(cmd);
831 break;
832 case FIO_NET_CMD_GS:
833 handle_gs(cmd);
834 free(cmd);
835 break;
836 case FIO_NET_CMD_ETA:
Jens Axboe89c17072011-10-11 10:15:51 +0200837 remove_reply_cmd(client, cmd);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200838 handle_eta(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200839 free(cmd);
840 break;
841 case FIO_NET_CMD_PROBE:
Jens Axboe89c17072011-10-11 10:15:51 +0200842 remove_reply_cmd(client, cmd);
Jens Axboeb5296dd2011-10-07 16:35:56 +0200843 handle_probe(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200844 free(cmd);
845 break;
Jens Axboe01be0382011-10-15 14:43:41 +0200846 case FIO_NET_CMD_RUN:
847 client->state = Client_running;
848 free(cmd);
849 break;
Jens Axboee951bdc2011-10-05 21:58:45 +0200850 case FIO_NET_CMD_START:
Jens Axboe11e950b2011-10-16 21:34:14 +0200851 handle_start(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200852 free(cmd);
853 break;
854 case FIO_NET_CMD_STOP:
Jens Axboe11e950b2011-10-16 21:34:14 +0200855 handle_stop(client, cmd);
Jens Axboee951bdc2011-10-05 21:58:45 +0200856 free(cmd);
857 break;
858 default:
Jens Axboe89c17072011-10-11 10:15:51 +0200859 log_err("fio: unknown client op: %s\n", fio_server_op(cmd->opcode));
Jens Axboee951bdc2011-10-05 21:58:45 +0200860 free(cmd);
861 break;
Jens Axboe37db14f2011-09-30 17:00:42 -0600862 }
863
Jens Axboee951bdc2011-10-05 21:58:45 +0200864 return 1;
Jens Axboe37db14f2011-09-30 17:00:42 -0600865}
Jens Axboeb66570d2011-10-01 11:11:35 -0600866
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200867static void request_client_etas(void)
868{
869 struct fio_client *client;
870 struct flist_head *entry;
871 struct client_eta *eta;
Jens Axboe82c1ed32011-10-10 08:56:18 +0200872 int skipped = 0;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200873
874 dprint(FD_NET, "client: request eta (%d)\n", nr_clients);
875
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200876 eta = malloc(sizeof(*eta));
877 memset(&eta->eta, 0, sizeof(eta->eta));
878 eta->pending = nr_clients;
879
880 flist_for_each(entry, &client_list) {
881 client = flist_entry(entry, struct fio_client, list);
882
Jens Axboe82c1ed32011-10-10 08:56:18 +0200883 if (!flist_empty(&client->eta_list)) {
884 skipped++;
885 continue;
886 }
Jens Axboe01be0382011-10-15 14:43:41 +0200887 if (client->state != Client_running)
888 continue;
Jens Axboe82c1ed32011-10-10 08:56:18 +0200889
Jens Axboef77d2672011-10-10 14:36:07 +0200890 assert(!client->eta_in_flight);
Jens Axboe82c1ed32011-10-10 08:56:18 +0200891 flist_add_tail(&client->eta_list, &eta_list);
Jens Axboef77d2672011-10-10 14:36:07 +0200892 client->eta_in_flight = eta;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200893 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA,
Jens Axboedf380932011-10-11 14:25:08 +0200894 (uintptr_t) eta, &client->cmd_list);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200895 }
896
Jens Axboe82c1ed32011-10-10 08:56:18 +0200897 while (skipped--)
898 dec_jobs_eta(eta);
899
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200900 dprint(FD_NET, "client: requested eta tag %p\n", eta);
901}
902
Jens Axboe89c17072011-10-11 10:15:51 +0200903static int client_check_cmd_timeout(struct fio_client *client,
904 struct timeval *now)
905{
906 struct fio_net_int_cmd *cmd;
907 struct flist_head *entry, *tmp;
908 int ret = 0;
909
910 flist_for_each_safe(entry, tmp, &client->cmd_list) {
911 cmd = flist_entry(entry, struct fio_net_int_cmd, list);
912
913 if (mtime_since(&cmd->tv, now) < FIO_NET_CLIENT_TIMEOUT)
914 continue;
915
916 log_err("fio: client %s, timeout on cmd %s\n", client->hostname,
917 fio_server_op(cmd->cmd.opcode));
918 flist_del(&cmd->list);
919 free(cmd);
920 ret = 1;
921 }
922
923 return flist_empty(&client->cmd_list) && ret;
924}
925
926static int fio_client_timed_out(void)
927{
928 struct fio_client *client;
929 struct flist_head *entry, *tmp;
930 struct timeval tv;
931 int ret = 0;
932
933 gettimeofday(&tv, NULL);
934
935 flist_for_each_safe(entry, tmp, &client_list) {
936 client = flist_entry(entry, struct fio_client, list);
937
938 if (flist_empty(&client->cmd_list))
939 continue;
940
941 if (!client_check_cmd_timeout(client, &tv))
942 continue;
943
944 log_err("fio: client %s timed out\n", client->hostname);
945 remove_client(client);
946 ret = 1;
947 }
948
949 return ret;
950}
951
Jens Axboeb66570d2011-10-01 11:11:35 -0600952int fio_handle_clients(void)
953{
954 struct fio_client *client;
955 struct flist_head *entry;
956 struct pollfd *pfds;
Jens Axboe82a4be12011-10-01 12:40:32 -0600957 int i, ret = 0;
Jens Axboeb66570d2011-10-01 11:11:35 -0600958
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200959 gettimeofday(&eta_tv, NULL);
960
Jens Axboeb66570d2011-10-01 11:11:35 -0600961 pfds = malloc(nr_clients * sizeof(struct pollfd));
962
Jens Axboe37f0c1a2011-10-11 14:08:33 +0200963 sum_stat_clients = nr_clients;
964 init_thread_stat(&client_ts);
965 init_group_run_stat(&client_gs);
966
Jens Axboeb66570d2011-10-01 11:11:35 -0600967 while (!exit_backend && nr_clients) {
Jens Axboe82a4be12011-10-01 12:40:32 -0600968 i = 0;
969 flist_for_each(entry, &client_list) {
970 client = flist_entry(entry, struct fio_client, list);
971
972 pfds[i].fd = client->fd;
973 pfds[i].events = POLLIN;
974 i++;
975 }
976
977 assert(i == nr_clients);
978
Jens Axboe5c2857f2011-10-04 13:14:32 +0200979 do {
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200980 struct timeval tv;
981
982 gettimeofday(&tv, NULL);
983 if (mtime_since(&eta_tv, &tv) >= 900) {
984 request_client_etas();
985 memcpy(&eta_tv, &tv, sizeof(tv));
Jens Axboe89c17072011-10-11 10:15:51 +0200986
987 if (fio_client_timed_out())
988 break;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200989 }
990
Jens Axboe5c2857f2011-10-04 13:14:32 +0200991 ret = poll(pfds, nr_clients, 100);
992 if (ret < 0) {
993 if (errno == EINTR)
994 continue;
995 log_err("fio: poll clients: %s\n", strerror(errno));
996 break;
997 } else if (!ret)
Jens Axboeb66570d2011-10-01 11:11:35 -0600998 continue;
Jens Axboe5c2857f2011-10-04 13:14:32 +0200999 } while (ret <= 0);
Jens Axboeb66570d2011-10-01 11:11:35 -06001000
1001 for (i = 0; i < nr_clients; i++) {
1002 if (!(pfds[i].revents & POLLIN))
1003 continue;
1004
1005 client = find_client_by_fd(pfds[i].fd);
1006 if (!client) {
Jens Axboe3c5f57e2011-10-06 12:37:50 +02001007 log_err("fio: unknown client fd %d\n", pfds[i].fd);
Jens Axboeb66570d2011-10-01 11:11:35 -06001008 continue;
1009 }
Jens Axboee951bdc2011-10-05 21:58:45 +02001010 if (!handle_client(client)) {
Jens Axboe28d3ab02011-10-05 20:45:37 +02001011 log_info("client: host=%s disconnected\n",
1012 client->hostname);
1013 remove_client(client);
1014 }
Jens Axboeb66570d2011-10-01 11:11:35 -06001015 }
1016 }
1017
1018 free(pfds);
Jens Axboeb66570d2011-10-01 11:11:35 -06001019 return 0;
1020}