Fix get_system_time()

This commit is contained in:
elad335 2024-10-29 16:11:44 +02:00 committed by Elad
parent 92bf6ed0a7
commit 97f2b6b701
4 changed files with 29 additions and 22 deletions

View File

@ -212,9 +212,9 @@ u64 get_system_time()
const u64 tsc = utils::get_tsc(); const u64 tsc = utils::get_tsc();
#if _MSC_VER #if _MSC_VER
const u64 result = static_cast<u64>(u128_from_mul(tsc, 1000000ull) / freq) * g_cfg.core.clocks_scale / 100u; const u64 result = static_cast<u64>(u128_from_mul(tsc, 1000000ull) / freq);
#else #else
const u64 result = (tsc / freq * 1000000ull + tsc % freq * 1000000ull / freq) * g_cfg.core.clocks_scale / 100u; const u64 result = (tsc / freq * 1000000ull + tsc % freq * 1000000ull / freq);
#endif #endif
return result; return result;
} }

View File

@ -569,9 +569,6 @@ int main(int argc, char** argv)
ensure(thread_ctrl::is_main(), "Not main thread"); ensure(thread_ctrl::is_main(), "Not main thread");
// Initialize TSC freq (in case it isn't)
static_cast<void>(utils::ensure_tsc_freq_init());
// Initialize thread pool finalizer (on first use) // Initialize thread pool finalizer (on first use)
static_cast<void>(named_thread("", [](int) {})); static_cast<void>(named_thread("", [](int) {}));

View File

@ -734,9 +734,24 @@ bool utils::get_low_power_mode()
#endif #endif
} }
static constexpr ullong round_tsc(ullong val) static constexpr ullong round_tsc(ullong val, ullong known_error)
{ {
return utils::rounded_div(val, 100'000) * 100'000; if (known_error >= 500'000)
{
// Do not accept large errors
return 0;
}
ullong by = 1000;
known_error /= 1000;
while (known_error && by < 100'000)
{
by *= 10;
known_error /= 10;
}
return utils::rounded_div(val, by) * by;
} }
namespace utils namespace utils
@ -744,7 +759,7 @@ namespace utils
u64 s_tsc_freq = 0; u64 s_tsc_freq = 0;
} }
named_thread<std::function<void()>> s_thread_evaluate_tsc_freq("TSC Evaluate Thread", []() static const bool s_tsc_freq_evaluated = []() -> bool
{ {
static const ullong cal_tsc = []() -> ullong static const ullong cal_tsc = []() -> ullong
{ {
@ -763,7 +778,7 @@ named_thread<std::function<void()>> s_thread_evaluate_tsc_freq("TSC Evaluate Thr
return 0; return 0;
if (freq.QuadPart <= 9'999'999) if (freq.QuadPart <= 9'999'999)
return round_tsc(freq.QuadPart * 1024); return 0;
const ullong timer_freq = freq.QuadPart; const ullong timer_freq = freq.QuadPart;
#else #else
@ -788,6 +803,8 @@ named_thread<std::function<void()>> s_thread_evaluate_tsc_freq("TSC Evaluate Thr
const ullong sec_base = ts0.tv_sec; const ullong sec_base = ts0.tv_sec;
#endif #endif
constexpr usz sleep_time_ms = 20;
for (usz sample = 0; sample < sample_count; sample++) for (usz sample = 0; sample < sample_count; sample++)
{ {
for (usz i = 0; i < retry_count; i++) for (usz i = 0; i < retry_count; i++)
@ -823,11 +840,11 @@ named_thread<std::function<void()>> s_thread_evaluate_tsc_freq("TSC Evaluate Thr
if (sample < sample_count - 1) if (sample < sample_count - 1)
{ {
// Sleep 20ms between first and last sample // Sleep between first and last sample
#ifdef _WIN32 #ifdef _WIN32
Sleep(20); Sleep(sleep_time_ms);
#else #else
usleep(20'000); usleep(sleep_time_ms * 1000);
#endif #endif
} }
} }
@ -843,17 +860,12 @@ named_thread<std::function<void()>> s_thread_evaluate_tsc_freq("TSC Evaluate Thr
const u64 res = utils::udiv128(static_cast<u64>(data >> 64), static_cast<u64>(data), (timer_data[1] - timer_data[0])); const u64 res = utils::udiv128(static_cast<u64>(data >> 64), static_cast<u64>(data), (timer_data[1] - timer_data[0]));
// Rounding // Rounding
return round_tsc(res); return round_tsc(res, utils::mul_saturate<u64>(utils::add_saturate<u64>(rdtsc_diff[0], rdtsc_diff[1]), utils::aligned_div(timer_freq, timer_data[1] - timer_data[0])));
}(); }();
atomic_storage<u64>::release(utils::s_tsc_freq, cal_tsc); atomic_storage<u64>::release(utils::s_tsc_freq, cal_tsc);
}); return true;
}();
void utils::ensure_tsc_freq_init()
{
// Join thread
s_thread_evaluate_tsc_freq();
}
u64 utils::get_total_memory() u64 utils::get_total_memory()
{ {

View File

@ -94,6 +94,4 @@ namespace utils
{ {
return s_tsc_freq; return s_tsc_freq;
} }
void ensure_tsc_freq_init();
} }