1 // Copyright (c) 2012 The Chromium 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 BASE_PROFILER_TRACKED_TIME_H_
6 #define BASE_PROFILER_TRACKED_TIME_H_
7 
8 #include <stdint.h>
9 
10 #include "base/base_export.h"
11 #include "base/time/time.h"
12 
13 namespace tracked_objects {
14 
15 //------------------------------------------------------------------------------
16 
17 // TimeTicks maintains a wasteful 64 bits of data (we need less than 32), and on
18 // windows, a 64 bit timer is expensive to even obtain. We use a simple
19 // millisecond counter for most of our time values, as well as millisecond units
20 // of duration between those values.  This means we can only handle durations
21 // up to 49 days (range), or 24 days (non-negative time durations).
22 // We only define enough methods to service the needs of the tracking classes,
23 // and our interfaces are modeled after what TimeTicks and TimeDelta use (so we
24 // can swap them into place if we want to use the "real" classes).
25 
26 class BASE_EXPORT Duration {  // Similar to base::TimeDelta.
27  public:
28   Duration();
29 
30   Duration& operator+=(const Duration& other);
31   Duration operator+(const Duration& other) const;
32 
33   bool operator==(const Duration& other) const;
34   bool operator!=(const Duration& other) const;
35   bool operator>(const Duration& other) const;
36 
37   static Duration FromMilliseconds(int ms);
38 
39   int32_t InMilliseconds() const;
40 
41  private:
42   friend class TrackedTime;
43   explicit Duration(int32_t duration);
44 
45   // Internal time is stored directly in milliseconds.
46   int32_t ms_;
47 };
48 
49 class BASE_EXPORT TrackedTime {  // Similar to base::TimeTicks.
50  public:
51   TrackedTime();
52   explicit TrackedTime(const base::TimeTicks& time);
53 
54   static TrackedTime Now();
55   Duration operator-(const TrackedTime& other) const;
56   TrackedTime operator+(const Duration& other) const;
57   bool is_null() const;
58 
FromMilliseconds(int32_t ms)59   static TrackedTime FromMilliseconds(int32_t ms) { return TrackedTime(ms); }
60 
61  private:
62   friend class Duration;
63   explicit TrackedTime(int32_t ms);
64 
65   // Internal duration is stored directly in milliseconds.
66   uint32_t ms_;
67 };
68 
69 }  // namespace tracked_objects
70 
71 #endif  // BASE_PROFILER_TRACKED_TIME_H_
72