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