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