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