1 // Copyright 2013 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef V8_BASE_PLATFORM_TIME_H_
6 #define V8_BASE_PLATFORM_TIME_H_
12 #include "src/base/macros.h"
14 // Forward declarations.
28 // -----------------------------------------------------------------------------
31 // This class represents a duration of time, internally represented in
34 class TimeDelta final {
36 TimeDelta() : delta_(0) {}
38 // Converts units of time to TimeDeltas.
39 static TimeDelta FromDays(int days);
40 static TimeDelta FromHours(int hours);
41 static TimeDelta FromMinutes(int minutes);
42 static TimeDelta FromSeconds(int64_t seconds);
43 static TimeDelta FromMilliseconds(int64_t milliseconds);
44 static TimeDelta FromMicroseconds(int64_t microseconds) {
45 return TimeDelta(microseconds);
47 static TimeDelta FromNanoseconds(int64_t nanoseconds);
49 // Returns the time delta in some unit. The F versions return a floating
50 // point value, the "regular" versions return a rounded-down value.
52 // InMillisecondsRoundedUp() instead returns an integer that is rounded up
53 // to the next full millisecond.
56 int InMinutes() const;
57 double InSecondsF() const;
58 int64_t InSeconds() const;
59 double InMillisecondsF() const;
60 int64_t InMilliseconds() const;
61 int64_t InMillisecondsRoundedUp() const;
62 int64_t InMicroseconds() const { return delta_; }
63 int64_t InNanoseconds() const;
65 // Converts to/from Mach time specs.
66 static TimeDelta FromMachTimespec(struct mach_timespec ts);
67 struct mach_timespec ToMachTimespec() const;
69 // Converts to/from POSIX time specs.
70 static TimeDelta FromTimespec(struct timespec ts);
71 struct timespec ToTimespec() const;
73 TimeDelta& operator=(const TimeDelta& other) {
74 delta_ = other.delta_;
78 // Computations with other deltas.
79 TimeDelta operator+(const TimeDelta& other) const {
80 return TimeDelta(delta_ + other.delta_);
82 TimeDelta operator-(const TimeDelta& other) const {
83 return TimeDelta(delta_ - other.delta_);
86 TimeDelta& operator+=(const TimeDelta& other) {
87 delta_ += other.delta_;
90 TimeDelta& operator-=(const TimeDelta& other) {
91 delta_ -= other.delta_;
94 TimeDelta operator-() const {
95 return TimeDelta(-delta_);
98 double TimesOf(const TimeDelta& other) const {
99 return static_cast<double>(delta_) / static_cast<double>(other.delta_);
101 double PercentOf(const TimeDelta& other) const {
102 return TimesOf(other) * 100.0;
105 // Computations with ints, note that we only allow multiplicative operations
106 // with ints, and additive operations with other deltas.
107 TimeDelta operator*(int64_t a) const {
108 return TimeDelta(delta_ * a);
110 TimeDelta operator/(int64_t a) const {
111 return TimeDelta(delta_ / a);
113 TimeDelta& operator*=(int64_t a) {
117 TimeDelta& operator/=(int64_t a) {
121 int64_t operator/(const TimeDelta& other) const {
122 return delta_ / other.delta_;
125 // Comparison operators.
126 bool operator==(const TimeDelta& other) const {
127 return delta_ == other.delta_;
129 bool operator!=(const TimeDelta& other) const {
130 return delta_ != other.delta_;
132 bool operator<(const TimeDelta& other) const {
133 return delta_ < other.delta_;
135 bool operator<=(const TimeDelta& other) const {
136 return delta_ <= other.delta_;
138 bool operator>(const TimeDelta& other) const {
139 return delta_ > other.delta_;
141 bool operator>=(const TimeDelta& other) const {
142 return delta_ >= other.delta_;
146 // Constructs a delta given the duration in microseconds. This is private
147 // to avoid confusion by callers with an integer constructor. Use
148 // FromSeconds, FromMilliseconds, etc. instead.
149 explicit TimeDelta(int64_t delta) : delta_(delta) {}
151 // Delta in microseconds.
156 // -----------------------------------------------------------------------------
159 // This class represents an absolute point in time, internally represented as
160 // microseconds (s/1,000,000) since 00:00:00 UTC, January 1, 1970.
164 static const int64_t kMillisecondsPerSecond = 1000;
165 static const int64_t kMicrosecondsPerMillisecond = 1000;
166 static const int64_t kMicrosecondsPerSecond = kMicrosecondsPerMillisecond *
167 kMillisecondsPerSecond;
168 static const int64_t kMicrosecondsPerMinute = kMicrosecondsPerSecond * 60;
169 static const int64_t kMicrosecondsPerHour = kMicrosecondsPerMinute * 60;
170 static const int64_t kMicrosecondsPerDay = kMicrosecondsPerHour * 24;
171 static const int64_t kMicrosecondsPerWeek = kMicrosecondsPerDay * 7;
172 static const int64_t kNanosecondsPerMicrosecond = 1000;
173 static const int64_t kNanosecondsPerSecond = kNanosecondsPerMicrosecond *
174 kMicrosecondsPerSecond;
176 // Contains the NULL time. Use Time::Now() to get the current time.
179 // Returns true if the time object has not been initialized.
180 bool IsNull() const { return us_ == 0; }
182 // Returns true if the time object is the maximum time.
183 bool IsMax() const { return us_ == std::numeric_limits<int64_t>::max(); }
186 static void SetTimeOffset(double time_offset);
188 // Returns the current time. Watch out, the system might adjust its clock
189 // in which case time will actually go backwards. We don't guarantee that
190 // times are increasing, or that two calls to Now() won't be the same.
193 // Returns the current time. Same as Now() except that this function always
194 // uses system time so that there are no discrepancies between the returned
195 // time and system time even on virtual environments including our test bot.
196 // For timing sensitive unittests, this function should be used.
197 static Time NowFromSystemTime();
199 // Returns the time for epoch in Unix-like system (Jan 1, 1970).
200 static Time UnixEpoch() { return Time(0); }
202 // Returns the maximum time, which should be greater than any reasonable time
203 // with which we might compare it.
204 static Time Max() { return Time(std::numeric_limits<int64_t>::max()); }
206 // Converts to/from internal values. The meaning of the "internal value" is
207 // completely up to the implementation, so it should be treated as opaque.
208 static Time FromInternalValue(int64_t value) {
211 int64_t ToInternalValue() const {
215 // Converts to/from POSIX time specs.
216 static Time FromTimespec(struct timespec ts);
217 struct timespec ToTimespec() const;
219 // Converts to/from POSIX time values.
220 static Time FromTimeval(struct timeval tv);
221 struct timeval ToTimeval() const;
223 // Converts to/from Windows file times.
224 static Time FromFiletime(struct _FILETIME ft);
225 struct _FILETIME ToFiletime() const;
227 // Converts to/from the Javascript convention for times, a number of
228 // milliseconds since the epoch:
229 static Time FromJsTime(double ms_since_epoch);
230 double ToJsTime() const;
232 Time& operator=(const Time& other) {
237 // Compute the difference between two times.
238 TimeDelta operator-(const Time& other) const {
239 return TimeDelta::FromMicroseconds(us_ - other.us_);
242 // Modify by some time delta.
243 Time& operator+=(const TimeDelta& delta) {
244 us_ += delta.InMicroseconds();
247 Time& operator-=(const TimeDelta& delta) {
248 us_ -= delta.InMicroseconds();
252 // Return a new time modified by some delta.
253 Time operator+(const TimeDelta& delta) const {
254 return Time(us_ + delta.InMicroseconds());
256 Time operator-(const TimeDelta& delta) const {
257 return Time(us_ - delta.InMicroseconds());
260 // Comparison operators
261 bool operator==(const Time& other) const {
262 return us_ == other.us_;
264 bool operator!=(const Time& other) const {
265 return us_ != other.us_;
267 bool operator<(const Time& other) const {
268 return us_ < other.us_;
270 bool operator<=(const Time& other) const {
271 return us_ <= other.us_;
273 bool operator>(const Time& other) const {
274 return us_ > other.us_;
276 bool operator>=(const Time& other) const {
277 return us_ >= other.us_;
281 explicit Time(int64_t us) : us_(us) {}
283 // Time in microseconds in UTC.
287 std::ostream& operator<<(std::ostream&, const Time&);
289 inline Time operator+(const TimeDelta& delta, const Time& time) {
294 // -----------------------------------------------------------------------------
297 // This class represents an abstract time that is most of the time incrementing
298 // for use in measuring time durations. It is internally represented in
299 // microseconds. It can not be converted to a human-readable time, but is
300 // guaranteed not to decrease (if the user changes the computer clock,
301 // Time::Now() may actually decrease or jump). But note that TimeTicks may
302 // "stand still", for example if the computer suspended.
304 class TimeTicks final {
306 TimeTicks() : ticks_(0) {}
308 // Platform-dependent tick count representing "right now."
309 // The resolution of this clock is ~1-15ms. Resolution varies depending
310 // on hardware/operating system configuration.
311 // This method never returns a null TimeTicks.
312 static TimeTicks Now();
314 // Returns a platform-dependent high-resolution tick count. Implementation
315 // is hardware dependent and may or may not return sub-millisecond
316 // resolution. THIS CALL IS GENERALLY MUCH MORE EXPENSIVE THAN Now() AND
317 // SHOULD ONLY BE USED WHEN IT IS REALLY NEEDED.
318 // This method never returns a null TimeTicks.
319 static TimeTicks HighResolutionNow();
321 // Returns true if the high-resolution clock is working on this system.
322 static bool IsHighResolutionClockWorking();
324 // Returns Linux kernel timestamp for generating profiler events. This method
325 // returns null TimeTicks if the kernel cannot provide the timestamps (e.g.,
326 // on non-Linux OS or if the kernel module for timestamps is not loaded).
328 static TimeTicks KernelTimestampNow();
329 static bool KernelTimestampAvailable();
331 // Returns true if this object has not been initialized.
332 bool IsNull() const { return ticks_ == 0; }
334 // Converts to/from internal values. The meaning of the "internal value" is
335 // completely up to the implementation, so it should be treated as opaque.
336 static TimeTicks FromInternalValue(int64_t value) {
337 return TimeTicks(value);
339 int64_t ToInternalValue() const {
343 TimeTicks& operator=(const TimeTicks other) {
344 ticks_ = other.ticks_;
348 // Compute the difference between two times.
349 TimeDelta operator-(const TimeTicks other) const {
350 return TimeDelta::FromMicroseconds(ticks_ - other.ticks_);
353 // Modify by some time delta.
354 TimeTicks& operator+=(const TimeDelta& delta) {
355 ticks_ += delta.InMicroseconds();
358 TimeTicks& operator-=(const TimeDelta& delta) {
359 ticks_ -= delta.InMicroseconds();
363 // Return a new TimeTicks modified by some delta.
364 TimeTicks operator+(const TimeDelta& delta) const {
365 return TimeTicks(ticks_ + delta.InMicroseconds());
367 TimeTicks operator-(const TimeDelta& delta) const {
368 return TimeTicks(ticks_ - delta.InMicroseconds());
371 // Comparison operators
372 bool operator==(const TimeTicks& other) const {
373 return ticks_ == other.ticks_;
375 bool operator!=(const TimeTicks& other) const {
376 return ticks_ != other.ticks_;
378 bool operator<(const TimeTicks& other) const {
379 return ticks_ < other.ticks_;
381 bool operator<=(const TimeTicks& other) const {
382 return ticks_ <= other.ticks_;
384 bool operator>(const TimeTicks& other) const {
385 return ticks_ > other.ticks_;
387 bool operator>=(const TimeTicks& other) const {
388 return ticks_ >= other.ticks_;
392 // Please use Now() to create a new object. This is for internal use
393 // and testing. Ticks is in microseconds.
394 explicit TimeTicks(int64_t ticks) : ticks_(ticks) {}
396 // Tick count in microseconds.
400 inline TimeTicks operator+(const TimeDelta& delta, const TimeTicks& ticks) {
401 return ticks + delta;
407 #endif // V8_BASE_PLATFORM_TIME_H_