diff options
| author | Amlal El Mahrouss <amlal@nekernel.org> | 2025-08-31 16:49:59 +0200 |
|---|---|---|
| committer | Amlal El Mahrouss <amlal@nekernel.org> | 2025-08-31 16:54:56 +0200 |
| commit | 7afa74b53110c0258c7fa9ae5aa8e7dde7534b4d (patch) | |
| tree | 157c8de0554bd681fdb30f7a814b21881d905fa4 /dev/misc/BenchKit/HWChronometer.h | |
| parent | 11034c004ee7b232e53d69e9f1ae9000f008285f (diff) | |
feat! BenchKit breaking changes.
why:
- Made it expandable to other traits and platforms.
- You can provide a `Traits` now to the `HWChrono`.
Signed-off-by: Amlal El Mahrouss <amlal@nekernel.org>
Diffstat (limited to 'dev/misc/BenchKit/HWChronometer.h')
| -rw-r--r-- | dev/misc/BenchKit/HWChronometer.h | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/dev/misc/BenchKit/HWChronometer.h b/dev/misc/BenchKit/HWChronometer.h new file mode 100644 index 00000000..2b8637d8 --- /dev/null +++ b/dev/misc/BenchKit/HWChronometer.h @@ -0,0 +1,66 @@ +/* ------------------------------------------- + +Copyright (C) 2025, Amlal El Mahrouss, all rights reserved. + +------------------------------------------- */ + +#pragma once + +#include <misc/BenchKit/Chronometer.h> + +namespace Kernel { +struct HWChronoTraits; + +template <typename ChronoTraits = HWChronoTraits> +class HWChrono; + +/// @brief BenchKit chrono logic for x64. +struct HWChronoTraits final { + private: + STATIC UInt64 TickImpl_(void) { +#ifdef __NE_AMD64__ + UInt64 a = 0, d = 0; + + asm volatile("rdtsc" : "=a"(a), "=d"(d)); + + return (d << 32) | a; +#elif defined(__NE_ARM64__) + UInt64 result; + + asm volatile("mrs %0, cntvct_el1" : "=r"(result)); + + return result; +#else +#error !!! no backend defined !!! +#endif + } + + friend HWChrono<HWChronoTraits>; +}; + +/// @brief hardware chronometer implementation using a trait to extract the data. +template <typename ChronoTraits> +class HWChrono BENCHKIT_INTERFACE { + public: + HWChrono() = default; + ~HWChrono() override = default; + + NE_COPY_DEFAULT(HWChrono) + + public: + Void Start() override { fStart = ChronoTraits::TickImpl_(); } + + Void Stop() override { fStop = ChronoTraits::TickImpl_(); } + + Void Reset() override { + fStart = 0; + fStop = 0; + } + + UInt64 GetElapsedTime() const override { return fStop - fStart; } + + private: + UInt64 fStart{}; + UInt64 fStop{}; +}; +} // namespace Kernel |
