diff options
author | pliard@chromium.org <pliard@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-06-05 14:07:33 +0000 |
---|---|---|
committer | pliard@chromium.org <pliard@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-06-05 14:07:33 +0000 |
commit | 21c9f94feb395b8755277fdff553122ffc04beea (patch) | |
tree | 3910aa814e2e79b1071ca9ed3930aa91d3e2993a /tools/android | |
parent | b090de9f1bef13cad16fbec4a50909993f51aa78 (diff) | |
download | chromium_src-21c9f94feb395b8755277fdff553122ffc04beea.zip chromium_src-21c9f94feb395b8755277fdff553122ffc04beea.tar.gz chromium_src-21c9f94feb395b8755277fdff553122ffc04beea.tar.bz2 |
Add tools/android/memdump/.
This adds the 'memdump' binary that dumps device memory information for a list
of processes and 'memreport.py' (executed on the host) used to process this
information.
Memdump is similar to /proc/<PID>/smaps except that it is multi-process aware.
For a list of processes (e.g. browser + renderer processes), it is able to
dinstinguish the memory pages that are mapped only in these processes from the
ones that are private or also mapped in external processes.
This is useful to gather high level memory usage information (e.g. heap size vs
code size) for any (non-instrumented) process.
R=bulach@chromium.org, digit@chromium.org, qsr@chromium.org
Review URL: https://codereview.chromium.org/15861023
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@204254 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'tools/android')
-rw-r--r-- | tools/android/android_tools.gyp | 7 | ||||
-rw-r--r-- | tools/android/memdump/memdump.cc | 362 | ||||
-rw-r--r-- | tools/android/memdump/memdump.gyp | 31 | ||||
-rwxr-xr-x | tools/android/memdump/memreport.py | 108 |
4 files changed, 508 insertions, 0 deletions
diff --git a/tools/android/android_tools.gyp b/tools/android/android_tools.gyp index 979d7c0..5aeb96f 100644 --- a/tools/android/android_tools.gyp +++ b/tools/android/android_tools.gyp @@ -16,5 +16,12 @@ 'adb_reboot/adb_reboot.gyp:adb_reboot', ], }, + { + 'target_name': 'memdump', + 'type': 'none', + 'dependencies': [ + 'memdump/memdump.gyp:memdump', + ], + } ], } diff --git a/tools/android/memdump/memdump.cc b/tools/android/memdump/memdump.cc new file mode 100644 index 0000000..c12d6f1 --- /dev/null +++ b/tools/android/memdump/memdump.cc @@ -0,0 +1,362 @@ +// Copyright (c) 2013 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include <fcntl.h> +#include <signal.h> +#include <sys/types.h> +#include <unistd.h> + +#include <algorithm> +#include <cstring> +#include <fstream> +#include <iostream> +#include <string> +#include <utility> +#include <vector> + +#include "base/basictypes.h" +#include "base/bind.h" +#include "base/bind_helpers.h" +#include "base/file_util.h" +#include "base/hash_tables.h" +#include "base/logging.h" +#include "base/memory/scoped_ptr.h" +#include "base/string_number_conversions.h" +#include "base/stringprintf.h" +#include "base/strings/string_piece.h" +#include "base/strings/string_split.h" + +namespace { + +// An entry in /proc/<pid>/pagemap. +struct PageMapEntry { + uint64 page_frame_number : 55; + uint unused : 8; + uint present : 1; +}; + +// Describes a virtual page. +struct PageInfo { + int64 page_frame_number : 55; // Physical page id, also known as PFN. + int64 times_mapped : 9 + 32; +}; + +struct MemoryMap { + std::string name; + std::string flags; + uint start_address; + uint end_address; + int private_count; + int app_shared_count; + int other_shared_count; + std::vector<PageInfo> committed_pages; +}; + +struct ProcessMemory { + pid_t pid; + std::vector<MemoryMap> memory_maps; +}; + +// Number of times a physical page is mapped in a process. +typedef base::hash_map<uint64, int> PFNMap; + +// Parses lines from /proc/<PID>/maps, e.g.: +// 401e7000-401f5000 r-xp 00000000 103:02 158 /system/bin/linker +bool ParseMemoryMapLine(const std::string& line, + std::vector<std::string>* tokens, + MemoryMap* memory_map) { + tokens->clear(); + base::SplitString(line, ' ', tokens); + if (tokens->size() < 2) + return false; + const int addr_len = 8; + const std::string& addr_range = tokens->at(0); + if (addr_range.length() != addr_len + 1 + addr_len) + return false; + uint64 tmp = 0; + if (!base::HexStringToUInt64( + base::StringPiece( + addr_range.begin(), addr_range.begin() + addr_len), + &tmp)) { + return false; + } + memory_map->start_address = static_cast<uint>(tmp); + const int end_addr_start_pos = addr_len + 1; + if (!base::HexStringToUInt64( + base::StringPiece( + addr_range.begin() + end_addr_start_pos, + addr_range.begin() + end_addr_start_pos + addr_len), + &tmp)) { + return false; + } + memory_map->end_address = static_cast<uint>(tmp); + if (tokens->at(1).size() != strlen("rwxp")) + return false; + memory_map->flags.swap(tokens->at(1)); + const int map_name_index = 5; + if (tokens->size() >= map_name_index + 1) { + for (std::vector<std::string>::const_iterator it = + tokens->begin() + map_name_index; it != tokens->end(); ++it) { + if (!it->empty()) { + if (!memory_map->name.empty()) + memory_map->name.append(" "); + memory_map->name.append(*it); + } + } + } + return true; +} + +// Reads sizeof(T) bytes from file |fd| at |offset|. +template <typename T> +bool ReadFromFileAtOffset(int fd, off_t offset, T* value) { + if (lseek64(fd, offset * sizeof(*value), SEEK_SET) < 0) { + PLOG(ERROR) << "lseek"; + return false; + } + ssize_t bytes = read(fd, value, sizeof(*value)); + if (bytes != sizeof(*value) && bytes != 0) { + PLOG(ERROR) << "read"; + return false; + } + return true; +} + +// Fills |process_maps| in with the process memory maps identified by |pid|. +bool GetProcessMaps(pid_t pid, std::vector<MemoryMap>* process_maps) { + std::ifstream maps_file(base::StringPrintf("/proc/%d/maps", pid).c_str()); + if (!maps_file.good()) { + PLOG(ERROR) << "open"; + return false; + } + std::string line; + std::vector<std::string> tokens; + while (std::getline(maps_file, line) && !line.empty()) { + MemoryMap memory_map = {}; + if (!ParseMemoryMapLine(line, &tokens, &memory_map)) { + LOG(ERROR) << "Could not parse line: " << line; + return false; + } + process_maps->push_back(memory_map); + } + return true; +} + +// Fills |committed_pages| in with the set of committed pages contained in the +// provided memory map. +bool GetPagesForMemoryMap(int pagemap_fd, + const MemoryMap& memory_map, + std::vector<PageInfo>* committed_pages) { + for (uint addr = memory_map.start_address; addr < memory_map.end_address; + addr += PAGE_SIZE) { + DCHECK_EQ(0, addr % PAGE_SIZE); + PageMapEntry page_map_entry = {}; + COMPILE_ASSERT(sizeof(PageMapEntry) == sizeof(uint64), unexpected_size); + const off64_t offset = addr / PAGE_SIZE; + if (!ReadFromFileAtOffset(pagemap_fd, offset, &page_map_entry)) + return false; + if (page_map_entry.present) { // Ignore non-committed pages. + if (page_map_entry.page_frame_number == 0) + continue; + PageInfo page_info = {}; + page_info.page_frame_number = page_map_entry.page_frame_number; + committed_pages->push_back(page_info); + } + } + return true; +} + +bool SetTimesMapped(int pagecount_fd, std::vector<PageInfo>* pages) { + for (std::vector<PageInfo>::iterator it = pages->begin(); + it != pages->end(); ++it) { + PageInfo* const page_info = &*it; + int64 times_mapped; + if (!ReadFromFileAtOffset( + pagecount_fd, page_info->page_frame_number, ×_mapped)) { + return false; + } + page_info->times_mapped = times_mapped; + } + return true; +} + +// Fills in the provided vector of Page Frame Number maps. This lets +// ClassifyPages() know how many times each page is mapped in the processes. +void FillPFNMaps(const std::vector<ProcessMemory>& processes_memory, + std::vector<PFNMap>* pfn_maps) { + int current_process_index = 0; + for (std::vector<ProcessMemory>::const_iterator it = processes_memory.begin(); + it != processes_memory.end(); ++it, ++current_process_index) { + const std::vector<MemoryMap>& memory_maps = it->memory_maps; + for (std::vector<MemoryMap>::const_iterator it = memory_maps.begin(); + it != memory_maps.end(); ++it) { + const std::vector<PageInfo>& pages = it->committed_pages; + for (std::vector<PageInfo>::const_iterator it = pages.begin(); + it != pages.end(); ++it) { + const PageInfo& page_info = *it; + PFNMap* const pfn_map = &(*pfn_maps)[current_process_index]; + const std::pair<PFNMap::iterator, bool> result = pfn_map->insert( + std::make_pair(page_info.page_frame_number, 0)); + ++result.first->second; + } + } + } +} + +// Sets the private_count/app_shared_count/other_shared_count fields of the +// provided memory maps for each process. +void ClassifyPages(std::vector<ProcessMemory>* processes_memory) { + std::vector<PFNMap> pfn_maps(processes_memory->size()); + FillPFNMaps(*processes_memory, &pfn_maps); + // Hash set keeping track of the physical pages mapped in a single process so + // that they can be counted only once. + std::hash_set<uint64> physical_pages_mapped_in_process; + + for (std::vector<ProcessMemory>::iterator it = processes_memory->begin(); + it != processes_memory->end(); ++it) { + std::vector<MemoryMap>* const memory_maps = &it->memory_maps; + physical_pages_mapped_in_process.clear(); + for (std::vector<MemoryMap>::iterator it = memory_maps->begin(); + it != memory_maps->end(); ++it) { + MemoryMap* const memory_map = &*it; + const std::vector<PageInfo>& pages = memory_map->committed_pages; + for (std::vector<PageInfo>::const_iterator it = pages.begin(); + it != pages.end(); ++it) { + const PageInfo& page_info = *it; + if (page_info.times_mapped == 1) { + ++memory_map->private_count; + continue; + } + const uint64 page_frame_number = page_info.page_frame_number; + const std::pair<std::hash_set<uint64>::iterator, bool> result = + physical_pages_mapped_in_process.insert(page_frame_number); + const bool did_insert = result.second; + if (!did_insert) { + // This physical page (mapped multiple times in the same process) was + // already counted. + continue; + } + // See if the current physical page is also mapped in the other + // processes that are being analyzed. + int times_mapped = 0; + bool mapped_in_multiple_processes = false; + for (std::vector<PFNMap>::const_iterator it = pfn_maps.begin(); + it != pfn_maps.end(); ++it) { + const PFNMap& pfn_map = *it; + const PFNMap::const_iterator found_it = pfn_map.find( + page_frame_number); + if (found_it == pfn_map.end()) + continue; + if (times_mapped) + mapped_in_multiple_processes = true; + times_mapped += found_it->second; + } + if (times_mapped == page_info.times_mapped) { + // The physical page is only mapped in the processes that are being + // analyzed. + if (mapped_in_multiple_processes) { + ++memory_map->app_shared_count; + } else { + // The physical page is mapped multiple times in the same process. + ++memory_map->private_count; + } + } else { + ++memory_map->other_shared_count; + } + } + } + } +} + +void DumpProcessesMemoryMaps( + const std::vector<ProcessMemory>& processes_memory) { + std::string buf; + for (std::vector<ProcessMemory>::const_iterator it = processes_memory.begin(); + it != processes_memory.end(); ++it) { + const ProcessMemory& process_memory = *it; + std::cout << "[ PID=" << process_memory.pid << "]" << '\n'; + const std::vector<MemoryMap>& memory_maps = process_memory.memory_maps; + for (std::vector<MemoryMap>::const_iterator it = memory_maps.begin(); + it != memory_maps.end(); ++it) { + const MemoryMap& memory_map = *it; + base::SStringPrintf( + &buf, "%x-%x %s private=%d shared_app=%d shared_other=%d %s\n", + memory_map.start_address, + memory_map.end_address, memory_map.flags.c_str(), + memory_map.private_count * PAGE_SIZE, + memory_map.app_shared_count * PAGE_SIZE, + memory_map.other_shared_count * PAGE_SIZE, + memory_map.name.c_str()); + std::cout << buf; + } + } +} + +bool CollectProcessMemoryInformation(int page_count_fd, + ProcessMemory* process_memory) { + const pid_t pid = process_memory->pid; + int pagemap_fd = open( + base::StringPrintf("/proc/%d/pagemap", pid).c_str(), O_RDONLY); + if (pagemap_fd < 0) { + PLOG(ERROR) << "open"; + return false; + } + file_util::ScopedFD auto_closer(&pagemap_fd); + std::vector<MemoryMap>* const process_maps = &process_memory->memory_maps; + if (!GetProcessMaps(pid, process_maps)) + return false; + for (std::vector<MemoryMap>::iterator it = process_maps->begin(); + it != process_maps->end(); ++it) { + std::vector<PageInfo>* const committed_pages = &it->committed_pages; + GetPagesForMemoryMap(pagemap_fd, *it, committed_pages); + SetTimesMapped(page_count_fd, committed_pages); + } + return true; +} + +void KillAll(const std::vector<pid_t>& pids, int signal_number) { + for (std::vector<pid_t>::const_iterator it = pids.begin(); it != pids.end(); + ++it) { + kill(*it, signal_number); + } +} + +} // namespace + +int main(int argc, char** argv) { + if (argc == 1) { + LOG(ERROR) << "Usage: " << argv[0] << " <PID1>... <PIDN>"; + return EXIT_FAILURE; + } + std::vector<pid_t> pids; + for (const char* const* ptr = argv + 1; *ptr; ++ptr) { + pid_t pid; + if (!base::StringToInt(*ptr, &pid)) + return EXIT_FAILURE; + pids.push_back(pid); + } + std::vector<ProcessMemory> processes_memory(pids.size()); + { + int page_count_fd = open("/proc/kpagecount", O_RDONLY); + if (page_count_fd < 0) { + PLOG(ERROR) << "open"; + return EXIT_FAILURE; + } + file_util::ScopedFD page_count_fd_closer(&page_count_fd); + base::ScopedClosureRunner auto_resume_processes( + base::Bind(&KillAll, pids, SIGCONT)); + KillAll(pids, SIGSTOP); + for (std::vector<pid_t>::const_iterator it = pids.begin(); it != pids.end(); + ++it) { + ProcessMemory* const process_memory = + &processes_memory[it - pids.begin()]; + process_memory->pid = *it; + if (!CollectProcessMemoryInformation(page_count_fd, process_memory)) + return EXIT_FAILURE; + } + } + ClassifyPages(&processes_memory); + DumpProcessesMemoryMaps(processes_memory); + return EXIT_SUCCESS; +} diff --git a/tools/android/memdump/memdump.gyp b/tools/android/memdump/memdump.gyp new file mode 100644 index 0000000..faa24d0 --- /dev/null +++ b/tools/android/memdump/memdump.gyp @@ -0,0 +1,31 @@ +# Copyright (c) 2012 The Chromium Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +{ + 'targets': [ + { + 'target_name': 'memdump', + 'type': 'executable', + 'dependencies': [ + '../../../base/base.gyp:base', + ], + 'conditions': [ + # Warning: A PIE tool cannot run on ICS 4.0.4, so only + # build it as position-independent when ASAN + # is activated. See b/6587214 for details. + [ 'asan==1', { + 'cflags': [ + '-fPIE', + ], + 'ldflags': [ + '-pie', + ], + }], + ], + 'sources': [ + 'memdump.cc', + ], + }, + ], +} diff --git a/tools/android/memdump/memreport.py b/tools/android/memdump/memreport.py new file mode 100755 index 0000000..d0c6fce --- /dev/null +++ b/tools/android/memdump/memreport.py @@ -0,0 +1,108 @@ +#!/usr/bin/env python +# +# Copyright (c) 2013 The Chromium Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +import re +import sys + +from sets import Set + + +_ENTRIES = [ + ('Total', '.* r... .*'), + ('Read-only', '.* r--. .*'), + ('Read-write', '.* rw-. .*'), + ('Executable', '.* ..x. .*'), + ('File read-write', '.* rw.. .* /.*'), + ('Anonymous read-write', '.* rw.. .* .*other=[0-9]+ ($|.*chromium:.*)'), + ('File executable', '.* ..x. .* /.*'), + ('Anonymous executable (JIT\'ed code)', '.* ..x. .* shared_other=[0-9]+ $'), + ('chromium mmap', '.* r... .*chromium:.*'), + ('chromium TransferBuffer', '.* r... .*chromium:.*CreateTransferBuffer.*'), + ('Galaxy Nexus GL driver', '.* r... .*pvrsrvkm.*'), + ('Dalvik', '.* rw.. .* /.*dalvik.*'), + ('Dalvik heap', '.* rw.. .* /.*dalvik-heap.*'), + ('Native heap (jemalloc)', '.* r... .* /.*jemalloc.*'), + ('System heap', '.* r... .* \\[heap\\]'), + ('Ashmem', '.* rw.. .* /dev/ashmem .*'), + ('libchromeview.so total', '.* r... .* /.*libchromeview.so'), + ('libchromeview.so read-only', '.* r--. .* /.*libchromeview.so'), + ('libchromeview.so read-write', '.* rw-. .* /.*libchromeview.so'), + ('libchromeview.so executable', '.* r.x. .* /.*libchromeview.so'), +] + + +def _CollectMemoryStats(memdump, region_filters): + processes = [] + mem_usage_for_regions = None + regexps = {} + for region_filter in region_filters: + regexps[region_filter] = re.compile(region_filter) + for line in memdump: + if 'PID=' in line: + mem_usage_for_regions = {} + processes.append(mem_usage_for_regions) + continue + matched_regions = Set([]) + for region_filter in region_filters: + if regexps[region_filter].match(line): + matched_regions.add(region_filter) + if not region_filter in mem_usage_for_regions: + mem_usage_for_regions[region_filter] = { + 'private': 0, + 'shared_app': 0, + 'shared_other': 0, + } + for matched_region in matched_regions: + mem_usage = mem_usage_for_regions[matched_region] + for key in mem_usage: + for token in line.split(' '): + if key in token: + mem_usage[key] += int(token.split('=')[1]) + break + return processes + + +def _ConvertMemoryField(field): + return str(field / (1024.0 * 1024)) + + +def _DumpCSV(processes_stats): + total_map = {} + i = 0 + for process in processes_stats: + i += 1 + print ',Process ' + str(i) + ',private,shared_app,shared_other,' + for (k, v) in _ENTRIES: + if not v in process: + print ',' + k + ',0,0,0,' + continue + if not v in total_map: + total_map[v] = 0 + total_map[v] += process[v]['private'] + ( + process[v]['shared_app'] / len(processes_stats)) + print ',' + k + ',' + _ConvertMemoryField(process[v]['private']) + ',' + ( + _ConvertMemoryField(process[v]['shared_app']) + ',' + ( + _ConvertMemoryField(process[v]['shared_other'])) + ',') + print '' + + if len(processes_stats) > 2: + print >>sys.stderr, ( + 'Total stats are not supported yet with more than two processes.') + return + for (k, v) in _ENTRIES: + if not v in total_map: + print ',' + k + ',0,0' + continue + print ',' + k + ',' + _ConvertMemoryField(total_map[v]) + ',' + print '' + + +def main(argv): + _DumpCSV(_CollectMemoryStats(sys.stdin, [value for (key, value) in _ENTRIES])) + + +if __name__ == '__main__': + main(sys.argv) |