summaryrefslogtreecommitdiffhomepage
path: root/dev/generic_kits/BenchKit/X64Chrono.h
blob: 229146cb9cfe50d2915d99f24810e066dff01c05 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/* -------------------------------------------

Copyright (C) 2025, Amlal El Mahrouss, all rights reserved.

------------------------------------------- */

#pragma once

#include <generic_kits/BenchKit/Chrono.h>

#if defined(__NE_AMD64__)

namespace Kernel {
class X64Chrono;
struct X64ChronoTraits;

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 : public ChronoInterface {
 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 = 0;
  UInt64 fStop  = 0;
};
}  // namespace Kernel

#endif  // defined(__NE_AMD64__)