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