cycleclock.cc 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. // The implementation of CycleClock::Frequency.
  15. //
  16. // NOTE: only i386 and x86_64 have been well tested.
  17. // PPC, sparc, alpha, and ia64 are based on
  18. // http://peter.kuscsik.com/wordpress/?p=14
  19. // with modifications by m3b. See also
  20. // https://setisvn.ssl.berkeley.edu/svn/lib/fftw-3.0.1/kernel/cycle.h
  21. #include "y_absl/base/internal/cycleclock.h"
  22. #include <atomic>
  23. #include <chrono> // NOLINT(build/c++11)
  24. #include "y_absl/base/attributes.h"
  25. #include "y_absl/base/config.h"
  26. #include "y_absl/base/internal/unscaledcycleclock.h"
  27. namespace y_absl {
  28. Y_ABSL_NAMESPACE_BEGIN
  29. namespace base_internal {
  30. #if Y_ABSL_USE_UNSCALED_CYCLECLOCK
  31. #ifdef Y_ABSL_INTERNAL_NEED_REDUNDANT_CONSTEXPR_DECL
  32. constexpr int32_t CycleClock::kShift;
  33. constexpr double CycleClock::kFrequencyScale;
  34. #endif
  35. Y_ABSL_CONST_INIT std::atomic<CycleClockSourceFunc>
  36. CycleClock::cycle_clock_source_{nullptr};
  37. void CycleClockSource::Register(CycleClockSourceFunc source) {
  38. // Corresponds to the load(std::memory_order_acquire) in LoadCycleClockSource.
  39. CycleClock::cycle_clock_source_.store(source, std::memory_order_release);
  40. }
  41. #ifdef _WIN32
  42. int64_t CycleClock::Now() {
  43. auto fn = LoadCycleClockSource();
  44. if (fn == nullptr) {
  45. return base_internal::UnscaledCycleClock::Now() >> kShift;
  46. }
  47. return fn() >> kShift;
  48. }
  49. #endif
  50. #else
  51. int64_t CycleClock::Now() {
  52. return std::chrono::duration_cast<std::chrono::nanoseconds>(
  53. std::chrono::steady_clock::now().time_since_epoch())
  54. .count();
  55. }
  56. double CycleClock::Frequency() {
  57. return 1e9;
  58. }
  59. #endif
  60. } // namespace base_internal
  61. Y_ABSL_NAMESPACE_END
  62. } // namespace y_absl