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