client/server: few select speedups
authorJens Axboe <axboe@kernel.dk>
Mon, 10 Oct 2011 07:55:21 +0000 (09:55 +0200)
committerJens Axboe <axboe@kernel.dk>
Mon, 10 Oct 2011 07:55:21 +0000 (09:55 +0200)
Don't alloc/free when we can reuse, and don't calculate string
lengths twice.

Signed-off-by: Jens Axboe <axboe@kernel.dk>
client.c
server.c

index 713194f9284e4c86a06738a7e99e35eefbce6301..66ef38ca1e93dd761975b5773adc215cae4efa57 100644 (file)
--- a/client.c
+++ b/client.c
@@ -295,17 +295,22 @@ static int send_client_cmd_line(struct fio_client *client)
        struct cmd_single_line_pdu *cslp;
        struct cmd_line_pdu *clp;
        unsigned long offset;
+       unsigned int *lens;
        void *pdu;
        size_t mem;
        int i, ret;
 
        dprint(FD_NET, "client: send cmdline %d\n", client->argc);
 
+       lens = malloc(client->argc * sizeof(unsigned int));
+
        /*
         * Find out how much mem we need
         */
-       for (i = 0, mem = 0; i < client->argc; i++)
-               mem += strlen(client->argv[i]) + 1;
+       for (i = 0, mem = 0; i < client->argc; i++) {
+               lens[i] = strlen(client->argv[i]) + 1;
+               mem += lens[i];
+       }
 
        /*
         * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu
@@ -317,7 +322,7 @@ static int send_client_cmd_line(struct fio_client *client)
        offset = sizeof(*clp);
 
        for (i = 0; i < client->argc; i++) {
-               uint16_t arg_len = strlen(client->argv[i]) + 1;
+               uint16_t arg_len = lens[i];
 
                cslp = pdu + offset;
                strcpy((char *) cslp->text, client->argv[i]);
@@ -325,6 +330,7 @@ static int send_client_cmd_line(struct fio_client *client)
                offset += sizeof(*cslp) + arg_len;
        }
 
+       free(lens);
        clp->lines = cpu_to_le16(client->argc);
        ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, 0);
        free(pdu);
index 5afd8d455f72c3178f9b9560719bd8e982cfca31..cf3a48ef25058288f698cab03471d5810a91ab71 100644 (file)
--- a/server.c
+++ b/server.c
@@ -208,8 +208,8 @@ void fio_net_cmd_crc(struct fio_net_cmd *cmd)
 int fio_net_send_cmd(int fd, uint16_t opcode, const void *buf, off_t size,
                     uint64_t tag)
 {
-       struct fio_net_cmd *cmd;
-       size_t this_len;
+       struct fio_net_cmd *cmd = NULL;
+       size_t this_len, cur_len = 0;
        int ret;
 
        do {
@@ -217,7 +217,13 @@ int fio_net_send_cmd(int fd, uint16_t opcode, const void *buf, off_t size,
                if (this_len > FIO_SERVER_MAX_PDU)
                        this_len = FIO_SERVER_MAX_PDU;
 
-               cmd = malloc(sizeof(*cmd) + this_len);
+               if (!cmd || cur_len < sizeof(*cmd) + this_len) {
+                       if (cmd)
+                               free(cmd);
+
+                       cur_len = sizeof(*cmd) + this_len;
+                       cmd = malloc(cur_len);
+               }
 
                fio_init_net_cmd(cmd, opcode, buf, this_len, tag);
 
@@ -227,11 +233,13 @@ int fio_net_send_cmd(int fd, uint16_t opcode, const void *buf, off_t size,
                fio_net_cmd_crc(cmd);
 
                ret = fio_send_data(fd, cmd, sizeof(*cmd) + this_len);
-               free(cmd);
                size -= this_len;
                buf += this_len;
        } while (!ret && size);
 
+       if (cmd)
+               free(cmd);
+
        return ret;
 }
 
@@ -332,13 +340,11 @@ static int handle_send_eta_cmd(struct fio_net_cmd *cmd)
 {
        struct jobs_eta *je;
        size_t size;
-       void *buf;
        int i;
 
        size = sizeof(*je) + thread_number * sizeof(char);
-       buf = malloc(size);
-       memset(buf, 0, size);
-       je = buf;
+       je = malloc(size);
+       memset(je, 0, size);
 
        if (!calc_thread_status(je, 1)) {
                free(je);
@@ -364,7 +370,7 @@ static int handle_send_eta_cmd(struct fio_net_cmd *cmd)
        je->elapsed_sec         = cpu_to_le32(je->nr_running);
        je->eta_sec             = cpu_to_le64(je->eta_sec);
 
-       fio_net_send_cmd(server_fd, FIO_NET_CMD_ETA, buf, size, cmd->tag);
+       fio_net_send_cmd(server_fd, FIO_NET_CMD_ETA, je, size, cmd->tag);
        free(je);
        return 0;
 }
@@ -373,7 +379,7 @@ static int handle_command(struct fio_net_cmd *cmd)
 {
        int ret;
 
-       dprint(FD_NET, "server: got opcode %d\n", cmd->opcode);
+       dprint(FD_NET, "server: got opcode %d, pdu=%u\n", cmd->opcode, cmd->pdu_len);
 
        switch (cmd->opcode) {
        case FIO_NET_CMD_QUIT: