• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "base/profiler/tracked_time.h"
6 
7 #include "build/build_config.h"
8 
9 #if defined(OS_WIN)
10 #include <mmsystem.h>  // Declare timeGetTime()... after including build_config.
11 #endif
12 
13 namespace tracked_objects {
14 
Duration()15 Duration::Duration() : ms_(0) {}
Duration(int32 duration)16 Duration::Duration(int32 duration) : ms_(duration) {}
17 
operator +=(const Duration & other)18 Duration& Duration::operator+=(const Duration& other) {
19   ms_ += other.ms_;
20   return *this;
21 }
22 
operator +(const Duration & other) const23 Duration Duration::operator+(const Duration& other) const {
24   return Duration(ms_ + other.ms_);
25 }
26 
operator ==(const Duration & other) const27 bool Duration::operator==(const Duration& other) const {
28   return ms_ == other.ms_;
29 }
30 
operator !=(const Duration & other) const31 bool Duration::operator!=(const Duration& other) const {
32   return ms_ != other.ms_;
33 }
34 
operator >(const Duration & other) const35 bool Duration::operator>(const Duration& other) const {
36   return ms_ > other.ms_;
37 }
38 
39 // static
FromMilliseconds(int ms)40 Duration Duration::FromMilliseconds(int ms) { return Duration(ms); }
41 
InMilliseconds() const42 int32 Duration::InMilliseconds() const { return ms_; }
43 
44 //------------------------------------------------------------------------------
45 
TrackedTime()46 TrackedTime::TrackedTime() : ms_(0) {}
TrackedTime(int32 ms)47 TrackedTime::TrackedTime(int32 ms) : ms_(ms) {}
TrackedTime(const base::TimeTicks & time)48 TrackedTime::TrackedTime(const base::TimeTicks& time)
49     : ms_((time - base::TimeTicks()).InMilliseconds()) {
50 }
51 
52 // static
Now()53 TrackedTime TrackedTime::Now() {
54 #if defined(OS_WIN)
55   // Use lock-free accessor to 32 bit time.
56   // Note that TimeTicks::Now() is built on this, so we have "compatible"
57   // times when we down-convert a TimeTicks sample.
58   return TrackedTime(base::TimeTicks::UnprotectedNow());
59 #else
60   // Posix has nice cheap 64 bit times, so we just down-convert it.
61   return TrackedTime(base::TimeTicks::Now());
62 #endif  // OS_WIN
63 }
64 
operator -(const TrackedTime & other) const65 Duration TrackedTime::operator-(const TrackedTime& other) const {
66   return Duration(ms_ - other.ms_);
67 }
68 
operator +(const Duration & other) const69 TrackedTime TrackedTime::operator+(const Duration& other) const {
70   return TrackedTime(ms_ + other.ms_);
71 }
72 
is_null() const73 bool TrackedTime::is_null() const { return ms_ == 0; }
74 
75 }  // namespace tracked_objects
76