Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 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> |
Jens Axboe | d05c4a0 | 2011-10-05 00:16:11 +0200 | [diff] [blame] | 11 | #include <sys/socket.h> |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 12 | #include <sys/un.h> |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 13 | #include <netinet/in.h> |
| 14 | #include <arpa/inet.h> |
| 15 | #include <netdb.h> |
Jens Axboe | 9e22ecb | 2011-10-04 14:50:21 +0200 | [diff] [blame] | 16 | #include <signal.h> |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 17 | |
| 18 | #include "fio.h" |
Stephen M. Cameron | dd36672 | 2012-02-24 08:17:30 +0100 | [diff] [blame] | 19 | #include "client.h" |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 20 | #include "server.h" |
Jens Axboe | b66570d | 2011-10-01 11:11:35 -0600 | [diff] [blame] | 21 | #include "flist.h" |
Jens Axboe | 3c5f57e | 2011-10-06 12:37:50 +0200 | [diff] [blame] | 22 | #include "hash.h" |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 23 | |
Stephen M. Cameron | dd36672 | 2012-02-24 08:17:30 +0100 | [diff] [blame] | 24 | static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd); |
Jens Axboe | 89e5fad | 2012-03-05 09:21:12 +0100 | [diff] [blame] | 25 | static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd); |
| 26 | static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd); |
Stephen M. Cameron | dd36672 | 2012-02-24 08:17:30 +0100 | [diff] [blame] | 27 | static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd); |
Jens Axboe | 084d1c6 | 2012-03-03 20:28:07 +0100 | [diff] [blame] | 28 | static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd); |
Jens Axboe | 6b79c80 | 2012-03-08 10:51:36 +0100 | [diff] [blame] | 29 | static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd); |
Stephen M. Cameron | dd36672 | 2012-02-24 08:17:30 +0100 | [diff] [blame] | 30 | |
| 31 | struct client_ops fio_client_ops = { |
Jens Axboe | 084d1c6 | 2012-03-03 20:28:07 +0100 | [diff] [blame] | 32 | .text_op = handle_text, |
Jens Axboe | 0420ba6 | 2012-02-29 11:16:52 +0100 | [diff] [blame] | 33 | .disk_util = handle_du, |
| 34 | .thread_status = handle_ts, |
| 35 | .group_stats = handle_gs, |
Jens Axboe | 6b79c80 | 2012-03-08 10:51:36 +0100 | [diff] [blame] | 36 | .stop = handle_stop, |
Jens Axboe | a527661 | 2012-03-04 15:15:08 +0100 | [diff] [blame] | 37 | .eta = display_thread_status, |
Jens Axboe | 0420ba6 | 2012-02-29 11:16:52 +0100 | [diff] [blame] | 38 | .probe = handle_probe, |
Stephen M. Cameron | dd36672 | 2012-02-24 08:17:30 +0100 | [diff] [blame] | 39 | }; |
| 40 | |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 41 | static struct timeval eta_tv; |
Jens Axboe | 48fbb46 | 2011-10-09 12:19:08 +0200 | [diff] [blame] | 42 | |
Jens Axboe | b66570d | 2011-10-01 11:11:35 -0600 | [diff] [blame] | 43 | static FLIST_HEAD(client_list); |
Jens Axboe | 82c1ed3 | 2011-10-10 08:56:18 +0200 | [diff] [blame] | 44 | static FLIST_HEAD(eta_list); |
Jens Axboe | b66570d | 2011-10-01 11:11:35 -0600 | [diff] [blame] | 45 | |
Jens Axboe | 3f3a454 | 2011-10-10 21:11:09 +0200 | [diff] [blame] | 46 | static FLIST_HEAD(arg_list); |
| 47 | |
Jens Axboe | 3650a3c | 2012-03-05 14:09:03 +0100 | [diff] [blame] | 48 | struct thread_stat client_ts; |
| 49 | struct group_run_stats client_gs; |
| 50 | int sum_stat_clients; |
| 51 | |
Jens Axboe | 37f0c1a | 2011-10-11 14:08:33 +0200 | [diff] [blame] | 52 | static int sum_stat_nr; |
| 53 | |
Jens Axboe | 3c5f57e | 2011-10-06 12:37:50 +0200 | [diff] [blame] | 54 | #define FIO_CLIENT_HASH_BITS 7 |
| 55 | #define FIO_CLIENT_HASH_SZ (1 << FIO_CLIENT_HASH_BITS) |
| 56 | #define FIO_CLIENT_HASH_MASK (FIO_CLIENT_HASH_SZ - 1) |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 57 | static struct flist_head client_hash[FIO_CLIENT_HASH_SZ]; |
Jens Axboe | 3c5f57e | 2011-10-06 12:37:50 +0200 | [diff] [blame] | 58 | |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 59 | static void fio_client_add_hash(struct fio_client *client) |
Jens Axboe | 3c5f57e | 2011-10-06 12:37:50 +0200 | [diff] [blame] | 60 | { |
| 61 | int bucket = hash_long(client->fd, FIO_CLIENT_HASH_BITS); |
| 62 | |
| 63 | bucket &= FIO_CLIENT_HASH_MASK; |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 64 | flist_add(&client->hash_list, &client_hash[bucket]); |
Jens Axboe | 3c5f57e | 2011-10-06 12:37:50 +0200 | [diff] [blame] | 65 | } |
| 66 | |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 67 | static void fio_client_remove_hash(struct fio_client *client) |
Jens Axboe | 3c5f57e | 2011-10-06 12:37:50 +0200 | [diff] [blame] | 68 | { |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 69 | if (!flist_empty(&client->hash_list)) |
| 70 | flist_del_init(&client->hash_list); |
Jens Axboe | 3c5f57e | 2011-10-06 12:37:50 +0200 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | static void fio_init fio_client_hash_init(void) |
| 74 | { |
| 75 | int i; |
| 76 | |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 77 | for (i = 0; i < FIO_CLIENT_HASH_SZ; i++) |
| 78 | INIT_FLIST_HEAD(&client_hash[i]); |
Jens Axboe | 3c5f57e | 2011-10-06 12:37:50 +0200 | [diff] [blame] | 79 | } |
| 80 | |
Jens Axboe | b66570d | 2011-10-01 11:11:35 -0600 | [diff] [blame] | 81 | static struct fio_client *find_client_by_fd(int fd) |
| 82 | { |
Jens Axboe | 3c5f57e | 2011-10-06 12:37:50 +0200 | [diff] [blame] | 83 | int bucket = hash_long(fd, FIO_CLIENT_HASH_BITS) & FIO_CLIENT_HASH_MASK; |
Jens Axboe | b66570d | 2011-10-01 11:11:35 -0600 | [diff] [blame] | 84 | struct fio_client *client; |
| 85 | struct flist_head *entry; |
| 86 | |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 87 | flist_for_each(entry, &client_hash[bucket]) { |
| 88 | client = flist_entry(entry, struct fio_client, hash_list); |
Jens Axboe | b66570d | 2011-10-01 11:11:35 -0600 | [diff] [blame] | 89 | |
Jens Axboe | 5121a9a | 2012-03-05 13:32:47 +0100 | [diff] [blame] | 90 | if (client->fd == fd) { |
| 91 | client->refs++; |
Jens Axboe | b66570d | 2011-10-01 11:11:35 -0600 | [diff] [blame] | 92 | return client; |
Jens Axboe | 5121a9a | 2012-03-05 13:32:47 +0100 | [diff] [blame] | 93 | } |
Jens Axboe | b66570d | 2011-10-01 11:11:35 -0600 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | return NULL; |
| 97 | } |
| 98 | |
Jens Axboe | b66570d | 2011-10-01 11:11:35 -0600 | [diff] [blame] | 99 | static void remove_client(struct fio_client *client) |
| 100 | { |
Jens Axboe | 5121a9a | 2012-03-05 13:32:47 +0100 | [diff] [blame] | 101 | assert(client->refs); |
| 102 | |
| 103 | if (--client->refs) |
| 104 | return; |
| 105 | |
Jens Axboe | 39e8e01 | 2011-10-04 13:46:08 +0200 | [diff] [blame] | 106 | dprint(FD_NET, "client: removed <%s>\n", client->hostname); |
Jens Axboe | b66570d | 2011-10-01 11:11:35 -0600 | [diff] [blame] | 107 | flist_del(&client->list); |
Jens Axboe | 3c5f57e | 2011-10-06 12:37:50 +0200 | [diff] [blame] | 108 | |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 109 | fio_client_remove_hash(client); |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 110 | |
Jens Axboe | 82c1ed3 | 2011-10-10 08:56:18 +0200 | [diff] [blame] | 111 | if (!flist_empty(&client->eta_list)) { |
| 112 | flist_del_init(&client->eta_list); |
Jens Axboe | a527661 | 2012-03-04 15:15:08 +0100 | [diff] [blame] | 113 | fio_client_dec_jobs_eta(client->eta_in_flight, client->ops->eta); |
Jens Axboe | 82c1ed3 | 2011-10-10 08:56:18 +0200 | [diff] [blame] | 114 | } |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 115 | |
Jens Axboe | b66570d | 2011-10-01 11:11:35 -0600 | [diff] [blame] | 116 | free(client->hostname); |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 117 | if (client->argv) |
| 118 | free(client->argv); |
Jens Axboe | b5296dd | 2011-10-07 16:35:56 +0200 | [diff] [blame] | 119 | if (client->name) |
| 120 | free(client->name); |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 121 | |
Jens Axboe | b66570d | 2011-10-01 11:11:35 -0600 | [diff] [blame] | 122 | free(client); |
Jens Axboe | 3c5f57e | 2011-10-06 12:37:50 +0200 | [diff] [blame] | 123 | nr_clients--; |
Jens Axboe | 5fd0acb | 2011-10-11 14:20:22 +0200 | [diff] [blame] | 124 | sum_stat_clients--; |
Jens Axboe | b66570d | 2011-10-01 11:11:35 -0600 | [diff] [blame] | 125 | } |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 126 | |
Jens Axboe | 5121a9a | 2012-03-05 13:32:47 +0100 | [diff] [blame] | 127 | static void put_client(struct fio_client *client) |
| 128 | { |
| 129 | remove_client(client); |
| 130 | } |
| 131 | |
Jens Axboe | fa2ea80 | 2011-10-08 21:07:29 +0200 | [diff] [blame] | 132 | static void __fio_client_add_cmd_option(struct fio_client *client, |
| 133 | const char *opt) |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 134 | { |
Jens Axboe | 39e8e01 | 2011-10-04 13:46:08 +0200 | [diff] [blame] | 135 | int index; |
| 136 | |
| 137 | index = client->argc++; |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 138 | client->argv = realloc(client->argv, sizeof(char *) * client->argc); |
Jens Axboe | 39e8e01 | 2011-10-04 13:46:08 +0200 | [diff] [blame] | 139 | client->argv[index] = strdup(opt); |
| 140 | dprint(FD_NET, "client: add cmd %d: %s\n", index, opt); |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 141 | } |
| 142 | |
Jens Axboe | fa2ea80 | 2011-10-08 21:07:29 +0200 | [diff] [blame] | 143 | void fio_client_add_cmd_option(void *cookie, const char *opt) |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 144 | { |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 145 | struct fio_client *client = cookie; |
Jens Axboe | 3f3a454 | 2011-10-10 21:11:09 +0200 | [diff] [blame] | 146 | struct flist_head *entry; |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 147 | |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 148 | if (!client || !opt) |
Jens Axboe | fa2ea80 | 2011-10-08 21:07:29 +0200 | [diff] [blame] | 149 | return; |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 150 | |
Jens Axboe | fa2ea80 | 2011-10-08 21:07:29 +0200 | [diff] [blame] | 151 | __fio_client_add_cmd_option(client, opt); |
Jens Axboe | 3f3a454 | 2011-10-10 21:11:09 +0200 | [diff] [blame] | 152 | |
| 153 | /* |
| 154 | * Duplicate arguments to shared client group |
| 155 | */ |
| 156 | flist_for_each(entry, &arg_list) { |
| 157 | client = flist_entry(entry, struct fio_client, arg_list); |
| 158 | |
| 159 | __fio_client_add_cmd_option(client, opt); |
| 160 | } |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 161 | } |
| 162 | |
Jens Axboe | a527661 | 2012-03-04 15:15:08 +0100 | [diff] [blame] | 163 | struct fio_client *fio_client_add_explicit(struct client_ops *ops, |
| 164 | const char *hostname, int type, |
Jens Axboe | 3ec62ec | 2012-03-01 12:01:29 +0100 | [diff] [blame] | 165 | int port) |
| 166 | { |
| 167 | struct fio_client *client; |
| 168 | |
| 169 | client = malloc(sizeof(*client)); |
| 170 | memset(client, 0, sizeof(*client)); |
| 171 | |
| 172 | INIT_FLIST_HEAD(&client->list); |
| 173 | INIT_FLIST_HEAD(&client->hash_list); |
| 174 | INIT_FLIST_HEAD(&client->arg_list); |
| 175 | INIT_FLIST_HEAD(&client->eta_list); |
| 176 | INIT_FLIST_HEAD(&client->cmd_list); |
| 177 | |
| 178 | client->hostname = strdup(hostname); |
| 179 | |
| 180 | if (type == Fio_client_socket) |
| 181 | client->is_sock = 1; |
| 182 | else { |
| 183 | int ipv6; |
| 184 | |
| 185 | ipv6 = type == Fio_client_ipv6; |
| 186 | if (fio_server_parse_host(hostname, &ipv6, |
| 187 | &client->addr.sin_addr, |
| 188 | &client->addr6.sin6_addr)) |
| 189 | goto err; |
| 190 | |
| 191 | client->port = port; |
| 192 | } |
| 193 | |
| 194 | client->fd = -1; |
Jens Axboe | a527661 | 2012-03-04 15:15:08 +0100 | [diff] [blame] | 195 | client->ops = ops; |
Jens Axboe | 5121a9a | 2012-03-05 13:32:47 +0100 | [diff] [blame] | 196 | client->refs = 1; |
Jens Axboe | 3ec62ec | 2012-03-01 12:01:29 +0100 | [diff] [blame] | 197 | |
| 198 | __fio_client_add_cmd_option(client, "fio"); |
| 199 | |
| 200 | flist_add(&client->list, &client_list); |
| 201 | nr_clients++; |
| 202 | dprint(FD_NET, "client: added <%s>\n", client->hostname); |
| 203 | return client; |
| 204 | err: |
| 205 | free(client); |
| 206 | return NULL; |
| 207 | } |
| 208 | |
Jens Axboe | a527661 | 2012-03-04 15:15:08 +0100 | [diff] [blame] | 209 | int fio_client_add(struct client_ops *ops, const char *hostname, void **cookie) |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 210 | { |
Jens Axboe | 3f3a454 | 2011-10-10 21:11:09 +0200 | [diff] [blame] | 211 | struct fio_client *existing = *cookie; |
Jens Axboe | b66570d | 2011-10-01 11:11:35 -0600 | [diff] [blame] | 212 | struct fio_client *client; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 213 | |
Jens Axboe | 3f3a454 | 2011-10-10 21:11:09 +0200 | [diff] [blame] | 214 | if (existing) { |
| 215 | /* |
| 216 | * We always add our "exec" name as the option, hence 1 |
| 217 | * means empty. |
| 218 | */ |
| 219 | if (existing->argc == 1) |
| 220 | flist_add_tail(&existing->arg_list, &arg_list); |
| 221 | else { |
| 222 | while (!flist_empty(&arg_list)) |
| 223 | flist_del_init(arg_list.next); |
| 224 | } |
| 225 | } |
| 226 | |
Jens Axboe | b66570d | 2011-10-01 11:11:35 -0600 | [diff] [blame] | 227 | client = malloc(sizeof(*client)); |
Jens Axboe | a37f69b | 2011-10-01 12:24:21 -0600 | [diff] [blame] | 228 | memset(client, 0, sizeof(*client)); |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 229 | |
Jens Axboe | 3c5f57e | 2011-10-06 12:37:50 +0200 | [diff] [blame] | 230 | INIT_FLIST_HEAD(&client->list); |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 231 | INIT_FLIST_HEAD(&client->hash_list); |
Jens Axboe | 3f3a454 | 2011-10-10 21:11:09 +0200 | [diff] [blame] | 232 | INIT_FLIST_HEAD(&client->arg_list); |
Jens Axboe | 82c1ed3 | 2011-10-10 08:56:18 +0200 | [diff] [blame] | 233 | INIT_FLIST_HEAD(&client->eta_list); |
Jens Axboe | 89c1707 | 2011-10-11 10:15:51 +0200 | [diff] [blame] | 234 | INIT_FLIST_HEAD(&client->cmd_list); |
Jens Axboe | 3c5f57e | 2011-10-06 12:37:50 +0200 | [diff] [blame] | 235 | |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 236 | if (fio_server_parse_string(hostname, &client->hostname, |
| 237 | &client->is_sock, &client->port, |
Jens Axboe | 811826b | 2011-10-24 09:11:50 +0200 | [diff] [blame] | 238 | &client->addr.sin_addr, |
| 239 | &client->addr6.sin6_addr, |
| 240 | &client->ipv6)) |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 241 | return -1; |
| 242 | |
Jens Axboe | a37f69b | 2011-10-01 12:24:21 -0600 | [diff] [blame] | 243 | client->fd = -1; |
Jens Axboe | a527661 | 2012-03-04 15:15:08 +0100 | [diff] [blame] | 244 | client->ops = ops; |
Jens Axboe | 5121a9a | 2012-03-05 13:32:47 +0100 | [diff] [blame] | 245 | client->refs = 1; |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 246 | |
| 247 | __fio_client_add_cmd_option(client, "fio"); |
| 248 | |
Jens Axboe | a37f69b | 2011-10-01 12:24:21 -0600 | [diff] [blame] | 249 | flist_add(&client->list, &client_list); |
| 250 | nr_clients++; |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 251 | dprint(FD_NET, "client: added <%s>\n", client->hostname); |
| 252 | *cookie = client; |
| 253 | return 0; |
Jens Axboe | a37f69b | 2011-10-01 12:24:21 -0600 | [diff] [blame] | 254 | } |
| 255 | |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 256 | static int fio_client_connect_ip(struct fio_client *client) |
Jens Axboe | a37f69b | 2011-10-01 12:24:21 -0600 | [diff] [blame] | 257 | { |
Jens Axboe | 811826b | 2011-10-24 09:11:50 +0200 | [diff] [blame] | 258 | struct sockaddr *addr; |
| 259 | fio_socklen_t socklen; |
| 260 | int fd, domain; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 261 | |
Jens Axboe | 811826b | 2011-10-24 09:11:50 +0200 | [diff] [blame] | 262 | if (client->ipv6) { |
| 263 | client->addr6.sin6_family = AF_INET6; |
| 264 | client->addr6.sin6_port = htons(client->port); |
| 265 | domain = AF_INET6; |
| 266 | addr = (struct sockaddr *) &client->addr6; |
| 267 | socklen = sizeof(client->addr6); |
| 268 | } else { |
| 269 | client->addr.sin_family = AF_INET; |
| 270 | client->addr.sin_port = htons(client->port); |
| 271 | domain = AF_INET; |
| 272 | addr = (struct sockaddr *) &client->addr; |
| 273 | socklen = sizeof(client->addr); |
| 274 | } |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 275 | |
Jens Axboe | 811826b | 2011-10-24 09:11:50 +0200 | [diff] [blame] | 276 | fd = socket(domain, SOCK_STREAM, 0); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 277 | if (fd < 0) { |
| 278 | log_err("fio: socket: %s\n", strerror(errno)); |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 279 | return -1; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 280 | } |
| 281 | |
Jens Axboe | 811826b | 2011-10-24 09:11:50 +0200 | [diff] [blame] | 282 | if (connect(fd, addr, socklen) < 0) { |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 283 | log_err("fio: connect: %s\n", strerror(errno)); |
Jens Axboe | a7de0a1 | 2011-10-07 12:55:14 +0200 | [diff] [blame] | 284 | log_err("fio: failed to connect to %s:%u\n", client->hostname, |
| 285 | client->port); |
Jens Axboe | b94cba4 | 2011-10-06 21:27:10 +0200 | [diff] [blame] | 286 | close(fd); |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 287 | return -1; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 288 | } |
| 289 | |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 290 | return fd; |
| 291 | } |
| 292 | |
| 293 | static int fio_client_connect_sock(struct fio_client *client) |
| 294 | { |
| 295 | struct sockaddr_un *addr = &client->addr_un; |
| 296 | fio_socklen_t len; |
| 297 | int fd; |
| 298 | |
| 299 | memset(addr, 0, sizeof(*addr)); |
| 300 | addr->sun_family = AF_UNIX; |
| 301 | strcpy(addr->sun_path, client->hostname); |
| 302 | |
| 303 | fd = socket(AF_UNIX, SOCK_STREAM, 0); |
| 304 | if (fd < 0) { |
| 305 | log_err("fio: socket: %s\n", strerror(errno)); |
| 306 | return -1; |
| 307 | } |
| 308 | |
| 309 | len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1; |
| 310 | if (connect(fd, (struct sockaddr *) addr, len) < 0) { |
| 311 | log_err("fio: connect; %s\n", strerror(errno)); |
Jens Axboe | b94cba4 | 2011-10-06 21:27:10 +0200 | [diff] [blame] | 312 | close(fd); |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 313 | return -1; |
| 314 | } |
| 315 | |
| 316 | return fd; |
| 317 | } |
| 318 | |
Jens Axboe | 2f99deb | 2012-03-09 14:37:29 +0100 | [diff] [blame^] | 319 | int fio_client_connect(struct fio_client *client) |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 320 | { |
| 321 | int fd; |
| 322 | |
| 323 | dprint(FD_NET, "client: connect to host %s\n", client->hostname); |
| 324 | |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 325 | if (client->is_sock) |
| 326 | fd = fio_client_connect_sock(client); |
| 327 | else |
| 328 | fd = fio_client_connect_ip(client); |
| 329 | |
Jens Axboe | 89c1707 | 2011-10-11 10:15:51 +0200 | [diff] [blame] | 330 | dprint(FD_NET, "client: %s connected %d\n", client->hostname, fd); |
| 331 | |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 332 | if (fd < 0) |
| 333 | return 1; |
| 334 | |
Jens Axboe | b66570d | 2011-10-01 11:11:35 -0600 | [diff] [blame] | 335 | client->fd = fd; |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 336 | fio_client_add_hash(client); |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 337 | client->state = Client_connected; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 338 | return 0; |
| 339 | } |
| 340 | |
Jens Axboe | 2f99deb | 2012-03-09 14:37:29 +0100 | [diff] [blame^] | 341 | void fio_client_terminate(struct fio_client *client) |
| 342 | { |
| 343 | fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_QUIT, 0, NULL); |
| 344 | } |
| 345 | |
Jens Axboe | cc0df00 | 2011-10-03 20:53:32 +0200 | [diff] [blame] | 346 | void fio_clients_terminate(void) |
| 347 | { |
| 348 | struct flist_head *entry; |
| 349 | struct fio_client *client; |
| 350 | |
Jens Axboe | 60efd14 | 2011-10-04 13:30:11 +0200 | [diff] [blame] | 351 | dprint(FD_NET, "client: terminate clients\n"); |
| 352 | |
Jens Axboe | cc0df00 | 2011-10-03 20:53:32 +0200 | [diff] [blame] | 353 | flist_for_each(entry, &client_list) { |
| 354 | client = flist_entry(entry, struct fio_client, list); |
Jens Axboe | 2f99deb | 2012-03-09 14:37:29 +0100 | [diff] [blame^] | 355 | fio_client_terminate(client); |
Jens Axboe | cc0df00 | 2011-10-03 20:53:32 +0200 | [diff] [blame] | 356 | } |
| 357 | } |
| 358 | |
| 359 | static void sig_int(int sig) |
| 360 | { |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 361 | dprint(FD_NET, "client: got signal %d\n", sig); |
Jens Axboe | cc0df00 | 2011-10-03 20:53:32 +0200 | [diff] [blame] | 362 | fio_clients_terminate(); |
| 363 | } |
| 364 | |
| 365 | static void client_signal_handler(void) |
| 366 | { |
| 367 | struct sigaction act; |
| 368 | |
| 369 | memset(&act, 0, sizeof(act)); |
| 370 | act.sa_handler = sig_int; |
| 371 | act.sa_flags = SA_RESTART; |
| 372 | sigaction(SIGINT, &act, NULL); |
| 373 | |
| 374 | memset(&act, 0, sizeof(act)); |
| 375 | act.sa_handler = sig_int; |
| 376 | act.sa_flags = SA_RESTART; |
| 377 | sigaction(SIGTERM, &act, NULL); |
| 378 | } |
| 379 | |
Jens Axboe | 0b8f30a | 2011-10-04 10:31:53 +0200 | [diff] [blame] | 380 | static void probe_client(struct fio_client *client) |
| 381 | { |
Jens Axboe | 60efd14 | 2011-10-04 13:30:11 +0200 | [diff] [blame] | 382 | dprint(FD_NET, "client: send probe\n"); |
| 383 | |
Jens Axboe | 89c1707 | 2011-10-11 10:15:51 +0200 | [diff] [blame] | 384 | fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_PROBE, 0, &client->cmd_list); |
Jens Axboe | 0b8f30a | 2011-10-04 10:31:53 +0200 | [diff] [blame] | 385 | } |
| 386 | |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 387 | static int send_client_cmd_line(struct fio_client *client) |
| 388 | { |
Jens Axboe | fa2ea80 | 2011-10-08 21:07:29 +0200 | [diff] [blame] | 389 | struct cmd_single_line_pdu *cslp; |
| 390 | struct cmd_line_pdu *clp; |
| 391 | unsigned long offset; |
Jens Axboe | 7f86831 | 2011-10-10 09:55:21 +0200 | [diff] [blame] | 392 | unsigned int *lens; |
Jens Axboe | fa2ea80 | 2011-10-08 21:07:29 +0200 | [diff] [blame] | 393 | void *pdu; |
| 394 | size_t mem; |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 395 | int i, ret; |
| 396 | |
Jens Axboe | 39e8e01 | 2011-10-04 13:46:08 +0200 | [diff] [blame] | 397 | dprint(FD_NET, "client: send cmdline %d\n", client->argc); |
Jens Axboe | 60efd14 | 2011-10-04 13:30:11 +0200 | [diff] [blame] | 398 | |
Jens Axboe | 7f86831 | 2011-10-10 09:55:21 +0200 | [diff] [blame] | 399 | lens = malloc(client->argc * sizeof(unsigned int)); |
| 400 | |
Jens Axboe | fa2ea80 | 2011-10-08 21:07:29 +0200 | [diff] [blame] | 401 | /* |
| 402 | * Find out how much mem we need |
| 403 | */ |
Jens Axboe | 7f86831 | 2011-10-10 09:55:21 +0200 | [diff] [blame] | 404 | for (i = 0, mem = 0; i < client->argc; i++) { |
| 405 | lens[i] = strlen(client->argv[i]) + 1; |
| 406 | mem += lens[i]; |
| 407 | } |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 408 | |
Jens Axboe | fa2ea80 | 2011-10-08 21:07:29 +0200 | [diff] [blame] | 409 | /* |
| 410 | * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu |
| 411 | */ |
| 412 | mem += sizeof(*clp) + (client->argc * sizeof(*cslp)); |
| 413 | |
| 414 | pdu = malloc(mem); |
| 415 | clp = pdu; |
| 416 | offset = sizeof(*clp); |
| 417 | |
| 418 | for (i = 0; i < client->argc; i++) { |
Jens Axboe | 7f86831 | 2011-10-10 09:55:21 +0200 | [diff] [blame] | 419 | uint16_t arg_len = lens[i]; |
Jens Axboe | fa2ea80 | 2011-10-08 21:07:29 +0200 | [diff] [blame] | 420 | |
| 421 | cslp = pdu + offset; |
| 422 | strcpy((char *) cslp->text, client->argv[i]); |
| 423 | cslp->len = cpu_to_le16(arg_len); |
| 424 | offset += sizeof(*cslp) + arg_len; |
| 425 | } |
| 426 | |
Jens Axboe | 7f86831 | 2011-10-10 09:55:21 +0200 | [diff] [blame] | 427 | free(lens); |
Jens Axboe | fa2ea80 | 2011-10-08 21:07:29 +0200 | [diff] [blame] | 428 | clp->lines = cpu_to_le16(client->argc); |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 429 | ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, 0); |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 430 | free(pdu); |
| 431 | return ret; |
| 432 | } |
| 433 | |
Jens Axboe | a37f69b | 2011-10-01 12:24:21 -0600 | [diff] [blame] | 434 | int fio_clients_connect(void) |
| 435 | { |
| 436 | struct fio_client *client; |
| 437 | struct flist_head *entry, *tmp; |
| 438 | int ret; |
| 439 | |
Bruce Cran | 93bcfd2 | 2012-02-20 20:18:19 +0100 | [diff] [blame] | 440 | #ifdef WIN32 |
| 441 | WSADATA wsd; |
| 442 | WSAStartup(MAKEWORD(2,2), &wsd); |
| 443 | #endif |
| 444 | |
Jens Axboe | 60efd14 | 2011-10-04 13:30:11 +0200 | [diff] [blame] | 445 | dprint(FD_NET, "client: connect all\n"); |
| 446 | |
Jens Axboe | cc0df00 | 2011-10-03 20:53:32 +0200 | [diff] [blame] | 447 | client_signal_handler(); |
| 448 | |
Jens Axboe | a37f69b | 2011-10-01 12:24:21 -0600 | [diff] [blame] | 449 | flist_for_each_safe(entry, tmp, &client_list) { |
| 450 | client = flist_entry(entry, struct fio_client, list); |
| 451 | |
| 452 | ret = fio_client_connect(client); |
Jens Axboe | 0b8f30a | 2011-10-04 10:31:53 +0200 | [diff] [blame] | 453 | if (ret) { |
Jens Axboe | a37f69b | 2011-10-01 12:24:21 -0600 | [diff] [blame] | 454 | remove_client(client); |
Jens Axboe | 0b8f30a | 2011-10-04 10:31:53 +0200 | [diff] [blame] | 455 | continue; |
| 456 | } |
| 457 | |
| 458 | probe_client(client); |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 459 | |
| 460 | if (client->argc > 1) |
| 461 | send_client_cmd_line(client); |
Jens Axboe | a37f69b | 2011-10-01 12:24:21 -0600 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | return !nr_clients; |
| 465 | } |
| 466 | |
Jens Axboe | b9d2f30 | 2012-03-08 20:36:28 +0100 | [diff] [blame] | 467 | int fio_start_client(struct fio_client *client) |
| 468 | { |
| 469 | dprint(FD_NET, "client: start %s\n", client->hostname); |
| 470 | return fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_RUN, 0, NULL); |
| 471 | } |
| 472 | |
| 473 | int fio_start_all_clients(void) |
| 474 | { |
| 475 | struct fio_client *client; |
| 476 | struct flist_head *entry, *tmp; |
| 477 | int ret; |
| 478 | |
| 479 | dprint(FD_NET, "client: start all\n"); |
| 480 | |
| 481 | flist_for_each_safe(entry, tmp, &client_list) { |
| 482 | client = flist_entry(entry, struct fio_client, list); |
| 483 | |
| 484 | ret = fio_start_client(client); |
| 485 | if (ret) { |
| 486 | remove_client(client); |
| 487 | continue; |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | return flist_empty(&client_list); |
| 492 | } |
| 493 | |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 494 | /* |
| 495 | * Send file contents to server backend. We could use sendfile(), but to remain |
| 496 | * more portable lets just read/write the darn thing. |
| 497 | */ |
Jens Axboe | a37f69b | 2011-10-01 12:24:21 -0600 | [diff] [blame] | 498 | static int fio_client_send_ini(struct fio_client *client, const char *filename) |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 499 | { |
| 500 | struct stat sb; |
| 501 | char *p, *buf; |
| 502 | off_t len; |
| 503 | int fd, ret; |
| 504 | |
Jens Axboe | 46c48f1 | 2011-10-01 12:50:51 -0600 | [diff] [blame] | 505 | dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname); |
| 506 | |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 507 | fd = open(filename, O_RDONLY); |
| 508 | if (fd < 0) { |
Jens Axboe | e951bdc | 2011-10-05 21:58:45 +0200 | [diff] [blame] | 509 | log_err("fio: job file <%s> open: %s\n", filename, strerror(errno)); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 510 | return 1; |
| 511 | } |
| 512 | |
| 513 | if (fstat(fd, &sb) < 0) { |
| 514 | log_err("fio: job file stat: %s\n", strerror(errno)); |
Jens Axboe | b94cba4 | 2011-10-06 21:27:10 +0200 | [diff] [blame] | 515 | close(fd); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 516 | return 1; |
| 517 | } |
| 518 | |
| 519 | buf = malloc(sb.st_size); |
| 520 | |
| 521 | len = sb.st_size; |
| 522 | p = buf; |
| 523 | do { |
| 524 | ret = read(fd, p, len); |
| 525 | if (ret > 0) { |
| 526 | len -= ret; |
| 527 | if (!len) |
| 528 | break; |
| 529 | p += ret; |
| 530 | continue; |
| 531 | } else if (!ret) |
| 532 | break; |
| 533 | else if (errno == EAGAIN || errno == EINTR) |
| 534 | continue; |
| 535 | } while (1); |
| 536 | |
Jens Axboe | 0b8f30a | 2011-10-04 10:31:53 +0200 | [diff] [blame] | 537 | if (len) { |
| 538 | log_err("fio: failed reading job file %s\n", filename); |
Jens Axboe | b94cba4 | 2011-10-06 21:27:10 +0200 | [diff] [blame] | 539 | close(fd); |
Jens Axboe | c524ef7 | 2011-10-07 12:23:34 +0200 | [diff] [blame] | 540 | free(buf); |
Jens Axboe | 0b8f30a | 2011-10-04 10:31:53 +0200 | [diff] [blame] | 541 | return 1; |
| 542 | } |
| 543 | |
Jens Axboe | c2cb686 | 2012-02-23 20:56:12 +0100 | [diff] [blame] | 544 | client->sent_job = 1; |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 545 | ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, buf, sb.st_size, 0); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 546 | free(buf); |
Jens Axboe | b94cba4 | 2011-10-06 21:27:10 +0200 | [diff] [blame] | 547 | close(fd); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 548 | return ret; |
| 549 | } |
Jens Axboe | 37db14f | 2011-09-30 17:00:42 -0600 | [diff] [blame] | 550 | |
Jens Axboe | a37f69b | 2011-10-01 12:24:21 -0600 | [diff] [blame] | 551 | int fio_clients_send_ini(const char *filename) |
| 552 | { |
| 553 | struct fio_client *client; |
| 554 | struct flist_head *entry, *tmp; |
| 555 | |
| 556 | flist_for_each_safe(entry, tmp, &client_list) { |
| 557 | client = flist_entry(entry, struct fio_client, list); |
| 558 | |
| 559 | if (fio_client_send_ini(client, filename)) |
| 560 | remove_client(client); |
Jens Axboe | c2cb686 | 2012-02-23 20:56:12 +0100 | [diff] [blame] | 561 | |
| 562 | client->sent_job = 1; |
Jens Axboe | a37f69b | 2011-10-01 12:24:21 -0600 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | return !nr_clients; |
| 566 | } |
| 567 | |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 568 | static void convert_io_stat(struct io_stat *dst, struct io_stat *src) |
| 569 | { |
| 570 | dst->max_val = le64_to_cpu(src->max_val); |
| 571 | dst->min_val = le64_to_cpu(src->min_val); |
| 572 | dst->samples = le64_to_cpu(src->samples); |
Jens Axboe | 802ad4a | 2011-10-05 09:51:58 +0200 | [diff] [blame] | 573 | |
| 574 | /* |
| 575 | * Floats arrive as IEEE 754 encoded uint64_t, convert back to double |
| 576 | */ |
| 577 | dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i)); |
| 578 | dst->S.u.f = fio_uint64_to_double(le64_to_cpu(dst->S.u.i)); |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | static void convert_ts(struct thread_stat *dst, struct thread_stat *src) |
| 582 | { |
| 583 | int i, j; |
| 584 | |
| 585 | dst->error = le32_to_cpu(src->error); |
| 586 | dst->groupid = le32_to_cpu(src->groupid); |
| 587 | dst->pid = le32_to_cpu(src->pid); |
| 588 | dst->members = le32_to_cpu(src->members); |
| 589 | |
| 590 | for (i = 0; i < 2; i++) { |
| 591 | convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]); |
| 592 | convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]); |
| 593 | convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]); |
| 594 | convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]); |
| 595 | } |
| 596 | |
| 597 | dst->usr_time = le64_to_cpu(src->usr_time); |
| 598 | dst->sys_time = le64_to_cpu(src->sys_time); |
| 599 | dst->ctx = le64_to_cpu(src->ctx); |
| 600 | dst->minf = le64_to_cpu(src->minf); |
| 601 | dst->majf = le64_to_cpu(src->majf); |
| 602 | dst->clat_percentiles = le64_to_cpu(src->clat_percentiles); |
Jens Axboe | 802ad4a | 2011-10-05 09:51:58 +0200 | [diff] [blame] | 603 | |
| 604 | for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) { |
| 605 | fio_fp64_t *fps = &src->percentile_list[i]; |
| 606 | fio_fp64_t *fpd = &dst->percentile_list[i]; |
| 607 | |
| 608 | fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i)); |
| 609 | } |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 610 | |
| 611 | for (i = 0; i < FIO_IO_U_MAP_NR; i++) { |
| 612 | dst->io_u_map[i] = le32_to_cpu(src->io_u_map[i]); |
| 613 | dst->io_u_submit[i] = le32_to_cpu(src->io_u_submit[i]); |
| 614 | dst->io_u_complete[i] = le32_to_cpu(src->io_u_complete[i]); |
| 615 | } |
| 616 | |
| 617 | for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) { |
| 618 | dst->io_u_lat_u[i] = le32_to_cpu(src->io_u_lat_u[i]); |
| 619 | dst->io_u_lat_m[i] = le32_to_cpu(src->io_u_lat_m[i]); |
| 620 | } |
| 621 | |
| 622 | for (i = 0; i < 2; i++) |
| 623 | for (j = 0; j < FIO_IO_U_PLAT_NR; j++) |
| 624 | dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]); |
| 625 | |
| 626 | for (i = 0; i < 3; i++) { |
| 627 | dst->total_io_u[i] = le64_to_cpu(src->total_io_u[i]); |
Jens Axboe | 93eee04 | 2011-10-03 21:08:48 +0200 | [diff] [blame] | 628 | dst->short_io_u[i] = le64_to_cpu(src->short_io_u[i]); |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | dst->total_submit = le64_to_cpu(src->total_submit); |
| 632 | dst->total_complete = le64_to_cpu(src->total_complete); |
| 633 | |
| 634 | for (i = 0; i < 2; i++) { |
| 635 | dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]); |
| 636 | dst->runtime[i] = le64_to_cpu(src->runtime[i]); |
| 637 | } |
| 638 | |
| 639 | dst->total_run_time = le64_to_cpu(src->total_run_time); |
| 640 | dst->continue_on_error = le16_to_cpu(src->continue_on_error); |
| 641 | dst->total_err_count = le64_to_cpu(src->total_err_count); |
Jens Axboe | ddcc0b6 | 2011-10-03 14:45:27 +0200 | [diff] [blame] | 642 | dst->first_error = le32_to_cpu(src->first_error); |
| 643 | dst->kb_base = le32_to_cpu(src->kb_base); |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src) |
| 647 | { |
| 648 | int i; |
| 649 | |
| 650 | for (i = 0; i < 2; i++) { |
| 651 | dst->max_run[i] = le64_to_cpu(src->max_run[i]); |
| 652 | dst->min_run[i] = le64_to_cpu(src->min_run[i]); |
| 653 | dst->max_bw[i] = le64_to_cpu(src->max_bw[i]); |
| 654 | dst->min_bw[i] = le64_to_cpu(src->min_bw[i]); |
| 655 | dst->io_kb[i] = le64_to_cpu(src->io_kb[i]); |
| 656 | dst->agg[i] = le64_to_cpu(src->agg[i]); |
| 657 | } |
| 658 | |
| 659 | dst->kb_base = le32_to_cpu(src->kb_base); |
| 660 | dst->groupid = le32_to_cpu(src->groupid); |
| 661 | } |
| 662 | |
Jens Axboe | 89e5fad | 2012-03-05 09:21:12 +0100 | [diff] [blame] | 663 | static void handle_ts(struct fio_client *client, struct fio_net_cmd *cmd) |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 664 | { |
| 665 | struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload; |
| 666 | |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 667 | show_thread_status(&p->ts, &p->rs); |
Jens Axboe | 37f0c1a | 2011-10-11 14:08:33 +0200 | [diff] [blame] | 668 | |
| 669 | if (sum_stat_clients == 1) |
| 670 | return; |
| 671 | |
| 672 | sum_thread_stats(&client_ts, &p->ts, sum_stat_nr); |
| 673 | sum_group_stats(&client_gs, &p->rs); |
| 674 | |
| 675 | client_ts.members++; |
| 676 | client_ts.groupid = p->ts.groupid; |
| 677 | |
| 678 | if (++sum_stat_nr == sum_stat_clients) { |
| 679 | strcpy(client_ts.name, "All clients"); |
| 680 | show_thread_status(&client_ts, &client_gs); |
| 681 | } |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 682 | } |
| 683 | |
Jens Axboe | 89e5fad | 2012-03-05 09:21:12 +0100 | [diff] [blame] | 684 | static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd) |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 685 | { |
| 686 | struct group_run_stats *gs = (struct group_run_stats *) cmd->payload; |
| 687 | |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 688 | show_group_stats(gs); |
| 689 | } |
| 690 | |
Jens Axboe | 3bf236c | 2012-03-03 20:35:50 +0100 | [diff] [blame] | 691 | static void handle_text(struct fio_client *client, struct fio_net_cmd *cmd) |
| 692 | { |
| 693 | struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload; |
| 694 | const char *buf = (const char *) pdu->buf; |
| 695 | const char *name; |
| 696 | int fio_unused ret; |
| 697 | |
| 698 | name = client->name ? client->name : client->hostname; |
| 699 | |
| 700 | if (!client->skip_newline) |
| 701 | fprintf(f_out, "<%s> ", name); |
| 702 | ret = fwrite(buf, pdu->buf_len, 1, f_out); |
| 703 | fflush(f_out); |
| 704 | client->skip_newline = strchr(buf, '\n') == NULL; |
| 705 | } |
| 706 | |
Jens Axboe | d09a64a | 2011-10-13 11:38:56 +0200 | [diff] [blame] | 707 | static void convert_agg(struct disk_util_agg *agg) |
| 708 | { |
| 709 | int i; |
| 710 | |
| 711 | for (i = 0; i < 2; i++) { |
| 712 | agg->ios[i] = le32_to_cpu(agg->ios[i]); |
| 713 | agg->merges[i] = le32_to_cpu(agg->merges[i]); |
| 714 | agg->sectors[i] = le64_to_cpu(agg->sectors[i]); |
| 715 | agg->ticks[i] = le32_to_cpu(agg->ticks[i]); |
| 716 | } |
| 717 | |
| 718 | agg->io_ticks = le32_to_cpu(agg->io_ticks); |
| 719 | agg->time_in_queue = le32_to_cpu(agg->time_in_queue); |
| 720 | agg->slavecount = le32_to_cpu(agg->slavecount); |
Anton Blanchard | 823ba54 | 2011-11-07 14:16:26 +0100 | [diff] [blame] | 721 | agg->max_util.u.f = fio_uint64_to_double(__le64_to_cpu(agg->max_util.u.i)); |
Jens Axboe | d09a64a | 2011-10-13 11:38:56 +0200 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | static void convert_dus(struct disk_util_stat *dus) |
| 725 | { |
| 726 | int i; |
| 727 | |
| 728 | for (i = 0; i < 2; i++) { |
| 729 | dus->ios[i] = le32_to_cpu(dus->ios[i]); |
| 730 | dus->merges[i] = le32_to_cpu(dus->merges[i]); |
| 731 | dus->sectors[i] = le64_to_cpu(dus->sectors[i]); |
| 732 | dus->ticks[i] = le32_to_cpu(dus->ticks[i]); |
| 733 | } |
| 734 | |
| 735 | dus->io_ticks = le32_to_cpu(dus->io_ticks); |
| 736 | dus->time_in_queue = le32_to_cpu(dus->time_in_queue); |
| 737 | dus->msec = le64_to_cpu(dus->msec); |
| 738 | } |
| 739 | |
| 740 | static void handle_du(struct fio_client *client, struct fio_net_cmd *cmd) |
| 741 | { |
| 742 | struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload; |
| 743 | |
Jens Axboe | d09a64a | 2011-10-13 11:38:56 +0200 | [diff] [blame] | 744 | if (!client->disk_stats_shown) { |
| 745 | client->disk_stats_shown = 1; |
| 746 | log_info("\nDisk stats (read/write):\n"); |
| 747 | } |
| 748 | |
Jens Axboe | f2f788d | 2011-10-13 14:03:52 +0200 | [diff] [blame] | 749 | print_disk_util(&du->dus, &du->agg, terse_output); |
Jens Axboe | d09a64a | 2011-10-13 11:38:56 +0200 | [diff] [blame] | 750 | } |
| 751 | |
Jens Axboe | 3bf236c | 2012-03-03 20:35:50 +0100 | [diff] [blame] | 752 | static void convert_jobs_eta(struct jobs_eta *je) |
Jens Axboe | cf451d1 | 2011-10-03 16:48:30 +0200 | [diff] [blame] | 753 | { |
Jens Axboe | cf451d1 | 2011-10-03 16:48:30 +0200 | [diff] [blame] | 754 | int i; |
| 755 | |
| 756 | je->nr_running = le32_to_cpu(je->nr_running); |
| 757 | je->nr_ramp = le32_to_cpu(je->nr_ramp); |
| 758 | je->nr_pending = le32_to_cpu(je->nr_pending); |
| 759 | je->files_open = le32_to_cpu(je->files_open); |
Jens Axboe | cf451d1 | 2011-10-03 16:48:30 +0200 | [diff] [blame] | 760 | |
| 761 | for (i = 0; i < 2; i++) { |
Jens Axboe | 3e47bd2 | 2012-02-29 13:45:02 +0100 | [diff] [blame] | 762 | je->m_rate[i] = le32_to_cpu(je->m_rate[i]); |
| 763 | je->t_rate[i] = le32_to_cpu(je->t_rate[i]); |
| 764 | je->m_iops[i] = le32_to_cpu(je->m_iops[i]); |
| 765 | je->t_iops[i] = le32_to_cpu(je->t_iops[i]); |
Jens Axboe | cf451d1 | 2011-10-03 16:48:30 +0200 | [diff] [blame] | 766 | je->rate[i] = le32_to_cpu(je->rate[i]); |
| 767 | je->iops[i] = le32_to_cpu(je->iops[i]); |
| 768 | } |
| 769 | |
Jens Axboe | b51eedb | 2011-10-09 12:13:39 +0200 | [diff] [blame] | 770 | je->elapsed_sec = le64_to_cpu(je->elapsed_sec); |
Jens Axboe | cf451d1 | 2011-10-03 16:48:30 +0200 | [diff] [blame] | 771 | je->eta_sec = le64_to_cpu(je->eta_sec); |
Jens Axboe | 8c621fb | 2012-03-08 12:30:48 +0100 | [diff] [blame] | 772 | je->nr_threads = le32_to_cpu(je->nr_threads); |
Jens Axboe | 48fbb46 | 2011-10-09 12:19:08 +0200 | [diff] [blame] | 773 | } |
Jens Axboe | cf451d1 | 2011-10-03 16:48:30 +0200 | [diff] [blame] | 774 | |
Jens Axboe | 3e47bd2 | 2012-02-29 13:45:02 +0100 | [diff] [blame] | 775 | void fio_client_sum_jobs_eta(struct jobs_eta *dst, struct jobs_eta *je) |
Jens Axboe | 48fbb46 | 2011-10-09 12:19:08 +0200 | [diff] [blame] | 776 | { |
Jens Axboe | 48fbb46 | 2011-10-09 12:19:08 +0200 | [diff] [blame] | 777 | int i; |
| 778 | |
| 779 | dst->nr_running += je->nr_running; |
| 780 | dst->nr_ramp += je->nr_ramp; |
| 781 | dst->nr_pending += je->nr_pending; |
| 782 | dst->files_open += je->files_open; |
Jens Axboe | 48fbb46 | 2011-10-09 12:19:08 +0200 | [diff] [blame] | 783 | |
| 784 | for (i = 0; i < 2; i++) { |
Jens Axboe | 3e47bd2 | 2012-02-29 13:45:02 +0100 | [diff] [blame] | 785 | dst->m_rate[i] += je->m_rate[i]; |
| 786 | dst->t_rate[i] += je->t_rate[i]; |
| 787 | dst->m_iops[i] += je->m_iops[i]; |
| 788 | dst->t_iops[i] += je->t_iops[i]; |
Jens Axboe | 48fbb46 | 2011-10-09 12:19:08 +0200 | [diff] [blame] | 789 | dst->rate[i] += je->rate[i]; |
| 790 | dst->iops[i] += je->iops[i]; |
| 791 | } |
| 792 | |
| 793 | dst->elapsed_sec += je->elapsed_sec; |
| 794 | |
| 795 | if (je->eta_sec > dst->eta_sec) |
| 796 | dst->eta_sec = je->eta_sec; |
Jens Axboe | 8c621fb | 2012-03-08 12:30:48 +0100 | [diff] [blame] | 797 | |
| 798 | dst->nr_threads += je->nr_threads; |
| 799 | /* we need to handle je->run_str too ... */ |
Jens Axboe | 48fbb46 | 2011-10-09 12:19:08 +0200 | [diff] [blame] | 800 | } |
| 801 | |
Jens Axboe | a527661 | 2012-03-04 15:15:08 +0100 | [diff] [blame] | 802 | void fio_client_dec_jobs_eta(struct client_eta *eta, client_eta_op eta_fn) |
Jens Axboe | 82c1ed3 | 2011-10-10 08:56:18 +0200 | [diff] [blame] | 803 | { |
| 804 | if (!--eta->pending) { |
Jens Axboe | a527661 | 2012-03-04 15:15:08 +0100 | [diff] [blame] | 805 | eta_fn(&eta->eta); |
Jens Axboe | 82c1ed3 | 2011-10-10 08:56:18 +0200 | [diff] [blame] | 806 | free(eta); |
| 807 | } |
| 808 | } |
| 809 | |
Jens Axboe | 89c1707 | 2011-10-11 10:15:51 +0200 | [diff] [blame] | 810 | static void remove_reply_cmd(struct fio_client *client, struct fio_net_cmd *cmd) |
| 811 | { |
| 812 | struct fio_net_int_cmd *icmd = NULL; |
| 813 | struct flist_head *entry; |
| 814 | |
| 815 | flist_for_each(entry, &client->cmd_list) { |
| 816 | icmd = flist_entry(entry, struct fio_net_int_cmd, list); |
| 817 | |
Jens Axboe | df38093 | 2011-10-11 14:25:08 +0200 | [diff] [blame] | 818 | if (cmd->tag == (uintptr_t) icmd) |
Jens Axboe | 89c1707 | 2011-10-11 10:15:51 +0200 | [diff] [blame] | 819 | break; |
| 820 | |
| 821 | icmd = NULL; |
| 822 | } |
| 823 | |
| 824 | if (!icmd) { |
| 825 | log_err("fio: client: unable to find matching tag\n"); |
| 826 | return; |
| 827 | } |
| 828 | |
| 829 | flist_del(&icmd->list); |
| 830 | cmd->tag = icmd->saved_tag; |
| 831 | free(icmd); |
| 832 | } |
| 833 | |
Jens Axboe | 82c1ed3 | 2011-10-10 08:56:18 +0200 | [diff] [blame] | 834 | static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd) |
Jens Axboe | 48fbb46 | 2011-10-09 12:19:08 +0200 | [diff] [blame] | 835 | { |
| 836 | struct jobs_eta *je = (struct jobs_eta *) cmd->payload; |
Jens Axboe | df38093 | 2011-10-11 14:25:08 +0200 | [diff] [blame] | 837 | struct client_eta *eta = (struct client_eta *) (uintptr_t) cmd->tag; |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 838 | |
| 839 | dprint(FD_NET, "client: got eta tag %p, %d\n", eta, eta->pending); |
Jens Axboe | 48fbb46 | 2011-10-09 12:19:08 +0200 | [diff] [blame] | 840 | |
Jens Axboe | f77d267 | 2011-10-10 14:36:07 +0200 | [diff] [blame] | 841 | assert(client->eta_in_flight == eta); |
| 842 | |
| 843 | client->eta_in_flight = NULL; |
Jens Axboe | 82c1ed3 | 2011-10-10 08:56:18 +0200 | [diff] [blame] | 844 | flist_del_init(&client->eta_list); |
| 845 | |
Jens Axboe | 2f99deb | 2012-03-09 14:37:29 +0100 | [diff] [blame^] | 846 | if (client->ops->jobs_eta) |
| 847 | client->ops->jobs_eta(client, je); |
| 848 | |
Jens Axboe | 3e47bd2 | 2012-02-29 13:45:02 +0100 | [diff] [blame] | 849 | fio_client_sum_jobs_eta(&eta->eta, je); |
Jens Axboe | a527661 | 2012-03-04 15:15:08 +0100 | [diff] [blame] | 850 | fio_client_dec_jobs_eta(eta, client->ops->eta); |
Jens Axboe | cf451d1 | 2011-10-03 16:48:30 +0200 | [diff] [blame] | 851 | } |
| 852 | |
Jens Axboe | b5296dd | 2011-10-07 16:35:56 +0200 | [diff] [blame] | 853 | static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd) |
Jens Axboe | 2e03b4b | 2011-10-04 09:00:40 +0200 | [diff] [blame] | 854 | { |
| 855 | struct cmd_probe_pdu *probe = (struct cmd_probe_pdu *) cmd->payload; |
Jens Axboe | d233335 | 2011-10-11 15:07:23 +0200 | [diff] [blame] | 856 | const char *os, *arch; |
| 857 | char bit[16]; |
Jens Axboe | 2e03b4b | 2011-10-04 09:00:40 +0200 | [diff] [blame] | 858 | |
Jens Axboe | cca8464 | 2011-10-07 12:47:57 +0200 | [diff] [blame] | 859 | os = fio_get_os_string(probe->os); |
| 860 | if (!os) |
| 861 | os = "unknown"; |
| 862 | |
| 863 | arch = fio_get_arch_string(probe->arch); |
| 864 | if (!arch) |
| 865 | os = "unknown"; |
| 866 | |
Jens Axboe | d233335 | 2011-10-11 15:07:23 +0200 | [diff] [blame] | 867 | sprintf(bit, "%d-bit", probe->bpp * 8); |
Jens Axboe | 38fdef2 | 2011-10-11 14:30:06 +0200 | [diff] [blame] | 868 | |
| 869 | log_info("hostname=%s, be=%u, %s, os=%s, arch=%s, fio=%u.%u.%u\n", |
| 870 | probe->hostname, probe->bigendian, bit, os, arch, |
| 871 | probe->fio_major, probe->fio_minor, probe->fio_patch); |
Jens Axboe | b5296dd | 2011-10-07 16:35:56 +0200 | [diff] [blame] | 872 | |
| 873 | if (!client->name) |
| 874 | client->name = strdup((char *) probe->hostname); |
Jens Axboe | 2e03b4b | 2011-10-04 09:00:40 +0200 | [diff] [blame] | 875 | } |
| 876 | |
Jens Axboe | 11e950b | 2011-10-16 21:34:14 +0200 | [diff] [blame] | 877 | static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd) |
| 878 | { |
| 879 | struct cmd_start_pdu *pdu = (struct cmd_start_pdu *) cmd->payload; |
| 880 | |
| 881 | client->state = Client_started; |
| 882 | client->jobs = le32_to_cpu(pdu->jobs); |
| 883 | } |
| 884 | |
| 885 | static void handle_stop(struct fio_client *client, struct fio_net_cmd *cmd) |
| 886 | { |
Jens Axboe | 498c92c | 2011-10-17 09:14:42 +0200 | [diff] [blame] | 887 | if (client->error) |
| 888 | log_info("client <%s>: exited with error %d\n", client->hostname, client->error); |
Jens Axboe | 11e950b | 2011-10-16 21:34:14 +0200 | [diff] [blame] | 889 | } |
| 890 | |
Jens Axboe | 6b79c80 | 2012-03-08 10:51:36 +0100 | [diff] [blame] | 891 | static void convert_stop(struct fio_net_cmd *cmd) |
| 892 | { |
| 893 | struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload; |
| 894 | |
| 895 | pdu->error = le32_to_cpu(pdu->error); |
| 896 | } |
| 897 | |
Jens Axboe | 084d1c6 | 2012-03-03 20:28:07 +0100 | [diff] [blame] | 898 | static void convert_text(struct fio_net_cmd *cmd) |
| 899 | { |
| 900 | struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmd->payload; |
| 901 | |
| 902 | pdu->level = le32_to_cpu(pdu->level); |
| 903 | pdu->buf_len = le32_to_cpu(pdu->buf_len); |
| 904 | pdu->log_sec = le64_to_cpu(pdu->log_sec); |
| 905 | pdu->log_usec = le64_to_cpu(pdu->log_usec); |
| 906 | } |
| 907 | |
Jens Axboe | a527661 | 2012-03-04 15:15:08 +0100 | [diff] [blame] | 908 | int fio_handle_client(struct fio_client *client) |
Jens Axboe | 37db14f | 2011-09-30 17:00:42 -0600 | [diff] [blame] | 909 | { |
Jens Axboe | a527661 | 2012-03-04 15:15:08 +0100 | [diff] [blame] | 910 | struct client_ops *ops = client->ops; |
Jens Axboe | 37db14f | 2011-09-30 17:00:42 -0600 | [diff] [blame] | 911 | struct fio_net_cmd *cmd; |
| 912 | |
Jens Axboe | 60efd14 | 2011-10-04 13:30:11 +0200 | [diff] [blame] | 913 | dprint(FD_NET, "client: handle %s\n", client->hostname); |
| 914 | |
Jens Axboe | e951bdc | 2011-10-05 21:58:45 +0200 | [diff] [blame] | 915 | cmd = fio_net_recv_cmd(client->fd); |
| 916 | if (!cmd) |
| 917 | return 0; |
Jens Axboe | c2c9458 | 2011-10-05 20:31:30 +0200 | [diff] [blame] | 918 | |
Jens Axboe | b9d2f30 | 2012-03-08 20:36:28 +0100 | [diff] [blame] | 919 | dprint(FD_NET, "client: got cmd op %s from %s (pdu=%u)\n", |
| 920 | fio_server_op(cmd->opcode), client->hostname, cmd->pdu_len); |
Jens Axboe | 46c48f1 | 2011-10-01 12:50:51 -0600 | [diff] [blame] | 921 | |
Jens Axboe | e951bdc | 2011-10-05 21:58:45 +0200 | [diff] [blame] | 922 | switch (cmd->opcode) { |
| 923 | case FIO_NET_CMD_QUIT: |
Jens Axboe | 3ec62ec | 2012-03-01 12:01:29 +0100 | [diff] [blame] | 924 | if (ops->quit) |
| 925 | ops->quit(client); |
Jens Axboe | e951bdc | 2011-10-05 21:58:45 +0200 | [diff] [blame] | 926 | remove_client(client); |
| 927 | free(cmd); |
| 928 | break; |
Jens Axboe | 084d1c6 | 2012-03-03 20:28:07 +0100 | [diff] [blame] | 929 | case FIO_NET_CMD_TEXT: |
| 930 | convert_text(cmd); |
| 931 | ops->text_op(client, cmd); |
Jens Axboe | e951bdc | 2011-10-05 21:58:45 +0200 | [diff] [blame] | 932 | free(cmd); |
| 933 | break; |
Jens Axboe | 3bf236c | 2012-03-03 20:35:50 +0100 | [diff] [blame] | 934 | case FIO_NET_CMD_DU: { |
| 935 | struct cmd_du_pdu *du = (struct cmd_du_pdu *) cmd->payload; |
| 936 | |
| 937 | convert_dus(&du->dus); |
| 938 | convert_agg(&du->agg); |
| 939 | |
Stephen M. Cameron | dd36672 | 2012-02-24 08:17:30 +0100 | [diff] [blame] | 940 | ops->disk_util(client, cmd); |
Jens Axboe | d09a64a | 2011-10-13 11:38:56 +0200 | [diff] [blame] | 941 | free(cmd); |
| 942 | break; |
Jens Axboe | 3bf236c | 2012-03-03 20:35:50 +0100 | [diff] [blame] | 943 | } |
| 944 | case FIO_NET_CMD_TS: { |
| 945 | struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload; |
| 946 | |
| 947 | convert_ts(&p->ts, &p->ts); |
| 948 | convert_gs(&p->rs, &p->rs); |
| 949 | |
Jens Axboe | 89e5fad | 2012-03-05 09:21:12 +0100 | [diff] [blame] | 950 | ops->thread_status(client, cmd); |
Jens Axboe | e951bdc | 2011-10-05 21:58:45 +0200 | [diff] [blame] | 951 | free(cmd); |
| 952 | break; |
Jens Axboe | 3bf236c | 2012-03-03 20:35:50 +0100 | [diff] [blame] | 953 | } |
| 954 | case FIO_NET_CMD_GS: { |
| 955 | struct group_run_stats *gs = (struct group_run_stats *) cmd->payload; |
| 956 | |
| 957 | convert_gs(gs, gs); |
| 958 | |
Jens Axboe | 89e5fad | 2012-03-05 09:21:12 +0100 | [diff] [blame] | 959 | ops->group_stats(client, cmd); |
Jens Axboe | e951bdc | 2011-10-05 21:58:45 +0200 | [diff] [blame] | 960 | free(cmd); |
| 961 | break; |
Jens Axboe | 3bf236c | 2012-03-03 20:35:50 +0100 | [diff] [blame] | 962 | } |
| 963 | case FIO_NET_CMD_ETA: { |
| 964 | struct jobs_eta *je = (struct jobs_eta *) cmd->payload; |
| 965 | |
Jens Axboe | 89c1707 | 2011-10-11 10:15:51 +0200 | [diff] [blame] | 966 | remove_reply_cmd(client, cmd); |
Jens Axboe | 3bf236c | 2012-03-03 20:35:50 +0100 | [diff] [blame] | 967 | convert_jobs_eta(je); |
Jens Axboe | a527661 | 2012-03-04 15:15:08 +0100 | [diff] [blame] | 968 | handle_eta(client, cmd); |
Jens Axboe | e951bdc | 2011-10-05 21:58:45 +0200 | [diff] [blame] | 969 | free(cmd); |
| 970 | break; |
Jens Axboe | 3bf236c | 2012-03-03 20:35:50 +0100 | [diff] [blame] | 971 | } |
Jens Axboe | e951bdc | 2011-10-05 21:58:45 +0200 | [diff] [blame] | 972 | case FIO_NET_CMD_PROBE: |
Jens Axboe | 89c1707 | 2011-10-11 10:15:51 +0200 | [diff] [blame] | 973 | remove_reply_cmd(client, cmd); |
Stephen M. Cameron | dd36672 | 2012-02-24 08:17:30 +0100 | [diff] [blame] | 974 | ops->probe(client, cmd); |
Jens Axboe | e951bdc | 2011-10-05 21:58:45 +0200 | [diff] [blame] | 975 | free(cmd); |
| 976 | break; |
Jens Axboe | 5d7793a | 2012-03-08 19:59:16 +0100 | [diff] [blame] | 977 | case FIO_NET_CMD_SERVER_START: |
Jens Axboe | 01be038 | 2011-10-15 14:43:41 +0200 | [diff] [blame] | 978 | client->state = Client_running; |
| 979 | free(cmd); |
| 980 | break; |
Jens Axboe | e951bdc | 2011-10-05 21:58:45 +0200 | [diff] [blame] | 981 | case FIO_NET_CMD_START: |
Jens Axboe | 11e950b | 2011-10-16 21:34:14 +0200 | [diff] [blame] | 982 | handle_start(client, cmd); |
Jens Axboe | e951bdc | 2011-10-05 21:58:45 +0200 | [diff] [blame] | 983 | free(cmd); |
| 984 | break; |
Jens Axboe | 6b79c80 | 2012-03-08 10:51:36 +0100 | [diff] [blame] | 985 | case FIO_NET_CMD_STOP: { |
| 986 | struct cmd_end_pdu *pdu = (struct cmd_end_pdu *) cmd->payload; |
| 987 | |
| 988 | convert_stop(cmd); |
| 989 | client->state = Client_stopped; |
| 990 | client->error = pdu->error; |
| 991 | ops->stop(client, cmd); |
Jens Axboe | e951bdc | 2011-10-05 21:58:45 +0200 | [diff] [blame] | 992 | free(cmd); |
| 993 | break; |
Jens Axboe | 6b79c80 | 2012-03-08 10:51:36 +0100 | [diff] [blame] | 994 | } |
Jens Axboe | 807f997 | 2012-03-02 10:25:24 +0100 | [diff] [blame] | 995 | case FIO_NET_CMD_ADD_JOB: |
| 996 | if (ops->add_job) |
| 997 | ops->add_job(client, cmd); |
| 998 | free(cmd); |
| 999 | break; |
Jens Axboe | e951bdc | 2011-10-05 21:58:45 +0200 | [diff] [blame] | 1000 | default: |
Jens Axboe | 89c1707 | 2011-10-11 10:15:51 +0200 | [diff] [blame] | 1001 | log_err("fio: unknown client op: %s\n", fio_server_op(cmd->opcode)); |
Jens Axboe | e951bdc | 2011-10-05 21:58:45 +0200 | [diff] [blame] | 1002 | free(cmd); |
| 1003 | break; |
Jens Axboe | 37db14f | 2011-09-30 17:00:42 -0600 | [diff] [blame] | 1004 | } |
| 1005 | |
Jens Axboe | e951bdc | 2011-10-05 21:58:45 +0200 | [diff] [blame] | 1006 | return 1; |
Jens Axboe | 37db14f | 2011-09-30 17:00:42 -0600 | [diff] [blame] | 1007 | } |
Jens Axboe | b66570d | 2011-10-01 11:11:35 -0600 | [diff] [blame] | 1008 | |
Jens Axboe | a527661 | 2012-03-04 15:15:08 +0100 | [diff] [blame] | 1009 | static void request_client_etas(struct client_ops *ops) |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 1010 | { |
| 1011 | struct fio_client *client; |
| 1012 | struct flist_head *entry; |
| 1013 | struct client_eta *eta; |
Jens Axboe | 82c1ed3 | 2011-10-10 08:56:18 +0200 | [diff] [blame] | 1014 | int skipped = 0; |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 1015 | |
| 1016 | dprint(FD_NET, "client: request eta (%d)\n", nr_clients); |
| 1017 | |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 1018 | eta = malloc(sizeof(*eta)); |
| 1019 | memset(&eta->eta, 0, sizeof(eta->eta)); |
| 1020 | eta->pending = nr_clients; |
| 1021 | |
| 1022 | flist_for_each(entry, &client_list) { |
| 1023 | client = flist_entry(entry, struct fio_client, list); |
| 1024 | |
Jens Axboe | 82c1ed3 | 2011-10-10 08:56:18 +0200 | [diff] [blame] | 1025 | if (!flist_empty(&client->eta_list)) { |
| 1026 | skipped++; |
| 1027 | continue; |
| 1028 | } |
Jens Axboe | 01be038 | 2011-10-15 14:43:41 +0200 | [diff] [blame] | 1029 | if (client->state != Client_running) |
| 1030 | continue; |
Jens Axboe | 82c1ed3 | 2011-10-10 08:56:18 +0200 | [diff] [blame] | 1031 | |
Jens Axboe | f77d267 | 2011-10-10 14:36:07 +0200 | [diff] [blame] | 1032 | assert(!client->eta_in_flight); |
Jens Axboe | 82c1ed3 | 2011-10-10 08:56:18 +0200 | [diff] [blame] | 1033 | flist_add_tail(&client->eta_list, &eta_list); |
Jens Axboe | f77d267 | 2011-10-10 14:36:07 +0200 | [diff] [blame] | 1034 | client->eta_in_flight = eta; |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 1035 | fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_SEND_ETA, |
Jens Axboe | df38093 | 2011-10-11 14:25:08 +0200 | [diff] [blame] | 1036 | (uintptr_t) eta, &client->cmd_list); |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 1037 | } |
| 1038 | |
Jens Axboe | 82c1ed3 | 2011-10-10 08:56:18 +0200 | [diff] [blame] | 1039 | while (skipped--) |
Jens Axboe | a527661 | 2012-03-04 15:15:08 +0100 | [diff] [blame] | 1040 | fio_client_dec_jobs_eta(eta, ops->eta); |
Jens Axboe | 82c1ed3 | 2011-10-10 08:56:18 +0200 | [diff] [blame] | 1041 | |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 1042 | dprint(FD_NET, "client: requested eta tag %p\n", eta); |
| 1043 | } |
| 1044 | |
Jens Axboe | 89c1707 | 2011-10-11 10:15:51 +0200 | [diff] [blame] | 1045 | static int client_check_cmd_timeout(struct fio_client *client, |
| 1046 | struct timeval *now) |
| 1047 | { |
| 1048 | struct fio_net_int_cmd *cmd; |
| 1049 | struct flist_head *entry, *tmp; |
| 1050 | int ret = 0; |
| 1051 | |
| 1052 | flist_for_each_safe(entry, tmp, &client->cmd_list) { |
| 1053 | cmd = flist_entry(entry, struct fio_net_int_cmd, list); |
| 1054 | |
| 1055 | if (mtime_since(&cmd->tv, now) < FIO_NET_CLIENT_TIMEOUT) |
| 1056 | continue; |
| 1057 | |
| 1058 | log_err("fio: client %s, timeout on cmd %s\n", client->hostname, |
| 1059 | fio_server_op(cmd->cmd.opcode)); |
| 1060 | flist_del(&cmd->list); |
| 1061 | free(cmd); |
| 1062 | ret = 1; |
| 1063 | } |
| 1064 | |
| 1065 | return flist_empty(&client->cmd_list) && ret; |
| 1066 | } |
| 1067 | |
Jens Axboe | a527661 | 2012-03-04 15:15:08 +0100 | [diff] [blame] | 1068 | static int fio_check_clients_timed_out(void) |
Jens Axboe | 89c1707 | 2011-10-11 10:15:51 +0200 | [diff] [blame] | 1069 | { |
| 1070 | struct fio_client *client; |
| 1071 | struct flist_head *entry, *tmp; |
| 1072 | struct timeval tv; |
| 1073 | int ret = 0; |
| 1074 | |
| 1075 | gettimeofday(&tv, NULL); |
| 1076 | |
| 1077 | flist_for_each_safe(entry, tmp, &client_list) { |
| 1078 | client = flist_entry(entry, struct fio_client, list); |
| 1079 | |
| 1080 | if (flist_empty(&client->cmd_list)) |
| 1081 | continue; |
| 1082 | |
| 1083 | if (!client_check_cmd_timeout(client, &tv)) |
| 1084 | continue; |
| 1085 | |
Jens Axboe | a527661 | 2012-03-04 15:15:08 +0100 | [diff] [blame] | 1086 | if (client->ops->timed_out) |
| 1087 | client->ops->timed_out(client); |
Jens Axboe | ed727a4 | 2012-03-02 12:14:40 +0100 | [diff] [blame] | 1088 | else |
| 1089 | log_err("fio: client %s timed out\n", client->hostname); |
| 1090 | |
Jens Axboe | 89c1707 | 2011-10-11 10:15:51 +0200 | [diff] [blame] | 1091 | remove_client(client); |
| 1092 | ret = 1; |
| 1093 | } |
| 1094 | |
| 1095 | return ret; |
| 1096 | } |
| 1097 | |
Stephen M. Cameron | dd36672 | 2012-02-24 08:17:30 +0100 | [diff] [blame] | 1098 | int fio_handle_clients(struct client_ops *ops) |
Jens Axboe | b66570d | 2011-10-01 11:11:35 -0600 | [diff] [blame] | 1099 | { |
Jens Axboe | b66570d | 2011-10-01 11:11:35 -0600 | [diff] [blame] | 1100 | struct pollfd *pfds; |
Jens Axboe | 498c92c | 2011-10-17 09:14:42 +0200 | [diff] [blame] | 1101 | int i, ret = 0, retval = 0; |
Jens Axboe | b66570d | 2011-10-01 11:11:35 -0600 | [diff] [blame] | 1102 | |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 1103 | gettimeofday(&eta_tv, NULL); |
| 1104 | |
Jens Axboe | b66570d | 2011-10-01 11:11:35 -0600 | [diff] [blame] | 1105 | pfds = malloc(nr_clients * sizeof(struct pollfd)); |
| 1106 | |
Jens Axboe | 37f0c1a | 2011-10-11 14:08:33 +0200 | [diff] [blame] | 1107 | sum_stat_clients = nr_clients; |
| 1108 | init_thread_stat(&client_ts); |
| 1109 | init_group_run_stat(&client_gs); |
| 1110 | |
Jens Axboe | b66570d | 2011-10-01 11:11:35 -0600 | [diff] [blame] | 1111 | while (!exit_backend && nr_clients) { |
Jens Axboe | c2cb686 | 2012-02-23 20:56:12 +0100 | [diff] [blame] | 1112 | struct flist_head *entry, *tmp; |
| 1113 | struct fio_client *client; |
| 1114 | |
Jens Axboe | 82a4be1 | 2011-10-01 12:40:32 -0600 | [diff] [blame] | 1115 | i = 0; |
Jens Axboe | c2cb686 | 2012-02-23 20:56:12 +0100 | [diff] [blame] | 1116 | flist_for_each_safe(entry, tmp, &client_list) { |
Jens Axboe | 82a4be1 | 2011-10-01 12:40:32 -0600 | [diff] [blame] | 1117 | client = flist_entry(entry, struct fio_client, list); |
| 1118 | |
Jens Axboe | a527661 | 2012-03-04 15:15:08 +0100 | [diff] [blame] | 1119 | if (!client->sent_job && !client->ops->stay_connected && |
Jens Axboe | c2cb686 | 2012-02-23 20:56:12 +0100 | [diff] [blame] | 1120 | flist_empty(&client->cmd_list)) { |
| 1121 | remove_client(client); |
| 1122 | continue; |
| 1123 | } |
| 1124 | |
Jens Axboe | 82a4be1 | 2011-10-01 12:40:32 -0600 | [diff] [blame] | 1125 | pfds[i].fd = client->fd; |
| 1126 | pfds[i].events = POLLIN; |
| 1127 | i++; |
| 1128 | } |
| 1129 | |
Jens Axboe | c2cb686 | 2012-02-23 20:56:12 +0100 | [diff] [blame] | 1130 | if (!nr_clients) |
| 1131 | break; |
| 1132 | |
Jens Axboe | 82a4be1 | 2011-10-01 12:40:32 -0600 | [diff] [blame] | 1133 | assert(i == nr_clients); |
| 1134 | |
Jens Axboe | 5c2857f | 2011-10-04 13:14:32 +0200 | [diff] [blame] | 1135 | do { |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 1136 | struct timeval tv; |
| 1137 | |
| 1138 | gettimeofday(&tv, NULL); |
| 1139 | if (mtime_since(&eta_tv, &tv) >= 900) { |
Jens Axboe | a527661 | 2012-03-04 15:15:08 +0100 | [diff] [blame] | 1140 | request_client_etas(ops); |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 1141 | memcpy(&eta_tv, &tv, sizeof(tv)); |
Jens Axboe | 89c1707 | 2011-10-11 10:15:51 +0200 | [diff] [blame] | 1142 | |
Jens Axboe | a527661 | 2012-03-04 15:15:08 +0100 | [diff] [blame] | 1143 | if (fio_check_clients_timed_out()) |
Jens Axboe | 89c1707 | 2011-10-11 10:15:51 +0200 | [diff] [blame] | 1144 | break; |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 1145 | } |
| 1146 | |
Jens Axboe | 5c2857f | 2011-10-04 13:14:32 +0200 | [diff] [blame] | 1147 | ret = poll(pfds, nr_clients, 100); |
| 1148 | if (ret < 0) { |
| 1149 | if (errno == EINTR) |
| 1150 | continue; |
| 1151 | log_err("fio: poll clients: %s\n", strerror(errno)); |
| 1152 | break; |
| 1153 | } else if (!ret) |
Jens Axboe | b66570d | 2011-10-01 11:11:35 -0600 | [diff] [blame] | 1154 | continue; |
Jens Axboe | 5c2857f | 2011-10-04 13:14:32 +0200 | [diff] [blame] | 1155 | } while (ret <= 0); |
Jens Axboe | b66570d | 2011-10-01 11:11:35 -0600 | [diff] [blame] | 1156 | |
| 1157 | for (i = 0; i < nr_clients; i++) { |
| 1158 | if (!(pfds[i].revents & POLLIN)) |
| 1159 | continue; |
| 1160 | |
| 1161 | client = find_client_by_fd(pfds[i].fd); |
| 1162 | if (!client) { |
Jens Axboe | 3c5f57e | 2011-10-06 12:37:50 +0200 | [diff] [blame] | 1163 | log_err("fio: unknown client fd %d\n", pfds[i].fd); |
Jens Axboe | b66570d | 2011-10-01 11:11:35 -0600 | [diff] [blame] | 1164 | continue; |
| 1165 | } |
Jens Axboe | a527661 | 2012-03-04 15:15:08 +0100 | [diff] [blame] | 1166 | if (!fio_handle_client(client)) { |
Jens Axboe | 28d3ab0 | 2011-10-05 20:45:37 +0200 | [diff] [blame] | 1167 | log_info("client: host=%s disconnected\n", |
| 1168 | client->hostname); |
| 1169 | remove_client(client); |
Jens Axboe | 498c92c | 2011-10-17 09:14:42 +0200 | [diff] [blame] | 1170 | retval = 1; |
Jens Axboe | 3899076 | 2011-10-17 13:31:33 +0200 | [diff] [blame] | 1171 | } else if (client->error) |
Jens Axboe | 498c92c | 2011-10-17 09:14:42 +0200 | [diff] [blame] | 1172 | retval = 1; |
Jens Axboe | 5121a9a | 2012-03-05 13:32:47 +0100 | [diff] [blame] | 1173 | put_client(client); |
Jens Axboe | b66570d | 2011-10-01 11:11:35 -0600 | [diff] [blame] | 1174 | } |
| 1175 | } |
| 1176 | |
| 1177 | free(pfds); |
Jens Axboe | 498c92c | 2011-10-17 09:14:42 +0200 | [diff] [blame] | 1178 | return retval; |
Jens Axboe | b66570d | 2011-10-01 11:11:35 -0600 | [diff] [blame] | 1179 | } |