Make json_object_add_value_string() duplicate its 'value' argument
[fio.git] / client.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <errno.h>
5 #include <fcntl.h>
6 #include <poll.h>
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <sys/socket.h>
10 #include <sys/un.h>
11 #include <netinet/in.h>
12 #include <arpa/inet.h>
13 #include <netdb.h>
14 #include <signal.h>
15 #ifdef CONFIG_ZLIB
16 #include <zlib.h>
17 #endif
18
19 #include "fio.h"
20 #include "client.h"
21 #include "server.h"
22 #include "flist.h"
23 #include "hash.h"
24 #include "verify-state.h"
25
26 static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd);
27 static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd);
28 static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd);
29 static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd);
30 static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd);
31 static void handle_stop(struct fio_client *client);
32 static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd);
33
34 static void convert_text(struct fio_net_cmd *cmd);
35 static void client_display_thread_status(struct jobs_eta *je);
36
37 struct client_ops fio_client_ops = {
38         .text           = handle_text,
39         .disk_util      = handle_du,
40         .thread_status  = handle_ts,
41         .group_stats    = handle_gs,
42         .stop           = handle_stop,
43         .start          = handle_start,
44         .eta            = client_display_thread_status,
45         .probe          = handle_probe,
46         .eta_msec       = FIO_CLIENT_DEF_ETA_MSEC,
47         .client_type    = FIO_CLIENT_TYPE_CLI,
48 };
49
50 static struct timespec eta_ts;
51
52 static FLIST_HEAD(client_list);
53 static FLIST_HEAD(eta_list);
54
55 static FLIST_HEAD(arg_list);
56
57 struct thread_stat client_ts;
58 struct group_run_stats client_gs;
59 int sum_stat_clients;
60
61 static int sum_stat_nr;
62 static struct buf_output allclients;
63 static struct json_object *root = NULL;
64 static struct json_object *job_opt_object = NULL;
65 static struct json_array *clients_array = NULL;
66 static struct json_array *du_array = NULL;
67
68 static int error_clients;
69
70 #define FIO_CLIENT_HASH_BITS    7
71 #define FIO_CLIENT_HASH_SZ      (1 << FIO_CLIENT_HASH_BITS)
72 #define FIO_CLIENT_HASH_MASK    (FIO_CLIENT_HASH_SZ - 1)
73 static struct flist_head client_hash[FIO_CLIENT_HASH_SZ];
74
75 static struct cmd_iolog_pdu *convert_iolog(struct fio_net_cmd *, bool *);
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 int read_data(int fd, void *data, size_t size)
100 {
101         ssize_t ret;
102
103         while (size) {
104                 ret = read(fd, data, size);
105                 if (ret < 0) {
106                         if (errno == EAGAIN || errno == EINTR)
107                                 continue;
108                         break;
109                 } else if (!ret)
110                         break;
111                 else {
112                         data += ret;
113                         size -= ret;
114                 }
115         }
116
117         if (size)
118                 return EAGAIN;
119
120         return 0;
121 }
122
123 static int read_ini_data(int fd, void *data, size_t size)
124 {
125         char *p = data;
126         int ret = 0;
127         FILE *fp;
128         int dupfd;
129
130         dupfd = dup(fd);
131         if (dupfd < 0)
132                 return errno;
133
134         fp = fdopen(dupfd, "r");
135         if (!fp) {
136                 ret = errno;
137                 close(dupfd);
138                 goto out;
139         }
140
141         while (1) {
142                 ssize_t len;
143                 char buf[OPT_LEN_MAX+1], *sub;
144
145                 if (!fgets(buf, sizeof(buf), fp)) {
146                         if (ferror(fp)) {
147                                 if (errno == EAGAIN || errno == EINTR)
148                                         continue;
149                                 ret = errno;
150                         }
151                         break;
152                 }
153
154                 sub = fio_option_dup_subs(buf);
155                 len = strlen(sub);
156                 if (len + 1 > size) {
157                         log_err("fio: no space left to read data\n");
158                         free(sub);
159                         ret = ENOSPC;
160                         break;
161                 }
162
163                 memcpy(p, sub, len);
164                 free(sub);
165                 p += len;
166                 *p = '\0';
167                 size -= len;
168         }
169
170         fclose(fp);
171 out:
172         return ret;
173 }
174
175 static void fio_client_json_init(void)
176 {
177         char time_buf[32];
178         time_t time_p;
179
180         if (!(output_format & FIO_OUTPUT_JSON))
181                 return;
182
183         time(&time_p);
184         os_ctime_r((const time_t *) &time_p, time_buf, sizeof(time_buf));
185         time_buf[strlen(time_buf) - 1] = '\0';
186
187         root = json_create_object();
188         json_object_add_value_string(root, "fio version", fio_version_string);
189         json_object_add_value_int(root, "timestamp", time_p);
190         json_object_add_value_string(root, "time", time_buf);
191
192         job_opt_object = json_create_object();
193         json_object_add_value_object(root, "global options", job_opt_object);
194         clients_array = json_create_array();
195         json_object_add_value_array(root, "client_stats", clients_array);
196         du_array = json_create_array();
197         json_object_add_value_array(root, "disk_util", du_array);
198 }
199
200 static void fio_client_json_fini(void)
201 {
202         struct buf_output out;
203
204         if (!root)
205                 return;
206
207         buf_output_init(&out);
208
209         __log_buf(&out, "\n");
210         json_print_object(root, &out);
211         __log_buf(&out, "\n");
212         log_info_buf(out.buf, out.buflen);
213
214         buf_output_free(&out);
215
216         json_free_object(root);
217         root = NULL;
218         job_opt_object = NULL;
219         clients_array = NULL;
220         du_array = NULL;
221 }
222
223 static struct fio_client *find_client_by_fd(int fd)
224 {
225         int bucket = hash_long(fd, FIO_CLIENT_HASH_BITS) & FIO_CLIENT_HASH_MASK;
226         struct fio_client *client;
227         struct flist_head *entry;
228
229         flist_for_each(entry, &client_hash[bucket]) {
230                 client = flist_entry(entry, struct fio_client, hash_list);
231
232                 if (client->fd == fd) {
233                         client->refs++;
234                         return client;
235                 }
236         }
237
238         return NULL;
239 }
240
241 void fio_put_client(struct fio_client *client)
242 {
243         if (--client->refs)
244                 return;
245
246         log_info_buf(client->buf.buf, client->buf.buflen);
247         buf_output_free(&client->buf);
248
249         free(client->hostname);
250         if (client->argv)
251                 free(client->argv);
252         if (client->name)
253                 free(client->name);
254         while (client->nr_files) {
255                 struct client_file *cf = &client->files[--client->nr_files];
256
257                 free(cf->file);
258         }
259         if (client->files)
260                 free(client->files);
261         if (client->opt_lists)
262                 free(client->opt_lists);
263
264         if (!client->did_stat)
265                 sum_stat_clients--;
266
267         if (client->error)
268                 error_clients++;
269
270         free(client);
271 }
272
273 static int fio_client_dec_jobs_eta(struct client_eta *eta, client_eta_op eta_fn)
274 {
275         if (!--eta->pending) {
276                 eta_fn(&eta->eta);
277                 free(eta);
278                 return 0;
279         }
280
281         return 1;
282 }
283
284 static void fio_drain_client_text(struct fio_client *client)
285 {
286         do {
287                 struct fio_net_cmd *cmd;
288
289                 cmd = fio_net_recv_cmd(client->fd, false);
290                 if (!cmd)
291                         break;
292
293                 if (cmd->opcode == FIO_NET_CMD_TEXT) {
294                         convert_text(cmd);
295                         client->ops->text(client, cmd);
296                 }
297
298                 free(cmd);
299         } while (1);
300 }
301
302 static void remove_client(struct fio_client *client)
303 {
304         assert(client->refs);
305
306         dprint(FD_NET, "client: removed <%s>\n", client->hostname);
307
308         fio_drain_client_text(client);
309
310         if (!flist_empty(&client->list))
311                 flist_del_init(&client->list);
312
313         fio_client_remove_hash(client);
314
315         if (!flist_empty(&client->eta_list)) {
316                 flist_del_init(&client->eta_list);
317                 fio_client_dec_jobs_eta(client->eta_in_flight, client->ops->eta);
318         }
319
320         close(client->fd);
321         client->fd = -1;
322
323         if (client->ops->removed)
324                 client->ops->removed(client);
325
326         nr_clients--;
327         fio_put_client(client);
328 }
329
330 struct fio_client *fio_get_client(struct fio_client *client)
331 {
332         client->refs++;
333         return client;
334 }
335
336 static void __fio_client_add_cmd_option(struct fio_client *client,
337                                         const char *opt)
338 {
339         int index;
340
341         index = client->argc++;
342         client->argv = realloc(client->argv, sizeof(char *) * client->argc);
343         client->argv[index] = strdup(opt);
344         dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
345 }
346
347 void fio_client_add_cmd_option(void *cookie, const char *opt)
348 {
349         struct fio_client *client = cookie;
350         struct flist_head *entry;
351
352         if (!client || !opt)
353                 return;
354
355         __fio_client_add_cmd_option(client, opt);
356
357         /*
358          * Duplicate arguments to shared client group
359          */
360         flist_for_each(entry, &arg_list) {
361                 client = flist_entry(entry, struct fio_client, arg_list);
362
363                 __fio_client_add_cmd_option(client, opt);
364         }
365 }
366
367 static struct fio_client *get_new_client(void)
368 {
369         struct fio_client *client;
370
371         client = malloc(sizeof(*client));
372         memset(client, 0, sizeof(*client));
373
374         INIT_FLIST_HEAD(&client->list);
375         INIT_FLIST_HEAD(&client->hash_list);
376         INIT_FLIST_HEAD(&client->arg_list);
377         INIT_FLIST_HEAD(&client->eta_list);
378         INIT_FLIST_HEAD(&client->cmd_list);
379
380         buf_output_init(&client->buf);
381
382         return client;
383 }
384
385 struct fio_client *fio_client_add_explicit(struct client_ops *ops,
386                                            const char *hostname, int type,
387                                            int port)
388 {
389         struct fio_client *client;
390
391         client = get_new_client();
392
393         client->hostname = strdup(hostname);
394
395         if (type == Fio_client_socket)
396                 client->is_sock = true;
397         else {
398                 int ipv6;
399
400                 ipv6 = type == Fio_client_ipv6;
401                 if (fio_server_parse_host(hostname, ipv6,
402                                                 &client->addr.sin_addr,
403                                                 &client->addr6.sin6_addr))
404                         goto err;
405
406                 client->port = port;
407         }
408
409         client->fd = -1;
410         client->ops = ops;
411         client->refs = 1;
412         client->type = ops->client_type;
413
414         __fio_client_add_cmd_option(client, "fio");
415
416         flist_add(&client->list, &client_list);
417         nr_clients++;
418         dprint(FD_NET, "client: added <%s>\n", client->hostname);
419         return client;
420 err:
421         free(client);
422         return NULL;
423 }
424
425 int fio_client_add_ini_file(void *cookie, const char *ini_file, bool remote)
426 {
427         struct fio_client *client = cookie;
428         struct client_file *cf;
429         size_t new_size;
430         void *new_files;
431
432         if (!client)
433                 return 1;
434
435         dprint(FD_NET, "client <%s>: add ini %s\n", client->hostname, ini_file);
436
437         new_size = (client->nr_files + 1) * sizeof(struct client_file);
438         new_files = realloc(client->files, new_size);
439         if (!new_files)
440                 return 1;
441
442         client->files = new_files;
443         cf = &client->files[client->nr_files];
444         cf->file = strdup(ini_file);
445         cf->remote = remote;
446         client->nr_files++;
447         return 0;
448 }
449
450 int fio_client_add(struct client_ops *ops, const char *hostname, void **cookie)
451 {
452         struct fio_client *existing = *cookie;
453         struct fio_client *client;
454
455         if (existing) {
456                 /*
457                  * We always add our "exec" name as the option, hence 1
458                  * means empty.
459                  */
460                 if (existing->argc == 1)
461                         flist_add_tail(&existing->arg_list, &arg_list);
462                 else {
463                         while (!flist_empty(&arg_list))
464                                 flist_del_init(arg_list.next);
465                 }
466         }
467
468         client = get_new_client();
469
470         if (fio_server_parse_string(hostname, &client->hostname,
471                                         &client->is_sock, &client->port,
472                                         &client->addr.sin_addr,
473                                         &client->addr6.sin6_addr,
474                                         &client->ipv6))
475                 return -1;
476
477         client->fd = -1;
478         client->ops = ops;
479         client->refs = 1;
480         client->type = ops->client_type;
481
482         __fio_client_add_cmd_option(client, "fio");
483
484         flist_add(&client->list, &client_list);
485         nr_clients++;
486         dprint(FD_NET, "client: added <%s>\n", client->hostname);
487         *cookie = client;
488         return 0;
489 }
490
491 static const char *server_name(struct fio_client *client, char *buf,
492                                size_t bufsize)
493 {
494         const char *from;
495
496         if (client->ipv6)
497                 from = inet_ntop(AF_INET6, (struct sockaddr *) &client->addr6.sin6_addr, buf, bufsize);
498         else if (client->is_sock)
499                 from = "sock";
500         else
501                 from = inet_ntop(AF_INET, (struct sockaddr *) &client->addr.sin_addr, buf, bufsize);
502
503         return from;
504 }
505
506 static void probe_client(struct fio_client *client)
507 {
508         struct cmd_client_probe_pdu pdu;
509         const char *sname;
510         uint64_t tag;
511         char buf[64];
512
513         dprint(FD_NET, "client: send probe\n");
514
515 #ifdef CONFIG_ZLIB
516         pdu.flags = __le64_to_cpu(FIO_PROBE_FLAG_ZLIB);
517 #else
518         pdu.flags = 0;
519 #endif
520
521         sname = server_name(client, buf, sizeof(buf));
522         memset(pdu.server, 0, sizeof(pdu.server));
523         snprintf((char *) pdu.server, sizeof(pdu.server), "%s", sname);
524
525         fio_net_send_cmd(client->fd, FIO_NET_CMD_PROBE, &pdu, sizeof(pdu), &tag, &client->cmd_list);
526 }
527
528 static int fio_client_connect_ip(struct fio_client *client)
529 {
530         struct sockaddr *addr;
531         socklen_t socklen;
532         int fd, domain;
533
534         if (client->ipv6) {
535                 client->addr6.sin6_family = AF_INET6;
536                 client->addr6.sin6_port = htons(client->port);
537                 domain = AF_INET6;
538                 addr = (struct sockaddr *) &client->addr6;
539                 socklen = sizeof(client->addr6);
540         } else {
541                 client->addr.sin_family = AF_INET;
542                 client->addr.sin_port = htons(client->port);
543                 domain = AF_INET;
544                 addr = (struct sockaddr *) &client->addr;
545                 socklen = sizeof(client->addr);
546         }
547
548         fd = socket(domain, SOCK_STREAM, 0);
549         if (fd < 0) {
550                 int ret = -errno;
551
552                 log_err("fio: socket: %s\n", strerror(errno));
553                 return ret;
554         }
555
556         if (connect(fd, addr, socklen) < 0) {
557                 int ret = -errno;
558
559                 log_err("fio: connect: %s\n", strerror(errno));
560                 log_err("fio: failed to connect to %s:%u\n", client->hostname,
561                                                                 client->port);
562                 close(fd);
563                 return ret;
564         }
565
566         return fd;
567 }
568
569 static int fio_client_connect_sock(struct fio_client *client)
570 {
571         struct sockaddr_un *addr = &client->addr_un;
572         socklen_t len;
573         int fd;
574
575         memset(addr, 0, sizeof(*addr));
576         addr->sun_family = AF_UNIX;
577         snprintf(addr->sun_path, sizeof(addr->sun_path), "%s",
578                  client->hostname);
579
580         fd = socket(AF_UNIX, SOCK_STREAM, 0);
581         if (fd < 0) {
582                 int ret = -errno;
583
584                 log_err("fio: socket: %s\n", strerror(errno));
585                 return ret;
586         }
587
588         len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
589         if (connect(fd, (struct sockaddr *) addr, len) < 0) {
590                 int ret = -errno;
591
592                 log_err("fio: connect; %s\n", strerror(errno));
593                 close(fd);
594                 return ret;
595         }
596
597         return fd;
598 }
599
600 int fio_client_connect(struct fio_client *client)
601 {
602         int fd;
603
604         dprint(FD_NET, "client: connect to host %s\n", client->hostname);
605
606         if (client->is_sock)
607                 fd = fio_client_connect_sock(client);
608         else
609                 fd = fio_client_connect_ip(client);
610
611         dprint(FD_NET, "client: %s connected %d\n", client->hostname, fd);
612
613         if (fd < 0)
614                 return fd;
615
616         client->fd = fd;
617         fio_client_add_hash(client);
618         client->state = Client_connected;
619
620         probe_client(client);
621         return 0;
622 }
623
624 int fio_client_terminate(struct fio_client *client)
625 {
626         return fio_net_send_quit(client->fd);
627 }
628
629 static void fio_clients_terminate(void)
630 {
631         struct flist_head *entry;
632         struct fio_client *client;
633
634         dprint(FD_NET, "client: terminate clients\n");
635
636         flist_for_each(entry, &client_list) {
637                 client = flist_entry(entry, struct fio_client, list);
638                 fio_client_terminate(client);
639         }
640 }
641
642 static void sig_int(int sig)
643 {
644         dprint(FD_NET, "client: got signal %d\n", sig);
645         fio_clients_terminate();
646 }
647
648 static void client_signal_handler(void)
649 {
650         struct sigaction act;
651
652         memset(&act, 0, sizeof(act));
653         act.sa_handler = sig_int;
654         act.sa_flags = SA_RESTART;
655         sigaction(SIGINT, &act, NULL);
656
657         memset(&act, 0, sizeof(act));
658         act.sa_handler = sig_int;
659         act.sa_flags = SA_RESTART;
660         sigaction(SIGTERM, &act, NULL);
661
662 /* Windows uses SIGBREAK as a quit signal from other applications */
663 #ifdef WIN32
664         memset(&act, 0, sizeof(act));
665         act.sa_handler = sig_int;
666         act.sa_flags = SA_RESTART;
667         sigaction(SIGBREAK, &act, NULL);
668 #endif
669
670         memset(&act, 0, sizeof(act));
671         act.sa_handler = sig_show_status;
672         act.sa_flags = SA_RESTART;
673         sigaction(SIGUSR1, &act, NULL);
674 }
675
676 static int send_client_cmd_line(struct fio_client *client)
677 {
678         struct cmd_single_line_pdu *cslp;
679         struct cmd_line_pdu *clp;
680         unsigned long offset;
681         unsigned int *lens;
682         void *pdu;
683         size_t mem;
684         int i, ret;
685
686         dprint(FD_NET, "client: send cmdline %d\n", client->argc);
687
688         lens = malloc(client->argc * sizeof(unsigned int));
689
690         /*
691          * Find out how much mem we need
692          */
693         for (i = 0, mem = 0; i < client->argc; i++) {
694                 lens[i] = strlen(client->argv[i]) + 1;
695                 mem += lens[i];
696         }
697
698         /*
699          * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu
700          */
701         mem += sizeof(*clp) + (client->argc * sizeof(*cslp));
702
703         pdu = malloc(mem);
704         clp = pdu;
705         offset = sizeof(*clp);
706
707         for (i = 0; i < client->argc; i++) {
708                 uint16_t arg_len = lens[i];
709
710                 cslp = pdu + offset;
711                 strcpy((char *) cslp->text, client->argv[i]);
712                 cslp->len = cpu_to_le16(arg_len);
713                 offset += sizeof(*cslp) + arg_len;
714         }
715
716         free(lens);
717         clp->lines = cpu_to_le16(client->argc);
718         clp->client_type = __cpu_to_le16(client->type);
719         ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, NULL, NULL);
720         free(pdu);
721         return ret;
722 }
723
724 int fio_clients_connect(void)
725 {
726         struct fio_client *client;
727         struct flist_head *entry, *tmp;
728         int ret;
729
730 #ifdef WIN32
731         WSADATA wsd;
732         WSAStartup(MAKEWORD(2, 2), &wsd);
733 #endif
734
735         dprint(FD_NET, "client: connect all\n");
736
737         client_signal_handler();
738
739         flist_for_each_safe(entry, tmp, &client_list) {
740                 client = flist_entry(entry, struct fio_client, list);
741
742                 ret = fio_client_connect(client);
743                 if (ret) {
744                         remove_client(client);
745                         continue;
746                 }
747
748                 if (client->argc > 1)
749                         send_client_cmd_line(client);
750         }
751
752         return !nr_clients;
753 }
754
755 int fio_start_client(struct fio_client *client)
756 {
757         dprint(FD_NET, "client: start %s\n", client->hostname);
758         return fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_RUN, 0, NULL);
759 }
760
761 int fio_start_all_clients(void)
762 {
763         struct fio_client *client;
764         struct flist_head *entry, *tmp;
765         int ret;
766
767         dprint(FD_NET, "client: start all\n");
768
769         fio_client_json_init();
770
771         flist_for_each_safe(entry, tmp, &client_list) {
772                 client = flist_entry(entry, struct fio_client, list);
773
774                 ret = fio_start_client(client);
775                 if (ret) {
776                         remove_client(client);
777                         continue;
778                 }
779         }
780
781         return flist_empty(&client_list);
782 }
783
784 static int __fio_client_send_remote_ini(struct fio_client *client,
785                                         const char *filename)
786 {
787         struct cmd_load_file_pdu *pdu;
788         size_t p_size;
789         int ret;
790
791         dprint(FD_NET, "send remote ini %s to %s\n", filename, client->hostname);
792
793         p_size = sizeof(*pdu) + strlen(filename) + 1;
794         pdu = malloc(p_size);
795         memset(pdu, 0, p_size);
796         pdu->name_len = strlen(filename);
797         strcpy((char *) pdu->file, filename);
798         pdu->client_type = cpu_to_le16((uint16_t) client->type);
799
800         client->sent_job = true;
801         ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_LOAD_FILE, pdu, p_size,NULL, NULL);
802         free(pdu);
803         return ret;
804 }
805
806 /*
807  * Send file contents to server backend. We could use sendfile(), but to remain
808  * more portable lets just read/write the darn thing.
809  */
810 static int __fio_client_send_local_ini(struct fio_client *client,
811                                        const char *filename)
812 {
813         struct cmd_job_pdu *pdu;
814         size_t p_size;
815         struct stat sb;
816         char *p;
817         void *buf;
818         off_t len;
819         int fd, ret;
820
821         dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
822
823         fd = open(filename, O_RDONLY);
824         if (fd < 0) {
825                 ret = -errno;
826                 log_err("fio: job file <%s> open: %s\n", filename, strerror(errno));
827                 return ret;
828         }
829
830         if (fstat(fd, &sb) < 0) {
831                 ret = -errno;
832                 log_err("fio: job file stat: %s\n", strerror(errno));
833                 close(fd);
834                 return ret;
835         }
836
837         /*
838          * Add extra space for variable expansion, but doesn't guarantee.
839          */
840         sb.st_size += OPT_LEN_MAX;
841         p_size = sb.st_size + sizeof(*pdu);
842         pdu = malloc(p_size);
843         buf = pdu->buf;
844
845         len = sb.st_size;
846         p = buf;
847         if (read_ini_data(fd, p, len)) {
848                 log_err("fio: failed reading job file %s\n", filename);
849                 close(fd);
850                 free(pdu);
851                 return 1;
852         }
853
854         pdu->buf_len = __cpu_to_le32(sb.st_size);
855         pdu->client_type = cpu_to_le32(client->type);
856
857         client->sent_job = true;
858         ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, pdu, p_size, NULL, NULL);
859         free(pdu);
860         close(fd);
861         return ret;
862 }
863
864 int fio_client_send_ini(struct fio_client *client, const char *filename,
865                         bool remote)
866 {
867         int ret;
868
869         if (!remote)
870                 ret = __fio_client_send_local_ini(client, filename);
871         else
872                 ret = __fio_client_send_remote_ini(client, filename);
873
874         if (!ret)
875                 client->sent_job = true;
876
877         return ret;
878 }
879
880 static int fio_client_send_cf(struct fio_client *client,
881                               struct client_file *cf)
882 {
883         return fio_client_send_ini(client, cf->file, cf->remote);
884 }
885
886 int fio_clients_send_ini(const char *filename)
887 {
888         struct fio_client *client;
889         struct flist_head *entry, *tmp;
890
891         flist_for_each_safe(entry, tmp, &client_list) {
892                 bool failed = false;
893
894                 client = flist_entry(entry, struct fio_client, list);
895
896                 if (client->nr_files) {
897                         int i;
898
899                         for (i = 0; i < client->nr_files; i++) {
900                                 struct client_file *cf;
901
902                                 cf = &client->files[i];
903
904                                 if (fio_client_send_cf(client, cf)) {
905                                         failed = true;
906                                         remove_client(client);
907                                         break;
908                                 }
909                         }
910                 }
911                 if (client->sent_job || failed)
912                         continue;
913                 if (!filename || fio_client_send_ini(client, filename, 0))
914                         remove_client(client);
915         }
916
917         return !nr_clients;
918 }
919
920 int fio_client_update_options(struct fio_client *client,
921                               struct thread_options *o, uint64_t *tag)
922 {
923         struct cmd_add_job_pdu pdu;
924
925         pdu.thread_number = cpu_to_le32(client->thread_number);
926         pdu.groupid = cpu_to_le32(client->groupid);
927         convert_thread_options_to_net(&pdu.top, o);
928
929         return fio_net_send_cmd(client->fd, FIO_NET_CMD_UPDATE_JOB, &pdu, sizeof(pdu), tag, &client->cmd_list);
930 }
931
932 static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
933 {
934         dst->max_val    = le64_to_cpu(src->max_val);
935         dst->min_val    = le64_to_cpu(src->min_val);
936         dst->samples    = le64_to_cpu(src->samples);
937
938         /*
939          * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
940          */
941         dst->mean.u.f   = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
942         dst->S.u.f      = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
943 }
944
945 static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
946 {
947         int i, j, k;
948
949         dst->error              = le32_to_cpu(src->error);
950         dst->thread_number      = le32_to_cpu(src->thread_number);
951         dst->groupid            = le32_to_cpu(src->groupid);
952         dst->pid                = le32_to_cpu(src->pid);
953         dst->members            = le32_to_cpu(src->members);
954         dst->unified_rw_rep     = le32_to_cpu(src->unified_rw_rep);
955
956         for (i = 0; i < DDIR_RWDIR_CNT; i++) {
957                 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
958                 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
959                 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
960                 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
961                 convert_io_stat(&dst->iops_stat[i], &src->iops_stat[i]);
962         }
963         convert_io_stat(&dst->sync_stat, &src->sync_stat);
964
965         dst->usr_time           = le64_to_cpu(src->usr_time);
966         dst->sys_time           = le64_to_cpu(src->sys_time);
967         dst->ctx                = le64_to_cpu(src->ctx);
968         dst->minf               = le64_to_cpu(src->minf);
969         dst->majf               = le64_to_cpu(src->majf);
970         dst->clat_percentiles   = le32_to_cpu(src->clat_percentiles);
971         dst->lat_percentiles    = le32_to_cpu(src->lat_percentiles);
972         dst->slat_percentiles   = le32_to_cpu(src->slat_percentiles);
973         dst->percentile_precision = le64_to_cpu(src->percentile_precision);
974
975         for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
976                 fio_fp64_t *fps = &src->percentile_list[i];
977                 fio_fp64_t *fpd = &dst->percentile_list[i];
978
979                 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
980         }
981
982         for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
983                 dst->io_u_map[i]        = le64_to_cpu(src->io_u_map[i]);
984                 dst->io_u_submit[i]     = le64_to_cpu(src->io_u_submit[i]);
985                 dst->io_u_complete[i]   = le64_to_cpu(src->io_u_complete[i]);
986         }
987
988         for (i = 0; i < FIO_IO_U_LAT_N_NR; i++)
989                 dst->io_u_lat_n[i]      = le64_to_cpu(src->io_u_lat_n[i]);
990         for (i = 0; i < FIO_IO_U_LAT_U_NR; i++)
991                 dst->io_u_lat_u[i]      = le64_to_cpu(src->io_u_lat_u[i]);
992         for (i = 0; i < FIO_IO_U_LAT_M_NR; i++)
993                 dst->io_u_lat_m[i]      = le64_to_cpu(src->io_u_lat_m[i]);
994
995         for (i = 0; i < FIO_LAT_CNT; i++)
996                 for (j = 0; j < DDIR_RWDIR_CNT; j++)
997                         for (k = 0; k < FIO_IO_U_PLAT_NR; k++)
998                                 dst->io_u_plat[i][j][k] = le64_to_cpu(src->io_u_plat[i][j][k]);
999
1000         for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
1001                 dst->io_u_sync_plat[j] = le64_to_cpu(src->io_u_sync_plat[j]);
1002
1003         for (i = 0; i < DDIR_RWDIR_SYNC_CNT; i++)
1004                 dst->total_io_u[i]      = le64_to_cpu(src->total_io_u[i]);
1005
1006         for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1007                 dst->short_io_u[i]      = le64_to_cpu(src->short_io_u[i]);
1008                 dst->drop_io_u[i]       = le64_to_cpu(src->drop_io_u[i]);
1009         }
1010
1011         dst->total_submit       = le64_to_cpu(src->total_submit);
1012         dst->total_complete     = le64_to_cpu(src->total_complete);
1013         dst->nr_zone_resets     = le64_to_cpu(src->nr_zone_resets);
1014
1015         for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1016                 dst->io_bytes[i]        = le64_to_cpu(src->io_bytes[i]);
1017                 dst->runtime[i]         = le64_to_cpu(src->runtime[i]);
1018         }
1019
1020         dst->total_run_time     = le64_to_cpu(src->total_run_time);
1021         dst->continue_on_error  = le16_to_cpu(src->continue_on_error);
1022         dst->total_err_count    = le64_to_cpu(src->total_err_count);
1023         dst->first_error        = le32_to_cpu(src->first_error);
1024         dst->kb_base            = le32_to_cpu(src->kb_base);
1025         dst->unit_base          = le32_to_cpu(src->unit_base);
1026
1027         dst->sig_figs           = le32_to_cpu(src->sig_figs);
1028
1029         dst->latency_depth      = le32_to_cpu(src->latency_depth);
1030         dst->latency_target     = le64_to_cpu(src->latency_target);
1031         dst->latency_window     = le64_to_cpu(src->latency_window);
1032         dst->latency_percentile.u.f = fio_uint64_to_double(le64_to_cpu(src->latency_percentile.u.i));
1033
1034         dst->nr_block_infos     = le64_to_cpu(src->nr_block_infos);
1035         for (i = 0; i < dst->nr_block_infos; i++)
1036                 dst->block_infos[i] = le32_to_cpu(src->block_infos[i]);
1037         for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1038                 for (j = 0; j < FIO_IO_U_PLAT_NR; j++) {
1039                         dst->io_u_plat_high_prio[i][j] = le64_to_cpu(src->io_u_plat_high_prio[i][j]);
1040                         dst->io_u_plat_low_prio[i][j] = le64_to_cpu(src->io_u_plat_low_prio[i][j]);
1041                 }
1042                 convert_io_stat(&dst->clat_high_prio_stat[i], &src->clat_high_prio_stat[i]);
1043                 convert_io_stat(&dst->clat_low_prio_stat[i], &src->clat_low_prio_stat[i]);
1044         }
1045
1046         dst->ss_dur             = le64_to_cpu(src->ss_dur);
1047         dst->ss_state           = le32_to_cpu(src->ss_state);
1048         dst->ss_head            = le32_to_cpu(src->ss_head);
1049         dst->ss_limit.u.f       = fio_uint64_to_double(le64_to_cpu(src->ss_limit.u.i));
1050         dst->ss_slope.u.f       = fio_uint64_to_double(le64_to_cpu(src->ss_slope.u.i));
1051         dst->ss_deviation.u.f   = fio_uint64_to_double(le64_to_cpu(src->ss_deviation.u.i));
1052         dst->ss_criterion.u.f   = fio_uint64_to_double(le64_to_cpu(src->ss_criterion.u.i));
1053
1054         if (dst->ss_state & FIO_SS_DATA) {
1055                 for (i = 0; i < dst->ss_dur; i++ ) {
1056                         dst->ss_iops_data[i] = le64_to_cpu(src->ss_iops_data[i]);
1057                         dst->ss_bw_data[i] = le64_to_cpu(src->ss_bw_data[i]);
1058                 }
1059         }
1060
1061         dst->cachehit           = le64_to_cpu(src->cachehit);
1062         dst->cachemiss          = le64_to_cpu(src->cachemiss);
1063 }
1064
1065 static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
1066 {
1067         int i;
1068
1069         for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1070                 dst->max_run[i]         = le64_to_cpu(src->max_run[i]);
1071                 dst->min_run[i]         = le64_to_cpu(src->min_run[i]);
1072                 dst->max_bw[i]          = le64_to_cpu(src->max_bw[i]);
1073                 dst->min_bw[i]          = le64_to_cpu(src->min_bw[i]);
1074                 dst->iobytes[i]         = le64_to_cpu(src->iobytes[i]);
1075                 dst->agg[i]             = le64_to_cpu(src->agg[i]);
1076         }
1077
1078         dst->kb_base    = le32_to_cpu(src->kb_base);
1079         dst->unit_base  = le32_to_cpu(src->unit_base);
1080         dst->sig_figs   = le32_to_cpu(src->sig_figs);
1081         dst->groupid    = le32_to_cpu(src->groupid);
1082         dst->unified_rw_rep     = le32_to_cpu(src->unified_rw_rep);
1083 }
1084
1085 static void json_object_add_client_info(struct json_object *obj,
1086                                         struct fio_client *client)
1087 {
1088         const char *hostname = client->hostname ? client->hostname : "";
1089
1090         json_object_add_value_string(obj, "hostname", hostname);
1091         json_object_add_value_int(obj, "port", client->port);
1092 }
1093
1094 static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd)
1095 {
1096         struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
1097         struct flist_head *opt_list = NULL;
1098         struct json_object *tsobj;
1099
1100         if (client->opt_lists && p->ts.thread_number <= client->jobs)
1101                 opt_list = &client->opt_lists[p->ts.thread_number - 1];
1102
1103         tsobj = show_thread_status(&p->ts, &p->rs, opt_list, &client->buf);
1104         client->did_stat = true;
1105         if (tsobj) {
1106                 json_object_add_client_info(tsobj, client);
1107                 json_array_add_value_object(clients_array, tsobj);
1108         }
1109
1110         if (sum_stat_clients <= 1)
1111                 return;
1112
1113         sum_thread_stats(&client_ts, &p->ts, sum_stat_nr == 1);
1114         sum_group_stats(&client_gs, &p->rs);
1115
1116         client_ts.members++;
1117         client_ts.thread_number = p->ts.thread_number;
1118         client_ts.groupid = p->ts.groupid;
1119         client_ts.unified_rw_rep = p->ts.unified_rw_rep;
1120         client_ts.sig_figs = p->ts.sig_figs;
1121
1122         if (++sum_stat_nr == sum_stat_clients) {
1123                 strcpy(client_ts.name, "All clients");
1124                 tsobj = show_thread_status(&client_ts, &client_gs, NULL, &allclients);
1125                 if (tsobj) {
1126                         json_object_add_client_info(tsobj, client);
1127                         json_array_add_value_object(clients_array, tsobj);
1128                 }
1129         }
1130 }
1131
1132 static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd)
1133 {
1134         struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
1135
1136         if (output_format & FIO_OUTPUT_NORMAL)
1137                 show_group_stats(gs, &client->buf);
1138 }
1139
1140 static void handle_job_opt(struct fio_client *client, struct fio_net_cmd *cmd)
1141 {
1142         struct cmd_job_option *pdu = (struct cmd_job_option *) cmd->payload;
1143         struct print_option *p;
1144
1145         if (!job_opt_object)
1146                 return;
1147
1148         pdu->global = le16_to_cpu(pdu->global);
1149         pdu->truncated = le16_to_cpu(pdu->truncated);
1150         pdu->groupid = le32_to_cpu(pdu->groupid);
1151
1152         p = malloc(sizeof(*p));
1153         p->name = strdup((char *) pdu->name);
1154
1155         if (pdu->global) {
1156                 json_object_add_value_string(job_opt_object, p->name,
1157                                              (const char *)pdu->value);
1158         } else if (client->opt_lists) {
1159                 struct flist_head *opt_list = &client->opt_lists[pdu->groupid];
1160
1161                 p->value = pdu->value[0] ? strdup((const char *)pdu->value) :
1162                         NULL;
1163                 flist_add_tail(&p->list, opt_list);
1164         } else {
1165                 free(p->name);
1166                 free(p);
1167         }
1168 }
1169
1170 static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd)
1171 {
1172         struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
1173         const char *buf = (const char *) pdu->buf;
1174         const char *name;
1175         int fio_unused ret;
1176         struct buf_output out;
1177
1178         buf_output_init(&out);
1179
1180         name = client->name ? client->name : client->hostname;
1181
1182         if (!client->skip_newline && !(output_format & FIO_OUTPUT_TERSE))
1183                 __log_buf(&out, "<%s> ", name);
1184         __log_buf(&out, "%s", buf);
1185         log_info_buf(out.buf, out.buflen);
1186         buf_output_free(&out);
1187         client->skip_newline = strchr(buf, '\n') == NULL;
1188 }
1189
1190 static void convert_agg(struct disk_util_agg *agg)
1191 {
1192         int i;
1193
1194         for (i = 0; i < 2; i++) {
1195                 agg->ios[i]     = le64_to_cpu(agg->ios[i]);
1196                 agg->merges[i]  = le64_to_cpu(agg->merges[i]);
1197                 agg->sectors[i] = le64_to_cpu(agg->sectors[i]);
1198                 agg->ticks[i]   = le64_to_cpu(agg->ticks[i]);
1199         }
1200
1201         agg->io_ticks           = le64_to_cpu(agg->io_ticks);
1202         agg->time_in_queue      = le64_to_cpu(agg->time_in_queue);
1203         agg->slavecount         = le32_to_cpu(agg->slavecount);
1204         agg->max_util.u.f       = fio_uint64_to_double(le64_to_cpu(agg->max_util.u.i));
1205 }
1206
1207 static void convert_dus(struct disk_util_stat *dus)
1208 {
1209         int i;
1210
1211         for (i = 0; i < 2; i++) {
1212                 dus->s.ios[i]           = le64_to_cpu(dus->s.ios[i]);
1213                 dus->s.merges[i]        = le64_to_cpu(dus->s.merges[i]);
1214                 dus->s.sectors[i]       = le64_to_cpu(dus->s.sectors[i]);
1215                 dus->s.ticks[i]         = le64_to_cpu(dus->s.ticks[i]);
1216         }
1217
1218         dus->s.io_ticks         = le64_to_cpu(dus->s.io_ticks);
1219         dus->s.time_in_queue    = le64_to_cpu(dus->s.time_in_queue);
1220         dus->s.msec             = le64_to_cpu(dus->s.msec);
1221 }
1222
1223 static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd)
1224 {
1225         struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
1226
1227         if (!client->disk_stats_shown)
1228                 client->disk_stats_shown = true;
1229
1230         if (output_format & FIO_OUTPUT_JSON) {
1231                 struct json_object *duobj;
1232
1233                 json_array_add_disk_util(&du->dus, &du->agg, du_array);
1234                 duobj = json_array_last_value_object(du_array);
1235                 json_object_add_client_info(duobj, client);
1236         }
1237         if (output_format & FIO_OUTPUT_NORMAL) {
1238                 __log_buf(&client->buf, "\nDisk stats (read/write):\n");
1239                 print_disk_util(&du->dus, &du->agg, 0, &client->buf);
1240         }
1241         if (output_format & FIO_OUTPUT_TERSE && terse_version >= 3) {
1242                 print_disk_util(&du->dus, &du->agg, 1, &client->buf);
1243                 __log_buf(&client->buf, "\n");
1244         }
1245 }
1246
1247 static void convert_jobs_eta(struct jobs_eta *je)
1248 {
1249         int i;
1250
1251         je->nr_running          = le32_to_cpu(je->nr_running);
1252         je->nr_ramp             = le32_to_cpu(je->nr_ramp);
1253         je->nr_pending          = le32_to_cpu(je->nr_pending);
1254         je->nr_setting_up       = le32_to_cpu(je->nr_setting_up);
1255         je->files_open          = le32_to_cpu(je->files_open);
1256
1257         for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1258                 je->m_rate[i]   = le64_to_cpu(je->m_rate[i]);
1259                 je->t_rate[i]   = le64_to_cpu(je->t_rate[i]);
1260                 je->m_iops[i]   = le32_to_cpu(je->m_iops[i]);
1261                 je->t_iops[i]   = le32_to_cpu(je->t_iops[i]);
1262                 je->rate[i]     = le64_to_cpu(je->rate[i]);
1263                 je->iops[i]     = le32_to_cpu(je->iops[i]);
1264         }
1265
1266         je->elapsed_sec         = le64_to_cpu(je->elapsed_sec);
1267         je->eta_sec             = le64_to_cpu(je->eta_sec);
1268         je->nr_threads          = le32_to_cpu(je->nr_threads);
1269         je->is_pow2             = le32_to_cpu(je->is_pow2);
1270         je->unit_base           = le32_to_cpu(je->unit_base);
1271         je->sig_figs            = le32_to_cpu(je->sig_figs);
1272 }
1273
1274 void fio_client_sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je)
1275 {
1276         int i;
1277
1278         dst->nr_running         += je->nr_running;
1279         dst->nr_ramp            += je->nr_ramp;
1280         dst->nr_pending         += je->nr_pending;
1281         dst->nr_setting_up      += je->nr_setting_up;
1282         dst->files_open         += je->files_open;
1283
1284         for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1285                 dst->m_rate[i]  += je->m_rate[i];
1286                 dst->t_rate[i]  += je->t_rate[i];
1287                 dst->m_iops[i]  += je->m_iops[i];
1288                 dst->t_iops[i]  += je->t_iops[i];
1289                 dst->rate[i]    += je->rate[i];
1290                 dst->iops[i]    += je->iops[i];
1291         }
1292
1293         dst->elapsed_sec        += je->elapsed_sec;
1294
1295         if (je->eta_sec > dst->eta_sec)
1296                 dst->eta_sec = je->eta_sec;
1297
1298         dst->nr_threads         += je->nr_threads;
1299
1300         /*
1301          * This wont be correct for multiple strings, but at least it
1302          * works for the basic cases.
1303          */
1304         strcpy((char *) dst->run_str, (char *) je->run_str);
1305 }
1306
1307 static bool remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd)
1308 {
1309         struct fio_net_cmd_reply *reply = NULL;
1310         struct flist_head *entry;
1311
1312         flist_for_each(entry, &client->cmd_list) {
1313                 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
1314
1315                 if (cmd->tag == (uintptr_t) reply)
1316                         break;
1317
1318                 reply = NULL;
1319         }
1320
1321         if (!reply) {
1322                 log_err("fio: client: unable to find matching tag (%llx)\n", (unsigned long long) cmd->tag);
1323                 return false;
1324         }
1325
1326         flist_del(&reply->list);
1327         cmd->tag = reply->saved_tag;
1328         free(reply);
1329         return true;
1330 }
1331
1332 int fio_client_wait_for_reply(struct fio_client *client, uint64_t tag)
1333 {
1334         do {
1335                 struct fio_net_cmd_reply *reply = NULL;
1336                 struct flist_head *entry;
1337
1338                 flist_for_each(entry, &client->cmd_list) {
1339                         reply = flist_entry(entry, struct fio_net_cmd_reply, list);
1340
1341                         if (tag == (uintptr_t) reply)
1342                                 break;
1343
1344                         reply = NULL;
1345                 }
1346
1347                 if (!reply)
1348                         break;
1349
1350                 usleep(1000);
1351         } while (1);
1352
1353         return 0;
1354 }
1355
1356 static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd)
1357 {
1358         struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
1359         struct client_eta *eta = (struct client_eta *) (uintptr_t) cmd->tag;
1360
1361         dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending);
1362
1363         assert(client->eta_in_flight == eta);
1364
1365         client->eta_in_flight = NULL;
1366         flist_del_init(&client->eta_list);
1367         client->eta_timeouts = 0;
1368
1369         if (client->ops->jobs_eta)
1370                 client->ops->jobs_eta(client, je);
1371
1372         fio_client_sum_jobs_eta(&eta->eta, je);
1373         fio_client_dec_jobs_eta(eta, client->ops->eta);
1374 }
1375
1376 static void client_flush_hist_samples(FILE *f, int hist_coarseness, void *samples,
1377                                       uint64_t sample_size)
1378 {
1379         struct io_sample *s;
1380         int log_offset;
1381         uint64_t i, j, nr_samples;
1382         struct io_u_plat_entry *entry;
1383         uint64_t *io_u_plat;
1384
1385         int stride = 1 << hist_coarseness;
1386
1387         if (!sample_size)
1388                 return;
1389
1390         s = __get_sample(samples, 0, 0);
1391         log_offset = (s->__ddir & LOG_OFFSET_SAMPLE_BIT) != 0;
1392
1393         nr_samples = sample_size / __log_entry_sz(log_offset);
1394
1395         for (i = 0; i < nr_samples; i++) {
1396
1397                 s = (struct io_sample *)((char *)__get_sample(samples, log_offset, i) +
1398                         i * sizeof(struct io_u_plat_entry));
1399
1400                 entry = s->data.plat_entry;
1401                 io_u_plat = entry->io_u_plat;
1402
1403                 fprintf(f, "%lu, %u, %llu, ", (unsigned long) s->time,
1404                                                 io_sample_ddir(s), (unsigned long long) s->bs);
1405                 for (j = 0; j < FIO_IO_U_PLAT_NR - stride; j += stride) {
1406                         fprintf(f, "%llu, ", (unsigned long long)hist_sum(j, stride, io_u_plat, NULL));
1407                 }
1408                 fprintf(f, "%llu\n", (unsigned long long)
1409                         hist_sum(FIO_IO_U_PLAT_NR - stride, stride, io_u_plat, NULL));
1410
1411         }
1412 }
1413
1414 static int fio_client_handle_iolog(struct fio_client *client,
1415                                    struct fio_net_cmd *cmd)
1416 {
1417         struct cmd_iolog_pdu *pdu = NULL;
1418         bool store_direct;
1419         char *log_pathname = NULL;
1420         int ret = 0;
1421
1422         pdu = convert_iolog(cmd, &store_direct);
1423         if (!pdu) {
1424                 log_err("fio: failed converting IO log\n");
1425                 ret = 1;
1426                 goto out;
1427         }
1428
1429         /* allocate buffer big enough for next sprintf() call */
1430         log_pathname = malloc(10 + strlen((char *)pdu->name) +
1431                         strlen(client->hostname));
1432         if (!log_pathname) {
1433                 log_err("fio: memory allocation of unique pathname failed\n");
1434                 ret = -1;
1435                 goto out;
1436         }
1437         /* generate a unique pathname for the log file using hostname */
1438         sprintf(log_pathname, "%s.%s", pdu->name, client->hostname);
1439
1440         if (store_direct) {
1441                 ssize_t wrote;
1442                 size_t sz;
1443                 int fd;
1444
1445                 fd = open((const char *) log_pathname,
1446                                 O_WRONLY | O_CREAT | O_TRUNC, 0644);
1447                 if (fd < 0) {
1448                         log_err("fio: open log %s: %s\n",
1449                                 log_pathname, strerror(errno));
1450                         ret = 1;
1451                         goto out;
1452                 }
1453
1454                 sz = cmd->pdu_len - sizeof(*pdu);
1455                 wrote = write(fd, pdu->samples, sz);
1456                 close(fd);
1457
1458                 if (wrote != sz) {
1459                         log_err("fio: short write on compressed log\n");
1460                         ret = 1;
1461                         goto out;
1462                 }
1463
1464                 ret = 0;
1465         } else {
1466                 FILE *f;
1467                 f = fopen((const char *) log_pathname, "w");
1468                 if (!f) {
1469                         log_err("fio: fopen log %s : %s\n",
1470                                 log_pathname, strerror(errno));
1471                         ret = 1;
1472                         goto out;
1473                 }
1474
1475                 if (pdu->log_type == IO_LOG_TYPE_HIST) {
1476                         client_flush_hist_samples(f, pdu->log_hist_coarseness, pdu->samples,
1477                                            pdu->nr_samples * sizeof(struct io_sample));
1478                 } else {
1479                         flush_samples(f, pdu->samples,
1480                                         pdu->nr_samples * sizeof(struct io_sample));
1481                 }
1482                 fclose(f);
1483                 ret = 0;
1484         }
1485
1486 out:
1487         if (pdu && pdu != (void *) cmd->payload)
1488                 free(pdu);
1489
1490         if (log_pathname)
1491                 free(log_pathname);
1492
1493         return ret;
1494 }
1495
1496 static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
1497 {
1498         struct cmd_probe_reply_pdu *probe = (struct cmd_probe_reply_pdu *) cmd->payload;
1499         const char *os, *arch;
1500         char bit[16];
1501
1502         os = fio_get_os_string(probe->os);
1503         if (!os)
1504                 os = "unknown";
1505
1506         arch = fio_get_arch_string(probe->arch);
1507         if (!arch)
1508                 os = "unknown";
1509
1510         sprintf(bit, "%d-bit", probe->bpp * 8);
1511         probe->flags = le64_to_cpu(probe->flags);
1512
1513         if (output_format & FIO_OUTPUT_NORMAL) {
1514                 log_info("hostname=%s, be=%u, %s, os=%s, arch=%s, fio=%s, flags=%lx\n",
1515                         probe->hostname, probe->bigendian, bit, os, arch,
1516                         probe->fio_version, (unsigned long) probe->flags);
1517         }
1518
1519         if (!client->name)
1520                 client->name = strdup((char *) probe->hostname);
1521 }
1522
1523 static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd)
1524 {
1525         struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1526
1527         client->state = Client_started;
1528         client->jobs = le32_to_cpu(pdu->jobs);
1529         client->nr_stat = le32_to_cpu(pdu->stat_outputs);
1530
1531         if (client->jobs) {
1532                 int i;
1533
1534                 if (client->opt_lists)
1535                         free(client->opt_lists);
1536
1537                 client->opt_lists = malloc(client->jobs * sizeof(struct flist_head));
1538                 for (i = 0; i < client->jobs; i++)
1539                         INIT_FLIST_HEAD(&client->opt_lists[i]);
1540         }
1541
1542         sum_stat_clients += client->nr_stat;
1543 }
1544
1545 static void handle_stop(struct fio_client *client)
1546 {
1547         if (client->error)
1548                 log_info("client <%s>: exited with error %d\n", client->hostname, client->error);
1549 }
1550
1551 static void convert_stop(struct fio_net_cmd *cmd)
1552 {
1553         struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1554
1555         pdu->error = le32_to_cpu(pdu->error);
1556 }
1557
1558 static void convert_text(struct fio_net_cmd *cmd)
1559 {
1560         struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
1561
1562         pdu->level      = le32_to_cpu(pdu->level);
1563         pdu->buf_len    = le32_to_cpu(pdu->buf_len);
1564         pdu->log_sec    = le64_to_cpu(pdu->log_sec);
1565         pdu->log_usec   = le64_to_cpu(pdu->log_usec);
1566 }
1567
1568 static struct cmd_iolog_pdu *convert_iolog_gz(struct fio_net_cmd *cmd,
1569                                               struct cmd_iolog_pdu *pdu)
1570 {
1571 #ifdef CONFIG_ZLIB
1572         struct cmd_iolog_pdu *ret;
1573         z_stream stream;
1574         uint64_t nr_samples;
1575         size_t total;
1576         char *p;
1577
1578         stream.zalloc = Z_NULL;
1579         stream.zfree = Z_NULL;
1580         stream.opaque = Z_NULL;
1581         stream.avail_in = 0;
1582         stream.next_in = Z_NULL;
1583
1584         if (inflateInit(&stream) != Z_OK)
1585                 return NULL;
1586
1587         /*
1588          * Get header first, it's not compressed
1589          */
1590         nr_samples = le64_to_cpu(pdu->nr_samples);
1591
1592         if (pdu->log_type == IO_LOG_TYPE_HIST)
1593                 total = nr_samples * (__log_entry_sz(le32_to_cpu(pdu->log_offset)) +
1594                                         sizeof(struct io_u_plat_entry));
1595         else
1596                 total = nr_samples * __log_entry_sz(le32_to_cpu(pdu->log_offset));
1597         ret = malloc(total + sizeof(*pdu));
1598         ret->nr_samples = nr_samples;
1599
1600         memcpy(ret, pdu, sizeof(*pdu));
1601
1602         p = (char *) ret + sizeof(*pdu);
1603
1604         stream.avail_in = cmd->pdu_len - sizeof(*pdu);
1605         stream.next_in = (void *)((char *) pdu + sizeof(*pdu));
1606         while (stream.avail_in) {
1607                 unsigned int this_chunk = 65536;
1608                 unsigned int this_len;
1609                 int err;
1610
1611                 if (this_chunk > total)
1612                         this_chunk = total;
1613
1614                 stream.avail_out = this_chunk;
1615                 stream.next_out = (void *)p;
1616                 err = inflate(&stream, Z_NO_FLUSH);
1617                 /* may be Z_OK, or Z_STREAM_END */
1618                 if (err < 0) {
1619                         /*
1620                          * Z_STREAM_ERROR and Z_BUF_ERROR can safely be
1621                          * ignored */
1622                         if (err == Z_STREAM_ERROR || err == Z_BUF_ERROR)
1623                                 break;
1624                         log_err("fio: inflate error %d\n", err);
1625                         free(ret);
1626                         ret = NULL;
1627                         goto err;
1628                 }
1629
1630                 this_len = this_chunk - stream.avail_out;
1631                 p += this_len;
1632                 total -= this_len;
1633         }
1634
1635 err:
1636         inflateEnd(&stream);
1637         return ret;
1638 #else
1639         return NULL;
1640 #endif
1641 }
1642
1643 /*
1644  * This has been compressed on the server side, since it can be big.
1645  * Uncompress here.
1646  */
1647 static struct cmd_iolog_pdu *convert_iolog(struct fio_net_cmd *cmd,
1648                                            bool *store_direct)
1649 {
1650         struct cmd_iolog_pdu *pdu = (struct cmd_iolog_pdu *) cmd->payload;
1651         struct cmd_iolog_pdu *ret;
1652         uint64_t i;
1653         int compressed;
1654         void *samples;
1655
1656         *store_direct = false;
1657
1658         /*
1659          * Convert if compressed and we support it. If it's not
1660          * compressed, we need not do anything.
1661          */
1662         compressed = le32_to_cpu(pdu->compressed);
1663         if (compressed == XMIT_COMPRESSED) {
1664 #ifndef CONFIG_ZLIB
1665                 log_err("fio: server sent compressed data by mistake\n");
1666                 return NULL;
1667 #endif
1668                 ret = convert_iolog_gz(cmd, pdu);
1669                 if (!ret) {
1670                         log_err("fio: failed decompressing log\n");
1671                         return NULL;
1672                 }
1673         } else if (compressed == STORE_COMPRESSED) {
1674                 *store_direct = true;
1675                 ret = pdu;
1676         } else
1677                 ret = pdu;
1678
1679         ret->nr_samples         = le64_to_cpu(ret->nr_samples);
1680         ret->thread_number      = le32_to_cpu(ret->thread_number);
1681         ret->log_type           = le32_to_cpu(ret->log_type);
1682         ret->compressed         = le32_to_cpu(ret->compressed);
1683         ret->log_offset         = le32_to_cpu(ret->log_offset);
1684         ret->log_hist_coarseness = le32_to_cpu(ret->log_hist_coarseness);
1685
1686         if (*store_direct)
1687                 return ret;
1688
1689         samples = &ret->samples[0];
1690         for (i = 0; i < ret->nr_samples; i++) {
1691                 struct io_sample *s;
1692
1693                 s = __get_sample(samples, ret->log_offset, i);
1694                 if (ret->log_type == IO_LOG_TYPE_HIST)
1695                         s = (struct io_sample *)((char *)s + sizeof(struct io_u_plat_entry) * i);
1696
1697                 s->time         = le64_to_cpu(s->time);
1698                 s->data.val     = le64_to_cpu(s->data.val);
1699                 s->__ddir       = __le32_to_cpu(s->__ddir);
1700                 s->bs           = le64_to_cpu(s->bs);
1701
1702                 if (ret->log_offset) {
1703                         struct io_sample_offset *so = (void *) s;
1704
1705                         so->offset = le64_to_cpu(so->offset);
1706                 }
1707
1708                 if (ret->log_type == IO_LOG_TYPE_HIST) {
1709                         s->data.plat_entry = (struct io_u_plat_entry *)(((char *)s) + sizeof(*s));
1710                         s->data.plat_entry->list.next = NULL;
1711                         s->data.plat_entry->list.prev = NULL;
1712                 }
1713         }
1714
1715         return ret;
1716 }
1717
1718 static void sendfile_reply(int fd, struct cmd_sendfile_reply *rep,
1719                            size_t size, uint64_t tag)
1720 {
1721         rep->error = cpu_to_le32(rep->error);
1722         fio_net_send_cmd(fd, FIO_NET_CMD_SENDFILE, rep, size, &tag, NULL);
1723 }
1724
1725 static int fio_send_file(struct fio_client *client, struct cmd_sendfile *pdu,
1726                          uint64_t tag)
1727 {
1728         struct cmd_sendfile_reply *rep;
1729         struct stat sb;
1730         size_t size;
1731         int fd;
1732
1733         size = sizeof(*rep);
1734         rep = malloc(size);
1735
1736         if (stat((char *)pdu->path, &sb) < 0) {
1737 fail:
1738                 rep->error = errno;
1739                 sendfile_reply(client->fd, rep, size, tag);
1740                 free(rep);
1741                 return 1;
1742         }
1743
1744         size += sb.st_size;
1745         rep = realloc(rep, size);
1746         rep->size = cpu_to_le32((uint32_t) sb.st_size);
1747
1748         fd = open((char *)pdu->path, O_RDONLY);
1749         if (fd == -1 )
1750                 goto fail;
1751
1752         rep->error = read_data(fd, &rep->data, sb.st_size);
1753         sendfile_reply(client->fd, rep, size, tag);
1754         free(rep);
1755         close(fd);
1756         return 0;
1757 }
1758
1759 int fio_handle_client(struct fio_client *client)
1760 {
1761         struct client_ops *ops = client->ops;
1762         struct fio_net_cmd *cmd;
1763         int size;
1764
1765         dprint(FD_NET, "client: handle %s\n", client->hostname);
1766
1767         cmd = fio_net_recv_cmd(client->fd, true);
1768         if (!cmd)
1769                 return 0;
1770
1771         dprint(FD_NET, "client: got cmd op %s from %s (pdu=%u)\n",
1772                 fio_server_op(cmd->opcode), client->hostname, cmd->pdu_len);
1773
1774         client->last_cmd = cmd->opcode;
1775
1776         switch (cmd->opcode) {
1777         case FIO_NET_CMD_QUIT:
1778                 if (ops->quit)
1779                         ops->quit(client, cmd);
1780                 remove_client(client);
1781                 break;
1782         case FIO_NET_CMD_TEXT:
1783                 convert_text(cmd);
1784                 ops->text(client, cmd);
1785                 break;
1786         case FIO_NET_CMD_DU: {
1787                 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
1788
1789                 convert_dus(&du->dus);
1790                 convert_agg(&du->agg);
1791
1792                 ops->disk_util(client, cmd);
1793                 break;
1794                 }
1795         case FIO_NET_CMD_TS: {
1796                 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
1797
1798                 dprint(FD_NET, "client: ts->ss_state = %u\n", (unsigned int) le32_to_cpu(p->ts.ss_state));
1799                 if (le32_to_cpu(p->ts.ss_state) & FIO_SS_DATA) {
1800                         dprint(FD_NET, "client: received steadystate ring buffers\n");
1801
1802                         size = le64_to_cpu(p->ts.ss_dur);
1803                         p->ts.ss_iops_data = (uint64_t *) ((struct cmd_ts_pdu *)cmd->payload + 1);
1804                         p->ts.ss_bw_data = p->ts.ss_iops_data + size;
1805                 }
1806
1807                 convert_ts(&p->ts, &p->ts);
1808                 convert_gs(&p->rs, &p->rs);
1809
1810                 ops->thread_status(client, cmd);
1811                 break;
1812                 }
1813         case FIO_NET_CMD_GS: {
1814                 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
1815
1816                 convert_gs(gs, gs);
1817
1818                 ops->group_stats(client, cmd);
1819                 break;
1820                 }
1821         case FIO_NET_CMD_ETA: {
1822                 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
1823
1824                 if (!remove_reply_cmd(client, cmd))
1825                         break;
1826                 convert_jobs_eta(je);
1827                 handle_eta(client, cmd);
1828                 break;
1829                 }
1830         case FIO_NET_CMD_PROBE:
1831                 remove_reply_cmd(client, cmd);
1832                 ops->probe(client, cmd);
1833                 break;
1834         case FIO_NET_CMD_SERVER_START:
1835                 client->state = Client_running;
1836                 if (ops->job_start)
1837                         ops->job_start(client, cmd);
1838                 break;
1839         case FIO_NET_CMD_START: {
1840                 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1841
1842                 pdu->jobs = le32_to_cpu(pdu->jobs);
1843                 ops->start(client, cmd);
1844                 break;
1845                 }
1846         case FIO_NET_CMD_STOP: {
1847                 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1848
1849                 convert_stop(cmd);
1850                 client->state = Client_stopped;
1851                 client->error = le32_to_cpu(pdu->error);
1852                 client->signal = le32_to_cpu(pdu->signal);
1853                 ops->stop(client);
1854                 break;
1855                 }
1856         case FIO_NET_CMD_ADD_JOB: {
1857                 struct cmd_add_job_pdu *pdu = (struct cmd_add_job_pdu *) cmd->payload;
1858
1859                 client->thread_number = le32_to_cpu(pdu->thread_number);
1860                 client->groupid = le32_to_cpu(pdu->groupid);
1861
1862                 if (ops->add_job)
1863                         ops->add_job(client, cmd);
1864                 break;
1865                 }
1866         case FIO_NET_CMD_IOLOG:
1867                 fio_client_handle_iolog(client, cmd);
1868                 break;
1869         case FIO_NET_CMD_UPDATE_JOB:
1870                 ops->update_job(client, cmd);
1871                 remove_reply_cmd(client, cmd);
1872                 break;
1873         case FIO_NET_CMD_VTRIGGER: {
1874                 struct all_io_list *pdu = (struct all_io_list *) cmd->payload;
1875                 char buf[128];
1876                 int off = 0;
1877
1878                 if (aux_path) {
1879                         strcpy(buf, aux_path);
1880                         off = strlen(buf);
1881                 }
1882
1883                 __verify_save_state(pdu, server_name(client, &buf[off], sizeof(buf) - off));
1884                 exec_trigger(trigger_cmd);
1885                 break;
1886                 }
1887         case FIO_NET_CMD_SENDFILE: {
1888                 struct cmd_sendfile *pdu = (struct cmd_sendfile *) cmd->payload;
1889                 fio_send_file(client, pdu, cmd->tag);
1890                 break;
1891                 }
1892         case FIO_NET_CMD_JOB_OPT: {
1893                 handle_job_opt(client, cmd);
1894                 break;
1895         }
1896         default:
1897                 log_err("fio: unknown client op: %s\n", fio_server_op(cmd->opcode));
1898                 break;
1899         }
1900
1901         free(cmd);
1902         return 1;
1903 }
1904
1905 int fio_clients_send_trigger(const char *cmd)
1906 {
1907         struct flist_head *entry;
1908         struct fio_client *client;
1909         size_t slen;
1910
1911         dprint(FD_NET, "client: send vtrigger: %s\n", cmd);
1912
1913         if (!cmd)
1914                 slen = 0;
1915         else
1916                 slen = strlen(cmd);
1917
1918         flist_for_each(entry, &client_list) {
1919                 struct cmd_vtrigger_pdu *pdu;
1920
1921                 client = flist_entry(entry, struct fio_client, list);
1922
1923                 pdu = malloc(sizeof(*pdu) + slen);
1924                 pdu->len = cpu_to_le16((uint16_t) slen);
1925                 if (slen)
1926                         memcpy(pdu->cmd, cmd, slen);
1927                 fio_net_send_cmd(client->fd, FIO_NET_CMD_VTRIGGER, pdu,
1928                                         sizeof(*pdu) + slen, NULL, NULL);
1929                 free(pdu);
1930         }
1931
1932         return 0;
1933 }
1934
1935 static void request_client_etas(struct client_ops *ops)
1936 {
1937         struct fio_client *client;
1938         struct flist_head *entry;
1939         struct client_eta *eta;
1940         int skipped = 0;
1941
1942         if (eta_print == FIO_ETA_NEVER)
1943                 return;
1944
1945         dprint(FD_NET, "client: request eta (%d)\n", nr_clients);
1946
1947         eta = calloc(1, sizeof(*eta) + __THREAD_RUNSTR_SZ(REAL_MAX_JOBS));
1948         eta->pending = nr_clients;
1949
1950         flist_for_each(entry, &client_list) {
1951                 client = flist_entry(entry, struct fio_client, list);
1952
1953                 if (!flist_empty(&client->eta_list)) {
1954                         skipped++;
1955                         continue;
1956                 }
1957                 if (client->state != Client_running)
1958                         continue;
1959
1960                 assert(!client->eta_in_flight);
1961                 flist_add_tail(&client->eta_list, &eta_list);
1962                 client->eta_in_flight = eta;
1963                 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA,
1964                                         (uintptr_t) eta, &client->cmd_list);
1965         }
1966
1967         while (skipped--) {
1968                 if (!fio_client_dec_jobs_eta(eta, ops->eta))
1969                         break;
1970         }
1971
1972         dprint(FD_NET, "client: requested eta tag %p\n", eta);
1973 }
1974
1975 /*
1976  * A single SEND_ETA timeout isn't fatal. Attempt to recover.
1977  */
1978 static int handle_cmd_timeout(struct fio_client *client,
1979                               struct fio_net_cmd_reply *reply)
1980 {
1981         uint16_t reply_opcode = reply->opcode;
1982
1983         flist_del(&reply->list);
1984         free(reply);
1985
1986         if (reply_opcode != FIO_NET_CMD_SEND_ETA)
1987                 return 1;
1988
1989         log_info("client <%s>: timeout on SEND_ETA\n", client->hostname);
1990
1991         flist_del_init(&client->eta_list);
1992         if (client->eta_in_flight) {
1993                 fio_client_dec_jobs_eta(client->eta_in_flight, client->ops->eta);
1994                 client->eta_in_flight = NULL;
1995         }
1996
1997         /*
1998          * If we fail 5 in a row, give up...
1999          */
2000         if (client->eta_timeouts++ > 5)
2001                 return 1;
2002
2003         return 0;
2004 }
2005
2006 static int client_check_cmd_timeout(struct fio_client *client,
2007                                     struct timespec *now)
2008 {
2009         struct fio_net_cmd_reply *reply;
2010         struct flist_head *entry, *tmp;
2011         int ret = 0;
2012
2013         flist_for_each_safe(entry, tmp, &client->cmd_list) {
2014                 unsigned int op;
2015
2016                 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
2017
2018                 if (mtime_since(&reply->ts, now) < FIO_NET_CLIENT_TIMEOUT)
2019                         continue;
2020
2021                 op = reply->opcode;
2022                 if (!handle_cmd_timeout(client, reply))
2023                         continue;
2024
2025                 log_err("fio: client %s, timeout on cmd %s\n", client->hostname,
2026                                                 fio_server_op(op));
2027                 ret = 1;
2028         }
2029
2030         return flist_empty(&client->cmd_list) && ret;
2031 }
2032
2033 static int fio_check_clients_timed_out(void)
2034 {
2035         struct fio_client *client;
2036         struct flist_head *entry, *tmp;
2037         struct timespec ts;
2038         int ret = 0;
2039
2040         fio_gettime(&ts, NULL);
2041
2042         flist_for_each_safe(entry, tmp, &client_list) {
2043                 client = flist_entry(entry, struct fio_client, list);
2044
2045                 if (flist_empty(&client->cmd_list))
2046                         continue;
2047
2048                 if (!client_check_cmd_timeout(client, &ts))
2049                         continue;
2050
2051                 if (client->ops->timed_out)
2052                         client->ops->timed_out(client);
2053                 else
2054                         log_err("fio: client %s timed out\n", client->hostname);
2055
2056                 if (client->last_cmd != FIO_NET_CMD_VTRIGGER)
2057                         client->error = ETIMEDOUT;
2058                 else
2059                         log_info("fio: ignoring timeout due to vtrigger\n");
2060                 remove_client(client);
2061                 ret = 1;
2062         }
2063
2064         return ret;
2065 }
2066
2067 int fio_handle_clients(struct client_ops *ops)
2068 {
2069         struct pollfd *pfds;
2070         int i, ret = 0, retval = 0;
2071
2072         fio_gettime(&eta_ts, NULL);
2073
2074         pfds = malloc(nr_clients * sizeof(struct pollfd));
2075
2076         init_thread_stat(&client_ts);
2077         init_group_run_stat(&client_gs);
2078
2079         while (!exit_backend && nr_clients) {
2080                 struct flist_head *entry, *tmp;
2081                 struct fio_client *client;
2082
2083                 i = 0;
2084                 flist_for_each_safe(entry, tmp, &client_list) {
2085                         client = flist_entry(entry, struct fio_client, list);
2086
2087                         if (!client->sent_job && !client->ops->stay_connected &&
2088                             flist_empty(&client->cmd_list)) {
2089                                 remove_client(client);
2090                                 continue;
2091                         }
2092
2093                         pfds[i].fd = client->fd;
2094                         pfds[i].events = POLLIN;
2095                         i++;
2096                 }
2097
2098                 if (!nr_clients)
2099                         break;
2100
2101                 assert(i == nr_clients);
2102
2103                 do {
2104                         struct timespec ts;
2105                         int timeout;
2106
2107                         fio_gettime(&ts, NULL);
2108                         if (eta_time_within_slack(mtime_since(&eta_ts, &ts))) {
2109                                 request_client_etas(ops);
2110                                 memcpy(&eta_ts, &ts, sizeof(ts));
2111
2112                                 if (fio_check_clients_timed_out())
2113                                         break;
2114                         }
2115
2116                         check_trigger_file();
2117
2118                         timeout = min(100u, ops->eta_msec);
2119
2120                         ret = poll(pfds, nr_clients, timeout);
2121                         if (ret < 0) {
2122                                 if (errno == EINTR)
2123                                         continue;
2124                                 log_err("fio: poll clients: %s\n", strerror(errno));
2125                                 break;
2126                         } else if (!ret)
2127                                 continue;
2128                 } while (ret <= 0);
2129
2130                 for (i = 0; i < nr_clients; i++) {
2131                         if (!(pfds[i].revents & POLLIN))
2132                                 continue;
2133
2134                         client = find_client_by_fd(pfds[i].fd);
2135                         if (!client) {
2136                                 log_err("fio: unknown client fd %ld\n", (long) pfds[i].fd);
2137                                 continue;
2138                         }
2139                         if (!fio_handle_client(client)) {
2140                                 log_info("client: host=%s disconnected\n",
2141                                                 client->hostname);
2142                                 remove_client(client);
2143                                 retval = 1;
2144                         } else if (client->error)
2145                                 retval = 1;
2146                         fio_put_client(client);
2147                 }
2148         }
2149
2150         log_info_buf(allclients.buf, allclients.buflen);
2151         buf_output_free(&allclients);
2152
2153         fio_client_json_fini();
2154
2155         free(pfds);
2156         return retval || error_clients;
2157 }
2158
2159 static void client_display_thread_status(struct jobs_eta *je)
2160 {
2161         if (!(output_format & FIO_OUTPUT_JSON))
2162                 display_thread_status(je);
2163 }