summaryrefslogtreecommitdiffstats
path: root/libmeminfo/include
diff options
context:
space:
mode:
authorSandeep Patil <sspatil@google.com>2019-01-01 16:04:04 -0800
committerSandeep Patil <sspatil@google.com>2019-01-08 17:08:10 -0800
commit82a48b160a8ae4e7bf05e2df8bdde432b5854fc2 (patch)
treed86b5b1b26d1920d09d7b144f74c87cff6b9ae2f /libmeminfo/include
parentfa2d8d55417d5e4acdd7dbabb1078643308af894 (diff)
downloadsystem_core-82a48b160a8ae4e7bf05e2df8bdde432b5854fc2.tar.gz
system_core-82a48b160a8ae4e7bf05e2df8bdde432b5854fc2.tar.bz2
system_core-82a48b160a8ae4e7bf05e2df8bdde432b5854fc2.zip
meminfo: Add Smaps(), showmap and friends.
Needed by showmap and also android_s_Debug to classify each allocation into multiple heaps. The APIs added are: ForEachVmaFromFile - Global API to parse a file expected to be in the same format as /proc/<pid>/smaps and make a callback for each VMA found. ProcMemInfo::ForEachVma - Same as 'ForEachVmaFromFile' but for a ProcMemInfo object corresponding to a process(pid). ProcMemInfo::Smaps - Wrapper to ProcMemInfo::ForEachVma, except the function collects 'struct Vma' in a member vector and returns the reference for the same. Added showmap2 using the APIs and the corresponding tests the same time. Bug: 111694435 Test: showmap_test.sh Test: libmeminfo_test 1 Change-Id: I3065809cf94ecf3da88529809701035c47a8ce34 Signed-off-by: Sandeep Patil <sspatil@google.com>
Diffstat (limited to 'libmeminfo/include')
-rw-r--r--libmeminfo/include/meminfo/meminfo.h7
-rw-r--r--libmeminfo/include/meminfo/procmeminfo.h19
2 files changed, 25 insertions, 1 deletions
diff --git a/libmeminfo/include/meminfo/meminfo.h b/libmeminfo/include/meminfo/meminfo.h
index 21e0af4af..5ee32d45f 100644
--- a/libmeminfo/include/meminfo/meminfo.h
+++ b/libmeminfo/include/meminfo/meminfo.h
@@ -16,6 +16,7 @@
#pragma once
+#include <string.h>
#include <sys/types.h>
#include <unistd.h>
@@ -66,10 +67,16 @@ struct Vma {
uint16_t flags;
std::string name;
+ Vma() : start(0), end(0), offset(0), flags(0), name("") {}
Vma(uint64_t s, uint64_t e, uint64_t off, uint16_t f, const char* n)
: start(s), end(e), offset(off), flags(f), name(n) {}
~Vma() = default;
+ void clear() {
+ memset(&usage, 0, sizeof(usage));
+ memset(&wss, 0, sizeof(wss));
+ }
+
// Memory usage of this mapping.
MemUsage usage;
// Working set within this mapping.
diff --git a/libmeminfo/include/meminfo/procmeminfo.h b/libmeminfo/include/meminfo/procmeminfo.h
index 4aafd7713..0bfd80f2f 100644
--- a/libmeminfo/include/meminfo/procmeminfo.h
+++ b/libmeminfo/include/meminfo/procmeminfo.h
@@ -26,6 +26,8 @@
namespace android {
namespace meminfo {
+using VmaCallback = std::function<void(const Vma&)>;
+
class ProcMemInfo final {
// Per-process memory accounting
public:
@@ -38,6 +40,18 @@ class ProcMemInfo final {
const MemUsage& Usage();
const MemUsage& Wss();
+ // Collect all 'vma' or 'maps' from /proc/<pid>/smaps and store them in 'maps_'. Returns a
+ // constant reference to the vma vector after the collection is done.
+ //
+ // Each 'struct Vma' is *fully* populated by this method (unlike SmapsOrRollup).
+ const std::vector<Vma>& Smaps(const std::string& path = "");
+
+ // This method reads /proc/<pid>/smaps and calls the callback() for each
+ // vma or map that it finds. The map is converted to 'struct Vma' object which is then
+ // passed to the callback.
+ // Returns 'false' if the file is malformed.
+ bool ForEachVma(const VmaCallback& callback);
+
// Used to parse either of /proc/<pid>/{smaps, smaps_rollup} and record the process's
// Pss and Private memory usage in 'stats'. In particular, the method only populates the fields
// of the MemUsage structure that are intended to be used by Android's periodic Pss collection.
@@ -49,7 +63,6 @@ class ProcMemInfo final {
// private_clean
// private_dirty
// SwapPss
- //
// All other fields of MemUsage are zeroed.
bool SmapsOrRollup(bool use_rollup, MemUsage* stats) const;
@@ -73,6 +86,10 @@ class ProcMemInfo final {
std::vector<uint16_t> swap_offsets_;
};
+// Makes callback for each 'vma' or 'map' found in file provided. The file is expected to be in the
+// same format as /proc/<pid>/smaps. Returns 'false' if the file is malformed.
+bool ForEachVmaFromFile(const std::string& path, const VmaCallback& callback);
+
// Same as ProcMemInfo::SmapsOrRollup but reads the statistics directly
// from a file. The file MUST be in the same format as /proc/<pid>/smaps
// or /proc/<pid>/smaps_rollup