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