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