target: fixes
[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);
1003
298921d6 1004 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
a64e88da
JA
1005 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
1006 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
1007 }
1008
1009 dst->total_run_time = le64_to_cpu(src->total_run_time);
1010 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
1011 dst->total_err_count = le64_to_cpu(src->total_err_count);
ddcc0b69
JA
1012 dst->first_error = le32_to_cpu(src->first_error);
1013 dst->kb_base = le32_to_cpu(src->kb_base);
ad705bcb 1014 dst->unit_base = le32_to_cpu(src->unit_base);
3e260a46 1015
e883cb35
JF
1016 dst->sig_figs = le32_to_cpu(src->sig_figs);
1017
3e260a46
JA
1018 dst->latency_depth = le32_to_cpu(src->latency_depth);
1019 dst->latency_target = le64_to_cpu(src->latency_target);
1020 dst->latency_window = le64_to_cpu(src->latency_window);
1021 dst->latency_percentile.u.f = fio_uint64_to_double(le64_to_cpu(src->latency_percentile.u.i));
66347cfa
DE
1022
1023 dst->nr_block_infos = le64_to_cpu(src->nr_block_infos);
1024 for (i = 0; i < dst->nr_block_infos; i++)
1025 dst->block_infos[i] = le32_to_cpu(src->block_infos[i]);
bb49c8bd 1026
1a7081c7
JA
1027 for (i = 0; i < ARRAY_SIZE(dst->step_stats); i++) {
1028 struct lat_step_stats *ls = &src->step_stats[i];
1029
1030 for (j = 0; j < DDIR_RWDIR_CNT; j++) {
1031 dst->step_stats[i].iops[j] = le64_to_cpu(ls->iops[j]);
1032 dst->step_stats[i].avg[j].u.f = fio_uint64_to_double(le64_to_cpu(ls->avg[j].u.i));
1033 }
1034 }
1035
bb49c8bd
VF
1036 dst->ss_dur = le64_to_cpu(src->ss_dur);
1037 dst->ss_state = le32_to_cpu(src->ss_state);
1038 dst->ss_head = le32_to_cpu(src->ss_head);
bb49c8bd
VF
1039 dst->ss_limit.u.f = fio_uint64_to_double(le64_to_cpu(src->ss_limit.u.i));
1040 dst->ss_slope.u.f = fio_uint64_to_double(le64_to_cpu(src->ss_slope.u.i));
1041 dst->ss_deviation.u.f = fio_uint64_to_double(le64_to_cpu(src->ss_deviation.u.i));
1042 dst->ss_criterion.u.f = fio_uint64_to_double(le64_to_cpu(src->ss_criterion.u.i));
1043
c8caba48 1044 if (dst->ss_state & FIO_SS_DATA) {
bb49c8bd
VF
1045 for (i = 0; i < dst->ss_dur; i++ ) {
1046 dst->ss_iops_data[i] = le64_to_cpu(src->ss_iops_data[i]);
1047 dst->ss_bw_data[i] = le64_to_cpu(src->ss_bw_data[i]);
1048 }
1049 }
a64e88da
JA
1050}
1051
1052static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
1053{
1054 int i;
1055
298921d6 1056 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
a64e88da
JA
1057 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
1058 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
1059 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
1060 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
af7f87cb 1061 dst->iobytes[i] = le64_to_cpu(src->iobytes[i]);
a64e88da
JA
1062 dst->agg[i] = le64_to_cpu(src->agg[i]);
1063 }
1064
1065 dst->kb_base = le32_to_cpu(src->kb_base);
ad705bcb 1066 dst->unit_base = le32_to_cpu(src->unit_base);
e883cb35 1067 dst->sig_figs = le32_to_cpu(src->sig_figs);
a64e88da 1068 dst->groupid = le32_to_cpu(src->groupid);
771e58be 1069 dst->unified_rw_rep = le32_to_cpu(src->unified_rw_rep);
a64e88da
JA
1070}
1071
952b05e0 1072static void json_object_add_client_info(struct json_object *obj,
9a4424c8 1073 struct fio_client *client)
952b05e0 1074{
9a4424c8
CF
1075 const char *hostname = client->hostname ? client->hostname : "";
1076
1077 json_object_add_value_string(obj, "hostname", hostname);
952b05e0
CF
1078 json_object_add_value_int(obj, "port", client->port);
1079}
1080
89e5fad9 1081static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd)
a64e88da
JA
1082{
1083 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
0279b880 1084 struct flist_head *opt_list = NULL;
952b05e0 1085 struct json_object *tsobj;
a64e88da 1086
0279b880
JA
1087 if (client->opt_lists && p->ts.thread_number <= client->jobs)
1088 opt_list = &client->opt_lists[p->ts.thread_number - 1];
1089
7c6686fc 1090 tsobj = show_thread_status(&p->ts, &p->rs, opt_list, &client->buf);
67d2a9b8 1091 client->did_stat = true;
952b05e0
CF
1092 if (tsobj) {
1093 json_object_add_client_info(tsobj, client);
1094 json_array_add_value_object(clients_array, tsobj);
1095 }
37f0c1ae 1096
7b38411c 1097 if (sum_stat_clients <= 1)
37f0c1ae
JA
1098 return;
1099
fd595830 1100 sum_thread_stats(&client_ts, &p->ts, sum_stat_nr == 1);
37f0c1ae
JA
1101 sum_group_stats(&client_gs, &p->rs);
1102
1103 client_ts.members++;
2f122b13 1104 client_ts.thread_number = p->ts.thread_number;
37f0c1ae 1105 client_ts.groupid = p->ts.groupid;
771e58be 1106 client_ts.unified_rw_rep = p->ts.unified_rw_rep;
18aa1998 1107 client_ts.sig_figs = p->ts.sig_figs;
37f0c1ae
JA
1108
1109 if (++sum_stat_nr == sum_stat_clients) {
1110 strcpy(client_ts.name, "All clients");
7c6686fc 1111 tsobj = show_thread_status(&client_ts, &client_gs, NULL, &client->buf);
952b05e0
CF
1112 if (tsobj) {
1113 json_object_add_client_info(tsobj, client);
1114 json_array_add_value_object(clients_array, tsobj);
1115 }
37f0c1ae 1116 }
a64e88da
JA
1117}
1118
89e5fad9 1119static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd)
a64e88da
JA
1120{
1121 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
1122
84f1f865 1123 if (output_format & FIO_OUTPUT_NORMAL)
7c6686fc 1124 show_group_stats(gs, &client->buf);
a64e88da
JA
1125}
1126
0279b880
JA
1127static void handle_job_opt(struct fio_client *client, struct fio_net_cmd *cmd)
1128{
1129 struct cmd_job_option *pdu = (struct cmd_job_option *) cmd->payload;
1130 struct print_option *p;
1131
b127b679
JA
1132 if (!job_opt_object)
1133 return;
1134
0279b880 1135 pdu->global = le16_to_cpu(pdu->global);
bc0fec0e
JA
1136 pdu->truncated = le16_to_cpu(pdu->truncated);
1137 pdu->groupid = le32_to_cpu(pdu->groupid);
0279b880
JA
1138
1139 p = malloc(sizeof(*p));
1140 p->name = strdup((char *) pdu->name);
1141 if (pdu->value[0] != '\0')
1142 p->value = strdup((char *) pdu->value);
1143 else
1144 p->value = NULL;
1145
1146 if (pdu->global) {
1147 const char *pos = "";
1148
1149 if (p->value)
1150 pos = p->value;
1151
1152 json_object_add_value_string(job_opt_object, p->name, pos);
1153 } else if (client->opt_lists) {
1154 struct flist_head *opt_list = &client->opt_lists[pdu->groupid];
1155
1156 flist_add_tail(&p->list, opt_list);
1157 }
1158}
1159
3bf236c0
JA
1160static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd)
1161{
1162 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
1163 const char *buf = (const char *) pdu->buf;
1164 const char *name;
1165 int fio_unused ret;
fecea437
JA
1166 struct buf_output out;
1167
1168 buf_output_init(&out);
3bf236c0
JA
1169
1170 name = client->name ? client->name : client->hostname;
1171
1d1b65dc 1172 if (!client->skip_newline && !(output_format & FIO_OUTPUT_TERSE))
c2fcb6bc
JA
1173 __log_buf(&out, "<%s> ", name);
1174 __log_buf(&out, "%s", buf);
fecea437 1175 log_info_buf(out.buf, out.buflen);
d5bb0a89 1176 buf_output_free(&out);
3bf236c0
JA
1177 client->skip_newline = strchr(buf, '\n') == NULL;
1178}
1179
d09a64a0
JA
1180static void convert_agg(struct disk_util_agg *agg)
1181{
1182 int i;
1183
1184 for (i = 0; i < 2; i++) {
504bc961
JA
1185 agg->ios[i] = le64_to_cpu(agg->ios[i]);
1186 agg->merges[i] = le64_to_cpu(agg->merges[i]);
d09a64a0 1187 agg->sectors[i] = le64_to_cpu(agg->sectors[i]);
504bc961 1188 agg->ticks[i] = le64_to_cpu(agg->ticks[i]);
d09a64a0
JA
1189 }
1190
504bc961
JA
1191 agg->io_ticks = le64_to_cpu(agg->io_ticks);
1192 agg->time_in_queue = le64_to_cpu(agg->time_in_queue);
d09a64a0 1193 agg->slavecount = le32_to_cpu(agg->slavecount);
e5c9093d 1194 agg->max_util.u.f = fio_uint64_to_double(le64_to_cpu(agg->max_util.u.i));
d09a64a0
JA
1195}
1196
1197static void convert_dus(struct disk_util_stat *dus)
1198{
1199 int i;
1200
1201 for (i = 0; i < 2; i++) {
504bc961
JA
1202 dus->s.ios[i] = le64_to_cpu(dus->s.ios[i]);
1203 dus->s.merges[i] = le64_to_cpu(dus->s.merges[i]);
a3b4cf7d 1204 dus->s.sectors[i] = le64_to_cpu(dus->s.sectors[i]);
504bc961 1205 dus->s.ticks[i] = le64_to_cpu(dus->s.ticks[i]);
d09a64a0
JA
1206 }
1207
504bc961
JA
1208 dus->s.io_ticks = le64_to_cpu(dus->s.io_ticks);
1209 dus->s.time_in_queue = le64_to_cpu(dus->s.time_in_queue);
a3b4cf7d 1210 dus->s.msec = le64_to_cpu(dus->s.msec);
d09a64a0
JA
1211}
1212
1213static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd)
1214{
1215 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
1216
2eca4892 1217 if (!client->disk_stats_shown)
67d2a9b8 1218 client->disk_stats_shown = true;
d09a64a0 1219
01cfefcc 1220 if (output_format & FIO_OUTPUT_JSON) {
952b05e0 1221 struct json_object *duobj;
7c6686fc 1222
952b05e0
CF
1223 json_array_add_disk_util(&du->dus, &du->agg, du_array);
1224 duobj = json_array_last_value_object(du_array);
1225 json_object_add_client_info(duobj, client);
2eca4892 1226 } else if (output_format & FIO_OUTPUT_TERSE)
7c6686fc 1227 print_disk_util(&du->dus, &du->agg, 1, &client->buf);
2eca4892
JA
1228 else if (output_format & FIO_OUTPUT_NORMAL) {
1229 __log_buf(&client->buf, "\nDisk stats (read/write):\n");
7c6686fc 1230 print_disk_util(&du->dus, &du->agg, 0, &client->buf);
2eca4892 1231 }
d09a64a0
JA
1232}
1233
3bf236c0 1234static void convert_jobs_eta(struct jobs_eta *je)
cf451d1e 1235{
cf451d1e
JA
1236 int i;
1237
1238 je->nr_running = le32_to_cpu(je->nr_running);
1239 je->nr_ramp = le32_to_cpu(je->nr_ramp);
1240 je->nr_pending = le32_to_cpu(je->nr_pending);
714e85f3 1241 je->nr_setting_up = le32_to_cpu(je->nr_setting_up);
cf451d1e 1242 je->files_open = le32_to_cpu(je->files_open);
cf451d1e 1243
298921d6 1244 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
90eff1c9
SW
1245 je->m_rate[i] = le64_to_cpu(je->m_rate[i]);
1246 je->t_rate[i] = le64_to_cpu(je->t_rate[i]);
298921d6
JA
1247 je->m_iops[i] = le32_to_cpu(je->m_iops[i]);
1248 je->t_iops[i] = le32_to_cpu(je->t_iops[i]);
90eff1c9 1249 je->rate[i] = le64_to_cpu(je->rate[i]);
16725bb0 1250 je->iops[i] = le32_to_cpu(je->iops[i]);
cf451d1e
JA
1251 }
1252
b51eedb7 1253 je->elapsed_sec = le64_to_cpu(je->elapsed_sec);
cf451d1e 1254 je->eta_sec = le64_to_cpu(je->eta_sec);
8c621fb2 1255 je->nr_threads = le32_to_cpu(je->nr_threads);
b7f05eb0 1256 je->is_pow2 = le32_to_cpu(je->is_pow2);
ed2c0a12 1257 je->unit_base = le32_to_cpu(je->unit_base);
e883cb35 1258 je->sig_figs = le32_to_cpu(je->sig_figs);
48fbb46e
JA
1259}
1260
3e47bd25 1261void fio_client_sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je)
48fbb46e 1262{
48fbb46e
JA
1263 int i;
1264
1265 dst->nr_running += je->nr_running;
1266 dst->nr_ramp += je->nr_ramp;
1267 dst->nr_pending += je->nr_pending;
714e85f3 1268 dst->nr_setting_up += je->nr_setting_up;
48fbb46e 1269 dst->files_open += je->files_open;
48fbb46e 1270
298921d6 1271 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
3e47bd25
JA
1272 dst->m_rate[i] += je->m_rate[i];
1273 dst->t_rate[i] += je->t_rate[i];
1274 dst->m_iops[i] += je->m_iops[i];
1275 dst->t_iops[i] += je->t_iops[i];
16725bb0
JA
1276 dst->rate[i] += je->rate[i];
1277 dst->iops[i] += je->iops[i];
48fbb46e
JA
1278 }
1279
1280 dst->elapsed_sec += je->elapsed_sec;
1281
1282 if (je->eta_sec > dst->eta_sec)
1283 dst->eta_sec = je->eta_sec;
8c621fb2
JA
1284
1285 dst->nr_threads += je->nr_threads;
afe22d3d
JA
1286
1287 /*
1288 * This wont be correct for multiple strings, but at least it
1289 * works for the basic cases.
1290 */
1291 strcpy((char *) dst->run_str, (char *) je->run_str);
48fbb46e
JA
1292}
1293
308d69b5 1294static bool remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd)
89c1707c 1295{
40c60516 1296 struct fio_net_cmd_reply *reply = NULL;
89c1707c
JA
1297 struct flist_head *entry;
1298
1299 flist_for_each(entry, &client->cmd_list) {
40c60516 1300 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
89c1707c 1301
40c60516 1302 if (cmd->tag == (uintptr_t) reply)
89c1707c
JA
1303 break;
1304
40c60516 1305 reply = NULL;
89c1707c
JA
1306 }
1307
40c60516 1308 if (!reply) {
4e0a8fa2 1309 log_err("fio: client: unable to find matching tag (%llx)\n", (unsigned long long) cmd->tag);
308d69b5 1310 return false;
89c1707c
JA
1311 }
1312
40c60516
JA
1313 flist_del(&reply->list);
1314 cmd->tag = reply->saved_tag;
1315 free(reply);
308d69b5 1316 return true;
40c60516
JA
1317}
1318
1319int fio_client_wait_for_reply(struct fio_client *client, uint64_t tag)
1320{
1321 do {
1322 struct fio_net_cmd_reply *reply = NULL;
1323 struct flist_head *entry;
1324
1325 flist_for_each(entry, &client->cmd_list) {
1326 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
1327
1328 if (tag == (uintptr_t) reply)
1329 break;
1330
1331 reply = NULL;
1332 }
1333
1334 if (!reply)
1335 break;
1336
1337 usleep(1000);
1338 } while (1);
1339
1340 return 0;
89c1707c
JA
1341}
1342
82c1ed38 1343static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd)
48fbb46e
JA
1344{
1345 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
df380934 1346 struct client_eta *eta = (struct client_eta *) (uintptr_t) cmd->tag;
af9c9fb3
JA
1347
1348 dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending);
cf451d1e 1349
f77d2676
JA
1350 assert(client->eta_in_flight == eta);
1351
1352 client->eta_in_flight = NULL;
82c1ed38 1353 flist_del_init(&client->eta_list);
a64c13a4 1354 client->eta_timeouts = 0;
82c1ed38 1355
2f99deb0
JA
1356 if (client->ops->jobs_eta)
1357 client->ops->jobs_eta(client, je);
1358
3e47bd25 1359 fio_client_sum_jobs_eta(&eta->eta, je);
a5276616 1360 fio_client_dec_jobs_eta(eta, client->ops->eta);
cf451d1e
JA
1361}
1362
868f6f03
KC
1363static void client_flush_hist_samples(FILE *f, int hist_coarseness, void *samples,
1364 uint64_t sample_size)
1365{
1366 struct io_sample *s;
1367 int log_offset;
1368 uint64_t i, j, nr_samples;
1369 struct io_u_plat_entry *entry;
6cc0e5aa 1370 uint64_t *io_u_plat;
868f6f03
KC
1371
1372 int stride = 1 << hist_coarseness;
1373
1374 if (!sample_size)
1375 return;
1376
1377 s = __get_sample(samples, 0, 0);
1378 log_offset = (s->__ddir & LOG_OFFSET_SAMPLE_BIT) != 0;
1379
1380 nr_samples = sample_size / __log_entry_sz(log_offset);
1381
1382 for (i = 0; i < nr_samples; i++) {
1383
1384 s = (struct io_sample *)((char *)__get_sample(samples, log_offset, i) +
1385 i * sizeof(struct io_u_plat_entry));
1386
af2fde19 1387 entry = s->data.plat_entry;
868f6f03
KC
1388 io_u_plat = entry->io_u_plat;
1389
5fff9543
JF
1390 fprintf(f, "%lu, %u, %llu, ", (unsigned long) s->time,
1391 io_sample_ddir(s), (unsigned long long) s->bs);
868f6f03 1392 for (j = 0; j < FIO_IO_U_PLAT_NR - stride; j += stride) {
6cc0e5aa 1393 fprintf(f, "%llu, ", (unsigned long long)hist_sum(j, stride, io_u_plat, NULL));
868f6f03 1394 }
6cc0e5aa 1395 fprintf(f, "%llu\n", (unsigned long long)
868f6f03
KC
1396 hist_sum(FIO_IO_U_PLAT_NR - stride, stride, io_u_plat, NULL));
1397
1398 }
1399}
1400
3da1d33a
JA
1401static int fio_client_handle_iolog(struct fio_client *client,
1402 struct fio_net_cmd *cmd)
bead01d7 1403{
44edeed3 1404 struct cmd_iolog_pdu *pdu = NULL;
0cba0f91 1405 bool store_direct;
44edeed3
EV
1406 char *log_pathname = NULL;
1407 int ret = 0;
bead01d7 1408
0cba0f91 1409 pdu = convert_iolog(cmd, &store_direct);
3da1d33a
JA
1410 if (!pdu) {
1411 log_err("fio: failed converting IO log\n");
44edeed3
EV
1412 ret = 1;
1413 goto out;
3da1d33a 1414 }
bead01d7 1415
d9644df9 1416 /* allocate buffer big enough for next sprintf() call */
21c9e728 1417 log_pathname = malloc(10 + strlen((char *)pdu->name) +
d9644df9 1418 strlen(client->hostname));
14a74557 1419 if (!log_pathname) {
c5839011 1420 log_err("fio: memory allocation of unique pathname failed\n");
44edeed3
EV
1421 ret = -1;
1422 goto out;
14a74557 1423 }
d9644df9 1424 /* generate a unique pathname for the log file using hostname */
14a74557
BE
1425 sprintf(log_pathname, "%s.%s", pdu->name, client->hostname);
1426
0cba0f91 1427 if (store_direct) {
583e0dfa 1428 ssize_t wrote;
0cba0f91
JA
1429 size_t sz;
1430 int fd;
1431
14a74557 1432 fd = open((const char *) log_pathname,
0cba0f91
JA
1433 O_WRONLY | O_CREAT | O_TRUNC, 0644);
1434 if (fd < 0) {
21c9e728 1435 log_err("fio: open log %s: %s\n",
14a74557 1436 log_pathname, strerror(errno));
44edeed3
EV
1437 ret = 1;
1438 goto out;
0cba0f91 1439 }
3da1d33a 1440
0cba0f91 1441 sz = cmd->pdu_len - sizeof(*pdu);
583e0dfa 1442 wrote = write(fd, pdu->samples, sz);
0cba0f91 1443 close(fd);
3da1d33a 1444
583e0dfa 1445 if (wrote != sz) {
3da1d33a 1446 log_err("fio: short write on compressed log\n");
44edeed3
EV
1447 ret = 1;
1448 goto out;
3da1d33a
JA
1449 }
1450
44edeed3 1451 ret = 0;
0cba0f91
JA
1452 } else {
1453 FILE *f;
14a74557 1454 f = fopen((const char *) log_pathname, "w");
0cba0f91 1455 if (!f) {
21c9e728 1456 log_err("fio: fopen log %s : %s\n",
14a74557 1457 log_pathname, strerror(errno));
44edeed3
EV
1458 ret = 1;
1459 goto out;
0cba0f91
JA
1460 }
1461
868f6f03
KC
1462 if (pdu->log_type == IO_LOG_TYPE_HIST) {
1463 client_flush_hist_samples(f, pdu->log_hist_coarseness, pdu->samples,
1464 pdu->nr_samples * sizeof(struct io_sample));
1465 } else {
1466 flush_samples(f, pdu->samples,
1467 pdu->nr_samples * sizeof(struct io_sample));
1468 }
0cba0f91 1469 fclose(f);
44edeed3 1470 ret = 0;
0cba0f91 1471 }
44edeed3
EV
1472
1473out:
e4651d46 1474 if (pdu && pdu != (void *) cmd->payload)
44edeed3
EV
1475 free(pdu);
1476
1477 if (log_pathname)
1478 free(log_pathname);
1479
1480 return ret;
bead01d7
JA
1481}
1482
b5296ddb 1483static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
2e03b4b2 1484{
3989b143 1485 struct cmd_probe_reply_pdu *probe = (struct cmd_probe_reply_pdu *) cmd->payload;
d2333358
JA
1486 const char *os, *arch;
1487 char bit[16];
2e03b4b2 1488
cca84643
JA
1489 os = fio_get_os_string(probe->os);
1490 if (!os)
1491 os = "unknown";
1492
1493 arch = fio_get_arch_string(probe->arch);
1494 if (!arch)
1495 os = "unknown";
1496
d2333358 1497 sprintf(bit, "%d-bit", probe->bpp * 8);
3989b143 1498 probe->flags = le64_to_cpu(probe->flags);
38fdef22 1499
2eca4892 1500 if (output_format & FIO_OUTPUT_NORMAL) {
44f4f5e6
TK
1501 log_info("hostname=%s, be=%u, %s, os=%s, arch=%s, fio=%s, flags=%lx\n",
1502 probe->hostname, probe->bigendian, bit, os, arch,
1503 probe->fio_version, (unsigned long) probe->flags);
2eca4892 1504 }
b5296ddb
JA
1505
1506 if (!client->name)
1507 client->name = strdup((char *) probe->hostname);
2e03b4b2
JA
1508}
1509
11e950bd
JA
1510static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd)
1511{
1512 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1513
1514 client->state = Client_started;
1515 client->jobs = le32_to_cpu(pdu->jobs);
a89d5319 1516 client->nr_stat = le32_to_cpu(pdu->stat_outputs);
108fea77 1517
0279b880
JA
1518 if (client->jobs) {
1519 int i;
1520
1521 if (client->opt_lists)
1522 free(client->opt_lists);
1523
1524 client->opt_lists = malloc(client->jobs * sizeof(struct flist_head));
1525 for (i = 0; i < client->jobs; i++)
1526 INIT_FLIST_HEAD(&client->opt_lists[i]);
1527 }
1528
a89d5319 1529 sum_stat_clients += client->nr_stat;
11e950bd
JA
1530}
1531
36ed2f9a 1532static void handle_stop(struct fio_client *client)
11e950bd 1533{
498c92c2
JA
1534 if (client->error)
1535 log_info("client <%s>: exited with error %d\n", client->hostname, client->error);
11e950bd
JA
1536}
1537
6b79c80c
JA
1538static void convert_stop(struct fio_net_cmd *cmd)
1539{
1540 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1541
1542 pdu->error = le32_to_cpu(pdu->error);
1543}
1544
084d1c6f
JA
1545static void convert_text(struct fio_net_cmd *cmd)
1546{
1547 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
1548
1549 pdu->level = le32_to_cpu(pdu->level);
1550 pdu->buf_len = le32_to_cpu(pdu->buf_len);
1551 pdu->log_sec = le64_to_cpu(pdu->log_sec);
1552 pdu->log_usec = le64_to_cpu(pdu->log_usec);
1553}
1554
3989b143
JA
1555static struct cmd_iolog_pdu *convert_iolog_gz(struct fio_net_cmd *cmd,
1556 struct cmd_iolog_pdu *pdu)
1b42725f 1557{
3989b143 1558#ifdef CONFIG_ZLIB
1b42725f 1559 struct cmd_iolog_pdu *ret;
1b42725f 1560 z_stream stream;
7df9ac89 1561 uint64_t nr_samples;
3989b143 1562 size_t total;
a9c9608e 1563 char *p;
1b42725f
JA
1564
1565 stream.zalloc = Z_NULL;
1566 stream.zfree = Z_NULL;
1567 stream.opaque = Z_NULL;
1568 stream.avail_in = 0;
1569 stream.next_in = Z_NULL;
1570
1571 if (inflateInit(&stream) != Z_OK)
1572 return NULL;
1573
1574 /*
f5ed765a 1575 * Get header first, it's not compressed
1b42725f 1576 */
3c865f32 1577 nr_samples = le64_to_cpu(pdu->nr_samples);
1b42725f 1578
868f6f03
KC
1579 if (pdu->log_type == IO_LOG_TYPE_HIST)
1580 total = nr_samples * (__log_entry_sz(le32_to_cpu(pdu->log_offset)) +
1581 sizeof(struct io_u_plat_entry));
1582 else
1583 total = nr_samples * __log_entry_sz(le32_to_cpu(pdu->log_offset));
f5ed765a 1584 ret = malloc(total + sizeof(*pdu));
1b42725f 1585 ret->nr_samples = nr_samples;
3989b143
JA
1586
1587 memcpy(ret, pdu, sizeof(*pdu));
f5ed765a 1588
a9c9608e 1589 p = (char *) ret + sizeof(*pdu);
1b42725f 1590
f5ed765a 1591 stream.avail_in = cmd->pdu_len - sizeof(*pdu);
a9c9608e 1592 stream.next_in = (void *)((char *) pdu + sizeof(*pdu));
1b42725f
JA
1593 while (stream.avail_in) {
1594 unsigned int this_chunk = 65536;
1595 unsigned int this_len;
1596 int err;
1597
1598 if (this_chunk > total)
1599 this_chunk = total;
1600
1601 stream.avail_out = this_chunk;
a9c9608e 1602 stream.next_out = (void *)p;
1b42725f 1603 err = inflate(&stream, Z_NO_FLUSH);
3c547fe0
JA
1604 /* may be Z_OK, or Z_STREAM_END */
1605 if (err < 0) {
1b42725f 1606 log_err("fio: inflate error %d\n", err);
f5ed765a
JA
1607 free(ret);
1608 ret = NULL;
3989b143 1609 goto err;
1b42725f
JA
1610 }
1611
1612 this_len = this_chunk - stream.avail_out;
1613 p += this_len;
1614 total -= this_len;
1615 }
1616
3989b143
JA
1617err:
1618 inflateEnd(&stream);
1619 return ret;
1620#else
1621 return NULL;
1622#endif
1623}
1624
1625/*
1626 * This has been compressed on the server side, since it can be big.
1627 * Uncompress here.
1628 */
0cba0f91
JA
1629static struct cmd_iolog_pdu *convert_iolog(struct fio_net_cmd *cmd,
1630 bool *store_direct)
3989b143
JA
1631{
1632 struct cmd_iolog_pdu *pdu = (struct cmd_iolog_pdu *) cmd->payload;
1633 struct cmd_iolog_pdu *ret;
ae588852 1634 uint64_t i;
0cba0f91 1635 int compressed;
ae588852 1636 void *samples;
3989b143 1637
0cba0f91
JA
1638 *store_direct = false;
1639
3989b143
JA
1640 /*
1641 * Convert if compressed and we support it. If it's not
1642 * compressed, we need not do anything.
1643 */
0cba0f91
JA
1644 compressed = le32_to_cpu(pdu->compressed);
1645 if (compressed == XMIT_COMPRESSED) {
3989b143
JA
1646#ifndef CONFIG_ZLIB
1647 log_err("fio: server sent compressed data by mistake\n");
1648 return NULL;
1649#endif
1650 ret = convert_iolog_gz(cmd, pdu);
1651 if (!ret) {
1652 log_err("fio: failed decompressing log\n");
1653 return NULL;
1654 }
0cba0f91
JA
1655 } else if (compressed == STORE_COMPRESSED) {
1656 *store_direct = true;
1657 ret = pdu;
3989b143
JA
1658 } else
1659 ret = pdu;
1660
ae588852 1661 ret->nr_samples = le64_to_cpu(ret->nr_samples);
3989b143 1662 ret->thread_number = le32_to_cpu(ret->thread_number);
3989b143
JA
1663 ret->log_type = le32_to_cpu(ret->log_type);
1664 ret->compressed = le32_to_cpu(ret->compressed);
ae588852 1665 ret->log_offset = le32_to_cpu(ret->log_offset);
868f6f03 1666 ret->log_hist_coarseness = le32_to_cpu(ret->log_hist_coarseness);
3989b143 1667
0cba0f91
JA
1668 if (*store_direct)
1669 return ret;
1670
3310fced 1671 samples = &ret->samples[0];
f5ed765a 1672 for (i = 0; i < ret->nr_samples; i++) {
ae588852 1673 struct io_sample *s;
f5ed765a 1674
ae588852 1675 s = __get_sample(samples, ret->log_offset, i);
868f6f03 1676 if (ret->log_type == IO_LOG_TYPE_HIST)
a9c9608e 1677 s = (struct io_sample *)((char *)s + sizeof(struct io_u_plat_entry) * i);
868f6f03 1678
b26317c9 1679 s->time = le64_to_cpu(s->time);
af2fde19 1680 s->data.val = le64_to_cpu(s->data.val);
b26317c9 1681 s->__ddir = le32_to_cpu(s->__ddir);
5fff9543 1682 s->bs = le64_to_cpu(s->bs);
ae588852
JA
1683
1684 if (ret->log_offset) {
1685 struct io_sample_offset *so = (void *) s;
1686
1687 so->offset = le64_to_cpu(so->offset);
1688 }
868f6f03
KC
1689
1690 if (ret->log_type == IO_LOG_TYPE_HIST) {
a9c9608e 1691 s->data.plat_entry = (struct io_u_plat_entry *)(((char *)s) + sizeof(*s));
af2fde19
SW
1692 s->data.plat_entry->list.next = NULL;
1693 s->data.plat_entry->list.prev = NULL;
868f6f03 1694 }
f5ed765a
JA
1695 }
1696
1b42725f
JA
1697 return ret;
1698}
1699
ca09be4b
JA
1700static void sendfile_reply(int fd, struct cmd_sendfile_reply *rep,
1701 size_t size, uint64_t tag)
1702{
1703 rep->error = cpu_to_le32(rep->error);
1704 fio_net_send_cmd(fd, FIO_NET_CMD_SENDFILE, rep, size, &tag, NULL);
1705}
1706
0748d801
JA
1707static int fio_send_file(struct fio_client *client, struct cmd_sendfile *pdu,
1708 uint64_t tag)
ca09be4b
JA
1709{
1710 struct cmd_sendfile_reply *rep;
1711 struct stat sb;
1712 size_t size;
1713 int fd;
1714
1715 size = sizeof(*rep);
1716 rep = malloc(size);
1717
1718 if (stat((char *)pdu->path, &sb) < 0) {
1719fail:
1720 rep->error = errno;
1721 sendfile_reply(client->fd, rep, size, tag);
1722 free(rep);
1723 return 1;
1724 }
1725
1726 size += sb.st_size;
1727 rep = realloc(rep, size);
1728 rep->size = cpu_to_le32((uint32_t) sb.st_size);
1729
1730 fd = open((char *)pdu->path, O_RDONLY);
1731 if (fd == -1 )
1732 goto fail;
1733
1734 rep->error = read_data(fd, &rep->data, sb.st_size);
1735 sendfile_reply(client->fd, rep, size, tag);
1736 free(rep);
1737 close(fd);
1738 return 0;
1739}
1740
a5276616 1741int fio_handle_client(struct fio_client *client)
37db14fe 1742{
a5276616 1743 struct client_ops *ops = client->ops;
37db14fe 1744 struct fio_net_cmd *cmd;
bb49c8bd 1745 int size;
37db14fe 1746
60efd14e
JA
1747 dprint(FD_NET, "client: handle %s\n", client->hostname);
1748
54e62ad4 1749 cmd = fio_net_recv_cmd(client->fd, true);
e951bdc4
JA
1750 if (!cmd)
1751 return 0;
c2c94585 1752
b9d2f30a
JA
1753 dprint(FD_NET, "client: got cmd op %s from %s (pdu=%u)\n",
1754 fio_server_op(cmd->opcode), client->hostname, cmd->pdu_len);
46c48f1f 1755
47714d68
JA
1756 client->last_cmd = cmd->opcode;
1757
e951bdc4
JA
1758 switch (cmd->opcode) {
1759 case FIO_NET_CMD_QUIT:
3ec62ec4 1760 if (ops->quit)
35c0ba7f 1761 ops->quit(client, cmd);
e951bdc4 1762 remove_client(client);
e951bdc4 1763 break;
084d1c6f
JA
1764 case FIO_NET_CMD_TEXT:
1765 convert_text(cmd);
35c0ba7f 1766 ops->text(client, cmd);
e951bdc4 1767 break;
3bf236c0
JA
1768 case FIO_NET_CMD_DU: {
1769 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
1770
1771 convert_dus(&du->dus);
1772 convert_agg(&du->agg);
1773
dd366728 1774 ops->disk_util(client, cmd);
d09a64a0 1775 break;
3bf236c0
JA
1776 }
1777 case FIO_NET_CMD_TS: {
1778 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
1779
bb49c8bd 1780 dprint(FD_NET, "client: ts->ss_state = %u\n", (unsigned int) le32_to_cpu(p->ts.ss_state));
c8caba48 1781 if (le32_to_cpu(p->ts.ss_state) & FIO_SS_DATA) {
bb49c8bd
VF
1782 dprint(FD_NET, "client: received steadystate ring buffers\n");
1783
1784 size = le64_to_cpu(p->ts.ss_dur);
1785 p->ts.ss_iops_data = (uint64_t *) ((struct cmd_ts_pdu *)cmd->payload + 1);
1786 p->ts.ss_bw_data = p->ts.ss_iops_data + size;
1787 }
1788
3bf236c0
JA
1789 convert_ts(&p->ts, &p->ts);
1790 convert_gs(&p->rs, &p->rs);
1791
89e5fad9 1792 ops->thread_status(client, cmd);
e951bdc4 1793 break;
3bf236c0
JA
1794 }
1795 case FIO_NET_CMD_GS: {
1796 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
1797
1798 convert_gs(gs, gs);
1799
89e5fad9 1800 ops->group_stats(client, cmd);
e951bdc4 1801 break;
3bf236c0
JA
1802 }
1803 case FIO_NET_CMD_ETA: {
1804 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
1805
308d69b5
JA
1806 if (!remove_reply_cmd(client, cmd))
1807 break;
3bf236c0 1808 convert_jobs_eta(je);
a5276616 1809 handle_eta(client, cmd);
e951bdc4 1810 break;
3bf236c0 1811 }
e951bdc4 1812 case FIO_NET_CMD_PROBE:
89c1707c 1813 remove_reply_cmd(client, cmd);
dd366728 1814 ops->probe(client, cmd);
e951bdc4 1815 break;
5d7793aa 1816 case FIO_NET_CMD_SERVER_START:
01be038e 1817 client->state = Client_running;
85dd01e7
JA
1818 if (ops->job_start)
1819 ops->job_start(client, cmd);
01be038e 1820 break;
85dd01e7
JA
1821 case FIO_NET_CMD_START: {
1822 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
1823
1824 pdu->jobs = le32_to_cpu(pdu->jobs);
1825 ops->start(client, cmd);
e951bdc4 1826 break;
85dd01e7 1827 }
6b79c80c
JA
1828 case FIO_NET_CMD_STOP: {
1829 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
1830
1831 convert_stop(cmd);
1832 client->state = Client_stopped;
122c7725
JA
1833 client->error = le32_to_cpu(pdu->error);
1834 client->signal = le32_to_cpu(pdu->signal);
36ed2f9a 1835 ops->stop(client);
e951bdc4 1836 break;
6b79c80c 1837 }
40c60516
JA
1838 case FIO_NET_CMD_ADD_JOB: {
1839 struct cmd_add_job_pdu *pdu = (struct cmd_add_job_pdu *) cmd->payload;
1840
1841 client->thread_number = le32_to_cpu(pdu->thread_number);
1842 client->groupid = le32_to_cpu(pdu->groupid);
1843
807f9971
JA
1844 if (ops->add_job)
1845 ops->add_job(client, cmd);
807f9971 1846 break;
40c60516 1847 }
1b42725f 1848 case FIO_NET_CMD_IOLOG:
0cba0f91 1849 fio_client_handle_iolog(client, cmd);
1b42725f 1850 break;
40c60516 1851 case FIO_NET_CMD_UPDATE_JOB:
40c60516 1852 ops->update_job(client, cmd);
649cee91 1853 remove_reply_cmd(client, cmd);
40c60516 1854 break;
ca09be4b
JA
1855 case FIO_NET_CMD_VTRIGGER: {
1856 struct all_io_list *pdu = (struct all_io_list *) cmd->payload;
d264264a
JA
1857 char buf[128];
1858 int off = 0;
ca09be4b 1859
d264264a
JA
1860 if (aux_path) {
1861 strcpy(buf, aux_path);
1862 off = strlen(buf);
1863 }
1864
1865 __verify_save_state(pdu, server_name(client, &buf[off], sizeof(buf) - off));
13c70218 1866 exec_trigger(trigger_cmd);
ca09be4b
JA
1867 break;
1868 }
1869 case FIO_NET_CMD_SENDFILE: {
1870 struct cmd_sendfile *pdu = (struct cmd_sendfile *) cmd->payload;
0748d801 1871 fio_send_file(client, pdu, cmd->tag);
ca09be4b
JA
1872 break;
1873 }
0279b880
JA
1874 case FIO_NET_CMD_JOB_OPT: {
1875 handle_job_opt(client, cmd);
1876 break;
1877 }
e951bdc4 1878 default:
89c1707c 1879 log_err("fio: unknown client op: %s\n", fio_server_op(cmd->opcode));
e951bdc4 1880 break;
37db14fe
JA
1881 }
1882
ca09be4b 1883 free(cmd);
e951bdc4 1884 return 1;
37db14fe 1885}
b66570dc 1886
ca09be4b
JA
1887int fio_clients_send_trigger(const char *cmd)
1888{
1889 struct flist_head *entry;
1890 struct fio_client *client;
1891 size_t slen;
1892
1893 dprint(FD_NET, "client: send vtrigger: %s\n", cmd);
1894
1895 if (!cmd)
1896 slen = 0;
1897 else
1898 slen = strlen(cmd);
1899
1900 flist_for_each(entry, &client_list) {
1901 struct cmd_vtrigger_pdu *pdu;
1902
1903 client = flist_entry(entry, struct fio_client, list);
1904
1905 pdu = malloc(sizeof(*pdu) + slen);
1906 pdu->len = cpu_to_le16((uint16_t) slen);
1907 if (slen)
1908 memcpy(pdu->cmd, cmd, slen);
1909 fio_net_send_cmd(client->fd, FIO_NET_CMD_VTRIGGER, pdu,
1910 sizeof(*pdu) + slen, NULL, NULL);
1911 free(pdu);
1912 }
1913
1914 return 0;
1915}
1916
a5276616 1917static void request_client_etas(struct client_ops *ops)
af9c9fb3
JA
1918{
1919 struct fio_client *client;
1920 struct flist_head *entry;
1921 struct client_eta *eta;
82c1ed38 1922 int skipped = 0;
af9c9fb3 1923
5610bcd8
JA
1924 if (eta_print == FIO_ETA_NEVER)
1925 return;
1926
af9c9fb3
JA
1927 dprint(FD_NET, "client: request eta (%d)\n", nr_clients);
1928
afe22d3d 1929 eta = calloc(1, sizeof(*eta) + __THREAD_RUNSTR_SZ(REAL_MAX_JOBS));
af9c9fb3
JA
1930 eta->pending = nr_clients;
1931
1932 flist_for_each(entry, &client_list) {
1933 client = flist_entry(entry, struct fio_client, list);
1934
82c1ed38
JA
1935 if (!flist_empty(&client->eta_list)) {
1936 skipped++;
1937 continue;
1938 }
01be038e
JA
1939 if (client->state != Client_running)
1940 continue;
82c1ed38 1941
f77d2676 1942 assert(!client->eta_in_flight);
82c1ed38 1943 flist_add_tail(&client->eta_list, &eta_list);
f77d2676 1944 client->eta_in_flight = eta;
af9c9fb3 1945 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA,
df380934 1946 (uintptr_t) eta, &client->cmd_list);
af9c9fb3
JA
1947 }
1948
f02f7dac
JA
1949 while (skipped--) {
1950 if (!fio_client_dec_jobs_eta(eta, ops->eta))
1951 break;
1952 }
82c1ed38 1953
af9c9fb3
JA
1954 dprint(FD_NET, "client: requested eta tag %p\n", eta);
1955}
1956
a64c13a4
JA
1957/*
1958 * A single SEND_ETA timeout isn't fatal. Attempt to recover.
1959 */
1960static int handle_cmd_timeout(struct fio_client *client,
1961 struct fio_net_cmd_reply *reply)
1962{
7d9407b7
EV
1963 uint16_t reply_opcode = reply->opcode;
1964
be6fa258
JA
1965 flist_del(&reply->list);
1966 free(reply);
1967
7d9407b7 1968 if (reply_opcode != FIO_NET_CMD_SEND_ETA)
a64c13a4
JA
1969 return 1;
1970
1971 log_info("client <%s>: timeout on SEND_ETA\n", client->hostname);
a64c13a4
JA
1972
1973 flist_del_init(&client->eta_list);
1974 if (client->eta_in_flight) {
1975 fio_client_dec_jobs_eta(client->eta_in_flight, client->ops->eta);
1976 client->eta_in_flight = NULL;
1977 }
1978
1979 /*
1980 * If we fail 5 in a row, give up...
1981 */
1982 if (client->eta_timeouts++ > 5)
1983 return 1;
1984
1985 return 0;
1986}
1987
89c1707c 1988static int client_check_cmd_timeout(struct fio_client *client,
8b6a404c 1989 struct timespec *now)
89c1707c 1990{
40c60516 1991 struct fio_net_cmd_reply *reply;
89c1707c
JA
1992 struct flist_head *entry, *tmp;
1993 int ret = 0;
1994
1995 flist_for_each_safe(entry, tmp, &client->cmd_list) {
1d484506
JA
1996 unsigned int op;
1997
40c60516 1998 reply = flist_entry(entry, struct fio_net_cmd_reply, list);
89c1707c 1999
8b6a404c 2000 if (mtime_since(&reply->ts, now) < FIO_NET_CLIENT_TIMEOUT)
89c1707c
JA
2001 continue;
2002
1d484506 2003 op = reply->opcode;
a64c13a4
JA
2004 if (!handle_cmd_timeout(client, reply))
2005 continue;
2006
89c1707c 2007 log_err("fio: client %s, timeout on cmd %s\n", client->hostname,
1d484506 2008 fio_server_op(op));
89c1707c
JA
2009 ret = 1;
2010 }
2011
2012 return flist_empty(&client->cmd_list) && ret;
2013}
2014
a5276616 2015static int fio_check_clients_timed_out(void)
89c1707c
JA
2016{
2017 struct fio_client *client;
2018 struct flist_head *entry, *tmp;
8b6a404c 2019 struct timespec ts;
89c1707c
JA
2020 int ret = 0;
2021
8b6a404c 2022 fio_gettime(&ts, NULL);
89c1707c
JA
2023
2024 flist_for_each_safe(entry, tmp, &client_list) {
2025 client = flist_entry(entry, struct fio_client, list);
2026
2027 if (flist_empty(&client->cmd_list))
2028 continue;
2029
8b6a404c 2030 if (!client_check_cmd_timeout(client, &ts))
89c1707c
JA
2031 continue;
2032
a5276616
JA
2033 if (client->ops->timed_out)
2034 client->ops->timed_out(client);
ed727a46
JA
2035 else
2036 log_err("fio: client %s timed out\n", client->hostname);
2037
47714d68
JA
2038 if (client->last_cmd != FIO_NET_CMD_VTRIGGER)
2039 client->error = ETIMEDOUT;
2040 else
2041 log_info("fio: ignoring timeout due to vtrigger\n");
89c1707c
JA
2042 remove_client(client);
2043 ret = 1;
2044 }
2045
2046 return ret;
2047}
2048
dd366728 2049int fio_handle_clients(struct client_ops *ops)
b66570dc 2050{
b66570dc 2051 struct pollfd *pfds;
498c92c2 2052 int i, ret = 0, retval = 0;
b66570dc 2053
8b6a404c 2054 fio_gettime(&eta_ts, NULL);
af9c9fb3 2055
b66570dc
JA
2056 pfds = malloc(nr_clients * sizeof(struct pollfd));
2057
37f0c1ae
JA
2058 init_thread_stat(&client_ts);
2059 init_group_run_stat(&client_gs);
2060
82a4be1b 2061 while (!exit_backend && nr_clients) {
c2cb6869
JA
2062 struct flist_head *entry, *tmp;
2063 struct fio_client *client;
2064
82a4be1b 2065 i = 0;
c2cb6869 2066 flist_for_each_safe(entry, tmp, &client_list) {
82a4be1b 2067 client = flist_entry(entry, struct fio_client, list);
b66570dc 2068
a5276616 2069 if (!client->sent_job && !client->ops->stay_connected &&
c2cb6869
JA
2070 flist_empty(&client->cmd_list)) {
2071 remove_client(client);
2072 continue;
2073 }
2074
82a4be1b
JA
2075 pfds[i].fd = client->fd;
2076 pfds[i].events = POLLIN;
2077 i++;
2078 }
2079
c2cb6869
JA
2080 if (!nr_clients)
2081 break;
2082
82a4be1b 2083 assert(i == nr_clients);
b66570dc 2084
5c2857f9 2085 do {
8b6a404c 2086 struct timespec ts;
ca09be4b 2087 int timeout;
af9c9fb3 2088
8b6a404c 2089 fio_gettime(&ts, NULL);
db37d890 2090 if (eta_time_within_slack(mtime_since(&eta_ts, &ts))) {
a5276616 2091 request_client_etas(ops);
8b6a404c 2092 memcpy(&eta_ts, &ts, sizeof(ts));
89c1707c 2093
a5276616 2094 if (fio_check_clients_timed_out())
89c1707c 2095 break;
af9c9fb3
JA
2096 }
2097
ca09be4b
JA
2098 check_trigger_file();
2099
2100 timeout = min(100u, ops->eta_msec);
2101
2102 ret = poll(pfds, nr_clients, timeout);
5c2857f9
JA
2103 if (ret < 0) {
2104 if (errno == EINTR)
2105 continue;
2106 log_err("fio: poll clients: %s\n", strerror(errno));
2107 break;
2108 } else if (!ret)
b66570dc 2109 continue;
5c2857f9 2110 } while (ret <= 0);
b66570dc
JA
2111
2112 for (i = 0; i < nr_clients; i++) {
2113 if (!(pfds[i].revents & POLLIN))
2114 continue;
2115
2116 client = find_client_by_fd(pfds[i].fd);
2117 if (!client) {
65e1a120 2118 log_err("fio: unknown client fd %ld\n", (long) pfds[i].fd);
b66570dc
JA
2119 continue;
2120 }
a5276616 2121 if (!fio_handle_client(client)) {
28d3ab07
JA
2122 log_info("client: host=%s disconnected\n",
2123 client->hostname);
2124 remove_client(client);
498c92c2 2125 retval = 1;
38990764 2126 } else if (client->error)
498c92c2 2127 retval = 1;
343cb4a9 2128 fio_put_client(client);
b66570dc
JA
2129 }
2130 }
2131
952b05e0
CF
2132 fio_client_json_fini();
2133
b66570dc 2134 free(pfds);
f9a33cc0 2135 return retval || error_clients;
b66570dc 2136}
44f4f5e6
TK
2137
2138static void client_display_thread_status(struct jobs_eta *je)
2139{
2140 if (!(output_format & FIO_OUTPUT_JSON))
2141 display_thread_status(je);
2142}