OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef COMPONENTS_METRICS_LEAK_DETECTOR_LEAK_ANALYZER_H_ |
| 6 #define COMPONENTS_METRICS_LEAK_DETECTOR_LEAK_ANALYZER_H_ |
| 7 |
| 8 #include <map> |
| 9 #include <vector> |
| 10 |
| 11 #include "base/macros.h" |
| 12 #include "components/metrics/leak_detector/custom_allocator.h" |
| 13 #include "components/metrics/leak_detector/leak_detector_value_type.h" |
| 14 #include "components/metrics/leak_detector/ranked_list.h" |
| 15 #include "components/metrics/leak_detector/stl_allocator.h" |
| 16 |
| 17 namespace metrics { |
| 18 namespace leak_detector { |
| 19 |
| 20 // This class looks for possible leak patterns in allocation data over time. |
| 21 // Not thread-safe. |
| 22 class LeakAnalyzer { |
| 23 public: |
| 24 using ValueType = LeakDetectorValueType; |
| 25 |
| 26 template <typename Type> |
| 27 using Allocator = STLAllocator<Type, CustomAllocator>; |
| 28 |
| 29 LeakAnalyzer(uint32_t ranking_size, uint32_t num_suspicions_threshold); |
| 30 ~LeakAnalyzer(); |
| 31 |
| 32 // Take in a RankedList of allocations, sorted by count. Removes the contents |
| 33 // of |ranked_list|, which must be passed in using move semantics. |
| 34 void AddSample(RankedList ranked_list); |
| 35 |
| 36 // Used to report suspected leaks. Reported leaks are sorted by ValueType. |
| 37 const std::vector<ValueType, Allocator<ValueType>>& suspected_leaks() const { |
| 38 return suspected_leaks_; |
| 39 } |
| 40 |
| 41 private: |
| 42 // Analyze a list of allocation count deltas from the previous iteration. If |
| 43 // anything looks like a possible leak, update the suspicion scores. |
| 44 void AnalyzeDeltas(const RankedList& ranked_deltas); |
| 45 |
| 46 // Returns the count for the given value from the previous analysis in |
| 47 // |count|. Returns true if the given value was present in the previous |
| 48 // analysis, or false if not. |
| 49 bool GetPreviousCountForValue(const ValueType& value, uint32_t* count) const; |
| 50 |
| 51 // Look for the top |ranking_size_| entries when analyzing leaks. |
| 52 const uint32_t ranking_size_; |
| 53 |
| 54 // Report suspected leaks when the suspicion score reaches this value. |
| 55 const uint32_t score_threshold_; |
| 56 |
| 57 // A mapping of allocation values to suspicion score. All allocations in this |
| 58 // container are suspected leaks. The score can increase or decrease over |
| 59 // time. Once the score reaches |score_threshold_|, the entry is reported as |
| 60 // a suspected leak in |suspected_leaks_|. |
| 61 std::map<ValueType, |
| 62 uint32_t, |
| 63 std::less<ValueType>, |
| 64 Allocator<std::pair<ValueType, uint32_t>>> suspected_histogram_; |
| 65 |
| 66 // Array of allocated values that passed the suspicion threshold and are being |
| 67 // reported. |
| 68 std::vector<ValueType, Allocator<ValueType>> suspected_leaks_; |
| 69 |
| 70 // The most recent allocation entries, since the last call to AddSample(). |
| 71 RankedList ranked_entries_; |
| 72 // The previous allocation entries, from before the last call to AddSample(). |
| 73 RankedList prev_ranked_entries_; |
| 74 |
| 75 DISALLOW_COPY_AND_ASSIGN(LeakAnalyzer); |
| 76 }; |
| 77 |
| 78 } // namespace leak_detector |
| 79 } // namespace metrics |
| 80 |
| 81 #endif // COMPONENTS_METRICS_LEAK_DETECTOR_LEAK_ANALYZER_H_ |
OLD | NEW |