From e058e825bff228b64f564696be2085e7a1fe9154 Mon Sep 17 00:00:00 2001 From: Peter Lieven Date: Tue, 3 Jan 2017 11:13:06 +0100 Subject: [PATCH] socket: break receive loop if there are no more outstanding PDUs If the length of iscsi->waitpdu and iscsi->inqueue are the same then except for any target initiated NOPs or async messages we should have received any and all possible pdus from this socket and can abort early. This avoids running the loop one more time just to fail with EAGAIN at the recs/readv. Just avoiding that recv/readv syscall will shave at least 10us off this function and thus the latency. Suggested-by: Ronnie Sahlberg Signed-off-by: Peter Lieven --- lib/socket.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/socket.c b/lib/socket.c index b225eaf..9ee86a4 100644 --- a/lib/socket.c +++ b/lib/socket.c @@ -567,6 +567,9 @@ iscsi_read_from_socket(struct iscsi_context *iscsi) { struct iscsi_in_pdu *in; ssize_t data_size, count, padding_size; + int waitpdu_len, inqueue_len = 0; + + ISCSI_LIST_LENGTH(&iscsi->waitpdu, waitpdu_len); do { if (iscsi->incoming == NULL) { @@ -638,7 +641,6 @@ iscsi_read_from_socket(struct iscsi_context *iscsi) } count = recv(iscsi->fd, buf, count, 0); } - if (count == 0) { /* remote side has closed the socket. */ return -1; @@ -652,7 +654,6 @@ iscsi_read_from_socket(struct iscsi_context *iscsi) iscsi_get_error(iscsi)); return -1; } - in->data_pos += count; } @@ -661,8 +662,9 @@ iscsi_read_from_socket(struct iscsi_context *iscsi) } ISCSI_LIST_ADD_END(&iscsi->inqueue, in); + inqueue_len++; iscsi->incoming = NULL; - } while (iscsi->is_loggedin && iscsi->tcp_nonblocking); + } while (iscsi->tcp_nonblocking && inqueue_len < waitpdu_len && iscsi->is_loggedin); while (iscsi->inqueue != NULL) { struct iscsi_in_pdu *current = iscsi->inqueue;