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