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

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: Replace tcmalloc's wrappers with pthread_spinlock and standard allocator (no more tcmalloc changes) Created 5 years, 2 months 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 leak_detector {
12
13 namespace {
14
15 using ValueType = LeakDetectorValueType;
16
17 // Get the top |kRankedListSize| entries.
18 const int kRankedListSize = 16;
19
20 // Initial number of hash table buckets.
21 const int kInitialHashTableSize = 1999;
22
23 } // namespace
24
25 size_t CallStackTable::StoredHash::operator() (
26 const CallStack* call_stack) const {
27 // The call stack object should already have a hash computed when it was
28 // created.
29 //
30 // This is NOT the actual hash computation function for a new call stack.
31 return call_stack->hash;
32 }
33
34 CallStackTable::CallStackTable(int call_stack_suspicion_threshold)
35 : num_allocs_(0),
36 num_frees_(0),
37 entry_map_(kInitialHashTableSize),
38 leak_analyzer_(kRankedListSize, call_stack_suspicion_threshold) {
39 }
40
41 CallStackTable::~CallStackTable() {}
42
43 void CallStackTable::Add(const CallStack* call_stack) {
44 auto iter = entry_map_.find(call_stack);
45 Entry* entry = nullptr;
46 if (iter == entry_map_.end()) {
47 entry = &entry_map_[call_stack];
48 } else {
49 entry = &iter->second;
50 }
51
52 ++entry->net_num_allocs;
53 ++num_allocs_;
54 }
55
56 void CallStackTable::Remove(const CallStack* call_stack) {
57 auto iter = entry_map_.find(call_stack);
58 if (iter == entry_map_.end())
59 return;
60 Entry* entry = &iter->second;
61 --entry->net_num_allocs;
62 ++num_frees_;
63
64 // Delete zero-alloc entries to free up space.
65 if (entry->net_num_allocs == 0)
66 entry_map_.erase(iter);
67 }
68
69 size_t CallStackTable::Dump(const size_t buffer_size, char* buffer) const {
70 size_t size_left = buffer_size;
71
72 if (entry_map_.empty())
73 return size_left;
74
75 int attempted_size =
76 snprintf(buffer, size_left,
77 "Total number of allocations: %u\n"
78 "Total number of frees: %u\n"
79 "Net number of allocations: %u\n"
80 "Total number of distinct stack traces: %zu\n",
81 num_allocs_, num_frees_, num_allocs_ - num_frees_,
82 entry_map_.size());
83 size_left -= attempted_size;
84 buffer += attempted_size;
85
86 if (size_left > 1) {
87 int attempted_size = leak_analyzer_.Dump(size_left, buffer);
88 size_left -= attempted_size;
89 buffer += attempted_size;
90 }
91
92 return buffer_size - size_left;
93 }
94
95 void CallStackTable::TestForLeaks() {
96 // Add all entries to the ranked list.
97 RankedList ranked_list(kRankedListSize);
98
99 for (const auto& entry_pair : entry_map_) {
100 const Entry& entry = entry_pair.second;
101 if (entry.net_num_allocs > 0) {
102 LeakDetectorValueType call_stack_value(entry_pair.first);
103 ranked_list.Add(call_stack_value, entry.net_num_allocs);
104 }
105 }
106 leak_analyzer_.AddSample(std::move(ranked_list));
107 }
108
109 } // namespace leak_detector
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698