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 // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
6 // Use of this source code is governed by a BSD-style license that can be
7 // found in the LICENSE file. See the AUTHORS file for names of contributors.
8 
9 #pragma once
10 #include <rocksdb/rocksdb_namespace.h>
11 #include <rocksdb/status.h>
12 #include <stdint.h>
13 
14 #include <memory>
15 
16 #ifdef _WIN32
17 // Windows API macro interference
18 #undef GetCurrentTime
19 #endif
20 
21 namespace ROCKSDB_NAMESPACE {
22 struct ConfigOptions;
23 
24 // A SystemClock is an interface used by the rocksdb implementation to access
25 // operating system time-related functionality.
26 class SystemClock {
27  public:
~SystemClock()28   virtual ~SystemClock() {}
29 
Type()30   static const char* Type() { return "SystemClock"; }
31 
32   // The name of this system clock
33   virtual const char* Name() const = 0;
34 
35   // Return a default SystemClock suitable for the current operating
36   // system.
37   static const std::shared_ptr<SystemClock>& Default();
38 
39   // Returns the number of micro-seconds since some fixed point in time.
40   // It is often used as system time such as in GenericRateLimiter
41   // and other places so a port needs to return system time in order to work.
42   virtual uint64_t NowMicros() = 0;
43 
44   // Returns the number of nano-seconds since some fixed point in time. Only
45   // useful for computing deltas of time in one run.
46   // Default implementation simply relies on NowMicros.
47   // In platform-specific implementations, NowNanos() should return time points
48   // that are MONOTONIC.
NowNanos()49   virtual uint64_t NowNanos() { return NowMicros() * 1000; }
50 
51   // Returns the number of micro-seconds of CPU time used by the current thread.
52   // 0 indicates not supported.
CPUMicros()53   virtual uint64_t CPUMicros() { return 0; }
54 
55   // Returns the number of nano-seconds of CPU time used by the current thread.
56   // Default implementation simply relies on CPUMicros.
57   // 0 indicates not supported.
CPUNanos()58   virtual uint64_t CPUNanos() { return CPUMicros() * 1000; }
59 
60   // Sleep/delay the thread for the prescribed number of micro-seconds.
61   virtual void SleepForMicroseconds(int micros) = 0;
62 
63   // Get the number of seconds since the Epoch, 1970-01-01 00:00:00 (UTC).
64   // Only overwrites *unix_time on success.
65   virtual Status GetCurrentTime(int64_t* unix_time) = 0;
66 
67   // Converts seconds-since-Jan-01-1970 to a printable string
68   virtual std::string TimeToString(uint64_t time) = 0;
69 };
70 
71 // Wrapper class for a SystemClock.  Redirects all methods (except Name)
72 // of the SystemClock interface to the target/wrapped class.
73 class SystemClockWrapper : public SystemClock {
74  public:
SystemClockWrapper(const std::shared_ptr<SystemClock> & t)75   explicit SystemClockWrapper(const std::shared_ptr<SystemClock>& t)
76       : target_(t) {}
77 
NowMicros()78   uint64_t NowMicros() override { return target_->NowMicros(); }
79 
NowNanos()80   uint64_t NowNanos() override { return target_->NowNanos(); }
81 
CPUMicros()82   uint64_t CPUMicros() override { return target_->CPUMicros(); }
83 
CPUNanos()84   uint64_t CPUNanos() override { return target_->CPUNanos(); }
85 
SleepForMicroseconds(int micros)86   virtual void SleepForMicroseconds(int micros) override {
87     return target_->SleepForMicroseconds(micros);
88   }
89 
GetCurrentTime(int64_t * unix_time)90   Status GetCurrentTime(int64_t* unix_time) override {
91     return target_->GetCurrentTime(unix_time);
92   }
93 
TimeToString(uint64_t time)94   std::string TimeToString(uint64_t time) override {
95     return target_->TimeToString(time);
96   }
97 
98  protected:
99   std::shared_ptr<SystemClock> target_;
100 };
101 
102 }  // end namespace ROCKSDB_NAMESPACE
103