Add client references
[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 static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd);
25 static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd);
26 static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd);
27 static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd);
28 static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd);
29
30 struct client_ops fio_client_ops = {
31         .text_op        = handle_text,
32         .disk_util      = handle_du,
33         .thread_status  = handle_ts,
34         .group_stats    = handle_gs,
35         .eta            = display_thread_status,
36         .probe          = handle_probe,
37 };
38
39 static struct timeval eta_tv;
40
41 enum {
42         Client_created          = 0,
43         Client_connected        = 1,
44         Client_started          = 2,
45         Client_running          = 3,
46         Client_stopped          = 4,
47         Client_exited           = 5,
48 };
49
50 static FLIST_HEAD(client_list);
51 static FLIST_HEAD(eta_list);
52
53 static FLIST_HEAD(arg_list);
54
55 static struct thread_stat client_ts;
56 static struct group_run_stats client_gs;
57 static int sum_stat_clients;
58 static int sum_stat_nr;
59
60 #define FIO_CLIENT_HASH_BITS    7
61 #define FIO_CLIENT_HASH_SZ      (1 << FIO_CLIENT_HASH_BITS)
62 #define FIO_CLIENT_HASH_MASK    (FIO_CLIENT_HASH_SZ - 1)
63 static struct flist_head client_hash[FIO_CLIENT_HASH_SZ];
64
65 static void fio_client_add_hash(struct fio_client *client)
66 {
67         int bucket = hash_long(client->fd, FIO_CLIENT_HASH_BITS);
68
69         bucket &= FIO_CLIENT_HASH_MASK;
70         flist_add(&client->hash_list, &client_hash[bucket]);
71 }
72
73 static void fio_client_remove_hash(struct fio_client *client)
74 {
75         if (!flist_empty(&client->hash_list))
76                 flist_del_init(&client->hash_list);
77 }
78
79 static void fio_init fio_client_hash_init(void)
80 {
81         int i;
82
83         for (i = 0; i < FIO_CLIENT_HASH_SZ; i++)
84                 INIT_FLIST_HEAD(&client_hash[i]);
85 }
86
87 static struct fio_client *find_client_by_fd(int fd)
88 {
89         int bucket = hash_long(fd, FIO_CLIENT_HASH_BITS) & FIO_CLIENT_HASH_MASK;
90         struct fio_client *client;
91         struct flist_head *entry;
92
93         flist_for_each(entry, &client_hash[bucket]) {
94                 client = flist_entry(entry, struct fio_client, hash_list);
95
96                 if (client->fd == fd) {
97                         client->refs++;
98                         return client;
99                 }
100         }
101
102         return NULL;
103 }
104
105 static void remove_client(struct fio_client *client)
106 {
107         assert(client->refs);
108
109         if (--client->refs)
110                 return;
111
112         dprint(FD_NET, "client: removed <%s>\n", client->hostname);
113         flist_del(&client->list);
114
115         fio_client_remove_hash(client);
116
117         if (!flist_empty(&client->eta_list)) {
118                 flist_del_init(&client->eta_list);
119                 fio_client_dec_jobs_eta(client->eta_in_flight, client->ops->eta);
120         }
121
122         free(client->hostname);
123         if (client->argv)
124                 free(client->argv);
125         if (client->name)
126                 free(client->name);
127
128         free(client);
129         nr_clients--;
130         sum_stat_clients--;
131 }
132
133 static void put_client(struct fio_client *client)
134 {
135         remove_client(client);
136 }
137
138 static void __fio_client_add_cmd_option(struct fio_client *client,
139                                         const char *opt)
140 {
141         int index;
142
143         index = client->argc++;
144         client->argv = realloc(client->argv, sizeof(char *) * client->argc);
145         client->argv[index] = strdup(opt);
146         dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
147 }
148
149 void fio_client_add_cmd_option(void *cookie, const char *opt)
150 {
151         struct fio_client *client = cookie;
152         struct flist_head *entry;
153
154         if (!client || !opt)
155                 return;
156
157         __fio_client_add_cmd_option(client, opt);
158
159         /*
160          * Duplicate arguments to shared client group
161          */
162         flist_for_each(entry, &arg_list) {
163                 client = flist_entry(entry, struct fio_client, arg_list);
164
165                 __fio_client_add_cmd_option(client, opt);
166         }
167 }
168
169 struct fio_client *fio_client_add_explicit(struct client_ops *ops,
170                                            const char *hostname, int type,
171                                            int port)
172 {
173         struct fio_client *client;
174
175         client = malloc(sizeof(*client));
176         memset(client, 0, sizeof(*client));
177
178         INIT_FLIST_HEAD(&client->list);
179         INIT_FLIST_HEAD(&client->hash_list);
180         INIT_FLIST_HEAD(&client->arg_list);
181         INIT_FLIST_HEAD(&client->eta_list);
182         INIT_FLIST_HEAD(&client->cmd_list);
183
184         client->hostname = strdup(hostname);
185
186         if (type == Fio_client_socket)
187                 client->is_sock = 1;
188         else {
189                 int ipv6;
190
191                 ipv6 = type == Fio_client_ipv6;
192                 if (fio_server_parse_host(hostname, &ipv6,
193                                                 &client->addr.sin_addr,
194                                                 &client->addr6.sin6_addr))
195                         goto err;
196
197                 client->port = port;
198         }
199
200         client->fd = -1;
201         client->ops = ops;
202         client->refs = 1;
203
204         __fio_client_add_cmd_option(client, "fio");
205
206         flist_add(&client->list, &client_list);
207         nr_clients++;
208         dprint(FD_NET, "client: added <%s>\n", client->hostname);
209         return client;
210 err:
211         free(client);
212         return NULL;
213 }
214
215 int fio_client_add(struct client_ops *ops, const char *hostname, void **cookie)
216 {
217         struct fio_client *existing = *cookie;
218         struct fio_client *client;
219
220         if (existing) {
221                 /*
222                  * We always add our "exec" name as the option, hence 1
223                  * means empty.
224                  */
225                 if (existing->argc == 1)
226                         flist_add_tail(&existing->arg_list, &arg_list);
227                 else {
228                         while (!flist_empty(&arg_list))
229                                 flist_del_init(arg_list.next);
230                 }
231         }
232
233         client = malloc(sizeof(*client));
234         memset(client, 0, sizeof(*client));
235
236         INIT_FLIST_HEAD(&client->list);
237         INIT_FLIST_HEAD(&client->hash_list);
238         INIT_FLIST_HEAD(&client->arg_list);
239         INIT_FLIST_HEAD(&client->eta_list);
240         INIT_FLIST_HEAD(&client->cmd_list);
241
242         if (fio_server_parse_string(hostname, &client->hostname,
243                                         &client->is_sock, &client->port,
244                                         &client->addr.sin_addr,
245                                         &client->addr6.sin6_addr,
246                                         &client->ipv6))
247                 return -1;
248
249         client->fd = -1;
250         client->ops = ops;
251         client->refs = 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_client *client, struct fio_net_cmd *cmd)
639 {
640         struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
641
642         show_thread_status(&p->ts, &p->rs);
643
644         if (sum_stat_clients == 1)
645                 return;
646
647         sum_thread_stats(&client_ts, &p->ts, sum_stat_nr);
648         sum_group_stats(&client_gs, &p->rs);
649
650         client_ts.members++;
651         client_ts.groupid = p->ts.groupid;
652
653         if (++sum_stat_nr == sum_stat_clients) {
654                 strcpy(client_ts.name, "All clients");
655                 show_thread_status(&client_ts, &client_gs);
656         }
657 }
658
659 static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd)
660 {
661         struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
662
663         show_group_stats(gs);
664 }
665
666 static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd)
667 {
668         struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
669         const char *buf = (const char *) pdu->buf;
670         const char *name;
671         int fio_unused ret;
672
673         name = client->name ? client->name : client->hostname;
674
675         if (!client->skip_newline)
676                 fprintf(f_out, "<%s> ", name);
677         ret = fwrite(buf, pdu->buf_len, 1, f_out);
678         fflush(f_out);
679         client->skip_newline = strchr(buf, '\n') == NULL;
680 }
681
682 static void convert_agg(struct disk_util_agg *agg)
683 {
684         int i;
685
686         for (i = 0; i < 2; i++) {
687                 agg->ios[i]     = le32_to_cpu(agg->ios[i]);
688                 agg->merges[i]  = le32_to_cpu(agg->merges[i]);
689                 agg->sectors[i] = le64_to_cpu(agg->sectors[i]);
690                 agg->ticks[i]   = le32_to_cpu(agg->ticks[i]);
691         }
692
693         agg->io_ticks           = le32_to_cpu(agg->io_ticks);
694         agg->time_in_queue      = le32_to_cpu(agg->time_in_queue);
695         agg->slavecount         = le32_to_cpu(agg->slavecount);
696         agg->max_util.u.f       = fio_uint64_to_double(__le64_to_cpu(agg->max_util.u.i));
697 }
698
699 static void convert_dus(struct disk_util_stat *dus)
700 {
701         int i;
702
703         for (i = 0; i < 2; i++) {
704                 dus->ios[i]     = le32_to_cpu(dus->ios[i]);
705                 dus->merges[i]  = le32_to_cpu(dus->merges[i]);
706                 dus->sectors[i] = le64_to_cpu(dus->sectors[i]);
707                 dus->ticks[i]   = le32_to_cpu(dus->ticks[i]);
708         }
709
710         dus->io_ticks           = le32_to_cpu(dus->io_ticks);
711         dus->time_in_queue      = le32_to_cpu(dus->time_in_queue);
712         dus->msec               = le64_to_cpu(dus->msec);
713 }
714
715 static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd)
716 {
717         struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
718
719         if (!client->disk_stats_shown) {
720                 client->disk_stats_shown = 1;
721                 log_info("\nDisk stats (read/write):\n");
722         }
723
724         print_disk_util(&du->dus, &du->agg, terse_output);
725 }
726
727 static void convert_jobs_eta(struct jobs_eta *je)
728 {
729         int i;
730
731         je->nr_running          = le32_to_cpu(je->nr_running);
732         je->nr_ramp             = le32_to_cpu(je->nr_ramp);
733         je->nr_pending          = le32_to_cpu(je->nr_pending);
734         je->files_open          = le32_to_cpu(je->files_open);
735
736         for (i = 0; i < 2; i++) {
737                 je->m_rate[i]           = le32_to_cpu(je->m_rate[i]);
738                 je->t_rate[i]           = le32_to_cpu(je->t_rate[i]);
739                 je->m_iops[i]           = le32_to_cpu(je->m_iops[i]);
740                 je->t_iops[i]           = le32_to_cpu(je->t_iops[i]);
741                 je->rate[i]     = le32_to_cpu(je->rate[i]);
742                 je->iops[i]     = le32_to_cpu(je->iops[i]);
743         }
744
745         je->elapsed_sec         = le64_to_cpu(je->elapsed_sec);
746         je->eta_sec             = le64_to_cpu(je->eta_sec);
747 }
748
749 void fio_client_sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je)
750 {
751         int i;
752
753         dst->nr_running         += je->nr_running;
754         dst->nr_ramp            += je->nr_ramp;
755         dst->nr_pending         += je->nr_pending;
756         dst->files_open         += je->files_open;
757
758         for (i = 0; i < 2; i++) {
759                 dst->m_rate[i]  += je->m_rate[i];
760                 dst->t_rate[i]  += je->t_rate[i];
761                 dst->m_iops[i]  += je->m_iops[i];
762                 dst->t_iops[i]  += je->t_iops[i];
763                 dst->rate[i]    += je->rate[i];
764                 dst->iops[i]    += je->iops[i];
765         }
766
767         dst->elapsed_sec        += je->elapsed_sec;
768
769         if (je->eta_sec > dst->eta_sec)
770                 dst->eta_sec = je->eta_sec;
771 }
772
773 void fio_client_dec_jobs_eta(struct client_eta *eta, client_eta_op eta_fn)
774 {
775         if (!--eta->pending) {
776                 eta_fn(&eta->eta);
777                 free(eta);
778         }
779 }
780
781 static void remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd)
782 {
783         struct fio_net_int_cmd *icmd = NULL;
784         struct flist_head *entry;
785
786         flist_for_each(entry, &client->cmd_list) {
787                 icmd = flist_entry(entry, struct fio_net_int_cmd, list);
788
789                 if (cmd->tag == (uintptr_t) icmd)
790                         break;
791
792                 icmd = NULL;
793         }
794
795         if (!icmd) {
796                 log_err("fio: client: unable to find matching tag\n");
797                 return;
798         }
799
800         flist_del(&icmd->list);
801         cmd->tag = icmd->saved_tag;
802         free(icmd);
803 }
804
805 static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd)
806 {
807         struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
808         struct client_eta *eta = (struct client_eta *) (uintptr_t) cmd->tag;
809
810         dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending);
811
812         assert(client->eta_in_flight == eta);
813
814         client->eta_in_flight = NULL;
815         flist_del_init(&client->eta_list);
816
817         fio_client_sum_jobs_eta(&eta->eta, je);
818         fio_client_dec_jobs_eta(eta, client->ops->eta);
819 }
820
821 static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
822 {
823         struct cmd_probe_pdu *probe = (struct cmd_probe_pdu *) cmd->payload;
824         const char *os, *arch;
825         char bit[16];
826
827         os = fio_get_os_string(probe->os);
828         if (!os)
829                 os = "unknown";
830
831         arch = fio_get_arch_string(probe->arch);
832         if (!arch)
833                 os = "unknown";
834
835         sprintf(bit, "%d-bit", probe->bpp * 8);
836
837         log_info("hostname=%s, be=%u, %s, os=%s, arch=%s, fio=%u.%u.%u\n",
838                 probe->hostname, probe->bigendian, bit, os, arch,
839                 probe->fio_major, probe->fio_minor, probe->fio_patch);
840
841         if (!client->name)
842                 client->name = strdup((char *) probe->hostname);
843 }
844
845 static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd)
846 {
847         struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
848
849         client->state = Client_started;
850         client->jobs = le32_to_cpu(pdu->jobs);
851 }
852
853 static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd)
854 {
855         struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
856
857         client->state = Client_stopped;
858         client->error = le32_to_cpu(pdu->error);
859
860         if (client->error)
861                 log_info("client <%s>: exited with error %d\n", client->hostname, client->error);
862 }
863
864 static void convert_text(struct fio_net_cmd *cmd)
865 {
866         struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
867
868         pdu->level      = le32_to_cpu(pdu->level);
869         pdu->buf_len    = le32_to_cpu(pdu->buf_len);
870         pdu->log_sec    = le64_to_cpu(pdu->log_sec);
871         pdu->log_usec   = le64_to_cpu(pdu->log_usec);
872 }
873
874 int fio_handle_client(struct fio_client *client)
875 {
876         struct client_ops *ops = client->ops;
877         struct fio_net_cmd *cmd;
878
879         dprint(FD_NET, "client: handle %s\n", client->hostname);
880
881         cmd = fio_net_recv_cmd(client->fd);
882         if (!cmd)
883                 return 0;
884
885         dprint(FD_NET, "client: got cmd op %s from %s\n",
886                                 fio_server_op(cmd->opcode), client->hostname);
887
888         switch (cmd->opcode) {
889         case FIO_NET_CMD_QUIT:
890                 if (ops->quit)
891                         ops->quit(client);
892                 remove_client(client);
893                 free(cmd);
894                 break;
895         case FIO_NET_CMD_TEXT:
896                 convert_text(cmd);
897                 ops->text_op(client, cmd);
898                 free(cmd);
899                 break;
900         case FIO_NET_CMD_DU: {
901                 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
902
903                 convert_dus(&du->dus);
904                 convert_agg(&du->agg);
905
906                 ops->disk_util(client, cmd);
907                 free(cmd);
908                 break;
909                 }
910         case FIO_NET_CMD_TS: {
911                 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
912
913                 convert_ts(&p->ts, &p->ts);
914                 convert_gs(&p->rs, &p->rs);
915
916                 ops->thread_status(client, cmd);
917                 free(cmd);
918                 break;
919                 }
920         case FIO_NET_CMD_GS: {
921                 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
922
923                 convert_gs(gs, gs);
924
925                 ops->group_stats(client, cmd);
926                 free(cmd);
927                 break;
928                 }
929         case FIO_NET_CMD_ETA: {
930                 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
931
932                 remove_reply_cmd(client, cmd);
933                 convert_jobs_eta(je);
934                 handle_eta(client, cmd);
935                 free(cmd);
936                 break;
937                 }
938         case FIO_NET_CMD_PROBE:
939                 remove_reply_cmd(client, cmd);
940                 ops->probe(client, cmd);
941                 free(cmd);
942                 break;
943         case FIO_NET_CMD_RUN:
944                 client->state = Client_running;
945                 free(cmd);
946                 break;
947         case FIO_NET_CMD_START:
948                 handle_start(client, cmd);
949                 free(cmd);
950                 break;
951         case FIO_NET_CMD_STOP:
952                 handle_stop(client, cmd);
953                 free(cmd);
954                 break;
955         case FIO_NET_CMD_ADD_JOB:
956                 if (ops->add_job)
957                         ops->add_job(client, cmd);
958                 free(cmd);
959                 break;
960         default:
961                 log_err("fio: unknown client op: %s\n", fio_server_op(cmd->opcode));
962                 free(cmd);
963                 break;
964         }
965
966         return 1;
967 }
968
969 static void request_client_etas(struct client_ops *ops)
970 {
971         struct fio_client *client;
972         struct flist_head *entry;
973         struct client_eta *eta;
974         int skipped = 0;
975
976         dprint(FD_NET, "client: request eta (%d)\n", nr_clients);
977
978         eta = malloc(sizeof(*eta));
979         memset(&eta->eta, 0, sizeof(eta->eta));
980         eta->pending = nr_clients;
981
982         flist_for_each(entry, &client_list) {
983                 client = flist_entry(entry, struct fio_client, list);
984
985                 if (!flist_empty(&client->eta_list)) {
986                         skipped++;
987                         continue;
988                 }
989                 if (client->state != Client_running)
990                         continue;
991
992                 assert(!client->eta_in_flight);
993                 flist_add_tail(&client->eta_list, &eta_list);
994                 client->eta_in_flight = eta;
995                 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA,
996                                         (uintptr_t) eta, &client->cmd_list);
997         }
998
999         while (skipped--)
1000                 fio_client_dec_jobs_eta(eta, ops->eta);
1001
1002         dprint(FD_NET, "client: requested eta tag %p\n", eta);
1003 }
1004
1005 static int client_check_cmd_timeout(struct fio_client *client,
1006                                     struct timeval *now)
1007 {
1008         struct fio_net_int_cmd *cmd;
1009         struct flist_head *entry, *tmp;
1010         int ret = 0;
1011
1012         flist_for_each_safe(entry, tmp, &client->cmd_list) {
1013                 cmd = flist_entry(entry, struct fio_net_int_cmd, list);
1014
1015                 if (mtime_since(&cmd->tv, now) < FIO_NET_CLIENT_TIMEOUT)
1016                         continue;
1017
1018                 log_err("fio: client %s, timeout on cmd %s\n", client->hostname,
1019                                                 fio_server_op(cmd->cmd.opcode));
1020                 flist_del(&cmd->list);
1021                 free(cmd);
1022                 ret = 1;
1023         }
1024
1025         return flist_empty(&client->cmd_list) && ret;
1026 }
1027
1028 static int fio_check_clients_timed_out(void)
1029 {
1030         struct fio_client *client;
1031         struct flist_head *entry, *tmp;
1032         struct timeval tv;
1033         int ret = 0;
1034
1035         gettimeofday(&tv, NULL);
1036
1037         flist_for_each_safe(entry, tmp, &client_list) {
1038                 client = flist_entry(entry, struct fio_client, list);
1039
1040                 if (flist_empty(&client->cmd_list))
1041                         continue;
1042
1043                 if (!client_check_cmd_timeout(client, &tv))
1044                         continue;
1045
1046                 if (client->ops->timed_out)
1047                         client->ops->timed_out(client);
1048                 else
1049                         log_err("fio: client %s timed out\n", client->hostname);
1050
1051                 remove_client(client);
1052                 ret = 1;
1053         }
1054
1055         return ret;
1056 }
1057
1058 int fio_handle_clients(struct client_ops *ops)
1059 {
1060         struct pollfd *pfds;
1061         int i, ret = 0, retval = 0;
1062
1063         gettimeofday(&eta_tv, NULL);
1064
1065         pfds = malloc(nr_clients * sizeof(struct pollfd));
1066
1067         sum_stat_clients = nr_clients;
1068         init_thread_stat(&client_ts);
1069         init_group_run_stat(&client_gs);
1070
1071         while (!exit_backend && nr_clients) {
1072                 struct flist_head *entry, *tmp;
1073                 struct fio_client *client;
1074
1075                 i = 0;
1076                 flist_for_each_safe(entry, tmp, &client_list) {
1077                         client = flist_entry(entry, struct fio_client, list);
1078
1079                         if (!client->sent_job && !client->ops->stay_connected &&
1080                             flist_empty(&client->cmd_list)) {
1081                                 remove_client(client);
1082                                 continue;
1083                         }
1084
1085                         pfds[i].fd = client->fd;
1086                         pfds[i].events = POLLIN;
1087                         i++;
1088                 }
1089
1090                 if (!nr_clients)
1091                         break;
1092
1093                 assert(i == nr_clients);
1094
1095                 do {
1096                         struct timeval tv;
1097
1098                         gettimeofday(&tv, NULL);
1099                         if (mtime_since(&eta_tv, &tv) >= 900) {
1100                                 request_client_etas(ops);
1101                                 memcpy(&eta_tv, &tv, sizeof(tv));
1102
1103                                 if (fio_check_clients_timed_out())
1104                                         break;
1105                         }
1106
1107                         ret = poll(pfds, nr_clients, 100);
1108                         if (ret < 0) {
1109                                 if (errno == EINTR)
1110                                         continue;
1111                                 log_err("fio: poll clients: %s\n", strerror(errno));
1112                                 break;
1113                         } else if (!ret)
1114                                 continue;
1115                 } while (ret <= 0);
1116
1117                 for (i = 0; i < nr_clients; i++) {
1118                         if (!(pfds[i].revents & POLLIN))
1119                                 continue;
1120
1121                         client = find_client_by_fd(pfds[i].fd);
1122                         if (!client) {
1123                                 log_err("fio: unknown client fd %d\n", pfds[i].fd);
1124                                 continue;
1125                         }
1126                         if (!fio_handle_client(client)) {
1127                                 log_info("client: host=%s disconnected\n",
1128                                                 client->hostname);
1129                                 remove_client(client);
1130                                 retval = 1;
1131                         } else if (client->error)
1132                                 retval = 1;
1133                         put_client(client);
1134                 }
1135         }
1136
1137         free(pfds);
1138         return retval;
1139 }