aboutsummaryrefslogtreecommitdiff
path: root/icing/util/clock.h
blob: d987a4c4810085c569b0daf771024cb0d0477b93 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright (C) 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef ICING_UTIL_CLOCK_H_
#define ICING_UTIL_CLOCK_H_

#include <cstdint>
#include <functional>
#include <memory>

namespace icing {
namespace lib {

// Returns the current steady time in nanoseconds. The steady clock is different
// from the system clock. It's monotonic and never returns a lower value than a
// previous call, while a system clock can be occasionally adjusted.
int64_t GetSteadyTimeNanoseconds();

// Returns the current steady time in Milliseconds. The steady clock is
// different from the system clock. It's monotonic and never returns a lower
// value than a previous call, while a system clock can be occasionally
// adjusted.
int64_t GetSteadyTimeMilliseconds();

// Used to calculate the elapsed time.
class Timer {
 public:
  // Creates and starts the timer.
  Timer() : start_timestamp_nanoseconds_(GetSteadyTimeNanoseconds()) {}

  virtual ~Timer() = default;

  // Returns the elapsed time from when timer started.
  virtual int64_t GetElapsedMilliseconds() const {
    return GetElapsedNanoseconds() / 1000000;
  }

  // Returns the elapsed time from when timer started.
  virtual int64_t GetElapsedNanoseconds() const {
    return GetSteadyTimeNanoseconds() - start_timestamp_nanoseconds_;
  }

 private:
  int64_t start_timestamp_nanoseconds_;
};

// Wrapper around real-time clock functions. This is separated primarily so
// tests can override this clock and inject it into the class under test.
class Clock {
 public:
  virtual ~Clock() = default;

  // Returns the current time in milliseconds, it's guaranteed that the return
  // value is non-negative.
  virtual int64_t GetSystemTimeMilliseconds() const;

  // Returns a timer used to calculate the elapsed time. The timer starts when
  // the method returns.
  virtual std::unique_ptr<Timer> GetNewTimer() const;
};

// A convenient RAII timer class that receives a callback. Upon destruction, the
// callback will be called with the elapsed milliseconds or nanoseconds passed
// as a parameter, depending on which Unit was passed in the constructor.
class ScopedTimer {
 public:
  enum class Unit { kMillisecond, kNanosecond };

  ScopedTimer(std::unique_ptr<Timer> timer,
              std::function<void(int64_t)> callback,
              Unit unit = Unit::kMillisecond)
      : timer_(std::move(timer)), callback_(std::move(callback)), unit_(unit) {}

  ~ScopedTimer() {
    if (unit_ == Unit::kMillisecond) {
      callback_(timer_->GetElapsedMilliseconds());
    } else {
      callback_(timer_->GetElapsedNanoseconds());
    }
  }

  const Timer& timer() const { return *timer_; }

 private:
  std::unique_ptr<Timer> timer_;
  std::function<void(int64_t)> callback_;
  Unit unit_;
};

}  // namespace lib
}  // namespace icing

#endif  // ICING_UTIL_CLOCK_H_