Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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/trace_event/heap_profiler_allocation_register.h" | 5 #include "base/trace_event/heap_profiler_allocation_register.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/trace_event/trace_event_memory_overhead.h" | 9 #include "base/trace_event/trace_event_memory_overhead.h" |
| 10 | 10 |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 74 : AllocationRegister(kAllocationCapacity, kBacktraceCapacity) {} | 74 : AllocationRegister(kAllocationCapacity, kBacktraceCapacity) {} |
| 75 | 75 |
| 76 AllocationRegister::AllocationRegister(size_t allocation_capacity, | 76 AllocationRegister::AllocationRegister(size_t allocation_capacity, |
| 77 size_t backtrace_capacity) | 77 size_t backtrace_capacity) |
| 78 : allocations_(allocation_capacity), | 78 : allocations_(allocation_capacity), |
| 79 backtraces_(backtrace_capacity) {} | 79 backtraces_(backtrace_capacity) {} |
| 80 | 80 |
| 81 AllocationRegister::~AllocationRegister() { | 81 AllocationRegister::~AllocationRegister() { |
| 82 } | 82 } |
| 83 | 83 |
| 84 void AllocationRegister::Insert(const void* address, | 84 bool AllocationRegister::Insert(const void* address, |
| 85 size_t size, | 85 size_t size, |
| 86 const AllocationContext& context) { | 86 const AllocationContext& context) { |
| 87 DCHECK(address != nullptr); | 87 DCHECK(address != nullptr); |
| 88 if (size == 0) { | 88 if (size == 0) { |
| 89 return; | 89 return false; |
| 90 } | 90 } |
| 91 | 91 |
| 92 AllocationInfo info = { | 92 AllocationInfo info = { |
| 93 size, | 93 size, |
| 94 context.type_name, | 94 context.type_name, |
| 95 InsertBacktrace(context.backtrace) | 95 InsertBacktrace(context.backtrace) |
| 96 }; | 96 }; |
| 97 | 97 |
| 98 // Try to insert the allocation. | 98 // Try to insert the allocation. |
| 99 auto index_and_flag = allocations_.Insert(address, info); | 99 auto index_and_flag = allocations_.Insert(address, info); |
| 100 if (!index_and_flag.second) { | 100 if (!index_and_flag.second && |
| 101 index_and_flag.first != AllocationMap::kInvalidKVIndex) { | |
| 101 // |address| is already there - overwrite the allocation info. | 102 // |address| is already there - overwrite the allocation info. |
| 102 auto& old_info = allocations_.Get(index_and_flag.first).second; | 103 auto& old_info = allocations_.Get(index_and_flag.first).second; |
| 103 RemoveBacktrace(old_info.backtrace_index); | 104 RemoveBacktrace(old_info.backtrace_index); |
| 104 old_info = info; | 105 old_info = info; |
| 105 } | 106 } |
|
Primiano Tucci (use gerrit)
2017/03/31 19:05:47
minor thing, but this branch should return true,
| |
| 107 | |
| 108 return index_and_flag.second; | |
| 106 } | 109 } |
| 107 | 110 |
| 108 void AllocationRegister::Remove(const void* address) { | 111 void AllocationRegister::Remove(const void* address) { |
| 109 auto index = allocations_.Find(address); | 112 auto index = allocations_.Find(address); |
| 110 if (index == AllocationMap::kInvalidKVIndex) { | 113 if (index == AllocationMap::kInvalidKVIndex) { |
| 111 return; | 114 return; |
| 112 } | 115 } |
| 113 | 116 |
| 114 const AllocationInfo& info = allocations_.Get(index).second; | 117 const AllocationInfo& info = allocations_.Get(index).second; |
| 115 RemoveBacktrace(info.backtrace_index); | 118 RemoveBacktrace(info.backtrace_index); |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 142 size_t allocated = sizeof(AllocationRegister); | 145 size_t allocated = sizeof(AllocationRegister); |
| 143 size_t resident = sizeof(AllocationRegister) | 146 size_t resident = sizeof(AllocationRegister) |
| 144 + allocations_.EstimateUsedMemory() | 147 + allocations_.EstimateUsedMemory() |
| 145 + backtraces_.EstimateUsedMemory(); | 148 + backtraces_.EstimateUsedMemory(); |
| 146 overhead->Add("AllocationRegister", allocated, resident); | 149 overhead->Add("AllocationRegister", allocated, resident); |
| 147 } | 150 } |
| 148 | 151 |
| 149 AllocationRegister::BacktraceMap::KVIndex AllocationRegister::InsertBacktrace( | 152 AllocationRegister::BacktraceMap::KVIndex AllocationRegister::InsertBacktrace( |
| 150 const Backtrace& backtrace) { | 153 const Backtrace& backtrace) { |
| 151 auto index = backtraces_.Insert(backtrace, 0).first; | 154 auto index = backtraces_.Insert(backtrace, 0).first; |
| 152 auto& backtrace_and_count = backtraces_.Get(index); | 155 if (index != BacktraceMap::kInvalidKVIndex) { |
| 153 backtrace_and_count.second++; | 156 auto& backtrace_and_count = backtraces_.Get(index); |
| 157 backtrace_and_count.second++; | |
| 158 } | |
| 154 return index; | 159 return index; |
| 155 } | 160 } |
| 156 | 161 |
| 157 void AllocationRegister::RemoveBacktrace(BacktraceMap::KVIndex index) { | 162 void AllocationRegister::RemoveBacktrace(BacktraceMap::KVIndex index) { |
| 158 auto& backtrace_and_count = backtraces_.Get(index); | 163 auto& backtrace_and_count = backtraces_.Get(index); |
|
Primiano Tucci (use gerrit)
2017/03/31 19:05:47
I think now you created a bug here.
If you run out
| |
| 159 if (--backtrace_and_count.second == 0) { | 164 if (--backtrace_and_count.second == 0) { |
| 160 // Backtrace is not referenced anymore - remove it. | 165 // Backtrace is not referenced anymore - remove it. |
| 161 backtraces_.Remove(index); | 166 backtraces_.Remove(index); |
| 162 } | 167 } |
| 163 } | 168 } |
| 164 | 169 |
| 165 AllocationRegister::Allocation AllocationRegister::GetAllocation( | 170 AllocationRegister::Allocation AllocationRegister::GetAllocation( |
| 166 AllocationMap::KVIndex index) const { | 171 AllocationMap::KVIndex index) const { |
| 167 const auto& address_and_info = allocations_.Get(index); | 172 const auto& address_and_info = allocations_.Get(index); |
| 168 const auto& backtrace_and_count = backtraces_.Get( | 173 const auto& backtrace_and_count = backtraces_.Get( |
| 169 address_and_info.second.backtrace_index); | 174 address_and_info.second.backtrace_index); |
| 170 return { | 175 return { |
| 171 address_and_info.first, | 176 address_and_info.first, |
| 172 address_and_info.second.size, | 177 address_and_info.second.size, |
| 173 AllocationContext( | 178 AllocationContext( |
| 174 backtrace_and_count.first, | 179 backtrace_and_count.first, |
| 175 address_and_info.second.type_name) | 180 address_and_info.second.type_name) |
| 176 }; | 181 }; |
| 177 } | 182 } |
| 178 | 183 |
| 179 } // namespace trace_event | 184 } // namespace trace_event |
| 180 } // namespace base | 185 } // namespace base |
| OLD | NEW |