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