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