summaryrefslogtreecommitdiffstats
path: root/base/profiler/tracked_time.h
blob: b32f41b39cb8a6185c568c0b26c8323e3546c21b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef BASE_PROFILER_TRACKED_TIME_H_
#define BASE_PROFILER_TRACKED_TIME_H_

#include <stdint.h>

#include "base/base_export.h"
#include "base/time/time.h"

namespace tracked_objects {

//------------------------------------------------------------------------------

// TimeTicks maintains a wasteful 64 bits of data (we need less than 32), and on
// windows, a 64 bit timer is expensive to even obtain. We use a simple
// millisecond counter for most of our time values, as well as millisecond units
// of duration between those values.  This means we can only handle durations
// up to 49 days (range), or 24 days (non-negative time durations).
// We only define enough methods to service the needs of the tracking classes,
// and our interfaces are modeled after what TimeTicks and TimeDelta use (so we
// can swap them into place if we want to use the "real" classes).

class BASE_EXPORT Duration {  // Similar to base::TimeDelta.
 public:
  Duration();

  Duration& operator+=(const Duration& other);
  Duration operator+(const Duration& other) const;

  bool operator==(const Duration& other) const;
  bool operator!=(const Duration& other) const;
  bool operator>(const Duration& other) const;

  static Duration FromMilliseconds(int ms);

  int32_t InMilliseconds() const;

 private:
  friend class TrackedTime;
  explicit Duration(int32_t duration);

  // Internal time is stored directly in milliseconds.
  int32_t ms_;
};

class BASE_EXPORT TrackedTime {  // Similar to base::TimeTicks.
 public:
  TrackedTime();
  explicit TrackedTime(const base::TimeTicks& time);

  static TrackedTime Now();
  Duration operator-(const TrackedTime& other) const;
  TrackedTime operator+(const Duration& other) const;
  bool is_null() const;

  static TrackedTime FromMilliseconds(int32_t ms) { return TrackedTime(ms); }

 private:
  friend class Duration;
  explicit TrackedTime(int32_t ms);

  // Internal duration is stored directly in milliseconds.
  uint32_t ms_;
};

}  // namespace tracked_objects

#endif  // BASE_PROFILER_TRACKED_TIME_H_