Don't attempt to set the window shape on Mac.
[chromium-blink-merge.git] / base / tracked_objects.cc
bloba60e260b23660b31140593f6b7e0b63652a18f8b
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/tracked_objects.h"
7 #include <limits.h>
8 #include <stdlib.h>
10 #include "base/atomicops.h"
11 #include "base/base_switches.h"
12 #include "base/command_line.h"
13 #include "base/compiler_specific.h"
14 #include "base/debug/leak_annotations.h"
15 #include "base/logging.h"
16 #include "base/process/process_handle.h"
17 #include "base/profiler/alternate_timer.h"
18 #include "base/strings/stringprintf.h"
19 #include "base/third_party/valgrind/memcheck.h"
20 #include "base/tracking_info.h"
22 using base::TimeDelta;
24 namespace base {
25 class TimeDelta;
28 namespace tracked_objects {
30 namespace {
31 // TODO(jar): Evaluate the perf impact of enabling this. If the perf impact is
32 // negligible, enable by default.
33 // Flag to compile out parent-child link recording.
34 const bool kTrackParentChildLinks = false;
36 // When ThreadData is first initialized, should we start in an ACTIVE state to
37 // record all of the startup-time tasks, or should we start up DEACTIVATED, so
38 // that we only record after parsing the command line flag --enable-tracking.
39 // Note that the flag may force either state, so this really controls only the
40 // period of time up until that flag is parsed. If there is no flag seen, then
41 // this state may prevail for much or all of the process lifetime.
42 const ThreadData::Status kInitialStartupState =
43 ThreadData::PROFILING_CHILDREN_ACTIVE;
45 // Control whether an alternate time source (Now() function) is supported by
46 // the ThreadData class. This compile time flag should be set to true if we
47 // want other modules (such as a memory allocator, or a thread-specific CPU time
48 // clock) to be able to provide a thread-specific Now() function. Without this
49 // compile-time flag, the code will only support the wall-clock time. This flag
50 // can be flipped to efficiently disable this path (if there is a performance
51 // problem with its presence).
52 static const bool kAllowAlternateTimeSourceHandling = true;
54 // Possible states of the profiler timing enabledness.
55 enum {
56 UNDEFINED_TIMING,
57 ENABLED_TIMING,
58 DISABLED_TIMING,
61 // State of the profiler timing enabledness.
62 base::subtle::Atomic32 g_profiler_timing_enabled = UNDEFINED_TIMING;
64 // Returns whether profiler timing is enabled. The default is true, but this may
65 // be overridden by a command-line flag. Some platforms may programmatically set
66 // this command-line flag to the "off" value if it's not specified.
67 // This in turn can be overridden by explicitly calling
68 // ThreadData::EnableProfilerTiming, say, based on a field trial.
69 inline bool IsProfilerTimingEnabled() {
70 // Reading |g_profiler_timing_enabled| is done without barrier because
71 // multiple initialization is not an issue while the barrier can be relatively
72 // costly given that this method is sometimes called in a tight loop.
73 base::subtle::Atomic32 current_timing_enabled =
74 base::subtle::NoBarrier_Load(&g_profiler_timing_enabled);
75 if (current_timing_enabled == UNDEFINED_TIMING) {
76 if (!base::CommandLine::InitializedForCurrentProcess())
77 return true;
78 current_timing_enabled =
79 (base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
80 switches::kProfilerTiming) ==
81 switches::kProfilerTimingDisabledValue)
82 ? DISABLED_TIMING
83 : ENABLED_TIMING;
84 base::subtle::NoBarrier_Store(&g_profiler_timing_enabled,
85 current_timing_enabled);
87 return current_timing_enabled == ENABLED_TIMING;
90 } // namespace
92 //------------------------------------------------------------------------------
93 // DeathData tallies durations when a death takes place.
95 DeathData::DeathData() {
96 Clear();
99 DeathData::DeathData(int count) {
100 Clear();
101 count_ = count;
104 // TODO(jar): I need to see if this macro to optimize branching is worth using.
106 // This macro has no branching, so it is surely fast, and is equivalent to:
107 // if (assign_it)
108 // target = source;
109 // We use a macro rather than a template to force this to inline.
110 // Related code for calculating max is discussed on the web.
111 #define CONDITIONAL_ASSIGN(assign_it, target, source) \
112 ((target) ^= ((target) ^ (source)) & -static_cast<int32>(assign_it))
114 void DeathData::RecordDeath(const int32 queue_duration,
115 const int32 run_duration,
116 const uint32 random_number) {
117 // We'll just clamp at INT_MAX, but we should note this in the UI as such.
118 if (count_ < INT_MAX)
119 ++count_;
120 queue_duration_sum_ += queue_duration;
121 run_duration_sum_ += run_duration;
123 if (queue_duration_max_ < queue_duration)
124 queue_duration_max_ = queue_duration;
125 if (run_duration_max_ < run_duration)
126 run_duration_max_ = run_duration;
128 // Take a uniformly distributed sample over all durations ever supplied.
129 // The probability that we (instead) use this new sample is 1/count_. This
130 // results in a completely uniform selection of the sample (at least when we
131 // don't clamp count_... but that should be inconsequentially likely).
132 // We ignore the fact that we correlated our selection of a sample to the run
133 // and queue times (i.e., we used them to generate random_number).
134 CHECK_GT(count_, 0);
135 if (0 == (random_number % count_)) {
136 queue_duration_sample_ = queue_duration;
137 run_duration_sample_ = run_duration;
141 int DeathData::count() const { return count_; }
143 int32 DeathData::run_duration_sum() const { return run_duration_sum_; }
145 int32 DeathData::run_duration_max() const { return run_duration_max_; }
147 int32 DeathData::run_duration_sample() const {
148 return run_duration_sample_;
151 int32 DeathData::queue_duration_sum() const {
152 return queue_duration_sum_;
155 int32 DeathData::queue_duration_max() const {
156 return queue_duration_max_;
159 int32 DeathData::queue_duration_sample() const {
160 return queue_duration_sample_;
163 void DeathData::Clear() {
164 count_ = 0;
165 run_duration_sum_ = 0;
166 run_duration_max_ = 0;
167 run_duration_sample_ = 0;
168 queue_duration_sum_ = 0;
169 queue_duration_max_ = 0;
170 queue_duration_sample_ = 0;
173 //------------------------------------------------------------------------------
174 DeathDataSnapshot::DeathDataSnapshot()
175 : count(-1),
176 run_duration_sum(-1),
177 run_duration_max(-1),
178 run_duration_sample(-1),
179 queue_duration_sum(-1),
180 queue_duration_max(-1),
181 queue_duration_sample(-1) {
184 DeathDataSnapshot::DeathDataSnapshot(
185 const tracked_objects::DeathData& death_data)
186 : count(death_data.count()),
187 run_duration_sum(death_data.run_duration_sum()),
188 run_duration_max(death_data.run_duration_max()),
189 run_duration_sample(death_data.run_duration_sample()),
190 queue_duration_sum(death_data.queue_duration_sum()),
191 queue_duration_max(death_data.queue_duration_max()),
192 queue_duration_sample(death_data.queue_duration_sample()) {
195 DeathDataSnapshot::~DeathDataSnapshot() {
198 //------------------------------------------------------------------------------
199 BirthOnThread::BirthOnThread(const Location& location,
200 const ThreadData& current)
201 : location_(location),
202 birth_thread_(&current) {
205 //------------------------------------------------------------------------------
206 BirthOnThreadSnapshot::BirthOnThreadSnapshot() {
209 BirthOnThreadSnapshot::BirthOnThreadSnapshot(
210 const tracked_objects::BirthOnThread& birth)
211 : location(birth.location()),
212 thread_name(birth.birth_thread()->thread_name()) {
215 BirthOnThreadSnapshot::~BirthOnThreadSnapshot() {
218 //------------------------------------------------------------------------------
219 Births::Births(const Location& location, const ThreadData& current)
220 : BirthOnThread(location, current),
221 birth_count_(1) { }
223 int Births::birth_count() const { return birth_count_; }
225 void Births::RecordBirth() { ++birth_count_; }
227 //------------------------------------------------------------------------------
228 // ThreadData maintains the central data for all births and deaths on a single
229 // thread.
231 // TODO(jar): We should pull all these static vars together, into a struct, and
232 // optimize layout so that we benefit from locality of reference during accesses
233 // to them.
235 // static
236 NowFunction* ThreadData::now_function_ = NULL;
238 // static
239 bool ThreadData::now_function_is_time_ = false;
241 // A TLS slot which points to the ThreadData instance for the current thread. We
242 // do a fake initialization here (zeroing out data), and then the real in-place
243 // construction happens when we call tls_index_.Initialize().
244 // static
245 base::ThreadLocalStorage::StaticSlot ThreadData::tls_index_ = TLS_INITIALIZER;
247 // static
248 int ThreadData::worker_thread_data_creation_count_ = 0;
250 // static
251 int ThreadData::cleanup_count_ = 0;
253 // static
254 int ThreadData::incarnation_counter_ = 0;
256 // static
257 ThreadData* ThreadData::all_thread_data_list_head_ = NULL;
259 // static
260 ThreadData* ThreadData::first_retired_worker_ = NULL;
262 // static
263 base::LazyInstance<base::Lock>::Leaky
264 ThreadData::list_lock_ = LAZY_INSTANCE_INITIALIZER;
266 // static
267 ThreadData::Status ThreadData::status_ = ThreadData::UNINITIALIZED;
269 ThreadData::ThreadData(const std::string& suggested_name)
270 : next_(NULL),
271 next_retired_worker_(NULL),
272 worker_thread_number_(0),
273 incarnation_count_for_pool_(-1),
274 current_stopwatch_(NULL) {
275 DCHECK_GE(suggested_name.size(), 0u);
276 thread_name_ = suggested_name;
277 PushToHeadOfList(); // Which sets real incarnation_count_for_pool_.
280 ThreadData::ThreadData(int thread_number)
281 : next_(NULL),
282 next_retired_worker_(NULL),
283 worker_thread_number_(thread_number),
284 incarnation_count_for_pool_(-1),
285 current_stopwatch_(NULL) {
286 CHECK_GT(thread_number, 0);
287 base::StringAppendF(&thread_name_, "WorkerThread-%d", thread_number);
288 PushToHeadOfList(); // Which sets real incarnation_count_for_pool_.
291 ThreadData::~ThreadData() {}
293 void ThreadData::PushToHeadOfList() {
294 // Toss in a hint of randomness (atop the uniniitalized value).
295 (void)VALGRIND_MAKE_MEM_DEFINED_IF_ADDRESSABLE(&random_number_,
296 sizeof(random_number_));
297 MSAN_UNPOISON(&random_number_, sizeof(random_number_));
298 random_number_ += static_cast<uint32>(this - static_cast<ThreadData*>(0));
299 random_number_ ^= (Now() - TrackedTime()).InMilliseconds();
301 DCHECK(!next_);
302 base::AutoLock lock(*list_lock_.Pointer());
303 incarnation_count_for_pool_ = incarnation_counter_;
304 next_ = all_thread_data_list_head_;
305 all_thread_data_list_head_ = this;
308 // static
309 ThreadData* ThreadData::first() {
310 base::AutoLock lock(*list_lock_.Pointer());
311 return all_thread_data_list_head_;
314 ThreadData* ThreadData::next() const { return next_; }
316 // static
317 void ThreadData::InitializeThreadContext(const std::string& suggested_name) {
318 if (!Initialize()) // Always initialize if needed.
319 return;
320 ThreadData* current_thread_data =
321 reinterpret_cast<ThreadData*>(tls_index_.Get());
322 if (current_thread_data)
323 return; // Browser tests instigate this.
324 current_thread_data = new ThreadData(suggested_name);
325 tls_index_.Set(current_thread_data);
328 // static
329 ThreadData* ThreadData::Get() {
330 if (!tls_index_.initialized())
331 return NULL; // For unittests only.
332 ThreadData* registered = reinterpret_cast<ThreadData*>(tls_index_.Get());
333 if (registered)
334 return registered;
336 // We must be a worker thread, since we didn't pre-register.
337 ThreadData* worker_thread_data = NULL;
338 int worker_thread_number = 0;
340 base::AutoLock lock(*list_lock_.Pointer());
341 if (first_retired_worker_) {
342 worker_thread_data = first_retired_worker_;
343 first_retired_worker_ = first_retired_worker_->next_retired_worker_;
344 worker_thread_data->next_retired_worker_ = NULL;
345 } else {
346 worker_thread_number = ++worker_thread_data_creation_count_;
350 // If we can't find a previously used instance, then we have to create one.
351 if (!worker_thread_data) {
352 DCHECK_GT(worker_thread_number, 0);
353 worker_thread_data = new ThreadData(worker_thread_number);
355 DCHECK_GT(worker_thread_data->worker_thread_number_, 0);
357 tls_index_.Set(worker_thread_data);
358 return worker_thread_data;
361 // static
362 void ThreadData::OnThreadTermination(void* thread_data) {
363 DCHECK(thread_data); // TLS should *never* call us with a NULL.
364 // We must NOT do any allocations during this callback. There is a chance
365 // that the allocator is no longer active on this thread.
366 reinterpret_cast<ThreadData*>(thread_data)->OnThreadTerminationCleanup();
369 void ThreadData::OnThreadTerminationCleanup() {
370 // The list_lock_ was created when we registered the callback, so it won't be
371 // allocated here despite the lazy reference.
372 base::AutoLock lock(*list_lock_.Pointer());
373 if (incarnation_counter_ != incarnation_count_for_pool_)
374 return; // ThreadData was constructed in an earlier unit test.
375 ++cleanup_count_;
376 // Only worker threads need to be retired and reused.
377 if (!worker_thread_number_) {
378 return;
380 // We must NOT do any allocations during this callback.
381 // Using the simple linked lists avoids all allocations.
382 DCHECK_EQ(this->next_retired_worker_, reinterpret_cast<ThreadData*>(NULL));
383 this->next_retired_worker_ = first_retired_worker_;
384 first_retired_worker_ = this;
387 // static
388 void ThreadData::Snapshot(ProcessDataSnapshot* process_data_snapshot) {
389 ThreadData::SnapshotCurrentPhase(
390 &process_data_snapshot->phased_process_data_snapshots[0]);
393 Births* ThreadData::TallyABirth(const Location& location) {
394 BirthMap::iterator it = birth_map_.find(location);
395 Births* child;
396 if (it != birth_map_.end()) {
397 child = it->second;
398 child->RecordBirth();
399 } else {
400 child = new Births(location, *this); // Leak this.
401 // Lock since the map may get relocated now, and other threads sometimes
402 // snapshot it (but they lock before copying it).
403 base::AutoLock lock(map_lock_);
404 birth_map_[location] = child;
407 if (kTrackParentChildLinks && status_ > PROFILING_ACTIVE &&
408 !parent_stack_.empty()) {
409 const Births* parent = parent_stack_.top();
410 ParentChildPair pair(parent, child);
411 if (parent_child_set_.find(pair) == parent_child_set_.end()) {
412 // Lock since the map may get relocated now, and other threads sometimes
413 // snapshot it (but they lock before copying it).
414 base::AutoLock lock(map_lock_);
415 parent_child_set_.insert(pair);
419 return child;
422 void ThreadData::TallyADeath(const Births& birth,
423 int32 queue_duration,
424 const TaskStopwatch& stopwatch) {
425 int32 run_duration = stopwatch.RunDurationMs();
427 // Stir in some randomness, plus add constant in case durations are zero.
428 const uint32 kSomePrimeNumber = 2147483647;
429 random_number_ += queue_duration + run_duration + kSomePrimeNumber;
430 // An address is going to have some randomness to it as well ;-).
431 random_number_ ^= static_cast<uint32>(&birth - reinterpret_cast<Births*>(0));
433 // We don't have queue durations without OS timer. OS timer is automatically
434 // used for task-post-timing, so the use of an alternate timer implies all
435 // queue times are invalid, unless it was explicitly said that we can trust
436 // the alternate timer.
437 if (kAllowAlternateTimeSourceHandling &&
438 now_function_ &&
439 !now_function_is_time_) {
440 queue_duration = 0;
443 DeathMap::iterator it = death_map_.find(&birth);
444 DeathData* death_data;
445 if (it != death_map_.end()) {
446 death_data = &it->second;
447 } else {
448 base::AutoLock lock(map_lock_); // Lock as the map may get relocated now.
449 death_data = &death_map_[&birth];
450 } // Release lock ASAP.
451 death_data->RecordDeath(queue_duration, run_duration, random_number_);
453 if (!kTrackParentChildLinks)
454 return;
455 if (!parent_stack_.empty()) { // We might get turned off.
456 DCHECK_EQ(parent_stack_.top(), &birth);
457 parent_stack_.pop();
461 // static
462 Births* ThreadData::TallyABirthIfActive(const Location& location) {
463 if (!TrackingStatus())
464 return NULL;
465 ThreadData* current_thread_data = Get();
466 if (!current_thread_data)
467 return NULL;
468 return current_thread_data->TallyABirth(location);
471 // static
472 void ThreadData::TallyRunOnNamedThreadIfTracking(
473 const base::TrackingInfo& completed_task,
474 const TaskStopwatch& stopwatch) {
475 // Even if we have been DEACTIVATED, we will process any pending births so
476 // that our data structures (which counted the outstanding births) remain
477 // consistent.
478 const Births* birth = completed_task.birth_tally;
479 if (!birth)
480 return;
481 ThreadData* current_thread_data = stopwatch.GetThreadData();
482 if (!current_thread_data)
483 return;
485 // Watch out for a race where status_ is changing, and hence one or both
486 // of start_of_run or end_of_run is zero. In that case, we didn't bother to
487 // get a time value since we "weren't tracking" and we were trying to be
488 // efficient by not calling for a genuine time value. For simplicity, we'll
489 // use a default zero duration when we can't calculate a true value.
490 TrackedTime start_of_run = stopwatch.StartTime();
491 int32 queue_duration = 0;
492 if (!start_of_run.is_null()) {
493 queue_duration = (start_of_run - completed_task.EffectiveTimePosted())
494 .InMilliseconds();
496 current_thread_data->TallyADeath(*birth, queue_duration, stopwatch);
499 // static
500 void ThreadData::TallyRunOnWorkerThreadIfTracking(
501 const Births* birth,
502 const TrackedTime& time_posted,
503 const TaskStopwatch& stopwatch) {
504 // Even if we have been DEACTIVATED, we will process any pending births so
505 // that our data structures (which counted the outstanding births) remain
506 // consistent.
507 if (!birth)
508 return;
510 // TODO(jar): Support the option to coalesce all worker-thread activity under
511 // one ThreadData instance that uses locks to protect *all* access. This will
512 // reduce memory (making it provably bounded), but run incrementally slower
513 // (since we'll use locks on TallyABirth and TallyADeath). The good news is
514 // that the locks on TallyADeath will be *after* the worker thread has run,
515 // and hence nothing will be waiting for the completion (... besides some
516 // other thread that might like to run). Also, the worker threads tasks are
517 // generally longer, and hence the cost of the lock may perchance be amortized
518 // over the long task's lifetime.
519 ThreadData* current_thread_data = stopwatch.GetThreadData();
520 if (!current_thread_data)
521 return;
523 TrackedTime start_of_run = stopwatch.StartTime();
524 int32 queue_duration = 0;
525 if (!start_of_run.is_null()) {
526 queue_duration = (start_of_run - time_posted).InMilliseconds();
528 current_thread_data->TallyADeath(*birth, queue_duration, stopwatch);
531 // static
532 void ThreadData::TallyRunInAScopedRegionIfTracking(
533 const Births* birth,
534 const TaskStopwatch& stopwatch) {
535 // Even if we have been DEACTIVATED, we will process any pending births so
536 // that our data structures (which counted the outstanding births) remain
537 // consistent.
538 if (!birth)
539 return;
541 ThreadData* current_thread_data = stopwatch.GetThreadData();
542 if (!current_thread_data)
543 return;
545 int32 queue_duration = 0;
546 current_thread_data->TallyADeath(*birth, queue_duration, stopwatch);
549 // static
550 void ThreadData::SnapshotAllExecutedTasks(
551 ProcessDataPhaseSnapshot* process_data_phase,
552 BirthCountMap* birth_counts) {
553 // Get an unchanging copy of a ThreadData list.
554 ThreadData* my_list = ThreadData::first();
556 // Gather data serially.
557 // This hackish approach *can* get some slighly corrupt tallies, as we are
558 // grabbing values without the protection of a lock, but it has the advantage
559 // of working even with threads that don't have message loops. If a user
560 // sees any strangeness, they can always just run their stats gathering a
561 // second time.
562 for (ThreadData* thread_data = my_list;
563 thread_data;
564 thread_data = thread_data->next()) {
565 thread_data->SnapshotExecutedTasks(process_data_phase, birth_counts);
569 // static
570 void ThreadData::SnapshotCurrentPhase(
571 ProcessDataPhaseSnapshot* process_data_phase) {
572 // Add births that have run to completion to |collected_data|.
573 // |birth_counts| tracks the total number of births recorded at each location
574 // for which we have not seen a death count.
575 BirthCountMap birth_counts;
576 ThreadData::SnapshotAllExecutedTasks(process_data_phase, &birth_counts);
578 // Add births that are still active -- i.e. objects that have tallied a birth,
579 // but have not yet tallied a matching death, and hence must be either
580 // running, queued up, or being held in limbo for future posting.
581 for (const auto& birth_count : birth_counts) {
582 if (birth_count.second > 0) {
583 process_data_phase->tasks.push_back(TaskSnapshot(
584 *birth_count.first, DeathData(birth_count.second), "Still_Alive"));
589 void ThreadData::SnapshotExecutedTasks(
590 ProcessDataPhaseSnapshot* process_data_phase,
591 BirthCountMap* birth_counts) {
592 // Get copy of data, so that the data will not change during the iterations
593 // and processing.
594 ThreadData::BirthMap birth_map;
595 ThreadData::DeathMap death_map;
596 ThreadData::ParentChildSet parent_child_set;
597 SnapshotMaps(&birth_map, &death_map, &parent_child_set);
599 for (const auto& death : death_map) {
600 process_data_phase->tasks.push_back(
601 TaskSnapshot(*death.first, death.second, thread_name()));
602 (*birth_counts)[death.first] -= death.first->birth_count();
605 for (const auto& birth : birth_map) {
606 (*birth_counts)[birth.second] += birth.second->birth_count();
609 if (!kTrackParentChildLinks)
610 return;
612 for (const auto& parent_child : parent_child_set) {
613 process_data_phase->descendants.push_back(
614 ParentChildPairSnapshot(parent_child));
618 // This may be called from another thread.
619 void ThreadData::SnapshotMaps(BirthMap* birth_map,
620 DeathMap* death_map,
621 ParentChildSet* parent_child_set) {
622 base::AutoLock lock(map_lock_);
623 for (const auto& birth : birth_map_)
624 (*birth_map)[birth.first] = birth.second;
625 for (const auto& death : death_map_)
626 (*death_map)[death.first] = death.second;
628 if (!kTrackParentChildLinks)
629 return;
631 for (const auto& parent_child : parent_child_set_)
632 parent_child_set->insert(parent_child);
635 static void OptionallyInitializeAlternateTimer() {
636 NowFunction* alternate_time_source = GetAlternateTimeSource();
637 if (alternate_time_source)
638 ThreadData::SetAlternateTimeSource(alternate_time_source);
641 bool ThreadData::Initialize() {
642 if (status_ >= DEACTIVATED)
643 return true; // Someone else did the initialization.
644 // Due to racy lazy initialization in tests, we'll need to recheck status_
645 // after we acquire the lock.
647 // Ensure that we don't double initialize tls. We are called when single
648 // threaded in the product, but some tests may be racy and lazy about our
649 // initialization.
650 base::AutoLock lock(*list_lock_.Pointer());
651 if (status_ >= DEACTIVATED)
652 return true; // Someone raced in here and beat us.
654 // Put an alternate timer in place if the environment calls for it, such as
655 // for tracking TCMalloc allocations. This insertion is idempotent, so we
656 // don't mind if there is a race, and we'd prefer not to be in a lock while
657 // doing this work.
658 if (kAllowAlternateTimeSourceHandling)
659 OptionallyInitializeAlternateTimer();
661 // Perform the "real" TLS initialization now, and leave it intact through
662 // process termination.
663 if (!tls_index_.initialized()) { // Testing may have initialized this.
664 DCHECK_EQ(status_, UNINITIALIZED);
665 tls_index_.Initialize(&ThreadData::OnThreadTermination);
666 if (!tls_index_.initialized())
667 return false;
668 } else {
669 // TLS was initialzed for us earlier.
670 DCHECK_EQ(status_, DORMANT_DURING_TESTS);
673 // Incarnation counter is only significant to testing, as it otherwise will
674 // never again change in this process.
675 ++incarnation_counter_;
677 // The lock is not critical for setting status_, but it doesn't hurt. It also
678 // ensures that if we have a racy initialization, that we'll bail as soon as
679 // we get the lock earlier in this method.
680 status_ = kInitialStartupState;
681 if (!kTrackParentChildLinks &&
682 kInitialStartupState == PROFILING_CHILDREN_ACTIVE)
683 status_ = PROFILING_ACTIVE;
684 DCHECK(status_ != UNINITIALIZED);
685 return true;
688 // static
689 bool ThreadData::InitializeAndSetTrackingStatus(Status status) {
690 DCHECK_GE(status, DEACTIVATED);
691 DCHECK_LE(status, PROFILING_CHILDREN_ACTIVE);
693 if (!Initialize()) // No-op if already initialized.
694 return false; // Not compiled in.
696 if (!kTrackParentChildLinks && status > DEACTIVATED)
697 status = PROFILING_ACTIVE;
698 status_ = status;
699 return true;
702 // static
703 ThreadData::Status ThreadData::status() {
704 return status_;
707 // static
708 bool ThreadData::TrackingStatus() {
709 return status_ > DEACTIVATED;
712 // static
713 bool ThreadData::TrackingParentChildStatus() {
714 return status_ >= PROFILING_CHILDREN_ACTIVE;
717 // static
718 void ThreadData::PrepareForStartOfRun(const Births* parent) {
719 if (kTrackParentChildLinks && parent && status_ > PROFILING_ACTIVE) {
720 ThreadData* current_thread_data = Get();
721 if (current_thread_data)
722 current_thread_data->parent_stack_.push(parent);
726 // static
727 void ThreadData::SetAlternateTimeSource(NowFunction* now_function) {
728 DCHECK(now_function);
729 if (kAllowAlternateTimeSourceHandling)
730 now_function_ = now_function;
733 // static
734 void ThreadData::EnableProfilerTiming() {
735 base::subtle::NoBarrier_Store(&g_profiler_timing_enabled, ENABLED_TIMING);
738 // static
739 TrackedTime ThreadData::Now() {
740 if (kAllowAlternateTimeSourceHandling && now_function_)
741 return TrackedTime::FromMilliseconds((*now_function_)());
742 if (IsProfilerTimingEnabled() && TrackingStatus())
743 return TrackedTime::Now();
744 return TrackedTime(); // Super fast when disabled, or not compiled.
747 // static
748 void ThreadData::EnsureCleanupWasCalled(int major_threads_shutdown_count) {
749 base::AutoLock lock(*list_lock_.Pointer());
750 if (worker_thread_data_creation_count_ == 0)
751 return; // We haven't really run much, and couldn't have leaked.
753 // TODO(jar): until this is working on XP, don't run the real test.
754 #if 0
755 // Verify that we've at least shutdown/cleanup the major namesd threads. The
756 // caller should tell us how many thread shutdowns should have taken place by
757 // now.
758 CHECK_GT(cleanup_count_, major_threads_shutdown_count);
759 #endif
762 // static
763 void ThreadData::ShutdownSingleThreadedCleanup(bool leak) {
764 // This is only called from test code, where we need to cleanup so that
765 // additional tests can be run.
766 // We must be single threaded... but be careful anyway.
767 if (!InitializeAndSetTrackingStatus(DEACTIVATED))
768 return;
769 ThreadData* thread_data_list;
771 base::AutoLock lock(*list_lock_.Pointer());
772 thread_data_list = all_thread_data_list_head_;
773 all_thread_data_list_head_ = NULL;
774 ++incarnation_counter_;
775 // To be clean, break apart the retired worker list (though we leak them).
776 while (first_retired_worker_) {
777 ThreadData* worker = first_retired_worker_;
778 CHECK_GT(worker->worker_thread_number_, 0);
779 first_retired_worker_ = worker->next_retired_worker_;
780 worker->next_retired_worker_ = NULL;
784 // Put most global static back in pristine shape.
785 worker_thread_data_creation_count_ = 0;
786 cleanup_count_ = 0;
787 tls_index_.Set(NULL);
788 status_ = DORMANT_DURING_TESTS; // Almost UNINITIALIZED.
790 // To avoid any chance of racing in unit tests, which is the only place we
791 // call this function, we may sometimes leak all the data structures we
792 // recovered, as they may still be in use on threads from prior tests!
793 if (leak) {
794 ThreadData* thread_data = thread_data_list;
795 while (thread_data) {
796 ANNOTATE_LEAKING_OBJECT_PTR(thread_data);
797 thread_data = thread_data->next();
799 return;
802 // When we want to cleanup (on a single thread), here is what we do.
804 // Do actual recursive delete in all ThreadData instances.
805 while (thread_data_list) {
806 ThreadData* next_thread_data = thread_data_list;
807 thread_data_list = thread_data_list->next();
809 for (BirthMap::iterator it = next_thread_data->birth_map_.begin();
810 next_thread_data->birth_map_.end() != it; ++it)
811 delete it->second; // Delete the Birth Records.
812 delete next_thread_data; // Includes all Death Records.
816 //------------------------------------------------------------------------------
817 TaskStopwatch::TaskStopwatch()
818 : wallclock_duration_ms_(0),
819 current_thread_data_(NULL),
820 excluded_duration_ms_(0),
821 parent_(NULL) {
822 #if DCHECK_IS_ON()
823 state_ = CREATED;
824 child_ = NULL;
825 #endif
828 TaskStopwatch::~TaskStopwatch() {
829 #if DCHECK_IS_ON()
830 DCHECK(state_ != RUNNING);
831 DCHECK(child_ == NULL);
832 #endif
835 void TaskStopwatch::Start() {
836 #if DCHECK_IS_ON()
837 DCHECK(state_ == CREATED);
838 state_ = RUNNING;
839 #endif
841 start_time_ = ThreadData::Now();
843 current_thread_data_ = ThreadData::Get();
844 if (!current_thread_data_)
845 return;
847 parent_ = current_thread_data_->current_stopwatch_;
848 #if DCHECK_IS_ON()
849 if (parent_) {
850 DCHECK(parent_->state_ == RUNNING);
851 DCHECK(parent_->child_ == NULL);
852 parent_->child_ = this;
854 #endif
855 current_thread_data_->current_stopwatch_ = this;
858 void TaskStopwatch::Stop() {
859 const TrackedTime end_time = ThreadData::Now();
860 #if DCHECK_IS_ON()
861 DCHECK(state_ == RUNNING);
862 state_ = STOPPED;
863 DCHECK(child_ == NULL);
864 #endif
866 if (!start_time_.is_null() && !end_time.is_null()) {
867 wallclock_duration_ms_ = (end_time - start_time_).InMilliseconds();
870 if (!current_thread_data_)
871 return;
873 DCHECK(current_thread_data_->current_stopwatch_ == this);
874 current_thread_data_->current_stopwatch_ = parent_;
875 if (!parent_)
876 return;
878 #if DCHECK_IS_ON()
879 DCHECK(parent_->state_ == RUNNING);
880 DCHECK(parent_->child_ == this);
881 parent_->child_ = NULL;
882 #endif
883 parent_->excluded_duration_ms_ += wallclock_duration_ms_;
884 parent_ = NULL;
887 TrackedTime TaskStopwatch::StartTime() const {
888 #if DCHECK_IS_ON()
889 DCHECK(state_ != CREATED);
890 #endif
892 return start_time_;
895 int32 TaskStopwatch::RunDurationMs() const {
896 #if DCHECK_IS_ON()
897 DCHECK(state_ == STOPPED);
898 #endif
900 return wallclock_duration_ms_ - excluded_duration_ms_;
903 ThreadData* TaskStopwatch::GetThreadData() const {
904 #if DCHECK_IS_ON()
905 DCHECK(state_ != CREATED);
906 #endif
908 return current_thread_data_;
911 //------------------------------------------------------------------------------
912 TaskSnapshot::TaskSnapshot() {
915 TaskSnapshot::TaskSnapshot(const BirthOnThread& birth,
916 const DeathData& death_data,
917 const std::string& death_thread_name)
918 : birth(birth),
919 death_data(death_data),
920 death_thread_name(death_thread_name) {
923 TaskSnapshot::~TaskSnapshot() {
926 //------------------------------------------------------------------------------
927 // ParentChildPairSnapshot
929 ParentChildPairSnapshot::ParentChildPairSnapshot() {
932 ParentChildPairSnapshot::ParentChildPairSnapshot(
933 const ThreadData::ParentChildPair& parent_child)
934 : parent(*parent_child.first),
935 child(*parent_child.second) {
938 ParentChildPairSnapshot::~ParentChildPairSnapshot() {
941 //------------------------------------------------------------------------------
942 // ProcessDataPhaseSnapshot
944 ProcessDataPhaseSnapshot::ProcessDataPhaseSnapshot() {
947 ProcessDataPhaseSnapshot::~ProcessDataPhaseSnapshot() {
950 //------------------------------------------------------------------------------
951 // ProcessDataPhaseSnapshot
953 ProcessDataSnapshot::ProcessDataSnapshot()
954 #if !defined(OS_NACL)
955 : process_id(base::GetCurrentProcId()) {
956 #else
957 : process_id(base::kNullProcessId) {
958 #endif
961 ProcessDataSnapshot::~ProcessDataSnapshot() {
964 } // namespace tracked_objects