Fix typo in spec file
[platform/upstream/v8.git] / src / base / platform / time.h
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.
4
5 #ifndef V8_BASE_PLATFORM_TIME_H_
6 #define V8_BASE_PLATFORM_TIME_H_
7
8 #include <ctime>
9 #include <iosfwd>
10 #include <limits>
11
12 #include "src/base/macros.h"
13
14 // Forward declarations.
15 extern "C" {
16 struct _FILETIME;
17 struct mach_timespec;
18 struct timespec;
19 struct timeval;
20 }
21
22 namespace v8 {
23 namespace base {
24
25 class Time;
26 class TimeTicks;
27
28 // -----------------------------------------------------------------------------
29 // TimeDelta
30 //
31 // This class represents a duration of time, internally represented in
32 // microseonds.
33
34 class TimeDelta final {
35  public:
36   TimeDelta() : delta_(0) {}
37
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);
46   }
47   static TimeDelta FromNanoseconds(int64_t nanoseconds);
48
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.
51   //
52   // InMillisecondsRoundedUp() instead returns an integer that is rounded up
53   // to the next full millisecond.
54   int InDays() const;
55   int InHours() const;
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;
64
65   // Converts to/from Mach time specs.
66   static TimeDelta FromMachTimespec(struct mach_timespec ts);
67   struct mach_timespec ToMachTimespec() const;
68
69   // Converts to/from POSIX time specs.
70   static TimeDelta FromTimespec(struct timespec ts);
71   struct timespec ToTimespec() const;
72
73   TimeDelta& operator=(const TimeDelta& other) {
74     delta_ = other.delta_;
75     return *this;
76   }
77
78   // Computations with other deltas.
79   TimeDelta operator+(const TimeDelta& other) const {
80     return TimeDelta(delta_ + other.delta_);
81   }
82   TimeDelta operator-(const TimeDelta& other) const {
83     return TimeDelta(delta_ - other.delta_);
84   }
85
86   TimeDelta& operator+=(const TimeDelta& other) {
87     delta_ += other.delta_;
88     return *this;
89   }
90   TimeDelta& operator-=(const TimeDelta& other) {
91     delta_ -= other.delta_;
92     return *this;
93   }
94   TimeDelta operator-() const {
95     return TimeDelta(-delta_);
96   }
97
98   double TimesOf(const TimeDelta& other) const {
99     return static_cast<double>(delta_) / static_cast<double>(other.delta_);
100   }
101   double PercentOf(const TimeDelta& other) const {
102     return TimesOf(other) * 100.0;
103   }
104
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);
109   }
110   TimeDelta operator/(int64_t a) const {
111     return TimeDelta(delta_ / a);
112   }
113   TimeDelta& operator*=(int64_t a) {
114     delta_ *= a;
115     return *this;
116   }
117   TimeDelta& operator/=(int64_t a) {
118     delta_ /= a;
119     return *this;
120   }
121   int64_t operator/(const TimeDelta& other) const {
122     return delta_ / other.delta_;
123   }
124
125   // Comparison operators.
126   bool operator==(const TimeDelta& other) const {
127     return delta_ == other.delta_;
128   }
129   bool operator!=(const TimeDelta& other) const {
130     return delta_ != other.delta_;
131   }
132   bool operator<(const TimeDelta& other) const {
133     return delta_ < other.delta_;
134   }
135   bool operator<=(const TimeDelta& other) const {
136     return delta_ <= other.delta_;
137   }
138   bool operator>(const TimeDelta& other) const {
139     return delta_ > other.delta_;
140   }
141   bool operator>=(const TimeDelta& other) const {
142     return delta_ >= other.delta_;
143   }
144
145  private:
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) {}
150
151   // Delta in microseconds.
152   int64_t delta_;
153 };
154
155
156 // -----------------------------------------------------------------------------
157 // Time
158 //
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.
161
162 class Time final {
163  public:
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;
175
176   // Contains the NULL time. Use Time::Now() to get the current time.
177   Time() : us_(0) {}
178
179   // Returns true if the time object has not been initialized.
180   bool IsNull() const { return us_ == 0; }
181
182   // Returns true if the time object is the maximum time.
183   bool IsMax() const { return us_ == std::numeric_limits<int64_t>::max(); }
184
185   // Sets time offset
186   static void SetTimeOffset(double time_offset);
187
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.
191   static Time Now();
192
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();
198
199   // Returns the time for epoch in Unix-like system (Jan 1, 1970).
200   static Time UnixEpoch() { return Time(0); }
201
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()); }
205
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) {
209     return Time(value);
210   }
211   int64_t ToInternalValue() const {
212     return us_;
213   }
214
215   // Converts to/from POSIX time specs.
216   static Time FromTimespec(struct timespec ts);
217   struct timespec ToTimespec() const;
218
219   // Converts to/from POSIX time values.
220   static Time FromTimeval(struct timeval tv);
221   struct timeval ToTimeval() const;
222
223   // Converts to/from Windows file times.
224   static Time FromFiletime(struct _FILETIME ft);
225   struct _FILETIME ToFiletime() const;
226
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;
231
232   Time& operator=(const Time& other) {
233     us_ = other.us_;
234     return *this;
235   }
236
237   // Compute the difference between two times.
238   TimeDelta operator-(const Time& other) const {
239     return TimeDelta::FromMicroseconds(us_ - other.us_);
240   }
241
242   // Modify by some time delta.
243   Time& operator+=(const TimeDelta& delta) {
244     us_ += delta.InMicroseconds();
245     return *this;
246   }
247   Time& operator-=(const TimeDelta& delta) {
248     us_ -= delta.InMicroseconds();
249     return *this;
250   }
251
252   // Return a new time modified by some delta.
253   Time operator+(const TimeDelta& delta) const {
254     return Time(us_ + delta.InMicroseconds());
255   }
256   Time operator-(const TimeDelta& delta) const {
257     return Time(us_ - delta.InMicroseconds());
258   }
259
260   // Comparison operators
261   bool operator==(const Time& other) const {
262     return us_ == other.us_;
263   }
264   bool operator!=(const Time& other) const {
265     return us_ != other.us_;
266   }
267   bool operator<(const Time& other) const {
268     return us_ < other.us_;
269   }
270   bool operator<=(const Time& other) const {
271     return us_ <= other.us_;
272   }
273   bool operator>(const Time& other) const {
274     return us_ > other.us_;
275   }
276   bool operator>=(const Time& other) const {
277     return us_ >= other.us_;
278   }
279
280  private:
281   explicit Time(int64_t us) : us_(us) {}
282
283   // Time in microseconds in UTC.
284   int64_t us_;
285 };
286
287 std::ostream& operator<<(std::ostream&, const Time&);
288
289 inline Time operator+(const TimeDelta& delta, const Time& time) {
290   return time + delta;
291 }
292
293
294 // -----------------------------------------------------------------------------
295 // TimeTicks
296 //
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.
303
304 class TimeTicks final {
305  public:
306   TimeTicks() : ticks_(0) {}
307
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();
313
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();
320
321   // Returns true if the high-resolution clock is working on this system.
322   static bool IsHighResolutionClockWorking();
323
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).
327
328   static TimeTicks KernelTimestampNow();
329   static bool KernelTimestampAvailable();
330
331   // Returns true if this object has not been initialized.
332   bool IsNull() const { return ticks_ == 0; }
333
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);
338   }
339   int64_t ToInternalValue() const {
340     return ticks_;
341   }
342
343   TimeTicks& operator=(const TimeTicks other) {
344     ticks_ = other.ticks_;
345     return *this;
346   }
347
348   // Compute the difference between two times.
349   TimeDelta operator-(const TimeTicks other) const {
350     return TimeDelta::FromMicroseconds(ticks_ - other.ticks_);
351   }
352
353   // Modify by some time delta.
354   TimeTicks& operator+=(const TimeDelta& delta) {
355     ticks_ += delta.InMicroseconds();
356     return *this;
357   }
358   TimeTicks& operator-=(const TimeDelta& delta) {
359     ticks_ -= delta.InMicroseconds();
360     return *this;
361   }
362
363   // Return a new TimeTicks modified by some delta.
364   TimeTicks operator+(const TimeDelta& delta) const {
365     return TimeTicks(ticks_ + delta.InMicroseconds());
366   }
367   TimeTicks operator-(const TimeDelta& delta) const {
368     return TimeTicks(ticks_ - delta.InMicroseconds());
369   }
370
371   // Comparison operators
372   bool operator==(const TimeTicks& other) const {
373     return ticks_ == other.ticks_;
374   }
375   bool operator!=(const TimeTicks& other) const {
376     return ticks_ != other.ticks_;
377   }
378   bool operator<(const TimeTicks& other) const {
379     return ticks_ < other.ticks_;
380   }
381   bool operator<=(const TimeTicks& other) const {
382     return ticks_ <= other.ticks_;
383   }
384   bool operator>(const TimeTicks& other) const {
385     return ticks_ > other.ticks_;
386   }
387   bool operator>=(const TimeTicks& other) const {
388     return ticks_ >= other.ticks_;
389   }
390
391  private:
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) {}
395
396   // Tick count in microseconds.
397   int64_t ticks_;
398 };
399
400 inline TimeTicks operator+(const TimeDelta& delta, const TimeTicks& ticks) {
401   return ticks + delta;
402 }
403
404 }  // namespace base
405 }  // namespace v8
406
407 #endif  // V8_BASE_PLATFORM_TIME_H_