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