Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
Jens Axboe | 142575e | 2011-09-30 17:35:45 -0600 | [diff] [blame] | 3 | #include <stdarg.h> |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 4 | #include <unistd.h> |
| 5 | #include <limits.h> |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 6 | #include <errno.h> |
| 7 | #include <fcntl.h> |
| 8 | #include <sys/poll.h> |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 9 | #include <sys/types.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/stat.h> |
| 13 | #include <sys/un.h> |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 14 | #include <netinet/in.h> |
| 15 | #include <arpa/inet.h> |
| 16 | #include <netdb.h> |
Jens Axboe | e46d809 | 2011-10-03 09:11:02 +0200 | [diff] [blame] | 17 | #include <syslog.h> |
Jens Axboe | 9e22ecb | 2011-10-04 14:50:21 +0200 | [diff] [blame] | 18 | #include <signal.h> |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 19 | |
| 20 | #include "fio.h" |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 21 | #include "server.h" |
Jens Axboe | fcee5ff | 2011-09-30 22:50:39 -0600 | [diff] [blame] | 22 | #include "crc/crc16.h" |
Jens Axboe | c7c6cb4 | 2011-10-13 14:12:40 +0200 | [diff] [blame^] | 23 | #include "lib/ieee754.h" |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 24 | |
Jens Axboe | 89cf148 | 2011-10-07 10:10:18 +0200 | [diff] [blame] | 25 | #include "fio_version.h" |
| 26 | |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 27 | int fio_net_port = 8765; |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 28 | |
Jens Axboe | 009b1be | 2011-09-29 18:27:02 -0600 | [diff] [blame] | 29 | int exit_backend = 0; |
| 30 | |
Jens Axboe | 46c48f1 | 2011-10-01 12:50:51 -0600 | [diff] [blame] | 31 | static int server_fd = -1; |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 32 | static char *fio_server_arg; |
| 33 | static char *bind_sock; |
| 34 | static struct sockaddr_in saddr_in; |
Jens Axboe | 37db14f | 2011-09-30 17:00:42 -0600 | [diff] [blame] | 35 | |
Jens Axboe | 89c1707 | 2011-10-11 10:15:51 +0200 | [diff] [blame] | 36 | static const char *fio_server_ops[FIO_NET_CMD_NR] = { |
| 37 | "", |
| 38 | "QUIT", |
| 39 | "EXIT", |
| 40 | "JOB", |
| 41 | "JOBLINE", |
| 42 | "TEXT", |
| 43 | "TS", |
| 44 | "GS", |
| 45 | "SEND_ETA", |
| 46 | "ETA", |
| 47 | "PROBE", |
| 48 | "START", |
Jens Axboe | d09a64a | 2011-10-13 11:38:56 +0200 | [diff] [blame] | 49 | "STOP", |
| 50 | "DISK_UTIL", |
Jens Axboe | 89c1707 | 2011-10-11 10:15:51 +0200 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | const char *fio_server_op(unsigned int op) |
| 54 | { |
| 55 | static char buf[32]; |
| 56 | |
| 57 | if (op < FIO_NET_CMD_NR) |
| 58 | return fio_server_ops[op]; |
| 59 | |
| 60 | sprintf(buf, "UNKNOWN/%d", op); |
| 61 | return buf; |
| 62 | } |
| 63 | |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 64 | int fio_send_data(int sk, const void *p, unsigned int len) |
| 65 | { |
Jens Axboe | 794d69c | 2011-10-01 08:48:50 -0600 | [diff] [blame] | 66 | assert(len <= sizeof(struct fio_net_cmd) + FIO_SERVER_MAX_PDU); |
| 67 | |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 68 | do { |
| 69 | int ret = send(sk, p, len, 0); |
| 70 | |
| 71 | if (ret > 0) { |
| 72 | len -= ret; |
| 73 | if (!len) |
| 74 | break; |
| 75 | p += ret; |
| 76 | continue; |
| 77 | } else if (!ret) |
| 78 | break; |
| 79 | else if (errno == EAGAIN || errno == EINTR) |
| 80 | continue; |
Jens Axboe | 7b82168 | 2011-10-11 12:16:32 +0200 | [diff] [blame] | 81 | else |
| 82 | break; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 83 | } while (!exit_backend); |
| 84 | |
| 85 | if (!len) |
| 86 | return 0; |
| 87 | |
| 88 | return 1; |
| 89 | } |
| 90 | |
| 91 | int fio_recv_data(int sk, void *p, unsigned int len) |
| 92 | { |
| 93 | do { |
| 94 | int ret = recv(sk, p, len, MSG_WAITALL); |
| 95 | |
| 96 | if (ret > 0) { |
| 97 | len -= ret; |
| 98 | if (!len) |
| 99 | break; |
| 100 | p += ret; |
| 101 | continue; |
| 102 | } else if (!ret) |
| 103 | break; |
| 104 | else if (errno == EAGAIN || errno == EINTR) |
| 105 | continue; |
Jens Axboe | 7b82168 | 2011-10-11 12:16:32 +0200 | [diff] [blame] | 106 | else |
| 107 | break; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 108 | } while (!exit_backend); |
| 109 | |
| 110 | if (!len) |
| 111 | return 0; |
| 112 | |
| 113 | return -1; |
| 114 | } |
| 115 | |
| 116 | static int verify_convert_cmd(struct fio_net_cmd *cmd) |
| 117 | { |
Jens Axboe | fcee5ff | 2011-09-30 22:50:39 -0600 | [diff] [blame] | 118 | uint16_t crc; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 119 | |
Jens Axboe | fcee5ff | 2011-09-30 22:50:39 -0600 | [diff] [blame] | 120 | cmd->cmd_crc16 = le16_to_cpu(cmd->cmd_crc16); |
| 121 | cmd->pdu_crc16 = le16_to_cpu(cmd->pdu_crc16); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 122 | |
Jens Axboe | fcee5ff | 2011-09-30 22:50:39 -0600 | [diff] [blame] | 123 | crc = crc16(cmd, FIO_NET_CMD_CRC_SZ); |
| 124 | if (crc != cmd->cmd_crc16) { |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 125 | log_err("fio: server bad crc on command (got %x, wanted %x)\n", |
Jens Axboe | fcee5ff | 2011-09-30 22:50:39 -0600 | [diff] [blame] | 126 | cmd->cmd_crc16, crc); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 127 | return 1; |
| 128 | } |
| 129 | |
| 130 | cmd->version = le16_to_cpu(cmd->version); |
| 131 | cmd->opcode = le16_to_cpu(cmd->opcode); |
| 132 | cmd->flags = le32_to_cpu(cmd->flags); |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 133 | cmd->tag = le64_to_cpu(cmd->tag); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 134 | cmd->pdu_len = le32_to_cpu(cmd->pdu_len); |
| 135 | |
| 136 | switch (cmd->version) { |
Jens Axboe | fa2ea80 | 2011-10-08 21:07:29 +0200 | [diff] [blame] | 137 | case FIO_SERVER_VER: |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 138 | break; |
| 139 | default: |
| 140 | log_err("fio: bad server cmd version %d\n", cmd->version); |
| 141 | return 1; |
| 142 | } |
| 143 | |
| 144 | if (cmd->pdu_len > FIO_SERVER_MAX_PDU) { |
| 145 | log_err("fio: command payload too large: %u\n", cmd->pdu_len); |
| 146 | return 1; |
| 147 | } |
| 148 | |
| 149 | return 0; |
| 150 | } |
| 151 | |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 152 | /* |
| 153 | * Read (and defragment, if necessary) incoming commands |
| 154 | */ |
Jens Axboe | e951bdc | 2011-10-05 21:58:45 +0200 | [diff] [blame] | 155 | struct fio_net_cmd *fio_net_recv_cmd(int sk) |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 156 | { |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 157 | struct fio_net_cmd cmd, *cmdret = NULL; |
| 158 | size_t cmd_size = 0, pdu_offset = 0; |
Jens Axboe | fcee5ff | 2011-09-30 22:50:39 -0600 | [diff] [blame] | 159 | uint16_t crc; |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 160 | int ret, first = 1; |
| 161 | void *pdu = NULL; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 162 | |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 163 | do { |
| 164 | ret = fio_recv_data(sk, &cmd, sizeof(cmd)); |
| 165 | if (ret) |
| 166 | break; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 167 | |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 168 | /* We have a command, verify it and swap if need be */ |
| 169 | ret = verify_convert_cmd(&cmd); |
| 170 | if (ret) |
| 171 | break; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 172 | |
Jens Axboe | 0b8f30a | 2011-10-04 10:31:53 +0200 | [diff] [blame] | 173 | if (first) { |
| 174 | /* if this is text, add room for \0 at the end */ |
| 175 | cmd_size = sizeof(cmd) + cmd.pdu_len + 1; |
| 176 | assert(!cmdret); |
| 177 | } else |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 178 | cmd_size += cmd.pdu_len; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 179 | |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 180 | cmdret = realloc(cmdret, cmd_size); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 181 | |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 182 | if (first) |
| 183 | memcpy(cmdret, &cmd, sizeof(cmd)); |
| 184 | else |
| 185 | assert(cmdret->opcode == cmd.opcode); |
| 186 | |
| 187 | if (!cmd.pdu_len) |
| 188 | break; |
| 189 | |
| 190 | /* There's payload, get it */ |
| 191 | pdu = (void *) cmdret->payload + pdu_offset; |
| 192 | ret = fio_recv_data(sk, pdu, cmd.pdu_len); |
| 193 | if (ret) |
| 194 | break; |
| 195 | |
| 196 | /* Verify payload crc */ |
| 197 | crc = crc16(pdu, cmd.pdu_len); |
| 198 | if (crc != cmd.pdu_crc16) { |
| 199 | log_err("fio: server bad crc on payload "); |
| 200 | log_err("(got %x, wanted %x)\n", cmd.pdu_crc16, crc); |
| 201 | ret = 1; |
| 202 | break; |
| 203 | } |
| 204 | |
| 205 | pdu_offset += cmd.pdu_len; |
Jens Axboe | 817f06b | 2011-10-03 15:03:08 +0200 | [diff] [blame] | 206 | if (!first) |
| 207 | cmdret->pdu_len += cmd.pdu_len; |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 208 | first = 0; |
| 209 | } while (cmd.flags & FIO_NET_CMD_F_MORE); |
| 210 | |
| 211 | if (ret) { |
| 212 | free(cmdret); |
| 213 | cmdret = NULL; |
Jens Axboe | 0b8f30a | 2011-10-04 10:31:53 +0200 | [diff] [blame] | 214 | } else if (cmdret) { |
| 215 | /* zero-terminate text input */ |
| 216 | if (cmdret->pdu_len && (cmdret->opcode == FIO_NET_CMD_TEXT || |
| 217 | cmdret->opcode == FIO_NET_CMD_JOB)) { |
| 218 | char *buf = (char *) cmdret->payload; |
| 219 | |
| 220 | buf[cmdret->pdu_len ] = '\0'; |
| 221 | } |
| 222 | /* frag flag is internal */ |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 223 | cmdret->flags &= ~FIO_NET_CMD_F_MORE; |
Jens Axboe | 0b8f30a | 2011-10-04 10:31:53 +0200 | [diff] [blame] | 224 | } |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 225 | |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 226 | return cmdret; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | void fio_net_cmd_crc(struct fio_net_cmd *cmd) |
| 230 | { |
| 231 | uint32_t pdu_len; |
| 232 | |
Jens Axboe | ddcc0b6 | 2011-10-03 14:45:27 +0200 | [diff] [blame] | 233 | cmd->cmd_crc16 = __cpu_to_le16(crc16(cmd, FIO_NET_CMD_CRC_SZ)); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 234 | |
| 235 | pdu_len = le32_to_cpu(cmd->pdu_len); |
| 236 | if (pdu_len) |
Jens Axboe | ddcc0b6 | 2011-10-03 14:45:27 +0200 | [diff] [blame] | 237 | cmd->pdu_crc16 = __cpu_to_le16(crc16(cmd->payload, pdu_len)); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 238 | } |
| 239 | |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 240 | int fio_net_send_cmd(int fd, uint16_t opcode, const void *buf, off_t size, |
| 241 | uint64_t tag) |
Jens Axboe | 794d69c | 2011-10-01 08:48:50 -0600 | [diff] [blame] | 242 | { |
Jens Axboe | 7f86831 | 2011-10-10 09:55:21 +0200 | [diff] [blame] | 243 | struct fio_net_cmd *cmd = NULL; |
| 244 | size_t this_len, cur_len = 0; |
Jens Axboe | 794d69c | 2011-10-01 08:48:50 -0600 | [diff] [blame] | 245 | int ret; |
| 246 | |
| 247 | do { |
| 248 | this_len = size; |
| 249 | if (this_len > FIO_SERVER_MAX_PDU) |
| 250 | this_len = FIO_SERVER_MAX_PDU; |
| 251 | |
Jens Axboe | 7f86831 | 2011-10-10 09:55:21 +0200 | [diff] [blame] | 252 | if (!cmd || cur_len < sizeof(*cmd) + this_len) { |
| 253 | if (cmd) |
| 254 | free(cmd); |
| 255 | |
| 256 | cur_len = sizeof(*cmd) + this_len; |
| 257 | cmd = malloc(cur_len); |
| 258 | } |
Jens Axboe | 794d69c | 2011-10-01 08:48:50 -0600 | [diff] [blame] | 259 | |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 260 | fio_init_net_cmd(cmd, opcode, buf, this_len, tag); |
Jens Axboe | 794d69c | 2011-10-01 08:48:50 -0600 | [diff] [blame] | 261 | |
| 262 | if (this_len < size) |
Jens Axboe | ddcc0b6 | 2011-10-03 14:45:27 +0200 | [diff] [blame] | 263 | cmd->flags = __cpu_to_le32(FIO_NET_CMD_F_MORE); |
Jens Axboe | 794d69c | 2011-10-01 08:48:50 -0600 | [diff] [blame] | 264 | |
| 265 | fio_net_cmd_crc(cmd); |
| 266 | |
| 267 | ret = fio_send_data(fd, cmd, sizeof(*cmd) + this_len); |
Jens Axboe | 794d69c | 2011-10-01 08:48:50 -0600 | [diff] [blame] | 268 | size -= this_len; |
| 269 | buf += this_len; |
| 270 | } while (!ret && size); |
| 271 | |
Jens Axboe | 7f86831 | 2011-10-10 09:55:21 +0200 | [diff] [blame] | 272 | if (cmd) |
| 273 | free(cmd); |
| 274 | |
Jens Axboe | 794d69c | 2011-10-01 08:48:50 -0600 | [diff] [blame] | 275 | return ret; |
| 276 | } |
| 277 | |
Jens Axboe | 89c1707 | 2011-10-11 10:15:51 +0200 | [diff] [blame] | 278 | static int fio_net_send_simple_stack_cmd(int sk, uint16_t opcode, uint64_t tag) |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 279 | { |
Jens Axboe | 178cde9 | 2011-10-05 22:14:31 +0200 | [diff] [blame] | 280 | struct fio_net_cmd cmd; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 281 | |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 282 | fio_init_net_cmd(&cmd, opcode, NULL, 0, tag); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 283 | fio_net_cmd_crc(&cmd); |
| 284 | |
| 285 | return fio_send_data(sk, &cmd, sizeof(cmd)); |
| 286 | } |
| 287 | |
Jens Axboe | 89c1707 | 2011-10-11 10:15:51 +0200 | [diff] [blame] | 288 | /* |
| 289 | * If 'list' is non-NULL, then allocate and store the sent command for |
| 290 | * later verification. |
| 291 | */ |
| 292 | int fio_net_send_simple_cmd(int sk, uint16_t opcode, uint64_t tag, |
| 293 | struct flist_head *list) |
| 294 | { |
| 295 | struct fio_net_int_cmd *cmd; |
| 296 | int ret; |
| 297 | |
| 298 | if (!list) |
| 299 | return fio_net_send_simple_stack_cmd(sk, opcode, tag); |
| 300 | |
| 301 | cmd = malloc(sizeof(*cmd)); |
| 302 | |
Jens Axboe | df38093 | 2011-10-11 14:25:08 +0200 | [diff] [blame] | 303 | fio_init_net_cmd(&cmd->cmd, opcode, NULL, 0, (uintptr_t) cmd); |
Jens Axboe | 89c1707 | 2011-10-11 10:15:51 +0200 | [diff] [blame] | 304 | fio_net_cmd_crc(&cmd->cmd); |
| 305 | |
| 306 | INIT_FLIST_HEAD(&cmd->list); |
| 307 | gettimeofday(&cmd->tv, NULL); |
| 308 | cmd->saved_tag = tag; |
| 309 | |
| 310 | ret = fio_send_data(sk, &cmd->cmd, sizeof(cmd->cmd)); |
| 311 | if (ret) { |
| 312 | free(cmd); |
| 313 | return ret; |
| 314 | } |
| 315 | |
| 316 | flist_add_tail(&cmd->list, list); |
| 317 | return 0; |
| 318 | } |
| 319 | |
Jens Axboe | 9abea48 | 2011-10-04 13:21:54 +0200 | [diff] [blame] | 320 | static int fio_server_send_quit_cmd(void) |
Jens Axboe | 437377e | 2011-10-01 08:31:30 -0600 | [diff] [blame] | 321 | { |
Jens Axboe | 46c48f1 | 2011-10-01 12:50:51 -0600 | [diff] [blame] | 322 | dprint(FD_NET, "server: sending quit\n"); |
Jens Axboe | 89c1707 | 2011-10-11 10:15:51 +0200 | [diff] [blame] | 323 | return fio_net_send_simple_cmd(server_fd, FIO_NET_CMD_QUIT, 0, NULL); |
Jens Axboe | 437377e | 2011-10-01 08:31:30 -0600 | [diff] [blame] | 324 | } |
| 325 | |
Jens Axboe | 0b8f30a | 2011-10-04 10:31:53 +0200 | [diff] [blame] | 326 | static int handle_job_cmd(struct fio_net_cmd *cmd) |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 327 | { |
Jens Axboe | 0b8f30a | 2011-10-04 10:31:53 +0200 | [diff] [blame] | 328 | char *buf = (char *) cmd->payload; |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 329 | int ret; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 330 | |
Jens Axboe | e6d1c66 | 2011-10-05 20:41:06 +0200 | [diff] [blame] | 331 | if (parse_jobs_ini(buf, 1, 0)) { |
| 332 | fio_server_send_quit_cmd(); |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 333 | return -1; |
Jens Axboe | e6d1c66 | 2011-10-05 20:41:06 +0200 | [diff] [blame] | 334 | } |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 335 | |
Jens Axboe | 89c1707 | 2011-10-11 10:15:51 +0200 | [diff] [blame] | 336 | fio_net_send_simple_cmd(server_fd, FIO_NET_CMD_START, 0, NULL); |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 337 | |
| 338 | ret = exec_run(); |
Jens Axboe | 9abea48 | 2011-10-04 13:21:54 +0200 | [diff] [blame] | 339 | fio_server_send_quit_cmd(); |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 340 | reset_fio_state(); |
| 341 | return ret; |
| 342 | } |
| 343 | |
| 344 | static int handle_jobline_cmd(struct fio_net_cmd *cmd) |
| 345 | { |
Jens Axboe | fa2ea80 | 2011-10-08 21:07:29 +0200 | [diff] [blame] | 346 | void *pdu = cmd->payload; |
| 347 | struct cmd_single_line_pdu *cslp; |
| 348 | struct cmd_line_pdu *clp; |
| 349 | unsigned long offset; |
| 350 | char **argv; |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 351 | int ret, i; |
| 352 | |
Jens Axboe | fa2ea80 | 2011-10-08 21:07:29 +0200 | [diff] [blame] | 353 | clp = pdu; |
| 354 | clp->lines = le16_to_cpu(clp->lines); |
| 355 | argv = malloc(clp->lines * sizeof(char *)); |
| 356 | offset = sizeof(*clp); |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 357 | |
Jens Axboe | fa2ea80 | 2011-10-08 21:07:29 +0200 | [diff] [blame] | 358 | dprint(FD_NET, "server: %d command line args\n", clp->lines); |
Jens Axboe | 39e8e01 | 2011-10-04 13:46:08 +0200 | [diff] [blame] | 359 | |
Jens Axboe | fa2ea80 | 2011-10-08 21:07:29 +0200 | [diff] [blame] | 360 | for (i = 0; i < clp->lines; i++) { |
| 361 | cslp = pdu + offset; |
| 362 | argv[i] = (char *) cslp->text; |
| 363 | |
| 364 | offset += sizeof(*cslp) + le16_to_cpu(cslp->len); |
Jens Axboe | 39e8e01 | 2011-10-04 13:46:08 +0200 | [diff] [blame] | 365 | dprint(FD_NET, "server: %d: %s\n", i, argv[i]); |
| 366 | } |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 367 | |
Jens Axboe | fa2ea80 | 2011-10-08 21:07:29 +0200 | [diff] [blame] | 368 | if (parse_cmd_line(clp->lines, argv)) { |
Jens Axboe | e6d1c66 | 2011-10-05 20:41:06 +0200 | [diff] [blame] | 369 | fio_server_send_quit_cmd(); |
Jens Axboe | fa2ea80 | 2011-10-08 21:07:29 +0200 | [diff] [blame] | 370 | free(argv); |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 371 | return -1; |
Jens Axboe | e6d1c66 | 2011-10-05 20:41:06 +0200 | [diff] [blame] | 372 | } |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 373 | |
Jens Axboe | fa2ea80 | 2011-10-08 21:07:29 +0200 | [diff] [blame] | 374 | free(argv); |
| 375 | |
Jens Axboe | 89c1707 | 2011-10-11 10:15:51 +0200 | [diff] [blame] | 376 | fio_net_send_simple_cmd(server_fd, FIO_NET_CMD_START, 0, NULL); |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 377 | |
Jens Axboe | 794d69c | 2011-10-01 08:48:50 -0600 | [diff] [blame] | 378 | ret = exec_run(); |
Jens Axboe | 9abea48 | 2011-10-04 13:21:54 +0200 | [diff] [blame] | 379 | fio_server_send_quit_cmd(); |
Jens Axboe | 794d69c | 2011-10-01 08:48:50 -0600 | [diff] [blame] | 380 | reset_fio_state(); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 381 | return ret; |
| 382 | } |
| 383 | |
Jens Axboe | c28e8e8 | 2011-10-04 08:57:39 +0200 | [diff] [blame] | 384 | static int handle_probe_cmd(struct fio_net_cmd *cmd) |
| 385 | { |
| 386 | struct cmd_probe_pdu probe; |
| 387 | |
Jens Axboe | 89c1707 | 2011-10-11 10:15:51 +0200 | [diff] [blame] | 388 | dprint(FD_NET, "server: sending probe reply\n"); |
| 389 | |
Jens Axboe | c28e8e8 | 2011-10-04 08:57:39 +0200 | [diff] [blame] | 390 | memset(&probe, 0, sizeof(probe)); |
| 391 | gethostname((char *) probe.hostname, sizeof(probe.hostname)); |
Jens Axboe | 6eb2479 | 2011-10-04 15:00:30 +0200 | [diff] [blame] | 392 | #ifdef FIO_BIG_ENDIAN |
| 393 | probe.bigendian = 1; |
| 394 | #endif |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 395 | probe.fio_major = FIO_MAJOR; |
| 396 | probe.fio_minor = FIO_MINOR; |
| 397 | probe.fio_patch = FIO_PATCH; |
Jens Axboe | c28e8e8 | 2011-10-04 08:57:39 +0200 | [diff] [blame] | 398 | |
Jens Axboe | cca8464 | 2011-10-07 12:47:57 +0200 | [diff] [blame] | 399 | probe.os = FIO_OS; |
| 400 | probe.arch = FIO_ARCH; |
| 401 | |
Jens Axboe | 38fdef2 | 2011-10-11 14:30:06 +0200 | [diff] [blame] | 402 | probe.bpp = sizeof(void *); |
| 403 | |
Jens Axboe | 89c1707 | 2011-10-11 10:15:51 +0200 | [diff] [blame] | 404 | return fio_net_send_cmd(server_fd, FIO_NET_CMD_PROBE, &probe, sizeof(probe), cmd->tag); |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | static int handle_send_eta_cmd(struct fio_net_cmd *cmd) |
| 408 | { |
| 409 | struct jobs_eta *je; |
| 410 | size_t size; |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 411 | int i; |
| 412 | |
Jens Axboe | b814fb2 | 2011-10-12 09:20:34 +0200 | [diff] [blame] | 413 | if (!thread_number) |
| 414 | return 0; |
| 415 | |
| 416 | size = sizeof(*je) + thread_number * sizeof(char) + 1; |
Jens Axboe | 7f86831 | 2011-10-10 09:55:21 +0200 | [diff] [blame] | 417 | je = malloc(size); |
| 418 | memset(je, 0, size); |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 419 | |
| 420 | if (!calc_thread_status(je, 1)) { |
| 421 | free(je); |
| 422 | return 0; |
| 423 | } |
| 424 | |
| 425 | dprint(FD_NET, "server sending status\n"); |
| 426 | |
| 427 | je->nr_running = cpu_to_le32(je->nr_running); |
| 428 | je->nr_ramp = cpu_to_le32(je->nr_ramp); |
| 429 | je->nr_pending = cpu_to_le32(je->nr_pending); |
| 430 | je->files_open = cpu_to_le32(je->files_open); |
| 431 | je->m_rate = cpu_to_le32(je->m_rate); |
| 432 | je->t_rate = cpu_to_le32(je->t_rate); |
| 433 | je->m_iops = cpu_to_le32(je->m_iops); |
| 434 | je->t_iops = cpu_to_le32(je->t_iops); |
| 435 | |
| 436 | for (i = 0; i < 2; i++) { |
| 437 | je->rate[i] = cpu_to_le32(je->rate[i]); |
| 438 | je->iops[i] = cpu_to_le32(je->iops[i]); |
| 439 | } |
| 440 | |
| 441 | je->elapsed_sec = cpu_to_le32(je->nr_running); |
| 442 | je->eta_sec = cpu_to_le64(je->eta_sec); |
| 443 | |
Jens Axboe | 7f86831 | 2011-10-10 09:55:21 +0200 | [diff] [blame] | 444 | fio_net_send_cmd(server_fd, FIO_NET_CMD_ETA, je, size, cmd->tag); |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 445 | free(je); |
| 446 | return 0; |
Jens Axboe | c28e8e8 | 2011-10-04 08:57:39 +0200 | [diff] [blame] | 447 | } |
| 448 | |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 449 | static int handle_command(struct fio_net_cmd *cmd) |
| 450 | { |
| 451 | int ret; |
| 452 | |
Jens Axboe | 89c1707 | 2011-10-11 10:15:51 +0200 | [diff] [blame] | 453 | dprint(FD_NET, "server: got op [%s], pdu=%u, tag=%lx\n", |
| 454 | fio_server_op(cmd->opcode), cmd->pdu_len, cmd->tag); |
Jens Axboe | 46c48f1 | 2011-10-01 12:50:51 -0600 | [diff] [blame] | 455 | |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 456 | switch (cmd->opcode) { |
| 457 | case FIO_NET_CMD_QUIT: |
Jens Axboe | cc0df00 | 2011-10-03 20:53:32 +0200 | [diff] [blame] | 458 | fio_terminate_threads(TERMINATE_ALL); |
Jens Axboe | c28e8e8 | 2011-10-04 08:57:39 +0200 | [diff] [blame] | 459 | return -1; |
Jens Axboe | d795918 | 2011-10-03 11:48:39 +0200 | [diff] [blame] | 460 | case FIO_NET_CMD_EXIT: |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 461 | exit_backend = 1; |
Jens Axboe | c28e8e8 | 2011-10-04 08:57:39 +0200 | [diff] [blame] | 462 | return -1; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 463 | case FIO_NET_CMD_JOB: |
Jens Axboe | 0b8f30a | 2011-10-04 10:31:53 +0200 | [diff] [blame] | 464 | ret = handle_job_cmd(cmd); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 465 | break; |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 466 | case FIO_NET_CMD_JOBLINE: |
| 467 | ret = handle_jobline_cmd(cmd); |
| 468 | break; |
Jens Axboe | c28e8e8 | 2011-10-04 08:57:39 +0200 | [diff] [blame] | 469 | case FIO_NET_CMD_PROBE: |
| 470 | ret = handle_probe_cmd(cmd); |
| 471 | break; |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 472 | case FIO_NET_CMD_SEND_ETA: |
| 473 | ret = handle_send_eta_cmd(cmd); |
| 474 | break; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 475 | default: |
Jens Axboe | 89c1707 | 2011-10-11 10:15:51 +0200 | [diff] [blame] | 476 | log_err("fio: unknown opcode: %s\n",fio_server_op(cmd->opcode)); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 477 | ret = 1; |
| 478 | } |
| 479 | |
| 480 | return ret; |
| 481 | } |
| 482 | |
Jens Axboe | 70e0c31 | 2011-10-04 09:18:30 +0200 | [diff] [blame] | 483 | static int handle_connection(int sk, int block) |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 484 | { |
| 485 | struct fio_net_cmd *cmd = NULL; |
| 486 | int ret = 0; |
| 487 | |
| 488 | /* read forever */ |
| 489 | while (!exit_backend) { |
Jens Axboe | e951bdc | 2011-10-05 21:58:45 +0200 | [diff] [blame] | 490 | struct pollfd pfd = { |
| 491 | .fd = sk, |
| 492 | .events = POLLIN, |
| 493 | }; |
| 494 | |
| 495 | ret = 0; |
| 496 | do { |
| 497 | ret = poll(&pfd, 1, 100); |
| 498 | if (ret < 0) { |
| 499 | if (errno == EINTR) |
| 500 | break; |
| 501 | log_err("fio: poll: %s\n", strerror(errno)); |
| 502 | break; |
Jens Axboe | 19c6517 | 2011-10-05 22:05:37 +0200 | [diff] [blame] | 503 | } else if (!ret) { |
| 504 | if (!block) |
| 505 | return 0; |
Jens Axboe | e951bdc | 2011-10-05 21:58:45 +0200 | [diff] [blame] | 506 | continue; |
Jens Axboe | 19c6517 | 2011-10-05 22:05:37 +0200 | [diff] [blame] | 507 | } |
Jens Axboe | e951bdc | 2011-10-05 21:58:45 +0200 | [diff] [blame] | 508 | |
| 509 | if (pfd.revents & POLLIN) |
| 510 | break; |
| 511 | if (pfd.revents & (POLLERR|POLLHUP)) { |
| 512 | ret = 1; |
| 513 | break; |
| 514 | } |
Jens Axboe | 19c6517 | 2011-10-05 22:05:37 +0200 | [diff] [blame] | 515 | } while (!exit_backend); |
Jens Axboe | e951bdc | 2011-10-05 21:58:45 +0200 | [diff] [blame] | 516 | |
| 517 | if (ret < 0) |
| 518 | break; |
| 519 | |
| 520 | cmd = fio_net_recv_cmd(sk); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 521 | if (!cmd) { |
Jens Axboe | c28e8e8 | 2011-10-04 08:57:39 +0200 | [diff] [blame] | 522 | ret = -1; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 523 | break; |
| 524 | } |
| 525 | |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 526 | ret = handle_command(cmd); |
| 527 | if (ret) |
| 528 | break; |
| 529 | |
| 530 | free(cmd); |
Jens Axboe | c77a99e | 2011-10-01 16:26:42 -0400 | [diff] [blame] | 531 | cmd = NULL; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | if (cmd) |
| 535 | free(cmd); |
| 536 | |
| 537 | return ret; |
| 538 | } |
| 539 | |
Jens Axboe | cc0df00 | 2011-10-03 20:53:32 +0200 | [diff] [blame] | 540 | void fio_server_idle_loop(void) |
| 541 | { |
| 542 | if (server_fd != -1) |
Jens Axboe | 70e0c31 | 2011-10-04 09:18:30 +0200 | [diff] [blame] | 543 | handle_connection(server_fd, 0); |
Jens Axboe | cc0df00 | 2011-10-03 20:53:32 +0200 | [diff] [blame] | 544 | } |
| 545 | |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 546 | static int accept_loop(int listen_sk) |
| 547 | { |
Jens Axboe | bb447a2 | 2011-10-04 15:06:42 +0200 | [diff] [blame] | 548 | struct sockaddr_in addr; |
Jens Axboe | 5ba13ea | 2011-10-04 23:50:28 +0200 | [diff] [blame] | 549 | fio_socklen_t len = sizeof(addr); |
Jens Axboe | 009b1be | 2011-09-29 18:27:02 -0600 | [diff] [blame] | 550 | struct pollfd pfd; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 551 | int ret, sk, flags, exitval = 0; |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 552 | |
Jens Axboe | 60efd14 | 2011-10-04 13:30:11 +0200 | [diff] [blame] | 553 | dprint(FD_NET, "server enter accept loop\n"); |
| 554 | |
Jens Axboe | 009b1be | 2011-09-29 18:27:02 -0600 | [diff] [blame] | 555 | flags = fcntl(listen_sk, F_GETFL); |
| 556 | flags |= O_NONBLOCK; |
| 557 | fcntl(listen_sk, F_SETFL, flags); |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 558 | again: |
Jens Axboe | 009b1be | 2011-09-29 18:27:02 -0600 | [diff] [blame] | 559 | pfd.fd = listen_sk; |
| 560 | pfd.events = POLLIN; |
| 561 | do { |
| 562 | ret = poll(&pfd, 1, 100); |
| 563 | if (ret < 0) { |
| 564 | if (errno == EINTR) |
| 565 | break; |
Jens Axboe | fcee5ff | 2011-09-30 22:50:39 -0600 | [diff] [blame] | 566 | log_err("fio: poll: %s\n", strerror(errno)); |
Jens Axboe | 009b1be | 2011-09-29 18:27:02 -0600 | [diff] [blame] | 567 | goto out; |
| 568 | } else if (!ret) |
| 569 | continue; |
| 570 | |
| 571 | if (pfd.revents & POLLIN) |
| 572 | break; |
| 573 | } while (!exit_backend); |
| 574 | |
| 575 | if (exit_backend) |
| 576 | goto out; |
| 577 | |
Jens Axboe | 6b976bf | 2011-10-04 15:07:43 +0200 | [diff] [blame] | 578 | sk = accept(listen_sk, (struct sockaddr *) &addr, &len); |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 579 | if (sk < 0) { |
Jens Axboe | 690e09a | 2011-09-29 18:38:08 -0600 | [diff] [blame] | 580 | log_err("fio: accept: %s\n", strerror(errno)); |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 581 | return -1; |
| 582 | } |
| 583 | |
Jens Axboe | bb447a2 | 2011-10-04 15:06:42 +0200 | [diff] [blame] | 584 | dprint(FD_NET, "server: connect from %s\n", inet_ntoa(addr.sin_addr)); |
Jens Axboe | 46c48f1 | 2011-10-01 12:50:51 -0600 | [diff] [blame] | 585 | |
Jens Axboe | 37db14f | 2011-09-30 17:00:42 -0600 | [diff] [blame] | 586 | server_fd = sk; |
| 587 | |
Jens Axboe | 70e0c31 | 2011-10-04 09:18:30 +0200 | [diff] [blame] | 588 | exitval = handle_connection(sk, 1); |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 589 | |
Jens Axboe | 37db14f | 2011-09-30 17:00:42 -0600 | [diff] [blame] | 590 | server_fd = -1; |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 591 | close(sk); |
Jens Axboe | 5c341e9 | 2011-09-29 18:00:35 -0600 | [diff] [blame] | 592 | |
Jens Axboe | 009b1be | 2011-09-29 18:27:02 -0600 | [diff] [blame] | 593 | if (!exit_backend) |
Jens Axboe | 5c341e9 | 2011-09-29 18:00:35 -0600 | [diff] [blame] | 594 | goto again; |
| 595 | |
Jens Axboe | 009b1be | 2011-09-29 18:27:02 -0600 | [diff] [blame] | 596 | out: |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 597 | return exitval; |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 598 | } |
| 599 | |
Jens Axboe | 13755d9 | 2011-10-10 19:51:26 +0200 | [diff] [blame] | 600 | int fio_server_text_output(const char *buf, size_t len) |
Jens Axboe | 37db14f | 2011-09-30 17:00:42 -0600 | [diff] [blame] | 601 | { |
Jens Axboe | 337d75a | 2011-10-01 12:36:32 -0600 | [diff] [blame] | 602 | if (server_fd != -1) |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 603 | return fio_net_send_cmd(server_fd, FIO_NET_CMD_TEXT, buf, len, 0); |
Jens Axboe | 337d75a | 2011-10-01 12:36:32 -0600 | [diff] [blame] | 604 | |
Jens Axboe | 13755d9 | 2011-10-10 19:51:26 +0200 | [diff] [blame] | 605 | return log_local_buf(buf, len); |
Jens Axboe | 142575e | 2011-09-30 17:35:45 -0600 | [diff] [blame] | 606 | } |
| 607 | |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 608 | static void convert_io_stat(struct io_stat *dst, struct io_stat *src) |
| 609 | { |
| 610 | dst->max_val = cpu_to_le64(src->max_val); |
| 611 | dst->min_val = cpu_to_le64(src->min_val); |
| 612 | dst->samples = cpu_to_le64(src->samples); |
Jens Axboe | 802ad4a | 2011-10-05 09:51:58 +0200 | [diff] [blame] | 613 | |
| 614 | /* |
| 615 | * Encode to IEEE 754 for network transfer |
| 616 | */ |
| 617 | dst->mean.u.i = __cpu_to_le64(fio_double_to_uint64(src->mean.u.f)); |
| 618 | dst->S.u.i = __cpu_to_le64(fio_double_to_uint64(src->S.u.f)); |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src) |
| 622 | { |
| 623 | int i; |
| 624 | |
| 625 | for (i = 0; i < 2; i++) { |
| 626 | dst->max_run[i] = cpu_to_le64(src->max_run[i]); |
| 627 | dst->min_run[i] = cpu_to_le64(src->min_run[i]); |
| 628 | dst->max_bw[i] = cpu_to_le64(src->max_bw[i]); |
| 629 | dst->min_bw[i] = cpu_to_le64(src->min_bw[i]); |
| 630 | dst->io_kb[i] = cpu_to_le64(src->io_kb[i]); |
| 631 | dst->agg[i] = cpu_to_le64(src->agg[i]); |
| 632 | } |
| 633 | |
| 634 | dst->kb_base = cpu_to_le32(src->kb_base); |
| 635 | dst->groupid = cpu_to_le32(src->groupid); |
| 636 | } |
| 637 | |
| 638 | /* |
| 639 | * Send a CMD_TS, which packs struct thread_stat and group_run_stats |
| 640 | * into a single payload. |
| 641 | */ |
| 642 | void fio_server_send_ts(struct thread_stat *ts, struct group_run_stats *rs) |
| 643 | { |
| 644 | struct cmd_ts_pdu p; |
| 645 | int i, j; |
| 646 | |
Jens Axboe | 60efd14 | 2011-10-04 13:30:11 +0200 | [diff] [blame] | 647 | dprint(FD_NET, "server sending end stats\n"); |
| 648 | |
Jens Axboe | 317b3c8 | 2011-10-03 21:47:27 +0200 | [diff] [blame] | 649 | memset(&p, 0, sizeof(p)); |
| 650 | |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 651 | strcpy(p.ts.name, ts->name); |
| 652 | strcpy(p.ts.verror, ts->verror); |
| 653 | strcpy(p.ts.description, ts->description); |
| 654 | |
Jens Axboe | ddcc0b6 | 2011-10-03 14:45:27 +0200 | [diff] [blame] | 655 | p.ts.error = cpu_to_le32(ts->error); |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 656 | p.ts.groupid = cpu_to_le32(ts->groupid); |
Jens Axboe | ddcc0b6 | 2011-10-03 14:45:27 +0200 | [diff] [blame] | 657 | p.ts.pid = cpu_to_le32(ts->pid); |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 658 | p.ts.members = cpu_to_le32(ts->members); |
| 659 | |
| 660 | for (i = 0; i < 2; i++) { |
| 661 | convert_io_stat(&p.ts.clat_stat[i], &ts->clat_stat[i]); |
| 662 | convert_io_stat(&p.ts.slat_stat[i], &ts->slat_stat[i]); |
| 663 | convert_io_stat(&p.ts.lat_stat[i], &ts->lat_stat[i]); |
| 664 | convert_io_stat(&p.ts.bw_stat[i], &ts->bw_stat[i]); |
| 665 | } |
| 666 | |
| 667 | p.ts.usr_time = cpu_to_le64(ts->usr_time); |
| 668 | p.ts.sys_time = cpu_to_le64(ts->sys_time); |
| 669 | p.ts.ctx = cpu_to_le64(ts->ctx); |
| 670 | p.ts.minf = cpu_to_le64(ts->minf); |
| 671 | p.ts.majf = cpu_to_le64(ts->majf); |
| 672 | p.ts.clat_percentiles = cpu_to_le64(ts->clat_percentiles); |
Jens Axboe | 802ad4a | 2011-10-05 09:51:58 +0200 | [diff] [blame] | 673 | |
| 674 | for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) { |
Jens Axboe | cfc03e4 | 2011-10-12 21:20:42 +0200 | [diff] [blame] | 675 | fio_fp64_t *src = &ts->percentile_list[i]; |
| 676 | fio_fp64_t *dst = &p.ts.percentile_list[i]; |
Jens Axboe | 802ad4a | 2011-10-05 09:51:58 +0200 | [diff] [blame] | 677 | |
Jens Axboe | cfc03e4 | 2011-10-12 21:20:42 +0200 | [diff] [blame] | 678 | dst->u.i = __cpu_to_le64(fio_double_to_uint64(src->u.f)); |
Jens Axboe | 802ad4a | 2011-10-05 09:51:58 +0200 | [diff] [blame] | 679 | } |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 680 | |
| 681 | for (i = 0; i < FIO_IO_U_MAP_NR; i++) { |
| 682 | p.ts.io_u_map[i] = cpu_to_le32(ts->io_u_map[i]); |
| 683 | p.ts.io_u_submit[i] = cpu_to_le32(ts->io_u_submit[i]); |
| 684 | p.ts.io_u_complete[i] = cpu_to_le32(ts->io_u_complete[i]); |
| 685 | } |
| 686 | |
| 687 | for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) { |
| 688 | p.ts.io_u_lat_u[i] = cpu_to_le32(ts->io_u_lat_u[i]); |
| 689 | p.ts.io_u_lat_m[i] = cpu_to_le32(ts->io_u_lat_m[i]); |
| 690 | } |
| 691 | |
| 692 | for (i = 0; i < 2; i++) |
| 693 | for (j = 0; j < FIO_IO_U_PLAT_NR; j++) |
| 694 | p.ts.io_u_plat[i][j] = cpu_to_le32(ts->io_u_plat[i][j]); |
| 695 | |
| 696 | for (i = 0; i < 3; i++) { |
| 697 | p.ts.total_io_u[i] = cpu_to_le64(ts->total_io_u[i]); |
Jens Axboe | 93eee04 | 2011-10-03 21:08:48 +0200 | [diff] [blame] | 698 | p.ts.short_io_u[i] = cpu_to_le64(ts->short_io_u[i]); |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 699 | } |
| 700 | |
Jens Axboe | 93eee04 | 2011-10-03 21:08:48 +0200 | [diff] [blame] | 701 | p.ts.total_submit = cpu_to_le64(ts->total_submit); |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 702 | p.ts.total_complete = cpu_to_le64(ts->total_complete); |
| 703 | |
| 704 | for (i = 0; i < 2; i++) { |
| 705 | p.ts.io_bytes[i] = cpu_to_le64(ts->io_bytes[i]); |
| 706 | p.ts.runtime[i] = cpu_to_le64(ts->runtime[i]); |
| 707 | } |
| 708 | |
| 709 | p.ts.total_run_time = cpu_to_le64(ts->total_run_time); |
| 710 | p.ts.continue_on_error = cpu_to_le16(ts->continue_on_error); |
| 711 | p.ts.total_err_count = cpu_to_le64(ts->total_err_count); |
Jens Axboe | ddcc0b6 | 2011-10-03 14:45:27 +0200 | [diff] [blame] | 712 | p.ts.first_error = cpu_to_le32(ts->first_error); |
| 713 | p.ts.kb_base = cpu_to_le32(ts->kb_base); |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 714 | |
| 715 | convert_gs(&p.rs, rs); |
| 716 | |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 717 | fio_net_send_cmd(server_fd, FIO_NET_CMD_TS, &p, sizeof(p), 0); |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | void fio_server_send_gs(struct group_run_stats *rs) |
| 721 | { |
| 722 | struct group_run_stats gs; |
| 723 | |
Jens Axboe | 60efd14 | 2011-10-04 13:30:11 +0200 | [diff] [blame] | 724 | dprint(FD_NET, "server sending group run stats\n"); |
| 725 | |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 726 | convert_gs(&gs, rs); |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 727 | fio_net_send_cmd(server_fd, FIO_NET_CMD_GS, &gs, sizeof(gs), 0); |
Jens Axboe | cf451d1 | 2011-10-03 16:48:30 +0200 | [diff] [blame] | 728 | } |
| 729 | |
Jens Axboe | d09a64a | 2011-10-13 11:38:56 +0200 | [diff] [blame] | 730 | static void convert_agg(struct disk_util_agg *dst, struct disk_util_agg *src) |
| 731 | { |
| 732 | int i; |
| 733 | |
| 734 | for (i = 0; i < 2; i++) { |
| 735 | dst->ios[i] = cpu_to_le32(src->ios[i]); |
| 736 | dst->merges[i] = cpu_to_le32(src->merges[i]); |
| 737 | dst->sectors[i] = cpu_to_le64(src->sectors[i]); |
| 738 | dst->ticks[i] = cpu_to_le32(src->ticks[i]); |
| 739 | } |
| 740 | |
| 741 | dst->io_ticks = cpu_to_le32(src->io_ticks); |
| 742 | dst->time_in_queue = cpu_to_le32(src->time_in_queue); |
| 743 | dst->slavecount = cpu_to_le32(src->slavecount); |
| 744 | dst->max_util.u.i = __cpu_to_le64(fio_double_to_uint64(src->max_util.u.f)); |
| 745 | } |
| 746 | |
| 747 | static void convert_dus(struct disk_util_stat *dst, struct disk_util_stat *src) |
| 748 | { |
| 749 | int i; |
| 750 | |
| 751 | strcpy((char *) dst->name, (char *) src->name); |
| 752 | |
| 753 | for (i = 0; i < 2; i++) { |
| 754 | dst->ios[i] = cpu_to_le32(src->ios[i]); |
| 755 | dst->merges[i] = cpu_to_le32(src->merges[i]); |
| 756 | dst->sectors[i] = cpu_to_le64(src->sectors[i]); |
| 757 | dst->ticks[i] = cpu_to_le32(src->ticks[i]); |
| 758 | } |
| 759 | |
| 760 | dst->io_ticks = cpu_to_le32(src->io_ticks); |
| 761 | dst->time_in_queue = cpu_to_le32(src->time_in_queue); |
| 762 | dst->msec = cpu_to_le64(src->msec); |
| 763 | } |
| 764 | |
| 765 | void fio_server_send_du(void) |
| 766 | { |
| 767 | struct disk_util *du; |
| 768 | struct flist_head *entry; |
| 769 | struct cmd_du_pdu pdu; |
| 770 | |
| 771 | dprint(FD_NET, "server: sending disk_util %d\n", !flist_empty(&disk_list)); |
| 772 | |
Jens Axboe | 0766d92 | 2011-10-13 12:02:08 +0200 | [diff] [blame] | 773 | memset(&pdu, 0, sizeof(pdu)); |
| 774 | |
Jens Axboe | d09a64a | 2011-10-13 11:38:56 +0200 | [diff] [blame] | 775 | flist_for_each(entry, &disk_list) { |
| 776 | du = flist_entry(entry, struct disk_util, list); |
| 777 | |
| 778 | convert_dus(&pdu.dus, &du->dus); |
| 779 | convert_agg(&pdu.agg, &du->agg); |
| 780 | |
| 781 | fio_net_send_cmd(server_fd, FIO_NET_CMD_DU, &pdu, sizeof(pdu), 0); |
| 782 | } |
| 783 | } |
| 784 | |
Jens Axboe | 142575e | 2011-09-30 17:35:45 -0600 | [diff] [blame] | 785 | int fio_server_log(const char *format, ...) |
| 786 | { |
| 787 | char buffer[1024]; |
| 788 | va_list args; |
Jens Axboe | 82fa6b2 | 2011-09-30 22:29:08 -0600 | [diff] [blame] | 789 | size_t len; |
Jens Axboe | 142575e | 2011-09-30 17:35:45 -0600 | [diff] [blame] | 790 | |
Jens Axboe | 60efd14 | 2011-10-04 13:30:11 +0200 | [diff] [blame] | 791 | dprint(FD_NET, "server log\n"); |
| 792 | |
Jens Axboe | 142575e | 2011-09-30 17:35:45 -0600 | [diff] [blame] | 793 | va_start(args, format); |
Jens Axboe | 82fa6b2 | 2011-09-30 22:29:08 -0600 | [diff] [blame] | 794 | len = vsnprintf(buffer, sizeof(buffer), format, args); |
Jens Axboe | 142575e | 2011-09-30 17:35:45 -0600 | [diff] [blame] | 795 | va_end(args); |
| 796 | |
Jens Axboe | 82fa6b2 | 2011-09-30 22:29:08 -0600 | [diff] [blame] | 797 | return fio_server_text_output(buffer, len); |
Jens Axboe | 37db14f | 2011-09-30 17:00:42 -0600 | [diff] [blame] | 798 | } |
Jens Axboe | e46d809 | 2011-10-03 09:11:02 +0200 | [diff] [blame] | 799 | |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 800 | static int fio_init_server_ip(void) |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 801 | { |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 802 | int sk, opt; |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 803 | |
| 804 | sk = socket(AF_INET, SOCK_STREAM, 0); |
| 805 | if (sk < 0) { |
| 806 | log_err("fio: socket: %s\n", strerror(errno)); |
| 807 | return -1; |
| 808 | } |
| 809 | |
| 810 | opt = 1; |
| 811 | if (setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) { |
| 812 | log_err("fio: setsockopt: %s\n", strerror(errno)); |
Jens Axboe | b94cba4 | 2011-10-06 21:27:10 +0200 | [diff] [blame] | 813 | close(sk); |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 814 | return -1; |
| 815 | } |
| 816 | #ifdef SO_REUSEPORT |
Jens Axboe | 6eb2479 | 2011-10-04 15:00:30 +0200 | [diff] [blame] | 817 | if (setsockopt(sk, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) { |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 818 | log_err("fio: setsockopt: %s\n", strerror(errno)); |
Jens Axboe | b94cba4 | 2011-10-06 21:27:10 +0200 | [diff] [blame] | 819 | close(sk); |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 820 | return -1; |
| 821 | } |
| 822 | #endif |
| 823 | |
| 824 | saddr_in.sin_family = AF_INET; |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 825 | |
| 826 | if (bind(sk, (struct sockaddr *) &saddr_in, sizeof(saddr_in)) < 0) { |
| 827 | log_err("fio: bind: %s\n", strerror(errno)); |
Jens Axboe | b94cba4 | 2011-10-06 21:27:10 +0200 | [diff] [blame] | 828 | close(sk); |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 829 | return -1; |
| 830 | } |
| 831 | |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 832 | return sk; |
| 833 | } |
| 834 | |
| 835 | static int fio_init_server_sock(void) |
| 836 | { |
| 837 | struct sockaddr_un addr; |
| 838 | fio_socklen_t len; |
| 839 | mode_t mode; |
| 840 | int sk; |
| 841 | |
| 842 | sk = socket(AF_UNIX, SOCK_STREAM, 0); |
| 843 | if (sk < 0) { |
| 844 | log_err("fio: socket: %s\n", strerror(errno)); |
| 845 | return -1; |
| 846 | } |
| 847 | |
| 848 | mode = umask(000); |
| 849 | |
| 850 | memset(&addr, 0, sizeof(addr)); |
| 851 | addr.sun_family = AF_UNIX; |
| 852 | strcpy(addr.sun_path, bind_sock); |
| 853 | unlink(bind_sock); |
| 854 | |
| 855 | len = sizeof(addr.sun_family) + strlen(bind_sock) + 1; |
| 856 | |
| 857 | if (bind(sk, (struct sockaddr *) &addr, len) < 0) { |
| 858 | log_err("fio: bind: %s\n", strerror(errno)); |
Jens Axboe | b94cba4 | 2011-10-06 21:27:10 +0200 | [diff] [blame] | 859 | close(sk); |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 860 | return -1; |
| 861 | } |
| 862 | |
| 863 | umask(mode); |
| 864 | return sk; |
| 865 | } |
| 866 | |
| 867 | static int fio_init_server_connection(void) |
| 868 | { |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 869 | char bind_str[128]; |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 870 | int sk; |
| 871 | |
| 872 | dprint(FD_NET, "starting server\n"); |
| 873 | |
| 874 | if (!bind_sock) |
| 875 | sk = fio_init_server_ip(); |
| 876 | else |
| 877 | sk = fio_init_server_sock(); |
| 878 | |
| 879 | if (sk < 0) |
| 880 | return sk; |
| 881 | |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 882 | if (!bind_sock) |
| 883 | sprintf(bind_str, "%s:%u", inet_ntoa(saddr_in.sin_addr), fio_net_port); |
| 884 | else |
| 885 | strcpy(bind_str, bind_sock); |
| 886 | |
| 887 | log_info("fio: server listening on %s\n", bind_str); |
| 888 | |
Jens Axboe | 89c1707 | 2011-10-11 10:15:51 +0200 | [diff] [blame] | 889 | if (listen(sk, 0) < 0) { |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 890 | log_err("fio: listen: %s\n", strerror(errno)); |
| 891 | return -1; |
| 892 | } |
| 893 | |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 894 | return sk; |
| 895 | } |
| 896 | |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 897 | int fio_server_parse_string(const char *str, char **ptr, int *is_sock, |
| 898 | int *port, struct in_addr *inp) |
| 899 | { |
| 900 | *ptr = NULL; |
| 901 | *is_sock = 0; |
Jens Axboe | 6d2cf39 | 2011-10-07 13:19:28 +0200 | [diff] [blame] | 902 | *port = fio_net_port; |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 903 | |
| 904 | if (!strncmp(str, "sock:", 5)) { |
| 905 | *ptr = strdup(str + 5); |
| 906 | *is_sock = 1; |
| 907 | } else { |
| 908 | const char *host = str; |
| 909 | char *portp; |
| 910 | int lport = 0; |
| 911 | |
| 912 | /* |
| 913 | * Is it ip:<ip or host>:port |
| 914 | */ |
| 915 | if (!strncmp(host, "ip:", 3)) |
| 916 | host += 3; |
| 917 | else if (host[0] == ':') { |
| 918 | /* String is :port */ |
| 919 | host++; |
| 920 | lport = atoi(host); |
| 921 | if (!lport || lport > 65535) { |
| 922 | log_err("fio: bad server port %u\n", port); |
| 923 | return 1; |
| 924 | } |
| 925 | /* no hostname given, we are done */ |
| 926 | *port = lport; |
| 927 | return 0; |
| 928 | } |
| 929 | |
| 930 | /* |
| 931 | * If no port seen yet, check if there's a last ':' at the end |
| 932 | */ |
| 933 | if (!lport) { |
| 934 | portp = strchr(host, ':'); |
| 935 | if (portp) { |
| 936 | *portp = '\0'; |
| 937 | portp++; |
| 938 | lport = atoi(portp); |
| 939 | if (!lport || lport > 65535) { |
| 940 | log_err("fio: bad server port %u\n", port); |
| 941 | return 1; |
| 942 | } |
| 943 | } |
| 944 | } |
| 945 | |
| 946 | if (lport) |
| 947 | *port = lport; |
| 948 | |
| 949 | *ptr = strdup(host); |
| 950 | |
| 951 | if (inet_aton(host, inp) != 1) { |
| 952 | struct hostent *hent; |
| 953 | |
| 954 | hent = gethostbyname(host); |
| 955 | if (!hent) { |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 956 | free(*ptr); |
| 957 | *ptr = NULL; |
| 958 | return 1; |
| 959 | } |
| 960 | |
| 961 | memcpy(inp, hent->h_addr, 4); |
| 962 | } |
| 963 | } |
| 964 | |
| 965 | if (*port == 0) |
| 966 | *port = fio_net_port; |
| 967 | |
| 968 | return 0; |
| 969 | } |
| 970 | |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 971 | /* |
| 972 | * Server arg should be one of: |
| 973 | * |
| 974 | * sock:/path/to/socket |
| 975 | * ip:1.2.3.4 |
| 976 | * 1.2.3.4 |
| 977 | * |
| 978 | * Where sock uses unix domain sockets, and ip binds the server to |
| 979 | * a specific interface. If no arguments are given to the server, it |
| 980 | * uses IP and binds to 0.0.0.0. |
| 981 | * |
| 982 | */ |
| 983 | static int fio_handle_server_arg(void) |
| 984 | { |
Jens Axboe | 6d2cf39 | 2011-10-07 13:19:28 +0200 | [diff] [blame] | 985 | int port = fio_net_port; |
Jens Axboe | a7de0a1 | 2011-10-07 12:55:14 +0200 | [diff] [blame] | 986 | int is_sock, ret = 0; |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 987 | |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 988 | saddr_in.sin_addr.s_addr = htonl(INADDR_ANY); |
| 989 | |
| 990 | if (!fio_server_arg) |
Jens Axboe | a7de0a1 | 2011-10-07 12:55:14 +0200 | [diff] [blame] | 991 | goto out; |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 992 | |
Jens Axboe | 4e5b8fb | 2011-10-07 10:03:44 +0200 | [diff] [blame] | 993 | ret = fio_server_parse_string(fio_server_arg, &bind_sock, &is_sock, |
Jens Axboe | 6d2cf39 | 2011-10-07 13:19:28 +0200 | [diff] [blame] | 994 | &port, &saddr_in.sin_addr); |
Jens Axboe | 4e5b8fb | 2011-10-07 10:03:44 +0200 | [diff] [blame] | 995 | |
| 996 | if (!is_sock && bind_sock) { |
| 997 | free(bind_sock); |
| 998 | bind_sock = NULL; |
| 999 | } |
| 1000 | |
Jens Axboe | a7de0a1 | 2011-10-07 12:55:14 +0200 | [diff] [blame] | 1001 | out: |
Jens Axboe | 6d2cf39 | 2011-10-07 13:19:28 +0200 | [diff] [blame] | 1002 | fio_net_port = port; |
| 1003 | saddr_in.sin_port = htons(port); |
Jens Axboe | 4e5b8fb | 2011-10-07 10:03:44 +0200 | [diff] [blame] | 1004 | return ret; |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 1005 | } |
| 1006 | |
| 1007 | static int fio_server(void) |
| 1008 | { |
| 1009 | int sk, ret; |
| 1010 | |
| 1011 | dprint(FD_NET, "starting server\n"); |
| 1012 | |
| 1013 | if (fio_handle_server_arg()) |
| 1014 | return -1; |
| 1015 | |
| 1016 | sk = fio_init_server_connection(); |
| 1017 | if (sk < 0) |
| 1018 | return -1; |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 1019 | |
| 1020 | ret = accept_loop(sk); |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 1021 | |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 1022 | close(sk); |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 1023 | |
| 1024 | if (fio_server_arg) { |
| 1025 | free(fio_server_arg); |
| 1026 | fio_server_arg = NULL; |
| 1027 | } |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 1028 | if (bind_sock) |
| 1029 | free(bind_sock); |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 1030 | |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 1031 | return ret; |
| 1032 | } |
| 1033 | |
Jens Axboe | 7b82168 | 2011-10-11 12:16:32 +0200 | [diff] [blame] | 1034 | void fio_server_got_signal(int signal) |
Jens Axboe | 9abea48 | 2011-10-04 13:21:54 +0200 | [diff] [blame] | 1035 | { |
Jens Axboe | 7b82168 | 2011-10-11 12:16:32 +0200 | [diff] [blame] | 1036 | if (signal == SIGPIPE) |
| 1037 | server_fd = -1; |
| 1038 | else { |
| 1039 | log_info("\nfio: terminating on signal %d\n", signal); |
| 1040 | exit_backend = 1; |
| 1041 | } |
Jens Axboe | 9abea48 | 2011-10-04 13:21:54 +0200 | [diff] [blame] | 1042 | } |
| 1043 | |
Jens Axboe | 13755d9 | 2011-10-10 19:51:26 +0200 | [diff] [blame] | 1044 | static int check_existing_pidfile(const char *pidfile) |
| 1045 | { |
| 1046 | struct stat sb; |
| 1047 | char buf[16]; |
| 1048 | pid_t pid; |
| 1049 | FILE *f; |
| 1050 | |
| 1051 | if (stat(pidfile, &sb)) |
| 1052 | return 0; |
| 1053 | |
| 1054 | f = fopen(pidfile, "r"); |
| 1055 | if (!f) |
| 1056 | return 0; |
| 1057 | |
Jens Axboe | bfc3b17 | 2011-10-10 21:16:55 +0200 | [diff] [blame] | 1058 | if (fread(buf, sb.st_size, 1, f) <= 0) { |
Jens Axboe | 13755d9 | 2011-10-10 19:51:26 +0200 | [diff] [blame] | 1059 | fclose(f); |
| 1060 | return 1; |
| 1061 | } |
| 1062 | fclose(f); |
| 1063 | |
| 1064 | pid = atoi(buf); |
| 1065 | if (kill(pid, SIGCONT) < 0) |
Jens Axboe | ea5aa1b | 2011-10-11 11:45:35 +0200 | [diff] [blame] | 1066 | return errno != ESRCH; |
Jens Axboe | 13755d9 | 2011-10-10 19:51:26 +0200 | [diff] [blame] | 1067 | |
| 1068 | return 1; |
| 1069 | } |
| 1070 | |
| 1071 | static int write_pid(pid_t pid, const char *pidfile) |
Jens Axboe | 402668f | 2011-10-10 15:28:58 +0200 | [diff] [blame] | 1072 | { |
| 1073 | FILE *fpid; |
| 1074 | |
| 1075 | fpid = fopen(pidfile, "w"); |
| 1076 | if (!fpid) { |
| 1077 | log_err("fio: failed opening pid file %s\n", pidfile); |
Jens Axboe | 13755d9 | 2011-10-10 19:51:26 +0200 | [diff] [blame] | 1078 | return 1; |
Jens Axboe | 402668f | 2011-10-10 15:28:58 +0200 | [diff] [blame] | 1079 | } |
| 1080 | |
| 1081 | fprintf(fpid, "%u\n", (unsigned int) pid); |
Jens Axboe | 13755d9 | 2011-10-10 19:51:26 +0200 | [diff] [blame] | 1082 | fclose(fpid); |
| 1083 | return 0; |
Jens Axboe | 402668f | 2011-10-10 15:28:58 +0200 | [diff] [blame] | 1084 | } |
| 1085 | |
| 1086 | /* |
| 1087 | * If pidfile is specified, background us. |
| 1088 | */ |
| 1089 | int fio_start_server(char *pidfile) |
Jens Axboe | e46d809 | 2011-10-03 09:11:02 +0200 | [diff] [blame] | 1090 | { |
| 1091 | pid_t pid; |
Jens Axboe | 402668f | 2011-10-10 15:28:58 +0200 | [diff] [blame] | 1092 | int ret; |
Jens Axboe | e46d809 | 2011-10-03 09:11:02 +0200 | [diff] [blame] | 1093 | |
Jens Axboe | 402668f | 2011-10-10 15:28:58 +0200 | [diff] [blame] | 1094 | if (!pidfile) |
Jens Axboe | e46d809 | 2011-10-03 09:11:02 +0200 | [diff] [blame] | 1095 | return fio_server(); |
| 1096 | |
Jens Axboe | 13755d9 | 2011-10-10 19:51:26 +0200 | [diff] [blame] | 1097 | if (check_existing_pidfile(pidfile)) { |
| 1098 | log_err("fio: pidfile %s exists and server appears alive\n", |
| 1099 | pidfile); |
| 1100 | return -1; |
| 1101 | } |
| 1102 | |
Jens Axboe | e46d809 | 2011-10-03 09:11:02 +0200 | [diff] [blame] | 1103 | pid = fork(); |
| 1104 | if (pid < 0) { |
Jens Axboe | 13755d9 | 2011-10-10 19:51:26 +0200 | [diff] [blame] | 1105 | log_err("fio: failed server fork: %s", strerror(errno)); |
Jens Axboe | 402668f | 2011-10-10 15:28:58 +0200 | [diff] [blame] | 1106 | free(pidfile); |
Jens Axboe | c28e8e8 | 2011-10-04 08:57:39 +0200 | [diff] [blame] | 1107 | return -1; |
Jens Axboe | 402668f | 2011-10-10 15:28:58 +0200 | [diff] [blame] | 1108 | } else if (pid) { |
Jens Axboe | 13755d9 | 2011-10-10 19:51:26 +0200 | [diff] [blame] | 1109 | int ret = write_pid(pid, pidfile); |
| 1110 | |
| 1111 | exit(ret); |
Jens Axboe | 402668f | 2011-10-10 15:28:58 +0200 | [diff] [blame] | 1112 | } |
Jens Axboe | e46d809 | 2011-10-03 09:11:02 +0200 | [diff] [blame] | 1113 | |
| 1114 | setsid(); |
Jens Axboe | 13755d9 | 2011-10-10 19:51:26 +0200 | [diff] [blame] | 1115 | openlog("fio", LOG_NDELAY|LOG_NOWAIT|LOG_PID, LOG_USER); |
| 1116 | log_syslog = 1; |
Jens Axboe | e46d809 | 2011-10-03 09:11:02 +0200 | [diff] [blame] | 1117 | close(STDIN_FILENO); |
| 1118 | close(STDOUT_FILENO); |
| 1119 | close(STDERR_FILENO); |
| 1120 | f_out = NULL; |
| 1121 | f_err = NULL; |
Jens Axboe | 402668f | 2011-10-10 15:28:58 +0200 | [diff] [blame] | 1122 | |
| 1123 | ret = fio_server(); |
| 1124 | |
| 1125 | closelog(); |
| 1126 | unlink(pidfile); |
| 1127 | free(pidfile); |
| 1128 | return ret; |
Jens Axboe | e46d809 | 2011-10-03 09:11:02 +0200 | [diff] [blame] | 1129 | } |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 1130 | |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 1131 | void fio_server_set_arg(const char *arg) |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 1132 | { |
| 1133 | fio_server_arg = strdup(arg); |
| 1134 | } |