blob: e8e6d178299240da016712aeee202c14e35b03f4 [file] [log] [blame]
jar@chromium.org3d6a82f2011-11-05 09:39:27 +09001// Copyright (c) 2011 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
9#include "base/base_export.h"
10#include "base/basictypes.h"
11#include "base/time.h"
12
13namespace tracked_objects {
14
15//------------------------------------------------------------------------------
16
17#define USE_FAST_TIME_CLASS_FOR_DURATION_CALCULATIONS
18
19#if defined(USE_FAST_TIME_CLASS_FOR_DURATION_CALCULATIONS)
20
21// TimeTicks maintains a wasteful 64 bits of data (we need less than 32), and on
22// windows, a 64 bit timer is expensive to even obtain. We use a simple
23// millisecond counter for most of our time values, as well as millisecond units
24// of duration between those values. This means we can only handle durations
25// up to 49 days (range), or 24 days (non-negative time durations).
26// We only define enough methods to service the needs of the tracking classes,
27// and our interfaces are modeled after what TimeTicks and TimeDelta use (so we
28// can swap them into place if we want to use the "real" classes).
29
30class BASE_EXPORT Duration { // Similar to base::TimeDelta.
31 public:
32 Duration();
33
34 Duration& operator+=(const Duration& other);
35 Duration operator+(const Duration& other) const;
36
37 bool operator==(const Duration& other) const;
38 bool operator!=(const Duration& other) const;
39 bool operator>(const Duration& other) const;
40
41 static Duration FromMilliseconds(int ms);
42
43 int32 InMilliseconds() const;
44
45 private:
46 friend class TrackedTime;
47 explicit Duration(int32 duration);
48
49 // Internal time is stored directly in milliseconds.
50 int32 ms_;
51};
52
53class BASE_EXPORT TrackedTime { // Similar to base::TimeTicks.
54 public:
55 TrackedTime();
56 explicit TrackedTime(const base::TimeTicks& time);
57
58 static TrackedTime Now();
59 Duration operator-(const TrackedTime& other) const;
60 TrackedTime operator+(const Duration& other) const;
61 bool is_null() const;
62
63 private:
64 friend class Duration;
65 explicit TrackedTime(int32 ms);
66
67 // Internal duration is stored directly in milliseconds.
68 uint32 ms_;
69};
70
71#else
72
73// Just use full 64 bit time calculations, and the slower TimeTicks::Now().
74// This allows us (as an alternative) to test with larger ranges of times, and
75// with a more thoroughly tested class.
76
77typedef base::TimeTicks TrackedTime;
78typedef base::TimeDelta Duration;
79
80#endif // USE_FAST_TIME_CLASS_FOR_DURATION_CALCULATIONS
81
82} // namespace tracked_objects
83
84#endif // BASE_PROFILER_TRACKED_TIME_H_