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" |
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 } | 156 } |
157 | 157 |
158 int32 DeathData::queue_duration_max() const { | 158 int32 DeathData::queue_duration_max() const { |
159 return queue_duration_max_; | 159 return queue_duration_max_; |
160 } | 160 } |
161 | 161 |
162 int32 DeathData::queue_duration_sample() const { | 162 int32 DeathData::queue_duration_sample() const { |
163 return queue_duration_sample_; | 163 return queue_duration_sample_; |
164 } | 164 } |
165 | 165 |
166 void DeathData::ResetMax() { | |
167 run_duration_max_ = 0; | |
168 queue_duration_max_ = 0; | |
169 } | |
170 | |
171 void DeathData::Clear() { | 166 void DeathData::Clear() { |
172 count_ = 0; | 167 count_ = 0; |
173 run_duration_sum_ = 0; | 168 run_duration_sum_ = 0; |
174 run_duration_max_ = 0; | 169 run_duration_max_ = 0; |
175 run_duration_sample_ = 0; | 170 run_duration_sample_ = 0; |
176 queue_duration_sum_ = 0; | 171 queue_duration_sum_ = 0; |
177 queue_duration_max_ = 0; | 172 queue_duration_max_ = 0; |
178 queue_duration_sample_ = 0; | 173 queue_duration_sample_ = 0; |
179 } | 174 } |
180 | 175 |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
225 | 220 |
226 //------------------------------------------------------------------------------ | 221 //------------------------------------------------------------------------------ |
227 Births::Births(const Location& location, const ThreadData& current) | 222 Births::Births(const Location& location, const ThreadData& current) |
228 : BirthOnThread(location, current), | 223 : BirthOnThread(location, current), |
229 birth_count_(1) { } | 224 birth_count_(1) { } |
230 | 225 |
231 int Births::birth_count() const { return birth_count_; } | 226 int Births::birth_count() const { return birth_count_; } |
232 | 227 |
233 void Births::RecordBirth() { ++birth_count_; } | 228 void Births::RecordBirth() { ++birth_count_; } |
234 | 229 |
235 void Births::ForgetBirth() { --birth_count_; } | |
236 | |
237 void Births::Clear() { birth_count_ = 0; } | |
238 | |
239 //------------------------------------------------------------------------------ | 230 //------------------------------------------------------------------------------ |
240 // ThreadData maintains the central data for all births and deaths on a single | 231 // ThreadData maintains the central data for all births and deaths on a single |
241 // thread. | 232 // thread. |
242 | 233 |
243 // TODO(jar): We should pull all these static vars together, into a struct, and | 234 // TODO(jar): We should pull all these static vars together, into a struct, and |
244 // optimize layout so that we benefit from locality of reference during accesses | 235 // optimize layout so that we benefit from locality of reference during accesses |
245 // to them. | 236 // to them. |
246 | 237 |
247 // static | 238 // static |
248 NowFunction* ThreadData::now_function_ = NULL; | 239 NowFunction* ThreadData::now_function_ = NULL; |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
392 return; | 383 return; |
393 } | 384 } |
394 // We must NOT do any allocations during this callback. | 385 // We must NOT do any allocations during this callback. |
395 // Using the simple linked lists avoids all allocations. | 386 // Using the simple linked lists avoids all allocations. |
396 DCHECK_EQ(this->next_retired_worker_, reinterpret_cast<ThreadData*>(NULL)); | 387 DCHECK_EQ(this->next_retired_worker_, reinterpret_cast<ThreadData*>(NULL)); |
397 this->next_retired_worker_ = first_retired_worker_; | 388 this->next_retired_worker_ = first_retired_worker_; |
398 first_retired_worker_ = this; | 389 first_retired_worker_ = this; |
399 } | 390 } |
400 | 391 |
401 // static | 392 // static |
402 void ThreadData::Snapshot(bool reset_max, ProcessDataSnapshot* process_data) { | 393 void ThreadData::Snapshot(ProcessDataSnapshot* process_data) { |
403 // Add births that have run to completion to |collected_data|. | 394 // Add births that have run to completion to |collected_data|. |
404 // |birth_counts| tracks the total number of births recorded at each location | 395 // |birth_counts| tracks the total number of births recorded at each location |
405 // for which we have not seen a death count. | 396 // for which we have not seen a death count. |
406 BirthCountMap birth_counts; | 397 BirthCountMap birth_counts; |
407 ThreadData::SnapshotAllExecutedTasks(reset_max, process_data, &birth_counts); | 398 ThreadData::SnapshotAllExecutedTasks(process_data, &birth_counts); |
408 | 399 |
409 // Add births that are still active -- i.e. objects that have tallied a birth, | 400 // Add births that are still active -- i.e. objects that have tallied a birth, |
410 // but have not yet tallied a matching death, and hence must be either | 401 // but have not yet tallied a matching death, and hence must be either |
411 // running, queued up, or being held in limbo for future posting. | 402 // running, queued up, or being held in limbo for future posting. |
412 for (BirthCountMap::const_iterator it = birth_counts.begin(); | 403 for (BirthCountMap::const_iterator it = birth_counts.begin(); |
413 it != birth_counts.end(); ++it) { | 404 it != birth_counts.end(); ++it) { |
414 if (it->second > 0) { | 405 if (it->second > 0) { |
415 process_data->tasks.push_back( | 406 process_data->tasks.push_back( |
416 TaskSnapshot(*it->first, DeathData(it->second), "Still_Alive")); | 407 TaskSnapshot(*it->first, DeathData(it->second), "Still_Alive")); |
417 } | 408 } |
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
580 | 571 |
581 ThreadData* current_thread_data = stopwatch.GetThreadData(); | 572 ThreadData* current_thread_data = stopwatch.GetThreadData(); |
582 if (!current_thread_data) | 573 if (!current_thread_data) |
583 return; | 574 return; |
584 | 575 |
585 int32 queue_duration = 0; | 576 int32 queue_duration = 0; |
586 current_thread_data->TallyADeath(*birth, queue_duration, stopwatch); | 577 current_thread_data->TallyADeath(*birth, queue_duration, stopwatch); |
587 } | 578 } |
588 | 579 |
589 // static | 580 // static |
590 void ThreadData::SnapshotAllExecutedTasks(bool reset_max, | 581 void ThreadData::SnapshotAllExecutedTasks(ProcessDataSnapshot* process_data, |
591 ProcessDataSnapshot* process_data, | |
592 BirthCountMap* birth_counts) { | 582 BirthCountMap* birth_counts) { |
593 if (!kTrackAllTaskObjects) | 583 if (!kTrackAllTaskObjects) |
594 return; // Not compiled in. | 584 return; // Not compiled in. |
595 | 585 |
596 // Get an unchanging copy of a ThreadData list. | 586 // Get an unchanging copy of a ThreadData list. |
597 ThreadData* my_list = ThreadData::first(); | 587 ThreadData* my_list = ThreadData::first(); |
598 | 588 |
599 // Gather data serially. | 589 // Gather data serially. |
600 // This hackish approach *can* get some slighly corrupt tallies, as we are | 590 // This hackish approach *can* get some slighly corrupt tallies, as we are |
601 // grabbing values without the protection of a lock, but it has the advantage | 591 // grabbing values without the protection of a lock, but it has the advantage |
602 // of working even with threads that don't have message loops. If a user | 592 // of working even with threads that don't have message loops. If a user |
603 // sees any strangeness, they can always just run their stats gathering a | 593 // sees any strangeness, they can always just run their stats gathering a |
604 // second time. | 594 // second time. |
605 for (ThreadData* thread_data = my_list; | 595 for (ThreadData* thread_data = my_list; |
606 thread_data; | 596 thread_data; |
607 thread_data = thread_data->next()) { | 597 thread_data = thread_data->next()) { |
608 thread_data->SnapshotExecutedTasks(reset_max, process_data, birth_counts); | 598 thread_data->SnapshotExecutedTasks(process_data, birth_counts); |
609 } | 599 } |
610 } | 600 } |
611 | 601 |
612 void ThreadData::SnapshotExecutedTasks(bool reset_max, | 602 void ThreadData::SnapshotExecutedTasks(ProcessDataSnapshot* process_data, |
613 ProcessDataSnapshot* process_data, | |
614 BirthCountMap* birth_counts) { | 603 BirthCountMap* birth_counts) { |
615 // Get copy of data, so that the data will not change during the iterations | 604 // Get copy of data, so that the data will not change during the iterations |
616 // and processing. | 605 // and processing. |
617 ThreadData::BirthMap birth_map; | 606 ThreadData::BirthMap birth_map; |
618 ThreadData::DeathMap death_map; | 607 ThreadData::DeathMap death_map; |
619 ThreadData::ParentChildSet parent_child_set; | 608 ThreadData::ParentChildSet parent_child_set; |
620 SnapshotMaps(reset_max, &birth_map, &death_map, &parent_child_set); | 609 SnapshotMaps(&birth_map, &death_map, &parent_child_set); |
621 | 610 |
622 for (ThreadData::DeathMap::const_iterator it = death_map.begin(); | 611 for (ThreadData::DeathMap::const_iterator it = death_map.begin(); |
623 it != death_map.end(); ++it) { | 612 it != death_map.end(); ++it) { |
624 process_data->tasks.push_back( | 613 process_data->tasks.push_back( |
625 TaskSnapshot(*it->first, it->second, thread_name())); | 614 TaskSnapshot(*it->first, it->second, thread_name())); |
626 (*birth_counts)[it->first] -= it->first->birth_count(); | 615 (*birth_counts)[it->first] -= it->first->birth_count(); |
627 } | 616 } |
628 | 617 |
629 for (ThreadData::BirthMap::const_iterator it = birth_map.begin(); | 618 for (ThreadData::BirthMap::const_iterator it = birth_map.begin(); |
630 it != birth_map.end(); ++it) { | 619 it != birth_map.end(); ++it) { |
631 (*birth_counts)[it->second] += it->second->birth_count(); | 620 (*birth_counts)[it->second] += it->second->birth_count(); |
632 } | 621 } |
633 | 622 |
634 if (!kTrackParentChildLinks) | 623 if (!kTrackParentChildLinks) |
635 return; | 624 return; |
636 | 625 |
637 for (ThreadData::ParentChildSet::const_iterator it = parent_child_set.begin(); | 626 for (ThreadData::ParentChildSet::const_iterator it = parent_child_set.begin(); |
638 it != parent_child_set.end(); ++it) { | 627 it != parent_child_set.end(); ++it) { |
639 process_data->descendants.push_back(ParentChildPairSnapshot(*it)); | 628 process_data->descendants.push_back(ParentChildPairSnapshot(*it)); |
640 } | 629 } |
641 } | 630 } |
642 | 631 |
643 // This may be called from another thread. | 632 // This may be called from another thread. |
644 void ThreadData::SnapshotMaps(bool reset_max, | 633 void ThreadData::SnapshotMaps(BirthMap* birth_map, |
645 BirthMap* birth_map, | |
646 DeathMap* death_map, | 634 DeathMap* death_map, |
647 ParentChildSet* parent_child_set) { | 635 ParentChildSet* parent_child_set) { |
648 base::AutoLock lock(map_lock_); | 636 base::AutoLock lock(map_lock_); |
649 for (BirthMap::const_iterator it = birth_map_.begin(); | 637 for (BirthMap::const_iterator it = birth_map_.begin(); |
650 it != birth_map_.end(); ++it) | 638 it != birth_map_.end(); ++it) |
651 (*birth_map)[it->first] = it->second; | 639 (*birth_map)[it->first] = it->second; |
652 for (DeathMap::iterator it = death_map_.begin(); | 640 for (DeathMap::iterator it = death_map_.begin(); |
653 it != death_map_.end(); ++it) { | 641 it != death_map_.end(); ++it) { |
654 (*death_map)[it->first] = it->second; | 642 (*death_map)[it->first] = it->second; |
655 if (reset_max) | |
656 it->second.ResetMax(); | |
657 } | 643 } |
658 | 644 |
659 if (!kTrackParentChildLinks) | 645 if (!kTrackParentChildLinks) |
660 return; | 646 return; |
661 | 647 |
662 for (ParentChildSet::iterator it = parent_child_set_.begin(); | 648 for (ParentChildSet::iterator it = parent_child_set_.begin(); |
663 it != parent_child_set_.end(); ++it) | 649 it != parent_child_set_.end(); ++it) |
664 parent_child_set->insert(*it); | 650 parent_child_set->insert(*it); |
665 } | 651 } |
666 | 652 |
667 // static | |
668 void ThreadData::ResetAllThreadData() { | |
669 ThreadData* my_list = first(); | |
670 | |
671 for (ThreadData* thread_data = my_list; | |
672 thread_data; | |
673 thread_data = thread_data->next()) | |
674 thread_data->Reset(); | |
675 } | |
676 | |
677 void ThreadData::Reset() { | |
678 base::AutoLock lock(map_lock_); | |
679 for (DeathMap::iterator it = death_map_.begin(); | |
680 it != death_map_.end(); ++it) | |
681 it->second.Clear(); | |
682 for (BirthMap::iterator it = birth_map_.begin(); | |
683 it != birth_map_.end(); ++it) | |
684 it->second->Clear(); | |
685 } | |
686 | |
687 static void OptionallyInitializeAlternateTimer() { | 653 static void OptionallyInitializeAlternateTimer() { |
688 NowFunction* alternate_time_source = GetAlternateTimeSource(); | 654 NowFunction* alternate_time_source = GetAlternateTimeSource(); |
689 if (alternate_time_source) | 655 if (alternate_time_source) |
690 ThreadData::SetAlternateTimeSource(alternate_time_source); | 656 ThreadData::SetAlternateTimeSource(alternate_time_source); |
691 } | 657 } |
692 | 658 |
693 bool ThreadData::Initialize() { | 659 bool ThreadData::Initialize() { |
694 if (!kTrackAllTaskObjects) | 660 if (!kTrackAllTaskObjects) |
695 return false; // Not compiled in. | 661 return false; // Not compiled in. |
696 if (status_ >= DEACTIVATED) | 662 if (status_ >= DEACTIVATED) |
(...skipping 303 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1000 : process_id(base::GetCurrentProcId()) { | 966 : process_id(base::GetCurrentProcId()) { |
1001 #else | 967 #else |
1002 : process_id(0) { | 968 : process_id(0) { |
1003 #endif | 969 #endif |
1004 } | 970 } |
1005 | 971 |
1006 ProcessDataSnapshot::~ProcessDataSnapshot() { | 972 ProcessDataSnapshot::~ProcessDataSnapshot() { |
1007 } | 973 } |
1008 | 974 |
1009 } // namespace tracked_objects | 975 } // namespace tracked_objects |
OLD | NEW |