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> |
3989b143 | 17 | #ifdef CONFIG_ZLIB |
1b42725f | 18 | #include <zlib.h> |
3989b143 | 19 | #endif |
132159a5 JA |
20 | |
21 | #include "fio.h" | |
dd366728 | 22 | #include "client.h" |
132159a5 | 23 | #include "server.h" |
b66570dc | 24 | #include "flist.h" |
3c5f57e3 | 25 | #include "hash.h" |
ca09be4b | 26 | #include "verify.h" |
132159a5 | 27 | |
dd366728 | 28 | static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd); |
89e5fad9 JA |
29 | static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd); |
30 | static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd); | |
dd366728 | 31 | static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd); |
084d1c6f | 32 | static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd); |
6b79c80c | 33 | static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd); |
85dd01e7 | 34 | static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd); |
dd366728 SC |
35 | |
36 | struct client_ops fio_client_ops = { | |
35c0ba7f | 37 | .text = handle_text, |
0420ba6a JA |
38 | .disk_util = handle_du, |
39 | .thread_status = handle_ts, | |
40 | .group_stats = handle_gs, | |
6b79c80c | 41 | .stop = handle_stop, |
85dd01e7 | 42 | .start = handle_start, |
a5276616 | 43 | .eta = display_thread_status, |
0420ba6a | 44 | .probe = handle_probe, |
6433ee05 | 45 | .eta_msec = FIO_CLIENT_DEF_ETA_MSEC, |
46bcd498 | 46 | .client_type = FIO_CLIENT_TYPE_CLI, |
dd366728 SC |
47 | }; |
48 | ||
af9c9fb3 | 49 | static struct timeval eta_tv; |
48fbb46e | 50 | |
b66570dc | 51 | static FLIST_HEAD(client_list); |
82c1ed38 | 52 | static FLIST_HEAD(eta_list); |
b66570dc | 53 | |
3f3a4542 JA |
54 | static FLIST_HEAD(arg_list); |
55 | ||
3650a3ca JA |
56 | struct thread_stat client_ts; |
57 | struct group_run_stats client_gs; | |
58 | int sum_stat_clients; | |
59 | ||
37f0c1ae | 60 | static int sum_stat_nr; |
952b05e0 CF |
61 | static struct json_object *root = NULL; |
62 | static struct json_array *clients_array = NULL; | |
63 | static struct json_array *du_array = NULL; | |
37f0c1ae | 64 | |
f9a33cc0 JA |
65 | static int error_clients; |
66 | ||
3c5f57e3 JA |
67 | #define FIO_CLIENT_HASH_BITS 7 |
68 | #define FIO_CLIENT_HASH_SZ (1 << FIO_CLIENT_HASH_BITS) | |
69 | #define FIO_CLIENT_HASH_MASK (FIO_CLIENT_HASH_SZ - 1) | |
bebe6398 | 70 | static struct flist_head client_hash[FIO_CLIENT_HASH_SZ]; |
3c5f57e3 | 71 | |
bebe6398 | 72 | static void fio_client_add_hash(struct fio_client *client) |
3c5f57e3 JA |
73 | { |
74 | int bucket = hash_long(client->fd, FIO_CLIENT_HASH_BITS); | |
75 | ||
76 | bucket &= FIO_CLIENT_HASH_MASK; | |
bebe6398 | 77 | flist_add(&client->hash_list, &client_hash[bucket]); |
3c5f57e3 JA |
78 | } |
79 | ||
bebe6398 | 80 | static void fio_client_remove_hash(struct fio_client *client) |
3c5f57e3 | 81 | { |
bebe6398 JA |
82 | if (!flist_empty(&client->hash_list)) |
83 | flist_del_init(&client->hash_list); | |
3c5f57e3 JA |
84 | } |
85 | ||
86 | static void fio_init fio_client_hash_init(void) | |
87 | { | |
88 | int i; | |
89 | ||
bebe6398 JA |
90 | for (i = 0; i < FIO_CLIENT_HASH_SZ; i++) |
91 | INIT_FLIST_HEAD(&client_hash[i]); | |
3c5f57e3 JA |
92 | } |
93 | ||
e5aa68b3 JA |
94 | static int read_data(int fd, void *data, size_t size) |
95 | { | |
96 | ssize_t ret; | |
97 | ||
98 | while (size) { | |
99 | ret = read(fd, data, size); | |
100 | if (ret < 0) { | |
101 | if (errno == EAGAIN || errno == EINTR) | |
102 | continue; | |
103 | break; | |
104 | } else if (!ret) | |
105 | break; | |
106 | else { | |
107 | data += ret; | |
108 | size -= ret; | |
109 | } | |
110 | } | |
111 | ||
112 | if (size) | |
113 | return EAGAIN; | |
114 | ||
115 | return 0; | |
116 | } | |
117 | ||
952b05e0 CF |
118 | static void fio_client_json_init(void) |
119 | { | |
01cfefcc | 120 | if (!(output_format & FIO_OUTPUT_JSON)) |
952b05e0 | 121 | return; |
84f1f865 | 122 | |
952b05e0 CF |
123 | root = json_create_object(); |
124 | json_object_add_value_string(root, "fio version", fio_version_string); | |
125 | clients_array = json_create_array(); | |
126 | json_object_add_value_array(root, "client_stats", clients_array); | |
127 | du_array = json_create_array(); | |
128 | json_object_add_value_array(root, "disk_util", du_array); | |
129 | } | |
130 | ||
131 | static void fio_client_json_fini(void) | |
132 | { | |
01cfefcc | 133 | if (!(output_format & FIO_OUTPUT_JSON)) |
952b05e0 | 134 | return; |
84f1f865 | 135 | log_info("\n"); |
a666cab8 | 136 | json_print_object(root, NULL); |
952b05e0 CF |
137 | log_info("\n"); |
138 | json_free_object(root); | |
139 | root = NULL; | |
140 | clients_array = NULL; | |
141 | du_array = NULL; | |
142 | } | |
143 | ||
b66570dc JA |
144 | static struct fio_client *find_client_by_fd(int fd) |
145 | { | |
3c5f57e3 | 146 | int bucket = hash_long(fd, FIO_CLIENT_HASH_BITS) & FIO_CLIENT_HASH_MASK; |
b66570dc JA |
147 | struct fio_client *client; |
148 | struct flist_head *entry; | |
149 | ||
bebe6398 JA |
150 | flist_for_each(entry, &client_hash[bucket]) { |
151 | client = flist_entry(entry, struct fio_client, hash_list); | |
b66570dc | 152 | |
e55f8f30 JA |
153 | if (client->fd == fd) { |
154 | client->refs++; | |
b66570dc | 155 | return client; |
e55f8f30 | 156 | } |
b66570dc JA |
157 | } |
158 | ||
159 | return NULL; | |
160 | } | |
161 | ||
3af45201 | 162 | void fio_put_client(struct fio_client *client) |
b66570dc | 163 | { |
5121a9aa JA |
164 | if (--client->refs) |
165 | return; | |
166 | ||
3af45201 JA |
167 | free(client->hostname); |
168 | if (client->argv) | |
169 | free(client->argv); | |
170 | if (client->name) | |
171 | free(client->name); | |
323255cc JA |
172 | while (client->nr_files) { |
173 | struct client_file *cf = &client->files[--client->nr_files]; | |
174 | ||
175 | free(cf->file); | |
176 | } | |
177 | if (client->files) | |
178 | free(client->files); | |
3af45201 | 179 | |
108fea77 | 180 | if (!client->did_stat) |
7b38411c | 181 | sum_stat_clients--; |
108fea77 | 182 | |
f9a33cc0 JA |
183 | if (client->error) |
184 | error_clients++; | |
185 | ||
3af45201 JA |
186 | free(client); |
187 | } | |
188 | ||
f02f7dac JA |
189 | static int fio_client_dec_jobs_eta(struct client_eta *eta, client_eta_op eta_fn) |
190 | { | |
191 | if (!--eta->pending) { | |
192 | eta_fn(&eta->eta); | |
193 | free(eta); | |
194 | return 0; | |
195 | } | |
196 | ||
197 | return 1; | |
198 | } | |
199 | ||
3af45201 JA |
200 | static void remove_client(struct fio_client *client) |
201 | { | |
202 | assert(client->refs); | |
203 | ||
39e8e016 | 204 | dprint(FD_NET, "client: removed <%s>\n", client->hostname); |
3af45201 JA |
205 | |
206 | if (!flist_empty(&client->list)) | |
207 | flist_del_init(&client->list); | |
3c5f57e3 | 208 | |
bebe6398 | 209 | fio_client_remove_hash(client); |
81179eec | 210 | |
82c1ed38 JA |
211 | if (!flist_empty(&client->eta_list)) { |
212 | flist_del_init(&client->eta_list); | |
a5276616 | 213 | fio_client_dec_jobs_eta(client->eta_in_flight, client->ops->eta); |
82c1ed38 | 214 | } |
af9c9fb3 | 215 | |
1e0c184e JA |
216 | close(client->fd); |
217 | client->fd = -1; | |
218 | ||
0cf3ece0 JA |
219 | if (client->ops->removed) |
220 | client->ops->removed(client); | |
221 | ||
3c5f57e3 | 222 | nr_clients--; |
3af45201 | 223 | fio_put_client(client); |
5121a9aa JA |
224 | } |
225 | ||
343cb4a9 JA |
226 | struct fio_client *fio_get_client(struct fio_client *client) |
227 | { | |
228 | client->refs++; | |
229 | return client; | |
230 | } | |
231 | ||
fa2ea806 JA |
232 | static void __fio_client_add_cmd_option(struct fio_client *client, |
233 | const char *opt) | |
81179eec | 234 | { |
39e8e016 JA |
235 | int index; |
236 | ||
237 | index = client->argc++; | |
81179eec | 238 | client->argv = realloc(client->argv, sizeof(char *) * client->argc); |
39e8e016 JA |
239 | client->argv[index] = strdup(opt); |
240 | dprint(FD_NET, "client: add cmd %d: %s\n", index, opt); | |
81179eec JA |
241 | } |
242 | ||
fa2ea806 | 243 | void fio_client_add_cmd_option(void *cookie, const char *opt) |
81179eec | 244 | { |
bebe6398 | 245 | struct fio_client *client = cookie; |
3f3a4542 | 246 | struct flist_head *entry; |
81179eec | 247 | |
bebe6398 | 248 | if (!client || !opt) |
fa2ea806 | 249 | return; |
81179eec | 250 | |
fa2ea806 | 251 | __fio_client_add_cmd_option(client, opt); |
3f3a4542 JA |
252 | |
253 | /* | |
254 | * Duplicate arguments to shared client group | |
255 | */ | |
256 | flist_for_each(entry, &arg_list) { | |
257 | client = flist_entry(entry, struct fio_client, arg_list); | |
258 | ||
259 | __fio_client_add_cmd_option(client, opt); | |
260 | } | |
81179eec JA |
261 | } |
262 | ||
a5276616 JA |
263 | struct fio_client *fio_client_add_explicit(struct client_ops *ops, |
264 | const char *hostname, int type, | |
3ec62ec4 JA |
265 | int port) |
266 | { | |
267 | struct fio_client *client; | |
268 | ||
269 | client = malloc(sizeof(*client)); | |
270 | memset(client, 0, sizeof(*client)); | |
271 | ||
272 | INIT_FLIST_HEAD(&client->list); | |
273 | INIT_FLIST_HEAD(&client->hash_list); | |
274 | INIT_FLIST_HEAD(&client->arg_list); | |
275 | INIT_FLIST_HEAD(&client->eta_list); | |
276 | INIT_FLIST_HEAD(&client->cmd_list); | |
277 | ||
278 | client->hostname = strdup(hostname); | |
279 | ||
280 | if (type == Fio_client_socket) | |
281 | client->is_sock = 1; | |
282 | else { | |
283 | int ipv6; | |
284 | ||
285 | ipv6 = type == Fio_client_ipv6; | |
3aa3ceeb | 286 | if (fio_server_parse_host(hostname, ipv6, |
3ec62ec4 JA |
287 | &client->addr.sin_addr, |
288 | &client->addr6.sin6_addr)) | |
289 | goto err; | |
290 | ||
291 | client->port = port; | |
292 | } | |
293 | ||
294 | client->fd = -1; | |
a5276616 | 295 | client->ops = ops; |
5121a9aa | 296 | client->refs = 1; |
46bcd498 | 297 | client->type = ops->client_type; |
3ec62ec4 JA |
298 | |
299 | __fio_client_add_cmd_option(client, "fio"); | |
300 | ||
301 | flist_add(&client->list, &client_list); | |
302 | nr_clients++; | |
303 | dprint(FD_NET, "client: added <%s>\n", client->hostname); | |
304 | return client; | |
305 | err: | |
306 | free(client); | |
307 | return NULL; | |
308 | } | |
309 | ||
323255cc | 310 | int fio_client_add_ini_file(void *cookie, const char *ini_file, int remote) |
14ea90ed JA |
311 | { |
312 | struct fio_client *client = cookie; | |
323255cc | 313 | struct client_file *cf; |
14ea90ed | 314 | size_t new_size; |
323255cc | 315 | void *new_files; |
14ea90ed | 316 | |
ea91da21 JA |
317 | if (!client) |
318 | return 1; | |
319 | ||
14ea90ed JA |
320 | dprint(FD_NET, "client <%s>: add ini %s\n", client->hostname, ini_file); |
321 | ||
323255cc JA |
322 | new_size = (client->nr_files + 1) * sizeof(struct client_file); |
323 | new_files = realloc(client->files, new_size); | |
324 | if (!new_files) | |
325 | return 1; | |
326 | ||
327 | client->files = new_files; | |
328 | cf = &client->files[client->nr_files]; | |
329 | cf->file = strdup(ini_file); | |
330 | cf->remote = remote; | |
331 | client->nr_files++; | |
332 | return 0; | |
14ea90ed JA |
333 | } |
334 | ||
a5276616 | 335 | int fio_client_add(struct client_ops *ops, const char *hostname, void **cookie) |
132159a5 | 336 | { |
3f3a4542 | 337 | struct fio_client *existing = *cookie; |
b66570dc | 338 | struct fio_client *client; |
132159a5 | 339 | |
3f3a4542 JA |
340 | if (existing) { |
341 | /* | |
342 | * We always add our "exec" name as the option, hence 1 | |
343 | * means empty. | |
344 | */ | |
345 | if (existing->argc == 1) | |
346 | flist_add_tail(&existing->arg_list, &arg_list); | |
347 | else { | |
348 | while (!flist_empty(&arg_list)) | |
349 | flist_del_init(arg_list.next); | |
350 | } | |
351 | } | |
352 | ||
b66570dc | 353 | client = malloc(sizeof(*client)); |
a37f69b7 | 354 | memset(client, 0, sizeof(*client)); |
81179eec | 355 | |
3c5f57e3 | 356 | INIT_FLIST_HEAD(&client->list); |
bebe6398 | 357 | INIT_FLIST_HEAD(&client->hash_list); |
3f3a4542 | 358 | INIT_FLIST_HEAD(&client->arg_list); |
82c1ed38 | 359 | INIT_FLIST_HEAD(&client->eta_list); |
89c1707c | 360 | INIT_FLIST_HEAD(&client->cmd_list); |
3c5f57e3 | 361 | |
bebe6398 JA |
362 | if (fio_server_parse_string(hostname, &client->hostname, |
363 | &client->is_sock, &client->port, | |
811826be JA |
364 | &client->addr.sin_addr, |
365 | &client->addr6.sin6_addr, | |
366 | &client->ipv6)) | |
bebe6398 | 367 | return -1; |
87aa8f19 | 368 | |
bebe6398 | 369 | client->fd = -1; |
a5276616 | 370 | client->ops = ops; |
5121a9aa | 371 | client->refs = 1; |
46bcd498 | 372 | client->type = ops->client_type; |
3c5f57e3 | 373 | |
81179eec JA |
374 | __fio_client_add_cmd_option(client, "fio"); |
375 | ||
a37f69b7 JA |
376 | flist_add(&client->list, &client_list); |
377 | nr_clients++; | |
bebe6398 JA |
378 | dprint(FD_NET, "client: added <%s>\n", client->hostname); |
379 | *cookie = client; | |
380 | return 0; | |
a37f69b7 JA |
381 | } |
382 | ||
ca09be4b JA |
383 | static const char *server_name(struct fio_client *client, char *buf, |
384 | size_t bufsize) | |
385 | { | |
386 | const char *from; | |
387 | ||
388 | if (client->ipv6) | |
389 | from = inet_ntop(AF_INET6, (struct sockaddr *) &client->addr6.sin6_addr, buf, bufsize); | |
390 | else if (client->is_sock) | |
391 | from = "sock"; | |
392 | else | |
393 | from = inet_ntop(AF_INET, (struct sockaddr *) &client->addr.sin_addr, buf, bufsize); | |
394 | ||
395 | return from; | |
396 | } | |
397 | ||
d824c3b8 JA |
398 | static void probe_client(struct fio_client *client) |
399 | { | |
3989b143 | 400 | struct cmd_client_probe_pdu pdu; |
e8b60617 | 401 | const char *sname; |
3989b143 | 402 | uint64_t tag; |
ca09be4b | 403 | char buf[64]; |
3989b143 | 404 | |
d824c3b8 JA |
405 | dprint(FD_NET, "client: send probe\n"); |
406 | ||
3989b143 JA |
407 | #ifdef CONFIG_ZLIB |
408 | pdu.flags = __le64_to_cpu(FIO_PROBE_FLAG_ZLIB); | |
409 | #else | |
410 | pdu.flags = 0; | |
411 | #endif | |
412 | ||
e8b60617 JA |
413 | sname = server_name(client, buf, sizeof(buf)); |
414 | memset(pdu.server, 0, sizeof(pdu.server)); | |
415 | strncpy((char *) pdu.server, sname, sizeof(pdu.server) - 1); | |
ca09be4b | 416 | |
3989b143 | 417 | fio_net_send_cmd(client->fd, FIO_NET_CMD_PROBE, &pdu, sizeof(pdu), &tag, &client->cmd_list); |
d824c3b8 JA |
418 | } |
419 | ||
87aa8f19 | 420 | static int fio_client_connect_ip(struct fio_client *client) |
a37f69b7 | 421 | { |
811826be | 422 | struct sockaddr *addr; |
67bf9823 | 423 | socklen_t socklen; |
811826be JA |
424 | int fd, domain; |
425 | ||
426 | if (client->ipv6) { | |
427 | client->addr6.sin6_family = AF_INET6; | |
428 | client->addr6.sin6_port = htons(client->port); | |
429 | domain = AF_INET6; | |
430 | addr = (struct sockaddr *) &client->addr6; | |
431 | socklen = sizeof(client->addr6); | |
432 | } else { | |
433 | client->addr.sin_family = AF_INET; | |
434 | client->addr.sin_port = htons(client->port); | |
435 | domain = AF_INET; | |
436 | addr = (struct sockaddr *) &client->addr; | |
437 | socklen = sizeof(client->addr); | |
438 | } | |
132159a5 | 439 | |
811826be | 440 | fd = socket(domain, SOCK_STREAM, 0); |
132159a5 | 441 | if (fd < 0) { |
25095e1b JA |
442 | int ret = -errno; |
443 | ||
132159a5 | 444 | log_err("fio: socket: %s\n", strerror(errno)); |
25095e1b | 445 | return ret; |
132159a5 JA |
446 | } |
447 | ||
811826be | 448 | if (connect(fd, addr, socklen) < 0) { |
25095e1b JA |
449 | int ret = -errno; |
450 | ||
132159a5 | 451 | log_err("fio: connect: %s\n", strerror(errno)); |
a7de0a11 JA |
452 | log_err("fio: failed to connect to %s:%u\n", client->hostname, |
453 | client->port); | |
b94cba47 | 454 | close(fd); |
25095e1b | 455 | return ret; |
87aa8f19 JA |
456 | } |
457 | ||
458 | return fd; | |
459 | } | |
460 | ||
461 | static int fio_client_connect_sock(struct fio_client *client) | |
462 | { | |
463 | struct sockaddr_un *addr = &client->addr_un; | |
67bf9823 | 464 | socklen_t len; |
87aa8f19 JA |
465 | int fd; |
466 | ||
467 | memset(addr, 0, sizeof(*addr)); | |
468 | addr->sun_family = AF_UNIX; | |
1cb96414 | 469 | strncpy(addr->sun_path, client->hostname, sizeof(addr->sun_path) - 1); |
87aa8f19 JA |
470 | |
471 | fd = socket(AF_UNIX, SOCK_STREAM, 0); | |
472 | if (fd < 0) { | |
25095e1b JA |
473 | int ret = -errno; |
474 | ||
87aa8f19 | 475 | log_err("fio: socket: %s\n", strerror(errno)); |
25095e1b | 476 | return ret; |
87aa8f19 JA |
477 | } |
478 | ||
479 | len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1; | |
480 | if (connect(fd, (struct sockaddr *) addr, len) < 0) { | |
25095e1b JA |
481 | int ret = -errno; |
482 | ||
87aa8f19 | 483 | log_err("fio: connect; %s\n", strerror(errno)); |
b94cba47 | 484 | close(fd); |
25095e1b | 485 | return ret; |
132159a5 JA |
486 | } |
487 | ||
87aa8f19 JA |
488 | return fd; |
489 | } | |
490 | ||
2f99deb0 | 491 | int fio_client_connect(struct fio_client *client) |
87aa8f19 JA |
492 | { |
493 | int fd; | |
494 | ||
495 | dprint(FD_NET, "client: connect to host %s\n", client->hostname); | |
496 | ||
87aa8f19 JA |
497 | if (client->is_sock) |
498 | fd = fio_client_connect_sock(client); | |
499 | else | |
500 | fd = fio_client_connect_ip(client); | |
501 | ||
89c1707c JA |
502 | dprint(FD_NET, "client: %s connected %d\n", client->hostname, fd); |
503 | ||
87aa8f19 | 504 | if (fd < 0) |
a1a3ed51 | 505 | return fd; |
87aa8f19 | 506 | |
b66570dc | 507 | client->fd = fd; |
bebe6398 | 508 | fio_client_add_hash(client); |
81179eec | 509 | client->state = Client_connected; |
d824c3b8 JA |
510 | |
511 | probe_client(client); | |
132159a5 JA |
512 | return 0; |
513 | } | |
514 | ||
0cf3ece0 | 515 | int fio_client_terminate(struct fio_client *client) |
2f99deb0 | 516 | { |
0cf3ece0 | 517 | return fio_net_send_quit(client->fd); |
2f99deb0 JA |
518 | } |
519 | ||
cc0df00a JA |
520 | void fio_clients_terminate(void) |
521 | { | |
522 | struct flist_head *entry; | |
523 | struct fio_client *client; | |
524 | ||
60efd14e JA |
525 | dprint(FD_NET, "client: terminate clients\n"); |
526 | ||
cc0df00a JA |
527 | flist_for_each(entry, &client_list) { |
528 | client = flist_entry(entry, struct fio_client, list); | |
2f99deb0 | 529 | fio_client_terminate(client); |
cc0df00a JA |
530 | } |
531 | } | |
532 | ||
533 | static void sig_int(int sig) | |
534 | { | |
bebe6398 | 535 | dprint(FD_NET, "client: got signal %d\n", sig); |
cc0df00a JA |
536 | fio_clients_terminate(); |
537 | } | |
538 | ||
539 | static void client_signal_handler(void) | |
540 | { | |
541 | struct sigaction act; | |
542 | ||
543 | memset(&act, 0, sizeof(act)); | |
544 | act.sa_handler = sig_int; | |
545 | act.sa_flags = SA_RESTART; | |
546 | sigaction(SIGINT, &act, NULL); | |
547 | ||
548 | memset(&act, 0, sizeof(act)); | |
549 | act.sa_handler = sig_int; | |
550 | act.sa_flags = SA_RESTART; | |
551 | sigaction(SIGTERM, &act, NULL); | |
b852e7cf | 552 | |
2f694507 BC |
553 | /* Windows uses SIGBREAK as a quit signal from other applications */ |
554 | #ifdef WIN32 | |
555 | memset(&act, 0, sizeof(act)); | |
556 | act.sa_handler = sig_int; | |
557 | act.sa_flags = SA_RESTART; | |
558 | sigaction(SIGBREAK, &act, NULL); | |
559 | #endif | |
560 | ||
b852e7cf JA |
561 | memset(&act, 0, sizeof(act)); |
562 | act.sa_handler = sig_show_status; | |
563 | act.sa_flags = SA_RESTART; | |
564 | sigaction(SIGUSR1, &act, NULL); | |
cc0df00a JA |
565 | } |
566 | ||
81179eec JA |
567 | static int send_client_cmd_line(struct fio_client *client) |
568 | { | |
fa2ea806 JA |
569 | struct cmd_single_line_pdu *cslp; |
570 | struct cmd_line_pdu *clp; | |
571 | unsigned long offset; | |
7f868316 | 572 | unsigned int *lens; |
fa2ea806 JA |
573 | void *pdu; |
574 | size_t mem; | |
81179eec JA |
575 | int i, ret; |
576 | ||
39e8e016 | 577 | dprint(FD_NET, "client: send cmdline %d\n", client->argc); |
60efd14e | 578 | |
7f868316 JA |
579 | lens = malloc(client->argc * sizeof(unsigned int)); |
580 | ||
fa2ea806 JA |
581 | /* |
582 | * Find out how much mem we need | |
583 | */ | |
7f868316 JA |
584 | for (i = 0, mem = 0; i < client->argc; i++) { |
585 | lens[i] = strlen(client->argv[i]) + 1; | |
586 | mem += lens[i]; | |
587 | } | |
fa2ea806 JA |
588 | |
589 | /* | |
590 | * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu | |
591 | */ | |
592 | mem += sizeof(*clp) + (client->argc * sizeof(*cslp)); | |
593 | ||
594 | pdu = malloc(mem); | |
595 | clp = pdu; | |
596 | offset = sizeof(*clp); | |
597 | ||
598 | for (i = 0; i < client->argc; i++) { | |
7f868316 | 599 | uint16_t arg_len = lens[i]; |
fa2ea806 JA |
600 | |
601 | cslp = pdu + offset; | |
602 | strcpy((char *) cslp->text, client->argv[i]); | |
603 | cslp->len = cpu_to_le16(arg_len); | |
604 | offset += sizeof(*cslp) + arg_len; | |
605 | } | |
81179eec | 606 | |
7f868316 | 607 | free(lens); |
fa2ea806 | 608 | clp->lines = cpu_to_le16(client->argc); |
46bcd498 | 609 | clp->client_type = __cpu_to_le16(client->type); |
40c60516 | 610 | ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, NULL, NULL); |
81179eec JA |
611 | free(pdu); |
612 | return ret; | |
613 | } | |
614 | ||
a37f69b7 JA |
615 | int fio_clients_connect(void) |
616 | { | |
617 | struct fio_client *client; | |
618 | struct flist_head *entry, *tmp; | |
619 | int ret; | |
620 | ||
93bcfd20 BC |
621 | #ifdef WIN32 |
622 | WSADATA wsd; | |
3c3ed070 | 623 | WSAStartup(MAKEWORD(2, 2), &wsd); |
93bcfd20 BC |
624 | #endif |
625 | ||
60efd14e JA |
626 | dprint(FD_NET, "client: connect all\n"); |
627 | ||
cc0df00a JA |
628 | client_signal_handler(); |
629 | ||
a37f69b7 JA |
630 | flist_for_each_safe(entry, tmp, &client_list) { |
631 | client = flist_entry(entry, struct fio_client, list); | |
632 | ||
633 | ret = fio_client_connect(client); | |
0b8f30a5 | 634 | if (ret) { |
a37f69b7 | 635 | remove_client(client); |
0b8f30a5 JA |
636 | continue; |
637 | } | |
638 | ||
81179eec JA |
639 | if (client->argc > 1) |
640 | send_client_cmd_line(client); | |
a37f69b7 JA |
641 | } |
642 | ||
643 | return !nr_clients; | |
644 | } | |
645 | ||
b9d2f30a JA |
646 | int fio_start_client(struct fio_client *client) |
647 | { | |
648 | dprint(FD_NET, "client: start %s\n", client->hostname); | |
649 | return fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_RUN, 0, NULL); | |
650 | } | |
651 | ||
652 | int fio_start_all_clients(void) | |
653 | { | |
654 | struct fio_client *client; | |
655 | struct flist_head *entry, *tmp; | |
656 | int ret; | |
657 | ||
658 | dprint(FD_NET, "client: start all\n"); | |
659 | ||
952b05e0 CF |
660 | fio_client_json_init(); |
661 | ||
b9d2f30a JA |
662 | flist_for_each_safe(entry, tmp, &client_list) { |
663 | client = flist_entry(entry, struct fio_client, list); | |
664 | ||
665 | ret = fio_start_client(client); | |
666 | if (ret) { | |
667 | remove_client(client); | |
668 | continue; | |
669 | } | |
670 | } | |
671 | ||
672 | return flist_empty(&client_list); | |
673 | } | |
674 | ||
323255cc JA |
675 | static int __fio_client_send_remote_ini(struct fio_client *client, |
676 | const char *filename) | |
677 | { | |
678 | struct cmd_load_file_pdu *pdu; | |
679 | size_t p_size; | |
680 | int ret; | |
681 | ||
682 | dprint(FD_NET, "send remote ini %s to %s\n", filename, client->hostname); | |
683 | ||
829c31b5 | 684 | p_size = sizeof(*pdu) + strlen(filename) + 1; |
323255cc | 685 | pdu = malloc(p_size); |
829c31b5 | 686 | memset(pdu, 0, p_size); |
323255cc JA |
687 | pdu->name_len = strlen(filename); |
688 | strcpy((char *) pdu->file, filename); | |
689 | pdu->client_type = cpu_to_le16((uint16_t) client->type); | |
690 | ||
691 | client->sent_job = 1; | |
692 | ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_LOAD_FILE, pdu, p_size,NULL, NULL); | |
693 | free(pdu); | |
694 | return ret; | |
695 | } | |
696 | ||
132159a5 JA |
697 | /* |
698 | * Send file contents to server backend. We could use sendfile(), but to remain | |
699 | * more portable lets just read/write the darn thing. | |
700 | */ | |
323255cc JA |
701 | static int __fio_client_send_local_ini(struct fio_client *client, |
702 | const char *filename) | |
132159a5 | 703 | { |
46bcd498 JA |
704 | struct cmd_job_pdu *pdu; |
705 | size_t p_size; | |
132159a5 | 706 | struct stat sb; |
46bcd498 JA |
707 | char *p; |
708 | void *buf; | |
132159a5 JA |
709 | off_t len; |
710 | int fd, ret; | |
711 | ||
46c48f1f JA |
712 | dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname); |
713 | ||
132159a5 JA |
714 | fd = open(filename, O_RDONLY); |
715 | if (fd < 0) { | |
8a1db9a1 | 716 | ret = -errno; |
e951bdc4 | 717 | log_err("fio: job file <%s> open: %s\n", filename, strerror(errno)); |
25095e1b | 718 | return ret; |
132159a5 JA |
719 | } |
720 | ||
721 | if (fstat(fd, &sb) < 0) { | |
8a1db9a1 | 722 | ret = -errno; |
132159a5 | 723 | log_err("fio: job file stat: %s\n", strerror(errno)); |
b94cba47 | 724 | close(fd); |
25095e1b | 725 | return ret; |
132159a5 JA |
726 | } |
727 | ||
46bcd498 JA |
728 | p_size = sb.st_size + sizeof(*pdu); |
729 | pdu = malloc(p_size); | |
730 | buf = pdu->buf; | |
132159a5 JA |
731 | |
732 | len = sb.st_size; | |
733 | p = buf; | |
e5aa68b3 | 734 | if (read_data(fd, p, len)) { |
0b8f30a5 | 735 | log_err("fio: failed reading job file %s\n", filename); |
b94cba47 | 736 | close(fd); |
03a22d91 | 737 | free(pdu); |
0b8f30a5 JA |
738 | return 1; |
739 | } | |
740 | ||
46bcd498 JA |
741 | pdu->buf_len = __cpu_to_le32(sb.st_size); |
742 | pdu->client_type = cpu_to_le32(client->type); | |
743 | ||
c2cb6869 | 744 | client->sent_job = 1; |
40c60516 | 745 | ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, pdu, p_size, NULL, NULL); |
46bcd498 | 746 | free(pdu); |
b94cba47 | 747 | close(fd); |
132159a5 JA |
748 | return ret; |
749 | } | |
37db14fe | 750 | |
323255cc JA |
751 | int fio_client_send_ini(struct fio_client *client, const char *filename, |
752 | int remote) | |
9988ca70 | 753 | { |
25095e1b JA |
754 | int ret; |
755 | ||
323255cc JA |
756 | if (!remote) |
757 | ret = __fio_client_send_local_ini(client, filename); | |
758 | else | |
759 | ret = __fio_client_send_remote_ini(client, filename); | |
760 | ||
3af45201 JA |
761 | if (!ret) |
762 | client->sent_job = 1; | |
9988ca70 | 763 | |
25095e1b | 764 | return ret; |
9988ca70 JA |
765 | } |
766 | ||
323255cc JA |
767 | static int fio_client_send_cf(struct fio_client *client, |
768 | struct client_file *cf) | |
769 | { | |
770 | return fio_client_send_ini(client, cf->file, cf->remote); | |
771 | } | |
772 | ||
a37f69b7 JA |
773 | int fio_clients_send_ini(const char *filename) |
774 | { | |
775 | struct fio_client *client; | |
776 | struct flist_head *entry, *tmp; | |
777 | ||
778 | flist_for_each_safe(entry, tmp, &client_list) { | |
779 | client = flist_entry(entry, struct fio_client, list); | |
780 | ||
323255cc | 781 | if (client->nr_files) { |
14ea90ed JA |
782 | int i; |
783 | ||
323255cc JA |
784 | for (i = 0; i < client->nr_files; i++) { |
785 | struct client_file *cf; | |
14ea90ed | 786 | |
323255cc JA |
787 | cf = &client->files[i]; |
788 | ||
789 | if (fio_client_send_cf(client, cf)) { | |
14ea90ed JA |
790 | remove_client(client); |
791 | break; | |
792 | } | |
793 | } | |
323255cc JA |
794 | } |
795 | if (client->sent_job) | |
796 | continue; | |
797 | if (!filename || fio_client_send_ini(client, filename, 0)) | |
3af45201 | 798 | remove_client(client); |
a37f69b7 JA |
799 | } |
800 | ||
801 | return !nr_clients; | |
802 | } | |
803 | ||
40c60516 JA |
804 | int fio_client_update_options(struct fio_client *client, |
805 | struct thread_options *o, uint64_t *tag) | |
806 | { | |
807 | struct cmd_add_job_pdu pdu; | |
808 | ||
809 | pdu.thread_number = cpu_to_le32(client->thread_number); | |
810 | pdu.groupid = cpu_to_le32(client->groupid); | |
811 | convert_thread_options_to_net(&pdu.top, o); | |
952b05e0 | 812 | |
40c60516 JA |
813 | return fio_net_send_cmd(client->fd, FIO_NET_CMD_UPDATE_JOB, &pdu, sizeof(pdu), tag, &client->cmd_list); |
814 | } | |
815 | ||
a64e88da JA |
816 | static void convert_io_stat(struct io_stat *dst, struct io_stat *src) |
817 | { | |
818 | dst->max_val = le64_to_cpu(src->max_val); | |
819 | dst->min_val = le64_to_cpu(src->min_val); | |
820 | dst->samples = le64_to_cpu(src->samples); | |
802ad4a8 JA |
821 | |
822 | /* | |
823 | * Floats arrive as IEEE 754 encoded uint64_t, convert back to double | |
824 | */ | |
825 | dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i)); | |
826 | dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i)); | |
a64e88da JA |
827 | } |
828 | ||
829 | static void convert_ts(struct thread_stat *dst, struct thread_stat *src) | |
830 | { | |
831 | int i, j; | |
832 | ||
2f122b13 JA |
833 | dst->error = le32_to_cpu(src->error); |
834 | dst->thread_number = le32_to_cpu(src->thread_number); | |
835 | dst->groupid = le32_to_cpu(src->groupid); | |
836 | dst->pid = le32_to_cpu(src->pid); | |
837 | dst->members = le32_to_cpu(src->members); | |
771e58be | 838 | dst->unified_rw_rep = le32_to_cpu(src->unified_rw_rep); |
a64e88da | 839 | |
298921d6 | 840 | for (i = 0; i < DDIR_RWDIR_CNT; i++) { |
a64e88da JA |
841 | convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]); |
842 | convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]); | |
843 | convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]); | |
844 | convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]); | |
845 | } | |
846 | ||
847 | dst->usr_time = le64_to_cpu(src->usr_time); | |
848 | dst->sys_time = le64_to_cpu(src->sys_time); | |
849 | dst->ctx = le64_to_cpu(src->ctx); | |
850 | dst->minf = le64_to_cpu(src->minf); | |
851 | dst->majf = le64_to_cpu(src->majf); | |
852 | dst->clat_percentiles = le64_to_cpu(src->clat_percentiles); | |
988d97ba | 853 | dst->percentile_precision = le64_to_cpu(src->percentile_precision); |
802ad4a8 JA |
854 | |
855 | for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) { | |
856 | fio_fp64_t *fps = &src->percentile_list[i]; | |
857 | fio_fp64_t *fpd = &dst->percentile_list[i]; | |
858 | ||
859 | fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i)); | |
860 | } | |
a64e88da JA |
861 | |
862 | for (i = 0; i < FIO_IO_U_MAP_NR; i++) { | |
863 | dst->io_u_map[i] = le32_to_cpu(src->io_u_map[i]); | |
864 | dst->io_u_submit[i] = le32_to_cpu(src->io_u_submit[i]); | |
865 | dst->io_u_complete[i] = le32_to_cpu(src->io_u_complete[i]); | |
866 | } | |
867 | ||
868 | for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) { | |
869 | dst->io_u_lat_u[i] = le32_to_cpu(src->io_u_lat_u[i]); | |
870 | dst->io_u_lat_m[i] = le32_to_cpu(src->io_u_lat_m[i]); | |
871 | } | |
872 | ||
298921d6 | 873 | for (i = 0; i < DDIR_RWDIR_CNT; i++) |
a64e88da JA |
874 | for (j = 0; j < FIO_IO_U_PLAT_NR; j++) |
875 | dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]); | |
876 | ||
78799deb | 877 | for (i = 0; i < DDIR_RWDIR_CNT; i++) { |
a64e88da | 878 | dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]); |
93eee04a | 879 | dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]); |
3bcb9d94 | 880 | dst->drop_io_u[i] = le64_to_cpu(src->drop_io_u[i]); |
a64e88da JA |
881 | } |
882 | ||
883 | dst->total_submit = le64_to_cpu(src->total_submit); | |
884 | dst->total_complete = le64_to_cpu(src->total_complete); | |
885 | ||
298921d6 | 886 | for (i = 0; i < DDIR_RWDIR_CNT; i++) { |
a64e88da JA |
887 | dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]); |
888 | dst->runtime[i] = le64_to_cpu(src->runtime[i]); | |
889 | } | |
890 | ||
891 | dst->total_run_time = le64_to_cpu(src->total_run_time); | |
892 | dst->continue_on_error = le16_to_cpu(src->continue_on_error); | |
893 | dst->total_err_count = le64_to_cpu(src->total_err_count); | |
ddcc0b69 JA |
894 | dst->first_error = le32_to_cpu(src->first_error); |
895 | dst->kb_base = le32_to_cpu(src->kb_base); | |
ad705bcb | 896 | dst->unit_base = le32_to_cpu(src->unit_base); |
3e260a46 JA |
897 | |
898 | dst->latency_depth = le32_to_cpu(src->latency_depth); | |
899 | dst->latency_target = le64_to_cpu(src->latency_target); | |
900 | dst->latency_window = le64_to_cpu(src->latency_window); | |
901 | dst->latency_percentile.u.f = fio_uint64_to_double(le64_to_cpu(src->latency_percentile.u.i)); | |
66347cfa DE |
902 | |
903 | dst->nr_block_infos = le64_to_cpu(src->nr_block_infos); | |
904 | for (i = 0; i < dst->nr_block_infos; i++) | |
905 | dst->block_infos[i] = le32_to_cpu(src->block_infos[i]); | |
a64e88da JA |
906 | } |
907 | ||
908 | static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src) | |
909 | { | |
910 | int i; | |
911 | ||
298921d6 | 912 | for (i = 0; i < DDIR_RWDIR_CNT; i++) { |
a64e88da JA |
913 | dst->max_run[i] = le64_to_cpu(src->max_run[i]); |
914 | dst->min_run[i] = le64_to_cpu(src->min_run[i]); | |
915 | dst->max_bw[i] = le64_to_cpu(src->max_bw[i]); | |
916 | dst->min_bw[i] = le64_to_cpu(src->min_bw[i]); | |
917 | dst->io_kb[i] = le64_to_cpu(src->io_kb[i]); | |
918 | dst->agg[i] = le64_to_cpu(src->agg[i]); | |
919 | } | |
920 | ||
921 | dst->kb_base = le32_to_cpu(src->kb_base); | |
ad705bcb | 922 | dst->unit_base = le32_to_cpu(src->unit_base); |
a64e88da | 923 | dst->groupid = le32_to_cpu(src->groupid); |
771e58be | 924 | dst->unified_rw_rep = le32_to_cpu(src->unified_rw_rep); |
a64e88da JA |
925 | } |
926 | ||
952b05e0 | 927 | static void json_object_add_client_info(struct json_object *obj, |
9a4424c8 | 928 | struct fio_client *client) |
952b05e0 | 929 | { |
9a4424c8 CF |
930 | const char *hostname = client->hostname ? client->hostname : ""; |
931 | ||
932 | json_object_add_value_string(obj, "hostname", hostname); | |
952b05e0 CF |
933 | json_object_add_value_int(obj, "port", client->port); |
934 | } | |
935 | ||
89e5fad9 | 936 | static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd) |
a64e88da JA |
937 | { |
938 | struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload; | |
952b05e0 | 939 | struct json_object *tsobj; |
a64e88da | 940 | |
a666cab8 | 941 | tsobj = show_thread_status(&p->ts, &p->rs, NULL); |
108fea77 | 942 | client->did_stat = 1; |
952b05e0 CF |
943 | if (tsobj) { |
944 | json_object_add_client_info(tsobj, client); | |
945 | json_array_add_value_object(clients_array, tsobj); | |
946 | } | |
37f0c1ae | 947 | |
7b38411c | 948 | if (sum_stat_clients <= 1) |
37f0c1ae JA |
949 | return; |
950 | ||
fd595830 | 951 | sum_thread_stats(&client_ts, &p->ts, sum_stat_nr == 1); |
37f0c1ae JA |
952 | sum_group_stats(&client_gs, &p->rs); |
953 | ||
954 | client_ts.members++; | |
2f122b13 | 955 | client_ts.thread_number = p->ts.thread_number; |
37f0c1ae | 956 | client_ts.groupid = p->ts.groupid; |
771e58be | 957 | client_ts.unified_rw_rep = p->ts.unified_rw_rep; |
37f0c1ae JA |
958 | |
959 | if (++sum_stat_nr == sum_stat_clients) { | |
960 | strcpy(client_ts.name, "All clients"); | |
a666cab8 | 961 | tsobj = show_thread_status(&client_ts, &client_gs, NULL); |
952b05e0 CF |
962 | if (tsobj) { |
963 | json_object_add_client_info(tsobj, client); | |
964 | json_array_add_value_object(clients_array, tsobj); | |
965 | } | |
37f0c1ae | 966 | } |
a64e88da JA |
967 | } |
968 | ||
89e5fad9 | 969 | static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd) |
a64e88da JA |
970 | { |
971 | struct group_run_stats *gs = (struct group_run_stats *) cmd->payload; | |
972 | ||
84f1f865 JA |
973 | if (output_format & FIO_OUTPUT_NORMAL) |
974 | show_group_stats(gs, NULL); | |
a64e88da JA |
975 | } |
976 | ||
3bf236c0 JA |
977 | static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd) |
978 | { | |
979 | struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload; | |
980 | const char *buf = (const char *) pdu->buf; | |
981 | const char *name; | |
982 | int fio_unused ret; | |
983 | ||
984 | name = client->name ? client->name : client->hostname; | |
985 | ||
986 | if (!client->skip_newline) | |
987 | fprintf(f_out, "<%s> ", name); | |
988 | ret = fwrite(buf, pdu->buf_len, 1, f_out); | |
989 | fflush(f_out); | |
990 | client->skip_newline = strchr(buf, '\n') == NULL; | |
991 | } | |
992 | ||
d09a64a0 JA |
993 | static void convert_agg(struct disk_util_agg *agg) |
994 | { | |
995 | int i; | |
996 | ||
997 | for (i = 0; i < 2; i++) { | |
504bc961 JA |
998 | agg->ios[i] = le64_to_cpu(agg->ios[i]); |
999 | agg->merges[i] = le64_to_cpu(agg->merges[i]); | |
d09a64a0 | 1000 | agg->sectors[i] = le64_to_cpu(agg->sectors[i]); |
504bc961 | 1001 | agg->ticks[i] = le64_to_cpu(agg->ticks[i]); |
d09a64a0 JA |
1002 | } |
1003 | ||
504bc961 JA |
1004 | agg->io_ticks = le64_to_cpu(agg->io_ticks); |
1005 | agg->time_in_queue = le64_to_cpu(agg->time_in_queue); | |
d09a64a0 | 1006 | agg->slavecount = le32_to_cpu(agg->slavecount); |
e5c9093d | 1007 | agg->max_util.u.f = fio_uint64_to_double(le64_to_cpu(agg->max_util.u.i)); |
d09a64a0 JA |
1008 | } |
1009 | ||
1010 | static void convert_dus(struct disk_util_stat *dus) | |
1011 | { | |
1012 | int i; | |
1013 | ||
1014 | for (i = 0; i < 2; i++) { | |
504bc961 JA |
1015 | dus->s.ios[i] = le64_to_cpu(dus->s.ios[i]); |
1016 | dus->s.merges[i] = le64_to_cpu(dus->s.merges[i]); | |
a3b4cf7d | 1017 | dus->s.sectors[i] = le64_to_cpu(dus->s.sectors[i]); |
504bc961 | 1018 | dus->s.ticks[i] = le64_to_cpu(dus->s.ticks[i]); |
d09a64a0 JA |
1019 | } |
1020 | ||
504bc961 JA |
1021 | dus->s.io_ticks = le64_to_cpu(dus->s.io_ticks); |
1022 | dus->s.time_in_queue = le64_to_cpu(dus->s.time_in_queue); | |
a3b4cf7d | 1023 | dus->s.msec = le64_to_cpu(dus->s.msec); |
d09a64a0 JA |
1024 | } |
1025 | ||
1026 | static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd) | |
1027 | { | |
1028 | struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload; | |
1029 | ||
d09a64a0 JA |
1030 | if (!client->disk_stats_shown) { |
1031 | client->disk_stats_shown = 1; | |
1032 | log_info("\nDisk stats (read/write):\n"); | |
1033 | } | |
1034 | ||
01cfefcc | 1035 | if (output_format & FIO_OUTPUT_JSON) { |
952b05e0 CF |
1036 | struct json_object *duobj; |
1037 | json_array_add_disk_util(&du->dus, &du->agg, du_array); | |
1038 | duobj = json_array_last_value_object(du_array); | |
1039 | json_object_add_client_info(duobj, client); | |
01cfefcc JA |
1040 | } |
1041 | if (output_format & FIO_OUTPUT_TERSE) | |
1042 | print_disk_util(&du->dus, &du->agg, 1, NULL); | |
1043 | if (output_format & FIO_OUTPUT_NORMAL) | |
1044 | print_disk_util(&du->dus, &du->agg, 0, NULL); | |
d09a64a0 JA |
1045 | } |
1046 | ||
3bf236c0 | 1047 | static void convert_jobs_eta(struct jobs_eta *je) |
cf451d1e | 1048 | { |
cf451d1e JA |
1049 | int i; |
1050 | ||
1051 | je->nr_running = le32_to_cpu(je->nr_running); | |
1052 | je->nr_ramp = le32_to_cpu(je->nr_ramp); | |
1053 | je->nr_pending = le32_to_cpu(je->nr_pending); | |
714e85f3 | 1054 | je->nr_setting_up = le32_to_cpu(je->nr_setting_up); |
cf451d1e | 1055 | je->files_open = le32_to_cpu(je->files_open); |
cf451d1e | 1056 | |
298921d6 JA |
1057 | for (i = 0; i < DDIR_RWDIR_CNT; i++) { |
1058 | je->m_rate[i] = le32_to_cpu(je->m_rate[i]); | |
1059 | je->t_rate[i] = le32_to_cpu(je->t_rate[i]); | |
1060 | je->m_iops[i] = le32_to_cpu(je->m_iops[i]); | |
1061 | je->t_iops[i] = le32_to_cpu(je->t_iops[i]); | |
16725bb0 JA |
1062 | je->rate[i] = le32_to_cpu(je->rate[i]); |
1063 | je->iops[i] = le32_to_cpu(je->iops[i]); | |
cf451d1e JA |
1064 | } |
1065 | ||
b51eedb7 | 1066 | je->elapsed_sec = le64_to_cpu(je->elapsed_sec); |
cf451d1e | 1067 | je->eta_sec = le64_to_cpu(je->eta_sec); |
8c621fb2 | 1068 | je->nr_threads = le32_to_cpu(je->nr_threads); |
b7f05eb0 | 1069 | je->is_pow2 = le32_to_cpu(je->is_pow2); |
ed2c0a12 | 1070 | je->unit_base = le32_to_cpu(je->unit_base); |
48fbb46e JA |
1071 | } |
1072 | ||
3e47bd25 | 1073 | void fio_client_sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je) |
48fbb46e | 1074 | { |
48fbb46e JA |
1075 | int i; |
1076 | ||
1077 | dst->nr_running += je->nr_running; | |
1078 | dst->nr_ramp += je->nr_ramp; | |
1079 | dst->nr_pending += je->nr_pending; | |
714e85f3 | 1080 | dst->nr_setting_up += je->nr_setting_up; |
48fbb46e | 1081 | dst->files_open += je->files_open; |
48fbb46e | 1082 | |
298921d6 | 1083 | for (i = 0; i < DDIR_RWDIR_CNT; i++) { |
3e47bd25 JA |
1084 | dst->m_rate[i] += je->m_rate[i]; |
1085 | dst->t_rate[i] += je->t_rate[i]; | |
1086 | dst->m_iops[i] += je->m_iops[i]; | |
1087 | dst->t_iops[i] += je->t_iops[i]; | |
16725bb0 JA |
1088 | dst->rate[i] += je->rate[i]; |
1089 | dst->iops[i] += je->iops[i]; | |
48fbb46e JA |
1090 | } |
1091 | ||
1092 | dst->elapsed_sec += je->elapsed_sec; | |
1093 | ||
1094 | if (je->eta_sec > dst->eta_sec) | |
1095 | dst->eta_sec = je->eta_sec; | |
8c621fb2 JA |
1096 | |
1097 | dst->nr_threads += je->nr_threads; | |
afe22d3d JA |
1098 | |
1099 | /* | |
1100 | * This wont be correct for multiple strings, but at least it | |
1101 | * works for the basic cases. | |
1102 | */ | |
1103 | strcpy((char *) dst->run_str, (char *) je->run_str); | |
48fbb46e JA |
1104 | } |
1105 | ||
89c1707c JA |
1106 | static void remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd) |
1107 | { | |
40c60516 | 1108 | struct fio_net_cmd_reply *reply = NULL; |
89c1707c JA |
1109 | struct flist_head *entry; |
1110 | ||
1111 | flist_for_each(entry, &client->cmd_list) { | |
40c60516 | 1112 | reply = flist_entry(entry, struct fio_net_cmd_reply, list); |
89c1707c | 1113 | |
40c60516 | 1114 | if (cmd->tag == (uintptr_t) reply) |
89c1707c JA |
1115 | break; |
1116 | ||
40c60516 | 1117 | reply = NULL; |
89c1707c JA |
1118 | } |
1119 | ||
40c60516 | 1120 | if (!reply) { |
4e0a8fa2 | 1121 | log_err("fio: client: unable to find matching tag (%llx)\n", (unsigned long long) cmd->tag); |
89c1707c JA |
1122 | return; |
1123 | } | |
1124 | ||
40c60516 JA |
1125 | flist_del(&reply->list); |
1126 | cmd->tag = reply->saved_tag; | |
1127 | free(reply); | |
1128 | } | |
1129 | ||
1130 | int fio_client_wait_for_reply(struct fio_client *client, uint64_t tag) | |
1131 | { | |
1132 | do { | |
1133 | struct fio_net_cmd_reply *reply = NULL; | |
1134 | struct flist_head *entry; | |
1135 | ||
1136 | flist_for_each(entry, &client->cmd_list) { | |
1137 | reply = flist_entry(entry, struct fio_net_cmd_reply, list); | |
1138 | ||
1139 | if (tag == (uintptr_t) reply) | |
1140 | break; | |
1141 | ||
1142 | reply = NULL; | |
1143 | } | |
1144 | ||
1145 | if (!reply) | |
1146 | break; | |
1147 | ||
1148 | usleep(1000); | |
1149 | } while (1); | |
1150 | ||
1151 | return 0; | |
89c1707c JA |
1152 | } |
1153 | ||
82c1ed38 | 1154 | static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd) |
48fbb46e JA |
1155 | { |
1156 | struct jobs_eta *je = (struct jobs_eta *) cmd->payload; | |
df380934 | 1157 | struct client_eta *eta = (struct client_eta *) (uintptr_t) cmd->tag; |
af9c9fb3 JA |
1158 | |
1159 | dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending); | |
cf451d1e | 1160 | |
f77d2676 JA |
1161 | assert(client->eta_in_flight == eta); |
1162 | ||
1163 | client->eta_in_flight = NULL; | |
82c1ed38 | 1164 | flist_del_init(&client->eta_list); |
a64c13a4 | 1165 | client->eta_timeouts = 0; |
82c1ed38 | 1166 | |
2f99deb0 JA |
1167 | if (client->ops->jobs_eta) |
1168 | client->ops->jobs_eta(client, je); | |
1169 | ||
3e47bd25 | 1170 | fio_client_sum_jobs_eta(&eta->eta, je); |
a5276616 | 1171 | fio_client_dec_jobs_eta(eta, client->ops->eta); |
cf451d1e JA |
1172 | } |
1173 | ||
b5296ddb | 1174 | static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd) |
2e03b4b2 | 1175 | { |
3989b143 | 1176 | struct cmd_probe_reply_pdu *probe = (struct cmd_probe_reply_pdu *) cmd->payload; |
d2333358 JA |
1177 | const char *os, *arch; |
1178 | char bit[16]; | |
2e03b4b2 | 1179 | |
cca84643 JA |
1180 | os = fio_get_os_string(probe->os); |
1181 | if (!os) | |
1182 | os = "unknown"; | |
1183 | ||
1184 | arch = fio_get_arch_string(probe->arch); | |
1185 | if (!arch) | |
1186 | os = "unknown"; | |
1187 | ||
d2333358 | 1188 | sprintf(bit, "%d-bit", probe->bpp * 8); |
3989b143 | 1189 | probe->flags = le64_to_cpu(probe->flags); |
38fdef22 | 1190 | |
3989b143 | 1191 | log_info("hostname=%s, be=%u, %s, os=%s, arch=%s, fio=%s, flags=%lx\n", |
38fdef22 | 1192 | probe->hostname, probe->bigendian, bit, os, arch, |
3989b143 | 1193 | probe->fio_version, (unsigned long) probe->flags); |
b5296ddb JA |
1194 | |
1195 | if (!client->name) | |
1196 | client->name = strdup((char *) probe->hostname); | |
2e03b4b2 JA |
1197 | } |
1198 | ||
11e950bd JA |
1199 | static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd) |
1200 | { | |
1201 | struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload; | |
1202 | ||
1203 | client->state = Client_started; | |
1204 | client->jobs = le32_to_cpu(pdu->jobs); | |
a89d5319 | 1205 | client->nr_stat = le32_to_cpu(pdu->stat_outputs); |
108fea77 | 1206 | |
a89d5319 | 1207 | sum_stat_clients += client->nr_stat; |
11e950bd JA |
1208 | } |
1209 | ||
1210 | static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd) | |
1211 | { | |
498c92c2 JA |
1212 | if (client->error) |
1213 | log_info("client <%s>: exited with error %d\n", client->hostname, client->error); | |
11e950bd JA |
1214 | } |
1215 | ||
6b79c80c JA |
1216 | static void convert_stop(struct fio_net_cmd *cmd) |
1217 | { | |
1218 | struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload; | |
1219 | ||
1220 | pdu->error = le32_to_cpu(pdu->error); | |
1221 | } | |
1222 | ||
084d1c6f JA |
1223 | static void convert_text(struct fio_net_cmd *cmd) |
1224 | { | |
1225 | struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload; | |
1226 | ||
1227 | pdu->level = le32_to_cpu(pdu->level); | |
1228 | pdu->buf_len = le32_to_cpu(pdu->buf_len); | |
1229 | pdu->log_sec = le64_to_cpu(pdu->log_sec); | |
1230 | pdu->log_usec = le64_to_cpu(pdu->log_usec); | |
1231 | } | |
1232 | ||
3989b143 JA |
1233 | static struct cmd_iolog_pdu *convert_iolog_gz(struct fio_net_cmd *cmd, |
1234 | struct cmd_iolog_pdu *pdu) | |
1b42725f | 1235 | { |
3989b143 | 1236 | #ifdef CONFIG_ZLIB |
1b42725f | 1237 | struct cmd_iolog_pdu *ret; |
1b42725f | 1238 | z_stream stream; |
3989b143 JA |
1239 | uint32_t nr_samples; |
1240 | size_t total; | |
1b42725f JA |
1241 | void *p; |
1242 | ||
1243 | stream.zalloc = Z_NULL; | |
1244 | stream.zfree = Z_NULL; | |
1245 | stream.opaque = Z_NULL; | |
1246 | stream.avail_in = 0; | |
1247 | stream.next_in = Z_NULL; | |
1248 | ||
1249 | if (inflateInit(&stream) != Z_OK) | |
1250 | return NULL; | |
1251 | ||
1252 | /* | |
f5ed765a | 1253 | * Get header first, it's not compressed |
1b42725f | 1254 | */ |
3c865f32 | 1255 | nr_samples = le64_to_cpu(pdu->nr_samples); |
1b42725f | 1256 | |
3c865f32 | 1257 | total = nr_samples * __log_entry_sz(le32_to_cpu(pdu->log_offset)); |
f5ed765a | 1258 | ret = malloc(total + sizeof(*pdu)); |
1b42725f | 1259 | ret->nr_samples = nr_samples; |
3989b143 JA |
1260 | |
1261 | memcpy(ret, pdu, sizeof(*pdu)); | |
f5ed765a JA |
1262 | |
1263 | p = (void *) ret + sizeof(*pdu); | |
1b42725f | 1264 | |
f5ed765a JA |
1265 | stream.avail_in = cmd->pdu_len - sizeof(*pdu); |
1266 | stream.next_in = (void *) pdu + sizeof(*pdu); | |
1b42725f JA |
1267 | while (stream.avail_in) { |
1268 | unsigned int this_chunk = 65536; | |
1269 | unsigned int this_len; | |
1270 | int err; | |
1271 | ||
1272 | if (this_chunk > total) | |
1273 | this_chunk = total; | |
1274 | ||
1275 | stream.avail_out = this_chunk; | |
1276 | stream.next_out = p; | |
1277 | err = inflate(&stream, Z_NO_FLUSH); | |
3c547fe0 JA |
1278 | /* may be Z_OK, or Z_STREAM_END */ |
1279 | if (err < 0) { | |
1b42725f | 1280 | log_err("fio: inflate error %d\n", err); |
f5ed765a JA |
1281 | free(ret); |
1282 | ret = NULL; | |
3989b143 | 1283 | goto err; |
1b42725f JA |
1284 | } |
1285 | ||
1286 | this_len = this_chunk - stream.avail_out; | |
1287 | p += this_len; | |
1288 | total -= this_len; | |
1289 | } | |
1290 | ||
3989b143 JA |
1291 | err: |
1292 | inflateEnd(&stream); | |
1293 | return ret; | |
1294 | #else | |
1295 | return NULL; | |
1296 | #endif | |
1297 | } | |
1298 | ||
1299 | /* | |
1300 | * This has been compressed on the server side, since it can be big. | |
1301 | * Uncompress here. | |
1302 | */ | |
1303 | static struct cmd_iolog_pdu *convert_iolog(struct fio_net_cmd *cmd) | |
1304 | { | |
1305 | struct cmd_iolog_pdu *pdu = (struct cmd_iolog_pdu *) cmd->payload; | |
1306 | struct cmd_iolog_pdu *ret; | |
ae588852 JA |
1307 | uint64_t i; |
1308 | void *samples; | |
3989b143 JA |
1309 | |
1310 | /* | |
1311 | * Convert if compressed and we support it. If it's not | |
1312 | * compressed, we need not do anything. | |
1313 | */ | |
1314 | if (le32_to_cpu(pdu->compressed)) { | |
1315 | #ifndef CONFIG_ZLIB | |
1316 | log_err("fio: server sent compressed data by mistake\n"); | |
1317 | return NULL; | |
1318 | #endif | |
1319 | ret = convert_iolog_gz(cmd, pdu); | |
1320 | if (!ret) { | |
1321 | log_err("fio: failed decompressing log\n"); | |
1322 | return NULL; | |
1323 | } | |
1324 | } else | |
1325 | ret = pdu; | |
1326 | ||
ae588852 | 1327 | ret->nr_samples = le64_to_cpu(ret->nr_samples); |
3989b143 | 1328 | ret->thread_number = le32_to_cpu(ret->thread_number); |
3989b143 JA |
1329 | ret->log_type = le32_to_cpu(ret->log_type); |
1330 | ret->compressed = le32_to_cpu(ret->compressed); | |
ae588852 | 1331 | ret->log_offset = le32_to_cpu(ret->log_offset); |
3989b143 | 1332 | |
3310fced | 1333 | samples = &ret->samples[0]; |
f5ed765a | 1334 | for (i = 0; i < ret->nr_samples; i++) { |
ae588852 | 1335 | struct io_sample *s; |
f5ed765a | 1336 | |
ae588852 | 1337 | s = __get_sample(samples, ret->log_offset, i); |
b26317c9 JA |
1338 | s->time = le64_to_cpu(s->time); |
1339 | s->val = le64_to_cpu(s->val); | |
1340 | s->__ddir = le32_to_cpu(s->__ddir); | |
1341 | s->bs = le32_to_cpu(s->bs); | |
ae588852 JA |
1342 | |
1343 | if (ret->log_offset) { | |
1344 | struct io_sample_offset *so = (void *) s; | |
1345 | ||
1346 | so->offset = le64_to_cpu(so->offset); | |
1347 | } | |
f5ed765a JA |
1348 | } |
1349 | ||
1b42725f JA |
1350 | return ret; |
1351 | } | |
1352 | ||
ca09be4b JA |
1353 | static void sendfile_reply(int fd, struct cmd_sendfile_reply *rep, |
1354 | size_t size, uint64_t tag) | |
1355 | { | |
1356 | rep->error = cpu_to_le32(rep->error); | |
1357 | fio_net_send_cmd(fd, FIO_NET_CMD_SENDFILE, rep, size, &tag, NULL); | |
1358 | } | |
1359 | ||
ca09be4b JA |
1360 | static int send_file(struct fio_client *client, struct cmd_sendfile *pdu, |
1361 | uint64_t tag) | |
1362 | { | |
1363 | struct cmd_sendfile_reply *rep; | |
1364 | struct stat sb; | |
1365 | size_t size; | |
1366 | int fd; | |
1367 | ||
1368 | size = sizeof(*rep); | |
1369 | rep = malloc(size); | |
1370 | ||
1371 | if (stat((char *)pdu->path, &sb) < 0) { | |
1372 | fail: | |
1373 | rep->error = errno; | |
1374 | sendfile_reply(client->fd, rep, size, tag); | |
1375 | free(rep); | |
1376 | return 1; | |
1377 | } | |
1378 | ||
1379 | size += sb.st_size; | |
1380 | rep = realloc(rep, size); | |
1381 | rep->size = cpu_to_le32((uint32_t) sb.st_size); | |
1382 | ||
1383 | fd = open((char *)pdu->path, O_RDONLY); | |
1384 | if (fd == -1 ) | |
1385 | goto fail; | |
1386 | ||
1387 | rep->error = read_data(fd, &rep->data, sb.st_size); | |
1388 | sendfile_reply(client->fd, rep, size, tag); | |
1389 | free(rep); | |
1390 | close(fd); | |
1391 | return 0; | |
1392 | } | |
1393 | ||
a5276616 | 1394 | int fio_handle_client(struct fio_client *client) |
37db14fe | 1395 | { |
a5276616 | 1396 | struct client_ops *ops = client->ops; |
37db14fe JA |
1397 | struct fio_net_cmd *cmd; |
1398 | ||
60efd14e JA |
1399 | dprint(FD_NET, "client: handle %s\n", client->hostname); |
1400 | ||
e951bdc4 JA |
1401 | cmd = fio_net_recv_cmd(client->fd); |
1402 | if (!cmd) | |
1403 | return 0; | |
c2c94585 | 1404 | |
b9d2f30a JA |
1405 | dprint(FD_NET, "client: got cmd op %s from %s (pdu=%u)\n", |
1406 | fio_server_op(cmd->opcode), client->hostname, cmd->pdu_len); | |
46c48f1f | 1407 | |
e951bdc4 JA |
1408 | switch (cmd->opcode) { |
1409 | case FIO_NET_CMD_QUIT: | |
3ec62ec4 | 1410 | if (ops->quit) |
35c0ba7f | 1411 | ops->quit(client, cmd); |
e951bdc4 | 1412 | remove_client(client); |
e951bdc4 | 1413 | break; |
084d1c6f JA |
1414 | case FIO_NET_CMD_TEXT: |
1415 | convert_text(cmd); | |
35c0ba7f | 1416 | ops->text(client, cmd); |
e951bdc4 | 1417 | break; |
3bf236c0 JA |
1418 | case FIO_NET_CMD_DU: { |
1419 | struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload; | |
1420 | ||
1421 | convert_dus(&du->dus); | |
1422 | convert_agg(&du->agg); | |
1423 | ||
dd366728 | 1424 | ops->disk_util(client, cmd); |
d09a64a0 | 1425 | break; |
3bf236c0 JA |
1426 | } |
1427 | case FIO_NET_CMD_TS: { | |
1428 | struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload; | |
1429 | ||
1430 | convert_ts(&p->ts, &p->ts); | |
1431 | convert_gs(&p->rs, &p->rs); | |
1432 | ||
89e5fad9 | 1433 | ops->thread_status(client, cmd); |
e951bdc4 | 1434 | break; |
3bf236c0 JA |
1435 | } |
1436 | case FIO_NET_CMD_GS: { | |
1437 | struct group_run_stats *gs = (struct group_run_stats *) cmd->payload; | |
1438 | ||
1439 | convert_gs(gs, gs); | |
1440 | ||
89e5fad9 | 1441 | ops->group_stats(client, cmd); |
e951bdc4 | 1442 | break; |
3bf236c0 JA |
1443 | } |
1444 | case FIO_NET_CMD_ETA: { | |
1445 | struct jobs_eta *je = (struct jobs_eta *) cmd->payload; | |
1446 | ||
89c1707c | 1447 | remove_reply_cmd(client, cmd); |
3bf236c0 | 1448 | convert_jobs_eta(je); |
a5276616 | 1449 | handle_eta(client, cmd); |
e951bdc4 | 1450 | break; |
3bf236c0 | 1451 | } |
e951bdc4 | 1452 | case FIO_NET_CMD_PROBE: |
89c1707c | 1453 | remove_reply_cmd(client, cmd); |
dd366728 | 1454 | ops->probe(client, cmd); |
e951bdc4 | 1455 | break; |
5d7793aa | 1456 | case FIO_NET_CMD_SERVER_START: |
01be038e | 1457 | client->state = Client_running; |
85dd01e7 JA |
1458 | if (ops->job_start) |
1459 | ops->job_start(client, cmd); | |
01be038e | 1460 | break; |
85dd01e7 JA |
1461 | case FIO_NET_CMD_START: { |
1462 | struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload; | |
1463 | ||
1464 | pdu->jobs = le32_to_cpu(pdu->jobs); | |
1465 | ops->start(client, cmd); | |
e951bdc4 | 1466 | break; |
85dd01e7 | 1467 | } |
6b79c80c JA |
1468 | case FIO_NET_CMD_STOP: { |
1469 | struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload; | |
1470 | ||
1471 | convert_stop(cmd); | |
1472 | client->state = Client_stopped; | |
122c7725 JA |
1473 | client->error = le32_to_cpu(pdu->error); |
1474 | client->signal = le32_to_cpu(pdu->signal); | |
6b79c80c | 1475 | ops->stop(client, cmd); |
e951bdc4 | 1476 | break; |
6b79c80c | 1477 | } |
40c60516 JA |
1478 | case FIO_NET_CMD_ADD_JOB: { |
1479 | struct cmd_add_job_pdu *pdu = (struct cmd_add_job_pdu *) cmd->payload; | |
1480 | ||
1481 | client->thread_number = le32_to_cpu(pdu->thread_number); | |
1482 | client->groupid = le32_to_cpu(pdu->groupid); | |
1483 | ||
807f9971 JA |
1484 | if (ops->add_job) |
1485 | ops->add_job(client, cmd); | |
807f9971 | 1486 | break; |
40c60516 | 1487 | } |
1b42725f JA |
1488 | case FIO_NET_CMD_IOLOG: |
1489 | if (ops->iolog) { | |
1490 | struct cmd_iolog_pdu *pdu; | |
1491 | ||
1492 | pdu = convert_iolog(cmd); | |
1493 | ops->iolog(client, pdu); | |
1494 | } | |
1b42725f | 1495 | break; |
40c60516 | 1496 | case FIO_NET_CMD_UPDATE_JOB: |
40c60516 | 1497 | ops->update_job(client, cmd); |
649cee91 | 1498 | remove_reply_cmd(client, cmd); |
40c60516 | 1499 | break; |
ca09be4b JA |
1500 | case FIO_NET_CMD_VTRIGGER: { |
1501 | struct all_io_list *pdu = (struct all_io_list *) cmd->payload; | |
d264264a JA |
1502 | char buf[128]; |
1503 | int off = 0; | |
ca09be4b | 1504 | |
d264264a JA |
1505 | if (aux_path) { |
1506 | strcpy(buf, aux_path); | |
1507 | off = strlen(buf); | |
1508 | } | |
1509 | ||
1510 | __verify_save_state(pdu, server_name(client, &buf[off], sizeof(buf) - off)); | |
13c70218 | 1511 | exec_trigger(trigger_cmd); |
ca09be4b JA |
1512 | break; |
1513 | } | |
1514 | case FIO_NET_CMD_SENDFILE: { | |
1515 | struct cmd_sendfile *pdu = (struct cmd_sendfile *) cmd->payload; | |
1516 | send_file(client, pdu, cmd->tag); | |
1517 | break; | |
1518 | } | |
e951bdc4 | 1519 | default: |
89c1707c | 1520 | log_err("fio: unknown client op: %s\n", fio_server_op(cmd->opcode)); |
e951bdc4 | 1521 | break; |
37db14fe JA |
1522 | } |
1523 | ||
ca09be4b | 1524 | free(cmd); |
e951bdc4 | 1525 | return 1; |
37db14fe | 1526 | } |
b66570dc | 1527 | |
ca09be4b JA |
1528 | int fio_clients_send_trigger(const char *cmd) |
1529 | { | |
1530 | struct flist_head *entry; | |
1531 | struct fio_client *client; | |
1532 | size_t slen; | |
1533 | ||
1534 | dprint(FD_NET, "client: send vtrigger: %s\n", cmd); | |
1535 | ||
1536 | if (!cmd) | |
1537 | slen = 0; | |
1538 | else | |
1539 | slen = strlen(cmd); | |
1540 | ||
1541 | flist_for_each(entry, &client_list) { | |
1542 | struct cmd_vtrigger_pdu *pdu; | |
1543 | ||
1544 | client = flist_entry(entry, struct fio_client, list); | |
1545 | ||
1546 | pdu = malloc(sizeof(*pdu) + slen); | |
1547 | pdu->len = cpu_to_le16((uint16_t) slen); | |
1548 | if (slen) | |
1549 | memcpy(pdu->cmd, cmd, slen); | |
1550 | fio_net_send_cmd(client->fd, FIO_NET_CMD_VTRIGGER, pdu, | |
1551 | sizeof(*pdu) + slen, NULL, NULL); | |
1552 | free(pdu); | |
1553 | } | |
1554 | ||
1555 | return 0; | |
1556 | } | |
1557 | ||
a5276616 | 1558 | static void request_client_etas(struct client_ops *ops) |
af9c9fb3 JA |
1559 | { |
1560 | struct fio_client *client; | |
1561 | struct flist_head *entry; | |
1562 | struct client_eta *eta; | |
82c1ed38 | 1563 | int skipped = 0; |
af9c9fb3 JA |
1564 | |
1565 | dprint(FD_NET, "client: request eta (%d)\n", nr_clients); | |
1566 | ||
afe22d3d | 1567 | eta = calloc(1, sizeof(*eta) + __THREAD_RUNSTR_SZ(REAL_MAX_JOBS)); |
af9c9fb3 JA |
1568 | eta->pending = nr_clients; |
1569 | ||
1570 | flist_for_each(entry, &client_list) { | |
1571 | client = flist_entry(entry, struct fio_client, list); | |
1572 | ||
82c1ed38 JA |
1573 | if (!flist_empty(&client->eta_list)) { |
1574 | skipped++; | |
1575 | continue; | |
1576 | } | |
01be038e JA |
1577 | if (client->state != Client_running) |
1578 | continue; | |
82c1ed38 | 1579 | |
f77d2676 | 1580 | assert(!client->eta_in_flight); |
82c1ed38 | 1581 | flist_add_tail(&client->eta_list, &eta_list); |
f77d2676 | 1582 | client->eta_in_flight = eta; |
af9c9fb3 | 1583 | fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA, |
df380934 | 1584 | (uintptr_t) eta, &client->cmd_list); |
af9c9fb3 JA |
1585 | } |
1586 | ||
f02f7dac JA |
1587 | while (skipped--) { |
1588 | if (!fio_client_dec_jobs_eta(eta, ops->eta)) | |
1589 | break; | |
1590 | } | |
82c1ed38 | 1591 | |
af9c9fb3 JA |
1592 | dprint(FD_NET, "client: requested eta tag %p\n", eta); |
1593 | } | |
1594 | ||
a64c13a4 JA |
1595 | /* |
1596 | * A single SEND_ETA timeout isn't fatal. Attempt to recover. | |
1597 | */ | |
1598 | static int handle_cmd_timeout(struct fio_client *client, | |
1599 | struct fio_net_cmd_reply *reply) | |
1600 | { | |
1601 | if (reply->opcode != FIO_NET_CMD_SEND_ETA) | |
1602 | return 1; | |
1603 | ||
1604 | log_info("client <%s>: timeout on SEND_ETA\n", client->hostname); | |
1605 | flist_del(&reply->list); | |
1606 | free(reply); | |
1607 | ||
1608 | flist_del_init(&client->eta_list); | |
1609 | if (client->eta_in_flight) { | |
1610 | fio_client_dec_jobs_eta(client->eta_in_flight, client->ops->eta); | |
1611 | client->eta_in_flight = NULL; | |
1612 | } | |
1613 | ||
1614 | /* | |
1615 | * If we fail 5 in a row, give up... | |
1616 | */ | |
1617 | if (client->eta_timeouts++ > 5) | |
1618 | return 1; | |
1619 | ||
1620 | return 0; | |
1621 | } | |
1622 | ||
89c1707c JA |
1623 | static int client_check_cmd_timeout(struct fio_client *client, |
1624 | struct timeval *now) | |
1625 | { | |
40c60516 | 1626 | struct fio_net_cmd_reply *reply; |
89c1707c JA |
1627 | struct flist_head *entry, *tmp; |
1628 | int ret = 0; | |
1629 | ||
1630 | flist_for_each_safe(entry, tmp, &client->cmd_list) { | |
40c60516 | 1631 | reply = flist_entry(entry, struct fio_net_cmd_reply, list); |
89c1707c | 1632 | |
40c60516 | 1633 | if (mtime_since(&reply->tv, now) < FIO_NET_CLIENT_TIMEOUT) |
89c1707c JA |
1634 | continue; |
1635 | ||
a64c13a4 JA |
1636 | if (!handle_cmd_timeout(client, reply)) |
1637 | continue; | |
1638 | ||
89c1707c | 1639 | log_err("fio: client %s, timeout on cmd %s\n", client->hostname, |
40c60516 JA |
1640 | fio_server_op(reply->opcode)); |
1641 | flist_del(&reply->list); | |
1642 | free(reply); | |
89c1707c JA |
1643 | ret = 1; |
1644 | } | |
1645 | ||
1646 | return flist_empty(&client->cmd_list) && ret; | |
1647 | } | |
1648 | ||
a5276616 | 1649 | static int fio_check_clients_timed_out(void) |
89c1707c JA |
1650 | { |
1651 | struct fio_client *client; | |
1652 | struct flist_head *entry, *tmp; | |
1653 | struct timeval tv; | |
1654 | int ret = 0; | |
1655 | ||
67bf9823 | 1656 | fio_gettime(&tv, NULL); |
89c1707c JA |
1657 | |
1658 | flist_for_each_safe(entry, tmp, &client_list) { | |
1659 | client = flist_entry(entry, struct fio_client, list); | |
1660 | ||
1661 | if (flist_empty(&client->cmd_list)) | |
1662 | continue; | |
1663 | ||
1664 | if (!client_check_cmd_timeout(client, &tv)) | |
1665 | continue; | |
1666 | ||
a5276616 JA |
1667 | if (client->ops->timed_out) |
1668 | client->ops->timed_out(client); | |
ed727a46 JA |
1669 | else |
1670 | log_err("fio: client %s timed out\n", client->hostname); | |
1671 | ||
f9a33cc0 | 1672 | client->error = ETIMEDOUT; |
89c1707c JA |
1673 | remove_client(client); |
1674 | ret = 1; | |
1675 | } | |
1676 | ||
1677 | return ret; | |
1678 | } | |
1679 | ||
dd366728 | 1680 | int fio_handle_clients(struct client_ops *ops) |
b66570dc | 1681 | { |
b66570dc | 1682 | struct pollfd *pfds; |
498c92c2 | 1683 | int i, ret = 0, retval = 0; |
b66570dc | 1684 | |
67bf9823 | 1685 | fio_gettime(&eta_tv, NULL); |
af9c9fb3 | 1686 | |
b66570dc JA |
1687 | pfds = malloc(nr_clients * sizeof(struct pollfd)); |
1688 | ||
37f0c1ae JA |
1689 | init_thread_stat(&client_ts); |
1690 | init_group_run_stat(&client_gs); | |
1691 | ||
82a4be1b | 1692 | while (!exit_backend && nr_clients) { |
c2cb6869 JA |
1693 | struct flist_head *entry, *tmp; |
1694 | struct fio_client *client; | |
1695 | ||
82a4be1b | 1696 | i = 0; |
c2cb6869 | 1697 | flist_for_each_safe(entry, tmp, &client_list) { |
82a4be1b | 1698 | client = flist_entry(entry, struct fio_client, list); |
b66570dc | 1699 | |
a5276616 | 1700 | if (!client->sent_job && !client->ops->stay_connected && |
c2cb6869 JA |
1701 | flist_empty(&client->cmd_list)) { |
1702 | remove_client(client); | |
1703 | continue; | |
1704 | } | |
1705 | ||
82a4be1b JA |
1706 | pfds[i].fd = client->fd; |
1707 | pfds[i].events = POLLIN; | |
1708 | i++; | |
1709 | } | |
1710 | ||
c2cb6869 JA |
1711 | if (!nr_clients) |
1712 | break; | |
1713 | ||
82a4be1b | 1714 | assert(i == nr_clients); |
b66570dc | 1715 | |
5c2857f9 | 1716 | do { |
af9c9fb3 | 1717 | struct timeval tv; |
ca09be4b | 1718 | int timeout; |
af9c9fb3 | 1719 | |
67bf9823 | 1720 | fio_gettime(&tv, NULL); |
af9c9fb3 | 1721 | if (mtime_since(&eta_tv, &tv) >= 900) { |
a5276616 | 1722 | request_client_etas(ops); |
af9c9fb3 | 1723 | memcpy(&eta_tv, &tv, sizeof(tv)); |
89c1707c | 1724 | |
a5276616 | 1725 | if (fio_check_clients_timed_out()) |
89c1707c | 1726 | break; |
af9c9fb3 JA |
1727 | } |
1728 | ||
ca09be4b JA |
1729 | check_trigger_file(); |
1730 | ||
1731 | timeout = min(100u, ops->eta_msec); | |
1732 | ||
1733 | ret = poll(pfds, nr_clients, timeout); | |
5c2857f9 JA |
1734 | if (ret < 0) { |
1735 | if (errno == EINTR) | |
1736 | continue; | |
1737 | log_err("fio: poll clients: %s\n", strerror(errno)); | |
1738 | break; | |
1739 | } else if (!ret) | |
b66570dc | 1740 | continue; |
5c2857f9 | 1741 | } while (ret <= 0); |
b66570dc JA |
1742 | |
1743 | for (i = 0; i < nr_clients; i++) { | |
1744 | if (!(pfds[i].revents & POLLIN)) | |
1745 | continue; | |
1746 | ||
1747 | client = find_client_by_fd(pfds[i].fd); | |
1748 | if (!client) { | |
65e1a120 | 1749 | log_err("fio: unknown client fd %ld\n", (long) pfds[i].fd); |
b66570dc JA |
1750 | continue; |
1751 | } | |
a5276616 | 1752 | if (!fio_handle_client(client)) { |
28d3ab07 JA |
1753 | log_info("client: host=%s disconnected\n", |
1754 | client->hostname); | |
1755 | remove_client(client); | |
498c92c2 | 1756 | retval = 1; |
38990764 | 1757 | } else if (client->error) |
498c92c2 | 1758 | retval = 1; |
343cb4a9 | 1759 | fio_put_client(client); |
b66570dc JA |
1760 | } |
1761 | } | |
1762 | ||
952b05e0 CF |
1763 | fio_client_json_fini(); |
1764 | ||
b66570dc | 1765 | free(pfds); |
f9a33cc0 | 1766 | return retval || error_clients; |
b66570dc | 1767 | } |