Commit | Line | Data |
---|---|---|
132159a5 JA |
1 | #include <stdio.h> |
2 | #include <stdlib.h> | |
3 | #include <unistd.h> | |
132159a5 JA |
4 | #include <errno.h> |
5 | #include <fcntl.h> | |
8393ca93 | 6 | #include <poll.h> |
132159a5 JA |
7 | #include <sys/types.h> |
8 | #include <sys/stat.h> | |
d05c4a03 | 9 | #include <sys/socket.h> |
87aa8f19 | 10 | #include <sys/un.h> |
132159a5 JA |
11 | #include <netinet/in.h> |
12 | #include <arpa/inet.h> | |
13 | #include <netdb.h> | |
9e22ecb0 | 14 | #include <signal.h> |
3989b143 | 15 | #ifdef CONFIG_ZLIB |
1b42725f | 16 | #include <zlib.h> |
3989b143 | 17 | #endif |
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" |
3d2d14bc | 24 | #include "verify-state.h" |
132159a5 | 25 | |
dd366728 | 26 | static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd); |
89e5fad9 JA |
27 | static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd); |
28 | static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd); | |
dd366728 | 29 | static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd); |
084d1c6f | 30 | static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd); |
36ed2f9a | 31 | static void handle_stop(struct fio_client *client); |
85dd01e7 | 32 | static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd); |
dd366728 | 33 | |
54e62ad4 | 34 | static void convert_text(struct fio_net_cmd *cmd); |
44f4f5e6 | 35 | static void client_display_thread_status(struct jobs_eta *je); |
54e62ad4 | 36 | |
dd366728 | 37 | struct client_ops fio_client_ops = { |
35c0ba7f | 38 | .text = handle_text, |
0420ba6a JA |
39 | .disk_util = handle_du, |
40 | .thread_status = handle_ts, | |
41 | .group_stats = handle_gs, | |
6b79c80c | 42 | .stop = handle_stop, |
85dd01e7 | 43 | .start = handle_start, |
44f4f5e6 | 44 | .eta = client_display_thread_status, |
0420ba6a | 45 | .probe = handle_probe, |
6433ee05 | 46 | .eta_msec = FIO_CLIENT_DEF_ETA_MSEC, |
46bcd498 | 47 | .client_type = FIO_CLIENT_TYPE_CLI, |
dd366728 SC |
48 | }; |
49 | ||
8b6a404c | 50 | static struct timespec eta_ts; |
48fbb46e | 51 | |
b66570dc | 52 | static FLIST_HEAD(client_list); |
82c1ed38 | 53 | static FLIST_HEAD(eta_list); |
b66570dc | 54 | |
3f3a4542 JA |
55 | static FLIST_HEAD(arg_list); |
56 | ||
3650a3ca JA |
57 | struct thread_stat client_ts; |
58 | struct group_run_stats client_gs; | |
59 | int sum_stat_clients; | |
60 | ||
37f0c1ae | 61 | static int sum_stat_nr; |
c2b5ddb2 | 62 | static struct buf_output allclients; |
952b05e0 | 63 | static struct json_object *root = NULL; |
0279b880 | 64 | static struct json_object *job_opt_object = NULL; |
952b05e0 CF |
65 | static struct json_array *clients_array = NULL; |
66 | static struct json_array *du_array = NULL; | |
37f0c1ae | 67 | |
f9a33cc0 JA |
68 | static int error_clients; |
69 | ||
3c5f57e3 JA |
70 | #define FIO_CLIENT_HASH_BITS 7 |
71 | #define FIO_CLIENT_HASH_SZ (1 << FIO_CLIENT_HASH_BITS) | |
72 | #define FIO_CLIENT_HASH_MASK (FIO_CLIENT_HASH_SZ - 1) | |
bebe6398 | 73 | static struct flist_head client_hash[FIO_CLIENT_HASH_SZ]; |
3c5f57e3 | 74 | |
0cba0f91 JA |
75 | static struct cmd_iolog_pdu *convert_iolog(struct fio_net_cmd *, bool *); |
76 | ||
bebe6398 | 77 | static void fio_client_add_hash(struct fio_client *client) |
3c5f57e3 JA |
78 | { |
79 | int bucket = hash_long(client->fd, FIO_CLIENT_HASH_BITS); | |
80 | ||
81 | bucket &= FIO_CLIENT_HASH_MASK; | |
bebe6398 | 82 | flist_add(&client->hash_list, &client_hash[bucket]); |
3c5f57e3 JA |
83 | } |
84 | ||
bebe6398 | 85 | static void fio_client_remove_hash(struct fio_client *client) |
3c5f57e3 | 86 | { |
bebe6398 JA |
87 | if (!flist_empty(&client->hash_list)) |
88 | flist_del_init(&client->hash_list); | |
3c5f57e3 JA |
89 | } |
90 | ||
91 | static void fio_init fio_client_hash_init(void) | |
92 | { | |
93 | int i; | |
94 | ||
bebe6398 JA |
95 | for (i = 0; i < FIO_CLIENT_HASH_SZ; i++) |
96 | INIT_FLIST_HEAD(&client_hash[i]); | |
3c5f57e3 JA |
97 | } |
98 | ||
e5aa68b3 JA |
99 | static int read_data(int fd, void *data, size_t size) |
100 | { | |
101 | ssize_t ret; | |
102 | ||
103 | while (size) { | |
104 | ret = read(fd, data, size); | |
105 | if (ret < 0) { | |
106 | if (errno == EAGAIN || errno == EINTR) | |
107 | continue; | |
108 | break; | |
109 | } else if (!ret) | |
110 | break; | |
111 | else { | |
112 | data += ret; | |
113 | size -= ret; | |
114 | } | |
115 | } | |
116 | ||
117 | if (size) | |
118 | return EAGAIN; | |
119 | ||
120 | return 0; | |
121 | } | |
122 | ||
b4f5e72f TK |
123 | static int read_ini_data(int fd, void *data, size_t size) |
124 | { | |
125 | char *p = data; | |
126 | int ret = 0; | |
127 | FILE *fp; | |
ec9e1334 | 128 | int dupfd; |
b4f5e72f | 129 | |
ec9e1334 JA |
130 | dupfd = dup(fd); |
131 | if (dupfd < 0) | |
132 | return errno; | |
133 | ||
134 | fp = fdopen(dupfd, "r"); | |
af84cd66 JA |
135 | if (!fp) { |
136 | ret = errno; | |
137 | close(dupfd); | |
138 | goto out; | |
139 | } | |
b4f5e72f TK |
140 | |
141 | while (1) { | |
142 | ssize_t len; | |
143 | char buf[OPT_LEN_MAX+1], *sub; | |
144 | ||
145 | if (!fgets(buf, sizeof(buf), fp)) { | |
146 | if (ferror(fp)) { | |
147 | if (errno == EAGAIN || errno == EINTR) | |
148 | continue; | |
149 | ret = errno; | |
150 | } | |
151 | break; | |
152 | } | |
153 | ||
154 | sub = fio_option_dup_subs(buf); | |
155 | len = strlen(sub); | |
156 | if (len + 1 > size) { | |
157 | log_err("fio: no space left to read data\n"); | |
158 | free(sub); | |
159 | ret = ENOSPC; | |
160 | break; | |
161 | } | |
162 | ||
163 | memcpy(p, sub, len); | |
164 | free(sub); | |
165 | p += len; | |
166 | *p = '\0'; | |
167 | size -= len; | |
168 | } | |
169 | ||
170 | fclose(fp); | |
af84cd66 | 171 | out: |
b4f5e72f TK |
172 | return ret; |
173 | } | |
174 | ||
952b05e0 CF |
175 | static void fio_client_json_init(void) |
176 | { | |
0279b880 JA |
177 | char time_buf[32]; |
178 | time_t time_p; | |
179 | ||
01cfefcc | 180 | if (!(output_format & FIO_OUTPUT_JSON)) |
952b05e0 | 181 | return; |
84f1f865 | 182 | |
0279b880 JA |
183 | time(&time_p); |
184 | os_ctime_r((const time_t *) &time_p, time_buf, sizeof(time_buf)); | |
185 | time_buf[strlen(time_buf) - 1] = '\0'; | |
186 | ||
952b05e0 CF |
187 | root = json_create_object(); |
188 | json_object_add_value_string(root, "fio version", fio_version_string); | |
0279b880 JA |
189 | json_object_add_value_int(root, "timestamp", time_p); |
190 | json_object_add_value_string(root, "time", time_buf); | |
191 | ||
192 | job_opt_object = json_create_object(); | |
193 | json_object_add_value_object(root, "global options", job_opt_object); | |
952b05e0 CF |
194 | clients_array = json_create_array(); |
195 | json_object_add_value_array(root, "client_stats", clients_array); | |
196 | du_array = json_create_array(); | |
197 | json_object_add_value_array(root, "disk_util", du_array); | |
198 | } | |
199 | ||
200 | static void fio_client_json_fini(void) | |
201 | { | |
83742c72 JA |
202 | struct buf_output out; |
203 | ||
2eca4892 | 204 | if (!root) |
952b05e0 | 205 | return; |
0279b880 | 206 | |
83742c72 | 207 | buf_output_init(&out); |
2eca4892 | 208 | |
83742c72 JA |
209 | __log_buf(&out, "\n"); |
210 | json_print_object(root, &out); | |
211 | __log_buf(&out, "\n"); | |
212 | log_info_buf(out.buf, out.buflen); | |
2eca4892 | 213 | |
83742c72 | 214 | buf_output_free(&out); |
2eca4892 | 215 | |
952b05e0 CF |
216 | json_free_object(root); |
217 | root = NULL; | |
2eca4892 | 218 | job_opt_object = NULL; |
952b05e0 CF |
219 | clients_array = NULL; |
220 | du_array = NULL; | |
221 | } | |
222 | ||
b66570dc JA |
223 | static struct fio_client *find_client_by_fd(int fd) |
224 | { | |
3c5f57e3 | 225 | int bucket = hash_long(fd, FIO_CLIENT_HASH_BITS) & FIO_CLIENT_HASH_MASK; |
b66570dc JA |
226 | struct fio_client *client; |
227 | struct flist_head *entry; | |
228 | ||
bebe6398 JA |
229 | flist_for_each(entry, &client_hash[bucket]) { |
230 | client = flist_entry(entry, struct fio_client, hash_list); | |
b66570dc | 231 | |
e55f8f30 JA |
232 | if (client->fd == fd) { |
233 | client->refs++; | |
b66570dc | 234 | return client; |
e55f8f30 | 235 | } |
b66570dc JA |
236 | } |
237 | ||
238 | return NULL; | |
239 | } | |
240 | ||
3af45201 | 241 | void fio_put_client(struct fio_client *client) |
b66570dc | 242 | { |
5121a9aa JA |
243 | if (--client->refs) |
244 | return; | |
245 | ||
7c6686fc JA |
246 | log_info_buf(client->buf.buf, client->buf.buflen); |
247 | buf_output_free(&client->buf); | |
248 | ||
3af45201 JA |
249 | free(client->hostname); |
250 | if (client->argv) | |
251 | free(client->argv); | |
252 | if (client->name) | |
253 | free(client->name); | |
323255cc JA |
254 | while (client->nr_files) { |
255 | struct client_file *cf = &client->files[--client->nr_files]; | |
256 | ||
257 | free(cf->file); | |
258 | } | |
259 | if (client->files) | |
260 | free(client->files); | |
0279b880 JA |
261 | if (client->opt_lists) |
262 | free(client->opt_lists); | |
3af45201 | 263 | |
108fea77 | 264 | if (!client->did_stat) |
7b38411c | 265 | sum_stat_clients--; |
108fea77 | 266 | |
f9a33cc0 JA |
267 | if (client->error) |
268 | error_clients++; | |
269 | ||
3af45201 JA |
270 | free(client); |
271 | } | |
272 | ||
f02f7dac JA |
273 | static int fio_client_dec_jobs_eta(struct client_eta *eta, client_eta_op eta_fn) |
274 | { | |
275 | if (!--eta->pending) { | |
276 | eta_fn(&eta->eta); | |
277 | free(eta); | |
278 | return 0; | |
279 | } | |
280 | ||
281 | return 1; | |
282 | } | |
283 | ||
54e62ad4 JA |
284 | static void fio_drain_client_text(struct fio_client *client) |
285 | { | |
286 | do { | |
287 | struct fio_net_cmd *cmd; | |
288 | ||
289 | cmd = fio_net_recv_cmd(client->fd, false); | |
290 | if (!cmd) | |
291 | break; | |
292 | ||
2baadb02 JA |
293 | if (cmd->opcode == FIO_NET_CMD_TEXT) { |
294 | convert_text(cmd); | |
295 | client->ops->text(client, cmd); | |
54e62ad4 JA |
296 | } |
297 | ||
2baadb02 | 298 | free(cmd); |
54e62ad4 JA |
299 | } while (1); |
300 | } | |
301 | ||
3af45201 JA |
302 | static void remove_client(struct fio_client *client) |
303 | { | |
304 | assert(client->refs); | |
305 | ||
39e8e016 | 306 | dprint(FD_NET, "client: removed <%s>\n", client->hostname); |
3af45201 | 307 | |
54e62ad4 JA |
308 | fio_drain_client_text(client); |
309 | ||
3af45201 JA |
310 | if (!flist_empty(&client->list)) |
311 | flist_del_init(&client->list); | |
3c5f57e3 | 312 | |
bebe6398 | 313 | fio_client_remove_hash(client); |
81179eec | 314 | |
82c1ed38 JA |
315 | if (!flist_empty(&client->eta_list)) { |
316 | flist_del_init(&client->eta_list); | |
a5276616 | 317 | fio_client_dec_jobs_eta(client->eta_in_flight, client->ops->eta); |
82c1ed38 | 318 | } |
af9c9fb3 | 319 | |
1e0c184e JA |
320 | close(client->fd); |
321 | client->fd = -1; | |
322 | ||
0cf3ece0 JA |
323 | if (client->ops->removed) |
324 | client->ops->removed(client); | |
325 | ||
3c5f57e3 | 326 | nr_clients--; |
3af45201 | 327 | fio_put_client(client); |
5121a9aa JA |
328 | } |
329 | ||
343cb4a9 JA |
330 | struct fio_client *fio_get_client(struct fio_client *client) |
331 | { | |
332 | client->refs++; | |
333 | return client; | |
334 | } | |
335 | ||
fa2ea806 JA |
336 | static void __fio_client_add_cmd_option(struct fio_client *client, |
337 | const char *opt) | |
81179eec | 338 | { |
39e8e016 JA |
339 | int index; |
340 | ||
341 | index = client->argc++; | |
81179eec | 342 | client->argv = realloc(client->argv, sizeof(char *) * client->argc); |
39e8e016 JA |
343 | client->argv[index] = strdup(opt); |
344 | dprint(FD_NET, "client: add cmd %d: %s\n", index, opt); | |
81179eec JA |
345 | } |
346 | ||
fa2ea806 | 347 | void fio_client_add_cmd_option(void *cookie, const char *opt) |
81179eec | 348 | { |
bebe6398 | 349 | struct fio_client *client = cookie; |
3f3a4542 | 350 | struct flist_head *entry; |
81179eec | 351 | |
bebe6398 | 352 | if (!client || !opt) |
fa2ea806 | 353 | return; |
81179eec | 354 | |
fa2ea806 | 355 | __fio_client_add_cmd_option(client, opt); |
3f3a4542 JA |
356 | |
357 | /* | |
358 | * Duplicate arguments to shared client group | |
359 | */ | |
360 | flist_for_each(entry, &arg_list) { | |
361 | client = flist_entry(entry, struct fio_client, arg_list); | |
362 | ||
363 | __fio_client_add_cmd_option(client, opt); | |
364 | } | |
81179eec JA |
365 | } |
366 | ||
7c6686fc | 367 | static struct fio_client *get_new_client(void) |
3ec62ec4 JA |
368 | { |
369 | struct fio_client *client; | |
370 | ||
371 | client = malloc(sizeof(*client)); | |
372 | memset(client, 0, sizeof(*client)); | |
373 | ||
374 | INIT_FLIST_HEAD(&client->list); | |
375 | INIT_FLIST_HEAD(&client->hash_list); | |
376 | INIT_FLIST_HEAD(&client->arg_list); | |
377 | INIT_FLIST_HEAD(&client->eta_list); | |
378 | INIT_FLIST_HEAD(&client->cmd_list); | |
379 | ||
7c6686fc JA |
380 | buf_output_init(&client->buf); |
381 | ||
382 | return client; | |
383 | } | |
384 | ||
385 | struct fio_client *fio_client_add_explicit(struct client_ops *ops, | |
386 | const char *hostname, int type, | |
387 | int port) | |
388 | { | |
389 | struct fio_client *client; | |
390 | ||
391 | client = get_new_client(); | |
392 | ||
3ec62ec4 JA |
393 | client->hostname = strdup(hostname); |
394 | ||
395 | if (type == Fio_client_socket) | |
67d2a9b8 | 396 | client->is_sock = true; |
3ec62ec4 JA |
397 | else { |
398 | int ipv6; | |
399 | ||
400 | ipv6 = type == Fio_client_ipv6; | |
3aa3ceeb | 401 | if (fio_server_parse_host(hostname, ipv6, |
3ec62ec4 JA |
402 | &client->addr.sin_addr, |
403 | &client->addr6.sin6_addr)) | |
404 | goto err; | |
405 | ||
406 | client->port = port; | |
407 | } | |
408 | ||
409 | client->fd = -1; | |
a5276616 | 410 | client->ops = ops; |
5121a9aa | 411 | client->refs = 1; |
46bcd498 | 412 | client->type = ops->client_type; |
3ec62ec4 JA |
413 | |
414 | __fio_client_add_cmd_option(client, "fio"); | |
415 | ||
416 | flist_add(&client->list, &client_list); | |
417 | nr_clients++; | |
418 | dprint(FD_NET, "client: added <%s>\n", client->hostname); | |
419 | return client; | |
420 | err: | |
421 | free(client); | |
422 | return NULL; | |
423 | } | |
424 | ||
bb781643 | 425 | int fio_client_add_ini_file(void *cookie, const char *ini_file, bool remote) |
14ea90ed JA |
426 | { |
427 | struct fio_client *client = cookie; | |
323255cc | 428 | struct client_file *cf; |
14ea90ed | 429 | size_t new_size; |
323255cc | 430 | void *new_files; |
14ea90ed | 431 | |
ea91da21 JA |
432 | if (!client) |
433 | return 1; | |
434 | ||
14ea90ed JA |
435 | dprint(FD_NET, "client <%s>: add ini %s\n", client->hostname, ini_file); |
436 | ||
323255cc JA |
437 | new_size = (client->nr_files + 1) * sizeof(struct client_file); |
438 | new_files = realloc(client->files, new_size); | |
439 | if (!new_files) | |
440 | return 1; | |
441 | ||
442 | client->files = new_files; | |
443 | cf = &client->files[client->nr_files]; | |
444 | cf->file = strdup(ini_file); | |
445 | cf->remote = remote; | |
446 | client->nr_files++; | |
447 | return 0; | |
14ea90ed JA |
448 | } |
449 | ||
a5276616 | 450 | int fio_client_add(struct client_ops *ops, const char *hostname, void **cookie) |
132159a5 | 451 | { |
3f3a4542 | 452 | struct fio_client *existing = *cookie; |
b66570dc | 453 | struct fio_client *client; |
132159a5 | 454 | |
3f3a4542 JA |
455 | if (existing) { |
456 | /* | |
457 | * We always add our "exec" name as the option, hence 1 | |
458 | * means empty. | |
459 | */ | |
460 | if (existing->argc == 1) | |
461 | flist_add_tail(&existing->arg_list, &arg_list); | |
462 | else { | |
463 | while (!flist_empty(&arg_list)) | |
464 | flist_del_init(arg_list.next); | |
465 | } | |
466 | } | |
467 | ||
7c6686fc | 468 | client = get_new_client(); |
3c5f57e3 | 469 | |
bebe6398 JA |
470 | if (fio_server_parse_string(hostname, &client->hostname, |
471 | &client->is_sock, &client->port, | |
811826be JA |
472 | &client->addr.sin_addr, |
473 | &client->addr6.sin6_addr, | |
bfb81533 BVA |
474 | &client->ipv6)) { |
475 | fio_put_client(client); | |
bebe6398 | 476 | return -1; |
bfb81533 | 477 | } |
87aa8f19 | 478 | |
bebe6398 | 479 | client->fd = -1; |
a5276616 | 480 | client->ops = ops; |
5121a9aa | 481 | client->refs = 1; |
46bcd498 | 482 | client->type = ops->client_type; |
3c5f57e3 | 483 | |
81179eec JA |
484 | __fio_client_add_cmd_option(client, "fio"); |
485 | ||
a37f69b7 JA |
486 | flist_add(&client->list, &client_list); |
487 | nr_clients++; | |
bebe6398 JA |
488 | dprint(FD_NET, "client: added <%s>\n", client->hostname); |
489 | *cookie = client; | |
490 | return 0; | |
a37f69b7 JA |
491 | } |
492 | ||
ca09be4b JA |
493 | static const char *server_name(struct fio_client *client, char *buf, |
494 | size_t bufsize) | |
495 | { | |
496 | const char *from; | |
497 | ||
498 | if (client->ipv6) | |
499 | from = inet_ntop(AF_INET6, (struct sockaddr *) &client->addr6.sin6_addr, buf, bufsize); | |
500 | else if (client->is_sock) | |
501 | from = "sock"; | |
502 | else | |
503 | from = inet_ntop(AF_INET, (struct sockaddr *) &client->addr.sin_addr, buf, bufsize); | |
504 | ||
505 | return from; | |
506 | } | |
507 | ||
d824c3b8 JA |
508 | static void probe_client(struct fio_client *client) |
509 | { | |
3989b143 | 510 | struct cmd_client_probe_pdu pdu; |
e8b60617 | 511 | const char *sname; |
3989b143 | 512 | uint64_t tag; |
ca09be4b | 513 | char buf[64]; |
3989b143 | 514 | |
d824c3b8 JA |
515 | dprint(FD_NET, "client: send probe\n"); |
516 | ||
3989b143 JA |
517 | #ifdef CONFIG_ZLIB |
518 | pdu.flags = __le64_to_cpu(FIO_PROBE_FLAG_ZLIB); | |
519 | #else | |
520 | pdu.flags = 0; | |
521 | #endif | |
522 | ||
e8b60617 JA |
523 | sname = server_name(client, buf, sizeof(buf)); |
524 | memset(pdu.server, 0, sizeof(pdu.server)); | |
36833fb0 | 525 | snprintf((char *) pdu.server, sizeof(pdu.server), "%s", sname); |
ca09be4b | 526 | |
3989b143 | 527 | fio_net_send_cmd(client->fd, FIO_NET_CMD_PROBE, &pdu, sizeof(pdu), &tag, &client->cmd_list); |
d824c3b8 JA |
528 | } |
529 | ||
87aa8f19 | 530 | static int fio_client_connect_ip(struct fio_client *client) |
a37f69b7 | 531 | { |
811826be | 532 | struct sockaddr *addr; |
67bf9823 | 533 | socklen_t socklen; |
811826be JA |
534 | int fd, domain; |
535 | ||
536 | if (client->ipv6) { | |
537 | client->addr6.sin6_family = AF_INET6; | |
538 | client->addr6.sin6_port = htons(client->port); | |
539 | domain = AF_INET6; | |
540 | addr = (struct sockaddr *) &client->addr6; | |
541 | socklen = sizeof(client->addr6); | |
542 | } else { | |
543 | client->addr.sin_family = AF_INET; | |
544 | client->addr.sin_port = htons(client->port); | |
545 | domain = AF_INET; | |
546 | addr = (struct sockaddr *) &client->addr; | |
547 | socklen = sizeof(client->addr); | |
548 | } | |
132159a5 | 549 | |
811826be | 550 | fd = socket(domain, SOCK_STREAM, 0); |
132159a5 | 551 | if (fd < 0) { |
25095e1b JA |
552 | int ret = -errno; |
553 | ||
132159a5 | 554 | log_err("fio: socket: %s\n", strerror(errno)); |
25095e1b | 555 | return ret; |
132159a5 JA |
556 | } |
557 | ||
811826be | 558 | if (connect(fd, addr, socklen) < 0) { |
25095e1b JA |
559 | int ret = -errno; |
560 | ||
132159a5 | 561 | log_err("fio: connect: %s\n", strerror(errno)); |
a7de0a11 JA |
562 | log_err("fio: failed to connect to %s:%u\n", client->hostname, |
563 | client->port); | |
b94cba47 | 564 | close(fd); |
25095e1b | 565 | return ret; |
87aa8f19 JA |
566 | } |
567 | ||
568 | return fd; | |
569 | } | |
570 | ||
571 | static int fio_client_connect_sock(struct fio_client *client) | |
572 | { | |
573 | struct sockaddr_un *addr = &client->addr_un; | |
67bf9823 | 574 | socklen_t len; |
87aa8f19 JA |
575 | int fd; |
576 | ||
577 | memset(addr, 0, sizeof(*addr)); | |
578 | addr->sun_family = AF_UNIX; | |
36833fb0 BVA |
579 | snprintf(addr->sun_path, sizeof(addr->sun_path), "%s", |
580 | client->hostname); | |
87aa8f19 JA |
581 | |
582 | fd = socket(AF_UNIX, SOCK_STREAM, 0); | |
583 | if (fd < 0) { | |
25095e1b JA |
584 | int ret = -errno; |
585 | ||
87aa8f19 | 586 | log_err("fio: socket: %s\n", strerror(errno)); |
25095e1b | 587 | return ret; |
87aa8f19 JA |
588 | } |
589 | ||
590 | len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1; | |
591 | if (connect(fd, (struct sockaddr *) addr, len) < 0) { | |
25095e1b JA |
592 | int ret = -errno; |
593 | ||
87aa8f19 | 594 | log_err("fio: connect; %s\n", strerror(errno)); |
b94cba47 | 595 | close(fd); |
25095e1b | 596 | return ret; |
132159a5 JA |
597 | } |
598 | ||
87aa8f19 JA |
599 | return fd; |
600 | } | |
601 | ||
2f99deb0 | 602 | int fio_client_connect(struct fio_client *client) |
87aa8f19 JA |
603 | { |
604 | int fd; | |
605 | ||
606 | dprint(FD_NET, "client: connect to host %s\n", client->hostname); | |
607 | ||
87aa8f19 JA |
608 | if (client->is_sock) |
609 | fd = fio_client_connect_sock(client); | |
610 | else | |
611 | fd = fio_client_connect_ip(client); | |
612 | ||
89c1707c JA |
613 | dprint(FD_NET, "client: %s connected %d\n", client->hostname, fd); |
614 | ||
87aa8f19 | 615 | if (fd < 0) |
a1a3ed51 | 616 | return fd; |
87aa8f19 | 617 | |
b66570dc | 618 | client->fd = fd; |
bebe6398 | 619 | fio_client_add_hash(client); |
81179eec | 620 | client->state = Client_connected; |
d824c3b8 JA |
621 | |
622 | probe_client(client); | |
132159a5 JA |
623 | return 0; |
624 | } | |
625 | ||
0cf3ece0 | 626 | int fio_client_terminate(struct fio_client *client) |
2f99deb0 | 627 | { |
0cf3ece0 | 628 | return fio_net_send_quit(client->fd); |
2f99deb0 JA |
629 | } |
630 | ||
8aa89d70 | 631 | static void fio_clients_terminate(void) |
cc0df00a JA |
632 | { |
633 | struct flist_head *entry; | |
634 | struct fio_client *client; | |
635 | ||
60efd14e JA |
636 | dprint(FD_NET, "client: terminate clients\n"); |
637 | ||
cc0df00a JA |
638 | flist_for_each(entry, &client_list) { |
639 | client = flist_entry(entry, struct fio_client, list); | |
2f99deb0 | 640 | fio_client_terminate(client); |
cc0df00a JA |
641 | } |
642 | } | |
643 | ||
644 | static void sig_int(int sig) | |
645 | { | |
bebe6398 | 646 | dprint(FD_NET, "client: got signal %d\n", sig); |
cc0df00a JA |
647 | fio_clients_terminate(); |
648 | } | |
649 | ||
650 | static void client_signal_handler(void) | |
651 | { | |
652 | struct sigaction act; | |
653 | ||
654 | memset(&act, 0, sizeof(act)); | |
655 | act.sa_handler = sig_int; | |
656 | act.sa_flags = SA_RESTART; | |
657 | sigaction(SIGINT, &act, NULL); | |
658 | ||
659 | memset(&act, 0, sizeof(act)); | |
660 | act.sa_handler = sig_int; | |
661 | act.sa_flags = SA_RESTART; | |
662 | sigaction(SIGTERM, &act, NULL); | |
b852e7cf | 663 | |
2f694507 BC |
664 | /* Windows uses SIGBREAK as a quit signal from other applications */ |
665 | #ifdef WIN32 | |
666 | memset(&act, 0, sizeof(act)); | |
667 | act.sa_handler = sig_int; | |
668 | act.sa_flags = SA_RESTART; | |
669 | sigaction(SIGBREAK, &act, NULL); | |
670 | #endif | |
671 | ||
b852e7cf JA |
672 | memset(&act, 0, sizeof(act)); |
673 | act.sa_handler = sig_show_status; | |
674 | act.sa_flags = SA_RESTART; | |
675 | sigaction(SIGUSR1, &act, NULL); | |
cc0df00a JA |
676 | } |
677 | ||
81179eec JA |
678 | static int send_client_cmd_line(struct fio_client *client) |
679 | { | |
fa2ea806 JA |
680 | struct cmd_single_line_pdu *cslp; |
681 | struct cmd_line_pdu *clp; | |
682 | unsigned long offset; | |
7f868316 | 683 | unsigned int *lens; |
fa2ea806 JA |
684 | void *pdu; |
685 | size_t mem; | |
81179eec JA |
686 | int i, ret; |
687 | ||
39e8e016 | 688 | dprint(FD_NET, "client: send cmdline %d\n", client->argc); |
60efd14e | 689 | |
7f868316 JA |
690 | lens = malloc(client->argc * sizeof(unsigned int)); |
691 | ||
fa2ea806 JA |
692 | /* |
693 | * Find out how much mem we need | |
694 | */ | |
7f868316 JA |
695 | for (i = 0, mem = 0; i < client->argc; i++) { |
696 | lens[i] = strlen(client->argv[i]) + 1; | |
697 | mem += lens[i]; | |
698 | } | |
fa2ea806 JA |
699 | |
700 | /* | |
701 | * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu | |
702 | */ | |
703 | mem += sizeof(*clp) + (client->argc * sizeof(*cslp)); | |
704 | ||
705 | pdu = malloc(mem); | |
706 | clp = pdu; | |
707 | offset = sizeof(*clp); | |
708 | ||
709 | for (i = 0; i < client->argc; i++) { | |
7f868316 | 710 | uint16_t arg_len = lens[i]; |
fa2ea806 JA |
711 | |
712 | cslp = pdu + offset; | |
713 | strcpy((char *) cslp->text, client->argv[i]); | |
714 | cslp->len = cpu_to_le16(arg_len); | |
715 | offset += sizeof(*cslp) + arg_len; | |
716 | } | |
81179eec | 717 | |
7f868316 | 718 | free(lens); |
fa2ea806 | 719 | clp->lines = cpu_to_le16(client->argc); |
46bcd498 | 720 | clp->client_type = __cpu_to_le16(client->type); |
40c60516 | 721 | ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, NULL, NULL); |
81179eec JA |
722 | free(pdu); |
723 | return ret; | |
724 | } | |
725 | ||
a37f69b7 JA |
726 | int fio_clients_connect(void) |
727 | { | |
728 | struct fio_client *client; | |
729 | struct flist_head *entry, *tmp; | |
730 | int ret; | |
731 | ||
93bcfd20 BC |
732 | #ifdef WIN32 |
733 | WSADATA wsd; | |
3c3ed070 | 734 | WSAStartup(MAKEWORD(2, 2), &wsd); |
93bcfd20 BC |
735 | #endif |
736 | ||
60efd14e JA |
737 | dprint(FD_NET, "client: connect all\n"); |
738 | ||
cc0df00a JA |
739 | client_signal_handler(); |
740 | ||
a37f69b7 JA |
741 | flist_for_each_safe(entry, tmp, &client_list) { |
742 | client = flist_entry(entry, struct fio_client, list); | |
743 | ||
744 | ret = fio_client_connect(client); | |
0b8f30a5 | 745 | if (ret) { |
a37f69b7 | 746 | remove_client(client); |
0b8f30a5 JA |
747 | continue; |
748 | } | |
749 | ||
81179eec JA |
750 | if (client->argc > 1) |
751 | send_client_cmd_line(client); | |
a37f69b7 JA |
752 | } |
753 | ||
754 | return !nr_clients; | |
755 | } | |
756 | ||
b9d2f30a JA |
757 | int fio_start_client(struct fio_client *client) |
758 | { | |
759 | dprint(FD_NET, "client: start %s\n", client->hostname); | |
760 | return fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_RUN, 0, NULL); | |
761 | } | |
762 | ||
763 | int fio_start_all_clients(void) | |
764 | { | |
765 | struct fio_client *client; | |
766 | struct flist_head *entry, *tmp; | |
767 | int ret; | |
768 | ||
769 | dprint(FD_NET, "client: start all\n"); | |
770 | ||
952b05e0 CF |
771 | fio_client_json_init(); |
772 | ||
b9d2f30a JA |
773 | flist_for_each_safe(entry, tmp, &client_list) { |
774 | client = flist_entry(entry, struct fio_client, list); | |
775 | ||
776 | ret = fio_start_client(client); | |
777 | if (ret) { | |
778 | remove_client(client); | |
779 | continue; | |
780 | } | |
781 | } | |
782 | ||
783 | return flist_empty(&client_list); | |
784 | } | |
785 | ||
323255cc JA |
786 | static int __fio_client_send_remote_ini(struct fio_client *client, |
787 | const char *filename) | |
788 | { | |
789 | struct cmd_load_file_pdu *pdu; | |
790 | size_t p_size; | |
791 | int ret; | |
792 | ||
793 | dprint(FD_NET, "send remote ini %s to %s\n", filename, client->hostname); | |
794 | ||
829c31b5 | 795 | p_size = sizeof(*pdu) + strlen(filename) + 1; |
323255cc | 796 | pdu = malloc(p_size); |
829c31b5 | 797 | memset(pdu, 0, p_size); |
323255cc JA |
798 | pdu->name_len = strlen(filename); |
799 | strcpy((char *) pdu->file, filename); | |
800 | pdu->client_type = cpu_to_le16((uint16_t) client->type); | |
801 | ||
67d2a9b8 | 802 | client->sent_job = true; |
323255cc JA |
803 | ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_LOAD_FILE, pdu, p_size,NULL, NULL); |
804 | free(pdu); | |
805 | return ret; | |
806 | } | |
807 | ||
132159a5 JA |
808 | /* |
809 | * Send file contents to server backend. We could use sendfile(), but to remain | |
810 | * more portable lets just read/write the darn thing. | |
811 | */ | |
323255cc JA |
812 | static int __fio_client_send_local_ini(struct fio_client *client, |
813 | const char *filename) | |
132159a5 | 814 | { |
46bcd498 JA |
815 | struct cmd_job_pdu *pdu; |
816 | size_t p_size; | |
132159a5 | 817 | struct stat sb; |
46bcd498 JA |
818 | char *p; |
819 | void *buf; | |
132159a5 JA |
820 | off_t len; |
821 | int fd, ret; | |
822 | ||
46c48f1f JA |
823 | dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname); |
824 | ||
132159a5 JA |
825 | fd = open(filename, O_RDONLY); |
826 | if (fd < 0) { | |
8a1db9a1 | 827 | ret = -errno; |
e951bdc4 | 828 | log_err("fio: job file <%s> open: %s\n", filename, strerror(errno)); |
25095e1b | 829 | return ret; |
132159a5 JA |
830 | } |
831 | ||
832 | if (fstat(fd, &sb) < 0) { | |
8a1db9a1 | 833 | ret = -errno; |
132159a5 | 834 | log_err("fio: job file stat: %s\n", strerror(errno)); |
b94cba47 | 835 | close(fd); |
25095e1b | 836 | return ret; |
132159a5 JA |
837 | } |
838 | ||
b4f5e72f TK |
839 | /* |
840 | * Add extra space for variable expansion, but doesn't guarantee. | |
841 | */ | |
842 | sb.st_size += OPT_LEN_MAX; | |
46bcd498 JA |
843 | p_size = sb.st_size + sizeof(*pdu); |
844 | pdu = malloc(p_size); | |
845 | buf = pdu->buf; | |
132159a5 JA |
846 | |
847 | len = sb.st_size; | |
848 | p = buf; | |
b4f5e72f | 849 | if (read_ini_data(fd, p, len)) { |
0b8f30a5 | 850 | log_err("fio: failed reading job file %s\n", filename); |
b94cba47 | 851 | close(fd); |
03a22d91 | 852 | free(pdu); |
0b8f30a5 JA |
853 | return 1; |
854 | } | |
855 | ||
46bcd498 JA |
856 | pdu->buf_len = __cpu_to_le32(sb.st_size); |
857 | pdu->client_type = cpu_to_le32(client->type); | |
858 | ||
67d2a9b8 | 859 | client->sent_job = true; |
40c60516 | 860 | ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, pdu, p_size, NULL, NULL); |
46bcd498 | 861 | free(pdu); |
b94cba47 | 862 | close(fd); |
132159a5 JA |
863 | return ret; |
864 | } | |
37db14fe | 865 | |
323255cc | 866 | int fio_client_send_ini(struct fio_client *client, const char *filename, |
bb781643 | 867 | bool remote) |
9988ca70 | 868 | { |
25095e1b JA |
869 | int ret; |
870 | ||
323255cc JA |
871 | if (!remote) |
872 | ret = __fio_client_send_local_ini(client, filename); | |
873 | else | |
874 | ret = __fio_client_send_remote_ini(client, filename); | |
875 | ||
3af45201 | 876 | if (!ret) |
67d2a9b8 | 877 | client->sent_job = true; |
9988ca70 | 878 | |
25095e1b | 879 | return ret; |
9988ca70 JA |
880 | } |
881 | ||
323255cc JA |
882 | static int fio_client_send_cf(struct fio_client *client, |
883 | struct client_file *cf) | |
884 | { | |
885 | return fio_client_send_ini(client, cf->file, cf->remote); | |
886 | } | |
887 | ||
a37f69b7 JA |
888 | int fio_clients_send_ini(const char *filename) |
889 | { | |
890 | struct fio_client *client; | |
891 | struct flist_head *entry, *tmp; | |
892 | ||
893 | flist_for_each_safe(entry, tmp, &client_list) { | |
4d057f1f JA |
894 | bool failed = false; |
895 | ||
a37f69b7 JA |
896 | client = flist_entry(entry, struct fio_client, list); |
897 | ||
323255cc | 898 | if (client->nr_files) { |
14ea90ed JA |
899 | int i; |
900 | ||
323255cc JA |
901 | for (i = 0; i < client->nr_files; i++) { |
902 | struct client_file *cf; | |
14ea90ed | 903 | |
323255cc JA |
904 | cf = &client->files[i]; |
905 | ||
906 | if (fio_client_send_cf(client, cf)) { | |
4d057f1f | 907 | failed = true; |
14ea90ed JA |
908 | remove_client(client); |
909 | break; | |
910 | } | |
911 | } | |
323255cc | 912 | } |
4d057f1f | 913 | if (client->sent_job || failed) |
323255cc JA |
914 | continue; |
915 | if (!filename || fio_client_send_ini(client, filename, 0)) | |
3af45201 | 916 | remove_client(client); |
a37f69b7 JA |
917 | } |
918 | ||
919 | return !nr_clients; | |
920 | } | |
921 | ||
40c60516 JA |
922 | int fio_client_update_options(struct fio_client *client, |
923 | struct thread_options *o, uint64_t *tag) | |
924 | { | |
925 | struct cmd_add_job_pdu pdu; | |
926 | ||
927 | pdu.thread_number = cpu_to_le32(client->thread_number); | |
928 | pdu.groupid = cpu_to_le32(client->groupid); | |
929 | convert_thread_options_to_net(&pdu.top, o); | |
952b05e0 | 930 | |
40c60516 JA |
931 | return fio_net_send_cmd(client->fd, FIO_NET_CMD_UPDATE_JOB, &pdu, sizeof(pdu), tag, &client->cmd_list); |
932 | } | |
933 | ||
a64e88da JA |
934 | static void convert_io_stat(struct io_stat *dst, struct io_stat *src) |
935 | { | |
936 | dst->max_val = le64_to_cpu(src->max_val); | |
937 | dst->min_val = le64_to_cpu(src->min_val); | |
938 | dst->samples = le64_to_cpu(src->samples); | |
802ad4a8 JA |
939 | |
940 | /* | |
941 | * Floats arrive as IEEE 754 encoded uint64_t, convert back to double | |
942 | */ | |
943 | dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i)); | |
944 | dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i)); | |
a64e88da JA |
945 | } |
946 | ||
947 | static void convert_ts(struct thread_stat *dst, struct thread_stat *src) | |
948 | { | |
df8781b6 | 949 | int i, j, k; |
a64e88da | 950 | |
2f122b13 JA |
951 | dst->error = le32_to_cpu(src->error); |
952 | dst->thread_number = le32_to_cpu(src->thread_number); | |
953 | dst->groupid = le32_to_cpu(src->groupid); | |
954 | dst->pid = le32_to_cpu(src->pid); | |
955 | dst->members = le32_to_cpu(src->members); | |
771e58be | 956 | dst->unified_rw_rep = le32_to_cpu(src->unified_rw_rep); |
a64e88da | 957 | |
298921d6 | 958 | for (i = 0; i < DDIR_RWDIR_CNT; i++) { |
a64e88da JA |
959 | convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]); |
960 | convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]); | |
961 | convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]); | |
962 | convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]); | |
188b6016 | 963 | convert_io_stat(&dst->iops_stat[i], &src->iops_stat[i]); |
a64e88da | 964 | } |
91ea42e5 | 965 | convert_io_stat(&dst->sync_stat, &src->sync_stat); |
a64e88da JA |
966 | |
967 | dst->usr_time = le64_to_cpu(src->usr_time); | |
968 | dst->sys_time = le64_to_cpu(src->sys_time); | |
969 | dst->ctx = le64_to_cpu(src->ctx); | |
970 | dst->minf = le64_to_cpu(src->minf); | |
971 | dst->majf = le64_to_cpu(src->majf); | |
b599759b JA |
972 | dst->clat_percentiles = le32_to_cpu(src->clat_percentiles); |
973 | dst->lat_percentiles = le32_to_cpu(src->lat_percentiles); | |
56440e63 | 974 | dst->slat_percentiles = le32_to_cpu(src->slat_percentiles); |
988d97ba | 975 | dst->percentile_precision = le64_to_cpu(src->percentile_precision); |
802ad4a8 JA |
976 | |
977 | for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) { | |
978 | fio_fp64_t *fps = &src->percentile_list[i]; | |
979 | fio_fp64_t *fpd = &dst->percentile_list[i]; | |
980 | ||
981 | fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i)); | |
982 | } | |
a64e88da JA |
983 | |
984 | for (i = 0; i < FIO_IO_U_MAP_NR; i++) { | |
6cc0e5aa AL |
985 | dst->io_u_map[i] = le64_to_cpu(src->io_u_map[i]); |
986 | dst->io_u_submit[i] = le64_to_cpu(src->io_u_submit[i]); | |
987 | dst->io_u_complete[i] = le64_to_cpu(src->io_u_complete[i]); | |
a64e88da JA |
988 | } |
989 | ||
d6bb626e | 990 | for (i = 0; i < FIO_IO_U_LAT_N_NR; i++) |
6cc0e5aa | 991 | dst->io_u_lat_n[i] = le64_to_cpu(src->io_u_lat_n[i]); |
d279fccc | 992 | for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) |
6cc0e5aa | 993 | dst->io_u_lat_u[i] = le64_to_cpu(src->io_u_lat_u[i]); |
d279fccc | 994 | for (i = 0; i < FIO_IO_U_LAT_M_NR; i++) |
6cc0e5aa | 995 | dst->io_u_lat_m[i] = le64_to_cpu(src->io_u_lat_m[i]); |
a64e88da | 996 | |
df8781b6 VF |
997 | for (i = 0; i < FIO_LAT_CNT; i++) |
998 | for (j = 0; j < DDIR_RWDIR_CNT; j++) | |
999 | for (k = 0; k < FIO_IO_U_PLAT_NR; k++) | |
1000 | dst->io_u_plat[i][j][k] = le64_to_cpu(src->io_u_plat[i][j][k]); | |
a64e88da | 1001 | |
91ea42e5 VF |
1002 | for (j = 0; j < FIO_IO_U_PLAT_NR; j++) |
1003 | dst->io_u_sync_plat[j] = le64_to_cpu(src->io_u_sync_plat[j]); | |
1004 | ||
1005 | for (i = 0; i < DDIR_RWDIR_SYNC_CNT; i++) | |
a64e88da | 1006 | dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]); |
91ea42e5 VF |
1007 | |
1008 | for (i = 0; i < DDIR_RWDIR_CNT; i++) { | |
93eee04a | 1009 | dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]); |
3bcb9d94 | 1010 | dst->drop_io_u[i] = le64_to_cpu(src->drop_io_u[i]); |
a64e88da JA |
1011 | } |
1012 | ||
1013 | dst->total_submit = le64_to_cpu(src->total_submit); | |
1014 | dst->total_complete = le64_to_cpu(src->total_complete); | |
a5951dd7 | 1015 | dst->nr_zone_resets = le64_to_cpu(src->nr_zone_resets); |
a64e88da | 1016 | |
298921d6 | 1017 | for (i = 0; i < DDIR_RWDIR_CNT; i++) { |
a64e88da JA |
1018 | dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]); |
1019 | dst->runtime[i] = le64_to_cpu(src->runtime[i]); | |
1020 | } | |
1021 | ||
1022 | dst->total_run_time = le64_to_cpu(src->total_run_time); | |
1023 | dst->continue_on_error = le16_to_cpu(src->continue_on_error); | |
1024 | dst->total_err_count = le64_to_cpu(src->total_err_count); | |
ddcc0b69 JA |
1025 | dst->first_error = le32_to_cpu(src->first_error); |
1026 | dst->kb_base = le32_to_cpu(src->kb_base); | |
ad705bcb | 1027 | dst->unit_base = le32_to_cpu(src->unit_base); |
3e260a46 | 1028 | |
e883cb35 JF |
1029 | dst->sig_figs = le32_to_cpu(src->sig_figs); |
1030 | ||
3e260a46 JA |
1031 | dst->latency_depth = le32_to_cpu(src->latency_depth); |
1032 | dst->latency_target = le64_to_cpu(src->latency_target); | |
1033 | dst->latency_window = le64_to_cpu(src->latency_window); | |
1034 | dst->latency_percentile.u.f = fio_uint64_to_double(le64_to_cpu(src->latency_percentile.u.i)); | |
66347cfa DE |
1035 | |
1036 | dst->nr_block_infos = le64_to_cpu(src->nr_block_infos); | |
1037 | for (i = 0; i < dst->nr_block_infos; i++) | |
1038 | dst->block_infos[i] = le32_to_cpu(src->block_infos[i]); | |
b2a432bf PC |
1039 | for (i = 0; i < DDIR_RWDIR_CNT; i++) { |
1040 | for (j = 0; j < FIO_IO_U_PLAT_NR; j++) { | |
1041 | dst->io_u_plat_high_prio[i][j] = le64_to_cpu(src->io_u_plat_high_prio[i][j]); | |
efd78265 | 1042 | dst->io_u_plat_low_prio[i][j] = le64_to_cpu(src->io_u_plat_low_prio[i][j]); |
b2a432bf PC |
1043 | } |
1044 | convert_io_stat(&dst->clat_high_prio_stat[i], &src->clat_high_prio_stat[i]); | |
efd78265 | 1045 | convert_io_stat(&dst->clat_low_prio_stat[i], &src->clat_low_prio_stat[i]); |
b2a432bf | 1046 | } |
bb49c8bd VF |
1047 | |
1048 | dst->ss_dur = le64_to_cpu(src->ss_dur); | |
1049 | dst->ss_state = le32_to_cpu(src->ss_state); | |
1050 | dst->ss_head = le32_to_cpu(src->ss_head); | |
bb49c8bd VF |
1051 | dst->ss_limit.u.f = fio_uint64_to_double(le64_to_cpu(src->ss_limit.u.i)); |
1052 | dst->ss_slope.u.f = fio_uint64_to_double(le64_to_cpu(src->ss_slope.u.i)); | |
1053 | dst->ss_deviation.u.f = fio_uint64_to_double(le64_to_cpu(src->ss_deviation.u.i)); | |
1054 | dst->ss_criterion.u.f = fio_uint64_to_double(le64_to_cpu(src->ss_criterion.u.i)); | |
1055 | ||
c8caba48 | 1056 | if (dst->ss_state & FIO_SS_DATA) { |
bb49c8bd VF |
1057 | for (i = 0; i < dst->ss_dur; i++ ) { |
1058 | dst->ss_iops_data[i] = le64_to_cpu(src->ss_iops_data[i]); | |
1059 | dst->ss_bw_data[i] = le64_to_cpu(src->ss_bw_data[i]); | |
1060 | } | |
1061 | } | |
96563db9 JA |
1062 | |
1063 | dst->cachehit = le64_to_cpu(src->cachehit); | |
1064 | dst->cachemiss = le64_to_cpu(src->cachemiss); | |
a64e88da JA |
1065 | } |
1066 | ||
1067 | static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src) | |
1068 | { | |
1069 | int i; | |
1070 | ||
298921d6 | 1071 | for (i = 0; i < DDIR_RWDIR_CNT; i++) { |
a64e88da JA |
1072 | dst->max_run[i] = le64_to_cpu(src->max_run[i]); |
1073 | dst->min_run[i] = le64_to_cpu(src->min_run[i]); | |
1074 | dst->max_bw[i] = le64_to_cpu(src->max_bw[i]); | |
1075 | dst->min_bw[i] = le64_to_cpu(src->min_bw[i]); | |
af7f87cb | 1076 | dst->iobytes[i] = le64_to_cpu(src->iobytes[i]); |
a64e88da JA |
1077 | dst->agg[i] = le64_to_cpu(src->agg[i]); |
1078 | } | |
1079 | ||
1080 | dst->kb_base = le32_to_cpu(src->kb_base); | |
ad705bcb | 1081 | dst->unit_base = le32_to_cpu(src->unit_base); |
e883cb35 | 1082 | dst->sig_figs = le32_to_cpu(src->sig_figs); |
a64e88da | 1083 | dst->groupid = le32_to_cpu(src->groupid); |
771e58be | 1084 | dst->unified_rw_rep = le32_to_cpu(src->unified_rw_rep); |
a64e88da JA |
1085 | } |
1086 | ||
952b05e0 | 1087 | static void json_object_add_client_info(struct json_object *obj, |
9a4424c8 | 1088 | struct fio_client *client) |
952b05e0 | 1089 | { |
9a4424c8 CF |
1090 | const char *hostname = client->hostname ? client->hostname : ""; |
1091 | ||
1092 | json_object_add_value_string(obj, "hostname", hostname); | |
952b05e0 CF |
1093 | json_object_add_value_int(obj, "port", client->port); |
1094 | } | |
1095 | ||
89e5fad9 | 1096 | static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd) |
a64e88da JA |
1097 | { |
1098 | struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload; | |
0279b880 | 1099 | struct flist_head *opt_list = NULL; |
952b05e0 | 1100 | struct json_object *tsobj; |
a64e88da | 1101 | |
0279b880 JA |
1102 | if (client->opt_lists && p->ts.thread_number <= client->jobs) |
1103 | opt_list = &client->opt_lists[p->ts.thread_number - 1]; | |
1104 | ||
7c6686fc | 1105 | tsobj = show_thread_status(&p->ts, &p->rs, opt_list, &client->buf); |
67d2a9b8 | 1106 | client->did_stat = true; |
952b05e0 CF |
1107 | if (tsobj) { |
1108 | json_object_add_client_info(tsobj, client); | |
1109 | json_array_add_value_object(clients_array, tsobj); | |
1110 | } | |
37f0c1ae | 1111 | |
7b38411c | 1112 | if (sum_stat_clients <= 1) |
37f0c1ae JA |
1113 | return; |
1114 | ||
fd595830 | 1115 | sum_thread_stats(&client_ts, &p->ts, sum_stat_nr == 1); |
37f0c1ae JA |
1116 | sum_group_stats(&client_gs, &p->rs); |
1117 | ||
1118 | client_ts.members++; | |
2f122b13 | 1119 | client_ts.thread_number = p->ts.thread_number; |
37f0c1ae | 1120 | client_ts.groupid = p->ts.groupid; |
771e58be | 1121 | client_ts.unified_rw_rep = p->ts.unified_rw_rep; |
18aa1998 | 1122 | client_ts.sig_figs = p->ts.sig_figs; |
37f0c1ae JA |
1123 | |
1124 | if (++sum_stat_nr == sum_stat_clients) { | |
1125 | strcpy(client_ts.name, "All clients"); | |
c2b5ddb2 | 1126 | tsobj = show_thread_status(&client_ts, &client_gs, NULL, &allclients); |
952b05e0 CF |
1127 | if (tsobj) { |
1128 | json_object_add_client_info(tsobj, client); | |
1129 | json_array_add_value_object(clients_array, tsobj); | |
1130 | } | |
37f0c1ae | 1131 | } |
a64e88da JA |
1132 | } |
1133 | ||
89e5fad9 | 1134 | static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd) |
a64e88da JA |
1135 | { |
1136 | struct group_run_stats *gs = (struct group_run_stats *) cmd->payload; | |
1137 | ||
84f1f865 | 1138 | if (output_format & FIO_OUTPUT_NORMAL) |
7c6686fc | 1139 | show_group_stats(gs, &client->buf); |
a64e88da JA |
1140 | } |
1141 | ||
0279b880 JA |
1142 | static void handle_job_opt(struct fio_client *client, struct fio_net_cmd *cmd) |
1143 | { | |
1144 | struct cmd_job_option *pdu = (struct cmd_job_option *) cmd->payload; | |
1145 | struct print_option *p; | |
1146 | ||
b127b679 JA |
1147 | if (!job_opt_object) |
1148 | return; | |
1149 | ||
0279b880 | 1150 | pdu->global = le16_to_cpu(pdu->global); |
bc0fec0e JA |
1151 | pdu->truncated = le16_to_cpu(pdu->truncated); |
1152 | pdu->groupid = le32_to_cpu(pdu->groupid); | |
0279b880 JA |
1153 | |
1154 | p = malloc(sizeof(*p)); | |
1155 | p->name = strdup((char *) pdu->name); | |
1156 | if (pdu->value[0] != '\0') | |
1157 | p->value = strdup((char *) pdu->value); | |
1158 | else | |
1159 | p->value = NULL; | |
1160 | ||
1161 | if (pdu->global) { | |
1162 | const char *pos = ""; | |
1163 | ||
1164 | if (p->value) | |
1165 | pos = p->value; | |
1166 | ||
1167 | json_object_add_value_string(job_opt_object, p->name, pos); | |
1168 | } else if (client->opt_lists) { | |
1169 | struct flist_head *opt_list = &client->opt_lists[pdu->groupid]; | |
1170 | ||
1171 | flist_add_tail(&p->list, opt_list); | |
ebae36a2 BVA |
1172 | } else { |
1173 | free(p->value); | |
1174 | free(p->name); | |
1175 | free(p); | |
0279b880 JA |
1176 | } |
1177 | } | |
1178 | ||
3bf236c0 JA |
1179 | static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd) |
1180 | { | |
1181 | struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload; | |
1182 | const char *buf = (const char *) pdu->buf; | |
1183 | const char *name; | |
1184 | int fio_unused ret; | |
fecea437 JA |
1185 | struct buf_output out; |
1186 | ||
1187 | buf_output_init(&out); | |
3bf236c0 JA |
1188 | |
1189 | name = client->name ? client->name : client->hostname; | |
1190 | ||
1d1b65dc | 1191 | if (!client->skip_newline && !(output_format & FIO_OUTPUT_TERSE)) |
c2fcb6bc JA |
1192 | __log_buf(&out, "<%s> ", name); |
1193 | __log_buf(&out, "%s", buf); | |
fecea437 | 1194 | log_info_buf(out.buf, out.buflen); |
d5bb0a89 | 1195 | buf_output_free(&out); |
3bf236c0 JA |
1196 | client->skip_newline = strchr(buf, '\n') == NULL; |
1197 | } | |
1198 | ||
d09a64a0 JA |
1199 | static void convert_agg(struct disk_util_agg *agg) |
1200 | { | |
1201 | int i; | |
1202 | ||
1203 | for (i = 0; i < 2; i++) { | |
504bc961 JA |
1204 | agg->ios[i] = le64_to_cpu(agg->ios[i]); |
1205 | agg->merges[i] = le64_to_cpu(agg->merges[i]); | |
d09a64a0 | 1206 | agg->sectors[i] = le64_to_cpu(agg->sectors[i]); |
504bc961 | 1207 | agg->ticks[i] = le64_to_cpu(agg->ticks[i]); |
d09a64a0 JA |
1208 | } |
1209 | ||
504bc961 JA |
1210 | agg->io_ticks = le64_to_cpu(agg->io_ticks); |
1211 | agg->time_in_queue = le64_to_cpu(agg->time_in_queue); | |
d09a64a0 | 1212 | agg->slavecount = le32_to_cpu(agg->slavecount); |
e5c9093d | 1213 | agg->max_util.u.f = fio_uint64_to_double(le64_to_cpu(agg->max_util.u.i)); |
d09a64a0 JA |
1214 | } |
1215 | ||
1216 | static void convert_dus(struct disk_util_stat *dus) | |
1217 | { | |
1218 | int i; | |
1219 | ||
1220 | for (i = 0; i < 2; i++) { | |
504bc961 JA |
1221 | dus->s.ios[i] = le64_to_cpu(dus->s.ios[i]); |
1222 | dus->s.merges[i] = le64_to_cpu(dus->s.merges[i]); | |
a3b4cf7d | 1223 | dus->s.sectors[i] = le64_to_cpu(dus->s.sectors[i]); |
504bc961 | 1224 | dus->s.ticks[i] = le64_to_cpu(dus->s.ticks[i]); |
d09a64a0 JA |
1225 | } |
1226 | ||
504bc961 JA |
1227 | dus->s.io_ticks = le64_to_cpu(dus->s.io_ticks); |
1228 | dus->s.time_in_queue = le64_to_cpu(dus->s.time_in_queue); | |
a3b4cf7d | 1229 | dus->s.msec = le64_to_cpu(dus->s.msec); |
d09a64a0 JA |
1230 | } |
1231 | ||
1232 | static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd) | |
1233 | { | |
1234 | struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload; | |
1235 | ||
2eca4892 | 1236 | if (!client->disk_stats_shown) |
67d2a9b8 | 1237 | client->disk_stats_shown = true; |
d09a64a0 | 1238 | |
01cfefcc | 1239 | if (output_format & FIO_OUTPUT_JSON) { |
952b05e0 | 1240 | struct json_object *duobj; |
7c6686fc | 1241 | |
952b05e0 CF |
1242 | json_array_add_disk_util(&du->dus, &du->agg, du_array); |
1243 | duobj = json_array_last_value_object(du_array); | |
1244 | json_object_add_client_info(duobj, client); | |
fd200c6a VF |
1245 | } |
1246 | if (output_format & FIO_OUTPUT_NORMAL) { | |
2eca4892 | 1247 | __log_buf(&client->buf, "\nDisk stats (read/write):\n"); |
7c6686fc | 1248 | print_disk_util(&du->dus, &du->agg, 0, &client->buf); |
2eca4892 | 1249 | } |
06e49041 | 1250 | if (output_format & FIO_OUTPUT_TERSE && terse_version >= 3) { |
fd200c6a | 1251 | print_disk_util(&du->dus, &du->agg, 1, &client->buf); |
06e49041 VF |
1252 | __log_buf(&client->buf, "\n"); |
1253 | } | |
d09a64a0 JA |
1254 | } |
1255 | ||
3bf236c0 | 1256 | static void convert_jobs_eta(struct jobs_eta *je) |
cf451d1e | 1257 | { |
cf451d1e JA |
1258 | int i; |
1259 | ||
1260 | je->nr_running = le32_to_cpu(je->nr_running); | |
1261 | je->nr_ramp = le32_to_cpu(je->nr_ramp); | |
1262 | je->nr_pending = le32_to_cpu(je->nr_pending); | |
714e85f3 | 1263 | je->nr_setting_up = le32_to_cpu(je->nr_setting_up); |
cf451d1e | 1264 | je->files_open = le32_to_cpu(je->files_open); |
cf451d1e | 1265 | |
298921d6 | 1266 | for (i = 0; i < DDIR_RWDIR_CNT; i++) { |
90eff1c9 SW |
1267 | je->m_rate[i] = le64_to_cpu(je->m_rate[i]); |
1268 | je->t_rate[i] = le64_to_cpu(je->t_rate[i]); | |
298921d6 JA |
1269 | je->m_iops[i] = le32_to_cpu(je->m_iops[i]); |
1270 | je->t_iops[i] = le32_to_cpu(je->t_iops[i]); | |
90eff1c9 | 1271 | je->rate[i] = le64_to_cpu(je->rate[i]); |
16725bb0 | 1272 | je->iops[i] = le32_to_cpu(je->iops[i]); |
cf451d1e JA |
1273 | } |
1274 | ||
b51eedb7 | 1275 | je->elapsed_sec = le64_to_cpu(je->elapsed_sec); |
cf451d1e | 1276 | je->eta_sec = le64_to_cpu(je->eta_sec); |
8c621fb2 | 1277 | je->nr_threads = le32_to_cpu(je->nr_threads); |
b7f05eb0 | 1278 | je->is_pow2 = le32_to_cpu(je->is_pow2); |
ed2c0a12 | 1279 | je->unit_base = le32_to_cpu(je->unit_base); |
e883cb35 | 1280 | je->sig_figs = le32_to_cpu(je->sig_figs); |
48fbb46e JA |
1281 | } |
1282 | ||
3e47bd25 | 1283 | void fio_client_sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je) |
48fbb46e | 1284 | { |
48fbb46e JA |
1285 | int i; |
1286 | ||
1287 | dst->nr_running += je->nr_running; | |
1288 | dst->nr_ramp += je->nr_ramp; | |
1289 | dst->nr_pending += je->nr_pending; | |
714e85f3 | 1290 | dst->nr_setting_up += je->nr_setting_up; |
48fbb46e | 1291 | dst->files_open += je->files_open; |
48fbb46e | 1292 | |
298921d6 | 1293 | for (i = 0; i < DDIR_RWDIR_CNT; i++) { |
3e47bd25 JA |
1294 | dst->m_rate[i] += je->m_rate[i]; |
1295 | dst->t_rate[i] += je->t_rate[i]; | |
1296 | dst->m_iops[i] += je->m_iops[i]; | |
1297 | dst->t_iops[i] += je->t_iops[i]; | |
16725bb0 JA |
1298 | dst->rate[i] += je->rate[i]; |
1299 | dst->iops[i] += je->iops[i]; | |
48fbb46e JA |
1300 | } |
1301 | ||
1302 | dst->elapsed_sec += je->elapsed_sec; | |
1303 | ||
1304 | if (je->eta_sec > dst->eta_sec) | |
1305 | dst->eta_sec = je->eta_sec; | |
8c621fb2 JA |
1306 | |
1307 | dst->nr_threads += je->nr_threads; | |
afe22d3d JA |
1308 | |
1309 | /* | |
1310 | * This wont be correct for multiple strings, but at least it | |
1311 | * works for the basic cases. | |
1312 | */ | |
1313 | strcpy((char *) dst->run_str, (char *) je->run_str); | |
48fbb46e JA |
1314 | } |
1315 | ||
308d69b5 | 1316 | static bool remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd) |
89c1707c | 1317 | { |
40c60516 | 1318 | struct fio_net_cmd_reply *reply = NULL; |
89c1707c JA |
1319 | struct flist_head *entry; |
1320 | ||
1321 | flist_for_each(entry, &client->cmd_list) { | |
40c60516 | 1322 | reply = flist_entry(entry, struct fio_net_cmd_reply, list); |
89c1707c | 1323 | |
40c60516 | 1324 | if (cmd->tag == (uintptr_t) reply) |
89c1707c JA |
1325 | break; |
1326 | ||
40c60516 | 1327 | reply = NULL; |
89c1707c JA |
1328 | } |
1329 | ||
40c60516 | 1330 | if (!reply) { |
4e0a8fa2 | 1331 | log_err("fio: client: unable to find matching tag (%llx)\n", (unsigned long long) cmd->tag); |
308d69b5 | 1332 | return false; |
89c1707c JA |
1333 | } |
1334 | ||
40c60516 JA |
1335 | flist_del(&reply->list); |
1336 | cmd->tag = reply->saved_tag; | |
1337 | free(reply); | |
308d69b5 | 1338 | return true; |
40c60516 JA |
1339 | } |
1340 | ||
1341 | int fio_client_wait_for_reply(struct fio_client *client, uint64_t tag) | |
1342 | { | |
1343 | do { | |
1344 | struct fio_net_cmd_reply *reply = NULL; | |
1345 | struct flist_head *entry; | |
1346 | ||
1347 | flist_for_each(entry, &client->cmd_list) { | |
1348 | reply = flist_entry(entry, struct fio_net_cmd_reply, list); | |
1349 | ||
1350 | if (tag == (uintptr_t) reply) | |
1351 | break; | |
1352 | ||
1353 | reply = NULL; | |
1354 | } | |
1355 | ||
1356 | if (!reply) | |
1357 | break; | |
1358 | ||
1359 | usleep(1000); | |
1360 | } while (1); | |
1361 | ||
1362 | return 0; | |
89c1707c JA |
1363 | } |
1364 | ||
82c1ed38 | 1365 | static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd) |
48fbb46e JA |
1366 | { |
1367 | struct jobs_eta *je = (struct jobs_eta *) cmd->payload; | |
df380934 | 1368 | struct client_eta *eta = (struct client_eta *) (uintptr_t) cmd->tag; |
af9c9fb3 JA |
1369 | |
1370 | dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending); | |
cf451d1e | 1371 | |
f77d2676 JA |
1372 | assert(client->eta_in_flight == eta); |
1373 | ||
1374 | client->eta_in_flight = NULL; | |
82c1ed38 | 1375 | flist_del_init(&client->eta_list); |
a64c13a4 | 1376 | client->eta_timeouts = 0; |
82c1ed38 | 1377 | |
2f99deb0 JA |
1378 | if (client->ops->jobs_eta) |
1379 | client->ops->jobs_eta(client, je); | |
1380 | ||
3e47bd25 | 1381 | fio_client_sum_jobs_eta(&eta->eta, je); |
a5276616 | 1382 | fio_client_dec_jobs_eta(eta, client->ops->eta); |
cf451d1e JA |
1383 | } |
1384 | ||
868f6f03 KC |
1385 | static void client_flush_hist_samples(FILE *f, int hist_coarseness, void *samples, |
1386 | uint64_t sample_size) | |
1387 | { | |
1388 | struct io_sample *s; | |
1389 | int log_offset; | |
1390 | uint64_t i, j, nr_samples; | |
1391 | struct io_u_plat_entry *entry; | |
6cc0e5aa | 1392 | uint64_t *io_u_plat; |
868f6f03 KC |
1393 | |
1394 | int stride = 1 << hist_coarseness; | |
1395 | ||
1396 | if (!sample_size) | |
1397 | return; | |
1398 | ||
1399 | s = __get_sample(samples, 0, 0); | |
1400 | log_offset = (s->__ddir & LOG_OFFSET_SAMPLE_BIT) != 0; | |
1401 | ||
1402 | nr_samples = sample_size / __log_entry_sz(log_offset); | |
1403 | ||
1404 | for (i = 0; i < nr_samples; i++) { | |
1405 | ||
1406 | s = (struct io_sample *)((char *)__get_sample(samples, log_offset, i) + | |
1407 | i * sizeof(struct io_u_plat_entry)); | |
1408 | ||
af2fde19 | 1409 | entry = s->data.plat_entry; |
868f6f03 KC |
1410 | io_u_plat = entry->io_u_plat; |
1411 | ||
5fff9543 JF |
1412 | fprintf(f, "%lu, %u, %llu, ", (unsigned long) s->time, |
1413 | io_sample_ddir(s), (unsigned long long) s->bs); | |
868f6f03 | 1414 | for (j = 0; j < FIO_IO_U_PLAT_NR - stride; j += stride) { |
6cc0e5aa | 1415 | fprintf(f, "%llu, ", (unsigned long long)hist_sum(j, stride, io_u_plat, NULL)); |
868f6f03 | 1416 | } |
6cc0e5aa | 1417 | fprintf(f, "%llu\n", (unsigned long long) |
868f6f03 KC |
1418 | hist_sum(FIO_IO_U_PLAT_NR - stride, stride, io_u_plat, NULL)); |
1419 | ||
1420 | } | |
1421 | } | |
1422 | ||
3da1d33a JA |
1423 | static int fio_client_handle_iolog(struct fio_client *client, |
1424 | struct fio_net_cmd *cmd) | |
bead01d7 | 1425 | { |
44edeed3 | 1426 | struct cmd_iolog_pdu *pdu = NULL; |
0cba0f91 | 1427 | bool store_direct; |
44edeed3 EV |
1428 | char *log_pathname = NULL; |
1429 | int ret = 0; | |
bead01d7 | 1430 | |
0cba0f91 | 1431 | pdu = convert_iolog(cmd, &store_direct); |
3da1d33a JA |
1432 | if (!pdu) { |
1433 | log_err("fio: failed converting IO log\n"); | |
44edeed3 EV |
1434 | ret = 1; |
1435 | goto out; | |
3da1d33a | 1436 | } |
bead01d7 | 1437 | |
d9644df9 | 1438 | /* allocate buffer big enough for next sprintf() call */ |
21c9e728 | 1439 | log_pathname = malloc(10 + strlen((char *)pdu->name) + |
d9644df9 | 1440 | strlen(client->hostname)); |
14a74557 | 1441 | if (!log_pathname) { |
c5839011 | 1442 | log_err("fio: memory allocation of unique pathname failed\n"); |
44edeed3 EV |
1443 | ret = -1; |
1444 | goto out; | |
14a74557 | 1445 | } |
d9644df9 | 1446 | /* generate a unique pathname for the log file using hostname */ |
14a74557 BE |
1447 | sprintf(log_pathname, "%s.%s", pdu->name, client->hostname); |
1448 | ||
0cba0f91 | 1449 | if (store_direct) { |
583e0dfa | 1450 | ssize_t wrote; |
0cba0f91 JA |
1451 | size_t sz; |
1452 | int fd; | |
1453 | ||
14a74557 | 1454 | fd = open((const char *) log_pathname, |
0cba0f91 JA |
1455 | O_WRONLY | O_CREAT | O_TRUNC, 0644); |
1456 | if (fd < 0) { | |
21c9e728 | 1457 | log_err("fio: open log %s: %s\n", |
14a74557 | 1458 | log_pathname, strerror(errno)); |
44edeed3 EV |
1459 | ret = 1; |
1460 | goto out; | |
0cba0f91 | 1461 | } |
3da1d33a | 1462 | |
0cba0f91 | 1463 | sz = cmd->pdu_len - sizeof(*pdu); |
583e0dfa | 1464 | wrote = write(fd, pdu->samples, sz); |
0cba0f91 | 1465 | close(fd); |
3da1d33a | 1466 | |
583e0dfa | 1467 | if (wrote != sz) { |
3da1d33a | 1468 | log_err("fio: short write on compressed log\n"); |
44edeed3 EV |
1469 | ret = 1; |
1470 | goto out; | |
3da1d33a JA |
1471 | } |
1472 | ||
44edeed3 | 1473 | ret = 0; |
0cba0f91 JA |
1474 | } else { |
1475 | FILE *f; | |
14a74557 | 1476 | f = fopen((const char *) log_pathname, "w"); |
0cba0f91 | 1477 | if (!f) { |
21c9e728 | 1478 | log_err("fio: fopen log %s : %s\n", |
14a74557 | 1479 | log_pathname, strerror(errno)); |
44edeed3 EV |
1480 | ret = 1; |
1481 | goto out; | |
0cba0f91 JA |
1482 | } |
1483 | ||
868f6f03 KC |
1484 | if (pdu->log_type == IO_LOG_TYPE_HIST) { |
1485 | client_flush_hist_samples(f, pdu->log_hist_coarseness, pdu->samples, | |
1486 | pdu->nr_samples * sizeof(struct io_sample)); | |
1487 | } else { | |
1488 | flush_samples(f, pdu->samples, | |
1489 | pdu->nr_samples * sizeof(struct io_sample)); | |
1490 | } | |
0cba0f91 | 1491 | fclose(f); |
44edeed3 | 1492 | ret = 0; |
0cba0f91 | 1493 | } |
44edeed3 EV |
1494 | |
1495 | out: | |
e4651d46 | 1496 | if (pdu && pdu != (void *) cmd->payload) |
44edeed3 EV |
1497 | free(pdu); |
1498 | ||
1499 | if (log_pathname) | |
1500 | free(log_pathname); | |
1501 | ||
1502 | return ret; | |
bead01d7 JA |
1503 | } |
1504 | ||
b5296ddb | 1505 | static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd) |
2e03b4b2 | 1506 | { |
3989b143 | 1507 | struct cmd_probe_reply_pdu *probe = (struct cmd_probe_reply_pdu *) cmd->payload; |
d2333358 JA |
1508 | const char *os, *arch; |
1509 | char bit[16]; | |
2e03b4b2 | 1510 | |
cca84643 JA |
1511 | os = fio_get_os_string(probe->os); |
1512 | if (!os) | |
1513 | os = "unknown"; | |
1514 | ||
1515 | arch = fio_get_arch_string(probe->arch); | |
1516 | if (!arch) | |
1517 | os = "unknown"; | |
1518 | ||
d2333358 | 1519 | sprintf(bit, "%d-bit", probe->bpp * 8); |
3989b143 | 1520 | probe->flags = le64_to_cpu(probe->flags); |
38fdef22 | 1521 | |
2eca4892 | 1522 | if (output_format & FIO_OUTPUT_NORMAL) { |
44f4f5e6 TK |
1523 | log_info("hostname=%s, be=%u, %s, os=%s, arch=%s, fio=%s, flags=%lx\n", |
1524 | probe->hostname, probe->bigendian, bit, os, arch, | |
1525 | probe->fio_version, (unsigned long) probe->flags); | |
2eca4892 | 1526 | } |
b5296ddb JA |
1527 | |
1528 | if (!client->name) | |
1529 | client->name = strdup((char *) probe->hostname); | |
2e03b4b2 JA |
1530 | } |
1531 | ||
11e950bd JA |
1532 | static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd) |
1533 | { | |
1534 | struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload; | |
1535 | ||
1536 | client->state = Client_started; | |
1537 | client->jobs = le32_to_cpu(pdu->jobs); | |
a89d5319 | 1538 | client->nr_stat = le32_to_cpu(pdu->stat_outputs); |
108fea77 | 1539 | |
0279b880 JA |
1540 | if (client->jobs) { |
1541 | int i; | |
1542 | ||
1543 | if (client->opt_lists) | |
1544 | free(client->opt_lists); | |
1545 | ||
1546 | client->opt_lists = malloc(client->jobs * sizeof(struct flist_head)); | |
1547 | for (i = 0; i < client->jobs; i++) | |
1548 | INIT_FLIST_HEAD(&client->opt_lists[i]); | |
1549 | } | |
1550 | ||
a89d5319 | 1551 | sum_stat_clients += client->nr_stat; |
11e950bd JA |
1552 | } |
1553 | ||
36ed2f9a | 1554 | static void handle_stop(struct fio_client *client) |
11e950bd | 1555 | { |
498c92c2 JA |
1556 | if (client->error) |
1557 | log_info("client <%s>: exited with error %d\n", client->hostname, client->error); | |
11e950bd JA |
1558 | } |
1559 | ||
6b79c80c JA |
1560 | static void convert_stop(struct fio_net_cmd *cmd) |
1561 | { | |
1562 | struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload; | |
1563 | ||
1564 | pdu->error = le32_to_cpu(pdu->error); | |
1565 | } | |
1566 | ||
084d1c6f JA |
1567 | static void convert_text(struct fio_net_cmd *cmd) |
1568 | { | |
1569 | struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload; | |
1570 | ||
1571 | pdu->level = le32_to_cpu(pdu->level); | |
1572 | pdu->buf_len = le32_to_cpu(pdu->buf_len); | |
1573 | pdu->log_sec = le64_to_cpu(pdu->log_sec); | |
1574 | pdu->log_usec = le64_to_cpu(pdu->log_usec); | |
1575 | } | |
1576 | ||
3989b143 JA |
1577 | static struct cmd_iolog_pdu *convert_iolog_gz(struct fio_net_cmd *cmd, |
1578 | struct cmd_iolog_pdu *pdu) | |
1b42725f | 1579 | { |
3989b143 | 1580 | #ifdef CONFIG_ZLIB |
1b42725f | 1581 | struct cmd_iolog_pdu *ret; |
1b42725f | 1582 | z_stream stream; |
7df9ac89 | 1583 | uint64_t nr_samples; |
3989b143 | 1584 | size_t total; |
a9c9608e | 1585 | char *p; |
1b42725f JA |
1586 | |
1587 | stream.zalloc = Z_NULL; | |
1588 | stream.zfree = Z_NULL; | |
1589 | stream.opaque = Z_NULL; | |
1590 | stream.avail_in = 0; | |
1591 | stream.next_in = Z_NULL; | |
1592 | ||
1593 | if (inflateInit(&stream) != Z_OK) | |
1594 | return NULL; | |
1595 | ||
1596 | /* | |
f5ed765a | 1597 | * Get header first, it's not compressed |
1b42725f | 1598 | */ |
3c865f32 | 1599 | nr_samples = le64_to_cpu(pdu->nr_samples); |
1b42725f | 1600 | |
868f6f03 KC |
1601 | if (pdu->log_type == IO_LOG_TYPE_HIST) |
1602 | total = nr_samples * (__log_entry_sz(le32_to_cpu(pdu->log_offset)) + | |
1603 | sizeof(struct io_u_plat_entry)); | |
1604 | else | |
1605 | total = nr_samples * __log_entry_sz(le32_to_cpu(pdu->log_offset)); | |
f5ed765a | 1606 | ret = malloc(total + sizeof(*pdu)); |
1b42725f | 1607 | ret->nr_samples = nr_samples; |
3989b143 JA |
1608 | |
1609 | memcpy(ret, pdu, sizeof(*pdu)); | |
f5ed765a | 1610 | |
a9c9608e | 1611 | p = (char *) ret + sizeof(*pdu); |
1b42725f | 1612 | |
f5ed765a | 1613 | stream.avail_in = cmd->pdu_len - sizeof(*pdu); |
a9c9608e | 1614 | stream.next_in = (void *)((char *) pdu + sizeof(*pdu)); |
1b42725f JA |
1615 | while (stream.avail_in) { |
1616 | unsigned int this_chunk = 65536; | |
1617 | unsigned int this_len; | |
1618 | int err; | |
1619 | ||
1620 | if (this_chunk > total) | |
1621 | this_chunk = total; | |
1622 | ||
1623 | stream.avail_out = this_chunk; | |
a9c9608e | 1624 | stream.next_out = (void *)p; |
1b42725f | 1625 | err = inflate(&stream, Z_NO_FLUSH); |
3c547fe0 JA |
1626 | /* may be Z_OK, or Z_STREAM_END */ |
1627 | if (err < 0) { | |
2f75f022 JF |
1628 | /* |
1629 | * Z_STREAM_ERROR and Z_BUF_ERROR can safely be | |
1630 | * ignored */ | |
1631 | if (err == Z_STREAM_ERROR || err == Z_BUF_ERROR) | |
1632 | break; | |
1b42725f | 1633 | log_err("fio: inflate error %d\n", err); |
f5ed765a JA |
1634 | free(ret); |
1635 | ret = NULL; | |
3989b143 | 1636 | goto err; |
1b42725f JA |
1637 | } |
1638 | ||
1639 | this_len = this_chunk - stream.avail_out; | |
1640 | p += this_len; | |
1641 | total -= this_len; | |
1642 | } | |
1643 | ||
3989b143 JA |
1644 | err: |
1645 | inflateEnd(&stream); | |
1646 | return ret; | |
1647 | #else | |
1648 | return NULL; | |
1649 | #endif | |
1650 | } | |
1651 | ||
1652 | /* | |
1653 | * This has been compressed on the server side, since it can be big. | |
1654 | * Uncompress here. | |
1655 | */ | |
0cba0f91 JA |
1656 | static struct cmd_iolog_pdu *convert_iolog(struct fio_net_cmd *cmd, |
1657 | bool *store_direct) | |
3989b143 JA |
1658 | { |
1659 | struct cmd_iolog_pdu *pdu = (struct cmd_iolog_pdu *) cmd->payload; | |
1660 | struct cmd_iolog_pdu *ret; | |
ae588852 | 1661 | uint64_t i; |
0cba0f91 | 1662 | int compressed; |
ae588852 | 1663 | void *samples; |
3989b143 | 1664 | |
0cba0f91 JA |
1665 | *store_direct = false; |
1666 | ||
3989b143 JA |
1667 | /* |
1668 | * Convert if compressed and we support it. If it's not | |
1669 | * compressed, we need not do anything. | |
1670 | */ | |
0cba0f91 JA |
1671 | compressed = le32_to_cpu(pdu->compressed); |
1672 | if (compressed == XMIT_COMPRESSED) { | |
3989b143 JA |
1673 | #ifndef CONFIG_ZLIB |
1674 | log_err("fio: server sent compressed data by mistake\n"); | |
1675 | return NULL; | |
1676 | #endif | |
1677 | ret = convert_iolog_gz(cmd, pdu); | |
1678 | if (!ret) { | |
1679 | log_err("fio: failed decompressing log\n"); | |
1680 | return NULL; | |
1681 | } | |
0cba0f91 JA |
1682 | } else if (compressed == STORE_COMPRESSED) { |
1683 | *store_direct = true; | |
1684 | ret = pdu; | |
3989b143 JA |
1685 | } else |
1686 | ret = pdu; | |
1687 | ||
ae588852 | 1688 | ret->nr_samples = le64_to_cpu(ret->nr_samples); |
3989b143 | 1689 | ret->thread_number = le32_to_cpu(ret->thread_number); |
3989b143 JA |
1690 | ret->log_type = le32_to_cpu(ret->log_type); |
1691 | ret->compressed = le32_to_cpu(ret->compressed); | |
ae588852 | 1692 | ret->log_offset = le32_to_cpu(ret->log_offset); |
868f6f03 | 1693 | ret->log_hist_coarseness = le32_to_cpu(ret->log_hist_coarseness); |
3989b143 | 1694 | |
0cba0f91 JA |
1695 | if (*store_direct) |
1696 | return ret; | |
1697 | ||
3310fced | 1698 | samples = &ret->samples[0]; |
f5ed765a | 1699 | for (i = 0; i < ret->nr_samples; i++) { |
ae588852 | 1700 | struct io_sample *s; |
f5ed765a | 1701 | |
ae588852 | 1702 | s = __get_sample(samples, ret->log_offset, i); |
868f6f03 | 1703 | if (ret->log_type == IO_LOG_TYPE_HIST) |
a9c9608e | 1704 | s = (struct io_sample *)((char *)s + sizeof(struct io_u_plat_entry) * i); |
868f6f03 | 1705 | |
b26317c9 | 1706 | s->time = le64_to_cpu(s->time); |
af2fde19 | 1707 | s->data.val = le64_to_cpu(s->data.val); |
b2a432bf | 1708 | s->__ddir = __le32_to_cpu(s->__ddir); |
5fff9543 | 1709 | s->bs = le64_to_cpu(s->bs); |
ae588852 JA |
1710 | |
1711 | if (ret->log_offset) { | |
1712 | struct io_sample_offset *so = (void *) s; | |
1713 | ||
1714 | so->offset = le64_to_cpu(so->offset); | |
1715 | } | |
868f6f03 KC |
1716 | |
1717 | if (ret->log_type == IO_LOG_TYPE_HIST) { | |
a9c9608e | 1718 | s->data.plat_entry = (struct io_u_plat_entry *)(((char *)s) + sizeof(*s)); |
af2fde19 SW |
1719 | s->data.plat_entry->list.next = NULL; |
1720 | s->data.plat_entry->list.prev = NULL; | |
868f6f03 | 1721 | } |
f5ed765a JA |
1722 | } |
1723 | ||
1b42725f JA |
1724 | return ret; |
1725 | } | |
1726 | ||
ca09be4b JA |
1727 | static void sendfile_reply(int fd, struct cmd_sendfile_reply *rep, |
1728 | size_t size, uint64_t tag) | |
1729 | { | |
1730 | rep->error = cpu_to_le32(rep->error); | |
1731 | fio_net_send_cmd(fd, FIO_NET_CMD_SENDFILE, rep, size, &tag, NULL); | |
1732 | } | |
1733 | ||
0748d801 JA |
1734 | static int fio_send_file(struct fio_client *client, struct cmd_sendfile *pdu, |
1735 | uint64_t tag) | |
ca09be4b JA |
1736 | { |
1737 | struct cmd_sendfile_reply *rep; | |
1738 | struct stat sb; | |
1739 | size_t size; | |
1740 | int fd; | |
1741 | ||
1742 | size = sizeof(*rep); | |
1743 | rep = malloc(size); | |
1744 | ||
1745 | if (stat((char *)pdu->path, &sb) < 0) { | |
1746 | fail: | |
1747 | rep->error = errno; | |
1748 | sendfile_reply(client->fd, rep, size, tag); | |
1749 | free(rep); | |
1750 | return 1; | |
1751 | } | |
1752 | ||
1753 | size += sb.st_size; | |
1754 | rep = realloc(rep, size); | |
1755 | rep->size = cpu_to_le32((uint32_t) sb.st_size); | |
1756 | ||
1757 | fd = open((char *)pdu->path, O_RDONLY); | |
1758 | if (fd == -1 ) | |
1759 | goto fail; | |
1760 | ||
1761 | rep->error = read_data(fd, &rep->data, sb.st_size); | |
1762 | sendfile_reply(client->fd, rep, size, tag); | |
1763 | free(rep); | |
1764 | close(fd); | |
1765 | return 0; | |
1766 | } | |
1767 | ||
a5276616 | 1768 | int fio_handle_client(struct fio_client *client) |
37db14fe | 1769 | { |
a5276616 | 1770 | struct client_ops *ops = client->ops; |
37db14fe | 1771 | struct fio_net_cmd *cmd; |
bb49c8bd | 1772 | int size; |
37db14fe | 1773 | |
60efd14e JA |
1774 | dprint(FD_NET, "client: handle %s\n", client->hostname); |
1775 | ||
54e62ad4 | 1776 | cmd = fio_net_recv_cmd(client->fd, true); |
e951bdc4 JA |
1777 | if (!cmd) |
1778 | return 0; | |
c2c94585 | 1779 | |
b9d2f30a JA |
1780 | dprint(FD_NET, "client: got cmd op %s from %s (pdu=%u)\n", |
1781 | fio_server_op(cmd->opcode), client->hostname, cmd->pdu_len); | |
46c48f1f | 1782 | |
47714d68 JA |
1783 | client->last_cmd = cmd->opcode; |
1784 | ||
e951bdc4 JA |
1785 | switch (cmd->opcode) { |
1786 | case FIO_NET_CMD_QUIT: | |
3ec62ec4 | 1787 | if (ops->quit) |
35c0ba7f | 1788 | ops->quit(client, cmd); |
e951bdc4 | 1789 | remove_client(client); |
e951bdc4 | 1790 | break; |
084d1c6f JA |
1791 | case FIO_NET_CMD_TEXT: |
1792 | convert_text(cmd); | |
35c0ba7f | 1793 | ops->text(client, cmd); |
e951bdc4 | 1794 | break; |
3bf236c0 JA |
1795 | case FIO_NET_CMD_DU: { |
1796 | struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload; | |
1797 | ||
1798 | convert_dus(&du->dus); | |
1799 | convert_agg(&du->agg); | |
1800 | ||
dd366728 | 1801 | ops->disk_util(client, cmd); |
d09a64a0 | 1802 | break; |
3bf236c0 JA |
1803 | } |
1804 | case FIO_NET_CMD_TS: { | |
1805 | struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload; | |
1806 | ||
bb49c8bd | 1807 | dprint(FD_NET, "client: ts->ss_state = %u\n", (unsigned int) le32_to_cpu(p->ts.ss_state)); |
c8caba48 | 1808 | if (le32_to_cpu(p->ts.ss_state) & FIO_SS_DATA) { |
bb49c8bd VF |
1809 | dprint(FD_NET, "client: received steadystate ring buffers\n"); |
1810 | ||
1811 | size = le64_to_cpu(p->ts.ss_dur); | |
1812 | p->ts.ss_iops_data = (uint64_t *) ((struct cmd_ts_pdu *)cmd->payload + 1); | |
1813 | p->ts.ss_bw_data = p->ts.ss_iops_data + size; | |
1814 | } | |
1815 | ||
3bf236c0 JA |
1816 | convert_ts(&p->ts, &p->ts); |
1817 | convert_gs(&p->rs, &p->rs); | |
1818 | ||
89e5fad9 | 1819 | ops->thread_status(client, cmd); |
e951bdc4 | 1820 | break; |
3bf236c0 JA |
1821 | } |
1822 | case FIO_NET_CMD_GS: { | |
1823 | struct group_run_stats *gs = (struct group_run_stats *) cmd->payload; | |
1824 | ||
1825 | convert_gs(gs, gs); | |
1826 | ||
89e5fad9 | 1827 | ops->group_stats(client, cmd); |
e951bdc4 | 1828 | break; |
3bf236c0 JA |
1829 | } |
1830 | case FIO_NET_CMD_ETA: { | |
1831 | struct jobs_eta *je = (struct jobs_eta *) cmd->payload; | |
1832 | ||
308d69b5 JA |
1833 | if (!remove_reply_cmd(client, cmd)) |
1834 | break; | |
3bf236c0 | 1835 | convert_jobs_eta(je); |
a5276616 | 1836 | handle_eta(client, cmd); |
e951bdc4 | 1837 | break; |
3bf236c0 | 1838 | } |
e951bdc4 | 1839 | case FIO_NET_CMD_PROBE: |
89c1707c | 1840 | remove_reply_cmd(client, cmd); |
dd366728 | 1841 | ops->probe(client, cmd); |
e951bdc4 | 1842 | break; |
5d7793aa | 1843 | case FIO_NET_CMD_SERVER_START: |
01be038e | 1844 | client->state = Client_running; |
85dd01e7 JA |
1845 | if (ops->job_start) |
1846 | ops->job_start(client, cmd); | |
01be038e | 1847 | break; |
85dd01e7 JA |
1848 | case FIO_NET_CMD_START: { |
1849 | struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload; | |
1850 | ||
1851 | pdu->jobs = le32_to_cpu(pdu->jobs); | |
1852 | ops->start(client, cmd); | |
e951bdc4 | 1853 | break; |
85dd01e7 | 1854 | } |
6b79c80c JA |
1855 | case FIO_NET_CMD_STOP: { |
1856 | struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload; | |
1857 | ||
1858 | convert_stop(cmd); | |
1859 | client->state = Client_stopped; | |
122c7725 JA |
1860 | client->error = le32_to_cpu(pdu->error); |
1861 | client->signal = le32_to_cpu(pdu->signal); | |
36ed2f9a | 1862 | ops->stop(client); |
e951bdc4 | 1863 | break; |
6b79c80c | 1864 | } |
40c60516 JA |
1865 | case FIO_NET_CMD_ADD_JOB: { |
1866 | struct cmd_add_job_pdu *pdu = (struct cmd_add_job_pdu *) cmd->payload; | |
1867 | ||
1868 | client->thread_number = le32_to_cpu(pdu->thread_number); | |
1869 | client->groupid = le32_to_cpu(pdu->groupid); | |
1870 | ||
807f9971 JA |
1871 | if (ops->add_job) |
1872 | ops->add_job(client, cmd); | |
807f9971 | 1873 | break; |
40c60516 | 1874 | } |
1b42725f | 1875 | case FIO_NET_CMD_IOLOG: |
0cba0f91 | 1876 | fio_client_handle_iolog(client, cmd); |
1b42725f | 1877 | break; |
40c60516 | 1878 | case FIO_NET_CMD_UPDATE_JOB: |
40c60516 | 1879 | ops->update_job(client, cmd); |
649cee91 | 1880 | remove_reply_cmd(client, cmd); |
40c60516 | 1881 | break; |
ca09be4b JA |
1882 | case FIO_NET_CMD_VTRIGGER: { |
1883 | struct all_io_list *pdu = (struct all_io_list *) cmd->payload; | |
d264264a JA |
1884 | char buf[128]; |
1885 | int off = 0; | |
ca09be4b | 1886 | |
d264264a JA |
1887 | if (aux_path) { |
1888 | strcpy(buf, aux_path); | |
1889 | off = strlen(buf); | |
1890 | } | |
1891 | ||
1892 | __verify_save_state(pdu, server_name(client, &buf[off], sizeof(buf) - off)); | |
13c70218 | 1893 | exec_trigger(trigger_cmd); |
ca09be4b JA |
1894 | break; |
1895 | } | |
1896 | case FIO_NET_CMD_SENDFILE: { | |
1897 | struct cmd_sendfile *pdu = (struct cmd_sendfile *) cmd->payload; | |
0748d801 | 1898 | fio_send_file(client, pdu, cmd->tag); |
ca09be4b JA |
1899 | break; |
1900 | } | |
0279b880 JA |
1901 | case FIO_NET_CMD_JOB_OPT: { |
1902 | handle_job_opt(client, cmd); | |
1903 | break; | |
1904 | } | |
e951bdc4 | 1905 | default: |
89c1707c | 1906 | log_err("fio: unknown client op: %s\n", fio_server_op(cmd->opcode)); |
e951bdc4 | 1907 | break; |
37db14fe JA |
1908 | } |
1909 | ||
ca09be4b | 1910 | free(cmd); |
e951bdc4 | 1911 | return 1; |
37db14fe | 1912 | } |
b66570dc | 1913 | |
ca09be4b JA |
1914 | int fio_clients_send_trigger(const char *cmd) |
1915 | { | |
1916 | struct flist_head *entry; | |
1917 | struct fio_client *client; | |
1918 | size_t slen; | |
1919 | ||
1920 | dprint(FD_NET, "client: send vtrigger: %s\n", cmd); | |
1921 | ||
1922 | if (!cmd) | |
1923 | slen = 0; | |
1924 | else | |
1925 | slen = strlen(cmd); | |
1926 | ||
1927 | flist_for_each(entry, &client_list) { | |
1928 | struct cmd_vtrigger_pdu *pdu; | |
1929 | ||
1930 | client = flist_entry(entry, struct fio_client, list); | |
1931 | ||
1932 | pdu = malloc(sizeof(*pdu) + slen); | |
1933 | pdu->len = cpu_to_le16((uint16_t) slen); | |
1934 | if (slen) | |
1935 | memcpy(pdu->cmd, cmd, slen); | |
1936 | fio_net_send_cmd(client->fd, FIO_NET_CMD_VTRIGGER, pdu, | |
1937 | sizeof(*pdu) + slen, NULL, NULL); | |
1938 | free(pdu); | |
1939 | } | |
1940 | ||
1941 | return 0; | |
1942 | } | |
1943 | ||
a5276616 | 1944 | static void request_client_etas(struct client_ops *ops) |
af9c9fb3 JA |
1945 | { |
1946 | struct fio_client *client; | |
1947 | struct flist_head *entry; | |
1948 | struct client_eta *eta; | |
82c1ed38 | 1949 | int skipped = 0; |
af9c9fb3 | 1950 | |
5610bcd8 JA |
1951 | if (eta_print == FIO_ETA_NEVER) |
1952 | return; | |
1953 | ||
af9c9fb3 JA |
1954 | dprint(FD_NET, "client: request eta (%d)\n", nr_clients); |
1955 | ||
afe22d3d | 1956 | eta = calloc(1, sizeof(*eta) + __THREAD_RUNSTR_SZ(REAL_MAX_JOBS)); |
af9c9fb3 JA |
1957 | eta->pending = nr_clients; |
1958 | ||
1959 | flist_for_each(entry, &client_list) { | |
1960 | client = flist_entry(entry, struct fio_client, list); | |
1961 | ||
82c1ed38 JA |
1962 | if (!flist_empty(&client->eta_list)) { |
1963 | skipped++; | |
1964 | continue; | |
1965 | } | |
01be038e JA |
1966 | if (client->state != Client_running) |
1967 | continue; | |
82c1ed38 | 1968 | |
f77d2676 | 1969 | assert(!client->eta_in_flight); |
82c1ed38 | 1970 | flist_add_tail(&client->eta_list, &eta_list); |
f77d2676 | 1971 | client->eta_in_flight = eta; |
af9c9fb3 | 1972 | fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA, |
df380934 | 1973 | (uintptr_t) eta, &client->cmd_list); |
af9c9fb3 JA |
1974 | } |
1975 | ||
f02f7dac JA |
1976 | while (skipped--) { |
1977 | if (!fio_client_dec_jobs_eta(eta, ops->eta)) | |
1978 | break; | |
1979 | } | |
82c1ed38 | 1980 | |
af9c9fb3 JA |
1981 | dprint(FD_NET, "client: requested eta tag %p\n", eta); |
1982 | } | |
1983 | ||
a64c13a4 JA |
1984 | /* |
1985 | * A single SEND_ETA timeout isn't fatal. Attempt to recover. | |
1986 | */ | |
1987 | static int handle_cmd_timeout(struct fio_client *client, | |
1988 | struct fio_net_cmd_reply *reply) | |
1989 | { | |
7d9407b7 EV |
1990 | uint16_t reply_opcode = reply->opcode; |
1991 | ||
be6fa258 JA |
1992 | flist_del(&reply->list); |
1993 | free(reply); | |
1994 | ||
7d9407b7 | 1995 | if (reply_opcode != FIO_NET_CMD_SEND_ETA) |
a64c13a4 JA |
1996 | return 1; |
1997 | ||
1998 | log_info("client <%s>: timeout on SEND_ETA\n", client->hostname); | |
a64c13a4 JA |
1999 | |
2000 | flist_del_init(&client->eta_list); | |
2001 | if (client->eta_in_flight) { | |
2002 | fio_client_dec_jobs_eta(client->eta_in_flight, client->ops->eta); | |
2003 | client->eta_in_flight = NULL; | |
2004 | } | |
2005 | ||
2006 | /* | |
2007 | * If we fail 5 in a row, give up... | |
2008 | */ | |
2009 | if (client->eta_timeouts++ > 5) | |
2010 | return 1; | |
2011 | ||
2012 | return 0; | |
2013 | } | |
2014 | ||
89c1707c | 2015 | static int client_check_cmd_timeout(struct fio_client *client, |
8b6a404c | 2016 | struct timespec *now) |
89c1707c | 2017 | { |
40c60516 | 2018 | struct fio_net_cmd_reply *reply; |
89c1707c JA |
2019 | struct flist_head *entry, *tmp; |
2020 | int ret = 0; | |
2021 | ||
2022 | flist_for_each_safe(entry, tmp, &client->cmd_list) { | |
1d484506 JA |
2023 | unsigned int op; |
2024 | ||
40c60516 | 2025 | reply = flist_entry(entry, struct fio_net_cmd_reply, list); |
89c1707c | 2026 | |
8b6a404c | 2027 | if (mtime_since(&reply->ts, now) < FIO_NET_CLIENT_TIMEOUT) |
89c1707c JA |
2028 | continue; |
2029 | ||
1d484506 | 2030 | op = reply->opcode; |
a64c13a4 JA |
2031 | if (!handle_cmd_timeout(client, reply)) |
2032 | continue; | |
2033 | ||
89c1707c | 2034 | log_err("fio: client %s, timeout on cmd %s\n", client->hostname, |
1d484506 | 2035 | fio_server_op(op)); |
89c1707c JA |
2036 | ret = 1; |
2037 | } | |
2038 | ||
2039 | return flist_empty(&client->cmd_list) && ret; | |
2040 | } | |
2041 | ||
a5276616 | 2042 | static int fio_check_clients_timed_out(void) |
89c1707c JA |
2043 | { |
2044 | struct fio_client *client; | |
2045 | struct flist_head *entry, *tmp; | |
8b6a404c | 2046 | struct timespec ts; |
89c1707c JA |
2047 | int ret = 0; |
2048 | ||
8b6a404c | 2049 | fio_gettime(&ts, NULL); |
89c1707c JA |
2050 | |
2051 | flist_for_each_safe(entry, tmp, &client_list) { | |
2052 | client = flist_entry(entry, struct fio_client, list); | |
2053 | ||
2054 | if (flist_empty(&client->cmd_list)) | |
2055 | continue; | |
2056 | ||
8b6a404c | 2057 | if (!client_check_cmd_timeout(client, &ts)) |
89c1707c JA |
2058 | continue; |
2059 | ||
a5276616 JA |
2060 | if (client->ops->timed_out) |
2061 | client->ops->timed_out(client); | |
ed727a46 JA |
2062 | else |
2063 | log_err("fio: client %s timed out\n", client->hostname); | |
2064 | ||
47714d68 JA |
2065 | if (client->last_cmd != FIO_NET_CMD_VTRIGGER) |
2066 | client->error = ETIMEDOUT; | |
2067 | else | |
2068 | log_info("fio: ignoring timeout due to vtrigger\n"); | |
89c1707c JA |
2069 | remove_client(client); |
2070 | ret = 1; | |
2071 | } | |
2072 | ||
2073 | return ret; | |
2074 | } | |
2075 | ||
dd366728 | 2076 | int fio_handle_clients(struct client_ops *ops) |
b66570dc | 2077 | { |
b66570dc | 2078 | struct pollfd *pfds; |
498c92c2 | 2079 | int i, ret = 0, retval = 0; |
b66570dc | 2080 | |
8b6a404c | 2081 | fio_gettime(&eta_ts, NULL); |
af9c9fb3 | 2082 | |
b66570dc JA |
2083 | pfds = malloc(nr_clients * sizeof(struct pollfd)); |
2084 | ||
37f0c1ae JA |
2085 | init_thread_stat(&client_ts); |
2086 | init_group_run_stat(&client_gs); | |
2087 | ||
82a4be1b | 2088 | while (!exit_backend && nr_clients) { |
c2cb6869 JA |
2089 | struct flist_head *entry, *tmp; |
2090 | struct fio_client *client; | |
2091 | ||
82a4be1b | 2092 | i = 0; |
c2cb6869 | 2093 | flist_for_each_safe(entry, tmp, &client_list) { |
82a4be1b | 2094 | client = flist_entry(entry, struct fio_client, list); |
b66570dc | 2095 | |
a5276616 | 2096 | if (!client->sent_job && !client->ops->stay_connected && |
c2cb6869 JA |
2097 | flist_empty(&client->cmd_list)) { |
2098 | remove_client(client); | |
2099 | continue; | |
2100 | } | |
2101 | ||
82a4be1b JA |
2102 | pfds[i].fd = client->fd; |
2103 | pfds[i].events = POLLIN; | |
2104 | i++; | |
2105 | } | |
2106 | ||
c2cb6869 JA |
2107 | if (!nr_clients) |
2108 | break; | |
2109 | ||
82a4be1b | 2110 | assert(i == nr_clients); |
b66570dc | 2111 | |
5c2857f9 | 2112 | do { |
8b6a404c | 2113 | struct timespec ts; |
ca09be4b | 2114 | int timeout; |
af9c9fb3 | 2115 | |
8b6a404c | 2116 | fio_gettime(&ts, NULL); |
db37d890 | 2117 | if (eta_time_within_slack(mtime_since(&eta_ts, &ts))) { |
a5276616 | 2118 | request_client_etas(ops); |
8b6a404c | 2119 | memcpy(&eta_ts, &ts, sizeof(ts)); |
89c1707c | 2120 | |
a5276616 | 2121 | if (fio_check_clients_timed_out()) |
89c1707c | 2122 | break; |
af9c9fb3 JA |
2123 | } |
2124 | ||
ca09be4b JA |
2125 | check_trigger_file(); |
2126 | ||
2127 | timeout = min(100u, ops->eta_msec); | |
2128 | ||
2129 | ret = poll(pfds, nr_clients, timeout); | |
5c2857f9 JA |
2130 | if (ret < 0) { |
2131 | if (errno == EINTR) | |
2132 | continue; | |
2133 | log_err("fio: poll clients: %s\n", strerror(errno)); | |
2134 | break; | |
2135 | } else if (!ret) | |
b66570dc | 2136 | continue; |
5c2857f9 | 2137 | } while (ret <= 0); |
b66570dc JA |
2138 | |
2139 | for (i = 0; i < nr_clients; i++) { | |
2140 | if (!(pfds[i].revents & POLLIN)) | |
2141 | continue; | |
2142 | ||
2143 | client = find_client_by_fd(pfds[i].fd); | |
2144 | if (!client) { | |
65e1a120 | 2145 | log_err("fio: unknown client fd %ld\n", (long) pfds[i].fd); |
b66570dc JA |
2146 | continue; |
2147 | } | |
a5276616 | 2148 | if (!fio_handle_client(client)) { |
28d3ab07 JA |
2149 | log_info("client: host=%s disconnected\n", |
2150 | client->hostname); | |
2151 | remove_client(client); | |
498c92c2 | 2152 | retval = 1; |
38990764 | 2153 | } else if (client->error) |
498c92c2 | 2154 | retval = 1; |
343cb4a9 | 2155 | fio_put_client(client); |
b66570dc JA |
2156 | } |
2157 | } | |
2158 | ||
c2b5ddb2 VF |
2159 | log_info_buf(allclients.buf, allclients.buflen); |
2160 | buf_output_free(&allclients); | |
2161 | ||
952b05e0 CF |
2162 | fio_client_json_fini(); |
2163 | ||
b66570dc | 2164 | free(pfds); |
f9a33cc0 | 2165 | return retval || error_clients; |
b66570dc | 2166 | } |
44f4f5e6 TK |
2167 | |
2168 | static void client_display_thread_status(struct jobs_eta *je) | |
2169 | { | |
2170 | if (!(output_format & FIO_OUTPUT_JSON)) | |
2171 | display_thread_status(je); | |
2172 | } |