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