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