fasttime.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. #include "dynlib.h"
  2. #include "fasttime.h"
  3. #include <util/generic/singleton.h>
  4. #include <util/generic/yexception.h>
  5. #include <utility>
  6. #include <util/thread/singleton.h>
  7. #if defined(_win_) || defined(_arm32_) || defined(_cygwin_)
  8. ui64 InterpolatedMicroSeconds() {
  9. return MicroSeconds();
  10. }
  11. #else
  12. #include <dlfcn.h>
  13. #include <sys/time.h>
  14. #if defined(_musl_)
  15. #include <util/generic/hash.h>
  16. #include <util/generic/vector.h>
  17. #include <util/generic/string.h>
  18. #include <contrib/libs/linuxvdso/interface.h>
  19. #endif
  20. namespace {
  21. using TTime = ui64;
  22. struct TSymbols {
  23. using TFunc = int (*)(struct timeval*, struct timezone*);
  24. inline TSymbols()
  25. : Func(nullptr)
  26. {
  27. // not DEFAULT, cause library/cpp/gettimeofday
  28. Func = reinterpret_cast<TFunc>(dlsym(RTLD_NEXT, "gettimeofday"));
  29. #if defined(_musl_)
  30. if (!Func) {
  31. Func = reinterpret_cast<TFunc>(NVdso::Function("__vdso_gettimeofday", "LINUX_2.6"));
  32. }
  33. #endif
  34. if (!Func) {
  35. Func = reinterpret_cast<TFunc>(Libc()->Sym("gettimeofday"));
  36. }
  37. }
  38. inline TTime SystemTime() {
  39. timeval tv;
  40. Zero(tv);
  41. Func(&tv, nullptr);
  42. return (((TTime)1000000) * (TTime)tv.tv_sec) + (TTime)tv.tv_usec;
  43. }
  44. static inline THolder<TDynamicLibrary> OpenLibc() {
  45. const char* libs[] = {
  46. "/lib/libc.so.8",
  47. "/lib/libc.so.7",
  48. "/lib/libc.so.6",
  49. };
  50. for (auto& lib : libs) {
  51. try {
  52. return MakeHolder<TDynamicLibrary>(lib);
  53. } catch (...) {
  54. // ¯\_(ツ)_/¯
  55. }
  56. }
  57. ythrow yexception() << "can not load libc";
  58. }
  59. inline TDynamicLibrary* Libc() {
  60. if (!Lib) {
  61. Lib = OpenLibc();
  62. }
  63. return Lib.Get();
  64. }
  65. THolder<TDynamicLibrary> Lib;
  66. TFunc Func;
  67. };
  68. static inline TTime SystemTime() {
  69. return Singleton<TSymbols>()->SystemTime();
  70. }
  71. struct TInitialTimes {
  72. inline TInitialTimes()
  73. : ITime(TimeBase())
  74. , IProc(RdtscBase())
  75. {
  76. }
  77. static TTime RdtscBase() {
  78. return GetCycleCount() / (TTime)1000;
  79. }
  80. static TTime TimeBase() {
  81. return SystemTime();
  82. }
  83. inline TTime Rdtsc() {
  84. return RdtscBase() - IProc;
  85. }
  86. inline TTime Time() {
  87. return TimeBase() - ITime;
  88. }
  89. const TTime ITime;
  90. const TTime IProc;
  91. };
  92. template <size_t N, class A, class B>
  93. class TLinePredictor {
  94. public:
  95. using TSample = std::pair<A, B>;
  96. inline TLinePredictor()
  97. : C_(0)
  98. , A_(0)
  99. , B_(0)
  100. {
  101. }
  102. inline void Add(const A& a, const B& b) noexcept {
  103. Add(TSample(a, b));
  104. }
  105. inline void Add(const TSample& s) noexcept {
  106. S_[(C_++) % N] = s;
  107. if (C_ > 1) {
  108. ReCalc();
  109. }
  110. }
  111. inline B Predict(A a) const noexcept {
  112. return A_ + a * B_;
  113. }
  114. inline size_t Size() const noexcept {
  115. return C_;
  116. }
  117. inline bool Enough() const noexcept {
  118. return Size() >= N;
  119. }
  120. inline A LastX() const noexcept {
  121. return S_[(C_ - 1) % N].first;
  122. }
  123. private:
  124. inline void ReCalc() noexcept {
  125. const size_t n = Min(N, C_);
  126. double sx = 0;
  127. double sy = 0;
  128. double sxx = 0;
  129. double sxy = 0;
  130. for (size_t i = 0; i < n; ++i) {
  131. const double x = S_[i].first;
  132. const double y = S_[i].second;
  133. sx += x;
  134. sy += y;
  135. sxx += x * x;
  136. sxy += x * y;
  137. }
  138. B_ = (n * sxy - sx * sy) / (n * sxx - sx * sx);
  139. A_ = (sy - B_ * sx) / n;
  140. }
  141. private:
  142. size_t C_;
  143. TSample S_[N];
  144. double A_;
  145. double B_;
  146. };
  147. class TTimePredictor: public TInitialTimes {
  148. public:
  149. inline TTimePredictor()
  150. : Next_(1)
  151. {
  152. }
  153. inline TTime Get() {
  154. return GetBase() + ITime;
  155. }
  156. private:
  157. inline TTime GetBase() {
  158. const TTime x = Rdtsc();
  159. if (TimeToSync(x)) {
  160. const TTime y = Time();
  161. P_.Add(x, y);
  162. return y;
  163. }
  164. if (P_.Enough()) {
  165. return P_.Predict(x);
  166. }
  167. return Time();
  168. }
  169. inline bool TimeToSync(TTime x) {
  170. if (x > Next_) {
  171. Next_ = Min(x + x / 10, x + 1000000);
  172. return true;
  173. }
  174. return false;
  175. }
  176. private:
  177. TLinePredictor<16, TTime, TTime> P_;
  178. TTime Next_;
  179. };
  180. } // namespace
  181. ui64 InterpolatedMicroSeconds() {
  182. return FastTlsSingleton<TTimePredictor>()->Get();
  183. }
  184. #endif