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