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