| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/tracked_objects.h" | 5 #include "base/tracked_objects.h" |
| 6 | 6 |
| 7 #include <limits.h> | 7 #include <limits.h> |
| 8 #include <stdlib.h> | 8 #include <stdlib.h> |
| 9 | 9 |
| 10 #include "base/atomicops.h" | 10 #include "base/atomicops.h" |
| 11 #include "base/base_switches.h" | 11 #include "base/base_switches.h" |
| 12 #include "base/command_line.h" | 12 #include "base/command_line.h" |
| 13 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
| 14 #include "base/debug/leak_annotations.h" | 14 #include "base/debug/leak_annotations.h" |
| 15 #include "base/logging.h" | 15 #include "base/logging.h" |
| 16 #include "base/process/process_handle.h" | 16 #include "base/process/process_handle.h" |
| 17 #include "base/profiler/alternate_timer.h" | 17 #include "base/profiler/alternate_timer.h" |
| 18 #include "base/stl_util.h" |
| 18 #include "base/strings/stringprintf.h" | 19 #include "base/strings/stringprintf.h" |
| 19 #include "base/third_party/valgrind/memcheck.h" | 20 #include "base/third_party/valgrind/memcheck.h" |
| 20 #include "base/tracking_info.h" | 21 #include "base/tracking_info.h" |
| 21 | 22 |
| 22 using base::TimeDelta; | 23 using base::TimeDelta; |
| 23 | 24 |
| 24 namespace base { | 25 namespace base { |
| 25 class TimeDelta; | 26 class TimeDelta; |
| 26 } | 27 } |
| 27 | 28 |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 86 } | 87 } |
| 87 return current_timing_enabled == ENABLED_TIMING; | 88 return current_timing_enabled == ENABLED_TIMING; |
| 88 } | 89 } |
| 89 | 90 |
| 90 } // namespace | 91 } // namespace |
| 91 | 92 |
| 92 //------------------------------------------------------------------------------ | 93 //------------------------------------------------------------------------------ |
| 93 // DeathData tallies durations when a death takes place. | 94 // DeathData tallies durations when a death takes place. |
| 94 | 95 |
| 95 DeathData::DeathData() { | 96 DeathData::DeathData() { |
| 96 Clear(); | 97 count_ = 0; |
| 98 sample_probability_count_ = 0; |
| 99 run_duration_sum_ = 0; |
| 100 run_duration_max_ = 0; |
| 101 run_duration_sample_ = 0; |
| 102 queue_duration_sum_ = 0; |
| 103 queue_duration_max_ = 0; |
| 104 queue_duration_sample_ = 0; |
| 105 last_phase_snapshot_ = nullptr; |
| 97 } | 106 } |
| 98 | 107 |
| 99 DeathData::DeathData(int count) { | 108 DeathData::DeathData(int count) : DeathData() { |
| 100 Clear(); | |
| 101 count_ = count; | 109 count_ = count; |
| 102 } | 110 } |
| 103 | 111 |
| 112 DeathData::~DeathData() { |
| 113 while (last_phase_snapshot_) { |
| 114 DeathDataPhaseSnapshot* snapshot = last_phase_snapshot_; |
| 115 last_phase_snapshot_ = snapshot->prev; |
| 116 delete snapshot; |
| 117 } |
| 118 } |
| 119 |
| 104 // TODO(jar): I need to see if this macro to optimize branching is worth using. | 120 // TODO(jar): I need to see if this macro to optimize branching is worth using. |
| 105 // | 121 // |
| 106 // This macro has no branching, so it is surely fast, and is equivalent to: | 122 // This macro has no branching, so it is surely fast, and is equivalent to: |
| 107 // if (assign_it) | 123 // if (assign_it) |
| 108 // target = source; | 124 // target = source; |
| 109 // We use a macro rather than a template to force this to inline. | 125 // We use a macro rather than a template to force this to inline. |
| 110 // Related code for calculating max is discussed on the web. | 126 // Related code for calculating max is discussed on the web. |
| 111 #define CONDITIONAL_ASSIGN(assign_it, target, source) \ | 127 #define CONDITIONAL_ASSIGN(assign_it, target, source) \ |
| 112 ((target) ^= ((target) ^ (source)) & -static_cast<int32>(assign_it)) | 128 ((target) ^= ((target) ^ (source)) & -static_cast<int32>(assign_it)) |
| 113 | 129 |
| 114 void DeathData::RecordDeath(const int32 queue_duration, | 130 void DeathData::RecordDeath(const int32 queue_duration, |
| 115 const int32 run_duration, | 131 const int32 run_duration, |
| 116 const uint32 random_number) { | 132 const uint32 random_number) { |
| 117 // We'll just clamp at INT_MAX, but we should note this in the UI as such. | 133 // We'll just clamp at INT_MAX, but we should note this in the UI as such. |
| 118 if (count_ < INT_MAX) | 134 if (count_ < INT_MAX) |
| 119 ++count_; | 135 ++count_; |
| 136 if (sample_probability_count_ < INT_MAX) |
| 137 ++sample_probability_count_; |
| 120 queue_duration_sum_ += queue_duration; | 138 queue_duration_sum_ += queue_duration; |
| 121 run_duration_sum_ += run_duration; | 139 run_duration_sum_ += run_duration; |
| 122 | 140 |
| 123 if (queue_duration_max_ < queue_duration) | 141 if (queue_duration_max_ < queue_duration) |
| 124 queue_duration_max_ = queue_duration; | 142 queue_duration_max_ = queue_duration; |
| 125 if (run_duration_max_ < run_duration) | 143 if (run_duration_max_ < run_duration) |
| 126 run_duration_max_ = run_duration; | 144 run_duration_max_ = run_duration; |
| 127 | 145 |
| 128 // Take a uniformly distributed sample over all durations ever supplied. | 146 // Take a uniformly distributed sample over all durations ever supplied during |
| 129 // The probability that we (instead) use this new sample is 1/count_. This | 147 // currrent profiling phase. |
| 130 // results in a completely uniform selection of the sample (at least when we | 148 // The probability that we (instead) use this new sample is |
| 131 // don't clamp count_... but that should be inconsequentially likely). | 149 // 1/sample_probability_count_. This results in a completely uniform selection |
| 132 // We ignore the fact that we correlated our selection of a sample to the run | 150 // of the sample (at least when we don't clamp sample_probability_count_... |
| 133 // and queue times (i.e., we used them to generate random_number). | 151 // but that should be inconsequentially likely). We ignore the fact that we |
| 134 CHECK_GT(count_, 0); | 152 // correlated our selection of a sample to the run and queue times (i.e., we |
| 135 if (0 == (random_number % count_)) { | 153 // used them to generate random_number). |
| 154 CHECK_GT(sample_probability_count_, 0); |
| 155 if (0 == (random_number % sample_probability_count_)) { |
| 136 queue_duration_sample_ = queue_duration; | 156 queue_duration_sample_ = queue_duration; |
| 137 run_duration_sample_ = run_duration; | 157 run_duration_sample_ = run_duration; |
| 138 } | 158 } |
| 139 } | 159 } |
| 140 | 160 |
| 141 int DeathData::count() const { return count_; } | 161 int DeathData::count() const { return count_; } |
| 142 | 162 |
| 143 int32 DeathData::run_duration_sum() const { return run_duration_sum_; } | 163 int32 DeathData::run_duration_sum() const { return run_duration_sum_; } |
| 144 | 164 |
| 145 int32 DeathData::run_duration_max() const { return run_duration_max_; } | 165 int32 DeathData::run_duration_max() const { return run_duration_max_; } |
| 146 | 166 |
| 147 int32 DeathData::run_duration_sample() const { | 167 int32 DeathData::run_duration_sample() const { |
| 148 return run_duration_sample_; | 168 return run_duration_sample_; |
| 149 } | 169 } |
| 150 | 170 |
| 151 int32 DeathData::queue_duration_sum() const { | 171 int32 DeathData::queue_duration_sum() const { |
| 152 return queue_duration_sum_; | 172 return queue_duration_sum_; |
| 153 } | 173 } |
| 154 | 174 |
| 155 int32 DeathData::queue_duration_max() const { | 175 int32 DeathData::queue_duration_max() const { |
| 156 return queue_duration_max_; | 176 return queue_duration_max_; |
| 157 } | 177 } |
| 158 | 178 |
| 159 int32 DeathData::queue_duration_sample() const { | 179 int32 DeathData::queue_duration_sample() const { |
| 160 return queue_duration_sample_; | 180 return queue_duration_sample_; |
| 161 } | 181 } |
| 162 | 182 |
| 163 void DeathData::Clear() { | 183 DeathDataPhaseSnapshot* DeathData::last_phase_snapshot() const { |
| 164 count_ = 0; | 184 return last_phase_snapshot_; |
| 165 run_duration_sum_ = 0; | 185 } |
| 186 |
| 187 void DeathData::OnProfilingPhaseCompleted(int profiling_phase) { |
| 188 // Snapshotting and storing current state. |
| 189 last_phase_snapshot_ = new DeathDataPhaseSnapshot(profiling_phase, *this); |
| 190 |
| 191 // Not touching fields for which a delta can be computed by comparing with a |
| 192 // snapshot from previos phase. Resetting other fields. Sample values will be |
| 193 // reset upon next death recording because sample_probability_count_ is set to |
| 194 // 0. |
| 195 sample_probability_count_ = 0; |
| 166 run_duration_max_ = 0; | 196 run_duration_max_ = 0; |
| 167 run_duration_sample_ = 0; | |
| 168 queue_duration_sum_ = 0; | |
| 169 queue_duration_max_ = 0; | 197 queue_duration_max_ = 0; |
| 170 queue_duration_sample_ = 0; | |
| 171 } | 198 } |
| 172 | 199 |
| 173 //------------------------------------------------------------------------------ | 200 //------------------------------------------------------------------------------ |
| 174 DeathDataSnapshot::DeathDataSnapshot() | 201 DeathDataSnapshot::DeathDataSnapshot() |
| 175 : count(-1), | 202 : count(-1), |
| 176 run_duration_sum(-1), | 203 run_duration_sum(-1), |
| 177 run_duration_max(-1), | 204 run_duration_max(-1), |
| 178 run_duration_sample(-1), | 205 run_duration_sample(-1), |
| 179 queue_duration_sum(-1), | 206 queue_duration_sum(-1), |
| 180 queue_duration_max(-1), | 207 queue_duration_max(-1), |
| 181 queue_duration_sample(-1) { | 208 queue_duration_sample(-1) { |
| 182 } | 209 } |
| 183 | 210 |
| 184 DeathDataSnapshot::DeathDataSnapshot( | 211 DeathDataSnapshot::DeathDataSnapshot(const DeathData& death_data) |
| 185 const tracked_objects::DeathData& death_data) | |
| 186 : count(death_data.count()), | 212 : count(death_data.count()), |
| 187 run_duration_sum(death_data.run_duration_sum()), | 213 run_duration_sum(death_data.run_duration_sum()), |
| 188 run_duration_max(death_data.run_duration_max()), | 214 run_duration_max(death_data.run_duration_max()), |
| 189 run_duration_sample(death_data.run_duration_sample()), | 215 run_duration_sample(death_data.run_duration_sample()), |
| 190 queue_duration_sum(death_data.queue_duration_sum()), | 216 queue_duration_sum(death_data.queue_duration_sum()), |
| 191 queue_duration_max(death_data.queue_duration_max()), | 217 queue_duration_max(death_data.queue_duration_max()), |
| 192 queue_duration_sample(death_data.queue_duration_sample()) { | 218 queue_duration_sample(death_data.queue_duration_sample()) { |
| 193 } | 219 } |
| 194 | 220 |
| 195 DeathDataSnapshot::~DeathDataSnapshot() { | 221 DeathDataSnapshot::~DeathDataSnapshot() { |
| 196 } | 222 } |
| 197 | 223 |
| 224 void DeathDataSnapshot::CalculateDelta(const DeathDataSnapshot& older) { |
| 225 count -= older.count; |
| 226 run_duration_sum -= older.run_duration_sum; |
| 227 queue_duration_sum -= older.queue_duration_sum; |
| 228 } |
| 229 |
| 198 //------------------------------------------------------------------------------ | 230 //------------------------------------------------------------------------------ |
| 199 BirthOnThread::BirthOnThread(const Location& location, | 231 BirthOnThread::BirthOnThread(const Location& location, |
| 200 const ThreadData& current) | 232 const ThreadData& current) |
| 201 : location_(location), | 233 : location_(location), |
| 202 birth_thread_(¤t) { | 234 birth_thread_(¤t) { |
| 203 } | 235 } |
| 204 | 236 |
| 205 //------------------------------------------------------------------------------ | 237 //------------------------------------------------------------------------------ |
| 206 BirthOnThreadSnapshot::BirthOnThreadSnapshot() { | 238 BirthOnThreadSnapshot::BirthOnThreadSnapshot() { |
| 207 } | 239 } |
| 208 | 240 |
| 209 BirthOnThreadSnapshot::BirthOnThreadSnapshot( | 241 BirthOnThreadSnapshot::BirthOnThreadSnapshot(const BirthOnThread& birth) |
| 210 const tracked_objects::BirthOnThread& birth) | |
| 211 : location(birth.location()), | 242 : location(birth.location()), |
| 212 thread_name(birth.birth_thread()->thread_name()) { | 243 thread_name(birth.birth_thread()->thread_name()) { |
| 213 } | 244 } |
| 214 | 245 |
| 215 BirthOnThreadSnapshot::~BirthOnThreadSnapshot() { | 246 BirthOnThreadSnapshot::~BirthOnThreadSnapshot() { |
| 216 } | 247 } |
| 217 | 248 |
| 218 //------------------------------------------------------------------------------ | 249 //------------------------------------------------------------------------------ |
| 219 Births::Births(const Location& location, const ThreadData& current) | 250 Births::Births(const Location& location, const ThreadData& current) |
| 220 : BirthOnThread(location, current), | 251 : BirthOnThread(location, current), |
| (...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 378 return; | 409 return; |
| 379 } | 410 } |
| 380 // We must NOT do any allocations during this callback. | 411 // We must NOT do any allocations during this callback. |
| 381 // Using the simple linked lists avoids all allocations. | 412 // Using the simple linked lists avoids all allocations. |
| 382 DCHECK_EQ(this->next_retired_worker_, reinterpret_cast<ThreadData*>(NULL)); | 413 DCHECK_EQ(this->next_retired_worker_, reinterpret_cast<ThreadData*>(NULL)); |
| 383 this->next_retired_worker_ = first_retired_worker_; | 414 this->next_retired_worker_ = first_retired_worker_; |
| 384 first_retired_worker_ = this; | 415 first_retired_worker_ = this; |
| 385 } | 416 } |
| 386 | 417 |
| 387 // static | 418 // static |
| 388 void ThreadData::Snapshot(ProcessDataSnapshot* process_data_snapshot) { | 419 void ThreadData::Snapshot(int current_profiling_phase, |
| 389 ThreadData::SnapshotCurrentPhase( | 420 ProcessDataSnapshot* process_data_snapshot) { |
| 390 &process_data_snapshot->phased_process_data_snapshots[0]); | 421 BirthCountMap birth_counts; |
| 422 |
| 423 // Get an unchanging copy of a ThreadData list. |
| 424 ThreadData* my_list = ThreadData::first(); |
| 425 |
| 426 // Gather data serially. |
| 427 // This hackish approach *can* get some slighly corrupt tallies, as we are |
| 428 // grabbing values without the protection of a lock, but it has the advantage |
| 429 // of working even with threads that don't have message loops. If a user |
| 430 // sees any strangeness, they can always just run their stats gathering a |
| 431 // second time. |
| 432 for (ThreadData* thread_data = my_list; thread_data; |
| 433 thread_data = thread_data->next()) { |
| 434 thread_data->SnapshotExecutedTasks( |
| 435 current_profiling_phase, |
| 436 &process_data_snapshot->phased_process_data_snapshots, &birth_counts); |
| 437 } |
| 438 |
| 439 // Add births that are still active -- i.e. objects that have tallied a birth, |
| 440 // but have not yet tallied a matching death, and hence must be either |
| 441 // running, queued up, or being held in limbo for future posting. |
| 442 for (const auto& birth_count : birth_counts) { |
| 443 if (birth_count.second > 0) { |
| 444 process_data_snapshot |
| 445 ->phased_process_data_snapshots[current_profiling_phase] |
| 446 .tasks.push_back(TaskSnapshot( |
| 447 BirthOnThreadSnapshot(*birth_count.first), |
| 448 DeathDataSnapshot(DeathData(birth_count.second)), "Still_Alive")); |
| 449 } |
| 450 } |
| 451 } |
| 452 |
| 453 // static |
| 454 void ThreadData::OnProfilingPhaseCompleted(int profiling_phase) { |
| 455 // Get an unchanging copy of a ThreadData list. |
| 456 ThreadData* my_list = ThreadData::first(); |
| 457 |
| 458 // Add snapshots for all death datas in all threads serially. |
| 459 // This hackish approach *can* get some slighly corrupt tallies, as we are |
| 460 // grabbing values without the protection of a lock, but it has the advantage |
| 461 // of working even with threads that don't have message loops. Any corruption |
| 462 // shouldn't cause "cascading damage" to anything else (in later phases). |
| 463 for (ThreadData* thread_data = my_list; thread_data; |
| 464 thread_data = thread_data->next()) { |
| 465 thread_data->OnProfilingPhaseCompletionOnThread(profiling_phase); |
| 466 } |
| 391 } | 467 } |
| 392 | 468 |
| 393 Births* ThreadData::TallyABirth(const Location& location) { | 469 Births* ThreadData::TallyABirth(const Location& location) { |
| 394 BirthMap::iterator it = birth_map_.find(location); | 470 BirthMap::iterator it = birth_map_.find(location); |
| 395 Births* child; | 471 Births* child; |
| 396 if (it != birth_map_.end()) { | 472 if (it != birth_map_.end()) { |
| 397 child = it->second; | 473 child = it->second; |
| 398 child->RecordBirth(); | 474 child->RecordBirth(); |
| 399 } else { | 475 } else { |
| 400 child = new Births(location, *this); // Leak this. | 476 child = new Births(location, *this); // Leak this. |
| (...skipping 11 matching lines...) Expand all Loading... |
| 412 // Lock since the map may get relocated now, and other threads sometimes | 488 // Lock since the map may get relocated now, and other threads sometimes |
| 413 // snapshot it (but they lock before copying it). | 489 // snapshot it (but they lock before copying it). |
| 414 base::AutoLock lock(map_lock_); | 490 base::AutoLock lock(map_lock_); |
| 415 parent_child_set_.insert(pair); | 491 parent_child_set_.insert(pair); |
| 416 } | 492 } |
| 417 } | 493 } |
| 418 | 494 |
| 419 return child; | 495 return child; |
| 420 } | 496 } |
| 421 | 497 |
| 422 void ThreadData::TallyADeath(const Births& birth, | 498 void ThreadData::TallyADeath(const Births& births, |
| 423 int32 queue_duration, | 499 int32 queue_duration, |
| 424 const TaskStopwatch& stopwatch) { | 500 const TaskStopwatch& stopwatch) { |
| 425 int32 run_duration = stopwatch.RunDurationMs(); | 501 int32 run_duration = stopwatch.RunDurationMs(); |
| 426 | 502 |
| 427 // Stir in some randomness, plus add constant in case durations are zero. | 503 // Stir in some randomness, plus add constant in case durations are zero. |
| 428 const uint32 kSomePrimeNumber = 2147483647; | 504 const uint32 kSomePrimeNumber = 2147483647; |
| 429 random_number_ += queue_duration + run_duration + kSomePrimeNumber; | 505 random_number_ += queue_duration + run_duration + kSomePrimeNumber; |
| 430 // An address is going to have some randomness to it as well ;-). | 506 // An address is going to have some randomness to it as well ;-). |
| 431 random_number_ ^= static_cast<uint32>(&birth - reinterpret_cast<Births*>(0)); | 507 random_number_ ^= static_cast<uint32>(&births - reinterpret_cast<Births*>(0)); |
| 432 | 508 |
| 433 // We don't have queue durations without OS timer. OS timer is automatically | 509 // 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 | 510 // 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 | 511 // queue times are invalid, unless it was explicitly said that we can trust |
| 436 // the alternate timer. | 512 // the alternate timer. |
| 437 if (kAllowAlternateTimeSourceHandling && | 513 if (kAllowAlternateTimeSourceHandling && |
| 438 now_function_ && | 514 now_function_ && |
| 439 !now_function_is_time_) { | 515 !now_function_is_time_) { |
| 440 queue_duration = 0; | 516 queue_duration = 0; |
| 441 } | 517 } |
| 442 | 518 |
| 443 DeathMap::iterator it = death_map_.find(&birth); | 519 DeathMap::iterator it = death_map_.find(&births); |
| 444 DeathData* death_data; | 520 DeathData* death_data; |
| 445 if (it != death_map_.end()) { | 521 if (it != death_map_.end()) { |
| 446 death_data = &it->second; | 522 death_data = &it->second; |
| 447 } else { | 523 } else { |
| 448 base::AutoLock lock(map_lock_); // Lock as the map may get relocated now. | 524 base::AutoLock lock(map_lock_); // Lock as the map may get relocated now. |
| 449 death_data = &death_map_[&birth]; | 525 death_data = &death_map_[&births]; |
| 450 } // Release lock ASAP. | 526 } // Release lock ASAP. |
| 451 death_data->RecordDeath(queue_duration, run_duration, random_number_); | 527 death_data->RecordDeath(queue_duration, run_duration, random_number_); |
| 452 | 528 |
| 453 if (!kTrackParentChildLinks) | 529 if (!kTrackParentChildLinks) |
| 454 return; | 530 return; |
| 455 if (!parent_stack_.empty()) { // We might get turned off. | 531 if (!parent_stack_.empty()) { // We might get turned off. |
| 456 DCHECK_EQ(parent_stack_.top(), &birth); | 532 DCHECK_EQ(parent_stack_.top(), &births); |
| 457 parent_stack_.pop(); | 533 parent_stack_.pop(); |
| 458 } | 534 } |
| 459 } | 535 } |
| 460 | 536 |
| 461 // static | 537 // static |
| 462 Births* ThreadData::TallyABirthIfActive(const Location& location) { | 538 Births* ThreadData::TallyABirthIfActive(const Location& location) { |
| 463 if (!TrackingStatus()) | 539 if (!TrackingStatus()) |
| 464 return NULL; | 540 return NULL; |
| 465 ThreadData* current_thread_data = Get(); | 541 ThreadData* current_thread_data = Get(); |
| 466 if (!current_thread_data) | 542 if (!current_thread_data) |
| 467 return NULL; | 543 return NULL; |
| 468 return current_thread_data->TallyABirth(location); | 544 return current_thread_data->TallyABirth(location); |
| 469 } | 545 } |
| 470 | 546 |
| 471 // static | 547 // static |
| 472 void ThreadData::TallyRunOnNamedThreadIfTracking( | 548 void ThreadData::TallyRunOnNamedThreadIfTracking( |
| 473 const base::TrackingInfo& completed_task, | 549 const base::TrackingInfo& completed_task, |
| 474 const TaskStopwatch& stopwatch) { | 550 const TaskStopwatch& stopwatch) { |
| 475 // Even if we have been DEACTIVATED, we will process any pending births so | 551 // Even if we have been DEACTIVATED, we will process any pending births so |
| 476 // that our data structures (which counted the outstanding births) remain | 552 // that our data structures (which counted the outstanding births) remain |
| 477 // consistent. | 553 // consistent. |
| 478 const Births* birth = completed_task.birth_tally; | 554 const Births* births = completed_task.birth_tally; |
| 479 if (!birth) | 555 if (!births) |
| 480 return; | 556 return; |
| 481 ThreadData* current_thread_data = stopwatch.GetThreadData(); | 557 ThreadData* current_thread_data = stopwatch.GetThreadData(); |
| 482 if (!current_thread_data) | 558 if (!current_thread_data) |
| 483 return; | 559 return; |
| 484 | 560 |
| 485 // Watch out for a race where status_ is changing, and hence one or both | 561 // 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 | 562 // 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 | 563 // 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 | 564 // 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. | 565 // use a default zero duration when we can't calculate a true value. |
| 490 TrackedTime start_of_run = stopwatch.StartTime(); | 566 TrackedTime start_of_run = stopwatch.StartTime(); |
| 491 int32 queue_duration = 0; | 567 int32 queue_duration = 0; |
| 492 if (!start_of_run.is_null()) { | 568 if (!start_of_run.is_null()) { |
| 493 queue_duration = (start_of_run - completed_task.EffectiveTimePosted()) | 569 queue_duration = (start_of_run - completed_task.EffectiveTimePosted()) |
| 494 .InMilliseconds(); | 570 .InMilliseconds(); |
| 495 } | 571 } |
| 496 current_thread_data->TallyADeath(*birth, queue_duration, stopwatch); | 572 current_thread_data->TallyADeath(*births, queue_duration, stopwatch); |
| 497 } | 573 } |
| 498 | 574 |
| 499 // static | 575 // static |
| 500 void ThreadData::TallyRunOnWorkerThreadIfTracking( | 576 void ThreadData::TallyRunOnWorkerThreadIfTracking( |
| 501 const Births* birth, | 577 const Births* births, |
| 502 const TrackedTime& time_posted, | 578 const TrackedTime& time_posted, |
| 503 const TaskStopwatch& stopwatch) { | 579 const TaskStopwatch& stopwatch) { |
| 504 // Even if we have been DEACTIVATED, we will process any pending births so | 580 // Even if we have been DEACTIVATED, we will process any pending births so |
| 505 // that our data structures (which counted the outstanding births) remain | 581 // that our data structures (which counted the outstanding births) remain |
| 506 // consistent. | 582 // consistent. |
| 507 if (!birth) | 583 if (!births) |
| 508 return; | 584 return; |
| 509 | 585 |
| 510 // TODO(jar): Support the option to coalesce all worker-thread activity under | 586 // 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 | 587 // one ThreadData instance that uses locks to protect *all* access. This will |
| 512 // reduce memory (making it provably bounded), but run incrementally slower | 588 // reduce memory (making it provably bounded), but run incrementally slower |
| 513 // (since we'll use locks on TallyABirth and TallyADeath). The good news is | 589 // (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, | 590 // 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 | 591 // 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 | 592 // 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 | 593 // generally longer, and hence the cost of the lock may perchance be amortized |
| 518 // over the long task's lifetime. | 594 // over the long task's lifetime. |
| 519 ThreadData* current_thread_data = stopwatch.GetThreadData(); | 595 ThreadData* current_thread_data = stopwatch.GetThreadData(); |
| 520 if (!current_thread_data) | 596 if (!current_thread_data) |
| 521 return; | 597 return; |
| 522 | 598 |
| 523 TrackedTime start_of_run = stopwatch.StartTime(); | 599 TrackedTime start_of_run = stopwatch.StartTime(); |
| 524 int32 queue_duration = 0; | 600 int32 queue_duration = 0; |
| 525 if (!start_of_run.is_null()) { | 601 if (!start_of_run.is_null()) { |
| 526 queue_duration = (start_of_run - time_posted).InMilliseconds(); | 602 queue_duration = (start_of_run - time_posted).InMilliseconds(); |
| 527 } | 603 } |
| 528 current_thread_data->TallyADeath(*birth, queue_duration, stopwatch); | 604 current_thread_data->TallyADeath(*births, queue_duration, stopwatch); |
| 529 } | 605 } |
| 530 | 606 |
| 531 // static | 607 // static |
| 532 void ThreadData::TallyRunInAScopedRegionIfTracking( | 608 void ThreadData::TallyRunInAScopedRegionIfTracking( |
| 533 const Births* birth, | 609 const Births* births, |
| 534 const TaskStopwatch& stopwatch) { | 610 const TaskStopwatch& stopwatch) { |
| 535 // Even if we have been DEACTIVATED, we will process any pending births so | 611 // Even if we have been DEACTIVATED, we will process any pending births so |
| 536 // that our data structures (which counted the outstanding births) remain | 612 // that our data structures (which counted the outstanding births) remain |
| 537 // consistent. | 613 // consistent. |
| 538 if (!birth) | 614 if (!births) |
| 539 return; | 615 return; |
| 540 | 616 |
| 541 ThreadData* current_thread_data = stopwatch.GetThreadData(); | 617 ThreadData* current_thread_data = stopwatch.GetThreadData(); |
| 542 if (!current_thread_data) | 618 if (!current_thread_data) |
| 543 return; | 619 return; |
| 544 | 620 |
| 545 int32 queue_duration = 0; | 621 int32 queue_duration = 0; |
| 546 current_thread_data->TallyADeath(*birth, queue_duration, stopwatch); | 622 current_thread_data->TallyADeath(*births, queue_duration, stopwatch); |
| 547 } | |
| 548 | |
| 549 // static | |
| 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(); | |
| 555 | |
| 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 | |
| 561 // second time. | |
| 562 for (ThreadData* thread_data = my_list; | |
| 563 thread_data; | |
| 564 thread_data = thread_data->next()) { | |
| 565 thread_data->SnapshotExecutedTasks(process_data_phase, birth_counts); | |
| 566 } | |
| 567 } | |
| 568 | |
| 569 // static | |
| 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); | |
| 577 | |
| 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")); | |
| 585 } | |
| 586 } | |
| 587 } | 623 } |
| 588 | 624 |
| 589 void ThreadData::SnapshotExecutedTasks( | 625 void ThreadData::SnapshotExecutedTasks( |
| 590 ProcessDataPhaseSnapshot* process_data_phase, | 626 int current_profiling_phase, |
| 627 PhasedProcessDataSnapshotMap* phased_process_data_snapshots, |
| 591 BirthCountMap* birth_counts) { | 628 BirthCountMap* birth_counts) { |
| 592 // Get copy of data, so that the data will not change during the iterations | 629 // Get copy of data, so that the data will not change during the iterations |
| 593 // and processing. | 630 // and processing. |
| 594 ThreadData::BirthMap birth_map; | 631 BirthMap birth_map; |
| 595 ThreadData::DeathMap death_map; | 632 DeathsSnapshot deaths; |
| 596 ThreadData::ParentChildSet parent_child_set; | 633 ParentChildSet parent_child_set; |
| 597 SnapshotMaps(&birth_map, &death_map, &parent_child_set); | 634 SnapshotMaps(current_profiling_phase, &birth_map, &deaths, &parent_child_set); |
| 598 | |
| 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(); | |
| 603 } | |
| 604 | 635 |
| 605 for (const auto& birth : birth_map) { | 636 for (const auto& birth : birth_map) { |
| 606 (*birth_counts)[birth.second] += birth.second->birth_count(); | 637 (*birth_counts)[birth.second] += birth.second->birth_count(); |
| 607 } | 638 } |
| 608 | 639 |
| 609 if (!kTrackParentChildLinks) | 640 for (const auto& death : deaths) { |
| 610 return; | 641 (*birth_counts)[death.first] -= death.first->birth_count(); |
| 611 | 642 |
| 612 for (const auto& parent_child : parent_child_set) { | 643 for (const DeathDataPhaseSnapshot* phase = &death.second; phase; |
| 613 process_data_phase->descendants.push_back( | 644 phase = phase->prev) { |
| 614 ParentChildPairSnapshot(parent_child)); | 645 DeathDataSnapshot death_data = phase->death_data; |
| 646 |
| 647 if (phase->prev) |
| 648 death_data.CalculateDelta(phase->prev->death_data); |
| 649 |
| 650 if (death_data.count > 0) { |
| 651 (*phased_process_data_snapshots)[phase->profiling_phase] |
| 652 .tasks.push_back(TaskSnapshot(BirthOnThreadSnapshot(*death.first), |
| 653 death_data, thread_name())); |
| 654 } |
| 655 } |
| 615 } | 656 } |
| 616 } | 657 } |
| 617 | 658 |
| 618 // This may be called from another thread. | 659 // This may be called from another thread. |
| 619 void ThreadData::SnapshotMaps(BirthMap* birth_map, | 660 void ThreadData::SnapshotMaps(int profiling_phase, |
| 620 DeathMap* death_map, | 661 BirthMap* birth_map, |
| 662 DeathsSnapshot* deaths, |
| 621 ParentChildSet* parent_child_set) { | 663 ParentChildSet* parent_child_set) { |
| 622 base::AutoLock lock(map_lock_); | 664 base::AutoLock lock(map_lock_); |
| 665 |
| 623 for (const auto& birth : birth_map_) | 666 for (const auto& birth : birth_map_) |
| 624 (*birth_map)[birth.first] = birth.second; | 667 (*birth_map)[birth.first] = birth.second; |
| 625 for (const auto& death : death_map_) | 668 |
| 626 (*death_map)[death.first] = death.second; | 669 for (const auto& death : death_map_) { |
| 670 deaths->push_back(DeathsSnapshot::value_type( |
| 671 death.first, DeathDataPhaseSnapshot(profiling_phase, death.second))); |
| 672 } |
| 627 | 673 |
| 628 if (!kTrackParentChildLinks) | 674 if (!kTrackParentChildLinks) |
| 629 return; | 675 return; |
| 630 | 676 |
| 631 for (const auto& parent_child : parent_child_set_) | 677 for (const auto& parent_child : parent_child_set_) |
| 632 parent_child_set->insert(parent_child); | 678 parent_child_set->insert(parent_child); |
| 633 } | 679 } |
| 634 | 680 |
| 681 void ThreadData::OnProfilingPhaseCompletionOnThread(int profiling_phase) { |
| 682 base::AutoLock lock(map_lock_); |
| 683 |
| 684 for (auto& death : death_map_) { |
| 685 death.second.OnProfilingPhaseCompleted(profiling_phase); |
| 686 } |
| 687 } |
| 688 |
| 635 static void OptionallyInitializeAlternateTimer() { | 689 static void OptionallyInitializeAlternateTimer() { |
| 636 NowFunction* alternate_time_source = GetAlternateTimeSource(); | 690 NowFunction* alternate_time_source = GetAlternateTimeSource(); |
| 637 if (alternate_time_source) | 691 if (alternate_time_source) |
| 638 ThreadData::SetAlternateTimeSource(alternate_time_source); | 692 ThreadData::SetAlternateTimeSource(alternate_time_source); |
| 639 } | 693 } |
| 640 | 694 |
| 641 bool ThreadData::Initialize() { | 695 bool ThreadData::Initialize() { |
| 642 if (status_ >= DEACTIVATED) | 696 if (status_ >= DEACTIVATED) |
| 643 return true; // Someone else did the initialization. | 697 return true; // Someone else did the initialization. |
| 644 // Due to racy lazy initialization in tests, we'll need to recheck status_ | 698 // Due to racy lazy initialization in tests, we'll need to recheck status_ |
| (...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 902 | 956 |
| 903 ThreadData* TaskStopwatch::GetThreadData() const { | 957 ThreadData* TaskStopwatch::GetThreadData() const { |
| 904 #if DCHECK_IS_ON() | 958 #if DCHECK_IS_ON() |
| 905 DCHECK(state_ != CREATED); | 959 DCHECK(state_ != CREATED); |
| 906 #endif | 960 #endif |
| 907 | 961 |
| 908 return current_thread_data_; | 962 return current_thread_data_; |
| 909 } | 963 } |
| 910 | 964 |
| 911 //------------------------------------------------------------------------------ | 965 //------------------------------------------------------------------------------ |
| 966 // DeathDataPhaseSnapshot |
| 967 |
| 968 DeathDataPhaseSnapshot::DeathDataPhaseSnapshot(int profiling_phase, |
| 969 const DeathData& death_data) |
| 970 : profiling_phase(profiling_phase), |
| 971 death_data(death_data), |
| 972 prev(death_data.last_phase_snapshot()) { |
| 973 } |
| 974 |
| 975 //------------------------------------------------------------------------------ |
| 976 // TaskSnapshot |
| 977 |
| 912 TaskSnapshot::TaskSnapshot() { | 978 TaskSnapshot::TaskSnapshot() { |
| 913 } | 979 } |
| 914 | 980 |
| 915 TaskSnapshot::TaskSnapshot(const BirthOnThread& birth, | 981 TaskSnapshot::TaskSnapshot(const BirthOnThreadSnapshot& birth, |
| 916 const DeathData& death_data, | 982 const DeathDataSnapshot& death_data, |
| 917 const std::string& death_thread_name) | 983 const std::string& death_thread_name) |
| 918 : birth(birth), | 984 : birth(birth), |
| 919 death_data(death_data), | 985 death_data(death_data), |
| 920 death_thread_name(death_thread_name) { | 986 death_thread_name(death_thread_name) { |
| 921 } | 987 } |
| 922 | 988 |
| 923 TaskSnapshot::~TaskSnapshot() { | 989 TaskSnapshot::~TaskSnapshot() { |
| 924 } | 990 } |
| 925 | 991 |
| 926 //------------------------------------------------------------------------------ | 992 //------------------------------------------------------------------------------ |
| (...skipping 28 matching lines...) Expand all Loading... |
| 955 : process_id(base::GetCurrentProcId()) { | 1021 : process_id(base::GetCurrentProcId()) { |
| 956 #else | 1022 #else |
| 957 : process_id(base::kNullProcessId) { | 1023 : process_id(base::kNullProcessId) { |
| 958 #endif | 1024 #endif |
| 959 } | 1025 } |
| 960 | 1026 |
| 961 ProcessDataSnapshot::~ProcessDataSnapshot() { | 1027 ProcessDataSnapshot::~ProcessDataSnapshot() { |
| 962 } | 1028 } |
| 963 | 1029 |
| 964 } // namespace tracked_objects | 1030 } // namespace tracked_objects |
| OLD | NEW |