timer.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // Copyright 2023 Google LLC
  2. // SPDX-License-Identifier: Apache-2.0
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. #ifndef HIGHWAY_HWY_TIMER_H_
  16. #define HIGHWAY_HWY_TIMER_H_
  17. // Platform-specific timer functions. Provides Now() and functions for
  18. // interpreting and converting the timer-inl.h Ticks.
  19. #include <stdint.h>
  20. #include "hwy/highway_export.h"
  21. namespace hwy {
  22. namespace platform {
  23. // Returns current timestamp [in seconds] relative to an unspecified origin.
  24. // Features: monotonic (no negative elapsed time), steady (unaffected by system
  25. // time changes), high-resolution (on the order of microseconds).
  26. // Uses InvariantTicksPerSecond and the baseline version of timer::Start().
  27. HWY_DLLEXPORT double Now();
  28. // Functions for use with timer-inl.h:
  29. // Returns whether it is safe to call timer::Stop without executing an illegal
  30. // instruction; if false, fills cpu100 (a pointer to a 100 character buffer)
  31. // via GetCpuString().
  32. HWY_DLLEXPORT bool HaveTimerStop(char* cpu100);
  33. // Returns tick rate, useful for converting timer::Ticks to seconds. Invariant
  34. // means the tick counter frequency is independent of CPU throttling or sleep.
  35. // This call may be expensive, callers should cache the result.
  36. HWY_DLLEXPORT double InvariantTicksPerSecond();
  37. // Returns ticks elapsed in back to back timer calls, i.e. a function of the
  38. // timer resolution (minimum measurable difference) and overhead.
  39. // This call is expensive, callers should cache the result.
  40. HWY_DLLEXPORT uint64_t TimerResolution();
  41. // Returns false if no detailed description is available, otherwise fills
  42. // `cpu100` with up to 100 characters (including \0) identifying the CPU model.
  43. HWY_DLLEXPORT bool GetCpuString(char* cpu100);
  44. } // namespace platform
  45. struct Timestamp {
  46. Timestamp() { t = platform::Now(); }
  47. double t;
  48. };
  49. static inline double SecondsSince(const Timestamp& t0) {
  50. const Timestamp t1;
  51. return t1.t - t0.t;
  52. }
  53. } // namespace hwy
  54. #endif // HIGHWAY_HWY_TIMER_H_