1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-14 08:34:57 +00:00

LibCore+Utilities: Replace ElapsedTimer precise flag with an enum

Previously, `true` was passed into the ElapsedTimer constructor if a
precise timer was required. We now use an enum to more explicitly
specify whether we would like a precise or a coarse timer.
This commit is contained in:
Tim Ledbetter 2024-02-26 17:52:03 +00:00 committed by Andrew Kaster
parent 0f168d9ca2
commit 679fe00d10
5 changed files with 13 additions and 8 deletions

View file

@ -20,7 +20,7 @@ ElapsedTimer ElapsedTimer::start_new()
void ElapsedTimer::start() void ElapsedTimer::start()
{ {
m_valid = true; m_valid = true;
m_origin_time = m_precise ? MonotonicTime::now() : MonotonicTime::now_coarse(); m_origin_time = m_timer_type == TimerType::Precise ? MonotonicTime::now() : MonotonicTime::now_coarse();
} }
void ElapsedTimer::reset() void ElapsedTimer::reset()
@ -36,7 +36,7 @@ i64 ElapsedTimer::elapsed_milliseconds() const
Duration ElapsedTimer::elapsed_time() const Duration ElapsedTimer::elapsed_time() const
{ {
VERIFY(is_valid()); VERIFY(is_valid());
auto now = m_precise ? MonotonicTime::now() : MonotonicTime::now_coarse(); auto now = m_timer_type == TimerType::Precise ? MonotonicTime::now() : MonotonicTime::now_coarse();
return now - m_origin_time; return now - m_origin_time;
} }

View file

@ -10,12 +10,17 @@
namespace Core { namespace Core {
enum class TimerType {
Precise,
Coarse
};
class ElapsedTimer { class ElapsedTimer {
public: public:
static ElapsedTimer start_new(); static ElapsedTimer start_new();
ElapsedTimer(bool precise = false) ElapsedTimer(TimerType timer_type = TimerType::Coarse)
: m_precise(precise) : m_timer_type(timer_type)
{ {
} }
@ -36,7 +41,7 @@ public:
private: private:
MonotonicTime m_origin_time { MonotonicTime::now() }; MonotonicTime m_origin_time { MonotonicTime::now() };
bool m_precise { false }; TimerType m_timer_type { TimerType::Coarse };
bool m_valid { false }; bool m_valid { false };
}; };

View file

@ -39,7 +39,7 @@ ErrorOr<int> serenity_main(Main::Arguments args)
} }
auto loader = maybe_loader.release_value(); auto loader = maybe_loader.release_value();
Core::ElapsedTimer sample_timer { true }; Core::ElapsedTimer sample_timer { Core::TimerType::Precise };
i64 total_loader_time = 0; i64 total_loader_time = 0;
int remaining_samples = sample_count > 0 ? sample_count : NumericLimits<int>::max(); int remaining_samples = sample_count > 0 ? sample_count : NumericLimits<int>::max();
unsigned total_loaded_samples = 0; unsigned total_loaded_samples = 0;

View file

@ -45,7 +45,7 @@ struct {
size_t total_bytes_copied = 0; size_t total_bytes_copied = 0;
size_t total_blocks_in = 0, partial_blocks_in = 0; size_t total_blocks_in = 0, partial_blocks_in = 0;
size_t total_blocks_out = 0, partial_blocks_out = 0; size_t total_blocks_out = 0, partial_blocks_out = 0;
Core::ElapsedTimer timer { true }; Core::ElapsedTimer timer { Core::TimerType::Precise };
} statistics; } statistics;
static void closing_statistics() static void closing_statistics()

View file

@ -78,7 +78,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
// 0: got ttl exhausted response // 0: got ttl exhausted response
// -1: error or no response // -1: error or no response
auto try_reach_host = [&](int ttl) -> ErrorOr<int> { auto try_reach_host = [&](int ttl) -> ErrorOr<int> {
Core::ElapsedTimer m_timer { true }; Core::ElapsedTimer m_timer { Core::TimerType::Precise };
auto ttl_number = ByteString::number(ttl); auto ttl_number = ByteString::number(ttl);
for (auto i = 0; i < max_retries; i++) { for (auto i = 0; i < max_retries; i++) {
icmp_request request {}; icmp_request request {};