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