130aeafc1a493efc8d3c92f63049200b9724a177
[fio.git] / client.c
1 #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>
11 #include <sys/socket.h>
12 #include <sys/un.h>
13 #include <netinet/in.h>
14 #include <arpa/inet.h>
15 #include <netdb.h>
16 #include <signal.h>
17
18 #include "fio.h"
19 #include "server.h"
20 #include "flist.h"
21 #include "hash.h"
22
23 struct client_eta {
24         struct jobs_eta eta;
25         unsigned int pending;
26 };
27
28 struct fio_client {
29         struct flist_head list;
30         struct flist_head hash_list;
31         struct flist_head arg_list;
32         struct sockaddr_in addr;
33         struct sockaddr_un addr_un;
34         char *hostname;
35         int port;
36         int fd;
37
38         char *name;
39
40         int state;
41
42         int skip_newline;
43         int is_sock;
44
45         struct flist_head eta_list;
46         struct client_eta *eta_in_flight;
47
48         struct flist_head cmd_list;
49
50         uint16_t argc;
51         char **argv;
52 };
53
54 static struct timeval eta_tv;
55
56 enum {
57         Client_created          = 0,
58         Client_connected        = 1,
59         Client_started          = 2,
60         Client_stopped          = 3,
61         Client_exited           = 4,
62 };
63
64 static FLIST_HEAD(client_list);
65 static FLIST_HEAD(eta_list);
66
67 static FLIST_HEAD(arg_list);
68
69 #define FIO_CLIENT_HASH_BITS    7
70 #define FIO_CLIENT_HASH_SZ      (1 << FIO_CLIENT_HASH_BITS)
71 #define FIO_CLIENT_HASH_MASK    (FIO_CLIENT_HASH_SZ - 1)
72 static struct flist_head client_hash[FIO_CLIENT_HASH_SZ];
73
74 static int handle_client(struct fio_client *client);
75 static void dec_jobs_eta(struct client_eta *eta);
76
77 static void fio_client_add_hash(struct fio_client *client)
78 {
79         int bucket = hash_long(client->fd, FIO_CLIENT_HASH_BITS);
80
81         bucket &= FIO_CLIENT_HASH_MASK;
82         flist_add(&client->hash_list, &client_hash[bucket]);
83 }
84
85 static void fio_client_remove_hash(struct fio_client *client)
86 {
87         if (!flist_empty(&client->hash_list))
88                 flist_del_init(&client->hash_list);
89 }
90
91 static void fio_init fio_client_hash_init(void)
92 {
93         int i;
94
95         for (i = 0; i < FIO_CLIENT_HASH_SZ; i++)
96                 INIT_FLIST_HEAD(&client_hash[i]);
97 }
98
99 static struct fio_client *find_client_by_fd(int fd)
100 {
101         int bucket = hash_long(fd, FIO_CLIENT_HASH_BITS) & FIO_CLIENT_HASH_MASK;
102         struct fio_client *client;
103         struct flist_head *entry;
104
105         flist_for_each(entry, &client_hash[bucket]) {
106                 client = flist_entry(entry, struct fio_client, hash_list);
107
108                 if (client->fd == fd)
109                         return client;
110         }
111
112         return NULL;
113 }
114
115 static void remove_client(struct fio_client *client)
116 {
117         dprint(FD_NET, "client: removed <%s>\n", client->hostname);
118         flist_del(&client->list);
119
120         fio_client_remove_hash(client);
121
122         if (!flist_empty(&client->eta_list)) {
123                 flist_del_init(&client->eta_list);
124                 dec_jobs_eta(client->eta_in_flight);
125         }
126
127         free(client->hostname);
128         if (client->argv)
129                 free(client->argv);
130         if (client->name)
131                 free(client->name);
132
133         free(client);
134         nr_clients--;
135 }
136
137 static void __fio_client_add_cmd_option(struct fio_client *client,
138                                         const char *opt)
139 {
140         int index;
141
142         index = client->argc++;
143         client->argv = realloc(client->argv, sizeof(char *) * client->argc);
144         client->argv[index] = strdup(opt);
145         dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
146 }
147
148 void fio_client_add_cmd_option(void *cookie, const char *opt)
149 {
150         struct fio_client *client = cookie;
151         struct flist_head *entry;
152
153         if (!client || !opt)
154                 return;
155
156         __fio_client_add_cmd_option(client, opt);
157
158         /*
159          * Duplicate arguments to shared client group
160          */
161         flist_for_each(entry, &arg_list) {
162                 client = flist_entry(entry, struct fio_client, arg_list);
163
164                 __fio_client_add_cmd_option(client, opt);
165         }
166 }
167
168 int fio_client_add(const char *hostname, void **cookie)
169 {
170         struct fio_client *existing = *cookie;
171         struct fio_client *client;
172
173         if (existing) {
174                 /*
175                  * We always add our "exec" name as the option, hence 1
176                  * means empty.
177                  */
178                 if (existing->argc == 1)
179                         flist_add_tail(&existing->arg_list, &arg_list);
180                 else {
181                         while (!flist_empty(&arg_list))
182                                 flist_del_init(arg_list.next);
183                 }
184         }
185
186         client = malloc(sizeof(*client));
187         memset(client, 0, sizeof(*client));
188
189         INIT_FLIST_HEAD(&client->list);
190         INIT_FLIST_HEAD(&client->hash_list);
191         INIT_FLIST_HEAD(&client->arg_list);
192         INIT_FLIST_HEAD(&client->eta_list);
193         INIT_FLIST_HEAD(&client->cmd_list);
194
195         if (fio_server_parse_string(hostname, &client->hostname,
196                                         &client->is_sock, &client->port,
197                                         &client->addr.sin_addr))
198                 return -1;
199
200         client->fd = -1;
201
202         __fio_client_add_cmd_option(client, "fio");
203
204         flist_add(&client->list, &client_list);
205         nr_clients++;
206         dprint(FD_NET, "client: added <%s>\n", client->hostname);
207         *cookie = client;
208         return 0;
209 }
210
211 static int fio_client_connect_ip(struct fio_client *client)
212 {
213         int fd;
214
215         client->addr.sin_family = AF_INET;
216         client->addr.sin_port = htons(client->port);
217
218         fd = socket(AF_INET, SOCK_STREAM, 0);
219         if (fd < 0) {
220                 log_err("fio: socket: %s\n", strerror(errno));
221                 return -1;
222         }
223
224         if (connect(fd, (struct sockaddr *) &client->addr, sizeof(client->addr)) < 0) {
225                 log_err("fio: connect: %s\n", strerror(errno));
226                 log_err("fio: failed to connect to %s:%u\n", client->hostname,
227                                                                 client->port);
228                 close(fd);
229                 return -1;
230         }
231
232         return fd;
233 }
234
235 static int fio_client_connect_sock(struct fio_client *client)
236 {
237         struct sockaddr_un *addr = &client->addr_un;
238         fio_socklen_t len;
239         int fd;
240
241         memset(addr, 0, sizeof(*addr));
242         addr->sun_family = AF_UNIX;
243         strcpy(addr->sun_path, client->hostname);
244
245         fd = socket(AF_UNIX, SOCK_STREAM, 0);
246         if (fd < 0) {
247                 log_err("fio: socket: %s\n", strerror(errno));
248                 return -1;
249         }
250
251         len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
252         if (connect(fd, (struct sockaddr *) addr, len) < 0) {
253                 log_err("fio: connect; %s\n", strerror(errno));
254                 close(fd);
255                 return -1;
256         }
257
258         return fd;
259 }
260
261 static int fio_client_connect(struct fio_client *client)
262 {
263         int fd;
264
265         dprint(FD_NET, "client: connect to host %s\n", client->hostname);
266
267         if (client->is_sock)
268                 fd = fio_client_connect_sock(client);
269         else
270                 fd = fio_client_connect_ip(client);
271
272         dprint(FD_NET, "client: %s connected %d\n", client->hostname, fd);
273
274         if (fd < 0)
275                 return 1;
276
277         client->fd = fd;
278         fio_client_add_hash(client);
279         client->state = Client_connected;
280         return 0;
281 }
282
283 void fio_clients_terminate(void)
284 {
285         struct flist_head *entry;
286         struct fio_client *client;
287
288         dprint(FD_NET, "client: terminate clients\n");
289
290         flist_for_each(entry, &client_list) {
291                 client = flist_entry(entry, struct fio_client, list);
292
293                 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_QUIT, 0, NULL);
294         }
295 }
296
297 static void sig_int(int sig)
298 {
299         dprint(FD_NET, "client: got signal %d\n", sig);
300         fio_clients_terminate();
301 }
302
303 static void client_signal_handler(void)
304 {
305         struct sigaction act;
306
307         memset(&act, 0, sizeof(act));
308         act.sa_handler = sig_int;
309         act.sa_flags = SA_RESTART;
310         sigaction(SIGINT, &act, NULL);
311
312         memset(&act, 0, sizeof(act));
313         act.sa_handler = sig_int;
314         act.sa_flags = SA_RESTART;
315         sigaction(SIGTERM, &act, NULL);
316 }
317
318 static void probe_client(struct fio_client *client)
319 {
320         dprint(FD_NET, "client: send probe\n");
321
322         fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_PROBE, 0, &client->cmd_list);
323 }
324
325 static int send_client_cmd_line(struct fio_client *client)
326 {
327         struct cmd_single_line_pdu *cslp;
328         struct cmd_line_pdu *clp;
329         unsigned long offset;
330         unsigned int *lens;
331         void *pdu;
332         size_t mem;
333         int i, ret;
334
335         dprint(FD_NET, "client: send cmdline %d\n", client->argc);
336
337         lens = malloc(client->argc * sizeof(unsigned int));
338
339         /*
340          * Find out how much mem we need
341          */
342         for (i = 0, mem = 0; i < client->argc; i++) {
343                 lens[i] = strlen(client->argv[i]) + 1;
344                 mem += lens[i];
345         }
346
347         /*
348          * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu
349          */
350         mem += sizeof(*clp) + (client->argc * sizeof(*cslp));
351
352         pdu = malloc(mem);
353         clp = pdu;
354         offset = sizeof(*clp);
355
356         for (i = 0; i < client->argc; i++) {
357                 uint16_t arg_len = lens[i];
358
359                 cslp = pdu + offset;
360                 strcpy((char *) cslp->text, client->argv[i]);
361                 cslp->len = cpu_to_le16(arg_len);
362                 offset += sizeof(*cslp) + arg_len;
363         }
364
365         free(lens);
366         clp->lines = cpu_to_le16(client->argc);
367         ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, 0);
368         free(pdu);
369         return ret;
370 }
371
372 int fio_clients_connect(void)
373 {
374         struct fio_client *client;
375         struct flist_head *entry, *tmp;
376         int ret;
377
378         dprint(FD_NET, "client: connect all\n");
379
380         client_signal_handler();
381
382         flist_for_each_safe(entry, tmp, &client_list) {
383                 client = flist_entry(entry, struct fio_client, list);
384
385                 ret = fio_client_connect(client);
386                 if (ret) {
387                         remove_client(client);
388                         continue;
389                 }
390
391                 probe_client(client);
392
393                 if (client->argc > 1)
394                         send_client_cmd_line(client);
395         }
396
397         return !nr_clients;
398 }
399
400 /*
401  * Send file contents to server backend. We could use sendfile(), but to remain
402  * more portable lets just read/write the darn thing.
403  */
404 static int fio_client_send_ini(struct fio_client *client, const char *filename)
405 {
406         struct stat sb;
407         char *p, *buf;
408         off_t len;
409         int fd, ret;
410
411         dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
412
413         fd = open(filename, O_RDONLY);
414         if (fd < 0) {
415                 log_err("fio: job file <%s> open: %s\n", filename, strerror(errno));
416                 return 1;
417         }
418
419         if (fstat(fd, &sb) < 0) {
420                 log_err("fio: job file stat: %s\n", strerror(errno));
421                 close(fd);
422                 return 1;
423         }
424
425         buf = malloc(sb.st_size);
426
427         len = sb.st_size;
428         p = buf;
429         do {
430                 ret = read(fd, p, len);
431                 if (ret > 0) {
432                         len -= ret;
433                         if (!len)
434                                 break;
435                         p += ret;
436                         continue;
437                 } else if (!ret)
438                         break;
439                 else if (errno == EAGAIN || errno == EINTR)
440                         continue;
441         } while (1);
442
443         if (len) {
444                 log_err("fio: failed reading job file %s\n", filename);
445                 close(fd);
446                 free(buf);
447                 return 1;
448         }
449
450         ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, buf, sb.st_size, 0);
451         free(buf);
452         close(fd);
453         return ret;
454 }
455
456 int fio_clients_send_ini(const char *filename)
457 {
458         struct fio_client *client;
459         struct flist_head *entry, *tmp;
460
461         flist_for_each_safe(entry, tmp, &client_list) {
462                 client = flist_entry(entry, struct fio_client, list);
463
464                 if (fio_client_send_ini(client, filename))
465                         remove_client(client);
466         }
467
468         return !nr_clients;
469 }
470
471 static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
472 {
473         dst->max_val    = le64_to_cpu(src->max_val);
474         dst->min_val    = le64_to_cpu(src->min_val);
475         dst->samples    = le64_to_cpu(src->samples);
476
477         /*
478          * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
479          */
480         dst->mean.u.f   = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
481         dst->S.u.f      = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
482 }
483
484 static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
485 {
486         int i, j;
487
488         dst->error      = le32_to_cpu(src->error);
489         dst->groupid    = le32_to_cpu(src->groupid);
490         dst->pid        = le32_to_cpu(src->pid);
491         dst->members    = le32_to_cpu(src->members);
492
493         for (i = 0; i < 2; i++) {
494                 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
495                 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
496                 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
497                 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
498         }
499
500         dst->usr_time           = le64_to_cpu(src->usr_time);
501         dst->sys_time           = le64_to_cpu(src->sys_time);
502         dst->ctx                = le64_to_cpu(src->ctx);
503         dst->minf               = le64_to_cpu(src->minf);
504         dst->majf               = le64_to_cpu(src->majf);
505         dst->clat_percentiles   = le64_to_cpu(src->clat_percentiles);
506
507         for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
508                 fio_fp64_t *fps = &src->percentile_list[i];
509                 fio_fp64_t *fpd = &dst->percentile_list[i];
510
511                 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
512         }
513
514         for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
515                 dst->io_u_map[i]        = le32_to_cpu(src->io_u_map[i]);
516                 dst->io_u_submit[i]     = le32_to_cpu(src->io_u_submit[i]);
517                 dst->io_u_complete[i]   = le32_to_cpu(src->io_u_complete[i]);
518         }
519
520         for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
521                 dst->io_u_lat_u[i]      = le32_to_cpu(src->io_u_lat_u[i]);
522                 dst->io_u_lat_m[i]      = le32_to_cpu(src->io_u_lat_m[i]);
523         }
524
525         for (i = 0; i < 2; i++)
526                 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
527                         dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]);
528
529         for (i = 0; i < 3; i++) {
530                 dst->total_io_u[i]      = le64_to_cpu(src->total_io_u[i]);
531                 dst->short_io_u[i]      = le64_to_cpu(src->short_io_u[i]);
532         }
533
534         dst->total_submit       = le64_to_cpu(src->total_submit);
535         dst->total_complete     = le64_to_cpu(src->total_complete);
536
537         for (i = 0; i < 2; i++) {
538                 dst->io_bytes[i]        = le64_to_cpu(src->io_bytes[i]);
539                 dst->runtime[i]         = le64_to_cpu(src->runtime[i]);
540         }
541
542         dst->total_run_time     = le64_to_cpu(src->total_run_time);
543         dst->continue_on_error  = le16_to_cpu(src->continue_on_error);
544         dst->total_err_count    = le64_to_cpu(src->total_err_count);
545         dst->first_error        = le32_to_cpu(src->first_error);
546         dst->kb_base            = le32_to_cpu(src->kb_base);
547 }
548
549 static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
550 {
551         int i;
552
553         for (i = 0; i < 2; i++) {
554                 dst->max_run[i]         = le64_to_cpu(src->max_run[i]);
555                 dst->min_run[i]         = le64_to_cpu(src->min_run[i]);
556                 dst->max_bw[i]          = le64_to_cpu(src->max_bw[i]);
557                 dst->min_bw[i]          = le64_to_cpu(src->min_bw[i]);
558                 dst->io_kb[i]           = le64_to_cpu(src->io_kb[i]);
559                 dst->agg[i]             = le64_to_cpu(src->agg[i]);
560         }
561
562         dst->kb_base    = le32_to_cpu(src->kb_base);
563         dst->groupid    = le32_to_cpu(src->groupid);
564 }
565
566 static void handle_ts(struct fio_net_cmd *cmd)
567 {
568         struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
569
570         convert_ts(&p->ts, &p->ts);
571         convert_gs(&p->rs, &p->rs);
572
573         show_thread_status(&p->ts, &p->rs);
574 }
575
576 static void handle_gs(struct fio_net_cmd *cmd)
577 {
578         struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
579
580         convert_gs(gs, gs);
581         show_group_stats(gs);
582 }
583
584 static void convert_jobs_eta(struct jobs_eta *je)
585 {
586         int i;
587
588         je->nr_running          = le32_to_cpu(je->nr_running);
589         je->nr_ramp             = le32_to_cpu(je->nr_ramp);
590         je->nr_pending          = le32_to_cpu(je->nr_pending);
591         je->files_open          = le32_to_cpu(je->files_open);
592         je->m_rate              = le32_to_cpu(je->m_rate);
593         je->t_rate              = le32_to_cpu(je->t_rate);
594         je->m_iops              = le32_to_cpu(je->m_iops);
595         je->t_iops              = le32_to_cpu(je->t_iops);
596
597         for (i = 0; i < 2; i++) {
598                 je->rate[i]     = le32_to_cpu(je->rate[i]);
599                 je->iops[i]     = le32_to_cpu(je->iops[i]);
600         }
601
602         je->elapsed_sec         = le64_to_cpu(je->elapsed_sec);
603         je->eta_sec             = le64_to_cpu(je->eta_sec);
604 }
605
606 static void sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je)
607 {
608         int i;
609
610         dst->nr_running         += je->nr_running;
611         dst->nr_ramp            += je->nr_ramp;
612         dst->nr_pending         += je->nr_pending;
613         dst->files_open         += je->files_open;
614         dst->m_rate             += je->m_rate;
615         dst->t_rate             += je->t_rate;
616         dst->m_iops             += je->m_iops;
617         dst->t_iops             += je->t_iops;
618
619         for (i = 0; i < 2; i++) {
620                 dst->rate[i]    += je->rate[i];
621                 dst->iops[i]    += je->iops[i];
622         }
623
624         dst->elapsed_sec        += je->elapsed_sec;
625
626         if (je->eta_sec > dst->eta_sec)
627                 dst->eta_sec = je->eta_sec;
628 }
629
630 static void dec_jobs_eta(struct client_eta *eta)
631 {
632         if (!--eta->pending) {
633                 display_thread_status(&eta->eta);
634                 free(eta);
635         }
636 }
637
638 static void remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd)
639 {
640         struct fio_net_int_cmd *icmd = NULL;
641         struct flist_head *entry;
642
643         flist_for_each(entry, &client->cmd_list) {
644                 icmd = flist_entry(entry, struct fio_net_int_cmd, list);
645
646                 if (cmd->tag == (uint64_t) icmd)
647                         break;
648
649                 icmd = NULL;
650         }
651
652         if (!icmd) {
653                 log_err("fio: client: unable to find matching tag\n");
654                 return;
655         }
656
657         flist_del(&icmd->list);
658         cmd->tag = icmd->saved_tag;
659         free(icmd);
660 }
661
662 static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd)
663 {
664         struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
665         struct client_eta *eta = (struct client_eta *) cmd->tag;
666
667         dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending);
668
669         assert(client->eta_in_flight == eta);
670
671         client->eta_in_flight = NULL;
672         flist_del_init(&client->eta_list);
673
674         convert_jobs_eta(je);
675         sum_jobs_eta(&eta->eta, je);
676         dec_jobs_eta(eta);
677 }
678
679 static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
680 {
681         struct cmd_probe_pdu *probe = (struct cmd_probe_pdu *) cmd->payload;
682         const char *os, *arch;
683
684         os = fio_get_os_string(probe->os);
685         if (!os)
686                 os = "unknown";
687
688         arch = fio_get_arch_string(probe->arch);
689         if (!arch)
690                 os = "unknown";
691
692         log_info("hostname=%s, be=%u, os=%s, arch=%s, fio=%u.%u.%u\n",
693                 probe->hostname, probe->bigendian, os, arch, probe->fio_major,
694                 probe->fio_minor, probe->fio_patch);
695
696         if (!client->name)
697                 client->name = strdup((char *) probe->hostname);
698 }
699
700 static int handle_client(struct fio_client *client)
701 {
702         struct fio_net_cmd *cmd;
703
704         dprint(FD_NET, "client: handle %s\n", client->hostname);
705
706         cmd = fio_net_recv_cmd(client->fd);
707         if (!cmd)
708                 return 0;
709
710         dprint(FD_NET, "client: got cmd op %s from %s\n",
711                                 fio_server_op(cmd->opcode), client->hostname);
712
713         switch (cmd->opcode) {
714         case FIO_NET_CMD_QUIT:
715                 remove_client(client);
716                 free(cmd);
717                 break;
718         case FIO_NET_CMD_TEXT: {
719                 const char *buf = (const char *) cmd->payload;
720                 const char *name;
721                 int fio_unused ret;
722
723                 name = client->name ? client->name : client->hostname;
724
725                 if (!client->skip_newline)
726                         fprintf(f_out, "<%s> ", name);
727                 ret = fwrite(buf, cmd->pdu_len, 1, f_out);
728                 fflush(f_out);
729                 client->skip_newline = strchr(buf, '\n') == NULL;
730                 free(cmd);
731                 break;
732                 }
733         case FIO_NET_CMD_TS:
734                 handle_ts(cmd);
735                 free(cmd);
736                 break;
737         case FIO_NET_CMD_GS:
738                 handle_gs(cmd);
739                 free(cmd);
740                 break;
741         case FIO_NET_CMD_ETA:
742                 remove_reply_cmd(client, cmd);
743                 handle_eta(client, cmd);
744                 free(cmd);
745                 break;
746         case FIO_NET_CMD_PROBE:
747                 remove_reply_cmd(client, cmd);
748                 handle_probe(client, cmd);
749                 free(cmd);
750                 break;
751         case FIO_NET_CMD_START:
752                 client->state = Client_started;
753                 free(cmd);
754                 break;
755         case FIO_NET_CMD_STOP:
756                 client->state = Client_stopped;
757                 free(cmd);
758                 break;
759         default:
760                 log_err("fio: unknown client op: %s\n", fio_server_op(cmd->opcode));
761                 free(cmd);
762                 break;
763         }
764
765         return 1;
766 }
767
768 static void request_client_etas(void)
769 {
770         struct fio_client *client;
771         struct flist_head *entry;
772         struct client_eta *eta;
773         int skipped = 0;
774
775         dprint(FD_NET, "client: request eta (%d)\n", nr_clients);
776
777         eta = malloc(sizeof(*eta));
778         memset(&eta->eta, 0, sizeof(eta->eta));
779         eta->pending = nr_clients;
780
781         flist_for_each(entry, &client_list) {
782                 client = flist_entry(entry, struct fio_client, list);
783
784                 if (!flist_empty(&client->eta_list)) {
785                         skipped++;
786                         continue;
787                 }
788
789                 assert(!client->eta_in_flight);
790                 flist_add_tail(&client->eta_list, &eta_list);
791                 client->eta_in_flight = eta;
792                 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA,
793                                         (uint64_t) eta, &client->cmd_list);
794         }
795
796         while (skipped--)
797                 dec_jobs_eta(eta);
798
799         dprint(FD_NET, "client: requested eta tag %p\n", eta);
800 }
801
802 static int client_check_cmd_timeout(struct fio_client *client,
803                                     struct timeval *now)
804 {
805         struct fio_net_int_cmd *cmd;
806         struct flist_head *entry, *tmp;
807         int ret = 0;
808
809         flist_for_each_safe(entry, tmp, &client->cmd_list) {
810                 cmd = flist_entry(entry, struct fio_net_int_cmd, list);
811
812                 if (mtime_since(&cmd->tv, now) < FIO_NET_CLIENT_TIMEOUT)
813                         continue;
814
815                 log_err("fio: client %s, timeout on cmd %s\n", client->hostname,
816                                                 fio_server_op(cmd->cmd.opcode));
817                 flist_del(&cmd->list);
818                 free(cmd);
819                 ret = 1;
820         }
821
822         return flist_empty(&client->cmd_list) && ret;
823 }
824
825 static int fio_client_timed_out(void)
826 {
827         struct fio_client *client;
828         struct flist_head *entry, *tmp;
829         struct timeval tv;
830         int ret = 0;
831
832         gettimeofday(&tv, NULL);
833
834         flist_for_each_safe(entry, tmp, &client_list) {
835                 client = flist_entry(entry, struct fio_client, list);
836
837                 if (flist_empty(&client->cmd_list))
838                         continue;
839
840                 if (!client_check_cmd_timeout(client, &tv))
841                         continue;
842
843                 log_err("fio: client %s timed out\n", client->hostname);
844                 remove_client(client);
845                 ret = 1;
846         }
847
848         return ret;
849 }
850
851 int fio_handle_clients(void)
852 {
853         struct fio_client *client;
854         struct flist_head *entry;
855         struct pollfd *pfds;
856         int i, ret = 0;
857
858         gettimeofday(&eta_tv, NULL);
859
860         pfds = malloc(nr_clients * sizeof(struct pollfd));
861
862         while (!exit_backend && nr_clients) {
863                 i = 0;
864                 flist_for_each(entry, &client_list) {
865                         client = flist_entry(entry, struct fio_client, list);
866
867                         pfds[i].fd = client->fd;
868                         pfds[i].events = POLLIN;
869                         i++;
870                 }
871
872                 assert(i == nr_clients);
873
874                 do {
875                         struct timeval tv;
876
877                         gettimeofday(&tv, NULL);
878                         if (mtime_since(&eta_tv, &tv) >= 900) {
879                                 request_client_etas();
880                                 memcpy(&eta_tv, &tv, sizeof(tv));
881
882                                 if (fio_client_timed_out())
883                                         break;
884                         }
885
886                         ret = poll(pfds, nr_clients, 100);
887                         if (ret < 0) {
888                                 if (errno == EINTR)
889                                         continue;
890                                 log_err("fio: poll clients: %s\n", strerror(errno));
891                                 break;
892                         } else if (!ret)
893                                 continue;
894                 } while (ret <= 0);
895
896                 for (i = 0; i < nr_clients; i++) {
897                         if (!(pfds[i].revents & POLLIN))
898                                 continue;
899
900                         client = find_client_by_fd(pfds[i].fd);
901                         if (!client) {
902                                 log_err("fio: unknown client fd %d\n", pfds[i].fd);
903                                 continue;
904                         }
905                         if (!handle_client(client)) {
906                                 log_info("client: host=%s disconnected\n",
907                                                 client->hostname);
908                                 remove_client(client);
909                         }
910                 }
911         }
912
913         free(pfds);
914         return 0;
915 }