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

Side by Side Diff: components/metrics/leak_detector/call_stack_table.cc

Issue 986503002: components/metrics: Add runtime memory leak detector (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: use namespace metrics::leak_detector; remove GYP flag (not needed for this CL) Created 5 years, 1 month 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 #include "components/metrics/leak_detector/call_stack_table.h"
6
7 #include <utility>
8
9 #include "components/metrics/leak_detector/call_stack_manager.h"
10
11 namespace metrics {
12 namespace leak_detector {
13
14 namespace {
15
16 using ValueType = LeakDetectorValueType;
17
18 // Get the top |kRankedListSize| entries.
19 const int kRankedListSize = 16;
20
21 // Initial number of hash table buckets.
22 const int kInitialHashTableSize = 1999;
23
24 } // namespace
25
26 size_t CallStackTable::StoredHash::operator() (
27 const CallStack* call_stack) const {
28 // The call stack object should already have a hash computed when it was
29 // created.
30 //
31 // This is NOT the actual hash computation function for a new call stack.
32 return call_stack->hash;
33 }
34
35 CallStackTable::CallStackTable(int call_stack_suspicion_threshold)
36 : num_allocs_(0),
37 num_frees_(0),
38 entry_map_(kInitialHashTableSize),
39 leak_analyzer_(kRankedListSize, call_stack_suspicion_threshold) {
40 }
41
42 CallStackTable::~CallStackTable() {}
43
44 void CallStackTable::Add(const CallStack* call_stack) {
45 auto iter = entry_map_.find(call_stack);
46 Entry* entry = nullptr;
47 if (iter == entry_map_.end()) {
48 entry = &entry_map_[call_stack];
49 } else {
50 entry = &iter->second;
51 }
52
53 ++entry->net_num_allocs;
54 ++num_allocs_;
55 }
56
57 void CallStackTable::Remove(const CallStack* call_stack) {
58 auto iter = entry_map_.find(call_stack);
59 if (iter == entry_map_.end())
60 return;
61 Entry* entry = &iter->second;
62 --entry->net_num_allocs;
63 ++num_frees_;
64
65 // Delete zero-alloc entries to free up space.
66 if (entry->net_num_allocs == 0)
67 entry_map_.erase(iter);
68 }
69
70 size_t CallStackTable::Dump(const size_t buffer_size, char* buffer) const {
71 size_t size_left = buffer_size;
72
73 if (entry_map_.empty())
74 return size_left;
75
76 int attempted_size =
77 snprintf(buffer, size_left,
78 "Total number of allocations: %u\n"
79 "Total number of frees: %u\n"
80 "Net number of allocations: %u\n"
81 "Total number of distinct stack traces: %zu\n",
82 num_allocs_, num_frees_, num_allocs_ - num_frees_,
83 entry_map_.size());
84 size_left -= attempted_size;
85 buffer += attempted_size;
86
87 if (size_left > 1) {
88 int attempted_size = leak_analyzer_.Dump(size_left, buffer);
89 size_left -= attempted_size;
90 buffer += attempted_size;
91 }
92
93 return buffer_size - size_left;
94 }
95
96 void CallStackTable::TestForLeaks() {
97 // Add all entries to the ranked list.
98 RankedList ranked_list(kRankedListSize);
99
100 for (const auto& entry_pair : entry_map_) {
101 const Entry& entry = entry_pair.second;
102 if (entry.net_num_allocs > 0) {
103 LeakDetectorValueType call_stack_value(entry_pair.first);
104 ranked_list.Add(call_stack_value, entry.net_num_allocs);
105 }
106 }
107 leak_analyzer_.AddSample(std::move(ranked_list));
108 }
109
110 } // namespace leak_detector
111 } // namespace metrics
OLDNEW
« no previous file with comments | « components/metrics/leak_detector/call_stack_table.h ('k') | components/metrics/leak_detector/call_stack_table_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698