Skip to content

Commit

Permalink
rsockets: Adjust poll timeout based on time already used
Browse files Browse the repository at this point in the history
In rpoll(), we spin for a short period of time prior to calling
poll().  And poll() is called in a loop, in order to handle
events that may not update the desired rsocket state.  For
example, we could wake up from poll() to process a control
message, such as a credit update, but be waiting for app data
to be avaialble.  This can result in the calling thread re-entering
poll().

In order to avoid waiting in poll() for longer than the user
specified timeout, track when we enter the call and update the
timeout passed to poll() accordingly.

Signed-off-by: Sean Hefty <sean.hefty@intel.com>
  • Loading branch information
shefty committed May 14, 2019
1 parent b60c79d commit 1d6eb18
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions librdmacm/rsocket.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#include <netinet/tcp.h>
#include <sys/epoll.h>
#include <search.h>
#include <time.h>
#include <byteswap.h>
#include <util/compiler.h>
#include <util/util.h>
Expand Down Expand Up @@ -430,6 +431,14 @@ static void read_all(int fd, void *msg, size_t len)
assert(rc == len);
}

static uint64_t rs_time_us(void)
{
struct timespec now;

clock_gettime(CLOCK_MONOTONIC, &now);
return now.tv_sec * 1000000 + now.tv_nsec / 1000;
}

static void ds_insert_qp(struct rsocket *rs, struct ds_qp *qp)
{
if (!rs->qp_list)
Expand Down Expand Up @@ -3142,8 +3151,8 @@ static int rs_poll_events(struct pollfd *rfds, struct pollfd *fds, nfds_t nfds)
*/
int rpoll(struct pollfd *fds, nfds_t nfds, int timeout)
{
struct timeval s, e;
struct pollfd *rfds;
uint64_t start_time;
uint32_t poll_time = 0;
int ret;

Expand All @@ -3153,11 +3162,9 @@ int rpoll(struct pollfd *fds, nfds_t nfds, int timeout)
return ret;

if (!poll_time)
gettimeofday(&s, NULL);
start_time = rs_time_us();

gettimeofday(&e, NULL);
poll_time = (e.tv_sec - s.tv_sec) * 1000000 +
(e.tv_usec - s.tv_usec) + 1;
poll_time = (uint32_t) (rs_time_us() - start_time);
} while (poll_time <= polling_time);

rfds = rs_fds_alloc(nfds);
Expand All @@ -3169,6 +3176,12 @@ int rpoll(struct pollfd *fds, nfds_t nfds, int timeout)
if (ret)
break;

if (timeout >= 0) {
timeout -= (int) ((rs_time_us() - start_time) / 1000);
if (timeout <= 0)
return 0;
}

ret = poll(rfds, nfds, timeout);
if (ret <= 0)
break;
Expand Down

0 comments on commit 1d6eb18

Please sign in to comment.