Copy Widevine files for swarming tests
[chromium-blink-merge.git] / base / tracked_objects.cc
blob32ec75cd79543b4e1e563446f37e1a26cc52783c
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 // Flag to compile out almost all of the task tracking code.
32 const bool kTrackAllTaskObjects = true;
34 // TODO(jar): Evaluate the perf impact of enabling this. If the perf impact is
35 // negligible, enable by default.
36 // Flag to compile out parent-child link recording.
37 const bool kTrackParentChildLinks = false;
39 // When ThreadData is first initialized, should we start in an ACTIVE state to
40 // record all of the startup-time tasks, or should we start up DEACTIVATED, so
41 // that we only record after parsing the command line flag --enable-tracking.
42 // Note that the flag may force either state, so this really controls only the
43 // period of time up until that flag is parsed. If there is no flag seen, then
44 // this state may prevail for much or all of the process lifetime.
45 const ThreadData::Status kInitialStartupState =
46 ThreadData::PROFILING_CHILDREN_ACTIVE;
48 // Control whether an alternate time source (Now() function) is supported by
49 // the ThreadData class. This compile time flag should be set to true if we
50 // want other modules (such as a memory allocator, or a thread-specific CPU time
51 // clock) to be able to provide a thread-specific Now() function. Without this
52 // compile-time flag, the code will only support the wall-clock time. This flag
53 // can be flipped to efficiently disable this path (if there is a performance
54 // problem with its presence).
55 static const bool kAllowAlternateTimeSourceHandling = true;
57 // Possible states of the profiler timing enabledness.
58 enum {
59 UNDEFINED_TIMING,
60 ENABLED_TIMING,
61 DISABLED_TIMING,
64 // State of the profiler timing enabledness.
65 base::subtle::Atomic32 g_profiler_timing_enabled = UNDEFINED_TIMING;
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.
72 inline bool IsProfilerTimingEnabled() {
73 // 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.
76 base::subtle::Atomic32 current_timing_enabled =
77 base::subtle::NoBarrier_Load(&g_profiler_timing_enabled);
78 if (current_timing_enabled == UNDEFINED_TIMING) {
79 if (!base::CommandLine::InitializedForCurrentProcess())
80 return true;
81 current_timing_enabled =
82 (base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
83 switches::kProfilerTiming) ==
84 switches::kProfilerTimingDisabledValue)
85 ? DISABLED_TIMING
86 : ENABLED_TIMING;
87 base::subtle::NoBarrier_Store(&g_profiler_timing_enabled,
88 current_timing_enabled);
90 return current_timing_enabled == ENABLED_TIMING;
93 } // namespace
95 //------------------------------------------------------------------------------
96 // DeathData tallies durations when a death takes place.
98 DeathData::DeathData() {
99 Clear();
102 DeathData::DeathData(int count) {
103 Clear();
104 count_ = count;
107 // TODO(jar): I need to see if this macro to optimize branching is worth using.
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) \
115 ((target) ^= ((target) ^ (source)) & -static_cast<int32>(assign_it))
117 void DeathData::RecordDeath(const int32 queue_duration,
118 const int32 run_duration,
119 const uint32 random_number) {
120 // We'll just clamp at INT_MAX, but we should note this in the UI as such.
121 if (count_ < INT_MAX)
122 ++count_;
123 queue_duration_sum_ += queue_duration;
124 run_duration_sum_ += run_duration;
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;
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
133 // 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).
137 CHECK_GT(count_, 0);
138 if (0 == (random_number % count_)) {
139 queue_duration_sample_ = queue_duration;
140 run_duration_sample_ = run_duration;
144 int DeathData::count() const { return count_; }
146 int32 DeathData::run_duration_sum() const { return run_duration_sum_; }
148 int32 DeathData::run_duration_max() const { return run_duration_max_; }
150 int32 DeathData::run_duration_sample() const {
151 return run_duration_sample_;
154 int32 DeathData::queue_duration_sum() const {
155 return queue_duration_sum_;
158 int32 DeathData::queue_duration_max() const {
159 return queue_duration_max_;
162 int32 DeathData::queue_duration_sample() const {
163 return queue_duration_sample_;
166 void DeathData::Clear() {
167 count_ = 0;
168 run_duration_sum_ = 0;
169 run_duration_max_ = 0;
170 run_duration_sample_ = 0;
171 queue_duration_sum_ = 0;
172 queue_duration_max_ = 0;
173 queue_duration_sample_ = 0;
176 //------------------------------------------------------------------------------
177 DeathDataSnapshot::DeathDataSnapshot()
178 : count(-1),
179 run_duration_sum(-1),
180 run_duration_max(-1),
181 run_duration_sample(-1),
182 queue_duration_sum(-1),
183 queue_duration_max(-1),
184 queue_duration_sample(-1) {
187 DeathDataSnapshot::DeathDataSnapshot(
188 const tracked_objects::DeathData& death_data)
189 : count(death_data.count()),
190 run_duration_sum(death_data.run_duration_sum()),
191 run_duration_max(death_data.run_duration_max()),
192 run_duration_sample(death_data.run_duration_sample()),
193 queue_duration_sum(death_data.queue_duration_sum()),
194 queue_duration_max(death_data.queue_duration_max()),
195 queue_duration_sample(death_data.queue_duration_sample()) {
198 DeathDataSnapshot::~DeathDataSnapshot() {
201 //------------------------------------------------------------------------------
202 BirthOnThread::BirthOnThread(const Location& location,
203 const ThreadData& current)
204 : location_(location),
205 birth_thread_(&current) {
208 //------------------------------------------------------------------------------
209 BirthOnThreadSnapshot::BirthOnThreadSnapshot() {
212 BirthOnThreadSnapshot::BirthOnThreadSnapshot(
213 const tracked_objects::BirthOnThread& birth)
214 : location(birth.location()),
215 thread_name(birth.birth_thread()->thread_name()) {
218 BirthOnThreadSnapshot::~BirthOnThreadSnapshot() {
221 //------------------------------------------------------------------------------
222 Births::Births(const Location& location, const ThreadData& current)
223 : BirthOnThread(location, current),
224 birth_count_(1) { }
226 int Births::birth_count() const { return birth_count_; }
228 void Births::RecordBirth() { ++birth_count_; }
230 //------------------------------------------------------------------------------
231 // ThreadData maintains the central data for all births and deaths on a single
232 // thread.
234 // TODO(jar): We should pull all these static vars together, into a struct, and
235 // optimize layout so that we benefit from locality of reference during accesses
236 // to them.
238 // static
239 NowFunction* ThreadData::now_function_ = NULL;
241 // static
242 bool ThreadData::now_function_is_time_ = false;
244 // A TLS slot which points to the ThreadData instance for the current thread. We
245 // do a fake initialization here (zeroing out data), and then the real in-place
246 // construction happens when we call tls_index_.Initialize().
247 // static
248 base::ThreadLocalStorage::StaticSlot ThreadData::tls_index_ = TLS_INITIALIZER;
250 // static
251 int ThreadData::worker_thread_data_creation_count_ = 0;
253 // static
254 int ThreadData::cleanup_count_ = 0;
256 // static
257 int ThreadData::incarnation_counter_ = 0;
259 // static
260 ThreadData* ThreadData::all_thread_data_list_head_ = NULL;
262 // static
263 ThreadData* ThreadData::first_retired_worker_ = NULL;
265 // static
266 base::LazyInstance<base::Lock>::Leaky
267 ThreadData::list_lock_ = LAZY_INSTANCE_INITIALIZER;
269 // static
270 ThreadData::Status ThreadData::status_ = ThreadData::UNINITIALIZED;
272 ThreadData::ThreadData(const std::string& suggested_name)
273 : next_(NULL),
274 next_retired_worker_(NULL),
275 worker_thread_number_(0),
276 incarnation_count_for_pool_(-1),
277 current_stopwatch_(NULL) {
278 DCHECK_GE(suggested_name.size(), 0u);
279 thread_name_ = suggested_name;
280 PushToHeadOfList(); // Which sets real incarnation_count_for_pool_.
283 ThreadData::ThreadData(int thread_number)
284 : next_(NULL),
285 next_retired_worker_(NULL),
286 worker_thread_number_(thread_number),
287 incarnation_count_for_pool_(-1),
288 current_stopwatch_(NULL) {
289 CHECK_GT(thread_number, 0);
290 base::StringAppendF(&thread_name_, "WorkerThread-%d", thread_number);
291 PushToHeadOfList(); // Which sets real incarnation_count_for_pool_.
294 ThreadData::~ThreadData() {}
296 void ThreadData::PushToHeadOfList() {
297 // Toss in a hint of randomness (atop the uniniitalized value).
298 (void)VALGRIND_MAKE_MEM_DEFINED_IF_ADDRESSABLE(&random_number_,
299 sizeof(random_number_));
300 MSAN_UNPOISON(&random_number_, sizeof(random_number_));
301 random_number_ += static_cast<uint32>(this - static_cast<ThreadData*>(0));
302 random_number_ ^= (Now() - TrackedTime()).InMilliseconds();
304 DCHECK(!next_);
305 base::AutoLock lock(*list_lock_.Pointer());
306 incarnation_count_for_pool_ = incarnation_counter_;
307 next_ = all_thread_data_list_head_;
308 all_thread_data_list_head_ = this;
311 // static
312 ThreadData* ThreadData::first() {
313 base::AutoLock lock(*list_lock_.Pointer());
314 return all_thread_data_list_head_;
317 ThreadData* ThreadData::next() const { return next_; }
319 // static
320 void ThreadData::InitializeThreadContext(const std::string& suggested_name) {
321 if (!Initialize()) // Always initialize if needed.
322 return;
323 ThreadData* current_thread_data =
324 reinterpret_cast<ThreadData*>(tls_index_.Get());
325 if (current_thread_data)
326 return; // Browser tests instigate this.
327 current_thread_data = new ThreadData(suggested_name);
328 tls_index_.Set(current_thread_data);
331 // static
332 ThreadData* ThreadData::Get() {
333 if (!tls_index_.initialized())
334 return NULL; // For unittests only.
335 ThreadData* registered = reinterpret_cast<ThreadData*>(tls_index_.Get());
336 if (registered)
337 return registered;
339 // We must be a worker thread, since we didn't pre-register.
340 ThreadData* worker_thread_data = NULL;
341 int worker_thread_number = 0;
343 base::AutoLock lock(*list_lock_.Pointer());
344 if (first_retired_worker_) {
345 worker_thread_data = first_retired_worker_;
346 first_retired_worker_ = first_retired_worker_->next_retired_worker_;
347 worker_thread_data->next_retired_worker_ = NULL;
348 } else {
349 worker_thread_number = ++worker_thread_data_creation_count_;
353 // If we can't find a previously used instance, then we have to create one.
354 if (!worker_thread_data) {
355 DCHECK_GT(worker_thread_number, 0);
356 worker_thread_data = new ThreadData(worker_thread_number);
358 DCHECK_GT(worker_thread_data->worker_thread_number_, 0);
360 tls_index_.Set(worker_thread_data);
361 return worker_thread_data;
364 // static
365 void ThreadData::OnThreadTermination(void* thread_data) {
366 DCHECK(thread_data); // TLS should *never* call us with a NULL.
367 // We must NOT do any allocations during this callback. There is a chance
368 // that the allocator is no longer active on this thread.
369 if (!kTrackAllTaskObjects)
370 return; // Not compiled in.
371 reinterpret_cast<ThreadData*>(thread_data)->OnThreadTerminationCleanup();
374 void ThreadData::OnThreadTerminationCleanup() {
375 // The list_lock_ was created when we registered the callback, so it won't be
376 // allocated here despite the lazy reference.
377 base::AutoLock lock(*list_lock_.Pointer());
378 if (incarnation_counter_ != incarnation_count_for_pool_)
379 return; // ThreadData was constructed in an earlier unit test.
380 ++cleanup_count_;
381 // Only worker threads need to be retired and reused.
382 if (!worker_thread_number_) {
383 return;
385 // We must NOT do any allocations during this callback.
386 // Using the simple linked lists avoids all allocations.
387 DCHECK_EQ(this->next_retired_worker_, reinterpret_cast<ThreadData*>(NULL));
388 this->next_retired_worker_ = first_retired_worker_;
389 first_retired_worker_ = this;
392 // static
393 void ThreadData::Snapshot(ProcessDataSnapshot* process_data_snapshot) {
394 ThreadData::SnapshotCurrentPhase(
395 &process_data_snapshot->phased_process_data_snapshots[0]);
398 Births* ThreadData::TallyABirth(const Location& location) {
399 BirthMap::iterator it = birth_map_.find(location);
400 Births* child;
401 if (it != birth_map_.end()) {
402 child = it->second;
403 child->RecordBirth();
404 } else {
405 child = new Births(location, *this); // Leak this.
406 // Lock since the map may get relocated now, and other threads sometimes
407 // snapshot it (but they lock before copying it).
408 base::AutoLock lock(map_lock_);
409 birth_map_[location] = child;
412 if (kTrackParentChildLinks && status_ > PROFILING_ACTIVE &&
413 !parent_stack_.empty()) {
414 const Births* parent = parent_stack_.top();
415 ParentChildPair pair(parent, child);
416 if (parent_child_set_.find(pair) == parent_child_set_.end()) {
417 // Lock since the map may get relocated now, and other threads sometimes
418 // snapshot it (but they lock before copying it).
419 base::AutoLock lock(map_lock_);
420 parent_child_set_.insert(pair);
424 return child;
427 void ThreadData::TallyADeath(const Births& birth,
428 int32 queue_duration,
429 const TaskStopwatch& stopwatch) {
430 int32 run_duration = stopwatch.RunDurationMs();
432 // Stir in some randomness, plus add constant in case durations are zero.
433 const uint32 kSomePrimeNumber = 2147483647;
434 random_number_ += queue_duration + run_duration + kSomePrimeNumber;
435 // An address is going to have some randomness to it as well ;-).
436 random_number_ ^= static_cast<uint32>(&birth - reinterpret_cast<Births*>(0));
438 // We don't have queue durations without OS timer. OS timer is automatically
439 // used for task-post-timing, so the use of an alternate timer implies all
440 // queue times are invalid, unless it was explicitly said that we can trust
441 // the alternate timer.
442 if (kAllowAlternateTimeSourceHandling &&
443 now_function_ &&
444 !now_function_is_time_) {
445 queue_duration = 0;
448 DeathMap::iterator it = death_map_.find(&birth);
449 DeathData* death_data;
450 if (it != death_map_.end()) {
451 death_data = &it->second;
452 } else {
453 base::AutoLock lock(map_lock_); // Lock as the map may get relocated now.
454 death_data = &death_map_[&birth];
455 } // Release lock ASAP.
456 death_data->RecordDeath(queue_duration, run_duration, random_number_);
458 if (!kTrackParentChildLinks)
459 return;
460 if (!parent_stack_.empty()) { // We might get turned off.
461 DCHECK_EQ(parent_stack_.top(), &birth);
462 parent_stack_.pop();
466 // static
467 Births* ThreadData::TallyABirthIfActive(const Location& location) {
468 if (!kTrackAllTaskObjects)
469 return NULL; // Not compiled in.
471 if (!TrackingStatus())
472 return NULL;
473 ThreadData* current_thread_data = Get();
474 if (!current_thread_data)
475 return NULL;
476 return current_thread_data->TallyABirth(location);
479 // static
480 void ThreadData::TallyRunOnNamedThreadIfTracking(
481 const base::TrackingInfo& completed_task,
482 const TaskStopwatch& stopwatch) {
483 if (!kTrackAllTaskObjects)
484 return; // Not compiled in.
486 // Even if we have been DEACTIVATED, we will process any pending births so
487 // that our data structures (which counted the outstanding births) remain
488 // consistent.
489 const Births* birth = completed_task.birth_tally;
490 if (!birth)
491 return;
492 ThreadData* current_thread_data = stopwatch.GetThreadData();
493 if (!current_thread_data)
494 return;
496 // Watch out for a race where status_ is changing, and hence one or both
497 // of start_of_run or end_of_run is zero. In that case, we didn't bother to
498 // get a time value since we "weren't tracking" and we were trying to be
499 // efficient by not calling for a genuine time value. For simplicity, we'll
500 // use a default zero duration when we can't calculate a true value.
501 TrackedTime start_of_run = stopwatch.StartTime();
502 int32 queue_duration = 0;
503 if (!start_of_run.is_null()) {
504 queue_duration = (start_of_run - completed_task.EffectiveTimePosted())
505 .InMilliseconds();
507 current_thread_data->TallyADeath(*birth, queue_duration, stopwatch);
510 // static
511 void ThreadData::TallyRunOnWorkerThreadIfTracking(
512 const Births* birth,
513 const TrackedTime& time_posted,
514 const TaskStopwatch& stopwatch) {
515 if (!kTrackAllTaskObjects)
516 return; // Not compiled in.
518 // Even if we have been DEACTIVATED, we will process any pending births so
519 // that our data structures (which counted the outstanding births) remain
520 // consistent.
521 if (!birth)
522 return;
524 // TODO(jar): Support the option to coalesce all worker-thread activity under
525 // one ThreadData instance that uses locks to protect *all* access. This will
526 // reduce memory (making it provably bounded), but run incrementally slower
527 // (since we'll use locks on TallyABirth and TallyADeath). The good news is
528 // that the locks on TallyADeath will be *after* the worker thread has run,
529 // and hence nothing will be waiting for the completion (... besides some
530 // other thread that might like to run). Also, the worker threads tasks are
531 // generally longer, and hence the cost of the lock may perchance be amortized
532 // over the long task's lifetime.
533 ThreadData* current_thread_data = stopwatch.GetThreadData();
534 if (!current_thread_data)
535 return;
537 TrackedTime start_of_run = stopwatch.StartTime();
538 int32 queue_duration = 0;
539 if (!start_of_run.is_null()) {
540 queue_duration = (start_of_run - time_posted).InMilliseconds();
542 current_thread_data->TallyADeath(*birth, queue_duration, stopwatch);
545 // static
546 void ThreadData::TallyRunInAScopedRegionIfTracking(
547 const Births* birth,
548 const TaskStopwatch& stopwatch) {
549 if (!kTrackAllTaskObjects)
550 return; // Not compiled in.
552 // Even if we have been DEACTIVATED, we will process any pending births so
553 // that our data structures (which counted the outstanding births) remain
554 // consistent.
555 if (!birth)
556 return;
558 ThreadData* current_thread_data = stopwatch.GetThreadData();
559 if (!current_thread_data)
560 return;
562 int32 queue_duration = 0;
563 current_thread_data->TallyADeath(*birth, queue_duration, stopwatch);
566 // static
567 void ThreadData::SnapshotAllExecutedTasks(
568 ProcessDataPhaseSnapshot* process_data_phase,
569 BirthCountMap* birth_counts) {
570 if (!kTrackAllTaskObjects)
571 return; // Not compiled in.
573 // Get an unchanging copy of a ThreadData list.
574 ThreadData* my_list = ThreadData::first();
576 // Gather data serially.
577 // This hackish approach *can* get some slighly corrupt tallies, as we are
578 // grabbing values without the protection of a lock, but it has the advantage
579 // of working even with threads that don't have message loops. If a user
580 // sees any strangeness, they can always just run their stats gathering a
581 // second time.
582 for (ThreadData* thread_data = my_list;
583 thread_data;
584 thread_data = thread_data->next()) {
585 thread_data->SnapshotExecutedTasks(process_data_phase, birth_counts);
589 // static
590 void ThreadData::SnapshotCurrentPhase(
591 ProcessDataPhaseSnapshot* process_data_phase) {
592 // Add births that have run to completion to |collected_data|.
593 // |birth_counts| tracks the total number of births recorded at each location
594 // for which we have not seen a death count.
595 BirthCountMap birth_counts;
596 ThreadData::SnapshotAllExecutedTasks(process_data_phase, &birth_counts);
598 // Add births that are still active -- i.e. objects that have tallied a birth,
599 // but have not yet tallied a matching death, and hence must be either
600 // running, queued up, or being held in limbo for future posting.
601 for (const auto& birth_count : birth_counts) {
602 if (birth_count.second > 0) {
603 process_data_phase->tasks.push_back(TaskSnapshot(
604 *birth_count.first, DeathData(birth_count.second), "Still_Alive"));
609 void ThreadData::SnapshotExecutedTasks(
610 ProcessDataPhaseSnapshot* process_data_phase,
611 BirthCountMap* birth_counts) {
612 // Get copy of data, so that the data will not change during the iterations
613 // and processing.
614 ThreadData::BirthMap birth_map;
615 ThreadData::DeathMap death_map;
616 ThreadData::ParentChildSet parent_child_set;
617 SnapshotMaps(&birth_map, &death_map, &parent_child_set);
619 for (const auto& death : death_map) {
620 process_data_phase->tasks.push_back(
621 TaskSnapshot(*death.first, death.second, thread_name()));
622 (*birth_counts)[death.first] -= death.first->birth_count();
625 for (const auto& birth : birth_map) {
626 (*birth_counts)[birth.second] += birth.second->birth_count();
629 if (!kTrackParentChildLinks)
630 return;
632 for (const auto& parent_child : parent_child_set) {
633 process_data_phase->descendants.push_back(
634 ParentChildPairSnapshot(parent_child));
638 // This may be called from another thread.
639 void ThreadData::SnapshotMaps(BirthMap* birth_map,
640 DeathMap* death_map,
641 ParentChildSet* parent_child_set) {
642 base::AutoLock lock(map_lock_);
643 for (const auto& birth : birth_map_)
644 (*birth_map)[birth.first] = birth.second;
645 for (const auto& death : death_map_)
646 (*death_map)[death.first] = death.second;
648 if (!kTrackParentChildLinks)
649 return;
651 for (const auto& parent_child : parent_child_set_)
652 parent_child_set->insert(parent_child);
655 static void OptionallyInitializeAlternateTimer() {
656 NowFunction* alternate_time_source = GetAlternateTimeSource();
657 if (alternate_time_source)
658 ThreadData::SetAlternateTimeSource(alternate_time_source);
661 bool ThreadData::Initialize() {
662 if (!kTrackAllTaskObjects)
663 return false; // Not compiled in.
664 if (status_ >= DEACTIVATED)
665 return true; // Someone else did the initialization.
666 // Due to racy lazy initialization in tests, we'll need to recheck status_
667 // after we acquire the lock.
669 // Ensure that we don't double initialize tls. We are called when single
670 // threaded in the product, but some tests may be racy and lazy about our
671 // initialization.
672 base::AutoLock lock(*list_lock_.Pointer());
673 if (status_ >= DEACTIVATED)
674 return true; // Someone raced in here and beat us.
676 // Put an alternate timer in place if the environment calls for it, such as
677 // for tracking TCMalloc allocations. This insertion is idempotent, so we
678 // don't mind if there is a race, and we'd prefer not to be in a lock while
679 // doing this work.
680 if (kAllowAlternateTimeSourceHandling)
681 OptionallyInitializeAlternateTimer();
683 // Perform the "real" TLS initialization now, and leave it intact through
684 // process termination.
685 if (!tls_index_.initialized()) { // Testing may have initialized this.
686 DCHECK_EQ(status_, UNINITIALIZED);
687 tls_index_.Initialize(&ThreadData::OnThreadTermination);
688 if (!tls_index_.initialized())
689 return false;
690 } else {
691 // TLS was initialzed for us earlier.
692 DCHECK_EQ(status_, DORMANT_DURING_TESTS);
695 // Incarnation counter is only significant to testing, as it otherwise will
696 // never again change in this process.
697 ++incarnation_counter_;
699 // The lock is not critical for setting status_, but it doesn't hurt. It also
700 // ensures that if we have a racy initialization, that we'll bail as soon as
701 // we get the lock earlier in this method.
702 status_ = kInitialStartupState;
703 if (!kTrackParentChildLinks &&
704 kInitialStartupState == PROFILING_CHILDREN_ACTIVE)
705 status_ = PROFILING_ACTIVE;
706 DCHECK(status_ != UNINITIALIZED);
707 return true;
710 // static
711 bool ThreadData::InitializeAndSetTrackingStatus(Status status) {
712 DCHECK_GE(status, DEACTIVATED);
713 DCHECK_LE(status, PROFILING_CHILDREN_ACTIVE);
715 if (!Initialize()) // No-op if already initialized.
716 return false; // Not compiled in.
718 if (!kTrackParentChildLinks && status > DEACTIVATED)
719 status = PROFILING_ACTIVE;
720 status_ = status;
721 return true;
724 // static
725 ThreadData::Status ThreadData::status() {
726 return status_;
729 // static
730 bool ThreadData::TrackingStatus() {
731 return status_ > DEACTIVATED;
734 // static
735 bool ThreadData::TrackingParentChildStatus() {
736 return status_ >= PROFILING_CHILDREN_ACTIVE;
739 // static
740 void ThreadData::PrepareForStartOfRun(const Births* parent) {
741 if (kTrackParentChildLinks && parent && status_ > PROFILING_ACTIVE) {
742 ThreadData* current_thread_data = Get();
743 if (current_thread_data)
744 current_thread_data->parent_stack_.push(parent);
748 // static
749 void ThreadData::SetAlternateTimeSource(NowFunction* now_function) {
750 DCHECK(now_function);
751 if (kAllowAlternateTimeSourceHandling)
752 now_function_ = now_function;
755 // static
756 void ThreadData::EnableProfilerTiming() {
757 base::subtle::NoBarrier_Store(&g_profiler_timing_enabled, ENABLED_TIMING);
760 // static
761 TrackedTime ThreadData::Now() {
762 if (kAllowAlternateTimeSourceHandling && now_function_)
763 return TrackedTime::FromMilliseconds((*now_function_)());
764 if (kTrackAllTaskObjects && IsProfilerTimingEnabled() && TrackingStatus())
765 return TrackedTime::Now();
766 return TrackedTime(); // Super fast when disabled, or not compiled.
769 // static
770 void ThreadData::EnsureCleanupWasCalled(int major_threads_shutdown_count) {
771 base::AutoLock lock(*list_lock_.Pointer());
772 if (worker_thread_data_creation_count_ == 0)
773 return; // We haven't really run much, and couldn't have leaked.
775 // TODO(jar): until this is working on XP, don't run the real test.
776 #if 0
777 // Verify that we've at least shutdown/cleanup the major namesd threads. The
778 // caller should tell us how many thread shutdowns should have taken place by
779 // now.
780 CHECK_GT(cleanup_count_, major_threads_shutdown_count);
781 #endif
784 // static
785 void ThreadData::ShutdownSingleThreadedCleanup(bool leak) {
786 // This is only called from test code, where we need to cleanup so that
787 // additional tests can be run.
788 // We must be single threaded... but be careful anyway.
789 if (!InitializeAndSetTrackingStatus(DEACTIVATED))
790 return;
791 ThreadData* thread_data_list;
793 base::AutoLock lock(*list_lock_.Pointer());
794 thread_data_list = all_thread_data_list_head_;
795 all_thread_data_list_head_ = NULL;
796 ++incarnation_counter_;
797 // To be clean, break apart the retired worker list (though we leak them).
798 while (first_retired_worker_) {
799 ThreadData* worker = first_retired_worker_;
800 CHECK_GT(worker->worker_thread_number_, 0);
801 first_retired_worker_ = worker->next_retired_worker_;
802 worker->next_retired_worker_ = NULL;
806 // Put most global static back in pristine shape.
807 worker_thread_data_creation_count_ = 0;
808 cleanup_count_ = 0;
809 tls_index_.Set(NULL);
810 status_ = DORMANT_DURING_TESTS; // Almost UNINITIALIZED.
812 // To avoid any chance of racing in unit tests, which is the only place we
813 // call this function, we may sometimes leak all the data structures we
814 // recovered, as they may still be in use on threads from prior tests!
815 if (leak) {
816 ThreadData* thread_data = thread_data_list;
817 while (thread_data) {
818 ANNOTATE_LEAKING_OBJECT_PTR(thread_data);
819 thread_data = thread_data->next();
821 return;
824 // When we want to cleanup (on a single thread), here is what we do.
826 // Do actual recursive delete in all ThreadData instances.
827 while (thread_data_list) {
828 ThreadData* next_thread_data = thread_data_list;
829 thread_data_list = thread_data_list->next();
831 for (BirthMap::iterator it = next_thread_data->birth_map_.begin();
832 next_thread_data->birth_map_.end() != it; ++it)
833 delete it->second; // Delete the Birth Records.
834 delete next_thread_data; // Includes all Death Records.
838 //------------------------------------------------------------------------------
839 TaskStopwatch::TaskStopwatch()
840 : wallclock_duration_ms_(0),
841 current_thread_data_(NULL),
842 excluded_duration_ms_(0),
843 parent_(NULL) {
844 #if DCHECK_IS_ON()
845 state_ = CREATED;
846 child_ = NULL;
847 #endif
850 TaskStopwatch::~TaskStopwatch() {
851 #if DCHECK_IS_ON()
852 DCHECK(state_ != RUNNING);
853 DCHECK(child_ == NULL);
854 #endif
857 void TaskStopwatch::Start() {
858 #if DCHECK_IS_ON()
859 DCHECK(state_ == CREATED);
860 state_ = RUNNING;
861 #endif
863 start_time_ = ThreadData::Now();
865 current_thread_data_ = ThreadData::Get();
866 if (!current_thread_data_)
867 return;
869 parent_ = current_thread_data_->current_stopwatch_;
870 #if DCHECK_IS_ON()
871 if (parent_) {
872 DCHECK(parent_->state_ == RUNNING);
873 DCHECK(parent_->child_ == NULL);
874 parent_->child_ = this;
876 #endif
877 current_thread_data_->current_stopwatch_ = this;
880 void TaskStopwatch::Stop() {
881 const TrackedTime end_time = ThreadData::Now();
882 #if DCHECK_IS_ON()
883 DCHECK(state_ == RUNNING);
884 state_ = STOPPED;
885 DCHECK(child_ == NULL);
886 #endif
888 if (!start_time_.is_null() && !end_time.is_null()) {
889 wallclock_duration_ms_ = (end_time - start_time_).InMilliseconds();
892 if (!current_thread_data_)
893 return;
895 DCHECK(current_thread_data_->current_stopwatch_ == this);
896 current_thread_data_->current_stopwatch_ = parent_;
897 if (!parent_)
898 return;
900 #if DCHECK_IS_ON()
901 DCHECK(parent_->state_ == RUNNING);
902 DCHECK(parent_->child_ == this);
903 parent_->child_ = NULL;
904 #endif
905 parent_->excluded_duration_ms_ += wallclock_duration_ms_;
906 parent_ = NULL;
909 TrackedTime TaskStopwatch::StartTime() const {
910 #if DCHECK_IS_ON()
911 DCHECK(state_ != CREATED);
912 #endif
914 return start_time_;
917 int32 TaskStopwatch::RunDurationMs() const {
918 #if DCHECK_IS_ON()
919 DCHECK(state_ == STOPPED);
920 #endif
922 return wallclock_duration_ms_ - excluded_duration_ms_;
925 ThreadData* TaskStopwatch::GetThreadData() const {
926 #if DCHECK_IS_ON()
927 DCHECK(state_ != CREATED);
928 #endif
930 return current_thread_data_;
933 //------------------------------------------------------------------------------
934 TaskSnapshot::TaskSnapshot() {
937 TaskSnapshot::TaskSnapshot(const BirthOnThread& birth,
938 const DeathData& death_data,
939 const std::string& death_thread_name)
940 : birth(birth),
941 death_data(death_data),
942 death_thread_name(death_thread_name) {
945 TaskSnapshot::~TaskSnapshot() {
948 //------------------------------------------------------------------------------
949 // ParentChildPairSnapshot
951 ParentChildPairSnapshot::ParentChildPairSnapshot() {
954 ParentChildPairSnapshot::ParentChildPairSnapshot(
955 const ThreadData::ParentChildPair& parent_child)
956 : parent(*parent_child.first),
957 child(*parent_child.second) {
960 ParentChildPairSnapshot::~ParentChildPairSnapshot() {
963 //------------------------------------------------------------------------------
964 // ProcessDataPhaseSnapshot
966 ProcessDataPhaseSnapshot::ProcessDataPhaseSnapshot() {
969 ProcessDataPhaseSnapshot::~ProcessDataPhaseSnapshot() {
972 //------------------------------------------------------------------------------
973 // ProcessDataPhaseSnapshot
975 ProcessDataSnapshot::ProcessDataSnapshot()
976 #if !defined(OS_NACL)
977 : process_id(base::GetCurrentProcId()) {
978 #else
979 : process_id(base::kNullProcessId) {
980 #endif
983 ProcessDataSnapshot::~ProcessDataSnapshot() {
986 } // namespace tracked_objects