Separate the act of adding and starting a job
[fio.git] / server.c
index 5e94696eb6aa6ee79d34668723b3981f1384d4dd..10851805a42bb87b95f35aa598fe398d76075ac1 100644 (file)
--- a/server.c
+++ b/server.c
@@ -24,7 +24,7 @@
 
 #include "fio_version.h"
 
-int fio_net_port = 8765;
+int fio_net_port = FIO_NET_PORT;
 
 int exit_backend = 0;
 
@@ -51,7 +51,9 @@ static const char *fio_server_ops[FIO_NET_CMD_NR] = {
        "START",
        "STOP",
        "DISK_UTIL",
-       "RUN",
+       "SERVER_START",
+       "ADD_JOB",
+       "CMD_RUN"
 };
 
 const char *fio_server_op(unsigned int op)
@@ -67,7 +69,7 @@ const char *fio_server_op(unsigned int op)
 
 int fio_send_data(int sk, const void *p, unsigned int len)
 {
-       assert(len <= sizeof(struct fio_net_cmd) + FIO_SERVER_MAX_PDU);
+       assert(len <= sizeof(struct fio_net_cmd) + FIO_SERVER_MAX_FRAGMENT_PDU);
 
        do {
                int ret = send(sk, p, len, 0);
@@ -145,7 +147,7 @@ static int verify_convert_cmd(struct fio_net_cmd *cmd)
                return 1;
        }
 
-       if (cmd->pdu_len > FIO_SERVER_MAX_PDU) {
+       if (cmd->pdu_len > FIO_SERVER_MAX_FRAGMENT_PDU) {
                log_err("fio: command payload too large: %u\n", cmd->pdu_len);
                return 1;
        }
@@ -221,12 +223,19 @@ struct fio_net_cmd *fio_net_recv_cmd(int sk)
                cmdret = NULL;
        } else if (cmdret) {
                /* zero-terminate text input */
-               if (cmdret->pdu_len && (cmdret->opcode == FIO_NET_CMD_TEXT ||
-                   cmdret->opcode == FIO_NET_CMD_JOB)) {
-                       char *buf = (char *) cmdret->payload;
+               if (cmdret->pdu_len) {
+                       if (cmdret->opcode == FIO_NET_CMD_TEXT) {
+                               struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmdret->payload;
+                               char *buf = (char *) pdu->buf;
 
-                       buf[cmdret->pdu_len ] = '\0';
+                               buf[pdu->buf_len ] = '\0';
+                       } else if (cmdret->opcode == FIO_NET_CMD_JOB) {
+                               char *buf = (char *) cmdret->payload;
+
+                               buf[cmdret->pdu_len ] = '\0';
+                       }
                }
+
                /* frag flag is internal */
                cmdret->flags &= ~FIO_NET_CMD_F_MORE;
        }
@@ -254,8 +263,8 @@ int fio_net_send_cmd(int fd, uint16_t opcode, const void *buf, off_t size,
 
        do {
                this_len = size;
-               if (this_len > FIO_SERVER_MAX_PDU)
-                       this_len = FIO_SERVER_MAX_PDU;
+               if (this_len > FIO_SERVER_MAX_FRAGMENT_PDU)
+                       this_len = FIO_SERVER_MAX_FRAGMENT_PDU;
 
                if (!cmd || cur_len < sizeof(*cmd) + this_len) {
                        if (cmd)
@@ -331,21 +340,11 @@ static int fio_server_send_quit_cmd(void)
        return fio_net_send_simple_cmd(server_fd, FIO_NET_CMD_QUIT, 0, NULL);
 }
 
-static int handle_job_cmd(struct fio_net_cmd *cmd)
+static int handle_run_cmd(struct fio_net_cmd *cmd)
 {
-       char *buf = (char *) cmd->payload;
-       struct cmd_start_pdu spdu;
        struct cmd_end_pdu epdu;
        int ret;
 
-       if (parse_jobs_ini(buf, 1, 0)) {
-               fio_server_send_quit_cmd();
-               return -1;
-       }
-
-       spdu.jobs = cpu_to_le32(thread_number);
-       fio_net_send_cmd(server_fd, FIO_NET_CMD_START, &spdu, sizeof(spdu), 0);
-
        ret = fio_backend();
 
        epdu.error = ret;
@@ -353,17 +352,34 @@ static int handle_job_cmd(struct fio_net_cmd *cmd)
 
        fio_server_send_quit_cmd();
        reset_fio_state();
+       first_cmd_check = 0;
        return ret;
 }
 
+static int handle_job_cmd(struct fio_net_cmd *cmd)
+{
+       char *buf = (char *) cmd->payload;
+       struct cmd_start_pdu spdu;
+
+       if (parse_jobs_ini(buf, 1, 0)) {
+               fio_server_send_quit_cmd();
+               return -1;
+       }
+
+       spdu.jobs = cpu_to_le32(thread_number);
+       fio_net_send_cmd(server_fd, FIO_NET_CMD_START, &spdu, sizeof(spdu), 0);
+       return 0;
+}
+
 static int handle_jobline_cmd(struct fio_net_cmd *cmd)
 {
        void *pdu = cmd->payload;
        struct cmd_single_line_pdu *cslp;
        struct cmd_line_pdu *clp;
        unsigned long offset;
+       struct cmd_start_pdu spdu;
        char **argv;
-       int ret, i;
+       int i;
 
        clp = pdu;
        clp->lines = le16_to_cpu(clp->lines);
@@ -388,12 +404,9 @@ static int handle_jobline_cmd(struct fio_net_cmd *cmd)
 
        free(argv);
 
-       fio_net_send_simple_cmd(server_fd, FIO_NET_CMD_START, 0, NULL);
-
-       ret = fio_backend();
-       fio_server_send_quit_cmd();
-       reset_fio_state();
-       return ret;
+       spdu.jobs = cpu_to_le32(thread_number);
+       fio_net_send_cmd(server_fd, FIO_NET_CMD_START, &spdu, sizeof(spdu), 0);
+       return 0;
 }
 
 static int handle_probe_cmd(struct fio_net_cmd *cmd)
@@ -443,18 +456,19 @@ static int handle_send_eta_cmd(struct fio_net_cmd *cmd)
        je->nr_ramp             = cpu_to_le32(je->nr_ramp);
        je->nr_pending          = cpu_to_le32(je->nr_pending);
        je->files_open          = cpu_to_le32(je->files_open);
-       je->m_rate              = cpu_to_le32(je->m_rate);
-       je->t_rate              = cpu_to_le32(je->t_rate);
-       je->m_iops              = cpu_to_le32(je->m_iops);
-       je->t_iops              = cpu_to_le32(je->t_iops);
 
        for (i = 0; i < 2; i++) {
+               je->m_rate[i]   = cpu_to_le32(je->m_rate[i]);
+               je->t_rate[i]   = cpu_to_le32(je->t_rate[i]);
+               je->m_iops[i]   = cpu_to_le32(je->m_iops[i]);
+               je->t_iops[i]   = cpu_to_le32(je->t_iops[i]);
                je->rate[i]     = cpu_to_le32(je->rate[i]);
                je->iops[i]     = cpu_to_le32(je->iops[i]);
        }
 
-       je->elapsed_sec         = cpu_to_le32(je->nr_running);
+       je->elapsed_sec         = cpu_to_le64(je->elapsed_sec);
        je->eta_sec             = cpu_to_le64(je->eta_sec);
+       je->nr_threads          = cpu_to_le32(je->nr_threads);
 
        fio_net_send_cmd(server_fd, FIO_NET_CMD_ETA, je, size, cmd->tag);
        free(je);
@@ -487,6 +501,9 @@ static int handle_command(struct fio_net_cmd *cmd)
        case FIO_NET_CMD_SEND_ETA:
                ret = handle_send_eta_cmd(cmd);
                break;
+       case FIO_NET_CMD_RUN:
+               ret = handle_run_cmd(cmd);
+               break;
        default:
                log_err("fio: unknown opcode: %s\n",fio_server_op(cmd->opcode));
                ret = 1;
@@ -554,8 +571,10 @@ static int handle_connection(int sk, int block)
 
 void fio_server_idle_loop(void)
 {
-       if (!first_cmd_check)
-               fio_net_send_simple_cmd(server_fd, FIO_NET_CMD_RUN, 0, NULL);
+       if (!first_cmd_check) {
+               fio_net_send_simple_cmd(server_fd, FIO_NET_CMD_SERVER_START, 0, NULL);
+               first_cmd_check = 1;
+       }
        if (server_fd != -1)
                handle_connection(server_fd, 0);
 }
@@ -614,12 +633,30 @@ out:
        return exitval;
 }
 
-int fio_server_text_output(const char *buf, size_t len)
+int fio_server_text_output(int level, const char *buf, size_t len)
 {
-       if (server_fd != -1)
-               return fio_net_send_cmd(server_fd, FIO_NET_CMD_TEXT, buf, len, 0);
+       struct cmd_text_pdu *pdu;
+       unsigned int tlen;
+       struct timeval tv;
 
-       return log_local_buf(buf, len);
+       if (server_fd == -1)
+               return log_local_buf(buf, len);
+
+       tlen = sizeof(*pdu) + len;
+       pdu = malloc(tlen);
+
+       pdu->level      = __cpu_to_le32(level);
+       pdu->buf_len    = __cpu_to_le32(len);
+
+       gettimeofday(&tv, NULL);
+       pdu->log_sec    = __cpu_to_le64(tv.tv_sec);
+       pdu->log_usec   = __cpu_to_le64(tv.tv_usec);
+
+       memcpy(pdu->buf, buf, len);
+
+       fio_net_send_cmd(server_fd, FIO_NET_CMD_TEXT, pdu, tlen, 0);
+       free(pdu);
+       return len;
 }
 
 static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
@@ -799,19 +836,13 @@ void fio_server_send_du(void)
        }
 }
 
-int fio_server_log(const char *format, ...)
+void fio_server_send_add_job(struct thread_options *o, const char *ioengine)
 {
-       char buffer[1024];
-       va_list args;
-       size_t len;
-
-       dprint(FD_NET, "server log\n");
+       struct cmd_add_job_pdu pdu;
 
-       va_start(args, format);
-       len = vsnprintf(buffer, sizeof(buffer), format, args);
-       va_end(args);
+       convert_thread_options_to_net(&pdu.top, o);
 
-       return fio_server_text_output(buffer, len);
+       fio_net_send_cmd(server_fd, FIO_NET_CMD_ADD_JOB, &pdu, sizeof(pdu), 0);
 }
 
 static int fio_init_server_ip(void)
@@ -943,6 +974,46 @@ static int fio_init_server_connection(void)
        return sk;
 }
 
+int fio_server_parse_host(const char *host, int *ipv6, struct in_addr *inp,
+                         struct in6_addr *inp6)
+
+{
+       int ret = 0;
+
+       if (*ipv6)
+               ret = inet_pton(AF_INET6, host, inp6);
+       else
+               ret = inet_pton(AF_INET, host, inp);
+
+       if (ret != 1) {
+               struct hostent *hent;
+
+               hent = gethostbyname(host);
+               if (!hent) {
+                       log_err("fio: failed to resolve <%s>\n", host);
+                       return 0;
+               }
+
+               if (*ipv6) {
+                       if (hent->h_addrtype != AF_INET6) {
+                               log_info("fio: falling back to IPv4\n");
+                               *ipv6 = 0;
+                       } else
+                               memcpy(inp6, hent->h_addr_list[0], 16);
+               }
+               if (!*ipv6) {
+                       if (hent->h_addrtype != AF_INET) {
+                               log_err("fio: lookup type mismatch\n");
+                               return 0;
+                       }
+                       memcpy(inp, hent->h_addr_list[0], 4);
+               }
+               ret = 1;
+       }
+
+       return !(ret == 1);
+}
+
 /*
  * Parse a host/ip/port string. Reads from 'str'.
  *
@@ -961,7 +1032,7 @@ int fio_server_parse_string(const char *str, char **ptr, int *is_sock,
 {
        const char *host = str;
        char *portp;
-       int ret, lport = 0;
+       int lport = 0;
 
        *ptr = NULL;
        *is_sock = 0;
@@ -1022,38 +1093,10 @@ int fio_server_parse_string(const char *str, char **ptr, int *is_sock,
 
        *ptr = strdup(host);
 
-       if (*ipv6)
-               ret = inet_pton(AF_INET6, host, inp6);
-       else
-               ret = inet_pton(AF_INET, host, inp);
-
-       if (ret != 1) {
-               struct hostent *hent;
-
-               hent = gethostbyname(host);
-               if (!hent) {
-                       log_err("fio: failed to resolve <%s>\n", host);
-                       free(*ptr);
-                       *ptr = NULL;
-                       return 1;
-               }
-
-               if (*ipv6) {
-                       if (hent->h_addrtype != AF_INET6) {
-                               log_info("fio: falling back to IPv4\n");
-                               *ipv6 = 0;
-                       } else
-                               memcpy(inp6, hent->h_addr_list[0], 16);
-               }
-               if (!*ipv6) {
-                       if (hent->h_addrtype != AF_INET) {
-                               log_err("fio: lookup type mismatch\n");
-                               free(*ptr);
-                               *ptr = NULL;
-                               return 1;
-                       }
-                       memcpy(inp, hent->h_addr_list[0], 4);
-               }
+       if (fio_server_parse_host(*ptr, ipv6, inp, inp6)) {
+               free(*ptr);
+               *ptr = NULL;
+               return 1;
        }
 
        if (*port == 0)