Thanks Macel.
Could you try on attached patch please?
On Thu, Sep 18, 2014 at 12:19 PM, Marcel Wirtz <[email protected]> wrote:
> The problem is definetly not IO related (And no other software I know has
> such a high CPU load transferring data from a file to a socket).
>
> A short analysis of the problem:
>
> in transfer_file_data() read is called to read data from the file to a
> buffer (0.29% of the total CPU instructions). Then ns_send() is called.
> Here the buffer is copied to another buffer (59.27% CPU instructions in
> memcpy). send() is called by ns_write_to_socket() and uses 0.49% of the CPU
> instructions. So the real problem is not reading from the file or writing
> to the socket, but the internal copy of the buffer in ns_send(). If there
> would be a way not to copy every byte of the file internaly before sending,
> the CPU load should be smaller (about 60%, which would be better by far).
>
> Am Mittwoch, 17. September 2014 09:54:11 UTC+2 schrieb Sergey Lyubka:
>>
>> Well if that is memcpy, then I don't find it surprising.
>> Server is mostly doing data copying, reading from file and copying to the
>> socket.
>>
>>
>>
>> On Sat, Sep 13, 2014 at 12:11 PM, Marcel Wirtz <[email protected]> wrote:
>>
>>> Yes
>>>
>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "mongoose-users" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to [email protected].
>>> To post to this group, send email to [email protected].
>>> Visit this group at http://groups.google.com/group/mongoose-users.
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>
>> --
> You received this message because you are subscribed to the Google Groups
> "mongoose-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/mongoose-users.
> For more options, visit https://groups.google.com/d/optout.
>
--
You received this message because you are subscribed to the Google Groups
"mongoose-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/mongoose-users.
For more options, visit https://groups.google.com/d/optout.
diff --git a/mongoose.c b/mongoose.c
index e32c63a..612ac3e 100644
--- a/mongoose.c
+++ b/mongoose.c
@@ -4741,21 +4741,29 @@ static void close_local_endpoint(struct connection
*conn) {
static void transfer_file_data(struct connection *conn) {
char buf[IOBUF_SIZE];
- int n;
+ struct iobuf *io = &conn->ns_conn->send_iobuf;
+ int n, space = io->size - io->len;
// If output buffer is too big, don't send anything. Wait until
// mongoose drains already buffered data to the client.
- if (conn->ns_conn->send_iobuf.len > sizeof(buf) * 2) return;
+ if (io->len > sizeof(buf) * 2) return;
- // Do not send anyt
- n = read(conn->endpoint.fd, buf, conn->cl < (int64_t) sizeof(buf) ?
- (int) conn->cl : (int) sizeof(buf));
+ if (space > 0) {
+ n = read(conn->endpoint.fd, io->buf + io->len, conn->cl < space ? conn->cl
: space);
+ } else {
+ n = read(conn->endpoint.fd, buf, conn->cl < (int64_t) sizeof(buf) ?
+ (int) conn->cl : (int) sizeof(buf));
+ }
if (n <= 0) {
close_local_endpoint(conn);
} else if (n > 0) {
conn->cl -= n;
- ns_send(conn->ns_conn, buf, n);
+ if (space > 0) {
+ io->len += n;
+ } else {
+ ns_send(conn->ns_conn, buf, n);
+ }
if (conn->cl <= 0) {
close_local_endpoint(conn);
}