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