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