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"
10 #include "base/compiler_specific.h"
11 #include "base/debug/leak_annotations.h"
12 #include "base/format_macros.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/port.h"
15 #include "base/process_util.h"
16 #include "base/profiler/alternate_timer.h"
17 #include "base/strings/stringprintf.h"
18 #include "base/third_party/valgrind/memcheck.h"
19 #include "base/threading/thread_restrictions.h"
21 using base::TimeDelta
;
23 namespace tracked_objects
{
26 // Flag to compile out almost all of the task tracking code.
27 const bool kTrackAllTaskObjects
= true;
29 // TODO(jar): Evaluate the perf impact of enabling this. If the perf impact is
30 // negligible, enable by default.
31 // Flag to compile out parent-child link recording.
32 const bool kTrackParentChildLinks
= false;
34 // When ThreadData is first initialized, should we start in an ACTIVE state to
35 // record all of the startup-time tasks, or should we start up DEACTIVATED, so
36 // that we only record after parsing the command line flag --enable-tracking.
37 // Note that the flag may force either state, so this really controls only the
38 // period of time up until that flag is parsed. If there is no flag seen, then
39 // this state may prevail for much or all of the process lifetime.
40 const ThreadData::Status kInitialStartupState
=
41 ThreadData::PROFILING_CHILDREN_ACTIVE
;
43 // Control whether an alternate time source (Now() function) is supported by
44 // the ThreadData class. This compile time flag should be set to true if we
45 // want other modules (such as a memory allocator, or a thread-specific CPU time
46 // clock) to be able to provide a thread-specific Now() function. Without this
47 // compile-time flag, the code will only support the wall-clock time. This flag
48 // can be flipped to efficiently disable this path (if there is a performance
49 // problem with its presence).
50 static const bool kAllowAlternateTimeSourceHandling
= true;
54 //------------------------------------------------------------------------------
55 // DeathData tallies durations when a death takes place.
57 DeathData::DeathData() {
61 DeathData::DeathData(int count
) {
66 // TODO(jar): I need to see if this macro to optimize branching is worth using.
68 // This macro has no branching, so it is surely fast, and is equivalent to:
71 // We use a macro rather than a template to force this to inline.
72 // Related code for calculating max is discussed on the web.
73 #define CONDITIONAL_ASSIGN(assign_it, target, source) \
74 ((target) ^= ((target) ^ (source)) & -static_cast<int32>(assign_it))
76 void DeathData::RecordDeath(const int32 queue_duration
,
77 const int32 run_duration
,
78 int32 random_number
) {
79 // We'll just clamp at INT_MAX, but we should note this in the UI as such.
82 queue_duration_sum_
+= queue_duration
;
83 run_duration_sum_
+= run_duration
;
85 if (queue_duration_max_
< queue_duration
)
86 queue_duration_max_
= queue_duration
;
87 if (run_duration_max_
< run_duration
)
88 run_duration_max_
= run_duration
;
90 // Take a uniformly distributed sample over all durations ever supplied.
91 // The probability that we (instead) use this new sample is 1/count_. This
92 // results in a completely uniform selection of the sample (at least when we
93 // don't clamp count_... but that should be inconsequentially likely).
94 // We ignore the fact that we correlated our selection of a sample to the run
95 // and queue times (i.e., we used them to generate random_number).
97 if (0 == (random_number
% count_
)) {
98 queue_duration_sample_
= queue_duration
;
99 run_duration_sample_
= run_duration
;
103 int DeathData::count() const { return count_
; }
105 int32
DeathData::run_duration_sum() const { return run_duration_sum_
; }
107 int32
DeathData::run_duration_max() const { return run_duration_max_
; }
109 int32
DeathData::run_duration_sample() const {
110 return run_duration_sample_
;
113 int32
DeathData::queue_duration_sum() const {
114 return queue_duration_sum_
;
117 int32
DeathData::queue_duration_max() const {
118 return queue_duration_max_
;
121 int32
DeathData::queue_duration_sample() const {
122 return queue_duration_sample_
;
125 void DeathData::ResetMax() {
126 run_duration_max_
= 0;
127 queue_duration_max_
= 0;
130 void DeathData::Clear() {
132 run_duration_sum_
= 0;
133 run_duration_max_
= 0;
134 run_duration_sample_
= 0;
135 queue_duration_sum_
= 0;
136 queue_duration_max_
= 0;
137 queue_duration_sample_
= 0;
140 //------------------------------------------------------------------------------
141 DeathDataSnapshot::DeathDataSnapshot()
143 run_duration_sum(-1),
144 run_duration_max(-1),
145 run_duration_sample(-1),
146 queue_duration_sum(-1),
147 queue_duration_max(-1),
148 queue_duration_sample(-1) {
151 DeathDataSnapshot::DeathDataSnapshot(
152 const tracked_objects::DeathData
& death_data
)
153 : count(death_data
.count()),
154 run_duration_sum(death_data
.run_duration_sum()),
155 run_duration_max(death_data
.run_duration_max()),
156 run_duration_sample(death_data
.run_duration_sample()),
157 queue_duration_sum(death_data
.queue_duration_sum()),
158 queue_duration_max(death_data
.queue_duration_max()),
159 queue_duration_sample(death_data
.queue_duration_sample()) {
162 DeathDataSnapshot::~DeathDataSnapshot() {
165 //------------------------------------------------------------------------------
166 BirthOnThread::BirthOnThread(const Location
& location
,
167 const ThreadData
& current
)
168 : location_(location
),
169 birth_thread_(¤t
) {
172 //------------------------------------------------------------------------------
173 BirthOnThreadSnapshot::BirthOnThreadSnapshot() {
176 BirthOnThreadSnapshot::BirthOnThreadSnapshot(
177 const tracked_objects::BirthOnThread
& birth
)
178 : location(birth
.location()),
179 thread_name(birth
.birth_thread()->thread_name()) {
182 BirthOnThreadSnapshot::~BirthOnThreadSnapshot() {
185 //------------------------------------------------------------------------------
186 Births::Births(const Location
& location
, const ThreadData
& current
)
187 : BirthOnThread(location
, current
),
190 int Births::birth_count() const { return birth_count_
; }
192 void Births::RecordBirth() { ++birth_count_
; }
194 void Births::ForgetBirth() { --birth_count_
; }
196 void Births::Clear() { birth_count_
= 0; }
198 //------------------------------------------------------------------------------
199 // ThreadData maintains the central data for all births and deaths on a single
202 // TODO(jar): We should pull all these static vars together, into a struct, and
203 // optimize layout so that we benefit from locality of reference during accesses
207 NowFunction
* ThreadData::now_function_
= NULL
;
209 // A TLS slot which points to the ThreadData instance for the current thread. We
210 // do a fake initialization here (zeroing out data), and then the real in-place
211 // construction happens when we call tls_index_.Initialize().
213 base::ThreadLocalStorage::StaticSlot
ThreadData::tls_index_
= TLS_INITIALIZER
;
216 int ThreadData::worker_thread_data_creation_count_
= 0;
219 int ThreadData::cleanup_count_
= 0;
222 int ThreadData::incarnation_counter_
= 0;
225 ThreadData
* ThreadData::all_thread_data_list_head_
= NULL
;
228 ThreadData
* ThreadData::first_retired_worker_
= NULL
;
231 base::LazyInstance
<base::Lock
>::Leaky
232 ThreadData::list_lock_
= LAZY_INSTANCE_INITIALIZER
;
235 ThreadData::Status
ThreadData::status_
= ThreadData::UNINITIALIZED
;
237 ThreadData::ThreadData(const std::string
& suggested_name
)
239 next_retired_worker_(NULL
),
240 worker_thread_number_(0),
241 incarnation_count_for_pool_(-1) {
242 DCHECK_GE(suggested_name
.size(), 0u);
243 thread_name_
= suggested_name
;
244 PushToHeadOfList(); // Which sets real incarnation_count_for_pool_.
247 ThreadData::ThreadData(int thread_number
)
249 next_retired_worker_(NULL
),
250 worker_thread_number_(thread_number
),
251 incarnation_count_for_pool_(-1) {
252 CHECK_GT(thread_number
, 0);
253 base::StringAppendF(&thread_name_
, "WorkerThread-%d", thread_number
);
254 PushToHeadOfList(); // Which sets real incarnation_count_for_pool_.
257 ThreadData::~ThreadData() {}
259 void ThreadData::PushToHeadOfList() {
260 // Toss in a hint of randomness (atop the uniniitalized value).
261 (void)VALGRIND_MAKE_MEM_DEFINED_IF_ADDRESSABLE(&random_number_
,
262 sizeof(random_number_
));
263 MSAN_UNPOISON(&random_number_
, sizeof(random_number_
));
264 random_number_
+= static_cast<int32
>(this - static_cast<ThreadData
*>(0));
265 random_number_
^= (Now() - TrackedTime()).InMilliseconds();
268 base::AutoLock
lock(*list_lock_
.Pointer());
269 incarnation_count_for_pool_
= incarnation_counter_
;
270 next_
= all_thread_data_list_head_
;
271 all_thread_data_list_head_
= this;
275 ThreadData
* ThreadData::first() {
276 base::AutoLock
lock(*list_lock_
.Pointer());
277 return all_thread_data_list_head_
;
280 ThreadData
* ThreadData::next() const { return next_
; }
283 void ThreadData::InitializeThreadContext(const std::string
& suggested_name
) {
284 if (!Initialize()) // Always initialize if needed.
286 ThreadData
* current_thread_data
=
287 reinterpret_cast<ThreadData
*>(tls_index_
.Get());
288 if (current_thread_data
)
289 return; // Browser tests instigate this.
290 current_thread_data
= new ThreadData(suggested_name
);
291 tls_index_
.Set(current_thread_data
);
295 ThreadData
* ThreadData::Get() {
296 if (!tls_index_
.initialized())
297 return NULL
; // For unittests only.
298 ThreadData
* registered
= reinterpret_cast<ThreadData
*>(tls_index_
.Get());
302 // We must be a worker thread, since we didn't pre-register.
303 ThreadData
* worker_thread_data
= NULL
;
304 int worker_thread_number
= 0;
306 base::AutoLock
lock(*list_lock_
.Pointer());
307 if (first_retired_worker_
) {
308 worker_thread_data
= first_retired_worker_
;
309 first_retired_worker_
= first_retired_worker_
->next_retired_worker_
;
310 worker_thread_data
->next_retired_worker_
= NULL
;
312 worker_thread_number
= ++worker_thread_data_creation_count_
;
316 // If we can't find a previously used instance, then we have to create one.
317 if (!worker_thread_data
) {
318 DCHECK_GT(worker_thread_number
, 0);
319 worker_thread_data
= new ThreadData(worker_thread_number
);
321 DCHECK_GT(worker_thread_data
->worker_thread_number_
, 0);
323 tls_index_
.Set(worker_thread_data
);
324 return worker_thread_data
;
328 void ThreadData::OnThreadTermination(void* thread_data
) {
329 DCHECK(thread_data
); // TLS should *never* call us with a NULL.
330 // We must NOT do any allocations during this callback. There is a chance
331 // that the allocator is no longer active on this thread.
332 if (!kTrackAllTaskObjects
)
333 return; // Not compiled in.
334 reinterpret_cast<ThreadData
*>(thread_data
)->OnThreadTerminationCleanup();
337 void ThreadData::OnThreadTerminationCleanup() {
338 // The list_lock_ was created when we registered the callback, so it won't be
339 // allocated here despite the lazy reference.
340 base::AutoLock
lock(*list_lock_
.Pointer());
341 if (incarnation_counter_
!= incarnation_count_for_pool_
)
342 return; // ThreadData was constructed in an earlier unit test.
344 // Only worker threads need to be retired and reused.
345 if (!worker_thread_number_
) {
348 // We must NOT do any allocations during this callback.
349 // Using the simple linked lists avoids all allocations.
350 DCHECK_EQ(this->next_retired_worker_
, reinterpret_cast<ThreadData
*>(NULL
));
351 this->next_retired_worker_
= first_retired_worker_
;
352 first_retired_worker_
= this;
356 void ThreadData::Snapshot(bool reset_max
, ProcessDataSnapshot
* process_data
) {
357 // Add births that have run to completion to |collected_data|.
358 // |birth_counts| tracks the total number of births recorded at each location
359 // for which we have not seen a death count.
360 BirthCountMap birth_counts
;
361 ThreadData::SnapshotAllExecutedTasks(reset_max
, process_data
, &birth_counts
);
363 // Add births that are still active -- i.e. objects that have tallied a birth,
364 // but have not yet tallied a matching death, and hence must be either
365 // running, queued up, or being held in limbo for future posting.
366 for (BirthCountMap::const_iterator it
= birth_counts
.begin();
367 it
!= birth_counts
.end(); ++it
) {
368 if (it
->second
> 0) {
369 process_data
->tasks
.push_back(
370 TaskSnapshot(*it
->first
, DeathData(it
->second
), "Still_Alive"));
375 Births
* ThreadData::TallyABirth(const Location
& location
) {
376 BirthMap::iterator it
= birth_map_
.find(location
);
378 if (it
!= birth_map_
.end()) {
380 child
->RecordBirth();
382 child
= new Births(location
, *this); // Leak this.
383 // Lock since the map may get relocated now, and other threads sometimes
384 // snapshot it (but they lock before copying it).
385 base::AutoLock
lock(map_lock_
);
386 birth_map_
[location
] = child
;
389 if (kTrackParentChildLinks
&& status_
> PROFILING_ACTIVE
&&
390 !parent_stack_
.empty()) {
391 const Births
* parent
= parent_stack_
.top();
392 ParentChildPair
pair(parent
, child
);
393 if (parent_child_set_
.find(pair
) == parent_child_set_
.end()) {
394 // Lock since the map may get relocated now, and other threads sometimes
395 // snapshot it (but they lock before copying it).
396 base::AutoLock
lock(map_lock_
);
397 parent_child_set_
.insert(pair
);
404 void ThreadData::TallyADeath(const Births
& birth
,
405 int32 queue_duration
,
406 int32 run_duration
) {
407 // Stir in some randomness, plus add constant in case durations are zero.
408 const int32 kSomePrimeNumber
= 2147483647;
409 random_number_
+= queue_duration
+ run_duration
+ kSomePrimeNumber
;
410 // An address is going to have some randomness to it as well ;-).
411 random_number_
^= static_cast<int32
>(&birth
- reinterpret_cast<Births
*>(0));
413 // We don't have queue durations without OS timer. OS timer is automatically
414 // used for task-post-timing, so the use of an alternate timer implies all
415 // queue times are invalid.
416 if (kAllowAlternateTimeSourceHandling
&& now_function_
)
419 DeathMap::iterator it
= death_map_
.find(&birth
);
420 DeathData
* death_data
;
421 if (it
!= death_map_
.end()) {
422 death_data
= &it
->second
;
424 base::AutoLock
lock(map_lock_
); // Lock as the map may get relocated now.
425 death_data
= &death_map_
[&birth
];
426 } // Release lock ASAP.
427 death_data
->RecordDeath(queue_duration
, run_duration
, random_number_
);
429 if (!kTrackParentChildLinks
)
431 if (!parent_stack_
.empty()) { // We might get turned off.
432 DCHECK_EQ(parent_stack_
.top(), &birth
);
438 Births
* ThreadData::TallyABirthIfActive(const Location
& location
) {
439 if (!kTrackAllTaskObjects
)
440 return NULL
; // Not compiled in.
442 if (!TrackingStatus())
444 ThreadData
* current_thread_data
= Get();
445 if (!current_thread_data
)
447 return current_thread_data
->TallyABirth(location
);
451 void ThreadData::TallyRunOnNamedThreadIfTracking(
452 const base::TrackingInfo
& completed_task
,
453 const TrackedTime
& start_of_run
,
454 const TrackedTime
& end_of_run
) {
455 if (!kTrackAllTaskObjects
)
456 return; // Not compiled in.
458 // Even if we have been DEACTIVATED, we will process any pending births so
459 // that our data structures (which counted the outstanding births) remain
461 const Births
* birth
= completed_task
.birth_tally
;
464 ThreadData
* current_thread_data
= Get();
465 if (!current_thread_data
)
468 // To avoid conflating our stats with the delay duration in a PostDelayedTask,
469 // we identify such tasks, and replace their post_time with the time they
470 // were scheduled (requested?) to emerge from the delayed task queue. This
471 // means that queueing delay for such tasks will show how long they went
472 // unserviced, after they *could* be serviced. This is the same stat as we
473 // have for non-delayed tasks, and we consistently call it queueing delay.
474 TrackedTime effective_post_time
= completed_task
.delayed_run_time
.is_null()
475 ? tracked_objects::TrackedTime(completed_task
.time_posted
)
476 : tracked_objects::TrackedTime(completed_task
.delayed_run_time
);
478 // Watch out for a race where status_ is changing, and hence one or both
479 // of start_of_run or end_of_run is zero. In that case, we didn't bother to
480 // get a time value since we "weren't tracking" and we were trying to be
481 // efficient by not calling for a genuine time value. For simplicity, we'll
482 // use a default zero duration when we can't calculate a true value.
483 int32 queue_duration
= 0;
484 int32 run_duration
= 0;
485 if (!start_of_run
.is_null()) {
486 queue_duration
= (start_of_run
- effective_post_time
).InMilliseconds();
487 if (!end_of_run
.is_null())
488 run_duration
= (end_of_run
- start_of_run
).InMilliseconds();
490 current_thread_data
->TallyADeath(*birth
, queue_duration
, run_duration
);
494 void ThreadData::TallyRunOnWorkerThreadIfTracking(
496 const TrackedTime
& time_posted
,
497 const TrackedTime
& start_of_run
,
498 const TrackedTime
& end_of_run
) {
499 if (!kTrackAllTaskObjects
)
500 return; // Not compiled in.
502 // Even if we have been DEACTIVATED, we will process any pending births so
503 // that our data structures (which counted the outstanding births) remain
508 // TODO(jar): Support the option to coalesce all worker-thread activity under
509 // one ThreadData instance that uses locks to protect *all* access. This will
510 // reduce memory (making it provably bounded), but run incrementally slower
511 // (since we'll use locks on TallyABirth and TallyADeath). The good news is
512 // that the locks on TallyADeath will be *after* the worker thread has run,
513 // and hence nothing will be waiting for the completion (... besides some
514 // other thread that might like to run). Also, the worker threads tasks are
515 // generally longer, and hence the cost of the lock may perchance be amortized
516 // over the long task's lifetime.
517 ThreadData
* current_thread_data
= Get();
518 if (!current_thread_data
)
521 int32 queue_duration
= 0;
522 int32 run_duration
= 0;
523 if (!start_of_run
.is_null()) {
524 queue_duration
= (start_of_run
- time_posted
).InMilliseconds();
525 if (!end_of_run
.is_null())
526 run_duration
= (end_of_run
- start_of_run
).InMilliseconds();
528 current_thread_data
->TallyADeath(*birth
, queue_duration
, run_duration
);
532 void ThreadData::TallyRunInAScopedRegionIfTracking(
534 const TrackedTime
& start_of_run
,
535 const TrackedTime
& end_of_run
) {
536 if (!kTrackAllTaskObjects
)
537 return; // Not compiled in.
539 // Even if we have been DEACTIVATED, we will process any pending births so
540 // that our data structures (which counted the outstanding births) remain
545 ThreadData
* current_thread_data
= Get();
546 if (!current_thread_data
)
549 int32 queue_duration
= 0;
550 int32 run_duration
= 0;
551 if (!start_of_run
.is_null() && !end_of_run
.is_null())
552 run_duration
= (end_of_run
- start_of_run
).InMilliseconds();
553 current_thread_data
->TallyADeath(*birth
, queue_duration
, run_duration
);
557 void ThreadData::SnapshotAllExecutedTasks(bool reset_max
,
558 ProcessDataSnapshot
* process_data
,
559 BirthCountMap
* birth_counts
) {
560 if (!kTrackAllTaskObjects
)
561 return; // Not compiled in.
563 // Get an unchanging copy of a ThreadData list.
564 ThreadData
* my_list
= ThreadData::first();
566 // Gather data serially.
567 // This hackish approach *can* get some slighly corrupt tallies, as we are
568 // grabbing values without the protection of a lock, but it has the advantage
569 // of working even with threads that don't have message loops. If a user
570 // sees any strangeness, they can always just run their stats gathering a
572 for (ThreadData
* thread_data
= my_list
;
574 thread_data
= thread_data
->next()) {
575 thread_data
->SnapshotExecutedTasks(reset_max
, process_data
, birth_counts
);
579 void ThreadData::SnapshotExecutedTasks(bool reset_max
,
580 ProcessDataSnapshot
* process_data
,
581 BirthCountMap
* birth_counts
) {
582 // Get copy of data, so that the data will not change during the iterations
584 ThreadData::BirthMap birth_map
;
585 ThreadData::DeathMap death_map
;
586 ThreadData::ParentChildSet parent_child_set
;
587 SnapshotMaps(reset_max
, &birth_map
, &death_map
, &parent_child_set
);
589 for (ThreadData::DeathMap::const_iterator it
= death_map
.begin();
590 it
!= death_map
.end(); ++it
) {
591 process_data
->tasks
.push_back(
592 TaskSnapshot(*it
->first
, it
->second
, thread_name()));
593 (*birth_counts
)[it
->first
] -= it
->first
->birth_count();
596 for (ThreadData::BirthMap::const_iterator it
= birth_map
.begin();
597 it
!= birth_map
.end(); ++it
) {
598 (*birth_counts
)[it
->second
] += it
->second
->birth_count();
601 if (!kTrackParentChildLinks
)
604 for (ThreadData::ParentChildSet::const_iterator it
= parent_child_set
.begin();
605 it
!= parent_child_set
.end(); ++it
) {
606 process_data
->descendants
.push_back(ParentChildPairSnapshot(*it
));
610 // This may be called from another thread.
611 void ThreadData::SnapshotMaps(bool reset_max
,
614 ParentChildSet
* parent_child_set
) {
615 base::AutoLock
lock(map_lock_
);
616 for (BirthMap::const_iterator it
= birth_map_
.begin();
617 it
!= birth_map_
.end(); ++it
)
618 (*birth_map
)[it
->first
] = it
->second
;
619 for (DeathMap::iterator it
= death_map_
.begin();
620 it
!= death_map_
.end(); ++it
) {
621 (*death_map
)[it
->first
] = it
->second
;
623 it
->second
.ResetMax();
626 if (!kTrackParentChildLinks
)
629 for (ParentChildSet::iterator it
= parent_child_set_
.begin();
630 it
!= parent_child_set_
.end(); ++it
)
631 parent_child_set
->insert(*it
);
635 void ThreadData::ResetAllThreadData() {
636 ThreadData
* my_list
= first();
638 for (ThreadData
* thread_data
= my_list
;
640 thread_data
= thread_data
->next())
641 thread_data
->Reset();
644 void ThreadData::Reset() {
645 base::AutoLock
lock(map_lock_
);
646 for (DeathMap::iterator it
= death_map_
.begin();
647 it
!= death_map_
.end(); ++it
)
649 for (BirthMap::iterator it
= birth_map_
.begin();
650 it
!= birth_map_
.end(); ++it
)
654 static void OptionallyInitializeAlternateTimer() {
655 NowFunction
* alternate_time_source
= GetAlternateTimeSource();
656 if (alternate_time_source
)
657 ThreadData::SetAlternateTimeSource(alternate_time_source
);
660 bool ThreadData::Initialize() {
661 if (!kTrackAllTaskObjects
)
662 return false; // Not compiled in.
663 if (status_
>= DEACTIVATED
)
664 return true; // Someone else did the initialization.
665 // Due to racy lazy initialization in tests, we'll need to recheck status_
666 // after we acquire the lock.
668 // Ensure that we don't double initialize tls. We are called when single
669 // threaded in the product, but some tests may be racy and lazy about our
671 base::AutoLock
lock(*list_lock_
.Pointer());
672 if (status_
>= DEACTIVATED
)
673 return true; // Someone raced in here and beat us.
675 // Put an alternate timer in place if the environment calls for it, such as
676 // for tracking TCMalloc allocations. This insertion is idempotent, so we
677 // don't mind if there is a race, and we'd prefer not to be in a lock while
679 if (kAllowAlternateTimeSourceHandling
)
680 OptionallyInitializeAlternateTimer();
682 // Perform the "real" TLS initialization now, and leave it intact through
683 // process termination.
684 if (!tls_index_
.initialized()) { // Testing may have initialized this.
685 DCHECK_EQ(status_
, UNINITIALIZED
);
686 tls_index_
.Initialize(&ThreadData::OnThreadTermination
);
687 if (!tls_index_
.initialized())
690 // TLS was initialzed for us earlier.
691 DCHECK_EQ(status_
, DORMANT_DURING_TESTS
);
694 // Incarnation counter is only significant to testing, as it otherwise will
695 // never again change in this process.
696 ++incarnation_counter_
;
698 // The lock is not critical for setting status_, but it doesn't hurt. It also
699 // ensures that if we have a racy initialization, that we'll bail as soon as
700 // we get the lock earlier in this method.
701 status_
= kInitialStartupState
;
702 if (!kTrackParentChildLinks
&&
703 kInitialStartupState
== PROFILING_CHILDREN_ACTIVE
)
704 status_
= PROFILING_ACTIVE
;
705 DCHECK(status_
!= UNINITIALIZED
);
710 bool ThreadData::InitializeAndSetTrackingStatus(Status status
) {
711 DCHECK_GE(status
, DEACTIVATED
);
712 DCHECK_LE(status
, PROFILING_CHILDREN_ACTIVE
);
714 if (!Initialize()) // No-op if already initialized.
715 return false; // Not compiled in.
717 if (!kTrackParentChildLinks
&& status
> DEACTIVATED
)
718 status
= PROFILING_ACTIVE
;
724 ThreadData::Status
ThreadData::status() {
729 bool ThreadData::TrackingStatus() {
730 return status_
> DEACTIVATED
;
734 bool ThreadData::TrackingParentChildStatus() {
735 return status_
>= PROFILING_CHILDREN_ACTIVE
;
739 TrackedTime
ThreadData::NowForStartOfRun(const Births
* parent
) {
740 if (kTrackParentChildLinks
&& parent
&& status_
> PROFILING_ACTIVE
) {
741 ThreadData
* current_thread_data
= Get();
742 if (current_thread_data
)
743 current_thread_data
->parent_stack_
.push(parent
);
749 TrackedTime
ThreadData::NowForEndOfRun() {
754 void ThreadData::SetAlternateTimeSource(NowFunction
* now_function
) {
755 DCHECK(now_function
);
756 if (kAllowAlternateTimeSourceHandling
)
757 now_function_
= now_function
;
761 TrackedTime
ThreadData::Now() {
762 if (kAllowAlternateTimeSourceHandling
&& now_function_
)
763 return TrackedTime::FromMilliseconds((*now_function_
)());
764 if (kTrackAllTaskObjects
&& TrackingStatus())
765 return TrackedTime::Now();
766 return TrackedTime(); // Super fast when disabled, or not compiled.
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.
774 // Verify that we've at least shutdown/cleanup the major namesd threads. The
775 // caller should tell us how many thread shutdowns should have taken place by
777 return; // TODO(jar): until this is working on XP, don't run the real test.
778 CHECK_GT(cleanup_count_
, major_threads_shutdown_count
);
782 void ThreadData::ShutdownSingleThreadedCleanup(bool leak
) {
783 // This is only called from test code, where we need to cleanup so that
784 // additional tests can be run.
785 // We must be single threaded... but be careful anyway.
786 if (!InitializeAndSetTrackingStatus(DEACTIVATED
))
788 ThreadData
* thread_data_list
;
790 base::AutoLock
lock(*list_lock_
.Pointer());
791 thread_data_list
= all_thread_data_list_head_
;
792 all_thread_data_list_head_
= NULL
;
793 ++incarnation_counter_
;
794 // To be clean, break apart the retired worker list (though we leak them).
795 while (first_retired_worker_
) {
796 ThreadData
* worker
= first_retired_worker_
;
797 CHECK_GT(worker
->worker_thread_number_
, 0);
798 first_retired_worker_
= worker
->next_retired_worker_
;
799 worker
->next_retired_worker_
= NULL
;
803 // Put most global static back in pristine shape.
804 worker_thread_data_creation_count_
= 0;
806 tls_index_
.Set(NULL
);
807 status_
= DORMANT_DURING_TESTS
; // Almost UNINITIALIZED.
809 // To avoid any chance of racing in unit tests, which is the only place we
810 // call this function, we may sometimes leak all the data structures we
811 // recovered, as they may still be in use on threads from prior tests!
813 ThreadData
* thread_data
= thread_data_list
;
814 while (thread_data
) {
815 ANNOTATE_LEAKING_OBJECT_PTR(thread_data
);
816 thread_data
= thread_data
->next();
821 // When we want to cleanup (on a single thread), here is what we do.
823 // Do actual recursive delete in all ThreadData instances.
824 while (thread_data_list
) {
825 ThreadData
* next_thread_data
= thread_data_list
;
826 thread_data_list
= thread_data_list
->next();
828 for (BirthMap::iterator it
= next_thread_data
->birth_map_
.begin();
829 next_thread_data
->birth_map_
.end() != it
; ++it
)
830 delete it
->second
; // Delete the Birth Records.
831 delete next_thread_data
; // Includes all Death Records.
835 //------------------------------------------------------------------------------
836 TaskSnapshot::TaskSnapshot() {
839 TaskSnapshot::TaskSnapshot(const BirthOnThread
& birth
,
840 const DeathData
& death_data
,
841 const std::string
& death_thread_name
)
843 death_data(death_data
),
844 death_thread_name(death_thread_name
) {
847 TaskSnapshot::~TaskSnapshot() {
850 //------------------------------------------------------------------------------
851 // ParentChildPairSnapshot
853 ParentChildPairSnapshot::ParentChildPairSnapshot() {
856 ParentChildPairSnapshot::ParentChildPairSnapshot(
857 const ThreadData::ParentChildPair
& parent_child
)
858 : parent(*parent_child
.first
),
859 child(*parent_child
.second
) {
862 ParentChildPairSnapshot::~ParentChildPairSnapshot() {
865 //------------------------------------------------------------------------------
866 // ProcessDataSnapshot
868 ProcessDataSnapshot::ProcessDataSnapshot()
869 #if !defined(OS_NACL)
870 : process_id(base::GetCurrentProcId()) {
876 ProcessDataSnapshot::~ProcessDataSnapshot() {
879 } // namespace tracked_objects