00001 // 00002 // Copyright 2017 The Abseil Authors. 00003 // 00004 // Licensed under the Apache License, Version 2.0 (the "License"); 00005 // you may not use this file except in compliance with the License. 00006 // You may obtain a copy of the License at 00007 // 00008 // https://www.apache.org/licenses/LICENSE-2.0 00009 // 00010 // Unless required by applicable law or agreed to in writing, software 00011 // distributed under the License is distributed on an "AS IS" BASIS, 00012 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 // See the License for the specific language governing permissions and 00014 // limitations under the License. 00015 // 00016 00017 // ----------------------------------------------------------------------------- 00018 // File: cycleclock.h 00019 // ----------------------------------------------------------------------------- 00020 // 00021 // This header file defines a `CycleClock`, which yields the value and frequency 00022 // of a cycle counter that increments at a rate that is approximately constant. 00023 // 00024 // NOTE: 00025 // 00026 // The cycle counter frequency is not necessarily related to the core clock 00027 // frequency and should not be treated as such. That is, `CycleClock` cycles are 00028 // not necessarily "CPU cycles" and code should not rely on that behavior, even 00029 // if experimentally observed. 00030 // 00031 // An arbitrary offset may have been added to the counter at power on. 00032 // 00033 // On some platforms, the rate and offset of the counter may differ 00034 // slightly when read from different CPUs of a multiprocessor. Usually, 00035 // we try to ensure that the operating system adjusts values periodically 00036 // so that values agree approximately. If you need stronger guarantees, 00037 // consider using alternate interfaces. 00038 // 00039 // The CPU is not required to maintain the ordering of a cycle counter read 00040 // with respect to surrounding instructions. 00041 00042 #ifndef ABSL_BASE_INTERNAL_CYCLECLOCK_H_ 00043 #define ABSL_BASE_INTERNAL_CYCLECLOCK_H_ 00044 00045 #include <cstdint> 00046 00047 namespace absl { 00048 namespace base_internal { 00049 00050 // ----------------------------------------------------------------------------- 00051 // CycleClock 00052 // ----------------------------------------------------------------------------- 00053 class CycleClock { 00054 public: 00055 // CycleClock::Now() 00056 // 00057 // Returns the value of a cycle counter that counts at a rate that is 00058 // approximately constant. 00059 static int64_t Now(); 00060 00061 // CycleClock::Frequency() 00062 // 00063 // Returns the amount by which `CycleClock::Now()` increases per second. Note 00064 // that this value may not necessarily match the core CPU clock frequency. 00065 static double Frequency(); 00066 00067 private: 00068 CycleClock() = delete; // no instances 00069 CycleClock(const CycleClock&) = delete; 00070 CycleClock& operator=(const CycleClock&) = delete; 00071 }; 00072 00073 using CycleClockSourceFunc = int64_t (*)(); 00074 00075 class CycleClockSource { 00076 private: 00077 // CycleClockSource::Register() 00078 // 00079 // Register a function that provides an alternate source for the unscaled CPU 00080 // cycle count value. The source function must be async signal safe, must not 00081 // call CycleClock::Now(), and must have a frequency that matches that of the 00082 // unscaled clock used by CycleClock. A nullptr value resets CycleClock to use 00083 // the default source. 00084 static void Register(CycleClockSourceFunc source); 00085 }; 00086 00087 } // namespace base_internal 00088 } // namespace absl 00089 00090 #endif // ABSL_BASE_INTERNAL_CYCLECLOCK_H_