Optimize implementation of "replace substrings after offset" utility function to
[chromium-blink-merge.git] / base / tracked_objects.cc
blobfc29e2e61d780da7d7537f71e9de4aadcde971cf
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::ResetMax() {
167 run_duration_max_ = 0;
168 queue_duration_max_ = 0;
171 void DeathData::Clear() {
172 count_ = 0;
173 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;
181 //------------------------------------------------------------------------------
182 DeathDataSnapshot::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) {
192 DeathDataSnapshot::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()) {
203 DeathDataSnapshot::~DeathDataSnapshot() {
206 //------------------------------------------------------------------------------
207 BirthOnThread::BirthOnThread(const Location& location,
208 const ThreadData& current)
209 : location_(location),
210 birth_thread_(&current) {
213 //------------------------------------------------------------------------------
214 BirthOnThreadSnapshot::BirthOnThreadSnapshot() {
217 BirthOnThreadSnapshot::BirthOnThreadSnapshot(
218 const tracked_objects::BirthOnThread& birth)
219 : location(birth.location()),
220 thread_name(birth.birth_thread()->thread_name()) {
223 BirthOnThreadSnapshot::~BirthOnThreadSnapshot() {
226 //------------------------------------------------------------------------------
227 Births::Births(const Location& location, const ThreadData& current)
228 : BirthOnThread(location, current),
229 birth_count_(1) { }
231 int Births::birth_count() const { return birth_count_; }
233 void Births::RecordBirth() { ++birth_count_; }
235 void Births::ForgetBirth() { --birth_count_; }
237 void Births::Clear() { birth_count_ = 0; }
239 //------------------------------------------------------------------------------
240 // ThreadData maintains the central data for all births and deaths on a single
241 // thread.
243 // 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.
247 // static
248 NowFunction* ThreadData::now_function_ = NULL;
250 // static
251 bool ThreadData::now_function_is_time_ = false;
253 // 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
257 base::ThreadLocalStorage::StaticSlot ThreadData::tls_index_ = TLS_INITIALIZER;
259 // static
260 int ThreadData::worker_thread_data_creation_count_ = 0;
262 // static
263 int ThreadData::cleanup_count_ = 0;
265 // static
266 int ThreadData::incarnation_counter_ = 0;
268 // static
269 ThreadData* ThreadData::all_thread_data_list_head_ = NULL;
271 // static
272 ThreadData* ThreadData::first_retired_worker_ = NULL;
274 // static
275 base::LazyInstance<base::Lock>::Leaky
276 ThreadData::list_lock_ = LAZY_INSTANCE_INITIALIZER;
278 // static
279 ThreadData::Status ThreadData::status_ = ThreadData::UNINITIALIZED;
281 ThreadData::ThreadData(const std::string& suggested_name)
282 : next_(NULL),
283 next_retired_worker_(NULL),
284 worker_thread_number_(0),
285 incarnation_count_for_pool_(-1),
286 current_stopwatch_(NULL) {
287 DCHECK_GE(suggested_name.size(), 0u);
288 thread_name_ = suggested_name;
289 PushToHeadOfList(); // Which sets real incarnation_count_for_pool_.
292 ThreadData::ThreadData(int thread_number)
293 : next_(NULL),
294 next_retired_worker_(NULL),
295 worker_thread_number_(thread_number),
296 incarnation_count_for_pool_(-1),
297 current_stopwatch_(NULL) {
298 CHECK_GT(thread_number, 0);
299 base::StringAppendF(&thread_name_, "WorkerThread-%d", thread_number);
300 PushToHeadOfList(); // Which sets real incarnation_count_for_pool_.
303 ThreadData::~ThreadData() {}
305 void ThreadData::PushToHeadOfList() {
306 // Toss in a hint of randomness (atop the uniniitalized value).
307 (void)VALGRIND_MAKE_MEM_DEFINED_IF_ADDRESSABLE(&random_number_,
308 sizeof(random_number_));
309 MSAN_UNPOISON(&random_number_, sizeof(random_number_));
310 random_number_ += static_cast<uint32>(this - static_cast<ThreadData*>(0));
311 random_number_ ^= (Now() - TrackedTime()).InMilliseconds();
313 DCHECK(!next_);
314 base::AutoLock lock(*list_lock_.Pointer());
315 incarnation_count_for_pool_ = incarnation_counter_;
316 next_ = all_thread_data_list_head_;
317 all_thread_data_list_head_ = this;
320 // static
321 ThreadData* ThreadData::first() {
322 base::AutoLock lock(*list_lock_.Pointer());
323 return all_thread_data_list_head_;
326 ThreadData* ThreadData::next() const { return next_; }
328 // static
329 void ThreadData::InitializeThreadContext(const std::string& suggested_name) {
330 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);
337 tls_index_.Set(current_thread_data);
340 // static
341 ThreadData* ThreadData::Get() {
342 if (!tls_index_.initialized())
343 return NULL; // For unittests only.
344 ThreadData* registered = reinterpret_cast<ThreadData*>(tls_index_.Get());
345 if (registered)
346 return registered;
348 // We must be a worker thread, since we didn't pre-register.
349 ThreadData* worker_thread_data = NULL;
350 int worker_thread_number = 0;
352 base::AutoLock lock(*list_lock_.Pointer());
353 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;
357 } else {
358 worker_thread_number = ++worker_thread_data_creation_count_;
362 // If we can't find a previously used instance, then we have to create one.
363 if (!worker_thread_data) {
364 DCHECK_GT(worker_thread_number, 0);
365 worker_thread_data = new ThreadData(worker_thread_number);
367 DCHECK_GT(worker_thread_data->worker_thread_number_, 0);
369 tls_index_.Set(worker_thread_data);
370 return worker_thread_data;
373 // static
374 void ThreadData::OnThreadTermination(void* thread_data) {
375 DCHECK(thread_data); // TLS should *never* call us with a NULL.
376 // We must NOT do any allocations during this callback. There is a chance
377 // that the allocator is no longer active on this thread.
378 if (!kTrackAllTaskObjects)
379 return; // Not compiled in.
380 reinterpret_cast<ThreadData*>(thread_data)->OnThreadTerminationCleanup();
383 void ThreadData::OnThreadTerminationCleanup() {
384 // The list_lock_ was created when we registered the callback, so it won't be
385 // allocated here despite the lazy reference.
386 base::AutoLock lock(*list_lock_.Pointer());
387 if (incarnation_counter_ != incarnation_count_for_pool_)
388 return; // ThreadData was constructed in an earlier unit test.
389 ++cleanup_count_;
390 // Only worker threads need to be retired and reused.
391 if (!worker_thread_number_) {
392 return;
394 // 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;
401 // static
402 void 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);
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"));
421 Births* ThreadData::TallyABirth(const Location& location) {
422 BirthMap::iterator it = birth_map_.find(location);
423 Births* child;
424 if (it != birth_map_.end()) {
425 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;
435 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);
447 return child;
450 void ThreadData::TallyADeath(const Births& birth,
451 int32 queue_duration,
452 const TaskStopwatch& stopwatch) {
453 int32 run_duration = stopwatch.RunDurationMs();
455 // Stir in some randomness, plus add constant in case durations are zero.
456 const uint32 kSomePrimeNumber = 2147483647;
457 random_number_ += queue_duration + run_duration + kSomePrimeNumber;
458 // An address is going to have some randomness to it as well ;-).
459 random_number_ ^= static_cast<uint32>(&birth - reinterpret_cast<Births*>(0));
461 // 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
463 // 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_) {
468 queue_duration = 0;
471 DeathMap::iterator it = death_map_.find(&birth);
472 DeathData* death_data;
473 if (it != death_map_.end()) {
474 death_data = &it->second;
475 } else {
476 base::AutoLock lock(map_lock_); // Lock as the map may get relocated now.
477 death_data = &death_map_[&birth];
478 } // Release lock ASAP.
479 death_data->RecordDeath(queue_duration, run_duration, random_number_);
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();
489 // static
490 Births* ThreadData::TallyABirthIfActive(const Location& location) {
491 if (!kTrackAllTaskObjects)
492 return NULL; // Not compiled in.
494 if (!TrackingStatus())
495 return NULL;
496 ThreadData* current_thread_data = Get();
497 if (!current_thread_data)
498 return NULL;
499 return current_thread_data->TallyABirth(location);
502 // static
503 void ThreadData::TallyRunOnNamedThreadIfTracking(
504 const base::TrackingInfo& completed_task,
505 const TaskStopwatch& stopwatch) {
506 if (!kTrackAllTaskObjects)
507 return; // Not compiled in.
509 // 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)
514 return;
515 ThreadData* current_thread_data = stopwatch.GetThreadData();
516 if (!current_thread_data)
517 return;
519 // Watch out for a race where status_ is changing, and hence one or both
520 // of start_of_run or end_of_run is zero. In that case, we didn't bother to
521 // 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.
524 TrackedTime start_of_run = stopwatch.StartTime();
525 int32 queue_duration = 0;
526 if (!start_of_run.is_null()) {
527 queue_duration = (start_of_run - completed_task.EffectiveTimePosted())
528 .InMilliseconds();
530 current_thread_data->TallyADeath(*birth, queue_duration, stopwatch);
533 // static
534 void ThreadData::TallyRunOnWorkerThreadIfTracking(
535 const Births* birth,
536 const TrackedTime& time_posted,
537 const TaskStopwatch& stopwatch) {
538 if (!kTrackAllTaskObjects)
539 return; // Not compiled in.
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;
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
550 // (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
554 // generally longer, and hence the cost of the lock may perchance be amortized
555 // over the long task's lifetime.
556 ThreadData* current_thread_data = stopwatch.GetThreadData();
557 if (!current_thread_data)
558 return;
560 TrackedTime start_of_run = stopwatch.StartTime();
561 int32 queue_duration = 0;
562 if (!start_of_run.is_null()) {
563 queue_duration = (start_of_run - time_posted).InMilliseconds();
565 current_thread_data->TallyADeath(*birth, queue_duration, stopwatch);
568 // static
569 void ThreadData::TallyRunInAScopedRegionIfTracking(
570 const Births* birth,
571 const TaskStopwatch& stopwatch) {
572 if (!kTrackAllTaskObjects)
573 return; // Not compiled in.
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;
581 ThreadData* current_thread_data = stopwatch.GetThreadData();
582 if (!current_thread_data)
583 return;
585 int32 queue_duration = 0;
586 current_thread_data->TallyADeath(*birth, queue_duration, stopwatch);
589 // static
590 void ThreadData::SnapshotAllExecutedTasks(bool reset_max,
591 ProcessDataSnapshot* process_data,
592 BirthCountMap* birth_counts) {
593 if (!kTrackAllTaskObjects)
594 return; // Not compiled in.
596 // Get an unchanging copy of a ThreadData list.
597 ThreadData* my_list = ThreadData::first();
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);
612 void 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);
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();
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();
634 if (!kTrackParentChildLinks)
635 return;
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));
643 // This may be called from another thread.
644 void ThreadData::SnapshotMaps(bool reset_max,
645 BirthMap* birth_map,
646 DeathMap* death_map,
647 ParentChildSet* parent_child_set) {
648 base::AutoLock lock(map_lock_);
649 for (BirthMap::const_iterator it = birth_map_.begin();
650 it != birth_map_.end(); ++it)
651 (*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();
659 if (!kTrackParentChildLinks)
660 return;
662 for (ParentChildSet::iterator it = parent_child_set_.begin();
663 it != parent_child_set_.end(); ++it)
664 parent_child_set->insert(*it);
667 // static
668 void ThreadData::ResetAllThreadData() {
669 ThreadData* my_list = first();
671 for (ThreadData* thread_data = my_list;
672 thread_data;
673 thread_data = thread_data->next())
674 thread_data->Reset();
677 void ThreadData::Reset() {
678 base::AutoLock lock(map_lock_);
679 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();
687 static void OptionallyInitializeAlternateTimer() {
688 NowFunction* alternate_time_source = GetAlternateTimeSource();
689 if (alternate_time_source)
690 ThreadData::SetAlternateTimeSource(alternate_time_source);
693 bool ThreadData::Initialize() {
694 if (!kTrackAllTaskObjects)
695 return false; // Not compiled in.
696 if (status_ >= DEACTIVATED)
697 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.
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());
705 if (status_ >= DEACTIVATED)
706 return true; // Someone raced in here and beat us.
708 // 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();
715 // Perform the "real" TLS initialization now, and leave it intact through
716 // process termination.
717 if (!tls_index_.initialized()) { // Testing may have initialized this.
718 DCHECK_EQ(status_, UNINITIALIZED);
719 tls_index_.Initialize(&ThreadData::OnThreadTermination);
720 if (!tls_index_.initialized())
721 return false;
722 } else {
723 // TLS was initialzed for us earlier.
724 DCHECK_EQ(status_, DORMANT_DURING_TESTS);
727 // Incarnation counter is only significant to testing, as it otherwise will
728 // never again change in this process.
729 ++incarnation_counter_;
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;
735 if (!kTrackParentChildLinks &&
736 kInitialStartupState == PROFILING_CHILDREN_ACTIVE)
737 status_ = PROFILING_ACTIVE;
738 DCHECK(status_ != UNINITIALIZED);
739 return true;
742 // static
743 bool ThreadData::InitializeAndSetTrackingStatus(Status status) {
744 DCHECK_GE(status, DEACTIVATED);
745 DCHECK_LE(status, PROFILING_CHILDREN_ACTIVE);
747 if (!Initialize()) // No-op if already initialized.
748 return false; // Not compiled in.
750 if (!kTrackParentChildLinks && status > DEACTIVATED)
751 status = PROFILING_ACTIVE;
752 status_ = status;
753 return true;
756 // static
757 ThreadData::Status ThreadData::status() {
758 return status_;
761 // static
762 bool ThreadData::TrackingStatus() {
763 return status_ > DEACTIVATED;
766 // static
767 bool ThreadData::TrackingParentChildStatus() {
768 return status_ >= PROFILING_CHILDREN_ACTIVE;
771 // static
772 void ThreadData::PrepareForStartOfRun(const Births* parent) {
773 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);
780 // static
781 void ThreadData::SetAlternateTimeSource(NowFunction* now_function) {
782 DCHECK(now_function);
783 if (kAllowAlternateTimeSourceHandling)
784 now_function_ = now_function;
787 // static
788 void ThreadData::EnableProfilerTiming() {
789 base::subtle::NoBarrier_Store(&g_profiler_timing_enabled, ENABLED_TIMING);
792 // static
793 TrackedTime ThreadData::Now() {
794 if (kAllowAlternateTimeSourceHandling && now_function_)
795 return TrackedTime::FromMilliseconds((*now_function_)());
796 if (kTrackAllTaskObjects && IsProfilerTimingEnabled() && TrackingStatus())
797 return TrackedTime::Now();
798 return TrackedTime(); // Super fast when disabled, or not compiled.
801 // static
802 void 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.
807 // TODO(jar): until this is working on XP, don't run the real test.
808 #if 0
809 // 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.
812 CHECK_GT(cleanup_count_, major_threads_shutdown_count);
813 #endif
816 // static
817 void ThreadData::ShutdownSingleThreadedCleanup(bool leak) {
818 // This is only called from test code, where we need to cleanup so that
819 // additional tests can be run.
820 // We must be single threaded... but be careful anyway.
821 if (!InitializeAndSetTrackingStatus(DEACTIVATED))
822 return;
823 ThreadData* thread_data_list;
825 base::AutoLock lock(*list_lock_.Pointer());
826 thread_data_list = all_thread_data_list_head_;
827 all_thread_data_list_head_ = NULL;
828 ++incarnation_counter_;
829 // To be clean, break apart the retired worker list (though we leak them).
830 while (first_retired_worker_) {
831 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;
838 // Put most global static back in pristine shape.
839 worker_thread_data_creation_count_ = 0;
840 cleanup_count_ = 0;
841 tls_index_.Set(NULL);
842 status_ = DORMANT_DURING_TESTS; // Almost UNINITIALIZED.
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!
847 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();
853 return;
856 // When we want to cleanup (on a single thread), here is what we do.
858 // Do actual recursive delete in all ThreadData instances.
859 while (thread_data_list) {
860 ThreadData* next_thread_data = thread_data_list;
861 thread_data_list = thread_data_list->next();
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.
866 delete next_thread_data; // Includes all Death Records.
870 //------------------------------------------------------------------------------
871 TaskStopwatch::TaskStopwatch()
872 : wallclock_duration_ms_(0),
873 current_thread_data_(NULL),
874 excluded_duration_ms_(0),
875 parent_(NULL) {
876 #if DCHECK_IS_ON()
877 state_ = CREATED;
878 child_ = NULL;
879 #endif
882 TaskStopwatch::~TaskStopwatch() {
883 #if DCHECK_IS_ON()
884 DCHECK(state_ != RUNNING);
885 DCHECK(child_ == NULL);
886 #endif
889 void TaskStopwatch::Start() {
890 #if DCHECK_IS_ON()
891 DCHECK(state_ == CREATED);
892 state_ = RUNNING;
893 #endif
895 start_time_ = ThreadData::Now();
897 current_thread_data_ = ThreadData::Get();
898 if (!current_thread_data_)
899 return;
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;
908 #endif
909 current_thread_data_->current_stopwatch_ = this;
912 void 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
920 if (!start_time_.is_null() && !end_time.is_null()) {
921 wallclock_duration_ms_ = (end_time - start_time_).InMilliseconds();
924 if (!current_thread_data_)
925 return;
927 DCHECK(current_thread_data_->current_stopwatch_ == this);
928 current_thread_data_->current_stopwatch_ = parent_;
929 if (!parent_)
930 return;
932 #if DCHECK_IS_ON()
933 DCHECK(parent_->state_ == RUNNING);
934 DCHECK(parent_->child_ == this);
935 parent_->child_ = NULL;
936 #endif
937 parent_->excluded_duration_ms_ += wallclock_duration_ms_;
938 parent_ = NULL;
941 TrackedTime TaskStopwatch::StartTime() const {
942 #if DCHECK_IS_ON()
943 DCHECK(state_ != CREATED);
944 #endif
946 return start_time_;
949 int32 TaskStopwatch::RunDurationMs() const {
950 #if DCHECK_IS_ON()
951 DCHECK(state_ == STOPPED);
952 #endif
954 return wallclock_duration_ms_ - excluded_duration_ms_;
957 ThreadData* TaskStopwatch::GetThreadData() const {
958 #if DCHECK_IS_ON()
959 DCHECK(state_ != CREATED);
960 #endif
962 return current_thread_data_;
965 //------------------------------------------------------------------------------
966 TaskSnapshot::TaskSnapshot() {
969 TaskSnapshot::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) {
977 TaskSnapshot::~TaskSnapshot() {
980 //------------------------------------------------------------------------------
981 // ParentChildPairSnapshot
983 ParentChildPairSnapshot::ParentChildPairSnapshot() {
986 ParentChildPairSnapshot::ParentChildPairSnapshot(
987 const ThreadData::ParentChildPair& parent_child)
988 : parent(*parent_child.first),
989 child(*parent_child.second) {
992 ParentChildPairSnapshot::~ParentChildPairSnapshot() {
995 //------------------------------------------------------------------------------
996 // ProcessDataSnapshot
998 ProcessDataSnapshot::ProcessDataSnapshot()
999 #if !defined(OS_NACL)
1000 : process_id(base::GetCurrentProcId()) {
1001 #else
1002 : process_id(0) {
1003 #endif
1006 ProcessDataSnapshot::~ProcessDataSnapshot() {
1009 } // namespace tracked_objects