datetime.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. // GetCycleCount guarantees to return synchronous values on different cores
  31. // and provide constant rate only on modern Intel and AMD processors
  32. // NOTE: rdtscp is used to prevent out of order execution
  33. // rdtsc can be reordered, while rdtscp cannot be reordered
  34. // with preceding instructions
  35. // PERFORMANCE: rdtsc - 15 cycles per call , rdtscp - 19 cycles per call
  36. // WARNING: following instruction can be executed out-of-order
  37. Y_FORCE_INLINE ui64 GetCycleCount() noexcept {
  38. #if defined(_MSC_VER)
  39. // Generates the rdtscp instruction, which returns the processor time stamp.
  40. // The processor time stamp records the number of clock cycles since the last reset.
  41. extern const bool HaveRdtscp;
  42. if (HaveRdtscp) {
  43. unsigned int aux;
  44. return __rdtscp(&aux);
  45. } else {
  46. return __rdtsc();
  47. }
  48. #elif defined(_x86_64_)
  49. extern const bool HaveRdtscp;
  50. unsigned hi, lo;
  51. if (HaveRdtscp) {
  52. __asm__ __volatile__("rdtscp"
  53. : "=a"(lo), "=d"(hi)::"%rcx");
  54. } else {
  55. __asm__ __volatile__("rdtsc"
  56. : "=a"(lo), "=d"(hi));
  57. }
  58. return ((unsigned long long)lo) | (((unsigned long long)hi) << 32);
  59. #elif defined(_i386_)
  60. extern const bool HaveRdtscp;
  61. ui64 x;
  62. if (HaveRdtscp) {
  63. __asm__ volatile("rdtscp\n\t"
  64. : "=A"(x)::"%ecx");
  65. } else {
  66. __asm__ volatile("rdtsc\n\t"
  67. : "=A"(x));
  68. }
  69. return x;
  70. #elif defined(_darwin_)
  71. return mach_absolute_time();
  72. #elif defined(__clang__) && !defined(_arm_)
  73. return __builtin_readcyclecounter();
  74. #elif defined(_arm32_)
  75. return MicroSeconds();
  76. #elif defined(_arm64_)
  77. ui64 x;
  78. __asm__ __volatile__("isb; mrs %0, cntvct_el0"
  79. : "=r"(x));
  80. return x;
  81. #else
  82. #error "unsupported arch"
  83. #endif
  84. }