Don't alloc/free when we can reuse, and don't calculate string
lengths twice.
Signed-off-by: Jens Axboe <axboe@kernel.dk>
struct cmd_single_line_pdu *cslp;
struct cmd_line_pdu *clp;
unsigned long offset;
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);
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
*/
/*
* 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
/*
* We need one cmd_line_pdu, and argc number of cmd_single_line_pdu
offset = sizeof(*clp);
for (i = 0; i < client->argc; i++) {
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]);
cslp = pdu + offset;
strcpy((char *) cslp->text, client->argv[i]);
offset += sizeof(*cslp) + arg_len;
}
offset += sizeof(*cslp) + arg_len;
}
clp->lines = cpu_to_le16(client->argc);
ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, 0);
free(pdu);
clp->lines = cpu_to_le16(client->argc);
ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem, 0);
free(pdu);
int fio_net_send_cmd(int fd, uint16_t opcode, const void *buf, off_t size,
uint64_t tag)
{
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;
if (this_len > FIO_SERVER_MAX_PDU)
this_len = FIO_SERVER_MAX_PDU;
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);
fio_init_net_cmd(cmd, opcode, buf, this_len, tag);
fio_net_cmd_crc(cmd);
ret = fio_send_data(fd, cmd, sizeof(*cmd) + this_len);
fio_net_cmd_crc(cmd);
ret = fio_send_data(fd, cmd, sizeof(*cmd) + this_len);
size -= this_len;
buf += this_len;
} while (!ret && size);
size -= this_len;
buf += this_len;
} while (!ret && size);
+ if (cmd)
+ free(cmd);
+
{
struct jobs_eta *je;
size_t size;
{
struct jobs_eta *je;
size_t size;
int i;
size = sizeof(*je) + thread_number * sizeof(char);
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);
if (!calc_thread_status(je, 1)) {
free(je);
je->elapsed_sec = cpu_to_le32(je->nr_running);
je->eta_sec = cpu_to_le64(je->eta_sec);
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);
- 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:
switch (cmd->opcode) {
case FIO_NET_CMD_QUIT: