Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-01-09 09:40:45

0001 // Copyright 2017 The Abseil Authors.
0002 //
0003 // Licensed under the Apache License, Version 2.0 (the "License");
0004 // you may not use this file except in compliance with the License.
0005 // You may obtain a copy of the License at
0006 //
0007 //      https://www.apache.org/licenses/LICENSE-2.0
0008 //
0009 // Unless required by applicable law or agreed to in writing, software
0010 // distributed under the License is distributed on an "AS IS" BASIS,
0011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0012 // See the License for the specific language governing permissions and
0013 // limitations under the License.
0014 //
0015 // UnscaledCycleClock
0016 //    An UnscaledCycleClock yields the value and frequency of a cycle counter
0017 //    that increments at a rate that is approximately constant.
0018 //    This class is for internal use only, you should consider using CycleClock
0019 //    instead.
0020 //
0021 // Notes:
0022 // The cycle counter frequency is not necessarily the core clock frequency.
0023 // That is, CycleCounter cycles are not necessarily "CPU cycles".
0024 //
0025 // An arbitrary offset may have been added to the counter at power on.
0026 //
0027 // On some platforms, the rate and offset of the counter may differ
0028 // slightly when read from different CPUs of a multiprocessor.  Usually,
0029 // we try to ensure that the operating system adjusts values periodically
0030 // so that values agree approximately.   If you need stronger guarantees,
0031 // consider using alternate interfaces.
0032 //
0033 // The CPU is not required to maintain the ordering of a cycle counter read
0034 // with respect to surrounding instructions.
0035 
0036 #ifndef ABSL_BASE_INTERNAL_UNSCALEDCYCLECLOCK_H_
0037 #define ABSL_BASE_INTERNAL_UNSCALEDCYCLECLOCK_H_
0038 
0039 #include <cstdint>
0040 
0041 #if defined(__APPLE__)
0042 #include <TargetConditionals.h>
0043 #endif
0044 
0045 #include "absl/base/config.h"
0046 #include "absl/base/internal/unscaledcycleclock_config.h"
0047 
0048 #if ABSL_USE_UNSCALED_CYCLECLOCK
0049 
0050 namespace absl {
0051 ABSL_NAMESPACE_BEGIN
0052 namespace time_internal {
0053 class UnscaledCycleClockWrapperForGetCurrentTime;
0054 }  // namespace time_internal
0055 
0056 namespace base_internal {
0057 class CycleClock;
0058 class UnscaledCycleClockWrapperForInitializeFrequency;
0059 
0060 class UnscaledCycleClock {
0061  private:
0062   UnscaledCycleClock() = delete;
0063 
0064   // Return the value of a cycle counter that counts at a rate that is
0065   // approximately constant.
0066   static int64_t Now();
0067 
0068   // Return the how much UnscaledCycleClock::Now() increases per second.
0069   // This is not necessarily the core CPU clock frequency.
0070   // It may be the nominal value report by the kernel, rather than a measured
0071   // value.
0072   static double Frequency();
0073 
0074   // Allowed users
0075   friend class base_internal::CycleClock;
0076   friend class time_internal::UnscaledCycleClockWrapperForGetCurrentTime;
0077   friend class base_internal::UnscaledCycleClockWrapperForInitializeFrequency;
0078 };
0079 
0080 #if defined(__x86_64__)
0081 
0082 inline int64_t UnscaledCycleClock::Now() {
0083   uint64_t low, high;
0084   __asm__ volatile("rdtsc" : "=a"(low), "=d"(high));
0085   return static_cast<int64_t>((high << 32) | low);
0086 }
0087 
0088 #endif
0089 
0090 }  // namespace base_internal
0091 ABSL_NAMESPACE_END
0092 }  // namespace absl
0093 
0094 #endif  // ABSL_USE_UNSCALED_CYCLECLOCK
0095 
0096 #endif  // ABSL_BASE_INTERNAL_UNSCALEDCYCLECLOCK_H_