summaryrefslogtreecommitdiffhomepage
path: root/dev/misc/BenchKit/X64Chrono.h
blob: 728e7d60c522b5b2f673f8d3e50e173ae470e88a (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
55
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__)