Skip to content

Commit c2d192f

Browse files
committed
msg/async/Timeout: always round up
Currently, we always round down, which has a bad side effect: when a timer comes closer, we have lots of early wakeups, and eventually we'll run into a busy loop (timeout=0) when the timeout is less than one millisecond; the process will remain this busy loop for one millisecond, wasting lots of CPU time. Signed-off-by: Max Kellermann <[email protected]>
1 parent 32dcaa9 commit c2d192f

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

src/msg/async/Timeout.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#ifndef CEPH_MSG_TIMEOUT_H
1818
#define CEPH_MSG_TIMEOUT_H
1919

20+
#include "include/intarith.h" // for div_round_up()
21+
2022
#include <time.h> // for struct timeval
2123

2224
/**
@@ -28,7 +30,8 @@
2830
constexpr int
2931
timeout_to_milliseconds(const struct timeval &tv) noexcept
3032
{
31-
return tv.tv_sec * 1000 + tv.tv_usec / 1000;
33+
/* round up to the next millisecond so we don't wake up too early */
34+
return tv.tv_sec * 1000 + div_round_up(tv.tv_usec, 1000);
3235
}
3336

3437
/**

0 commit comments

Comments
 (0)