Add client references
[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
dd366728 24static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd);
89e5fad9
JA
25static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd);
26static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd);
dd366728 27static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd);
084d1c6f 28static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd);
dd366728
SC
29
30struct client_ops fio_client_ops = {
084d1c6f 31 .text_op = handle_text,
0420ba6a
JA
32 .disk_util = handle_du,
33 .thread_status = handle_ts,
34 .group_stats = handle_gs,
a5276616 35 .eta = display_thread_status,
0420ba6a 36 .probe = handle_probe,
dd366728
SC
37};
38
af9c9fb3 39static struct timeval eta_tv;
48fbb46e 40
81179eec 41enum {
5c2857f9 42 Client_created = 0,
81179eec
JA
43 Client_connected = 1,
44 Client_started = 2,
01be038e
JA
45 Client_running = 3,
46 Client_stopped = 4,
47 Client_exited = 5,
b66570dc
JA
48};
49
50static FLIST_HEAD(client_list);
82c1ed38 51static FLIST_HEAD(eta_list);
b66570dc 52
3f3a4542
JA
53static FLIST_HEAD(arg_list);
54
37f0c1ae
JA
55static struct thread_stat client_ts;
56static struct group_run_stats client_gs;
57static int sum_stat_clients;
58static int sum_stat_nr;
59
3c5f57e3
JA
60#define FIO_CLIENT_HASH_BITS 7
61#define FIO_CLIENT_HASH_SZ (1 << FIO_CLIENT_HASH_BITS)
62#define FIO_CLIENT_HASH_MASK (FIO_CLIENT_HASH_SZ - 1)
bebe6398 63static struct flist_head client_hash[FIO_CLIENT_HASH_SZ];
3c5f57e3 64
bebe6398 65static void fio_client_add_hash(struct fio_client *client)
3c5f57e3
JA
66{
67 int bucket = hash_long(client->fd, FIO_CLIENT_HASH_BITS);
68
69 bucket &= FIO_CLIENT_HASH_MASK;
bebe6398 70 flist_add(&client->hash_list, &client_hash[bucket]);
3c5f57e3
JA
71}
72
bebe6398 73static void fio_client_remove_hash(struct fio_client *client)
3c5f57e3 74{
bebe6398
JA
75 if (!flist_empty(&client->hash_list))
76 flist_del_init(&client->hash_list);
3c5f57e3
JA
77}
78
79static void fio_init fio_client_hash_init(void)
80{
81 int i;
82
bebe6398
JA
83 for (i = 0; i < FIO_CLIENT_HASH_SZ; i++)
84 INIT_FLIST_HEAD(&client_hash[i]);
3c5f57e3
JA
85}
86
b66570dc
JA
87static struct fio_client *find_client_by_fd(int fd)
88{
3c5f57e3 89 int bucket = hash_long(fd, FIO_CLIENT_HASH_BITS) & FIO_CLIENT_HASH_MASK;
b66570dc
JA
90 struct fio_client *client;
91 struct flist_head *entry;
92
bebe6398
JA
93 flist_for_each(entry, &client_hash[bucket]) {
94 client = flist_entry(entry, struct fio_client, hash_list);
b66570dc 95
5121a9aa
JA
96 if (client->fd == fd) {
97 client->refs++;
b66570dc 98 return client;
5121a9aa 99 }
b66570dc
JA
100 }
101
102 return NULL;
103}
104
b66570dc
JA
105static void remove_client(struct fio_client *client)
106{
5121a9aa
JA
107 assert(client->refs);
108
109 if (--client->refs)
110 return;
111
39e8e016 112 dprint(FD_NET, "client: removed <%s>\n", client->hostname);
b66570dc 113 flist_del(&client->list);
3c5f57e3 114
bebe6398 115 fio_client_remove_hash(client);
81179eec 116
82c1ed38
JA
117 if (!flist_empty(&client->eta_list)) {
118 flist_del_init(&client->eta_list);
a5276616 119 fio_client_dec_jobs_eta(client->eta_in_flight, client->ops->eta);
82c1ed38 120 }
af9c9fb3 121
b66570dc 122 free(client->hostname);
81179eec
JA
123 if (client->argv)
124 free(client->argv);
b5296ddb
JA
125 if (client->name)
126 free(client->name);
81179eec 127
b66570dc 128 free(client);
3c5f57e3 129 nr_clients--;
5fd0acbd 130 sum_stat_clients--;
b66570dc 131}
132159a5 132
5121a9aa
JA
133static void put_client(struct fio_client *client)
134{
135 remove_client(client);
136}
137
fa2ea806
JA
138static void __fio_client_add_cmd_option(struct fio_client *client,
139 const char *opt)
81179eec 140{
39e8e016
JA
141 int index;
142
143 index = client->argc++;
81179eec 144 client->argv = realloc(client->argv, sizeof(char *) * client->argc);
39e8e016
JA
145 client->argv[index] = strdup(opt);
146 dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
81179eec
JA
147}
148
fa2ea806 149void fio_client_add_cmd_option(void *cookie, const char *opt)
81179eec 150{
bebe6398 151 struct fio_client *client = cookie;
3f3a4542 152 struct flist_head *entry;
81179eec 153
bebe6398 154 if (!client || !opt)
fa2ea806 155 return;
81179eec 156
fa2ea806 157 __fio_client_add_cmd_option(client, opt);
3f3a4542
JA
158
159 /*
160 * Duplicate arguments to shared client group
161 */
162 flist_for_each(entry, &arg_list) {
163 client = flist_entry(entry, struct fio_client, arg_list);
164
165 __fio_client_add_cmd_option(client, opt);
166 }
81179eec
JA
167}
168
a5276616
JA
169struct fio_client *fio_client_add_explicit(struct client_ops *ops,
170 const char *hostname, int type,
3ec62ec4
JA
171 int port)
172{
173 struct fio_client *client;
174
175 client = malloc(sizeof(*client));
176 memset(client, 0, sizeof(*client));
177
178 INIT_FLIST_HEAD(&client->list);
179 INIT_FLIST_HEAD(&client->hash_list);
180 INIT_FLIST_HEAD(&client->arg_list);
181 INIT_FLIST_HEAD(&client->eta_list);
182 INIT_FLIST_HEAD(&client->cmd_list);
183
184 client->hostname = strdup(hostname);
185
186 if (type == Fio_client_socket)
187 client->is_sock = 1;
188 else {
189 int ipv6;
190
191 ipv6 = type == Fio_client_ipv6;
192 if (fio_server_parse_host(hostname, &ipv6,
193 &client->addr.sin_addr,
194 &client->addr6.sin6_addr))
195 goto err;
196
197 client->port = port;
198 }
199
200 client->fd = -1;
a5276616 201 client->ops = ops;
5121a9aa 202 client->refs = 1;
3ec62ec4
JA
203
204 __fio_client_add_cmd_option(client, "fio");
205
206 flist_add(&client->list, &client_list);
207 nr_clients++;
208 dprint(FD_NET, "client: added <%s>\n", client->hostname);
209 return client;
210err:
211 free(client);
212 return NULL;
213}
214
a5276616 215int fio_client_add(struct client_ops *ops, const char *hostname, void **cookie)
132159a5 216{
3f3a4542 217 struct fio_client *existing = *cookie;
b66570dc 218 struct fio_client *client;
132159a5 219
3f3a4542
JA
220 if (existing) {
221 /*
222 * We always add our "exec" name as the option, hence 1
223 * means empty.
224 */
225 if (existing->argc == 1)
226 flist_add_tail(&existing->arg_list, &arg_list);
227 else {
228 while (!flist_empty(&arg_list))
229 flist_del_init(arg_list.next);
230 }
231 }
232
b66570dc 233 client = malloc(sizeof(*client));
a37f69b7 234 memset(client, 0, sizeof(*client));
81179eec 235
3c5f57e3 236 INIT_FLIST_HEAD(&client->list);
bebe6398 237 INIT_FLIST_HEAD(&client->hash_list);
3f3a4542 238 INIT_FLIST_HEAD(&client->arg_list);
82c1ed38 239 INIT_FLIST_HEAD(&client->eta_list);
89c1707c 240 INIT_FLIST_HEAD(&client->cmd_list);
3c5f57e3 241
bebe6398
JA
242 if (fio_server_parse_string(hostname, &client->hostname,
243 &client->is_sock, &client->port,
811826be
JA
244 &client->addr.sin_addr,
245 &client->addr6.sin6_addr,
246 &client->ipv6))
bebe6398 247 return -1;
87aa8f19 248
bebe6398 249 client->fd = -1;
a5276616 250 client->ops = ops;
5121a9aa 251 client->refs = 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
89e5fad9 638static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd)
a64e88da
JA
639{
640 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
641
a64e88da 642 show_thread_status(&p->ts, &p->rs);
37f0c1ae
JA
643
644 if (sum_stat_clients == 1)
645 return;
646
647 sum_thread_stats(&client_ts, &p->ts, sum_stat_nr);
648 sum_group_stats(&client_gs, &p->rs);
649
650 client_ts.members++;
651 client_ts.groupid = p->ts.groupid;
652
653 if (++sum_stat_nr == sum_stat_clients) {
654 strcpy(client_ts.name, "All clients");
655 show_thread_status(&client_ts, &client_gs);
656 }
a64e88da
JA
657}
658
89e5fad9 659static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd)
a64e88da
JA
660{
661 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
662
a64e88da
JA
663 show_group_stats(gs);
664}
665
3bf236c0
JA
666static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd)
667{
668 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
669 const char *buf = (const char *) pdu->buf;
670 const char *name;
671 int fio_unused ret;
672
673 name = client->name ? client->name : client->hostname;
674
675 if (!client->skip_newline)
676 fprintf(f_out, "<%s> ", name);
677 ret = fwrite(buf, pdu->buf_len, 1, f_out);
678 fflush(f_out);
679 client->skip_newline = strchr(buf, '\n') == NULL;
680}
681
d09a64a0
JA
682static void convert_agg(struct disk_util_agg *agg)
683{
684 int i;
685
686 for (i = 0; i < 2; i++) {
687 agg->ios[i] = le32_to_cpu(agg->ios[i]);
688 agg->merges[i] = le32_to_cpu(agg->merges[i]);
689 agg->sectors[i] = le64_to_cpu(agg->sectors[i]);
690 agg->ticks[i] = le32_to_cpu(agg->ticks[i]);
691 }
692
693 agg->io_ticks = le32_to_cpu(agg->io_ticks);
694 agg->time_in_queue = le32_to_cpu(agg->time_in_queue);
695 agg->slavecount = le32_to_cpu(agg->slavecount);
823ba54b 696 agg->max_util.u.f = fio_uint64_to_double(__le64_to_cpu(agg->max_util.u.i));
d09a64a0
JA
697}
698
699static void convert_dus(struct disk_util_stat *dus)
700{
701 int i;
702
703 for (i = 0; i < 2; i++) {
704 dus->ios[i] = le32_to_cpu(dus->ios[i]);
705 dus->merges[i] = le32_to_cpu(dus->merges[i]);
706 dus->sectors[i] = le64_to_cpu(dus->sectors[i]);
707 dus->ticks[i] = le32_to_cpu(dus->ticks[i]);
708 }
709
710 dus->io_ticks = le32_to_cpu(dus->io_ticks);
711 dus->time_in_queue = le32_to_cpu(dus->time_in_queue);
712 dus->msec = le64_to_cpu(dus->msec);
713}
714
715static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd)
716{
717 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
718
d09a64a0
JA
719 if (!client->disk_stats_shown) {
720 client->disk_stats_shown = 1;
721 log_info("\nDisk stats (read/write):\n");
722 }
723
f2f788dd 724 print_disk_util(&du->dus, &du->agg, terse_output);
d09a64a0
JA
725}
726
3bf236c0 727static void convert_jobs_eta(struct jobs_eta *je)
cf451d1e 728{
cf451d1e
JA
729 int i;
730
731 je->nr_running = le32_to_cpu(je->nr_running);
732 je->nr_ramp = le32_to_cpu(je->nr_ramp);
733 je->nr_pending = le32_to_cpu(je->nr_pending);
734 je->files_open = le32_to_cpu(je->files_open);
cf451d1e
JA
735
736 for (i = 0; i < 2; i++) {
3e47bd25
JA
737 je->m_rate[i] = le32_to_cpu(je->m_rate[i]);
738 je->t_rate[i] = le32_to_cpu(je->t_rate[i]);
739 je->m_iops[i] = le32_to_cpu(je->m_iops[i]);
740 je->t_iops[i] = le32_to_cpu(je->t_iops[i]);
cf451d1e
JA
741 je->rate[i] = le32_to_cpu(je->rate[i]);
742 je->iops[i] = le32_to_cpu(je->iops[i]);
743 }
744
b51eedb7 745 je->elapsed_sec = le64_to_cpu(je->elapsed_sec);
cf451d1e 746 je->eta_sec = le64_to_cpu(je->eta_sec);
48fbb46e
JA
747}
748
3e47bd25 749void fio_client_sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je)
48fbb46e 750{
48fbb46e
JA
751 int i;
752
753 dst->nr_running += je->nr_running;
754 dst->nr_ramp += je->nr_ramp;
755 dst->nr_pending += je->nr_pending;
756 dst->files_open += je->files_open;
48fbb46e
JA
757
758 for (i = 0; i < 2; i++) {
3e47bd25
JA
759 dst->m_rate[i] += je->m_rate[i];
760 dst->t_rate[i] += je->t_rate[i];
761 dst->m_iops[i] += je->m_iops[i];
762 dst->t_iops[i] += je->t_iops[i];
48fbb46e
JA
763 dst->rate[i] += je->rate[i];
764 dst->iops[i] += je->iops[i];
765 }
766
767 dst->elapsed_sec += je->elapsed_sec;
768
769 if (je->eta_sec > dst->eta_sec)
770 dst->eta_sec = je->eta_sec;
771}
772
a5276616 773void fio_client_dec_jobs_eta(struct client_eta *eta, client_eta_op eta_fn)
82c1ed38
JA
774{
775 if (!--eta->pending) {
a5276616 776 eta_fn(&eta->eta);
82c1ed38
JA
777 free(eta);
778 }
779}
780
89c1707c
JA
781static void remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd)
782{
783 struct fio_net_int_cmd *icmd = NULL;
784 struct flist_head *entry;
785
786 flist_for_each(entry, &client->cmd_list) {
787 icmd = flist_entry(entry, struct fio_net_int_cmd, list);
788
df380934 789 if (cmd->tag == (uintptr_t) icmd)
89c1707c
JA
790 break;
791
792 icmd = NULL;
793 }
794
795 if (!icmd) {
796 log_err("fio: client: unable to find matching tag\n");
797 return;
798 }
799
800 flist_del(&icmd->list);
801 cmd->tag = icmd->saved_tag;
802 free(icmd);
803}
804
82c1ed38 805static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd)
48fbb46e
JA
806{
807 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
df380934 808 struct client_eta *eta = (struct client_eta *) (uintptr_t) cmd->tag;
af9c9fb3
JA
809
810 dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending);
cf451d1e 811
f77d2676
JA
812 assert(client->eta_in_flight == eta);
813
814 client->eta_in_flight = NULL;
82c1ed38
JA
815 flist_del_init(&client->eta_list);
816
3e47bd25 817 fio_client_sum_jobs_eta(&eta->eta, je);
a5276616 818 fio_client_dec_jobs_eta(eta, client->ops->eta);
cf451d1e
JA
819}
820
b5296ddb 821static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd)
2e03b4b2
JA
822{
823 struct cmd_probe_pdu *probe = (struct cmd_probe_pdu *) cmd->payload;
d2333358
JA
824 const char *os, *arch;
825 char bit[16];
2e03b4b2 826
cca84643
JA
827 os = fio_get_os_string(probe->os);
828 if (!os)
829 os = "unknown";
830
831 arch = fio_get_arch_string(probe->arch);
832 if (!arch)
833 os = "unknown";
834
d2333358 835 sprintf(bit, "%d-bit", probe->bpp * 8);
38fdef22
JA
836
837 log_info("hostname=%s, be=%u, %s, os=%s, arch=%s, fio=%u.%u.%u\n",
838 probe->hostname, probe->bigendian, bit, os, arch,
839 probe->fio_major, probe->fio_minor, probe->fio_patch);
b5296ddb
JA
840
841 if (!client->name)
842 client->name = strdup((char *) probe->hostname);
2e03b4b2
JA
843}
844
11e950bd
JA
845static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd)
846{
847 struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload;
848
849 client->state = Client_started;
850 client->jobs = le32_to_cpu(pdu->jobs);
851}
852
853static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd)
854{
855 struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload;
856
857 client->state = Client_stopped;
858 client->error = le32_to_cpu(pdu->error);
498c92c2
JA
859
860 if (client->error)
861 log_info("client <%s>: exited with error %d\n", client->hostname, client->error);
11e950bd
JA
862}
863
084d1c6f
JA
864static void convert_text(struct fio_net_cmd *cmd)
865{
866 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload;
867
868 pdu->level = le32_to_cpu(pdu->level);
869 pdu->buf_len = le32_to_cpu(pdu->buf_len);
870 pdu->log_sec = le64_to_cpu(pdu->log_sec);
871 pdu->log_usec = le64_to_cpu(pdu->log_usec);
872}
873
a5276616 874int fio_handle_client(struct fio_client *client)
37db14fe 875{
a5276616 876 struct client_ops *ops = client->ops;
37db14fe
JA
877 struct fio_net_cmd *cmd;
878
60efd14e
JA
879 dprint(FD_NET, "client: handle %s\n", client->hostname);
880
e951bdc4
JA
881 cmd = fio_net_recv_cmd(client->fd);
882 if (!cmd)
883 return 0;
c2c94585 884
89c1707c
JA
885 dprint(FD_NET, "client: got cmd op %s from %s\n",
886 fio_server_op(cmd->opcode), client->hostname);
46c48f1f 887
e951bdc4
JA
888 switch (cmd->opcode) {
889 case FIO_NET_CMD_QUIT:
3ec62ec4
JA
890 if (ops->quit)
891 ops->quit(client);
e951bdc4
JA
892 remove_client(client);
893 free(cmd);
894 break;
084d1c6f
JA
895 case FIO_NET_CMD_TEXT:
896 convert_text(cmd);
897 ops->text_op(client, cmd);
e951bdc4
JA
898 free(cmd);
899 break;
3bf236c0
JA
900 case FIO_NET_CMD_DU: {
901 struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload;
902
903 convert_dus(&du->dus);
904 convert_agg(&du->agg);
905
dd366728 906 ops->disk_util(client, cmd);
d09a64a0
JA
907 free(cmd);
908 break;
3bf236c0
JA
909 }
910 case FIO_NET_CMD_TS: {
911 struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload;
912
913 convert_ts(&p->ts, &p->ts);
914 convert_gs(&p->rs, &p->rs);
915
89e5fad9 916 ops->thread_status(client, cmd);
e951bdc4
JA
917 free(cmd);
918 break;
3bf236c0
JA
919 }
920 case FIO_NET_CMD_GS: {
921 struct group_run_stats *gs = (struct group_run_stats *) cmd->payload;
922
923 convert_gs(gs, gs);
924
89e5fad9 925 ops->group_stats(client, cmd);
e951bdc4
JA
926 free(cmd);
927 break;
3bf236c0
JA
928 }
929 case FIO_NET_CMD_ETA: {
930 struct jobs_eta *je = (struct jobs_eta *) cmd->payload;
931
89c1707c 932 remove_reply_cmd(client, cmd);
3bf236c0 933 convert_jobs_eta(je);
a5276616 934 handle_eta(client, cmd);
e951bdc4
JA
935 free(cmd);
936 break;
3bf236c0 937 }
e951bdc4 938 case FIO_NET_CMD_PROBE:
89c1707c 939 remove_reply_cmd(client, cmd);
dd366728 940 ops->probe(client, cmd);
e951bdc4
JA
941 free(cmd);
942 break;
01be038e
JA
943 case FIO_NET_CMD_RUN:
944 client->state = Client_running;
945 free(cmd);
946 break;
e951bdc4 947 case FIO_NET_CMD_START:
11e950bd 948 handle_start(client, cmd);
e951bdc4
JA
949 free(cmd);
950 break;
951 case FIO_NET_CMD_STOP:
11e950bd 952 handle_stop(client, cmd);
e951bdc4
JA
953 free(cmd);
954 break;
807f9971
JA
955 case FIO_NET_CMD_ADD_JOB:
956 if (ops->add_job)
957 ops->add_job(client, cmd);
958 free(cmd);
959 break;
e951bdc4 960 default:
89c1707c 961 log_err("fio: unknown client op: %s\n", fio_server_op(cmd->opcode));
e951bdc4
JA
962 free(cmd);
963 break;
37db14fe
JA
964 }
965
e951bdc4 966 return 1;
37db14fe 967}
b66570dc 968
a5276616 969static void request_client_etas(struct client_ops *ops)
af9c9fb3
JA
970{
971 struct fio_client *client;
972 struct flist_head *entry;
973 struct client_eta *eta;
82c1ed38 974 int skipped = 0;
af9c9fb3
JA
975
976 dprint(FD_NET, "client: request eta (%d)\n", nr_clients);
977
af9c9fb3
JA
978 eta = malloc(sizeof(*eta));
979 memset(&eta->eta, 0, sizeof(eta->eta));
980 eta->pending = nr_clients;
981
982 flist_for_each(entry, &client_list) {
983 client = flist_entry(entry, struct fio_client, list);
984
82c1ed38
JA
985 if (!flist_empty(&client->eta_list)) {
986 skipped++;
987 continue;
988 }
01be038e
JA
989 if (client->state != Client_running)
990 continue;
82c1ed38 991
f77d2676 992 assert(!client->eta_in_flight);
82c1ed38 993 flist_add_tail(&client->eta_list, &eta_list);
f77d2676 994 client->eta_in_flight = eta;
af9c9fb3 995 fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA,
df380934 996 (uintptr_t) eta, &client->cmd_list);
af9c9fb3
JA
997 }
998
82c1ed38 999 while (skipped--)
a5276616 1000 fio_client_dec_jobs_eta(eta, ops->eta);
82c1ed38 1001
af9c9fb3
JA
1002 dprint(FD_NET, "client: requested eta tag %p\n", eta);
1003}
1004
89c1707c
JA
1005static int client_check_cmd_timeout(struct fio_client *client,
1006 struct timeval *now)
1007{
1008 struct fio_net_int_cmd *cmd;
1009 struct flist_head *entry, *tmp;
1010 int ret = 0;
1011
1012 flist_for_each_safe(entry, tmp, &client->cmd_list) {
1013 cmd = flist_entry(entry, struct fio_net_int_cmd, list);
1014
1015 if (mtime_since(&cmd->tv, now) < FIO_NET_CLIENT_TIMEOUT)
1016 continue;
1017
1018 log_err("fio: client %s, timeout on cmd %s\n", client->hostname,
1019 fio_server_op(cmd->cmd.opcode));
1020 flist_del(&cmd->list);
1021 free(cmd);
1022 ret = 1;
1023 }
1024
1025 return flist_empty(&client->cmd_list) && ret;
1026}
1027
a5276616 1028static int fio_check_clients_timed_out(void)
89c1707c
JA
1029{
1030 struct fio_client *client;
1031 struct flist_head *entry, *tmp;
1032 struct timeval tv;
1033 int ret = 0;
1034
1035 gettimeofday(&tv, NULL);
1036
1037 flist_for_each_safe(entry, tmp, &client_list) {
1038 client = flist_entry(entry, struct fio_client, list);
1039
1040 if (flist_empty(&client->cmd_list))
1041 continue;
1042
1043 if (!client_check_cmd_timeout(client, &tv))
1044 continue;
1045
a5276616
JA
1046 if (client->ops->timed_out)
1047 client->ops->timed_out(client);
ed727a46
JA
1048 else
1049 log_err("fio: client %s timed out\n", client->hostname);
1050
89c1707c
JA
1051 remove_client(client);
1052 ret = 1;
1053 }
1054
1055 return ret;
1056}
1057
dd366728 1058int fio_handle_clients(struct client_ops *ops)
b66570dc 1059{
b66570dc 1060 struct pollfd *pfds;
498c92c2 1061 int i, ret = 0, retval = 0;
b66570dc 1062
af9c9fb3
JA
1063 gettimeofday(&eta_tv, NULL);
1064
b66570dc
JA
1065 pfds = malloc(nr_clients * sizeof(struct pollfd));
1066
37f0c1ae
JA
1067 sum_stat_clients = nr_clients;
1068 init_thread_stat(&client_ts);
1069 init_group_run_stat(&client_gs);
1070
82a4be1b 1071 while (!exit_backend && nr_clients) {
c2cb6869
JA
1072 struct flist_head *entry, *tmp;
1073 struct fio_client *client;
1074
82a4be1b 1075 i = 0;
c2cb6869 1076 flist_for_each_safe(entry, tmp, &client_list) {
82a4be1b 1077 client = flist_entry(entry, struct fio_client, list);
b66570dc 1078
a5276616 1079 if (!client->sent_job && !client->ops->stay_connected &&
c2cb6869
JA
1080 flist_empty(&client->cmd_list)) {
1081 remove_client(client);
1082 continue;
1083 }
1084
82a4be1b
JA
1085 pfds[i].fd = client->fd;
1086 pfds[i].events = POLLIN;
1087 i++;
1088 }
1089
c2cb6869
JA
1090 if (!nr_clients)
1091 break;
1092
82a4be1b 1093 assert(i == nr_clients);
b66570dc 1094
5c2857f9 1095 do {
af9c9fb3
JA
1096 struct timeval tv;
1097
1098 gettimeofday(&tv, NULL);
1099 if (mtime_since(&eta_tv, &tv) >= 900) {
a5276616 1100 request_client_etas(ops);
af9c9fb3 1101 memcpy(&eta_tv, &tv, sizeof(tv));
89c1707c 1102
a5276616 1103 if (fio_check_clients_timed_out())
89c1707c 1104 break;
af9c9fb3
JA
1105 }
1106
5c2857f9
JA
1107 ret = poll(pfds, nr_clients, 100);
1108 if (ret < 0) {
1109 if (errno == EINTR)
1110 continue;
1111 log_err("fio: poll clients: %s\n", strerror(errno));
1112 break;
1113 } else if (!ret)
b66570dc 1114 continue;
5c2857f9 1115 } while (ret <= 0);
b66570dc
JA
1116
1117 for (i = 0; i < nr_clients; i++) {
1118 if (!(pfds[i].revents & POLLIN))
1119 continue;
1120
1121 client = find_client_by_fd(pfds[i].fd);
1122 if (!client) {
3c5f57e3 1123 log_err("fio: unknown client fd %d\n", pfds[i].fd);
b66570dc
JA
1124 continue;
1125 }
a5276616 1126 if (!fio_handle_client(client)) {
28d3ab07
JA
1127 log_info("client: host=%s disconnected\n",
1128 client->hostname);
1129 remove_client(client);
498c92c2 1130 retval = 1;
38990764 1131 } else if (client->error)
498c92c2 1132 retval = 1;
5121a9aa 1133 put_client(client);
b66570dc
JA
1134 }
1135 }
1136
1137 free(pfds);
498c92c2 1138 return retval;
b66570dc 1139}