blob: 43205094987e8fb4d05e07e1d2bde988383c788c [file] [log] [blame]
[email protected]9fc44162012-01-23 22:56:411// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commitd7cae122008-07-26 21:49:384
5#include "base/tracked_objects.h"
6
[email protected]c014f2b32013-09-03 23:29:127#include <limits.h>
[email protected]7f8a4eb2012-03-19 21:46:278#include <stdlib.h>
[email protected]a5b94a92008-08-12 23:25:439
[email protected]7caab6dc2013-12-12 19:29:1010#include "base/atomicops.h"
[email protected]915b344f2013-12-11 12:49:1711#include "base/base_switches.h"
12#include "base/command_line.h"
[email protected]75086be2013-03-20 21:18:2213#include "base/compiler_specific.h"
[email protected]bf709abd2013-06-10 11:32:2014#include "base/debug/leak_annotations.h"
[email protected]c014f2b32013-09-03 23:29:1215#include "base/logging.h"
[email protected]dd4b51262013-07-25 21:38:2316#include "base/process/process_handle.h"
[email protected]90895d0f2012-02-15 23:05:0117#include "base/profiler/alternate_timer.h"
[email protected]c851cfd2013-06-10 20:11:1418#include "base/strings/stringprintf.h"
[email protected]a0447ff2011-12-04 21:14:0519#include "base/third_party/valgrind/memcheck.h"
[email protected]c014f2b32013-09-03 23:29:1220#include "base/tracking_info.h"
initial.commitd7cae122008-07-26 21:49:3821
[email protected]e1acf6f2008-10-27 20:43:3322using base::TimeDelta;
23
[email protected]c014f2b32013-09-03 23:29:1224namespace base {
25class TimeDelta;
26}
27
initial.commitd7cae122008-07-26 21:49:3828namespace tracked_objects {
29
[email protected]b2a9bbd2011-10-31 22:36:2130namespace {
31// Flag to compile out almost all of the task tracking code.
[email protected]da9ccfb2012-01-28 00:34:4032const bool kTrackAllTaskObjects = true;
[email protected]3f095c0a2011-10-31 15:32:0833
[email protected]1cb05db2012-04-13 00:39:2634// TODO(jar): Evaluate the perf impact of enabling this. If the perf impact is
35// negligible, enable by default.
[email protected]8aa1e6e2011-12-14 01:36:4836// Flag to compile out parent-child link recording.
[email protected]da9ccfb2012-01-28 00:34:4037const bool kTrackParentChildLinks = false;
[email protected]8aa1e6e2011-12-14 01:36:4838
[email protected]b2a9bbd2011-10-31 22:36:2139// When ThreadData is first initialized, should we start in an ACTIVE state to
40// record all of the startup-time tasks, or should we start up DEACTIVATED, so
41// that we only record after parsing the command line flag --enable-tracking.
42// Note that the flag may force either state, so this really controls only the
43// period of time up until that flag is parsed. If there is no flag seen, then
44// this state may prevail for much or all of the process lifetime.
[email protected]da9ccfb2012-01-28 00:34:4045const ThreadData::Status kInitialStartupState =
[email protected]8aa1e6e2011-12-14 01:36:4846 ThreadData::PROFILING_CHILDREN_ACTIVE;
[email protected]da9ccfb2012-01-28 00:34:4047
[email protected]90895d0f2012-02-15 23:05:0148// 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;
[email protected]1cb05db2012-04-13 00:39:2656
vadimta1568312014-11-06 22:27:4357// Possible states of the profiler timing enabledness.
58enum {
59 UNDEFINED_TIMING,
60 ENABLED_TIMING,
61 DISABLED_TIMING,
62};
63
64// State of the profiler timing enabledness.
65base::subtle::Atomic32 g_profiler_timing_enabled = UNDEFINED_TIMING;
66
67// Returns whether profiler timing is enabled. The default is true, but this may
68// be overridden by a command-line flag. Some platforms may programmatically set
69// this command-line flag to the "off" value if it's not specified.
70// This in turn can be overridden by explicitly calling
71// ThreadData::EnableProfilerTiming, say, based on a field trial.
[email protected]915b344f2013-12-11 12:49:1772inline bool IsProfilerTimingEnabled() {
vadimta1568312014-11-06 22:27:4373 // Reading |g_profiler_timing_enabled| is done without barrier because
74 // multiple initialization is not an issue while the barrier can be relatively
75 // costly given that this method is sometimes called in a tight loop.
[email protected]7caab6dc2013-12-12 19:29:1076 base::subtle::Atomic32 current_timing_enabled =
vadimta1568312014-11-06 22:27:4377 base::subtle::NoBarrier_Load(&g_profiler_timing_enabled);
[email protected]7caab6dc2013-12-12 19:29:1078 if (current_timing_enabled == UNDEFINED_TIMING) {
pgal.u-szeged421dddb2014-11-25 12:55:0279 if (!base::CommandLine::InitializedForCurrentProcess())
[email protected]915b344f2013-12-11 12:49:1780 return true;
[email protected]7caab6dc2013-12-12 19:29:1081 current_timing_enabled =
pgal.u-szeged421dddb2014-11-25 12:55:0282 (base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
[email protected]7caab6dc2013-12-12 19:29:1083 switches::kProfilerTiming) ==
84 switches::kProfilerTimingDisabledValue)
85 ? DISABLED_TIMING
86 : ENABLED_TIMING;
vadimta1568312014-11-06 22:27:4387 base::subtle::NoBarrier_Store(&g_profiler_timing_enabled,
88 current_timing_enabled);
[email protected]915b344f2013-12-11 12:49:1789 }
[email protected]7caab6dc2013-12-12 19:29:1090 return current_timing_enabled == ENABLED_TIMING;
[email protected]915b344f2013-12-11 12:49:1791}
92
[email protected]8aa1e6e2011-12-14 01:36:4893} // namespace
[email protected]84b57952011-10-15 23:52:4594
initial.commitd7cae122008-07-26 21:49:3895//------------------------------------------------------------------------------
[email protected]63f5b0e2011-11-04 00:23:2796// DeathData tallies durations when a death takes place.
initial.commitd7cae122008-07-26 21:49:3897
[email protected]b6b2b892011-12-04 07:19:1098DeathData::DeathData() {
99 Clear();
100}
101
102DeathData::DeathData(int count) {
103 Clear();
104 count_ = count;
105}
106
[email protected]7ceb44482011-12-09 03:41:04107// TODO(jar): I need to see if this macro to optimize branching is worth using.
[email protected]b6b2b892011-12-04 07:19:10108//
109// This macro has no branching, so it is surely fast, and is equivalent to:
110// if (assign_it)
111// target = source;
112// We use a macro rather than a template to force this to inline.
113// Related code for calculating max is discussed on the web.
114#define CONDITIONAL_ASSIGN(assign_it, target, source) \
[email protected]c186e962012-03-24 22:17:18115 ((target) ^= ((target) ^ (source)) & -static_cast<int32>(assign_it))
[email protected]b6b2b892011-12-04 07:19:10116
[email protected]c186e962012-03-24 22:17:18117void DeathData::RecordDeath(const int32 queue_duration,
118 const int32 run_duration,
gliderf78125752014-11-12 21:05:33119 const uint32 random_number) {
[email protected]59b15da2013-02-28 04:15:43120 // We'll just clamp at INT_MAX, but we should note this in the UI as such.
121 if (count_ < INT_MAX)
122 ++count_;
[email protected]b6b2b892011-12-04 07:19:10123 queue_duration_sum_ += queue_duration;
124 run_duration_sum_ += run_duration;
[email protected]7ceb44482011-12-09 03:41:04125
126 if (queue_duration_max_ < queue_duration)
127 queue_duration_max_ = queue_duration;
128 if (run_duration_max_ < run_duration)
129 run_duration_max_ = run_duration;
[email protected]b6b2b892011-12-04 07:19:10130
131 // Take a uniformly distributed sample over all durations ever supplied.
132 // The probability that we (instead) use this new sample is 1/count_. This
[email protected]15dd9012012-07-26 22:03:01133 // results in a completely uniform selection of the sample (at least when we
134 // don't clamp count_... but that should be inconsequentially likely).
135 // We ignore the fact that we correlated our selection of a sample to the run
136 // and queue times (i.e., we used them to generate random_number).
[email protected]59b15da2013-02-28 04:15:43137 CHECK_GT(count_, 0);
[email protected]7ceb44482011-12-09 03:41:04138 if (0 == (random_number % count_)) {
139 queue_duration_sample_ = queue_duration;
140 run_duration_sample_ = run_duration;
141 }
initial.commitd7cae122008-07-26 21:49:38142}
143
[email protected]b6b2b892011-12-04 07:19:10144int DeathData::count() const { return count_; }
145
[email protected]c186e962012-03-24 22:17:18146int32 DeathData::run_duration_sum() const { return run_duration_sum_; }
[email protected]b6b2b892011-12-04 07:19:10147
[email protected]c186e962012-03-24 22:17:18148int32 DeathData::run_duration_max() const { return run_duration_max_; }
[email protected]b6b2b892011-12-04 07:19:10149
[email protected]c186e962012-03-24 22:17:18150int32 DeathData::run_duration_sample() const {
[email protected]b6b2b892011-12-04 07:19:10151 return run_duration_sample_;
initial.commitd7cae122008-07-26 21:49:38152}
153
[email protected]c186e962012-03-24 22:17:18154int32 DeathData::queue_duration_sum() const {
[email protected]b6b2b892011-12-04 07:19:10155 return queue_duration_sum_;
initial.commitd7cae122008-07-26 21:49:38156}
157
[email protected]c186e962012-03-24 22:17:18158int32 DeathData::queue_duration_max() const {
[email protected]b6b2b892011-12-04 07:19:10159 return queue_duration_max_;
initial.commitd7cae122008-07-26 21:49:38160}
161
[email protected]c186e962012-03-24 22:17:18162int32 DeathData::queue_duration_sample() const {
[email protected]b6b2b892011-12-04 07:19:10163 return queue_duration_sample_;
164}
165
[email protected]b6b2b892011-12-04 07:19:10166void DeathData::ResetMax() {
167 run_duration_max_ = 0;
168 queue_duration_max_ = 0;
169}
170
initial.commitd7cae122008-07-26 21:49:38171void DeathData::Clear() {
172 count_ = 0;
[email protected]b6b2b892011-12-04 07:19:10173 run_duration_sum_ = 0;
174 run_duration_max_ = 0;
175 run_duration_sample_ = 0;
176 queue_duration_sum_ = 0;
177 queue_duration_max_ = 0;
178 queue_duration_sample_ = 0;
initial.commitd7cae122008-07-26 21:49:38179}
180
181//------------------------------------------------------------------------------
[email protected]1cb05db2012-04-13 00:39:26182DeathDataSnapshot::DeathDataSnapshot()
183 : count(-1),
184 run_duration_sum(-1),
185 run_duration_max(-1),
186 run_duration_sample(-1),
187 queue_duration_sum(-1),
188 queue_duration_max(-1),
189 queue_duration_sample(-1) {
190}
191
192DeathDataSnapshot::DeathDataSnapshot(
193 const tracked_objects::DeathData& death_data)
194 : count(death_data.count()),
195 run_duration_sum(death_data.run_duration_sum()),
196 run_duration_max(death_data.run_duration_max()),
197 run_duration_sample(death_data.run_duration_sample()),
198 queue_duration_sum(death_data.queue_duration_sum()),
199 queue_duration_max(death_data.queue_duration_max()),
200 queue_duration_sample(death_data.queue_duration_sample()) {
201}
202
203DeathDataSnapshot::~DeathDataSnapshot() {
204}
205
206//------------------------------------------------------------------------------
[email protected]84baeca2011-10-24 18:55:16207BirthOnThread::BirthOnThread(const Location& location,
208 const ThreadData& current)
initial.commitd7cae122008-07-26 21:49:38209 : location_(location),
[email protected]b6b2b892011-12-04 07:19:10210 birth_thread_(&current) {
211}
212
[email protected]1cb05db2012-04-13 00:39:26213//------------------------------------------------------------------------------
214BirthOnThreadSnapshot::BirthOnThreadSnapshot() {
215}
initial.commitd7cae122008-07-26 21:49:38216
[email protected]1cb05db2012-04-13 00:39:26217BirthOnThreadSnapshot::BirthOnThreadSnapshot(
218 const tracked_objects::BirthOnThread& birth)
219 : location(birth.location()),
220 thread_name(birth.birth_thread()->thread_name()) {
221}
222
223BirthOnThreadSnapshot::~BirthOnThreadSnapshot() {
[email protected]8aa1e6e2011-12-14 01:36:48224}
225
initial.commitd7cae122008-07-26 21:49:38226//------------------------------------------------------------------------------
[email protected]84baeca2011-10-24 18:55:16227Births::Births(const Location& location, const ThreadData& current)
228 : BirthOnThread(location, current),
[email protected]75b79202009-12-30 07:31:45229 birth_count_(1) { }
initial.commitd7cae122008-07-26 21:49:38230
[email protected]b6b2b892011-12-04 07:19:10231int Births::birth_count() const { return birth_count_; }
232
233void Births::RecordBirth() { ++birth_count_; }
234
235void Births::ForgetBirth() { --birth_count_; }
236
237void Births::Clear() { birth_count_ = 0; }
238
initial.commitd7cae122008-07-26 21:49:38239//------------------------------------------------------------------------------
[email protected]b6b2b892011-12-04 07:19:10240// ThreadData maintains the central data for all births and deaths on a single
241// thread.
initial.commitd7cae122008-07-26 21:49:38242
[email protected]b2a9bbd2011-10-31 22:36:21243// TODO(jar): We should pull all these static vars together, into a struct, and
244// optimize layout so that we benefit from locality of reference during accesses
245// to them.
246
[email protected]90895d0f2012-02-15 23:05:01247// static
248NowFunction* ThreadData::now_function_ = NULL;
249
vadimt12f0f7d2014-09-15 19:19:38250// static
251bool ThreadData::now_function_is_time_ = false;
252
[email protected]b2a9bbd2011-10-31 22:36:21253// A TLS slot which points to the ThreadData instance for the current thread. We
254// do a fake initialization here (zeroing out data), and then the real in-place
255// construction happens when we call tls_index_.Initialize().
256// static
[email protected]444b8a3c2012-01-30 16:52:09257base::ThreadLocalStorage::StaticSlot ThreadData::tls_index_ = TLS_INITIALIZER;
[email protected]b2a9bbd2011-10-31 22:36:21258
[email protected]b2a9bbd2011-10-31 22:36:21259// static
[email protected]9a88c902011-11-24 00:00:31260int ThreadData::worker_thread_data_creation_count_ = 0;
261
262// static
263int ThreadData::cleanup_count_ = 0;
[email protected]b2a9bbd2011-10-31 22:36:21264
265// static
266int ThreadData::incarnation_counter_ = 0;
267
initial.commitd7cae122008-07-26 21:49:38268// static
[email protected]84baeca2011-10-24 18:55:16269ThreadData* ThreadData::all_thread_data_list_head_ = NULL;
270
271// static
[email protected]26cdeb962011-11-20 04:17:07272ThreadData* ThreadData::first_retired_worker_ = NULL;
[email protected]84baeca2011-10-24 18:55:16273
initial.commitd7cae122008-07-26 21:49:38274// static
[email protected]9fc44162012-01-23 22:56:41275base::LazyInstance<base::Lock>::Leaky
[email protected]6de0fd1d2011-11-15 13:31:49276 ThreadData::list_lock_ = LAZY_INSTANCE_INITIALIZER;
initial.commitd7cae122008-07-26 21:49:38277
278// static
279ThreadData::Status ThreadData::status_ = ThreadData::UNINITIALIZED;
280
[email protected]84baeca2011-10-24 18:55:16281ThreadData::ThreadData(const std::string& suggested_name)
[email protected]8aa1e6e2011-12-14 01:36:48282 : next_(NULL),
[email protected]26cdeb962011-11-20 04:17:07283 next_retired_worker_(NULL),
[email protected]8aa1e6e2011-12-14 01:36:48284 worker_thread_number_(0),
vadimt12f0f7d2014-09-15 19:19:38285 incarnation_count_for_pool_(-1),
286 current_stopwatch_(NULL) {
[email protected]84b57952011-10-15 23:52:45287 DCHECK_GE(suggested_name.size(), 0u);
288 thread_name_ = suggested_name;
[email protected]b2a9bbd2011-10-31 22:36:21289 PushToHeadOfList(); // Which sets real incarnation_count_for_pool_.
[email protected]84b57952011-10-15 23:52:45290}
291
[email protected]26cdeb962011-11-20 04:17:07292ThreadData::ThreadData(int thread_number)
[email protected]8aa1e6e2011-12-14 01:36:48293 : next_(NULL),
[email protected]26cdeb962011-11-20 04:17:07294 next_retired_worker_(NULL),
[email protected]8aa1e6e2011-12-14 01:36:48295 worker_thread_number_(thread_number),
vadimt12f0f7d2014-09-15 19:19:38296 incarnation_count_for_pool_(-1),
297 current_stopwatch_(NULL) {
[email protected]26cdeb962011-11-20 04:17:07298 CHECK_GT(thread_number, 0);
299 base::StringAppendF(&thread_name_, "WorkerThread-%d", thread_number);
[email protected]63f5b0e2011-11-04 00:23:27300 PushToHeadOfList(); // Which sets real incarnation_count_for_pool_.
[email protected]359d2bf2010-11-19 20:34:18301}
initial.commitd7cae122008-07-26 21:49:38302
[email protected]d4799a32010-09-28 22:54:58303ThreadData::~ThreadData() {}
304
[email protected]84baeca2011-10-24 18:55:16305void ThreadData::PushToHeadOfList() {
[email protected]b6b2b892011-12-04 07:19:10306 // Toss in a hint of randomness (atop the uniniitalized value).
[email protected]ff5e9422011-12-05 15:24:28307 (void)VALGRIND_MAKE_MEM_DEFINED_IF_ADDRESSABLE(&random_number_,
[email protected]a0447ff2011-12-04 21:14:05308 sizeof(random_number_));
[email protected]75086be2013-03-20 21:18:22309 MSAN_UNPOISON(&random_number_, sizeof(random_number_));
gliderf78125752014-11-12 21:05:33310 random_number_ += static_cast<uint32>(this - static_cast<ThreadData*>(0));
[email protected]b6b2b892011-12-04 07:19:10311 random_number_ ^= (Now() - TrackedTime()).InMilliseconds();
312
[email protected]84baeca2011-10-24 18:55:16313 DCHECK(!next_);
[email protected]77169a62011-11-14 20:36:46314 base::AutoLock lock(*list_lock_.Pointer());
[email protected]b2a9bbd2011-10-31 22:36:21315 incarnation_count_for_pool_ = incarnation_counter_;
[email protected]84baeca2011-10-24 18:55:16316 next_ = all_thread_data_list_head_;
317 all_thread_data_list_head_ = this;
318}
319
initial.commitd7cae122008-07-26 21:49:38320// static
[email protected]b6b2b892011-12-04 07:19:10321ThreadData* ThreadData::first() {
322 base::AutoLock lock(*list_lock_.Pointer());
323 return all_thread_data_list_head_;
324}
325
326ThreadData* ThreadData::next() const { return next_; }
327
328// static
[email protected]84b57952011-10-15 23:52:45329void ThreadData::InitializeThreadContext(const std::string& suggested_name) {
[email protected]b2a9bbd2011-10-31 22:36:21330 if (!Initialize()) // Always initialize if needed.
331 return;
332 ThreadData* current_thread_data =
333 reinterpret_cast<ThreadData*>(tls_index_.Get());
334 if (current_thread_data)
335 return; // Browser tests instigate this.
336 current_thread_data = new ThreadData(suggested_name);
[email protected]84baeca2011-10-24 18:55:16337 tls_index_.Set(current_thread_data);
[email protected]84b57952011-10-15 23:52:45338}
initial.commitd7cae122008-07-26 21:49:38339
[email protected]84b57952011-10-15 23:52:45340// static
341ThreadData* ThreadData::Get() {
342 if (!tls_index_.initialized())
343 return NULL; // For unittests only.
[email protected]84baeca2011-10-24 18:55:16344 ThreadData* registered = reinterpret_cast<ThreadData*>(tls_index_.Get());
345 if (registered)
346 return registered;
347
348 // We must be a worker thread, since we didn't pre-register.
349 ThreadData* worker_thread_data = NULL;
[email protected]9a88c902011-11-24 00:00:31350 int worker_thread_number = 0;
[email protected]84baeca2011-10-24 18:55:16351 {
[email protected]77169a62011-11-14 20:36:46352 base::AutoLock lock(*list_lock_.Pointer());
[email protected]26cdeb962011-11-20 04:17:07353 if (first_retired_worker_) {
354 worker_thread_data = first_retired_worker_;
355 first_retired_worker_ = first_retired_worker_->next_retired_worker_;
356 worker_thread_data->next_retired_worker_ = NULL;
[email protected]445029fb2011-11-18 17:03:33357 } else {
[email protected]9a88c902011-11-24 00:00:31358 worker_thread_number = ++worker_thread_data_creation_count_;
[email protected]84baeca2011-10-24 18:55:16359 }
initial.commitd7cae122008-07-26 21:49:38360 }
[email protected]84baeca2011-10-24 18:55:16361
362 // If we can't find a previously used instance, then we have to create one.
[email protected]9a88c902011-11-24 00:00:31363 if (!worker_thread_data) {
364 DCHECK_GT(worker_thread_number, 0);
365 worker_thread_data = new ThreadData(worker_thread_number);
366 }
[email protected]26cdeb962011-11-20 04:17:07367 DCHECK_GT(worker_thread_data->worker_thread_number_, 0);
[email protected]84baeca2011-10-24 18:55:16368
369 tls_index_.Set(worker_thread_data);
370 return worker_thread_data;
[email protected]84b57952011-10-15 23:52:45371}
372
373// static
[email protected]84baeca2011-10-24 18:55:16374void ThreadData::OnThreadTermination(void* thread_data) {
[email protected]8aa1e6e2011-12-14 01:36:48375 DCHECK(thread_data); // TLS should *never* call us with a NULL.
[email protected]9a88c902011-11-24 00:00:31376 // We must NOT do any allocations during this callback. There is a chance
377 // that the allocator is no longer active on this thread.
[email protected]84baeca2011-10-24 18:55:16378 if (!kTrackAllTaskObjects)
379 return; // Not compiled in.
[email protected]84baeca2011-10-24 18:55:16380 reinterpret_cast<ThreadData*>(thread_data)->OnThreadTerminationCleanup();
[email protected]84baeca2011-10-24 18:55:16381}
382
[email protected]26cdeb962011-11-20 04:17:07383void ThreadData::OnThreadTerminationCleanup() {
[email protected]9a88c902011-11-24 00:00:31384 // The list_lock_ was created when we registered the callback, so it won't be
385 // allocated here despite the lazy reference.
[email protected]77169a62011-11-14 20:36:46386 base::AutoLock lock(*list_lock_.Pointer());
[email protected]b2a9bbd2011-10-31 22:36:21387 if (incarnation_counter_ != incarnation_count_for_pool_)
388 return; // ThreadData was constructed in an earlier unit test.
[email protected]9a88c902011-11-24 00:00:31389 ++cleanup_count_;
390 // Only worker threads need to be retired and reused.
391 if (!worker_thread_number_) {
392 return;
393 }
[email protected]26cdeb962011-11-20 04:17:07394 // We must NOT do any allocations during this callback.
395 // Using the simple linked lists avoids all allocations.
396 DCHECK_EQ(this->next_retired_worker_, reinterpret_cast<ThreadData*>(NULL));
397 this->next_retired_worker_ = first_retired_worker_;
398 first_retired_worker_ = this;
initial.commitd7cae122008-07-26 21:49:38399}
400
initial.commitd7cae122008-07-26 21:49:38401// static
[email protected]1cb05db2012-04-13 00:39:26402void ThreadData::Snapshot(bool reset_max, ProcessDataSnapshot* process_data) {
403 // Add births that have run to completion to |collected_data|.
404 // |birth_counts| tracks the total number of births recorded at each location
405 // for which we have not seen a death count.
406 BirthCountMap birth_counts;
407 ThreadData::SnapshotAllExecutedTasks(reset_max, process_data, &birth_counts);
408
409 // Add births that are still active -- i.e. objects that have tallied a birth,
410 // but have not yet tallied a matching death, and hence must be either
411 // running, queued up, or being held in limbo for future posting.
412 for (BirthCountMap::const_iterator it = birth_counts.begin();
413 it != birth_counts.end(); ++it) {
414 if (it->second > 0) {
415 process_data->tasks.push_back(
416 TaskSnapshot(*it->first, DeathData(it->second), "Still_Alive"));
417 }
418 }
[email protected]84baeca2011-10-24 18:55:16419}
420
[email protected]75b79202009-12-30 07:31:45421Births* ThreadData::TallyABirth(const Location& location) {
initial.commitd7cae122008-07-26 21:49:38422 BirthMap::iterator it = birth_map_.find(location);
[email protected]8aa1e6e2011-12-14 01:36:48423 Births* child;
[email protected]75b79202009-12-30 07:31:45424 if (it != birth_map_.end()) {
[email protected]8aa1e6e2011-12-14 01:36:48425 child = it->second;
426 child->RecordBirth();
427 } else {
428 child = new Births(location, *this); // Leak this.
429 // Lock since the map may get relocated now, and other threads sometimes
430 // snapshot it (but they lock before copying it).
431 base::AutoLock lock(map_lock_);
432 birth_map_[location] = child;
[email protected]75b79202009-12-30 07:31:45433 }
initial.commitd7cae122008-07-26 21:49:38434
[email protected]8aa1e6e2011-12-14 01:36:48435 if (kTrackParentChildLinks && status_ > PROFILING_ACTIVE &&
436 !parent_stack_.empty()) {
437 const Births* parent = parent_stack_.top();
438 ParentChildPair pair(parent, child);
439 if (parent_child_set_.find(pair) == parent_child_set_.end()) {
440 // Lock since the map may get relocated now, and other threads sometimes
441 // snapshot it (but they lock before copying it).
442 base::AutoLock lock(map_lock_);
443 parent_child_set_.insert(pair);
444 }
445 }
446
447 return child;
initial.commitd7cae122008-07-26 21:49:38448}
449
[email protected]84baeca2011-10-24 18:55:16450void ThreadData::TallyADeath(const Births& birth,
[email protected]c186e962012-03-24 22:17:18451 int32 queue_duration,
vadimt12f0f7d2014-09-15 19:19:38452 const TaskStopwatch& stopwatch) {
453 int32 run_duration = stopwatch.RunDurationMs();
454
[email protected]b6b2b892011-12-04 07:19:10455 // Stir in some randomness, plus add constant in case durations are zero.
gliderf78125752014-11-12 21:05:33456 const uint32 kSomePrimeNumber = 2147483647;
[email protected]b6b2b892011-12-04 07:19:10457 random_number_ += queue_duration + run_duration + kSomePrimeNumber;
458 // An address is going to have some randomness to it as well ;-).
gliderf78125752014-11-12 21:05:33459 random_number_ ^= static_cast<uint32>(&birth - reinterpret_cast<Births*>(0));
[email protected]b6b2b892011-12-04 07:19:10460
[email protected]90895d0f2012-02-15 23:05:01461 // We don't have queue durations without OS timer. OS timer is automatically
462 // used for task-post-timing, so the use of an alternate timer implies all
vadimt12f0f7d2014-09-15 19:19:38463 // queue times are invalid, unless it was explicitly said that we can trust
464 // the alternate timer.
465 if (kAllowAlternateTimeSourceHandling &&
466 now_function_ &&
467 !now_function_is_time_) {
[email protected]90895d0f2012-02-15 23:05:01468 queue_duration = 0;
vadimt12f0f7d2014-09-15 19:19:38469 }
[email protected]90895d0f2012-02-15 23:05:01470
[email protected]84baeca2011-10-24 18:55:16471 DeathMap::iterator it = death_map_.find(&birth);
472 DeathData* death_data;
initial.commitd7cae122008-07-26 21:49:38473 if (it != death_map_.end()) {
[email protected]84baeca2011-10-24 18:55:16474 death_data = &it->second;
475 } else {
[email protected]9a88c902011-11-24 00:00:31476 base::AutoLock lock(map_lock_); // Lock as the map may get relocated now.
[email protected]84baeca2011-10-24 18:55:16477 death_data = &death_map_[&birth];
478 } // Release lock ASAP.
[email protected]b6b2b892011-12-04 07:19:10479 death_data->RecordDeath(queue_duration, run_duration, random_number_);
[email protected]8aa1e6e2011-12-14 01:36:48480
481 if (!kTrackParentChildLinks)
482 return;
483 if (!parent_stack_.empty()) { // We might get turned off.
484 DCHECK_EQ(parent_stack_.top(), &birth);
485 parent_stack_.pop();
486 }
initial.commitd7cae122008-07-26 21:49:38487}
488
489// static
[email protected]180c85e2011-07-26 18:25:16490Births* ThreadData::TallyABirthIfActive(const Location& location) {
[email protected]84baeca2011-10-24 18:55:16491 if (!kTrackAllTaskObjects)
492 return NULL; // Not compiled in.
493
[email protected]702a12d2012-02-10 19:43:42494 if (!TrackingStatus())
[email protected]84b57952011-10-15 23:52:45495 return NULL;
496 ThreadData* current_thread_data = Get();
497 if (!current_thread_data)
498 return NULL;
499 return current_thread_data->TallyABirth(location);
[email protected]180c85e2011-07-26 18:25:16500}
501
502// static
[email protected]b2a9bbd2011-10-31 22:36:21503void ThreadData::TallyRunOnNamedThreadIfTracking(
504 const base::TrackingInfo& completed_task,
vadimt12f0f7d2014-09-15 19:19:38505 const TaskStopwatch& stopwatch) {
[email protected]84baeca2011-10-24 18:55:16506 if (!kTrackAllTaskObjects)
507 return; // Not compiled in.
508
[email protected]b2a9bbd2011-10-31 22:36:21509 // Even if we have been DEACTIVATED, we will process any pending births so
510 // that our data structures (which counted the outstanding births) remain
511 // consistent.
512 const Births* birth = completed_task.birth_tally;
513 if (!birth)
[email protected]84b57952011-10-15 23:52:45514 return;
vadimt12f0f7d2014-09-15 19:19:38515 ThreadData* current_thread_data = stopwatch.GetThreadData();
[email protected]84b57952011-10-15 23:52:45516 if (!current_thread_data)
517 return;
518
[email protected]b2a9bbd2011-10-31 22:36:21519 // Watch out for a race where status_ is changing, and hence one or both
[email protected]8aa1e6e2011-12-14 01:36:48520 // of start_of_run or end_of_run is zero. In that case, we didn't bother to
[email protected]b2a9bbd2011-10-31 22:36:21521 // get a time value since we "weren't tracking" and we were trying to be
522 // efficient by not calling for a genuine time value. For simplicity, we'll
523 // use a default zero duration when we can't calculate a true value.
vadimt12f0f7d2014-09-15 19:19:38524 TrackedTime start_of_run = stopwatch.StartTime();
[email protected]c186e962012-03-24 22:17:18525 int32 queue_duration = 0;
[email protected]b2a9bbd2011-10-31 22:36:21526 if (!start_of_run.is_null()) {
[email protected]e1a38d602013-07-10 17:50:22527 queue_duration = (start_of_run - completed_task.EffectiveTimePosted())
528 .InMilliseconds();
[email protected]b2a9bbd2011-10-31 22:36:21529 }
vadimt12f0f7d2014-09-15 19:19:38530 current_thread_data->TallyADeath(*birth, queue_duration, stopwatch);
[email protected]b2a9bbd2011-10-31 22:36:21531}
532
533// static
534void ThreadData::TallyRunOnWorkerThreadIfTracking(
535 const Births* birth,
536 const TrackedTime& time_posted,
vadimt12f0f7d2014-09-15 19:19:38537 const TaskStopwatch& stopwatch) {
[email protected]b2a9bbd2011-10-31 22:36:21538 if (!kTrackAllTaskObjects)
539 return; // Not compiled in.
540
541 // Even if we have been DEACTIVATED, we will process any pending births so
542 // that our data structures (which counted the outstanding births) remain
543 // consistent.
544 if (!birth)
545 return;
546
547 // TODO(jar): Support the option to coalesce all worker-thread activity under
548 // one ThreadData instance that uses locks to protect *all* access. This will
549 // reduce memory (making it provably bounded), but run incrementally slower
[email protected]d6992b5b2013-05-20 18:53:13550 // (since we'll use locks on TallyABirth and TallyADeath). The good news is
551 // that the locks on TallyADeath will be *after* the worker thread has run,
552 // and hence nothing will be waiting for the completion (... besides some
553 // other thread that might like to run). Also, the worker threads tasks are
[email protected]b2a9bbd2011-10-31 22:36:21554 // generally longer, and hence the cost of the lock may perchance be amortized
555 // over the long task's lifetime.
vadimt12f0f7d2014-09-15 19:19:38556 ThreadData* current_thread_data = stopwatch.GetThreadData();
[email protected]b2a9bbd2011-10-31 22:36:21557 if (!current_thread_data)
558 return;
559
vadimt12f0f7d2014-09-15 19:19:38560 TrackedTime start_of_run = stopwatch.StartTime();
[email protected]c186e962012-03-24 22:17:18561 int32 queue_duration = 0;
[email protected]b2a9bbd2011-10-31 22:36:21562 if (!start_of_run.is_null()) {
[email protected]c25db182011-11-11 22:40:27563 queue_duration = (start_of_run - time_posted).InMilliseconds();
[email protected]b2a9bbd2011-10-31 22:36:21564 }
vadimt12f0f7d2014-09-15 19:19:38565 current_thread_data->TallyADeath(*birth, queue_duration, stopwatch);
[email protected]180c85e2011-07-26 18:25:16566}
567
568// static
[email protected]dbe5d2072011-11-08 17:09:21569void ThreadData::TallyRunInAScopedRegionIfTracking(
570 const Births* birth,
vadimt12f0f7d2014-09-15 19:19:38571 const TaskStopwatch& stopwatch) {
[email protected]dbe5d2072011-11-08 17:09:21572 if (!kTrackAllTaskObjects)
573 return; // Not compiled in.
574
575 // Even if we have been DEACTIVATED, we will process any pending births so
576 // that our data structures (which counted the outstanding births) remain
577 // consistent.
578 if (!birth)
579 return;
580
vadimt12f0f7d2014-09-15 19:19:38581 ThreadData* current_thread_data = stopwatch.GetThreadData();
[email protected]dbe5d2072011-11-08 17:09:21582 if (!current_thread_data)
583 return;
584
[email protected]c186e962012-03-24 22:17:18585 int32 queue_duration = 0;
vadimt12f0f7d2014-09-15 19:19:38586 current_thread_data->TallyADeath(*birth, queue_duration, stopwatch);
[email protected]dbe5d2072011-11-08 17:09:21587}
588
[email protected]1cb05db2012-04-13 00:39:26589// static
590void ThreadData::SnapshotAllExecutedTasks(bool reset_max,
591 ProcessDataSnapshot* process_data,
592 BirthCountMap* birth_counts) {
593 if (!kTrackAllTaskObjects)
594 return; // Not compiled in.
595
596 // Get an unchanging copy of a ThreadData list.
597 ThreadData* my_list = ThreadData::first();
598
599 // Gather data serially.
600 // This hackish approach *can* get some slighly corrupt tallies, as we are
601 // grabbing values without the protection of a lock, but it has the advantage
602 // of working even with threads that don't have message loops. If a user
603 // sees any strangeness, they can always just run their stats gathering a
604 // second time.
605 for (ThreadData* thread_data = my_list;
606 thread_data;
607 thread_data = thread_data->next()) {
608 thread_data->SnapshotExecutedTasks(reset_max, process_data, birth_counts);
609 }
610}
611
612void ThreadData::SnapshotExecutedTasks(bool reset_max,
613 ProcessDataSnapshot* process_data,
614 BirthCountMap* birth_counts) {
615 // Get copy of data, so that the data will not change during the iterations
616 // and processing.
617 ThreadData::BirthMap birth_map;
618 ThreadData::DeathMap death_map;
619 ThreadData::ParentChildSet parent_child_set;
620 SnapshotMaps(reset_max, &birth_map, &death_map, &parent_child_set);
621
622 for (ThreadData::DeathMap::const_iterator it = death_map.begin();
623 it != death_map.end(); ++it) {
624 process_data->tasks.push_back(
625 TaskSnapshot(*it->first, it->second, thread_name()));
626 (*birth_counts)[it->first] -= it->first->birth_count();
627 }
628
629 for (ThreadData::BirthMap::const_iterator it = birth_map.begin();
630 it != birth_map.end(); ++it) {
631 (*birth_counts)[it->second] += it->second->birth_count();
632 }
633
634 if (!kTrackParentChildLinks)
635 return;
636
637 for (ThreadData::ParentChildSet::const_iterator it = parent_child_set.begin();
638 it != parent_child_set.end(); ++it) {
639 process_data->descendants.push_back(ParentChildPairSnapshot(*it));
640 }
641}
642
initial.commitd7cae122008-07-26 21:49:38643// This may be called from another thread.
[email protected]b6b2b892011-12-04 07:19:10644void ThreadData::SnapshotMaps(bool reset_max,
645 BirthMap* birth_map,
[email protected]8aa1e6e2011-12-14 01:36:48646 DeathMap* death_map,
647 ParentChildSet* parent_child_set) {
[email protected]9a88c902011-11-24 00:00:31648 base::AutoLock lock(map_lock_);
initial.commitd7cae122008-07-26 21:49:38649 for (BirthMap::const_iterator it = birth_map_.begin();
650 it != birth_map_.end(); ++it)
[email protected]b6b2b892011-12-04 07:19:10651 (*birth_map)[it->first] = it->second;
652 for (DeathMap::iterator it = death_map_.begin();
653 it != death_map_.end(); ++it) {
654 (*death_map)[it->first] = it->second;
655 if (reset_max)
656 it->second.ResetMax();
657 }
[email protected]8aa1e6e2011-12-14 01:36:48658
659 if (!kTrackParentChildLinks)
660 return;
661
662 for (ParentChildSet::iterator it = parent_child_set_.begin();
663 it != parent_child_set_.end(); ++it)
664 parent_child_set->insert(*it);
initial.commitd7cae122008-07-26 21:49:38665}
666
[email protected]b6b2b892011-12-04 07:19:10667// static
[email protected]75b79202009-12-30 07:31:45668void ThreadData::ResetAllThreadData() {
[email protected]84baeca2011-10-24 18:55:16669 ThreadData* my_list = first();
[email protected]75b79202009-12-30 07:31:45670
671 for (ThreadData* thread_data = my_list;
672 thread_data;
673 thread_data = thread_data->next())
674 thread_data->Reset();
675}
676
677void ThreadData::Reset() {
[email protected]9a88c902011-11-24 00:00:31678 base::AutoLock lock(map_lock_);
[email protected]75b79202009-12-30 07:31:45679 for (DeathMap::iterator it = death_map_.begin();
680 it != death_map_.end(); ++it)
681 it->second.Clear();
682 for (BirthMap::iterator it = birth_map_.begin();
683 it != birth_map_.end(); ++it)
684 it->second->Clear();
685}
686
[email protected]90895d0f2012-02-15 23:05:01687static void OptionallyInitializeAlternateTimer() {
[email protected]ed0fd002012-04-25 23:10:34688 NowFunction* alternate_time_source = GetAlternateTimeSource();
689 if (alternate_time_source)
690 ThreadData::SetAlternateTimeSource(alternate_time_source);
[email protected]90895d0f2012-02-15 23:05:01691}
692
[email protected]b2a9bbd2011-10-31 22:36:21693bool ThreadData::Initialize() {
[email protected]84baeca2011-10-24 18:55:16694 if (!kTrackAllTaskObjects)
695 return false; // Not compiled in.
[email protected]8aa1e6e2011-12-14 01:36:48696 if (status_ >= DEACTIVATED)
[email protected]94b555ee2011-11-15 21:50:36697 return true; // Someone else did the initialization.
698 // Due to racy lazy initialization in tests, we'll need to recheck status_
699 // after we acquire the lock.
700
701 // Ensure that we don't double initialize tls. We are called when single
702 // threaded in the product, but some tests may be racy and lazy about our
703 // initialization.
704 base::AutoLock lock(*list_lock_.Pointer());
[email protected]8aa1e6e2011-12-14 01:36:48705 if (status_ >= DEACTIVATED)
[email protected]94b555ee2011-11-15 21:50:36706 return true; // Someone raced in here and beat us.
707
[email protected]90895d0f2012-02-15 23:05:01708 // Put an alternate timer in place if the environment calls for it, such as
709 // for tracking TCMalloc allocations. This insertion is idempotent, so we
710 // don't mind if there is a race, and we'd prefer not to be in a lock while
711 // doing this work.
712 if (kAllowAlternateTimeSourceHandling)
713 OptionallyInitializeAlternateTimer();
714
[email protected]b2a9bbd2011-10-31 22:36:21715 // Perform the "real" TLS initialization now, and leave it intact through
[email protected]84baeca2011-10-24 18:55:16716 // process termination.
[email protected]94b555ee2011-11-15 21:50:36717 if (!tls_index_.initialized()) { // Testing may have initialized this.
[email protected]8aa1e6e2011-12-14 01:36:48718 DCHECK_EQ(status_, UNINITIALIZED);
[email protected]84baeca2011-10-24 18:55:16719 tls_index_.Initialize(&ThreadData::OnThreadTermination);
[email protected]94b555ee2011-11-15 21:50:36720 if (!tls_index_.initialized())
721 return false;
[email protected]8aa1e6e2011-12-14 01:36:48722 } else {
723 // TLS was initialzed for us earlier.
724 DCHECK_EQ(status_, DORMANT_DURING_TESTS);
[email protected]94b555ee2011-11-15 21:50:36725 }
[email protected]3f095c0a2011-10-31 15:32:08726
[email protected]94b555ee2011-11-15 21:50:36727 // Incarnation counter is only significant to testing, as it otherwise will
728 // never again change in this process.
[email protected]b2a9bbd2011-10-31 22:36:21729 ++incarnation_counter_;
[email protected]94b555ee2011-11-15 21:50:36730
731 // The lock is not critical for setting status_, but it doesn't hurt. It also
732 // ensures that if we have a racy initialization, that we'll bail as soon as
733 // we get the lock earlier in this method.
734 status_ = kInitialStartupState;
[email protected]8aa1e6e2011-12-14 01:36:48735 if (!kTrackParentChildLinks &&
736 kInitialStartupState == PROFILING_CHILDREN_ACTIVE)
737 status_ = PROFILING_ACTIVE;
[email protected]94b555ee2011-11-15 21:50:36738 DCHECK(status_ != UNINITIALIZED);
initial.commitd7cae122008-07-26 21:49:38739 return true;
740}
741
742// static
[email protected]702a12d2012-02-10 19:43:42743bool ThreadData::InitializeAndSetTrackingStatus(Status status) {
744 DCHECK_GE(status, DEACTIVATED);
745 DCHECK_LE(status, PROFILING_CHILDREN_ACTIVE);
746
[email protected]b2a9bbd2011-10-31 22:36:21747 if (!Initialize()) // No-op if already initialized.
748 return false; // Not compiled in.
749
[email protected]702a12d2012-02-10 19:43:42750 if (!kTrackParentChildLinks && status > DEACTIVATED)
751 status = PROFILING_ACTIVE;
752 status_ = status;
[email protected]b2a9bbd2011-10-31 22:36:21753 return true;
754}
755
756// static
[email protected]702a12d2012-02-10 19:43:42757ThreadData::Status ThreadData::status() {
758 return status_;
759}
760
761// static
762bool ThreadData::TrackingStatus() {
[email protected]8aa1e6e2011-12-14 01:36:48763 return status_ > DEACTIVATED;
initial.commitd7cae122008-07-26 21:49:38764}
765
766// static
[email protected]702a12d2012-02-10 19:43:42767bool ThreadData::TrackingParentChildStatus() {
[email protected]8aa1e6e2011-12-14 01:36:48768 return status_ >= PROFILING_CHILDREN_ACTIVE;
769}
770
771// static
vadimt12f0f7d2014-09-15 19:19:38772void ThreadData::PrepareForStartOfRun(const Births* parent) {
[email protected]8aa1e6e2011-12-14 01:36:48773 if (kTrackParentChildLinks && parent && status_ > PROFILING_ACTIVE) {
774 ThreadData* current_thread_data = Get();
775 if (current_thread_data)
776 current_thread_data->parent_stack_.push(parent);
777 }
[email protected]dda97682011-11-14 05:24:07778}
779
780// static
[email protected]90895d0f2012-02-15 23:05:01781void ThreadData::SetAlternateTimeSource(NowFunction* now_function) {
782 DCHECK(now_function);
783 if (kAllowAlternateTimeSourceHandling)
784 now_function_ = now_function;
785}
786
787// static
vadimta1568312014-11-06 22:27:43788void ThreadData::EnableProfilerTiming() {
789 base::subtle::NoBarrier_Store(&g_profiler_timing_enabled, ENABLED_TIMING);
790}
791
792// static
[email protected]b2a9bbd2011-10-31 22:36:21793TrackedTime ThreadData::Now() {
[email protected]90895d0f2012-02-15 23:05:01794 if (kAllowAlternateTimeSourceHandling && now_function_)
795 return TrackedTime::FromMilliseconds((*now_function_)());
[email protected]915b344f2013-12-11 12:49:17796 if (kTrackAllTaskObjects && IsProfilerTimingEnabled() && TrackingStatus())
[email protected]b2a9bbd2011-10-31 22:36:21797 return TrackedTime::Now();
798 return TrackedTime(); // Super fast when disabled, or not compiled.
[email protected]84b57952011-10-15 23:52:45799}
initial.commitd7cae122008-07-26 21:49:38800
801// static
[email protected]9a88c902011-11-24 00:00:31802void ThreadData::EnsureCleanupWasCalled(int major_threads_shutdown_count) {
803 base::AutoLock lock(*list_lock_.Pointer());
804 if (worker_thread_data_creation_count_ == 0)
805 return; // We haven't really run much, and couldn't have leaked.
[email protected]30de3a32014-03-14 18:25:48806
807 // TODO(jar): until this is working on XP, don't run the real test.
808#if 0
[email protected]9a88c902011-11-24 00:00:31809 // Verify that we've at least shutdown/cleanup the major namesd threads. The
810 // caller should tell us how many thread shutdowns should have taken place by
811 // now.
[email protected]9a88c902011-11-24 00:00:31812 CHECK_GT(cleanup_count_, major_threads_shutdown_count);
[email protected]30de3a32014-03-14 18:25:48813#endif
[email protected]9a88c902011-11-24 00:00:31814}
815
816// static
[email protected]b2a9bbd2011-10-31 22:36:21817void ThreadData::ShutdownSingleThreadedCleanup(bool leak) {
[email protected]84baeca2011-10-24 18:55:16818 // This is only called from test code, where we need to cleanup so that
819 // additional tests can be run.
initial.commitd7cae122008-07-26 21:49:38820 // We must be single threaded... but be careful anyway.
[email protected]702a12d2012-02-10 19:43:42821 if (!InitializeAndSetTrackingStatus(DEACTIVATED))
initial.commitd7cae122008-07-26 21:49:38822 return;
823 ThreadData* thread_data_list;
824 {
[email protected]77169a62011-11-14 20:36:46825 base::AutoLock lock(*list_lock_.Pointer());
[email protected]84baeca2011-10-24 18:55:16826 thread_data_list = all_thread_data_list_head_;
827 all_thread_data_list_head_ = NULL;
[email protected]b2a9bbd2011-10-31 22:36:21828 ++incarnation_counter_;
[email protected]26cdeb962011-11-20 04:17:07829 // To be clean, break apart the retired worker list (though we leak them).
[email protected]b6b2b892011-12-04 07:19:10830 while (first_retired_worker_) {
[email protected]26cdeb962011-11-20 04:17:07831 ThreadData* worker = first_retired_worker_;
832 CHECK_GT(worker->worker_thread_number_, 0);
833 first_retired_worker_ = worker->next_retired_worker_;
834 worker->next_retired_worker_ = NULL;
835 }
initial.commitd7cae122008-07-26 21:49:38836 }
837
[email protected]b2a9bbd2011-10-31 22:36:21838 // Put most global static back in pristine shape.
[email protected]9a88c902011-11-24 00:00:31839 worker_thread_data_creation_count_ = 0;
840 cleanup_count_ = 0;
[email protected]b2a9bbd2011-10-31 22:36:21841 tls_index_.Set(NULL);
[email protected]8aa1e6e2011-12-14 01:36:48842 status_ = DORMANT_DURING_TESTS; // Almost UNINITIALIZED.
[email protected]b2a9bbd2011-10-31 22:36:21843
844 // To avoid any chance of racing in unit tests, which is the only place we
845 // call this function, we may sometimes leak all the data structures we
846 // recovered, as they may still be in use on threads from prior tests!
[email protected]bf709abd2013-06-10 11:32:20847 if (leak) {
848 ThreadData* thread_data = thread_data_list;
849 while (thread_data) {
850 ANNOTATE_LEAKING_OBJECT_PTR(thread_data);
851 thread_data = thread_data->next();
852 }
[email protected]b2a9bbd2011-10-31 22:36:21853 return;
[email protected]bf709abd2013-06-10 11:32:20854 }
[email protected]b2a9bbd2011-10-31 22:36:21855
856 // When we want to cleanup (on a single thread), here is what we do.
857
[email protected]84baeca2011-10-24 18:55:16858 // Do actual recursive delete in all ThreadData instances.
initial.commitd7cae122008-07-26 21:49:38859 while (thread_data_list) {
860 ThreadData* next_thread_data = thread_data_list;
861 thread_data_list = thread_data_list->next();
862
863 for (BirthMap::iterator it = next_thread_data->birth_map_.begin();
864 next_thread_data->birth_map_.end() != it; ++it)
865 delete it->second; // Delete the Birth Records.
initial.commitd7cae122008-07-26 21:49:38866 delete next_thread_data; // Includes all Death Records.
867 }
initial.commitd7cae122008-07-26 21:49:38868}
869
initial.commitd7cae122008-07-26 21:49:38870//------------------------------------------------------------------------------
vadimt12f0f7d2014-09-15 19:19:38871TaskStopwatch::TaskStopwatch()
vadimt20175532014-10-28 20:14:20872 : wallclock_duration_ms_(0),
873 current_thread_data_(NULL),
vadimt12f0f7d2014-09-15 19:19:38874 excluded_duration_ms_(0),
875 parent_(NULL) {
876#if DCHECK_IS_ON
vadimt20175532014-10-28 20:14:20877 state_ = CREATED;
vadimt12f0f7d2014-09-15 19:19:38878 child_ = NULL;
879#endif
vadimt20175532014-10-28 20:14:20880}
vadimt12f0f7d2014-09-15 19:19:38881
vadimt20175532014-10-28 20:14:20882TaskStopwatch::~TaskStopwatch() {
883#if DCHECK_IS_ON
884 DCHECK(state_ != RUNNING);
885 DCHECK(child_ == NULL);
886#endif
887}
888
889void TaskStopwatch::Start() {
890#if DCHECK_IS_ON
891 DCHECK(state_ == CREATED);
892 state_ = RUNNING;
893#endif
894
895 start_time_ = ThreadData::Now();
896
897 current_thread_data_ = ThreadData::Get();
vadimt12f0f7d2014-09-15 19:19:38898 if (!current_thread_data_)
899 return;
900
901 parent_ = current_thread_data_->current_stopwatch_;
902#if DCHECK_IS_ON
903 if (parent_) {
904 DCHECK(parent_->state_ == RUNNING);
905 DCHECK(parent_->child_ == NULL);
906 parent_->child_ = this;
907 }
908#endif
909 current_thread_data_->current_stopwatch_ = this;
910}
911
vadimt12f0f7d2014-09-15 19:19:38912void TaskStopwatch::Stop() {
913 const TrackedTime end_time = ThreadData::Now();
914#if DCHECK_IS_ON
915 DCHECK(state_ == RUNNING);
916 state_ = STOPPED;
917 DCHECK(child_ == NULL);
918#endif
919
920 if (!start_time_.is_null() && !end_time.is_null()) {
921 wallclock_duration_ms_ = (end_time - start_time_).InMilliseconds();
922 }
923
924 if (!current_thread_data_)
925 return;
926
927 DCHECK(current_thread_data_->current_stopwatch_ == this);
928 current_thread_data_->current_stopwatch_ = parent_;
929 if (!parent_)
930 return;
931
932#if DCHECK_IS_ON
933 DCHECK(parent_->state_ == RUNNING);
934 DCHECK(parent_->child_ == this);
935 parent_->child_ = NULL;
936#endif
vadimt20175532014-10-28 20:14:20937 parent_->excluded_duration_ms_ += wallclock_duration_ms_;
vadimt12f0f7d2014-09-15 19:19:38938 parent_ = NULL;
939}
940
941TrackedTime TaskStopwatch::StartTime() const {
vadimt20175532014-10-28 20:14:20942#if DCHECK_IS_ON
943 DCHECK(state_ != CREATED);
944#endif
945
vadimt12f0f7d2014-09-15 19:19:38946 return start_time_;
947}
948
949int32 TaskStopwatch::RunDurationMs() const {
950#if DCHECK_IS_ON
951 DCHECK(state_ == STOPPED);
952#endif
953
954 return wallclock_duration_ms_ - excluded_duration_ms_;
955}
956
957ThreadData* TaskStopwatch::GetThreadData() const {
vadimt20175532014-10-28 20:14:20958#if DCHECK_IS_ON
959 DCHECK(state_ != CREATED);
960#endif
961
vadimt12f0f7d2014-09-15 19:19:38962 return current_thread_data_;
963}
964
965//------------------------------------------------------------------------------
[email protected]1cb05db2012-04-13 00:39:26966TaskSnapshot::TaskSnapshot() {
initial.commitd7cae122008-07-26 21:49:38967}
968
[email protected]1cb05db2012-04-13 00:39:26969TaskSnapshot::TaskSnapshot(const BirthOnThread& birth,
970 const DeathData& death_data,
971 const std::string& death_thread_name)
972 : birth(birth),
973 death_data(death_data),
974 death_thread_name(death_thread_name) {
initial.commitd7cae122008-07-26 21:49:38975}
976
[email protected]1cb05db2012-04-13 00:39:26977TaskSnapshot::~TaskSnapshot() {
[email protected]84baeca2011-10-24 18:55:16978}
979
initial.commitd7cae122008-07-26 21:49:38980//------------------------------------------------------------------------------
[email protected]1cb05db2012-04-13 00:39:26981// ParentChildPairSnapshot
initial.commitd7cae122008-07-26 21:49:38982
[email protected]15dd9012012-07-26 22:03:01983ParentChildPairSnapshot::ParentChildPairSnapshot() {
[email protected]d4799a32010-09-28 22:54:58984}
985
[email protected]1cb05db2012-04-13 00:39:26986ParentChildPairSnapshot::ParentChildPairSnapshot(
987 const ThreadData::ParentChildPair& parent_child)
988 : parent(*parent_child.first),
989 child(*parent_child.second) {
initial.commitd7cae122008-07-26 21:49:38990}
991
[email protected]1cb05db2012-04-13 00:39:26992ParentChildPairSnapshot::~ParentChildPairSnapshot() {
initial.commitd7cae122008-07-26 21:49:38993}
994
[email protected]1cb05db2012-04-13 00:39:26995//------------------------------------------------------------------------------
996// ProcessDataSnapshot
997
998ProcessDataSnapshot::ProcessDataSnapshot()
[email protected]fe5d4062012-04-23 21:18:19999#if !defined(OS_NACL)
[email protected]1cb05db2012-04-13 00:39:261000 : process_id(base::GetCurrentProcId()) {
[email protected]fe5d4062012-04-23 21:18:191001#else
1002 : process_id(0) {
1003#endif
initial.commitd7cae122008-07-26 21:49:381004}
1005
[email protected]1cb05db2012-04-13 00:39:261006ProcessDataSnapshot::~ProcessDataSnapshot() {
[email protected]84baeca2011-10-24 18:55:161007}
1008
initial.commitd7cae122008-07-26 21:49:381009} // namespace tracked_objects