Skip to content

Commit

Permalink
Decrease CPU usage caused by exmpp_tls:send with large data.
Browse files Browse the repository at this point in the history
Sending one large chunk of data with exmpp_tls:send eats lots of
CPU power and blocks whole Erlang emulator. This is caused by the
fact that encrypted output is read from memory BIO in 1k chunks.
Memory BIO, after reading data, shifts the remaining part.
If large chunks of data (few MB) is sent and then read in 1k
chunks, then a _lot_ of shifting is performed eating CPU.

The solution is to simply allocate binary of the needed size
(amount of data in memory BIO can be retrieved with
BIO_ctrl_pending) and then issue only one read that reads the
whole data.
  • Loading branch information
rraptorr authored and badlop committed Sep 20, 2011
1 parent 7245e55 commit a6c43b7
Showing 1 changed file with 2 additions and 12 deletions.
14 changes: 2 additions & 12 deletions c_src/exmpp_tls_openssl.c
Expand Up @@ -387,21 +387,11 @@ exmpp_tls_openssl_control(ErlDrvData drv_data, unsigned int command,
break;
case COMMAND_GET_ENCRYPTED_OUTPUT:
/* Allocate binary to copy encrypted data. */
size = BUF_SIZE + 1;
rlen = 1;
size = BIO_ctrl_pending(edd->bio_write) + 1;
b = driver_alloc_binary(size);
b->orig_bytes[0] = RET_OK;

/* Copy data. */
while ((ret = BIO_read(edd->bio_write,
b->orig_bytes + rlen, BUF_SIZE)) > 0) {
rlen += ret;
size += BUF_SIZE;
b = driver_realloc_binary(b, size);
}

size = rlen;
b = driver_realloc_binary(b, size);
BIO_read(edd->bio_write, b->orig_bytes + 1, size - 1);

break;
case COMMAND_GET_PEER_CERTIFICATE:
Expand Down

0 comments on commit a6c43b7

Please sign in to comment.