Add protocol support for an arbitrary number of command line arguments
authorJens Axboe <axboe@kernel.dk>
Sat, 8 Oct 2011 19:07:29 +0000 (21:07 +0200)
committerJens Axboe <axboe@kernel.dk>
Sat, 8 Oct 2011 19:07:29 +0000 (21:07 +0200)
Make it more efficient as well, don't pass a lot of potentially
padded space, pass only the exact amount required.

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

index 8f8dc90acc53eceef00eb4e89ceb5407d5f9f301..874f0aecd3b80ccd1b8bed646be783410a4e99e0 100644 (file)
--- a/client.c
+++ b/client.c
@@ -111,32 +111,25 @@ static void remove_client(struct fio_client *client)
        nr_clients--;
 }
 
        nr_clients--;
 }
 
-static int __fio_client_add_cmd_option(struct fio_client *client,
-                                      const char *opt)
+static void __fio_client_add_cmd_option(struct fio_client *client,
+                                       const char *opt)
 {
        int index;
 
 {
        int index;
 
-       if (client->argc == FIO_NET_CMD_JOBLINE_ARGV) {
-               log_err("fio: max cmd line number reached.\n");
-               log_err("fio: cmd line <%s> has been ignored.\n", opt);
-               return 1;
-       }
-
        index = client->argc++;
        client->argv = realloc(client->argv, sizeof(char *) * client->argc);
        client->argv[index] = strdup(opt);
        dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
        index = client->argc++;
        client->argv = realloc(client->argv, sizeof(char *) * client->argc);
        client->argv[index] = strdup(opt);
        dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
-       return 0;
 }
 
 }
 
-int fio_client_add_cmd_option(void *cookie, const char *opt)
+void fio_client_add_cmd_option(void *cookie, const char *opt)
 {
        struct fio_client *client = cookie;
 
        if (!client || !opt)
 {
        struct fio_client *client = cookie;
 
        if (!client || !opt)
-               return 0;
+               return;
 
 
-       return __fio_client_add_cmd_option(client, opt);
+       __fio_client_add_cmd_option(client, opt);
 }
 
 int fio_client_add(const char *hostname, void **cookie)
 }
 
 int fio_client_add(const char *hostname, void **cookie)
@@ -280,17 +273,41 @@ static void probe_client(struct fio_client *client)
 
 static int send_client_cmd_line(struct fio_client *client)
 {
 
 static int send_client_cmd_line(struct fio_client *client)
 {
-       struct cmd_line_pdu *pdu;
+       struct cmd_single_line_pdu *cslp;
+       struct cmd_line_pdu *clp;
+       unsigned long offset;
+       void *pdu;
+       size_t mem;
        int i, ret;
 
        dprint(FD_NET, "client: send cmdline %d\n", client->argc);
 
        int i, ret;
 
        dprint(FD_NET, "client: send cmdline %d\n", client->argc);
 
-       pdu = malloc(sizeof(*pdu));
-       for (i = 0; i < client->argc; i++)
-               strcpy((char *) pdu->argv[i], client->argv[i]);
+       /*
+        * Find out how much mem we need
+        */
+       for (i = 0, mem = 0; i < client->argc; i++)
+               mem += strlen(client->argv[i]) + 1;
+
+       /*
+        * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu
+        */
+       mem += sizeof(*clp) + (client->argc * sizeof(*cslp));
+
+       pdu = malloc(mem);
+       clp = pdu;
+       offset = sizeof(*clp);
+
+       for (i = 0; i < client->argc; i++) {
+               uint16_t arg_len = strlen(client->argv[i]) + 1;
+
+               cslp = pdu + offset;
+               strcpy((char *) cslp->text, client->argv[i]);
+               cslp->len = cpu_to_le16(arg_len);
+               offset += sizeof(*cslp) + arg_len;
+       }
 
 
-       pdu->argc = cpu_to_le16(client->argc);
-       ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, sizeof(*pdu));
+       clp->lines = cpu_to_le16(client->argc);
+       ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem);
        free(pdu);
        return ret;
 }
        free(pdu);
        return ret;
 }
diff --git a/init.c b/init.c
index e3ff39c01d0bdd78c450d9ee2b321982f0b14a4a..e2f7baba5cba40fd1d276f0462156d8b985c7593 100644 (file)
--- a/init.c
+++ b/init.c
@@ -1243,9 +1243,9 @@ static int client_flag_set(char c)
        return 0;
 }
 
        return 0;
 }
 
-int parse_cmd_client(void *client, char *opt)
+void parse_cmd_client(void *client, char *opt)
 {
 {
-       return fio_client_add_cmd_option(client, opt);
+       fio_client_add_cmd_option(client, opt);
 }
 
 int parse_cmd_line(int argc, char *argv[])
 }
 
 int parse_cmd_line(int argc, char *argv[])
@@ -1267,11 +1267,7 @@ int parse_cmd_line(int argc, char *argv[])
                did_arg = 1;
 
                if ((c & FIO_CLIENT_FLAG) || client_flag_set(c)) {
                did_arg = 1;
 
                if ((c & FIO_CLIENT_FLAG) || client_flag_set(c)) {
-                       if (parse_cmd_client(cur_client, argv[optind - 1])) {
-                               exit_val = 1;
-                               do_exit++;
-                               break;
-                       }
+                       parse_cmd_client(cur_client, argv[optind - 1]);
                        c &= ~FIO_CLIENT_FLAG;
                }
 
                        c &= ~FIO_CLIENT_FLAG;
                }
 
index f9c3c579d692ac5dd7a4110920dc7342712113d6..a54db7bf5d385aee5dbbc9e66d159169681f4de3 100644 (file)
--- a/server.c
+++ b/server.c
@@ -102,7 +102,7 @@ static int verify_convert_cmd(struct fio_net_cmd *cmd)
        cmd->pdu_len    = le32_to_cpu(cmd->pdu_len);
 
        switch (cmd->version) {
        cmd->pdu_len    = le32_to_cpu(cmd->pdu_len);
 
        switch (cmd->version) {
-       case FIO_SERVER_VER3:
+       case FIO_SERVER_VER:
                break;
        default:
                log_err("fio: bad server cmd version %d\n", cmd->version);
                break;
        default:
                log_err("fio: bad server cmd version %d\n", cmd->version);
@@ -270,24 +270,36 @@ static int handle_job_cmd(struct fio_net_cmd *cmd)
 
 static int handle_jobline_cmd(struct fio_net_cmd *cmd)
 {
 
 static int handle_jobline_cmd(struct fio_net_cmd *cmd)
 {
-       struct cmd_line_pdu *pdu = (struct cmd_line_pdu *) cmd->payload;
-       char *argv[FIO_NET_CMD_JOBLINE_ARGV];
+       void *pdu = cmd->payload;
+       struct cmd_single_line_pdu *cslp;
+       struct cmd_line_pdu *clp;
+       unsigned long offset;
+       char **argv;
        int ret, i;
 
        int ret, i;
 
-       pdu->argc = le16_to_cpu(pdu->argc);
+       clp = pdu;
+       clp->lines = le16_to_cpu(clp->lines);
+       argv = malloc(clp->lines * sizeof(char *));
+       offset = sizeof(*clp);
 
 
-       dprint(FD_NET, "server: %d command line args\n", pdu->argc);
+       dprint(FD_NET, "server: %d command line args\n", clp->lines);
 
 
-       for (i = 0; i < pdu->argc; i++) {
-               argv[i] = (char *) pdu->argv[i];
+       for (i = 0; i < clp->lines; i++) {
+               cslp = pdu + offset;
+               argv[i] = (char *) cslp->text;
+
+               offset += sizeof(*cslp) + le16_to_cpu(cslp->len);
                dprint(FD_NET, "server: %d: %s\n", i, argv[i]);
        }
 
                dprint(FD_NET, "server: %d: %s\n", i, argv[i]);
        }
 
-       if (parse_cmd_line(pdu->argc, argv)) {
+       if (parse_cmd_line(clp->lines, argv)) {
                fio_server_send_quit_cmd();
                fio_server_send_quit_cmd();
+               free(argv);
                return -1;
        }
 
                return -1;
        }
 
+       free(argv);
+
        fio_net_send_simple_cmd(server_fd, FIO_NET_CMD_START, 0);
 
        ret = exec_run();
        fio_net_send_simple_cmd(server_fd, FIO_NET_CMD_START, 0);
 
        ret = exec_run();
index c6e9ce215b9c2e6ecb077ac61aa0d170cc05ff8e..d68cbf52dbbaa3f90f0a8fc283ef79e84b1be57a 100644 (file)
--- a/server.h
+++ b/server.h
@@ -26,8 +26,7 @@ struct fio_net_cmd {
 };
 
 enum {
 };
 
 enum {
-       FIO_SERVER_VER          = 3,
-       FIO_SERVER_VER3         = 3,
+       FIO_SERVER_VER          = 4,
 
        FIO_SERVER_MAX_PDU      = 1024,
 
 
        FIO_SERVER_MAX_PDU      = 1024,
 
@@ -48,8 +47,6 @@ enum {
        /* crc does not include the crc fields */
        FIO_NET_CMD_CRC_SZ      = sizeof(struct fio_net_cmd) -
                                        2 * sizeof(uint16_t),
        /* crc does not include the crc fields */
        FIO_NET_CMD_CRC_SZ      = sizeof(struct fio_net_cmd) -
                                        2 * sizeof(uint16_t),
-
-       FIO_NET_CMD_JOBLINE_ARGV        = 128,
 };
 
 struct cmd_ts_pdu {
 };
 
 struct cmd_ts_pdu {
@@ -67,9 +64,14 @@ struct cmd_probe_pdu {
        uint8_t arch;
 };
 
        uint8_t arch;
 };
 
+struct cmd_single_line_pdu {
+       uint16_t len;
+       uint8_t text[0];
+};
+
 struct cmd_line_pdu {
 struct cmd_line_pdu {
-       uint16_t argc;
-       uint8_t argv[FIO_NET_CMD_JOBLINE_ARGV][64];
+       uint16_t lines;
+       struct cmd_single_line_pdu options[0];
 };
 
 extern int fio_start_server(int);
 };
 
 extern int fio_start_server(int);
@@ -91,7 +93,7 @@ extern int fio_clients_connect(void);
 extern int fio_clients_send_ini(const char *);
 extern int fio_handle_clients(void);
 extern int fio_client_add(const char *, void **);
 extern int fio_clients_send_ini(const char *);
 extern int fio_handle_clients(void);
 extern int fio_client_add(const char *, void **);
-extern int fio_client_add_cmd_option(void *, const char *);
+extern void fio_client_add_cmd_option(void *, const char *);
 
 extern int fio_recv_data(int sk, void *p, unsigned int len);
 extern int fio_send_data(int sk, const void *p, unsigned int len);
 
 extern int fio_recv_data(int sk, void *p, unsigned int len);
 extern int fio_send_data(int sk, const void *p, unsigned int len);
@@ -106,7 +108,7 @@ static inline void fio_init_net_cmd(struct fio_net_cmd *cmd, uint16_t opcode,
 {
        memset(cmd, 0, sizeof(*cmd));
 
 {
        memset(cmd, 0, sizeof(*cmd));
 
-       cmd->version    = __cpu_to_le16(FIO_SERVER_VER3);
+       cmd->version    = __cpu_to_le16(FIO_SERVER_VER);
        cmd->opcode     = cpu_to_le16(opcode);
 
        if (pdu) {
        cmd->opcode     = cpu_to_le16(opcode);
 
        if (pdu) {