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