Coverage Report

Created: 2026-05-23 07:02

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/abseil-cpp/absl/base/internal/unscaledcycleclock.h
Line
Count
Source
1
// Copyright 2017 The Abseil Authors.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//      https://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
//
15
// UnscaledCycleClock
16
//    An UnscaledCycleClock yields the value and frequency of a cycle counter
17
//    that increments at a rate that is approximately constant.
18
//    This class is for internal use only, you should consider using CycleClock
19
//    instead.
20
//
21
// Notes:
22
// The cycle counter frequency is not necessarily the core clock frequency.
23
// That is, CycleCounter cycles are not necessarily "CPU cycles".
24
//
25
// An arbitrary offset may have been added to the counter at power on.
26
//
27
// On some platforms, the rate and offset of the counter may differ
28
// slightly when read from different CPUs of a multiprocessor.  Usually,
29
// we try to ensure that the operating system adjusts values periodically
30
// so that values agree approximately.   If you need stronger guarantees,
31
// consider using alternate interfaces.
32
//
33
// The CPU is not required to maintain the ordering of a cycle counter read
34
// with respect to surrounding instructions.
35
36
#ifndef ABSL_BASE_INTERNAL_UNSCALEDCYCLECLOCK_H_
37
#define ABSL_BASE_INTERNAL_UNSCALEDCYCLECLOCK_H_
38
39
#include <cstdint>
40
41
#if defined(__APPLE__)
42
#include <TargetConditionals.h>
43
#endif
44
45
#include "absl/base/config.h"
46
#include "absl/base/internal/unscaledcycleclock_config.h"
47
48
#if ABSL_USE_UNSCALED_CYCLECLOCK
49
50
namespace gloop_do_not_use {
51
class UnscaledCycleClockWrapperForPerCpuTest;
52
}  // namespace gloop_do_not_use
53
54
namespace absl {
55
ABSL_NAMESPACE_BEGIN
56
namespace time_internal {
57
class UnscaledCycleClockWrapperForGetCurrentTime;
58
}  // namespace time_internal
59
60
namespace base_internal {
61
class CycleClock;
62
class UnscaledCycleClockWrapperForInitializeFrequency;
63
64
class UnscaledCycleClock {
65
 private:
66
  UnscaledCycleClock() = delete;
67
68
  // Return the value of a cycle counter that counts at a rate that is
69
  // approximately constant.
70
  static int64_t Now();
71
72
  // Return the how much UnscaledCycleClock::Now() increases per second.
73
  // This is not necessarily the core CPU clock frequency.
74
  // It may be the nominal value report by the kernel, rather than a measured
75
  // value.
76
  static double Frequency();
77
78
  // Allowed users
79
  friend class base_internal::CycleClock;
80
  friend class time_internal::UnscaledCycleClockWrapperForGetCurrentTime;
81
  friend class base_internal::UnscaledCycleClockWrapperForInitializeFrequency;
82
  friend class gloop_do_not_use::UnscaledCycleClockWrapperForPerCpuTest;
83
};
84
85
#if defined(__x86_64__)
86
87
0
inline int64_t UnscaledCycleClock::Now() {
88
0
  uint64_t low, high;
89
0
  __asm__ volatile("rdtsc" : "=a"(low), "=d"(high));
90
0
  return static_cast<int64_t>((high << 32) | low);
91
0
}
92
93
#elif defined(__aarch64__)
94
95
// System timer of ARMv8 runs at a different frequency than the CPU's.
96
//
97
// Frequency is fixed. From Armv8.6-A and Armv9.1-A on, the frequency is 1GHz.
98
// Pre-Armv8.6-A, the frequency was a system design choice, typically in the
99
// range of 1MHz to 50MHz. See also:
100
// https://developer.arm.com/documentation/102379/0101/What-is-the-Generic-Timer-
101
//
102
// It can be read at CNTFRQ special register.  We assume the OS has set up the
103
// virtual timer properly.
104
inline int64_t UnscaledCycleClock::Now() {
105
  int64_t virtual_timer_value;
106
  asm volatile("mrs %0, cntvct_el0" : "=r"(virtual_timer_value));
107
  return virtual_timer_value;
108
}
109
110
#endif
111
112
}  // namespace base_internal
113
ABSL_NAMESPACE_END
114
}  // namespace absl
115
116
#endif  // ABSL_USE_UNSCALED_CYCLECLOCK
117
118
#endif  // ABSL_BASE_INTERNAL_UNSCALEDCYCLECLOCK_H_