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