From 2639fd2f06e1912fcddc63e920f058e83fa189d8 Mon Sep 17 00:00:00 2001 From: Schrodinger ZHU Yifan Date: Sun, 24 Nov 2024 00:28:50 -0500 Subject: [PATCH] avoid chrono inclusion when clock_gettime is available --- src/snmalloc/aal/aal.h | 25 ++++++++++++++++++++++++- src/snmalloc/ds_core/defines.h | 9 +++++++++ src/snmalloc/pal/pal_posix.h | 1 + src/snmalloc/pal/pal_timer_default.h | 2 -- 4 files changed, 34 insertions(+), 3 deletions(-) diff --git a/src/snmalloc/aal/aal.h b/src/snmalloc/aal/aal.h index 92baaf1f6..2d40f9f65 100644 --- a/src/snmalloc/aal/aal.h +++ b/src/snmalloc/aal/aal.h @@ -10,7 +10,14 @@ #include "aal_concept.h" #include "aal_consts.h" -#include +#if __has_include() +# include +# ifdef CLOCK_MONOTONIC +# define SNMALLOC_TICK_USE_CLOCK_GETTIME +# endif +#else +# include +#endif #include #include @@ -169,11 +176,27 @@ namespace snmalloc if constexpr ( (Arch::aal_features & NoCpuCycleCounters) == NoCpuCycleCounters) { +#ifdef SNMALLOC_TICK_USE_CLOCK_GETTIME + // the buf is populated by clock_gettime + SNMALLOC_UNINITIALISED timespec buf; + // we can skip the error checking here: + // * EFAULT: for out-of-bound pointers (buf is always valid stack + // memory) + // * EINVAL: for invalid clock_id (we only use CLOCK_MONOTONIC enforced + // by POSIX.1) + // Notice that clock_gettime is a usually a vDSO call, so the overhead + // is minimal. + ::clock_gettime(CLOCK_MONOTONIC, &buf); + return static_cast(buf.tv_sec) * 1000'000'000 + + static_cast(buf.tv_nsec); +# undef SNMALLOC_TICK_USE_CLOCK_GETTIME +#else auto tick = std::chrono::high_resolution_clock::now(); return static_cast( std::chrono::duration_cast( tick.time_since_epoch()) .count()); +#endif } else { diff --git a/src/snmalloc/ds_core/defines.h b/src/snmalloc/ds_core/defines.h index 6b36d6e54..d50939ad0 100644 --- a/src/snmalloc/ds_core/defines.h +++ b/src/snmalloc/ds_core/defines.h @@ -194,6 +194,15 @@ namespace snmalloc # endif #endif +// Used to suppress pattern filling for potentially unintialized variables with +// automatic storage duration. +// https://clang.llvm.org/docs/AttributeReference.html#uninitialized +#ifdef __clang__ +# define SNMALLOC_UNINITIALISED [[clang::uninitialized]] +#else +# define SNMALLOC_UNINITIALISED +#endif + namespace snmalloc { /** diff --git a/src/snmalloc/pal/pal_posix.h b/src/snmalloc/pal/pal_posix.h index 37026761d..1214ff327 100644 --- a/src/snmalloc/pal/pal_posix.h +++ b/src/snmalloc/pal/pal_posix.h @@ -6,6 +6,7 @@ #if defined(SNMALLOC_BACKTRACE_HEADER) # include SNMALLOC_BACKTRACE_HEADER #endif +#include #include #include #include diff --git a/src/snmalloc/pal/pal_timer_default.h b/src/snmalloc/pal/pal_timer_default.h index c7761effe..d70abd526 100644 --- a/src/snmalloc/pal/pal_timer_default.h +++ b/src/snmalloc/pal/pal_timer_default.h @@ -4,8 +4,6 @@ #include "pal_consts.h" #include "pal_ds.h" -#include - namespace snmalloc { template