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