client: cleanup json output
[fio.git] / client.c
... / ...
CommitLineData
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#ifdef CONFIG_ZLIB
18#include <zlib.h>
19#endif
20
21#include "fio.h"
22#include "client.h"
23#include "server.h"
24#include "flist.h"
25#include "hash.h"
26#include "verify.h"
27
28static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd);
29static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd);
30static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd);
31static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd);
32static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd);
33static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd);
34static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd);
35
36struct client_ops fio_client_ops = {
37 .text = handle_text,
38 .disk_util = handle_du,
39 .thread_status = handle_ts,
40 .group_stats = handle_gs,
41 .stop = handle_stop,
42 .start = handle_start,
43 .eta = display_thread_status,
44 .probe = handle_probe,
45 .eta_msec = FIO_CLIENT_DEF_ETA_MSEC,
46 .client_type = FIO_CLIENT_TYPE_CLI,
47};
48
49static struct timeval eta_tv;
50
51static FLIST_HEAD(client_list);
52static FLIST_HEAD(eta_list);
53
54static FLIST_HEAD(arg_list);
55
56struct thread_stat client_ts;
57struct group_run_stats client_gs;
58int sum_stat_clients;
59
60static int sum_stat_nr;
61static struct json_object *root = NULL;
62static struct json_array *clients_array = NULL;
63static struct json_array *du_array = NULL;
64
65static int error_clients;
66
67#define FIO_CLIENT_HASH_BITS 7
68#define FIO_CLIENT_HASH_SZ (1 << FIO_CLIENT_HASH_BITS)
69#define FIO_CLIENT_HASH_MASK (FIO_CLIENT_HASH_SZ - 1)
70static struct flist_head client_hash[FIO_CLIENT_HASH_SZ];
71
72static void fio_client_add_hash(struct fio_client *client)
73{
74 int bucket = hash_long(client->fd, FIO_CLIENT_HASH_BITS);
75
76 bucket &= FIO_CLIENT_HASH_MASK;
77 flist_add(&client->hash_list, &client_hash[bucket]);
78}
79
80static void fio_client_remove_hash(struct fio_client *client)
81{
82 if (!flist_empty(&client->hash_list))
83 flist_del_init(&client->hash_list);
84}
85
86static void fio_init fio_client_hash_init(void)
87{
88 int i;
89
90 for (i = 0; i < FIO_CLIENT_HASH_SZ; i++)
91 INIT_FLIST_HEAD(&client_hash[i]);
92}
93
94static int read_data(int fd, void *data, size_t size)
95{
96 ssize_t ret;
97
98 while (size) {
99 ret = read(fd, data, size);
100 if (ret < 0) {
101 if (errno == EAGAIN || errno == EINTR)
102 continue;
103 break;
104 } else if (!ret)
105 break;
106 else {
107 data += ret;
108 size -= ret;
109 }
110 }
111
112 if (size)
113 return EAGAIN;
114
115 return 0;
116}
117
118static void fio_client_json_init(void)
119{
120 if (!(output_format & FIO_OUTPUT_JSON))
121 return;
122
123 root = json_create_object();
124 json_object_add_value_string(root, "fio version", fio_version_string);
125 clients_array = json_create_array();
126 json_object_add_value_array(root, "client_stats", clients_array);
127 du_array = json_create_array();
128 json_object_add_value_array(root, "disk_util", du_array);
129}
130
131static void fio_client_json_fini(void)
132{
133 if (!(output_format & FIO_OUTPUT_JSON))
134 return;
135 log_info("\n");
136 json_print_object(root, NULL);
137 log_info("\n");
138 json_free_object(root);
139 root = NULL;
140 clients_array = NULL;
141 du_array = NULL;
142}
143
144static struct fio_client *find_client_by_fd(int fd)
145{
146 int bucket = hash_long(fd, FIO_CLIENT_HASH_BITS) & FIO_CLIENT_HASH_MASK;
147 struct fio_client *client;
148 struct flist_head *entry;
149
150 flist_for_each(entry, &client_hash[bucket]) {
151 client = flist_entry(entry, struct fio_client, hash_list);
152
153 if (client->fd == fd) {
154 client->refs++;
155 return client;
156 }
157 }
158
159 return NULL;
160}
161
162void fio_put_client(struct fio_client *client)
163{
164 if (--client->refs)
165 return;
166
167 free(client->hostname);
168 if (client->argv)
169 free(client->argv);
170 if (client->name)
171 free(client->name);
172 while (client->nr_files) {
173 struct client_file *cf = &client->files[--client->nr_files];
174
175 free(cf->file);
176 }
177 if (client->files)
178 free(client->files);
179
180 if (!client->did_stat)
181 sum_stat_clients--;
182
183 if (client->error)
184 error_clients++;
185
186 free(client);
187}
188
189static int fio_client_dec_jobs_eta(struct client_eta *eta, client_eta_op eta_fn)
190{
191 if (!--eta->pending) {
192 eta_fn(&eta->eta);
193 free(eta);
194 return 0;
195 }
196
197 return 1;
198}
199
200static void remove_client(struct fio_client *client)
201{
202 assert(client->refs);
203
204 dprint(FD_NET, "client: removed <%s>\n", client->hostname);
205
206 if (!flist_empty(&client->list))
207 flist_del_init(&client->list);
208
209 fio_client_remove_hash(client);
210
211 if (!flist_empty(&client->eta_list)) {
212 flist_del_init(&client->eta_list);
213 fio_client_dec_jobs_eta(client->eta_in_flight, client->ops->eta);
214 }
215
216 close(client->fd);
217 client->fd = -1;
218
219 if (client->ops->removed)
220 client->ops->removed(client);
221
222 nr_clients--;
223 fio_put_client(client);
224}
225
226struct fio_client *fio_get_client(struct fio_client *client)
227{
228 client->refs++;
229 return client;
230}
231
232static void __fio_client_add_cmd_option(struct fio_client *client,
233 const char *opt)
234{
235 int index;
236
237 index = client->argc++;
238 client->argv = realloc(client->argv, sizeof(char *) * client->argc);
239 client->argv[index] = strdup(opt);
240 dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
241}
242
243void fio_client_add_cmd_option(void *cookie, const char *opt)
244{
245 struct fio_client *client = cookie;
246 struct flist_head *entry;
247
248 if (!client || !opt)
249 return;
250
251 __fio_client_add_cmd_option(client, opt);
252
253 /*
254 * Duplicate arguments to shared client group
255 */
256 flist_for_each(entry, &arg_list) {
257 client = flist_entry(entry, struct fio_client, arg_list);
258
259 __fio_client_add_cmd_option(client, opt);
260 }
261}
262
263struct fio_client *fio_client_add_explicit(struct client_ops *ops,
264 const char *hostname, int type,
265 int port)
266{
267 struct fio_client *client;
268
269 client = malloc(sizeof(*client));
270 memset(client, 0, sizeof(*client));
271
272 INIT_FLIST_HEAD(&client->list);
273 INIT_FLIST_HEAD(&client->hash_list);
274 INIT_FLIST_HEAD(&client->arg_list);
275 INIT_FLIST_HEAD(&client->eta_list);
276 INIT_FLIST_HEAD(&client->cmd_list);
277
278 client->hostname = strdup(hostname);
279
280 if (type == Fio_client_socket)
281 client->is_sock = 1;
282 else {
283 int ipv6;
284
285 ipv6 = type == Fio_client_ipv6;
286 if (fio_server_parse_host(hostname, ipv6,
287 &client->addr.sin_addr,
288 &client->addr6.sin6_addr))
289 goto err;
290
291 client->port = port;
292 }
293
294 client->fd = -1;
295 client->ops = ops;
296 client->refs = 1;
297 client->type = ops->client_type;
298
299 __fio_client_add_cmd_option(client, "fio");
300
301 flist_add(&client->list, &client_list);
302 nr_clients++;
303 dprint(FD_NET, "client: added <%s>\n", client->hostname);
304 return client;
305err:
306 free(client);
307 return NULL;
308}
309
310int fio_client_add_ini_file(void *cookie, const char *ini_file, int remote)
311{
312 struct fio_client *client = cookie;
313 struct client_file *cf;
314 size_t new_size;
315 void *new_files;
316
317 if (!client)
318 return 1;
319
320 dprint(FD_NET, "client <%s>: add ini %s\n", client->hostname, ini_file);
321
322 new_size = (client->nr_files + 1) * sizeof(struct client_file);
323 new_files = realloc(client->files, new_size);
324 if (!new_files)
325 return 1;
326
327 client->files = new_files;
328 cf = &client->files[client->nr_files];
329 cf->file = strdup(ini_file);
330 cf->remote = remote;
331 client->nr_files++;
332 return 0;
333}
334
335int fio_client_add(struct client_ops *ops, const char *hostname, void **cookie)
336{
337 struct fio_client *existing = *cookie;
338 struct fio_client *client;
339
340 if (existing) {
341 /*
342 * We always add our "exec" name as the option, hence 1
343 * means empty.
344 */
345 if (existing->argc == 1)
346 flist_add_tail(&existing->arg_list, &arg_list);
347 else {
348 while (!flist_empty(&arg_list))
349 flist_del_init(arg_list.next);
350 }
351 }
352
353 client = malloc(sizeof(*client));
354 memset(client, 0, sizeof(*client));
355
356 INIT_FLIST_HEAD(&client->list);
357 INIT_FLIST_HEAD(&client->hash_list);
358 INIT_FLIST_HEAD(&client->arg_list);
359 INIT_FLIST_HEAD(&client->eta_list);
360 INIT_FLIST_HEAD(&client->cmd_list);
361
362 if (fio_server_parse_string(hostname, &client->hostname,
363 &client->is_sock, &client->port,
364 &client->addr.sin_addr,
365 &client->addr6.sin6_addr,
366 &client->ipv6))
367 return -1;
368
369 client->fd = -1;
370 client->ops = ops;
371 client->refs = 1;
372 client->type = ops->client_type;
373
374 __fio_client_add_cmd_option(client, "fio");
375
376 flist_add(&client->list, &client_list);
377 nr_clients++;
378 dprint(FD_NET, "client: added <%s>\n", client->hostname);
379 *cookie = client;
380 return 0;
381}
382
383static const char *server_name(struct fio_client *client, char *buf,
384 size_t bufsize)
385{
386 const char *from;
387
388 if (client->ipv6)
389 from = inet_ntop(AF_INET6, (struct sockaddr *) &client->addr6.sin6_addr, buf, bufsize);
390 else if (client->is_sock)
391 from = "sock";
392 else
393 from = inet_ntop(AF_INET, (struct sockaddr *) &client->addr.sin_addr, buf, bufsize);
394
395 return from;
396}
397
398static void probe_client(struct fio_client *client)
399{
400 struct cmd_client_probe_pdu pdu;
401 const char *sname;
402 uint64_t tag;
403 char buf[64];
404
405 dprint(FD_NET, "client: send probe\n");
406
407#ifdef CONFIG_ZLIB
408 pdu.flags = __le64_to_cpu(FIO_PROBE_FLAG_ZLIB);
409#else
410 pdu.flags = 0;
411#endif
412
413 sname = server_name(client, buf, sizeof(buf));
414 memset(pdu.server, 0, sizeof(pdu.server));
415 strncpy((char *) pdu.server, sname, sizeof(pdu.server) - 1);
416
417 fio_net_send_cmd(client->fd, FIO_NET_CMD_PROBE, &pdu, sizeof(pdu), &tag, &client->cmd_list);
418}
419
420static int fio_client_connect_ip(struct fio_client *client)
421{
422 struct sockaddr *addr;
423 socklen_t socklen;
424 int fd, domain;
425
426 if (client->ipv6) {
427 client->addr6.sin6_family = AF_INET6;
428 client->addr6.sin6_port = htons(client->port);
429 domain = AF_INET6;
430 addr = (struct sockaddr *) &client->addr6;
431 socklen = sizeof(client->addr6);
432 } else {
433 client->addr.sin_family = AF_INET;
434 client->addr.sin_port = htons(client->port);
435 domain = AF_INET;
436 addr = (struct sockaddr *) &client->addr;
437 socklen = sizeof(client->addr);
438 }
439
440 fd = socket(domain, SOCK_STREAM, 0);
441 if (fd < 0) {
442 int ret = -errno;
443
444 log_err("fio: socket: %s\n", strerror(errno));
445 return ret;
446 }
447
448 if (connect(fd, addr, socklen) < 0) {
449 int ret = -errno;
450
451 log_err("fio: connect: %s\n", strerror(errno));
452 log_err("fio: failed to connect to %s:%u\n", client->hostname,
453 client->port);
454 close(fd);
455 return ret;
456 }
457
458 return fd;
459}
460
461static int fio_client_connect_sock(struct fio_client *client)
462{
463 struct sockaddr_un *addr = &client->addr_un;
464 socklen_t len;
465 int fd;
466
467 memset(addr, 0, sizeof(*addr));
468 addr->sun_family = AF_UNIX;
469 strncpy(addr->sun_path, client->hostname, sizeof(addr->sun_path) - 1);
470
471 fd = socket(AF_UNIX, SOCK_STREAM, 0);
472 if (fd < 0) {
473 int ret = -errno;
474
475 log_err("fio: socket: %s\n", strerror(errno));
476 return ret;
477 }
478
479 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
480 if (connect(fd, (struct sockaddr *) addr, len) < 0) {
481 int ret = -errno;
482
483 log_err("fio: connect; %s\n", strerror(errno));
484 close(fd);
485 return ret;
486 }
487
488 return fd;
489}
490
491int fio_client_connect(struct fio_client *client)
492{
493 int fd;
494
495 dprint(FD_NET, "client: connect to host %s\n", client->hostname);
496
497 if (client->is_sock)
498 fd = fio_client_connect_sock(client);
499 else
500 fd = fio_client_connect_ip(client);
501
502 dprint(FD_NET, "client: %s connected %d\n", client->hostname, fd);
503
504 if (fd < 0)
505 return fd;
506
507 client->fd = fd;
508 fio_client_add_hash(client);
509 client->state = Client_connected;
510
511 probe_client(client);
512 return 0;
513}
514
515int fio_client_terminate(struct fio_client *client)
516{
517 return fio_net_send_quit(client->fd);
518}
519
520void fio_clients_terminate(void)
521{
522 struct flist_head *entry;
523 struct fio_client *client;
524
525 dprint(FD_NET, "client: terminate clients\n");
526
527 flist_for_each(entry, &client_list) {
528 client = flist_entry(entry, struct fio_client, list);
529 fio_client_terminate(client);
530 }
531}
532
533static void sig_int(int sig)
534{
535 dprint(FD_NET, "client: got signal %d\n", sig);
536 fio_clients_terminate();
537}
538
539static void client_signal_handler(void)
540{
541 struct sigaction act;
542
543 memset(&act, 0, sizeof(act));
544 act.sa_handler = sig_int;
545 act.sa_flags = SA_RESTART;
546 sigaction(SIGINT, &act, NULL);
547
548 memset(&act, 0, sizeof(act));
549 act.sa_handler = sig_int;
550 act.sa_flags = SA_RESTART;
551 sigaction(SIGTERM, &act, NULL);
552
553/* Windows uses SIGBREAK as a quit signal from other applications */
554#ifdef WIN32
555 memset(&act, 0, sizeof(act));
556 act.sa_handler = sig_int;
557 act.sa_flags = SA_RESTART;
558 sigaction(SIGBREAK, &act, NULL);
559#endif
560
561 memset(&act, 0, sizeof(act));
562 act.sa_handler = sig_show_status;
563 act.sa_flags = SA_RESTART;
564 sigaction(SIGUSR1, &act, NULL);
565}
566
567static int send_client_cmd_line(struct fio_client *client)
568{
569 struct cmd_single_line_pdu *cslp;
570 struct cmd_line_pdu *clp;
571 unsigned long offset;
572 unsigned int *lens;
573 void *pdu;
574 size_t mem;
575 int i, ret;
576
577 dprint(FD_NET, "client: send cmdline %d\n", client->argc);
578
579 lens = malloc(client->argc * sizeof(unsigned int));
580
581 /*
582 * Find out how much mem we need
583 */
584 for (i = 0, mem = 0; i < client->argc; i++) {
585 lens[i] = strlen(client->argv[i]) + 1;
586 mem += lens[i];
587 }
588
589 /*
590 * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu
591 */
592 mem += sizeof(*clp) + (client->argc * sizeof(*cslp));
593
594 pdu = malloc(mem);
595 clp = pdu;
596 offset = sizeof(*clp);
597
598 for (i = 0; i < client->argc; i++) {
599 uint16_t arg_len = lens[i];
600
601 cslp = pdu + offset;
602 strcpy((char *) cslp->text, client->argv[i]);
603 cslp->len = cpu_to_le16(arg_len);
604 offset += sizeof(*cslp) + arg_len;
605 }
606
607 free(lens);
608 clp->lines = cpu_to_le16(client->argc);
609 clp->client_type = __cpu_to_le16(client->type);
610 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, NULL, NULL);
611 free(pdu);
612 return ret;
613}
614
615int fio_clients_connect(void)
616{
617 struct fio_client *client;
618 struct flist_head *entry, *tmp;
619 int ret;
620
621#ifdef WIN32
622 WSADATA wsd;
623 WSAStartup(MAKEWORD(2, 2), &wsd);
624#endif
625
626 dprint(FD_NET, "client: connect all\n");
627
628 client_signal_handler();
629
630 flist_for_each_safe(entry, tmp, &client_list) {
631 client = flist_entry(entry, struct fio_client, list);
632
633 ret = fio_client_connect(client);
634 if (ret) {
635 remove_client(client);
636 continue;
637 }
638
639 if (client->argc > 1)
640 send_client_cmd_line(client);
641 }
642
643 return !nr_clients;
644}
645
646int fio_start_client(struct fio_client *client)
647{
648 dprint(FD_NET, "client: start %s\n", client->hostname);
649 return fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_RUN, 0, NULL);
650}
651
652int fio_start_all_clients(void)
653{
654 struct fio_client *client;
655 struct flist_head *entry, *tmp;
656 int ret;
657
658 dprint(FD_NET, "client: start all\n");
659
660 fio_client_json_init();
661
662 flist_for_each_safe(entry, tmp, &client_list) {
663 client = flist_entry(entry, struct fio_client, list);
664
665 ret = fio_start_client(client);
666 if (ret) {
667 remove_client(client);
668 continue;
669 }
670 }
671
672 return flist_empty(&client_list);
673}
674
675static int __fio_client_send_remote_ini(struct fio_client *client,
676 const char *filename)
677{
678 struct cmd_load_file_pdu *pdu;
679 size_t p_size;
680 int ret;
681
682 dprint(FD_NET, "send remote ini %s to %s\n", filename, client->hostname);
683
684 p_size = sizeof(*pdu) + strlen(filename) + 1;
685 pdu = malloc(p_size);
686 memset(pdu, 0, p_size);
687 pdu->name_len = strlen(filename);
688 strcpy((char *) pdu->file, filename);
689 pdu->client_type = cpu_to_le16((uint16_t) client->type);
690
691 client->sent_job = 1;
692 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_LOAD_FILE, pdu, p_size,NULL, NULL);
693 free(pdu);
694 return ret;
695}
696
697/*
698 * Send file contents to server backend. We could use sendfile(), but to remain
699 * more portable lets just read/write the darn thing.
700 */
701static int __fio_client_send_local_ini(struct fio_client *client,
702 const char *filename)
703{
704 struct cmd_job_pdu *pdu;
705 size_t p_size;
706 struct stat sb;
707 char *p;
708 void *buf;
709 off_t len;
710 int fd, ret;
711
712 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
713
714 fd = open(filename, O_RDONLY);
715 if (fd < 0) {
716 ret = -errno;
717 log_err("fio: job file <%s> open: %s\n", filename, strerror(errno));
718 return ret;
719 }
720
721 if (fstat(fd, &sb) < 0) {
722 ret = -errno;
723 log_err("fio: job file stat: %s\n", strerror(errno));
724 close(fd);
725 return ret;
726 }
727
728 p_size = sb.st_size + sizeof(*pdu);
729 pdu = malloc(p_size);
730 buf = pdu->buf;
731
732 len = sb.st_size;
733 p = buf;
734 if (read_data(fd, p, len)) {
735 log_err("fio: failed reading job file %s\n", filename);
736 close(fd);
737 free(pdu);
738 return 1;
739 }
740
741 pdu->buf_len = __cpu_to_le32(sb.st_size);
742 pdu->client_type = cpu_to_le32(client->type);
743
744 client->sent_job = 1;
745 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, pdu, p_size, NULL, NULL);
746 free(pdu);
747 close(fd);
748 return ret;
749}
750
751int fio_client_send_ini(struct fio_client *client, const char *filename,
752 int remote)
753{
754 int ret;
755
756 if (!remote)
757 ret = __fio_client_send_local_ini(client, filename);
758 else
759 ret = __fio_client_send_remote_ini(client, filename);
760
761 if (!ret)
762 client->sent_job = 1;
763
764 return ret;
765}
766
767static int fio_client_send_cf(struct fio_client *client,
768 struct client_file *cf)
769{
770 return fio_client_send_ini(client, cf->file, cf->remote);
771}
772
773int fio_clients_send_ini(const char *filename)
774{
775 struct fio_client *client;
776 struct flist_head *entry, *tmp;
777
778 flist_for_each_safe(entry, tmp, &client_list) {
779 client = flist_entry(entry, struct fio_client, list);
780
781 if (client->nr_files) {
782 int i;
783
784 for (i = 0; i < client->nr_files; i++) {
785 struct client_file *cf;
786
787 cf = &client->files[i];
788
789 if (fio_client_send_cf(client, cf)) {
790 remove_client(client);
791 break;
792 }
793 }
794 }
795 if (client->sent_job)
796 continue;
797 if (!filename || fio_client_send_ini(client, filename, 0))
798 remove_client(client);
799 }
800
801 return !nr_clients;
802}
803
804int fio_client_update_options(struct fio_client *client,
805 struct thread_options *o, uint64_t *tag)
806{
807 struct cmd_add_job_pdu pdu;
808
809 pdu.thread_number = cpu_to_le32(client->thread_number);
810 pdu.groupid = cpu_to_le32(client->groupid);
811 convert_thread_options_to_net(&pdu.top, o);
812
813 return fio_net_send_cmd(client->fd, FIO_NET_CMD_UPDATE_JOB, &pdu, sizeof(pdu), tag, &client->cmd_list);
814}
815
816static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
817{
818 dst->max_val = le64_to_cpu(src->max_val);
819 dst->min_val = le64_to_cpu(src->min_val);
820 dst->samples = le64_to_cpu(src->samples);
821
822 /*
823 * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
824 */
825 dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
826 dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
827}
828
829static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
830{
831 int i, j;
832
833 dst->error = le32_to_cpu(src->error);
834 dst->thread_number = le32_to_cpu(src->thread_number);
835 dst->groupid = le32_to_cpu(src->groupid);
836 dst->pid = le32_to_cpu(src->pid);
837 dst->members = le32_to_cpu(src->members);
838 dst->unified_rw_rep = le32_to_cpu(src->unified_rw_rep);
839
840 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
841 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
842 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
843 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
844 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
845 }
846
847 dst->usr_time = le64_to_cpu(src->usr_time);
848 dst->sys_time = le64_to_cpu(src->sys_time);
849 dst->ctx = le64_to_cpu(src->ctx);
850 dst->minf = le64_to_cpu(src->minf);
851 dst->majf = le64_to_cpu(src->majf);
852 dst->clat_percentiles = le64_to_cpu(src->clat_percentiles);
853 dst->percentile_precision = le64_to_cpu(src->percentile_precision);
854
855 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
856 fio_fp64_t *fps = &src->percentile_list[i];
857 fio_fp64_t *fpd = &dst->percentile_list[i];
858
859 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
860 }
861
862 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
863 dst->io_u_map[i] = le32_to_cpu(src->io_u_map[i]);
864 dst->io_u_submit[i] = le32_to_cpu(src->io_u_submit[i]);
865 dst->io_u_complete[i] = le32_to_cpu(src->io_u_complete[i]);
866 }
867
868 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
869 dst->io_u_lat_u[i] = le32_to_cpu(src->io_u_lat_u[i]);
870 dst->io_u_lat_m[i] = le32_to_cpu(src->io_u_lat_m[i]);
871 }
872
873 for (i = 0; i < DDIR_RWDIR_CNT; i++)
874 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
875 dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]);
876
877 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
878 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
879 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
880 dst->drop_io_u[i] = le64_to_cpu(src->drop_io_u[i]);
881 }
882
883 dst->total_submit = le64_to_cpu(src->total_submit);
884 dst->total_complete = le64_to_cpu(src->total_complete);
885
886 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
887 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
888 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
889 }
890
891 dst->total_run_time = le64_to_cpu(src->total_run_time);
892 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
893 dst->total_err_count = le64_to_cpu(src->total_err_count);
894 dst->first_error = le32_to_cpu(src->first_error);
895 dst->kb_base = le32_to_cpu(src->kb_base);
896 dst->unit_base = le32_to_cpu(src->unit_base);
897
898 dst->latency_depth = le32_to_cpu(src->latency_depth);
899 dst->latency_target = le64_to_cpu(src->latency_target);
900 dst->latency_window = le64_to_cpu(src->latency_window);
901 dst->latency_percentile.u.f = fio_uint64_to_double(le64_to_cpu(src->latency_percentile.u.i));
902
903 dst->nr_block_infos = le64_to_cpu(src->nr_block_infos);
904 for (i = 0; i < dst->nr_block_infos; i++)
905 dst->block_infos[i] = le32_to_cpu(src->block_infos[i]);
906}
907
908static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
909{
910 int i;
911
912 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
913 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
914 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
915 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
916 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
917 dst->io_kb[i] = le64_to_cpu(src->io_kb[i]);
918 dst->agg[i] = le64_to_cpu(src->agg[i]);
919 }
920
921 dst->kb_base = le32_to_cpu(src->kb_base);
922 dst->unit_base = le32_to_cpu(src->unit_base);
923 dst->groupid = le32_to_cpu(src->groupid);
924 dst->unified_rw_rep = le32_to_cpu(src->unified_rw_rep);
925}
926
927static void json_object_add_client_info(struct json_object *obj,
928 struct fio_client *client)
929{
930 const char *hostname = client->hostname ? client->hostname : "";
931
932 json_object_add_value_string(obj, "hostname", hostname);
933 json_object_add_value_int(obj, "port", client->port);
934}
935
936static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd)
937{
938 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
939 struct json_object *tsobj;
940
941 tsobj = show_thread_status(&p->ts, &p->rs, NULL);
942 client->did_stat = 1;
943 if (tsobj) {
944 json_object_add_client_info(tsobj, client);
945 json_array_add_value_object(clients_array, tsobj);
946 }
947
948 if (sum_stat_clients <= 1)
949 return;
950
951 sum_thread_stats(&client_ts, &p->ts, sum_stat_nr == 1);
952 sum_group_stats(&client_gs, &p->rs);
953
954 client_ts.members++;
955 client_ts.thread_number = p->ts.thread_number;
956 client_ts.groupid = p->ts.groupid;
957 client_ts.unified_rw_rep = p->ts.unified_rw_rep;
958
959 if (++sum_stat_nr == sum_stat_clients) {
960 strcpy(client_ts.name, "All clients");
961 tsobj = show_thread_status(&client_ts, &client_gs, NULL);
962 if (tsobj) {
963 json_object_add_client_info(tsobj, client);
964 json_array_add_value_object(clients_array, tsobj);
965 }
966 }
967}
968
969static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd)
970{
971 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
972
973 if (output_format & FIO_OUTPUT_NORMAL)
974 show_group_stats(gs, NULL);
975}
976
977static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd)
978{
979 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
980 const char *buf = (const char *) pdu->buf;
981 const char *name;
982 int fio_unused ret;
983
984 name = client->name ? client->name : client->hostname;
985
986 if (!client->skip_newline)
987 fprintf(f_out, "<%s> ", name);
988 ret = fwrite(buf, pdu->buf_len, 1, f_out);
989 fflush(f_out);
990 client->skip_newline = strchr(buf, '\n') == NULL;
991}
992
993static void convert_agg(struct disk_util_agg *agg)
994{
995 int i;
996
997 for (i = 0; i < 2; i++) {
998 agg->ios[i] = le64_to_cpu(agg->ios[i]);
999 agg->merges[i] = le64_to_cpu(agg->merges[i]);
1000 agg->sectors[i] = le64_to_cpu(agg->sectors[i]);
1001 agg->ticks[i] = le64_to_cpu(agg->ticks[i]);
1002 }
1003
1004 agg->io_ticks = le64_to_cpu(agg->io_ticks);
1005 agg->time_in_queue = le64_to_cpu(agg->time_in_queue);
1006 agg->slavecount = le32_to_cpu(agg->slavecount);
1007 agg->max_util.u.f = fio_uint64_to_double(le64_to_cpu(agg->max_util.u.i));
1008}
1009
1010static void convert_dus(struct disk_util_stat *dus)
1011{
1012 int i;
1013
1014 for (i = 0; i < 2; i++) {
1015 dus->s.ios[i] = le64_to_cpu(dus->s.ios[i]);
1016 dus->s.merges[i] = le64_to_cpu(dus->s.merges[i]);
1017 dus->s.sectors[i] = le64_to_cpu(dus->s.sectors[i]);
1018 dus->s.ticks[i] = le64_to_cpu(dus->s.ticks[i]);
1019 }
1020
1021 dus->s.io_ticks = le64_to_cpu(dus->s.io_ticks);
1022 dus->s.time_in_queue = le64_to_cpu(dus->s.time_in_queue);
1023 dus->s.msec = le64_to_cpu(dus->s.msec);
1024}
1025
1026static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd)
1027{
1028 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
1029
1030 if (!client->disk_stats_shown) {
1031 client->disk_stats_shown = 1;
1032 log_info("\nDisk stats (read/write):\n");
1033 }
1034
1035 if (output_format & FIO_OUTPUT_JSON) {
1036 struct json_object *duobj;
1037 json_array_add_disk_util(&du->dus, &du->agg, du_array);
1038 duobj = json_array_last_value_object(du_array);
1039 json_object_add_client_info(duobj, client);
1040 }
1041 if (output_format & FIO_OUTPUT_TERSE)
1042 print_disk_util(&du->dus, &du->agg, 1, NULL);
1043 if (output_format & FIO_OUTPUT_NORMAL)
1044 print_disk_util(&du->dus, &du->agg, 0, NULL);
1045}
1046
1047static void convert_jobs_eta(struct jobs_eta *je)
1048{
1049 int i;
1050
1051 je->nr_running = le32_to_cpu(je->nr_running);
1052 je->nr_ramp = le32_to_cpu(je->nr_ramp);
1053 je->nr_pending = le32_to_cpu(je->nr_pending);
1054 je->nr_setting_up = le32_to_cpu(je->nr_setting_up);
1055 je->files_open = le32_to_cpu(je->files_open);
1056
1057 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1058 je->m_rate[i] = le32_to_cpu(je->m_rate[i]);
1059 je->t_rate[i] = le32_to_cpu(je->t_rate[i]);
1060 je->m_iops[i] = le32_to_cpu(je->m_iops[i]);
1061 je->t_iops[i] = le32_to_cpu(je->t_iops[i]);
1062 je->rate[i] = le32_to_cpu(je->rate[i]);
1063 je->iops[i] = le32_to_cpu(je->iops[i]);
1064 }
1065
1066 je->elapsed_sec = le64_to_cpu(je->elapsed_sec);
1067 je->eta_sec = le64_to_cpu(je->eta_sec);
1068 je->nr_threads = le32_to_cpu(je->nr_threads);
1069 je->is_pow2 = le32_to_cpu(je->is_pow2);
1070 je->unit_base = le32_to_cpu(je->unit_base);
1071}
1072
1073void fio_client_sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je)
1074{
1075 int i;
1076
1077 dst->nr_running += je->nr_running;
1078 dst->nr_ramp += je->nr_ramp;
1079 dst->nr_pending += je->nr_pending;
1080 dst->nr_setting_up += je->nr_setting_up;
1081 dst->files_open += je->files_open;
1082
1083 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1084 dst->m_rate[i] += je->m_rate[i];
1085 dst->t_rate[i] += je->t_rate[i];
1086 dst->m_iops[i] += je->m_iops[i];
1087 dst->t_iops[i] += je->t_iops[i];
1088 dst->rate[i] += je->rate[i];
1089 dst->iops[i] += je->iops[i];
1090 }
1091
1092 dst->elapsed_sec += je->elapsed_sec;
1093
1094 if (je->eta_sec > dst->eta_sec)
1095 dst->eta_sec = je->eta_sec;
1096
1097 dst->nr_threads += je->nr_threads;
1098
1099 /*
1100 * This wont be correct for multiple strings, but at least it
1101 * works for the basic cases.
1102 */
1103 strcpy((char *) dst->run_str, (char *) je->run_str);
1104}
1105
1106static void remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd)
1107{
1108 struct fio_net_cmd_reply *reply = NULL;
1109 struct flist_head *entry;
1110
1111 flist_for_each(entry, &client->cmd_list) {
1112 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
1113
1114 if (cmd->tag == (uintptr_t) reply)
1115 break;
1116
1117 reply = NULL;
1118 }
1119
1120 if (!reply) {
1121 log_err("fio: client: unable to find matching tag (%llx)\n", (unsigned long long) cmd->tag);
1122 return;
1123 }
1124
1125 flist_del(&reply->list);
1126 cmd->tag = reply->saved_tag;
1127 free(reply);
1128}
1129
1130int fio_client_wait_for_reply(struct fio_client *client, uint64_t tag)
1131{
1132 do {
1133 struct fio_net_cmd_reply *reply = NULL;
1134 struct flist_head *entry;
1135
1136 flist_for_each(entry, &client->cmd_list) {
1137 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
1138
1139 if (tag == (uintptr_t) reply)
1140 break;
1141
1142 reply = NULL;
1143 }
1144
1145 if (!reply)
1146 break;
1147
1148 usleep(1000);
1149 } while (1);
1150
1151 return 0;
1152}
1153
1154static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd)
1155{
1156 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
1157 struct client_eta *eta = (struct client_eta *) (uintptr_t) cmd->tag;
1158
1159 dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending);
1160
1161 assert(client->eta_in_flight == eta);
1162
1163 client->eta_in_flight = NULL;
1164 flist_del_init(&client->eta_list);
1165 client->eta_timeouts = 0;
1166
1167 if (client->ops->jobs_eta)
1168 client->ops->jobs_eta(client, je);
1169
1170 fio_client_sum_jobs_eta(&eta->eta, je);
1171 fio_client_dec_jobs_eta(eta, client->ops->eta);
1172}
1173
1174static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
1175{
1176 struct cmd_probe_reply_pdu *probe = (struct cmd_probe_reply_pdu *) cmd->payload;
1177 const char *os, *arch;
1178 char bit[16];
1179
1180 os = fio_get_os_string(probe->os);
1181 if (!os)
1182 os = "unknown";
1183
1184 arch = fio_get_arch_string(probe->arch);
1185 if (!arch)
1186 os = "unknown";
1187
1188 sprintf(bit, "%d-bit", probe->bpp * 8);
1189 probe->flags = le64_to_cpu(probe->flags);
1190
1191 log_info("hostname=%s, be=%u, %s, os=%s, arch=%s, fio=%s, flags=%lx\n",
1192 probe->hostname, probe->bigendian, bit, os, arch,
1193 probe->fio_version, (unsigned long) probe->flags);
1194
1195 if (!client->name)
1196 client->name = strdup((char *) probe->hostname);
1197}
1198
1199static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd)
1200{
1201 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1202
1203 client->state = Client_started;
1204 client->jobs = le32_to_cpu(pdu->jobs);
1205 client->nr_stat = le32_to_cpu(pdu->stat_outputs);
1206
1207 sum_stat_clients += client->nr_stat;
1208}
1209
1210static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd)
1211{
1212 if (client->error)
1213 log_info("client <%s>: exited with error %d\n", client->hostname, client->error);
1214}
1215
1216static void convert_stop(struct fio_net_cmd *cmd)
1217{
1218 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1219
1220 pdu->error = le32_to_cpu(pdu->error);
1221}
1222
1223static void convert_text(struct fio_net_cmd *cmd)
1224{
1225 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
1226
1227 pdu->level = le32_to_cpu(pdu->level);
1228 pdu->buf_len = le32_to_cpu(pdu->buf_len);
1229 pdu->log_sec = le64_to_cpu(pdu->log_sec);
1230 pdu->log_usec = le64_to_cpu(pdu->log_usec);
1231}
1232
1233static struct cmd_iolog_pdu *convert_iolog_gz(struct fio_net_cmd *cmd,
1234 struct cmd_iolog_pdu *pdu)
1235{
1236#ifdef CONFIG_ZLIB
1237 struct cmd_iolog_pdu *ret;
1238 z_stream stream;
1239 uint32_t nr_samples;
1240 size_t total;
1241 void *p;
1242
1243 stream.zalloc = Z_NULL;
1244 stream.zfree = Z_NULL;
1245 stream.opaque = Z_NULL;
1246 stream.avail_in = 0;
1247 stream.next_in = Z_NULL;
1248
1249 if (inflateInit(&stream) != Z_OK)
1250 return NULL;
1251
1252 /*
1253 * Get header first, it's not compressed
1254 */
1255 nr_samples = le64_to_cpu(pdu->nr_samples);
1256
1257 total = nr_samples * __log_entry_sz(le32_to_cpu(pdu->log_offset));
1258 ret = malloc(total + sizeof(*pdu));
1259 ret->nr_samples = nr_samples;
1260
1261 memcpy(ret, pdu, sizeof(*pdu));
1262
1263 p = (void *) ret + sizeof(*pdu);
1264
1265 stream.avail_in = cmd->pdu_len - sizeof(*pdu);
1266 stream.next_in = (void *) pdu + sizeof(*pdu);
1267 while (stream.avail_in) {
1268 unsigned int this_chunk = 65536;
1269 unsigned int this_len;
1270 int err;
1271
1272 if (this_chunk > total)
1273 this_chunk = total;
1274
1275 stream.avail_out = this_chunk;
1276 stream.next_out = p;
1277 err = inflate(&stream, Z_NO_FLUSH);
1278 /* may be Z_OK, or Z_STREAM_END */
1279 if (err < 0) {
1280 log_err("fio: inflate error %d\n", err);
1281 free(ret);
1282 ret = NULL;
1283 goto err;
1284 }
1285
1286 this_len = this_chunk - stream.avail_out;
1287 p += this_len;
1288 total -= this_len;
1289 }
1290
1291err:
1292 inflateEnd(&stream);
1293 return ret;
1294#else
1295 return NULL;
1296#endif
1297}
1298
1299/*
1300 * This has been compressed on the server side, since it can be big.
1301 * Uncompress here.
1302 */
1303static struct cmd_iolog_pdu *convert_iolog(struct fio_net_cmd *cmd)
1304{
1305 struct cmd_iolog_pdu *pdu = (struct cmd_iolog_pdu *) cmd->payload;
1306 struct cmd_iolog_pdu *ret;
1307 uint64_t i;
1308 void *samples;
1309
1310 /*
1311 * Convert if compressed and we support it. If it's not
1312 * compressed, we need not do anything.
1313 */
1314 if (le32_to_cpu(pdu->compressed)) {
1315#ifndef CONFIG_ZLIB
1316 log_err("fio: server sent compressed data by mistake\n");
1317 return NULL;
1318#endif
1319 ret = convert_iolog_gz(cmd, pdu);
1320 if (!ret) {
1321 log_err("fio: failed decompressing log\n");
1322 return NULL;
1323 }
1324 } else
1325 ret = pdu;
1326
1327 ret->nr_samples = le64_to_cpu(ret->nr_samples);
1328 ret->thread_number = le32_to_cpu(ret->thread_number);
1329 ret->log_type = le32_to_cpu(ret->log_type);
1330 ret->compressed = le32_to_cpu(ret->compressed);
1331 ret->log_offset = le32_to_cpu(ret->log_offset);
1332
1333 samples = &ret->samples[0];
1334 for (i = 0; i < ret->nr_samples; i++) {
1335 struct io_sample *s;
1336
1337 s = __get_sample(samples, ret->log_offset, i);
1338 s->time = le64_to_cpu(s->time);
1339 s->val = le64_to_cpu(s->val);
1340 s->__ddir = le32_to_cpu(s->__ddir);
1341 s->bs = le32_to_cpu(s->bs);
1342
1343 if (ret->log_offset) {
1344 struct io_sample_offset *so = (void *) s;
1345
1346 so->offset = le64_to_cpu(so->offset);
1347 }
1348 }
1349
1350 return ret;
1351}
1352
1353static void sendfile_reply(int fd, struct cmd_sendfile_reply *rep,
1354 size_t size, uint64_t tag)
1355{
1356 rep->error = cpu_to_le32(rep->error);
1357 fio_net_send_cmd(fd, FIO_NET_CMD_SENDFILE, rep, size, &tag, NULL);
1358}
1359
1360static int send_file(struct fio_client *client, struct cmd_sendfile *pdu,
1361 uint64_t tag)
1362{
1363 struct cmd_sendfile_reply *rep;
1364 struct stat sb;
1365 size_t size;
1366 int fd;
1367
1368 size = sizeof(*rep);
1369 rep = malloc(size);
1370
1371 if (stat((char *)pdu->path, &sb) < 0) {
1372fail:
1373 rep->error = errno;
1374 sendfile_reply(client->fd, rep, size, tag);
1375 free(rep);
1376 return 1;
1377 }
1378
1379 size += sb.st_size;
1380 rep = realloc(rep, size);
1381 rep->size = cpu_to_le32((uint32_t) sb.st_size);
1382
1383 fd = open((char *)pdu->path, O_RDONLY);
1384 if (fd == -1 )
1385 goto fail;
1386
1387 rep->error = read_data(fd, &rep->data, sb.st_size);
1388 sendfile_reply(client->fd, rep, size, tag);
1389 free(rep);
1390 close(fd);
1391 return 0;
1392}
1393
1394int fio_handle_client(struct fio_client *client)
1395{
1396 struct client_ops *ops = client->ops;
1397 struct fio_net_cmd *cmd;
1398
1399 dprint(FD_NET, "client: handle %s\n", client->hostname);
1400
1401 cmd = fio_net_recv_cmd(client->fd);
1402 if (!cmd)
1403 return 0;
1404
1405 dprint(FD_NET, "client: got cmd op %s from %s (pdu=%u)\n",
1406 fio_server_op(cmd->opcode), client->hostname, cmd->pdu_len);
1407
1408 switch (cmd->opcode) {
1409 case FIO_NET_CMD_QUIT:
1410 if (ops->quit)
1411 ops->quit(client, cmd);
1412 remove_client(client);
1413 break;
1414 case FIO_NET_CMD_TEXT:
1415 convert_text(cmd);
1416 ops->text(client, cmd);
1417 break;
1418 case FIO_NET_CMD_DU: {
1419 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
1420
1421 convert_dus(&du->dus);
1422 convert_agg(&du->agg);
1423
1424 ops->disk_util(client, cmd);
1425 break;
1426 }
1427 case FIO_NET_CMD_TS: {
1428 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
1429
1430 convert_ts(&p->ts, &p->ts);
1431 convert_gs(&p->rs, &p->rs);
1432
1433 ops->thread_status(client, cmd);
1434 break;
1435 }
1436 case FIO_NET_CMD_GS: {
1437 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
1438
1439 convert_gs(gs, gs);
1440
1441 ops->group_stats(client, cmd);
1442 break;
1443 }
1444 case FIO_NET_CMD_ETA: {
1445 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
1446
1447 remove_reply_cmd(client, cmd);
1448 convert_jobs_eta(je);
1449 handle_eta(client, cmd);
1450 break;
1451 }
1452 case FIO_NET_CMD_PROBE:
1453 remove_reply_cmd(client, cmd);
1454 ops->probe(client, cmd);
1455 break;
1456 case FIO_NET_CMD_SERVER_START:
1457 client->state = Client_running;
1458 if (ops->job_start)
1459 ops->job_start(client, cmd);
1460 break;
1461 case FIO_NET_CMD_START: {
1462 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1463
1464 pdu->jobs = le32_to_cpu(pdu->jobs);
1465 ops->start(client, cmd);
1466 break;
1467 }
1468 case FIO_NET_CMD_STOP: {
1469 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1470
1471 convert_stop(cmd);
1472 client->state = Client_stopped;
1473 client->error = le32_to_cpu(pdu->error);
1474 client->signal = le32_to_cpu(pdu->signal);
1475 ops->stop(client, cmd);
1476 break;
1477 }
1478 case FIO_NET_CMD_ADD_JOB: {
1479 struct cmd_add_job_pdu *pdu = (struct cmd_add_job_pdu *) cmd->payload;
1480
1481 client->thread_number = le32_to_cpu(pdu->thread_number);
1482 client->groupid = le32_to_cpu(pdu->groupid);
1483
1484 if (ops->add_job)
1485 ops->add_job(client, cmd);
1486 break;
1487 }
1488 case FIO_NET_CMD_IOLOG:
1489 if (ops->iolog) {
1490 struct cmd_iolog_pdu *pdu;
1491
1492 pdu = convert_iolog(cmd);
1493 ops->iolog(client, pdu);
1494 }
1495 break;
1496 case FIO_NET_CMD_UPDATE_JOB:
1497 ops->update_job(client, cmd);
1498 remove_reply_cmd(client, cmd);
1499 break;
1500 case FIO_NET_CMD_VTRIGGER: {
1501 struct all_io_list *pdu = (struct all_io_list *) cmd->payload;
1502 char buf[128];
1503 int off = 0;
1504
1505 if (aux_path) {
1506 strcpy(buf, aux_path);
1507 off = strlen(buf);
1508 }
1509
1510 __verify_save_state(pdu, server_name(client, &buf[off], sizeof(buf) - off));
1511 exec_trigger(trigger_cmd);
1512 break;
1513 }
1514 case FIO_NET_CMD_SENDFILE: {
1515 struct cmd_sendfile *pdu = (struct cmd_sendfile *) cmd->payload;
1516 send_file(client, pdu, cmd->tag);
1517 break;
1518 }
1519 default:
1520 log_err("fio: unknown client op: %s\n", fio_server_op(cmd->opcode));
1521 break;
1522 }
1523
1524 free(cmd);
1525 return 1;
1526}
1527
1528int fio_clients_send_trigger(const char *cmd)
1529{
1530 struct flist_head *entry;
1531 struct fio_client *client;
1532 size_t slen;
1533
1534 dprint(FD_NET, "client: send vtrigger: %s\n", cmd);
1535
1536 if (!cmd)
1537 slen = 0;
1538 else
1539 slen = strlen(cmd);
1540
1541 flist_for_each(entry, &client_list) {
1542 struct cmd_vtrigger_pdu *pdu;
1543
1544 client = flist_entry(entry, struct fio_client, list);
1545
1546 pdu = malloc(sizeof(*pdu) + slen);
1547 pdu->len = cpu_to_le16((uint16_t) slen);
1548 if (slen)
1549 memcpy(pdu->cmd, cmd, slen);
1550 fio_net_send_cmd(client->fd, FIO_NET_CMD_VTRIGGER, pdu,
1551 sizeof(*pdu) + slen, NULL, NULL);
1552 free(pdu);
1553 }
1554
1555 return 0;
1556}
1557
1558static void request_client_etas(struct client_ops *ops)
1559{
1560 struct fio_client *client;
1561 struct flist_head *entry;
1562 struct client_eta *eta;
1563 int skipped = 0;
1564
1565 dprint(FD_NET, "client: request eta (%d)\n", nr_clients);
1566
1567 eta = calloc(1, sizeof(*eta) + __THREAD_RUNSTR_SZ(REAL_MAX_JOBS));
1568 eta->pending = nr_clients;
1569
1570 flist_for_each(entry, &client_list) {
1571 client = flist_entry(entry, struct fio_client, list);
1572
1573 if (!flist_empty(&client->eta_list)) {
1574 skipped++;
1575 continue;
1576 }
1577 if (client->state != Client_running)
1578 continue;
1579
1580 assert(!client->eta_in_flight);
1581 flist_add_tail(&client->eta_list, &eta_list);
1582 client->eta_in_flight = eta;
1583 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA,
1584 (uintptr_t) eta, &client->cmd_list);
1585 }
1586
1587 while (skipped--) {
1588 if (!fio_client_dec_jobs_eta(eta, ops->eta))
1589 break;
1590 }
1591
1592 dprint(FD_NET, "client: requested eta tag %p\n", eta);
1593}
1594
1595/*
1596 * A single SEND_ETA timeout isn't fatal. Attempt to recover.
1597 */
1598static int handle_cmd_timeout(struct fio_client *client,
1599 struct fio_net_cmd_reply *reply)
1600{
1601 if (reply->opcode != FIO_NET_CMD_SEND_ETA)
1602 return 1;
1603
1604 log_info("client <%s>: timeout on SEND_ETA\n", client->hostname);
1605 flist_del(&reply->list);
1606 free(reply);
1607
1608 flist_del_init(&client->eta_list);
1609 if (client->eta_in_flight) {
1610 fio_client_dec_jobs_eta(client->eta_in_flight, client->ops->eta);
1611 client->eta_in_flight = NULL;
1612 }
1613
1614 /*
1615 * If we fail 5 in a row, give up...
1616 */
1617 if (client->eta_timeouts++ > 5)
1618 return 1;
1619
1620 return 0;
1621}
1622
1623static int client_check_cmd_timeout(struct fio_client *client,
1624 struct timeval *now)
1625{
1626 struct fio_net_cmd_reply *reply;
1627 struct flist_head *entry, *tmp;
1628 int ret = 0;
1629
1630 flist_for_each_safe(entry, tmp, &client->cmd_list) {
1631 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
1632
1633 if (mtime_since(&reply->tv, now) < FIO_NET_CLIENT_TIMEOUT)
1634 continue;
1635
1636 if (!handle_cmd_timeout(client, reply))
1637 continue;
1638
1639 log_err("fio: client %s, timeout on cmd %s\n", client->hostname,
1640 fio_server_op(reply->opcode));
1641 flist_del(&reply->list);
1642 free(reply);
1643 ret = 1;
1644 }
1645
1646 return flist_empty(&client->cmd_list) && ret;
1647}
1648
1649static int fio_check_clients_timed_out(void)
1650{
1651 struct fio_client *client;
1652 struct flist_head *entry, *tmp;
1653 struct timeval tv;
1654 int ret = 0;
1655
1656 fio_gettime(&tv, NULL);
1657
1658 flist_for_each_safe(entry, tmp, &client_list) {
1659 client = flist_entry(entry, struct fio_client, list);
1660
1661 if (flist_empty(&client->cmd_list))
1662 continue;
1663
1664 if (!client_check_cmd_timeout(client, &tv))
1665 continue;
1666
1667 if (client->ops->timed_out)
1668 client->ops->timed_out(client);
1669 else
1670 log_err("fio: client %s timed out\n", client->hostname);
1671
1672 client->error = ETIMEDOUT;
1673 remove_client(client);
1674 ret = 1;
1675 }
1676
1677 return ret;
1678}
1679
1680int fio_handle_clients(struct client_ops *ops)
1681{
1682 struct pollfd *pfds;
1683 int i, ret = 0, retval = 0;
1684
1685 fio_gettime(&eta_tv, NULL);
1686
1687 pfds = malloc(nr_clients * sizeof(struct pollfd));
1688
1689 init_thread_stat(&client_ts);
1690 init_group_run_stat(&client_gs);
1691
1692 while (!exit_backend && nr_clients) {
1693 struct flist_head *entry, *tmp;
1694 struct fio_client *client;
1695
1696 i = 0;
1697 flist_for_each_safe(entry, tmp, &client_list) {
1698 client = flist_entry(entry, struct fio_client, list);
1699
1700 if (!client->sent_job && !client->ops->stay_connected &&
1701 flist_empty(&client->cmd_list)) {
1702 remove_client(client);
1703 continue;
1704 }
1705
1706 pfds[i].fd = client->fd;
1707 pfds[i].events = POLLIN;
1708 i++;
1709 }
1710
1711 if (!nr_clients)
1712 break;
1713
1714 assert(i == nr_clients);
1715
1716 do {
1717 struct timeval tv;
1718 int timeout;
1719
1720 fio_gettime(&tv, NULL);
1721 if (mtime_since(&eta_tv, &tv) >= 900) {
1722 request_client_etas(ops);
1723 memcpy(&eta_tv, &tv, sizeof(tv));
1724
1725 if (fio_check_clients_timed_out())
1726 break;
1727 }
1728
1729 check_trigger_file();
1730
1731 timeout = min(100u, ops->eta_msec);
1732
1733 ret = poll(pfds, nr_clients, timeout);
1734 if (ret < 0) {
1735 if (errno == EINTR)
1736 continue;
1737 log_err("fio: poll clients: %s\n", strerror(errno));
1738 break;
1739 } else if (!ret)
1740 continue;
1741 } while (ret <= 0);
1742
1743 for (i = 0; i < nr_clients; i++) {
1744 if (!(pfds[i].revents & POLLIN))
1745 continue;
1746
1747 client = find_client_by_fd(pfds[i].fd);
1748 if (!client) {
1749 log_err("fio: unknown client fd %ld\n", (long) pfds[i].fd);
1750 continue;
1751 }
1752 if (!fio_handle_client(client)) {
1753 log_info("client: host=%s disconnected\n",
1754 client->hostname);
1755 remove_client(client);
1756 retval = 1;
1757 } else if (client->error)
1758 retval = 1;
1759 fio_put_client(client);
1760 }
1761 }
1762
1763 fio_client_json_fini();
1764
1765 free(pfds);
1766 return retval || error_clients;
1767}