Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/profiler/tracked_time.h" | 5 #include "base/profiler/tracked_time.h" |
| 6 | 6 |
| 7 #include "base/base_switches.h" | |
| 8 #include "base/command_line.h" | |
| 7 #include "build/build_config.h" | 9 #include "build/build_config.h" |
| 8 | 10 |
| 9 #if defined(OS_WIN) | 11 #if defined(OS_WIN) |
| 10 #include <mmsystem.h> // Declare timeGetTime()... after including build_config. | 12 #include <mmsystem.h> // Declare timeGetTime()... after including build_config. |
| 11 #endif | 13 #endif |
| 12 | 14 |
| 15 namespace { | |
| 16 inline bool IsProfilerTimingEnabled() { | |
| 17 // This initialization is not thread safe. In particular, some early calls | |
|
Mark Mentovai
2013/12/05 23:54:04
Nit: thread-safe (with dash).
qsr
2013/12/06 14:29:43
Done.
| |
| 18 // might see |timing_disabled| being false, even if this is incorrect. This is | |
| 19 // OK, as the timing is only disabled for performance reasons, and having a | |
| 20 // few calls to TimeTicks::Now() is acceptable, while needing to use a lock or | |
| 21 // a memory barrier would be more costly. | |
| 22 static bool timing_disabled = | |
| 23 CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | |
| 24 switches::kProfilerTiming) == "0"; | |
|
jar (doing other things)
2013/12/05 23:51:06
You can indeed change the name to avoid negatives.
Mark Mentovai
2013/12/05 23:56:05
jar wrote:
qsr
2013/12/06 14:29:43
Done.
no sievers
2013/12/09 18:59:29
Or you could just use LazyInstance (or Singleton),
| |
| 25 return !timing_disabled; | |
| 26 } | |
| 27 } // namespace | |
| 28 | |
| 13 namespace tracked_objects { | 29 namespace tracked_objects { |
| 14 | 30 |
| 15 Duration::Duration() : ms_(0) {} | 31 Duration::Duration() : ms_(0) {} |
| 16 Duration::Duration(int32 duration) : ms_(duration) {} | 32 Duration::Duration(int32 duration) : ms_(duration) {} |
| 17 | 33 |
| 18 Duration& Duration::operator+=(const Duration& other) { | 34 Duration& Duration::operator+=(const Duration& other) { |
| 19 ms_ += other.ms_; | 35 ms_ += other.ms_; |
| 20 return *this; | 36 return *this; |
| 21 } | 37 } |
| 22 | 38 |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 44 //------------------------------------------------------------------------------ | 60 //------------------------------------------------------------------------------ |
| 45 | 61 |
| 46 TrackedTime::TrackedTime() : ms_(0) {} | 62 TrackedTime::TrackedTime() : ms_(0) {} |
| 47 TrackedTime::TrackedTime(int32 ms) : ms_(ms) {} | 63 TrackedTime::TrackedTime(int32 ms) : ms_(ms) {} |
| 48 TrackedTime::TrackedTime(const base::TimeTicks& time) | 64 TrackedTime::TrackedTime(const base::TimeTicks& time) |
| 49 : ms_((time - base::TimeTicks()).InMilliseconds()) { | 65 : ms_((time - base::TimeTicks()).InMilliseconds()) { |
| 50 } | 66 } |
| 51 | 67 |
| 52 // static | 68 // static |
| 53 TrackedTime TrackedTime::Now() { | 69 TrackedTime TrackedTime::Now() { |
| 70 if (IsProfilerTimingEnabled()) { | |
|
jar (doing other things)
2013/12/05 23:51:06
nit: Early return, inducing less indentation, etc.
Mark Mentovai
2013/12/05 23:54:04
I’d rather flip the logic. Having the “real” defin
qsr
2013/12/06 14:29:43
Done.
| |
| 54 #if defined(OS_WIN) | 71 #if defined(OS_WIN) |
| 55 // Use lock-free accessor to 32 bit time. | 72 // Use lock-free accessor to 32 bit time. |
| 56 // Note that TimeTicks::Now() is built on this, so we have "compatible" | 73 // Note that TimeTicks::Now() is built on this, so we have "compatible" |
| 57 // times when we down-convert a TimeTicks sample. | 74 // times when we down-convert a TimeTicks sample. |
| 58 return TrackedTime(base::TimeTicks::UnprotectedNow()); | 75 return TrackedTime(base::TimeTicks::UnprotectedNow()); |
| 59 #else | 76 #else |
| 60 // Posix has nice cheap 64 bit times, so we just down-convert it. | 77 // Posix has nice cheap 64 bit times, so we just down-convert it. |
| 61 return TrackedTime(base::TimeTicks::Now()); | 78 return TrackedTime(base::TimeTicks::Now()); |
| 62 #endif // OS_WIN | 79 #endif // OS_WIN |
| 80 } | |
| 81 return TrackedTime(); | |
| 63 } | 82 } |
| 64 | 83 |
| 65 Duration TrackedTime::operator-(const TrackedTime& other) const { | 84 Duration TrackedTime::operator-(const TrackedTime& other) const { |
| 66 return Duration(ms_ - other.ms_); | 85 return Duration(ms_ - other.ms_); |
| 67 } | 86 } |
| 68 | 87 |
| 69 TrackedTime TrackedTime::operator+(const Duration& other) const { | 88 TrackedTime TrackedTime::operator+(const Duration& other) const { |
| 70 return TrackedTime(ms_ + other.ms_); | 89 return TrackedTime(ms_ + other.ms_); |
| 71 } | 90 } |
| 72 | 91 |
| 73 bool TrackedTime::is_null() const { return ms_ == 0; } | 92 bool TrackedTime::is_null() const { return ms_ == 0; } |
| 74 | 93 |
| 75 } // namespace tracked_objects | 94 } // namespace tracked_objects |
| OLD | NEW |