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