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