summaryrefslogtreecommitdiff
path: root/common/libs/time/monotonic_time.h
blob: 8f5de132122ec966513e5e8c600de4c89a1398e2 (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * 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.
 */
#pragma once

#include <stdint.h>
#include <time.h>

namespace cvd {
namespace time {

static const int64_t kNanosecondsPerSecond = 1000000000;

class TimeDifference {
 public:
  TimeDifference(time_t seconds, long nanoseconds, int64_t scale) :
      scale_(scale), truncated_(false) {
    ts_.tv_sec = seconds;
    ts_.tv_nsec = nanoseconds;
    if (scale_ == kNanosecondsPerSecond) {
      truncated_ = true;
      truncated_ns_ = 0;
    }
  }

  TimeDifference(const TimeDifference& in, int64_t scale) :
      scale_(scale), truncated_(false) {
    ts_ = in.GetTS();
    if (scale_ == kNanosecondsPerSecond) {
      truncated_ = true;
      truncated_ns_ = 0;
    } else if ((in.scale_ % scale_) == 0) {
      truncated_ = true;
      truncated_ns_ = ts_.tv_nsec;
    }
  }

  TimeDifference(const struct timespec& in, int64_t scale) :
      ts_(in), scale_(scale), truncated_(false) { }

  TimeDifference operator*(const uint32_t factor) {
    TimeDifference rval = *this;
    rval.ts_.tv_sec = ts_.tv_sec * factor;
    // Create temporary variable to hold the multiplied
    // nanoseconds so that no overflow is possible.
    // Nanoseconds must be in [0, 10^9) and so all are less
    // then 2^30. Even multiplied by the largest uint32
    // this will fit in a 64-bit int without overflow.
    int64_t tv_nsec = static_cast<int64_t>(ts_.tv_nsec) * factor;
    rval.ts_.tv_sec += (tv_nsec / kNanosecondsPerSecond);
    rval.ts_.tv_nsec = tv_nsec % kNanosecondsPerSecond;
    return rval;
  }

  TimeDifference operator+(const TimeDifference& other) const {
    struct timespec ret = ts_;
    ret.tv_nsec = (ts_.tv_nsec + other.ts_.tv_nsec) % 1000000000;
    ret.tv_sec = (ts_.tv_sec + other.ts_.tv_sec) +
                  (ts_.tv_nsec + other.ts_.tv_nsec) / 1000000000;
    return TimeDifference(ret, scale_ < other.scale_ ? scale_: other.scale_);
  }

  TimeDifference operator-(const TimeDifference& other) const {
    struct timespec ret = ts_;
    // Keeps nanoseconds positive and allow negative numbers only on
    // seconds.
    ret.tv_nsec = (1000000000 + ts_.tv_nsec - other.ts_.tv_nsec) % 1000000000;
    ret.tv_sec = (ts_.tv_sec - other.ts_.tv_sec) -
                  (ts_.tv_nsec < other.ts_.tv_nsec ? 1 : 0);
    return TimeDifference(ret, scale_ < other.scale_ ? scale_: other.scale_);
  }

  bool operator<(const TimeDifference& other) const {
    return ts_.tv_sec < other.ts_.tv_sec ||
           (ts_.tv_sec == other.ts_.tv_sec && ts_.tv_nsec < other.ts_.tv_nsec);
  }

  int64_t count() const {
    return ts_.tv_sec * (kNanosecondsPerSecond / scale_) + ts_.tv_nsec / scale_;
  }

  time_t seconds() const {
    return ts_.tv_sec;
  }

  long subseconds_in_ns() const {
    if (!truncated_) {
      truncated_ns_ = (ts_.tv_nsec / scale_) * scale_;
      truncated_ = true;
    }
    return truncated_ns_;
  }

  struct timespec GetTS() const {
    // We can't assume C++11, so avoid extended initializer lists.
    struct timespec rval = { ts_.tv_sec, subseconds_in_ns()};
    return rval;
  }

 protected:
  struct timespec ts_;
  int64_t scale_;
  mutable bool truncated_;
  mutable long truncated_ns_;
};

class MonotonicTimePoint {
 public:
  static MonotonicTimePoint Now() {
    struct timespec ts;
#ifdef CLOCK_MONOTONIC_RAW
    // WARNING:
    // While we do have CLOCK_MONOTONIC_RAW, we can't depend on it until:
    // - ALL places relying on MonotonicTimePoint are fixed,
    // - pthread supports pthread_timewait_monotonic.
    //
    // This is currently observable as a LEGITIMATE problem while running
    // pthread_test. DO NOT revert this to CLOCK_MONOTONIC_RAW until test
    // passes.
    clock_gettime(CLOCK_MONOTONIC, &ts);
#else
    clock_gettime(CLOCK_MONOTONIC, &ts);
#endif
    return MonotonicTimePoint(ts);
  }

  MonotonicTimePoint() {
    ts_.tv_sec = 0;
    ts_.tv_nsec = 0;
  }

  explicit MonotonicTimePoint(const struct timespec& ts) {
    ts_ = ts;
  }

  TimeDifference SinceEpoch() const {
    return TimeDifference(ts_, 1);
  }

  TimeDifference operator-(const MonotonicTimePoint& other) const {
    struct timespec rval;
    rval.tv_sec = ts_.tv_sec - other.ts_.tv_sec;
    rval.tv_nsec = ts_.tv_nsec - other.ts_.tv_nsec;
    if (rval.tv_nsec < 0) {
      --rval.tv_sec;
      rval.tv_nsec += kNanosecondsPerSecond;
    }
    return TimeDifference(rval, 1);
  }

  MonotonicTimePoint operator+(const TimeDifference& other) const {
    MonotonicTimePoint rval = *this;
    rval.ts_.tv_sec += other.seconds();
    rval.ts_.tv_nsec += other.subseconds_in_ns();
    if (rval.ts_.tv_nsec >= kNanosecondsPerSecond) {
      ++rval.ts_.tv_sec;
      rval.ts_.tv_nsec -= kNanosecondsPerSecond;
    }
    return rval;
  }

  bool operator==(const MonotonicTimePoint& other) const {
    return (ts_.tv_sec == other.ts_.tv_sec) &&
        (ts_.tv_nsec == other.ts_.tv_nsec);
  }

  bool operator!=(const MonotonicTimePoint& other) const {
    return !(*this == other);
  }

  bool operator<(const MonotonicTimePoint& other) const {
    return ((ts_.tv_sec - other.ts_.tv_sec) < 0) ||
        ((ts_.tv_sec == other.ts_.tv_sec) &&
         (ts_.tv_nsec < other.ts_.tv_nsec));
  }

  bool operator>(const MonotonicTimePoint& other) const {
    return other < *this;
  }

  bool operator<=(const MonotonicTimePoint& other) const {
    return !(*this > other);
  }

  bool operator>=(const MonotonicTimePoint& other) const {
    return !(*this < other);
  }

  MonotonicTimePoint& operator+=(const TimeDifference& other) {
    ts_.tv_sec += other.seconds();
    ts_.tv_nsec += other.subseconds_in_ns();
    if (ts_.tv_nsec >= kNanosecondsPerSecond) {
      ++ts_.tv_sec;
      ts_.tv_nsec -= kNanosecondsPerSecond;
    }
    return *this;
  }

  MonotonicTimePoint& operator-=(const TimeDifference& other) {
    ts_.tv_sec -= other.seconds();
    ts_.tv_nsec -= other.subseconds_in_ns();
    if (ts_.tv_nsec < 0) {
      --ts_.tv_sec;
      ts_.tv_nsec += kNanosecondsPerSecond;
    }
    return *this;
  }

  void ToTimespec(struct timespec* dest) const {
    *dest = ts_;
  }

 protected:
  struct timespec ts_;
};

class MonotonicTimePointFactory {
 public:
  static MonotonicTimePointFactory* GetInstance();

  virtual ~MonotonicTimePointFactory() { }

  virtual void FetchCurrentTime(MonotonicTimePoint* dest) const {
    *dest = MonotonicTimePoint::Now();
  }
};

class Seconds : public TimeDifference {
 public:
  explicit Seconds(const TimeDifference& difference) :
      TimeDifference(difference, kNanosecondsPerSecond) { }

  Seconds(int64_t seconds) :
      TimeDifference(seconds, 0, kNanosecondsPerSecond) { }
};

class Milliseconds : public TimeDifference {
 public:
  explicit Milliseconds(const TimeDifference& difference) :
      TimeDifference(difference, kScale) { }

  Milliseconds(int64_t ms) : TimeDifference(
      ms / 1000, (ms % 1000) * kScale, kScale) { }

 protected:
  static const int kScale = kNanosecondsPerSecond / 1000;
};

class Microseconds : public TimeDifference {
 public:
  explicit Microseconds(const TimeDifference& difference) :
      TimeDifference(difference, kScale) { }

  Microseconds(int64_t micros) : TimeDifference(
      micros / 1000000, (micros % 1000000) * kScale, kScale) { }

 protected:
  static const int kScale = kNanosecondsPerSecond / 1000000;
};

class Nanoseconds : public TimeDifference {
 public:
  explicit Nanoseconds(const TimeDifference& difference) :
      TimeDifference(difference, 1) { }
  Nanoseconds(int64_t ns) : TimeDifference(ns / kNanosecondsPerSecond,
                                           ns % kNanosecondsPerSecond, 1) { }
};

}  // namespace time
}  // namespace cvd

/**
 * Legacy support for microseconds. Use MonotonicTimePoint in new code.
 */
static const int64_t kSecsToUsecs = static_cast<int64_t>(1000) * 1000;

static inline int64_t get_monotonic_usecs() {
  return cvd::time::Microseconds(
      cvd::time::MonotonicTimePoint::Now().SinceEpoch()).count();
}