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