zbd: Fix max_open_zones checks
[fio.git] / client.c
CommitLineData
132159a5
JA
1#include <stdio.h>
2#include <stdlib.h>
3#include <unistd.h>
132159a5
JA
4#include <errno.h>
5#include <fcntl.h>
8393ca93 6#include <poll.h>
132159a5
JA
7#include <sys/types.h>
8#include <sys/stat.h>
d05c4a03 9#include <sys/socket.h>
87aa8f19 10#include <sys/un.h>
132159a5
JA
11#include <netinet/in.h>
12#include <arpa/inet.h>
13#include <netdb.h>
9e22ecb0 14#include <signal.h>
3989b143 15#ifdef CONFIG_ZLIB
1b42725f 16#include <zlib.h>
3989b143 17#endif
132159a5
JA
18
19#include "fio.h"
dd366728 20#include "client.h"
132159a5 21#include "server.h"
b66570dc 22#include "flist.h"
3c5f57e3 23#include "hash.h"
3d2d14bc 24#include "verify-state.h"
132159a5 25
dd366728 26static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd);
89e5fad9
JA
27static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd);
28static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd);
dd366728 29static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd);
084d1c6f 30static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd);
36ed2f9a 31static void handle_stop(struct fio_client *client);
85dd01e7 32static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd);
dd366728 33
54e62ad4 34static void convert_text(struct fio_net_cmd *cmd);
44f4f5e6 35static void client_display_thread_status(struct jobs_eta *je);
54e62ad4 36
dd366728 37struct client_ops fio_client_ops = {
35c0ba7f 38 .text = handle_text,
0420ba6a
JA
39 .disk_util = handle_du,
40 .thread_status = handle_ts,
41 .group_stats = handle_gs,
6b79c80c 42 .stop = handle_stop,
85dd01e7 43 .start = handle_start,
44f4f5e6 44 .eta = client_display_thread_status,
0420ba6a 45 .probe = handle_probe,
6433ee05 46 .eta_msec = FIO_CLIENT_DEF_ETA_MSEC,
46bcd498 47 .client_type = FIO_CLIENT_TYPE_CLI,
dd366728
SC
48};
49
8b6a404c 50static struct timespec eta_ts;
48fbb46e 51
b66570dc 52static FLIST_HEAD(client_list);
82c1ed38 53static FLIST_HEAD(eta_list);
b66570dc 54
3f3a4542
JA
55static FLIST_HEAD(arg_list);
56
3650a3ca
JA
57struct thread_stat client_ts;
58struct group_run_stats client_gs;
59int sum_stat_clients;
60
37f0c1ae 61static int sum_stat_nr;
c2b5ddb2 62static struct buf_output allclients;
952b05e0 63static struct json_object *root = NULL;
0279b880 64static struct json_object *job_opt_object = NULL;
952b05e0
CF
65static struct json_array *clients_array = NULL;
66static struct json_array *du_array = NULL;
37f0c1ae 67
f9a33cc0
JA
68static int error_clients;
69
3c5f57e3
JA
70#define FIO_CLIENT_HASH_BITS 7
71#define FIO_CLIENT_HASH_SZ (1 << FIO_CLIENT_HASH_BITS)
72#define FIO_CLIENT_HASH_MASK (FIO_CLIENT_HASH_SZ - 1)
bebe6398 73static struct flist_head client_hash[FIO_CLIENT_HASH_SZ];
3c5f57e3 74
0cba0f91
JA
75static struct cmd_iolog_pdu *convert_iolog(struct fio_net_cmd *, bool *);
76
bebe6398 77static void fio_client_add_hash(struct fio_client *client)
3c5f57e3
JA
78{
79 int bucket = hash_long(client->fd, FIO_CLIENT_HASH_BITS);
80
81 bucket &= FIO_CLIENT_HASH_MASK;
bebe6398 82 flist_add(&client->hash_list, &client_hash[bucket]);
3c5f57e3
JA
83}
84
bebe6398 85static void fio_client_remove_hash(struct fio_client *client)
3c5f57e3 86{
bebe6398
JA
87 if (!flist_empty(&client->hash_list))
88 flist_del_init(&client->hash_list);
3c5f57e3
JA
89}
90
91static void fio_init fio_client_hash_init(void)
92{
93 int i;
94
bebe6398
JA
95 for (i = 0; i < FIO_CLIENT_HASH_SZ; i++)
96 INIT_FLIST_HEAD(&client_hash[i]);
3c5f57e3
JA
97}
98
e5aa68b3
JA
99static int read_data(int fd, void *data, size_t size)
100{
101 ssize_t ret;
102
103 while (size) {
104 ret = read(fd, data, size);
105 if (ret < 0) {
106 if (errno == EAGAIN || errno == EINTR)
107 continue;
108 break;
109 } else if (!ret)
110 break;
111 else {
112 data += ret;
113 size -= ret;
114 }
115 }
116
117 if (size)
118 return EAGAIN;
119
120 return 0;
121}
122
b4f5e72f
TK
123static int read_ini_data(int fd, void *data, size_t size)
124{
125 char *p = data;
126 int ret = 0;
127 FILE *fp;
ec9e1334 128 int dupfd;
b4f5e72f 129
ec9e1334
JA
130 dupfd = dup(fd);
131 if (dupfd < 0)
132 return errno;
133
134 fp = fdopen(dupfd, "r");
af84cd66
JA
135 if (!fp) {
136 ret = errno;
137 close(dupfd);
138 goto out;
139 }
b4f5e72f
TK
140
141 while (1) {
142 ssize_t len;
143 char buf[OPT_LEN_MAX+1], *sub;
144
145 if (!fgets(buf, sizeof(buf), fp)) {
146 if (ferror(fp)) {
147 if (errno == EAGAIN || errno == EINTR)
148 continue;
149 ret = errno;
150 }
151 break;
152 }
153
154 sub = fio_option_dup_subs(buf);
155 len = strlen(sub);
156 if (len + 1 > size) {
157 log_err("fio: no space left to read data\n");
158 free(sub);
159 ret = ENOSPC;
160 break;
161 }
162
163 memcpy(p, sub, len);
164 free(sub);
165 p += len;
166 *p = '\0';
167 size -= len;
168 }
169
170 fclose(fp);
af84cd66 171out:
b4f5e72f
TK
172 return ret;
173}
174
952b05e0
CF
175static void fio_client_json_init(void)
176{
0279b880
JA
177 char time_buf[32];
178 time_t time_p;
179
01cfefcc 180 if (!(output_format & FIO_OUTPUT_JSON))
952b05e0 181 return;
84f1f865 182
0279b880
JA
183 time(&time_p);
184 os_ctime_r((const time_t *) &time_p, time_buf, sizeof(time_buf));
185 time_buf[strlen(time_buf) - 1] = '\0';
186
952b05e0
CF
187 root = json_create_object();
188 json_object_add_value_string(root, "fio version", fio_version_string);
0279b880
JA
189 json_object_add_value_int(root, "timestamp", time_p);
190 json_object_add_value_string(root, "time", time_buf);
191
192 job_opt_object = json_create_object();
193 json_object_add_value_object(root, "global options", job_opt_object);
952b05e0
CF
194 clients_array = json_create_array();
195 json_object_add_value_array(root, "client_stats", clients_array);
196 du_array = json_create_array();
197 json_object_add_value_array(root, "disk_util", du_array);
198}
199
200static void fio_client_json_fini(void)
201{
83742c72
JA
202 struct buf_output out;
203
2eca4892 204 if (!root)
952b05e0 205 return;
0279b880 206
83742c72 207 buf_output_init(&out);
2eca4892 208
83742c72
JA
209 __log_buf(&out, "\n");
210 json_print_object(root, &out);
211 __log_buf(&out, "\n");
212 log_info_buf(out.buf, out.buflen);
2eca4892 213
83742c72 214 buf_output_free(&out);
2eca4892 215
952b05e0
CF
216 json_free_object(root);
217 root = NULL;
2eca4892 218 job_opt_object = NULL;
952b05e0
CF
219 clients_array = NULL;
220 du_array = NULL;
221}
222
b66570dc
JA
223static struct fio_client *find_client_by_fd(int fd)
224{
3c5f57e3 225 int bucket = hash_long(fd, FIO_CLIENT_HASH_BITS) & FIO_CLIENT_HASH_MASK;
b66570dc
JA
226 struct fio_client *client;
227 struct flist_head *entry;
228
bebe6398
JA
229 flist_for_each(entry, &client_hash[bucket]) {
230 client = flist_entry(entry, struct fio_client, hash_list);
b66570dc 231
e55f8f30
JA
232 if (client->fd == fd) {
233 client->refs++;
b66570dc 234 return client;
e55f8f30 235 }
b66570dc
JA
236 }
237
238 return NULL;
239}
240
3af45201 241void fio_put_client(struct fio_client *client)
b66570dc 242{
5121a9aa
JA
243 if (--client->refs)
244 return;
245
7c6686fc
JA
246 log_info_buf(client->buf.buf, client->buf.buflen);
247 buf_output_free(&client->buf);
248
3af45201
JA
249 free(client->hostname);
250 if (client->argv)
251 free(client->argv);
252 if (client->name)
253 free(client->name);
323255cc
JA
254 while (client->nr_files) {
255 struct client_file *cf = &client->files[--client->nr_files];
256
257 free(cf->file);
258 }
259 if (client->files)
260 free(client->files);
0279b880
JA
261 if (client->opt_lists)
262 free(client->opt_lists);
3af45201 263
108fea77 264 if (!client->did_stat)
7b38411c 265 sum_stat_clients--;
108fea77 266
f9a33cc0
JA
267 if (client->error)
268 error_clients++;
269
3af45201
JA
270 free(client);
271}
272
f02f7dac
JA
273static int fio_client_dec_jobs_eta(struct client_eta *eta, client_eta_op eta_fn)
274{
275 if (!--eta->pending) {
276 eta_fn(&eta->eta);
277 free(eta);
278 return 0;
279 }
280
281 return 1;
282}
283
54e62ad4
JA
284static void fio_drain_client_text(struct fio_client *client)
285{
286 do {
287 struct fio_net_cmd *cmd;
288
289 cmd = fio_net_recv_cmd(client->fd, false);
290 if (!cmd)
291 break;
292
2baadb02
JA
293 if (cmd->opcode == FIO_NET_CMD_TEXT) {
294 convert_text(cmd);
295 client->ops->text(client, cmd);
54e62ad4
JA
296 }
297
2baadb02 298 free(cmd);
54e62ad4
JA
299 } while (1);
300}
301
3af45201
JA
302static void remove_client(struct fio_client *client)
303{
304 assert(client->refs);
305
39e8e016 306 dprint(FD_NET, "client: removed <%s>\n", client->hostname);
3af45201 307
54e62ad4
JA
308 fio_drain_client_text(client);
309
3af45201
JA
310 if (!flist_empty(&client->list))
311 flist_del_init(&client->list);
3c5f57e3 312
bebe6398 313 fio_client_remove_hash(client);
81179eec 314
82c1ed38
JA
315 if (!flist_empty(&client->eta_list)) {
316 flist_del_init(&client->eta_list);
a5276616 317 fio_client_dec_jobs_eta(client->eta_in_flight, client->ops->eta);
82c1ed38 318 }
af9c9fb3 319
1e0c184e
JA
320 close(client->fd);
321 client->fd = -1;
322
0cf3ece0
JA
323 if (client->ops->removed)
324 client->ops->removed(client);
325
3c5f57e3 326 nr_clients--;
3af45201 327 fio_put_client(client);
5121a9aa
JA
328}
329
343cb4a9
JA
330struct fio_client *fio_get_client(struct fio_client *client)
331{
332 client->refs++;
333 return client;
334}
335
fa2ea806
JA
336static void __fio_client_add_cmd_option(struct fio_client *client,
337 const char *opt)
81179eec 338{
39e8e016
JA
339 int index;
340
341 index = client->argc++;
81179eec 342 client->argv = realloc(client->argv, sizeof(char *) * client->argc);
39e8e016
JA
343 client->argv[index] = strdup(opt);
344 dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
81179eec
JA
345}
346
fa2ea806 347void fio_client_add_cmd_option(void *cookie, const char *opt)
81179eec 348{
bebe6398 349 struct fio_client *client = cookie;
3f3a4542 350 struct flist_head *entry;
81179eec 351
bebe6398 352 if (!client || !opt)
fa2ea806 353 return;
81179eec 354
fa2ea806 355 __fio_client_add_cmd_option(client, opt);
3f3a4542
JA
356
357 /*
358 * Duplicate arguments to shared client group
359 */
360 flist_for_each(entry, &arg_list) {
361 client = flist_entry(entry, struct fio_client, arg_list);
362
363 __fio_client_add_cmd_option(client, opt);
364 }
81179eec
JA
365}
366
7c6686fc 367static struct fio_client *get_new_client(void)
3ec62ec4
JA
368{
369 struct fio_client *client;
370
371 client = malloc(sizeof(*client));
372 memset(client, 0, sizeof(*client));
373
374 INIT_FLIST_HEAD(&client->list);
375 INIT_FLIST_HEAD(&client->hash_list);
376 INIT_FLIST_HEAD(&client->arg_list);
377 INIT_FLIST_HEAD(&client->eta_list);
378 INIT_FLIST_HEAD(&client->cmd_list);
379
7c6686fc
JA
380 buf_output_init(&client->buf);
381
382 return client;
383}
384
385struct fio_client *fio_client_add_explicit(struct client_ops *ops,
386 const char *hostname, int type,
387 int port)
388{
389 struct fio_client *client;
390
391 client = get_new_client();
392
3ec62ec4
JA
393 client->hostname = strdup(hostname);
394
395 if (type == Fio_client_socket)
67d2a9b8 396 client->is_sock = true;
3ec62ec4
JA
397 else {
398 int ipv6;
399
400 ipv6 = type == Fio_client_ipv6;
3aa3ceeb 401 if (fio_server_parse_host(hostname, ipv6,
3ec62ec4
JA
402 &client->addr.sin_addr,
403 &client->addr6.sin6_addr))
404 goto err;
405
406 client->port = port;
407 }
408
409 client->fd = -1;
a5276616 410 client->ops = ops;
5121a9aa 411 client->refs = 1;
46bcd498 412 client->type = ops->client_type;
3ec62ec4
JA
413
414 __fio_client_add_cmd_option(client, "fio");
415
416 flist_add(&client->list, &client_list);
417 nr_clients++;
418 dprint(FD_NET, "client: added <%s>\n", client->hostname);
419 return client;
420err:
421 free(client);
422 return NULL;
423}
424
bb781643 425int fio_client_add_ini_file(void *cookie, const char *ini_file, bool remote)
14ea90ed
JA
426{
427 struct fio_client *client = cookie;
323255cc 428 struct client_file *cf;
14ea90ed 429 size_t new_size;
323255cc 430 void *new_files;
14ea90ed 431
ea91da21
JA
432 if (!client)
433 return 1;
434
14ea90ed
JA
435 dprint(FD_NET, "client <%s>: add ini %s\n", client->hostname, ini_file);
436
323255cc
JA
437 new_size = (client->nr_files + 1) * sizeof(struct client_file);
438 new_files = realloc(client->files, new_size);
439 if (!new_files)
440 return 1;
441
442 client->files = new_files;
443 cf = &client->files[client->nr_files];
444 cf->file = strdup(ini_file);
445 cf->remote = remote;
446 client->nr_files++;
447 return 0;
14ea90ed
JA
448}
449
a5276616 450int fio_client_add(struct client_ops *ops, const char *hostname, void **cookie)
132159a5 451{
3f3a4542 452 struct fio_client *existing = *cookie;
b66570dc 453 struct fio_client *client;
132159a5 454
3f3a4542
JA
455 if (existing) {
456 /*
457 * We always add our "exec" name as the option, hence 1
458 * means empty.
459 */
460 if (existing->argc == 1)
461 flist_add_tail(&existing->arg_list, &arg_list);
462 else {
463 while (!flist_empty(&arg_list))
464 flist_del_init(arg_list.next);
465 }
466 }
467
7c6686fc 468 client = get_new_client();
3c5f57e3 469
bebe6398
JA
470 if (fio_server_parse_string(hostname, &client->hostname,
471 &client->is_sock, &client->port,
811826be
JA
472 &client->addr.sin_addr,
473 &client->addr6.sin6_addr,
474 &client->ipv6))
bebe6398 475 return -1;
87aa8f19 476
bebe6398 477 client->fd = -1;
a5276616 478 client->ops = ops;
5121a9aa 479 client->refs = 1;
46bcd498 480 client->type = ops->client_type;
3c5f57e3 481
81179eec
JA
482 __fio_client_add_cmd_option(client, "fio");
483
a37f69b7
JA
484 flist_add(&client->list, &client_list);
485 nr_clients++;
bebe6398
JA
486 dprint(FD_NET, "client: added <%s>\n", client->hostname);
487 *cookie = client;
488 return 0;
a37f69b7
JA
489}
490
ca09be4b
JA
491static const char *server_name(struct fio_client *client, char *buf,
492 size_t bufsize)
493{
494 const char *from;
495
496 if (client->ipv6)
497 from = inet_ntop(AF_INET6, (struct sockaddr *) &client->addr6.sin6_addr, buf, bufsize);
498 else if (client->is_sock)
499 from = "sock";
500 else
501 from = inet_ntop(AF_INET, (struct sockaddr *) &client->addr.sin_addr, buf, bufsize);
502
503 return from;
504}
505
d824c3b8
JA
506static void probe_client(struct fio_client *client)
507{
3989b143 508 struct cmd_client_probe_pdu pdu;
e8b60617 509 const char *sname;
3989b143 510 uint64_t tag;
ca09be4b 511 char buf[64];
3989b143 512
d824c3b8
JA
513 dprint(FD_NET, "client: send probe\n");
514
3989b143
JA
515#ifdef CONFIG_ZLIB
516 pdu.flags = __le64_to_cpu(FIO_PROBE_FLAG_ZLIB);
517#else
518 pdu.flags = 0;
519#endif
520
e8b60617
JA
521 sname = server_name(client, buf, sizeof(buf));
522 memset(pdu.server, 0, sizeof(pdu.server));
36833fb0 523 snprintf((char *) pdu.server, sizeof(pdu.server), "%s", sname);
ca09be4b 524
3989b143 525 fio_net_send_cmd(client->fd, FIO_NET_CMD_PROBE, &pdu, sizeof(pdu), &tag, &client->cmd_list);
d824c3b8
JA
526}
527
87aa8f19 528static int fio_client_connect_ip(struct fio_client *client)
a37f69b7 529{
811826be 530 struct sockaddr *addr;
67bf9823 531 socklen_t socklen;
811826be
JA
532 int fd, domain;
533
534 if (client->ipv6) {
535 client->addr6.sin6_family = AF_INET6;
536 client->addr6.sin6_port = htons(client->port);
537 domain = AF_INET6;
538 addr = (struct sockaddr *) &client->addr6;
539 socklen = sizeof(client->addr6);
540 } else {
541 client->addr.sin_family = AF_INET;
542 client->addr.sin_port = htons(client->port);
543 domain = AF_INET;
544 addr = (struct sockaddr *) &client->addr;
545 socklen = sizeof(client->addr);
546 }
132159a5 547
811826be 548 fd = socket(domain, SOCK_STREAM, 0);
132159a5 549 if (fd < 0) {
25095e1b
JA
550 int ret = -errno;
551
132159a5 552 log_err("fio: socket: %s\n", strerror(errno));
25095e1b 553 return ret;
132159a5
JA
554 }
555
811826be 556 if (connect(fd, addr, socklen) < 0) {
25095e1b
JA
557 int ret = -errno;
558
132159a5 559 log_err("fio: connect: %s\n", strerror(errno));
a7de0a11
JA
560 log_err("fio: failed to connect to %s:%u\n", client->hostname,
561 client->port);
b94cba47 562 close(fd);
25095e1b 563 return ret;
87aa8f19
JA
564 }
565
566 return fd;
567}
568
569static int fio_client_connect_sock(struct fio_client *client)
570{
571 struct sockaddr_un *addr = &client->addr_un;
67bf9823 572 socklen_t len;
87aa8f19
JA
573 int fd;
574
575 memset(addr, 0, sizeof(*addr));
576 addr->sun_family = AF_UNIX;
36833fb0
BVA
577 snprintf(addr->sun_path, sizeof(addr->sun_path), "%s",
578 client->hostname);
87aa8f19
JA
579
580 fd = socket(AF_UNIX, SOCK_STREAM, 0);
581 if (fd < 0) {
25095e1b
JA
582 int ret = -errno;
583
87aa8f19 584 log_err("fio: socket: %s\n", strerror(errno));
25095e1b 585 return ret;
87aa8f19
JA
586 }
587
588 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
589 if (connect(fd, (struct sockaddr *) addr, len) < 0) {
25095e1b
JA
590 int ret = -errno;
591
87aa8f19 592 log_err("fio: connect; %s\n", strerror(errno));
b94cba47 593 close(fd);
25095e1b 594 return ret;
132159a5
JA
595 }
596
87aa8f19
JA
597 return fd;
598}
599
2f99deb0 600int fio_client_connect(struct fio_client *client)
87aa8f19
JA
601{
602 int fd;
603
604 dprint(FD_NET, "client: connect to host %s\n", client->hostname);
605
87aa8f19
JA
606 if (client->is_sock)
607 fd = fio_client_connect_sock(client);
608 else
609 fd = fio_client_connect_ip(client);
610
89c1707c
JA
611 dprint(FD_NET, "client: %s connected %d\n", client->hostname, fd);
612
87aa8f19 613 if (fd < 0)
a1a3ed51 614 return fd;
87aa8f19 615
b66570dc 616 client->fd = fd;
bebe6398 617 fio_client_add_hash(client);
81179eec 618 client->state = Client_connected;
d824c3b8
JA
619
620 probe_client(client);
132159a5
JA
621 return 0;
622}
623
0cf3ece0 624int fio_client_terminate(struct fio_client *client)
2f99deb0 625{
0cf3ece0 626 return fio_net_send_quit(client->fd);
2f99deb0
JA
627}
628
8aa89d70 629static void fio_clients_terminate(void)
cc0df00a
JA
630{
631 struct flist_head *entry;
632 struct fio_client *client;
633
60efd14e
JA
634 dprint(FD_NET, "client: terminate clients\n");
635
cc0df00a
JA
636 flist_for_each(entry, &client_list) {
637 client = flist_entry(entry, struct fio_client, list);
2f99deb0 638 fio_client_terminate(client);
cc0df00a
JA
639 }
640}
641
642static void sig_int(int sig)
643{
bebe6398 644 dprint(FD_NET, "client: got signal %d\n", sig);
cc0df00a
JA
645 fio_clients_terminate();
646}
647
648static void client_signal_handler(void)
649{
650 struct sigaction act;
651
652 memset(&act, 0, sizeof(act));
653 act.sa_handler = sig_int;
654 act.sa_flags = SA_RESTART;
655 sigaction(SIGINT, &act, NULL);
656
657 memset(&act, 0, sizeof(act));
658 act.sa_handler = sig_int;
659 act.sa_flags = SA_RESTART;
660 sigaction(SIGTERM, &act, NULL);
b852e7cf 661
2f694507
BC
662/* Windows uses SIGBREAK as a quit signal from other applications */
663#ifdef WIN32
664 memset(&act, 0, sizeof(act));
665 act.sa_handler = sig_int;
666 act.sa_flags = SA_RESTART;
667 sigaction(SIGBREAK, &act, NULL);
668#endif
669
b852e7cf
JA
670 memset(&act, 0, sizeof(act));
671 act.sa_handler = sig_show_status;
672 act.sa_flags = SA_RESTART;
673 sigaction(SIGUSR1, &act, NULL);
cc0df00a
JA
674}
675
81179eec
JA
676static int send_client_cmd_line(struct fio_client *client)
677{
fa2ea806
JA
678 struct cmd_single_line_pdu *cslp;
679 struct cmd_line_pdu *clp;
680 unsigned long offset;
7f868316 681 unsigned int *lens;
fa2ea806
JA
682 void *pdu;
683 size_t mem;
81179eec
JA
684 int i, ret;
685
39e8e016 686 dprint(FD_NET, "client: send cmdline %d\n", client->argc);
60efd14e 687
7f868316
JA
688 lens = malloc(client->argc * sizeof(unsigned int));
689
fa2ea806
JA
690 /*
691 * Find out how much mem we need
692 */
7f868316
JA
693 for (i = 0, mem = 0; i < client->argc; i++) {
694 lens[i] = strlen(client->argv[i]) + 1;
695 mem += lens[i];
696 }
fa2ea806
JA
697
698 /*
699 * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu
700 */
701 mem += sizeof(*clp) + (client->argc * sizeof(*cslp));
702
703 pdu = malloc(mem);
704 clp = pdu;
705 offset = sizeof(*clp);
706
707 for (i = 0; i < client->argc; i++) {
7f868316 708 uint16_t arg_len = lens[i];
fa2ea806
JA
709
710 cslp = pdu + offset;
711 strcpy((char *) cslp->text, client->argv[i]);
712 cslp->len = cpu_to_le16(arg_len);
713 offset += sizeof(*cslp) + arg_len;
714 }
81179eec 715
7f868316 716 free(lens);
fa2ea806 717 clp->lines = cpu_to_le16(client->argc);
46bcd498 718 clp->client_type = __cpu_to_le16(client->type);
40c60516 719 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, NULL, NULL);
81179eec
JA
720 free(pdu);
721 return ret;
722}
723
a37f69b7
JA
724int fio_clients_connect(void)
725{
726 struct fio_client *client;
727 struct flist_head *entry, *tmp;
728 int ret;
729
93bcfd20
BC
730#ifdef WIN32
731 WSADATA wsd;
3c3ed070 732 WSAStartup(MAKEWORD(2, 2), &wsd);
93bcfd20
BC
733#endif
734
60efd14e
JA
735 dprint(FD_NET, "client: connect all\n");
736
cc0df00a
JA
737 client_signal_handler();
738
a37f69b7
JA
739 flist_for_each_safe(entry, tmp, &client_list) {
740 client = flist_entry(entry, struct fio_client, list);
741
742 ret = fio_client_connect(client);
0b8f30a5 743 if (ret) {
a37f69b7 744 remove_client(client);
0b8f30a5
JA
745 continue;
746 }
747
81179eec
JA
748 if (client->argc > 1)
749 send_client_cmd_line(client);
a37f69b7
JA
750 }
751
752 return !nr_clients;
753}
754
b9d2f30a
JA
755int fio_start_client(struct fio_client *client)
756{
757 dprint(FD_NET, "client: start %s\n", client->hostname);
758 return fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_RUN, 0, NULL);
759}
760
761int fio_start_all_clients(void)
762{
763 struct fio_client *client;
764 struct flist_head *entry, *tmp;
765 int ret;
766
767 dprint(FD_NET, "client: start all\n");
768
952b05e0
CF
769 fio_client_json_init();
770
b9d2f30a
JA
771 flist_for_each_safe(entry, tmp, &client_list) {
772 client = flist_entry(entry, struct fio_client, list);
773
774 ret = fio_start_client(client);
775 if (ret) {
776 remove_client(client);
777 continue;
778 }
779 }
780
781 return flist_empty(&client_list);
782}
783
323255cc
JA
784static int __fio_client_send_remote_ini(struct fio_client *client,
785 const char *filename)
786{
787 struct cmd_load_file_pdu *pdu;
788 size_t p_size;
789 int ret;
790
791 dprint(FD_NET, "send remote ini %s to %s\n", filename, client->hostname);
792
829c31b5 793 p_size = sizeof(*pdu) + strlen(filename) + 1;
323255cc 794 pdu = malloc(p_size);
829c31b5 795 memset(pdu, 0, p_size);
323255cc
JA
796 pdu->name_len = strlen(filename);
797 strcpy((char *) pdu->file, filename);
798 pdu->client_type = cpu_to_le16((uint16_t) client->type);
799
67d2a9b8 800 client->sent_job = true;
323255cc
JA
801 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_LOAD_FILE, pdu, p_size,NULL, NULL);
802 free(pdu);
803 return ret;
804}
805
132159a5
JA
806/*
807 * Send file contents to server backend. We could use sendfile(), but to remain
808 * more portable lets just read/write the darn thing.
809 */
323255cc
JA
810static int __fio_client_send_local_ini(struct fio_client *client,
811 const char *filename)
132159a5 812{
46bcd498
JA
813 struct cmd_job_pdu *pdu;
814 size_t p_size;
132159a5 815 struct stat sb;
46bcd498
JA
816 char *p;
817 void *buf;
132159a5
JA
818 off_t len;
819 int fd, ret;
820
46c48f1f
JA
821 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
822
132159a5
JA
823 fd = open(filename, O_RDONLY);
824 if (fd < 0) {
8a1db9a1 825 ret = -errno;
e951bdc4 826 log_err("fio: job file <%s> open: %s\n", filename, strerror(errno));
25095e1b 827 return ret;
132159a5
JA
828 }
829
830 if (fstat(fd, &sb) < 0) {
8a1db9a1 831 ret = -errno;
132159a5 832 log_err("fio: job file stat: %s\n", strerror(errno));
b94cba47 833 close(fd);
25095e1b 834 return ret;
132159a5
JA
835 }
836
b4f5e72f
TK
837 /*
838 * Add extra space for variable expansion, but doesn't guarantee.
839 */
840 sb.st_size += OPT_LEN_MAX;
46bcd498
JA
841 p_size = sb.st_size + sizeof(*pdu);
842 pdu = malloc(p_size);
843 buf = pdu->buf;
132159a5
JA
844
845 len = sb.st_size;
846 p = buf;
b4f5e72f 847 if (read_ini_data(fd, p, len)) {
0b8f30a5 848 log_err("fio: failed reading job file %s\n", filename);
b94cba47 849 close(fd);
03a22d91 850 free(pdu);
0b8f30a5
JA
851 return 1;
852 }
853
46bcd498
JA
854 pdu->buf_len = __cpu_to_le32(sb.st_size);
855 pdu->client_type = cpu_to_le32(client->type);
856
67d2a9b8 857 client->sent_job = true;
40c60516 858 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, pdu, p_size, NULL, NULL);
46bcd498 859 free(pdu);
b94cba47 860 close(fd);
132159a5
JA
861 return ret;
862}
37db14fe 863
323255cc 864int fio_client_send_ini(struct fio_client *client, const char *filename,
bb781643 865 bool remote)
9988ca70 866{
25095e1b
JA
867 int ret;
868
323255cc
JA
869 if (!remote)
870 ret = __fio_client_send_local_ini(client, filename);
871 else
872 ret = __fio_client_send_remote_ini(client, filename);
873
3af45201 874 if (!ret)
67d2a9b8 875 client->sent_job = true;
9988ca70 876
25095e1b 877 return ret;
9988ca70
JA
878}
879
323255cc
JA
880static int fio_client_send_cf(struct fio_client *client,
881 struct client_file *cf)
882{
883 return fio_client_send_ini(client, cf->file, cf->remote);
884}
885
a37f69b7
JA
886int fio_clients_send_ini(const char *filename)
887{
888 struct fio_client *client;
889 struct flist_head *entry, *tmp;
890
891 flist_for_each_safe(entry, tmp, &client_list) {
4d057f1f
JA
892 bool failed = false;
893
a37f69b7
JA
894 client = flist_entry(entry, struct fio_client, list);
895
323255cc 896 if (client->nr_files) {
14ea90ed
JA
897 int i;
898
323255cc
JA
899 for (i = 0; i < client->nr_files; i++) {
900 struct client_file *cf;
14ea90ed 901
323255cc
JA
902 cf = &client->files[i];
903
904 if (fio_client_send_cf(client, cf)) {
4d057f1f 905 failed = true;
14ea90ed
JA
906 remove_client(client);
907 break;
908 }
909 }
323255cc 910 }
4d057f1f 911 if (client->sent_job || failed)
323255cc
JA
912 continue;
913 if (!filename || fio_client_send_ini(client, filename, 0))
3af45201 914 remove_client(client);
a37f69b7
JA
915 }
916
917 return !nr_clients;
918}
919
40c60516
JA
920int fio_client_update_options(struct fio_client *client,
921 struct thread_options *o, uint64_t *tag)
922{
923 struct cmd_add_job_pdu pdu;
924
925 pdu.thread_number = cpu_to_le32(client->thread_number);
926 pdu.groupid = cpu_to_le32(client->groupid);
927 convert_thread_options_to_net(&pdu.top, o);
952b05e0 928
40c60516
JA
929 return fio_net_send_cmd(client->fd, FIO_NET_CMD_UPDATE_JOB, &pdu, sizeof(pdu), tag, &client->cmd_list);
930}
931
a64e88da
JA
932static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
933{
934 dst->max_val = le64_to_cpu(src->max_val);
935 dst->min_val = le64_to_cpu(src->min_val);
936 dst->samples = le64_to_cpu(src->samples);
802ad4a8
JA
937
938 /*
939 * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
940 */
941 dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
942 dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
a64e88da
JA
943}
944
945static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
946{
df8781b6 947 int i, j, k;
a64e88da 948
2f122b13
JA
949 dst->error = le32_to_cpu(src->error);
950 dst->thread_number = le32_to_cpu(src->thread_number);
951 dst->groupid = le32_to_cpu(src->groupid);
952 dst->pid = le32_to_cpu(src->pid);
953 dst->members = le32_to_cpu(src->members);
771e58be 954 dst->unified_rw_rep = le32_to_cpu(src->unified_rw_rep);
a64e88da 955
298921d6 956 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
a64e88da
JA
957 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
958 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
959 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
960 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
188b6016 961 convert_io_stat(&dst->iops_stat[i], &src->iops_stat[i]);
a64e88da 962 }
91ea42e5 963 convert_io_stat(&dst->sync_stat, &src->sync_stat);
a64e88da
JA
964
965 dst->usr_time = le64_to_cpu(src->usr_time);
966 dst->sys_time = le64_to_cpu(src->sys_time);
967 dst->ctx = le64_to_cpu(src->ctx);
968 dst->minf = le64_to_cpu(src->minf);
969 dst->majf = le64_to_cpu(src->majf);
b599759b
JA
970 dst->clat_percentiles = le32_to_cpu(src->clat_percentiles);
971 dst->lat_percentiles = le32_to_cpu(src->lat_percentiles);
56440e63 972 dst->slat_percentiles = le32_to_cpu(src->slat_percentiles);
988d97ba 973 dst->percentile_precision = le64_to_cpu(src->percentile_precision);
802ad4a8
JA
974
975 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
976 fio_fp64_t *fps = &src->percentile_list[i];
977 fio_fp64_t *fpd = &dst->percentile_list[i];
978
979 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
980 }
a64e88da
JA
981
982 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
6cc0e5aa
AL
983 dst->io_u_map[i] = le64_to_cpu(src->io_u_map[i]);
984 dst->io_u_submit[i] = le64_to_cpu(src->io_u_submit[i]);
985 dst->io_u_complete[i] = le64_to_cpu(src->io_u_complete[i]);
a64e88da
JA
986 }
987
d6bb626e 988 for (i = 0; i < FIO_IO_U_LAT_N_NR; i++)
6cc0e5aa 989 dst->io_u_lat_n[i] = le64_to_cpu(src->io_u_lat_n[i]);
d279fccc 990 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++)
6cc0e5aa 991 dst->io_u_lat_u[i] = le64_to_cpu(src->io_u_lat_u[i]);
d279fccc 992 for (i = 0; i < FIO_IO_U_LAT_M_NR; i++)
6cc0e5aa 993 dst->io_u_lat_m[i] = le64_to_cpu(src->io_u_lat_m[i]);
a64e88da 994
df8781b6
VF
995 for (i = 0; i < FIO_LAT_CNT; i++)
996 for (j = 0; j < DDIR_RWDIR_CNT; j++)
997 for (k = 0; k < FIO_IO_U_PLAT_NR; k++)
998 dst->io_u_plat[i][j][k] = le64_to_cpu(src->io_u_plat[i][j][k]);
a64e88da 999
91ea42e5
VF
1000 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
1001 dst->io_u_sync_plat[j] = le64_to_cpu(src->io_u_sync_plat[j]);
1002
1003 for (i = 0; i < DDIR_RWDIR_SYNC_CNT; i++)
a64e88da 1004 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
91ea42e5
VF
1005
1006 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
93eee04a 1007 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
3bcb9d94 1008 dst->drop_io_u[i] = le64_to_cpu(src->drop_io_u[i]);
a64e88da
JA
1009 }
1010
1011 dst->total_submit = le64_to_cpu(src->total_submit);
1012 dst->total_complete = le64_to_cpu(src->total_complete);
a5951dd7 1013 dst->nr_zone_resets = le64_to_cpu(src->nr_zone_resets);
a64e88da 1014
298921d6 1015 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
a64e88da
JA
1016 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
1017 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
1018 }
1019
1020 dst->total_run_time = le64_to_cpu(src->total_run_time);
1021 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
1022 dst->total_err_count = le64_to_cpu(src->total_err_count);
ddcc0b69
JA
1023 dst->first_error = le32_to_cpu(src->first_error);
1024 dst->kb_base = le32_to_cpu(src->kb_base);
ad705bcb 1025 dst->unit_base = le32_to_cpu(src->unit_base);
3e260a46 1026
e883cb35
JF
1027 dst->sig_figs = le32_to_cpu(src->sig_figs);
1028
3e260a46
JA
1029 dst->latency_depth = le32_to_cpu(src->latency_depth);
1030 dst->latency_target = le64_to_cpu(src->latency_target);
1031 dst->latency_window = le64_to_cpu(src->latency_window);
1032 dst->latency_percentile.u.f = fio_uint64_to_double(le64_to_cpu(src->latency_percentile.u.i));
66347cfa
DE
1033
1034 dst->nr_block_infos = le64_to_cpu(src->nr_block_infos);
1035 for (i = 0; i < dst->nr_block_infos; i++)
1036 dst->block_infos[i] = le32_to_cpu(src->block_infos[i]);
b2a432bf
PC
1037 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1038 for (j = 0; j < FIO_IO_U_PLAT_NR; j++) {
1039 dst->io_u_plat_high_prio[i][j] = le64_to_cpu(src->io_u_plat_high_prio[i][j]);
efd78265 1040 dst->io_u_plat_low_prio[i][j] = le64_to_cpu(src->io_u_plat_low_prio[i][j]);
b2a432bf
PC
1041 }
1042 convert_io_stat(&dst->clat_high_prio_stat[i], &src->clat_high_prio_stat[i]);
efd78265 1043 convert_io_stat(&dst->clat_low_prio_stat[i], &src->clat_low_prio_stat[i]);
b2a432bf 1044 }
bb49c8bd
VF
1045
1046 dst->ss_dur = le64_to_cpu(src->ss_dur);
1047 dst->ss_state = le32_to_cpu(src->ss_state);
1048 dst->ss_head = le32_to_cpu(src->ss_head);
bb49c8bd
VF
1049 dst->ss_limit.u.f = fio_uint64_to_double(le64_to_cpu(src->ss_limit.u.i));
1050 dst->ss_slope.u.f = fio_uint64_to_double(le64_to_cpu(src->ss_slope.u.i));
1051 dst->ss_deviation.u.f = fio_uint64_to_double(le64_to_cpu(src->ss_deviation.u.i));
1052 dst->ss_criterion.u.f = fio_uint64_to_double(le64_to_cpu(src->ss_criterion.u.i));
1053
c8caba48 1054 if (dst->ss_state & FIO_SS_DATA) {
bb49c8bd
VF
1055 for (i = 0; i < dst->ss_dur; i++ ) {
1056 dst->ss_iops_data[i] = le64_to_cpu(src->ss_iops_data[i]);
1057 dst->ss_bw_data[i] = le64_to_cpu(src->ss_bw_data[i]);
1058 }
1059 }
96563db9
JA
1060
1061 dst->cachehit = le64_to_cpu(src->cachehit);
1062 dst->cachemiss = le64_to_cpu(src->cachemiss);
a64e88da
JA
1063}
1064
1065static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
1066{
1067 int i;
1068
298921d6 1069 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
a64e88da
JA
1070 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
1071 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
1072 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
1073 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
af7f87cb 1074 dst->iobytes[i] = le64_to_cpu(src->iobytes[i]);
a64e88da
JA
1075 dst->agg[i] = le64_to_cpu(src->agg[i]);
1076 }
1077
1078 dst->kb_base = le32_to_cpu(src->kb_base);
ad705bcb 1079 dst->unit_base = le32_to_cpu(src->unit_base);
e883cb35 1080 dst->sig_figs = le32_to_cpu(src->sig_figs);
a64e88da 1081 dst->groupid = le32_to_cpu(src->groupid);
771e58be 1082 dst->unified_rw_rep = le32_to_cpu(src->unified_rw_rep);
a64e88da
JA
1083}
1084
952b05e0 1085static void json_object_add_client_info(struct json_object *obj,
9a4424c8 1086 struct fio_client *client)
952b05e0 1087{
9a4424c8
CF
1088 const char *hostname = client->hostname ? client->hostname : "";
1089
1090 json_object_add_value_string(obj, "hostname", hostname);
952b05e0
CF
1091 json_object_add_value_int(obj, "port", client->port);
1092}
1093
89e5fad9 1094static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd)
a64e88da
JA
1095{
1096 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
0279b880 1097 struct flist_head *opt_list = NULL;
952b05e0 1098 struct json_object *tsobj;
a64e88da 1099
0279b880
JA
1100 if (client->opt_lists && p->ts.thread_number <= client->jobs)
1101 opt_list = &client->opt_lists[p->ts.thread_number - 1];
1102
7c6686fc 1103 tsobj = show_thread_status(&p->ts, &p->rs, opt_list, &client->buf);
67d2a9b8 1104 client->did_stat = true;
952b05e0
CF
1105 if (tsobj) {
1106 json_object_add_client_info(tsobj, client);
1107 json_array_add_value_object(clients_array, tsobj);
1108 }
37f0c1ae 1109
7b38411c 1110 if (sum_stat_clients <= 1)
37f0c1ae
JA
1111 return;
1112
fd595830 1113 sum_thread_stats(&client_ts, &p->ts, sum_stat_nr == 1);
37f0c1ae
JA
1114 sum_group_stats(&client_gs, &p->rs);
1115
1116 client_ts.members++;
2f122b13 1117 client_ts.thread_number = p->ts.thread_number;
37f0c1ae 1118 client_ts.groupid = p->ts.groupid;
771e58be 1119 client_ts.unified_rw_rep = p->ts.unified_rw_rep;
18aa1998 1120 client_ts.sig_figs = p->ts.sig_figs;
37f0c1ae
JA
1121
1122 if (++sum_stat_nr == sum_stat_clients) {
1123 strcpy(client_ts.name, "All clients");
c2b5ddb2 1124 tsobj = show_thread_status(&client_ts, &client_gs, NULL, &allclients);
952b05e0
CF
1125 if (tsobj) {
1126 json_object_add_client_info(tsobj, client);
1127 json_array_add_value_object(clients_array, tsobj);
1128 }
37f0c1ae 1129 }
a64e88da
JA
1130}
1131
89e5fad9 1132static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd)
a64e88da
JA
1133{
1134 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
1135
84f1f865 1136 if (output_format & FIO_OUTPUT_NORMAL)
7c6686fc 1137 show_group_stats(gs, &client->buf);
a64e88da
JA
1138}
1139
0279b880
JA
1140static void handle_job_opt(struct fio_client *client, struct fio_net_cmd *cmd)
1141{
1142 struct cmd_job_option *pdu = (struct cmd_job_option *) cmd->payload;
1143 struct print_option *p;
1144
b127b679
JA
1145 if (!job_opt_object)
1146 return;
1147
0279b880 1148 pdu->global = le16_to_cpu(pdu->global);
bc0fec0e
JA
1149 pdu->truncated = le16_to_cpu(pdu->truncated);
1150 pdu->groupid = le32_to_cpu(pdu->groupid);
0279b880
JA
1151
1152 p = malloc(sizeof(*p));
1153 p->name = strdup((char *) pdu->name);
1154 if (pdu->value[0] != '\0')
1155 p->value = strdup((char *) pdu->value);
1156 else
1157 p->value = NULL;
1158
1159 if (pdu->global) {
1160 const char *pos = "";
1161
1162 if (p->value)
1163 pos = p->value;
1164
1165 json_object_add_value_string(job_opt_object, p->name, pos);
1166 } else if (client->opt_lists) {
1167 struct flist_head *opt_list = &client->opt_lists[pdu->groupid];
1168
1169 flist_add_tail(&p->list, opt_list);
ebae36a2
BVA
1170 } else {
1171 free(p->value);
1172 free(p->name);
1173 free(p);
0279b880
JA
1174 }
1175}
1176
3bf236c0
JA
1177static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd)
1178{
1179 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
1180 const char *buf = (const char *) pdu->buf;
1181 const char *name;
1182 int fio_unused ret;
fecea437
JA
1183 struct buf_output out;
1184
1185 buf_output_init(&out);
3bf236c0
JA
1186
1187 name = client->name ? client->name : client->hostname;
1188
1d1b65dc 1189 if (!client->skip_newline && !(output_format & FIO_OUTPUT_TERSE))
c2fcb6bc
JA
1190 __log_buf(&out, "<%s> ", name);
1191 __log_buf(&out, "%s", buf);
fecea437 1192 log_info_buf(out.buf, out.buflen);
d5bb0a89 1193 buf_output_free(&out);
3bf236c0
JA
1194 client->skip_newline = strchr(buf, '\n') == NULL;
1195}
1196
d09a64a0
JA
1197static void convert_agg(struct disk_util_agg *agg)
1198{
1199 int i;
1200
1201 for (i = 0; i < 2; i++) {
504bc961
JA
1202 agg->ios[i] = le64_to_cpu(agg->ios[i]);
1203 agg->merges[i] = le64_to_cpu(agg->merges[i]);
d09a64a0 1204 agg->sectors[i] = le64_to_cpu(agg->sectors[i]);
504bc961 1205 agg->ticks[i] = le64_to_cpu(agg->ticks[i]);
d09a64a0
JA
1206 }
1207
504bc961
JA
1208 agg->io_ticks = le64_to_cpu(agg->io_ticks);
1209 agg->time_in_queue = le64_to_cpu(agg->time_in_queue);
d09a64a0 1210 agg->slavecount = le32_to_cpu(agg->slavecount);
e5c9093d 1211 agg->max_util.u.f = fio_uint64_to_double(le64_to_cpu(agg->max_util.u.i));
d09a64a0
JA
1212}
1213
1214static void convert_dus(struct disk_util_stat *dus)
1215{
1216 int i;
1217
1218 for (i = 0; i < 2; i++) {
504bc961
JA
1219 dus->s.ios[i] = le64_to_cpu(dus->s.ios[i]);
1220 dus->s.merges[i] = le64_to_cpu(dus->s.merges[i]);
a3b4cf7d 1221 dus->s.sectors[i] = le64_to_cpu(dus->s.sectors[i]);
504bc961 1222 dus->s.ticks[i] = le64_to_cpu(dus->s.ticks[i]);
d09a64a0
JA
1223 }
1224
504bc961
JA
1225 dus->s.io_ticks = le64_to_cpu(dus->s.io_ticks);
1226 dus->s.time_in_queue = le64_to_cpu(dus->s.time_in_queue);
a3b4cf7d 1227 dus->s.msec = le64_to_cpu(dus->s.msec);
d09a64a0
JA
1228}
1229
1230static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd)
1231{
1232 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
1233
2eca4892 1234 if (!client->disk_stats_shown)
67d2a9b8 1235 client->disk_stats_shown = true;
d09a64a0 1236
01cfefcc 1237 if (output_format & FIO_OUTPUT_JSON) {
952b05e0 1238 struct json_object *duobj;
7c6686fc 1239
952b05e0
CF
1240 json_array_add_disk_util(&du->dus, &du->agg, du_array);
1241 duobj = json_array_last_value_object(du_array);
1242 json_object_add_client_info(duobj, client);
fd200c6a
VF
1243 }
1244 if (output_format & FIO_OUTPUT_NORMAL) {
2eca4892 1245 __log_buf(&client->buf, "\nDisk stats (read/write):\n");
7c6686fc 1246 print_disk_util(&du->dus, &du->agg, 0, &client->buf);
2eca4892 1247 }
06e49041 1248 if (output_format & FIO_OUTPUT_TERSE && terse_version >= 3) {
fd200c6a 1249 print_disk_util(&du->dus, &du->agg, 1, &client->buf);
06e49041
VF
1250 __log_buf(&client->buf, "\n");
1251 }
d09a64a0
JA
1252}
1253
3bf236c0 1254static void convert_jobs_eta(struct jobs_eta *je)
cf451d1e 1255{
cf451d1e
JA
1256 int i;
1257
1258 je->nr_running = le32_to_cpu(je->nr_running);
1259 je->nr_ramp = le32_to_cpu(je->nr_ramp);
1260 je->nr_pending = le32_to_cpu(je->nr_pending);
714e85f3 1261 je->nr_setting_up = le32_to_cpu(je->nr_setting_up);
cf451d1e 1262 je->files_open = le32_to_cpu(je->files_open);
cf451d1e 1263
298921d6 1264 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
90eff1c9
SW
1265 je->m_rate[i] = le64_to_cpu(je->m_rate[i]);
1266 je->t_rate[i] = le64_to_cpu(je->t_rate[i]);
298921d6
JA
1267 je->m_iops[i] = le32_to_cpu(je->m_iops[i]);
1268 je->t_iops[i] = le32_to_cpu(je->t_iops[i]);
90eff1c9 1269 je->rate[i] = le64_to_cpu(je->rate[i]);
16725bb0 1270 je->iops[i] = le32_to_cpu(je->iops[i]);
cf451d1e
JA
1271 }
1272
b51eedb7 1273 je->elapsed_sec = le64_to_cpu(je->elapsed_sec);
cf451d1e 1274 je->eta_sec = le64_to_cpu(je->eta_sec);
8c621fb2 1275 je->nr_threads = le32_to_cpu(je->nr_threads);
b7f05eb0 1276 je->is_pow2 = le32_to_cpu(je->is_pow2);
ed2c0a12 1277 je->unit_base = le32_to_cpu(je->unit_base);
e883cb35 1278 je->sig_figs = le32_to_cpu(je->sig_figs);
48fbb46e
JA
1279}
1280
3e47bd25 1281void fio_client_sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je)
48fbb46e 1282{
48fbb46e
JA
1283 int i;
1284
1285 dst->nr_running += je->nr_running;
1286 dst->nr_ramp += je->nr_ramp;
1287 dst->nr_pending += je->nr_pending;
714e85f3 1288 dst->nr_setting_up += je->nr_setting_up;
48fbb46e 1289 dst->files_open += je->files_open;
48fbb46e 1290
298921d6 1291 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
3e47bd25
JA
1292 dst->m_rate[i] += je->m_rate[i];
1293 dst->t_rate[i] += je->t_rate[i];
1294 dst->m_iops[i] += je->m_iops[i];
1295 dst->t_iops[i] += je->t_iops[i];
16725bb0
JA
1296 dst->rate[i] += je->rate[i];
1297 dst->iops[i] += je->iops[i];
48fbb46e
JA
1298 }
1299
1300 dst->elapsed_sec += je->elapsed_sec;
1301
1302 if (je->eta_sec > dst->eta_sec)
1303 dst->eta_sec = je->eta_sec;
8c621fb2
JA
1304
1305 dst->nr_threads += je->nr_threads;
afe22d3d
JA
1306
1307 /*
1308 * This wont be correct for multiple strings, but at least it
1309 * works for the basic cases.
1310 */
1311 strcpy((char *) dst->run_str, (char *) je->run_str);
48fbb46e
JA
1312}
1313
308d69b5 1314static bool remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd)
89c1707c 1315{
40c60516 1316 struct fio_net_cmd_reply *reply = NULL;
89c1707c
JA
1317 struct flist_head *entry;
1318
1319 flist_for_each(entry, &client->cmd_list) {
40c60516 1320 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
89c1707c 1321
40c60516 1322 if (cmd->tag == (uintptr_t) reply)
89c1707c
JA
1323 break;
1324
40c60516 1325 reply = NULL;
89c1707c
JA
1326 }
1327
40c60516 1328 if (!reply) {
4e0a8fa2 1329 log_err("fio: client: unable to find matching tag (%llx)\n", (unsigned long long) cmd->tag);
308d69b5 1330 return false;
89c1707c
JA
1331 }
1332
40c60516
JA
1333 flist_del(&reply->list);
1334 cmd->tag = reply->saved_tag;
1335 free(reply);
308d69b5 1336 return true;
40c60516
JA
1337}
1338
1339int fio_client_wait_for_reply(struct fio_client *client, uint64_t tag)
1340{
1341 do {
1342 struct fio_net_cmd_reply *reply = NULL;
1343 struct flist_head *entry;
1344
1345 flist_for_each(entry, &client->cmd_list) {
1346 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
1347
1348 if (tag == (uintptr_t) reply)
1349 break;
1350
1351 reply = NULL;
1352 }
1353
1354 if (!reply)
1355 break;
1356
1357 usleep(1000);
1358 } while (1);
1359
1360 return 0;
89c1707c
JA
1361}
1362
82c1ed38 1363static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd)
48fbb46e
JA
1364{
1365 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
df380934 1366 struct client_eta *eta = (struct client_eta *) (uintptr_t) cmd->tag;
af9c9fb3
JA
1367
1368 dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending);
cf451d1e 1369
f77d2676
JA
1370 assert(client->eta_in_flight == eta);
1371
1372 client->eta_in_flight = NULL;
82c1ed38 1373 flist_del_init(&client->eta_list);
a64c13a4 1374 client->eta_timeouts = 0;
82c1ed38 1375
2f99deb0
JA
1376 if (client->ops->jobs_eta)
1377 client->ops->jobs_eta(client, je);
1378
3e47bd25 1379 fio_client_sum_jobs_eta(&eta->eta, je);
a5276616 1380 fio_client_dec_jobs_eta(eta, client->ops->eta);
cf451d1e
JA
1381}
1382
868f6f03
KC
1383static void client_flush_hist_samples(FILE *f, int hist_coarseness, void *samples,
1384 uint64_t sample_size)
1385{
1386 struct io_sample *s;
1387 int log_offset;
1388 uint64_t i, j, nr_samples;
1389 struct io_u_plat_entry *entry;
6cc0e5aa 1390 uint64_t *io_u_plat;
868f6f03
KC
1391
1392 int stride = 1 << hist_coarseness;
1393
1394 if (!sample_size)
1395 return;
1396
1397 s = __get_sample(samples, 0, 0);
1398 log_offset = (s->__ddir & LOG_OFFSET_SAMPLE_BIT) != 0;
1399
1400 nr_samples = sample_size / __log_entry_sz(log_offset);
1401
1402 for (i = 0; i < nr_samples; i++) {
1403
1404 s = (struct io_sample *)((char *)__get_sample(samples, log_offset, i) +
1405 i * sizeof(struct io_u_plat_entry));
1406
af2fde19 1407 entry = s->data.plat_entry;
868f6f03
KC
1408 io_u_plat = entry->io_u_plat;
1409
5fff9543
JF
1410 fprintf(f, "%lu, %u, %llu, ", (unsigned long) s->time,
1411 io_sample_ddir(s), (unsigned long long) s->bs);
868f6f03 1412 for (j = 0; j < FIO_IO_U_PLAT_NR - stride; j += stride) {
6cc0e5aa 1413 fprintf(f, "%llu, ", (unsigned long long)hist_sum(j, stride, io_u_plat, NULL));
868f6f03 1414 }
6cc0e5aa 1415 fprintf(f, "%llu\n", (unsigned long long)
868f6f03
KC
1416 hist_sum(FIO_IO_U_PLAT_NR - stride, stride, io_u_plat, NULL));
1417
1418 }
1419}
1420
3da1d33a
JA
1421static int fio_client_handle_iolog(struct fio_client *client,
1422 struct fio_net_cmd *cmd)
bead01d7 1423{
44edeed3 1424 struct cmd_iolog_pdu *pdu = NULL;
0cba0f91 1425 bool store_direct;
44edeed3
EV
1426 char *log_pathname = NULL;
1427 int ret = 0;
bead01d7 1428
0cba0f91 1429 pdu = convert_iolog(cmd, &store_direct);
3da1d33a
JA
1430 if (!pdu) {
1431 log_err("fio: failed converting IO log\n");
44edeed3
EV
1432 ret = 1;
1433 goto out;
3da1d33a 1434 }
bead01d7 1435
d9644df9 1436 /* allocate buffer big enough for next sprintf() call */
21c9e728 1437 log_pathname = malloc(10 + strlen((char *)pdu->name) +
d9644df9 1438 strlen(client->hostname));
14a74557 1439 if (!log_pathname) {
c5839011 1440 log_err("fio: memory allocation of unique pathname failed\n");
44edeed3
EV
1441 ret = -1;
1442 goto out;
14a74557 1443 }
d9644df9 1444 /* generate a unique pathname for the log file using hostname */
14a74557
BE
1445 sprintf(log_pathname, "%s.%s", pdu->name, client->hostname);
1446
0cba0f91 1447 if (store_direct) {
583e0dfa 1448 ssize_t wrote;
0cba0f91
JA
1449 size_t sz;
1450 int fd;
1451
14a74557 1452 fd = open((const char *) log_pathname,
0cba0f91
JA
1453 O_WRONLY | O_CREAT | O_TRUNC, 0644);
1454 if (fd < 0) {
21c9e728 1455 log_err("fio: open log %s: %s\n",
14a74557 1456 log_pathname, strerror(errno));
44edeed3
EV
1457 ret = 1;
1458 goto out;
0cba0f91 1459 }
3da1d33a 1460
0cba0f91 1461 sz = cmd->pdu_len - sizeof(*pdu);
583e0dfa 1462 wrote = write(fd, pdu->samples, sz);
0cba0f91 1463 close(fd);
3da1d33a 1464
583e0dfa 1465 if (wrote != sz) {
3da1d33a 1466 log_err("fio: short write on compressed log\n");
44edeed3
EV
1467 ret = 1;
1468 goto out;
3da1d33a
JA
1469 }
1470
44edeed3 1471 ret = 0;
0cba0f91
JA
1472 } else {
1473 FILE *f;
14a74557 1474 f = fopen((const char *) log_pathname, "w");
0cba0f91 1475 if (!f) {
21c9e728 1476 log_err("fio: fopen log %s : %s\n",
14a74557 1477 log_pathname, strerror(errno));
44edeed3
EV
1478 ret = 1;
1479 goto out;
0cba0f91
JA
1480 }
1481
868f6f03
KC
1482 if (pdu->log_type == IO_LOG_TYPE_HIST) {
1483 client_flush_hist_samples(f, pdu->log_hist_coarseness, pdu->samples,
1484 pdu->nr_samples * sizeof(struct io_sample));
1485 } else {
1486 flush_samples(f, pdu->samples,
1487 pdu->nr_samples * sizeof(struct io_sample));
1488 }
0cba0f91 1489 fclose(f);
44edeed3 1490 ret = 0;
0cba0f91 1491 }
44edeed3
EV
1492
1493out:
e4651d46 1494 if (pdu && pdu != (void *) cmd->payload)
44edeed3
EV
1495 free(pdu);
1496
1497 if (log_pathname)
1498 free(log_pathname);
1499
1500 return ret;
bead01d7
JA
1501}
1502
b5296ddb 1503static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
2e03b4b2 1504{
3989b143 1505 struct cmd_probe_reply_pdu *probe = (struct cmd_probe_reply_pdu *) cmd->payload;
d2333358
JA
1506 const char *os, *arch;
1507 char bit[16];
2e03b4b2 1508
cca84643
JA
1509 os = fio_get_os_string(probe->os);
1510 if (!os)
1511 os = "unknown";
1512
1513 arch = fio_get_arch_string(probe->arch);
1514 if (!arch)
1515 os = "unknown";
1516
d2333358 1517 sprintf(bit, "%d-bit", probe->bpp * 8);
3989b143 1518 probe->flags = le64_to_cpu(probe->flags);
38fdef22 1519
2eca4892 1520 if (output_format & FIO_OUTPUT_NORMAL) {
44f4f5e6
TK
1521 log_info("hostname=%s, be=%u, %s, os=%s, arch=%s, fio=%s, flags=%lx\n",
1522 probe->hostname, probe->bigendian, bit, os, arch,
1523 probe->fio_version, (unsigned long) probe->flags);
2eca4892 1524 }
b5296ddb
JA
1525
1526 if (!client->name)
1527 client->name = strdup((char *) probe->hostname);
2e03b4b2
JA
1528}
1529
11e950bd
JA
1530static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd)
1531{
1532 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1533
1534 client->state = Client_started;
1535 client->jobs = le32_to_cpu(pdu->jobs);
a89d5319 1536 client->nr_stat = le32_to_cpu(pdu->stat_outputs);
108fea77 1537
0279b880
JA
1538 if (client->jobs) {
1539 int i;
1540
1541 if (client->opt_lists)
1542 free(client->opt_lists);
1543
1544 client->opt_lists = malloc(client->jobs * sizeof(struct flist_head));
1545 for (i = 0; i < client->jobs; i++)
1546 INIT_FLIST_HEAD(&client->opt_lists[i]);
1547 }
1548
a89d5319 1549 sum_stat_clients += client->nr_stat;
11e950bd
JA
1550}
1551
36ed2f9a 1552static void handle_stop(struct fio_client *client)
11e950bd 1553{
498c92c2
JA
1554 if (client->error)
1555 log_info("client <%s>: exited with error %d\n", client->hostname, client->error);
11e950bd
JA
1556}
1557
6b79c80c
JA
1558static void convert_stop(struct fio_net_cmd *cmd)
1559{
1560 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1561
1562 pdu->error = le32_to_cpu(pdu->error);
1563}
1564
084d1c6f
JA
1565static void convert_text(struct fio_net_cmd *cmd)
1566{
1567 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
1568
1569 pdu->level = le32_to_cpu(pdu->level);
1570 pdu->buf_len = le32_to_cpu(pdu->buf_len);
1571 pdu->log_sec = le64_to_cpu(pdu->log_sec);
1572 pdu->log_usec = le64_to_cpu(pdu->log_usec);
1573}
1574
3989b143
JA
1575static struct cmd_iolog_pdu *convert_iolog_gz(struct fio_net_cmd *cmd,
1576 struct cmd_iolog_pdu *pdu)
1b42725f 1577{
3989b143 1578#ifdef CONFIG_ZLIB
1b42725f 1579 struct cmd_iolog_pdu *ret;
1b42725f 1580 z_stream stream;
7df9ac89 1581 uint64_t nr_samples;
3989b143 1582 size_t total;
a9c9608e 1583 char *p;
1b42725f
JA
1584
1585 stream.zalloc = Z_NULL;
1586 stream.zfree = Z_NULL;
1587 stream.opaque = Z_NULL;
1588 stream.avail_in = 0;
1589 stream.next_in = Z_NULL;
1590
1591 if (inflateInit(&stream) != Z_OK)
1592 return NULL;
1593
1594 /*
f5ed765a 1595 * Get header first, it's not compressed
1b42725f 1596 */
3c865f32 1597 nr_samples = le64_to_cpu(pdu->nr_samples);
1b42725f 1598
868f6f03
KC
1599 if (pdu->log_type == IO_LOG_TYPE_HIST)
1600 total = nr_samples * (__log_entry_sz(le32_to_cpu(pdu->log_offset)) +
1601 sizeof(struct io_u_plat_entry));
1602 else
1603 total = nr_samples * __log_entry_sz(le32_to_cpu(pdu->log_offset));
f5ed765a 1604 ret = malloc(total + sizeof(*pdu));
1b42725f 1605 ret->nr_samples = nr_samples;
3989b143
JA
1606
1607 memcpy(ret, pdu, sizeof(*pdu));
f5ed765a 1608
a9c9608e 1609 p = (char *) ret + sizeof(*pdu);
1b42725f 1610
f5ed765a 1611 stream.avail_in = cmd->pdu_len - sizeof(*pdu);
a9c9608e 1612 stream.next_in = (void *)((char *) pdu + sizeof(*pdu));
1b42725f
JA
1613 while (stream.avail_in) {
1614 unsigned int this_chunk = 65536;
1615 unsigned int this_len;
1616 int err;
1617
1618 if (this_chunk > total)
1619 this_chunk = total;
1620
1621 stream.avail_out = this_chunk;
a9c9608e 1622 stream.next_out = (void *)p;
1b42725f 1623 err = inflate(&stream, Z_NO_FLUSH);
3c547fe0
JA
1624 /* may be Z_OK, or Z_STREAM_END */
1625 if (err < 0) {
2f75f022
JF
1626 /*
1627 * Z_STREAM_ERROR and Z_BUF_ERROR can safely be
1628 * ignored */
1629 if (err == Z_STREAM_ERROR || err == Z_BUF_ERROR)
1630 break;
1b42725f 1631 log_err("fio: inflate error %d\n", err);
f5ed765a
JA
1632 free(ret);
1633 ret = NULL;
3989b143 1634 goto err;
1b42725f
JA
1635 }
1636
1637 this_len = this_chunk - stream.avail_out;
1638 p += this_len;
1639 total -= this_len;
1640 }
1641
3989b143
JA
1642err:
1643 inflateEnd(&stream);
1644 return ret;
1645#else
1646 return NULL;
1647#endif
1648}
1649
1650/*
1651 * This has been compressed on the server side, since it can be big.
1652 * Uncompress here.
1653 */
0cba0f91
JA
1654static struct cmd_iolog_pdu *convert_iolog(struct fio_net_cmd *cmd,
1655 bool *store_direct)
3989b143
JA
1656{
1657 struct cmd_iolog_pdu *pdu = (struct cmd_iolog_pdu *) cmd->payload;
1658 struct cmd_iolog_pdu *ret;
ae588852 1659 uint64_t i;
0cba0f91 1660 int compressed;
ae588852 1661 void *samples;
3989b143 1662
0cba0f91
JA
1663 *store_direct = false;
1664
3989b143
JA
1665 /*
1666 * Convert if compressed and we support it. If it's not
1667 * compressed, we need not do anything.
1668 */
0cba0f91
JA
1669 compressed = le32_to_cpu(pdu->compressed);
1670 if (compressed == XMIT_COMPRESSED) {
3989b143
JA
1671#ifndef CONFIG_ZLIB
1672 log_err("fio: server sent compressed data by mistake\n");
1673 return NULL;
1674#endif
1675 ret = convert_iolog_gz(cmd, pdu);
1676 if (!ret) {
1677 log_err("fio: failed decompressing log\n");
1678 return NULL;
1679 }
0cba0f91
JA
1680 } else if (compressed == STORE_COMPRESSED) {
1681 *store_direct = true;
1682 ret = pdu;
3989b143
JA
1683 } else
1684 ret = pdu;
1685
ae588852 1686 ret->nr_samples = le64_to_cpu(ret->nr_samples);
3989b143 1687 ret->thread_number = le32_to_cpu(ret->thread_number);
3989b143
JA
1688 ret->log_type = le32_to_cpu(ret->log_type);
1689 ret->compressed = le32_to_cpu(ret->compressed);
ae588852 1690 ret->log_offset = le32_to_cpu(ret->log_offset);
868f6f03 1691 ret->log_hist_coarseness = le32_to_cpu(ret->log_hist_coarseness);
3989b143 1692
0cba0f91
JA
1693 if (*store_direct)
1694 return ret;
1695
3310fced 1696 samples = &ret->samples[0];
f5ed765a 1697 for (i = 0; i < ret->nr_samples; i++) {
ae588852 1698 struct io_sample *s;
f5ed765a 1699
ae588852 1700 s = __get_sample(samples, ret->log_offset, i);
868f6f03 1701 if (ret->log_type == IO_LOG_TYPE_HIST)
a9c9608e 1702 s = (struct io_sample *)((char *)s + sizeof(struct io_u_plat_entry) * i);
868f6f03 1703
b26317c9 1704 s->time = le64_to_cpu(s->time);
af2fde19 1705 s->data.val = le64_to_cpu(s->data.val);
b2a432bf 1706 s->__ddir = __le32_to_cpu(s->__ddir);
5fff9543 1707 s->bs = le64_to_cpu(s->bs);
ae588852
JA
1708
1709 if (ret->log_offset) {
1710 struct io_sample_offset *so = (void *) s;
1711
1712 so->offset = le64_to_cpu(so->offset);
1713 }
868f6f03
KC
1714
1715 if (ret->log_type == IO_LOG_TYPE_HIST) {
a9c9608e 1716 s->data.plat_entry = (struct io_u_plat_entry *)(((char *)s) + sizeof(*s));
af2fde19
SW
1717 s->data.plat_entry->list.next = NULL;
1718 s->data.plat_entry->list.prev = NULL;
868f6f03 1719 }
f5ed765a
JA
1720 }
1721
1b42725f
JA
1722 return ret;
1723}
1724
ca09be4b
JA
1725static void sendfile_reply(int fd, struct cmd_sendfile_reply *rep,
1726 size_t size, uint64_t tag)
1727{
1728 rep->error = cpu_to_le32(rep->error);
1729 fio_net_send_cmd(fd, FIO_NET_CMD_SENDFILE, rep, size, &tag, NULL);
1730}
1731
0748d801
JA
1732static int fio_send_file(struct fio_client *client, struct cmd_sendfile *pdu,
1733 uint64_t tag)
ca09be4b
JA
1734{
1735 struct cmd_sendfile_reply *rep;
1736 struct stat sb;
1737 size_t size;
1738 int fd;
1739
1740 size = sizeof(*rep);
1741 rep = malloc(size);
1742
1743 if (stat((char *)pdu->path, &sb) < 0) {
1744fail:
1745 rep->error = errno;
1746 sendfile_reply(client->fd, rep, size, tag);
1747 free(rep);
1748 return 1;
1749 }
1750
1751 size += sb.st_size;
1752 rep = realloc(rep, size);
1753 rep->size = cpu_to_le32((uint32_t) sb.st_size);
1754
1755 fd = open((char *)pdu->path, O_RDONLY);
1756 if (fd == -1 )
1757 goto fail;
1758
1759 rep->error = read_data(fd, &rep->data, sb.st_size);
1760 sendfile_reply(client->fd, rep, size, tag);
1761 free(rep);
1762 close(fd);
1763 return 0;
1764}
1765
a5276616 1766int fio_handle_client(struct fio_client *client)
37db14fe 1767{
a5276616 1768 struct client_ops *ops = client->ops;
37db14fe 1769 struct fio_net_cmd *cmd;
bb49c8bd 1770 int size;
37db14fe 1771
60efd14e
JA
1772 dprint(FD_NET, "client: handle %s\n", client->hostname);
1773
54e62ad4 1774 cmd = fio_net_recv_cmd(client->fd, true);
e951bdc4
JA
1775 if (!cmd)
1776 return 0;
c2c94585 1777
b9d2f30a
JA
1778 dprint(FD_NET, "client: got cmd op %s from %s (pdu=%u)\n",
1779 fio_server_op(cmd->opcode), client->hostname, cmd->pdu_len);
46c48f1f 1780
47714d68
JA
1781 client->last_cmd = cmd->opcode;
1782
e951bdc4
JA
1783 switch (cmd->opcode) {
1784 case FIO_NET_CMD_QUIT:
3ec62ec4 1785 if (ops->quit)
35c0ba7f 1786 ops->quit(client, cmd);
e951bdc4 1787 remove_client(client);
e951bdc4 1788 break;
084d1c6f
JA
1789 case FIO_NET_CMD_TEXT:
1790 convert_text(cmd);
35c0ba7f 1791 ops->text(client, cmd);
e951bdc4 1792 break;
3bf236c0
JA
1793 case FIO_NET_CMD_DU: {
1794 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
1795
1796 convert_dus(&du->dus);
1797 convert_agg(&du->agg);
1798
dd366728 1799 ops->disk_util(client, cmd);
d09a64a0 1800 break;
3bf236c0
JA
1801 }
1802 case FIO_NET_CMD_TS: {
1803 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
1804
bb49c8bd 1805 dprint(FD_NET, "client: ts->ss_state = %u\n", (unsigned int) le32_to_cpu(p->ts.ss_state));
c8caba48 1806 if (le32_to_cpu(p->ts.ss_state) & FIO_SS_DATA) {
bb49c8bd
VF
1807 dprint(FD_NET, "client: received steadystate ring buffers\n");
1808
1809 size = le64_to_cpu(p->ts.ss_dur);
1810 p->ts.ss_iops_data = (uint64_t *) ((struct cmd_ts_pdu *)cmd->payload + 1);
1811 p->ts.ss_bw_data = p->ts.ss_iops_data + size;
1812 }
1813
3bf236c0
JA
1814 convert_ts(&p->ts, &p->ts);
1815 convert_gs(&p->rs, &p->rs);
1816
89e5fad9 1817 ops->thread_status(client, cmd);
e951bdc4 1818 break;
3bf236c0
JA
1819 }
1820 case FIO_NET_CMD_GS: {
1821 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
1822
1823 convert_gs(gs, gs);
1824
89e5fad9 1825 ops->group_stats(client, cmd);
e951bdc4 1826 break;
3bf236c0
JA
1827 }
1828 case FIO_NET_CMD_ETA: {
1829 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
1830
308d69b5
JA
1831 if (!remove_reply_cmd(client, cmd))
1832 break;
3bf236c0 1833 convert_jobs_eta(je);
a5276616 1834 handle_eta(client, cmd);
e951bdc4 1835 break;
3bf236c0 1836 }
e951bdc4 1837 case FIO_NET_CMD_PROBE:
89c1707c 1838 remove_reply_cmd(client, cmd);
dd366728 1839 ops->probe(client, cmd);
e951bdc4 1840 break;
5d7793aa 1841 case FIO_NET_CMD_SERVER_START:
01be038e 1842 client->state = Client_running;
85dd01e7
JA
1843 if (ops->job_start)
1844 ops->job_start(client, cmd);
01be038e 1845 break;
85dd01e7
JA
1846 case FIO_NET_CMD_START: {
1847 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1848
1849 pdu->jobs = le32_to_cpu(pdu->jobs);
1850 ops->start(client, cmd);
e951bdc4 1851 break;
85dd01e7 1852 }
6b79c80c
JA
1853 case FIO_NET_CMD_STOP: {
1854 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1855
1856 convert_stop(cmd);
1857 client->state = Client_stopped;
122c7725
JA
1858 client->error = le32_to_cpu(pdu->error);
1859 client->signal = le32_to_cpu(pdu->signal);
36ed2f9a 1860 ops->stop(client);
e951bdc4 1861 break;
6b79c80c 1862 }
40c60516
JA
1863 case FIO_NET_CMD_ADD_JOB: {
1864 struct cmd_add_job_pdu *pdu = (struct cmd_add_job_pdu *) cmd->payload;
1865
1866 client->thread_number = le32_to_cpu(pdu->thread_number);
1867 client->groupid = le32_to_cpu(pdu->groupid);
1868
807f9971
JA
1869 if (ops->add_job)
1870 ops->add_job(client, cmd);
807f9971 1871 break;
40c60516 1872 }
1b42725f 1873 case FIO_NET_CMD_IOLOG:
0cba0f91 1874 fio_client_handle_iolog(client, cmd);
1b42725f 1875 break;
40c60516 1876 case FIO_NET_CMD_UPDATE_JOB:
40c60516 1877 ops->update_job(client, cmd);
649cee91 1878 remove_reply_cmd(client, cmd);
40c60516 1879 break;
ca09be4b
JA
1880 case FIO_NET_CMD_VTRIGGER: {
1881 struct all_io_list *pdu = (struct all_io_list *) cmd->payload;
d264264a
JA
1882 char buf[128];
1883 int off = 0;
ca09be4b 1884
d264264a
JA
1885 if (aux_path) {
1886 strcpy(buf, aux_path);
1887 off = strlen(buf);
1888 }
1889
1890 __verify_save_state(pdu, server_name(client, &buf[off], sizeof(buf) - off));
13c70218 1891 exec_trigger(trigger_cmd);
ca09be4b
JA
1892 break;
1893 }
1894 case FIO_NET_CMD_SENDFILE: {
1895 struct cmd_sendfile *pdu = (struct cmd_sendfile *) cmd->payload;
0748d801 1896 fio_send_file(client, pdu, cmd->tag);
ca09be4b
JA
1897 break;
1898 }
0279b880
JA
1899 case FIO_NET_CMD_JOB_OPT: {
1900 handle_job_opt(client, cmd);
1901 break;
1902 }
e951bdc4 1903 default:
89c1707c 1904 log_err("fio: unknown client op: %s\n", fio_server_op(cmd->opcode));
e951bdc4 1905 break;
37db14fe
JA
1906 }
1907
ca09be4b 1908 free(cmd);
e951bdc4 1909 return 1;
37db14fe 1910}
b66570dc 1911
ca09be4b
JA
1912int fio_clients_send_trigger(const char *cmd)
1913{
1914 struct flist_head *entry;
1915 struct fio_client *client;
1916 size_t slen;
1917
1918 dprint(FD_NET, "client: send vtrigger: %s\n", cmd);
1919
1920 if (!cmd)
1921 slen = 0;
1922 else
1923 slen = strlen(cmd);
1924
1925 flist_for_each(entry, &client_list) {
1926 struct cmd_vtrigger_pdu *pdu;
1927
1928 client = flist_entry(entry, struct fio_client, list);
1929
1930 pdu = malloc(sizeof(*pdu) + slen);
1931 pdu->len = cpu_to_le16((uint16_t) slen);
1932 if (slen)
1933 memcpy(pdu->cmd, cmd, slen);
1934 fio_net_send_cmd(client->fd, FIO_NET_CMD_VTRIGGER, pdu,
1935 sizeof(*pdu) + slen, NULL, NULL);
1936 free(pdu);
1937 }
1938
1939 return 0;
1940}
1941
a5276616 1942static void request_client_etas(struct client_ops *ops)
af9c9fb3
JA
1943{
1944 struct fio_client *client;
1945 struct flist_head *entry;
1946 struct client_eta *eta;
82c1ed38 1947 int skipped = 0;
af9c9fb3 1948
5610bcd8
JA
1949 if (eta_print == FIO_ETA_NEVER)
1950 return;
1951
af9c9fb3
JA
1952 dprint(FD_NET, "client: request eta (%d)\n", nr_clients);
1953
afe22d3d 1954 eta = calloc(1, sizeof(*eta) + __THREAD_RUNSTR_SZ(REAL_MAX_JOBS));
af9c9fb3
JA
1955 eta->pending = nr_clients;
1956
1957 flist_for_each(entry, &client_list) {
1958 client = flist_entry(entry, struct fio_client, list);
1959
82c1ed38
JA
1960 if (!flist_empty(&client->eta_list)) {
1961 skipped++;
1962 continue;
1963 }
01be038e
JA
1964 if (client->state != Client_running)
1965 continue;
82c1ed38 1966
f77d2676 1967 assert(!client->eta_in_flight);
82c1ed38 1968 flist_add_tail(&client->eta_list, &eta_list);
f77d2676 1969 client->eta_in_flight = eta;
af9c9fb3 1970 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA,
df380934 1971 (uintptr_t) eta, &client->cmd_list);
af9c9fb3
JA
1972 }
1973
f02f7dac
JA
1974 while (skipped--) {
1975 if (!fio_client_dec_jobs_eta(eta, ops->eta))
1976 break;
1977 }
82c1ed38 1978
af9c9fb3
JA
1979 dprint(FD_NET, "client: requested eta tag %p\n", eta);
1980}
1981
a64c13a4
JA
1982/*
1983 * A single SEND_ETA timeout isn't fatal. Attempt to recover.
1984 */
1985static int handle_cmd_timeout(struct fio_client *client,
1986 struct fio_net_cmd_reply *reply)
1987{
7d9407b7
EV
1988 uint16_t reply_opcode = reply->opcode;
1989
be6fa258
JA
1990 flist_del(&reply->list);
1991 free(reply);
1992
7d9407b7 1993 if (reply_opcode != FIO_NET_CMD_SEND_ETA)
a64c13a4
JA
1994 return 1;
1995
1996 log_info("client <%s>: timeout on SEND_ETA\n", client->hostname);
a64c13a4
JA
1997
1998 flist_del_init(&client->eta_list);
1999 if (client->eta_in_flight) {
2000 fio_client_dec_jobs_eta(client->eta_in_flight, client->ops->eta);
2001 client->eta_in_flight = NULL;
2002 }
2003
2004 /*
2005 * If we fail 5 in a row, give up...
2006 */
2007 if (client->eta_timeouts++ > 5)
2008 return 1;
2009
2010 return 0;
2011}
2012
89c1707c 2013static int client_check_cmd_timeout(struct fio_client *client,
8b6a404c 2014 struct timespec *now)
89c1707c 2015{
40c60516 2016 struct fio_net_cmd_reply *reply;
89c1707c
JA
2017 struct flist_head *entry, *tmp;
2018 int ret = 0;
2019
2020 flist_for_each_safe(entry, tmp, &client->cmd_list) {
1d484506
JA
2021 unsigned int op;
2022
40c60516 2023 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
89c1707c 2024
8b6a404c 2025 if (mtime_since(&reply->ts, now) < FIO_NET_CLIENT_TIMEOUT)
89c1707c
JA
2026 continue;
2027
1d484506 2028 op = reply->opcode;
a64c13a4
JA
2029 if (!handle_cmd_timeout(client, reply))
2030 continue;
2031
89c1707c 2032 log_err("fio: client %s, timeout on cmd %s\n", client->hostname,
1d484506 2033 fio_server_op(op));
89c1707c
JA
2034 ret = 1;
2035 }
2036
2037 return flist_empty(&client->cmd_list) && ret;
2038}
2039
a5276616 2040static int fio_check_clients_timed_out(void)
89c1707c
JA
2041{
2042 struct fio_client *client;
2043 struct flist_head *entry, *tmp;
8b6a404c 2044 struct timespec ts;
89c1707c
JA
2045 int ret = 0;
2046
8b6a404c 2047 fio_gettime(&ts, NULL);
89c1707c
JA
2048
2049 flist_for_each_safe(entry, tmp, &client_list) {
2050 client = flist_entry(entry, struct fio_client, list);
2051
2052 if (flist_empty(&client->cmd_list))
2053 continue;
2054
8b6a404c 2055 if (!client_check_cmd_timeout(client, &ts))
89c1707c
JA
2056 continue;
2057
a5276616
JA
2058 if (client->ops->timed_out)
2059 client->ops->timed_out(client);
ed727a46
JA
2060 else
2061 log_err("fio: client %s timed out\n", client->hostname);
2062
47714d68
JA
2063 if (client->last_cmd != FIO_NET_CMD_VTRIGGER)
2064 client->error = ETIMEDOUT;
2065 else
2066 log_info("fio: ignoring timeout due to vtrigger\n");
89c1707c
JA
2067 remove_client(client);
2068 ret = 1;
2069 }
2070
2071 return ret;
2072}
2073
dd366728 2074int fio_handle_clients(struct client_ops *ops)
b66570dc 2075{
b66570dc 2076 struct pollfd *pfds;
498c92c2 2077 int i, ret = 0, retval = 0;
b66570dc 2078
8b6a404c 2079 fio_gettime(&eta_ts, NULL);
af9c9fb3 2080
b66570dc
JA
2081 pfds = malloc(nr_clients * sizeof(struct pollfd));
2082
37f0c1ae
JA
2083 init_thread_stat(&client_ts);
2084 init_group_run_stat(&client_gs);
2085
82a4be1b 2086 while (!exit_backend && nr_clients) {
c2cb6869
JA
2087 struct flist_head *entry, *tmp;
2088 struct fio_client *client;
2089
82a4be1b 2090 i = 0;
c2cb6869 2091 flist_for_each_safe(entry, tmp, &client_list) {
82a4be1b 2092 client = flist_entry(entry, struct fio_client, list);
b66570dc 2093
a5276616 2094 if (!client->sent_job && !client->ops->stay_connected &&
c2cb6869
JA
2095 flist_empty(&client->cmd_list)) {
2096 remove_client(client);
2097 continue;
2098 }
2099
82a4be1b
JA
2100 pfds[i].fd = client->fd;
2101 pfds[i].events = POLLIN;
2102 i++;
2103 }
2104
c2cb6869
JA
2105 if (!nr_clients)
2106 break;
2107
82a4be1b 2108 assert(i == nr_clients);
b66570dc 2109
5c2857f9 2110 do {
8b6a404c 2111 struct timespec ts;
ca09be4b 2112 int timeout;
af9c9fb3 2113
8b6a404c 2114 fio_gettime(&ts, NULL);
db37d890 2115 if (eta_time_within_slack(mtime_since(&eta_ts, &ts))) {
a5276616 2116 request_client_etas(ops);
8b6a404c 2117 memcpy(&eta_ts, &ts, sizeof(ts));
89c1707c 2118
a5276616 2119 if (fio_check_clients_timed_out())
89c1707c 2120 break;
af9c9fb3
JA
2121 }
2122
ca09be4b
JA
2123 check_trigger_file();
2124
2125 timeout = min(100u, ops->eta_msec);
2126
2127 ret = poll(pfds, nr_clients, timeout);
5c2857f9
JA
2128 if (ret < 0) {
2129 if (errno == EINTR)
2130 continue;
2131 log_err("fio: poll clients: %s\n", strerror(errno));
2132 break;
2133 } else if (!ret)
b66570dc 2134 continue;
5c2857f9 2135 } while (ret <= 0);
b66570dc
JA
2136
2137 for (i = 0; i < nr_clients; i++) {
2138 if (!(pfds[i].revents & POLLIN))
2139 continue;
2140
2141 client = find_client_by_fd(pfds[i].fd);
2142 if (!client) {
65e1a120 2143 log_err("fio: unknown client fd %ld\n", (long) pfds[i].fd);
b66570dc
JA
2144 continue;
2145 }
a5276616 2146 if (!fio_handle_client(client)) {
28d3ab07
JA
2147 log_info("client: host=%s disconnected\n",
2148 client->hostname);
2149 remove_client(client);
498c92c2 2150 retval = 1;
38990764 2151 } else if (client->error)
498c92c2 2152 retval = 1;
343cb4a9 2153 fio_put_client(client);
b66570dc
JA
2154 }
2155 }
2156
c2b5ddb2
VF
2157 log_info_buf(allclients.buf, allclients.buflen);
2158 buf_output_free(&allclients);
2159
952b05e0
CF
2160 fio_client_json_fini();
2161
b66570dc 2162 free(pfds);
f9a33cc0 2163 return retval || error_clients;
b66570dc 2164}
44f4f5e6
TK
2165
2166static void client_display_thread_status(struct jobs_eta *je)
2167{
2168 if (!(output_format & FIO_OUTPUT_JSON))
2169 display_thread_status(je);
2170}