cputimer.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #pragma once
  2. #include "base.h"
  3. #include <util/system/rusage.h>
  4. #include <util/generic/string.h>
  5. #include <util/stream/str.h>
  6. class TTimer {
  7. private:
  8. TInstant Start_;
  9. TStringStream Message_;
  10. public:
  11. TTimer(const TStringBuf message = TStringBuf(" took: "));
  12. ~TTimer();
  13. };
  14. class TSimpleTimer {
  15. TInstant T;
  16. public:
  17. TSimpleTimer() {
  18. Reset();
  19. }
  20. TDuration Get() const {
  21. return TInstant::Now() - T;
  22. }
  23. void Reset() {
  24. T = TInstant::Now();
  25. }
  26. };
  27. class TProfileTimer {
  28. TDuration T;
  29. public:
  30. TProfileTimer() {
  31. Reset();
  32. }
  33. TDuration Get() const {
  34. return TRusage::Get().Utime - T;
  35. }
  36. TDuration Step() {
  37. TRusage r;
  38. r.Fill();
  39. TDuration d = r.Utime - T;
  40. T = r.Utime;
  41. return d;
  42. }
  43. void Reset() {
  44. T = TRusage::Get().Utime;
  45. }
  46. };
  47. /// Return cached processor cycle count per second. Method takes 1 second at first invocation.
  48. /// Note, on older systems cycle rate may change during program lifetime,
  49. /// so returned value may be incorrect. Modern Intel and AMD processors keep constant TSC rate.
  50. ui64 GetCyclesPerMillisecond();
  51. void SetCyclesPerSecond(ui64 cycles);
  52. TDuration CyclesToDuration(ui64 cycles);
  53. ui64 DurationToCycles(TDuration duration);
  54. // NBS-3400 - CyclesToDuration and DurationToCycles may overflow for long running events
  55. TDuration CyclesToDurationSafe(ui64 cycles);
  56. ui64 DurationToCyclesSafe(TDuration duration);
  57. class TPrecisionTimer {
  58. private:
  59. ui64 Start = 0;
  60. public:
  61. TPrecisionTimer();
  62. ui64 GetCycleCount() const;
  63. };
  64. TString FormatCycles(ui64 cycles);
  65. class TFormattedPrecisionTimer {
  66. private:
  67. ui64 Start;
  68. const char* Message;
  69. IOutputStream* Out;
  70. public:
  71. TFormattedPrecisionTimer(const char* message = "took ", IOutputStream* out = &Cout);
  72. ~TFormattedPrecisionTimer();
  73. };
  74. class TFuncTimer {
  75. public:
  76. TFuncTimer(const char* func);
  77. ~TFuncTimer();
  78. private:
  79. const TInstant Start_;
  80. const char* Func_;
  81. };
  82. class TFakeTimer {
  83. public:
  84. inline TFakeTimer(const char* = nullptr) noexcept {
  85. }
  86. };
  87. #if defined(WITH_DEBUG)
  88. #define TDebugTimer TFuncTimer
  89. #else
  90. #define TDebugTimer TFakeTimer
  91. #endif
  92. class TTimeLogger {
  93. private:
  94. TString Message;
  95. bool Verbose;
  96. bool OK;
  97. time_t Begin;
  98. ui64 BeginCycles;
  99. public:
  100. TTimeLogger(const TString& message, bool verbose = true);
  101. ~TTimeLogger();
  102. void SetOK();
  103. double ElapsedTime() const;
  104. };