datetime.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #pragma once
  2. #include "defaults.h"
  3. #include "platform.h"
  4. #if defined(_win_)
  5. #include <intrin.h>
  6. #pragma intrinsic(__rdtsc)
  7. #endif // _win_
  8. #if defined(_darwin_) && !defined(_x86_)
  9. #include <mach/mach_time.h>
  10. #endif
  11. /// util/system/datetime.h contains only system time providers
  12. /// for handy datetime utilities include util/datetime/base.h
  13. /// Current time in microseconds since epoch
  14. ui64 MicroSeconds() noexcept;
  15. /// Current time in milliseconds since epoch
  16. inline ui64 MilliSeconds() {
  17. return MicroSeconds() / ui64(1000);
  18. }
  19. /// Current time in milliseconds since epoch (deprecated, use MilliSeconds instead)
  20. inline ui64 millisec() {
  21. return MilliSeconds();
  22. }
  23. /// Current time in seconds since epoch
  24. ui32 Seconds() noexcept;
  25. /// Current thread time in microseconds
  26. ui64 ThreadCPUUserTime() noexcept;
  27. ui64 ThreadCPUSystemTime() noexcept;
  28. ui64 ThreadCPUTime() noexcept;
  29. void NanoSleep(ui64 ns) noexcept;
  30. #if defined(_x86_)
  31. namespace NPrivate {
  32. bool HaveRdtscpImpl();
  33. } // namespace NPrivate
  34. #endif
  35. // GetCycleCount guarantees to return synchronous values on different cores
  36. // and provide constant rate only on modern Intel and AMD processors
  37. // NOTE: rdtscp is used to prevent out of order execution
  38. // rdtsc can be reordered, while rdtscp cannot be reordered
  39. // with preceding instructions
  40. // PERFORMANCE: rdtsc - 15 cycles per call , rdtscp - 19 cycles per call
  41. // WARNING: following instruction can be executed out-of-order
  42. Y_FORCE_INLINE ui64 GetCycleCount() noexcept {
  43. #if defined(_MSC_VER)
  44. // Generates the rdtscp instruction, which returns the processor time stamp.
  45. // The processor time stamp records the number of clock cycles since the last reset.
  46. static const bool haveRdtscp = ::NPrivate::HaveRdtscpImpl();
  47. if (haveRdtscp) {
  48. unsigned int aux;
  49. return __rdtscp(&aux);
  50. } else {
  51. return __rdtsc();
  52. }
  53. #elif defined(_x86_64_)
  54. static const bool haveRdtscp = ::NPrivate::HaveRdtscpImpl();
  55. unsigned hi, lo;
  56. if (haveRdtscp) {
  57. __asm__ __volatile__("rdtscp"
  58. : "=a"(lo), "=d"(hi)::"%rcx");
  59. } else {
  60. __asm__ __volatile__("rdtsc"
  61. : "=a"(lo), "=d"(hi));
  62. }
  63. return ((unsigned long long)lo) | (((unsigned long long)hi) << 32);
  64. #elif defined(_i386_)
  65. static const bool haveRdtscp = ::NPrivate::HaveRdtscpImpl();
  66. ui64 x;
  67. if (haveRdtscp) {
  68. __asm__ volatile("rdtscp\n\t"
  69. : "=A"(x)::"%ecx");
  70. } else {
  71. __asm__ volatile("rdtsc\n\t"
  72. : "=A"(x));
  73. }
  74. return x;
  75. #elif defined(_darwin_)
  76. return mach_absolute_time();
  77. #elif defined(__clang__) && !defined(_arm_)
  78. return __builtin_readcyclecounter();
  79. #elif defined(_arm32_)
  80. return MicroSeconds();
  81. #elif defined(_arm64_)
  82. ui64 x;
  83. __asm__ __volatile__("isb; mrs %0, cntvct_el0"
  84. : "=r"(x));
  85. return x;
  86. #else
  87. #error "unsupported arch"
  88. #endif
  89. }