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