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