From 7dcac92491eece90864670896abdd262c0d09ca2 Mon Sep 17 00:00:00 2001 From: Jonathan Date: Mon, 20 May 2024 13:00:43 -0400 Subject: [PATCH] add estimated time counter --- common/log.cc | 46 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 43 insertions(+), 3 deletions(-) diff --git a/common/log.cc b/common/log.cc index 9eb128e4..81510d22 100644 --- a/common/log.cc +++ b/common/log.cc @@ -184,6 +184,32 @@ static bool is_timing = false; static uint64_t last_count = -1; static time_point last_indeterminate_time; static std::atomic_bool locked = false; +static std::array one_percent_times; +static size_t num_percent_times, percent_time_index; +static time_point last_percent_time; + +static duration average_times_for_one_percent() +{ + duration pt {}; + + for (size_t i = 0; i < num_percent_times; i++) { + pt += one_percent_times[i]; + } + + pt /= num_percent_times; + + return pt; +} + +static void register_average_time(duration dt) +{ + one_percent_times[percent_time_index] = dt; + percent_time_index = (percent_time_index + 1) % one_percent_times.size(); + + if (num_percent_times < one_percent_times.size()) { + num_percent_times++; + } +} void header(const char *name) { @@ -233,6 +259,9 @@ void percent(uint64_t count, uint64_t max, bool displayElapsed) is_timing = true; last_count = -1; last_indeterminate_time = {}; + num_percent_times = 0; + percent_time_index = 0; + last_percent_time = I_FloatTime(); } if (count == max) { @@ -243,13 +272,13 @@ void percent(uint64_t count, uint64_t max, bool displayElapsed) if (active_percent_callback) { active_percent_callback(std::nullopt, elapsed); } else { - print(flag::PERCENT, "[done] time elapsed: {:.3}\n", elapsed); + print(flag::PERCENT, "[done] time elapsed: {:%H:%M:%S}\n", elapsed); } } else { if (active_percent_callback) { active_percent_callback(100u, elapsed); } else { - print(flag::PERCENT, "[100%] time elapsed: {:.3}\n", elapsed); + print(flag::PERCENT, "[100%] time elapsed: {:%H:%M:%S}\n", elapsed); } } } @@ -261,7 +290,18 @@ void percent(uint64_t count, uint64_t max, bool displayElapsed) if (active_percent_callback) { active_percent_callback(pct, std::nullopt); } else { - print(flag::PERCENT, "[{:>3}%]\r", pct); + if (pct) { + // we shifted from some other percent value to a non-zero value; + // calculate the time it took to get a 1% change + uint64_t diff = pct - last_count; // should always be >= 1 + duration dt = I_FloatTime() - last_percent_time; + dt /= diff; + register_average_time(dt); + last_percent_time = I_FloatTime(); + print(flag::PERCENT, "[{:>3}%] est: {:%H:%M:%S}\r", pct, std::chrono::duration_cast>(average_times_for_one_percent() * (100 - pct))); + } else { + print(flag::PERCENT, "[{:>3}%] ...\r", pct); + } } last_count = pct; }