James Dong | 8635b7b | 2011-03-14 17:01:38 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011, The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
James Dong | 8635b7b | 2011-03-14 17:01:38 -0700 | [diff] [blame] | 17 | |
Andy Hung | 07b745e | 2016-05-23 16:21:07 -0700 | [diff] [blame] | 18 | //#define LOG_NDEBUG 0 |
| 19 | #define LOG_TAG "MemoryLeackTrackUtil" |
| 20 | #include <utils/Log.h> |
| 21 | |
| 22 | #include "media/MemoryLeakTrackUtil.h" |
| 23 | #include <sstream> |
James Dong | 8635b7b | 2011-03-14 17:01:38 -0700 | [diff] [blame] | 24 | |
| 25 | /* |
Andy Hung | 07b745e | 2016-05-23 16:21:07 -0700 | [diff] [blame] | 26 | * The code here originally resided in MediaPlayerService.cpp |
James Dong | 8635b7b | 2011-03-14 17:01:38 -0700 | [diff] [blame] | 27 | */ |
James Dong | 8635b7b | 2011-03-14 17:01:38 -0700 | [diff] [blame] | 28 | |
Andy Hung | 07b745e | 2016-05-23 16:21:07 -0700 | [diff] [blame] | 29 | // Figure out the abi based on defined macros. |
James Dong | 8635b7b | 2011-03-14 17:01:38 -0700 | [diff] [blame] | 30 | #if defined(__arm__) |
Andy Hung | 07b745e | 2016-05-23 16:21:07 -0700 | [diff] [blame] | 31 | #define ABI_STRING "arm" |
| 32 | #elif defined(__aarch64__) |
| 33 | #define ABI_STRING "arm64" |
| 34 | #elif defined(__mips__) && !defined(__LP64__) |
| 35 | #define ABI_STRING "mips" |
| 36 | #elif defined(__mips__) && defined(__LP64__) |
| 37 | #define ABI_STRING "mips64" |
| 38 | #elif defined(__i386__) |
| 39 | #define ABI_STRING "x86" |
| 40 | #elif defined(__x86_64__) |
| 41 | #define ABI_STRING "x86_64" |
| 42 | #else |
| 43 | #error "Unsupported ABI" |
| 44 | #endif |
| 45 | |
| 46 | extern std::string backtrace_string(const uintptr_t* frames, size_t frame_count); |
| 47 | |
| 48 | namespace android { |
James Dong | 8635b7b | 2011-03-14 17:01:38 -0700 | [diff] [blame] | 49 | |
| 50 | extern "C" void get_malloc_leak_info(uint8_t** info, size_t* overallSize, |
| 51 | size_t* infoSize, size_t* totalMemory, size_t* backtraceSize); |
| 52 | |
| 53 | extern "C" void free_malloc_leak_info(uint8_t* info); |
| 54 | |
Andy Hung | 07b745e | 2016-05-23 16:21:07 -0700 | [diff] [blame] | 55 | std::string dumpMemoryAddresses(size_t limit) |
James Dong | 8635b7b | 2011-03-14 17:01:38 -0700 | [diff] [blame] | 56 | { |
Andy Hung | 07b745e | 2016-05-23 16:21:07 -0700 | [diff] [blame] | 57 | uint8_t *info; |
| 58 | size_t overallSize; |
| 59 | size_t infoSize; |
| 60 | size_t totalMemory; |
| 61 | size_t backtraceSize; |
James Dong | 8635b7b | 2011-03-14 17:01:38 -0700 | [diff] [blame] | 62 | get_malloc_leak_info(&info, &overallSize, &infoSize, &totalMemory, &backtraceSize); |
James Dong | 8635b7b | 2011-03-14 17:01:38 -0700 | [diff] [blame] | 63 | |
Andy Hung | 07b745e | 2016-05-23 16:21:07 -0700 | [diff] [blame] | 64 | size_t count; |
| 65 | if (info == nullptr || overallSize == 0 || infoSize == 0 |
| 66 | || (count = overallSize / infoSize) == 0) { |
| 67 | ALOGD("no malloc info, libc.debug.malloc.program property should be set"); |
| 68 | return std::string(); |
James Dong | 8635b7b | 2011-03-14 17:01:38 -0700 | [diff] [blame] | 69 | } |
Andy Hung | 07b745e | 2016-05-23 16:21:07 -0700 | [diff] [blame] | 70 | |
| 71 | std::ostringstream oss; |
| 72 | oss << totalMemory << " bytes in " << count << " allocations\n"; |
| 73 | oss << " ABI: '" ABI_STRING "'" << "\n\n"; |
| 74 | if (count > limit) count = limit; |
| 75 | |
| 76 | // The memory is sorted based on total size which is useful for finding |
| 77 | // worst memory offenders. For diffs, sometimes it is preferable to sort |
| 78 | // based on the backtrace. |
| 79 | for (size_t i = 0; i < count; i++) { |
| 80 | struct AllocEntry { |
| 81 | size_t size; // bit 31 is set if this is zygote allocated memory |
| 82 | size_t allocations; |
| 83 | uintptr_t backtrace[]; |
| 84 | }; |
| 85 | |
| 86 | const AllocEntry * const e = (AllocEntry *)(info + i * infoSize); |
| 87 | |
| 88 | oss << (e->size * e->allocations) |
| 89 | << " bytes ( " << e->size << " bytes * " << e->allocations << " allocations )\n"; |
| 90 | oss << backtrace_string(e->backtrace, backtraceSize) << "\n"; |
| 91 | } |
| 92 | oss << "\n"; |
| 93 | free_malloc_leak_info(info); |
| 94 | return oss.str(); |
James Dong | 8635b7b | 2011-03-14 17:01:38 -0700 | [diff] [blame] | 95 | } |
| 96 | |
James Dong | 8635b7b | 2011-03-14 17:01:38 -0700 | [diff] [blame] | 97 | } // namespace android |