[email protected] | 9fc4416 | 2012-01-23 22:56:41 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 4 | |
| 5 | #include "base/tracked_objects.h" |
| 6 | |
[email protected] | c014f2b3 | 2013-09-03 23:29:12 | [diff] [blame] | 7 | #include <limits.h> |
[email protected] | 7f8a4eb | 2012-03-19 21:46:27 | [diff] [blame] | 8 | #include <stdlib.h> |
[email protected] | a5b94a9 | 2008-08-12 23:25:43 | [diff] [blame] | 9 | |
[email protected] | 7caab6dc | 2013-12-12 19:29:10 | [diff] [blame] | 10 | #include "base/atomicops.h" |
[email protected] | 915b344f | 2013-12-11 12:49:17 | [diff] [blame] | 11 | #include "base/base_switches.h" |
| 12 | #include "base/command_line.h" |
[email protected] | 75086be | 2013-03-20 21:18:22 | [diff] [blame] | 13 | #include "base/compiler_specific.h" |
[email protected] | bf709abd | 2013-06-10 11:32:20 | [diff] [blame] | 14 | #include "base/debug/leak_annotations.h" |
[email protected] | c014f2b3 | 2013-09-03 23:29:12 | [diff] [blame] | 15 | #include "base/logging.h" |
[email protected] | dd4b5126 | 2013-07-25 21:38:23 | [diff] [blame] | 16 | #include "base/process/process_handle.h" |
[email protected] | 90895d0f | 2012-02-15 23:05:01 | [diff] [blame] | 17 | #include "base/profiler/alternate_timer.h" |
[email protected] | c851cfd | 2013-06-10 20:11:14 | [diff] [blame] | 18 | #include "base/strings/stringprintf.h" |
[email protected] | a0447ff | 2011-12-04 21:14:05 | [diff] [blame] | 19 | #include "base/third_party/valgrind/memcheck.h" |
[email protected] | c014f2b3 | 2013-09-03 23:29:12 | [diff] [blame] | 20 | #include "base/tracking_info.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 21 | |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 22 | using base::TimeDelta; |
| 23 | |
[email protected] | c014f2b3 | 2013-09-03 23:29:12 | [diff] [blame] | 24 | namespace base { |
| 25 | class TimeDelta; |
| 26 | } |
| 27 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 28 | namespace tracked_objects { |
| 29 | |
[email protected] | b2a9bbd | 2011-10-31 22:36:21 | [diff] [blame] | 30 | namespace { |
| 31 | // Flag to compile out almost all of the task tracking code. |
[email protected] | da9ccfb | 2012-01-28 00:34:40 | [diff] [blame] | 32 | const bool kTrackAllTaskObjects = true; |
[email protected] | 3f095c0a | 2011-10-31 15:32:08 | [diff] [blame] | 33 | |
[email protected] | 1cb05db | 2012-04-13 00:39:26 | [diff] [blame] | 34 | // TODO(jar): Evaluate the perf impact of enabling this. If the perf impact is |
| 35 | // negligible, enable by default. |
[email protected] | 8aa1e6e | 2011-12-14 01:36:48 | [diff] [blame] | 36 | // Flag to compile out parent-child link recording. |
[email protected] | da9ccfb | 2012-01-28 00:34:40 | [diff] [blame] | 37 | const bool kTrackParentChildLinks = false; |
[email protected] | 8aa1e6e | 2011-12-14 01:36:48 | [diff] [blame] | 38 | |
[email protected] | b2a9bbd | 2011-10-31 22:36:21 | [diff] [blame] | 39 | // When ThreadData is first initialized, should we start in an ACTIVE state to |
| 40 | // record all of the startup-time tasks, or should we start up DEACTIVATED, so |
| 41 | // that we only record after parsing the command line flag --enable-tracking. |
| 42 | // Note that the flag may force either state, so this really controls only the |
| 43 | // period of time up until that flag is parsed. If there is no flag seen, then |
| 44 | // this state may prevail for much or all of the process lifetime. |
[email protected] | da9ccfb | 2012-01-28 00:34:40 | [diff] [blame] | 45 | const ThreadData::Status kInitialStartupState = |
[email protected] | 8aa1e6e | 2011-12-14 01:36:48 | [diff] [blame] | 46 | ThreadData::PROFILING_CHILDREN_ACTIVE; |
[email protected] | da9ccfb | 2012-01-28 00:34:40 | [diff] [blame] | 47 | |
[email protected] | 90895d0f | 2012-02-15 23:05:01 | [diff] [blame] | 48 | // Control whether an alternate time source (Now() function) is supported by |
| 49 | // the ThreadData class. This compile time flag should be set to true if we |
| 50 | // want other modules (such as a memory allocator, or a thread-specific CPU time |
| 51 | // clock) to be able to provide a thread-specific Now() function. Without this |
| 52 | // compile-time flag, the code will only support the wall-clock time. This flag |
| 53 | // can be flipped to efficiently disable this path (if there is a performance |
| 54 | // problem with its presence). |
| 55 | static const bool kAllowAlternateTimeSourceHandling = true; |
[email protected] | 1cb05db | 2012-04-13 00:39:26 | [diff] [blame] | 56 | |
vadimt | a156831 | 2014-11-06 22:27:43 | [diff] [blame] | 57 | // Possible states of the profiler timing enabledness. |
| 58 | enum { |
| 59 | UNDEFINED_TIMING, |
| 60 | ENABLED_TIMING, |
| 61 | DISABLED_TIMING, |
| 62 | }; |
| 63 | |
| 64 | // State of the profiler timing enabledness. |
| 65 | base::subtle::Atomic32 g_profiler_timing_enabled = UNDEFINED_TIMING; |
| 66 | |
| 67 | // Returns whether profiler timing is enabled. The default is true, but this may |
| 68 | // be overridden by a command-line flag. Some platforms may programmatically set |
| 69 | // this command-line flag to the "off" value if it's not specified. |
| 70 | // This in turn can be overridden by explicitly calling |
| 71 | // ThreadData::EnableProfilerTiming, say, based on a field trial. |
[email protected] | 915b344f | 2013-12-11 12:49:17 | [diff] [blame] | 72 | inline bool IsProfilerTimingEnabled() { |
vadimt | a156831 | 2014-11-06 22:27:43 | [diff] [blame] | 73 | // Reading |g_profiler_timing_enabled| is done without barrier because |
| 74 | // multiple initialization is not an issue while the barrier can be relatively |
| 75 | // costly given that this method is sometimes called in a tight loop. |
[email protected] | 7caab6dc | 2013-12-12 19:29:10 | [diff] [blame] | 76 | base::subtle::Atomic32 current_timing_enabled = |
vadimt | a156831 | 2014-11-06 22:27:43 | [diff] [blame] | 77 | base::subtle::NoBarrier_Load(&g_profiler_timing_enabled); |
[email protected] | 7caab6dc | 2013-12-12 19:29:10 | [diff] [blame] | 78 | if (current_timing_enabled == UNDEFINED_TIMING) { |
pgal.u-szeged | 421dddb | 2014-11-25 12:55:02 | [diff] [blame^] | 79 | if (!base::CommandLine::InitializedForCurrentProcess()) |
[email protected] | 915b344f | 2013-12-11 12:49:17 | [diff] [blame] | 80 | return true; |
[email protected] | 7caab6dc | 2013-12-12 19:29:10 | [diff] [blame] | 81 | current_timing_enabled = |
pgal.u-szeged | 421dddb | 2014-11-25 12:55:02 | [diff] [blame^] | 82 | (base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
[email protected] | 7caab6dc | 2013-12-12 19:29:10 | [diff] [blame] | 83 | switches::kProfilerTiming) == |
| 84 | switches::kProfilerTimingDisabledValue) |
| 85 | ? DISABLED_TIMING |
| 86 | : ENABLED_TIMING; |
vadimt | a156831 | 2014-11-06 22:27:43 | [diff] [blame] | 87 | base::subtle::NoBarrier_Store(&g_profiler_timing_enabled, |
| 88 | current_timing_enabled); |
[email protected] | 915b344f | 2013-12-11 12:49:17 | [diff] [blame] | 89 | } |
[email protected] | 7caab6dc | 2013-12-12 19:29:10 | [diff] [blame] | 90 | return current_timing_enabled == ENABLED_TIMING; |
[email protected] | 915b344f | 2013-12-11 12:49:17 | [diff] [blame] | 91 | } |
| 92 | |
[email protected] | 8aa1e6e | 2011-12-14 01:36:48 | [diff] [blame] | 93 | } // namespace |
[email protected] | 84b5795 | 2011-10-15 23:52:45 | [diff] [blame] | 94 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 95 | //------------------------------------------------------------------------------ |
[email protected] | 63f5b0e | 2011-11-04 00:23:27 | [diff] [blame] | 96 | // DeathData tallies durations when a death takes place. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 97 | |
[email protected] | b6b2b89 | 2011-12-04 07:19:10 | [diff] [blame] | 98 | DeathData::DeathData() { |
| 99 | Clear(); |
| 100 | } |
| 101 | |
| 102 | DeathData::DeathData(int count) { |
| 103 | Clear(); |
| 104 | count_ = count; |
| 105 | } |
| 106 | |
[email protected] | 7ceb4448 | 2011-12-09 03:41:04 | [diff] [blame] | 107 | // TODO(jar): I need to see if this macro to optimize branching is worth using. |
[email protected] | b6b2b89 | 2011-12-04 07:19:10 | [diff] [blame] | 108 | // |
| 109 | // This macro has no branching, so it is surely fast, and is equivalent to: |
| 110 | // if (assign_it) |
| 111 | // target = source; |
| 112 | // We use a macro rather than a template to force this to inline. |
| 113 | // Related code for calculating max is discussed on the web. |
| 114 | #define CONDITIONAL_ASSIGN(assign_it, target, source) \ |
[email protected] | c186e96 | 2012-03-24 22:17:18 | [diff] [blame] | 115 | ((target) ^= ((target) ^ (source)) & -static_cast<int32>(assign_it)) |
[email protected] | b6b2b89 | 2011-12-04 07:19:10 | [diff] [blame] | 116 | |
[email protected] | c186e96 | 2012-03-24 22:17:18 | [diff] [blame] | 117 | void DeathData::RecordDeath(const int32 queue_duration, |
| 118 | const int32 run_duration, |
glider | f7812575 | 2014-11-12 21:05:33 | [diff] [blame] | 119 | const uint32 random_number) { |
[email protected] | 59b15da | 2013-02-28 04:15:43 | [diff] [blame] | 120 | // We'll just clamp at INT_MAX, but we should note this in the UI as such. |
| 121 | if (count_ < INT_MAX) |
| 122 | ++count_; |
[email protected] | b6b2b89 | 2011-12-04 07:19:10 | [diff] [blame] | 123 | queue_duration_sum_ += queue_duration; |
| 124 | run_duration_sum_ += run_duration; |
[email protected] | 7ceb4448 | 2011-12-09 03:41:04 | [diff] [blame] | 125 | |
| 126 | if (queue_duration_max_ < queue_duration) |
| 127 | queue_duration_max_ = queue_duration; |
| 128 | if (run_duration_max_ < run_duration) |
| 129 | run_duration_max_ = run_duration; |
[email protected] | b6b2b89 | 2011-12-04 07:19:10 | [diff] [blame] | 130 | |
| 131 | // Take a uniformly distributed sample over all durations ever supplied. |
| 132 | // The probability that we (instead) use this new sample is 1/count_. This |
[email protected] | 15dd901 | 2012-07-26 22:03:01 | [diff] [blame] | 133 | // results in a completely uniform selection of the sample (at least when we |
| 134 | // don't clamp count_... but that should be inconsequentially likely). |
| 135 | // We ignore the fact that we correlated our selection of a sample to the run |
| 136 | // and queue times (i.e., we used them to generate random_number). |
[email protected] | 59b15da | 2013-02-28 04:15:43 | [diff] [blame] | 137 | CHECK_GT(count_, 0); |
[email protected] | 7ceb4448 | 2011-12-09 03:41:04 | [diff] [blame] | 138 | if (0 == (random_number % count_)) { |
| 139 | queue_duration_sample_ = queue_duration; |
| 140 | run_duration_sample_ = run_duration; |
| 141 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 142 | } |
| 143 | |
[email protected] | b6b2b89 | 2011-12-04 07:19:10 | [diff] [blame] | 144 | int DeathData::count() const { return count_; } |
| 145 | |
[email protected] | c186e96 | 2012-03-24 22:17:18 | [diff] [blame] | 146 | int32 DeathData::run_duration_sum() const { return run_duration_sum_; } |
[email protected] | b6b2b89 | 2011-12-04 07:19:10 | [diff] [blame] | 147 | |
[email protected] | c186e96 | 2012-03-24 22:17:18 | [diff] [blame] | 148 | int32 DeathData::run_duration_max() const { return run_duration_max_; } |
[email protected] | b6b2b89 | 2011-12-04 07:19:10 | [diff] [blame] | 149 | |
[email protected] | c186e96 | 2012-03-24 22:17:18 | [diff] [blame] | 150 | int32 DeathData::run_duration_sample() const { |
[email protected] | b6b2b89 | 2011-12-04 07:19:10 | [diff] [blame] | 151 | return run_duration_sample_; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 152 | } |
| 153 | |
[email protected] | c186e96 | 2012-03-24 22:17:18 | [diff] [blame] | 154 | int32 DeathData::queue_duration_sum() const { |
[email protected] | b6b2b89 | 2011-12-04 07:19:10 | [diff] [blame] | 155 | return queue_duration_sum_; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 156 | } |
| 157 | |
[email protected] | c186e96 | 2012-03-24 22:17:18 | [diff] [blame] | 158 | int32 DeathData::queue_duration_max() const { |
[email protected] | b6b2b89 | 2011-12-04 07:19:10 | [diff] [blame] | 159 | return queue_duration_max_; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 160 | } |
| 161 | |
[email protected] | c186e96 | 2012-03-24 22:17:18 | [diff] [blame] | 162 | int32 DeathData::queue_duration_sample() const { |
[email protected] | b6b2b89 | 2011-12-04 07:19:10 | [diff] [blame] | 163 | return queue_duration_sample_; |
| 164 | } |
| 165 | |
[email protected] | b6b2b89 | 2011-12-04 07:19:10 | [diff] [blame] | 166 | void DeathData::ResetMax() { |
| 167 | run_duration_max_ = 0; |
| 168 | queue_duration_max_ = 0; |
| 169 | } |
| 170 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 171 | void DeathData::Clear() { |
| 172 | count_ = 0; |
[email protected] | b6b2b89 | 2011-12-04 07:19:10 | [diff] [blame] | 173 | run_duration_sum_ = 0; |
| 174 | run_duration_max_ = 0; |
| 175 | run_duration_sample_ = 0; |
| 176 | queue_duration_sum_ = 0; |
| 177 | queue_duration_max_ = 0; |
| 178 | queue_duration_sample_ = 0; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | //------------------------------------------------------------------------------ |
[email protected] | 1cb05db | 2012-04-13 00:39:26 | [diff] [blame] | 182 | DeathDataSnapshot::DeathDataSnapshot() |
| 183 | : count(-1), |
| 184 | run_duration_sum(-1), |
| 185 | run_duration_max(-1), |
| 186 | run_duration_sample(-1), |
| 187 | queue_duration_sum(-1), |
| 188 | queue_duration_max(-1), |
| 189 | queue_duration_sample(-1) { |
| 190 | } |
| 191 | |
| 192 | DeathDataSnapshot::DeathDataSnapshot( |
| 193 | const tracked_objects::DeathData& death_data) |
| 194 | : count(death_data.count()), |
| 195 | run_duration_sum(death_data.run_duration_sum()), |
| 196 | run_duration_max(death_data.run_duration_max()), |
| 197 | run_duration_sample(death_data.run_duration_sample()), |
| 198 | queue_duration_sum(death_data.queue_duration_sum()), |
| 199 | queue_duration_max(death_data.queue_duration_max()), |
| 200 | queue_duration_sample(death_data.queue_duration_sample()) { |
| 201 | } |
| 202 | |
| 203 | DeathDataSnapshot::~DeathDataSnapshot() { |
| 204 | } |
| 205 | |
| 206 | //------------------------------------------------------------------------------ |
[email protected] | 84baeca | 2011-10-24 18:55:16 | [diff] [blame] | 207 | BirthOnThread::BirthOnThread(const Location& location, |
| 208 | const ThreadData& current) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 209 | : location_(location), |
[email protected] | b6b2b89 | 2011-12-04 07:19:10 | [diff] [blame] | 210 | birth_thread_(¤t) { |
| 211 | } |
| 212 | |
[email protected] | 1cb05db | 2012-04-13 00:39:26 | [diff] [blame] | 213 | //------------------------------------------------------------------------------ |
| 214 | BirthOnThreadSnapshot::BirthOnThreadSnapshot() { |
| 215 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 216 | |
[email protected] | 1cb05db | 2012-04-13 00:39:26 | [diff] [blame] | 217 | BirthOnThreadSnapshot::BirthOnThreadSnapshot( |
| 218 | const tracked_objects::BirthOnThread& birth) |
| 219 | : location(birth.location()), |
| 220 | thread_name(birth.birth_thread()->thread_name()) { |
| 221 | } |
| 222 | |
| 223 | BirthOnThreadSnapshot::~BirthOnThreadSnapshot() { |
[email protected] | 8aa1e6e | 2011-12-14 01:36:48 | [diff] [blame] | 224 | } |
| 225 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 226 | //------------------------------------------------------------------------------ |
[email protected] | 84baeca | 2011-10-24 18:55:16 | [diff] [blame] | 227 | Births::Births(const Location& location, const ThreadData& current) |
| 228 | : BirthOnThread(location, current), |
[email protected] | 75b7920 | 2009-12-30 07:31:45 | [diff] [blame] | 229 | birth_count_(1) { } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 230 | |
[email protected] | b6b2b89 | 2011-12-04 07:19:10 | [diff] [blame] | 231 | int Births::birth_count() const { return birth_count_; } |
| 232 | |
| 233 | void Births::RecordBirth() { ++birth_count_; } |
| 234 | |
| 235 | void Births::ForgetBirth() { --birth_count_; } |
| 236 | |
| 237 | void Births::Clear() { birth_count_ = 0; } |
| 238 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 239 | //------------------------------------------------------------------------------ |
[email protected] | b6b2b89 | 2011-12-04 07:19:10 | [diff] [blame] | 240 | // ThreadData maintains the central data for all births and deaths on a single |
| 241 | // thread. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 242 | |
[email protected] | b2a9bbd | 2011-10-31 22:36:21 | [diff] [blame] | 243 | // TODO(jar): We should pull all these static vars together, into a struct, and |
| 244 | // optimize layout so that we benefit from locality of reference during accesses |
| 245 | // to them. |
| 246 | |
[email protected] | 90895d0f | 2012-02-15 23:05:01 | [diff] [blame] | 247 | // static |
| 248 | NowFunction* ThreadData::now_function_ = NULL; |
| 249 | |
vadimt | 12f0f7d | 2014-09-15 19:19:38 | [diff] [blame] | 250 | // static |
| 251 | bool ThreadData::now_function_is_time_ = false; |
| 252 | |
[email protected] | b2a9bbd | 2011-10-31 22:36:21 | [diff] [blame] | 253 | // A TLS slot which points to the ThreadData instance for the current thread. We |
| 254 | // do a fake initialization here (zeroing out data), and then the real in-place |
| 255 | // construction happens when we call tls_index_.Initialize(). |
| 256 | // static |
[email protected] | 444b8a3c | 2012-01-30 16:52:09 | [diff] [blame] | 257 | base::ThreadLocalStorage::StaticSlot ThreadData::tls_index_ = TLS_INITIALIZER; |
[email protected] | b2a9bbd | 2011-10-31 22:36:21 | [diff] [blame] | 258 | |
[email protected] | b2a9bbd | 2011-10-31 22:36:21 | [diff] [blame] | 259 | // static |
[email protected] | 9a88c90 | 2011-11-24 00:00:31 | [diff] [blame] | 260 | int ThreadData::worker_thread_data_creation_count_ = 0; |
| 261 | |
| 262 | // static |
| 263 | int ThreadData::cleanup_count_ = 0; |
[email protected] | b2a9bbd | 2011-10-31 22:36:21 | [diff] [blame] | 264 | |
| 265 | // static |
| 266 | int ThreadData::incarnation_counter_ = 0; |
| 267 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 268 | // static |
[email protected] | 84baeca | 2011-10-24 18:55:16 | [diff] [blame] | 269 | ThreadData* ThreadData::all_thread_data_list_head_ = NULL; |
| 270 | |
| 271 | // static |
[email protected] | 26cdeb96 | 2011-11-20 04:17:07 | [diff] [blame] | 272 | ThreadData* ThreadData::first_retired_worker_ = NULL; |
[email protected] | 84baeca | 2011-10-24 18:55:16 | [diff] [blame] | 273 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 274 | // static |
[email protected] | 9fc4416 | 2012-01-23 22:56:41 | [diff] [blame] | 275 | base::LazyInstance<base::Lock>::Leaky |
[email protected] | 6de0fd1d | 2011-11-15 13:31:49 | [diff] [blame] | 276 | ThreadData::list_lock_ = LAZY_INSTANCE_INITIALIZER; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 277 | |
| 278 | // static |
| 279 | ThreadData::Status ThreadData::status_ = ThreadData::UNINITIALIZED; |
| 280 | |
[email protected] | 84baeca | 2011-10-24 18:55:16 | [diff] [blame] | 281 | ThreadData::ThreadData(const std::string& suggested_name) |
[email protected] | 8aa1e6e | 2011-12-14 01:36:48 | [diff] [blame] | 282 | : next_(NULL), |
[email protected] | 26cdeb96 | 2011-11-20 04:17:07 | [diff] [blame] | 283 | next_retired_worker_(NULL), |
[email protected] | 8aa1e6e | 2011-12-14 01:36:48 | [diff] [blame] | 284 | worker_thread_number_(0), |
vadimt | 12f0f7d | 2014-09-15 19:19:38 | [diff] [blame] | 285 | incarnation_count_for_pool_(-1), |
| 286 | current_stopwatch_(NULL) { |
[email protected] | 84b5795 | 2011-10-15 23:52:45 | [diff] [blame] | 287 | DCHECK_GE(suggested_name.size(), 0u); |
| 288 | thread_name_ = suggested_name; |
[email protected] | b2a9bbd | 2011-10-31 22:36:21 | [diff] [blame] | 289 | PushToHeadOfList(); // Which sets real incarnation_count_for_pool_. |
[email protected] | 84b5795 | 2011-10-15 23:52:45 | [diff] [blame] | 290 | } |
| 291 | |
[email protected] | 26cdeb96 | 2011-11-20 04:17:07 | [diff] [blame] | 292 | ThreadData::ThreadData(int thread_number) |
[email protected] | 8aa1e6e | 2011-12-14 01:36:48 | [diff] [blame] | 293 | : next_(NULL), |
[email protected] | 26cdeb96 | 2011-11-20 04:17:07 | [diff] [blame] | 294 | next_retired_worker_(NULL), |
[email protected] | 8aa1e6e | 2011-12-14 01:36:48 | [diff] [blame] | 295 | worker_thread_number_(thread_number), |
vadimt | 12f0f7d | 2014-09-15 19:19:38 | [diff] [blame] | 296 | incarnation_count_for_pool_(-1), |
| 297 | current_stopwatch_(NULL) { |
[email protected] | 26cdeb96 | 2011-11-20 04:17:07 | [diff] [blame] | 298 | CHECK_GT(thread_number, 0); |
| 299 | base::StringAppendF(&thread_name_, "WorkerThread-%d", thread_number); |
[email protected] | 63f5b0e | 2011-11-04 00:23:27 | [diff] [blame] | 300 | PushToHeadOfList(); // Which sets real incarnation_count_for_pool_. |
[email protected] | 359d2bf | 2010-11-19 20:34:18 | [diff] [blame] | 301 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 302 | |
[email protected] | d4799a3 | 2010-09-28 22:54:58 | [diff] [blame] | 303 | ThreadData::~ThreadData() {} |
| 304 | |
[email protected] | 84baeca | 2011-10-24 18:55:16 | [diff] [blame] | 305 | void ThreadData::PushToHeadOfList() { |
[email protected] | b6b2b89 | 2011-12-04 07:19:10 | [diff] [blame] | 306 | // Toss in a hint of randomness (atop the uniniitalized value). |
[email protected] | ff5e942 | 2011-12-05 15:24:28 | [diff] [blame] | 307 | (void)VALGRIND_MAKE_MEM_DEFINED_IF_ADDRESSABLE(&random_number_, |
[email protected] | a0447ff | 2011-12-04 21:14:05 | [diff] [blame] | 308 | sizeof(random_number_)); |
[email protected] | 75086be | 2013-03-20 21:18:22 | [diff] [blame] | 309 | MSAN_UNPOISON(&random_number_, sizeof(random_number_)); |
glider | f7812575 | 2014-11-12 21:05:33 | [diff] [blame] | 310 | random_number_ += static_cast<uint32>(this - static_cast<ThreadData*>(0)); |
[email protected] | b6b2b89 | 2011-12-04 07:19:10 | [diff] [blame] | 311 | random_number_ ^= (Now() - TrackedTime()).InMilliseconds(); |
| 312 | |
[email protected] | 84baeca | 2011-10-24 18:55:16 | [diff] [blame] | 313 | DCHECK(!next_); |
[email protected] | 77169a6 | 2011-11-14 20:36:46 | [diff] [blame] | 314 | base::AutoLock lock(*list_lock_.Pointer()); |
[email protected] | b2a9bbd | 2011-10-31 22:36:21 | [diff] [blame] | 315 | incarnation_count_for_pool_ = incarnation_counter_; |
[email protected] | 84baeca | 2011-10-24 18:55:16 | [diff] [blame] | 316 | next_ = all_thread_data_list_head_; |
| 317 | all_thread_data_list_head_ = this; |
| 318 | } |
| 319 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 320 | // static |
[email protected] | b6b2b89 | 2011-12-04 07:19:10 | [diff] [blame] | 321 | ThreadData* ThreadData::first() { |
| 322 | base::AutoLock lock(*list_lock_.Pointer()); |
| 323 | return all_thread_data_list_head_; |
| 324 | } |
| 325 | |
| 326 | ThreadData* ThreadData::next() const { return next_; } |
| 327 | |
| 328 | // static |
[email protected] | 84b5795 | 2011-10-15 23:52:45 | [diff] [blame] | 329 | void ThreadData::InitializeThreadContext(const std::string& suggested_name) { |
[email protected] | b2a9bbd | 2011-10-31 22:36:21 | [diff] [blame] | 330 | if (!Initialize()) // Always initialize if needed. |
| 331 | return; |
| 332 | ThreadData* current_thread_data = |
| 333 | reinterpret_cast<ThreadData*>(tls_index_.Get()); |
| 334 | if (current_thread_data) |
| 335 | return; // Browser tests instigate this. |
| 336 | current_thread_data = new ThreadData(suggested_name); |
[email protected] | 84baeca | 2011-10-24 18:55:16 | [diff] [blame] | 337 | tls_index_.Set(current_thread_data); |
[email protected] | 84b5795 | 2011-10-15 23:52:45 | [diff] [blame] | 338 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 339 | |
[email protected] | 84b5795 | 2011-10-15 23:52:45 | [diff] [blame] | 340 | // static |
| 341 | ThreadData* ThreadData::Get() { |
| 342 | if (!tls_index_.initialized()) |
| 343 | return NULL; // For unittests only. |
[email protected] | 84baeca | 2011-10-24 18:55:16 | [diff] [blame] | 344 | ThreadData* registered = reinterpret_cast<ThreadData*>(tls_index_.Get()); |
| 345 | if (registered) |
| 346 | return registered; |
| 347 | |
| 348 | // We must be a worker thread, since we didn't pre-register. |
| 349 | ThreadData* worker_thread_data = NULL; |
[email protected] | 9a88c90 | 2011-11-24 00:00:31 | [diff] [blame] | 350 | int worker_thread_number = 0; |
[email protected] | 84baeca | 2011-10-24 18:55:16 | [diff] [blame] | 351 | { |
[email protected] | 77169a6 | 2011-11-14 20:36:46 | [diff] [blame] | 352 | base::AutoLock lock(*list_lock_.Pointer()); |
[email protected] | 26cdeb96 | 2011-11-20 04:17:07 | [diff] [blame] | 353 | if (first_retired_worker_) { |
| 354 | worker_thread_data = first_retired_worker_; |
| 355 | first_retired_worker_ = first_retired_worker_->next_retired_worker_; |
| 356 | worker_thread_data->next_retired_worker_ = NULL; |
[email protected] | 445029fb | 2011-11-18 17:03:33 | [diff] [blame] | 357 | } else { |
[email protected] | 9a88c90 | 2011-11-24 00:00:31 | [diff] [blame] | 358 | worker_thread_number = ++worker_thread_data_creation_count_; |
[email protected] | 84baeca | 2011-10-24 18:55:16 | [diff] [blame] | 359 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 360 | } |
[email protected] | 84baeca | 2011-10-24 18:55:16 | [diff] [blame] | 361 | |
| 362 | // If we can't find a previously used instance, then we have to create one. |
[email protected] | 9a88c90 | 2011-11-24 00:00:31 | [diff] [blame] | 363 | if (!worker_thread_data) { |
| 364 | DCHECK_GT(worker_thread_number, 0); |
| 365 | worker_thread_data = new ThreadData(worker_thread_number); |
| 366 | } |
[email protected] | 26cdeb96 | 2011-11-20 04:17:07 | [diff] [blame] | 367 | DCHECK_GT(worker_thread_data->worker_thread_number_, 0); |
[email protected] | 84baeca | 2011-10-24 18:55:16 | [diff] [blame] | 368 | |
| 369 | tls_index_.Set(worker_thread_data); |
| 370 | return worker_thread_data; |
[email protected] | 84b5795 | 2011-10-15 23:52:45 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | // static |
[email protected] | 84baeca | 2011-10-24 18:55:16 | [diff] [blame] | 374 | void ThreadData::OnThreadTermination(void* thread_data) { |
[email protected] | 8aa1e6e | 2011-12-14 01:36:48 | [diff] [blame] | 375 | DCHECK(thread_data); // TLS should *never* call us with a NULL. |
[email protected] | 9a88c90 | 2011-11-24 00:00:31 | [diff] [blame] | 376 | // We must NOT do any allocations during this callback. There is a chance |
| 377 | // that the allocator is no longer active on this thread. |
[email protected] | 84baeca | 2011-10-24 18:55:16 | [diff] [blame] | 378 | if (!kTrackAllTaskObjects) |
| 379 | return; // Not compiled in. |
[email protected] | 84baeca | 2011-10-24 18:55:16 | [diff] [blame] | 380 | reinterpret_cast<ThreadData*>(thread_data)->OnThreadTerminationCleanup(); |
[email protected] | 84baeca | 2011-10-24 18:55:16 | [diff] [blame] | 381 | } |
| 382 | |
[email protected] | 26cdeb96 | 2011-11-20 04:17:07 | [diff] [blame] | 383 | void ThreadData::OnThreadTerminationCleanup() { |
[email protected] | 9a88c90 | 2011-11-24 00:00:31 | [diff] [blame] | 384 | // The list_lock_ was created when we registered the callback, so it won't be |
| 385 | // allocated here despite the lazy reference. |
[email protected] | 77169a6 | 2011-11-14 20:36:46 | [diff] [blame] | 386 | base::AutoLock lock(*list_lock_.Pointer()); |
[email protected] | b2a9bbd | 2011-10-31 22:36:21 | [diff] [blame] | 387 | if (incarnation_counter_ != incarnation_count_for_pool_) |
| 388 | return; // ThreadData was constructed in an earlier unit test. |
[email protected] | 9a88c90 | 2011-11-24 00:00:31 | [diff] [blame] | 389 | ++cleanup_count_; |
| 390 | // Only worker threads need to be retired and reused. |
| 391 | if (!worker_thread_number_) { |
| 392 | return; |
| 393 | } |
[email protected] | 26cdeb96 | 2011-11-20 04:17:07 | [diff] [blame] | 394 | // We must NOT do any allocations during this callback. |
| 395 | // Using the simple linked lists avoids all allocations. |
| 396 | DCHECK_EQ(this->next_retired_worker_, reinterpret_cast<ThreadData*>(NULL)); |
| 397 | this->next_retired_worker_ = first_retired_worker_; |
| 398 | first_retired_worker_ = this; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 399 | } |
| 400 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 401 | // static |
[email protected] | 1cb05db | 2012-04-13 00:39:26 | [diff] [blame] | 402 | void ThreadData::Snapshot(bool reset_max, ProcessDataSnapshot* process_data) { |
| 403 | // Add births that have run to completion to |collected_data|. |
| 404 | // |birth_counts| tracks the total number of births recorded at each location |
| 405 | // for which we have not seen a death count. |
| 406 | BirthCountMap birth_counts; |
| 407 | ThreadData::SnapshotAllExecutedTasks(reset_max, process_data, &birth_counts); |
| 408 | |
| 409 | // Add births that are still active -- i.e. objects that have tallied a birth, |
| 410 | // but have not yet tallied a matching death, and hence must be either |
| 411 | // running, queued up, or being held in limbo for future posting. |
| 412 | for (BirthCountMap::const_iterator it = birth_counts.begin(); |
| 413 | it != birth_counts.end(); ++it) { |
| 414 | if (it->second > 0) { |
| 415 | process_data->tasks.push_back( |
| 416 | TaskSnapshot(*it->first, DeathData(it->second), "Still_Alive")); |
| 417 | } |
| 418 | } |
[email protected] | 84baeca | 2011-10-24 18:55:16 | [diff] [blame] | 419 | } |
| 420 | |
[email protected] | 75b7920 | 2009-12-30 07:31:45 | [diff] [blame] | 421 | Births* ThreadData::TallyABirth(const Location& location) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 422 | BirthMap::iterator it = birth_map_.find(location); |
[email protected] | 8aa1e6e | 2011-12-14 01:36:48 | [diff] [blame] | 423 | Births* child; |
[email protected] | 75b7920 | 2009-12-30 07:31:45 | [diff] [blame] | 424 | if (it != birth_map_.end()) { |
[email protected] | 8aa1e6e | 2011-12-14 01:36:48 | [diff] [blame] | 425 | child = it->second; |
| 426 | child->RecordBirth(); |
| 427 | } else { |
| 428 | child = new Births(location, *this); // Leak this. |
| 429 | // Lock since the map may get relocated now, and other threads sometimes |
| 430 | // snapshot it (but they lock before copying it). |
| 431 | base::AutoLock lock(map_lock_); |
| 432 | birth_map_[location] = child; |
[email protected] | 75b7920 | 2009-12-30 07:31:45 | [diff] [blame] | 433 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 434 | |
[email protected] | 8aa1e6e | 2011-12-14 01:36:48 | [diff] [blame] | 435 | if (kTrackParentChildLinks && status_ > PROFILING_ACTIVE && |
| 436 | !parent_stack_.empty()) { |
| 437 | const Births* parent = parent_stack_.top(); |
| 438 | ParentChildPair pair(parent, child); |
| 439 | if (parent_child_set_.find(pair) == parent_child_set_.end()) { |
| 440 | // Lock since the map may get relocated now, and other threads sometimes |
| 441 | // snapshot it (but they lock before copying it). |
| 442 | base::AutoLock lock(map_lock_); |
| 443 | parent_child_set_.insert(pair); |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | return child; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 448 | } |
| 449 | |
[email protected] | 84baeca | 2011-10-24 18:55:16 | [diff] [blame] | 450 | void ThreadData::TallyADeath(const Births& birth, |
[email protected] | c186e96 | 2012-03-24 22:17:18 | [diff] [blame] | 451 | int32 queue_duration, |
vadimt | 12f0f7d | 2014-09-15 19:19:38 | [diff] [blame] | 452 | const TaskStopwatch& stopwatch) { |
| 453 | int32 run_duration = stopwatch.RunDurationMs(); |
| 454 | |
[email protected] | b6b2b89 | 2011-12-04 07:19:10 | [diff] [blame] | 455 | // Stir in some randomness, plus add constant in case durations are zero. |
glider | f7812575 | 2014-11-12 21:05:33 | [diff] [blame] | 456 | const uint32 kSomePrimeNumber = 2147483647; |
[email protected] | b6b2b89 | 2011-12-04 07:19:10 | [diff] [blame] | 457 | random_number_ += queue_duration + run_duration + kSomePrimeNumber; |
| 458 | // An address is going to have some randomness to it as well ;-). |
glider | f7812575 | 2014-11-12 21:05:33 | [diff] [blame] | 459 | random_number_ ^= static_cast<uint32>(&birth - reinterpret_cast<Births*>(0)); |
[email protected] | b6b2b89 | 2011-12-04 07:19:10 | [diff] [blame] | 460 | |
[email protected] | 90895d0f | 2012-02-15 23:05:01 | [diff] [blame] | 461 | // We don't have queue durations without OS timer. OS timer is automatically |
| 462 | // used for task-post-timing, so the use of an alternate timer implies all |
vadimt | 12f0f7d | 2014-09-15 19:19:38 | [diff] [blame] | 463 | // queue times are invalid, unless it was explicitly said that we can trust |
| 464 | // the alternate timer. |
| 465 | if (kAllowAlternateTimeSourceHandling && |
| 466 | now_function_ && |
| 467 | !now_function_is_time_) { |
[email protected] | 90895d0f | 2012-02-15 23:05:01 | [diff] [blame] | 468 | queue_duration = 0; |
vadimt | 12f0f7d | 2014-09-15 19:19:38 | [diff] [blame] | 469 | } |
[email protected] | 90895d0f | 2012-02-15 23:05:01 | [diff] [blame] | 470 | |
[email protected] | 84baeca | 2011-10-24 18:55:16 | [diff] [blame] | 471 | DeathMap::iterator it = death_map_.find(&birth); |
| 472 | DeathData* death_data; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 473 | if (it != death_map_.end()) { |
[email protected] | 84baeca | 2011-10-24 18:55:16 | [diff] [blame] | 474 | death_data = &it->second; |
| 475 | } else { |
[email protected] | 9a88c90 | 2011-11-24 00:00:31 | [diff] [blame] | 476 | base::AutoLock lock(map_lock_); // Lock as the map may get relocated now. |
[email protected] | 84baeca | 2011-10-24 18:55:16 | [diff] [blame] | 477 | death_data = &death_map_[&birth]; |
| 478 | } // Release lock ASAP. |
[email protected] | b6b2b89 | 2011-12-04 07:19:10 | [diff] [blame] | 479 | death_data->RecordDeath(queue_duration, run_duration, random_number_); |
[email protected] | 8aa1e6e | 2011-12-14 01:36:48 | [diff] [blame] | 480 | |
| 481 | if (!kTrackParentChildLinks) |
| 482 | return; |
| 483 | if (!parent_stack_.empty()) { // We might get turned off. |
| 484 | DCHECK_EQ(parent_stack_.top(), &birth); |
| 485 | parent_stack_.pop(); |
| 486 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | // static |
[email protected] | 180c85e | 2011-07-26 18:25:16 | [diff] [blame] | 490 | Births* ThreadData::TallyABirthIfActive(const Location& location) { |
[email protected] | 84baeca | 2011-10-24 18:55:16 | [diff] [blame] | 491 | if (!kTrackAllTaskObjects) |
| 492 | return NULL; // Not compiled in. |
| 493 | |
[email protected] | 702a12d | 2012-02-10 19:43:42 | [diff] [blame] | 494 | if (!TrackingStatus()) |
[email protected] | 84b5795 | 2011-10-15 23:52:45 | [diff] [blame] | 495 | return NULL; |
| 496 | ThreadData* current_thread_data = Get(); |
| 497 | if (!current_thread_data) |
| 498 | return NULL; |
| 499 | return current_thread_data->TallyABirth(location); |
[email protected] | 180c85e | 2011-07-26 18:25:16 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | // static |
[email protected] | b2a9bbd | 2011-10-31 22:36:21 | [diff] [blame] | 503 | void ThreadData::TallyRunOnNamedThreadIfTracking( |
| 504 | const base::TrackingInfo& completed_task, |
vadimt | 12f0f7d | 2014-09-15 19:19:38 | [diff] [blame] | 505 | const TaskStopwatch& stopwatch) { |
[email protected] | 84baeca | 2011-10-24 18:55:16 | [diff] [blame] | 506 | if (!kTrackAllTaskObjects) |
| 507 | return; // Not compiled in. |
| 508 | |
[email protected] | b2a9bbd | 2011-10-31 22:36:21 | [diff] [blame] | 509 | // Even if we have been DEACTIVATED, we will process any pending births so |
| 510 | // that our data structures (which counted the outstanding births) remain |
| 511 | // consistent. |
| 512 | const Births* birth = completed_task.birth_tally; |
| 513 | if (!birth) |
[email protected] | 84b5795 | 2011-10-15 23:52:45 | [diff] [blame] | 514 | return; |
vadimt | 12f0f7d | 2014-09-15 19:19:38 | [diff] [blame] | 515 | ThreadData* current_thread_data = stopwatch.GetThreadData(); |
[email protected] | 84b5795 | 2011-10-15 23:52:45 | [diff] [blame] | 516 | if (!current_thread_data) |
| 517 | return; |
| 518 | |
[email protected] | b2a9bbd | 2011-10-31 22:36:21 | [diff] [blame] | 519 | // Watch out for a race where status_ is changing, and hence one or both |
[email protected] | 8aa1e6e | 2011-12-14 01:36:48 | [diff] [blame] | 520 | // of start_of_run or end_of_run is zero. In that case, we didn't bother to |
[email protected] | b2a9bbd | 2011-10-31 22:36:21 | [diff] [blame] | 521 | // get a time value since we "weren't tracking" and we were trying to be |
| 522 | // efficient by not calling for a genuine time value. For simplicity, we'll |
| 523 | // use a default zero duration when we can't calculate a true value. |
vadimt | 12f0f7d | 2014-09-15 19:19:38 | [diff] [blame] | 524 | TrackedTime start_of_run = stopwatch.StartTime(); |
[email protected] | c186e96 | 2012-03-24 22:17:18 | [diff] [blame] | 525 | int32 queue_duration = 0; |
[email protected] | b2a9bbd | 2011-10-31 22:36:21 | [diff] [blame] | 526 | if (!start_of_run.is_null()) { |
[email protected] | e1a38d60 | 2013-07-10 17:50:22 | [diff] [blame] | 527 | queue_duration = (start_of_run - completed_task.EffectiveTimePosted()) |
| 528 | .InMilliseconds(); |
[email protected] | b2a9bbd | 2011-10-31 22:36:21 | [diff] [blame] | 529 | } |
vadimt | 12f0f7d | 2014-09-15 19:19:38 | [diff] [blame] | 530 | current_thread_data->TallyADeath(*birth, queue_duration, stopwatch); |
[email protected] | b2a9bbd | 2011-10-31 22:36:21 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | // static |
| 534 | void ThreadData::TallyRunOnWorkerThreadIfTracking( |
| 535 | const Births* birth, |
| 536 | const TrackedTime& time_posted, |
vadimt | 12f0f7d | 2014-09-15 19:19:38 | [diff] [blame] | 537 | const TaskStopwatch& stopwatch) { |
[email protected] | b2a9bbd | 2011-10-31 22:36:21 | [diff] [blame] | 538 | if (!kTrackAllTaskObjects) |
| 539 | return; // Not compiled in. |
| 540 | |
| 541 | // Even if we have been DEACTIVATED, we will process any pending births so |
| 542 | // that our data structures (which counted the outstanding births) remain |
| 543 | // consistent. |
| 544 | if (!birth) |
| 545 | return; |
| 546 | |
| 547 | // TODO(jar): Support the option to coalesce all worker-thread activity under |
| 548 | // one ThreadData instance that uses locks to protect *all* access. This will |
| 549 | // reduce memory (making it provably bounded), but run incrementally slower |
[email protected] | d6992b5b | 2013-05-20 18:53:13 | [diff] [blame] | 550 | // (since we'll use locks on TallyABirth and TallyADeath). The good news is |
| 551 | // that the locks on TallyADeath will be *after* the worker thread has run, |
| 552 | // and hence nothing will be waiting for the completion (... besides some |
| 553 | // other thread that might like to run). Also, the worker threads tasks are |
[email protected] | b2a9bbd | 2011-10-31 22:36:21 | [diff] [blame] | 554 | // generally longer, and hence the cost of the lock may perchance be amortized |
| 555 | // over the long task's lifetime. |
vadimt | 12f0f7d | 2014-09-15 19:19:38 | [diff] [blame] | 556 | ThreadData* current_thread_data = stopwatch.GetThreadData(); |
[email protected] | b2a9bbd | 2011-10-31 22:36:21 | [diff] [blame] | 557 | if (!current_thread_data) |
| 558 | return; |
| 559 | |
vadimt | 12f0f7d | 2014-09-15 19:19:38 | [diff] [blame] | 560 | TrackedTime start_of_run = stopwatch.StartTime(); |
[email protected] | c186e96 | 2012-03-24 22:17:18 | [diff] [blame] | 561 | int32 queue_duration = 0; |
[email protected] | b2a9bbd | 2011-10-31 22:36:21 | [diff] [blame] | 562 | if (!start_of_run.is_null()) { |
[email protected] | c25db18 | 2011-11-11 22:40:27 | [diff] [blame] | 563 | queue_duration = (start_of_run - time_posted).InMilliseconds(); |
[email protected] | b2a9bbd | 2011-10-31 22:36:21 | [diff] [blame] | 564 | } |
vadimt | 12f0f7d | 2014-09-15 19:19:38 | [diff] [blame] | 565 | current_thread_data->TallyADeath(*birth, queue_duration, stopwatch); |
[email protected] | 180c85e | 2011-07-26 18:25:16 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | // static |
[email protected] | dbe5d207 | 2011-11-08 17:09:21 | [diff] [blame] | 569 | void ThreadData::TallyRunInAScopedRegionIfTracking( |
| 570 | const Births* birth, |
vadimt | 12f0f7d | 2014-09-15 19:19:38 | [diff] [blame] | 571 | const TaskStopwatch& stopwatch) { |
[email protected] | dbe5d207 | 2011-11-08 17:09:21 | [diff] [blame] | 572 | if (!kTrackAllTaskObjects) |
| 573 | return; // Not compiled in. |
| 574 | |
| 575 | // Even if we have been DEACTIVATED, we will process any pending births so |
| 576 | // that our data structures (which counted the outstanding births) remain |
| 577 | // consistent. |
| 578 | if (!birth) |
| 579 | return; |
| 580 | |
vadimt | 12f0f7d | 2014-09-15 19:19:38 | [diff] [blame] | 581 | ThreadData* current_thread_data = stopwatch.GetThreadData(); |
[email protected] | dbe5d207 | 2011-11-08 17:09:21 | [diff] [blame] | 582 | if (!current_thread_data) |
| 583 | return; |
| 584 | |
[email protected] | c186e96 | 2012-03-24 22:17:18 | [diff] [blame] | 585 | int32 queue_duration = 0; |
vadimt | 12f0f7d | 2014-09-15 19:19:38 | [diff] [blame] | 586 | current_thread_data->TallyADeath(*birth, queue_duration, stopwatch); |
[email protected] | dbe5d207 | 2011-11-08 17:09:21 | [diff] [blame] | 587 | } |
| 588 | |
[email protected] | 1cb05db | 2012-04-13 00:39:26 | [diff] [blame] | 589 | // static |
| 590 | void ThreadData::SnapshotAllExecutedTasks(bool reset_max, |
| 591 | ProcessDataSnapshot* process_data, |
| 592 | BirthCountMap* birth_counts) { |
| 593 | if (!kTrackAllTaskObjects) |
| 594 | return; // Not compiled in. |
| 595 | |
| 596 | // Get an unchanging copy of a ThreadData list. |
| 597 | ThreadData* my_list = ThreadData::first(); |
| 598 | |
| 599 | // Gather data serially. |
| 600 | // This hackish approach *can* get some slighly corrupt tallies, as we are |
| 601 | // grabbing values without the protection of a lock, but it has the advantage |
| 602 | // of working even with threads that don't have message loops. If a user |
| 603 | // sees any strangeness, they can always just run their stats gathering a |
| 604 | // second time. |
| 605 | for (ThreadData* thread_data = my_list; |
| 606 | thread_data; |
| 607 | thread_data = thread_data->next()) { |
| 608 | thread_data->SnapshotExecutedTasks(reset_max, process_data, birth_counts); |
| 609 | } |
| 610 | } |
| 611 | |
| 612 | void ThreadData::SnapshotExecutedTasks(bool reset_max, |
| 613 | ProcessDataSnapshot* process_data, |
| 614 | BirthCountMap* birth_counts) { |
| 615 | // Get copy of data, so that the data will not change during the iterations |
| 616 | // and processing. |
| 617 | ThreadData::BirthMap birth_map; |
| 618 | ThreadData::DeathMap death_map; |
| 619 | ThreadData::ParentChildSet parent_child_set; |
| 620 | SnapshotMaps(reset_max, &birth_map, &death_map, &parent_child_set); |
| 621 | |
| 622 | for (ThreadData::DeathMap::const_iterator it = death_map.begin(); |
| 623 | it != death_map.end(); ++it) { |
| 624 | process_data->tasks.push_back( |
| 625 | TaskSnapshot(*it->first, it->second, thread_name())); |
| 626 | (*birth_counts)[it->first] -= it->first->birth_count(); |
| 627 | } |
| 628 | |
| 629 | for (ThreadData::BirthMap::const_iterator it = birth_map.begin(); |
| 630 | it != birth_map.end(); ++it) { |
| 631 | (*birth_counts)[it->second] += it->second->birth_count(); |
| 632 | } |
| 633 | |
| 634 | if (!kTrackParentChildLinks) |
| 635 | return; |
| 636 | |
| 637 | for (ThreadData::ParentChildSet::const_iterator it = parent_child_set.begin(); |
| 638 | it != parent_child_set.end(); ++it) { |
| 639 | process_data->descendants.push_back(ParentChildPairSnapshot(*it)); |
| 640 | } |
| 641 | } |
| 642 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 643 | // This may be called from another thread. |
[email protected] | b6b2b89 | 2011-12-04 07:19:10 | [diff] [blame] | 644 | void ThreadData::SnapshotMaps(bool reset_max, |
| 645 | BirthMap* birth_map, |
[email protected] | 8aa1e6e | 2011-12-14 01:36:48 | [diff] [blame] | 646 | DeathMap* death_map, |
| 647 | ParentChildSet* parent_child_set) { |
[email protected] | 9a88c90 | 2011-11-24 00:00:31 | [diff] [blame] | 648 | base::AutoLock lock(map_lock_); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 649 | for (BirthMap::const_iterator it = birth_map_.begin(); |
| 650 | it != birth_map_.end(); ++it) |
[email protected] | b6b2b89 | 2011-12-04 07:19:10 | [diff] [blame] | 651 | (*birth_map)[it->first] = it->second; |
| 652 | for (DeathMap::iterator it = death_map_.begin(); |
| 653 | it != death_map_.end(); ++it) { |
| 654 | (*death_map)[it->first] = it->second; |
| 655 | if (reset_max) |
| 656 | it->second.ResetMax(); |
| 657 | } |
[email protected] | 8aa1e6e | 2011-12-14 01:36:48 | [diff] [blame] | 658 | |
| 659 | if (!kTrackParentChildLinks) |
| 660 | return; |
| 661 | |
| 662 | for (ParentChildSet::iterator it = parent_child_set_.begin(); |
| 663 | it != parent_child_set_.end(); ++it) |
| 664 | parent_child_set->insert(*it); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 665 | } |
| 666 | |
[email protected] | b6b2b89 | 2011-12-04 07:19:10 | [diff] [blame] | 667 | // static |
[email protected] | 75b7920 | 2009-12-30 07:31:45 | [diff] [blame] | 668 | void ThreadData::ResetAllThreadData() { |
[email protected] | 84baeca | 2011-10-24 18:55:16 | [diff] [blame] | 669 | ThreadData* my_list = first(); |
[email protected] | 75b7920 | 2009-12-30 07:31:45 | [diff] [blame] | 670 | |
| 671 | for (ThreadData* thread_data = my_list; |
| 672 | thread_data; |
| 673 | thread_data = thread_data->next()) |
| 674 | thread_data->Reset(); |
| 675 | } |
| 676 | |
| 677 | void ThreadData::Reset() { |
[email protected] | 9a88c90 | 2011-11-24 00:00:31 | [diff] [blame] | 678 | base::AutoLock lock(map_lock_); |
[email protected] | 75b7920 | 2009-12-30 07:31:45 | [diff] [blame] | 679 | for (DeathMap::iterator it = death_map_.begin(); |
| 680 | it != death_map_.end(); ++it) |
| 681 | it->second.Clear(); |
| 682 | for (BirthMap::iterator it = birth_map_.begin(); |
| 683 | it != birth_map_.end(); ++it) |
| 684 | it->second->Clear(); |
| 685 | } |
| 686 | |
[email protected] | 90895d0f | 2012-02-15 23:05:01 | [diff] [blame] | 687 | static void OptionallyInitializeAlternateTimer() { |
[email protected] | ed0fd00 | 2012-04-25 23:10:34 | [diff] [blame] | 688 | NowFunction* alternate_time_source = GetAlternateTimeSource(); |
| 689 | if (alternate_time_source) |
| 690 | ThreadData::SetAlternateTimeSource(alternate_time_source); |
[email protected] | 90895d0f | 2012-02-15 23:05:01 | [diff] [blame] | 691 | } |
| 692 | |
[email protected] | b2a9bbd | 2011-10-31 22:36:21 | [diff] [blame] | 693 | bool ThreadData::Initialize() { |
[email protected] | 84baeca | 2011-10-24 18:55:16 | [diff] [blame] | 694 | if (!kTrackAllTaskObjects) |
| 695 | return false; // Not compiled in. |
[email protected] | 8aa1e6e | 2011-12-14 01:36:48 | [diff] [blame] | 696 | if (status_ >= DEACTIVATED) |
[email protected] | 94b555ee | 2011-11-15 21:50:36 | [diff] [blame] | 697 | return true; // Someone else did the initialization. |
| 698 | // Due to racy lazy initialization in tests, we'll need to recheck status_ |
| 699 | // after we acquire the lock. |
| 700 | |
| 701 | // Ensure that we don't double initialize tls. We are called when single |
| 702 | // threaded in the product, but some tests may be racy and lazy about our |
| 703 | // initialization. |
| 704 | base::AutoLock lock(*list_lock_.Pointer()); |
[email protected] | 8aa1e6e | 2011-12-14 01:36:48 | [diff] [blame] | 705 | if (status_ >= DEACTIVATED) |
[email protected] | 94b555ee | 2011-11-15 21:50:36 | [diff] [blame] | 706 | return true; // Someone raced in here and beat us. |
| 707 | |
[email protected] | 90895d0f | 2012-02-15 23:05:01 | [diff] [blame] | 708 | // Put an alternate timer in place if the environment calls for it, such as |
| 709 | // for tracking TCMalloc allocations. This insertion is idempotent, so we |
| 710 | // don't mind if there is a race, and we'd prefer not to be in a lock while |
| 711 | // doing this work. |
| 712 | if (kAllowAlternateTimeSourceHandling) |
| 713 | OptionallyInitializeAlternateTimer(); |
| 714 | |
[email protected] | b2a9bbd | 2011-10-31 22:36:21 | [diff] [blame] | 715 | // Perform the "real" TLS initialization now, and leave it intact through |
[email protected] | 84baeca | 2011-10-24 18:55:16 | [diff] [blame] | 716 | // process termination. |
[email protected] | 94b555ee | 2011-11-15 21:50:36 | [diff] [blame] | 717 | if (!tls_index_.initialized()) { // Testing may have initialized this. |
[email protected] | 8aa1e6e | 2011-12-14 01:36:48 | [diff] [blame] | 718 | DCHECK_EQ(status_, UNINITIALIZED); |
[email protected] | 84baeca | 2011-10-24 18:55:16 | [diff] [blame] | 719 | tls_index_.Initialize(&ThreadData::OnThreadTermination); |
[email protected] | 94b555ee | 2011-11-15 21:50:36 | [diff] [blame] | 720 | if (!tls_index_.initialized()) |
| 721 | return false; |
[email protected] | 8aa1e6e | 2011-12-14 01:36:48 | [diff] [blame] | 722 | } else { |
| 723 | // TLS was initialzed for us earlier. |
| 724 | DCHECK_EQ(status_, DORMANT_DURING_TESTS); |
[email protected] | 94b555ee | 2011-11-15 21:50:36 | [diff] [blame] | 725 | } |
[email protected] | 3f095c0a | 2011-10-31 15:32:08 | [diff] [blame] | 726 | |
[email protected] | 94b555ee | 2011-11-15 21:50:36 | [diff] [blame] | 727 | // Incarnation counter is only significant to testing, as it otherwise will |
| 728 | // never again change in this process. |
[email protected] | b2a9bbd | 2011-10-31 22:36:21 | [diff] [blame] | 729 | ++incarnation_counter_; |
[email protected] | 94b555ee | 2011-11-15 21:50:36 | [diff] [blame] | 730 | |
| 731 | // The lock is not critical for setting status_, but it doesn't hurt. It also |
| 732 | // ensures that if we have a racy initialization, that we'll bail as soon as |
| 733 | // we get the lock earlier in this method. |
| 734 | status_ = kInitialStartupState; |
[email protected] | 8aa1e6e | 2011-12-14 01:36:48 | [diff] [blame] | 735 | if (!kTrackParentChildLinks && |
| 736 | kInitialStartupState == PROFILING_CHILDREN_ACTIVE) |
| 737 | status_ = PROFILING_ACTIVE; |
[email protected] | 94b555ee | 2011-11-15 21:50:36 | [diff] [blame] | 738 | DCHECK(status_ != UNINITIALIZED); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 739 | return true; |
| 740 | } |
| 741 | |
| 742 | // static |
[email protected] | 702a12d | 2012-02-10 19:43:42 | [diff] [blame] | 743 | bool ThreadData::InitializeAndSetTrackingStatus(Status status) { |
| 744 | DCHECK_GE(status, DEACTIVATED); |
| 745 | DCHECK_LE(status, PROFILING_CHILDREN_ACTIVE); |
| 746 | |
[email protected] | b2a9bbd | 2011-10-31 22:36:21 | [diff] [blame] | 747 | if (!Initialize()) // No-op if already initialized. |
| 748 | return false; // Not compiled in. |
| 749 | |
[email protected] | 702a12d | 2012-02-10 19:43:42 | [diff] [blame] | 750 | if (!kTrackParentChildLinks && status > DEACTIVATED) |
| 751 | status = PROFILING_ACTIVE; |
| 752 | status_ = status; |
[email protected] | b2a9bbd | 2011-10-31 22:36:21 | [diff] [blame] | 753 | return true; |
| 754 | } |
| 755 | |
| 756 | // static |
[email protected] | 702a12d | 2012-02-10 19:43:42 | [diff] [blame] | 757 | ThreadData::Status ThreadData::status() { |
| 758 | return status_; |
| 759 | } |
| 760 | |
| 761 | // static |
| 762 | bool ThreadData::TrackingStatus() { |
[email protected] | 8aa1e6e | 2011-12-14 01:36:48 | [diff] [blame] | 763 | return status_ > DEACTIVATED; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | // static |
[email protected] | 702a12d | 2012-02-10 19:43:42 | [diff] [blame] | 767 | bool ThreadData::TrackingParentChildStatus() { |
[email protected] | 8aa1e6e | 2011-12-14 01:36:48 | [diff] [blame] | 768 | return status_ >= PROFILING_CHILDREN_ACTIVE; |
| 769 | } |
| 770 | |
| 771 | // static |
vadimt | 12f0f7d | 2014-09-15 19:19:38 | [diff] [blame] | 772 | void ThreadData::PrepareForStartOfRun(const Births* parent) { |
[email protected] | 8aa1e6e | 2011-12-14 01:36:48 | [diff] [blame] | 773 | if (kTrackParentChildLinks && parent && status_ > PROFILING_ACTIVE) { |
| 774 | ThreadData* current_thread_data = Get(); |
| 775 | if (current_thread_data) |
| 776 | current_thread_data->parent_stack_.push(parent); |
| 777 | } |
[email protected] | dda9768 | 2011-11-14 05:24:07 | [diff] [blame] | 778 | } |
| 779 | |
| 780 | // static |
[email protected] | 90895d0f | 2012-02-15 23:05:01 | [diff] [blame] | 781 | void ThreadData::SetAlternateTimeSource(NowFunction* now_function) { |
| 782 | DCHECK(now_function); |
| 783 | if (kAllowAlternateTimeSourceHandling) |
| 784 | now_function_ = now_function; |
| 785 | } |
| 786 | |
| 787 | // static |
vadimt | a156831 | 2014-11-06 22:27:43 | [diff] [blame] | 788 | void ThreadData::EnableProfilerTiming() { |
| 789 | base::subtle::NoBarrier_Store(&g_profiler_timing_enabled, ENABLED_TIMING); |
| 790 | } |
| 791 | |
| 792 | // static |
[email protected] | b2a9bbd | 2011-10-31 22:36:21 | [diff] [blame] | 793 | TrackedTime ThreadData::Now() { |
[email protected] | 90895d0f | 2012-02-15 23:05:01 | [diff] [blame] | 794 | if (kAllowAlternateTimeSourceHandling && now_function_) |
| 795 | return TrackedTime::FromMilliseconds((*now_function_)()); |
[email protected] | 915b344f | 2013-12-11 12:49:17 | [diff] [blame] | 796 | if (kTrackAllTaskObjects && IsProfilerTimingEnabled() && TrackingStatus()) |
[email protected] | b2a9bbd | 2011-10-31 22:36:21 | [diff] [blame] | 797 | return TrackedTime::Now(); |
| 798 | return TrackedTime(); // Super fast when disabled, or not compiled. |
[email protected] | 84b5795 | 2011-10-15 23:52:45 | [diff] [blame] | 799 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 800 | |
| 801 | // static |
[email protected] | 9a88c90 | 2011-11-24 00:00:31 | [diff] [blame] | 802 | void ThreadData::EnsureCleanupWasCalled(int major_threads_shutdown_count) { |
| 803 | base::AutoLock lock(*list_lock_.Pointer()); |
| 804 | if (worker_thread_data_creation_count_ == 0) |
| 805 | return; // We haven't really run much, and couldn't have leaked. |
[email protected] | 30de3a3 | 2014-03-14 18:25:48 | [diff] [blame] | 806 | |
| 807 | // TODO(jar): until this is working on XP, don't run the real test. |
| 808 | #if 0 |
[email protected] | 9a88c90 | 2011-11-24 00:00:31 | [diff] [blame] | 809 | // Verify that we've at least shutdown/cleanup the major namesd threads. The |
| 810 | // caller should tell us how many thread shutdowns should have taken place by |
| 811 | // now. |
[email protected] | 9a88c90 | 2011-11-24 00:00:31 | [diff] [blame] | 812 | CHECK_GT(cleanup_count_, major_threads_shutdown_count); |
[email protected] | 30de3a3 | 2014-03-14 18:25:48 | [diff] [blame] | 813 | #endif |
[email protected] | 9a88c90 | 2011-11-24 00:00:31 | [diff] [blame] | 814 | } |
| 815 | |
| 816 | // static |
[email protected] | b2a9bbd | 2011-10-31 22:36:21 | [diff] [blame] | 817 | void ThreadData::ShutdownSingleThreadedCleanup(bool leak) { |
[email protected] | 84baeca | 2011-10-24 18:55:16 | [diff] [blame] | 818 | // This is only called from test code, where we need to cleanup so that |
| 819 | // additional tests can be run. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 820 | // We must be single threaded... but be careful anyway. |
[email protected] | 702a12d | 2012-02-10 19:43:42 | [diff] [blame] | 821 | if (!InitializeAndSetTrackingStatus(DEACTIVATED)) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 822 | return; |
| 823 | ThreadData* thread_data_list; |
| 824 | { |
[email protected] | 77169a6 | 2011-11-14 20:36:46 | [diff] [blame] | 825 | base::AutoLock lock(*list_lock_.Pointer()); |
[email protected] | 84baeca | 2011-10-24 18:55:16 | [diff] [blame] | 826 | thread_data_list = all_thread_data_list_head_; |
| 827 | all_thread_data_list_head_ = NULL; |
[email protected] | b2a9bbd | 2011-10-31 22:36:21 | [diff] [blame] | 828 | ++incarnation_counter_; |
[email protected] | 26cdeb96 | 2011-11-20 04:17:07 | [diff] [blame] | 829 | // To be clean, break apart the retired worker list (though we leak them). |
[email protected] | b6b2b89 | 2011-12-04 07:19:10 | [diff] [blame] | 830 | while (first_retired_worker_) { |
[email protected] | 26cdeb96 | 2011-11-20 04:17:07 | [diff] [blame] | 831 | ThreadData* worker = first_retired_worker_; |
| 832 | CHECK_GT(worker->worker_thread_number_, 0); |
| 833 | first_retired_worker_ = worker->next_retired_worker_; |
| 834 | worker->next_retired_worker_ = NULL; |
| 835 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 836 | } |
| 837 | |
[email protected] | b2a9bbd | 2011-10-31 22:36:21 | [diff] [blame] | 838 | // Put most global static back in pristine shape. |
[email protected] | 9a88c90 | 2011-11-24 00:00:31 | [diff] [blame] | 839 | worker_thread_data_creation_count_ = 0; |
| 840 | cleanup_count_ = 0; |
[email protected] | b2a9bbd | 2011-10-31 22:36:21 | [diff] [blame] | 841 | tls_index_.Set(NULL); |
[email protected] | 8aa1e6e | 2011-12-14 01:36:48 | [diff] [blame] | 842 | status_ = DORMANT_DURING_TESTS; // Almost UNINITIALIZED. |
[email protected] | b2a9bbd | 2011-10-31 22:36:21 | [diff] [blame] | 843 | |
| 844 | // To avoid any chance of racing in unit tests, which is the only place we |
| 845 | // call this function, we may sometimes leak all the data structures we |
| 846 | // recovered, as they may still be in use on threads from prior tests! |
[email protected] | bf709abd | 2013-06-10 11:32:20 | [diff] [blame] | 847 | if (leak) { |
| 848 | ThreadData* thread_data = thread_data_list; |
| 849 | while (thread_data) { |
| 850 | ANNOTATE_LEAKING_OBJECT_PTR(thread_data); |
| 851 | thread_data = thread_data->next(); |
| 852 | } |
[email protected] | b2a9bbd | 2011-10-31 22:36:21 | [diff] [blame] | 853 | return; |
[email protected] | bf709abd | 2013-06-10 11:32:20 | [diff] [blame] | 854 | } |
[email protected] | b2a9bbd | 2011-10-31 22:36:21 | [diff] [blame] | 855 | |
| 856 | // When we want to cleanup (on a single thread), here is what we do. |
| 857 | |
[email protected] | 84baeca | 2011-10-24 18:55:16 | [diff] [blame] | 858 | // Do actual recursive delete in all ThreadData instances. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 859 | while (thread_data_list) { |
| 860 | ThreadData* next_thread_data = thread_data_list; |
| 861 | thread_data_list = thread_data_list->next(); |
| 862 | |
| 863 | for (BirthMap::iterator it = next_thread_data->birth_map_.begin(); |
| 864 | next_thread_data->birth_map_.end() != it; ++it) |
| 865 | delete it->second; // Delete the Birth Records. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 866 | delete next_thread_data; // Includes all Death Records. |
| 867 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 868 | } |
| 869 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 870 | //------------------------------------------------------------------------------ |
vadimt | 12f0f7d | 2014-09-15 19:19:38 | [diff] [blame] | 871 | TaskStopwatch::TaskStopwatch() |
vadimt | 2017553 | 2014-10-28 20:14:20 | [diff] [blame] | 872 | : wallclock_duration_ms_(0), |
| 873 | current_thread_data_(NULL), |
vadimt | 12f0f7d | 2014-09-15 19:19:38 | [diff] [blame] | 874 | excluded_duration_ms_(0), |
| 875 | parent_(NULL) { |
| 876 | #if DCHECK_IS_ON |
vadimt | 2017553 | 2014-10-28 20:14:20 | [diff] [blame] | 877 | state_ = CREATED; |
vadimt | 12f0f7d | 2014-09-15 19:19:38 | [diff] [blame] | 878 | child_ = NULL; |
| 879 | #endif |
vadimt | 2017553 | 2014-10-28 20:14:20 | [diff] [blame] | 880 | } |
vadimt | 12f0f7d | 2014-09-15 19:19:38 | [diff] [blame] | 881 | |
vadimt | 2017553 | 2014-10-28 20:14:20 | [diff] [blame] | 882 | TaskStopwatch::~TaskStopwatch() { |
| 883 | #if DCHECK_IS_ON |
| 884 | DCHECK(state_ != RUNNING); |
| 885 | DCHECK(child_ == NULL); |
| 886 | #endif |
| 887 | } |
| 888 | |
| 889 | void TaskStopwatch::Start() { |
| 890 | #if DCHECK_IS_ON |
| 891 | DCHECK(state_ == CREATED); |
| 892 | state_ = RUNNING; |
| 893 | #endif |
| 894 | |
| 895 | start_time_ = ThreadData::Now(); |
| 896 | |
| 897 | current_thread_data_ = ThreadData::Get(); |
vadimt | 12f0f7d | 2014-09-15 19:19:38 | [diff] [blame] | 898 | if (!current_thread_data_) |
| 899 | return; |
| 900 | |
| 901 | parent_ = current_thread_data_->current_stopwatch_; |
| 902 | #if DCHECK_IS_ON |
| 903 | if (parent_) { |
| 904 | DCHECK(parent_->state_ == RUNNING); |
| 905 | DCHECK(parent_->child_ == NULL); |
| 906 | parent_->child_ = this; |
| 907 | } |
| 908 | #endif |
| 909 | current_thread_data_->current_stopwatch_ = this; |
| 910 | } |
| 911 | |
vadimt | 12f0f7d | 2014-09-15 19:19:38 | [diff] [blame] | 912 | void TaskStopwatch::Stop() { |
| 913 | const TrackedTime end_time = ThreadData::Now(); |
| 914 | #if DCHECK_IS_ON |
| 915 | DCHECK(state_ == RUNNING); |
| 916 | state_ = STOPPED; |
| 917 | DCHECK(child_ == NULL); |
| 918 | #endif |
| 919 | |
| 920 | if (!start_time_.is_null() && !end_time.is_null()) { |
| 921 | wallclock_duration_ms_ = (end_time - start_time_).InMilliseconds(); |
| 922 | } |
| 923 | |
| 924 | if (!current_thread_data_) |
| 925 | return; |
| 926 | |
| 927 | DCHECK(current_thread_data_->current_stopwatch_ == this); |
| 928 | current_thread_data_->current_stopwatch_ = parent_; |
| 929 | if (!parent_) |
| 930 | return; |
| 931 | |
| 932 | #if DCHECK_IS_ON |
| 933 | DCHECK(parent_->state_ == RUNNING); |
| 934 | DCHECK(parent_->child_ == this); |
| 935 | parent_->child_ = NULL; |
| 936 | #endif |
vadimt | 2017553 | 2014-10-28 20:14:20 | [diff] [blame] | 937 | parent_->excluded_duration_ms_ += wallclock_duration_ms_; |
vadimt | 12f0f7d | 2014-09-15 19:19:38 | [diff] [blame] | 938 | parent_ = NULL; |
| 939 | } |
| 940 | |
| 941 | TrackedTime TaskStopwatch::StartTime() const { |
vadimt | 2017553 | 2014-10-28 20:14:20 | [diff] [blame] | 942 | #if DCHECK_IS_ON |
| 943 | DCHECK(state_ != CREATED); |
| 944 | #endif |
| 945 | |
vadimt | 12f0f7d | 2014-09-15 19:19:38 | [diff] [blame] | 946 | return start_time_; |
| 947 | } |
| 948 | |
| 949 | int32 TaskStopwatch::RunDurationMs() const { |
| 950 | #if DCHECK_IS_ON |
| 951 | DCHECK(state_ == STOPPED); |
| 952 | #endif |
| 953 | |
| 954 | return wallclock_duration_ms_ - excluded_duration_ms_; |
| 955 | } |
| 956 | |
| 957 | ThreadData* TaskStopwatch::GetThreadData() const { |
vadimt | 2017553 | 2014-10-28 20:14:20 | [diff] [blame] | 958 | #if DCHECK_IS_ON |
| 959 | DCHECK(state_ != CREATED); |
| 960 | #endif |
| 961 | |
vadimt | 12f0f7d | 2014-09-15 19:19:38 | [diff] [blame] | 962 | return current_thread_data_; |
| 963 | } |
| 964 | |
| 965 | //------------------------------------------------------------------------------ |
[email protected] | 1cb05db | 2012-04-13 00:39:26 | [diff] [blame] | 966 | TaskSnapshot::TaskSnapshot() { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 967 | } |
| 968 | |
[email protected] | 1cb05db | 2012-04-13 00:39:26 | [diff] [blame] | 969 | TaskSnapshot::TaskSnapshot(const BirthOnThread& birth, |
| 970 | const DeathData& death_data, |
| 971 | const std::string& death_thread_name) |
| 972 | : birth(birth), |
| 973 | death_data(death_data), |
| 974 | death_thread_name(death_thread_name) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 975 | } |
| 976 | |
[email protected] | 1cb05db | 2012-04-13 00:39:26 | [diff] [blame] | 977 | TaskSnapshot::~TaskSnapshot() { |
[email protected] | 84baeca | 2011-10-24 18:55:16 | [diff] [blame] | 978 | } |
| 979 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 980 | //------------------------------------------------------------------------------ |
[email protected] | 1cb05db | 2012-04-13 00:39:26 | [diff] [blame] | 981 | // ParentChildPairSnapshot |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 982 | |
[email protected] | 15dd901 | 2012-07-26 22:03:01 | [diff] [blame] | 983 | ParentChildPairSnapshot::ParentChildPairSnapshot() { |
[email protected] | d4799a3 | 2010-09-28 22:54:58 | [diff] [blame] | 984 | } |
| 985 | |
[email protected] | 1cb05db | 2012-04-13 00:39:26 | [diff] [blame] | 986 | ParentChildPairSnapshot::ParentChildPairSnapshot( |
| 987 | const ThreadData::ParentChildPair& parent_child) |
| 988 | : parent(*parent_child.first), |
| 989 | child(*parent_child.second) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 990 | } |
| 991 | |
[email protected] | 1cb05db | 2012-04-13 00:39:26 | [diff] [blame] | 992 | ParentChildPairSnapshot::~ParentChildPairSnapshot() { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 993 | } |
| 994 | |
[email protected] | 1cb05db | 2012-04-13 00:39:26 | [diff] [blame] | 995 | //------------------------------------------------------------------------------ |
| 996 | // ProcessDataSnapshot |
| 997 | |
| 998 | ProcessDataSnapshot::ProcessDataSnapshot() |
[email protected] | fe5d406 | 2012-04-23 21:18:19 | [diff] [blame] | 999 | #if !defined(OS_NACL) |
[email protected] | 1cb05db | 2012-04-13 00:39:26 | [diff] [blame] | 1000 | : process_id(base::GetCurrentProcId()) { |
[email protected] | fe5d406 | 2012-04-23 21:18:19 | [diff] [blame] | 1001 | #else |
| 1002 | : process_id(0) { |
| 1003 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1004 | } |
| 1005 | |
[email protected] | 1cb05db | 2012-04-13 00:39:26 | [diff] [blame] | 1006 | ProcessDataSnapshot::~ProcessDataSnapshot() { |
[email protected] | 84baeca | 2011-10-24 18:55:16 | [diff] [blame] | 1007 | } |
| 1008 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1009 | } // namespace tracked_objects |