blob: 56b44c10b2c389fbb053bee442769c0eda51941a [file] [log] [blame]
fischman@chromium.org998561e2012-01-24 07:56:41 +09001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botf003cfe2008-08-24 09:55:55 +09002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit3f4a7322008-07-27 06:49:38 +09004
5#include "base/tracked_objects.h"
6
mostynb@opera.comf29dd612013-09-04 08:29:12 +09007#include <limits.h>
vapier@chromium.org58b69ff2012-03-20 06:46:27 +09008#include <stdlib.h>
darin@google.comee6fa722008-08-13 08:25:43 +09009
qsr@chromium.org67f4ef82013-12-13 04:29:10 +090010#include "base/atomicops.h"
qsr@chromium.org4b1cc322013-12-11 21:49:17 +090011#include "base/base_switches.h"
12#include "base/command_line.h"
eugenis@chromium.org5eff8312013-03-21 06:18:22 +090013#include "base/compiler_specific.h"
earthdok@google.comeb54f422013-06-10 20:32:20 +090014#include "base/debug/leak_annotations.h"
mostynb@opera.comf29dd612013-09-04 08:29:12 +090015#include "base/logging.h"
rsesek@chromium.org687756f2013-07-26 06:38:23 +090016#include "base/process/process_handle.h"
jar@chromium.org4626ccb2012-02-16 08:05:01 +090017#include "base/profiler/alternate_timer.h"
avi@chromium.org68a745c2013-06-11 05:11:14 +090018#include "base/strings/stringprintf.h"
rnk@chromium.org1a280df2011-12-05 06:14:05 +090019#include "base/third_party/valgrind/memcheck.h"
mostynb@opera.comf29dd612013-09-04 08:29:12 +090020#include "base/tracking_info.h"
initial.commit3f4a7322008-07-27 06:49:38 +090021
dsh@google.com0f8dd262008-10-28 05:43:33 +090022using base::TimeDelta;
23
mostynb@opera.comf29dd612013-09-04 08:29:12 +090024namespace base {
25class TimeDelta;
26}
27
initial.commit3f4a7322008-07-27 06:49:38 +090028namespace tracked_objects {
29
jar@chromium.org4be2cb02011-11-01 07:36:21 +090030namespace {
31// Flag to compile out almost all of the task tracking code.
jhawkins@chromium.org88fbaf62012-01-28 09:34:40 +090032const bool kTrackAllTaskObjects = true;
joth@chromium.orgc8b867c2011-11-01 00:32:08 +090033
isherman@chromium.org98c10d22012-04-13 09:39:26 +090034// TODO(jar): Evaluate the perf impact of enabling this. If the perf impact is
35// negligible, enable by default.
jar@chromium.orgb5c974b2011-12-14 10:36:48 +090036// Flag to compile out parent-child link recording.
jhawkins@chromium.org88fbaf62012-01-28 09:34:40 +090037const bool kTrackParentChildLinks = false;
jar@chromium.orgb5c974b2011-12-14 10:36:48 +090038
jar@chromium.org4be2cb02011-11-01 07:36:21 +090039// 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.org88fbaf62012-01-28 09:34:40 +090045const ThreadData::Status kInitialStartupState =
jar@chromium.orgb5c974b2011-12-14 10:36:48 +090046 ThreadData::PROFILING_CHILDREN_ACTIVE;
jhawkins@chromium.org88fbaf62012-01-28 09:34:40 +090047
jar@chromium.org4626ccb2012-02-16 08:05:01 +090048// 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).
55static const bool kAllowAlternateTimeSourceHandling = true;
isherman@chromium.org98c10d22012-04-13 09:39:26 +090056
qsr@chromium.org4b1cc322013-12-11 21:49:17 +090057inline bool IsProfilerTimingEnabled() {
qsr@chromium.org67f4ef82013-12-13 04:29:10 +090058 enum {
qsr@chromium.org4b1cc322013-12-11 21:49:17 +090059 UNDEFINED_TIMING,
60 ENABLED_TIMING,
61 DISABLED_TIMING,
qsr@chromium.org67f4ef82013-12-13 04:29:10 +090062 };
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.org4b1cc322013-12-11 21:49:17 +090070 if (!CommandLine::InitializedForCurrentProcess())
71 return true;
qsr@chromium.org67f4ef82013-12-13 04:29:10 +090072 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.org4b1cc322013-12-11 21:49:17 +090079 }
qsr@chromium.org67f4ef82013-12-13 04:29:10 +090080 return current_timing_enabled == ENABLED_TIMING;
qsr@chromium.org4b1cc322013-12-11 21:49:17 +090081}
82
jar@chromium.orgb5c974b2011-12-14 10:36:48 +090083} // namespace
jar@chromium.org79a58c32011-10-16 08:52:45 +090084
initial.commit3f4a7322008-07-27 06:49:38 +090085//------------------------------------------------------------------------------
jar@chromium.org0d46f3b2011-11-04 09:23:27 +090086// DeathData tallies durations when a death takes place.
initial.commit3f4a7322008-07-27 06:49:38 +090087
jar@chromium.orga0260412011-12-04 16:19:10 +090088DeathData::DeathData() {
89 Clear();
90}
91
92DeathData::DeathData(int count) {
93 Clear();
94 count_ = count;
95}
96
jar@chromium.org26abc1f2011-12-09 12:41:04 +090097// TODO(jar): I need to see if this macro to optimize branching is worth using.
jar@chromium.orga0260412011-12-04 16:19:10 +090098//
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.org3306fc02012-03-25 07:17:18 +0900105 ((target) ^= ((target) ^ (source)) & -static_cast<int32>(assign_it))
jar@chromium.orga0260412011-12-04 16:19:10 +0900106
isherman@chromium.org3306fc02012-03-25 07:17:18 +0900107void DeathData::RecordDeath(const int32 queue_duration,
108 const int32 run_duration,
jar@chromium.orga0260412011-12-04 16:19:10 +0900109 int32 random_number) {
rtenneti@chromium.org086c9d22013-02-28 13:15:43 +0900110 // 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.orga0260412011-12-04 16:19:10 +0900113 queue_duration_sum_ += queue_duration;
114 run_duration_sum_ += run_duration;
jar@chromium.org26abc1f2011-12-09 12:41:04 +0900115
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.orga0260412011-12-04 16:19:10 +0900120
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.orgeff10e42012-07-27 07:03:01 +0900123 // 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.org086c9d22013-02-28 13:15:43 +0900127 CHECK_GT(count_, 0);
jar@chromium.org26abc1f2011-12-09 12:41:04 +0900128 if (0 == (random_number % count_)) {
129 queue_duration_sample_ = queue_duration;
130 run_duration_sample_ = run_duration;
131 }
initial.commit3f4a7322008-07-27 06:49:38 +0900132}
133
jar@chromium.orga0260412011-12-04 16:19:10 +0900134int DeathData::count() const { return count_; }
135
isherman@chromium.org3306fc02012-03-25 07:17:18 +0900136int32 DeathData::run_duration_sum() const { return run_duration_sum_; }
jar@chromium.orga0260412011-12-04 16:19:10 +0900137
isherman@chromium.org3306fc02012-03-25 07:17:18 +0900138int32 DeathData::run_duration_max() const { return run_duration_max_; }
jar@chromium.orga0260412011-12-04 16:19:10 +0900139
isherman@chromium.org3306fc02012-03-25 07:17:18 +0900140int32 DeathData::run_duration_sample() const {
jar@chromium.orga0260412011-12-04 16:19:10 +0900141 return run_duration_sample_;
initial.commit3f4a7322008-07-27 06:49:38 +0900142}
143
isherman@chromium.org3306fc02012-03-25 07:17:18 +0900144int32 DeathData::queue_duration_sum() const {
jar@chromium.orga0260412011-12-04 16:19:10 +0900145 return queue_duration_sum_;
initial.commit3f4a7322008-07-27 06:49:38 +0900146}
147
isherman@chromium.org3306fc02012-03-25 07:17:18 +0900148int32 DeathData::queue_duration_max() const {
jar@chromium.orga0260412011-12-04 16:19:10 +0900149 return queue_duration_max_;
initial.commit3f4a7322008-07-27 06:49:38 +0900150}
151
isherman@chromium.org3306fc02012-03-25 07:17:18 +0900152int32 DeathData::queue_duration_sample() const {
jar@chromium.orga0260412011-12-04 16:19:10 +0900153 return queue_duration_sample_;
154}
155
jar@chromium.orga0260412011-12-04 16:19:10 +0900156void DeathData::ResetMax() {
157 run_duration_max_ = 0;
158 queue_duration_max_ = 0;
159}
160
initial.commit3f4a7322008-07-27 06:49:38 +0900161void DeathData::Clear() {
162 count_ = 0;
jar@chromium.orga0260412011-12-04 16:19:10 +0900163 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.commit3f4a7322008-07-27 06:49:38 +0900169}
170
171//------------------------------------------------------------------------------
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900172DeathDataSnapshot::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
182DeathDataSnapshot::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
193DeathDataSnapshot::~DeathDataSnapshot() {
194}
195
196//------------------------------------------------------------------------------
jar@chromium.org666ef9c2011-10-25 03:55:16 +0900197BirthOnThread::BirthOnThread(const Location& location,
198 const ThreadData& current)
initial.commit3f4a7322008-07-27 06:49:38 +0900199 : location_(location),
jar@chromium.orga0260412011-12-04 16:19:10 +0900200 birth_thread_(&current) {
201}
202
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900203//------------------------------------------------------------------------------
204BirthOnThreadSnapshot::BirthOnThreadSnapshot() {
205}
initial.commit3f4a7322008-07-27 06:49:38 +0900206
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900207BirthOnThreadSnapshot::BirthOnThreadSnapshot(
208 const tracked_objects::BirthOnThread& birth)
209 : location(birth.location()),
210 thread_name(birth.birth_thread()->thread_name()) {
211}
212
213BirthOnThreadSnapshot::~BirthOnThreadSnapshot() {
jar@chromium.orgb5c974b2011-12-14 10:36:48 +0900214}
215
initial.commit3f4a7322008-07-27 06:49:38 +0900216//------------------------------------------------------------------------------
jar@chromium.org666ef9c2011-10-25 03:55:16 +0900217Births::Births(const Location& location, const ThreadData& current)
218 : BirthOnThread(location, current),
jar@chromium.orgfebe0e42009-12-30 16:31:45 +0900219 birth_count_(1) { }
initial.commit3f4a7322008-07-27 06:49:38 +0900220
jar@chromium.orga0260412011-12-04 16:19:10 +0900221int Births::birth_count() const { return birth_count_; }
222
223void Births::RecordBirth() { ++birth_count_; }
224
225void Births::ForgetBirth() { --birth_count_; }
226
227void Births::Clear() { birth_count_ = 0; }
228
initial.commit3f4a7322008-07-27 06:49:38 +0900229//------------------------------------------------------------------------------
jar@chromium.orga0260412011-12-04 16:19:10 +0900230// ThreadData maintains the central data for all births and deaths on a single
231// thread.
initial.commit3f4a7322008-07-27 06:49:38 +0900232
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900233// 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.org4626ccb2012-02-16 08:05:01 +0900237// static
238NowFunction* ThreadData::now_function_ = NULL;
239
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900240// A TLS slot which points to the ThreadData instance for the current thread. We
241// do a fake initialization here (zeroing out data), and then the real in-place
242// construction happens when we call tls_index_.Initialize().
243// static
thakis@chromium.org82306bf2012-01-31 01:52:09 +0900244base::ThreadLocalStorage::StaticSlot ThreadData::tls_index_ = TLS_INITIALIZER;
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900245
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900246// static
jar@chromium.org92703d52011-11-24 09:00:31 +0900247int ThreadData::worker_thread_data_creation_count_ = 0;
248
249// static
250int ThreadData::cleanup_count_ = 0;
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900251
252// static
253int ThreadData::incarnation_counter_ = 0;
254
initial.commit3f4a7322008-07-27 06:49:38 +0900255// static
jar@chromium.org666ef9c2011-10-25 03:55:16 +0900256ThreadData* ThreadData::all_thread_data_list_head_ = NULL;
257
258// static
jar@chromium.org50c48db2011-11-20 13:17:07 +0900259ThreadData* ThreadData::first_retired_worker_ = NULL;
jar@chromium.org666ef9c2011-10-25 03:55:16 +0900260
initial.commit3f4a7322008-07-27 06:49:38 +0900261// static
fischman@chromium.org998561e2012-01-24 07:56:41 +0900262base::LazyInstance<base::Lock>::Leaky
joth@chromium.orgb24883c2011-11-15 22:31:49 +0900263 ThreadData::list_lock_ = LAZY_INSTANCE_INITIALIZER;
initial.commit3f4a7322008-07-27 06:49:38 +0900264
265// static
266ThreadData::Status ThreadData::status_ = ThreadData::UNINITIALIZED;
267
jar@chromium.org666ef9c2011-10-25 03:55:16 +0900268ThreadData::ThreadData(const std::string& suggested_name)
jar@chromium.orgb5c974b2011-12-14 10:36:48 +0900269 : next_(NULL),
jar@chromium.org50c48db2011-11-20 13:17:07 +0900270 next_retired_worker_(NULL),
jar@chromium.orgb5c974b2011-12-14 10:36:48 +0900271 worker_thread_number_(0),
272 incarnation_count_for_pool_(-1) {
jar@chromium.org79a58c32011-10-16 08:52:45 +0900273 DCHECK_GE(suggested_name.size(), 0u);
274 thread_name_ = suggested_name;
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900275 PushToHeadOfList(); // Which sets real incarnation_count_for_pool_.
jar@chromium.org79a58c32011-10-16 08:52:45 +0900276}
277
jar@chromium.org50c48db2011-11-20 13:17:07 +0900278ThreadData::ThreadData(int thread_number)
jar@chromium.orgb5c974b2011-12-14 10:36:48 +0900279 : next_(NULL),
jar@chromium.org50c48db2011-11-20 13:17:07 +0900280 next_retired_worker_(NULL),
jar@chromium.orgb5c974b2011-12-14 10:36:48 +0900281 worker_thread_number_(thread_number),
282 incarnation_count_for_pool_(-1) {
jar@chromium.org50c48db2011-11-20 13:17:07 +0900283 CHECK_GT(thread_number, 0);
284 base::StringAppendF(&thread_name_, "WorkerThread-%d", thread_number);
jar@chromium.org0d46f3b2011-11-04 09:23:27 +0900285 PushToHeadOfList(); // Which sets real incarnation_count_for_pool_.
willchan@chromium.org25726ef2010-11-20 05:34:18 +0900286}
initial.commit3f4a7322008-07-27 06:49:38 +0900287
erg@google.com71915232010-09-29 07:54:58 +0900288ThreadData::~ThreadData() {}
289
jar@chromium.org666ef9c2011-10-25 03:55:16 +0900290void ThreadData::PushToHeadOfList() {
jar@chromium.orga0260412011-12-04 16:19:10 +0900291 // Toss in a hint of randomness (atop the uniniitalized value).
rnk@chromium.org69708412011-12-06 00:24:28 +0900292 (void)VALGRIND_MAKE_MEM_DEFINED_IF_ADDRESSABLE(&random_number_,
rnk@chromium.org1a280df2011-12-05 06:14:05 +0900293 sizeof(random_number_));
eugenis@chromium.org5eff8312013-03-21 06:18:22 +0900294 MSAN_UNPOISON(&random_number_, sizeof(random_number_));
jar@chromium.orga0260412011-12-04 16:19:10 +0900295 random_number_ += static_cast<int32>(this - static_cast<ThreadData*>(0));
296 random_number_ ^= (Now() - TrackedTime()).InMilliseconds();
297
jar@chromium.org666ef9c2011-10-25 03:55:16 +0900298 DCHECK(!next_);
jar@chromium.orgb2845c82011-11-15 05:36:46 +0900299 base::AutoLock lock(*list_lock_.Pointer());
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900300 incarnation_count_for_pool_ = incarnation_counter_;
jar@chromium.org666ef9c2011-10-25 03:55:16 +0900301 next_ = all_thread_data_list_head_;
302 all_thread_data_list_head_ = this;
303}
304
initial.commit3f4a7322008-07-27 06:49:38 +0900305// static
jar@chromium.orga0260412011-12-04 16:19:10 +0900306ThreadData* ThreadData::first() {
307 base::AutoLock lock(*list_lock_.Pointer());
308 return all_thread_data_list_head_;
309}
310
311ThreadData* ThreadData::next() const { return next_; }
312
313// static
jar@chromium.org79a58c32011-10-16 08:52:45 +0900314void ThreadData::InitializeThreadContext(const std::string& suggested_name) {
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900315 if (!Initialize()) // Always initialize if needed.
316 return;
317 ThreadData* current_thread_data =
318 reinterpret_cast<ThreadData*>(tls_index_.Get());
319 if (current_thread_data)
320 return; // Browser tests instigate this.
321 current_thread_data = new ThreadData(suggested_name);
jar@chromium.org666ef9c2011-10-25 03:55:16 +0900322 tls_index_.Set(current_thread_data);
jar@chromium.org79a58c32011-10-16 08:52:45 +0900323}
initial.commit3f4a7322008-07-27 06:49:38 +0900324
jar@chromium.org79a58c32011-10-16 08:52:45 +0900325// static
326ThreadData* ThreadData::Get() {
327 if (!tls_index_.initialized())
328 return NULL; // For unittests only.
jar@chromium.org666ef9c2011-10-25 03:55:16 +0900329 ThreadData* registered = reinterpret_cast<ThreadData*>(tls_index_.Get());
330 if (registered)
331 return registered;
332
333 // We must be a worker thread, since we didn't pre-register.
334 ThreadData* worker_thread_data = NULL;
jar@chromium.org92703d52011-11-24 09:00:31 +0900335 int worker_thread_number = 0;
jar@chromium.org666ef9c2011-10-25 03:55:16 +0900336 {
jar@chromium.orgb2845c82011-11-15 05:36:46 +0900337 base::AutoLock lock(*list_lock_.Pointer());
jar@chromium.org50c48db2011-11-20 13:17:07 +0900338 if (first_retired_worker_) {
339 worker_thread_data = first_retired_worker_;
340 first_retired_worker_ = first_retired_worker_->next_retired_worker_;
341 worker_thread_data->next_retired_worker_ = NULL;
jar@chromium.org10ef9902011-11-19 02:03:33 +0900342 } else {
jar@chromium.org92703d52011-11-24 09:00:31 +0900343 worker_thread_number = ++worker_thread_data_creation_count_;
jar@chromium.org666ef9c2011-10-25 03:55:16 +0900344 }
initial.commit3f4a7322008-07-27 06:49:38 +0900345 }
jar@chromium.org666ef9c2011-10-25 03:55:16 +0900346
347 // If we can't find a previously used instance, then we have to create one.
jar@chromium.org92703d52011-11-24 09:00:31 +0900348 if (!worker_thread_data) {
349 DCHECK_GT(worker_thread_number, 0);
350 worker_thread_data = new ThreadData(worker_thread_number);
351 }
jar@chromium.org50c48db2011-11-20 13:17:07 +0900352 DCHECK_GT(worker_thread_data->worker_thread_number_, 0);
jar@chromium.org666ef9c2011-10-25 03:55:16 +0900353
354 tls_index_.Set(worker_thread_data);
355 return worker_thread_data;
jar@chromium.org79a58c32011-10-16 08:52:45 +0900356}
357
358// static
jar@chromium.org666ef9c2011-10-25 03:55:16 +0900359void ThreadData::OnThreadTermination(void* thread_data) {
jar@chromium.orgb5c974b2011-12-14 10:36:48 +0900360 DCHECK(thread_data); // TLS should *never* call us with a NULL.
jar@chromium.org92703d52011-11-24 09:00:31 +0900361 // We must NOT do any allocations during this callback. There is a chance
362 // that the allocator is no longer active on this thread.
jar@chromium.org666ef9c2011-10-25 03:55:16 +0900363 if (!kTrackAllTaskObjects)
364 return; // Not compiled in.
jar@chromium.org666ef9c2011-10-25 03:55:16 +0900365 reinterpret_cast<ThreadData*>(thread_data)->OnThreadTerminationCleanup();
jar@chromium.org666ef9c2011-10-25 03:55:16 +0900366}
367
jar@chromium.org50c48db2011-11-20 13:17:07 +0900368void ThreadData::OnThreadTerminationCleanup() {
jar@chromium.org92703d52011-11-24 09:00:31 +0900369 // The list_lock_ was created when we registered the callback, so it won't be
370 // allocated here despite the lazy reference.
jar@chromium.orgb2845c82011-11-15 05:36:46 +0900371 base::AutoLock lock(*list_lock_.Pointer());
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900372 if (incarnation_counter_ != incarnation_count_for_pool_)
373 return; // ThreadData was constructed in an earlier unit test.
jar@chromium.org92703d52011-11-24 09:00:31 +0900374 ++cleanup_count_;
375 // Only worker threads need to be retired and reused.
376 if (!worker_thread_number_) {
377 return;
378 }
jar@chromium.org50c48db2011-11-20 13:17:07 +0900379 // We must NOT do any allocations during this callback.
380 // Using the simple linked lists avoids all allocations.
381 DCHECK_EQ(this->next_retired_worker_, reinterpret_cast<ThreadData*>(NULL));
382 this->next_retired_worker_ = first_retired_worker_;
383 first_retired_worker_ = this;
initial.commit3f4a7322008-07-27 06:49:38 +0900384}
385
initial.commit3f4a7322008-07-27 06:49:38 +0900386// static
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900387void ThreadData::Snapshot(bool reset_max, ProcessDataSnapshot* process_data) {
388 // Add births that have run to completion to |collected_data|.
389 // |birth_counts| tracks the total number of births recorded at each location
390 // for which we have not seen a death count.
391 BirthCountMap birth_counts;
392 ThreadData::SnapshotAllExecutedTasks(reset_max, process_data, &birth_counts);
393
394 // Add births that are still active -- i.e. objects that have tallied a birth,
395 // but have not yet tallied a matching death, and hence must be either
396 // running, queued up, or being held in limbo for future posting.
397 for (BirthCountMap::const_iterator it = birth_counts.begin();
398 it != birth_counts.end(); ++it) {
399 if (it->second > 0) {
400 process_data->tasks.push_back(
401 TaskSnapshot(*it->first, DeathData(it->second), "Still_Alive"));
402 }
403 }
jar@chromium.org666ef9c2011-10-25 03:55:16 +0900404}
405
jar@chromium.orgfebe0e42009-12-30 16:31:45 +0900406Births* ThreadData::TallyABirth(const Location& location) {
initial.commit3f4a7322008-07-27 06:49:38 +0900407 BirthMap::iterator it = birth_map_.find(location);
jar@chromium.orgb5c974b2011-12-14 10:36:48 +0900408 Births* child;
jar@chromium.orgfebe0e42009-12-30 16:31:45 +0900409 if (it != birth_map_.end()) {
jar@chromium.orgb5c974b2011-12-14 10:36:48 +0900410 child = it->second;
411 child->RecordBirth();
412 } else {
413 child = new Births(location, *this); // Leak this.
414 // Lock since the map may get relocated now, and other threads sometimes
415 // snapshot it (but they lock before copying it).
416 base::AutoLock lock(map_lock_);
417 birth_map_[location] = child;
jar@chromium.orgfebe0e42009-12-30 16:31:45 +0900418 }
initial.commit3f4a7322008-07-27 06:49:38 +0900419
jar@chromium.orgb5c974b2011-12-14 10:36:48 +0900420 if (kTrackParentChildLinks && status_ > PROFILING_ACTIVE &&
421 !parent_stack_.empty()) {
422 const Births* parent = parent_stack_.top();
423 ParentChildPair pair(parent, child);
424 if (parent_child_set_.find(pair) == parent_child_set_.end()) {
425 // Lock since the map may get relocated now, and other threads sometimes
426 // snapshot it (but they lock before copying it).
427 base::AutoLock lock(map_lock_);
428 parent_child_set_.insert(pair);
429 }
430 }
431
432 return child;
initial.commit3f4a7322008-07-27 06:49:38 +0900433}
434
jar@chromium.org666ef9c2011-10-25 03:55:16 +0900435void ThreadData::TallyADeath(const Births& birth,
isherman@chromium.org3306fc02012-03-25 07:17:18 +0900436 int32 queue_duration,
437 int32 run_duration) {
jar@chromium.orga0260412011-12-04 16:19:10 +0900438 // Stir in some randomness, plus add constant in case durations are zero.
isherman@chromium.org3306fc02012-03-25 07:17:18 +0900439 const int32 kSomePrimeNumber = 2147483647;
jar@chromium.orga0260412011-12-04 16:19:10 +0900440 random_number_ += queue_duration + run_duration + kSomePrimeNumber;
441 // An address is going to have some randomness to it as well ;-).
442 random_number_ ^= static_cast<int32>(&birth - reinterpret_cast<Births*>(0));
443
jar@chromium.org4626ccb2012-02-16 08:05:01 +0900444 // We don't have queue durations without OS timer. OS timer is automatically
445 // used for task-post-timing, so the use of an alternate timer implies all
446 // queue times are invalid.
447 if (kAllowAlternateTimeSourceHandling && now_function_)
448 queue_duration = 0;
449
jar@chromium.org666ef9c2011-10-25 03:55:16 +0900450 DeathMap::iterator it = death_map_.find(&birth);
451 DeathData* death_data;
initial.commit3f4a7322008-07-27 06:49:38 +0900452 if (it != death_map_.end()) {
jar@chromium.org666ef9c2011-10-25 03:55:16 +0900453 death_data = &it->second;
454 } else {
jar@chromium.org92703d52011-11-24 09:00:31 +0900455 base::AutoLock lock(map_lock_); // Lock as the map may get relocated now.
jar@chromium.org666ef9c2011-10-25 03:55:16 +0900456 death_data = &death_map_[&birth];
457 } // Release lock ASAP.
jar@chromium.orga0260412011-12-04 16:19:10 +0900458 death_data->RecordDeath(queue_duration, run_duration, random_number_);
jar@chromium.orgb5c974b2011-12-14 10:36:48 +0900459
460 if (!kTrackParentChildLinks)
461 return;
462 if (!parent_stack_.empty()) { // We might get turned off.
463 DCHECK_EQ(parent_stack_.top(), &birth);
464 parent_stack_.pop();
465 }
initial.commit3f4a7322008-07-27 06:49:38 +0900466}
467
468// static
ajwong@chromium.org12fa0922011-07-27 03:25:16 +0900469Births* ThreadData::TallyABirthIfActive(const Location& location) {
jar@chromium.org666ef9c2011-10-25 03:55:16 +0900470 if (!kTrackAllTaskObjects)
471 return NULL; // Not compiled in.
472
jar@chromium.org23b00722012-02-11 04:43:42 +0900473 if (!TrackingStatus())
jar@chromium.org79a58c32011-10-16 08:52:45 +0900474 return NULL;
475 ThreadData* current_thread_data = Get();
476 if (!current_thread_data)
477 return NULL;
478 return current_thread_data->TallyABirth(location);
ajwong@chromium.org12fa0922011-07-27 03:25:16 +0900479}
480
481// static
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900482void ThreadData::TallyRunOnNamedThreadIfTracking(
483 const base::TrackingInfo& completed_task,
484 const TrackedTime& start_of_run,
485 const TrackedTime& end_of_run) {
jar@chromium.org666ef9c2011-10-25 03:55:16 +0900486 if (!kTrackAllTaskObjects)
487 return; // Not compiled in.
488
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900489 // Even if we have been DEACTIVATED, we will process any pending births so
490 // that our data structures (which counted the outstanding births) remain
491 // consistent.
492 const Births* birth = completed_task.birth_tally;
493 if (!birth)
jar@chromium.org79a58c32011-10-16 08:52:45 +0900494 return;
495 ThreadData* current_thread_data = Get();
496 if (!current_thread_data)
497 return;
498
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900499 // Watch out for a race where status_ is changing, and hence one or both
jar@chromium.orgb5c974b2011-12-14 10:36:48 +0900500 // of start_of_run or end_of_run is zero. In that case, we didn't bother to
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900501 // get a time value since we "weren't tracking" and we were trying to be
502 // efficient by not calling for a genuine time value. For simplicity, we'll
503 // use a default zero duration when we can't calculate a true value.
isherman@chromium.org3306fc02012-03-25 07:17:18 +0900504 int32 queue_duration = 0;
505 int32 run_duration = 0;
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900506 if (!start_of_run.is_null()) {
wangxianzhu@chromium.org115411e2013-07-11 02:50:22 +0900507 queue_duration = (start_of_run - completed_task.EffectiveTimePosted())
508 .InMilliseconds();
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900509 if (!end_of_run.is_null())
jar@chromium.org51c8dfb2011-11-12 07:40:27 +0900510 run_duration = (end_of_run - start_of_run).InMilliseconds();
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900511 }
512 current_thread_data->TallyADeath(*birth, queue_duration, run_duration);
513}
514
515// static
516void ThreadData::TallyRunOnWorkerThreadIfTracking(
517 const Births* birth,
518 const TrackedTime& time_posted,
519 const TrackedTime& start_of_run,
520 const TrackedTime& end_of_run) {
521 if (!kTrackAllTaskObjects)
522 return; // Not compiled in.
523
524 // Even if we have been DEACTIVATED, we will process any pending births so
525 // that our data structures (which counted the outstanding births) remain
526 // consistent.
527 if (!birth)
528 return;
529
530 // TODO(jar): Support the option to coalesce all worker-thread activity under
531 // one ThreadData instance that uses locks to protect *all* access. This will
532 // reduce memory (making it provably bounded), but run incrementally slower
wtc@chromium.org4a357912013-05-21 03:53:13 +0900533 // (since we'll use locks on TallyABirth and TallyADeath). The good news is
534 // that the locks on TallyADeath will be *after* the worker thread has run,
535 // and hence nothing will be waiting for the completion (... besides some
536 // other thread that might like to run). Also, the worker threads tasks are
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900537 // generally longer, and hence the cost of the lock may perchance be amortized
538 // over the long task's lifetime.
539 ThreadData* current_thread_data = Get();
540 if (!current_thread_data)
541 return;
542
isherman@chromium.org3306fc02012-03-25 07:17:18 +0900543 int32 queue_duration = 0;
544 int32 run_duration = 0;
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900545 if (!start_of_run.is_null()) {
jar@chromium.org51c8dfb2011-11-12 07:40:27 +0900546 queue_duration = (start_of_run - time_posted).InMilliseconds();
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900547 if (!end_of_run.is_null())
jar@chromium.org51c8dfb2011-11-12 07:40:27 +0900548 run_duration = (end_of_run - start_of_run).InMilliseconds();
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900549 }
jar@chromium.org666ef9c2011-10-25 03:55:16 +0900550 current_thread_data->TallyADeath(*birth, queue_duration, run_duration);
ajwong@chromium.org12fa0922011-07-27 03:25:16 +0900551}
552
553// static
jar@chromium.org27cf2972011-11-09 02:09:21 +0900554void ThreadData::TallyRunInAScopedRegionIfTracking(
555 const Births* birth,
556 const TrackedTime& start_of_run,
557 const TrackedTime& end_of_run) {
558 if (!kTrackAllTaskObjects)
559 return; // Not compiled in.
560
561 // Even if we have been DEACTIVATED, we will process any pending births so
562 // that our data structures (which counted the outstanding births) remain
563 // consistent.
564 if (!birth)
565 return;
566
567 ThreadData* current_thread_data = Get();
568 if (!current_thread_data)
569 return;
570
isherman@chromium.org3306fc02012-03-25 07:17:18 +0900571 int32 queue_duration = 0;
572 int32 run_duration = 0;
jar@chromium.org69800902011-11-16 08:59:36 +0900573 if (!start_of_run.is_null() && !end_of_run.is_null())
574 run_duration = (end_of_run - start_of_run).InMilliseconds();
jar@chromium.org27cf2972011-11-09 02:09:21 +0900575 current_thread_data->TallyADeath(*birth, queue_duration, run_duration);
576}
577
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900578// static
579void ThreadData::SnapshotAllExecutedTasks(bool reset_max,
580 ProcessDataSnapshot* process_data,
581 BirthCountMap* birth_counts) {
582 if (!kTrackAllTaskObjects)
583 return; // Not compiled in.
584
585 // Get an unchanging copy of a ThreadData list.
586 ThreadData* my_list = ThreadData::first();
587
588 // Gather data serially.
589 // This hackish approach *can* get some slighly corrupt tallies, as we are
590 // grabbing values without the protection of a lock, but it has the advantage
591 // of working even with threads that don't have message loops. If a user
592 // sees any strangeness, they can always just run their stats gathering a
593 // second time.
594 for (ThreadData* thread_data = my_list;
595 thread_data;
596 thread_data = thread_data->next()) {
597 thread_data->SnapshotExecutedTasks(reset_max, process_data, birth_counts);
598 }
599}
600
601void ThreadData::SnapshotExecutedTasks(bool reset_max,
602 ProcessDataSnapshot* process_data,
603 BirthCountMap* birth_counts) {
604 // Get copy of data, so that the data will not change during the iterations
605 // and processing.
606 ThreadData::BirthMap birth_map;
607 ThreadData::DeathMap death_map;
608 ThreadData::ParentChildSet parent_child_set;
609 SnapshotMaps(reset_max, &birth_map, &death_map, &parent_child_set);
610
611 for (ThreadData::DeathMap::const_iterator it = death_map.begin();
612 it != death_map.end(); ++it) {
613 process_data->tasks.push_back(
614 TaskSnapshot(*it->first, it->second, thread_name()));
615 (*birth_counts)[it->first] -= it->first->birth_count();
616 }
617
618 for (ThreadData::BirthMap::const_iterator it = birth_map.begin();
619 it != birth_map.end(); ++it) {
620 (*birth_counts)[it->second] += it->second->birth_count();
621 }
622
623 if (!kTrackParentChildLinks)
624 return;
625
626 for (ThreadData::ParentChildSet::const_iterator it = parent_child_set.begin();
627 it != parent_child_set.end(); ++it) {
628 process_data->descendants.push_back(ParentChildPairSnapshot(*it));
629 }
630}
631
initial.commit3f4a7322008-07-27 06:49:38 +0900632// This may be called from another thread.
jar@chromium.orga0260412011-12-04 16:19:10 +0900633void ThreadData::SnapshotMaps(bool reset_max,
634 BirthMap* birth_map,
jar@chromium.orgb5c974b2011-12-14 10:36:48 +0900635 DeathMap* death_map,
636 ParentChildSet* parent_child_set) {
jar@chromium.org92703d52011-11-24 09:00:31 +0900637 base::AutoLock lock(map_lock_);
initial.commit3f4a7322008-07-27 06:49:38 +0900638 for (BirthMap::const_iterator it = birth_map_.begin();
639 it != birth_map_.end(); ++it)
jar@chromium.orga0260412011-12-04 16:19:10 +0900640 (*birth_map)[it->first] = it->second;
641 for (DeathMap::iterator it = death_map_.begin();
642 it != death_map_.end(); ++it) {
643 (*death_map)[it->first] = it->second;
644 if (reset_max)
645 it->second.ResetMax();
646 }
jar@chromium.orgb5c974b2011-12-14 10:36:48 +0900647
648 if (!kTrackParentChildLinks)
649 return;
650
651 for (ParentChildSet::iterator it = parent_child_set_.begin();
652 it != parent_child_set_.end(); ++it)
653 parent_child_set->insert(*it);
initial.commit3f4a7322008-07-27 06:49:38 +0900654}
655
jar@chromium.orga0260412011-12-04 16:19:10 +0900656// static
jar@chromium.orgfebe0e42009-12-30 16:31:45 +0900657void ThreadData::ResetAllThreadData() {
jar@chromium.org666ef9c2011-10-25 03:55:16 +0900658 ThreadData* my_list = first();
jar@chromium.orgfebe0e42009-12-30 16:31:45 +0900659
660 for (ThreadData* thread_data = my_list;
661 thread_data;
662 thread_data = thread_data->next())
663 thread_data->Reset();
664}
665
666void ThreadData::Reset() {
jar@chromium.org92703d52011-11-24 09:00:31 +0900667 base::AutoLock lock(map_lock_);
jar@chromium.orgfebe0e42009-12-30 16:31:45 +0900668 for (DeathMap::iterator it = death_map_.begin();
669 it != death_map_.end(); ++it)
670 it->second.Clear();
671 for (BirthMap::iterator it = birth_map_.begin();
672 it != birth_map_.end(); ++it)
673 it->second->Clear();
674}
675
jar@chromium.org4626ccb2012-02-16 08:05:01 +0900676static void OptionallyInitializeAlternateTimer() {
isherman@chromium.org33d14ae2012-04-26 08:10:34 +0900677 NowFunction* alternate_time_source = GetAlternateTimeSource();
678 if (alternate_time_source)
679 ThreadData::SetAlternateTimeSource(alternate_time_source);
jar@chromium.org4626ccb2012-02-16 08:05:01 +0900680}
681
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900682bool ThreadData::Initialize() {
jar@chromium.org666ef9c2011-10-25 03:55:16 +0900683 if (!kTrackAllTaskObjects)
684 return false; // Not compiled in.
jar@chromium.orgb5c974b2011-12-14 10:36:48 +0900685 if (status_ >= DEACTIVATED)
jar@chromium.org340a7c52011-11-16 06:50:36 +0900686 return true; // Someone else did the initialization.
687 // Due to racy lazy initialization in tests, we'll need to recheck status_
688 // after we acquire the lock.
689
690 // Ensure that we don't double initialize tls. We are called when single
691 // threaded in the product, but some tests may be racy and lazy about our
692 // initialization.
693 base::AutoLock lock(*list_lock_.Pointer());
jar@chromium.orgb5c974b2011-12-14 10:36:48 +0900694 if (status_ >= DEACTIVATED)
jar@chromium.org340a7c52011-11-16 06:50:36 +0900695 return true; // Someone raced in here and beat us.
696
jar@chromium.org4626ccb2012-02-16 08:05:01 +0900697 // Put an alternate timer in place if the environment calls for it, such as
698 // for tracking TCMalloc allocations. This insertion is idempotent, so we
699 // don't mind if there is a race, and we'd prefer not to be in a lock while
700 // doing this work.
701 if (kAllowAlternateTimeSourceHandling)
702 OptionallyInitializeAlternateTimer();
703
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900704 // Perform the "real" TLS initialization now, and leave it intact through
jar@chromium.org666ef9c2011-10-25 03:55:16 +0900705 // process termination.
jar@chromium.org340a7c52011-11-16 06:50:36 +0900706 if (!tls_index_.initialized()) { // Testing may have initialized this.
jar@chromium.orgb5c974b2011-12-14 10:36:48 +0900707 DCHECK_EQ(status_, UNINITIALIZED);
jar@chromium.org666ef9c2011-10-25 03:55:16 +0900708 tls_index_.Initialize(&ThreadData::OnThreadTermination);
jar@chromium.org340a7c52011-11-16 06:50:36 +0900709 if (!tls_index_.initialized())
710 return false;
jar@chromium.orgb5c974b2011-12-14 10:36:48 +0900711 } else {
712 // TLS was initialzed for us earlier.
713 DCHECK_EQ(status_, DORMANT_DURING_TESTS);
jar@chromium.org340a7c52011-11-16 06:50:36 +0900714 }
joth@chromium.orgc8b867c2011-11-01 00:32:08 +0900715
jar@chromium.org340a7c52011-11-16 06:50:36 +0900716 // Incarnation counter is only significant to testing, as it otherwise will
717 // never again change in this process.
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900718 ++incarnation_counter_;
jar@chromium.org340a7c52011-11-16 06:50:36 +0900719
720 // The lock is not critical for setting status_, but it doesn't hurt. It also
721 // ensures that if we have a racy initialization, that we'll bail as soon as
722 // we get the lock earlier in this method.
723 status_ = kInitialStartupState;
jar@chromium.orgb5c974b2011-12-14 10:36:48 +0900724 if (!kTrackParentChildLinks &&
725 kInitialStartupState == PROFILING_CHILDREN_ACTIVE)
726 status_ = PROFILING_ACTIVE;
jar@chromium.org340a7c52011-11-16 06:50:36 +0900727 DCHECK(status_ != UNINITIALIZED);
initial.commit3f4a7322008-07-27 06:49:38 +0900728 return true;
729}
730
731// static
jar@chromium.org23b00722012-02-11 04:43:42 +0900732bool ThreadData::InitializeAndSetTrackingStatus(Status status) {
733 DCHECK_GE(status, DEACTIVATED);
734 DCHECK_LE(status, PROFILING_CHILDREN_ACTIVE);
735
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900736 if (!Initialize()) // No-op if already initialized.
737 return false; // Not compiled in.
738
jar@chromium.org23b00722012-02-11 04:43:42 +0900739 if (!kTrackParentChildLinks && status > DEACTIVATED)
740 status = PROFILING_ACTIVE;
741 status_ = status;
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900742 return true;
743}
744
745// static
jar@chromium.org23b00722012-02-11 04:43:42 +0900746ThreadData::Status ThreadData::status() {
747 return status_;
748}
749
750// static
751bool ThreadData::TrackingStatus() {
jar@chromium.orgb5c974b2011-12-14 10:36:48 +0900752 return status_ > DEACTIVATED;
initial.commit3f4a7322008-07-27 06:49:38 +0900753}
754
755// static
jar@chromium.org23b00722012-02-11 04:43:42 +0900756bool ThreadData::TrackingParentChildStatus() {
jar@chromium.orgb5c974b2011-12-14 10:36:48 +0900757 return status_ >= PROFILING_CHILDREN_ACTIVE;
758}
759
760// static
761TrackedTime ThreadData::NowForStartOfRun(const Births* parent) {
762 if (kTrackParentChildLinks && parent && status_ > PROFILING_ACTIVE) {
763 ThreadData* current_thread_data = Get();
764 if (current_thread_data)
765 current_thread_data->parent_stack_.push(parent);
766 }
jar@chromium.orgb536eef2011-11-14 14:24:07 +0900767 return Now();
768}
769
770// static
771TrackedTime ThreadData::NowForEndOfRun() {
772 return Now();
773}
774
775// static
jar@chromium.org4626ccb2012-02-16 08:05:01 +0900776void ThreadData::SetAlternateTimeSource(NowFunction* now_function) {
777 DCHECK(now_function);
778 if (kAllowAlternateTimeSourceHandling)
779 now_function_ = now_function;
780}
781
782// static
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900783TrackedTime ThreadData::Now() {
jar@chromium.org4626ccb2012-02-16 08:05:01 +0900784 if (kAllowAlternateTimeSourceHandling && now_function_)
785 return TrackedTime::FromMilliseconds((*now_function_)());
qsr@chromium.org4b1cc322013-12-11 21:49:17 +0900786 if (kTrackAllTaskObjects && IsProfilerTimingEnabled() && TrackingStatus())
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900787 return TrackedTime::Now();
788 return TrackedTime(); // Super fast when disabled, or not compiled.
jar@chromium.org79a58c32011-10-16 08:52:45 +0900789}
initial.commit3f4a7322008-07-27 06:49:38 +0900790
791// static
jar@chromium.org92703d52011-11-24 09:00:31 +0900792void ThreadData::EnsureCleanupWasCalled(int major_threads_shutdown_count) {
793 base::AutoLock lock(*list_lock_.Pointer());
794 if (worker_thread_data_creation_count_ == 0)
795 return; // We haven't really run much, and couldn't have leaked.
pkasting@chromium.org0bc25d52014-03-15 03:25:48 +0900796
797 // TODO(jar): until this is working on XP, don't run the real test.
798#if 0
jar@chromium.org92703d52011-11-24 09:00:31 +0900799 // Verify that we've at least shutdown/cleanup the major namesd threads. The
800 // caller should tell us how many thread shutdowns should have taken place by
801 // now.
jar@chromium.org92703d52011-11-24 09:00:31 +0900802 CHECK_GT(cleanup_count_, major_threads_shutdown_count);
pkasting@chromium.org0bc25d52014-03-15 03:25:48 +0900803#endif
jar@chromium.org92703d52011-11-24 09:00:31 +0900804}
805
806// static
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900807void ThreadData::ShutdownSingleThreadedCleanup(bool leak) {
jar@chromium.org666ef9c2011-10-25 03:55:16 +0900808 // This is only called from test code, where we need to cleanup so that
809 // additional tests can be run.
initial.commit3f4a7322008-07-27 06:49:38 +0900810 // We must be single threaded... but be careful anyway.
jar@chromium.org23b00722012-02-11 04:43:42 +0900811 if (!InitializeAndSetTrackingStatus(DEACTIVATED))
initial.commit3f4a7322008-07-27 06:49:38 +0900812 return;
813 ThreadData* thread_data_list;
814 {
jar@chromium.orgb2845c82011-11-15 05:36:46 +0900815 base::AutoLock lock(*list_lock_.Pointer());
jar@chromium.org666ef9c2011-10-25 03:55:16 +0900816 thread_data_list = all_thread_data_list_head_;
817 all_thread_data_list_head_ = NULL;
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900818 ++incarnation_counter_;
jar@chromium.org50c48db2011-11-20 13:17:07 +0900819 // To be clean, break apart the retired worker list (though we leak them).
jar@chromium.orga0260412011-12-04 16:19:10 +0900820 while (first_retired_worker_) {
jar@chromium.org50c48db2011-11-20 13:17:07 +0900821 ThreadData* worker = first_retired_worker_;
822 CHECK_GT(worker->worker_thread_number_, 0);
823 first_retired_worker_ = worker->next_retired_worker_;
824 worker->next_retired_worker_ = NULL;
825 }
initial.commit3f4a7322008-07-27 06:49:38 +0900826 }
827
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900828 // Put most global static back in pristine shape.
jar@chromium.org92703d52011-11-24 09:00:31 +0900829 worker_thread_data_creation_count_ = 0;
830 cleanup_count_ = 0;
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900831 tls_index_.Set(NULL);
jar@chromium.orgb5c974b2011-12-14 10:36:48 +0900832 status_ = DORMANT_DURING_TESTS; // Almost UNINITIALIZED.
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900833
834 // To avoid any chance of racing in unit tests, which is the only place we
835 // call this function, we may sometimes leak all the data structures we
836 // recovered, as they may still be in use on threads from prior tests!
earthdok@google.comeb54f422013-06-10 20:32:20 +0900837 if (leak) {
838 ThreadData* thread_data = thread_data_list;
839 while (thread_data) {
840 ANNOTATE_LEAKING_OBJECT_PTR(thread_data);
841 thread_data = thread_data->next();
842 }
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900843 return;
earthdok@google.comeb54f422013-06-10 20:32:20 +0900844 }
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900845
846 // When we want to cleanup (on a single thread), here is what we do.
847
jar@chromium.org666ef9c2011-10-25 03:55:16 +0900848 // Do actual recursive delete in all ThreadData instances.
initial.commit3f4a7322008-07-27 06:49:38 +0900849 while (thread_data_list) {
850 ThreadData* next_thread_data = thread_data_list;
851 thread_data_list = thread_data_list->next();
852
853 for (BirthMap::iterator it = next_thread_data->birth_map_.begin();
854 next_thread_data->birth_map_.end() != it; ++it)
855 delete it->second; // Delete the Birth Records.
initial.commit3f4a7322008-07-27 06:49:38 +0900856 delete next_thread_data; // Includes all Death Records.
857 }
initial.commit3f4a7322008-07-27 06:49:38 +0900858}
859
initial.commit3f4a7322008-07-27 06:49:38 +0900860//------------------------------------------------------------------------------
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900861TaskSnapshot::TaskSnapshot() {
initial.commit3f4a7322008-07-27 06:49:38 +0900862}
863
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900864TaskSnapshot::TaskSnapshot(const BirthOnThread& birth,
865 const DeathData& death_data,
866 const std::string& death_thread_name)
867 : birth(birth),
868 death_data(death_data),
869 death_thread_name(death_thread_name) {
initial.commit3f4a7322008-07-27 06:49:38 +0900870}
871
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900872TaskSnapshot::~TaskSnapshot() {
jar@chromium.org666ef9c2011-10-25 03:55:16 +0900873}
874
initial.commit3f4a7322008-07-27 06:49:38 +0900875//------------------------------------------------------------------------------
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900876// ParentChildPairSnapshot
initial.commit3f4a7322008-07-27 06:49:38 +0900877
jar@chromium.orgeff10e42012-07-27 07:03:01 +0900878ParentChildPairSnapshot::ParentChildPairSnapshot() {
erg@google.com71915232010-09-29 07:54:58 +0900879}
880
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900881ParentChildPairSnapshot::ParentChildPairSnapshot(
882 const ThreadData::ParentChildPair& parent_child)
883 : parent(*parent_child.first),
884 child(*parent_child.second) {
initial.commit3f4a7322008-07-27 06:49:38 +0900885}
886
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900887ParentChildPairSnapshot::~ParentChildPairSnapshot() {
initial.commit3f4a7322008-07-27 06:49:38 +0900888}
889
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900890//------------------------------------------------------------------------------
891// ProcessDataSnapshot
892
893ProcessDataSnapshot::ProcessDataSnapshot()
bbudge@chromium.orgaa4d16a2012-04-24 06:18:19 +0900894#if !defined(OS_NACL)
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900895 : process_id(base::GetCurrentProcId()) {
bbudge@chromium.orgaa4d16a2012-04-24 06:18:19 +0900896#else
897 : process_id(0) {
898#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900899}
900
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900901ProcessDataSnapshot::~ProcessDataSnapshot() {
jar@chromium.org666ef9c2011-10-25 03:55:16 +0900902}
903
initial.commit3f4a7322008-07-27 06:49:38 +0900904} // namespace tracked_objects