]>
git.proxmox.com Git - ceph.git/blob - 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).
11 #include "rocksdb/system_clock.h"
13 namespace ROCKSDB_NAMESPACE
{
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
{
20 explicit MockSystemClock(const std::shared_ptr
<SystemClock
>& base
)
21 : SystemClockWrapper(base
) {}
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
);
31 virtual uint64_t NowSeconds() { return current_time_us_
/ kMicrosInSecond
; }
33 virtual uint64_t NowMicros() override
{ return current_time_us_
; }
35 virtual uint64_t NowNanos() override
{
36 assert(current_time_us_
<= std::numeric_limits
<uint64_t>::max() / 1000);
37 return current_time_us_
* 1000;
40 uint64_t RealNowMicros() { return target_
->NowMicros(); }
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
;
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
{
56 assert(current_time_us_
+ static_cast<uint64_t>(micros
) >=
58 current_time_us_
.fetch_add(micros
);
61 void MockSleepForSeconds(int seconds
) {
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
);
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.
71 void InstallTimedWaitFixCallback();
74 std::atomic
<uint64_t> current_time_us_
{0};
75 static constexpr uint64_t kMicrosInSecond
= 1000U * 1000U;
78 } // namespace ROCKSDB_NAMESPACE