Chromium Code Reviews| Index: base/tracked_objects.cc |
| diff --git a/base/tracked_objects.cc b/base/tracked_objects.cc |
| index 5359d892116577750cacff82a8522322c0b0b04b..7e2a713b1b1151e1cc9c96426598fe16c7bdff65 100644 |
| --- a/base/tracked_objects.cc |
| +++ b/base/tracked_objects.cc |
| @@ -227,6 +227,10 @@ int Births::birth_count() const { return birth_count_; } |
| void Births::RecordBirth() { ++birth_count_; } |
| +void Births::SubtractBirths(int count) { |
| + birth_count_ -= count; |
| +} |
| + |
| //------------------------------------------------------------------------------ |
| // ThreadData maintains the central data for all births and deaths on a single |
| // thread. |
| @@ -269,6 +273,10 @@ base::LazyInstance<base::Lock>::Leaky |
| // static |
| ThreadData::Status ThreadData::status_ = ThreadData::UNINITIALIZED; |
| +// static |
| +base::LazyInstance<PhasedProcessDataSnapshots> |
| + ThreadData::completed_phases_snapshots_ = LAZY_INSTANCE_INITIALIZER; |
| + |
| ThreadData::ThreadData(const std::string& suggested_name) |
| : next_(NULL), |
| next_retired_worker_(NULL), |
| @@ -390,23 +398,23 @@ void ThreadData::OnThreadTerminationCleanup() { |
| } |
| // static |
| -void ThreadData::Snapshot(ProcessDataSnapshot* process_data) { |
| - // Add births that have run to completion to |collected_data|. |
| - // |birth_counts| tracks the total number of births recorded at each location |
| - // for which we have not seen a death count. |
| - BirthCountMap birth_counts; |
| - ThreadData::SnapshotAllExecutedTasks(process_data, &birth_counts); |
| +void ThreadData::GetProcessDataSnapshot( |
| + int current_profiling_phase, |
| + ProcessDataSnapshot* process_data_snapshot) { |
| + process_data_snapshot->phased_process_data_snapshots = |
| + completed_phases_snapshots_.Get(); |
| + ThreadData::Snapshot( |
| + false, &process_data_snapshot |
| + ->phased_process_data_snapshots[current_profiling_phase]); |
| +} |
| - // Add births that are still active -- i.e. objects that have tallied a birth, |
| - // but have not yet tallied a matching death, and hence must be either |
| - // running, queued up, or being held in limbo for future posting. |
| - 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")); |
| - } |
| - } |
| +// static |
| +void ThreadData::OnProfilingPhaseCompletion(int profiling_phase) { |
| + if (!kTrackAllTaskObjects) |
| + return; // Not compiled in. |
| + |
| + ThreadData::Snapshot(true, |
| + &completed_phases_snapshots_.Get()[profiling_phase]); |
|
Alexei Svitkine (slow)
2015/03/06 20:06:40
Can this function check that this isn't calling wi
vadimt
2015/03/06 21:34:58
Done.
|
| } |
| Births* ThreadData::TallyABirth(const Location& location) { |
| @@ -438,16 +446,16 @@ Births* ThreadData::TallyABirth(const Location& location) { |
| return child; |
| } |
| -void ThreadData::TallyADeath(const Births& birth, |
| - int32 queue_duration, |
| - const TaskStopwatch& stopwatch) { |
| +void ThreadData::TallyADeath(int32 queue_duration, |
| + const TaskStopwatch& stopwatch, |
| + Births* birth) { |
| int32 run_duration = stopwatch.RunDurationMs(); |
| // Stir in some randomness, plus add constant in case durations are zero. |
| const uint32 kSomePrimeNumber = 2147483647; |
| random_number_ += queue_duration + run_duration + kSomePrimeNumber; |
| // An address is going to have some randomness to it as well ;-). |
| - random_number_ ^= static_cast<uint32>(&birth - reinterpret_cast<Births*>(0)); |
| + random_number_ ^= static_cast<uint32>(birth - reinterpret_cast<Births*>(0)); |
| // We don't have queue durations without OS timer. OS timer is automatically |
| // used for task-post-timing, so the use of an alternate timer implies all |
| @@ -459,20 +467,20 @@ void ThreadData::TallyADeath(const Births& birth, |
| queue_duration = 0; |
| } |
| - DeathMap::iterator it = death_map_.find(&birth); |
| + 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_); // Lock as the map may get relocated now. |
| - death_data = &death_map_[&birth]; |
| + death_data = &death_map_[birth]; |
| } // Release lock ASAP. |
| death_data->RecordDeath(queue_duration, run_duration, random_number_); |
| if (!kTrackParentChildLinks) |
| return; |
| if (!parent_stack_.empty()) { // We might get turned off. |
| - DCHECK_EQ(parent_stack_.top(), &birth); |
| + DCHECK_EQ(parent_stack_.top(), birth); |
| parent_stack_.pop(); |
| } |
| } |
| @@ -500,7 +508,7 @@ void ThreadData::TallyRunOnNamedThreadIfTracking( |
| // Even if we have been DEACTIVATED, we will process any pending births so |
| // that our data structures (which counted the outstanding births) remain |
| // consistent. |
| - const Births* birth = completed_task.birth_tally; |
| + Births* birth = completed_task.birth_tally; |
| if (!birth) |
| return; |
| ThreadData* current_thread_data = stopwatch.GetThreadData(); |
| @@ -518,14 +526,14 @@ void ThreadData::TallyRunOnNamedThreadIfTracking( |
| queue_duration = (start_of_run - completed_task.EffectiveTimePosted()) |
| .InMilliseconds(); |
| } |
| - current_thread_data->TallyADeath(*birth, queue_duration, stopwatch); |
| + current_thread_data->TallyADeath(queue_duration, stopwatch, birth); |
| } |
| // static |
| void ThreadData::TallyRunOnWorkerThreadIfTracking( |
| - const Births* birth, |
| const TrackedTime& time_posted, |
| - const TaskStopwatch& stopwatch) { |
| + const TaskStopwatch& stopwatch, |
| + Births* birth) { |
| if (!kTrackAllTaskObjects) |
| return; // Not compiled in. |
| @@ -553,13 +561,13 @@ void ThreadData::TallyRunOnWorkerThreadIfTracking( |
| if (!start_of_run.is_null()) { |
| queue_duration = (start_of_run - time_posted).InMilliseconds(); |
| } |
| - current_thread_data->TallyADeath(*birth, queue_duration, stopwatch); |
| + current_thread_data->TallyADeath(queue_duration, stopwatch, birth); |
| } |
| // static |
| void ThreadData::TallyRunInAScopedRegionIfTracking( |
| - const Births* birth, |
| - const TaskStopwatch& stopwatch) { |
| + const TaskStopwatch& stopwatch, |
| + Births* birth) { |
| if (!kTrackAllTaskObjects) |
| return; // Not compiled in. |
| @@ -574,12 +582,14 @@ void ThreadData::TallyRunInAScopedRegionIfTracking( |
| return; |
| int32 queue_duration = 0; |
| - current_thread_data->TallyADeath(*birth, queue_duration, stopwatch); |
| + current_thread_data->TallyADeath(queue_duration, stopwatch, birth); |
| } |
| // static |
| -void ThreadData::SnapshotAllExecutedTasks(ProcessDataSnapshot* process_data, |
| - BirthCountMap* birth_counts) { |
| +void ThreadData::SnapshotAllExecutedTasks( |
| + bool reset, |
| + ProcessDataPhaseSnapshot* process_data_phase, |
| + BirthCountMap* birth_counts) { |
| if (!kTrackAllTaskObjects) |
| return; // Not compiled in. |
| @@ -595,59 +605,91 @@ void ThreadData::SnapshotAllExecutedTasks(ProcessDataSnapshot* process_data, |
| for (ThreadData* thread_data = my_list; |
| thread_data; |
| thread_data = thread_data->next()) { |
| - thread_data->SnapshotExecutedTasks(process_data, birth_counts); |
| + thread_data->SnapshotExecutedTasks(reset, process_data_phase, birth_counts); |
| } |
| } |
| -void ThreadData::SnapshotExecutedTasks(ProcessDataSnapshot* process_data, |
| - BirthCountMap* birth_counts) { |
| +// static |
| +void ThreadData::Snapshot(bool reset, |
| + ProcessDataPhaseSnapshot* process_data_phase) { |
| + // Add births that have run to completion to |collected_data|. |
| + // |birth_counts| tracks the total number of births recorded at each location |
| + // for which we have not seen a death count. |
| + BirthCountMap birth_counts; |
| + ThreadData::SnapshotAllExecutedTasks(reset, process_data_phase, |
| + &birth_counts); |
| + |
| + // Add births that are still active -- i.e. objects that have tallied a birth, |
| + // but have not yet tallied a matching death, and hence must be either |
| + // running, queued up, or being held in limbo for future posting. |
| + for (const auto& i : birth_counts) { |
| + if (i.second > 0) { |
| + process_data_phase->tasks.push_back( |
| + TaskSnapshot(*i.first, DeathData(i.second), "Still_Alive")); |
| + } |
| + } |
| +} |
| + |
| +void ThreadData::SnapshotExecutedTasks( |
| + bool reset, |
| + ProcessDataPhaseSnapshot* process_data_phase, |
| + BirthCountMap* birth_counts) { |
| // Get copy of data, so that the data will not change during the iterations |
| // and processing. |
| ThreadData::BirthMap birth_map; |
| ThreadData::DeathMap death_map; |
| ThreadData::ParentChildSet parent_child_set; |
| - SnapshotMaps(&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(); |
| + SnapshotMaps(reset, &birth_map, &death_map, &parent_child_set); |
| + |
| + for (const auto& i : death_map) { |
| + process_data_phase->tasks.push_back( |
| + TaskSnapshot(*i.first, i.second, thread_name())); |
| + // We don't populate birth_counts if |reset| is true. |
| + if (!reset) |
| + (*birth_counts)[i.first] -= i.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(); |
| + for (const auto& i : birth_map) { |
| + (*birth_counts)[i.second] += i.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)); |
| + for (const auto& i : parent_child_set) { |
| + process_data_phase->descendants.push_back(ParentChildPairSnapshot(i)); |
| } |
| } |
| // This may be called from another thread. |
| -void ThreadData::SnapshotMaps(BirthMap* birth_map, |
| +void ThreadData::SnapshotMaps(bool reset, |
| + 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) { |
| + // When reset is not requested, snapshot births. |
| + for (const auto& i : birth_map_) |
| + (*birth_map)[i.first] = i.second; |
| + } |
| + for (auto& i : death_map_) { |
| + // Don't snapshot deaths with 0 count. Deaths with 0 count can result from |
| + // prior calls to SnapshotMaps with reset=true param. |
| + if (i.second.count() <= 0) |
| + continue; |
| + |
| + (*death_map)[i.first] = i.second; |
| + if (reset) { |
| + i.first->SubtractBirths(i.second.count()); |
| + i.second.Clear(); |
| + } |
| } |
| if (!kTrackParentChildLinks) |
| return; |
| - for (ParentChildSet::iterator it = parent_child_set_.begin(); |
| - it != parent_child_set_.end(); ++it) |
| - parent_child_set->insert(*it); |
| + for (const auto& i : parent_child_set_) |
| + parent_child_set->insert(i); |
| } |
| static void OptionallyInitializeAlternateTimer() { |
| @@ -831,6 +873,8 @@ void ThreadData::ShutdownSingleThreadedCleanup(bool leak) { |
| delete it->second; // Delete the Birth Records. |
| delete next_thread_data; // Includes all Death Records. |
| } |
| + |
| + completed_phases_snapshots_.Get().clear(); |
| } |
| //------------------------------------------------------------------------------ |
| @@ -959,7 +1003,16 @@ ParentChildPairSnapshot::~ParentChildPairSnapshot() { |
| } |
| //------------------------------------------------------------------------------ |
| -// ProcessDataSnapshot |
| +// ProcessDataPhaseSnapshot |
| + |
| +ProcessDataPhaseSnapshot::ProcessDataPhaseSnapshot() { |
| +} |
| + |
| +ProcessDataPhaseSnapshot::~ProcessDataPhaseSnapshot() { |
| +} |
| + |
| +//------------------------------------------------------------------------------ |
| +// ProcessDataPhaseSnapshot |
| ProcessDataSnapshot::ProcessDataSnapshot() |
| #if !defined(OS_NACL) |