This source file includes following definitions.
- IsProfilerTimingEnabled
- count
- run_duration_sum
- run_duration_max
- run_duration_sample
- queue_duration_sum
- queue_duration_max
- queue_duration_sample
- ResetMax
- Clear
- queue_duration_sample
- queue_duration_sample
- birth_thread_
- thread_name
- birth_count_
- birth_count
- RecordBirth
- ForgetBirth
- Clear
- incarnation_count_for_pool_
- incarnation_count_for_pool_
- PushToHeadOfList
- first
- next
- InitializeThreadContext
- Get
- OnThreadTermination
- OnThreadTerminationCleanup
- Snapshot
- TallyABirth
- TallyADeath
- TallyABirthIfActive
- TallyRunOnNamedThreadIfTracking
- TallyRunOnWorkerThreadIfTracking
- TallyRunInAScopedRegionIfTracking
- SnapshotAllExecutedTasks
- SnapshotExecutedTasks
- SnapshotMaps
- ResetAllThreadData
- Reset
- OptionallyInitializeAlternateTimer
- Initialize
- InitializeAndSetTrackingStatus
- status
- TrackingStatus
- TrackingParentChildStatus
- NowForStartOfRun
- NowForEndOfRun
- SetAlternateTimeSource
- Now
- EnsureCleanupWasCalled
- ShutdownSingleThreadedCleanup
- death_thread_name
- child
- process_id
#include "base/tracked_objects.h"
#include <limits.h>
#include <stdlib.h>
#include "base/atomicops.h"
#include "base/base_switches.h"
#include "base/command_line.h"
#include "base/compiler_specific.h"
#include "base/debug/leak_annotations.h"
#include "base/logging.h"
#include "base/process/process_handle.h"
#include "base/profiler/alternate_timer.h"
#include "base/strings/stringprintf.h"
#include "base/third_party/valgrind/memcheck.h"
#include "base/tracking_info.h"
using base::TimeDelta;
namespace base {
class TimeDelta;
}
namespace tracked_objects {
namespace {
const bool kTrackAllTaskObjects = true;
const bool kTrackParentChildLinks = false;
const ThreadData::Status kInitialStartupState =
ThreadData::PROFILING_CHILDREN_ACTIVE;
static const bool kAllowAlternateTimeSourceHandling = true;
inline bool IsProfilerTimingEnabled() {
enum {
UNDEFINED_TIMING,
ENABLED_TIMING,
DISABLED_TIMING,
};
static base::subtle::Atomic32 timing_enabled = UNDEFINED_TIMING;
base::subtle::Atomic32 current_timing_enabled =
base::subtle::NoBarrier_Load(&timing_enabled);
if (current_timing_enabled == UNDEFINED_TIMING) {
if (!CommandLine::InitializedForCurrentProcess())
return true;
current_timing_enabled =
(CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
switches::kProfilerTiming) ==
switches::kProfilerTimingDisabledValue)
? DISABLED_TIMING
: ENABLED_TIMING;
base::subtle::NoBarrier_Store(&timing_enabled, current_timing_enabled);
}
return current_timing_enabled == ENABLED_TIMING;
}
}
DeathData::DeathData() {
Clear();
}
DeathData::DeathData(int count) {
Clear();
count_ = count;
}
#define CONDITIONAL_ASSIGN(assign_it, target, source) \
((target) ^= ((target) ^ (source)) & -static_cast<int32>(assign_it))
void DeathData::RecordDeath(const int32 queue_duration,
const int32 run_duration,
int32 random_number) {
if (count_ < INT_MAX)
++count_;
queue_duration_sum_ += queue_duration;
run_duration_sum_ += run_duration;
if (queue_duration_max_ < queue_duration)
queue_duration_max_ = queue_duration;
if (run_duration_max_ < run_duration)
run_duration_max_ = run_duration;
CHECK_GT(count_, 0);
if (0 == (random_number % count_)) {
queue_duration_sample_ = queue_duration;
run_duration_sample_ = run_duration;
}
}
int DeathData::count() const { return count_; }
int32 DeathData::run_duration_sum() const { return run_duration_sum_; }
int32 DeathData::run_duration_max() const { return run_duration_max_; }
int32 DeathData::run_duration_sample() const {
return run_duration_sample_;
}
int32 DeathData::queue_duration_sum() const {
return queue_duration_sum_;
}
int32 DeathData::queue_duration_max() const {
return queue_duration_max_;
}
int32 DeathData::queue_duration_sample() const {
return queue_duration_sample_;
}
void DeathData::ResetMax() {
run_duration_max_ = 0;
queue_duration_max_ = 0;
}
void DeathData::Clear() {
count_ = 0;
run_duration_sum_ = 0;
run_duration_max_ = 0;
run_duration_sample_ = 0;
queue_duration_sum_ = 0;
queue_duration_max_ = 0;
queue_duration_sample_ = 0;
}
DeathDataSnapshot::DeathDataSnapshot()
: count(-1),
run_duration_sum(-1),
run_duration_max(-1),
run_duration_sample(-1),
queue_duration_sum(-1),
queue_duration_max(-1),
queue_duration_sample(-1) {
}
DeathDataSnapshot::DeathDataSnapshot(
const tracked_objects::DeathData& death_data)
: count(death_data.count()),
run_duration_sum(death_data.run_duration_sum()),
run_duration_max(death_data.run_duration_max()),
run_duration_sample(death_data.run_duration_sample()),
queue_duration_sum(death_data.queue_duration_sum()),
queue_duration_max(death_data.queue_duration_max()),
queue_duration_sample(death_data.queue_duration_sample()) {
}
DeathDataSnapshot::~DeathDataSnapshot() {
}
BirthOnThread::BirthOnThread(const Location& location,
const ThreadData& current)
: location_(location),
birth_thread_(¤t) {
}
BirthOnThreadSnapshot::BirthOnThreadSnapshot() {
}
BirthOnThreadSnapshot::BirthOnThreadSnapshot(
const tracked_objects::BirthOnThread& birth)
: location(birth.location()),
thread_name(birth.birth_thread()->thread_name()) {
}
BirthOnThreadSnapshot::~BirthOnThreadSnapshot() {
}
Births::Births(const Location& location, const ThreadData& current)
: BirthOnThread(location, current),
birth_count_(1) { }
int Births::birth_count() const { return birth_count_; }
void Births::RecordBirth() { ++birth_count_; }
void Births::ForgetBirth() { --birth_count_; }
void Births::Clear() { birth_count_ = 0; }
NowFunction* ThreadData::now_function_ = NULL;
base::ThreadLocalStorage::StaticSlot ThreadData::tls_index_ = TLS_INITIALIZER;
int ThreadData::worker_thread_data_creation_count_ = 0;
int ThreadData::cleanup_count_ = 0;
int ThreadData::incarnation_counter_ = 0;
ThreadData* ThreadData::all_thread_data_list_head_ = NULL;
ThreadData* ThreadData::first_retired_worker_ = NULL;
base::LazyInstance<base::Lock>::Leaky
ThreadData::list_lock_ = LAZY_INSTANCE_INITIALIZER;
ThreadData::Status ThreadData::status_ = ThreadData::UNINITIALIZED;
ThreadData::ThreadData(const std::string& suggested_name)
: next_(NULL),
next_retired_worker_(NULL),
worker_thread_number_(0),
incarnation_count_for_pool_(-1) {
DCHECK_GE(suggested_name.size(), 0u);
thread_name_ = suggested_name;
PushToHeadOfList();
}
ThreadData::ThreadData(int thread_number)
: next_(NULL),
next_retired_worker_(NULL),
worker_thread_number_(thread_number),
incarnation_count_for_pool_(-1) {
CHECK_GT(thread_number, 0);
base::StringAppendF(&thread_name_, "WorkerThread-%d", thread_number);
PushToHeadOfList();
}
ThreadData::~ThreadData() {}
void ThreadData::PushToHeadOfList() {
(void)VALGRIND_MAKE_MEM_DEFINED_IF_ADDRESSABLE(&random_number_,
sizeof(random_number_));
MSAN_UNPOISON(&random_number_, sizeof(random_number_));
random_number_ += static_cast<int32>(this - static_cast<ThreadData*>(0));
random_number_ ^= (Now() - TrackedTime()).InMilliseconds();
DCHECK(!next_);
base::AutoLock lock(*list_lock_.Pointer());
incarnation_count_for_pool_ = incarnation_counter_;
next_ = all_thread_data_list_head_;
all_thread_data_list_head_ = this;
}
ThreadData* ThreadData::first() {
base::AutoLock lock(*list_lock_.Pointer());
return all_thread_data_list_head_;
}
ThreadData* ThreadData::next() const { return next_; }
void ThreadData::InitializeThreadContext(const std::string& suggested_name) {
if (!Initialize())
return;
ThreadData* current_thread_data =
reinterpret_cast<ThreadData*>(tls_index_.Get());
if (current_thread_data)
return;
current_thread_data = new ThreadData(suggested_name);
tls_index_.Set(current_thread_data);
}
ThreadData* ThreadData::Get() {
if (!tls_index_.initialized())
return NULL;
ThreadData* registered = reinterpret_cast<ThreadData*>(tls_index_.Get());
if (registered)
return registered;
ThreadData* worker_thread_data = NULL;
int worker_thread_number = 0;
{
base::AutoLock lock(*list_lock_.Pointer());
if (first_retired_worker_) {
worker_thread_data = first_retired_worker_;
first_retired_worker_ = first_retired_worker_->next_retired_worker_;
worker_thread_data->next_retired_worker_ = NULL;
} else {
worker_thread_number = ++worker_thread_data_creation_count_;
}
}
if (!worker_thread_data) {
DCHECK_GT(worker_thread_number, 0);
worker_thread_data = new ThreadData(worker_thread_number);
}
DCHECK_GT(worker_thread_data->worker_thread_number_, 0);
tls_index_.Set(worker_thread_data);
return worker_thread_data;
}
void ThreadData::OnThreadTermination(void* thread_data) {
DCHECK(thread_data);
if (!kTrackAllTaskObjects)
return;
reinterpret_cast<ThreadData*>(thread_data)->OnThreadTerminationCleanup();
}
void ThreadData::OnThreadTerminationCleanup() {
base::AutoLock lock(*list_lock_.Pointer());
if (incarnation_counter_ != incarnation_count_for_pool_)
return;
++cleanup_count_;
if (!worker_thread_number_) {
return;
}
DCHECK_EQ(this->next_retired_worker_, reinterpret_cast<ThreadData*>(NULL));
this->next_retired_worker_ = first_retired_worker_;
first_retired_worker_ = this;
}
void ThreadData::Snapshot(bool reset_max, ProcessDataSnapshot* process_data) {
BirthCountMap birth_counts;
ThreadData::SnapshotAllExecutedTasks(reset_max, process_data, &birth_counts);
for (BirthCountMap::const_iterator it = birth_counts.begin();
it != birth_counts.end(); ++it) {
if (it->second > 0) {
process_data->tasks.push_back(
TaskSnapshot(*it->first, DeathData(it->second), "Still_Alive"));
}
}
}
Births* ThreadData::TallyABirth(const Location& location) {
BirthMap::iterator it = birth_map_.find(location);
Births* child;
if (it != birth_map_.end()) {
child = it->second;
child->RecordBirth();
} else {
child = new Births(location, *this);
base::AutoLock lock(map_lock_);
birth_map_[location] = child;
}
if (kTrackParentChildLinks && status_ > PROFILING_ACTIVE &&
!parent_stack_.empty()) {
const Births* parent = parent_stack_.top();
ParentChildPair pair(parent, child);
if (parent_child_set_.find(pair) == parent_child_set_.end()) {
base::AutoLock lock(map_lock_);
parent_child_set_.insert(pair);
}
}
return child;
}
void ThreadData::TallyADeath(const Births& birth,
int32 queue_duration,
int32 run_duration) {
const int32 kSomePrimeNumber = 2147483647;
random_number_ += queue_duration + run_duration + kSomePrimeNumber;
random_number_ ^= static_cast<int32>(&birth - reinterpret_cast<Births*>(0));
if (kAllowAlternateTimeSourceHandling && now_function_)
queue_duration = 0;
DeathMap::iterator it = death_map_.find(&birth);
DeathData* death_data;
if (it != death_map_.end()) {
death_data = &it->second;
} else {
base::AutoLock lock(map_lock_);
death_data = &death_map_[&birth];
}
death_data->RecordDeath(queue_duration, run_duration, random_number_);
if (!kTrackParentChildLinks)
return;
if (!parent_stack_.empty()) {
DCHECK_EQ(parent_stack_.top(), &birth);
parent_stack_.pop();
}
}
Births* ThreadData::TallyABirthIfActive(const Location& location) {
if (!kTrackAllTaskObjects)
return NULL;
if (!TrackingStatus())
return NULL;
ThreadData* current_thread_data = Get();
if (!current_thread_data)
return NULL;
return current_thread_data->TallyABirth(location);
}
void ThreadData::TallyRunOnNamedThreadIfTracking(
const base::TrackingInfo& completed_task,
const TrackedTime& start_of_run,
const TrackedTime& end_of_run) {
if (!kTrackAllTaskObjects)
return;
const Births* birth = completed_task.birth_tally;
if (!birth)
return;
ThreadData* current_thread_data = Get();
if (!current_thread_data)
return;
int32 queue_duration = 0;
int32 run_duration = 0;
if (!start_of_run.is_null()) {
queue_duration = (start_of_run - completed_task.EffectiveTimePosted())
.InMilliseconds();
if (!end_of_run.is_null())
run_duration = (end_of_run - start_of_run).InMilliseconds();
}
current_thread_data->TallyADeath(*birth, queue_duration, run_duration);
}
void ThreadData::TallyRunOnWorkerThreadIfTracking(
const Births* birth,
const TrackedTime& time_posted,
const TrackedTime& start_of_run,
const TrackedTime& end_of_run) {
if (!kTrackAllTaskObjects)
return;
if (!birth)
return;
ThreadData* current_thread_data = Get();
if (!current_thread_data)
return;
int32 queue_duration = 0;
int32 run_duration = 0;
if (!start_of_run.is_null()) {
queue_duration = (start_of_run - time_posted).InMilliseconds();
if (!end_of_run.is_null())
run_duration = (end_of_run - start_of_run).InMilliseconds();
}
current_thread_data->TallyADeath(*birth, queue_duration, run_duration);
}
void ThreadData::TallyRunInAScopedRegionIfTracking(
const Births* birth,
const TrackedTime& start_of_run,
const TrackedTime& end_of_run) {
if (!kTrackAllTaskObjects)
return;
if (!birth)
return;
ThreadData* current_thread_data = Get();
if (!current_thread_data)
return;
int32 queue_duration = 0;
int32 run_duration = 0;
if (!start_of_run.is_null() && !end_of_run.is_null())
run_duration = (end_of_run - start_of_run).InMilliseconds();
current_thread_data->TallyADeath(*birth, queue_duration, run_duration);
}
void ThreadData::SnapshotAllExecutedTasks(bool reset_max,
ProcessDataSnapshot* process_data,
BirthCountMap* birth_counts) {
if (!kTrackAllTaskObjects)
return;
ThreadData* my_list = ThreadData::first();
for (ThreadData* thread_data = my_list;
thread_data;
thread_data = thread_data->next()) {
thread_data->SnapshotExecutedTasks(reset_max, process_data, birth_counts);
}
}
void ThreadData::SnapshotExecutedTasks(bool reset_max,
ProcessDataSnapshot* process_data,
BirthCountMap* birth_counts) {
ThreadData::BirthMap birth_map;
ThreadData::DeathMap death_map;
ThreadData::ParentChildSet parent_child_set;
SnapshotMaps(reset_max, &birth_map, &death_map, &parent_child_set);
for (ThreadData::DeathMap::const_iterator it = death_map.begin();
it != death_map.end(); ++it) {
process_data->tasks.push_back(
TaskSnapshot(*it->first, it->second, thread_name()));
(*birth_counts)[it->first] -= it->first->birth_count();
}
for (ThreadData::BirthMap::const_iterator it = birth_map.begin();
it != birth_map.end(); ++it) {
(*birth_counts)[it->second] += it->second->birth_count();
}
if (!kTrackParentChildLinks)
return;
for (ThreadData::ParentChildSet::const_iterator it = parent_child_set.begin();
it != parent_child_set.end(); ++it) {
process_data->descendants.push_back(ParentChildPairSnapshot(*it));
}
}
void ThreadData::SnapshotMaps(bool reset_max,
BirthMap* birth_map,
DeathMap* death_map,
ParentChildSet* parent_child_set) {
base::AutoLock lock(map_lock_);
for (BirthMap::const_iterator it = birth_map_.begin();
it != birth_map_.end(); ++it)
(*birth_map)[it->first] = it->second;
for (DeathMap::iterator it = death_map_.begin();
it != death_map_.end(); ++it) {
(*death_map)[it->first] = it->second;
if (reset_max)
it->second.ResetMax();
}
if (!kTrackParentChildLinks)
return;
for (ParentChildSet::iterator it = parent_child_set_.begin();
it != parent_child_set_.end(); ++it)
parent_child_set->insert(*it);
}
void ThreadData::ResetAllThreadData() {
ThreadData* my_list = first();
for (ThreadData* thread_data = my_list;
thread_data;
thread_data = thread_data->next())
thread_data->Reset();
}
void ThreadData::Reset() {
base::AutoLock lock(map_lock_);
for (DeathMap::iterator it = death_map_.begin();
it != death_map_.end(); ++it)
it->second.Clear();
for (BirthMap::iterator it = birth_map_.begin();
it != birth_map_.end(); ++it)
it->second->Clear();
}
static void OptionallyInitializeAlternateTimer() {
NowFunction* alternate_time_source = GetAlternateTimeSource();
if (alternate_time_source)
ThreadData::SetAlternateTimeSource(alternate_time_source);
}
bool ThreadData::Initialize() {
if (!kTrackAllTaskObjects)
return false;
if (status_ >= DEACTIVATED)
return true;
base::AutoLock lock(*list_lock_.Pointer());
if (status_ >= DEACTIVATED)
return true;
if (kAllowAlternateTimeSourceHandling)
OptionallyInitializeAlternateTimer();
if (!tls_index_.initialized()) {
DCHECK_EQ(status_, UNINITIALIZED);
tls_index_.Initialize(&ThreadData::OnThreadTermination);
if (!tls_index_.initialized())
return false;
} else {
DCHECK_EQ(status_, DORMANT_DURING_TESTS);
}
++incarnation_counter_;
status_ = kInitialStartupState;
if (!kTrackParentChildLinks &&
kInitialStartupState == PROFILING_CHILDREN_ACTIVE)
status_ = PROFILING_ACTIVE;
DCHECK(status_ != UNINITIALIZED);
return true;
}
bool ThreadData::InitializeAndSetTrackingStatus(Status status) {
DCHECK_GE(status, DEACTIVATED);
DCHECK_LE(status, PROFILING_CHILDREN_ACTIVE);
if (!Initialize())
return false;
if (!kTrackParentChildLinks && status > DEACTIVATED)
status = PROFILING_ACTIVE;
status_ = status;
return true;
}
ThreadData::Status ThreadData::status() {
return status_;
}
bool ThreadData::TrackingStatus() {
return status_ > DEACTIVATED;
}
bool ThreadData::TrackingParentChildStatus() {
return status_ >= PROFILING_CHILDREN_ACTIVE;
}
TrackedTime ThreadData::NowForStartOfRun(const Births* parent) {
if (kTrackParentChildLinks && parent && status_ > PROFILING_ACTIVE) {
ThreadData* current_thread_data = Get();
if (current_thread_data)
current_thread_data->parent_stack_.push(parent);
}
return Now();
}
TrackedTime ThreadData::NowForEndOfRun() {
return Now();
}
void ThreadData::SetAlternateTimeSource(NowFunction* now_function) {
DCHECK(now_function);
if (kAllowAlternateTimeSourceHandling)
now_function_ = now_function;
}
TrackedTime ThreadData::Now() {
if (kAllowAlternateTimeSourceHandling && now_function_)
return TrackedTime::FromMilliseconds((*now_function_)());
if (kTrackAllTaskObjects && IsProfilerTimingEnabled() && TrackingStatus())
return TrackedTime::Now();
return TrackedTime();
}
void ThreadData::EnsureCleanupWasCalled(int major_threads_shutdown_count) {
base::AutoLock lock(*list_lock_.Pointer());
if (worker_thread_data_creation_count_ == 0)
return;
#if 0
CHECK_GT(cleanup_count_, major_threads_shutdown_count);
#endif
}
void ThreadData::ShutdownSingleThreadedCleanup(bool leak) {
if (!InitializeAndSetTrackingStatus(DEACTIVATED))
return;
ThreadData* thread_data_list;
{
base::AutoLock lock(*list_lock_.Pointer());
thread_data_list = all_thread_data_list_head_;
all_thread_data_list_head_ = NULL;
++incarnation_counter_;
while (first_retired_worker_) {
ThreadData* worker = first_retired_worker_;
CHECK_GT(worker->worker_thread_number_, 0);
first_retired_worker_ = worker->next_retired_worker_;
worker->next_retired_worker_ = NULL;
}
}
worker_thread_data_creation_count_ = 0;
cleanup_count_ = 0;
tls_index_.Set(NULL);
status_ = DORMANT_DURING_TESTS;
if (leak) {
ThreadData* thread_data = thread_data_list;
while (thread_data) {
ANNOTATE_LEAKING_OBJECT_PTR(thread_data);
thread_data = thread_data->next();
}
return;
}
while (thread_data_list) {
ThreadData* next_thread_data = thread_data_list;
thread_data_list = thread_data_list->next();
for (BirthMap::iterator it = next_thread_data->birth_map_.begin();
next_thread_data->birth_map_.end() != it; ++it)
delete it->second;
delete next_thread_data;
}
}
TaskSnapshot::TaskSnapshot() {
}
TaskSnapshot::TaskSnapshot(const BirthOnThread& birth,
const DeathData& death_data,
const std::string& death_thread_name)
: birth(birth),
death_data(death_data),
death_thread_name(death_thread_name) {
}
TaskSnapshot::~TaskSnapshot() {
}
ParentChildPairSnapshot::ParentChildPairSnapshot() {
}
ParentChildPairSnapshot::ParentChildPairSnapshot(
const ThreadData::ParentChildPair& parent_child)
: parent(*parent_child.first),
child(*parent_child.second) {
}
ParentChildPairSnapshot::~ParentChildPairSnapshot() {
}
ProcessDataSnapshot::ProcessDataSnapshot()
#if !defined(OS_NACL)
: process_id(base::GetCurrentProcId()) {
#else
: process_id(0) {
#endif
}
ProcessDataSnapshot::~ProcessDataSnapshot() {
}
}