Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(14)

Side by Side Diff: components/metrics/leak_detector/leak_analyzer.h

Issue 1472763003: Reland of: components/metrics: Add runtime memory leak detector (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebased Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 // This class uses CustomAllocator to avoid recursive malloc hook invocation
27 // when analyzing allocs and frees.
28 template <typename Type>
29 using Allocator = STLAllocator<Type, CustomAllocator>;
30
31 LeakAnalyzer(uint32_t ranking_size, uint32_t num_suspicions_threshold);
32 ~LeakAnalyzer();
33
34 // Take in a RankedList of allocations, sorted by count. Removes the contents
35 // of |ranked_list| to be stored internally, which is why it is not passed in
36 // as a const reference.
37 void AddSample(RankedList ranked_list);
38
39 // Used to report suspected leaks. Reported leaks are sorted by ValueType.
40 const std::vector<ValueType, Allocator<ValueType>>& suspected_leaks() const {
41 return suspected_leaks_;
42 }
43
44 private:
45 // Analyze a list of allocation count deltas from the previous iteration. If
46 // anything looks like a possible leak, update the suspicion scores.
47 void AnalyzeDeltas(const RankedList& ranked_deltas);
48
49 // Returns the count for the given value from the previous analysis in
50 // |count|. Returns true if the given value was present in the previous
51 // analysis, or false if not.
52 bool GetPreviousCountForValue(const ValueType& value, uint32_t* count) const;
53
54 // Look for the top |ranking_size_| entries when analyzing leaks.
55 const uint32_t ranking_size_;
56
57 // Report suspected leaks when the suspicion score reaches this value.
58 const uint32_t score_threshold_;
59
60 // A mapping of allocation values to suspicion score. All allocations in this
61 // container are suspected leaks. The score can increase or decrease over
62 // time. Once the score reaches |score_threshold_|, the entry is reported as
63 // a suspected leak in |suspected_leaks_|.
64 std::map<ValueType,
65 uint32_t,
66 std::less<ValueType>,
67 Allocator<std::pair<ValueType, uint32_t>>> suspected_histogram_;
68
69 // Array of allocated values that passed the suspicion threshold and are being
70 // reported.
71 std::vector<ValueType, Allocator<ValueType>> suspected_leaks_;
72
73 // The most recent allocation entries, since the last call to AddSample().
74 RankedList ranked_entries_;
75 // The previous allocation entries, from before the last call to AddSample().
76 RankedList prev_ranked_entries_;
77
78 DISALLOW_COPY_AND_ASSIGN(LeakAnalyzer);
79 };
80
81 } // namespace leak_detector
82 } // namespace metrics
83
84 #endif // COMPONENTS_METRICS_LEAK_DETECTOR_LEAK_ANALYZER_H_
OLDNEW
« no previous file with comments | « components/metrics/leak_detector/custom_allocator.cc ('k') | components/metrics/leak_detector/leak_analyzer.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698