From 7afa74b53110c0258c7fa9ae5aa8e7dde7534b4d Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Sun, 31 Aug 2025 16:49:59 +0200 Subject: 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 --- dev/misc/BenchKit/Chrono.h | 40 ------------------------ dev/misc/BenchKit/Chronometer.h | 40 ++++++++++++++++++++++++ dev/misc/BenchKit/HWChronometer.h | 66 +++++++++++++++++++++++++++++++++++++++ dev/misc/BenchKit/X64Chrono.h | 56 --------------------------------- 4 files changed, 106 insertions(+), 96 deletions(-) delete mode 100644 dev/misc/BenchKit/Chrono.h create mode 100644 dev/misc/BenchKit/Chronometer.h create mode 100644 dev/misc/BenchKit/HWChronometer.h delete mode 100644 dev/misc/BenchKit/X64Chrono.h (limited to 'dev/misc/BenchKit') diff --git a/dev/misc/BenchKit/Chrono.h b/dev/misc/BenchKit/Chrono.h deleted file mode 100644 index 3a82a94e..00000000 --- a/dev/misc/BenchKit/Chrono.h +++ /dev/null @@ -1,40 +0,0 @@ -/* ------------------------------------------- - -Copyright (C) 2025, Amlal El Mahrouss, all rights reserved. - -------------------------------------------- */ - -#ifndef BENCHKIT_CHRONO_H -#define BENCHKIT_CHRONO_H - -#include -#include - -/// @author Amlal El Mahrouss -/// @brief BenchKit Chrono contract. - -#define BENCHKIT_INTERFACE : public ::Kernel::ChronoInterface - -namespace Kernel { -class ChronoInterface; - -/// @brief a Chronometer interface used for benchmarking. -class ChronoInterface { - public: - ChronoInterface() = default; - virtual ~ChronoInterface() = default; - - NE_COPY_DEFAULT(ChronoInterface) - - virtual Void Start() = 0; - virtual Void Stop() = 0; - virtual Void Reset() = 0; - virtual UInt64 GetElapsedTime() const = 0; -}; -} // namespace Kernel - -namespace BenchKit { -using namespace Kernel; -} - -#endif // BENCHKIT_CHRONO_H diff --git a/dev/misc/BenchKit/Chronometer.h b/dev/misc/BenchKit/Chronometer.h new file mode 100644 index 00000000..3a82a94e --- /dev/null +++ b/dev/misc/BenchKit/Chronometer.h @@ -0,0 +1,40 @@ +/* ------------------------------------------- + +Copyright (C) 2025, Amlal El Mahrouss, all rights reserved. + +------------------------------------------- */ + +#ifndef BENCHKIT_CHRONO_H +#define BENCHKIT_CHRONO_H + +#include +#include + +/// @author Amlal El Mahrouss +/// @brief BenchKit Chrono contract. + +#define BENCHKIT_INTERFACE : public ::Kernel::ChronoInterface + +namespace Kernel { +class ChronoInterface; + +/// @brief a Chronometer interface used for benchmarking. +class ChronoInterface { + public: + ChronoInterface() = default; + virtual ~ChronoInterface() = default; + + NE_COPY_DEFAULT(ChronoInterface) + + virtual Void Start() = 0; + virtual Void Stop() = 0; + virtual Void Reset() = 0; + virtual UInt64 GetElapsedTime() const = 0; +}; +} // namespace Kernel + +namespace BenchKit { +using namespace Kernel; +} + +#endif // BENCHKIT_CHRONO_H 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 + +namespace Kernel { +struct HWChronoTraits; + +template +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; +}; + +/// @brief hardware chronometer implementation using a trait to extract the data. +template +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 diff --git a/dev/misc/BenchKit/X64Chrono.h b/dev/misc/BenchKit/X64Chrono.h deleted file mode 100644 index 728e7d60..00000000 --- a/dev/misc/BenchKit/X64Chrono.h +++ /dev/null @@ -1,56 +0,0 @@ -/* ------------------------------------------- - -Copyright (C) 2025, Amlal El Mahrouss, all rights reserved. - -------------------------------------------- */ - -#pragma once - -#include - -#if defined(__NE_AMD64__) - -namespace Kernel { -class X64Chrono; -struct X64ChronoTraits; - -/// @brief BenchKit chrono logic for x64. -struct X64ChronoTraits { - private: - STATIC UInt64 TickImpl_(void) { - UInt64 a = 0, d = 0; - - asm volatile("rdtsc" : "=a"(a), "=d"(d)); - return (d << 32) | a; - } - - friend X64Chrono; -}; - -/// @brief X86_64 hardware chrono implementation using the `rdtsc` instruction. -class X64Chrono BENCHKIT_INTERFACE { - public: - X64Chrono() = default; - ~X64Chrono() override = default; - - NE_COPY_DEFAULT(X64Chrono) - - public: - Void Start() override { fStart = X64ChronoTraits::TickImpl_(); } - - Void Stop() override { fStop = X64ChronoTraits::TickImpl_(); } - - Void Reset() override { - fStart = 0; - fStop = 0; - } - - UInt64 GetElapsedTime() const override { return fStop - fStart; } - - private: - UInt64 fStart{}; - UInt64 fStop{}; -}; -} // namespace Kernel - -#endif // defined(__NE_AMD64__) \ No newline at end of file -- cgit v1.2.3