OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/message_loop.h" | 10 #include "base/message_loop.h" |
11 #include "base/string_util.h" | |
12 #include "base/stringprintf.h" | 11 #include "base/stringprintf.h" |
13 #include "base/threading/thread_restrictions.h" | 12 #include "base/threading/thread_restrictions.h" |
14 #include "build/build_config.h" | 13 #include "build/build_config.h" |
15 | 14 |
16 using base::TimeDelta; | 15 using base::TimeDelta; |
17 | 16 |
18 namespace tracked_objects { | 17 namespace tracked_objects { |
19 | 18 |
20 namespace { | 19 namespace { |
21 // Flag to compile out almost all of the task tracking code. | 20 // Flag to compile out almost all of the task tracking code. |
(...skipping 25 matching lines...) Expand all Loading... |
47 DurationInt DeathData::AverageMsQueueDuration() const { | 46 DurationInt DeathData::AverageMsQueueDuration() const { |
48 return queue_time_.AverageMsDuration(count_); | 47 return queue_time_.AverageMsDuration(count_); |
49 } | 48 } |
50 | 49 |
51 void DeathData::AddDeathData(const DeathData& other) { | 50 void DeathData::AddDeathData(const DeathData& other) { |
52 count_ += other.count_; | 51 count_ += other.count_; |
53 queue_time_.AddData(other.queue_time_); | 52 queue_time_.AddData(other.queue_time_); |
54 run_time_.AddData(other.run_time_); | 53 run_time_.AddData(other.run_time_); |
55 } | 54 } |
56 | 55 |
57 void DeathData::WriteHTML(std::string* output) const { | |
58 if (!count_) | |
59 return; | |
60 base::StringAppendF(output, "%s:%d, ", | |
61 (count_ == 1) ? "Life" : "Lives", count_); | |
62 output->append("Run:"); | |
63 run_time_.WriteHTML(count_, output); | |
64 | |
65 output->append("Queue:"); | |
66 queue_time_.WriteHTML(count_, output); | |
67 } | |
68 | |
69 base::DictionaryValue* DeathData::ToValue() const { | 56 base::DictionaryValue* DeathData::ToValue() const { |
70 base::DictionaryValue* dictionary = new base::DictionaryValue; | 57 base::DictionaryValue* dictionary = new base::DictionaryValue; |
71 dictionary->Set("count", base::Value::CreateIntegerValue(count_)); | 58 dictionary->Set("count", base::Value::CreateIntegerValue(count_)); |
72 dictionary->Set("run_ms", | 59 dictionary->Set("run_ms", |
73 base::Value::CreateIntegerValue(run_time_.duration())); | 60 base::Value::CreateIntegerValue(run_time_.duration())); |
74 dictionary->Set("queue_ms", | 61 dictionary->Set("queue_ms", |
75 base::Value::CreateIntegerValue(queue_time_.duration())); | 62 base::Value::CreateIntegerValue(queue_time_.duration())); |
76 dictionary->Set("run_ms_max", | 63 dictionary->Set("run_ms_max", |
77 base::Value::CreateIntegerValue(run_time_.max())); | 64 base::Value::CreateIntegerValue(run_time_.max())); |
78 dictionary->Set("queue_ms_max", | 65 dictionary->Set("queue_ms_max", |
79 base::Value::CreateIntegerValue(queue_time_.max())); | 66 base::Value::CreateIntegerValue(queue_time_.max())); |
80 return dictionary; | 67 return dictionary; |
81 } | 68 } |
82 | 69 |
83 void DeathData::Clear() { | 70 void DeathData::Clear() { |
84 count_ = 0; | 71 count_ = 0; |
85 run_time_.Clear(); | 72 run_time_.Clear(); |
86 queue_time_.Clear(); | 73 queue_time_.Clear(); |
87 } | 74 } |
88 | 75 |
89 //------------------------------------------------------------------------------ | 76 //------------------------------------------------------------------------------ |
90 | 77 |
91 void DeathData::Data::WriteHTML(int count, std::string* output) const { | |
92 // Be careful to leave static_casts intact, as the type returned by | |
93 // InMilliseconds() may not always be an int, even if it can generally fit | |
94 // into an int. | |
95 base::StringAppendF(output, "%dms", | |
96 static_cast<int>(duration_)); | |
97 if (count == 1) { | |
98 output->append(" "); | |
99 return; | |
100 } | |
101 base::StringAppendF(output, "(%dms/life,max:%dms) ", | |
102 static_cast<int>(AverageMsDuration(count)), | |
103 static_cast<int>(max_)); | |
104 } | |
105 | |
106 void DeathData::Data::AddData(const Data& other) { | 78 void DeathData::Data::AddData(const Data& other) { |
107 duration_ += other.duration_; | 79 duration_ += other.duration_; |
108 if (max_ > other.max_) | 80 if (max_ > other.max_) |
109 return; | 81 return; |
110 max_ = other.max_; | 82 max_ = other.max_; |
111 } | 83 } |
112 | 84 |
113 void DeathData::Data::AddDuration(DurationInt duration) { | 85 void DeathData::Data::AddDuration(DurationInt duration) { |
114 duration_ += duration; | 86 duration_ += duration; |
115 if (max_ > duration) | 87 if (max_ > duration) |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
263 // Handle case where we are in unit tests, and have become UNINITIALIZED. | 235 // Handle case where we are in unit tests, and have become UNINITIALIZED. |
264 // In that case, the pool might be NULL. We really should detect this via the | 236 // In that case, the pool might be NULL. We really should detect this via the |
265 // incarnation_counter_, but this call is rarely made, so we can afford to | 237 // incarnation_counter_, but this call is rarely made, so we can afford to |
266 // code defensively. | 238 // code defensively. |
267 if (!unregistered_thread_data_pool_) | 239 if (!unregistered_thread_data_pool_) |
268 unregistered_thread_data_pool_ = new ThreadDataPool; | 240 unregistered_thread_data_pool_ = new ThreadDataPool; |
269 unregistered_thread_data_pool_->push(this); | 241 unregistered_thread_data_pool_->push(this); |
270 } | 242 } |
271 | 243 |
272 // static | 244 // static |
273 void ThreadData::WriteHTML(const std::string& query, std::string* output) { | |
274 if (status_ == UNINITIALIZED) | |
275 return; // Not yet initialized. | |
276 | |
277 DataCollector collected_data; // Gather data. | |
278 collected_data.AddListOfLivingObjects(); // Add births that are still alive. | |
279 | |
280 // Data Gathering is complete. Now to sort/process/render. | |
281 DataCollector::Collection* collection = collected_data.collection(); | |
282 | |
283 // Create filtering and sort comparison object. | |
284 Comparator comparator; | |
285 comparator.ParseQuery(query); | |
286 | |
287 // Filter out acceptable (matching) instances. | |
288 DataCollector::Collection match_array; | |
289 for (DataCollector::Collection::iterator it = collection->begin(); | |
290 it != collection->end(); ++it) { | |
291 if (comparator.Acceptable(*it)) | |
292 match_array.push_back(*it); | |
293 } | |
294 | |
295 comparator.Sort(&match_array); | |
296 | |
297 WriteHTMLTotalAndSubtotals(match_array, comparator, output); | |
298 | |
299 comparator.Clear(); // Delete tiebreaker_ instances. | |
300 | |
301 output->append("</pre>"); | |
302 | |
303 const char* help_string = "The following are the keywords that can be used to" | |
304 " sort and aggregate the data, or to select data.<br><ul>" | |
305 "<li><b>Count</b> Number of instances seen." | |
306 "<li><b>Duration</b> Average duration in ms of Run() time." | |
307 "<li><b>TotalDuration</b> Summed durations in ms of Run() times." | |
308 "<li><b>MaxDuration</b> Largest duration in ms of Run() times." | |
309 "<li><b>AverageQueueDuration</b> Average duration in ms of queueing time." | |
310 "<li><b>TotalQueueDuration</b> Summed queuing durations in ms." | |
311 "<li><b>MaxQueueDuration</b> Largest duration in ms of queueing times." | |
312 "<li><b>Birth</b> Thread on which the task was constructed." | |
313 "<li><b>Death</b> Thread on which the task was run and deleted." | |
314 "<li><b>File</b> File in which the task was contructed." | |
315 "<li><b>Function</b> Function in which the task was constructed." | |
316 "<li><b>Line</b> Line number of the file in which the task was constructed." | |
317 "</ul><br>" | |
318 "As examples:<ul>" | |
319 "<li><b>about:tracking/file</b> would sort the above data by file, and" | |
320 " aggregate data on a per-file basis." | |
321 "<li><b>about:tracking/file=Dns</b> would only list data for tasks" | |
322 " constructed in a file containing the text |Dns|." | |
323 "<li><b>about:tracking/death/duration</b> would sort the data by death" | |
324 " thread(i.e., where tasks ran) and then by the average runtime for the" | |
325 " tasks. Form an aggregation group, one per thread, showing the results on" | |
326 " each thread." | |
327 "<li><b>about:tracking/birth/death</b> would sort the above list by birth" | |
328 " thread, and then by death thread, and would aggregate data for each pair" | |
329 " of lifetime events." | |
330 "</ul>" | |
331 " The data can be reset to zero (discarding all births, deaths, etc.) using" | |
332 " <b>about:tracking/reset</b>. The existing stats will be displayed, but" | |
333 " the internal stats will be set to zero, and start accumulating afresh." | |
334 " This option is very helpful if you only wish to consider tasks created" | |
335 " after some point in time.<br><br>" | |
336 "If you wish to monitor Renderer events, be sure to run in --single-process" | |
337 " mode."; | |
338 output->append(help_string); | |
339 } | |
340 | |
341 // static | |
342 void ThreadData::WriteHTMLTotalAndSubtotals( | |
343 const DataCollector::Collection& match_array, | |
344 const Comparator& comparator, | |
345 std::string* output) { | |
346 if (match_array.empty()) { | |
347 output->append("There were no tracked matches."); | |
348 return; | |
349 } | |
350 // Aggregate during printing | |
351 Aggregation totals; | |
352 for (size_t i = 0; i < match_array.size(); ++i) { | |
353 totals.AddDeathSnapshot(match_array[i]); | |
354 } | |
355 output->append("Aggregate Stats: "); | |
356 totals.WriteHTML(output); | |
357 output->append("<hr><hr>"); | |
358 | |
359 Aggregation subtotals; | |
360 for (size_t i = 0; i < match_array.size(); ++i) { | |
361 if (0 == i || !comparator.Equivalent(match_array[i - 1], | |
362 match_array[i])) { | |
363 // Print group's defining characteristics. | |
364 comparator.WriteSortGrouping(match_array[i], output); | |
365 output->append("<br><br>"); | |
366 } | |
367 comparator.WriteSnapshotHTML(match_array[i], output); | |
368 output->append("<br>"); | |
369 subtotals.AddDeathSnapshot(match_array[i]); | |
370 if (i + 1 >= match_array.size() || | |
371 !comparator.Equivalent(match_array[i], | |
372 match_array[i + 1])) { | |
373 // Print aggregate stats for the group. | |
374 output->append("<br>"); | |
375 subtotals.WriteHTML(output); | |
376 output->append("<br><hr><br>"); | |
377 subtotals.Clear(); | |
378 } | |
379 } | |
380 } | |
381 | |
382 // static | |
383 base::DictionaryValue* ThreadData::ToValue() { | 245 base::DictionaryValue* ThreadData::ToValue() { |
384 DataCollector collected_data; // Gather data. | 246 DataCollector collected_data; // Gather data. |
385 collected_data.AddListOfLivingObjects(); // Add births that are still alive. | 247 collected_data.AddListOfLivingObjects(); // Add births that are still alive. |
386 base::ListValue* list = collected_data.ToValue(); | 248 base::ListValue* list = collected_data.ToValue(); |
387 base::DictionaryValue* dictionary = new base::DictionaryValue(); | 249 base::DictionaryValue* dictionary = new base::DictionaryValue(); |
388 dictionary->Set("list", list); | 250 dictionary->Set("list", list); |
389 return dictionary; | 251 return dictionary; |
390 } | 252 } |
391 | 253 |
392 Births* ThreadData::TallyABirth(const Location& location) { | 254 Births* ThreadData::TallyABirth(const Location& location) { |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
528 | 390 |
529 ThreadData* current_thread_data = Get(); | 391 ThreadData* current_thread_data = Get(); |
530 if (!current_thread_data) | 392 if (!current_thread_data) |
531 return; | 393 return; |
532 | 394 |
533 DurationInt queue_duration = 0; | 395 DurationInt queue_duration = 0; |
534 DurationInt run_duration = (end_of_run - start_of_run).InMilliseconds(); | 396 DurationInt run_duration = (end_of_run - start_of_run).InMilliseconds(); |
535 current_thread_data->TallyADeath(*birth, queue_duration, run_duration); | 397 current_thread_data->TallyADeath(*birth, queue_duration, run_duration); |
536 } | 398 } |
537 | 399 |
538 | |
539 | |
540 // static | 400 // static |
541 ThreadData* ThreadData::first() { | 401 ThreadData* ThreadData::first() { |
542 base::AutoLock lock(*list_lock_.Pointer()); | 402 base::AutoLock lock(*list_lock_.Pointer()); |
543 return all_thread_data_list_head_; | 403 return all_thread_data_list_head_; |
544 } | 404 } |
545 | 405 |
546 // This may be called from another thread. | 406 // This may be called from another thread. |
547 void ThreadData::SnapshotBirthMap(BirthMap *output) const { | 407 void ThreadData::SnapshotBirthMap(BirthMap *output) const { |
548 base::AutoLock lock(lock_); | 408 base::AutoLock lock(lock_); |
549 for (BirthMap::const_iterator it = birth_map_.begin(); | 409 for (BirthMap::const_iterator it = birth_map_.begin(); |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
701 death_thread_(NULL), | 561 death_thread_(NULL), |
702 death_data_(DeathData(count)) { | 562 death_data_(DeathData(count)) { |
703 } | 563 } |
704 | 564 |
705 const std::string Snapshot::DeathThreadName() const { | 565 const std::string Snapshot::DeathThreadName() const { |
706 if (death_thread_) | 566 if (death_thread_) |
707 return death_thread_->thread_name(); | 567 return death_thread_->thread_name(); |
708 return "Still_Alive"; | 568 return "Still_Alive"; |
709 } | 569 } |
710 | 570 |
711 void Snapshot::WriteHTML(std::string* output) const { | |
712 death_data_.WriteHTML(output); | |
713 base::StringAppendF(output, "%s->%s ", | |
714 birth_->birth_thread()->thread_name().c_str(), | |
715 DeathThreadName().c_str()); | |
716 birth_->location().Write(true, true, output); | |
717 } | |
718 | |
719 base::DictionaryValue* Snapshot::ToValue() const { | 571 base::DictionaryValue* Snapshot::ToValue() const { |
720 base::DictionaryValue* dictionary = new base::DictionaryValue; | 572 base::DictionaryValue* dictionary = new base::DictionaryValue; |
721 dictionary->Set("death_data", death_data_.ToValue()); | 573 dictionary->Set("death_data", death_data_.ToValue()); |
722 dictionary->Set("birth_thread", | 574 dictionary->Set("birth_thread", |
723 base::Value::CreateStringValue(birth_->birth_thread()->thread_name())); | 575 base::Value::CreateStringValue(birth_->birth_thread()->thread_name())); |
724 dictionary->Set("death_thread", | 576 dictionary->Set("death_thread", |
725 base::Value::CreateStringValue(DeathThreadName())); | 577 base::Value::CreateStringValue(DeathThreadName())); |
726 dictionary->Set("location", birth_->location().ToValue()); | 578 dictionary->Set("location", birth_->location().ToValue()); |
727 return dictionary; | 579 return dictionary; |
728 } | 580 } |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
785 } | 637 } |
786 | 638 |
787 base::ListValue* DataCollector::ToValue() const { | 639 base::ListValue* DataCollector::ToValue() const { |
788 base::ListValue* list = new base::ListValue; | 640 base::ListValue* list = new base::ListValue; |
789 for (size_t i = 0; i < collection_.size(); ++i) { | 641 for (size_t i = 0; i < collection_.size(); ++i) { |
790 list->Append(collection_[i].ToValue()); | 642 list->Append(collection_[i].ToValue()); |
791 } | 643 } |
792 return list; | 644 return list; |
793 } | 645 } |
794 | 646 |
795 //------------------------------------------------------------------------------ | |
796 // Aggregation | |
797 | |
798 Aggregation::Aggregation() | |
799 : birth_count_(0) { | |
800 } | |
801 | |
802 Aggregation::~Aggregation() { | |
803 } | |
804 | |
805 void Aggregation::AddDeathSnapshot(const Snapshot& snapshot) { | |
806 AddBirth(snapshot.birth()); | |
807 death_threads_[snapshot.death_thread()]++; | |
808 AddDeathData(snapshot.death_data()); | |
809 } | |
810 | |
811 void Aggregation::AddBirths(const Births& births) { | |
812 AddBirth(births); | |
813 birth_count_ += births.birth_count(); | |
814 } | |
815 void Aggregation::AddBirth(const BirthOnThread& birth) { | |
816 AddBirthPlace(birth.location()); | |
817 birth_threads_[birth.birth_thread()]++; | |
818 } | |
819 | |
820 void Aggregation::AddBirthPlace(const Location& location) { | |
821 locations_[location]++; | |
822 birth_files_[location.file_name()]++; | |
823 } | |
824 | |
825 void Aggregation::WriteHTML(std::string* output) const { | |
826 if (locations_.size() == 1) { | |
827 locations_.begin()->first.Write(true, true, output); | |
828 } else { | |
829 base::StringAppendF(output, "%" PRIuS " Locations. ", locations_.size()); | |
830 if (birth_files_.size() > 1) { | |
831 base::StringAppendF(output, "%" PRIuS " Files. ", birth_files_.size()); | |
832 } else { | |
833 base::StringAppendF(output, "All born in %s. ", | |
834 birth_files_.begin()->first.c_str()); | |
835 } | |
836 } | |
837 | |
838 if (birth_threads_.size() > 1) { | |
839 base::StringAppendF(output, "%" PRIuS " BirthingThreads. ", | |
840 birth_threads_.size()); | |
841 } else { | |
842 base::StringAppendF(output, "All born on %s. ", | |
843 birth_threads_.begin()->first->thread_name().c_str()); | |
844 } | |
845 | |
846 if (death_threads_.size() > 1) { | |
847 base::StringAppendF(output, "%" PRIuS " DeathThreads. ", | |
848 death_threads_.size()); | |
849 } else { | |
850 if (death_threads_.begin()->first) { | |
851 base::StringAppendF(output, "All deleted on %s. ", | |
852 death_threads_.begin()->first->thread_name().c_str()); | |
853 } else { | |
854 output->append("All these objects are still alive."); | |
855 } | |
856 } | |
857 | |
858 if (birth_count_ > 1) | |
859 base::StringAppendF(output, "Births=%d ", birth_count_); | |
860 | |
861 DeathData::WriteHTML(output); | |
862 } | |
863 | |
864 void Aggregation::Clear() { | |
865 birth_count_ = 0; | |
866 birth_files_.clear(); | |
867 locations_.clear(); | |
868 birth_threads_.clear(); | |
869 DeathData::Clear(); | |
870 death_threads_.clear(); | |
871 } | |
872 | |
873 //------------------------------------------------------------------------------ | |
874 // Comparison object for sorting. | |
875 | |
876 Comparator::Comparator() | |
877 : selector_(NIL), | |
878 tiebreaker_(NULL), | |
879 combined_selectors_(0), | |
880 use_tiebreaker_for_sort_only_(false) {} | |
881 | |
882 void Comparator::Clear() { | |
883 if (tiebreaker_) { | |
884 tiebreaker_->Clear(); | |
885 delete tiebreaker_; | |
886 tiebreaker_ = NULL; | |
887 } | |
888 use_tiebreaker_for_sort_only_ = false; | |
889 selector_ = NIL; | |
890 } | |
891 | |
892 // static | |
893 Comparator::Selector Comparator::FindSelector(const std::string& keyword) { | |
894 // Sorting and aggretation keywords, which specify how to sort the data, or | |
895 // can specify a required match from the specified field in the record. | |
896 if (0 == keyword.compare("count")) | |
897 return COUNT; | |
898 if (0 == keyword.compare("totalduration")) | |
899 return TOTAL_RUN_DURATION; | |
900 if (0 == keyword.compare("duration")) | |
901 return AVERAGE_RUN_DURATION; | |
902 if (0 == keyword.compare("maxduration")) | |
903 return MAX_RUN_DURATION; | |
904 if (0 == keyword.compare("totalqueueduration")) | |
905 return TOTAL_QUEUE_DURATION; | |
906 if (0 == keyword.compare("averagequeueduration")) | |
907 return AVERAGE_QUEUE_DURATION; | |
908 if (0 == keyword.compare("maxqueueduration")) | |
909 return MAX_QUEUE_DURATION; | |
910 if (0 == keyword.compare("birth")) | |
911 return BIRTH_THREAD; | |
912 if (0 == keyword.compare("death")) | |
913 return DEATH_THREAD; | |
914 if (0 == keyword.compare("file")) | |
915 return BIRTH_FILE; | |
916 if (0 == keyword.compare("function")) | |
917 return BIRTH_FUNCTION; | |
918 if (0 == keyword.compare("line")) | |
919 return BIRTH_LINE; | |
920 if (0 == keyword.compare("reset")) | |
921 return RESET_ALL_DATA; | |
922 return UNKNOWN_KEYWORD; | |
923 } | |
924 | |
925 bool Comparator::operator()(const Snapshot& left, | |
926 const Snapshot& right) const { | |
927 switch (selector_) { | |
928 case BIRTH_THREAD: | |
929 if (left.birth_thread() != right.birth_thread() && | |
930 left.birth_thread()->thread_name() != | |
931 right.birth_thread()->thread_name()) | |
932 return left.birth_thread()->thread_name() < | |
933 right.birth_thread()->thread_name(); | |
934 break; | |
935 | |
936 case DEATH_THREAD: | |
937 if (left.death_thread() != right.death_thread() && | |
938 left.DeathThreadName() != | |
939 right.DeathThreadName()) { | |
940 if (!left.death_thread()) | |
941 return true; | |
942 if (!right.death_thread()) | |
943 return false; | |
944 return left.DeathThreadName() < | |
945 right.DeathThreadName(); | |
946 } | |
947 break; | |
948 | |
949 case BIRTH_FILE: | |
950 if (left.location().file_name() != right.location().file_name()) { | |
951 int comp = strcmp(left.location().file_name(), | |
952 right.location().file_name()); | |
953 if (comp) | |
954 return 0 > comp; | |
955 } | |
956 break; | |
957 | |
958 case BIRTH_FUNCTION: | |
959 if (left.location().function_name() != right.location().function_name()) { | |
960 int comp = strcmp(left.location().function_name(), | |
961 right.location().function_name()); | |
962 if (comp) | |
963 return 0 > comp; | |
964 } | |
965 break; | |
966 | |
967 case BIRTH_LINE: | |
968 if (left.location().line_number() != right.location().line_number()) | |
969 return left.location().line_number() < | |
970 right.location().line_number(); | |
971 break; | |
972 | |
973 case COUNT: | |
974 if (left.count() != right.count()) | |
975 return left.count() > right.count(); // Sort large at front of vector. | |
976 break; | |
977 | |
978 case AVERAGE_RUN_DURATION: | |
979 if (!left.count() || !right.count()) | |
980 break; | |
981 if (left.AverageMsRunDuration() != right.AverageMsRunDuration()) | |
982 return left.AverageMsRunDuration() > right.AverageMsRunDuration(); | |
983 break; | |
984 | |
985 case TOTAL_RUN_DURATION: | |
986 if (!left.count() || !right.count()) | |
987 break; | |
988 if (left.run_duration() != right.run_duration()) | |
989 return left.run_duration() > right.run_duration(); | |
990 break; | |
991 | |
992 case MAX_RUN_DURATION: | |
993 if (!left.count() || !right.count()) | |
994 break; | |
995 if (left.run_duration_max() != right.run_duration_max()) | |
996 return left.run_duration_max() > right.run_duration_max(); | |
997 break; | |
998 | |
999 case AVERAGE_QUEUE_DURATION: | |
1000 if (!left.count() || !right.count()) | |
1001 break; | |
1002 if (left.AverageMsQueueDuration() != right.AverageMsQueueDuration()) | |
1003 return left.AverageMsQueueDuration() > right.AverageMsQueueDuration(); | |
1004 break; | |
1005 | |
1006 case TOTAL_QUEUE_DURATION: | |
1007 if (!left.count() || !right.count()) | |
1008 break; | |
1009 if (left.queue_duration() != right.queue_duration()) | |
1010 return left.queue_duration() > right.queue_duration(); | |
1011 break; | |
1012 | |
1013 case MAX_QUEUE_DURATION: | |
1014 if (!left.count() || !right.count()) | |
1015 break; | |
1016 if (left.queue_duration_max() != right.queue_duration_max()) | |
1017 return left.queue_duration_max() > right.queue_duration_max(); | |
1018 break; | |
1019 | |
1020 default: | |
1021 break; | |
1022 } | |
1023 if (tiebreaker_) | |
1024 return tiebreaker_->operator()(left, right); | |
1025 return false; | |
1026 } | |
1027 | |
1028 void Comparator::Sort(DataCollector::Collection* collection) const { | |
1029 std::sort(collection->begin(), collection->end(), *this); | |
1030 } | |
1031 | |
1032 bool Comparator::Equivalent(const Snapshot& left, | |
1033 const Snapshot& right) const { | |
1034 switch (selector_) { | |
1035 case BIRTH_THREAD: | |
1036 if (left.birth_thread() != right.birth_thread() && | |
1037 left.birth_thread()->thread_name() != | |
1038 right.birth_thread()->thread_name()) | |
1039 return false; | |
1040 break; | |
1041 | |
1042 case DEATH_THREAD: | |
1043 if (left.death_thread() != right.death_thread() && | |
1044 left.DeathThreadName() != right.DeathThreadName()) | |
1045 return false; | |
1046 break; | |
1047 | |
1048 case BIRTH_FILE: | |
1049 if (!required_.empty()) | |
1050 break; // No reason to aggregate when we've filtered out some. | |
1051 if (left.location().file_name() != right.location().file_name()) { | |
1052 int comp = strcmp(left.location().file_name(), | |
1053 right.location().file_name()); | |
1054 if (comp) | |
1055 return false; | |
1056 } | |
1057 break; | |
1058 | |
1059 case BIRTH_FUNCTION: | |
1060 if (!required_.empty()) | |
1061 break; // No reason to aggregate when we've filtered out some. | |
1062 if (left.location().function_name() != right.location().function_name()) { | |
1063 int comp = strcmp(left.location().function_name(), | |
1064 right.location().function_name()); | |
1065 if (comp) | |
1066 return false; | |
1067 } | |
1068 break; | |
1069 | |
1070 case COUNT: | |
1071 case AVERAGE_RUN_DURATION: | |
1072 case TOTAL_RUN_DURATION: | |
1073 case MAX_RUN_DURATION: | |
1074 case AVERAGE_QUEUE_DURATION: | |
1075 case TOTAL_QUEUE_DURATION: | |
1076 case MAX_QUEUE_DURATION: | |
1077 // We don't produce separate aggretation when only counts or times differ. | |
1078 break; | |
1079 | |
1080 default: | |
1081 break; | |
1082 } | |
1083 if (tiebreaker_ && !use_tiebreaker_for_sort_only_) | |
1084 return tiebreaker_->Equivalent(left, right); | |
1085 return true; | |
1086 } | |
1087 | |
1088 bool Comparator::Acceptable(const Snapshot& sample) const { | |
1089 if (required_.size()) { | |
1090 switch (selector_) { | |
1091 case BIRTH_THREAD: | |
1092 if (sample.birth_thread()->thread_name().find(required_) == | |
1093 std::string::npos) | |
1094 return false; | |
1095 break; | |
1096 | |
1097 case DEATH_THREAD: | |
1098 if (sample.DeathThreadName().find(required_) == std::string::npos) | |
1099 return false; | |
1100 break; | |
1101 | |
1102 case BIRTH_FILE: | |
1103 if (!strstr(sample.location().file_name(), required_.c_str())) | |
1104 return false; | |
1105 break; | |
1106 | |
1107 case BIRTH_FUNCTION: | |
1108 if (!strstr(sample.location().function_name(), required_.c_str())) | |
1109 return false; | |
1110 break; | |
1111 | |
1112 default: | |
1113 break; | |
1114 } | |
1115 } | |
1116 if (tiebreaker_ && !use_tiebreaker_for_sort_only_) | |
1117 return tiebreaker_->Acceptable(sample); | |
1118 return true; | |
1119 } | |
1120 | |
1121 void Comparator::SetTiebreaker(Selector selector, const std::string& required) { | |
1122 if (selector == selector_ || NIL == selector) | |
1123 return; | |
1124 combined_selectors_ |= selector; | |
1125 if (NIL == selector_) { | |
1126 selector_ = selector; | |
1127 if (required.size()) | |
1128 required_ = required; | |
1129 return; | |
1130 } | |
1131 if (tiebreaker_) { | |
1132 if (use_tiebreaker_for_sort_only_) { | |
1133 Comparator* temp = new Comparator; | |
1134 temp->tiebreaker_ = tiebreaker_; | |
1135 tiebreaker_ = temp; | |
1136 } | |
1137 } else { | |
1138 tiebreaker_ = new Comparator; | |
1139 DCHECK(!use_tiebreaker_for_sort_only_); | |
1140 } | |
1141 tiebreaker_->SetTiebreaker(selector, required); | |
1142 } | |
1143 | |
1144 bool Comparator::IsGroupedBy(Selector selector) const { | |
1145 return 0 != (selector & combined_selectors_); | |
1146 } | |
1147 | |
1148 void Comparator::SetSubgroupTiebreaker(Selector selector) { | |
1149 if (selector == selector_ || NIL == selector) | |
1150 return; | |
1151 if (!tiebreaker_) { | |
1152 use_tiebreaker_for_sort_only_ = true; | |
1153 tiebreaker_ = new Comparator; | |
1154 tiebreaker_->SetTiebreaker(selector, ""); | |
1155 } else { | |
1156 tiebreaker_->SetSubgroupTiebreaker(selector); | |
1157 } | |
1158 } | |
1159 | |
1160 void Comparator::ParseKeyphrase(const std::string& key_phrase) { | |
1161 std::string required; | |
1162 // Watch for: "sort_key=value" as we parse. | |
1163 size_t equal_offset = key_phrase.find('=', 0); | |
1164 if (key_phrase.npos != equal_offset) { | |
1165 // There is a value that must be matched for the data to display. | |
1166 required = key_phrase.substr(equal_offset + 1, key_phrase.npos); | |
1167 } | |
1168 std::string keyword(key_phrase.substr(0, equal_offset)); | |
1169 keyword = StringToLowerASCII(keyword); | |
1170 Selector selector = FindSelector(keyword); | |
1171 if (selector == UNKNOWN_KEYWORD) | |
1172 return; | |
1173 if (selector == RESET_ALL_DATA) { | |
1174 ThreadData::ResetAllThreadData(); | |
1175 return; | |
1176 } | |
1177 SetTiebreaker(selector, required); | |
1178 } | |
1179 | |
1180 bool Comparator::ParseQuery(const std::string& query) { | |
1181 // Parse each keyphrase between consecutive slashes. | |
1182 for (size_t i = 0; i < query.size();) { | |
1183 size_t slash_offset = query.find('/', i); | |
1184 ParseKeyphrase(query.substr(i, slash_offset - i)); | |
1185 if (query.npos == slash_offset) | |
1186 break; | |
1187 i = slash_offset + 1; | |
1188 } | |
1189 | |
1190 // Select subgroup ordering (if we want to display the subgroup) | |
1191 SetSubgroupTiebreaker(COUNT); | |
1192 SetSubgroupTiebreaker(AVERAGE_RUN_DURATION); | |
1193 SetSubgroupTiebreaker(TOTAL_RUN_DURATION); | |
1194 SetSubgroupTiebreaker(MAX_RUN_DURATION); | |
1195 SetSubgroupTiebreaker(AVERAGE_QUEUE_DURATION); | |
1196 SetSubgroupTiebreaker(TOTAL_QUEUE_DURATION); | |
1197 SetSubgroupTiebreaker(MAX_QUEUE_DURATION); | |
1198 SetSubgroupTiebreaker(BIRTH_THREAD); | |
1199 SetSubgroupTiebreaker(DEATH_THREAD); | |
1200 SetSubgroupTiebreaker(BIRTH_FUNCTION); | |
1201 SetSubgroupTiebreaker(BIRTH_FILE); | |
1202 SetSubgroupTiebreaker(BIRTH_LINE); | |
1203 | |
1204 return true; | |
1205 } | |
1206 | |
1207 bool Comparator::WriteSortGrouping(const Snapshot& sample, | |
1208 std::string* output) const { | |
1209 bool wrote_data = false; | |
1210 switch (selector_) { | |
1211 case BIRTH_THREAD: | |
1212 base::StringAppendF(output, "All new on %s ", | |
1213 sample.birth_thread()->thread_name().c_str()); | |
1214 wrote_data = true; | |
1215 break; | |
1216 | |
1217 case DEATH_THREAD: | |
1218 if (sample.death_thread()) { | |
1219 base::StringAppendF(output, "All deleted on %s ", | |
1220 sample.DeathThreadName().c_str()); | |
1221 } else { | |
1222 output->append("All still alive "); | |
1223 } | |
1224 wrote_data = true; | |
1225 break; | |
1226 | |
1227 case BIRTH_FILE: | |
1228 base::StringAppendF(output, "All born in %s ", | |
1229 sample.location().file_name()); | |
1230 break; | |
1231 | |
1232 case BIRTH_FUNCTION: | |
1233 output->append("All born in "); | |
1234 sample.location().WriteFunctionName(output); | |
1235 output->push_back(' '); | |
1236 break; | |
1237 | |
1238 default: | |
1239 break; | |
1240 } | |
1241 if (tiebreaker_ && !use_tiebreaker_for_sort_only_) { | |
1242 wrote_data |= tiebreaker_->WriteSortGrouping(sample, output); | |
1243 } | |
1244 return wrote_data; | |
1245 } | |
1246 | |
1247 void Comparator::WriteSnapshotHTML(const Snapshot& sample, | |
1248 std::string* output) const { | |
1249 sample.death_data().WriteHTML(output); | |
1250 if (!(combined_selectors_ & BIRTH_THREAD) || | |
1251 !(combined_selectors_ & DEATH_THREAD)) | |
1252 base::StringAppendF(output, "%s->%s ", | |
1253 (combined_selectors_ & BIRTH_THREAD) ? "*" : | |
1254 sample.birth().birth_thread()->thread_name().c_str(), | |
1255 (combined_selectors_ & DEATH_THREAD) ? "*" : | |
1256 sample.DeathThreadName().c_str()); | |
1257 sample.birth().location().Write(!(combined_selectors_ & BIRTH_FILE), | |
1258 !(combined_selectors_ & BIRTH_FUNCTION), | |
1259 output); | |
1260 } | |
1261 | |
1262 } // namespace tracked_objects | 647 } // namespace tracked_objects |
OLD | NEW |