net: fix compile warning on Windows (and others)
[fio.git] / client.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <limits.h>
5 #include <errno.h>
6 #include <fcntl.h>
7 #include <sys/poll.h>
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <sys/wait.h>
11 #include <sys/socket.h>
12 #include <sys/un.h>
13 #include <netinet/in.h>
14 #include <arpa/inet.h>
15 #include <netdb.h>
16 #include <signal.h>
17
18 #include "fio.h"
19 #include "server.h"
20 #include "flist.h"
21 #include "hash.h"
22
23 struct client_eta {
24         unsigned int pending;
25         struct jobs_eta eta;
26 };
27
28 struct fio_client {
29         struct flist_head list;
30         struct flist_head hash_list;
31         struct flist_head arg_list;
32         union {
33                 struct sockaddr_in addr;
34                 struct sockaddr_in6 addr6;
35                 struct sockaddr_un addr_un;
36         };
37         char *hostname;
38         int port;
39         int fd;
40         unsigned int refs;
41
42         char *name;
43
44         int state;
45
46         int skip_newline;
47         int is_sock;
48         int disk_stats_shown;
49         unsigned int jobs;
50         unsigned int nr_stat;
51         int error;
52         int ipv6;
53         int sent_job;
54         int did_stat;
55
56         struct flist_head eta_list;
57         struct client_eta *eta_in_flight;
58
59         struct flist_head cmd_list;
60
61         uint16_t argc;
62         char **argv;
63
64         char **ini_file;
65         unsigned int nr_ini_file;
66 };
67
68 static struct timeval eta_tv;
69
70 enum {
71         Client_created          = 0,
72         Client_connected        = 1,
73         Client_started          = 2,
74         Client_running          = 3,
75         Client_stopped          = 4,
76         Client_exited           = 5,
77 };
78
79 static FLIST_HEAD(client_list);
80 static FLIST_HEAD(eta_list);
81
82 static FLIST_HEAD(arg_list);
83
84 static struct thread_stat client_ts;
85 static struct group_run_stats client_gs;
86 static int sum_stat_clients = 0;
87 static int sum_stat_nr;
88 static int do_output_all_clients;
89
90 #define FIO_CLIENT_HASH_BITS    7
91 #define FIO_CLIENT_HASH_SZ      (1 << FIO_CLIENT_HASH_BITS)
92 #define FIO_CLIENT_HASH_MASK    (FIO_CLIENT_HASH_SZ - 1)
93 static struct flist_head client_hash[FIO_CLIENT_HASH_SZ];
94
95 static int handle_client(struct fio_client *client);
96 static void dec_jobs_eta(struct client_eta *eta);
97
98 static void fio_client_add_hash(struct fio_client *client)
99 {
100         int bucket = hash_long(client->fd, FIO_CLIENT_HASH_BITS);
101
102         bucket &= FIO_CLIENT_HASH_MASK;
103         flist_add(&client->hash_list, &client_hash[bucket]);
104 }
105
106 static void fio_client_remove_hash(struct fio_client *client)
107 {
108         if (!flist_empty(&client->hash_list))
109                 flist_del_init(&client->hash_list);
110 }
111
112 static void fio_init fio_client_hash_init(void)
113 {
114         int i;
115
116         for (i = 0; i < FIO_CLIENT_HASH_SZ; i++)
117                 INIT_FLIST_HEAD(&client_hash[i]);
118 }
119
120 static struct fio_client *find_client_by_fd(int fd)
121 {
122         int bucket = hash_long(fd, FIO_CLIENT_HASH_BITS) & FIO_CLIENT_HASH_MASK;
123         struct fio_client *client;
124         struct flist_head *entry;
125
126         flist_for_each(entry, &client_hash[bucket]) {
127                 client = flist_entry(entry, struct fio_client, hash_list);
128
129                 if (client->fd == fd) {
130                         client->refs++;
131                         return client;
132                 }
133         }
134
135         return NULL;
136 }
137
138 static void remove_client(struct fio_client *client)
139 {
140         assert(client->refs);
141
142         if (--client->refs)
143                 return;
144
145         dprint(FD_NET, "client: removed <%s>\n", client->hostname);
146         flist_del(&client->list);
147
148         fio_client_remove_hash(client);
149
150         if (!flist_empty(&client->eta_list)) {
151                 flist_del_init(&client->eta_list);
152                 dec_jobs_eta(client->eta_in_flight);
153         }
154
155         free(client->hostname);
156         if (client->argv)
157                 free(client->argv);
158         if (client->name)
159                 free(client->name);
160         while (client->nr_ini_file)
161                 free(client->ini_file[--client->nr_ini_file]);
162         if (client->ini_file)
163                 free(client->ini_file);
164
165         if (!client->did_stat)
166                 sum_stat_clients -= client->nr_stat;
167
168         free(client);
169         nr_clients--;
170 }
171
172 static void put_client(struct fio_client *client)
173 {
174         remove_client(client);
175 }
176
177 static void __fio_client_add_cmd_option(struct fio_client *client,
178                                         const char *opt)
179 {
180         int index;
181
182         index = client->argc++;
183         client->argv = realloc(client->argv, sizeof(char *) * client->argc);
184         client->argv[index] = strdup(opt);
185         dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
186 }
187
188 void fio_client_add_cmd_option(void *cookie, const char *opt)
189 {
190         struct fio_client *client = cookie;
191         struct flist_head *entry;
192
193         if (!client || !opt)
194                 return;
195
196         __fio_client_add_cmd_option(client, opt);
197
198         /*
199          * Duplicate arguments to shared client group
200          */
201         flist_for_each(entry, &arg_list) {
202                 client = flist_entry(entry, struct fio_client, arg_list);
203
204                 __fio_client_add_cmd_option(client, opt);
205         }
206 }
207
208 void fio_client_add_ini_file(void *cookie, const char *ini_file)
209 {
210         struct fio_client *client = cookie;
211         size_t new_size;
212
213         dprint(FD_NET, "client <%s>: add ini %s\n", client->hostname, ini_file);
214
215         new_size = (client->nr_ini_file + 1) * sizeof(char *);
216         client->ini_file = realloc(client->ini_file, new_size);
217         client->ini_file[client->nr_ini_file] = strdup(ini_file);
218         client->nr_ini_file++;
219 }
220
221 int fio_client_add(const char *hostname, void **cookie)
222 {
223         struct fio_client *existing = *cookie;
224         struct fio_client *client;
225
226         if (existing) {
227                 /*
228                  * We always add our "exec" name as the option, hence 1
229                  * means empty.
230                  */
231                 if (existing->argc == 1)
232                         flist_add_tail(&existing->arg_list, &arg_list);
233                 else {
234                         while (!flist_empty(&arg_list))
235                                 flist_del_init(arg_list.next);
236                 }
237         }
238
239         client = malloc(sizeof(*client));
240         memset(client, 0, sizeof(*client));
241
242         INIT_FLIST_HEAD(&client->list);
243         INIT_FLIST_HEAD(&client->hash_list);
244         INIT_FLIST_HEAD(&client->arg_list);
245         INIT_FLIST_HEAD(&client->eta_list);
246         INIT_FLIST_HEAD(&client->cmd_list);
247
248         if (fio_server_parse_string(hostname, &client->hostname,
249                                         &client->is_sock, &client->port,
250                                         &client->addr.sin_addr,
251                                         &client->addr6.sin6_addr,
252                                         &client->ipv6))
253                 return -1;
254
255         client->fd = -1;
256         client->refs = 1;
257
258         __fio_client_add_cmd_option(client, "fio");
259
260         flist_add(&client->list, &client_list);
261         nr_clients++;
262         dprint(FD_NET, "client: added <%s>\n", client->hostname);
263         *cookie = client;
264         return 0;
265 }
266
267 static int fio_client_connect_ip(struct fio_client *client)
268 {
269         struct sockaddr *addr;
270         socklen_t socklen;
271         int fd, domain;
272
273         if (client->ipv6) {
274                 client->addr6.sin6_family = AF_INET6;
275                 client->addr6.sin6_port = htons(client->port);
276                 domain = AF_INET6;
277                 addr = (struct sockaddr *) &client->addr6;
278                 socklen = sizeof(client->addr6);
279         } else {
280                 client->addr.sin_family = AF_INET;
281                 client->addr.sin_port = htons(client->port);
282                 domain = AF_INET;
283                 addr = (struct sockaddr *) &client->addr;
284                 socklen = sizeof(client->addr);
285         }
286
287         fd = socket(domain, SOCK_STREAM, 0);
288         if (fd < 0) {
289                 log_err("fio: socket: %s\n", strerror(errno));
290                 return -1;
291         }
292
293         if (connect(fd, addr, socklen) < 0) {
294                 log_err("fio: connect: %s\n", strerror(errno));
295                 log_err("fio: failed to connect to %s:%u\n", client->hostname,
296                                                                 client->port);
297                 close(fd);
298                 return -1;
299         }
300
301         return fd;
302 }
303
304 static int fio_client_connect_sock(struct fio_client *client)
305 {
306         struct sockaddr_un *addr = &client->addr_un;
307         socklen_t len;
308         int fd;
309
310         memset(addr, 0, sizeof(*addr));
311         addr->sun_family = AF_UNIX;
312         strcpy(addr->sun_path, client->hostname);
313
314         fd = socket(AF_UNIX, SOCK_STREAM, 0);
315         if (fd < 0) {
316                 log_err("fio: socket: %s\n", strerror(errno));
317                 return -1;
318         }
319
320         len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
321         if (connect(fd, (struct sockaddr *) addr, len) < 0) {
322                 log_err("fio: connect; %s\n", strerror(errno));
323                 close(fd);
324                 return -1;
325         }
326
327         return fd;
328 }
329
330 static int fio_client_connect(struct fio_client *client)
331 {
332         int fd;
333
334         dprint(FD_NET, "client: connect to host %s\n", client->hostname);
335
336         if (client->is_sock)
337                 fd = fio_client_connect_sock(client);
338         else
339                 fd = fio_client_connect_ip(client);
340
341         dprint(FD_NET, "client: %s connected %d\n", client->hostname, fd);
342
343         if (fd < 0)
344                 return 1;
345
346         client->fd = fd;
347         fio_client_add_hash(client);
348         client->state = Client_connected;
349         return 0;
350 }
351
352 void fio_clients_terminate(void)
353 {
354         struct flist_head *entry;
355         struct fio_client *client;
356
357         dprint(FD_NET, "client: terminate clients\n");
358
359         flist_for_each(entry, &client_list) {
360                 client = flist_entry(entry, struct fio_client, list);
361
362                 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_QUIT, 0, NULL);
363         }
364 }
365
366 static void sig_int(int sig)
367 {
368         dprint(FD_NET, "client: got signal %d\n", sig);
369         fio_clients_terminate();
370 }
371
372 static void sig_show_status(int sig)
373 {
374         show_running_run_stats();
375 }
376
377 static void client_signal_handler(void)
378 {
379         struct sigaction act;
380
381         memset(&act, 0, sizeof(act));
382         act.sa_handler = sig_int;
383         act.sa_flags = SA_RESTART;
384         sigaction(SIGINT, &act, NULL);
385
386         memset(&act, 0, sizeof(act));
387         act.sa_handler = sig_int;
388         act.sa_flags = SA_RESTART;
389         sigaction(SIGTERM, &act, NULL);
390
391 /* Windows uses SIGBREAK as a quit signal from other applications */
392 #ifdef WIN32
393         memset(&act, 0, sizeof(act));
394         act.sa_handler = sig_int;
395         act.sa_flags = SA_RESTART;
396         sigaction(SIGBREAK, &act, NULL);
397 #endif
398
399         memset(&act, 0, sizeof(act));
400         act.sa_handler = sig_show_status;
401         act.sa_flags = SA_RESTART;
402         sigaction(SIGUSR1, &act, NULL);
403 }
404
405 static void probe_client(struct fio_client *client)
406 {
407         dprint(FD_NET, "client: send probe\n");
408
409         fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_PROBE, 0, &client->cmd_list);
410 }
411
412 static int send_client_cmd_line(struct fio_client *client)
413 {
414         struct cmd_single_line_pdu *cslp;
415         struct cmd_line_pdu *clp;
416         unsigned long offset;
417         unsigned int *lens;
418         void *pdu;
419         size_t mem;
420         int i, ret;
421
422         dprint(FD_NET, "client: send cmdline %d\n", client->argc);
423
424         lens = malloc(client->argc * sizeof(unsigned int));
425
426         /*
427          * Find out how much mem we need
428          */
429         for (i = 0, mem = 0; i < client->argc; i++) {
430                 lens[i] = strlen(client->argv[i]) + 1;
431                 mem += lens[i];
432         }
433
434         /*
435          * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu
436          */
437         mem += sizeof(*clp) + (client->argc * sizeof(*cslp));
438
439         pdu = malloc(mem);
440         clp = pdu;
441         offset = sizeof(*clp);
442
443         for (i = 0; i < client->argc; i++) {
444                 uint16_t arg_len = lens[i];
445
446                 cslp = pdu + offset;
447                 strcpy((char *) cslp->text, client->argv[i]);
448                 cslp->len = cpu_to_le16(arg_len);
449                 offset += sizeof(*cslp) + arg_len;
450         }
451
452         free(lens);
453         clp->lines = cpu_to_le16(client->argc);
454         ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, 0);
455         free(pdu);
456         return ret;
457 }
458
459 int fio_clients_connect(void)
460 {
461         struct fio_client *client;
462         struct flist_head *entry, *tmp;
463         int ret;
464
465 #ifdef WIN32
466         WSADATA wsd;
467         WSAStartup(MAKEWORD(2,2), &wsd);
468 #endif
469
470         dprint(FD_NET, "client: connect all\n");
471
472         client_signal_handler();
473
474         flist_for_each_safe(entry, tmp, &client_list) {
475                 client = flist_entry(entry, struct fio_client, list);
476
477                 ret = fio_client_connect(client);
478                 if (ret) {
479                         remove_client(client);
480                         continue;
481                 }
482
483                 probe_client(client);
484
485                 if (client->argc > 1)
486                         send_client_cmd_line(client);
487         }
488
489         return !nr_clients;
490 }
491
492 /*
493  * Send file contents to server backend. We could use sendfile(), but to remain
494  * more portable lets just read/write the darn thing.
495  */
496 static int fio_client_send_ini(struct fio_client *client, const char *filename)
497 {
498         struct stat sb;
499         char *p, *buf;
500         off_t len;
501         int fd, ret;
502
503         dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
504
505         fd = open(filename, O_RDONLY);
506         if (fd < 0) {
507                 log_err("fio: job file <%s> open: %s\n", filename, strerror(errno));
508                 return 1;
509         }
510
511         if (fstat(fd, &sb) < 0) {
512                 log_err("fio: job file stat: %s\n", strerror(errno));
513                 close(fd);
514                 return 1;
515         }
516
517         buf = malloc(sb.st_size);
518
519         len = sb.st_size;
520         p = buf;
521         do {
522                 ret = read(fd, p, len);
523                 if (ret > 0) {
524                         len -= ret;
525                         if (!len)
526                                 break;
527                         p += ret;
528                         continue;
529                 } else if (!ret)
530                         break;
531                 else if (errno == EAGAIN || errno == EINTR)
532                         continue;
533         } while (1);
534
535         if (len) {
536                 log_err("fio: failed reading job file %s\n", filename);
537                 close(fd);
538                 free(buf);
539                 return 1;
540         }
541
542         client->sent_job = 1;
543         ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, buf, sb.st_size, 0);
544         free(buf);
545         close(fd);
546         return ret;
547 }
548
549 int fio_clients_send_ini(const char *filename)
550 {
551         struct fio_client *client;
552         struct flist_head *entry, *tmp;
553
554         flist_for_each_safe(entry, tmp, &client_list) {
555                 client = flist_entry(entry, struct fio_client, list);
556
557                 if (client->nr_ini_file) {
558                         int i;
559
560                         for (i = 0; i < client->nr_ini_file; i++) {
561                                 const char *ini = client->ini_file[i];
562
563                                 if (fio_client_send_ini(client, ini)) {
564                                         remove_client(client);
565                                         break;
566                                 }
567                         }
568                 } else if (!filename || fio_client_send_ini(client, filename))
569                         remove_client(client);
570
571                 client->sent_job = 1;
572         }
573
574         return !nr_clients;
575 }
576
577 static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
578 {
579         dst->max_val    = le64_to_cpu(src->max_val);
580         dst->min_val    = le64_to_cpu(src->min_val);
581         dst->samples    = le64_to_cpu(src->samples);
582
583         /*
584          * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
585          */
586         dst->mean.u.f   = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
587         dst->S.u.f      = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
588 }
589
590 static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
591 {
592         int i, j;
593
594         dst->error      = le32_to_cpu(src->error);
595         dst->groupid    = le32_to_cpu(src->groupid);
596         dst->pid        = le32_to_cpu(src->pid);
597         dst->members    = le32_to_cpu(src->members);
598         dst->unified_rw_rep     = le32_to_cpu(src->unified_rw_rep);
599
600         for (i = 0; i < DDIR_RWDIR_CNT; i++) {
601                 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
602                 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
603                 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
604                 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
605         }
606
607         dst->usr_time           = le64_to_cpu(src->usr_time);
608         dst->sys_time           = le64_to_cpu(src->sys_time);
609         dst->ctx                = le64_to_cpu(src->ctx);
610         dst->minf               = le64_to_cpu(src->minf);
611         dst->majf               = le64_to_cpu(src->majf);
612         dst->clat_percentiles   = le64_to_cpu(src->clat_percentiles);
613
614         for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
615                 fio_fp64_t *fps = &src->percentile_list[i];
616                 fio_fp64_t *fpd = &dst->percentile_list[i];
617
618                 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
619         }
620
621         for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
622                 dst->io_u_map[i]        = le32_to_cpu(src->io_u_map[i]);
623                 dst->io_u_submit[i]     = le32_to_cpu(src->io_u_submit[i]);
624                 dst->io_u_complete[i]   = le32_to_cpu(src->io_u_complete[i]);
625         }
626
627         for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
628                 dst->io_u_lat_u[i]      = le32_to_cpu(src->io_u_lat_u[i]);
629                 dst->io_u_lat_m[i]      = le32_to_cpu(src->io_u_lat_m[i]);
630         }
631
632         for (i = 0; i < DDIR_RWDIR_CNT; i++)
633                 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
634                         dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]);
635
636         for (i = 0; i < DDIR_RWDIR_CNT; i++) {
637                 dst->total_io_u[i]      = le64_to_cpu(src->total_io_u[i]);
638                 dst->short_io_u[i]      = le64_to_cpu(src->short_io_u[i]);
639         }
640
641         dst->total_submit       = le64_to_cpu(src->total_submit);
642         dst->total_complete     = le64_to_cpu(src->total_complete);
643
644         for (i = 0; i < DDIR_RWDIR_CNT; i++) {
645                 dst->io_bytes[i]        = le64_to_cpu(src->io_bytes[i]);
646                 dst->runtime[i]         = le64_to_cpu(src->runtime[i]);
647         }
648
649         dst->total_run_time     = le64_to_cpu(src->total_run_time);
650         dst->continue_on_error  = le16_to_cpu(src->continue_on_error);
651         dst->total_err_count    = le64_to_cpu(src->total_err_count);
652         dst->first_error        = le32_to_cpu(src->first_error);
653         dst->kb_base            = le32_to_cpu(src->kb_base);
654 }
655
656 static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
657 {
658         int i;
659
660         for (i = 0; i < DDIR_RWDIR_CNT; i++) {
661                 dst->max_run[i]         = le64_to_cpu(src->max_run[i]);
662                 dst->min_run[i]         = le64_to_cpu(src->min_run[i]);
663                 dst->max_bw[i]          = le64_to_cpu(src->max_bw[i]);
664                 dst->min_bw[i]          = le64_to_cpu(src->min_bw[i]);
665                 dst->io_kb[i]           = le64_to_cpu(src->io_kb[i]);
666                 dst->agg[i]             = le64_to_cpu(src->agg[i]);
667         }
668
669         dst->kb_base    = le32_to_cpu(src->kb_base);
670         dst->groupid    = le32_to_cpu(src->groupid);
671         dst->unified_rw_rep     = le32_to_cpu(src->unified_rw_rep);
672 }
673
674 static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd)
675 {
676         struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
677
678         convert_ts(&p->ts, &p->ts);
679         convert_gs(&p->rs, &p->rs);
680
681         show_thread_status(&p->ts, &p->rs);
682         client->did_stat = 1;
683
684         if (!do_output_all_clients)
685                 return;
686
687         sum_thread_stats(&client_ts, &p->ts, sum_stat_nr);
688         sum_group_stats(&client_gs, &p->rs);
689
690         client_ts.members++;
691         client_ts.groupid = p->ts.groupid;
692         client_ts.unified_rw_rep = p->ts.unified_rw_rep;
693
694         if (++sum_stat_nr == sum_stat_clients) {
695                 strcpy(client_ts.name, "All clients");
696                 show_thread_status(&client_ts, &client_gs);
697         }
698 }
699
700 static void handle_gs(struct fio_net_cmd *cmd)
701 {
702         struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
703
704         convert_gs(gs, gs);
705         show_group_stats(gs);
706 }
707
708 static void convert_agg(struct disk_util_agg *agg)
709 {
710         int i;
711
712         for (i = 0; i < 2; i++) {
713                 agg->ios[i]     = le32_to_cpu(agg->ios[i]);
714                 agg->merges[i]  = le32_to_cpu(agg->merges[i]);
715                 agg->sectors[i] = le64_to_cpu(agg->sectors[i]);
716                 agg->ticks[i]   = le32_to_cpu(agg->ticks[i]);
717         }
718
719         agg->io_ticks           = le32_to_cpu(agg->io_ticks);
720         agg->time_in_queue      = le32_to_cpu(agg->time_in_queue);
721         agg->slavecount         = le32_to_cpu(agg->slavecount);
722         agg->max_util.u.f       = fio_uint64_to_double(__le64_to_cpu(agg->max_util.u.i));
723 }
724
725 static void convert_dus(struct disk_util_stat *dus)
726 {
727         int i;
728
729         for (i = 0; i < 2; i++) {
730                 dus->ios[i]     = le32_to_cpu(dus->ios[i]);
731                 dus->merges[i]  = le32_to_cpu(dus->merges[i]);
732                 dus->sectors[i] = le64_to_cpu(dus->sectors[i]);
733                 dus->ticks[i]   = le32_to_cpu(dus->ticks[i]);
734         }
735
736         dus->io_ticks           = le32_to_cpu(dus->io_ticks);
737         dus->time_in_queue      = le32_to_cpu(dus->time_in_queue);
738         dus->msec               = le64_to_cpu(dus->msec);
739 }
740
741 static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd)
742 {
743         struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
744
745         convert_dus(&du->dus);
746         convert_agg(&du->agg);
747
748         if (!client->disk_stats_shown) {
749                 client->disk_stats_shown = 1;
750                 log_info("\nDisk stats (read/write):\n");
751         }
752
753         print_disk_util(&du->dus, &du->agg, output_format == FIO_OUTPUT_TERSE);
754 }
755
756 static void convert_jobs_eta(struct jobs_eta *je)
757 {
758         int i;
759
760         je->nr_running          = le32_to_cpu(je->nr_running);
761         je->nr_ramp             = le32_to_cpu(je->nr_ramp);
762         je->nr_pending          = le32_to_cpu(je->nr_pending);
763         je->files_open          = le32_to_cpu(je->files_open);
764         je->m_rate              = le32_to_cpu(je->m_rate);
765         je->t_rate              = le32_to_cpu(je->t_rate);
766         je->m_iops              = le32_to_cpu(je->m_iops);
767         je->t_iops              = le32_to_cpu(je->t_iops);
768
769         for (i = 0; i < DDIR_RWDIR_CNT; i++) {
770                 je->rate[i]     = le32_to_cpu(je->rate[i]);
771                 je->iops[i]     = le32_to_cpu(je->iops[i]);
772         }
773
774         je->elapsed_sec         = le64_to_cpu(je->elapsed_sec);
775         je->eta_sec             = le64_to_cpu(je->eta_sec);
776         je->is_pow2             = le32_to_cpu(je->is_pow2);
777 }
778
779 static void sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je)
780 {
781         int i;
782
783         dst->nr_running         += je->nr_running;
784         dst->nr_ramp            += je->nr_ramp;
785         dst->nr_pending         += je->nr_pending;
786         dst->files_open         += je->files_open;
787         dst->m_rate             += je->m_rate;
788         dst->t_rate             += je->t_rate;
789         dst->m_iops             += je->m_iops;
790         dst->t_iops             += je->t_iops;
791
792         for (i = 0; i < DDIR_RWDIR_CNT; i++) {
793                 dst->rate[i]    += je->rate[i];
794                 dst->iops[i]    += je->iops[i];
795         }
796
797         dst->elapsed_sec        += je->elapsed_sec;
798
799         if (je->eta_sec > dst->eta_sec)
800                 dst->eta_sec = je->eta_sec;
801 }
802
803 static void dec_jobs_eta(struct client_eta *eta)
804 {
805         if (!--eta->pending) {
806                 display_thread_status(&eta->eta);
807                 free(eta);
808         }
809 }
810
811 static void remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd)
812 {
813         struct fio_net_int_cmd *icmd = NULL;
814         struct flist_head *entry;
815
816         flist_for_each(entry, &client->cmd_list) {
817                 icmd = flist_entry(entry, struct fio_net_int_cmd, list);
818
819                 if (cmd->tag == (uintptr_t) icmd)
820                         break;
821
822                 icmd = NULL;
823         }
824
825         if (!icmd) {
826                 log_err("fio: client: unable to find matching tag\n");
827                 return;
828         }
829
830         flist_del(&icmd->list);
831         cmd->tag = icmd->saved_tag;
832         free(icmd);
833 }
834
835 static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd)
836 {
837         struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
838         struct client_eta *eta = (struct client_eta *) (uintptr_t) cmd->tag;
839
840         dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending);
841
842         assert(client->eta_in_flight == eta);
843
844         client->eta_in_flight = NULL;
845         flist_del_init(&client->eta_list);
846
847         convert_jobs_eta(je);
848         sum_jobs_eta(&eta->eta, je);
849         dec_jobs_eta(eta);
850 }
851
852 static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
853 {
854         struct cmd_probe_pdu *probe = (struct cmd_probe_pdu *) cmd->payload;
855         const char *os, *arch;
856         char bit[16];
857
858         os = fio_get_os_string(probe->os);
859         if (!os)
860                 os = "unknown";
861
862         arch = fio_get_arch_string(probe->arch);
863         if (!arch)
864                 os = "unknown";
865
866         sprintf(bit, "%d-bit", probe->bpp * 8);
867
868         log_info("hostname=%s, be=%u, %s, os=%s, arch=%s, fio=%s\n",
869                 probe->hostname, probe->bigendian, bit, os, arch,
870                 probe->fio_version);
871
872         if (!client->name)
873                 client->name = strdup((char *) probe->hostname);
874 }
875
876 static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd)
877 {
878         struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
879
880         client->state = Client_started;
881         client->jobs = le32_to_cpu(pdu->jobs);
882         client->nr_stat = le32_to_cpu(pdu->stat_outputs);
883
884         if (sum_stat_clients > 1)
885                 do_output_all_clients = 1;
886
887         sum_stat_clients += client->nr_stat;
888 }
889
890 static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd)
891 {
892         struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
893
894         client->state = Client_stopped;
895         client->error = le32_to_cpu(pdu->error);
896
897         if (client->error)
898                 log_info("client <%s>: exited with error %d\n", client->hostname, client->error);
899 }
900
901 static int handle_client(struct fio_client *client)
902 {
903         struct fio_net_cmd *cmd;
904
905         dprint(FD_NET, "client: handle %s\n", client->hostname);
906
907         cmd = fio_net_recv_cmd(client->fd);
908         if (!cmd)
909                 return 0;
910
911         dprint(FD_NET, "client: got cmd op %s from %s\n",
912                                 fio_server_op(cmd->opcode), client->hostname);
913
914         switch (cmd->opcode) {
915         case FIO_NET_CMD_QUIT:
916                 remove_client(client);
917                 free(cmd);
918                 break;
919         case FIO_NET_CMD_TEXT: {
920                 const char *buf = (const char *) cmd->payload;
921                 const char *name;
922                 int fio_unused ret;
923
924                 name = client->name ? client->name : client->hostname;
925
926                 if (!client->skip_newline)
927                         fprintf(f_out, "<%s> ", name);
928                 ret = fwrite(buf, cmd->pdu_len, 1, f_out);
929                 fflush(f_out);
930                 client->skip_newline = strchr(buf, '\n') == NULL;
931                 free(cmd);
932                 break;
933                 }
934         case FIO_NET_CMD_DU:
935                 handle_du(client, cmd);
936                 free(cmd);
937                 break;
938         case FIO_NET_CMD_TS:
939                 handle_ts(client, cmd);
940                 free(cmd);
941                 break;
942         case FIO_NET_CMD_GS:
943                 handle_gs(cmd);
944                 free(cmd);
945                 break;
946         case FIO_NET_CMD_ETA:
947                 remove_reply_cmd(client, cmd);
948                 handle_eta(client, cmd);
949                 free(cmd);
950                 break;
951         case FIO_NET_CMD_PROBE:
952                 remove_reply_cmd(client, cmd);
953                 handle_probe(client, cmd);
954                 free(cmd);
955                 break;
956         case FIO_NET_CMD_RUN:
957                 client->state = Client_running;
958                 free(cmd);
959                 break;
960         case FIO_NET_CMD_START:
961                 handle_start(client, cmd);
962                 free(cmd);
963                 break;
964         case FIO_NET_CMD_STOP:
965                 handle_stop(client, cmd);
966                 free(cmd);
967                 break;
968         default:
969                 log_err("fio: unknown client op: %s\n", fio_server_op(cmd->opcode));
970                 free(cmd);
971                 break;
972         }
973
974         return 1;
975 }
976
977 static void request_client_etas(void)
978 {
979         struct fio_client *client;
980         struct flist_head *entry;
981         struct client_eta *eta;
982         int skipped = 0;
983
984         dprint(FD_NET, "client: request eta (%d)\n", nr_clients);
985
986         eta = malloc(sizeof(*eta));
987         memset(&eta->eta, 0, sizeof(eta->eta));
988         eta->pending = nr_clients;
989
990         flist_for_each(entry, &client_list) {
991                 client = flist_entry(entry, struct fio_client, list);
992
993                 if (!flist_empty(&client->eta_list)) {
994                         skipped++;
995                         continue;
996                 }
997                 if (client->state != Client_running)
998                         continue;
999
1000                 assert(!client->eta_in_flight);
1001                 flist_add_tail(&client->eta_list, &eta_list);
1002                 client->eta_in_flight = eta;
1003                 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA,
1004                                         (uintptr_t) eta, &client->cmd_list);
1005         }
1006
1007         while (skipped--)
1008                 dec_jobs_eta(eta);
1009
1010         dprint(FD_NET, "client: requested eta tag %p\n", eta);
1011 }
1012
1013 static int client_check_cmd_timeout(struct fio_client *client,
1014                                     struct timeval *now)
1015 {
1016         struct fio_net_int_cmd *cmd;
1017         struct flist_head *entry, *tmp;
1018         int ret = 0;
1019
1020         flist_for_each_safe(entry, tmp, &client->cmd_list) {
1021                 cmd = flist_entry(entry, struct fio_net_int_cmd, list);
1022
1023                 if (mtime_since(&cmd->tv, now) < FIO_NET_CLIENT_TIMEOUT)
1024                         continue;
1025
1026                 log_err("fio: client %s, timeout on cmd %s\n", client->hostname,
1027                                                 fio_server_op(cmd->cmd.opcode));
1028                 flist_del(&cmd->list);
1029                 free(cmd);
1030                 ret = 1;
1031         }
1032
1033         return flist_empty(&client->cmd_list) && ret;
1034 }
1035
1036 static int fio_client_timed_out(void)
1037 {
1038         struct fio_client *client;
1039         struct flist_head *entry, *tmp;
1040         struct timeval tv;
1041         int ret = 0;
1042
1043         fio_gettime(&tv, NULL);
1044
1045         flist_for_each_safe(entry, tmp, &client_list) {
1046                 client = flist_entry(entry, struct fio_client, list);
1047
1048                 if (flist_empty(&client->cmd_list))
1049                         continue;
1050
1051                 if (!client_check_cmd_timeout(client, &tv))
1052                         continue;
1053
1054                 log_err("fio: client %s timed out\n", client->hostname);
1055                 remove_client(client);
1056                 ret = 1;
1057         }
1058
1059         return ret;
1060 }
1061
1062 int fio_handle_clients(void)
1063 {
1064         struct pollfd *pfds;
1065         int i, ret = 0, retval = 0;
1066
1067         fio_gettime(&eta_tv, NULL);
1068
1069         pfds = malloc(nr_clients * sizeof(struct pollfd));
1070
1071         init_thread_stat(&client_ts);
1072         init_group_run_stat(&client_gs);
1073
1074         while (!exit_backend && nr_clients) {
1075                 struct flist_head *entry, *tmp;
1076                 struct fio_client *client;
1077
1078                 i = 0;
1079                 flist_for_each_safe(entry, tmp, &client_list) {
1080                         client = flist_entry(entry, struct fio_client, list);
1081
1082                         if (!client->sent_job &&
1083                             flist_empty(&client->cmd_list)) {
1084                                 remove_client(client);
1085                                 continue;
1086                         }
1087
1088                         pfds[i].fd = client->fd;
1089                         pfds[i].events = POLLIN;
1090                         i++;
1091                 }
1092
1093                 if (!nr_clients)
1094                         break;
1095
1096                 assert(i == nr_clients);
1097
1098                 do {
1099                         struct timeval tv;
1100
1101                         fio_gettime(&tv, NULL);
1102                         if (mtime_since(&eta_tv, &tv) >= 900) {
1103                                 request_client_etas();
1104                                 memcpy(&eta_tv, &tv, sizeof(tv));
1105
1106                                 if (fio_client_timed_out())
1107                                         break;
1108                         }
1109
1110                         ret = poll(pfds, nr_clients, 100);
1111                         if (ret < 0) {
1112                                 if (errno == EINTR)
1113                                         continue;
1114                                 log_err("fio: poll clients: %s\n", strerror(errno));
1115                                 break;
1116                         } else if (!ret)
1117                                 continue;
1118                 } while (ret <= 0);
1119
1120                 for (i = 0; i < nr_clients; i++) {
1121                         if (!(pfds[i].revents & POLLIN))
1122                                 continue;
1123
1124                         client = find_client_by_fd(pfds[i].fd);
1125                         if (!client) {
1126                                 log_err("fio: unknown client fd %d\n", pfds[i].fd);
1127                                 continue;
1128                         }
1129                         if (!handle_client(client)) {
1130                                 log_info("client: host=%s disconnected\n",
1131                                                 client->hostname);
1132                                 remove_client(client);
1133                                 retval = 1;
1134                         } else if (client->error)
1135                                 retval = 1;
1136                         put_client(client);
1137                 }
1138         }
1139
1140         free(pfds);
1141         return retval;
1142 }