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