Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(417)

Side by Side Diff: src/platform/time.h

Issue 23490015: Reland^2 "Add Chromium-style TimeDelta, Time and TimeTicks classes, and a new ElapsedTimer class." (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Some windows cleanup Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/platform/elapsed-timer.h ('k') | src/platform/time.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2013 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
4 // met:
5 //
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28 #ifndef V8_PLATFORM_TIME_H_
29 #define V8_PLATFORM_TIME_H_
30
31 #include <ctime>
32 #include <limits>
33
34 #include "allocation.h"
35
36 // Forward declarations.
37 extern "C" {
38 struct _FILETIME;
39 struct timeval;
40 }
41
42 namespace v8 {
43 namespace internal {
44
45 class Time;
46 class TimeTicks;
47
48 // -----------------------------------------------------------------------------
49 // TimeDelta
50 //
51 // This class represents a duration of time, internally represented in
52 // microseonds.
53
54 class TimeDelta V8_FINAL BASE_EMBEDDED {
55 public:
56 TimeDelta() : delta_(0) {}
57
58 // Converts units of time to TimeDeltas.
59 static TimeDelta FromDays(int days);
60 static TimeDelta FromHours(int hours);
61 static TimeDelta FromMinutes(int minutes);
62 static TimeDelta FromSeconds(int64_t seconds);
63 static TimeDelta FromMilliseconds(int64_t milliseconds);
64 static TimeDelta FromMicroseconds(int64_t microseconds) {
65 return TimeDelta(microseconds);
66 }
67 static TimeDelta FromNanoseconds(int64_t nanoseconds);
68
69 // Returns the time delta in some unit. The F versions return a floating
70 // point value, the "regular" versions return a rounded-down value.
71 //
72 // InMillisecondsRoundedUp() instead returns an integer that is rounded up
73 // to the next full millisecond.
74 int InDays() const;
75 int InHours() const;
76 int InMinutes() const;
77 double InSecondsF() const;
78 int64_t InSeconds() const;
79 double InMillisecondsF() const;
80 int64_t InMilliseconds() const;
81 int64_t InMillisecondsRoundedUp() const;
82 int64_t InMicroseconds() const { return delta_; }
83 int64_t InNanoseconds() const;
84
85 TimeDelta& operator=(const TimeDelta& other) {
86 delta_ = other.delta_;
87 return *this;
88 }
89
90 // Computations with other deltas.
91 TimeDelta operator+(const TimeDelta& other) const {
92 return TimeDelta(delta_ + other.delta_);
93 }
94 TimeDelta operator-(const TimeDelta& other) const {
95 return TimeDelta(delta_ - other.delta_);
96 }
97
98 TimeDelta& operator+=(const TimeDelta& other) {
99 delta_ += other.delta_;
100 return *this;
101 }
102 TimeDelta& operator-=(const TimeDelta& other) {
103 delta_ -= other.delta_;
104 return *this;
105 }
106 TimeDelta operator-() const {
107 return TimeDelta(-delta_);
108 }
109
110 double TimesOf(const TimeDelta& other) const {
111 return static_cast<double>(delta_) / static_cast<double>(other.delta_);
112 }
113 double PercentOf(const TimeDelta& other) const {
114 return TimesOf(other) * 100.0;
115 }
116
117 // Computations with ints, note that we only allow multiplicative operations
118 // with ints, and additive operations with other deltas.
119 TimeDelta operator*(int64_t a) const {
120 return TimeDelta(delta_ * a);
121 }
122 TimeDelta operator/(int64_t a) const {
123 return TimeDelta(delta_ / a);
124 }
125 TimeDelta& operator*=(int64_t a) {
126 delta_ *= a;
127 return *this;
128 }
129 TimeDelta& operator/=(int64_t a) {
130 delta_ /= a;
131 return *this;
132 }
133 int64_t operator/(const TimeDelta& other) const {
134 return delta_ / other.delta_;
135 }
136
137 // Comparison operators.
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 bool operator<(const TimeDelta& other) const {
145 return delta_ < other.delta_;
146 }
147 bool operator<=(const TimeDelta& other) const {
148 return delta_ <= other.delta_;
149 }
150 bool operator>(const TimeDelta& other) const {
151 return delta_ > other.delta_;
152 }
153 bool operator>=(const TimeDelta& other) const {
154 return delta_ >= other.delta_;
155 }
156
157 private:
158 // Constructs a delta given the duration in microseconds. This is private
159 // to avoid confusion by callers with an integer constructor. Use
160 // FromSeconds, FromMilliseconds, etc. instead.
161 explicit TimeDelta(int64_t delta) : delta_(delta) {}
162
163 // Delta in microseconds.
164 int64_t delta_;
165 };
166
167
168 // -----------------------------------------------------------------------------
169 // Time
170 //
171 // This class represents an absolute point in time, internally represented as
172 // microseconds (s/1,000,000) since 00:00:00 UTC, January 1, 1970.
173
174 class Time V8_FINAL BASE_EMBEDDED {
175 public:
176 static const int64_t kMillisecondsPerSecond = 1000;
177 static const int64_t kMicrosecondsPerMillisecond = 1000;
178 static const int64_t kMicrosecondsPerSecond = kMicrosecondsPerMillisecond *
179 kMillisecondsPerSecond;
180 static const int64_t kMicrosecondsPerMinute = kMicrosecondsPerSecond * 60;
181 static const int64_t kMicrosecondsPerHour = kMicrosecondsPerMinute * 60;
182 static const int64_t kMicrosecondsPerDay = kMicrosecondsPerHour * 24;
183 static const int64_t kMicrosecondsPerWeek = kMicrosecondsPerDay * 7;
184 static const int64_t kNanosecondsPerMicrosecond = 1000;
185 static const int64_t kNanosecondsPerSecond = kNanosecondsPerMicrosecond *
186 kMicrosecondsPerSecond;
187
188 // Contains the NULL time. Use Time::Now() to get the current time.
189 Time() : us_(0) {}
190
191 // Returns true if the time object has not been initialized.
192 bool IsNull() const { return us_ == 0; }
193
194 // Returns true if the time object is the maximum time.
195 bool IsMax() const { return us_ == std::numeric_limits<int64_t>::max(); }
196
197 // Returns the current time. Watch out, the system might adjust its clock
198 // in which case time will actually go backwards. We don't guarantee that
199 // times are increasing, or that two calls to Now() won't be the same.
200 static Time Now();
201
202 // Returns the current time. Same as Now() except that this function always
203 // uses system time so that there are no discrepancies between the returned
204 // time and system time even on virtual environments including our test bot.
205 // For timing sensitive unittests, this function should be used.
206 static Time NowFromSystemTime();
207
208 // Returns the time for epoch in Unix-like system (Jan 1, 1970).
209 static Time UnixEpoch() { return Time(0); }
210
211 // Returns the maximum time, which should be greater than any reasonable time
212 // with which we might compare it.
213 static Time Max() { return Time(std::numeric_limits<int64_t>::max()); }
214
215 // Converts to/from POSIX time values.
216 static Time FromTimeval(struct timeval tv);
217 struct timeval ToTimeval() const;
218
219 // Converts to/from Windows file times.
220 static Time FromFiletime(struct _FILETIME ft);
221 struct _FILETIME ToFiletime() const;
222
223 // Converts to/from the Javascript convention for times, a number of
224 // milliseconds since the epoch:
225 static Time FromJsTime(double ms_since_epoch);
226 double ToJsTime() const;
227
228 Time& operator=(const Time& other) {
229 us_ = other.us_;
230 return *this;
231 }
232
233 // Compute the difference between two times.
234 TimeDelta operator-(const Time& other) const {
235 return TimeDelta::FromMicroseconds(us_ - other.us_);
236 }
237
238 // Modify by some time delta.
239 Time& operator+=(const TimeDelta& delta) {
240 us_ += delta.InMicroseconds();
241 return *this;
242 }
243 Time& operator-=(const TimeDelta& delta) {
244 us_ -= delta.InMicroseconds();
245 return *this;
246 }
247
248 // Return a new time modified by some delta.
249 Time operator+(const TimeDelta& delta) const {
250 return Time(us_ + delta.InMicroseconds());
251 }
252 Time operator-(const TimeDelta& delta) const {
253 return Time(us_ - delta.InMicroseconds());
254 }
255
256 // Comparison operators
257 bool operator==(const Time& other) const {
258 return us_ == other.us_;
259 }
260 bool operator!=(const Time& other) const {
261 return us_ != other.us_;
262 }
263 bool operator<(const Time& other) const {
264 return us_ < other.us_;
265 }
266 bool operator<=(const Time& other) const {
267 return us_ <= other.us_;
268 }
269 bool operator>(const Time& other) const {
270 return us_ > other.us_;
271 }
272 bool operator>=(const Time& other) const {
273 return us_ >= other.us_;
274 }
275
276 private:
277 explicit Time(int64_t us) : us_(us) {}
278
279 // Time in microseconds in UTC.
280 int64_t us_;
281 };
282
283 inline Time operator+(const TimeDelta& delta, const Time& time) {
284 return time + delta;
285 }
286
287
288 // -----------------------------------------------------------------------------
289 // TimeTicks
290 //
291 // This class represents an abstract time that is most of the time incrementing
292 // for use in measuring time durations. It is internally represented in
293 // microseconds. It can not be converted to a human-readable time, but is
294 // guaranteed not to decrease (if the user changes the computer clock,
295 // Time::Now() may actually decrease or jump). But note that TimeTicks may
296 // "stand still", for example if the computer suspended.
297
298 class TimeTicks V8_FINAL BASE_EMBEDDED {
299 public:
300 TimeTicks() : ticks_(0) {}
301
302 // Platform-dependent tick count representing "right now."
303 // The resolution of this clock is ~1-15ms. Resolution varies depending
304 // on hardware/operating system configuration.
305 // This method never returns a null TimeTicks.
306 static TimeTicks Now();
307
308 // Returns a platform-dependent high-resolution tick count. Implementation
309 // is hardware dependent and may or may not return sub-millisecond
310 // resolution. THIS CALL IS GENERALLY MUCH MORE EXPENSIVE THAN Now() AND
311 // SHOULD ONLY BE USED WHEN IT IS REALLY NEEDED.
312 // This method never returns a null TimeTicks.
313 static TimeTicks HighResNow();
314
315 // Returns true if this object has not been initialized.
316 bool IsNull() const { return ticks_ == 0; }
317
318 TimeTicks& operator=(const TimeTicks other) {
319 ticks_ = other.ticks_;
320 return *this;
321 }
322
323 // Compute the difference between two times.
324 TimeDelta operator-(const TimeTicks other) const {
325 return TimeDelta::FromMicroseconds(ticks_ - other.ticks_);
326 }
327
328 // Modify by some time delta.
329 TimeTicks& operator+=(const TimeDelta& delta) {
330 ticks_ += delta.InMicroseconds();
331 return *this;
332 }
333 TimeTicks& operator-=(const TimeDelta& delta) {
334 ticks_ -= delta.InMicroseconds();
335 return *this;
336 }
337
338 // Return a new TimeTicks modified by some delta.
339 TimeTicks operator+(const TimeDelta& delta) const {
340 return TimeTicks(ticks_ + delta.InMicroseconds());
341 }
342 TimeTicks operator-(const TimeDelta& delta) const {
343 return TimeTicks(ticks_ - delta.InMicroseconds());
344 }
345
346 // Comparison operators
347 bool operator==(const TimeTicks& other) const {
348 return ticks_ == other.ticks_;
349 }
350 bool operator!=(const TimeTicks& other) const {
351 return ticks_ != other.ticks_;
352 }
353 bool operator<(const TimeTicks& other) const {
354 return ticks_ < other.ticks_;
355 }
356 bool operator<=(const TimeTicks& other) const {
357 return ticks_ <= other.ticks_;
358 }
359 bool operator>(const TimeTicks& other) const {
360 return ticks_ > other.ticks_;
361 }
362 bool operator>=(const TimeTicks& other) const {
363 return ticks_ >= other.ticks_;
364 }
365
366 private:
367 // Please use Now() to create a new object. This is for internal use
368 // and testing. Ticks is in microseconds.
369 explicit TimeTicks(int64_t ticks) : ticks_(ticks) {}
370
371 // Tick count in microseconds.
372 int64_t ticks_;
373 };
374
375 inline TimeTicks operator+(const TimeDelta& delta, const TimeTicks& ticks) {
376 return ticks + delta;
377 }
378
379 } } // namespace v8::internal
380
381 #endif // V8_PLATFORM_TIME_H_
OLDNEW
« no previous file with comments | « src/platform/elapsed-timer.h ('k') | src/platform/time.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698