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" |
| 19 | #include "server.h" |
Jens Axboe | b66570d | 2011-10-01 11:11:35 -0600 | [diff] [blame] | 20 | #include "flist.h" |
Jens Axboe | 3c5f57e | 2011-10-06 12:37:50 +0200 | [diff] [blame] | 21 | #include "hash.h" |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 22 | |
Jens Axboe | b66570d | 2011-10-01 11:11:35 -0600 | [diff] [blame] | 23 | struct fio_client { |
| 24 | struct flist_head list; |
Jens Axboe | 3c5f57e | 2011-10-06 12:37:50 +0200 | [diff] [blame] | 25 | struct flist_head fd_hash_list; |
| 26 | struct flist_head name_hash_list; |
Jens Axboe | b66570d | 2011-10-01 11:11:35 -0600 | [diff] [blame] | 27 | struct sockaddr_in addr; |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame^] | 28 | struct sockaddr_un addr_un; |
Jens Axboe | b66570d | 2011-10-01 11:11:35 -0600 | [diff] [blame] | 29 | char *hostname; |
| 30 | int fd; |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 31 | |
| 32 | int state; |
Jens Axboe | 17dd176 | 2011-10-04 13:27:34 +0200 | [diff] [blame] | 33 | int skip_newline; |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame^] | 34 | int is_sock; |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 35 | |
| 36 | uint16_t argc; |
| 37 | char **argv; |
| 38 | }; |
| 39 | |
| 40 | enum { |
Jens Axboe | 5c2857f | 2011-10-04 13:14:32 +0200 | [diff] [blame] | 41 | Client_created = 0, |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 42 | Client_connected = 1, |
| 43 | Client_started = 2, |
| 44 | Client_stopped = 3, |
Jens Axboe | 5c2857f | 2011-10-04 13:14:32 +0200 | [diff] [blame] | 45 | Client_exited = 4, |
Jens Axboe | b66570d | 2011-10-01 11:11:35 -0600 | [diff] [blame] | 46 | }; |
| 47 | |
| 48 | static FLIST_HEAD(client_list); |
Jens Axboe | b66570d | 2011-10-01 11:11:35 -0600 | [diff] [blame] | 49 | |
Jens Axboe | 3c5f57e | 2011-10-06 12:37:50 +0200 | [diff] [blame] | 50 | #define FIO_CLIENT_HASH_BITS 7 |
| 51 | #define FIO_CLIENT_HASH_SZ (1 << FIO_CLIENT_HASH_BITS) |
| 52 | #define FIO_CLIENT_HASH_MASK (FIO_CLIENT_HASH_SZ - 1) |
| 53 | static struct flist_head client_fd_hash[FIO_CLIENT_HASH_SZ]; |
| 54 | static struct flist_head client_name_hash[FIO_CLIENT_HASH_SZ]; |
| 55 | |
Jens Axboe | e951bdc | 2011-10-05 21:58:45 +0200 | [diff] [blame] | 56 | static int handle_client(struct fio_client *client); |
Jens Axboe | 0b8f30a | 2011-10-04 10:31:53 +0200 | [diff] [blame] | 57 | |
Jens Axboe | 3c5f57e | 2011-10-06 12:37:50 +0200 | [diff] [blame] | 58 | static void fio_client_add_fd_hash(struct fio_client *client) |
| 59 | { |
| 60 | int bucket = hash_long(client->fd, FIO_CLIENT_HASH_BITS); |
| 61 | |
| 62 | bucket &= FIO_CLIENT_HASH_MASK; |
| 63 | flist_add(&client->fd_hash_list, &client_fd_hash[bucket]); |
| 64 | } |
| 65 | |
| 66 | static void fio_client_remove_fd_hash(struct fio_client *client) |
| 67 | { |
| 68 | if (!flist_empty(&client->fd_hash_list)) |
| 69 | flist_del_init(&client->fd_hash_list); |
| 70 | } |
| 71 | |
| 72 | static void fio_client_add_name_hash(struct fio_client *client) |
| 73 | { |
| 74 | int bucket = jhash(client->hostname, strlen(client->hostname), 0); |
| 75 | |
| 76 | bucket &= FIO_CLIENT_HASH_MASK; |
| 77 | flist_add(&client->name_hash_list, &client_name_hash[bucket]); |
| 78 | } |
| 79 | |
| 80 | static void fio_client_remove_name_hash(struct fio_client *client) |
| 81 | { |
| 82 | if (!flist_empty(&client->name_hash_list)) |
| 83 | flist_del_init(&client->name_hash_list); |
| 84 | } |
| 85 | |
| 86 | static void fio_init fio_client_hash_init(void) |
| 87 | { |
| 88 | int i; |
| 89 | |
| 90 | for (i = 0; i < FIO_CLIENT_HASH_SZ; i++) { |
| 91 | INIT_FLIST_HEAD(&client_fd_hash[i]); |
| 92 | INIT_FLIST_HEAD(&client_name_hash[i]); |
| 93 | } |
| 94 | } |
| 95 | |
Jens Axboe | b66570d | 2011-10-01 11:11:35 -0600 | [diff] [blame] | 96 | static struct fio_client *find_client_by_fd(int fd) |
| 97 | { |
Jens Axboe | 3c5f57e | 2011-10-06 12:37:50 +0200 | [diff] [blame] | 98 | 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] | 99 | struct fio_client *client; |
| 100 | struct flist_head *entry; |
| 101 | |
Jens Axboe | 3c5f57e | 2011-10-06 12:37:50 +0200 | [diff] [blame] | 102 | flist_for_each(entry, &client_fd_hash[bucket]) { |
| 103 | client = flist_entry(entry, struct fio_client, fd_hash_list); |
Jens Axboe | b66570d | 2011-10-01 11:11:35 -0600 | [diff] [blame] | 104 | |
| 105 | if (client->fd == fd) |
| 106 | return client; |
| 107 | } |
| 108 | |
| 109 | return NULL; |
| 110 | } |
| 111 | |
| 112 | static struct fio_client *find_client_by_name(const char *name) |
| 113 | { |
Jens Axboe | 3c5f57e | 2011-10-06 12:37:50 +0200 | [diff] [blame] | 114 | int bucket = jhash(name, strlen(name), 0) & FIO_CLIENT_HASH_BITS; |
Jens Axboe | b66570d | 2011-10-01 11:11:35 -0600 | [diff] [blame] | 115 | struct fio_client *client; |
| 116 | struct flist_head *entry; |
| 117 | |
Jens Axboe | 3c5f57e | 2011-10-06 12:37:50 +0200 | [diff] [blame] | 118 | flist_for_each(entry, &client_name_hash[bucket]) { |
| 119 | client = flist_entry(entry, struct fio_client, name_hash_list); |
Jens Axboe | b66570d | 2011-10-01 11:11:35 -0600 | [diff] [blame] | 120 | |
| 121 | if (!strcmp(name, client->hostname)) |
| 122 | return client; |
| 123 | } |
| 124 | |
| 125 | return NULL; |
| 126 | } |
| 127 | |
| 128 | static void remove_client(struct fio_client *client) |
| 129 | { |
Jens Axboe | 39e8e01 | 2011-10-04 13:46:08 +0200 | [diff] [blame] | 130 | dprint(FD_NET, "client: removed <%s>\n", client->hostname); |
Jens Axboe | b66570d | 2011-10-01 11:11:35 -0600 | [diff] [blame] | 131 | flist_del(&client->list); |
Jens Axboe | 3c5f57e | 2011-10-06 12:37:50 +0200 | [diff] [blame] | 132 | |
| 133 | fio_client_remove_fd_hash(client); |
| 134 | fio_client_remove_name_hash(client); |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 135 | |
Jens Axboe | b66570d | 2011-10-01 11:11:35 -0600 | [diff] [blame] | 136 | free(client->hostname); |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 137 | if (client->argv) |
| 138 | free(client->argv); |
| 139 | |
Jens Axboe | b66570d | 2011-10-01 11:11:35 -0600 | [diff] [blame] | 140 | free(client); |
Jens Axboe | 3c5f57e | 2011-10-06 12:37:50 +0200 | [diff] [blame] | 141 | nr_clients--; |
Jens Axboe | b66570d | 2011-10-01 11:11:35 -0600 | [diff] [blame] | 142 | } |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 143 | |
Jens Axboe | 7a4b824 | 2011-10-05 17:35:15 +0200 | [diff] [blame] | 144 | static int __fio_client_add_cmd_option(struct fio_client *client, |
| 145 | const char *opt) |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 146 | { |
Jens Axboe | 39e8e01 | 2011-10-04 13:46:08 +0200 | [diff] [blame] | 147 | int index; |
| 148 | |
Jens Axboe | 7a4b824 | 2011-10-05 17:35:15 +0200 | [diff] [blame] | 149 | if (client->argc == FIO_NET_CMD_JOBLINE_ARGV) { |
| 150 | log_err("fio: max cmd line number reached.\n"); |
| 151 | log_err("fio: cmd line <%s> has been ignored.\n", opt); |
| 152 | return 1; |
| 153 | } |
| 154 | |
Jens Axboe | 39e8e01 | 2011-10-04 13:46:08 +0200 | [diff] [blame] | 155 | index = client->argc++; |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 156 | client->argv = realloc(client->argv, sizeof(char *) * client->argc); |
Jens Axboe | 39e8e01 | 2011-10-04 13:46:08 +0200 | [diff] [blame] | 157 | client->argv[index] = strdup(opt); |
| 158 | dprint(FD_NET, "client: add cmd %d: %s\n", index, opt); |
Jens Axboe | 7a4b824 | 2011-10-05 17:35:15 +0200 | [diff] [blame] | 159 | return 0; |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 160 | } |
| 161 | |
Jens Axboe | 7a4b824 | 2011-10-05 17:35:15 +0200 | [diff] [blame] | 162 | int fio_client_add_cmd_option(const char *hostname, const char *opt) |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 163 | { |
| 164 | struct fio_client *client; |
| 165 | |
| 166 | if (!hostname || !opt) |
Jens Axboe | 7a4b824 | 2011-10-05 17:35:15 +0200 | [diff] [blame] | 167 | return 0; |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 168 | |
| 169 | client = find_client_by_name(hostname); |
| 170 | if (!client) { |
| 171 | log_err("fio: unknown client %s\n", hostname); |
Jens Axboe | 7a4b824 | 2011-10-05 17:35:15 +0200 | [diff] [blame] | 172 | return 1; |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 173 | } |
| 174 | |
Jens Axboe | 7a4b824 | 2011-10-05 17:35:15 +0200 | [diff] [blame] | 175 | return __fio_client_add_cmd_option(client, opt); |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 176 | } |
| 177 | |
Jens Axboe | a37f69b | 2011-10-01 12:24:21 -0600 | [diff] [blame] | 178 | void fio_client_add(const char *hostname) |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 179 | { |
Jens Axboe | b66570d | 2011-10-01 11:11:35 -0600 | [diff] [blame] | 180 | struct fio_client *client; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 181 | |
Jens Axboe | 39e8e01 | 2011-10-04 13:46:08 +0200 | [diff] [blame] | 182 | dprint(FD_NET, "client: added <%s>\n", hostname); |
Jens Axboe | b66570d | 2011-10-01 11:11:35 -0600 | [diff] [blame] | 183 | client = malloc(sizeof(*client)); |
Jens Axboe | a37f69b | 2011-10-01 12:24:21 -0600 | [diff] [blame] | 184 | memset(client, 0, sizeof(*client)); |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 185 | |
Jens Axboe | 3c5f57e | 2011-10-06 12:37:50 +0200 | [diff] [blame] | 186 | INIT_FLIST_HEAD(&client->list); |
| 187 | INIT_FLIST_HEAD(&client->fd_hash_list); |
| 188 | INIT_FLIST_HEAD(&client->name_hash_list); |
| 189 | |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame^] | 190 | if (!strncmp(hostname, "sock:", 5)) { |
| 191 | client->hostname = strdup(hostname + 5); |
| 192 | client->is_sock = 1; |
| 193 | } else |
| 194 | client->hostname = strdup(hostname); |
| 195 | |
Jens Axboe | a37f69b | 2011-10-01 12:24:21 -0600 | [diff] [blame] | 196 | client->fd = -1; |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 197 | |
Jens Axboe | 3c5f57e | 2011-10-06 12:37:50 +0200 | [diff] [blame] | 198 | fio_client_add_name_hash(client); |
| 199 | |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 200 | __fio_client_add_cmd_option(client, "fio"); |
| 201 | |
Jens Axboe | a37f69b | 2011-10-01 12:24:21 -0600 | [diff] [blame] | 202 | flist_add(&client->list, &client_list); |
| 203 | nr_clients++; |
| 204 | } |
| 205 | |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame^] | 206 | static int fio_client_connect_ip(struct fio_client *client) |
Jens Axboe | a37f69b | 2011-10-01 12:24:21 -0600 | [diff] [blame] | 207 | { |
| 208 | int fd; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 209 | |
Jens Axboe | b66570d | 2011-10-01 11:11:35 -0600 | [diff] [blame] | 210 | client->addr.sin_family = AF_INET; |
| 211 | client->addr.sin_port = htons(fio_net_port); |
| 212 | |
Jens Axboe | a37f69b | 2011-10-01 12:24:21 -0600 | [diff] [blame] | 213 | if (inet_aton(client->hostname, &client->addr.sin_addr) != 1) { |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 214 | struct hostent *hent; |
| 215 | |
Jens Axboe | a37f69b | 2011-10-01 12:24:21 -0600 | [diff] [blame] | 216 | hent = gethostbyname(client->hostname); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 217 | if (!hent) { |
| 218 | log_err("fio: gethostbyname: %s\n", strerror(errno)); |
Jens Axboe | e951bdc | 2011-10-05 21:58:45 +0200 | [diff] [blame] | 219 | log_err("fio: failed looking up hostname %s\n", |
| 220 | client->hostname); |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame^] | 221 | return -1; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 222 | } |
| 223 | |
Jens Axboe | b66570d | 2011-10-01 11:11:35 -0600 | [diff] [blame] | 224 | memcpy(&client->addr.sin_addr, hent->h_addr, 4); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | fd = socket(AF_INET, SOCK_STREAM, 0); |
| 228 | if (fd < 0) { |
| 229 | log_err("fio: socket: %s\n", strerror(errno)); |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame^] | 230 | return -1; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 231 | } |
| 232 | |
Jens Axboe | b66570d | 2011-10-01 11:11:35 -0600 | [diff] [blame] | 233 | if (connect(fd, (struct sockaddr *) &client->addr, sizeof(client->addr)) < 0) { |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 234 | log_err("fio: connect: %s\n", strerror(errno)); |
Jens Axboe | cdf54d8 | 2011-10-04 08:31:40 +0200 | [diff] [blame] | 235 | log_err("fio: failed to connect to %s\n", client->hostname); |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame^] | 236 | return -1; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 237 | } |
| 238 | |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame^] | 239 | return fd; |
| 240 | } |
| 241 | |
| 242 | static int fio_client_connect_sock(struct fio_client *client) |
| 243 | { |
| 244 | struct sockaddr_un *addr = &client->addr_un; |
| 245 | fio_socklen_t len; |
| 246 | int fd; |
| 247 | |
| 248 | memset(addr, 0, sizeof(*addr)); |
| 249 | addr->sun_family = AF_UNIX; |
| 250 | strcpy(addr->sun_path, client->hostname); |
| 251 | |
| 252 | fd = socket(AF_UNIX, SOCK_STREAM, 0); |
| 253 | if (fd < 0) { |
| 254 | log_err("fio: socket: %s\n", strerror(errno)); |
| 255 | return -1; |
| 256 | } |
| 257 | |
| 258 | len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1; |
| 259 | if (connect(fd, (struct sockaddr *) addr, len) < 0) { |
| 260 | log_err("fio: connect; %s\n", strerror(errno)); |
| 261 | return -1; |
| 262 | } |
| 263 | |
| 264 | return fd; |
| 265 | } |
| 266 | |
| 267 | static int fio_client_connect(struct fio_client *client) |
| 268 | { |
| 269 | int fd; |
| 270 | |
| 271 | dprint(FD_NET, "client: connect to host %s\n", client->hostname); |
| 272 | |
| 273 | memset(&client->addr, 0, sizeof(client->addr)); |
| 274 | |
| 275 | if (client->is_sock) |
| 276 | fd = fio_client_connect_sock(client); |
| 277 | else |
| 278 | fd = fio_client_connect_ip(client); |
| 279 | |
| 280 | if (fd < 0) |
| 281 | return 1; |
| 282 | |
Jens Axboe | b66570d | 2011-10-01 11:11:35 -0600 | [diff] [blame] | 283 | client->fd = fd; |
Jens Axboe | 3c5f57e | 2011-10-06 12:37:50 +0200 | [diff] [blame] | 284 | fio_client_add_fd_hash(client); |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 285 | client->state = Client_connected; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 286 | return 0; |
| 287 | } |
| 288 | |
Jens Axboe | cc0df00 | 2011-10-03 20:53:32 +0200 | [diff] [blame] | 289 | void fio_clients_terminate(void) |
| 290 | { |
| 291 | struct flist_head *entry; |
| 292 | struct fio_client *client; |
| 293 | |
Jens Axboe | 60efd14 | 2011-10-04 13:30:11 +0200 | [diff] [blame] | 294 | dprint(FD_NET, "client: terminate clients\n"); |
| 295 | |
Jens Axboe | cc0df00 | 2011-10-03 20:53:32 +0200 | [diff] [blame] | 296 | flist_for_each(entry, &client_list) { |
| 297 | client = flist_entry(entry, struct fio_client, list); |
| 298 | |
| 299 | fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_QUIT, 0); |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | static void sig_int(int sig) |
| 304 | { |
Jens Axboe | 60efd14 | 2011-10-04 13:30:11 +0200 | [diff] [blame] | 305 | dprint(FD_NET, "client: got sign %d\n", sig); |
Jens Axboe | cc0df00 | 2011-10-03 20:53:32 +0200 | [diff] [blame] | 306 | fio_clients_terminate(); |
| 307 | } |
| 308 | |
| 309 | static void client_signal_handler(void) |
| 310 | { |
| 311 | struct sigaction act; |
| 312 | |
| 313 | memset(&act, 0, sizeof(act)); |
| 314 | act.sa_handler = sig_int; |
| 315 | act.sa_flags = SA_RESTART; |
| 316 | sigaction(SIGINT, &act, NULL); |
| 317 | |
| 318 | memset(&act, 0, sizeof(act)); |
| 319 | act.sa_handler = sig_int; |
| 320 | act.sa_flags = SA_RESTART; |
| 321 | sigaction(SIGTERM, &act, NULL); |
| 322 | } |
| 323 | |
Jens Axboe | 0b8f30a | 2011-10-04 10:31:53 +0200 | [diff] [blame] | 324 | static void probe_client(struct fio_client *client) |
| 325 | { |
Jens Axboe | 60efd14 | 2011-10-04 13:30:11 +0200 | [diff] [blame] | 326 | dprint(FD_NET, "client: send probe\n"); |
| 327 | |
Jens Axboe | 0b8f30a | 2011-10-04 10:31:53 +0200 | [diff] [blame] | 328 | fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_PROBE, 0); |
Jens Axboe | e951bdc | 2011-10-05 21:58:45 +0200 | [diff] [blame] | 329 | handle_client(client); |
Jens Axboe | 0b8f30a | 2011-10-04 10:31:53 +0200 | [diff] [blame] | 330 | } |
| 331 | |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 332 | static int send_client_cmd_line(struct fio_client *client) |
| 333 | { |
| 334 | struct cmd_line_pdu *pdu; |
| 335 | int i, ret; |
| 336 | |
Jens Axboe | 39e8e01 | 2011-10-04 13:46:08 +0200 | [diff] [blame] | 337 | dprint(FD_NET, "client: send cmdline %d\n", client->argc); |
Jens Axboe | 60efd14 | 2011-10-04 13:30:11 +0200 | [diff] [blame] | 338 | |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 339 | pdu = malloc(sizeof(*pdu)); |
| 340 | for (i = 0; i < client->argc; i++) |
| 341 | strcpy((char *) pdu->argv[i], client->argv[i]); |
| 342 | |
| 343 | pdu->argc = cpu_to_le16(client->argc); |
| 344 | ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, sizeof(*pdu)); |
| 345 | free(pdu); |
| 346 | return ret; |
| 347 | } |
| 348 | |
Jens Axboe | a37f69b | 2011-10-01 12:24:21 -0600 | [diff] [blame] | 349 | int fio_clients_connect(void) |
| 350 | { |
| 351 | struct fio_client *client; |
| 352 | struct flist_head *entry, *tmp; |
| 353 | int ret; |
| 354 | |
Jens Axboe | 60efd14 | 2011-10-04 13:30:11 +0200 | [diff] [blame] | 355 | dprint(FD_NET, "client: connect all\n"); |
| 356 | |
Jens Axboe | cc0df00 | 2011-10-03 20:53:32 +0200 | [diff] [blame] | 357 | client_signal_handler(); |
| 358 | |
Jens Axboe | a37f69b | 2011-10-01 12:24:21 -0600 | [diff] [blame] | 359 | flist_for_each_safe(entry, tmp, &client_list) { |
| 360 | client = flist_entry(entry, struct fio_client, list); |
| 361 | |
| 362 | ret = fio_client_connect(client); |
Jens Axboe | 0b8f30a | 2011-10-04 10:31:53 +0200 | [diff] [blame] | 363 | if (ret) { |
Jens Axboe | a37f69b | 2011-10-01 12:24:21 -0600 | [diff] [blame] | 364 | remove_client(client); |
Jens Axboe | 0b8f30a | 2011-10-04 10:31:53 +0200 | [diff] [blame] | 365 | continue; |
| 366 | } |
| 367 | |
| 368 | probe_client(client); |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 369 | |
| 370 | if (client->argc > 1) |
| 371 | send_client_cmd_line(client); |
Jens Axboe | a37f69b | 2011-10-01 12:24:21 -0600 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | return !nr_clients; |
| 375 | } |
| 376 | |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 377 | /* |
| 378 | * Send file contents to server backend. We could use sendfile(), but to remain |
| 379 | * more portable lets just read/write the darn thing. |
| 380 | */ |
Jens Axboe | a37f69b | 2011-10-01 12:24:21 -0600 | [diff] [blame] | 381 | 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] | 382 | { |
| 383 | struct stat sb; |
| 384 | char *p, *buf; |
| 385 | off_t len; |
| 386 | int fd, ret; |
| 387 | |
Jens Axboe | 46c48f1 | 2011-10-01 12:50:51 -0600 | [diff] [blame] | 388 | dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname); |
| 389 | |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 390 | fd = open(filename, O_RDONLY); |
| 391 | if (fd < 0) { |
Jens Axboe | e951bdc | 2011-10-05 21:58:45 +0200 | [diff] [blame] | 392 | log_err("fio: job file <%s> open: %s\n", filename, strerror(errno)); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 393 | return 1; |
| 394 | } |
| 395 | |
| 396 | if (fstat(fd, &sb) < 0) { |
| 397 | log_err("fio: job file stat: %s\n", strerror(errno)); |
| 398 | return 1; |
| 399 | } |
| 400 | |
| 401 | buf = malloc(sb.st_size); |
| 402 | |
| 403 | len = sb.st_size; |
| 404 | p = buf; |
| 405 | do { |
| 406 | ret = read(fd, p, len); |
| 407 | if (ret > 0) { |
| 408 | len -= ret; |
| 409 | if (!len) |
| 410 | break; |
| 411 | p += ret; |
| 412 | continue; |
| 413 | } else if (!ret) |
| 414 | break; |
| 415 | else if (errno == EAGAIN || errno == EINTR) |
| 416 | continue; |
| 417 | } while (1); |
| 418 | |
Jens Axboe | 0b8f30a | 2011-10-04 10:31:53 +0200 | [diff] [blame] | 419 | if (len) { |
| 420 | log_err("fio: failed reading job file %s\n", filename); |
| 421 | return 1; |
| 422 | } |
| 423 | |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 424 | ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, buf, sb.st_size); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 425 | free(buf); |
| 426 | return ret; |
| 427 | } |
Jens Axboe | 37db14f | 2011-09-30 17:00:42 -0600 | [diff] [blame] | 428 | |
Jens Axboe | a37f69b | 2011-10-01 12:24:21 -0600 | [diff] [blame] | 429 | int fio_clients_send_ini(const char *filename) |
| 430 | { |
| 431 | struct fio_client *client; |
| 432 | struct flist_head *entry, *tmp; |
| 433 | |
| 434 | flist_for_each_safe(entry, tmp, &client_list) { |
| 435 | client = flist_entry(entry, struct fio_client, list); |
| 436 | |
| 437 | if (fio_client_send_ini(client, filename)) |
| 438 | remove_client(client); |
| 439 | } |
| 440 | |
| 441 | return !nr_clients; |
| 442 | } |
| 443 | |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 444 | static void convert_io_stat(struct io_stat *dst, struct io_stat *src) |
| 445 | { |
| 446 | dst->max_val = le64_to_cpu(src->max_val); |
| 447 | dst->min_val = le64_to_cpu(src->min_val); |
| 448 | dst->samples = le64_to_cpu(src->samples); |
Jens Axboe | 802ad4a | 2011-10-05 09:51:58 +0200 | [diff] [blame] | 449 | |
| 450 | /* |
| 451 | * Floats arrive as IEEE 754 encoded uint64_t, convert back to double |
| 452 | */ |
| 453 | dst->mean.u.f = fio_uint64_to_double(le64_to_cpu(dst->mean.u.i)); |
| 454 | 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] | 455 | } |
| 456 | |
| 457 | static void convert_ts(struct thread_stat *dst, struct thread_stat *src) |
| 458 | { |
| 459 | int i, j; |
| 460 | |
| 461 | dst->error = le32_to_cpu(src->error); |
| 462 | dst->groupid = le32_to_cpu(src->groupid); |
| 463 | dst->pid = le32_to_cpu(src->pid); |
| 464 | dst->members = le32_to_cpu(src->members); |
| 465 | |
| 466 | for (i = 0; i < 2; i++) { |
| 467 | convert_io_stat(&dst->clat_stat[i], &src->clat_stat[i]); |
| 468 | convert_io_stat(&dst->slat_stat[i], &src->slat_stat[i]); |
| 469 | convert_io_stat(&dst->lat_stat[i], &src->lat_stat[i]); |
| 470 | convert_io_stat(&dst->bw_stat[i], &src->bw_stat[i]); |
| 471 | } |
| 472 | |
| 473 | dst->usr_time = le64_to_cpu(src->usr_time); |
| 474 | dst->sys_time = le64_to_cpu(src->sys_time); |
| 475 | dst->ctx = le64_to_cpu(src->ctx); |
| 476 | dst->minf = le64_to_cpu(src->minf); |
| 477 | dst->majf = le64_to_cpu(src->majf); |
| 478 | dst->clat_percentiles = le64_to_cpu(src->clat_percentiles); |
Jens Axboe | 802ad4a | 2011-10-05 09:51:58 +0200 | [diff] [blame] | 479 | |
| 480 | for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) { |
| 481 | fio_fp64_t *fps = &src->percentile_list[i]; |
| 482 | fio_fp64_t *fpd = &dst->percentile_list[i]; |
| 483 | |
| 484 | fpd->u.f = fio_uint64_to_double(le64_to_cpu(fps->u.i)); |
| 485 | } |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 486 | |
| 487 | for (i = 0; i < FIO_IO_U_MAP_NR; i++) { |
| 488 | dst->io_u_map[i] = le32_to_cpu(src->io_u_map[i]); |
| 489 | dst->io_u_submit[i] = le32_to_cpu(src->io_u_submit[i]); |
| 490 | dst->io_u_complete[i] = le32_to_cpu(src->io_u_complete[i]); |
| 491 | } |
| 492 | |
| 493 | for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) { |
| 494 | dst->io_u_lat_u[i] = le32_to_cpu(src->io_u_lat_u[i]); |
| 495 | dst->io_u_lat_m[i] = le32_to_cpu(src->io_u_lat_m[i]); |
| 496 | } |
| 497 | |
| 498 | for (i = 0; i < 2; i++) |
| 499 | for (j = 0; j < FIO_IO_U_PLAT_NR; j++) |
| 500 | dst->io_u_plat[i][j] = le32_to_cpu(src->io_u_plat[i][j]); |
| 501 | |
| 502 | for (i = 0; i < 3; i++) { |
| 503 | 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] | 504 | 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] | 505 | } |
| 506 | |
| 507 | dst->total_submit = le64_to_cpu(src->total_submit); |
| 508 | dst->total_complete = le64_to_cpu(src->total_complete); |
| 509 | |
| 510 | for (i = 0; i < 2; i++) { |
| 511 | dst->io_bytes[i] = le64_to_cpu(src->io_bytes[i]); |
| 512 | dst->runtime[i] = le64_to_cpu(src->runtime[i]); |
| 513 | } |
| 514 | |
| 515 | dst->total_run_time = le64_to_cpu(src->total_run_time); |
| 516 | dst->continue_on_error = le16_to_cpu(src->continue_on_error); |
| 517 | dst->total_err_count = le64_to_cpu(src->total_err_count); |
Jens Axboe | ddcc0b6 | 2011-10-03 14:45:27 +0200 | [diff] [blame] | 518 | dst->first_error = le32_to_cpu(src->first_error); |
| 519 | dst->kb_base = le32_to_cpu(src->kb_base); |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src) |
| 523 | { |
| 524 | int i; |
| 525 | |
| 526 | for (i = 0; i < 2; i++) { |
| 527 | dst->max_run[i] = le64_to_cpu(src->max_run[i]); |
| 528 | dst->min_run[i] = le64_to_cpu(src->min_run[i]); |
| 529 | dst->max_bw[i] = le64_to_cpu(src->max_bw[i]); |
| 530 | dst->min_bw[i] = le64_to_cpu(src->min_bw[i]); |
| 531 | dst->io_kb[i] = le64_to_cpu(src->io_kb[i]); |
| 532 | dst->agg[i] = le64_to_cpu(src->agg[i]); |
| 533 | } |
| 534 | |
| 535 | dst->kb_base = le32_to_cpu(src->kb_base); |
| 536 | dst->groupid = le32_to_cpu(src->groupid); |
| 537 | } |
| 538 | |
| 539 | static void handle_ts(struct fio_net_cmd *cmd) |
| 540 | { |
| 541 | struct cmd_ts_pdu *p = (struct cmd_ts_pdu *) cmd->payload; |
| 542 | |
| 543 | convert_ts(&p->ts, &p->ts); |
| 544 | convert_gs(&p->rs, &p->rs); |
| 545 | |
| 546 | show_thread_status(&p->ts, &p->rs); |
| 547 | } |
| 548 | |
| 549 | static void handle_gs(struct fio_net_cmd *cmd) |
| 550 | { |
| 551 | struct group_run_stats *gs = (struct group_run_stats *) cmd->payload; |
| 552 | |
| 553 | convert_gs(gs, gs); |
| 554 | show_group_stats(gs); |
| 555 | } |
| 556 | |
Jens Axboe | cf451d1 | 2011-10-03 16:48:30 +0200 | [diff] [blame] | 557 | static void handle_eta(struct fio_net_cmd *cmd) |
| 558 | { |
| 559 | struct jobs_eta *je = (struct jobs_eta *) cmd->payload; |
| 560 | int i; |
| 561 | |
| 562 | je->nr_running = le32_to_cpu(je->nr_running); |
| 563 | je->nr_ramp = le32_to_cpu(je->nr_ramp); |
| 564 | je->nr_pending = le32_to_cpu(je->nr_pending); |
| 565 | je->files_open = le32_to_cpu(je->files_open); |
| 566 | je->m_rate = le32_to_cpu(je->m_rate); |
| 567 | je->t_rate = le32_to_cpu(je->t_rate); |
| 568 | je->m_iops = le32_to_cpu(je->m_iops); |
| 569 | je->t_iops = le32_to_cpu(je->t_iops); |
| 570 | |
| 571 | for (i = 0; i < 2; i++) { |
| 572 | je->rate[i] = le32_to_cpu(je->rate[i]); |
| 573 | je->iops[i] = le32_to_cpu(je->iops[i]); |
| 574 | } |
| 575 | |
| 576 | je->elapsed_sec = le32_to_cpu(je->nr_running); |
| 577 | je->eta_sec = le64_to_cpu(je->eta_sec); |
| 578 | |
| 579 | display_thread_status(je); |
| 580 | } |
| 581 | |
Jens Axboe | 2e03b4b | 2011-10-04 09:00:40 +0200 | [diff] [blame] | 582 | static void handle_probe(struct fio_net_cmd *cmd) |
| 583 | { |
| 584 | struct cmd_probe_pdu *probe = (struct cmd_probe_pdu *) cmd->payload; |
| 585 | |
Jens Axboe | 6eb2479 | 2011-10-04 15:00:30 +0200 | [diff] [blame] | 586 | log_info("Probe: hostname=%s, be=%u, fio ver %u.%u.%u\n", |
| 587 | probe->hostname, probe->bigendian, probe->fio_major, |
| 588 | probe->fio_minor, probe->fio_patch); |
Jens Axboe | 2e03b4b | 2011-10-04 09:00:40 +0200 | [diff] [blame] | 589 | } |
| 590 | |
Jens Axboe | e951bdc | 2011-10-05 21:58:45 +0200 | [diff] [blame] | 591 | static int handle_client(struct fio_client *client) |
Jens Axboe | 37db14f | 2011-09-30 17:00:42 -0600 | [diff] [blame] | 592 | { |
| 593 | struct fio_net_cmd *cmd; |
| 594 | |
Jens Axboe | 60efd14 | 2011-10-04 13:30:11 +0200 | [diff] [blame] | 595 | dprint(FD_NET, "client: handle %s\n", client->hostname); |
| 596 | |
Jens Axboe | e951bdc | 2011-10-05 21:58:45 +0200 | [diff] [blame] | 597 | cmd = fio_net_recv_cmd(client->fd); |
| 598 | if (!cmd) |
| 599 | return 0; |
Jens Axboe | c2c9458 | 2011-10-05 20:31:30 +0200 | [diff] [blame] | 600 | |
Jens Axboe | e951bdc | 2011-10-05 21:58:45 +0200 | [diff] [blame] | 601 | dprint(FD_NET, "client: got cmd op %d from %s\n", |
Jens Axboe | c2c9458 | 2011-10-05 20:31:30 +0200 | [diff] [blame] | 602 | cmd->opcode, client->hostname); |
Jens Axboe | 46c48f1 | 2011-10-01 12:50:51 -0600 | [diff] [blame] | 603 | |
Jens Axboe | e951bdc | 2011-10-05 21:58:45 +0200 | [diff] [blame] | 604 | switch (cmd->opcode) { |
| 605 | case FIO_NET_CMD_QUIT: |
| 606 | remove_client(client); |
| 607 | free(cmd); |
| 608 | break; |
| 609 | case FIO_NET_CMD_TEXT: { |
| 610 | const char *buf = (const char *) cmd->payload; |
| 611 | int fio_unused ret; |
Jens Axboe | 17dd176 | 2011-10-04 13:27:34 +0200 | [diff] [blame] | 612 | |
Jens Axboe | e951bdc | 2011-10-05 21:58:45 +0200 | [diff] [blame] | 613 | if (!client->skip_newline) |
| 614 | fprintf(f_out, "<%s> ", client->hostname); |
| 615 | ret = fwrite(buf, cmd->pdu_len, 1, f_out); |
| 616 | fflush(f_out); |
| 617 | client->skip_newline = strchr(buf, '\n') == NULL; |
| 618 | free(cmd); |
| 619 | break; |
Jens Axboe | 37db14f | 2011-09-30 17:00:42 -0600 | [diff] [blame] | 620 | } |
Jens Axboe | e951bdc | 2011-10-05 21:58:45 +0200 | [diff] [blame] | 621 | case FIO_NET_CMD_TS: |
| 622 | handle_ts(cmd); |
| 623 | free(cmd); |
| 624 | break; |
| 625 | case FIO_NET_CMD_GS: |
| 626 | handle_gs(cmd); |
| 627 | free(cmd); |
| 628 | break; |
| 629 | case FIO_NET_CMD_ETA: |
| 630 | handle_eta(cmd); |
| 631 | free(cmd); |
| 632 | break; |
| 633 | case FIO_NET_CMD_PROBE: |
| 634 | handle_probe(cmd); |
| 635 | free(cmd); |
| 636 | break; |
| 637 | case FIO_NET_CMD_START: |
| 638 | client->state = Client_started; |
| 639 | free(cmd); |
| 640 | break; |
| 641 | case FIO_NET_CMD_STOP: |
| 642 | client->state = Client_stopped; |
| 643 | free(cmd); |
| 644 | break; |
| 645 | default: |
| 646 | log_err("fio: unknown client op: %d\n", cmd->opcode); |
| 647 | free(cmd); |
| 648 | break; |
Jens Axboe | 37db14f | 2011-09-30 17:00:42 -0600 | [diff] [blame] | 649 | } |
| 650 | |
Jens Axboe | e951bdc | 2011-10-05 21:58:45 +0200 | [diff] [blame] | 651 | return 1; |
Jens Axboe | 37db14f | 2011-09-30 17:00:42 -0600 | [diff] [blame] | 652 | } |
Jens Axboe | b66570d | 2011-10-01 11:11:35 -0600 | [diff] [blame] | 653 | |
| 654 | int fio_handle_clients(void) |
| 655 | { |
| 656 | struct fio_client *client; |
| 657 | struct flist_head *entry; |
| 658 | struct pollfd *pfds; |
Jens Axboe | 82a4be1 | 2011-10-01 12:40:32 -0600 | [diff] [blame] | 659 | int i, ret = 0; |
Jens Axboe | b66570d | 2011-10-01 11:11:35 -0600 | [diff] [blame] | 660 | |
| 661 | pfds = malloc(nr_clients * sizeof(struct pollfd)); |
| 662 | |
Jens Axboe | b66570d | 2011-10-01 11:11:35 -0600 | [diff] [blame] | 663 | while (!exit_backend && nr_clients) { |
Jens Axboe | 82a4be1 | 2011-10-01 12:40:32 -0600 | [diff] [blame] | 664 | i = 0; |
| 665 | flist_for_each(entry, &client_list) { |
| 666 | client = flist_entry(entry, struct fio_client, list); |
| 667 | |
| 668 | pfds[i].fd = client->fd; |
| 669 | pfds[i].events = POLLIN; |
| 670 | i++; |
| 671 | } |
| 672 | |
| 673 | assert(i == nr_clients); |
| 674 | |
Jens Axboe | 5c2857f | 2011-10-04 13:14:32 +0200 | [diff] [blame] | 675 | do { |
| 676 | ret = poll(pfds, nr_clients, 100); |
| 677 | if (ret < 0) { |
| 678 | if (errno == EINTR) |
| 679 | continue; |
| 680 | log_err("fio: poll clients: %s\n", strerror(errno)); |
| 681 | break; |
| 682 | } else if (!ret) |
Jens Axboe | b66570d | 2011-10-01 11:11:35 -0600 | [diff] [blame] | 683 | continue; |
Jens Axboe | 5c2857f | 2011-10-04 13:14:32 +0200 | [diff] [blame] | 684 | } while (ret <= 0); |
Jens Axboe | b66570d | 2011-10-01 11:11:35 -0600 | [diff] [blame] | 685 | |
| 686 | for (i = 0; i < nr_clients; i++) { |
| 687 | if (!(pfds[i].revents & POLLIN)) |
| 688 | continue; |
| 689 | |
| 690 | client = find_client_by_fd(pfds[i].fd); |
| 691 | if (!client) { |
Jens Axboe | 3c5f57e | 2011-10-06 12:37:50 +0200 | [diff] [blame] | 692 | log_err("fio: unknown client fd %d\n", pfds[i].fd); |
Jens Axboe | b66570d | 2011-10-01 11:11:35 -0600 | [diff] [blame] | 693 | continue; |
| 694 | } |
Jens Axboe | e951bdc | 2011-10-05 21:58:45 +0200 | [diff] [blame] | 695 | if (!handle_client(client)) { |
Jens Axboe | 28d3ab0 | 2011-10-05 20:45:37 +0200 | [diff] [blame] | 696 | log_info("client: host=%s disconnected\n", |
| 697 | client->hostname); |
| 698 | remove_client(client); |
| 699 | } |
Jens Axboe | b66570d | 2011-10-01 11:11:35 -0600 | [diff] [blame] | 700 | } |
| 701 | } |
| 702 | |
| 703 | free(pfds); |
Jens Axboe | b66570d | 2011-10-01 11:11:35 -0600 | [diff] [blame] | 704 | return 0; |
| 705 | } |