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