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