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> |
1b42725f | 17 | #include <zlib.h> |
132159a5 JA |
18 | |
19 | #include "fio.h" | |
dd366728 | 20 | #include "client.h" |
132159a5 | 21 | #include "server.h" |
b66570dc | 22 | #include "flist.h" |
3c5f57e3 | 23 | #include "hash.h" |
132159a5 | 24 | |
dd366728 | 25 | static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd); |
89e5fad9 JA |
26 | static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd); |
27 | static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd); | |
dd366728 | 28 | static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd); |
084d1c6f | 29 | static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd); |
6b79c80c | 30 | static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd); |
85dd01e7 | 31 | static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd); |
dd366728 SC |
32 | |
33 | struct client_ops fio_client_ops = { | |
35c0ba7f | 34 | .text = handle_text, |
0420ba6a JA |
35 | .disk_util = handle_du, |
36 | .thread_status = handle_ts, | |
37 | .group_stats = handle_gs, | |
6b79c80c | 38 | .stop = handle_stop, |
85dd01e7 | 39 | .start = handle_start, |
a5276616 | 40 | .eta = display_thread_status, |
0420ba6a | 41 | .probe = handle_probe, |
6433ee05 | 42 | .eta_msec = FIO_CLIENT_DEF_ETA_MSEC, |
46bcd498 | 43 | .client_type = FIO_CLIENT_TYPE_CLI, |
dd366728 SC |
44 | }; |
45 | ||
af9c9fb3 | 46 | static struct timeval eta_tv; |
48fbb46e | 47 | |
b66570dc | 48 | static FLIST_HEAD(client_list); |
82c1ed38 | 49 | static FLIST_HEAD(eta_list); |
b66570dc | 50 | |
3f3a4542 JA |
51 | static FLIST_HEAD(arg_list); |
52 | ||
3650a3ca JA |
53 | struct thread_stat client_ts; |
54 | struct group_run_stats client_gs; | |
55 | int sum_stat_clients; | |
56 | ||
37f0c1ae JA |
57 | static int sum_stat_nr; |
58 | ||
3c5f57e3 JA |
59 | #define FIO_CLIENT_HASH_BITS 7 |
60 | #define FIO_CLIENT_HASH_SZ (1 << FIO_CLIENT_HASH_BITS) | |
61 | #define FIO_CLIENT_HASH_MASK (FIO_CLIENT_HASH_SZ - 1) | |
bebe6398 | 62 | static struct flist_head client_hash[FIO_CLIENT_HASH_SZ]; |
3c5f57e3 | 63 | |
bebe6398 | 64 | static void fio_client_add_hash(struct fio_client *client) |
3c5f57e3 JA |
65 | { |
66 | int bucket = hash_long(client->fd, FIO_CLIENT_HASH_BITS); | |
67 | ||
68 | bucket &= FIO_CLIENT_HASH_MASK; | |
bebe6398 | 69 | flist_add(&client->hash_list, &client_hash[bucket]); |
3c5f57e3 JA |
70 | } |
71 | ||
bebe6398 | 72 | static void fio_client_remove_hash(struct fio_client *client) |
3c5f57e3 | 73 | { |
bebe6398 JA |
74 | if (!flist_empty(&client->hash_list)) |
75 | flist_del_init(&client->hash_list); | |
3c5f57e3 JA |
76 | } |
77 | ||
78 | static void fio_init fio_client_hash_init(void) | |
79 | { | |
80 | int i; | |
81 | ||
bebe6398 JA |
82 | for (i = 0; i < FIO_CLIENT_HASH_SZ; i++) |
83 | INIT_FLIST_HEAD(&client_hash[i]); | |
3c5f57e3 JA |
84 | } |
85 | ||
b66570dc JA |
86 | static struct fio_client *find_client_by_fd(int fd) |
87 | { | |
3c5f57e3 | 88 | int bucket = hash_long(fd, FIO_CLIENT_HASH_BITS) & FIO_CLIENT_HASH_MASK; |
b66570dc JA |
89 | struct fio_client *client; |
90 | struct flist_head *entry; | |
91 | ||
bebe6398 JA |
92 | flist_for_each(entry, &client_hash[bucket]) { |
93 | client = flist_entry(entry, struct fio_client, hash_list); | |
b66570dc | 94 | |
e55f8f30 JA |
95 | if (client->fd == fd) { |
96 | client->refs++; | |
b66570dc | 97 | return client; |
e55f8f30 | 98 | } |
b66570dc JA |
99 | } |
100 | ||
101 | return NULL; | |
102 | } | |
103 | ||
3af45201 | 104 | void fio_put_client(struct fio_client *client) |
b66570dc | 105 | { |
5121a9aa JA |
106 | if (--client->refs) |
107 | return; | |
108 | ||
3af45201 JA |
109 | free(client->hostname); |
110 | if (client->argv) | |
111 | free(client->argv); | |
112 | if (client->name) | |
113 | free(client->name); | |
14ea90ed JA |
114 | while (client->nr_ini_file) |
115 | free(client->ini_file[--client->nr_ini_file]); | |
116 | if (client->ini_file) | |
117 | free(client->ini_file); | |
3af45201 JA |
118 | |
119 | free(client); | |
120 | } | |
121 | ||
122 | static void remove_client(struct fio_client *client) | |
123 | { | |
124 | assert(client->refs); | |
125 | ||
39e8e016 | 126 | dprint(FD_NET, "client: removed <%s>\n", client->hostname); |
3af45201 JA |
127 | |
128 | if (!flist_empty(&client->list)) | |
129 | flist_del_init(&client->list); | |
3c5f57e3 | 130 | |
bebe6398 | 131 | fio_client_remove_hash(client); |
81179eec | 132 | |
82c1ed38 JA |
133 | if (!flist_empty(&client->eta_list)) { |
134 | flist_del_init(&client->eta_list); | |
a5276616 | 135 | fio_client_dec_jobs_eta(client->eta_in_flight, client->ops->eta); |
82c1ed38 | 136 | } |
af9c9fb3 | 137 | |
1e0c184e JA |
138 | close(client->fd); |
139 | client->fd = -1; | |
140 | ||
0cf3ece0 JA |
141 | if (client->ops->removed) |
142 | client->ops->removed(client); | |
143 | ||
3c5f57e3 | 144 | nr_clients--; |
5fd0acbd | 145 | sum_stat_clients--; |
132159a5 | 146 | |
3af45201 | 147 | fio_put_client(client); |
5121a9aa JA |
148 | } |
149 | ||
343cb4a9 JA |
150 | struct fio_client *fio_get_client(struct fio_client *client) |
151 | { | |
152 | client->refs++; | |
153 | return client; | |
154 | } | |
155 | ||
fa2ea806 JA |
156 | static void __fio_client_add_cmd_option(struct fio_client *client, |
157 | const char *opt) | |
81179eec | 158 | { |
39e8e016 JA |
159 | int index; |
160 | ||
161 | index = client->argc++; | |
81179eec | 162 | client->argv = realloc(client->argv, sizeof(char *) * client->argc); |
39e8e016 JA |
163 | client->argv[index] = strdup(opt); |
164 | dprint(FD_NET, "client: add cmd %d: %s\n", index, opt); | |
81179eec JA |
165 | } |
166 | ||
fa2ea806 | 167 | void fio_client_add_cmd_option(void *cookie, const char *opt) |
81179eec | 168 | { |
bebe6398 | 169 | struct fio_client *client = cookie; |
3f3a4542 | 170 | struct flist_head *entry; |
81179eec | 171 | |
bebe6398 | 172 | if (!client || !opt) |
fa2ea806 | 173 | return; |
81179eec | 174 | |
fa2ea806 | 175 | __fio_client_add_cmd_option(client, opt); |
3f3a4542 JA |
176 | |
177 | /* | |
178 | * Duplicate arguments to shared client group | |
179 | */ | |
180 | flist_for_each(entry, &arg_list) { | |
181 | client = flist_entry(entry, struct fio_client, arg_list); | |
182 | ||
183 | __fio_client_add_cmd_option(client, opt); | |
184 | } | |
81179eec JA |
185 | } |
186 | ||
a5276616 JA |
187 | struct fio_client *fio_client_add_explicit(struct client_ops *ops, |
188 | const char *hostname, int type, | |
3ec62ec4 JA |
189 | int port) |
190 | { | |
191 | struct fio_client *client; | |
192 | ||
193 | client = malloc(sizeof(*client)); | |
194 | memset(client, 0, sizeof(*client)); | |
195 | ||
196 | INIT_FLIST_HEAD(&client->list); | |
197 | INIT_FLIST_HEAD(&client->hash_list); | |
198 | INIT_FLIST_HEAD(&client->arg_list); | |
199 | INIT_FLIST_HEAD(&client->eta_list); | |
200 | INIT_FLIST_HEAD(&client->cmd_list); | |
201 | ||
202 | client->hostname = strdup(hostname); | |
203 | ||
204 | if (type == Fio_client_socket) | |
205 | client->is_sock = 1; | |
206 | else { | |
207 | int ipv6; | |
208 | ||
209 | ipv6 = type == Fio_client_ipv6; | |
210 | if (fio_server_parse_host(hostname, &ipv6, | |
211 | &client->addr.sin_addr, | |
212 | &client->addr6.sin6_addr)) | |
213 | goto err; | |
214 | ||
215 | client->port = port; | |
216 | } | |
217 | ||
218 | client->fd = -1; | |
a5276616 | 219 | client->ops = ops; |
5121a9aa | 220 | client->refs = 1; |
46bcd498 | 221 | client->type = ops->client_type; |
3ec62ec4 JA |
222 | |
223 | __fio_client_add_cmd_option(client, "fio"); | |
224 | ||
225 | flist_add(&client->list, &client_list); | |
226 | nr_clients++; | |
227 | dprint(FD_NET, "client: added <%s>\n", client->hostname); | |
228 | return client; | |
229 | err: | |
230 | free(client); | |
231 | return NULL; | |
232 | } | |
233 | ||
14ea90ed JA |
234 | void fio_client_add_ini_file(void *cookie, const char *ini_file) |
235 | { | |
236 | struct fio_client *client = cookie; | |
237 | size_t new_size; | |
238 | ||
239 | dprint(FD_NET, "client <%s>: add ini %s\n", client->hostname, ini_file); | |
240 | ||
241 | new_size = (client->nr_ini_file + 1) * sizeof(char *); | |
242 | client->ini_file = realloc(client->ini_file, new_size); | |
243 | client->ini_file[client->nr_ini_file] = strdup(ini_file); | |
244 | client->nr_ini_file++; | |
245 | } | |
246 | ||
a5276616 | 247 | int fio_client_add(struct client_ops *ops, const char *hostname, void **cookie) |
132159a5 | 248 | { |
3f3a4542 | 249 | struct fio_client *existing = *cookie; |
b66570dc | 250 | struct fio_client *client; |
132159a5 | 251 | |
3f3a4542 JA |
252 | if (existing) { |
253 | /* | |
254 | * We always add our "exec" name as the option, hence 1 | |
255 | * means empty. | |
256 | */ | |
257 | if (existing->argc == 1) | |
258 | flist_add_tail(&existing->arg_list, &arg_list); | |
259 | else { | |
260 | while (!flist_empty(&arg_list)) | |
261 | flist_del_init(arg_list.next); | |
262 | } | |
263 | } | |
264 | ||
b66570dc | 265 | client = malloc(sizeof(*client)); |
a37f69b7 | 266 | memset(client, 0, sizeof(*client)); |
81179eec | 267 | |
3c5f57e3 | 268 | INIT_FLIST_HEAD(&client->list); |
bebe6398 | 269 | INIT_FLIST_HEAD(&client->hash_list); |
3f3a4542 | 270 | INIT_FLIST_HEAD(&client->arg_list); |
82c1ed38 | 271 | INIT_FLIST_HEAD(&client->eta_list); |
89c1707c | 272 | INIT_FLIST_HEAD(&client->cmd_list); |
3c5f57e3 | 273 | |
bebe6398 JA |
274 | if (fio_server_parse_string(hostname, &client->hostname, |
275 | &client->is_sock, &client->port, | |
811826be JA |
276 | &client->addr.sin_addr, |
277 | &client->addr6.sin6_addr, | |
278 | &client->ipv6)) | |
bebe6398 | 279 | return -1; |
87aa8f19 | 280 | |
bebe6398 | 281 | client->fd = -1; |
a5276616 | 282 | client->ops = ops; |
5121a9aa | 283 | client->refs = 1; |
46bcd498 | 284 | client->type = ops->client_type; |
3c5f57e3 | 285 | |
81179eec JA |
286 | __fio_client_add_cmd_option(client, "fio"); |
287 | ||
a37f69b7 JA |
288 | flist_add(&client->list, &client_list); |
289 | nr_clients++; | |
bebe6398 JA |
290 | dprint(FD_NET, "client: added <%s>\n", client->hostname); |
291 | *cookie = client; | |
292 | return 0; | |
a37f69b7 JA |
293 | } |
294 | ||
d824c3b8 JA |
295 | static void probe_client(struct fio_client *client) |
296 | { | |
297 | dprint(FD_NET, "client: send probe\n"); | |
298 | ||
299 | fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_PROBE, 0, &client->cmd_list); | |
300 | } | |
301 | ||
87aa8f19 | 302 | static int fio_client_connect_ip(struct fio_client *client) |
a37f69b7 | 303 | { |
811826be JA |
304 | struct sockaddr *addr; |
305 | fio_socklen_t socklen; | |
306 | int fd, domain; | |
307 | ||
308 | if (client->ipv6) { | |
309 | client->addr6.sin6_family = AF_INET6; | |
310 | client->addr6.sin6_port = htons(client->port); | |
311 | domain = AF_INET6; | |
312 | addr = (struct sockaddr *) &client->addr6; | |
313 | socklen = sizeof(client->addr6); | |
314 | } else { | |
315 | client->addr.sin_family = AF_INET; | |
316 | client->addr.sin_port = htons(client->port); | |
317 | domain = AF_INET; | |
318 | addr = (struct sockaddr *) &client->addr; | |
319 | socklen = sizeof(client->addr); | |
320 | } | |
132159a5 | 321 | |
811826be | 322 | fd = socket(domain, SOCK_STREAM, 0); |
132159a5 | 323 | if (fd < 0) { |
25095e1b JA |
324 | int ret = -errno; |
325 | ||
132159a5 | 326 | log_err("fio: socket: %s\n", strerror(errno)); |
25095e1b | 327 | return ret; |
132159a5 JA |
328 | } |
329 | ||
811826be | 330 | if (connect(fd, addr, socklen) < 0) { |
25095e1b JA |
331 | int ret = -errno; |
332 | ||
132159a5 | 333 | log_err("fio: connect: %s\n", strerror(errno)); |
a7de0a11 JA |
334 | log_err("fio: failed to connect to %s:%u\n", client->hostname, |
335 | client->port); | |
b94cba47 | 336 | close(fd); |
25095e1b | 337 | return ret; |
87aa8f19 JA |
338 | } |
339 | ||
340 | return fd; | |
341 | } | |
342 | ||
343 | static int fio_client_connect_sock(struct fio_client *client) | |
344 | { | |
345 | struct sockaddr_un *addr = &client->addr_un; | |
346 | fio_socklen_t len; | |
347 | int fd; | |
348 | ||
349 | memset(addr, 0, sizeof(*addr)); | |
350 | addr->sun_family = AF_UNIX; | |
351 | strcpy(addr->sun_path, client->hostname); | |
352 | ||
353 | fd = socket(AF_UNIX, SOCK_STREAM, 0); | |
354 | if (fd < 0) { | |
25095e1b JA |
355 | int ret = -errno; |
356 | ||
87aa8f19 | 357 | log_err("fio: socket: %s\n", strerror(errno)); |
25095e1b | 358 | return ret; |
87aa8f19 JA |
359 | } |
360 | ||
361 | len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1; | |
362 | if (connect(fd, (struct sockaddr *) addr, len) < 0) { | |
25095e1b JA |
363 | int ret = -errno; |
364 | ||
87aa8f19 | 365 | log_err("fio: connect; %s\n", strerror(errno)); |
b94cba47 | 366 | close(fd); |
25095e1b | 367 | return ret; |
132159a5 JA |
368 | } |
369 | ||
87aa8f19 JA |
370 | return fd; |
371 | } | |
372 | ||
2f99deb0 | 373 | int fio_client_connect(struct fio_client *client) |
87aa8f19 JA |
374 | { |
375 | int fd; | |
376 | ||
377 | dprint(FD_NET, "client: connect to host %s\n", client->hostname); | |
378 | ||
87aa8f19 JA |
379 | if (client->is_sock) |
380 | fd = fio_client_connect_sock(client); | |
381 | else | |
382 | fd = fio_client_connect_ip(client); | |
383 | ||
89c1707c JA |
384 | dprint(FD_NET, "client: %s connected %d\n", client->hostname, fd); |
385 | ||
87aa8f19 | 386 | if (fd < 0) |
a1a3ed51 | 387 | return fd; |
87aa8f19 | 388 | |
b66570dc | 389 | client->fd = fd; |
bebe6398 | 390 | fio_client_add_hash(client); |
81179eec | 391 | client->state = Client_connected; |
d824c3b8 JA |
392 | |
393 | probe_client(client); | |
132159a5 JA |
394 | return 0; |
395 | } | |
396 | ||
0cf3ece0 | 397 | int fio_client_terminate(struct fio_client *client) |
2f99deb0 | 398 | { |
0cf3ece0 | 399 | return fio_net_send_quit(client->fd); |
2f99deb0 JA |
400 | } |
401 | ||
cc0df00a JA |
402 | void fio_clients_terminate(void) |
403 | { | |
404 | struct flist_head *entry; | |
405 | struct fio_client *client; | |
406 | ||
60efd14e JA |
407 | dprint(FD_NET, "client: terminate clients\n"); |
408 | ||
cc0df00a JA |
409 | flist_for_each(entry, &client_list) { |
410 | client = flist_entry(entry, struct fio_client, list); | |
2f99deb0 | 411 | fio_client_terminate(client); |
cc0df00a JA |
412 | } |
413 | } | |
414 | ||
415 | static void sig_int(int sig) | |
416 | { | |
bebe6398 | 417 | dprint(FD_NET, "client: got signal %d\n", sig); |
cc0df00a JA |
418 | fio_clients_terminate(); |
419 | } | |
420 | ||
b852e7cf JA |
421 | static void sig_show_status(int sig) |
422 | { | |
423 | show_running_run_stats(); | |
424 | } | |
425 | ||
cc0df00a JA |
426 | static void client_signal_handler(void) |
427 | { | |
428 | struct sigaction act; | |
429 | ||
430 | memset(&act, 0, sizeof(act)); | |
431 | act.sa_handler = sig_int; | |
432 | act.sa_flags = SA_RESTART; | |
433 | sigaction(SIGINT, &act, NULL); | |
434 | ||
435 | memset(&act, 0, sizeof(act)); | |
436 | act.sa_handler = sig_int; | |
437 | act.sa_flags = SA_RESTART; | |
438 | sigaction(SIGTERM, &act, NULL); | |
b852e7cf | 439 | |
2f694507 BC |
440 | /* Windows uses SIGBREAK as a quit signal from other applications */ |
441 | #ifdef WIN32 | |
442 | memset(&act, 0, sizeof(act)); | |
443 | act.sa_handler = sig_int; | |
444 | act.sa_flags = SA_RESTART; | |
445 | sigaction(SIGBREAK, &act, NULL); | |
446 | #endif | |
447 | ||
b852e7cf JA |
448 | memset(&act, 0, sizeof(act)); |
449 | act.sa_handler = sig_show_status; | |
450 | act.sa_flags = SA_RESTART; | |
451 | sigaction(SIGUSR1, &act, NULL); | |
cc0df00a JA |
452 | } |
453 | ||
81179eec JA |
454 | static int send_client_cmd_line(struct fio_client *client) |
455 | { | |
fa2ea806 JA |
456 | struct cmd_single_line_pdu *cslp; |
457 | struct cmd_line_pdu *clp; | |
458 | unsigned long offset; | |
7f868316 | 459 | unsigned int *lens; |
fa2ea806 JA |
460 | void *pdu; |
461 | size_t mem; | |
81179eec JA |
462 | int i, ret; |
463 | ||
39e8e016 | 464 | dprint(FD_NET, "client: send cmdline %d\n", client->argc); |
60efd14e | 465 | |
7f868316 JA |
466 | lens = malloc(client->argc * sizeof(unsigned int)); |
467 | ||
fa2ea806 JA |
468 | /* |
469 | * Find out how much mem we need | |
470 | */ | |
7f868316 JA |
471 | for (i = 0, mem = 0; i < client->argc; i++) { |
472 | lens[i] = strlen(client->argv[i]) + 1; | |
473 | mem += lens[i]; | |
474 | } | |
fa2ea806 JA |
475 | |
476 | /* | |
477 | * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu | |
478 | */ | |
479 | mem += sizeof(*clp) + (client->argc * sizeof(*cslp)); | |
480 | ||
481 | pdu = malloc(mem); | |
482 | clp = pdu; | |
483 | offset = sizeof(*clp); | |
484 | ||
485 | for (i = 0; i < client->argc; i++) { | |
7f868316 | 486 | uint16_t arg_len = lens[i]; |
fa2ea806 JA |
487 | |
488 | cslp = pdu + offset; | |
489 | strcpy((char *) cslp->text, client->argv[i]); | |
490 | cslp->len = cpu_to_le16(arg_len); | |
491 | offset += sizeof(*cslp) + arg_len; | |
492 | } | |
81179eec | 493 | |
7f868316 | 494 | free(lens); |
fa2ea806 | 495 | clp->lines = cpu_to_le16(client->argc); |
46bcd498 | 496 | clp->client_type = __cpu_to_le16(client->type); |
40c60516 | 497 | ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, NULL, NULL); |
81179eec JA |
498 | free(pdu); |
499 | return ret; | |
500 | } | |
501 | ||
a37f69b7 JA |
502 | int fio_clients_connect(void) |
503 | { | |
504 | struct fio_client *client; | |
505 | struct flist_head *entry, *tmp; | |
506 | int ret; | |
507 | ||
93bcfd20 BC |
508 | #ifdef WIN32 |
509 | WSADATA wsd; | |
3c3ed070 | 510 | WSAStartup(MAKEWORD(2, 2), &wsd); |
93bcfd20 BC |
511 | #endif |
512 | ||
60efd14e JA |
513 | dprint(FD_NET, "client: connect all\n"); |
514 | ||
cc0df00a JA |
515 | client_signal_handler(); |
516 | ||
a37f69b7 JA |
517 | flist_for_each_safe(entry, tmp, &client_list) { |
518 | client = flist_entry(entry, struct fio_client, list); | |
519 | ||
520 | ret = fio_client_connect(client); | |
0b8f30a5 | 521 | if (ret) { |
a37f69b7 | 522 | remove_client(client); |
0b8f30a5 JA |
523 | continue; |
524 | } | |
525 | ||
81179eec JA |
526 | if (client->argc > 1) |
527 | send_client_cmd_line(client); | |
a37f69b7 JA |
528 | } |
529 | ||
530 | return !nr_clients; | |
531 | } | |
532 | ||
b9d2f30a JA |
533 | int fio_start_client(struct fio_client *client) |
534 | { | |
535 | dprint(FD_NET, "client: start %s\n", client->hostname); | |
536 | return fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_RUN, 0, NULL); | |
537 | } | |
538 | ||
539 | int fio_start_all_clients(void) | |
540 | { | |
541 | struct fio_client *client; | |
542 | struct flist_head *entry, *tmp; | |
543 | int ret; | |
544 | ||
545 | dprint(FD_NET, "client: start all\n"); | |
546 | ||
547 | flist_for_each_safe(entry, tmp, &client_list) { | |
548 | client = flist_entry(entry, struct fio_client, list); | |
549 | ||
550 | ret = fio_start_client(client); | |
551 | if (ret) { | |
552 | remove_client(client); | |
553 | continue; | |
554 | } | |
555 | } | |
556 | ||
557 | return flist_empty(&client_list); | |
558 | } | |
559 | ||
132159a5 JA |
560 | /* |
561 | * Send file contents to server backend. We could use sendfile(), but to remain | |
562 | * more portable lets just read/write the darn thing. | |
563 | */ | |
9988ca70 | 564 | static int __fio_client_send_ini(struct fio_client *client, const char *filename) |
132159a5 | 565 | { |
46bcd498 JA |
566 | struct cmd_job_pdu *pdu; |
567 | size_t p_size; | |
132159a5 | 568 | struct stat sb; |
46bcd498 JA |
569 | char *p; |
570 | void *buf; | |
132159a5 JA |
571 | off_t len; |
572 | int fd, ret; | |
573 | ||
46c48f1f JA |
574 | dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname); |
575 | ||
132159a5 JA |
576 | fd = open(filename, O_RDONLY); |
577 | if (fd < 0) { | |
25095e1b JA |
578 | int ret = -errno; |
579 | ||
e951bdc4 | 580 | log_err("fio: job file <%s> open: %s\n", filename, strerror(errno)); |
25095e1b | 581 | return ret; |
132159a5 JA |
582 | } |
583 | ||
584 | if (fstat(fd, &sb) < 0) { | |
25095e1b JA |
585 | int ret = -errno; |
586 | ||
132159a5 | 587 | log_err("fio: job file stat: %s\n", strerror(errno)); |
b94cba47 | 588 | close(fd); |
25095e1b | 589 | return ret; |
132159a5 JA |
590 | } |
591 | ||
46bcd498 JA |
592 | p_size = sb.st_size + sizeof(*pdu); |
593 | pdu = malloc(p_size); | |
594 | buf = pdu->buf; | |
132159a5 JA |
595 | |
596 | len = sb.st_size; | |
597 | p = buf; | |
598 | do { | |
599 | ret = read(fd, p, len); | |
600 | if (ret > 0) { | |
601 | len -= ret; | |
602 | if (!len) | |
603 | break; | |
604 | p += ret; | |
605 | continue; | |
606 | } else if (!ret) | |
607 | break; | |
608 | else if (errno == EAGAIN || errno == EINTR) | |
609 | continue; | |
610 | } while (1); | |
611 | ||
0b8f30a5 JA |
612 | if (len) { |
613 | log_err("fio: failed reading job file %s\n", filename); | |
b94cba47 | 614 | close(fd); |
c524ef72 | 615 | free(buf); |
0b8f30a5 JA |
616 | return 1; |
617 | } | |
618 | ||
46bcd498 JA |
619 | pdu->buf_len = __cpu_to_le32(sb.st_size); |
620 | pdu->client_type = cpu_to_le32(client->type); | |
621 | ||
c2cb6869 | 622 | client->sent_job = 1; |
40c60516 | 623 | ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, pdu, p_size, NULL, NULL); |
46bcd498 | 624 | free(pdu); |
b94cba47 | 625 | close(fd); |
132159a5 JA |
626 | return ret; |
627 | } | |
37db14fe | 628 | |
9988ca70 JA |
629 | int fio_client_send_ini(struct fio_client *client, const char *filename) |
630 | { | |
25095e1b JA |
631 | int ret; |
632 | ||
633 | ret = __fio_client_send_ini(client, filename); | |
3af45201 JA |
634 | if (!ret) |
635 | client->sent_job = 1; | |
9988ca70 | 636 | |
25095e1b | 637 | return ret; |
9988ca70 JA |
638 | } |
639 | ||
a37f69b7 JA |
640 | int fio_clients_send_ini(const char *filename) |
641 | { | |
642 | struct fio_client *client; | |
643 | struct flist_head *entry, *tmp; | |
644 | ||
645 | flist_for_each_safe(entry, tmp, &client_list) { | |
646 | client = flist_entry(entry, struct fio_client, list); | |
647 | ||
14ea90ed JA |
648 | if (client->nr_ini_file) { |
649 | int i; | |
650 | ||
651 | for (i = 0; i < client->nr_ini_file; i++) { | |
652 | const char *ini = client->ini_file[i]; | |
653 | ||
654 | if (fio_client_send_ini(client, ini)) { | |
655 | remove_client(client); | |
656 | break; | |
657 | } | |
658 | } | |
659 | } else if (!filename || fio_client_send_ini(client, filename)) | |
3af45201 | 660 | remove_client(client); |
a37f69b7 JA |
661 | } |
662 | ||
663 | return !nr_clients; | |
664 | } | |
665 | ||
40c60516 JA |
666 | int fio_client_update_options(struct fio_client *client, |
667 | struct thread_options *o, uint64_t *tag) | |
668 | { | |
669 | struct cmd_add_job_pdu pdu; | |
670 | ||
671 | pdu.thread_number = cpu_to_le32(client->thread_number); | |
672 | pdu.groupid = cpu_to_le32(client->groupid); | |
673 | convert_thread_options_to_net(&pdu.top, o); | |
674 | ||
675 | return fio_net_send_cmd(client->fd, FIO_NET_CMD_UPDATE_JOB, &pdu, sizeof(pdu), tag, &client->cmd_list); | |
676 | } | |
677 | ||
a64e88da JA |
678 | static void convert_io_stat(struct io_stat *dst, struct io_stat *src) |
679 | { | |
680 | dst->max_val = le64_to_cpu(src->max_val); | |
681 | dst->min_val = le64_to_cpu(src->min_val); | |
682 | dst->samples = le64_to_cpu(src->samples); | |
802ad4a8 JA |
683 | |
684 | /* | |
685 | * Floats arrive as IEEE 754 encoded uint64_t, convert back to double | |
686 | */ | |
687 | dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i)); | |
688 | dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i)); | |
a64e88da JA |
689 | } |
690 | ||
691 | static void convert_ts(struct thread_stat *dst, struct thread_stat *src) | |
692 | { | |
693 | int i, j; | |
694 | ||
2f122b13 JA |
695 | dst->error = le32_to_cpu(src->error); |
696 | dst->thread_number = le32_to_cpu(src->thread_number); | |
697 | dst->groupid = le32_to_cpu(src->groupid); | |
698 | dst->pid = le32_to_cpu(src->pid); | |
699 | dst->members = le32_to_cpu(src->members); | |
a64e88da | 700 | |
298921d6 | 701 | for (i = 0; i < DDIR_RWDIR_CNT; i++) { |
a64e88da JA |
702 | convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]); |
703 | convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]); | |
704 | convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]); | |
705 | convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]); | |
706 | } | |
707 | ||
708 | dst->usr_time = le64_to_cpu(src->usr_time); | |
709 | dst->sys_time = le64_to_cpu(src->sys_time); | |
710 | dst->ctx = le64_to_cpu(src->ctx); | |
711 | dst->minf = le64_to_cpu(src->minf); | |
712 | dst->majf = le64_to_cpu(src->majf); | |
713 | dst->clat_percentiles = le64_to_cpu(src->clat_percentiles); | |
802ad4a8 JA |
714 | |
715 | for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) { | |
716 | fio_fp64_t *fps = &src->percentile_list[i]; | |
717 | fio_fp64_t *fpd = &dst->percentile_list[i]; | |
718 | ||
719 | fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i)); | |
720 | } | |
a64e88da JA |
721 | |
722 | for (i = 0; i < FIO_IO_U_MAP_NR; i++) { | |
723 | dst->io_u_map[i] = le32_to_cpu(src->io_u_map[i]); | |
724 | dst->io_u_submit[i] = le32_to_cpu(src->io_u_submit[i]); | |
725 | dst->io_u_complete[i] = le32_to_cpu(src->io_u_complete[i]); | |
726 | } | |
727 | ||
728 | for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) { | |
729 | dst->io_u_lat_u[i] = le32_to_cpu(src->io_u_lat_u[i]); | |
730 | dst->io_u_lat_m[i] = le32_to_cpu(src->io_u_lat_m[i]); | |
731 | } | |
732 | ||
298921d6 | 733 | for (i = 0; i < DDIR_RWDIR_CNT; i++) |
a64e88da JA |
734 | for (j = 0; j < FIO_IO_U_PLAT_NR; j++) |
735 | dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]); | |
736 | ||
737 | for (i = 0; i < 3; i++) { | |
738 | dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]); | |
93eee04a | 739 | dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]); |
a64e88da JA |
740 | } |
741 | ||
742 | dst->total_submit = le64_to_cpu(src->total_submit); | |
743 | dst->total_complete = le64_to_cpu(src->total_complete); | |
744 | ||
298921d6 | 745 | for (i = 0; i < DDIR_RWDIR_CNT; i++) { |
a64e88da JA |
746 | dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]); |
747 | dst->runtime[i] = le64_to_cpu(src->runtime[i]); | |
748 | } | |
749 | ||
750 | dst->total_run_time = le64_to_cpu(src->total_run_time); | |
751 | dst->continue_on_error = le16_to_cpu(src->continue_on_error); | |
752 | dst->total_err_count = le64_to_cpu(src->total_err_count); | |
ddcc0b69 JA |
753 | dst->first_error = le32_to_cpu(src->first_error); |
754 | dst->kb_base = le32_to_cpu(src->kb_base); | |
a64e88da JA |
755 | } |
756 | ||
757 | static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src) | |
758 | { | |
759 | int i; | |
760 | ||
298921d6 | 761 | for (i = 0; i < DDIR_RWDIR_CNT; i++) { |
a64e88da JA |
762 | dst->max_run[i] = le64_to_cpu(src->max_run[i]); |
763 | dst->min_run[i] = le64_to_cpu(src->min_run[i]); | |
764 | dst->max_bw[i] = le64_to_cpu(src->max_bw[i]); | |
765 | dst->min_bw[i] = le64_to_cpu(src->min_bw[i]); | |
766 | dst->io_kb[i] = le64_to_cpu(src->io_kb[i]); | |
767 | dst->agg[i] = le64_to_cpu(src->agg[i]); | |
768 | } | |
769 | ||
770 | dst->kb_base = le32_to_cpu(src->kb_base); | |
771 | dst->groupid = le32_to_cpu(src->groupid); | |
772 | } | |
773 | ||
89e5fad9 | 774 | static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd) |
a64e88da JA |
775 | { |
776 | struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload; | |
777 | ||
a64e88da | 778 | show_thread_status(&p->ts, &p->rs); |
37f0c1ae JA |
779 | |
780 | if (sum_stat_clients == 1) | |
781 | return; | |
782 | ||
783 | sum_thread_stats(&client_ts, &p->ts, sum_stat_nr); | |
784 | sum_group_stats(&client_gs, &p->rs); | |
785 | ||
786 | client_ts.members++; | |
2f122b13 | 787 | client_ts.thread_number = p->ts.thread_number; |
37f0c1ae JA |
788 | client_ts.groupid = p->ts.groupid; |
789 | ||
790 | if (++sum_stat_nr == sum_stat_clients) { | |
791 | strcpy(client_ts.name, "All clients"); | |
792 | show_thread_status(&client_ts, &client_gs); | |
793 | } | |
a64e88da JA |
794 | } |
795 | ||
89e5fad9 | 796 | static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd) |
a64e88da JA |
797 | { |
798 | struct group_run_stats *gs = (struct group_run_stats *) cmd->payload; | |
799 | ||
a64e88da JA |
800 | show_group_stats(gs); |
801 | } | |
802 | ||
3bf236c0 JA |
803 | static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd) |
804 | { | |
805 | struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload; | |
806 | const char *buf = (const char *) pdu->buf; | |
807 | const char *name; | |
808 | int fio_unused ret; | |
809 | ||
810 | name = client->name ? client->name : client->hostname; | |
811 | ||
812 | if (!client->skip_newline) | |
813 | fprintf(f_out, "<%s> ", name); | |
814 | ret = fwrite(buf, pdu->buf_len, 1, f_out); | |
815 | fflush(f_out); | |
816 | client->skip_newline = strchr(buf, '\n') == NULL; | |
817 | } | |
818 | ||
d09a64a0 JA |
819 | static void convert_agg(struct disk_util_agg *agg) |
820 | { | |
821 | int i; | |
822 | ||
823 | for (i = 0; i < 2; i++) { | |
824 | agg->ios[i] = le32_to_cpu(agg->ios[i]); | |
825 | agg->merges[i] = le32_to_cpu(agg->merges[i]); | |
826 | agg->sectors[i] = le64_to_cpu(agg->sectors[i]); | |
827 | agg->ticks[i] = le32_to_cpu(agg->ticks[i]); | |
828 | } | |
829 | ||
830 | agg->io_ticks = le32_to_cpu(agg->io_ticks); | |
831 | agg->time_in_queue = le32_to_cpu(agg->time_in_queue); | |
832 | agg->slavecount = le32_to_cpu(agg->slavecount); | |
823ba54b | 833 | agg->max_util.u.f = fio_uint64_to_double(__le64_to_cpu(agg->max_util.u.i)); |
d09a64a0 JA |
834 | } |
835 | ||
836 | static void convert_dus(struct disk_util_stat *dus) | |
837 | { | |
838 | int i; | |
839 | ||
840 | for (i = 0; i < 2; i++) { | |
841 | dus->ios[i] = le32_to_cpu(dus->ios[i]); | |
842 | dus->merges[i] = le32_to_cpu(dus->merges[i]); | |
843 | dus->sectors[i] = le64_to_cpu(dus->sectors[i]); | |
844 | dus->ticks[i] = le32_to_cpu(dus->ticks[i]); | |
845 | } | |
846 | ||
847 | dus->io_ticks = le32_to_cpu(dus->io_ticks); | |
848 | dus->time_in_queue = le32_to_cpu(dus->time_in_queue); | |
849 | dus->msec = le64_to_cpu(dus->msec); | |
850 | } | |
851 | ||
852 | static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd) | |
853 | { | |
854 | struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload; | |
855 | ||
d09a64a0 JA |
856 | if (!client->disk_stats_shown) { |
857 | client->disk_stats_shown = 1; | |
858 | log_info("\nDisk stats (read/write):\n"); | |
859 | } | |
860 | ||
f3afa57e | 861 | print_disk_util(&du->dus, &du->agg, output_format == FIO_OUTPUT_TERSE); |
d09a64a0 JA |
862 | } |
863 | ||
3bf236c0 | 864 | static void convert_jobs_eta(struct jobs_eta *je) |
cf451d1e | 865 | { |
cf451d1e JA |
866 | int i; |
867 | ||
868 | je->nr_running = le32_to_cpu(je->nr_running); | |
869 | je->nr_ramp = le32_to_cpu(je->nr_ramp); | |
870 | je->nr_pending = le32_to_cpu(je->nr_pending); | |
871 | je->files_open = le32_to_cpu(je->files_open); | |
cf451d1e | 872 | |
298921d6 JA |
873 | for (i = 0; i < DDIR_RWDIR_CNT; i++) { |
874 | je->m_rate[i] = le32_to_cpu(je->m_rate[i]); | |
875 | je->t_rate[i] = le32_to_cpu(je->t_rate[i]); | |
876 | je->m_iops[i] = le32_to_cpu(je->m_iops[i]); | |
877 | je->t_iops[i] = le32_to_cpu(je->t_iops[i]); | |
cf451d1e JA |
878 | je->rate[i] = le32_to_cpu(je->rate[i]); |
879 | je->iops[i] = le32_to_cpu(je->iops[i]); | |
880 | } | |
881 | ||
b51eedb7 | 882 | je->elapsed_sec = le64_to_cpu(je->elapsed_sec); |
cf451d1e | 883 | je->eta_sec = le64_to_cpu(je->eta_sec); |
8c621fb2 | 884 | je->nr_threads = le32_to_cpu(je->nr_threads); |
b7f05eb0 | 885 | je->is_pow2 = le32_to_cpu(je->is_pow2); |
48fbb46e JA |
886 | } |
887 | ||
3e47bd25 | 888 | void fio_client_sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je) |
48fbb46e | 889 | { |
48fbb46e JA |
890 | int i; |
891 | ||
892 | dst->nr_running += je->nr_running; | |
893 | dst->nr_ramp += je->nr_ramp; | |
894 | dst->nr_pending += je->nr_pending; | |
895 | dst->files_open += je->files_open; | |
48fbb46e | 896 | |
298921d6 | 897 | for (i = 0; i < DDIR_RWDIR_CNT; i++) { |
3e47bd25 JA |
898 | dst->m_rate[i] += je->m_rate[i]; |
899 | dst->t_rate[i] += je->t_rate[i]; | |
900 | dst->m_iops[i] += je->m_iops[i]; | |
901 | dst->t_iops[i] += je->t_iops[i]; | |
48fbb46e JA |
902 | dst->rate[i] += je->rate[i]; |
903 | dst->iops[i] += je->iops[i]; | |
904 | } | |
905 | ||
906 | dst->elapsed_sec += je->elapsed_sec; | |
907 | ||
908 | if (je->eta_sec > dst->eta_sec) | |
909 | dst->eta_sec = je->eta_sec; | |
8c621fb2 JA |
910 | |
911 | dst->nr_threads += je->nr_threads; | |
912 | /* we need to handle je->run_str too ... */ | |
48fbb46e JA |
913 | } |
914 | ||
a5276616 | 915 | void fio_client_dec_jobs_eta(struct client_eta *eta, client_eta_op eta_fn) |
82c1ed38 JA |
916 | { |
917 | if (!--eta->pending) { | |
a5276616 | 918 | eta_fn(&eta->eta); |
82c1ed38 JA |
919 | free(eta); |
920 | } | |
921 | } | |
922 | ||
89c1707c JA |
923 | static void remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd) |
924 | { | |
40c60516 | 925 | struct fio_net_cmd_reply *reply = NULL; |
89c1707c JA |
926 | struct flist_head *entry; |
927 | ||
928 | flist_for_each(entry, &client->cmd_list) { | |
40c60516 | 929 | reply = flist_entry(entry, struct fio_net_cmd_reply, list); |
89c1707c | 930 | |
40c60516 | 931 | if (cmd->tag == (uintptr_t) reply) |
89c1707c JA |
932 | break; |
933 | ||
40c60516 | 934 | reply = NULL; |
89c1707c JA |
935 | } |
936 | ||
40c60516 JA |
937 | if (!reply) { |
938 | log_err("fio: client: unable to find matching tag (%lx)\n", cmd->tag); | |
89c1707c JA |
939 | return; |
940 | } | |
941 | ||
40c60516 JA |
942 | flist_del(&reply->list); |
943 | cmd->tag = reply->saved_tag; | |
944 | free(reply); | |
945 | } | |
946 | ||
947 | int fio_client_wait_for_reply(struct fio_client *client, uint64_t tag) | |
948 | { | |
949 | do { | |
950 | struct fio_net_cmd_reply *reply = NULL; | |
951 | struct flist_head *entry; | |
952 | ||
953 | flist_for_each(entry, &client->cmd_list) { | |
954 | reply = flist_entry(entry, struct fio_net_cmd_reply, list); | |
955 | ||
956 | if (tag == (uintptr_t) reply) | |
957 | break; | |
958 | ||
959 | reply = NULL; | |
960 | } | |
961 | ||
962 | if (!reply) | |
963 | break; | |
964 | ||
965 | usleep(1000); | |
966 | } while (1); | |
967 | ||
968 | return 0; | |
89c1707c JA |
969 | } |
970 | ||
82c1ed38 | 971 | static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd) |
48fbb46e JA |
972 | { |
973 | struct jobs_eta *je = (struct jobs_eta *) cmd->payload; | |
df380934 | 974 | struct client_eta *eta = (struct client_eta *) (uintptr_t) cmd->tag; |
af9c9fb3 JA |
975 | |
976 | dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending); | |
cf451d1e | 977 | |
f77d2676 JA |
978 | assert(client->eta_in_flight == eta); |
979 | ||
980 | client->eta_in_flight = NULL; | |
82c1ed38 JA |
981 | flist_del_init(&client->eta_list); |
982 | ||
2f99deb0 JA |
983 | if (client->ops->jobs_eta) |
984 | client->ops->jobs_eta(client, je); | |
985 | ||
3e47bd25 | 986 | fio_client_sum_jobs_eta(&eta->eta, je); |
a5276616 | 987 | fio_client_dec_jobs_eta(eta, client->ops->eta); |
cf451d1e JA |
988 | } |
989 | ||
b5296ddb | 990 | static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd) |
2e03b4b2 JA |
991 | { |
992 | struct cmd_probe_pdu *probe = (struct cmd_probe_pdu *) cmd->payload; | |
d2333358 JA |
993 | const char *os, *arch; |
994 | char bit[16]; | |
2e03b4b2 | 995 | |
cca84643 JA |
996 | os = fio_get_os_string(probe->os); |
997 | if (!os) | |
998 | os = "unknown"; | |
999 | ||
1000 | arch = fio_get_arch_string(probe->arch); | |
1001 | if (!arch) | |
1002 | os = "unknown"; | |
1003 | ||
d2333358 | 1004 | sprintf(bit, "%d-bit", probe->bpp * 8); |
38fdef22 | 1005 | |
8029542b | 1006 | log_info("hostname=%s, be=%u, %s, os=%s, arch=%s, fio=%s\n", |
38fdef22 | 1007 | probe->hostname, probe->bigendian, bit, os, arch, |
8029542b | 1008 | probe->fio_version); |
b5296ddb JA |
1009 | |
1010 | if (!client->name) | |
1011 | client->name = strdup((char *) probe->hostname); | |
2e03b4b2 JA |
1012 | } |
1013 | ||
11e950bd JA |
1014 | static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd) |
1015 | { | |
1016 | struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload; | |
1017 | ||
1018 | client->state = Client_started; | |
85dd01e7 | 1019 | client->jobs = pdu->jobs; |
11e950bd JA |
1020 | } |
1021 | ||
1022 | static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd) | |
1023 | { | |
498c92c2 JA |
1024 | if (client->error) |
1025 | log_info("client <%s>: exited with error %d\n", client->hostname, client->error); | |
11e950bd JA |
1026 | } |
1027 | ||
6b79c80c JA |
1028 | static void convert_stop(struct fio_net_cmd *cmd) |
1029 | { | |
1030 | struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload; | |
1031 | ||
1032 | pdu->error = le32_to_cpu(pdu->error); | |
1033 | } | |
1034 | ||
084d1c6f JA |
1035 | static void convert_text(struct fio_net_cmd *cmd) |
1036 | { | |
1037 | struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload; | |
1038 | ||
1039 | pdu->level = le32_to_cpu(pdu->level); | |
1040 | pdu->buf_len = le32_to_cpu(pdu->buf_len); | |
1041 | pdu->log_sec = le64_to_cpu(pdu->log_sec); | |
1042 | pdu->log_usec = le64_to_cpu(pdu->log_usec); | |
1043 | } | |
1044 | ||
1b42725f JA |
1045 | /* |
1046 | * This has been compressed on the server side, since it can be big. | |
1047 | * Uncompress here. | |
1048 | */ | |
1049 | static struct cmd_iolog_pdu *convert_iolog(struct fio_net_cmd *cmd) | |
1050 | { | |
1051 | struct cmd_iolog_pdu *pdu = (struct cmd_iolog_pdu *) cmd->payload; | |
1052 | struct cmd_iolog_pdu *ret; | |
1053 | uint32_t nr_samples; | |
1054 | unsigned long total; | |
1055 | z_stream stream; | |
1056 | void *p; | |
f5ed765a | 1057 | int i; |
1b42725f JA |
1058 | |
1059 | stream.zalloc = Z_NULL; | |
1060 | stream.zfree = Z_NULL; | |
1061 | stream.opaque = Z_NULL; | |
1062 | stream.avail_in = 0; | |
1063 | stream.next_in = Z_NULL; | |
1064 | ||
1065 | if (inflateInit(&stream) != Z_OK) | |
1066 | return NULL; | |
1067 | ||
1068 | /* | |
f5ed765a | 1069 | * Get header first, it's not compressed |
1b42725f JA |
1070 | */ |
1071 | nr_samples = le32_to_cpu(pdu->nr_samples); | |
1072 | ||
f5ed765a JA |
1073 | total = nr_samples * sizeof(struct io_sample); |
1074 | ret = malloc(total + sizeof(*pdu)); | |
2f122b13 | 1075 | ret->thread_number = le32_to_cpu(pdu->thread_number); |
1b42725f | 1076 | ret->nr_samples = nr_samples; |
f5ed765a JA |
1077 | ret->log_type = le32_to_cpu(pdu->log_type); |
1078 | strcpy((char *) ret->name, (char *) pdu->name); | |
1079 | ||
1080 | p = (void *) ret + sizeof(*pdu); | |
1b42725f | 1081 | |
f5ed765a JA |
1082 | stream.avail_in = cmd->pdu_len - sizeof(*pdu); |
1083 | stream.next_in = (void *) pdu + sizeof(*pdu); | |
1b42725f JA |
1084 | while (stream.avail_in) { |
1085 | unsigned int this_chunk = 65536; | |
1086 | unsigned int this_len; | |
1087 | int err; | |
1088 | ||
1089 | if (this_chunk > total) | |
1090 | this_chunk = total; | |
1091 | ||
1092 | stream.avail_out = this_chunk; | |
1093 | stream.next_out = p; | |
1094 | err = inflate(&stream, Z_NO_FLUSH); | |
3c547fe0 JA |
1095 | /* may be Z_OK, or Z_STREAM_END */ |
1096 | if (err < 0) { | |
1b42725f | 1097 | log_err("fio: inflate error %d\n", err); |
f5ed765a JA |
1098 | free(ret); |
1099 | ret = NULL; | |
1b42725f JA |
1100 | goto out; |
1101 | } | |
1102 | ||
1103 | this_len = this_chunk - stream.avail_out; | |
1104 | p += this_len; | |
1105 | total -= this_len; | |
1106 | } | |
1107 | ||
f5ed765a JA |
1108 | for (i = 0; i < ret->nr_samples; i++) { |
1109 | struct io_sample *s = &ret->samples[i]; | |
1110 | ||
1111 | s->time = le64_to_cpu(s->time); | |
1112 | s->val = le64_to_cpu(s->val); | |
1113 | s->ddir = le32_to_cpu(s->ddir); | |
1114 | s->bs = le32_to_cpu(s->bs); | |
1115 | } | |
1116 | ||
1b42725f JA |
1117 | out: |
1118 | inflateEnd(&stream); | |
1119 | return ret; | |
1120 | } | |
1121 | ||
a5276616 | 1122 | int fio_handle_client(struct fio_client *client) |
37db14fe | 1123 | { |
a5276616 | 1124 | struct client_ops *ops = client->ops; |
37db14fe JA |
1125 | struct fio_net_cmd *cmd; |
1126 | ||
60efd14e JA |
1127 | dprint(FD_NET, "client: handle %s\n", client->hostname); |
1128 | ||
e951bdc4 JA |
1129 | cmd = fio_net_recv_cmd(client->fd); |
1130 | if (!cmd) | |
1131 | return 0; | |
c2c94585 | 1132 | |
b9d2f30a JA |
1133 | dprint(FD_NET, "client: got cmd op %s from %s (pdu=%u)\n", |
1134 | fio_server_op(cmd->opcode), client->hostname, cmd->pdu_len); | |
46c48f1f | 1135 | |
e951bdc4 JA |
1136 | switch (cmd->opcode) { |
1137 | case FIO_NET_CMD_QUIT: | |
3ec62ec4 | 1138 | if (ops->quit) |
35c0ba7f | 1139 | ops->quit(client, cmd); |
e951bdc4 JA |
1140 | remove_client(client); |
1141 | free(cmd); | |
1142 | break; | |
084d1c6f JA |
1143 | case FIO_NET_CMD_TEXT: |
1144 | convert_text(cmd); | |
35c0ba7f | 1145 | ops->text(client, cmd); |
e951bdc4 JA |
1146 | free(cmd); |
1147 | break; | |
3bf236c0 JA |
1148 | case FIO_NET_CMD_DU: { |
1149 | struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload; | |
1150 | ||
1151 | convert_dus(&du->dus); | |
1152 | convert_agg(&du->agg); | |
1153 | ||
dd366728 | 1154 | ops->disk_util(client, cmd); |
d09a64a0 JA |
1155 | free(cmd); |
1156 | break; | |
3bf236c0 JA |
1157 | } |
1158 | case FIO_NET_CMD_TS: { | |
1159 | struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload; | |
1160 | ||
1161 | convert_ts(&p->ts, &p->ts); | |
1162 | convert_gs(&p->rs, &p->rs); | |
1163 | ||
89e5fad9 | 1164 | ops->thread_status(client, cmd); |
e951bdc4 JA |
1165 | free(cmd); |
1166 | break; | |
3bf236c0 JA |
1167 | } |
1168 | case FIO_NET_CMD_GS: { | |
1169 | struct group_run_stats *gs = (struct group_run_stats *) cmd->payload; | |
1170 | ||
1171 | convert_gs(gs, gs); | |
1172 | ||
89e5fad9 | 1173 | ops->group_stats(client, cmd); |
e951bdc4 JA |
1174 | free(cmd); |
1175 | break; | |
3bf236c0 JA |
1176 | } |
1177 | case FIO_NET_CMD_ETA: { | |
1178 | struct jobs_eta *je = (struct jobs_eta *) cmd->payload; | |
1179 | ||
89c1707c | 1180 | remove_reply_cmd(client, cmd); |
3bf236c0 | 1181 | convert_jobs_eta(je); |
a5276616 | 1182 | handle_eta(client, cmd); |
e951bdc4 JA |
1183 | free(cmd); |
1184 | break; | |
3bf236c0 | 1185 | } |
e951bdc4 | 1186 | case FIO_NET_CMD_PROBE: |
89c1707c | 1187 | remove_reply_cmd(client, cmd); |
dd366728 | 1188 | ops->probe(client, cmd); |
e951bdc4 JA |
1189 | free(cmd); |
1190 | break; | |
5d7793aa | 1191 | case FIO_NET_CMD_SERVER_START: |
01be038e | 1192 | client->state = Client_running; |
85dd01e7 JA |
1193 | if (ops->job_start) |
1194 | ops->job_start(client, cmd); | |
01be038e JA |
1195 | free(cmd); |
1196 | break; | |
85dd01e7 JA |
1197 | case FIO_NET_CMD_START: { |
1198 | struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload; | |
1199 | ||
1200 | pdu->jobs = le32_to_cpu(pdu->jobs); | |
1201 | ops->start(client, cmd); | |
e951bdc4 JA |
1202 | free(cmd); |
1203 | break; | |
85dd01e7 | 1204 | } |
6b79c80c JA |
1205 | case FIO_NET_CMD_STOP: { |
1206 | struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload; | |
1207 | ||
1208 | convert_stop(cmd); | |
1209 | client->state = Client_stopped; | |
122c7725 JA |
1210 | client->error = le32_to_cpu(pdu->error); |
1211 | client->signal = le32_to_cpu(pdu->signal); | |
6b79c80c | 1212 | ops->stop(client, cmd); |
e951bdc4 JA |
1213 | free(cmd); |
1214 | break; | |
6b79c80c | 1215 | } |
40c60516 JA |
1216 | case FIO_NET_CMD_ADD_JOB: { |
1217 | struct cmd_add_job_pdu *pdu = (struct cmd_add_job_pdu *) cmd->payload; | |
1218 | ||
1219 | client->thread_number = le32_to_cpu(pdu->thread_number); | |
1220 | client->groupid = le32_to_cpu(pdu->groupid); | |
1221 | ||
807f9971 JA |
1222 | if (ops->add_job) |
1223 | ops->add_job(client, cmd); | |
1224 | free(cmd); | |
1225 | break; | |
40c60516 | 1226 | } |
1b42725f JA |
1227 | case FIO_NET_CMD_IOLOG: |
1228 | if (ops->iolog) { | |
1229 | struct cmd_iolog_pdu *pdu; | |
1230 | ||
1231 | pdu = convert_iolog(cmd); | |
1232 | ops->iolog(client, pdu); | |
1233 | } | |
1234 | free(cmd); | |
1235 | break; | |
40c60516 | 1236 | case FIO_NET_CMD_UPDATE_JOB: |
40c60516 | 1237 | ops->update_job(client, cmd); |
649cee91 | 1238 | remove_reply_cmd(client, cmd); |
40c60516 JA |
1239 | free(cmd); |
1240 | break; | |
e951bdc4 | 1241 | default: |
89c1707c | 1242 | log_err("fio: unknown client op: %s\n", fio_server_op(cmd->opcode)); |
e951bdc4 JA |
1243 | free(cmd); |
1244 | break; | |
37db14fe JA |
1245 | } |
1246 | ||
e951bdc4 | 1247 | return 1; |
37db14fe | 1248 | } |
b66570dc | 1249 | |
a5276616 | 1250 | static void request_client_etas(struct client_ops *ops) |
af9c9fb3 JA |
1251 | { |
1252 | struct fio_client *client; | |
1253 | struct flist_head *entry; | |
1254 | struct client_eta *eta; | |
82c1ed38 | 1255 | int skipped = 0; |
af9c9fb3 JA |
1256 | |
1257 | dprint(FD_NET, "client: request eta (%d)\n", nr_clients); | |
1258 | ||
af9c9fb3 JA |
1259 | eta = malloc(sizeof(*eta)); |
1260 | memset(&eta->eta, 0, sizeof(eta->eta)); | |
1261 | eta->pending = nr_clients; | |
1262 | ||
1263 | flist_for_each(entry, &client_list) { | |
1264 | client = flist_entry(entry, struct fio_client, list); | |
1265 | ||
82c1ed38 JA |
1266 | if (!flist_empty(&client->eta_list)) { |
1267 | skipped++; | |
1268 | continue; | |
1269 | } | |
01be038e JA |
1270 | if (client->state != Client_running) |
1271 | continue; | |
82c1ed38 | 1272 | |
f77d2676 | 1273 | assert(!client->eta_in_flight); |
82c1ed38 | 1274 | flist_add_tail(&client->eta_list, &eta_list); |
f77d2676 | 1275 | client->eta_in_flight = eta; |
af9c9fb3 | 1276 | fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA, |
df380934 | 1277 | (uintptr_t) eta, &client->cmd_list); |
af9c9fb3 JA |
1278 | } |
1279 | ||
82c1ed38 | 1280 | while (skipped--) |
a5276616 | 1281 | fio_client_dec_jobs_eta(eta, ops->eta); |
82c1ed38 | 1282 | |
af9c9fb3 JA |
1283 | dprint(FD_NET, "client: requested eta tag %p\n", eta); |
1284 | } | |
1285 | ||
89c1707c JA |
1286 | static int client_check_cmd_timeout(struct fio_client *client, |
1287 | struct timeval *now) | |
1288 | { | |
40c60516 | 1289 | struct fio_net_cmd_reply *reply; |
89c1707c JA |
1290 | struct flist_head *entry, *tmp; |
1291 | int ret = 0; | |
1292 | ||
1293 | flist_for_each_safe(entry, tmp, &client->cmd_list) { | |
40c60516 | 1294 | reply = flist_entry(entry, struct fio_net_cmd_reply, list); |
89c1707c | 1295 | |
40c60516 | 1296 | if (mtime_since(&reply->tv, now) < FIO_NET_CLIENT_TIMEOUT) |
89c1707c JA |
1297 | continue; |
1298 | ||
1299 | log_err("fio: client %s, timeout on cmd %s\n", client->hostname, | |
40c60516 JA |
1300 | fio_server_op(reply->opcode)); |
1301 | flist_del(&reply->list); | |
1302 | free(reply); | |
89c1707c JA |
1303 | ret = 1; |
1304 | } | |
1305 | ||
1306 | return flist_empty(&client->cmd_list) && ret; | |
1307 | } | |
1308 | ||
a5276616 | 1309 | static int fio_check_clients_timed_out(void) |
89c1707c JA |
1310 | { |
1311 | struct fio_client *client; | |
1312 | struct flist_head *entry, *tmp; | |
1313 | struct timeval tv; | |
1314 | int ret = 0; | |
1315 | ||
1316 | gettimeofday(&tv, NULL); | |
1317 | ||
1318 | flist_for_each_safe(entry, tmp, &client_list) { | |
1319 | client = flist_entry(entry, struct fio_client, list); | |
1320 | ||
1321 | if (flist_empty(&client->cmd_list)) | |
1322 | continue; | |
1323 | ||
1324 | if (!client_check_cmd_timeout(client, &tv)) | |
1325 | continue; | |
1326 | ||
a5276616 JA |
1327 | if (client->ops->timed_out) |
1328 | client->ops->timed_out(client); | |
ed727a46 JA |
1329 | else |
1330 | log_err("fio: client %s timed out\n", client->hostname); | |
1331 | ||
89c1707c JA |
1332 | remove_client(client); |
1333 | ret = 1; | |
1334 | } | |
1335 | ||
1336 | return ret; | |
1337 | } | |
1338 | ||
dd366728 | 1339 | int fio_handle_clients(struct client_ops *ops) |
b66570dc | 1340 | { |
b66570dc | 1341 | struct pollfd *pfds; |
498c92c2 | 1342 | int i, ret = 0, retval = 0; |
b66570dc | 1343 | |
af9c9fb3 JA |
1344 | gettimeofday(&eta_tv, NULL); |
1345 | ||
b66570dc JA |
1346 | pfds = malloc(nr_clients * sizeof(struct pollfd)); |
1347 | ||
37f0c1ae JA |
1348 | sum_stat_clients = nr_clients; |
1349 | init_thread_stat(&client_ts); | |
1350 | init_group_run_stat(&client_gs); | |
1351 | ||
82a4be1b | 1352 | while (!exit_backend && nr_clients) { |
c2cb6869 JA |
1353 | struct flist_head *entry, *tmp; |
1354 | struct fio_client *client; | |
1355 | ||
82a4be1b | 1356 | i = 0; |
c2cb6869 | 1357 | flist_for_each_safe(entry, tmp, &client_list) { |
82a4be1b | 1358 | client = flist_entry(entry, struct fio_client, list); |
b66570dc | 1359 | |
a5276616 | 1360 | if (!client->sent_job && !client->ops->stay_connected && |
c2cb6869 JA |
1361 | flist_empty(&client->cmd_list)) { |
1362 | remove_client(client); | |
1363 | continue; | |
1364 | } | |
1365 | ||
82a4be1b JA |
1366 | pfds[i].fd = client->fd; |
1367 | pfds[i].events = POLLIN; | |
1368 | i++; | |
1369 | } | |
1370 | ||
c2cb6869 JA |
1371 | if (!nr_clients) |
1372 | break; | |
1373 | ||
82a4be1b | 1374 | assert(i == nr_clients); |
b66570dc | 1375 | |
5c2857f9 | 1376 | do { |
af9c9fb3 JA |
1377 | struct timeval tv; |
1378 | ||
1379 | gettimeofday(&tv, NULL); | |
6433ee05 | 1380 | if (mtime_since(&eta_tv, &tv) >= ops->eta_msec) { |
a5276616 | 1381 | request_client_etas(ops); |
af9c9fb3 | 1382 | memcpy(&eta_tv, &tv, sizeof(tv)); |
89c1707c | 1383 | |
a5276616 | 1384 | if (fio_check_clients_timed_out()) |
89c1707c | 1385 | break; |
af9c9fb3 JA |
1386 | } |
1387 | ||
80102c86 | 1388 | ret = poll(pfds, nr_clients, ops->eta_msec); |
5c2857f9 JA |
1389 | if (ret < 0) { |
1390 | if (errno == EINTR) | |
1391 | continue; | |
1392 | log_err("fio: poll clients: %s\n", strerror(errno)); | |
1393 | break; | |
1394 | } else if (!ret) | |
b66570dc | 1395 | continue; |
5c2857f9 | 1396 | } while (ret <= 0); |
b66570dc JA |
1397 | |
1398 | for (i = 0; i < nr_clients; i++) { | |
1399 | if (!(pfds[i].revents & POLLIN)) | |
1400 | continue; | |
1401 | ||
1402 | client = find_client_by_fd(pfds[i].fd); | |
1403 | if (!client) { | |
3c5f57e3 | 1404 | log_err("fio: unknown client fd %d\n", pfds[i].fd); |
b66570dc JA |
1405 | continue; |
1406 | } | |
a5276616 | 1407 | if (!fio_handle_client(client)) { |
28d3ab07 JA |
1408 | log_info("client: host=%s disconnected\n", |
1409 | client->hostname); | |
1410 | remove_client(client); | |
498c92c2 | 1411 | retval = 1; |
38990764 | 1412 | } else if (client->error) |
498c92c2 | 1413 | retval = 1; |
343cb4a9 | 1414 | fio_put_client(client); |
b66570dc JA |
1415 | } |
1416 | } | |
1417 | ||
1418 | free(pfds); | |
498c92c2 | 1419 | return retval; |
b66570dc | 1420 | } |