diff options
Diffstat (limited to 'dev/misc')
| -rw-r--r-- | dev/misc/.keep | 0 | ||||
| -rw-r--r-- | dev/misc/BenchKit/Chrono.h | 40 | ||||
| -rw-r--r-- | dev/misc/BenchKit/X64Chrono.h | 56 |
3 files changed, 96 insertions, 0 deletions
diff --git a/dev/misc/.keep b/dev/misc/.keep new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/dev/misc/.keep diff --git a/dev/misc/BenchKit/Chrono.h b/dev/misc/BenchKit/Chrono.h new file mode 100644 index 00000000..3a82a94e --- /dev/null +++ b/dev/misc/BenchKit/Chrono.h @@ -0,0 +1,40 @@ +/* ------------------------------------------- + +Copyright (C) 2025, Amlal El Mahrouss, all rights reserved. + +------------------------------------------- */ + +#ifndef BENCHKIT_CHRONO_H +#define BENCHKIT_CHRONO_H + +#include <CompilerKit/CompilerKit.h> +#include <NeKit/Defines.h> + +/// @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/X64Chrono.h b/dev/misc/BenchKit/X64Chrono.h new file mode 100644 index 00000000..728e7d60 --- /dev/null +++ b/dev/misc/BenchKit/X64Chrono.h @@ -0,0 +1,56 @@ +/* ------------------------------------------- + +Copyright (C) 2025, Amlal El Mahrouss, all rights reserved. + +------------------------------------------- */ + +#pragma once + +#include <misc/BenchKit/Chrono.h> + +#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 |
