OLD | NEW |
---|---|
1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #include "platform/globals.h" | 5 #include "platform/globals.h" |
6 | 6 |
7 #if defined(DART_USE_TCMALLOC) && !defined(PRODUCT) && \ | 7 #if defined(DART_USE_TCMALLOC) && !defined(PRODUCT) && \ |
8 !defined(TARGET_ARCH_DBC) && !defined(TARGET_OS_FUCHSIA) | 8 !defined(TARGET_ARCH_DBC) && !defined(TARGET_OS_FUCHSIA) |
9 | 9 |
10 #include "vm/malloc_hooks.h" | 10 #include "vm/malloc_hooks.h" |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
124 Mutex* mutex_; | 124 Mutex* mutex_; |
125 ThreadId* owner_; | 125 ThreadId* owner_; |
126 }; | 126 }; |
127 | 127 |
128 // AllocationInfo contains all information related to a given allocation | 128 // AllocationInfo contains all information related to a given allocation |
129 // including: | 129 // including: |
130 // -Allocation size in bytes | 130 // -Allocation size in bytes |
131 // -Stack trace corresponding to the location of allocation, if applicable | 131 // -Stack trace corresponding to the location of allocation, if applicable |
132 class AllocationInfo { | 132 class AllocationInfo { |
133 public: | 133 public: |
134 explicit AllocationInfo(intptr_t allocation_size) | 134 explicit AllocationInfo(uword address, intptr_t allocation_size) |
zra
2017/03/16 20:36:37
Don't need 'explicit' when there is more than one
bkonyi
2017/03/21 01:53:23
Done.
| |
135 : sample_(NULL), allocation_size_(allocation_size) { | 135 : sample_(NULL), address_(address), allocation_size_(allocation_size) { |
136 // Stack trace collection is disabled when we are in the process of creating | 136 // Stack trace collection is disabled when we are in the process of creating |
137 // the first OSThread in order to prevent deadlocks. | 137 // the first OSThread in order to prevent deadlocks. |
138 if (MallocHooksState::ProfilingEnabled() && | 138 if (MallocHooksState::ProfilingEnabled() && |
139 MallocHooksState::stack_trace_collection_enabled()) { | 139 MallocHooksState::stack_trace_collection_enabled()) { |
140 sample_ = Profiler::SampleNativeAllocation(kSkipCount); | 140 sample_ = Profiler::SampleNativeAllocation(kSkipCount, address, |
141 allocation_size); | |
142 ASSERT(sample_->native_allocation_address() == address_); | |
141 } | 143 } |
142 } | 144 } |
143 | 145 |
146 ~AllocationInfo() { | |
147 if (sample_ == NULL) { | |
148 return; | |
149 } | |
150 uword allocation_address = sample_->native_allocation_address(); | |
151 if (allocation_address != address_) { | |
zra
2017/03/16 20:36:37
Why might this happen?
bkonyi
2017/03/16 20:48:40
The samples are stored in a ring buffer and are ev
bkonyi
2017/03/21 01:53:23
I've changed how this is done so that we should be
| |
152 return; | |
153 } | |
154 sample_->set_native_allocation_address(0); | |
Cutch
2017/03/16 20:38:16
what about the size too?
bkonyi
2017/03/21 01:53:23
I've changed how I check if a native allocation sa
| |
155 } | |
156 | |
144 Sample* sample() const { return sample_; } | 157 Sample* sample() const { return sample_; } |
145 intptr_t allocation_size() const { return allocation_size_; } | 158 intptr_t allocation_size() const { return allocation_size_; } |
146 | 159 |
147 private: | 160 private: |
148 Sample* sample_; | 161 Sample* sample_; |
zra
2017/03/16 20:36:37
It might be worthwhile to make an explicit note he
bkonyi
2017/03/21 01:53:23
Done.
| |
162 uword address_; | |
149 intptr_t allocation_size_; | 163 intptr_t allocation_size_; |
150 | 164 |
151 // The number of frames that are generated by the malloc hooks and collection | 165 // The number of frames that are generated by the malloc hooks and collection |
152 // of the stack trace. These frames are ignored when collecting the stack | 166 // of the stack trace. These frames are ignored when collecting the stack |
153 // trace for a memory allocation. If this number is incorrect, some tests in | 167 // trace for a memory allocation. If this number is incorrect, some tests in |
154 // malloc_hook_tests.cc might fail, particularily | 168 // malloc_hook_tests.cc might fail, particularily |
155 // StackTraceMallocHookLengthTest. If this value is updated, please make sure | 169 // StackTraceMallocHookLengthTest. If this value is updated, please make sure |
156 // that the MallocHooks test cases pass on all platforms. | 170 // that the MallocHooks test cases pass on all platforms. |
157 static const intptr_t kSkipCount = 5; | 171 static const intptr_t kSkipCount = 6; |
158 }; | 172 }; |
159 | 173 |
160 | 174 |
161 // Custom key/value trait specifically for address/size pairs. Unlike | 175 // Custom key/value trait specifically for address/size pairs. Unlike |
162 // RawPointerKeyValueTrait, the default value is -1 as 0 can be a valid entry. | 176 // RawPointerKeyValueTrait, the default value is -1 as 0 can be a valid entry. |
163 class AddressKeyValueTrait : public AllStatic { | 177 class AddressKeyValueTrait : public AllStatic { |
164 public: | 178 public: |
165 typedef const void* Key; | 179 typedef const void* Key; |
166 typedef AllocationInfo* Value; | 180 typedef AllocationInfo* Value; |
167 | 181 |
(...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
409 if (MallocHooksState::IsLockHeldByCurrentThread() || | 423 if (MallocHooksState::IsLockHeldByCurrentThread() || |
410 !MallocHooksState::IsOriginalProcess()) { | 424 !MallocHooksState::IsOriginalProcess()) { |
411 return; | 425 return; |
412 } | 426 } |
413 | 427 |
414 MallocLocker ml(MallocHooksState::malloc_hook_mutex(), | 428 MallocLocker ml(MallocHooksState::malloc_hook_mutex(), |
415 MallocHooksState::malloc_hook_mutex_owner()); | 429 MallocHooksState::malloc_hook_mutex_owner()); |
416 // Now that we hold the lock, check to make sure everything is still active. | 430 // Now that we hold the lock, check to make sure everything is still active. |
417 if ((ptr != NULL) && MallocHooksState::Active()) { | 431 if ((ptr != NULL) && MallocHooksState::Active()) { |
418 MallocHooksState::IncrementHeapAllocatedMemoryInBytes(size); | 432 MallocHooksState::IncrementHeapAllocatedMemoryInBytes(size); |
419 MallocHooksState::address_map()->Insert(ptr, new AllocationInfo(size)); | 433 MallocHooksState::address_map()->Insert( |
434 ptr, new AllocationInfo(reinterpret_cast<uword>(ptr), size)); | |
420 } | 435 } |
421 } | 436 } |
422 | 437 |
423 | 438 |
424 void MallocHooksState::RecordFreeHook(const void* ptr) { | 439 void MallocHooksState::RecordFreeHook(const void* ptr) { |
425 if (MallocHooksState::IsLockHeldByCurrentThread() || | 440 if (MallocHooksState::IsLockHeldByCurrentThread() || |
426 !MallocHooksState::IsOriginalProcess()) { | 441 !MallocHooksState::IsOriginalProcess()) { |
427 return; | 442 return; |
428 } | 443 } |
429 | 444 |
430 MallocLocker ml(MallocHooksState::malloc_hook_mutex(), | 445 MallocLocker ml(MallocHooksState::malloc_hook_mutex(), |
431 MallocHooksState::malloc_hook_mutex_owner()); | 446 MallocHooksState::malloc_hook_mutex_owner()); |
432 // Now that we hold the lock, check to make sure everything is still active. | 447 // Now that we hold the lock, check to make sure everything is still active. |
433 if ((ptr != NULL) && MallocHooksState::Active()) { | 448 if ((ptr != NULL) && MallocHooksState::Active()) { |
434 AllocationInfo* allocation_info = NULL; | 449 AllocationInfo* allocation_info = NULL; |
435 if (MallocHooksState::address_map()->Lookup(ptr, &allocation_info)) { | 450 if (MallocHooksState::address_map()->Lookup(ptr, &allocation_info)) { |
436 MallocHooksState::DecrementHeapAllocatedMemoryInBytes( | 451 MallocHooksState::DecrementHeapAllocatedMemoryInBytes( |
437 allocation_info->allocation_size()); | 452 allocation_info->allocation_size()); |
438 MallocHooksState::address_map()->Remove(ptr); | 453 MallocHooksState::address_map()->Remove(ptr); |
439 delete allocation_info; | 454 delete allocation_info; |
440 } | 455 } |
441 } | 456 } |
442 } | 457 } |
443 | 458 |
444 } // namespace dart | 459 } // namespace dart |
445 | 460 |
446 #endif // defined(DART_USE_TCMALLOC) && !defined(PRODUCT) && | 461 #endif // defined(DART_USE_TCMALLOC) && !defined(PRODUCT) && |
447 // !defined(TARGET_ARCH_DBC) && !defined(TARGET_OS_FUCHSIA) | 462 // !defined(TARGET_ARCH_DBC) && !defined(TARGET_OS_FUCHSIA) |
OLD | NEW |