clock_win.cc 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Copyright 2014 The Crashpad Authors. All rights reserved.
  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. // http://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. #include "util/misc/clock.h"
  15. #include <windows.h>
  16. #include <sys/types.h>
  17. namespace {
  18. LARGE_INTEGER QpcFrequencyInternal() {
  19. LARGE_INTEGER frequency;
  20. QueryPerformanceFrequency(&frequency);
  21. return frequency;
  22. }
  23. int64_t QpcFrequency() {
  24. static LARGE_INTEGER frequency = QpcFrequencyInternal();
  25. return frequency.QuadPart;
  26. }
  27. } // namespace
  28. namespace crashpad {
  29. uint64_t ClockMonotonicNanoseconds() {
  30. LARGE_INTEGER time;
  31. QueryPerformanceCounter(&time);
  32. int64_t frequency = QpcFrequency();
  33. int64_t whole_seconds = time.QuadPart / frequency;
  34. int64_t leftover_ticks = time.QuadPart % frequency;
  35. constexpr int64_t kNanosecondsPerSecond = static_cast<const int64_t>(1E9);
  36. return (whole_seconds * kNanosecondsPerSecond) +
  37. ((leftover_ticks * kNanosecondsPerSecond) / frequency);
  38. }
  39. void SleepNanoseconds(uint64_t nanoseconds) {
  40. // This is both inaccurate (will be way too long for short sleeps) and
  41. // incorrect (can sleep for less than requested). But it's what's available
  42. // without implementing a busy loop.
  43. constexpr uint64_t kNanosecondsPerMillisecond = static_cast<uint64_t>(1E6);
  44. Sleep(static_cast<DWORD>(nanoseconds / kNanosecondsPerMillisecond));
  45. }
  46. } // namespace crashpad