mock_time_env.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
  2. // This source code is licensed under both the GPLv2 (found in the
  3. // COPYING file in the root directory) and Apache 2.0 License
  4. // (found in the LICENSE.Apache file in the root directory).
  5. #pragma once
  6. #include <atomic>
  7. #include <limits>
  8. #include "port/port.h"
  9. #include "rocksdb/system_clock.h"
  10. #include "test_util/mock_time_env.h"
  11. #include "test_util/sync_point.h"
  12. #include "util/random.h"
  13. namespace ROCKSDB_NAMESPACE {
  14. // NOTE: SpecialEnv offers most of this functionality, along with hooks
  15. // for safe DB behavior under a mock time environment, so should be used
  16. // instead of MockSystemClock for DB tests.
  17. class MockSystemClock : public SystemClockWrapper {
  18. public:
  19. explicit MockSystemClock(const std::shared_ptr<SystemClock>& base)
  20. : SystemClockWrapper(base) {}
  21. static const char* kClassName() { return "MockSystemClock"; }
  22. const char* Name() const override { return kClassName(); }
  23. Status GetCurrentTime(int64_t* time_sec) override {
  24. assert(time_sec != nullptr);
  25. *time_sec = static_cast<int64_t>(current_time_us_ / kMicrosInSecond);
  26. return Status::OK();
  27. }
  28. virtual uint64_t NowSeconds() { return current_time_us_ / kMicrosInSecond; }
  29. uint64_t NowMicros() override { return current_time_us_; }
  30. uint64_t NowNanos() override {
  31. assert(current_time_us_ <= std::numeric_limits<uint64_t>::max() / 1000);
  32. return current_time_us_ * 1000;
  33. }
  34. uint64_t RealNowMicros() { return target_->NowMicros(); }
  35. void SetCurrentTime(uint64_t time_sec) {
  36. assert(time_sec < std::numeric_limits<uint64_t>::max() / kMicrosInSecond);
  37. assert(time_sec * kMicrosInSecond >= current_time_us_);
  38. current_time_us_ = time_sec * kMicrosInSecond;
  39. }
  40. // It's a fake sleep that just updates the Env current time, which is similar
  41. // to `NoSleepEnv.SleepForMicroseconds()` and
  42. // `SpecialEnv.MockSleepForMicroseconds()`.
  43. // It's also similar to `set_current_time()`, which takes an absolute time in
  44. // seconds, vs. this one takes the sleep in microseconds.
  45. // Note: Not thread safe.
  46. void SleepForMicroseconds(int micros) override {
  47. assert(micros >= 0);
  48. assert(current_time_us_ + static_cast<uint64_t>(micros) >=
  49. current_time_us_);
  50. current_time_us_.fetch_add(micros);
  51. }
  52. void MockSleepForSeconds(int seconds) {
  53. assert(seconds >= 0);
  54. uint64_t micros = static_cast<uint64_t>(seconds) * kMicrosInSecond;
  55. assert(current_time_us_ + micros >= current_time_us_);
  56. current_time_us_.fetch_add(micros);
  57. }
  58. bool TimedWait(port::CondVar* cv,
  59. std::chrono::microseconds deadline) override {
  60. uint64_t now_micros = NowMicros();
  61. uint64_t deadline_micros = static_cast<uint64_t>(deadline.count());
  62. uint64_t delay_micros;
  63. if (deadline_micros > now_micros) {
  64. delay_micros = deadline_micros - now_micros;
  65. } else {
  66. delay_micros = 0;
  67. }
  68. // To prevent slowdown, this `TimedWait()` is completely synthetic. First,
  69. // it yields to coerce other threads to run while the lock is released.
  70. // Second, it randomly selects between mocking an immediate wakeup and a
  71. // timeout.
  72. cv->GetMutex()->Unlock();
  73. std::this_thread::yield();
  74. bool mock_timeout = Random::GetTLSInstance()->OneIn(2);
  75. if (mock_timeout) {
  76. TEST_SYNC_POINT("MockSystemClock::TimedWait:UnlockedPreSleep");
  77. current_time_us_.fetch_add(delay_micros);
  78. TEST_SYNC_POINT("MockSystemClock::TimedWait:UnlockedPostSleep1");
  79. TEST_SYNC_POINT("MockSystemClock::TimedWait:UnlockedPostSleep2");
  80. }
  81. cv->GetMutex()->Lock();
  82. return mock_timeout;
  83. }
  84. // TODO: this is a workaround for the different behavior on different platform
  85. // for timedwait timeout. Ideally timedwait API should be moved to env.
  86. // details: PR #7101.
  87. void InstallTimedWaitFixCallback();
  88. private:
  89. std::atomic<uint64_t> current_time_us_{0};
  90. static constexpr uint64_t kMicrosInSecond = 1000U * 1000U;
  91. };
  92. } // namespace ROCKSDB_NAMESPACE