xref: /aosp_15_r20/external/google-benchmark/src/timers.h (revision dbb99499c3810fa1611fa2242a2fc446be01a57c)
1 #ifndef BENCHMARK_TIMERS_H
2 #define BENCHMARK_TIMERS_H
3 
4 #include <chrono>
5 #include <string>
6 
7 namespace benchmark {
8 
9 // Return the CPU usage of the current process
10 double ProcessCPUUsage();
11 
12 // Return the CPU usage of the children of the current process
13 double ChildrenCPUUsage();
14 
15 // Return the CPU usage of the current thread
16 double ThreadCPUUsage();
17 
18 #if defined(BENCHMARK_OS_QURT)
19 
20 // std::chrono::now() can return 0 on some Hexagon devices;
21 // this reads the value of a 56-bit, 19.2MHz hardware counter
22 // and converts it to seconds. Unlike std::chrono, this doesn't
23 // return an absolute time, but since ChronoClockNow() is only used
24 // to compute elapsed time, this shouldn't matter.
25 struct QuRTClock {
26   typedef uint64_t rep;
27   typedef std::ratio<1, 19200000> period;
28   typedef std::chrono::duration<rep, period> duration;
29   typedef std::chrono::time_point<QuRTClock> time_point;
30   static const bool is_steady = false;
31 
nowQuRTClock32   static time_point now() {
33     unsigned long long count;
34     asm volatile(" %0 = c31:30 " : "=r"(count));
35     return time_point(static_cast<duration>(count));
36   }
37 };
38 
39 #else
40 
41 #if defined(HAVE_STEADY_CLOCK)
42 template <bool HighResIsSteady = std::chrono::high_resolution_clock::is_steady>
43 struct ChooseSteadyClock {
44   typedef std::chrono::high_resolution_clock type;
45 };
46 
47 template <>
48 struct ChooseSteadyClock<false> {
49   typedef std::chrono::steady_clock type;
50 };
51 #endif  // HAVE_STEADY_CLOCK
52 
53 #endif
54 
55 struct ChooseClockType {
56 #if defined(BENCHMARK_OS_QURT)
57   typedef QuRTClock type;
58 #elif defined(HAVE_STEADY_CLOCK)
59   typedef ChooseSteadyClock<>::type type;
60 #else
61   typedef std::chrono::high_resolution_clock type;
62 #endif
63 };
64 
65 inline double ChronoClockNow() {
66   typedef ChooseClockType::type ClockType;
67   using FpSeconds = std::chrono::duration<double, std::chrono::seconds::period>;
68   return FpSeconds(ClockType::now().time_since_epoch()).count();
69 }
70 
71 std::string LocalDateTimeString();
72 
73 }  // end namespace benchmark
74 
75 #endif  // BENCHMARK_TIMERS_H
76