Add bloom filter
[fio.git] / server.c
index ab3bece1f61f47e3f7e8dd704a5acf64ac50223d..36713ee5716058fddf49f7213fe6d444b26b7c53 100644 (file)
--- a/server.c
+++ b/server.c
@@ -4,7 +4,6 @@
 #include <unistd.h>
 #include <limits.h>
 #include <errno.h>
-#include <fcntl.h>
 #include <sys/poll.h>
 #include <sys/types.h>
 #include <sys/wait.h>
@@ -209,7 +208,7 @@ static int verify_convert_cmd(struct fio_net_cmd *cmd)
  */
 struct fio_net_cmd *fio_net_recv_cmd(int sk)
 {
-       struct fio_net_cmd cmd, *cmdret = NULL;
+       struct fio_net_cmd cmd, *tmp, *cmdret = NULL;
        size_t cmd_size = 0, pdu_offset = 0;
        uint16_t crc;
        int ret, first = 1;
@@ -232,7 +231,19 @@ struct fio_net_cmd *fio_net_recv_cmd(int sk)
                } else
                        cmd_size += cmd.pdu_len;
 
-               cmdret = realloc(cmdret, cmd_size);
+               if (cmd_size / 1024 > FIO_SERVER_MAX_CMD_MB * 1024) {
+                       log_err("fio: cmd+pdu too large (%llu)\n", (unsigned long long) cmd_size);
+                       ret = 1;
+                       break;
+               }
+
+               tmp = realloc(cmdret, cmd_size);
+               if (!tmp) {
+                       log_err("fio: server failed allocating cmd\n");
+                       ret = 1;
+                       break;
+               }
+               cmdret = tmp;
 
                if (first)
                        memcpy(cmdret, &cmd, sizeof(cmd));
@@ -544,6 +555,7 @@ static int handle_run_cmd(struct flist_head *job_list, struct fio_net_cmd *cmd)
        pid_t pid;
        int ret;
 
+       fio_time_init();
        set_genesis_time();
 
        pid = fork();
@@ -654,22 +666,14 @@ static int handle_probe_cmd(struct fio_net_cmd *cmd)
 static int handle_send_eta_cmd(struct fio_net_cmd *cmd)
 {
        struct jobs_eta *je;
-       size_t size;
        uint64_t tag = cmd->tag;
+       size_t size;
        int i;
 
-       if (!thread_number)
+       je = get_jobs_eta(1, &size);
+       if (!je)
                return 0;
 
-       size = sizeof(*je) + thread_number * sizeof(char) + 1;
-       je = malloc(size);
-       memset(je, 0, size);
-
-       if (!calc_thread_status(je, 1)) {
-               free(je);
-               return 0;
-       }
-
        dprint(FD_NET, "server sending status\n");
 
        je->nr_running          = cpu_to_le32(je->nr_running);
@@ -843,14 +847,12 @@ static int accept_loop(int listen_sk)
        struct sockaddr_in6 addr6;
        socklen_t len = use_ipv6 ? sizeof(addr6) : sizeof(addr);
        struct pollfd pfd;
-       int ret = 0, sk, flags, exitval = 0;
+       int ret = 0, sk, exitval = 0;
        FLIST_HEAD(conn_list);
 
        dprint(FD_NET, "server enter accept loop\n");
 
-       flags = fcntl(listen_sk, F_GETFL);
-       flags |= O_NONBLOCK;
-       fcntl(listen_sk, F_SETFL, flags);
+       fio_set_fd_nonblocking(listen_sk, "server");
 
        while (!exit_backend) {
                const char *from;
@@ -1175,7 +1177,7 @@ static int fio_send_iolog_gz(struct cmd_iolog_pdu *pdu, struct io_log *log)
        }
 
        stream.next_in = (void *) log->log;
-       stream.avail_in = log->nr_samples * sizeof(struct io_sample);
+       stream.avail_in = log->nr_samples * log_entry_sz(log);
 
        do {
                unsigned int this_len, flags = 0;
@@ -1212,19 +1214,27 @@ int fio_send_iolog(struct thread_data *td, struct io_log *log, const char *name)
        struct cmd_iolog_pdu pdu;
        int i, ret = 0;
 
+       pdu.nr_samples = cpu_to_le64(log->nr_samples);
        pdu.thread_number = cpu_to_le32(td->thread_number);
-       pdu.nr_samples = __cpu_to_le32(log->nr_samples);
        pdu.log_type = cpu_to_le32(log->log_type);
        pdu.compressed = cpu_to_le32(use_zlib);
-       strcpy((char *) pdu.name, name);
+
+       strncpy((char *) pdu.name, name, FIO_NET_NAME_MAX);
+       pdu.name[FIO_NET_NAME_MAX - 1] = '\0';
 
        for (i = 0; i < log->nr_samples; i++) {
-               struct io_sample *s = &log->log[i];
+               struct io_sample *s = get_sample(log, i);
+
+               s->time         = cpu_to_le64(s->time);
+               s->val          = cpu_to_le64(s->val);
+               s->__ddir       = cpu_to_le32(s->__ddir);
+               s->bs           = cpu_to_le32(s->bs);
 
-               s->time = cpu_to_le64(s->time);
-               s->val  = cpu_to_le64(s->val);
-               s->ddir = cpu_to_le32(s->ddir);
-               s->bs   = cpu_to_le32(s->bs);
+               if (log->log_offset) {
+                       struct io_sample_offset *so = (void *) s;
+
+                       so->offset = cpu_to_le64(so->offset);
+               }
        }
 
        /*
@@ -1242,7 +1252,7 @@ int fio_send_iolog(struct thread_data *td, struct io_log *log, const char *name)
                return fio_send_iolog_gz(&pdu, log);
 
        return fio_send_cmd_ext_pdu(server_fd, FIO_NET_CMD_IOLOG, log->log,
-                       log->nr_samples * sizeof(struct io_sample), 0, 0);
+                       log->nr_samples * log_entry_sz(log), 0, 0);
 }
 
 void fio_server_send_add_job(struct thread_data *td)
@@ -1699,7 +1709,7 @@ int fio_start_server(char *pidfile)
                int ret = write_pid(pid, pidfile);
 
                free(pidfile);
-               exit(ret);
+               _exit(ret);
        }
 
        setsid();