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