| 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 <math.h> | 7 #include <math.h> |
| 8 | 8 |
| 9 #include "base/format_macros.h" | 9 #include "base/format_macros.h" |
| 10 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
| 12 #include "base/process_util.h" |
| 11 #include "base/profiler/alternate_timer.h" | 13 #include "base/profiler/alternate_timer.h" |
| 12 #include "base/stringprintf.h" | 14 #include "base/stringprintf.h" |
| 13 #include "base/third_party/valgrind/memcheck.h" | 15 #include "base/third_party/valgrind/memcheck.h" |
| 14 #include "base/threading/thread_restrictions.h" | 16 #include "base/threading/thread_restrictions.h" |
| 17 #include "base/port.h" |
| 18 #include "base/values.h" |
| 15 #include "build/build_config.h" | 19 #include "build/build_config.h" |
| 16 #include "base/port.h" | |
| 17 | 20 |
| 21 using base::DictionaryValue; |
| 18 using base::TimeDelta; | 22 using base::TimeDelta; |
| 23 using base::Value; |
| 19 | 24 |
| 20 namespace tracked_objects { | 25 namespace tracked_objects { |
| 21 | 26 |
| 22 namespace { | 27 namespace { |
| 23 | 28 |
| 24 // Flag to compile out almost all of the task tracking code. | 29 // Flag to compile out almost all of the task tracking code. |
| 25 const bool kTrackAllTaskObjects = true; | 30 const bool kTrackAllTaskObjects = true; |
| 26 | 31 |
| 27 // Flag to compile out parent-child link recording. | 32 // Flag to compile out parent-child link recording. |
| 28 const bool kTrackParentChildLinks = false; | 33 const bool kTrackParentChildLinks = false; |
| 29 | 34 |
| 30 // When ThreadData is first initialized, should we start in an ACTIVE state to | 35 // When ThreadData is first initialized, should we start in an ACTIVE state to |
| 31 // record all of the startup-time tasks, or should we start up DEACTIVATED, so | 36 // record all of the startup-time tasks, or should we start up DEACTIVATED, so |
| 32 // that we only record after parsing the command line flag --enable-tracking. | 37 // that we only record after parsing the command line flag --enable-tracking. |
| 33 // Note that the flag may force either state, so this really controls only the | 38 // Note that the flag may force either state, so this really controls only the |
| 34 // period of time up until that flag is parsed. If there is no flag seen, then | 39 // period of time up until that flag is parsed. If there is no flag seen, then |
| 35 // this state may prevail for much or all of the process lifetime. | 40 // this state may prevail for much or all of the process lifetime. |
| 36 const ThreadData::Status kInitialStartupState = | 41 const ThreadData::Status kInitialStartupState = |
| 37 ThreadData::PROFILING_CHILDREN_ACTIVE; | 42 ThreadData::PROFILING_CHILDREN_ACTIVE; |
| 38 | 43 |
| 39 // Control whether an alternate time source (Now() function) is supported by | 44 // Control whether an alternate time source (Now() function) is supported by |
| 40 // the ThreadData class. This compile time flag should be set to true if we | 45 // the ThreadData class. This compile time flag should be set to true if we |
| 41 // want other modules (such as a memory allocator, or a thread-specific CPU time | 46 // want other modules (such as a memory allocator, or a thread-specific CPU time |
| 42 // clock) to be able to provide a thread-specific Now() function. Without this | 47 // clock) to be able to provide a thread-specific Now() function. Without this |
| 43 // compile-time flag, the code will only support the wall-clock time. This flag | 48 // compile-time flag, the code will only support the wall-clock time. This flag |
| 44 // can be flipped to efficiently disable this path (if there is a performance | 49 // can be flipped to efficiently disable this path (if there is a performance |
| 45 // problem with its presence). | 50 // problem with its presence). |
| 46 static const bool kAllowAlternateTimeSourceHandling = true; | 51 static const bool kAllowAlternateTimeSourceHandling = true; |
| 52 |
| 47 } // namespace | 53 } // namespace |
| 48 | 54 |
| 49 //------------------------------------------------------------------------------ | 55 //------------------------------------------------------------------------------ |
| 50 // DeathData tallies durations when a death takes place. | 56 // DeathData tallies durations when a death takes place. |
| 51 | 57 |
| 52 DeathData::DeathData() { | 58 DeathData::DeathData() { |
| 53 Clear(); | 59 Clear(); |
| 54 } | 60 } |
| 55 | 61 |
| 56 DeathData::DeathData(int count) { | 62 DeathData::DeathData(int count) { |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 106 } | 112 } |
| 107 | 113 |
| 108 DurationInt DeathData::queue_duration_max() const { | 114 DurationInt DeathData::queue_duration_max() const { |
| 109 return queue_duration_max_; | 115 return queue_duration_max_; |
| 110 } | 116 } |
| 111 | 117 |
| 112 DurationInt DeathData::queue_duration_sample() const { | 118 DurationInt DeathData::queue_duration_sample() const { |
| 113 return queue_duration_sample_; | 119 return queue_duration_sample_; |
| 114 } | 120 } |
| 115 | 121 |
| 116 | |
| 117 base::DictionaryValue* DeathData::ToValue() const { | |
| 118 base::DictionaryValue* dictionary = new base::DictionaryValue; | |
| 119 dictionary->Set("count", base::Value::CreateIntegerValue(count_)); | |
| 120 dictionary->Set("run_ms", | |
| 121 base::Value::CreateIntegerValue(run_duration_sum())); | |
| 122 dictionary->Set("run_ms_max", | |
| 123 base::Value::CreateIntegerValue(run_duration_max())); | |
| 124 dictionary->Set("run_ms_sample", | |
| 125 base::Value::CreateIntegerValue(run_duration_sample())); | |
| 126 dictionary->Set("queue_ms", | |
| 127 base::Value::CreateIntegerValue(queue_duration_sum())); | |
| 128 dictionary->Set("queue_ms_max", | |
| 129 base::Value::CreateIntegerValue(queue_duration_max())); | |
| 130 dictionary->Set("queue_ms_sample", | |
| 131 base::Value::CreateIntegerValue(queue_duration_sample())); | |
| 132 return dictionary; | |
| 133 } | |
| 134 | |
| 135 void DeathData::ResetMax() { | 122 void DeathData::ResetMax() { |
| 136 run_duration_max_ = 0; | 123 run_duration_max_ = 0; |
| 137 queue_duration_max_ = 0; | 124 queue_duration_max_ = 0; |
| 138 } | 125 } |
| 139 | 126 |
| 140 void DeathData::Clear() { | 127 void DeathData::Clear() { |
| 141 count_ = 0; | 128 count_ = 0; |
| 142 run_duration_sum_ = 0; | 129 run_duration_sum_ = 0; |
| 143 run_duration_max_ = 0; | 130 run_duration_max_ = 0; |
| 144 run_duration_sample_ = 0; | 131 run_duration_sample_ = 0; |
| 145 queue_duration_sum_ = 0; | 132 queue_duration_sum_ = 0; |
| 146 queue_duration_max_ = 0; | 133 queue_duration_max_ = 0; |
| 147 queue_duration_sample_ = 0; | 134 queue_duration_sample_ = 0; |
| 148 } | 135 } |
| 149 | 136 |
| 150 //------------------------------------------------------------------------------ | 137 //------------------------------------------------------------------------------ |
| 138 SerializedDeathData::SerializedDeathData() { |
| 139 } |
| 140 |
| 141 SerializedDeathData::SerializedDeathData( |
| 142 const tracked_objects::DeathData& death_data) |
| 143 : count(death_data.count()), |
| 144 run_duration_sum(death_data.run_duration_sum()), |
| 145 run_duration_max(death_data.run_duration_max()), |
| 146 run_duration_sample(death_data.run_duration_sample()), |
| 147 queue_duration_sum(death_data.queue_duration_sum()), |
| 148 queue_duration_max(death_data.queue_duration_max()), |
| 149 queue_duration_sample(death_data.queue_duration_sample()) { |
| 150 } |
| 151 |
| 152 SerializedDeathData::~SerializedDeathData() { |
| 153 } |
| 154 |
| 155 void SerializedDeathData::ToValue(DictionaryValue* dictionary) const { |
| 156 dictionary->Set("count", Value::CreateIntegerValue(count)); |
| 157 dictionary->Set("run_ms", Value::CreateIntegerValue(run_duration_sum)); |
| 158 dictionary->Set("run_ms_max", Value::CreateIntegerValue(run_duration_max)); |
| 159 dictionary->Set("run_ms_sample", |
| 160 Value::CreateIntegerValue(run_duration_sample)); |
| 161 dictionary->Set("queue_ms", Value::CreateIntegerValue(queue_duration_sum)); |
| 162 dictionary->Set("queue_ms_max", |
| 163 Value::CreateIntegerValue(queue_duration_max)); |
| 164 dictionary->Set("queue_ms_sample", |
| 165 Value::CreateIntegerValue(queue_duration_sample)); |
| 166 } |
| 167 |
| 168 //------------------------------------------------------------------------------ |
| 151 BirthOnThread::BirthOnThread(const Location& location, | 169 BirthOnThread::BirthOnThread(const Location& location, |
| 152 const ThreadData& current) | 170 const ThreadData& current) |
| 153 : location_(location), | 171 : location_(location), |
| 154 birth_thread_(¤t) { | 172 birth_thread_(¤t) { |
| 155 } | 173 } |
| 156 | 174 |
| 157 const Location BirthOnThread::location() const { return location_; } | 175 //------------------------------------------------------------------------------ |
| 158 const ThreadData* BirthOnThread::birth_thread() const { return birth_thread_; } | 176 SerializedBirthOnThread::SerializedBirthOnThread() { |
| 177 } |
| 159 | 178 |
| 160 void BirthOnThread::ToValue(const std::string& prefix, | 179 SerializedBirthOnThread::SerializedBirthOnThread( |
| 161 base::DictionaryValue* dictionary) const { | 180 const tracked_objects::BirthOnThread& birth) |
| 162 dictionary->Set(prefix + "_location", location_.ToValue()); | 181 : location(birth.location()), |
| 163 dictionary->Set(prefix + "_thread", | 182 thread_name(birth.birth_thread()->thread_name()) { |
| 164 base::Value::CreateStringValue(birth_thread_->thread_name())); | 183 } |
| 184 |
| 185 SerializedBirthOnThread::~SerializedBirthOnThread() { |
| 186 } |
| 187 |
| 188 void SerializedBirthOnThread::ToValue(const std::string& prefix, |
| 189 DictionaryValue* dictionary) const { |
| 190 scoped_ptr<DictionaryValue> location_value(new DictionaryValue); |
| 191 location.ToValue(location_value.get()); |
| 192 |
| 193 // TODO(jar): Remove the if/else below and disallow passing an empty prefix, |
| 194 // but that will require fixing unit tests, and JS to take "birth_location" |
| 195 // rather than "location". |
| 196 std::string location_key = "location"; |
| 197 std::string thread_name_key = "thread"; |
| 198 if (!prefix.empty()) { |
| 199 location_key = prefix + "_" + location_key; |
| 200 thread_name_key = prefix + "_" + thread_name_key; |
| 201 } else { |
| 202 // For not we special-case the birth thread, as the renderer code expects |
| 203 // "location" rather than "birth_location". |
| 204 thread_name_key = "birth_" + thread_name_key; |
| 205 } |
| 206 |
| 207 dictionary->Set(location_key, location_value.release()); |
| 208 dictionary->Set(thread_name_key, Value::CreateStringValue(thread_name)); |
| 165 } | 209 } |
| 166 | 210 |
| 167 //------------------------------------------------------------------------------ | 211 //------------------------------------------------------------------------------ |
| 168 Births::Births(const Location& location, const ThreadData& current) | 212 Births::Births(const Location& location, const ThreadData& current) |
| 169 : BirthOnThread(location, current), | 213 : BirthOnThread(location, current), |
| 170 birth_count_(1) { } | 214 birth_count_(1) { } |
| 171 | 215 |
| 172 int Births::birth_count() const { return birth_count_; } | 216 int Births::birth_count() const { return birth_count_; } |
| 173 | 217 |
| 174 void Births::RecordBirth() { ++birth_count_; } | 218 void Births::RecordBirth() { ++birth_count_; } |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 327 return; | 371 return; |
| 328 } | 372 } |
| 329 // We must NOT do any allocations during this callback. | 373 // We must NOT do any allocations during this callback. |
| 330 // Using the simple linked lists avoids all allocations. | 374 // Using the simple linked lists avoids all allocations. |
| 331 DCHECK_EQ(this->next_retired_worker_, reinterpret_cast<ThreadData*>(NULL)); | 375 DCHECK_EQ(this->next_retired_worker_, reinterpret_cast<ThreadData*>(NULL)); |
| 332 this->next_retired_worker_ = first_retired_worker_; | 376 this->next_retired_worker_ = first_retired_worker_; |
| 333 first_retired_worker_ = this; | 377 first_retired_worker_ = this; |
| 334 } | 378 } |
| 335 | 379 |
| 336 // static | 380 // static |
| 337 base::DictionaryValue* ThreadData::ToValue(bool reset_max) { | 381 void ThreadData::ToSerializedProcessData(bool reset_max, |
| 338 DataCollector collected_data; // Gather data. | 382 SerializedProcessData* process_data) { |
| 339 // Request multiple calls to collected_data.Append() for all threads. | 383 // Add births that have run to completion to |collected_data|. |
| 340 SendAllMaps(reset_max, &collected_data); | 384 // |birth_counts| tracks the total number of births recorded at each location |
| 341 collected_data.AddListOfLivingObjects(); // Add births that are still alive. | 385 // for which we have not seen a death count. |
| 342 base::DictionaryValue* dictionary = new base::DictionaryValue(); | 386 std::map<const BirthOnThread*, int> birth_counts; |
| 343 collected_data.ToValue(dictionary); | 387 ThreadData::SerializeAllExecutedTasks(reset_max, process_data, &birth_counts); |
| 344 return dictionary; | 388 |
| 389 // Add births that are still active -- i.e. objects that have tallied a birth, |
| 390 // but have not yet tallied a matching death, and hence must be either |
| 391 // running, queued up, or being held in limbo for future posting. |
| 392 for (std::map<const BirthOnThread*, int>::const_iterator it = |
| 393 birth_counts.begin(); |
| 394 it != birth_counts.end(); ++it) { |
| 395 if (it->second > 0) { |
| 396 process_data->snapshots.push_back( |
| 397 SerializedSnapshot(*it->first, |
| 398 DeathData(it->second), |
| 399 "Still_Alive")); |
| 400 } |
| 401 } |
| 345 } | 402 } |
| 346 | 403 |
| 347 Births* ThreadData::TallyABirth(const Location& location) { | 404 Births* ThreadData::TallyABirth(const Location& location) { |
| 348 BirthMap::iterator it = birth_map_.find(location); | 405 BirthMap::iterator it = birth_map_.find(location); |
| 349 Births* child; | 406 Births* child; |
| 350 if (it != birth_map_.end()) { | 407 if (it != birth_map_.end()) { |
| 351 child = it->second; | 408 child = it->second; |
| 352 child->RecordBirth(); | 409 child->RecordBirth(); |
| 353 } else { | 410 } else { |
| 354 child = new Births(location, *this); // Leak this. | 411 child = new Births(location, *this); // Leak this. |
| (...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 520 | 577 |
| 521 DurationInt queue_duration = 0; | 578 DurationInt queue_duration = 0; |
| 522 DurationInt run_duration = 0; | 579 DurationInt run_duration = 0; |
| 523 if (!start_of_run.is_null() && !end_of_run.is_null()) | 580 if (!start_of_run.is_null() && !end_of_run.is_null()) |
| 524 run_duration = (end_of_run - start_of_run).InMilliseconds(); | 581 run_duration = (end_of_run - start_of_run).InMilliseconds(); |
| 525 current_thread_data->TallyADeath(*birth, queue_duration, run_duration); | 582 current_thread_data->TallyADeath(*birth, queue_duration, run_duration); |
| 526 } | 583 } |
| 527 | 584 |
| 528 const std::string ThreadData::thread_name() const { return thread_name_; } | 585 const std::string ThreadData::thread_name() const { return thread_name_; } |
| 529 | 586 |
| 587 // static |
| 588 void ThreadData::SerializeAllExecutedTasks( |
| 589 bool reset_max, |
| 590 SerializedProcessData* process_data, |
| 591 std::map<const BirthOnThread*, int>* birth_counts) { |
| 592 if (!kTrackAllTaskObjects) |
| 593 return; // Not compiled in. |
| 594 |
| 595 // Get an unchanging copy of a ThreadData list. |
| 596 ThreadData* my_list = ThreadData::first(); |
| 597 |
| 598 // Gather data serially. |
| 599 // This hackish approach *can* get some slighly corrupt tallies, as we are |
| 600 // grabbing values without the protection of a lock, but it has the advantage |
| 601 // of working even with threads that don't have message loops. If a user |
| 602 // sees any strangeness, they can always just run their stats gathering a |
| 603 // second time. |
| 604 for (ThreadData* thread_data = my_list; |
| 605 thread_data; |
| 606 thread_data = thread_data->next()) { |
| 607 thread_data->SerializeExecutedTasks(reset_max, process_data, birth_counts); |
| 608 } |
| 609 } |
| 610 |
| 611 void ThreadData::SerializeExecutedTasks( |
| 612 bool reset_max, |
| 613 SerializedProcessData* process_data, |
| 614 std::map<const BirthOnThread*, int>* birth_counts) { |
| 615 // Get copy of data, so that the data will not change during the iterations |
| 616 // and processing. |
| 617 ThreadData::BirthMap birth_map; |
| 618 ThreadData::DeathMap death_map; |
| 619 ThreadData::ParentChildSet parent_child_set; |
| 620 SnapshotMaps(reset_max, &birth_map, &death_map, &parent_child_set); |
| 621 |
| 622 for (ThreadData::DeathMap::const_iterator it = death_map.begin(); |
| 623 it != death_map.end(); ++it) { |
| 624 process_data->snapshots.push_back( |
| 625 SerializedSnapshot(*it->first, it->second, thread_name())); |
| 626 (*birth_counts)[it->first] -= it->first->birth_count(); |
| 627 } |
| 628 |
| 629 for (ThreadData::BirthMap::const_iterator it = birth_map.begin(); |
| 630 it != birth_map.end(); ++it) { |
| 631 (*birth_counts)[it->second] += it->second->birth_count(); |
| 632 } |
| 633 |
| 634 if (!kTrackParentChildLinks) |
| 635 return; |
| 636 |
| 637 for (ThreadData::ParentChildSet::const_iterator it = parent_child_set.begin(); |
| 638 it != parent_child_set.end(); ++it) { |
| 639 process_data->descendants.push_back(SerializedParentChildPair(*it)); |
| 640 } |
| 641 } |
| 642 |
| 530 // This may be called from another thread. | 643 // This may be called from another thread. |
| 531 void ThreadData::SnapshotMaps(bool reset_max, | 644 void ThreadData::SnapshotMaps(bool reset_max, |
| 532 BirthMap* birth_map, | 645 BirthMap* birth_map, |
| 533 DeathMap* death_map, | 646 DeathMap* death_map, |
| 534 ParentChildSet* parent_child_set) { | 647 ParentChildSet* parent_child_set) { |
| 535 base::AutoLock lock(map_lock_); | 648 base::AutoLock lock(map_lock_); |
| 536 for (BirthMap::const_iterator it = birth_map_.begin(); | 649 for (BirthMap::const_iterator it = birth_map_.begin(); |
| 537 it != birth_map_.end(); ++it) | 650 it != birth_map_.end(); ++it) |
| 538 (*birth_map)[it->first] = it->second; | 651 (*birth_map)[it->first] = it->second; |
| 539 for (DeathMap::iterator it = death_map_.begin(); | 652 for (DeathMap::iterator it = death_map_.begin(); |
| 540 it != death_map_.end(); ++it) { | 653 it != death_map_.end(); ++it) { |
| 541 (*death_map)[it->first] = it->second; | 654 (*death_map)[it->first] = it->second; |
| 542 if (reset_max) | 655 if (reset_max) |
| 543 it->second.ResetMax(); | 656 it->second.ResetMax(); |
| 544 } | 657 } |
| 545 | 658 |
| 546 if (!kTrackParentChildLinks) | 659 if (!kTrackParentChildLinks) |
| 547 return; | 660 return; |
| 548 | 661 |
| 549 for (ParentChildSet::iterator it = parent_child_set_.begin(); | 662 for (ParentChildSet::iterator it = parent_child_set_.begin(); |
| 550 it != parent_child_set_.end(); ++it) | 663 it != parent_child_set_.end(); ++it) |
| 551 parent_child_set->insert(*it); | 664 parent_child_set->insert(*it); |
| 552 } | 665 } |
| 553 | 666 |
| 554 // static | 667 // static |
| 555 void ThreadData::SendAllMaps(bool reset_max, class DataCollector* target) { | |
| 556 if (!kTrackAllTaskObjects) | |
| 557 return; // Not compiled in. | |
| 558 // Get an unchanging copy of a ThreadData list. | |
| 559 ThreadData* my_list = ThreadData::first(); | |
| 560 | |
| 561 // Gather data serially. | |
| 562 // This hackish approach *can* get some slighly corrupt tallies, as we are | |
| 563 // grabbing values without the protection of a lock, but it has the advantage | |
| 564 // of working even with threads that don't have message loops. If a user | |
| 565 // sees any strangeness, they can always just run their stats gathering a | |
| 566 // second time. | |
| 567 for (ThreadData* thread_data = my_list; | |
| 568 thread_data; | |
| 569 thread_data = thread_data->next()) { | |
| 570 // Get copy of data. | |
| 571 ThreadData::BirthMap birth_map; | |
| 572 ThreadData::DeathMap death_map; | |
| 573 ThreadData::ParentChildSet parent_child_set; | |
| 574 thread_data->SnapshotMaps(reset_max, &birth_map, &death_map, | |
| 575 &parent_child_set); | |
| 576 target->Append(*thread_data, birth_map, death_map, parent_child_set); | |
| 577 } | |
| 578 } | |
| 579 | |
| 580 // static | |
| 581 void ThreadData::ResetAllThreadData() { | 668 void ThreadData::ResetAllThreadData() { |
| 582 ThreadData* my_list = first(); | 669 ThreadData* my_list = first(); |
| 583 | 670 |
| 584 for (ThreadData* thread_data = my_list; | 671 for (ThreadData* thread_data = my_list; |
| 585 thread_data; | 672 thread_data; |
| 586 thread_data = thread_data->next()) | 673 thread_data = thread_data->next()) |
| 587 thread_data->Reset(); | 674 thread_data->Reset(); |
| 588 } | 675 } |
| 589 | 676 |
| 590 void ThreadData::Reset() { | 677 void ThreadData::Reset() { |
| (...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 778 thread_data_list = thread_data_list->next(); | 865 thread_data_list = thread_data_list->next(); |
| 779 | 866 |
| 780 for (BirthMap::iterator it = next_thread_data->birth_map_.begin(); | 867 for (BirthMap::iterator it = next_thread_data->birth_map_.begin(); |
| 781 next_thread_data->birth_map_.end() != it; ++it) | 868 next_thread_data->birth_map_.end() != it; ++it) |
| 782 delete it->second; // Delete the Birth Records. | 869 delete it->second; // Delete the Birth Records. |
| 783 delete next_thread_data; // Includes all Death Records. | 870 delete next_thread_data; // Includes all Death Records. |
| 784 } | 871 } |
| 785 } | 872 } |
| 786 | 873 |
| 787 //------------------------------------------------------------------------------ | 874 //------------------------------------------------------------------------------ |
| 788 // Individual 3-tuple of birth (place and thread) along with death thread, and | 875 SerializedSnapshot::SerializedSnapshot() { |
| 789 // the accumulated stats for instances (DeathData). | |
| 790 | |
| 791 Snapshot::Snapshot(const BirthOnThread& birth_on_thread, | |
| 792 const ThreadData& death_thread, | |
| 793 const DeathData& death_data) | |
| 794 : birth_(&birth_on_thread), | |
| 795 death_thread_(&death_thread), | |
| 796 death_data_(death_data) { | |
| 797 } | 876 } |
| 798 | 877 |
| 799 Snapshot::Snapshot(const BirthOnThread& birth_on_thread, int count) | 878 SerializedSnapshot::SerializedSnapshot(const BirthOnThread& birth, |
| 800 : birth_(&birth_on_thread), | 879 const DeathData& death_data, |
| 801 death_thread_(NULL), | 880 const std::string& death_thread_name) |
| 802 death_data_(DeathData(count)) { | 881 : birth(birth), |
| 882 death_data(death_data), |
| 883 death_thread_name(death_thread_name) { |
| 803 } | 884 } |
| 804 | 885 |
| 805 const std::string Snapshot::DeathThreadName() const { | 886 SerializedSnapshot::~SerializedSnapshot() { |
| 806 if (death_thread_) | |
| 807 return death_thread_->thread_name(); | |
| 808 return "Still_Alive"; | |
| 809 } | 887 } |
| 810 | 888 |
| 811 base::DictionaryValue* Snapshot::ToValue() const { | 889 void SerializedSnapshot::ToValue(DictionaryValue* dictionary) const { |
| 812 base::DictionaryValue* dictionary = new base::DictionaryValue; | 890 // TODO(jar): Update the first argument below to be "birth" rather than an |
| 813 // TODO(jar): Switch the next two lines to: | 891 // empty string, but that will require fixing unit tests, and JS to take |
| 814 // birth_->ToValue("birth", dictionary); | 892 // "birth_location" rather than "location". |
| 815 // ...but that will require fixing unit tests, and JS to take | 893 birth.ToValue(std::string(), dictionary); |
| 816 // "birth_location" rather than "location" | |
| 817 dictionary->Set("birth_thread", | |
| 818 base::Value::CreateStringValue(birth_->birth_thread()->thread_name())); | |
| 819 dictionary->Set("location", birth_->location().ToValue()); | |
| 820 | 894 |
| 821 dictionary->Set("death_data", death_data_.ToValue()); | 895 scoped_ptr<DictionaryValue> death_value(new DictionaryValue); |
| 822 dictionary->Set("death_thread", | 896 death_data.ToValue(death_value.get()); |
| 823 base::Value::CreateStringValue(DeathThreadName())); | 897 dictionary->Set("death_data", death_value.release()); |
| 824 return dictionary; | 898 |
| 899 dictionary->Set("death_thread", Value::CreateStringValue(death_thread_name)); |
| 825 } | 900 } |
| 826 | 901 |
| 827 //------------------------------------------------------------------------------ | 902 //------------------------------------------------------------------------------ |
| 828 // DataCollector | 903 // SerializedParentChildPair |
| 829 | 904 |
| 830 DataCollector::DataCollector() {} | 905 SerializedParentChildPair::SerializedParentChildPair(){ |
| 831 | |
| 832 DataCollector::~DataCollector() { | |
| 833 } | 906 } |
| 834 | 907 |
| 835 void DataCollector::Append(const ThreadData& thread_data, | 908 SerializedParentChildPair::SerializedParentChildPair( |
| 836 const ThreadData::BirthMap& birth_map, | 909 const ThreadData::ParentChildPair& parent_child) |
| 837 const ThreadData::DeathMap& death_map, | 910 : parent(*parent_child.first), |
| 838 const ThreadData::ParentChildSet& parent_child_set) { | 911 child(*parent_child.second) { |
| 839 for (ThreadData::DeathMap::const_iterator it = death_map.begin(); | |
| 840 it != death_map.end(); ++it) { | |
| 841 collection_.push_back(Snapshot(*it->first, thread_data, it->second)); | |
| 842 global_birth_count_[it->first] -= it->first->birth_count(); | |
| 843 } | |
| 844 | |
| 845 for (ThreadData::BirthMap::const_iterator it = birth_map.begin(); | |
| 846 it != birth_map.end(); ++it) { | |
| 847 global_birth_count_[it->second] += it->second->birth_count(); | |
| 848 } | |
| 849 | |
| 850 if (!kTrackParentChildLinks) | |
| 851 return; | |
| 852 | |
| 853 for (ThreadData::ParentChildSet::const_iterator it = parent_child_set.begin(); | |
| 854 it != parent_child_set.end(); ++it) { | |
| 855 parent_child_set_.insert(*it); | |
| 856 } | |
| 857 } | 912 } |
| 858 | 913 |
| 859 DataCollector::Collection* DataCollector::collection() { | 914 SerializedParentChildPair::~SerializedParentChildPair() { |
| 860 return &collection_; | |
| 861 } | 915 } |
| 862 | 916 |
| 863 void DataCollector::AddListOfLivingObjects() { | 917 void SerializedParentChildPair::ToValue( |
| 864 for (BirthCount::iterator it = global_birth_count_.begin(); | 918 base::DictionaryValue* dictionary) const { |
| 865 it != global_birth_count_.end(); ++it) { | 919 parent.ToValue("parent", dictionary); |
| 866 if (it->second > 0) | 920 child.ToValue("child", dictionary); |
| 867 collection_.push_back(Snapshot(*it->first, it->second)); | |
| 868 } | |
| 869 } | 921 } |
| 870 | 922 |
| 871 void DataCollector::ToValue(base::DictionaryValue* dictionary) const { | 923 |
| 872 base::ListValue* list = new base::ListValue; | 924 //------------------------------------------------------------------------------ |
| 873 for (size_t i = 0; i < collection_.size(); ++i) { | 925 // SerializedProcessData |
| 874 list->Append(collection_[i].ToValue()); | 926 |
| 927 SerializedProcessData::SerializedProcessData() |
| 928 : process_id(base::GetCurrentProcId()) { |
| 929 } |
| 930 |
| 931 SerializedProcessData::~SerializedProcessData() { |
| 932 } |
| 933 |
| 934 void SerializedProcessData::ToValue(DictionaryValue* dictionary) const { |
| 935 scoped_ptr<base::ListValue> snapshots_list(new base::ListValue); |
| 936 for (std::vector<tracked_objects::SerializedSnapshot>::const_iterator it = |
| 937 snapshots.begin(); |
| 938 it != snapshots.end(); ++it) { |
| 939 scoped_ptr<DictionaryValue> snapshot(new DictionaryValue); |
| 940 it->ToValue(snapshot.get()); |
| 941 snapshots_list->Append(snapshot.release()); |
| 875 } | 942 } |
| 876 dictionary->Set("list", list); | 943 dictionary->Set("list", snapshots_list.release()); |
| 877 | 944 |
| 878 base::ListValue* descendants = new base::ListValue; | 945 dictionary->SetInteger("process_id", process_id); |
| 879 for (ThreadData::ParentChildSet::const_iterator it = | 946 |
| 880 parent_child_set_.begin(); | 947 scoped_ptr<base::ListValue> descendants_list(new base::ListValue); |
| 881 it != parent_child_set_.end(); | 948 for (std::vector<SerializedParentChildPair>::const_iterator it = |
| 882 ++it) { | 949 descendants.begin(); |
| 883 base::DictionaryValue* parent_child = new base::DictionaryValue; | 950 it != descendants.end(); ++it) { |
| 884 it->first->ToValue("parent", parent_child); | 951 scoped_ptr<base::DictionaryValue> parent_child(new base::DictionaryValue); |
| 885 it->second->ToValue("child", parent_child); | 952 it->ToValue(parent_child.get()); |
| 886 descendants->Append(parent_child); | 953 descendants_list->Append(parent_child.release()); |
| 887 } | 954 } |
| 888 dictionary->Set("descendants", descendants); | 955 dictionary->Set("descendants", descendants_list.release()); |
| 889 } | 956 } |
| 890 | 957 |
| 891 } // namespace tracked_objects | 958 } // namespace tracked_objects |
| OLD | NEW |