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