]> git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/test_util/mock_time_env.h
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / rocksdb / test_util / mock_time_env.h
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
6 #pragma once
7
8 #include <atomic>
9 #include <limits>
10
11 #include "rocksdb/system_clock.h"
12
13 namespace ROCKSDB_NAMESPACE {
14
15 // NOTE: SpecialEnv offers most of this functionality, along with hooks
16 // for safe DB behavior under a mock time environment, so should be used
17 // instead of MockSystemClock for DB tests.
18 class MockSystemClock : public SystemClockWrapper {
19 public:
20 explicit MockSystemClock(const std::shared_ptr<SystemClock>& base)
21 : SystemClockWrapper(base) {}
22
23 static const char* kClassName() { return "MockSystemClock"; }
24 const char* Name() const override { return kClassName(); }
25 virtual Status GetCurrentTime(int64_t* time_sec) override {
26 assert(time_sec != nullptr);
27 *time_sec = static_cast<int64_t>(current_time_us_ / kMicrosInSecond);
28 return Status::OK();
29 }
30
31 virtual uint64_t NowSeconds() { return current_time_us_ / kMicrosInSecond; }
32
33 virtual uint64_t NowMicros() override { return current_time_us_; }
34
35 virtual uint64_t NowNanos() override {
36 assert(current_time_us_ <= std::numeric_limits<uint64_t>::max() / 1000);
37 return current_time_us_ * 1000;
38 }
39
40 uint64_t RealNowMicros() { return target_->NowMicros(); }
41
42 void SetCurrentTime(uint64_t time_sec) {
43 assert(time_sec < std::numeric_limits<uint64_t>::max() / kMicrosInSecond);
44 assert(time_sec * kMicrosInSecond >= current_time_us_);
45 current_time_us_ = time_sec * kMicrosInSecond;
46 }
47
48 // It's a fake sleep that just updates the Env current time, which is similar
49 // to `NoSleepEnv.SleepForMicroseconds()` and
50 // `SpecialEnv.MockSleepForMicroseconds()`.
51 // It's also similar to `set_current_time()`, which takes an absolute time in
52 // seconds, vs. this one takes the sleep in microseconds.
53 // Note: Not thread safe.
54 void SleepForMicroseconds(int micros) override {
55 assert(micros >= 0);
56 assert(current_time_us_ + static_cast<uint64_t>(micros) >=
57 current_time_us_);
58 current_time_us_.fetch_add(micros);
59 }
60
61 void MockSleepForSeconds(int seconds) {
62 assert(seconds >= 0);
63 uint64_t micros = static_cast<uint64_t>(seconds) * kMicrosInSecond;
64 assert(current_time_us_ + micros >= current_time_us_);
65 current_time_us_.fetch_add(micros);
66 }
67
68 // TODO: this is a workaround for the different behavior on different platform
69 // for timedwait timeout. Ideally timedwait API should be moved to env.
70 // details: PR #7101.
71 void InstallTimedWaitFixCallback();
72
73 private:
74 std::atomic<uint64_t> current_time_us_{0};
75 static constexpr uint64_t kMicrosInSecond = 1000U * 1000U;
76 };
77
78 } // namespace ROCKSDB_NAMESPACE