Rename job files
[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>
132159a5
JA
17
18#include "fio.h"
dd366728 19#include "client.h"
132159a5 20#include "server.h"
b66570dc 21#include "flist.h"
3c5f57e3 22#include "hash.h"
132159a5 23
04cc6b77 24extern void (*update_thread_status)(char *status_message, double perc);
ba936c4b 25
82c1ed38
JA
26struct client_eta {
27 struct jobs_eta eta;
28 unsigned int pending;
29};
30
b66570dc
JA
31struct fio_client {
32 struct flist_head list;
bebe6398 33 struct flist_head hash_list;
3f3a4542 34 struct flist_head arg_list;
811826be
JA
35 union {
36 struct sockaddr_in addr;
37 struct sockaddr_in6 addr6;
38 struct sockaddr_un addr_un;
39 };
b66570dc 40 char *hostname;
bebe6398 41 int port;
b66570dc 42 int fd;
81179eec 43
b5296ddb
JA
44 char *name;
45
81179eec 46 int state;
af9c9fb3 47
17dd1764 48 int skip_newline;
87aa8f19 49 int is_sock;
d09a64a0 50 int disk_stats_shown;
11e950bd
JA
51 unsigned int jobs;
52 int error;
811826be 53 int ipv6;
c2cb6869 54 int sent_job;
82c1ed38
JA
55
56 struct flist_head eta_list;
57 struct client_eta *eta_in_flight;
81179eec 58
89c1707c
JA
59 struct flist_head cmd_list;
60
81179eec
JA
61 uint16_t argc;
62 char **argv;
63};
64
dd366728
SC
65static void fio_client_text_op(struct fio_client *client,
66 FILE *f, __u16 pdu_len, const char *buf)
67{
68 const char *name;
69 int fio_unused ret;
70
71 name = client->name ? client->name : client->hostname;
72
73 if (!client->skip_newline)
74 fprintf(f, "<%s> ", name);
75 ret = fwrite(buf, pdu_len, 1, f);
76 fflush(f);
77 client->skip_newline = strchr(buf, '\n') == NULL;
78}
79
80static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd);
81static void handle_ts(struct fio_net_cmd *cmd);
82static void handle_gs(struct fio_net_cmd *cmd);
83static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd);
84static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd);
85
86struct client_ops fio_client_ops = {
87 fio_client_text_op,
88 handle_du,
89 handle_ts,
90 handle_gs,
91 handle_eta,
92 handle_probe,
ba936c4b 93 NULL, /* status display, if NULL, printf is used */
dd366728
SC
94};
95
af9c9fb3 96static struct timeval eta_tv;
48fbb46e 97
81179eec 98enum {
5c2857f9 99 Client_created = 0,
81179eec
JA
100 Client_connected = 1,
101 Client_started = 2,
01be038e
JA
102 Client_running = 3,
103 Client_stopped = 4,
104 Client_exited = 5,
b66570dc
JA
105};
106
107static FLIST_HEAD(client_list);
82c1ed38 108static FLIST_HEAD(eta_list);
b66570dc 109
3f3a4542
JA
110static FLIST_HEAD(arg_list);
111
37f0c1ae
JA
112static struct thread_stat client_ts;
113static struct group_run_stats client_gs;
114static int sum_stat_clients;
115static int sum_stat_nr;
116
3c5f57e3
JA
117#define FIO_CLIENT_HASH_BITS 7
118#define FIO_CLIENT_HASH_SZ (1 << FIO_CLIENT_HASH_BITS)
119#define FIO_CLIENT_HASH_MASK (FIO_CLIENT_HASH_SZ - 1)
bebe6398 120static struct flist_head client_hash[FIO_CLIENT_HASH_SZ];
3c5f57e3 121
dd366728 122static int handle_client(struct fio_client *client, struct client_ops *ops);
82c1ed38 123static void dec_jobs_eta(struct client_eta *eta);
0b8f30a5 124
bebe6398 125static void fio_client_add_hash(struct fio_client *client)
3c5f57e3
JA
126{
127 int bucket = hash_long(client->fd, FIO_CLIENT_HASH_BITS);
128
129 bucket &= FIO_CLIENT_HASH_MASK;
bebe6398 130 flist_add(&client->hash_list, &client_hash[bucket]);
3c5f57e3
JA
131}
132
bebe6398 133static void fio_client_remove_hash(struct fio_client *client)
3c5f57e3 134{
bebe6398
JA
135 if (!flist_empty(&client->hash_list))
136 flist_del_init(&client->hash_list);
3c5f57e3
JA
137}
138
139static void fio_init fio_client_hash_init(void)
140{
141 int i;
142
bebe6398
JA
143 for (i = 0; i < FIO_CLIENT_HASH_SZ; i++)
144 INIT_FLIST_HEAD(&client_hash[i]);
3c5f57e3
JA
145}
146
b66570dc
JA
147static struct fio_client *find_client_by_fd(int fd)
148{
3c5f57e3 149 int bucket = hash_long(fd, FIO_CLIENT_HASH_BITS) & FIO_CLIENT_HASH_MASK;
b66570dc
JA
150 struct fio_client *client;
151 struct flist_head *entry;
152
bebe6398
JA
153 flist_for_each(entry, &client_hash[bucket]) {
154 client = flist_entry(entry, struct fio_client, hash_list);
b66570dc
JA
155
156 if (client->fd == fd)
157 return client;
158 }
159
160 return NULL;
161}
162
b66570dc
JA
163static void remove_client(struct fio_client *client)
164{
39e8e016 165 dprint(FD_NET, "client: removed <%s>\n", client->hostname);
b66570dc 166 flist_del(&client->list);
3c5f57e3 167
bebe6398 168 fio_client_remove_hash(client);
81179eec 169
82c1ed38
JA
170 if (!flist_empty(&client->eta_list)) {
171 flist_del_init(&client->eta_list);
172 dec_jobs_eta(client->eta_in_flight);
173 }
af9c9fb3 174
b66570dc 175 free(client->hostname);
81179eec
JA
176 if (client->argv)
177 free(client->argv);
b5296ddb
JA
178 if (client->name)
179 free(client->name);
81179eec 180
b66570dc 181 free(client);
3c5f57e3 182 nr_clients--;
5fd0acbd 183 sum_stat_clients--;
b66570dc 184}
132159a5 185
fa2ea806
JA
186static void __fio_client_add_cmd_option(struct fio_client *client,
187 const char *opt)
81179eec 188{
39e8e016
JA
189 int index;
190
191 index = client->argc++;
81179eec 192 client->argv = realloc(client->argv, sizeof(char *) * client->argc);
39e8e016
JA
193 client->argv[index] = strdup(opt);
194 dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
81179eec
JA
195}
196
fa2ea806 197void fio_client_add_cmd_option(void *cookie, const char *opt)
81179eec 198{
bebe6398 199 struct fio_client *client = cookie;
3f3a4542 200 struct flist_head *entry;
81179eec 201
bebe6398 202 if (!client || !opt)
fa2ea806 203 return;
81179eec 204
fa2ea806 205 __fio_client_add_cmd_option(client, opt);
3f3a4542
JA
206
207 /*
208 * Duplicate arguments to shared client group
209 */
210 flist_for_each(entry, &arg_list) {
211 client = flist_entry(entry, struct fio_client, arg_list);
212
213 __fio_client_add_cmd_option(client, opt);
214 }
81179eec
JA
215}
216
bebe6398 217int fio_client_add(const char *hostname, void **cookie)
132159a5 218{
3f3a4542 219 struct fio_client *existing = *cookie;
b66570dc 220 struct fio_client *client;
132159a5 221
3f3a4542
JA
222 if (existing) {
223 /*
224 * We always add our "exec" name as the option, hence 1
225 * means empty.
226 */
227 if (existing->argc == 1)
228 flist_add_tail(&existing->arg_list, &arg_list);
229 else {
230 while (!flist_empty(&arg_list))
231 flist_del_init(arg_list.next);
232 }
233 }
234
b66570dc 235 client = malloc(sizeof(*client));
a37f69b7 236 memset(client, 0, sizeof(*client));
81179eec 237
3c5f57e3 238 INIT_FLIST_HEAD(&client->list);
bebe6398 239 INIT_FLIST_HEAD(&client->hash_list);
3f3a4542 240 INIT_FLIST_HEAD(&client->arg_list);
82c1ed38 241 INIT_FLIST_HEAD(&client->eta_list);
89c1707c 242 INIT_FLIST_HEAD(&client->cmd_list);
3c5f57e3 243
bebe6398
JA
244 if (fio_server_parse_string(hostname, &client->hostname,
245 &client->is_sock, &client->port,
811826be
JA
246 &client->addr.sin_addr,
247 &client->addr6.sin6_addr,
248 &client->ipv6))
bebe6398 249 return -1;
87aa8f19 250
bebe6398 251 client->fd = -1;
3c5f57e3 252
81179eec
JA
253 __fio_client_add_cmd_option(client, "fio");
254
a37f69b7
JA
255 flist_add(&client->list, &client_list);
256 nr_clients++;
bebe6398
JA
257 dprint(FD_NET, "client: added <%s>\n", client->hostname);
258 *cookie = client;
259 return 0;
a37f69b7
JA
260}
261
87aa8f19 262static int fio_client_connect_ip(struct fio_client *client)
a37f69b7 263{
811826be
JA
264 struct sockaddr *addr;
265 fio_socklen_t socklen;
266 int fd, domain;
267
268 if (client->ipv6) {
269 client->addr6.sin6_family = AF_INET6;
270 client->addr6.sin6_port = htons(client->port);
271 domain = AF_INET6;
272 addr = (struct sockaddr *) &client->addr6;
273 socklen = sizeof(client->addr6);
274 } else {
275 client->addr.sin_family = AF_INET;
276 client->addr.sin_port = htons(client->port);
277 domain = AF_INET;
278 addr = (struct sockaddr *) &client->addr;
279 socklen = sizeof(client->addr);
280 }
132159a5 281
811826be 282 fd = socket(domain, SOCK_STREAM, 0);
132159a5
JA
283 if (fd < 0) {
284 log_err("fio: socket: %s\n", strerror(errno));
87aa8f19 285 return -1;
132159a5
JA
286 }
287
811826be 288 if (connect(fd, addr, socklen) < 0) {
132159a5 289 log_err("fio: connect: %s\n", strerror(errno));
a7de0a11
JA
290 log_err("fio: failed to connect to %s:%u\n", client->hostname,
291 client->port);
b94cba47 292 close(fd);
87aa8f19
JA
293 return -1;
294 }
295
296 return fd;
297}
298
299static int fio_client_connect_sock(struct fio_client *client)
300{
301 struct sockaddr_un *addr = &client->addr_un;
302 fio_socklen_t len;
303 int fd;
304
305 memset(addr, 0, sizeof(*addr));
306 addr->sun_family = AF_UNIX;
307 strcpy(addr->sun_path, client->hostname);
308
309 fd = socket(AF_UNIX, SOCK_STREAM, 0);
310 if (fd < 0) {
311 log_err("fio: socket: %s\n", strerror(errno));
312 return -1;
313 }
314
315 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
316 if (connect(fd, (struct sockaddr *) addr, len) < 0) {
317 log_err("fio: connect; %s\n", strerror(errno));
b94cba47 318 close(fd);
87aa8f19 319 return -1;
132159a5
JA
320 }
321
87aa8f19
JA
322 return fd;
323}
324
325static int fio_client_connect(struct fio_client *client)
326{
327 int fd;
328
329 dprint(FD_NET, "client: connect to host %s\n", client->hostname);
330
87aa8f19
JA
331 if (client->is_sock)
332 fd = fio_client_connect_sock(client);
333 else
334 fd = fio_client_connect_ip(client);
335
89c1707c
JA
336 dprint(FD_NET, "client: %s connected %d\n", client->hostname, fd);
337
87aa8f19
JA
338 if (fd < 0)
339 return 1;
340
b66570dc 341 client->fd = fd;
bebe6398 342 fio_client_add_hash(client);
81179eec 343 client->state = Client_connected;
132159a5
JA
344 return 0;
345}
346
cc0df00a
JA
347void fio_clients_terminate(void)
348{
349 struct flist_head *entry;
350 struct fio_client *client;
351
60efd14e
JA
352 dprint(FD_NET, "client: terminate clients\n");
353
cc0df00a
JA
354 flist_for_each(entry, &client_list) {
355 client = flist_entry(entry, struct fio_client, list);
356
89c1707c 357 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_QUIT, 0, NULL);
cc0df00a
JA
358 }
359}
360
361static void sig_int(int sig)
362{
bebe6398 363 dprint(FD_NET, "client: got signal %d\n", sig);
cc0df00a
JA
364 fio_clients_terminate();
365}
366
367static void client_signal_handler(void)
368{
369 struct sigaction act;
370
371 memset(&act, 0, sizeof(act));
372 act.sa_handler = sig_int;
373 act.sa_flags = SA_RESTART;
374 sigaction(SIGINT, &act, NULL);
375
376 memset(&act, 0, sizeof(act));
377 act.sa_handler = sig_int;
378 act.sa_flags = SA_RESTART;
379 sigaction(SIGTERM, &act, NULL);
380}
381
0b8f30a5
JA
382static void probe_client(struct fio_client *client)
383{
60efd14e
JA
384 dprint(FD_NET, "client: send probe\n");
385
89c1707c 386 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_PROBE, 0, &client->cmd_list);
0b8f30a5
JA
387}
388
81179eec
JA
389static int send_client_cmd_line(struct fio_client *client)
390{
fa2ea806
JA
391 struct cmd_single_line_pdu *cslp;
392 struct cmd_line_pdu *clp;
393 unsigned long offset;
7f868316 394 unsigned int *lens;
fa2ea806
JA
395 void *pdu;
396 size_t mem;
81179eec
JA
397 int i, ret;
398
39e8e016 399 dprint(FD_NET, "client: send cmdline %d\n", client->argc);
60efd14e 400
7f868316
JA
401 lens = malloc(client->argc * sizeof(unsigned int));
402
fa2ea806
JA
403 /*
404 * Find out how much mem we need
405 */
7f868316
JA
406 for (i = 0, mem = 0; i < client->argc; i++) {
407 lens[i] = strlen(client->argv[i]) + 1;
408 mem += lens[i];
409 }
fa2ea806
JA
410
411 /*
412 * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu
413 */
414 mem += sizeof(*clp) + (client->argc * sizeof(*cslp));
415
416 pdu = malloc(mem);
417 clp = pdu;
418 offset = sizeof(*clp);
419
420 for (i = 0; i < client->argc; i++) {
7f868316 421 uint16_t arg_len = lens[i];
fa2ea806
JA
422
423 cslp = pdu + offset;
424 strcpy((char *) cslp->text, client->argv[i]);
425 cslp->len = cpu_to_le16(arg_len);
426 offset += sizeof(*cslp) + arg_len;
427 }
81179eec 428
7f868316 429 free(lens);
fa2ea806 430 clp->lines = cpu_to_le16(client->argc);
af9c9fb3 431 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, 0);
81179eec
JA
432 free(pdu);
433 return ret;
434}
435
a37f69b7
JA
436int fio_clients_connect(void)
437{
438 struct fio_client *client;
439 struct flist_head *entry, *tmp;
440 int ret;
441
93bcfd20
BC
442#ifdef WIN32
443 WSADATA wsd;
444 WSAStartup(MAKEWORD(2,2), &wsd);
445#endif
446
60efd14e
JA
447 dprint(FD_NET, "client: connect all\n");
448
cc0df00a
JA
449 client_signal_handler();
450
a37f69b7
JA
451 flist_for_each_safe(entry, tmp, &client_list) {
452 client = flist_entry(entry, struct fio_client, list);
453
454 ret = fio_client_connect(client);
0b8f30a5 455 if (ret) {
a37f69b7 456 remove_client(client);
0b8f30a5
JA
457 continue;
458 }
459
460 probe_client(client);
81179eec
JA
461
462 if (client->argc > 1)
463 send_client_cmd_line(client);
a37f69b7
JA
464 }
465
466 return !nr_clients;
467}
468
132159a5
JA
469/*
470 * Send file contents to server backend. We could use sendfile(), but to remain
471 * more portable lets just read/write the darn thing.
472 */
a37f69b7 473static int fio_client_send_ini(struct fio_client *client, const char *filename)
132159a5
JA
474{
475 struct stat sb;
476 char *p, *buf;
477 off_t len;
478 int fd, ret;
479
46c48f1f
JA
480 dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
481
132159a5
JA
482 fd = open(filename, O_RDONLY);
483 if (fd < 0) {
e951bdc4 484 log_err("fio: job file <%s> open: %s\n", filename, strerror(errno));
132159a5
JA
485 return 1;
486 }
487
488 if (fstat(fd, &sb) < 0) {
489 log_err("fio: job file stat: %s\n", strerror(errno));
b94cba47 490 close(fd);
132159a5
JA
491 return 1;
492 }
493
494 buf = malloc(sb.st_size);
495
496 len = sb.st_size;
497 p = buf;
498 do {
499 ret = read(fd, p, len);
500 if (ret > 0) {
501 len -= ret;
502 if (!len)
503 break;
504 p += ret;
505 continue;
506 } else if (!ret)
507 break;
508 else if (errno == EAGAIN || errno == EINTR)
509 continue;
510 } while (1);
511
0b8f30a5
JA
512 if (len) {
513 log_err("fio: failed reading job file %s\n", filename);
b94cba47 514 close(fd);
c524ef72 515 free(buf);
0b8f30a5
JA
516 return 1;
517 }
518
c2cb6869 519 client->sent_job = 1;
af9c9fb3 520 ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, buf, sb.st_size, 0);
132159a5 521 free(buf);
b94cba47 522 close(fd);
132159a5
JA
523 return ret;
524}
37db14fe 525
a37f69b7
JA
526int fio_clients_send_ini(const char *filename)
527{
528 struct fio_client *client;
529 struct flist_head *entry, *tmp;
530
531 flist_for_each_safe(entry, tmp, &client_list) {
532 client = flist_entry(entry, struct fio_client, list);
533
534 if (fio_client_send_ini(client, filename))
535 remove_client(client);
c2cb6869
JA
536
537 client->sent_job = 1;
a37f69b7
JA
538 }
539
540 return !nr_clients;
541}
542
a64e88da
JA
543static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
544{
545 dst->max_val = le64_to_cpu(src->max_val);
546 dst->min_val = le64_to_cpu(src->min_val);
547 dst->samples = le64_to_cpu(src->samples);
802ad4a8
JA
548
549 /*
550 * Floats arrive as IEEE 754 encoded uint64_t, convert back to double
551 */
552 dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i));
553 dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i));
a64e88da
JA
554}
555
556static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
557{
558 int i, j;
559
560 dst->error = le32_to_cpu(src->error);
561 dst->groupid = le32_to_cpu(src->groupid);
562 dst->pid = le32_to_cpu(src->pid);
563 dst->members = le32_to_cpu(src->members);
564
565 for (i = 0; i < 2; i++) {
566 convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]);
567 convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]);
568 convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]);
569 convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]);
570 }
571
572 dst->usr_time = le64_to_cpu(src->usr_time);
573 dst->sys_time = le64_to_cpu(src->sys_time);
574 dst->ctx = le64_to_cpu(src->ctx);
575 dst->minf = le64_to_cpu(src->minf);
576 dst->majf = le64_to_cpu(src->majf);
577 dst->clat_percentiles = le64_to_cpu(src->clat_percentiles);
802ad4a8
JA
578
579 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
580 fio_fp64_t *fps = &src->percentile_list[i];
581 fio_fp64_t *fpd = &dst->percentile_list[i];
582
583 fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i));
584 }
a64e88da
JA
585
586 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
587 dst->io_u_map[i] = le32_to_cpu(src->io_u_map[i]);
588 dst->io_u_submit[i] = le32_to_cpu(src->io_u_submit[i]);
589 dst->io_u_complete[i] = le32_to_cpu(src->io_u_complete[i]);
590 }
591
592 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
593 dst->io_u_lat_u[i] = le32_to_cpu(src->io_u_lat_u[i]);
594 dst->io_u_lat_m[i] = le32_to_cpu(src->io_u_lat_m[i]);
595 }
596
597 for (i = 0; i < 2; i++)
598 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
599 dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]);
600
601 for (i = 0; i < 3; i++) {
602 dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]);
93eee04a 603 dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]);
a64e88da
JA
604 }
605
606 dst->total_submit = le64_to_cpu(src->total_submit);
607 dst->total_complete = le64_to_cpu(src->total_complete);
608
609 for (i = 0; i < 2; i++) {
610 dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]);
611 dst->runtime[i] = le64_to_cpu(src->runtime[i]);
612 }
613
614 dst->total_run_time = le64_to_cpu(src->total_run_time);
615 dst->continue_on_error = le16_to_cpu(src->continue_on_error);
616 dst->total_err_count = le64_to_cpu(src->total_err_count);
ddcc0b69
JA
617 dst->first_error = le32_to_cpu(src->first_error);
618 dst->kb_base = le32_to_cpu(src->kb_base);
a64e88da
JA
619}
620
621static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
622{
623 int i;
624
625 for (i = 0; i < 2; i++) {
626 dst->max_run[i] = le64_to_cpu(src->max_run[i]);
627 dst->min_run[i] = le64_to_cpu(src->min_run[i]);
628 dst->max_bw[i] = le64_to_cpu(src->max_bw[i]);
629 dst->min_bw[i] = le64_to_cpu(src->min_bw[i]);
630 dst->io_kb[i] = le64_to_cpu(src->io_kb[i]);
631 dst->agg[i] = le64_to_cpu(src->agg[i]);
632 }
633
634 dst->kb_base = le32_to_cpu(src->kb_base);
635 dst->groupid = le32_to_cpu(src->groupid);
636}
637
638static void handle_ts(struct fio_net_cmd *cmd)
639{
640 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
641
642 convert_ts(&p->ts, &p->ts);
643 convert_gs(&p->rs, &p->rs);
644
645 show_thread_status(&p->ts, &p->rs);
37f0c1ae
JA
646
647 if (sum_stat_clients == 1)
648 return;
649
650 sum_thread_stats(&client_ts, &p->ts, sum_stat_nr);
651 sum_group_stats(&client_gs, &p->rs);
652
653 client_ts.members++;
654 client_ts.groupid = p->ts.groupid;
655
656 if (++sum_stat_nr == sum_stat_clients) {
657 strcpy(client_ts.name, "All clients");
658 show_thread_status(&client_ts, &client_gs);
659 }
a64e88da
JA
660}
661
662static void handle_gs(struct fio_net_cmd *cmd)
663{
664 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
665
666 convert_gs(gs, gs);
667 show_group_stats(gs);
668}
669
d09a64a0
JA
670static void convert_agg(struct disk_util_agg *agg)
671{
672 int i;
673
674 for (i = 0; i < 2; i++) {
675 agg->ios[i] = le32_to_cpu(agg->ios[i]);
676 agg->merges[i] = le32_to_cpu(agg->merges[i]);
677 agg->sectors[i] = le64_to_cpu(agg->sectors[i]);
678 agg->ticks[i] = le32_to_cpu(agg->ticks[i]);
679 }
680
681 agg->io_ticks = le32_to_cpu(agg->io_ticks);
682 agg->time_in_queue = le32_to_cpu(agg->time_in_queue);
683 agg->slavecount = le32_to_cpu(agg->slavecount);
823ba54b 684 agg->max_util.u.f = fio_uint64_to_double(__le64_to_cpu(agg->max_util.u.i));
d09a64a0
JA
685}
686
687static void convert_dus(struct disk_util_stat *dus)
688{
689 int i;
690
691 for (i = 0; i < 2; i++) {
692 dus->ios[i] = le32_to_cpu(dus->ios[i]);
693 dus->merges[i] = le32_to_cpu(dus->merges[i]);
694 dus->sectors[i] = le64_to_cpu(dus->sectors[i]);
695 dus->ticks[i] = le32_to_cpu(dus->ticks[i]);
696 }
697
698 dus->io_ticks = le32_to_cpu(dus->io_ticks);
699 dus->time_in_queue = le32_to_cpu(dus->time_in_queue);
700 dus->msec = le64_to_cpu(dus->msec);
701}
702
703static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd)
704{
705 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
706
707 convert_dus(&du->dus);
708 convert_agg(&du->agg);
709
710 if (!client->disk_stats_shown) {
711 client->disk_stats_shown = 1;
712 log_info("\nDisk stats (read/write):\n");
713 }
714
f2f788dd 715 print_disk_util(&du->dus, &du->agg, terse_output);
d09a64a0
JA
716}
717
48fbb46e 718static void convert_jobs_eta(struct jobs_eta *je)
cf451d1e 719{
cf451d1e
JA
720 int i;
721
722 je->nr_running = le32_to_cpu(je->nr_running);
723 je->nr_ramp = le32_to_cpu(je->nr_ramp);
724 je->nr_pending = le32_to_cpu(je->nr_pending);
725 je->files_open = le32_to_cpu(je->files_open);
726 je->m_rate = le32_to_cpu(je->m_rate);
727 je->t_rate = le32_to_cpu(je->t_rate);
728 je->m_iops = le32_to_cpu(je->m_iops);
729 je->t_iops = le32_to_cpu(je->t_iops);
730
731 for (i = 0; i < 2; i++) {
732 je->rate[i] = le32_to_cpu(je->rate[i]);
733 je->iops[i] = le32_to_cpu(je->iops[i]);
734 }
735
b51eedb7 736 je->elapsed_sec = le64_to_cpu(je->elapsed_sec);
cf451d1e 737 je->eta_sec = le64_to_cpu(je->eta_sec);
48fbb46e
JA
738}
739
af9c9fb3 740static void sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je)
48fbb46e 741{
48fbb46e
JA
742 int i;
743
744 dst->nr_running += je->nr_running;
745 dst->nr_ramp += je->nr_ramp;
746 dst->nr_pending += je->nr_pending;
747 dst->files_open += je->files_open;
748 dst->m_rate += je->m_rate;
749 dst->t_rate += je->t_rate;
750 dst->m_iops += je->m_iops;
751 dst->t_iops += je->t_iops;
752
753 for (i = 0; i < 2; i++) {
754 dst->rate[i] += je->rate[i];
755 dst->iops[i] += je->iops[i];
756 }
757
758 dst->elapsed_sec += je->elapsed_sec;
759
760 if (je->eta_sec > dst->eta_sec)
761 dst->eta_sec = je->eta_sec;
762}
763
82c1ed38
JA
764static void dec_jobs_eta(struct client_eta *eta)
765{
766 if (!--eta->pending) {
767 display_thread_status(&eta->eta);
768 free(eta);
769 }
770}
771
89c1707c
JA
772static void remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd)
773{
774 struct fio_net_int_cmd *icmd = NULL;
775 struct flist_head *entry;
776
777 flist_for_each(entry, &client->cmd_list) {
778 icmd = flist_entry(entry, struct fio_net_int_cmd, list);
779
df380934 780 if (cmd->tag == (uintptr_t) icmd)
89c1707c
JA
781 break;
782
783 icmd = NULL;
784 }
785
786 if (!icmd) {
787 log_err("fio: client: unable to find matching tag\n");
788 return;
789 }
790
791 flist_del(&icmd->list);
792 cmd->tag = icmd->saved_tag;
793 free(icmd);
794}
795
82c1ed38 796static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd)
48fbb46e
JA
797{
798 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
df380934 799 struct client_eta *eta = (struct client_eta *) (uintptr_t) cmd->tag;
af9c9fb3
JA
800
801 dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending);
cf451d1e 802
f77d2676
JA
803 assert(client->eta_in_flight == eta);
804
805 client->eta_in_flight = NULL;
82c1ed38
JA
806 flist_del_init(&client->eta_list);
807
48fbb46e 808 convert_jobs_eta(je);
af9c9fb3 809 sum_jobs_eta(&eta->eta, je);
82c1ed38 810 dec_jobs_eta(eta);
cf451d1e
JA
811}
812
b5296ddb 813static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
2e03b4b2
JA
814{
815 struct cmd_probe_pdu *probe = (struct cmd_probe_pdu *) cmd->payload;
d2333358
JA
816 const char *os, *arch;
817 char bit[16];
2e03b4b2 818
cca84643
JA
819 os = fio_get_os_string(probe->os);
820 if (!os)
821 os = "unknown";
822
823 arch = fio_get_arch_string(probe->arch);
824 if (!arch)
825 os = "unknown";
826
d2333358 827 sprintf(bit, "%d-bit", probe->bpp * 8);
38fdef22
JA
828
829 log_info("hostname=%s, be=%u, %s, os=%s, arch=%s, fio=%u.%u.%u\n",
830 probe->hostname, probe->bigendian, bit, os, arch,
831 probe->fio_major, probe->fio_minor, probe->fio_patch);
b5296ddb
JA
832
833 if (!client->name)
834 client->name = strdup((char *) probe->hostname);
2e03b4b2
JA
835}
836
11e950bd
JA
837static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd)
838{
839 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
840
841 client->state = Client_started;
842 client->jobs = le32_to_cpu(pdu->jobs);
843}
844
845static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd)
846{
847 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
848
849 client->state = Client_stopped;
850 client->error = le32_to_cpu(pdu->error);
498c92c2
JA
851
852 if (client->error)
853 log_info("client <%s>: exited with error %d\n", client->hostname, client->error);
11e950bd
JA
854}
855
dd366728 856static int handle_client(struct fio_client *client, struct client_ops *ops)
37db14fe
JA
857{
858 struct fio_net_cmd *cmd;
859
60efd14e
JA
860 dprint(FD_NET, "client: handle %s\n", client->hostname);
861
e951bdc4
JA
862 cmd = fio_net_recv_cmd(client->fd);
863 if (!cmd)
864 return 0;
c2c94585 865
89c1707c
JA
866 dprint(FD_NET, "client: got cmd op %s from %s\n",
867 fio_server_op(cmd->opcode), client->hostname);
46c48f1f 868
e951bdc4
JA
869 switch (cmd->opcode) {
870 case FIO_NET_CMD_QUIT:
871 remove_client(client);
872 free(cmd);
873 break;
874 case FIO_NET_CMD_TEXT: {
875 const char *buf = (const char *) cmd->payload;
dd366728 876 ops->text_op(client, f_out, cmd->pdu_len, buf);
e951bdc4
JA
877 free(cmd);
878 break;
37db14fe 879 }
d09a64a0 880 case FIO_NET_CMD_DU:
dd366728 881 ops->disk_util(client, cmd);
d09a64a0
JA
882 free(cmd);
883 break;
e951bdc4 884 case FIO_NET_CMD_TS:
dd366728 885 ops->thread_status(cmd);
e951bdc4
JA
886 free(cmd);
887 break;
888 case FIO_NET_CMD_GS:
dd366728 889 ops->group_stats(cmd);
e951bdc4
JA
890 free(cmd);
891 break;
892 case FIO_NET_CMD_ETA:
89c1707c 893 remove_reply_cmd(client, cmd);
dd366728 894 ops->eta(client, cmd);
e951bdc4
JA
895 free(cmd);
896 break;
897 case FIO_NET_CMD_PROBE:
89c1707c 898 remove_reply_cmd(client, cmd);
dd366728 899 ops->probe(client, cmd);
e951bdc4
JA
900 free(cmd);
901 break;
01be038e
JA
902 case FIO_NET_CMD_RUN:
903 client->state = Client_running;
904 free(cmd);
905 break;
e951bdc4 906 case FIO_NET_CMD_START:
11e950bd 907 handle_start(client, cmd);
e951bdc4
JA
908 free(cmd);
909 break;
910 case FIO_NET_CMD_STOP:
11e950bd 911 handle_stop(client, cmd);
e951bdc4
JA
912 free(cmd);
913 break;
914 default:
89c1707c 915 log_err("fio: unknown client op: %s\n", fio_server_op(cmd->opcode));
e951bdc4
JA
916 free(cmd);
917 break;
37db14fe
JA
918 }
919
e951bdc4 920 return 1;
37db14fe 921}
b66570dc 922
af9c9fb3
JA
923static void request_client_etas(void)
924{
925 struct fio_client *client;
926 struct flist_head *entry;
927 struct client_eta *eta;
82c1ed38 928 int skipped = 0;
af9c9fb3
JA
929
930 dprint(FD_NET, "client: request eta (%d)\n", nr_clients);
931
af9c9fb3
JA
932 eta = malloc(sizeof(*eta));
933 memset(&eta->eta, 0, sizeof(eta->eta));
934 eta->pending = nr_clients;
935
936 flist_for_each(entry, &client_list) {
937 client = flist_entry(entry, struct fio_client, list);
938
82c1ed38
JA
939 if (!flist_empty(&client->eta_list)) {
940 skipped++;
941 continue;
942 }
01be038e
JA
943 if (client->state != Client_running)
944 continue;
82c1ed38 945
f77d2676 946 assert(!client->eta_in_flight);
82c1ed38 947 flist_add_tail(&client->eta_list, &eta_list);
f77d2676 948 client->eta_in_flight = eta;
af9c9fb3 949 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA,
df380934 950 (uintptr_t) eta, &client->cmd_list);
af9c9fb3
JA
951 }
952
82c1ed38
JA
953 while (skipped--)
954 dec_jobs_eta(eta);
955
af9c9fb3
JA
956 dprint(FD_NET, "client: requested eta tag %p\n", eta);
957}
958
89c1707c
JA
959static int client_check_cmd_timeout(struct fio_client *client,
960 struct timeval *now)
961{
962 struct fio_net_int_cmd *cmd;
963 struct flist_head *entry, *tmp;
964 int ret = 0;
965
966 flist_for_each_safe(entry, tmp, &client->cmd_list) {
967 cmd = flist_entry(entry, struct fio_net_int_cmd, list);
968
969 if (mtime_since(&cmd->tv, now) < FIO_NET_CLIENT_TIMEOUT)
970 continue;
971
972 log_err("fio: client %s, timeout on cmd %s\n", client->hostname,
973 fio_server_op(cmd->cmd.opcode));
974 flist_del(&cmd->list);
975 free(cmd);
976 ret = 1;
977 }
978
979 return flist_empty(&client->cmd_list) && ret;
980}
981
982static int fio_client_timed_out(void)
983{
984 struct fio_client *client;
985 struct flist_head *entry, *tmp;
986 struct timeval tv;
987 int ret = 0;
988
989 gettimeofday(&tv, NULL);
990
991 flist_for_each_safe(entry, tmp, &client_list) {
992 client = flist_entry(entry, struct fio_client, list);
993
994 if (flist_empty(&client->cmd_list))
995 continue;
996
997 if (!client_check_cmd_timeout(client, &tv))
998 continue;
999
1000 log_err("fio: client %s timed out\n", client->hostname);
1001 remove_client(client);
1002 ret = 1;
1003 }
1004
1005 return ret;
1006}
1007
dd366728 1008int fio_handle_clients(struct client_ops *ops)
b66570dc 1009{
b66570dc 1010 struct pollfd *pfds;
498c92c2 1011 int i, ret = 0, retval = 0;
b66570dc 1012
af9c9fb3
JA
1013 gettimeofday(&eta_tv, NULL);
1014
b66570dc
JA
1015 pfds = malloc(nr_clients * sizeof(struct pollfd));
1016
37f0c1ae
JA
1017 sum_stat_clients = nr_clients;
1018 init_thread_stat(&client_ts);
1019 init_group_run_stat(&client_gs);
1020
ba936c4b
SC
1021 /* Used by eta.c:display_thread_status() */
1022 update_thread_status = ops->thread_status_display;
1023
82a4be1b 1024 while (!exit_backend && nr_clients) {
c2cb6869
JA
1025 struct flist_head *entry, *tmp;
1026 struct fio_client *client;
1027
82a4be1b 1028 i = 0;
c2cb6869 1029 flist_for_each_safe(entry, tmp, &client_list) {
82a4be1b 1030 client = flist_entry(entry, struct fio_client, list);
b66570dc 1031
c2cb6869
JA
1032 if (!client->sent_job &&
1033 flist_empty(&client->cmd_list)) {
1034 remove_client(client);
1035 continue;
1036 }
1037
82a4be1b
JA
1038 pfds[i].fd = client->fd;
1039 pfds[i].events = POLLIN;
1040 i++;
1041 }
1042
c2cb6869
JA
1043 if (!nr_clients)
1044 break;
1045
82a4be1b 1046 assert(i == nr_clients);
b66570dc 1047
5c2857f9 1048 do {
af9c9fb3
JA
1049 struct timeval tv;
1050
1051 gettimeofday(&tv, NULL);
1052 if (mtime_since(&eta_tv, &tv) >= 900) {
1053 request_client_etas();
1054 memcpy(&eta_tv, &tv, sizeof(tv));
89c1707c
JA
1055
1056 if (fio_client_timed_out())
1057 break;
af9c9fb3
JA
1058 }
1059
5c2857f9
JA
1060 ret = poll(pfds, nr_clients, 100);
1061 if (ret < 0) {
1062 if (errno == EINTR)
1063 continue;
1064 log_err("fio: poll clients: %s\n", strerror(errno));
1065 break;
1066 } else if (!ret)
b66570dc 1067 continue;
5c2857f9 1068 } while (ret <= 0);
b66570dc
JA
1069
1070 for (i = 0; i < nr_clients; i++) {
1071 if (!(pfds[i].revents & POLLIN))
1072 continue;
1073
1074 client = find_client_by_fd(pfds[i].fd);
1075 if (!client) {
3c5f57e3 1076 log_err("fio: unknown client fd %d\n", pfds[i].fd);
b66570dc
JA
1077 continue;
1078 }
dd366728 1079 if (!handle_client(client, ops)) {
28d3ab07
JA
1080 log_info("client: host=%s disconnected\n",
1081 client->hostname);
1082 remove_client(client);
498c92c2 1083 retval = 1;
38990764 1084 } else if (client->error)
498c92c2 1085 retval = 1;
b66570dc
JA
1086 }
1087 }
1088
1089 free(pfds);
498c92c2 1090 return retval;
b66570dc 1091}