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