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