Skip to content

Commit

Permalink
Use sleep-aware monotonic clock if available
Browse files Browse the repository at this point in the history
  • Loading branch information
Bret Ambrose committed Feb 18, 2025
1 parent 6401c83 commit bb9ac7b
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
58 changes: 58 additions & 0 deletions cmake/AwsFeatureTests.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,64 @@ if(MSVC)
}" AWS_HAVE_MSVC_INTRINSICS_X64)
endif()

if(NOT CMAKE_CROSSCOMPILING)
check_c_source_runs("
#include <stdint.h>
#include <time.h>
static const uint64_t NS_PER_SEC = 1000000000;
bool get_time(uint64_t *current_time) {
struct timespec ts;
if (clock_gettime(CLOCK_BOOTTIME, &ts)) {
return false;
}
uint64_t secs = (uint64_t)ts.tv_sec;
uint64_t n_secs = (uint64_t)ts.tv_nsec;
*current_time = (secs * NS_PER_SEC) + n_secs;
return true;
}
int main() {
uint64_t start_timestap = 0;
if (!get_time(&start_timestap)) {
return -1;
}
return 0;
}" AWS_HAS_POSIX_BOOT_CLOCK)
else()
check_c_source_compiles("
#include <stdint.h>
#include <time.h>
static const uint64_t NS_PER_SEC = 1000000000;
bool get_time(uint64_t *current_time) {
struct timespec ts;
if (clock_gettime(CLOCK_BOOTTIME, &ts)) {
return false;
}
uint64_t secs = (uint64_t)ts.tv_sec;
uint64_t n_secs = (uint64_t)ts.tv_nsec;
*current_time = (secs * NS_PER_SEC) + n_secs;
return true;
}
int main() {
uint64_t start_timestap = 0;
if (!get_time(&start_timestap)) {
return -1;
}
return 0;
}" AWS_HAS_POSIX_BOOT_CLOCK)
endif()

# This does a lot to detect when intrinsics are available and has to set cflags to do so.
# leave it in its own file for ease of managing it.
include(AwsSIMD)
1 change: 1 addition & 0 deletions include/aws/common/config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@
#cmakedefine AWS_ARCH_INTEL
#cmakedefine AWS_ARCH_INTEL_X64
#cmakedefine AWS_USE_CPU_EXTENSIONS
#cmakedefine AWS_HAS_POSIX_BOOT_CLOCK

#endif
4 changes: 3 additions & 1 deletion source/posix/clock.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@

static const uint64_t NS_PER_SEC = 1000000000;

#if defined(CLOCK_MONOTONIC_RAW)
#if defined(AWS_HAS_POSIX_BOOT_CLOCK)
# define HIGH_RES_CLOCK CLOCK_BOOTTIME
#elif defined(CLOCK_MONOTONIC_RAW)
# define HIGH_RES_CLOCK CLOCK_MONOTONIC_RAW
#else
# define HIGH_RES_CLOCK CLOCK_MONOTONIC
Expand Down

0 comments on commit bb9ac7b

Please sign in to comment.