Add support for --remote-config
[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 #ifdef CONFIG_ZLIB
18 #include <zlib.h>
19 #endif
20
21 #include "fio.h"
22 #include "client.h"
23 #include "server.h"
24 #include "flist.h"
25 #include "hash.h"
26
27 static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd);
28 static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd);
29 static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd);
30 static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd);
31 static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd);
32 static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd);
33 static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd);
34
35 struct client_ops fio_client_ops = {
36         .text           = handle_text,
37         .disk_util      = handle_du,
38         .thread_status  = handle_ts,
39         .group_stats    = handle_gs,
40         .stop           = handle_stop,
41         .start          = handle_start,
42         .eta            = display_thread_status,
43         .probe          = handle_probe,
44         .eta_msec       = FIO_CLIENT_DEF_ETA_MSEC,
45         .client_type    = FIO_CLIENT_TYPE_CLI,
46 };
47
48 static struct timeval eta_tv;
49
50 static FLIST_HEAD(client_list);
51 static FLIST_HEAD(eta_list);
52
53 static FLIST_HEAD(arg_list);
54
55 struct thread_stat client_ts;
56 struct group_run_stats client_gs;
57 int sum_stat_clients;
58
59 static int sum_stat_nr;
60 static struct json_object *root = NULL;
61 static struct json_array *clients_array = NULL;
62 static struct json_array *du_array = NULL;
63 static int do_output_all_clients;
64
65 #define FIO_CLIENT_HASH_BITS    7
66 #define FIO_CLIENT_HASH_SZ      (1 << FIO_CLIENT_HASH_BITS)
67 #define FIO_CLIENT_HASH_MASK    (FIO_CLIENT_HASH_SZ - 1)
68 static struct flist_head client_hash[FIO_CLIENT_HASH_SZ];
69
70 static void fio_client_add_hash(struct fio_client *client)
71 {
72         int bucket = hash_long(client->fd, FIO_CLIENT_HASH_BITS);
73
74         bucket &= FIO_CLIENT_HASH_MASK;
75         flist_add(&client->hash_list, &client_hash[bucket]);
76 }
77
78 static void fio_client_remove_hash(struct fio_client *client)
79 {
80         if (!flist_empty(&client->hash_list))
81                 flist_del_init(&client->hash_list);
82 }
83
84 static void fio_init fio_client_hash_init(void)
85 {
86         int i;
87
88         for (i = 0; i < FIO_CLIENT_HASH_SZ; i++)
89                 INIT_FLIST_HEAD(&client_hash[i]);
90 }
91
92 static void fio_client_json_init(void)
93 {
94         if (output_format != FIO_OUTPUT_JSON)
95                 return;
96         root = json_create_object();
97         json_object_add_value_string(root, "fio version", fio_version_string);
98         clients_array = json_create_array();
99         json_object_add_value_array(root, "client_stats", clients_array);
100         du_array = json_create_array();
101         json_object_add_value_array(root, "disk_util", du_array);
102 }
103
104 static void fio_client_json_fini(void)
105 {
106         if (output_format != FIO_OUTPUT_JSON)
107                 return;
108         json_print_object(root);
109         log_info("\n");
110         json_free_object(root);
111         root = NULL;
112         clients_array = NULL;
113         du_array = NULL;
114 }
115
116 static struct fio_client *find_client_by_fd(int fd)
117 {
118         int bucket = hash_long(fd, FIO_CLIENT_HASH_BITS) & FIO_CLIENT_HASH_MASK;
119         struct fio_client *client;
120         struct flist_head *entry;
121
122         flist_for_each(entry, &client_hash[bucket]) {
123                 client = flist_entry(entry, struct fio_client, hash_list);
124
125                 if (client->fd == fd) {
126                         client->refs++;
127                         return client;
128                 }
129         }
130
131         return NULL;
132 }
133
134 void fio_put_client(struct fio_client *client)
135 {
136         if (--client->refs)
137                 return;
138
139         free(client->hostname);
140         if (client->argv)
141                 free(client->argv);
142         if (client->name)
143                 free(client->name);
144         while (client->nr_files) {
145                 struct client_file *cf = &client->files[--client->nr_files];
146
147                 free(cf->file);
148         }
149         if (client->files)
150                 free(client->files);
151
152         if (!client->did_stat)
153                 sum_stat_clients -= client->nr_stat;
154
155         free(client);
156 }
157
158 static void remove_client(struct fio_client *client)
159 {
160         assert(client->refs);
161
162         dprint(FD_NET, "client: removed <%s>\n", client->hostname);
163
164         if (!flist_empty(&client->list))
165                 flist_del_init(&client->list);
166
167         fio_client_remove_hash(client);
168
169         if (!flist_empty(&client->eta_list)) {
170                 flist_del_init(&client->eta_list);
171                 fio_client_dec_jobs_eta(client->eta_in_flight, client->ops->eta);
172         }
173
174         close(client->fd);
175         client->fd = -1;
176
177         if (client->ops->removed)
178                 client->ops->removed(client);
179
180         nr_clients--;
181         fio_put_client(client);
182 }
183
184 struct fio_client *fio_get_client(struct fio_client *client)
185 {
186         client->refs++;
187         return client;
188 }
189
190 static void __fio_client_add_cmd_option(struct fio_client *client,
191                                         const char *opt)
192 {
193         int index;
194
195         index = client->argc++;
196         client->argv = realloc(client->argv, sizeof(char *) * client->argc);
197         client->argv[index] = strdup(opt);
198         dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
199 }
200
201 void fio_client_add_cmd_option(void *cookie, const char *opt)
202 {
203         struct fio_client *client = cookie;
204         struct flist_head *entry;
205
206         if (!client || !opt)
207                 return;
208
209         __fio_client_add_cmd_option(client, opt);
210
211         /*
212          * Duplicate arguments to shared client group
213          */
214         flist_for_each(entry, &arg_list) {
215                 client = flist_entry(entry, struct fio_client, arg_list);
216
217                 __fio_client_add_cmd_option(client, opt);
218         }
219 }
220
221 struct fio_client *fio_client_add_explicit(struct client_ops *ops,
222                                            const char *hostname, int type,
223                                            int port)
224 {
225         struct fio_client *client;
226
227         client = malloc(sizeof(*client));
228         memset(client, 0, sizeof(*client));
229
230         INIT_FLIST_HEAD(&client->list);
231         INIT_FLIST_HEAD(&client->hash_list);
232         INIT_FLIST_HEAD(&client->arg_list);
233         INIT_FLIST_HEAD(&client->eta_list);
234         INIT_FLIST_HEAD(&client->cmd_list);
235
236         client->hostname = strdup(hostname);
237
238         if (type == Fio_client_socket)
239                 client->is_sock = 1;
240         else {
241                 int ipv6;
242
243                 ipv6 = type == Fio_client_ipv6;
244                 if (fio_server_parse_host(hostname, ipv6,
245                                                 &client->addr.sin_addr,
246                                                 &client->addr6.sin6_addr))
247                         goto err;
248
249                 client->port = port;
250         }
251
252         client->fd = -1;
253         client->ops = ops;
254         client->refs = 1;
255         client->type = ops->client_type;
256
257         __fio_client_add_cmd_option(client, "fio");
258
259         flist_add(&client->list, &client_list);
260         nr_clients++;
261         dprint(FD_NET, "client: added <%s>\n", client->hostname);
262         return client;
263 err:
264         free(client);
265         return NULL;
266 }
267
268 int fio_client_add_ini_file(void *cookie, const char *ini_file, int remote)
269 {
270         struct fio_client *client = cookie;
271         struct client_file *cf;
272         size_t new_size;
273         void *new_files;
274
275         dprint(FD_NET, "client <%s>: add ini %s\n", client->hostname, ini_file);
276
277         new_size = (client->nr_files + 1) * sizeof(struct client_file);
278         new_files = realloc(client->files, new_size);
279         if (!new_files)
280                 return 1;
281
282         client->files = new_files;
283         cf = &client->files[client->nr_files];
284         cf->file = strdup(ini_file);
285         cf->remote = remote;
286         client->nr_files++;
287         return 0;
288 }
289
290 int fio_client_add(struct client_ops *ops, const char *hostname, void **cookie)
291 {
292         struct fio_client *existing = *cookie;
293         struct fio_client *client;
294
295         if (existing) {
296                 /*
297                  * We always add our "exec" name as the option, hence 1
298                  * means empty.
299                  */
300                 if (existing->argc == 1)
301                         flist_add_tail(&existing->arg_list, &arg_list);
302                 else {
303                         while (!flist_empty(&arg_list))
304                                 flist_del_init(arg_list.next);
305                 }
306         }
307
308         client = malloc(sizeof(*client));
309         memset(client, 0, sizeof(*client));
310
311         INIT_FLIST_HEAD(&client->list);
312         INIT_FLIST_HEAD(&client->hash_list);
313         INIT_FLIST_HEAD(&client->arg_list);
314         INIT_FLIST_HEAD(&client->eta_list);
315         INIT_FLIST_HEAD(&client->cmd_list);
316
317         if (fio_server_parse_string(hostname, &client->hostname,
318                                         &client->is_sock, &client->port,
319                                         &client->addr.sin_addr,
320                                         &client->addr6.sin6_addr,
321                                         &client->ipv6))
322                 return -1;
323
324         client->fd = -1;
325         client->ops = ops;
326         client->refs = 1;
327         client->type = ops->client_type;
328
329         __fio_client_add_cmd_option(client, "fio");
330
331         flist_add(&client->list, &client_list);
332         nr_clients++;
333         dprint(FD_NET, "client: added <%s>\n", client->hostname);
334         *cookie = client;
335         return 0;
336 }
337
338 static void probe_client(struct fio_client *client)
339 {
340         struct cmd_client_probe_pdu pdu;
341         uint64_t tag;
342
343         dprint(FD_NET, "client: send probe\n");
344
345 #ifdef CONFIG_ZLIB
346         pdu.flags = __le64_to_cpu(FIO_PROBE_FLAG_ZLIB);
347 #else
348         pdu.flags = 0;
349 #endif
350
351         fio_net_send_cmd(client->fd, FIO_NET_CMD_PROBE, &pdu, sizeof(pdu), &tag, &client->cmd_list);
352 }
353
354 static int fio_client_connect_ip(struct fio_client *client)
355 {
356         struct sockaddr *addr;
357         socklen_t socklen;
358         int fd, domain;
359
360         if (client->ipv6) {
361                 client->addr6.sin6_family = AF_INET6;
362                 client->addr6.sin6_port = htons(client->port);
363                 domain = AF_INET6;
364                 addr = (struct sockaddr *) &client->addr6;
365                 socklen = sizeof(client->addr6);
366         } else {
367                 client->addr.sin_family = AF_INET;
368                 client->addr.sin_port = htons(client->port);
369                 domain = AF_INET;
370                 addr = (struct sockaddr *) &client->addr;
371                 socklen = sizeof(client->addr);
372         }
373
374         fd = socket(domain, SOCK_STREAM, 0);
375         if (fd < 0) {
376                 int ret = -errno;
377
378                 log_err("fio: socket: %s\n", strerror(errno));
379                 return ret;
380         }
381
382         if (connect(fd, addr, socklen) < 0) {
383                 int ret = -errno;
384
385                 log_err("fio: connect: %s\n", strerror(errno));
386                 log_err("fio: failed to connect to %s:%u\n", client->hostname,
387                                                                 client->port);
388                 close(fd);
389                 return ret;
390         }
391
392         return fd;
393 }
394
395 static int fio_client_connect_sock(struct fio_client *client)
396 {
397         struct sockaddr_un *addr = &client->addr_un;
398         socklen_t len;
399         int fd;
400
401         memset(addr, 0, sizeof(*addr));
402         addr->sun_family = AF_UNIX;
403         strncpy(addr->sun_path, client->hostname, sizeof(addr->sun_path) - 1);
404
405         fd = socket(AF_UNIX, SOCK_STREAM, 0);
406         if (fd < 0) {
407                 int ret = -errno;
408
409                 log_err("fio: socket: %s\n", strerror(errno));
410                 return ret;
411         }
412
413         len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
414         if (connect(fd, (struct sockaddr *) addr, len) < 0) {
415                 int ret = -errno;
416
417                 log_err("fio: connect; %s\n", strerror(errno));
418                 close(fd);
419                 return ret;
420         }
421
422         return fd;
423 }
424
425 int fio_client_connect(struct fio_client *client)
426 {
427         int fd;
428
429         dprint(FD_NET, "client: connect to host %s\n", client->hostname);
430
431         if (client->is_sock)
432                 fd = fio_client_connect_sock(client);
433         else
434                 fd = fio_client_connect_ip(client);
435
436         dprint(FD_NET, "client: %s connected %d\n", client->hostname, fd);
437
438         if (fd < 0)
439                 return fd;
440
441         client->fd = fd;
442         fio_client_add_hash(client);
443         client->state = Client_connected;
444
445         probe_client(client);
446         return 0;
447 }
448
449 int fio_client_terminate(struct fio_client *client)
450 {
451         return fio_net_send_quit(client->fd);
452 }
453
454 void fio_clients_terminate(void)
455 {
456         struct flist_head *entry;
457         struct fio_client *client;
458
459         dprint(FD_NET, "client: terminate clients\n");
460
461         flist_for_each(entry, &client_list) {
462                 client = flist_entry(entry, struct fio_client, list);
463                 fio_client_terminate(client);
464         }
465 }
466
467 static void sig_int(int sig)
468 {
469         dprint(FD_NET, "client: got signal %d\n", sig);
470         fio_clients_terminate();
471 }
472
473 static void sig_show_status(int sig)
474 {
475         show_running_run_stats();
476 }
477
478 static void client_signal_handler(void)
479 {
480         struct sigaction act;
481
482         memset(&act, 0, sizeof(act));
483         act.sa_handler = sig_int;
484         act.sa_flags = SA_RESTART;
485         sigaction(SIGINT, &act, NULL);
486
487         memset(&act, 0, sizeof(act));
488         act.sa_handler = sig_int;
489         act.sa_flags = SA_RESTART;
490         sigaction(SIGTERM, &act, NULL);
491
492 /* Windows uses SIGBREAK as a quit signal from other applications */
493 #ifdef WIN32
494         memset(&act, 0, sizeof(act));
495         act.sa_handler = sig_int;
496         act.sa_flags = SA_RESTART;
497         sigaction(SIGBREAK, &act, NULL);
498 #endif
499
500         memset(&act, 0, sizeof(act));
501         act.sa_handler = sig_show_status;
502         act.sa_flags = SA_RESTART;
503         sigaction(SIGUSR1, &act, NULL);
504 }
505
506 static int send_client_cmd_line(struct fio_client *client)
507 {
508         struct cmd_single_line_pdu *cslp;
509         struct cmd_line_pdu *clp;
510         unsigned long offset;
511         unsigned int *lens;
512         void *pdu;
513         size_t mem;
514         int i, ret;
515
516         dprint(FD_NET, "client: send cmdline %d\n", client->argc);
517
518         lens = malloc(client->argc * sizeof(unsigned int));
519
520         /*
521          * Find out how much mem we need
522          */
523         for (i = 0, mem = 0; i < client->argc; i++) {
524                 lens[i] = strlen(client->argv[i]) + 1;
525                 mem += lens[i];
526         }
527
528         /*
529          * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu
530          */
531         mem += sizeof(*clp) + (client->argc * sizeof(*cslp));
532
533         pdu = malloc(mem);
534         clp = pdu;
535         offset = sizeof(*clp);
536
537         for (i = 0; i < client->argc; i++) {
538                 uint16_t arg_len = lens[i];
539
540                 cslp = pdu + offset;
541                 strcpy((char *) cslp->text, client->argv[i]);
542                 cslp->len = cpu_to_le16(arg_len);
543                 offset += sizeof(*cslp) + arg_len;
544         }
545
546         free(lens);
547         clp->lines = cpu_to_le16(client->argc);
548         clp->client_type = __cpu_to_le16(client->type);
549         ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, NULL, NULL);
550         free(pdu);
551         return ret;
552 }
553
554 int fio_clients_connect(void)
555 {
556         struct fio_client *client;
557         struct flist_head *entry, *tmp;
558         int ret;
559
560 #ifdef WIN32
561         WSADATA wsd;
562         WSAStartup(MAKEWORD(2, 2), &wsd);
563 #endif
564
565         dprint(FD_NET, "client: connect all\n");
566
567         client_signal_handler();
568
569         flist_for_each_safe(entry, tmp, &client_list) {
570                 client = flist_entry(entry, struct fio_client, list);
571
572                 ret = fio_client_connect(client);
573                 if (ret) {
574                         remove_client(client);
575                         continue;
576                 }
577
578                 if (client->argc > 1)
579                         send_client_cmd_line(client);
580         }
581
582         return !nr_clients;
583 }
584
585 int fio_start_client(struct fio_client *client)
586 {
587         dprint(FD_NET, "client: start %s\n", client->hostname);
588         return fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_RUN, 0, NULL);
589 }
590
591 int fio_start_all_clients(void)
592 {
593         struct fio_client *client;
594         struct flist_head *entry, *tmp;
595         int ret;
596
597         dprint(FD_NET, "client: start all\n");
598
599         fio_client_json_init();
600
601         flist_for_each_safe(entry, tmp, &client_list) {
602                 client = flist_entry(entry, struct fio_client, list);
603
604                 ret = fio_start_client(client);
605                 if (ret) {
606                         remove_client(client);
607                         continue;
608                 }
609         }
610
611         return flist_empty(&client_list);
612 }
613
614 static int __fio_client_send_remote_ini(struct fio_client *client,
615                                         const char *filename)
616 {
617         struct cmd_load_file_pdu *pdu;
618         size_t p_size;
619         int ret;
620
621         dprint(FD_NET, "send remote ini %s to %s\n", filename, client->hostname);
622
623         p_size = sizeof(*pdu) + strlen(filename);
624         pdu = malloc(p_size);
625         pdu->name_len = strlen(filename);
626         strcpy((char *) pdu->file, filename);
627         pdu->client_type = cpu_to_le16((uint16_t) client->type);
628
629         client->sent_job = 1;
630         ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_LOAD_FILE, pdu, p_size,NULL, NULL);
631         free(pdu);
632         return ret;
633 }
634
635 /*
636  * Send file contents to server backend. We could use sendfile(), but to remain
637  * more portable lets just read/write the darn thing.
638  */
639 static int __fio_client_send_local_ini(struct fio_client *client,
640                                        const char *filename)
641 {
642         struct cmd_job_pdu *pdu;
643         size_t p_size;
644         struct stat sb;
645         char *p;
646         void *buf;
647         off_t len;
648         int fd, ret;
649
650         dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
651
652         fd = open(filename, O_RDONLY);
653         if (fd < 0) {
654                 int ret = -errno;
655
656                 log_err("fio: job file <%s> open: %s\n", filename, strerror(errno));
657                 return ret;
658         }
659
660         if (fstat(fd, &sb) < 0) {
661                 int ret = -errno;
662
663                 log_err("fio: job file stat: %s\n", strerror(errno));
664                 close(fd);
665                 return ret;
666         }
667
668         p_size = sb.st_size + sizeof(*pdu);
669         pdu = malloc(p_size);
670         buf = pdu->buf;
671
672         len = sb.st_size;
673         p = buf;
674         do {
675                 ret = read(fd, p, len);
676                 if (ret > 0) {
677                         len -= ret;
678                         if (!len)
679                                 break;
680                         p += ret;
681                         continue;
682                 } else if (!ret)
683                         break;
684                 else if (errno == EAGAIN || errno == EINTR)
685                         continue;
686         } while (1);
687
688         if (len) {
689                 log_err("fio: failed reading job file %s\n", filename);
690                 close(fd);
691                 free(pdu);
692                 return 1;
693         }
694
695         pdu->buf_len = __cpu_to_le32(sb.st_size);
696         pdu->client_type = cpu_to_le32(client->type);
697
698         client->sent_job = 1;
699         ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, pdu, p_size, NULL, NULL);
700         free(pdu);
701         close(fd);
702         return ret;
703 }
704
705 int fio_client_send_ini(struct fio_client *client, const char *filename,
706                         int remote)
707 {
708         int ret;
709
710         if (!remote)
711                 ret = __fio_client_send_local_ini(client, filename);
712         else
713                 ret = __fio_client_send_remote_ini(client, filename);
714
715         if (!ret)
716                 client->sent_job = 1;
717
718         return ret;
719 }
720
721 static int fio_client_send_cf(struct fio_client *client,
722                               struct client_file *cf)
723 {
724         return fio_client_send_ini(client, cf->file, cf->remote);
725 }
726
727 int fio_clients_send_ini(const char *filename)
728 {
729         struct fio_client *client;
730         struct flist_head *entry, *tmp;
731
732         flist_for_each_safe(entry, tmp, &client_list) {
733                 client = flist_entry(entry, struct fio_client, list);
734
735                 if (client->nr_files) {
736                         int i;
737
738                         for (i = 0; i < client->nr_files; i++) {
739                                 struct client_file *cf;
740
741                                 cf = &client->files[i];
742
743                                 if (fio_client_send_cf(client, cf)) {
744                                         remove_client(client);
745                                         break;
746                                 }
747                         }
748                 }
749                 if (client->sent_job)
750                         continue;
751                 if (!filename || fio_client_send_ini(client, filename, 0))
752                         remove_client(client);
753         }
754
755         return !nr_clients;
756 }
757
758 int fio_client_update_options(struct fio_client *client,
759                               struct thread_options *o, uint64_t *tag)
760 {
761         struct cmd_add_job_pdu pdu;
762
763         pdu.thread_number = cpu_to_le32(client->thread_number);
764         pdu.groupid = cpu_to_le32(client->groupid);
765         convert_thread_options_to_net(&pdu.top, o);
766
767         return fio_net_send_cmd(client->fd, FIO_NET_CMD_UPDATE_JOB, &pdu, sizeof(pdu), tag, &client->cmd_list);
768 }
769
770 static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
771 {
772         dst->max_val    = le64_to_cpu(src->max_val);
773         dst->min_val    = le64_to_cpu(src->min_val);
774         dst->samples    = le64_to_cpu(src->samples);
775
776         /*
777          * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
778          */
779         dst->mean.u.f   = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
780         dst->S.u.f      = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
781 }
782
783 static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
784 {
785         int i, j;
786
787         dst->error              = le32_to_cpu(src->error);
788         dst->thread_number      = le32_to_cpu(src->thread_number);
789         dst->groupid            = le32_to_cpu(src->groupid);
790         dst->pid                = le32_to_cpu(src->pid);
791         dst->members            = le32_to_cpu(src->members);
792         dst->unified_rw_rep     = le32_to_cpu(src->unified_rw_rep);
793
794         for (i = 0; i < DDIR_RWDIR_CNT; i++) {
795                 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
796                 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
797                 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
798                 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
799         }
800
801         dst->usr_time           = le64_to_cpu(src->usr_time);
802         dst->sys_time           = le64_to_cpu(src->sys_time);
803         dst->ctx                = le64_to_cpu(src->ctx);
804         dst->minf               = le64_to_cpu(src->minf);
805         dst->majf               = le64_to_cpu(src->majf);
806         dst->clat_percentiles   = le64_to_cpu(src->clat_percentiles);
807
808         for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
809                 fio_fp64_t *fps = &src->percentile_list[i];
810                 fio_fp64_t *fpd = &dst->percentile_list[i];
811
812                 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
813         }
814
815         for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
816                 dst->io_u_map[i]        = le32_to_cpu(src->io_u_map[i]);
817                 dst->io_u_submit[i]     = le32_to_cpu(src->io_u_submit[i]);
818                 dst->io_u_complete[i]   = le32_to_cpu(src->io_u_complete[i]);
819         }
820
821         for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
822                 dst->io_u_lat_u[i]      = le32_to_cpu(src->io_u_lat_u[i]);
823                 dst->io_u_lat_m[i]      = le32_to_cpu(src->io_u_lat_m[i]);
824         }
825
826         for (i = 0; i < DDIR_RWDIR_CNT; i++)
827                 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
828                         dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]);
829
830         for (i = 0; i < DDIR_RWDIR_CNT; i++) {
831                 dst->total_io_u[i]      = le64_to_cpu(src->total_io_u[i]);
832                 dst->short_io_u[i]      = le64_to_cpu(src->short_io_u[i]);
833         }
834
835         dst->total_submit       = le64_to_cpu(src->total_submit);
836         dst->total_complete     = le64_to_cpu(src->total_complete);
837
838         for (i = 0; i < DDIR_RWDIR_CNT; i++) {
839                 dst->io_bytes[i]        = le64_to_cpu(src->io_bytes[i]);
840                 dst->runtime[i]         = le64_to_cpu(src->runtime[i]);
841         }
842
843         dst->total_run_time     = le64_to_cpu(src->total_run_time);
844         dst->continue_on_error  = le16_to_cpu(src->continue_on_error);
845         dst->total_err_count    = le64_to_cpu(src->total_err_count);
846         dst->first_error        = le32_to_cpu(src->first_error);
847         dst->kb_base            = le32_to_cpu(src->kb_base);
848         dst->unit_base          = le32_to_cpu(src->unit_base);
849
850         dst->latency_depth      = le32_to_cpu(src->latency_depth);
851         dst->latency_target     = le64_to_cpu(src->latency_target);
852         dst->latency_window     = le64_to_cpu(src->latency_window);
853         dst->latency_percentile.u.f = fio_uint64_to_double(le64_to_cpu(src->latency_percentile.u.i));
854 }
855
856 static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
857 {
858         int i;
859
860         for (i = 0; i < DDIR_RWDIR_CNT; i++) {
861                 dst->max_run[i]         = le64_to_cpu(src->max_run[i]);
862                 dst->min_run[i]         = le64_to_cpu(src->min_run[i]);
863                 dst->max_bw[i]          = le64_to_cpu(src->max_bw[i]);
864                 dst->min_bw[i]          = le64_to_cpu(src->min_bw[i]);
865                 dst->io_kb[i]           = le64_to_cpu(src->io_kb[i]);
866                 dst->agg[i]             = le64_to_cpu(src->agg[i]);
867         }
868
869         dst->kb_base    = le32_to_cpu(src->kb_base);
870         dst->unit_base  = le32_to_cpu(src->unit_base);
871         dst->groupid    = le32_to_cpu(src->groupid);
872         dst->unified_rw_rep     = le32_to_cpu(src->unified_rw_rep);
873 }
874
875 static void json_object_add_client_info(struct json_object *obj,
876                                         struct fio_client *client)
877 {
878         const char *hostname = client->hostname ? client->hostname : "";
879
880         json_object_add_value_string(obj, "hostname", hostname);
881         json_object_add_value_int(obj, "port", client->port);
882 }
883
884 static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd)
885 {
886         struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
887         struct json_object *tsobj;
888
889         tsobj = show_thread_status(&p->ts, &p->rs);
890         client->did_stat = 1;
891         if (tsobj) {
892                 json_object_add_client_info(tsobj, client);
893                 json_array_add_value_object(clients_array, tsobj);
894         }
895
896         if (!do_output_all_clients)
897                 return;
898
899         sum_thread_stats(&client_ts, &p->ts, sum_stat_nr);
900         sum_group_stats(&client_gs, &p->rs);
901
902         client_ts.members++;
903         client_ts.thread_number = p->ts.thread_number;
904         client_ts.groupid = p->ts.groupid;
905         client_ts.unified_rw_rep = p->ts.unified_rw_rep;
906
907         if (++sum_stat_nr == sum_stat_clients) {
908                 strcpy(client_ts.name, "All clients");
909                 tsobj = show_thread_status(&client_ts, &client_gs);
910                 if (tsobj) {
911                         json_object_add_client_info(tsobj, client);
912                         json_array_add_value_object(clients_array, tsobj);
913                 }
914         }
915 }
916
917 static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd)
918 {
919         struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
920
921         show_group_stats(gs);
922 }
923
924 static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd)
925 {
926         struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
927         const char *buf = (const char *) pdu->buf;
928         const char *name;
929         int fio_unused ret;
930
931         name = client->name ? client->name : client->hostname;
932
933         if (!client->skip_newline)
934                 fprintf(f_out, "<%s> ", name);
935         ret = fwrite(buf, pdu->buf_len, 1, f_out);
936         fflush(f_out);
937         client->skip_newline = strchr(buf, '\n') == NULL;
938 }
939
940 static void convert_agg(struct disk_util_agg *agg)
941 {
942         int i;
943
944         for (i = 0; i < 2; i++) {
945                 agg->ios[i]     = le32_to_cpu(agg->ios[i]);
946                 agg->merges[i]  = le32_to_cpu(agg->merges[i]);
947                 agg->sectors[i] = le64_to_cpu(agg->sectors[i]);
948                 agg->ticks[i]   = le32_to_cpu(agg->ticks[i]);
949         }
950
951         agg->io_ticks           = le32_to_cpu(agg->io_ticks);
952         agg->time_in_queue      = le32_to_cpu(agg->time_in_queue);
953         agg->slavecount         = le32_to_cpu(agg->slavecount);
954         agg->max_util.u.f       = fio_uint64_to_double(__le64_to_cpu(agg->max_util.u.i));
955 }
956
957 static void convert_dus(struct disk_util_stat *dus)
958 {
959         int i;
960
961         for (i = 0; i < 2; i++) {
962                 dus->s.ios[i]           = le32_to_cpu(dus->s.ios[i]);
963                 dus->s.merges[i]        = le32_to_cpu(dus->s.merges[i]);
964                 dus->s.sectors[i]       = le64_to_cpu(dus->s.sectors[i]);
965                 dus->s.ticks[i]         = le32_to_cpu(dus->s.ticks[i]);
966         }
967
968         dus->s.io_ticks         = le32_to_cpu(dus->s.io_ticks);
969         dus->s.time_in_queue    = le32_to_cpu(dus->s.time_in_queue);
970         dus->s.msec             = le64_to_cpu(dus->s.msec);
971 }
972
973 static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd)
974 {
975         struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
976
977         if (!client->disk_stats_shown) {
978                 client->disk_stats_shown = 1;
979                 log_info("\nDisk stats (read/write):\n");
980         }
981
982         if (output_format == FIO_OUTPUT_JSON) {
983                 struct json_object *duobj;
984                 json_array_add_disk_util(&du->dus, &du->agg, du_array);
985                 duobj = json_array_last_value_object(du_array);
986                 json_object_add_client_info(duobj, client);
987         } else
988                 print_disk_util(&du->dus, &du->agg, output_format == FIO_OUTPUT_TERSE);
989 }
990
991 static void convert_jobs_eta(struct jobs_eta *je)
992 {
993         int i;
994
995         je->nr_running          = le32_to_cpu(je->nr_running);
996         je->nr_ramp             = le32_to_cpu(je->nr_ramp);
997         je->nr_pending          = le32_to_cpu(je->nr_pending);
998         je->nr_setting_up       = le32_to_cpu(je->nr_setting_up);
999         je->files_open          = le32_to_cpu(je->files_open);
1000
1001         for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1002                 je->m_rate[i]   = le32_to_cpu(je->m_rate[i]);
1003                 je->t_rate[i]   = le32_to_cpu(je->t_rate[i]);
1004                 je->m_iops[i]   = le32_to_cpu(je->m_iops[i]);
1005                 je->t_iops[i]   = le32_to_cpu(je->t_iops[i]);
1006                 je->rate[i]     = le32_to_cpu(je->rate[i]);
1007                 je->iops[i]     = le32_to_cpu(je->iops[i]);
1008         }
1009
1010         je->elapsed_sec         = le64_to_cpu(je->elapsed_sec);
1011         je->eta_sec             = le64_to_cpu(je->eta_sec);
1012         je->nr_threads          = le32_to_cpu(je->nr_threads);
1013         je->is_pow2             = le32_to_cpu(je->is_pow2);
1014         je->unit_base           = le32_to_cpu(je->unit_base);
1015 }
1016
1017 void fio_client_sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je)
1018 {
1019         int i;
1020
1021         dst->nr_running         += je->nr_running;
1022         dst->nr_ramp            += je->nr_ramp;
1023         dst->nr_pending         += je->nr_pending;
1024         dst->nr_setting_up      += je->nr_setting_up;
1025         dst->files_open         += je->files_open;
1026
1027         for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1028                 dst->m_rate[i]  += je->m_rate[i];
1029                 dst->t_rate[i]  += je->t_rate[i];
1030                 dst->m_iops[i]  += je->m_iops[i];
1031                 dst->t_iops[i]  += je->t_iops[i];
1032                 dst->rate[i]    += je->rate[i];
1033                 dst->iops[i]    += je->iops[i];
1034         }
1035
1036         dst->elapsed_sec        += je->elapsed_sec;
1037
1038         if (je->eta_sec > dst->eta_sec)
1039                 dst->eta_sec = je->eta_sec;
1040
1041         dst->nr_threads         += je->nr_threads;
1042
1043         /*
1044          * This wont be correct for multiple strings, but at least it
1045          * works for the basic cases.
1046          */
1047         strcpy((char *) dst->run_str, (char *) je->run_str);
1048 }
1049
1050 void fio_client_dec_jobs_eta(struct client_eta *eta, client_eta_op eta_fn)
1051 {
1052         if (!--eta->pending) {
1053                 eta_fn(&eta->eta);
1054                 free(eta);
1055         }
1056 }
1057
1058 static void remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd)
1059 {
1060         struct fio_net_cmd_reply *reply = NULL;
1061         struct flist_head *entry;
1062
1063         flist_for_each(entry, &client->cmd_list) {
1064                 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
1065
1066                 if (cmd->tag == (uintptr_t) reply)
1067                         break;
1068
1069                 reply = NULL;
1070         }
1071
1072         if (!reply) {
1073                 log_err("fio: client: unable to find matching tag (%llx)\n", (unsigned long long) cmd->tag);
1074                 return;
1075         }
1076
1077         flist_del(&reply->list);
1078         cmd->tag = reply->saved_tag;
1079         free(reply);
1080 }
1081
1082 int fio_client_wait_for_reply(struct fio_client *client, uint64_t tag)
1083 {
1084         do {
1085                 struct fio_net_cmd_reply *reply = NULL;
1086                 struct flist_head *entry;
1087
1088                 flist_for_each(entry, &client->cmd_list) {
1089                         reply = flist_entry(entry, struct fio_net_cmd_reply, list);
1090
1091                         if (tag == (uintptr_t) reply)
1092                                 break;
1093
1094                         reply = NULL;
1095                 }
1096
1097                 if (!reply)
1098                         break;
1099
1100                 usleep(1000);
1101         } while (1);
1102
1103         return 0;
1104 }
1105
1106 static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd)
1107 {
1108         struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
1109         struct client_eta *eta = (struct client_eta *) (uintptr_t) cmd->tag;
1110
1111         dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending);
1112
1113         assert(client->eta_in_flight == eta);
1114
1115         client->eta_in_flight = NULL;
1116         flist_del_init(&client->eta_list);
1117
1118         if (client->ops->jobs_eta)
1119                 client->ops->jobs_eta(client, je);
1120
1121         fio_client_sum_jobs_eta(&eta->eta, je);
1122         fio_client_dec_jobs_eta(eta, client->ops->eta);
1123 }
1124
1125 static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
1126 {
1127         struct cmd_probe_reply_pdu *probe = (struct cmd_probe_reply_pdu *) cmd->payload;
1128         const char *os, *arch;
1129         char bit[16];
1130
1131         os = fio_get_os_string(probe->os);
1132         if (!os)
1133                 os = "unknown";
1134
1135         arch = fio_get_arch_string(probe->arch);
1136         if (!arch)
1137                 os = "unknown";
1138
1139         sprintf(bit, "%d-bit", probe->bpp * 8);
1140         probe->flags = le64_to_cpu(probe->flags);
1141
1142         log_info("hostname=%s, be=%u, %s, os=%s, arch=%s, fio=%s, flags=%lx\n",
1143                 probe->hostname, probe->bigendian, bit, os, arch,
1144                 probe->fio_version, (unsigned long) probe->flags);
1145
1146         if (!client->name)
1147                 client->name = strdup((char *) probe->hostname);
1148 }
1149
1150 static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd)
1151 {
1152         struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1153
1154         client->state = Client_started;
1155         client->jobs = le32_to_cpu(pdu->jobs);
1156         client->nr_stat = le32_to_cpu(pdu->stat_outputs);
1157
1158         if (sum_stat_clients > 1)
1159                 do_output_all_clients = 1;
1160
1161         sum_stat_clients += client->nr_stat;
1162 }
1163
1164 static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd)
1165 {
1166         if (client->error)
1167                 log_info("client <%s>: exited with error %d\n", client->hostname, client->error);
1168 }
1169
1170 static void convert_stop(struct fio_net_cmd *cmd)
1171 {
1172         struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1173
1174         pdu->error = le32_to_cpu(pdu->error);
1175 }
1176
1177 static void convert_text(struct fio_net_cmd *cmd)
1178 {
1179         struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
1180
1181         pdu->level      = le32_to_cpu(pdu->level);
1182         pdu->buf_len    = le32_to_cpu(pdu->buf_len);
1183         pdu->log_sec    = le64_to_cpu(pdu->log_sec);
1184         pdu->log_usec   = le64_to_cpu(pdu->log_usec);
1185 }
1186
1187 static struct cmd_iolog_pdu *convert_iolog_gz(struct fio_net_cmd *cmd,
1188                                               struct cmd_iolog_pdu *pdu)
1189 {
1190 #ifdef CONFIG_ZLIB
1191         struct cmd_iolog_pdu *ret;
1192         z_stream stream;
1193         uint32_t nr_samples;
1194         size_t total;
1195         void *p;
1196
1197         stream.zalloc = Z_NULL;
1198         stream.zfree = Z_NULL;
1199         stream.opaque = Z_NULL;
1200         stream.avail_in = 0;
1201         stream.next_in = Z_NULL;
1202
1203         if (inflateInit(&stream) != Z_OK)
1204                 return NULL;
1205
1206         /*
1207          * Get header first, it's not compressed
1208          */
1209         nr_samples = le64_to_cpu(pdu->nr_samples);
1210
1211         total = nr_samples * __log_entry_sz(le32_to_cpu(pdu->log_offset));
1212         ret = malloc(total + sizeof(*pdu));
1213         ret->nr_samples = nr_samples;
1214
1215         memcpy(ret, pdu, sizeof(*pdu));
1216
1217         p = (void *) ret + sizeof(*pdu);
1218
1219         stream.avail_in = cmd->pdu_len - sizeof(*pdu);
1220         stream.next_in = (void *) pdu + sizeof(*pdu);
1221         while (stream.avail_in) {
1222                 unsigned int this_chunk = 65536;
1223                 unsigned int this_len;
1224                 int err;
1225
1226                 if (this_chunk > total)
1227                         this_chunk = total;
1228
1229                 stream.avail_out = this_chunk;
1230                 stream.next_out = p;
1231                 err = inflate(&stream, Z_NO_FLUSH);
1232                 /* may be Z_OK, or Z_STREAM_END */
1233                 if (err < 0) {
1234                         log_err("fio: inflate error %d\n", err);
1235                         free(ret);
1236                         ret = NULL;
1237                         goto err;
1238                 }
1239
1240                 this_len = this_chunk - stream.avail_out;
1241                 p += this_len;
1242                 total -= this_len;
1243         }
1244
1245 err:
1246         inflateEnd(&stream);
1247         return ret;
1248 #else
1249         return NULL;
1250 #endif
1251 }
1252
1253 /*
1254  * This has been compressed on the server side, since it can be big.
1255  * Uncompress here.
1256  */
1257 static struct cmd_iolog_pdu *convert_iolog(struct fio_net_cmd *cmd)
1258 {
1259         struct cmd_iolog_pdu *pdu = (struct cmd_iolog_pdu *) cmd->payload;
1260         struct cmd_iolog_pdu *ret;
1261         uint64_t i;
1262         void *samples;
1263
1264         /*
1265          * Convert if compressed and we support it. If it's not
1266          * compressed, we need not do anything.
1267          */
1268         if (le32_to_cpu(pdu->compressed)) {
1269 #ifndef CONFIG_ZLIB
1270                 log_err("fio: server sent compressed data by mistake\n");
1271                 return NULL;
1272 #endif
1273                 ret = convert_iolog_gz(cmd, pdu);
1274                 if (!ret) {
1275                         log_err("fio: failed decompressing log\n");
1276                         return NULL;
1277                 }
1278         } else
1279                 ret = pdu;
1280
1281         ret->nr_samples         = le64_to_cpu(ret->nr_samples);
1282         ret->thread_number      = le32_to_cpu(ret->thread_number);
1283         ret->log_type           = le32_to_cpu(ret->log_type);
1284         ret->compressed         = le32_to_cpu(ret->compressed);
1285         ret->log_offset         = le32_to_cpu(ret->log_offset);
1286
1287         samples = &ret->samples[0];
1288         for (i = 0; i < ret->nr_samples; i++) {
1289                 struct io_sample *s;
1290
1291                 s = __get_sample(samples, ret->log_offset, i);
1292                 s->time         = le64_to_cpu(s->time);
1293                 s->val          = le64_to_cpu(s->val);
1294                 s->__ddir       = le32_to_cpu(s->__ddir);
1295                 s->bs           = le32_to_cpu(s->bs);
1296
1297                 if (ret->log_offset) {
1298                         struct io_sample_offset *so = (void *) s;
1299
1300                         so->offset = le64_to_cpu(so->offset);
1301                 }
1302         }
1303
1304         return ret;
1305 }
1306
1307 int fio_handle_client(struct fio_client *client)
1308 {
1309         struct client_ops *ops = client->ops;
1310         struct fio_net_cmd *cmd;
1311
1312         dprint(FD_NET, "client: handle %s\n", client->hostname);
1313
1314         cmd = fio_net_recv_cmd(client->fd);
1315         if (!cmd)
1316                 return 0;
1317
1318         dprint(FD_NET, "client: got cmd op %s from %s (pdu=%u)\n",
1319                 fio_server_op(cmd->opcode), client->hostname, cmd->pdu_len);
1320
1321         switch (cmd->opcode) {
1322         case FIO_NET_CMD_QUIT:
1323                 if (ops->quit)
1324                         ops->quit(client, cmd);
1325                 remove_client(client);
1326                 free(cmd);
1327                 break;
1328         case FIO_NET_CMD_TEXT:
1329                 convert_text(cmd);
1330                 ops->text(client, cmd);
1331                 free(cmd);
1332                 break;
1333         case FIO_NET_CMD_DU: {
1334                 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
1335
1336                 convert_dus(&du->dus);
1337                 convert_agg(&du->agg);
1338
1339                 ops->disk_util(client, cmd);
1340                 free(cmd);
1341                 break;
1342                 }
1343         case FIO_NET_CMD_TS: {
1344                 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
1345
1346                 convert_ts(&p->ts, &p->ts);
1347                 convert_gs(&p->rs, &p->rs);
1348
1349                 ops->thread_status(client, cmd);
1350                 free(cmd);
1351                 break;
1352                 }
1353         case FIO_NET_CMD_GS: {
1354                 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
1355
1356                 convert_gs(gs, gs);
1357
1358                 ops->group_stats(client, cmd);
1359                 free(cmd);
1360                 break;
1361                 }
1362         case FIO_NET_CMD_ETA: {
1363                 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
1364
1365                 remove_reply_cmd(client, cmd);
1366                 convert_jobs_eta(je);
1367                 handle_eta(client, cmd);
1368                 free(cmd);
1369                 break;
1370                 }
1371         case FIO_NET_CMD_PROBE:
1372                 remove_reply_cmd(client, cmd);
1373                 ops->probe(client, cmd);
1374                 free(cmd);
1375                 break;
1376         case FIO_NET_CMD_SERVER_START:
1377                 client->state = Client_running;
1378                 if (ops->job_start)
1379                         ops->job_start(client, cmd);
1380                 free(cmd);
1381                 break;
1382         case FIO_NET_CMD_START: {
1383                 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1384
1385                 pdu->jobs = le32_to_cpu(pdu->jobs);
1386                 ops->start(client, cmd);
1387                 free(cmd);
1388                 break;
1389                 }
1390         case FIO_NET_CMD_STOP: {
1391                 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1392
1393                 convert_stop(cmd);
1394                 client->state = Client_stopped;
1395                 client->error = le32_to_cpu(pdu->error);
1396                 client->signal = le32_to_cpu(pdu->signal);
1397                 ops->stop(client, cmd);
1398                 free(cmd);
1399                 break;
1400                 }
1401         case FIO_NET_CMD_ADD_JOB: {
1402                 struct cmd_add_job_pdu *pdu = (struct cmd_add_job_pdu *) cmd->payload;
1403
1404                 client->thread_number = le32_to_cpu(pdu->thread_number);
1405                 client->groupid = le32_to_cpu(pdu->groupid);
1406
1407                 if (ops->add_job)
1408                         ops->add_job(client, cmd);
1409                 free(cmd);
1410                 break;
1411                 }
1412         case FIO_NET_CMD_IOLOG:
1413                 if (ops->iolog) {
1414                         struct cmd_iolog_pdu *pdu;
1415
1416                         pdu = convert_iolog(cmd);
1417                         ops->iolog(client, pdu);
1418                 }
1419                 free(cmd);
1420                 break;
1421         case FIO_NET_CMD_UPDATE_JOB:
1422                 ops->update_job(client, cmd);
1423                 remove_reply_cmd(client, cmd);
1424                 free(cmd);
1425                 break;
1426         default:
1427                 log_err("fio: unknown client op: %s\n", fio_server_op(cmd->opcode));
1428                 free(cmd);
1429                 break;
1430         }
1431
1432         return 1;
1433 }
1434
1435 static void request_client_etas(struct client_ops *ops)
1436 {
1437         struct fio_client *client;
1438         struct flist_head *entry;
1439         struct client_eta *eta;
1440         int skipped = 0;
1441
1442         dprint(FD_NET, "client: request eta (%d)\n", nr_clients);
1443
1444         eta = calloc(1, sizeof(*eta) + __THREAD_RUNSTR_SZ(REAL_MAX_JOBS));
1445         eta->pending = nr_clients;
1446
1447         flist_for_each(entry, &client_list) {
1448                 client = flist_entry(entry, struct fio_client, list);
1449
1450                 if (!flist_empty(&client->eta_list)) {
1451                         skipped++;
1452                         continue;
1453                 }
1454                 if (client->state != Client_running)
1455                         continue;
1456
1457                 assert(!client->eta_in_flight);
1458                 flist_add_tail(&client->eta_list, &eta_list);
1459                 client->eta_in_flight = eta;
1460                 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA,
1461                                         (uintptr_t) eta, &client->cmd_list);
1462         }
1463
1464         while (skipped--)
1465                 fio_client_dec_jobs_eta(eta, ops->eta);
1466
1467         dprint(FD_NET, "client: requested eta tag %p\n", eta);
1468 }
1469
1470 static int client_check_cmd_timeout(struct fio_client *client,
1471                                     struct timeval *now)
1472 {
1473         struct fio_net_cmd_reply *reply;
1474         struct flist_head *entry, *tmp;
1475         int ret = 0;
1476
1477         flist_for_each_safe(entry, tmp, &client->cmd_list) {
1478                 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
1479
1480                 if (mtime_since(&reply->tv, now) < FIO_NET_CLIENT_TIMEOUT)
1481                         continue;
1482
1483                 log_err("fio: client %s, timeout on cmd %s\n", client->hostname,
1484                                                 fio_server_op(reply->opcode));
1485                 flist_del(&reply->list);
1486                 free(reply);
1487                 ret = 1;
1488         }
1489
1490         return flist_empty(&client->cmd_list) && ret;
1491 }
1492
1493 static int fio_check_clients_timed_out(void)
1494 {
1495         struct fio_client *client;
1496         struct flist_head *entry, *tmp;
1497         struct timeval tv;
1498         int ret = 0;
1499
1500         fio_gettime(&tv, NULL);
1501
1502         flist_for_each_safe(entry, tmp, &client_list) {
1503                 client = flist_entry(entry, struct fio_client, list);
1504
1505                 if (flist_empty(&client->cmd_list))
1506                         continue;
1507
1508                 if (!client_check_cmd_timeout(client, &tv))
1509                         continue;
1510
1511                 if (client->ops->timed_out)
1512                         client->ops->timed_out(client);
1513                 else
1514                         log_err("fio: client %s timed out\n", client->hostname);
1515
1516                 remove_client(client);
1517                 ret = 1;
1518         }
1519
1520         return ret;
1521 }
1522
1523 int fio_handle_clients(struct client_ops *ops)
1524 {
1525         struct pollfd *pfds;
1526         int i, ret = 0, retval = 0;
1527
1528         fio_gettime(&eta_tv, NULL);
1529
1530         pfds = malloc(nr_clients * sizeof(struct pollfd));
1531
1532         init_thread_stat(&client_ts);
1533         init_group_run_stat(&client_gs);
1534
1535         while (!exit_backend && nr_clients) {
1536                 struct flist_head *entry, *tmp;
1537                 struct fio_client *client;
1538
1539                 i = 0;
1540                 flist_for_each_safe(entry, tmp, &client_list) {
1541                         client = flist_entry(entry, struct fio_client, list);
1542
1543                         if (!client->sent_job && !client->ops->stay_connected &&
1544                             flist_empty(&client->cmd_list)) {
1545                                 remove_client(client);
1546                                 continue;
1547                         }
1548
1549                         pfds[i].fd = client->fd;
1550                         pfds[i].events = POLLIN;
1551                         i++;
1552                 }
1553
1554                 if (!nr_clients)
1555                         break;
1556
1557                 assert(i == nr_clients);
1558
1559                 do {
1560                         struct timeval tv;
1561
1562                         fio_gettime(&tv, NULL);
1563                         if (mtime_since(&eta_tv, &tv) >= 900) {
1564                                 request_client_etas(ops);
1565                                 memcpy(&eta_tv, &tv, sizeof(tv));
1566
1567                                 if (fio_check_clients_timed_out())
1568                                         break;
1569                         }
1570
1571                         ret = poll(pfds, nr_clients, ops->eta_msec);
1572                         if (ret < 0) {
1573                                 if (errno == EINTR)
1574                                         continue;
1575                                 log_err("fio: poll clients: %s\n", strerror(errno));
1576                                 break;
1577                         } else if (!ret)
1578                                 continue;
1579                 } while (ret <= 0);
1580
1581                 for (i = 0; i < nr_clients; i++) {
1582                         if (!(pfds[i].revents & POLLIN))
1583                                 continue;
1584
1585                         client = find_client_by_fd(pfds[i].fd);
1586                         if (!client) {
1587                                 log_err("fio: unknown client fd %ld\n", (long) pfds[i].fd);
1588                                 continue;
1589                         }
1590                         if (!fio_handle_client(client)) {
1591                                 log_info("client: host=%s disconnected\n",
1592                                                 client->hostname);
1593                                 remove_client(client);
1594                                 retval = 1;
1595                         } else if (client->error)
1596                                 retval = 1;
1597                         fio_put_client(client);
1598                 }
1599         }
1600
1601         fio_client_json_fini();
1602
1603         free(pfds);
1604         return retval;
1605 }