Merge "Update fs_mgr_update_verity_state() for new C++ Fstab"
diff --git a/adb/client/adb_install.cpp b/adb/client/adb_install.cpp
index 7f37c45..079a975 100644
--- a/adb/client/adb_install.cpp
+++ b/adb/client/adb_install.cpp
@@ -226,18 +226,8 @@
static int install_app_legacy(int argc, const char** argv, bool use_fastdeploy,
bool use_localagent) {
- static const char* const DATA_DEST = "/data/local/tmp/%s";
- static const char* const SD_DEST = "/sdcard/tmp/%s";
- const char* where = DATA_DEST;
-
printf("Performing Push Install\n");
- for (int i = 1; i < argc; i++) {
- if (!strcmp(argv[i], "-s")) {
- where = SD_DEST;
- }
- }
-
// Find last APK argument.
// All other arguments passed through verbatim.
int last_apk = -1;
@@ -256,7 +246,7 @@
int result = -1;
std::vector<const char*> apk_file = {argv[last_apk]};
std::string apk_dest =
- android::base::StringPrintf(where, android::base::Basename(argv[last_apk]).c_str());
+ "/data/local/tmp/" + android::base::Basename(argv[last_apk]);
if (use_fastdeploy == true) {
TemporaryFile metadataTmpFile;
diff --git a/adb/client/commandline.cpp b/adb/client/commandline.cpp
index c11052d..95e66ae 100644
--- a/adb/client/commandline.cpp
+++ b/adb/client/commandline.cpp
@@ -146,10 +146,8 @@
" install [-lrtsdg] [--instant] PACKAGE\n"
" install-multiple [-lrtsdpg] [--instant] PACKAGE...\n"
" push package(s) to the device and install them\n"
- " -l: forward lock application\n"
" -r: replace existing application\n"
" -t: allow test packages\n"
- " -s: install application on sdcard\n"
" -d: allow version code downgrade (debuggable packages only)\n"
" -p: partial application install (install-multiple only)\n"
" -g: grant all runtime permissions\n"
diff --git a/adb/client/fastdeploy.cpp b/adb/client/fastdeploy.cpp
index 45f3cca..e82f15a 100644
--- a/adb/client/fastdeploy.cpp
+++ b/adb/client/fastdeploy.cpp
@@ -228,11 +228,12 @@
android::base::StringPrintf(kAgentExtractCommandPattern, packageName.c_str());
std::vector<char> extractErrorBuffer;
- int statusCode;
- DeployAgentFileCallback cb(outputFp, &extractErrorBuffer, &statusCode);
+ DeployAgentFileCallback cb(outputFp, &extractErrorBuffer);
int returnCode = send_shell_command(extractCommand, false, &cb);
if (returnCode != 0) {
- error_exit("Executing %s returned %d", extractCommand.c_str(), returnCode);
+ fprintf(stderr, "Executing %s returned %d\n", extractCommand.c_str(), returnCode);
+ fprintf(stderr, "%*s\n", int(extractErrorBuffer.size()), extractErrorBuffer.data());
+ error_exit("Aborting");
}
}
diff --git a/adb/client/fastdeploycallbacks.cpp b/adb/client/fastdeploycallbacks.cpp
index 6c9a21f..23a0aca 100644
--- a/adb/client/fastdeploycallbacks.cpp
+++ b/adb/client/fastdeploycallbacks.cpp
@@ -35,8 +35,7 @@
class DeployAgentBufferCallback : public StandardStreamsCallbackInterface {
public:
- DeployAgentBufferCallback(std::vector<char>* outBuffer, std::vector<char>* errBuffer,
- int* statusCode);
+ DeployAgentBufferCallback(std::vector<char>* outBuffer, std::vector<char>* errBuffer);
virtual void OnStdout(const char* buffer, int length);
virtual void OnStderr(const char* buffer, int length);
@@ -45,27 +44,17 @@
private:
std::vector<char>* mpOutBuffer;
std::vector<char>* mpErrBuffer;
- int* mpStatusCode;
};
int capture_shell_command(const char* command, std::vector<char>* outBuffer,
std::vector<char>* errBuffer) {
- int statusCode;
- DeployAgentBufferCallback cb(outBuffer, errBuffer, &statusCode);
- int ret = send_shell_command(command, false, &cb);
-
- if (ret == 0) {
- return statusCode;
- } else {
- return ret;
- }
+ DeployAgentBufferCallback cb(outBuffer, errBuffer);
+ return send_shell_command(command, false, &cb);
}
-DeployAgentFileCallback::DeployAgentFileCallback(FILE* outputFile, std::vector<char>* errBuffer,
- int* statusCode) {
+DeployAgentFileCallback::DeployAgentFileCallback(FILE* outputFile, std::vector<char>* errBuffer) {
mpOutFile = outputFile;
mpErrBuffer = errBuffer;
- mpStatusCode = statusCode;
mBytesWritten = 0;
}
@@ -84,10 +73,7 @@
}
int DeployAgentFileCallback::Done(int status) {
- if (mpStatusCode != NULL) {
- *mpStatusCode = status;
- }
- return 0;
+ return status;
}
int DeployAgentFileCallback::getBytesWritten() {
@@ -95,11 +81,9 @@
}
DeployAgentBufferCallback::DeployAgentBufferCallback(std::vector<char>* outBuffer,
- std::vector<char>* errBuffer,
- int* statusCode) {
+ std::vector<char>* errBuffer) {
mpOutBuffer = outBuffer;
mpErrBuffer = errBuffer;
- mpStatusCode = statusCode;
}
void DeployAgentBufferCallback::OnStdout(const char* buffer, int length) {
@@ -111,8 +95,5 @@
}
int DeployAgentBufferCallback::Done(int status) {
- if (mpStatusCode != NULL) {
- *mpStatusCode = status;
- }
- return 0;
+ return status;
}
diff --git a/adb/client/fastdeploycallbacks.h b/adb/client/fastdeploycallbacks.h
index b428b50..7e049c5 100644
--- a/adb/client/fastdeploycallbacks.h
+++ b/adb/client/fastdeploycallbacks.h
@@ -21,7 +21,7 @@
class DeployAgentFileCallback : public StandardStreamsCallbackInterface {
public:
- DeployAgentFileCallback(FILE* outputFile, std::vector<char>* errBuffer, int* statusCode);
+ DeployAgentFileCallback(FILE* outputFile, std::vector<char>* errBuffer);
virtual void OnStdout(const char* buffer, int length);
virtual void OnStderr(const char* buffer, int length);
@@ -33,7 +33,6 @@
FILE* mpOutFile;
std::vector<char>* mpErrBuffer;
int mBytesWritten;
- int* mpStatusCode;
};
int capture_shell_command(const char* command, std::vector<char>* outBuffer,
diff --git a/base/file.cpp b/base/file.cpp
index d5bb7fe..2f4a517 100644
--- a/base/file.cpp
+++ b/base/file.cpp
@@ -22,6 +22,7 @@
#include <libgen.h>
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
diff --git a/fastboot/fastboot.cpp b/fastboot/fastboot.cpp
index b717aef..0b8d9b2 100644
--- a/fastboot/fastboot.cpp
+++ b/fastboot/fastboot.cpp
@@ -1158,6 +1158,10 @@
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
fb->set_transport(open_device());
+
+ if (!is_userspace_fastboot()) {
+ die("Failed to boot into userspace fastboot; one or more components might be unbootable.");
+ }
}
class ImageSource {
@@ -1314,9 +1318,6 @@
if (!is_userspace_fastboot()) {
reboot_to_userspace_fastboot();
}
- if (!is_userspace_fastboot()) {
- die("Failed to boot into userspace; one or more components might be unbootable.");
- }
std::string super_name;
if (fb->GetVar("super-partition-name", &super_name) != fastboot::RetCode::SUCCESS) {
@@ -1962,8 +1963,7 @@
fb->RebootTo("recovery");
fb->WaitForDisconnect();
} else if (wants_reboot_fastboot) {
- fb->RebootTo("fastboot");
- fb->WaitForDisconnect();
+ reboot_to_userspace_fastboot();
}
fprintf(stderr, "Finished. Total time: %.3fs\n", (now() - start));
diff --git a/init/first_stage_mount.cpp b/init/first_stage_mount.cpp
index c124656..6ae1123 100644
--- a/init/first_stage_mount.cpp
+++ b/init/first_stage_mount.cpp
@@ -132,7 +132,9 @@
// Class Definitions
// -----------------
FirstStageMount::FirstStageMount()
- : need_dm_verity_(false), fstab_(fs_mgr_read_fstab_dt(), fs_mgr_free_fstab) {
+ : need_dm_verity_(false),
+ fstab_(fs_mgr_read_fstab_dt(), fs_mgr_free_fstab),
+ uevent_listener_(16 * 1024 * 1024) {
// Stores fstab_->recs[] into mount_fstab_recs_ (vector<fstab_rec*>)
// for easier manipulation later, e.g., range-base for loop.
if (fstab_) {
diff --git a/init/uevent_listener.cpp b/init/uevent_listener.cpp
index d6765b7..62cd2be 100644
--- a/init/uevent_listener.cpp
+++ b/init/uevent_listener.cpp
@@ -86,9 +86,8 @@
}
}
-UeventListener::UeventListener() {
- // is 16MB enough? udev uses 128MB!
- device_fd_.reset(uevent_open_socket(16 * 1024 * 1024, true));
+UeventListener::UeventListener(size_t uevent_socket_rcvbuf_size) {
+ device_fd_.reset(uevent_open_socket(uevent_socket_rcvbuf_size, true));
if (device_fd_ == -1) {
LOG(FATAL) << "Could not open uevent socket";
}
diff --git a/init/uevent_listener.h b/init/uevent_listener.h
index 5b453fe..aea094e 100644
--- a/init/uevent_listener.h
+++ b/init/uevent_listener.h
@@ -41,7 +41,7 @@
class UeventListener {
public:
- UeventListener();
+ UeventListener(size_t uevent_socket_rcvbuf_size);
void RegenerateUevents(const ListenerCallback& callback) const;
ListenerAction RegenerateUeventsForPath(const std::string& path,
diff --git a/init/ueventd.cpp b/init/ueventd.cpp
index 66491dd..7545d53 100644
--- a/init/ueventd.cpp
+++ b/init/ueventd.cpp
@@ -233,29 +233,26 @@
SelabelInitialize();
std::vector<std::unique_ptr<UeventHandler>> uevent_handlers;
- UeventListener uevent_listener;
- {
- // Keep the current product name base configuration so we remain backwards compatible and
- // allow it to override everything.
- // TODO: cleanup platform ueventd.rc to remove vendor specific device node entries (b/34968103)
- auto hardware = android::base::GetProperty("ro.hardware", "");
+ // Keep the current product name base configuration so we remain backwards compatible and
+ // allow it to override everything.
+ // TODO: cleanup platform ueventd.rc to remove vendor specific device node entries (b/34968103)
+ auto hardware = android::base::GetProperty("ro.hardware", "");
- auto ueventd_configuration =
- ParseConfig({"/ueventd.rc", "/vendor/ueventd.rc", "/odm/ueventd.rc",
- "/ueventd." + hardware + ".rc"});
+ auto ueventd_configuration = ParseConfig({"/ueventd.rc", "/vendor/ueventd.rc",
+ "/odm/ueventd.rc", "/ueventd." + hardware + ".rc"});
- uevent_handlers.emplace_back(std::make_unique<DeviceHandler>(
- std::move(ueventd_configuration.dev_permissions),
- std::move(ueventd_configuration.sysfs_permissions),
- std::move(ueventd_configuration.subsystems), fs_mgr_get_boot_devices(), true));
- uevent_handlers.emplace_back(std::make_unique<FirmwareHandler>(
- std::move(ueventd_configuration.firmware_directories)));
+ uevent_handlers.emplace_back(std::make_unique<DeviceHandler>(
+ std::move(ueventd_configuration.dev_permissions),
+ std::move(ueventd_configuration.sysfs_permissions),
+ std::move(ueventd_configuration.subsystems), fs_mgr_get_boot_devices(), true));
+ uevent_handlers.emplace_back(std::make_unique<FirmwareHandler>(
+ std::move(ueventd_configuration.firmware_directories)));
- if (ueventd_configuration.enable_modalias_handling) {
- uevent_handlers.emplace_back(std::make_unique<ModaliasHandler>());
- }
+ if (ueventd_configuration.enable_modalias_handling) {
+ uevent_handlers.emplace_back(std::make_unique<ModaliasHandler>());
}
+ UeventListener uevent_listener(ueventd_configuration.uevent_socket_rcvbuf_size);
if (access(COLDBOOT_DONE, F_OK) != 0) {
ColdBoot cold_boot(uevent_listener, uevent_handlers);
diff --git a/init/ueventd_parser.cpp b/init/ueventd_parser.cpp
index 677938e..aac3fe5 100644
--- a/init/ueventd_parser.cpp
+++ b/init/ueventd_parser.cpp
@@ -19,9 +19,13 @@
#include <grp.h>
#include <pwd.h>
+#include <android-base/parseint.h>
+
#include "keyword_map.h"
#include "parser.h"
+using android::base::ParseByteCount;
+
namespace android {
namespace init {
@@ -101,6 +105,22 @@
return Success();
}
+Result<Success> ParseUeventSocketRcvbufSizeLine(std::vector<std::string>&& args,
+ size_t* uevent_socket_rcvbuf_size) {
+ if (args.size() != 2) {
+ return Error() << "uevent_socket_rcvbuf_size lines take exactly one parameter";
+ }
+
+ size_t parsed_size;
+ if (!ParseByteCount(args[1], &parsed_size)) {
+ return Error() << "could not parse size '" << args[1] << "' for uevent_socket_rcvbuf_line";
+ }
+
+ *uevent_socket_rcvbuf_size = parsed_size;
+
+ return Success();
+}
+
class SubsystemParser : public SectionParser {
public:
SubsystemParser(std::vector<Subsystem>* subsystems) : subsystems_(subsystems) {}
@@ -202,6 +222,9 @@
parser.AddSingleLineParser("modalias_handling",
std::bind(ParseModaliasHandlingLine, _1,
&ueventd_configuration.enable_modalias_handling));
+ parser.AddSingleLineParser("uevent_socket_rcvbuf_size",
+ std::bind(ParseUeventSocketRcvbufSizeLine, _1,
+ &ueventd_configuration.uevent_socket_rcvbuf_size));
for (const auto& config : configs) {
parser.ParseConfig(config);
diff --git a/init/ueventd_parser.h b/init/ueventd_parser.h
index 7d30edf..d476dec 100644
--- a/init/ueventd_parser.h
+++ b/init/ueventd_parser.h
@@ -31,6 +31,7 @@
std::vector<Permissions> dev_permissions;
std::vector<std::string> firmware_directories;
bool enable_modalias_handling = false;
+ size_t uevent_socket_rcvbuf_size = 0;
};
UeventdConfiguration ParseConfig(const std::vector<std::string>& configs);
diff --git a/init/ueventd_parser_test.cpp b/init/ueventd_parser_test.cpp
index c3af341..9c1cedf 100644
--- a/init/ueventd_parser_test.cpp
+++ b/init/ueventd_parser_test.cpp
@@ -138,6 +138,15 @@
TestUeventdFile(ueventd_file, {{}, {}, {}, firmware_directories});
}
+TEST(ueventd_parser, UeventSocketRcvbufSize) {
+ auto ueventd_file = R"(
+uevent_socket_rcvbuf_size 8k
+uevent_socket_rcvbuf_size 8M
+)";
+
+ TestUeventdFile(ueventd_file, {{}, {}, {}, {}, false, 8 * 1024 * 1024});
+}
+
TEST(ueventd_parser, AllTogether) {
auto ueventd_file = R"(
@@ -169,6 +178,8 @@
/sys/devices/virtual/*/input poll_delay 0660 root input
firmware_directories /more
+uevent_socket_rcvbuf_size 6M
+
#ending comment
)";
@@ -197,8 +208,10 @@
"/more",
};
- TestUeventdFile(ueventd_file,
- {subsystems, sysfs_permissions, permissions, firmware_directories});
+ size_t uevent_socket_rcvbuf_size = 6 * 1024 * 1024;
+
+ TestUeventdFile(ueventd_file, {subsystems, sysfs_permissions, permissions, firmware_directories,
+ false, uevent_socket_rcvbuf_size});
}
// All of these lines are ill-formed, so test that there is 0 output.
@@ -213,6 +226,8 @@
/sys/devices/platform/trusty.* trusty_version 0440 baduidbad log
/sys/devices/platform/trusty.* trusty_version 0440 root baduidbad
+uevent_socket_rcvbuf_size blah
+
subsystem #no name
)";
diff --git a/libcutils/fs_config.cpp b/libcutils/fs_config.cpp
index ee52f5e..db59569 100644
--- a/libcutils/fs_config.cpp
+++ b/libcutils/fs_config.cpp
@@ -108,7 +108,9 @@
// although the developer is advised to restrict the scope to the /vendor or
// oem/ file-system since the intent is to provide support for customized
// portions of a separate vendor.img or oem.img. Has to remain open so that
-// customization can also land on /system/vendor, /system/oem or /system/odm.
+// customization can also land on /system/vendor, /system/oem, /system/odm,
+// /system/product or /system/product_services.
+//
// We expect build-time checking or filtering when constructing the associated
// fs_config_* files (see build/tools/fs_config/fs_config_generate.c)
static const char ven_conf_dir[] = "/vendor/etc/fs_config_dirs";
@@ -117,11 +119,17 @@
static const char oem_conf_file[] = "/oem/etc/fs_config_files";
static const char odm_conf_dir[] = "/odm/etc/fs_config_dirs";
static const char odm_conf_file[] = "/odm/etc/fs_config_files";
+static const char product_conf_dir[] = "/product/etc/fs_config_dirs";
+static const char product_conf_file[] = "/product/etc/fs_config_files";
+static const char product_services_conf_dir[] = "/product_services/etc/fs_config_dirs";
+static const char product_services_conf_file[] = "/product_services/etc/fs_config_files";
static const char* conf[][2] = {
- {sys_conf_file, sys_conf_dir},
- {ven_conf_file, ven_conf_dir},
- {oem_conf_file, oem_conf_dir},
- {odm_conf_file, odm_conf_dir},
+ {sys_conf_file, sys_conf_dir},
+ {ven_conf_file, ven_conf_dir},
+ {oem_conf_file, oem_conf_dir},
+ {odm_conf_file, odm_conf_dir},
+ {product_conf_file, product_conf_dir},
+ {product_services_conf_file, product_services_conf_dir},
};
// Do not use android_files to grant Linux capabilities. Use ambient capabilities in their
@@ -150,7 +158,11 @@
{ 00444, AID_ROOT, AID_ROOT, 0, oem_conf_dir + 1 },
{ 00444, AID_ROOT, AID_ROOT, 0, oem_conf_file + 1 },
{ 00600, AID_ROOT, AID_ROOT, 0, "product/build.prop" },
+ { 00444, AID_ROOT, AID_ROOT, 0, product_conf_dir + 1 },
+ { 00444, AID_ROOT, AID_ROOT, 0, product_conf_file + 1 },
{ 00600, AID_ROOT, AID_ROOT, 0, "product_services/build.prop" },
+ { 00444, AID_ROOT, AID_ROOT, 0, product_services_conf_dir + 1 },
+ { 00444, AID_ROOT, AID_ROOT, 0, product_services_conf_file + 1 },
{ 00750, AID_ROOT, AID_SHELL, 0, "sbin/fs_mgr" },
{ 00755, AID_ROOT, AID_SHELL, 0, "system/bin/crash_dump32" },
{ 00755, AID_ROOT, AID_SHELL, 0, "system/bin/crash_dump64" },
@@ -236,10 +248,10 @@
return fd;
}
-// if path is "odm/<stuff>", "oem/<stuff>", "product/<stuff>" or
-// "vendor/<stuff>"
+// if path is "odm/<stuff>", "oem/<stuff>", "product/<stuff>",
+// "product_services/<stuff>" or "vendor/<stuff>"
static bool is_partition(const char* path, size_t len) {
- static const char* partitions[] = {"odm/", "oem/", "product/", "vendor/"};
+ static const char* partitions[] = {"odm/", "oem/", "product/", "product_services/", "vendor/"};
for (size_t i = 0; i < (sizeof(partitions) / sizeof(partitions[0])); ++i) {
size_t plen = strlen(partitions[i]);
if (len <= plen) continue;
diff --git a/libmeminfo/Android.bp b/libmeminfo/Android.bp
index aab3743..3e191ad 100644
--- a/libmeminfo/Android.bp
+++ b/libmeminfo/Android.bp
@@ -54,6 +54,11 @@
srcs: [
"libmeminfo_test.cpp"
],
+
+ data: [
+ "testdata1/*",
+ "testdata2/*"
+ ],
}
cc_benchmark {
@@ -67,4 +72,8 @@
"libmeminfo",
"libprocinfo",
],
+
+ data: [
+ "testdata1/*",
+ ],
}
diff --git a/libmeminfo/OWNERS b/libmeminfo/OWNERS
new file mode 100644
index 0000000..26e71fe
--- /dev/null
+++ b/libmeminfo/OWNERS
@@ -0,0 +1 @@
+sspatil@google.com
diff --git a/libmeminfo/include/meminfo/meminfo.h b/libmeminfo/include/meminfo/meminfo.h
index c328648..809054b 100644
--- a/libmeminfo/include/meminfo/meminfo.h
+++ b/libmeminfo/include/meminfo/meminfo.h
@@ -31,6 +31,8 @@
uint64_t pss;
uint64_t uss;
+ uint64_t swap;
+
uint64_t private_clean;
uint64_t private_dirty;
uint64_t shared_clean;
@@ -41,6 +43,7 @@
rss(0),
pss(0),
uss(0),
+ swap(0),
private_clean(0),
private_dirty(0),
shared_clean(0),
@@ -49,7 +52,7 @@
~MemUsage() = default;
void clear() {
- vss = rss = pss = uss = 0;
+ vss = rss = pss = uss = swap = 0;
private_clean = private_dirty = shared_clean = shared_dirty = 0;
}
};
diff --git a/libmeminfo/include/meminfo/procmeminfo.h b/libmeminfo/include/meminfo/procmeminfo.h
index b37c56b..92375d3 100644
--- a/libmeminfo/include/meminfo/procmeminfo.h
+++ b/libmeminfo/include/meminfo/procmeminfo.h
@@ -29,13 +29,16 @@
class ProcMemInfo final {
// Per-process memory accounting
public:
- ProcMemInfo(pid_t pid, bool get_wss = false);
+ // Reset the working set accounting of the process via /proc/<pid>/clear_refs
+ static bool ResetWorkingSet(pid_t pid);
+
+ ProcMemInfo(pid_t pid, bool get_wss = false, uint64_t pgflags = 0, uint64_t pgflags_mask = 0);
const std::vector<Vma>& Maps();
const MemUsage& Usage();
const MemUsage& Wss();
+ const std::vector<uint16_t>& SwapOffsets();
- bool WssReset();
~ProcMemInfo() = default;
private:
@@ -44,11 +47,14 @@
pid_t pid_;
bool get_wss_;
+ uint64_t pgflags_;
+ uint64_t pgflags_mask_;
std::vector<Vma> maps_;
MemUsage usage_;
MemUsage wss_;
+ std::vector<uint16_t> swap_offsets_;
};
} // namespace meminfo
diff --git a/libmeminfo/include/meminfo/sysmeminfo.h b/libmeminfo/include/meminfo/sysmeminfo.h
index f5e05bd..885be1d 100644
--- a/libmeminfo/include/meminfo/sysmeminfo.h
+++ b/libmeminfo/include/meminfo/sysmeminfo.h
@@ -28,6 +28,21 @@
class SysMemInfo final {
// System or Global memory accounting
public:
+ static constexpr const char* kMemTotal = "MemTotal:";
+ static constexpr const char* kMemFree = "MemFree:";
+ static constexpr const char* kMemBuffers = "Buffers:";
+ static constexpr const char* kMemCached = "Cached:";
+ static constexpr const char* kMemShmem = "Shmem:";
+ static constexpr const char* kMemSlab = "Slab:";
+ static constexpr const char* kMemSReclaim = "SReclaimable:";
+ static constexpr const char* kMemSUnreclaim = "SUnreclaim:";
+ static constexpr const char* kMemSwapTotal = "SwapTotal:";
+ static constexpr const char* kMemSwapFree = "SwapFree:";
+ static constexpr const char* kMemMapped = "Mapped:";
+ static constexpr const char* kMemVmallocUsed = "VmallocUsed:";
+ static constexpr const char* kMemPageTables = "PageTables:";
+ static constexpr const char* kMemKernelStack = "KernelStack:";
+
static const std::vector<std::string> kDefaultSysMemInfoTags;
SysMemInfo() = default;
@@ -38,24 +53,25 @@
const std::string& path = "/proc/meminfo");
// getters
- uint64_t mem_total_kb() { return mem_in_kb_["MemTotal:"]; }
- uint64_t mem_free_kb() { return mem_in_kb_["MemFree:"]; }
- uint64_t mem_buffers_kb() { return mem_in_kb_["Buffers:"]; }
- uint64_t mem_cached_kb() { return mem_in_kb_["Cached:"]; }
- uint64_t mem_shmem_kb() { return mem_in_kb_["Shmem:"]; }
- uint64_t mem_slab_kb() { return mem_in_kb_["Slab:"]; }
- uint64_t mem_slab_reclailmable_kb() { return mem_in_kb_["SReclaimable:"]; }
- uint64_t mem_slab_unreclaimable_kb() { return mem_in_kb_["SUnreclaim:"]; }
- uint64_t mem_swap_kb() { return mem_in_kb_["SwapTotal:"]; }
- uint64_t mem_free_swap_kb() { return mem_in_kb_["SwapFree:"]; }
- uint64_t mem_zram_kb() { return mem_in_kb_["Zram:"]; }
- uint64_t mem_mapped_kb() { return mem_in_kb_["Mapped:"]; }
- uint64_t mem_vmalloc_used_kb() { return mem_in_kb_["VmallocUsed:"]; }
- uint64_t mem_page_tables_kb() { return mem_in_kb_["PageTables:"]; }
- uint64_t mem_kernel_stack_kb() { return mem_in_kb_["KernelStack:"]; }
+ uint64_t mem_total_kb() { return mem_in_kb_[kMemTotal]; }
+ uint64_t mem_free_kb() { return mem_in_kb_[kMemFree]; }
+ uint64_t mem_buffers_kb() { return mem_in_kb_[kMemBuffers]; }
+ uint64_t mem_cached_kb() { return mem_in_kb_[kMemCached]; }
+ uint64_t mem_shmem_kb() { return mem_in_kb_[kMemShmem]; }
+ uint64_t mem_slab_kb() { return mem_in_kb_[kMemSlab]; }
+ uint64_t mem_slab_reclailmable_kb() { return mem_in_kb_[kMemSReclaim]; }
+ uint64_t mem_slab_unreclaimable_kb() { return mem_in_kb_[kMemSUnreclaim]; }
+ uint64_t mem_swap_kb() { return mem_in_kb_[kMemSwapTotal]; }
+ uint64_t mem_swap_free_kb() { return mem_in_kb_[kMemSwapFree]; }
+ uint64_t mem_mapped_kb() { return mem_in_kb_[kMemMapped]; }
+ uint64_t mem_vmalloc_used_kb() { return mem_in_kb_[kMemVmallocUsed]; }
+ uint64_t mem_page_tables_kb() { return mem_in_kb_[kMemPageTables]; }
+ uint64_t mem_kernel_stack_kb() { return mem_in_kb_[kMemPageTables]; }
+ uint64_t mem_zram_kb(const std::string& zram_dev = "");
private:
std::map<std::string, uint64_t> mem_in_kb_;
+ bool MemZramDevice(const std::string& zram_dev, uint64_t* mem_zram_dev);
};
} // namespace meminfo
diff --git a/libmeminfo/libmeminfo_benchmark.cpp b/libmeminfo/libmeminfo_benchmark.cpp
index e2239f0..1db0824 100644
--- a/libmeminfo/libmeminfo_benchmark.cpp
+++ b/libmeminfo/libmeminfo_benchmark.cpp
@@ -17,6 +17,8 @@
#include <meminfo/sysmeminfo.h>
#include <fcntl.h>
+#include <inttypes.h>
+#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
@@ -46,7 +48,7 @@
MEMINFO_COUNT
};
-void get_mem_info(uint64_t mem[], const char* file) {
+static void get_mem_info(uint64_t mem[], const char* file) {
char buffer[4096];
unsigned int numFound = 0;
@@ -67,9 +69,10 @@
buffer[len] = 0;
static const char* const tags[] = {
- "MemTotal:", "MemFree:", "Buffers:", "Cached:", "Shmem:", "Slab:",
- "SReclaimable:", "SUnreclaim:", "SwapTotal:", "SwapFree:", "ZRam:", "Mapped:",
- "VmallocUsed:", "PageTables:", "KernelStack:", NULL};
+ "MemTotal:", "MemFree:", "Buffers:", "Cached:", "Shmem:", "Slab:",
+ "SReclaimable:", "SUnreclaim:", "SwapTotal:", "SwapFree:", "ZRam:", "Mapped:",
+ "VmallocUsed:", "PageTables:", "KernelStack:", NULL
+ };
static const int tagsLen[] = {9, 8, 8, 7, 6, 5, 13, 11, 10, 9, 5, 7, 12, 11, 12, 0};
@@ -78,7 +81,8 @@
while (*p && (numFound < (sizeof(tagsLen) / sizeof(tagsLen[0])))) {
int i = 0;
while (tags[i]) {
- //std::cout << "tag =" << tags[i] << " p = " << std::string(p, tagsLen[i]) << std::endl;
+ // std::cout << "tag =" << tags[i] << " p = " << std::string(p, tagsLen[i]) <<
+ // std::endl;
if (strncmp(p, tags[i], tagsLen[i]) == 0) {
p += tagsLen[i];
while (*p == ' ') p++;
@@ -214,4 +218,51 @@
}
BENCHMARK(BM_ReadMemInfo);
+static uint64_t get_zram_mem_used(const std::string& zram_dir) {
+ FILE* f = fopen((zram_dir + "mm_stat").c_str(), "r");
+ if (f) {
+ uint64_t mem_used_total = 0;
+
+ int matched = fscanf(f, "%*d %*d %" SCNu64 " %*d %*d %*d %*d", &mem_used_total);
+ if (matched != 1)
+ fprintf(stderr, "warning: failed to parse %s\n", (zram_dir + "mm_stat").c_str());
+
+ fclose(f);
+ return mem_used_total;
+ }
+
+ f = fopen((zram_dir + "mem_used_total").c_str(), "r");
+ if (f) {
+ uint64_t mem_used_total = 0;
+
+ int matched = fscanf(f, "%" SCNu64, &mem_used_total);
+ if (matched != 1)
+ fprintf(stderr, "warning: failed to parse %s\n", (zram_dir + "mem_used_total").c_str());
+
+ fclose(f);
+ return mem_used_total;
+ }
+
+ return 0;
+}
+
+static void BM_OldReadZramTotal(benchmark::State& state) {
+ std::string exec_dir = ::android::base::GetExecutableDirectory();
+ std::string zram_mmstat_dir = exec_dir + "/testdata1/";
+ for (auto _ : state) {
+ uint64_t zram_total __attribute__((unused)) = get_zram_mem_used(zram_mmstat_dir) / 1024;
+ }
+}
+BENCHMARK(BM_OldReadZramTotal);
+
+static void BM_NewReadZramTotal(benchmark::State& state) {
+ std::string exec_dir = ::android::base::GetExecutableDirectory();
+ std::string zram_mmstat_dir = exec_dir + "/testdata1/";
+ ::android::meminfo::SysMemInfo mi;
+ for (auto _ : state) {
+ uint64_t zram_total __attribute__((unused)) = mi.mem_zram_kb(zram_mmstat_dir);
+ }
+}
+BENCHMARK(BM_NewReadZramTotal);
+
BENCHMARK_MAIN();
diff --git a/libmeminfo/libmeminfo_test.cpp b/libmeminfo/libmeminfo_test.cpp
index 7a2be41..b7a4b6b 100644
--- a/libmeminfo/libmeminfo_test.cpp
+++ b/libmeminfo/libmeminfo_test.cpp
@@ -73,7 +73,7 @@
}
}
-TEST_F(ValidateProcMemInfo, TestMapsUsage) {
+TEST_F(ValidateProcMemInfo, TestMaps) {
const std::vector<Vma>& maps = proc_mem->Maps();
ASSERT_FALSE(maps.empty());
ASSERT_EQ(proc->num_maps, maps.size());
@@ -96,6 +96,30 @@
EXPECT_EQ(proc_usage.uss, proc_mem->Usage().uss);
}
+TEST_F(ValidateProcMemInfo, TestSwapUsage) {
+ const std::vector<Vma>& maps = proc_mem->Maps();
+ ASSERT_FALSE(maps.empty());
+ ASSERT_EQ(proc->num_maps, maps.size());
+
+ pm_memusage_t map_usage, proc_usage;
+ pm_memusage_zero(&map_usage);
+ pm_memusage_zero(&proc_usage);
+ for (size_t i = 0; i < maps.size(); i++) {
+ ASSERT_EQ(0, pm_map_usage(proc->maps[i], &map_usage));
+ EXPECT_EQ(map_usage.swap, maps[i].usage.swap) << "SWAP mismatch for map: " << maps[i].name;
+ pm_memusage_add(&proc_usage, &map_usage);
+ }
+
+ EXPECT_EQ(proc_usage.swap, proc_mem->Usage().swap);
+}
+
+TEST_F(ValidateProcMemInfo, TestSwapOffsets) {
+ const MemUsage& proc_usage = proc_mem->Usage();
+ const std::vector<uint16_t>& swap_offsets = proc_mem->SwapOffsets();
+
+ EXPECT_EQ(proc_usage.swap / getpagesize(), swap_offsets.size());
+}
+
class ValidateProcMemInfoWss : public ::testing::Test {
protected:
void SetUp() override {
@@ -118,7 +142,7 @@
TEST_F(ValidateProcMemInfoWss, TestWorkingTestReset) {
// Expect reset to succeed
- EXPECT_TRUE(proc_mem->WssReset());
+ EXPECT_TRUE(ProcMemInfo::ResetWorkingSet(pid));
}
TEST_F(ValidateProcMemInfoWss, TestWssEquality) {
@@ -221,6 +245,93 @@
}
}
+TEST(TestProcMemInfo, TestMapsEmpty) {
+ ProcMemInfo proc_mem(pid);
+ const std::vector<Vma>& maps = proc_mem.Maps();
+ EXPECT_GT(maps.size(), 0);
+}
+
+TEST(TestProcMemInfo, TestUsageEmpty) {
+ // If we created the object for getting working set,
+ // the usage must be empty
+ ProcMemInfo proc_mem(pid, true);
+ const MemUsage& usage = proc_mem.Usage();
+ EXPECT_EQ(usage.rss, 0);
+ EXPECT_EQ(usage.vss, 0);
+ EXPECT_EQ(usage.pss, 0);
+ EXPECT_EQ(usage.uss, 0);
+ EXPECT_EQ(usage.swap, 0);
+}
+
+TEST(TestProcMemInfoWssReset, TestWssEmpty) {
+ // If we created the object for getting usage,
+ // the working set must be empty
+ ProcMemInfo proc_mem(pid, false);
+ const MemUsage& wss = proc_mem.Wss();
+ EXPECT_EQ(wss.rss, 0);
+ EXPECT_EQ(wss.vss, 0);
+ EXPECT_EQ(wss.pss, 0);
+ EXPECT_EQ(wss.uss, 0);
+ EXPECT_EQ(wss.swap, 0);
+}
+
+TEST(TestProcMemInfoWssReset, TestSwapOffsetsEmpty) {
+ // If we created the object for getting working set,
+ // the swap offsets must be empty
+ ProcMemInfo proc_mem(pid, true);
+ const std::vector<uint16_t>& swap_offsets = proc_mem.SwapOffsets();
+ EXPECT_EQ(swap_offsets.size(), 0);
+}
+
+TEST(ValidateProcMemInfoFlags, TestPageFlags1) {
+ // Create proc object using libpagemap
+ pm_kernel_t* ker;
+ ASSERT_EQ(0, pm_kernel_create(&ker));
+ pm_process_t* proc;
+ ASSERT_EQ(0, pm_process_create(ker, pid, &proc));
+
+ // count swapbacked pages using libpagemap
+ pm_memusage_t proc_usage;
+ pm_memusage_zero(&proc_usage);
+ ASSERT_EQ(0, pm_process_usage_flags(proc, &proc_usage, (1 << KPF_SWAPBACKED),
+ (1 << KPF_SWAPBACKED)));
+
+ // Create ProcMemInfo that counts swapbacked pages
+ ProcMemInfo proc_mem(pid, false, (1 << KPF_SWAPBACKED), (1 << KPF_SWAPBACKED));
+
+ EXPECT_EQ(proc_usage.vss, proc_mem.Usage().vss);
+ EXPECT_EQ(proc_usage.rss, proc_mem.Usage().rss);
+ EXPECT_EQ(proc_usage.pss, proc_mem.Usage().pss);
+ EXPECT_EQ(proc_usage.uss, proc_mem.Usage().uss);
+
+ pm_process_destroy(proc);
+ pm_kernel_destroy(ker);
+}
+
+TEST(ValidateProcMemInfoFlags, TestPageFlags2) {
+ // Create proc object using libpagemap
+ pm_kernel_t* ker;
+ ASSERT_EQ(0, pm_kernel_create(&ker));
+ pm_process_t* proc;
+ ASSERT_EQ(0, pm_process_create(ker, pid, &proc));
+
+ // count non-swapbacked pages using libpagemap
+ pm_memusage_t proc_usage;
+ pm_memusage_zero(&proc_usage);
+ ASSERT_EQ(0, pm_process_usage_flags(proc, &proc_usage, (1 << KPF_SWAPBACKED), 0));
+
+ // Create ProcMemInfo that counts non-swapbacked pages
+ ProcMemInfo proc_mem(pid, false, 0, (1 << KPF_SWAPBACKED));
+
+ EXPECT_EQ(proc_usage.vss, proc_mem.Usage().vss);
+ EXPECT_EQ(proc_usage.rss, proc_mem.Usage().rss);
+ EXPECT_EQ(proc_usage.pss, proc_mem.Usage().pss);
+ EXPECT_EQ(proc_usage.uss, proc_mem.Usage().uss);
+
+ pm_process_destroy(proc);
+ pm_kernel_destroy(ker);
+}
+
TEST(SysMemInfoParser, TestSysMemInfoFile) {
std::string meminfo = R"meminfo(MemTotal: 3019740 kB
MemFree: 1809728 kB
@@ -288,6 +399,17 @@
EXPECT_EQ(mi.mem_total_kb(), 0);
}
+TEST(SysMemInfoParse, TestZramTotal) {
+ std::string exec_dir = ::android::base::GetExecutableDirectory();
+
+ SysMemInfo mi;
+ std::string zram_mmstat_dir = exec_dir + "/testdata1/";
+ EXPECT_EQ(mi.mem_zram_kb(zram_mmstat_dir), 30504);
+
+ std::string zram_memused_dir = exec_dir + "/testdata2/";
+ EXPECT_EQ(mi.mem_zram_kb(zram_memused_dir), 30504);
+}
+
int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
if (argc <= 1) {
diff --git a/libmeminfo/procmeminfo.cpp b/libmeminfo/procmeminfo.cpp
index fe91d25..8867e14 100644
--- a/libmeminfo/procmeminfo.cpp
+++ b/libmeminfo/procmeminfo.cpp
@@ -44,6 +44,8 @@
to->pss += from.pss;
to->uss += from.uss;
+ to->swap += from.swap;
+
to->private_clean += from.private_clean;
to->private_dirty += from.private_dirty;
@@ -51,48 +53,78 @@
to->shared_dirty += from.shared_dirty;
}
-ProcMemInfo::ProcMemInfo(pid_t pid, bool get_wss) : pid_(pid), get_wss_(get_wss) {
- if (!ReadMaps(get_wss_)) {
- LOG(ERROR) << "Failed to read maps for Process " << pid_;
- }
-}
-
-const std::vector<Vma>& ProcMemInfo::Maps() {
- return maps_;
-}
-
-const MemUsage& ProcMemInfo::Usage() {
- if (get_wss_) {
- LOG(WARNING) << "Trying to read memory usage from working set object";
- }
- return usage_;
-}
-
-const MemUsage& ProcMemInfo::Wss() {
- if (!get_wss_) {
- LOG(WARNING) << "Trying to read working set when there is none";
- }
-
- return wss_;
-}
-
-bool ProcMemInfo::WssReset() {
- if (!get_wss_) {
- LOG(ERROR) << "Trying to reset working set from a memory usage counting object";
- return false;
- }
-
- std::string clear_refs_path = ::android::base::StringPrintf("/proc/%d/clear_refs", pid_);
+bool ProcMemInfo::ResetWorkingSet(pid_t pid) {
+ std::string clear_refs_path = ::android::base::StringPrintf("/proc/%d/clear_refs", pid);
if (!::android::base::WriteStringToFile("1\n", clear_refs_path)) {
PLOG(ERROR) << "Failed to write to " << clear_refs_path;
return false;
}
- wss_.clear();
return true;
}
+ProcMemInfo::ProcMemInfo(pid_t pid, bool get_wss, uint64_t pgflags, uint64_t pgflags_mask)
+ : pid_(pid), get_wss_(get_wss), pgflags_(pgflags), pgflags_mask_(pgflags_mask) {}
+
+const std::vector<Vma>& ProcMemInfo::Maps() {
+ if (maps_.empty() && !ReadMaps(get_wss_)) {
+ LOG(ERROR) << "Failed to read maps for Process " << pid_;
+ }
+
+ return maps_;
+}
+
+const MemUsage& ProcMemInfo::Usage() {
+ if (get_wss_) {
+ LOG(WARNING) << "Trying to read process memory usage for " << pid_
+ << " using invalid object";
+ return usage_;
+ }
+
+ if (maps_.empty() && !ReadMaps(get_wss_)) {
+ LOG(ERROR) << "Failed to get memory usage for Process " << pid_;
+ }
+
+ return usage_;
+}
+
+const MemUsage& ProcMemInfo::Wss() {
+ if (!get_wss_) {
+ LOG(WARNING) << "Trying to read process working set for " << pid_
+ << " using invalid object";
+ return wss_;
+ }
+
+ if (maps_.empty() && !ReadMaps(get_wss_)) {
+ LOG(ERROR) << "Failed to get working set for Process " << pid_;
+ }
+
+ return wss_;
+}
+
+const std::vector<uint16_t>& ProcMemInfo::SwapOffsets() {
+ if (get_wss_) {
+ LOG(WARNING) << "Trying to read process swap offsets for " << pid_
+ << " using invalid object";
+ return swap_offsets_;
+ }
+
+ if (maps_.empty() && !ReadMaps(get_wss_)) {
+ LOG(ERROR) << "Failed to get swap offsets for Process " << pid_;
+ }
+
+ return swap_offsets_;
+}
+
bool ProcMemInfo::ReadMaps(bool get_wss) {
+ // Each object reads /proc/<pid>/maps only once. This is done to make sure programs that are
+ // running for the lifetime of the system can recycle the objects and don't have to
+ // unnecessarily retain and update this object in memory (which can get significantly large).
+ // E.g. A program that only needs to reset the working set will never all ->Maps(), ->Usage().
+ // E.g. A program that is monitoring smaps_rollup, may never call ->maps(), Usage(), so it
+ // doesn't make sense for us to parse and retain unnecessary memory accounting stats by default.
+ if (!maps_.empty()) return true;
+
// parse and read /proc/<pid>/maps
std::string maps_file = ::android::base::StringPrintf("/proc/%d/maps", pid_);
if (!::android::procinfo::ReadMapFile(
@@ -115,8 +147,8 @@
for (auto& vma : maps_) {
if (!ReadVmaStats(pagemap_fd.get(), vma, get_wss)) {
- LOG(ERROR) << "Failed to read page map for vma " << vma.name << "[" << vma.start
- << "-" << vma.end << "]";
+ LOG(ERROR) << "Failed to read page map for vma " << vma.name << "[" << vma.start << "-"
+ << vma.end << "]";
maps_.clear();
return false;
}
@@ -153,18 +185,24 @@
if (!PAGE_PRESENT(p) && !PAGE_SWAPPED(p)) continue;
if (PAGE_SWAPPED(p)) {
- // TODO: do what's needed for swapped pages
+ vma.usage.swap += pagesz;
+ swap_offsets_.emplace_back(PAGE_SWAP_OFFSET(p));
continue;
}
uint64_t page_frame = PAGE_PFN(p);
if (!pinfo.PageFlags(page_frame, &pg_flags[i])) {
LOG(ERROR) << "Failed to get page flags for " << page_frame << " in process " << pid_;
+ swap_offsets_.clear();
return false;
}
+ // skip unwanted pages from the count
+ if ((pg_flags[i] & pgflags_mask_) != pgflags_) continue;
+
if (!pinfo.PageMapCount(page_frame, &pg_counts[i])) {
LOG(ERROR) << "Failed to get page count for " << page_frame << " in process " << pid_;
+ swap_offsets_.clear();
return false;
}
diff --git a/libmeminfo/sysmeminfo.cpp b/libmeminfo/sysmeminfo.cpp
index 50fa213..7e56238 100644
--- a/libmeminfo/sysmeminfo.cpp
+++ b/libmeminfo/sysmeminfo.cpp
@@ -17,10 +17,12 @@
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
+#include <inttypes.h>
#include <stdlib.h>
#include <unistd.h>
#include <cctype>
+#include <cstdio>
#include <fstream>
#include <string>
#include <utility>
@@ -29,7 +31,9 @@
#include <android-base/file.h>
#include <android-base/logging.h>
#include <android-base/parseint.h>
+#include <android-base/stringprintf.h>
#include <android-base/strings.h>
+#include <android-base/unique_fd.h>
#include "meminfo_private.h"
@@ -37,9 +41,11 @@
namespace meminfo {
const std::vector<std::string> SysMemInfo::kDefaultSysMemInfoTags = {
- "MemTotal:", "MemFree:", "Buffers:", "Cached:", "Shmem:",
- "Slab:", "SReclaimable:", "SUnreclaim:", "SwapTotal:", "SwapFree:",
- "ZRam:", "Mapped:", "VmallocUsed:", "PageTables:", "KernelStack:",
+ SysMemInfo::kMemTotal, SysMemInfo::kMemFree, SysMemInfo::kMemBuffers,
+ SysMemInfo::kMemCached, SysMemInfo::kMemShmem, SysMemInfo::kMemSlab,
+ SysMemInfo::kMemSReclaim, SysMemInfo::kMemSUnreclaim, SysMemInfo::kMemSwapTotal,
+ SysMemInfo::kMemSwapFree, SysMemInfo::kMemMapped, SysMemInfo::kMemVmallocUsed,
+ SysMemInfo::kMemPageTables, SysMemInfo::kMemKernelStack,
};
bool SysMemInfo::ReadMemInfo(const std::string& path) {
@@ -99,6 +105,7 @@
buffer[len] = '\0';
char* p = buffer;
uint32_t found = 0;
+ uint32_t lineno = 0;
while (*p && found < tags.size()) {
for (auto& tag : tags) {
if (strncmp(p, tag.c_str(), tag.size()) == 0) {
@@ -107,7 +114,7 @@
char* endptr = nullptr;
mem_in_kb_[tag] = strtoull(p, &endptr, 10);
if (p == endptr) {
- PLOG(ERROR) << "Failed to parse line in file: " << path;
+ PLOG(ERROR) << "Failed to parse line:" << lineno + 1 << " in file: " << path;
return false;
}
p = endptr;
@@ -119,11 +126,75 @@
p++;
}
if (*p) p++;
+ lineno++;
}
return true;
}
#endif
+uint64_t SysMemInfo::mem_zram_kb(const std::string& zram_dev) {
+ uint64_t mem_zram_total = 0;
+ if (!zram_dev.empty()) {
+ if (!MemZramDevice(zram_dev, &mem_zram_total)) {
+ return 0;
+ }
+ return mem_zram_total / 1024;
+ }
+
+ constexpr uint32_t kMaxZramDevices = 256;
+ for (uint32_t i = 0; i < kMaxZramDevices; i++) {
+ std::string zram_dev = ::android::base::StringPrintf("/sys/block/zram%u/", i);
+ if (access(zram_dev.c_str(), F_OK)) {
+ // We assume zram devices appear in range 0-255 and appear always in sequence
+ // under /sys/block. So, stop looking for them once we find one is missing.
+ break;
+ }
+
+ uint64_t mem_zram_dev;
+ if (!MemZramDevice(zram_dev, &mem_zram_dev)) {
+ return 0;
+ }
+
+ mem_zram_total += mem_zram_dev;
+ }
+
+ return mem_zram_total / 1024;
+}
+
+bool SysMemInfo::MemZramDevice(const std::string& zram_dev, uint64_t* mem_zram_dev) {
+ std::string content;
+ if (android::base::ReadFileToString(zram_dev + "mm_stat", &content)) {
+ std::vector<std::string> values = ::android::base::Split(content, " ");
+ if (values.size() < 3) {
+ LOG(ERROR) << "Malformed mm_stat file for zram dev: " << zram_dev
+ << " content: " << content;
+ return false;
+ }
+
+ if (!::android::base::ParseUint(values[2], mem_zram_dev)) {
+ LOG(ERROR) << "Malformed mm_stat file for zram dev: " << zram_dev
+ << " value: " << values[2];
+ return false;
+ }
+
+ return true;
+ }
+
+ if (::android::base::ReadFileToString(zram_dev + "mem_used_total", &content)) {
+ *mem_zram_dev = strtoull(content.c_str(), NULL, 10);
+ if (*mem_zram_dev == ULLONG_MAX) {
+ PLOG(ERROR) << "Malformed mem_used_total file for zram dev: " << zram_dev
+ << " content: " << content;
+ return false;
+ }
+
+ return true;
+ }
+
+ LOG(ERROR) << "Can't find memory status under: " << zram_dev;
+ return false;
+}
+
} // namespace meminfo
} // namespace android
diff --git a/libmeminfo/testdata1/mm_stat b/libmeminfo/testdata1/mm_stat
new file mode 100644
index 0000000..684f567
--- /dev/null
+++ b/libmeminfo/testdata1/mm_stat
@@ -0,0 +1 @@
+145674240 26801454 31236096 0 45772800 3042 1887 517
diff --git a/libmeminfo/testdata2/mem_used_total b/libmeminfo/testdata2/mem_used_total
new file mode 100644
index 0000000..97fcf41
--- /dev/null
+++ b/libmeminfo/testdata2/mem_used_total
@@ -0,0 +1 @@
+31236096
diff --git a/libmeminfo/tools/Android.bp b/libmeminfo/tools/Android.bp
index 0870130..715d63d 100644
--- a/libmeminfo/tools/Android.bp
+++ b/libmeminfo/tools/Android.bp
@@ -22,6 +22,22 @@
srcs: ["procmem.cpp"],
shared_libs: [
+ "libbase",
+ "libmeminfo",
+ ],
+}
+
+cc_binary {
+ name: "procrank2",
+ cflags: [
+ "-Wall",
+ "-Werror",
+ "-Wno-unused-parameter",
+ ],
+
+ srcs: ["procrank.cpp"],
+ shared_libs: [
+ "libbase",
"libmeminfo",
],
}
diff --git a/libmeminfo/tools/procmem.cpp b/libmeminfo/tools/procmem.cpp
index 3571e41..56de12d 100644
--- a/libmeminfo/tools/procmem.cpp
+++ b/libmeminfo/tools/procmem.cpp
@@ -15,20 +15,33 @@
*/
#include <errno.h>
+#include <inttypes.h>
#include <stdlib.h>
#include <unistd.h>
-#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
+#include <android-base/stringprintf.h>
#include <meminfo/procmeminfo.h>
+using Vma = ::android::meminfo::Vma;
using ProcMemInfo = ::android::meminfo::ProcMemInfo;
using MemUsage = ::android::meminfo::MemUsage;
+// Global flags to control procmem output
+
+// Set to use page idle bits for working set detection
+bool use_pageidle = false;
+// hides map entries with zero rss
+bool hide_zeroes = false;
+// Reset working set and exit
+bool reset_wss = false;
+// Show working set, mutually exclusive with reset_wss;
+bool show_wss = false;
+
static void usage(const char* cmd) {
fprintf(stderr,
"Usage: %s [-i] [ -w | -W ] [ -p | -m ] [ -h ] pid\n"
@@ -42,68 +55,56 @@
cmd);
}
-static void show_footer(uint32_t nelem, const std::string& padding) {
- std::string elem(7, '-');
-
- for (uint32_t i = 0; i < nelem; ++i) {
- std::cout << std::setw(7) << elem << padding;
+static void print_separator(std::stringstream& ss) {
+ if (show_wss) {
+ ss << ::android::base::StringPrintf("%7s %7s %7s %7s %7s %7s %7s %s\n", "-------",
+ "-------", "-------", "-------", "-------", "-------",
+ "-------", "");
+ return;
}
- std::cout << std::endl;
+ ss << ::android::base::StringPrintf("%7s %7s %7s %7s %7s %7s %7s %7s %s\n", "-------",
+ "-------", "-------", "-------", "-------", "-------",
+ "-------", "-------", "");
}
-static void show_header(const std::vector<std::string>& header, const std::string& padding) {
- if (header.empty()) return;
-
- for (size_t i = 0; i < header.size() - 1; ++i) {
- std::cout << std::setw(7) << header[i] << padding;
+static void print_header(std::stringstream& ss) {
+ if (show_wss) {
+ ss << ::android::base::StringPrintf("%7s %7s %7s %7s %7s %7s %7s %s\n", "WRss",
+ "WPss", "WUss", "WShCl", "WShDi", "WPrCl", "WPrDi",
+ "Name");
+ } else {
+ ss << ::android::base::StringPrintf("%7s %7s %7s %7s %7s %7s %7s %7s %s\n", "Vss",
+ "Rss", "Pss", "Uss", "ShCl", "ShDi", "PrCl", "PrDi",
+ "Name");
}
- std::cout << header.back() << std::endl;
- show_footer(header.size() - 1, padding);
+ print_separator(ss);
}
-static void scan_usage(std::stringstream& ss, const MemUsage& usage, const std::string& padding,
- bool show_wss) {
- // clear string stream first.
- ss.str("");
- // TODO: use ::android::base::StringPrintf instead of <iomanip> here.
- if (!show_wss)
- ss << std::setw(6) << usage.vss/1024 << padding;
- ss << std::setw(6) << usage.rss/1024 << padding << std::setw(6)
- << usage.pss/1024 << padding << std::setw(6) << usage.uss/1024 << padding
- << std::setw(6) << usage.shared_clean/1024 << padding << std::setw(6)
- << usage.shared_dirty/1024 << padding << std::setw(6)
- << usage.private_clean/1024 << padding << std::setw(6)
- << usage.private_dirty/1024 << padding;
+static void print_stats(std::stringstream& ss, const MemUsage& stats) {
+ if (!show_wss) {
+ ss << ::android::base::StringPrintf("%6" PRIu64 "K ", stats.vss / 1024);
+ }
+
+ ss << ::android::base::StringPrintf("%6" PRIu64 "K %6" PRIu64 "K %6" PRIu64 "K %6" PRIu64
+ "K %6" PRIu64 "K %6" PRIu64 "K %6" PRIu64 "K ",
+ stats.rss / 1024, stats.pss / 1024, stats.uss / 1024,
+ stats.shared_clean / 1024, stats.shared_dirty / 1024,
+ stats.private_clean / 1024, stats.private_dirty / 1024);
}
-static int show(ProcMemInfo& proc, bool hide_zeroes, bool show_wss) {
- const std::vector<std::string> main_header = {"Vss", "Rss", "Pss", "Uss", "ShCl",
- "ShDi", "PrCl", "PrDi", "Name"};
- const std::vector<std::string> wss_header = {"WRss", "WPss", "WUss", "WShCl",
- "WShDi", "WPrCl", "WPrDi", "Name"};
- const std::vector<std::string>& header = show_wss ? wss_header : main_header;
-
- // Read process memory stats
- const MemUsage& stats = show_wss ? proc.Wss() : proc.Usage();
- const std::vector<::android::meminfo::Vma>& maps = proc.Maps();
-
- // following retains 'procmem' output so as to not break any scripts
- // that rely on it.
- std::string spaces = " ";
- show_header(header, spaces);
- const std::string padding = "K ";
+static int show(const MemUsage& proc_stats, const std::vector<Vma>& maps) {
std::stringstream ss;
+ print_header(ss);
for (auto& vma : maps) {
const MemUsage& vma_stats = show_wss ? vma.wss : vma.usage;
if (hide_zeroes && vma_stats.rss == 0) {
continue;
}
- scan_usage(ss, vma_stats, padding, show_wss);
+ print_stats(ss, vma_stats);
ss << vma.name << std::endl;
- std::cout << ss.str();
}
- show_footer(header.size() - 1, spaces);
- scan_usage(ss, stats, padding, show_wss);
+ print_separator(ss);
+ print_stats(ss, proc_stats);
ss << "TOTAL" << std::endl;
std::cout << ss.str();
@@ -111,28 +112,43 @@
}
int main(int argc, char* argv[]) {
- bool use_pageidle = false;
- bool hide_zeroes = false;
- bool wss_reset = false;
- bool show_wss = false;
int opt;
+ auto pss_sort = [](const Vma& a, const Vma& b) {
+ uint64_t pss_a = show_wss ? a.wss.pss : a.usage.pss;
+ uint64_t pss_b = show_wss ? b.wss.pss : b.usage.pss;
+ return pss_a > pss_b;
+ };
+ auto uss_sort = [](const Vma& a, const Vma& b) {
+ uint64_t uss_a = show_wss ? a.wss.uss : a.usage.uss;
+ uint64_t uss_b = show_wss ? b.wss.uss : b.usage.uss;
+ return uss_a > uss_b;
+ };
+
+ std::function<bool(const Vma& a, const Vma& b)> sort_func = nullptr;
while ((opt = getopt(argc, argv, "himpuWw")) != -1) {
switch (opt) {
case 'h':
hide_zeroes = true;
break;
case 'i':
+ // TODO: libmeminfo doesn't support the flag to chose
+ // between idle page tracking vs clear_refs. So for now,
+ // this flag is unused and the library defaults to using
+ // /proc/<pid>/clear_refs for finding the working set.
use_pageidle = true;
break;
case 'm':
+ // this is the default
break;
case 'p':
+ sort_func = pss_sort;
break;
case 'u':
+ sort_func = uss_sort;
break;
case 'W':
- wss_reset = true;
+ reset_wss = true;
break;
case 'w':
show_wss = true;
@@ -157,15 +173,20 @@
exit(EXIT_FAILURE);
}
- bool need_wss = wss_reset || show_wss;
- ProcMemInfo proc(pid, need_wss);
- if (wss_reset) {
- if (!proc.WssReset()) {
+ if (reset_wss) {
+ if (!ProcMemInfo::ResetWorkingSet(pid)) {
std::cerr << "Failed to reset working set of pid : " << pid << std::endl;
exit(EXIT_FAILURE);
}
return 0;
}
- return show(proc, hide_zeroes, show_wss);
+ ProcMemInfo proc(pid, show_wss);
+ const MemUsage& proc_stats = show_wss ? proc.Wss() : proc.Usage();
+ std::vector<Vma> maps(proc.Maps());
+ if (sort_func != nullptr) {
+ std::sort(maps.begin(), maps.end(), sort_func);
+ }
+
+ return show(proc_stats, maps);
}
diff --git a/libmeminfo/tools/procrank.cpp b/libmeminfo/tools/procrank.cpp
new file mode 100644
index 0000000..e39e7fa
--- /dev/null
+++ b/libmeminfo/tools/procrank.cpp
@@ -0,0 +1,530 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <dirent.h>
+#include <errno.h>
+#include <inttypes.h>
+#include <linux/kernel-page-flags.h>
+#include <linux/oom.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+#include <iostream>
+#include <memory>
+#include <sstream>
+#include <vector>
+
+#include <android-base/file.h>
+#include <android-base/parseint.h>
+#include <android-base/stringprintf.h>
+#include <android-base/strings.h>
+
+#include <meminfo/procmeminfo.h>
+#include <meminfo/sysmeminfo.h>
+
+using ::android::meminfo::MemUsage;
+using ::android::meminfo::ProcMemInfo;
+
+struct ProcessRecord {
+ public:
+ ProcessRecord(pid_t pid, bool get_wss = false, uint64_t pgflags = 0, uint64_t pgflags_mask = 0)
+ : pid_(-1),
+ procmem_(nullptr),
+ oomadj_(OOM_SCORE_ADJ_MAX + 1),
+ cmdline_(""),
+ proportional_swap_(0),
+ unique_swap_(0),
+ zswap_(0) {
+ std::unique_ptr<ProcMemInfo> procmem =
+ std::make_unique<ProcMemInfo>(pid, get_wss, pgflags, pgflags_mask);
+ if (procmem == nullptr) {
+ std::cerr << "Failed to create ProcMemInfo for: " << pid << std::endl;
+ return;
+ }
+
+ std::string fname = ::android::base::StringPrintf("/proc/%d/oom_score_adj", pid);
+ auto oomscore_fp =
+ std::unique_ptr<FILE, decltype(&fclose)>{fopen(fname.c_str(), "re"), fclose};
+ if (oomscore_fp == nullptr) {
+ std::cerr << "Failed to open oom_score_adj file: " << fname << std::endl;
+ return;
+ }
+
+ if (fscanf(oomscore_fp.get(), "%d\n", &oomadj_) != 1) {
+ std::cerr << "Failed to read oomadj from: " << fname << std::endl;
+ return;
+ }
+
+ fname = ::android::base::StringPrintf("/proc/%d/cmdline", pid);
+ if (!::android::base::ReadFileToString(fname, &cmdline_)) {
+ std::cerr << "Failed to read cmdline from: " << fname << std::endl;
+ cmdline_ = "<unknown>";
+ }
+ // We deliberately don't read the proc/<pid>cmdline file directly into 'cmdline_'
+ // because of some processes showing up cmdlines that end with "0x00 0x0A 0x00"
+ // e.g. xtra-daemon, lowi-server
+ // The .c_str() assignment below then takes care of trimming the cmdline at the first
+ // 0x00. This is how original procrank worked (luckily)
+ cmdline_.resize(strlen(cmdline_.c_str()));
+ procmem_ = std::move(procmem);
+ pid_ = pid;
+ }
+
+ bool valid() const { return pid_ != -1; }
+
+ void CalculateSwap(const uint16_t* swap_offset_array, float zram_compression_ratio) {
+ const std::vector<uint16_t>& swp_offs = procmem_->SwapOffsets();
+ for (auto& off : swp_offs) {
+ proportional_swap_ += getpagesize() / swap_offset_array[off];
+ unique_swap_ += swap_offset_array[off] == 1 ? getpagesize() : 0;
+ zswap_ = proportional_swap_ * zram_compression_ratio;
+ }
+ }
+
+ // Getters
+ pid_t pid() const { return pid_; }
+ const std::string& cmdline() const { return cmdline_; }
+ int32_t oomadj() const { return oomadj_; }
+ uint64_t proportional_swap() const { return proportional_swap_; }
+ uint64_t unique_swap() const { return unique_swap_; }
+ uint64_t zswap() const { return zswap_; }
+
+ // Wrappers to ProcMemInfo
+ const std::vector<uint16_t>& SwapOffsets() const { return procmem_->SwapOffsets(); }
+ const MemUsage& Usage() const { return procmem_->Usage(); }
+ const MemUsage& Wss() const { return procmem_->Wss(); }
+
+ private:
+ pid_t pid_;
+ std::unique_ptr<ProcMemInfo> procmem_;
+ int32_t oomadj_;
+ std::string cmdline_;
+ uint64_t proportional_swap_;
+ uint64_t unique_swap_;
+ uint64_t zswap_;
+};
+
+// Show working set instead of memory consumption
+bool show_wss = false;
+// Reset working set of each process
+bool reset_wss = false;
+// Show per-process oom_score_adj column
+bool show_oomadj = false;
+// True if the device has swap enabled
+bool has_swap = false;
+// True, if device has zram enabled
+bool has_zram = false;
+// If zram is enabled, the compression ratio is zram used / swap used.
+float zram_compression_ratio = 0.0;
+// Sort process in reverse, default is descending
+bool reverse_sort = false;
+
+// Calculated total memory usage across all processes in the system
+uint64_t total_pss = 0;
+uint64_t total_uss = 0;
+uint64_t total_swap = 0;
+uint64_t total_pswap = 0;
+uint64_t total_uswap = 0;
+uint64_t total_zswap = 0;
+
+static void usage(const char* myname) {
+ std::cerr << "Usage: " << myname << " [ -W ] [ -v | -r | -p | -u | -s | -h ]" << std::endl
+ << " -v Sort by VSS." << std::endl
+ << " -r Sort by RSS." << std::endl
+ << " -p Sort by PSS." << std::endl
+ << " -u Sort by USS." << std::endl
+ << " -s Sort by swap." << std::endl
+ << " (Default sort order is PSS.)" << std::endl
+ << " -R Reverse sort order (default is descending)." << std::endl
+ << " -c Only show cached (storage backed) pages" << std::endl
+ << " -C Only show non-cached (ram/swap backed) pages" << std::endl
+ << " -k Only show pages collapsed by KSM" << std::endl
+ << " -w Display statistics for working set only." << std::endl
+ << " -W Reset working set of all processes." << std::endl
+ << " -o Show and sort by oom score against lowmemorykiller thresholds."
+ << std::endl
+ << " -h Display this help screen." << std::endl;
+}
+
+static bool read_all_pids(std::vector<pid_t>* pids, std::function<bool(pid_t pid)> for_each_pid) {
+ pids->clear();
+ std::unique_ptr<DIR, int (*)(DIR*)> procdir(opendir("/proc"), closedir);
+ if (!procdir) return false;
+
+ struct dirent* dir;
+ pid_t pid;
+ while ((dir = readdir(procdir.get()))) {
+ if (!::android::base::ParseInt(dir->d_name, &pid)) continue;
+ if (!for_each_pid(pid)) return false;
+ pids->push_back(pid);
+ }
+
+ return true;
+}
+
+static bool count_swap_offsets(const ProcessRecord& proc, uint16_t* swap_offset_array,
+ uint32_t size) {
+ const std::vector<uint16_t>& swp_offs = proc.SwapOffsets();
+ for (auto& off : swp_offs) {
+ if (off >= size) {
+ std::cerr << "swap offset " << off << " is out of bounds for process: " << proc.pid()
+ << std::endl;
+ return false;
+ }
+
+ if (swap_offset_array[off] == USHRT_MAX) {
+ std::cerr << "swap offset " << off << " ref count overflow in process: " << proc.pid()
+ << std::endl;
+ return false;
+ }
+
+ swap_offset_array[off]++;
+ }
+
+ return true;
+}
+
+static void print_header(std::stringstream& ss) {
+ ss.str("");
+ ss << ::android::base::StringPrintf("%5s ", "PID");
+ if (show_oomadj) {
+ ss << ::android::base::StringPrintf("%5s ", "oom");
+ }
+
+ if (show_wss) {
+ ss << ::android::base::StringPrintf("%7s %7s %7s ", "WRss", "WPss", "WUss");
+ // now swap statistics here, working set pages by definition shouldn't end up in swap.
+ } else {
+ ss << ::android::base::StringPrintf("%8s %7s %7s %7s ", "Vss", "Rss", "Pss", "Uss");
+ if (has_swap) {
+ ss << ::android::base::StringPrintf("%7s %7s %7s ", "Swap", "PSwap", "USwap");
+ if (has_zram) {
+ ss << ::android::base::StringPrintf("%7s ", "ZSwap");
+ }
+ }
+ }
+
+ ss << "cmdline";
+}
+
+static void print_process_record(std::stringstream& ss, ProcessRecord& proc) {
+ ss << ::android::base::StringPrintf("%5d ", proc.pid());
+ if (show_oomadj) {
+ ss << ::android::base::StringPrintf("%5d ", proc.oomadj());
+ }
+
+ if (show_wss) {
+ ss << ::android::base::StringPrintf("%6" PRIu64 "K %6" PRIu64 "K %6" PRIu64 "K ",
+ proc.Wss().rss / 1024, proc.Wss().pss / 1024,
+ proc.Wss().uss / 1024);
+ } else {
+ ss << ::android::base::StringPrintf("%7" PRIu64 "K %6" PRIu64 "K %6" PRIu64 "K %6" PRIu64
+ "K ",
+ proc.Usage().vss / 1024, proc.Usage().rss / 1024,
+ proc.Usage().pss / 1024, proc.Usage().uss / 1024);
+ if (has_swap) {
+ ss << ::android::base::StringPrintf("%6" PRIu64 "K ", proc.Usage().swap / 1024);
+ ss << ::android::base::StringPrintf("%6" PRIu64 "K ", proc.proportional_swap() / 1024);
+ ss << ::android::base::StringPrintf("%6" PRIu64 "K ", proc.unique_swap() / 1024);
+ if (has_zram) {
+ ss << ::android::base::StringPrintf("%6" PRIu64 "K ", (proc.zswap() / 1024));
+ }
+ }
+ }
+}
+
+static void print_processes(std::stringstream& ss, std::vector<ProcessRecord>& procs,
+ uint16_t* swap_offset_array) {
+ for (auto& proc : procs) {
+ total_pss += show_wss ? proc.Wss().pss : proc.Usage().pss;
+ total_uss += show_wss ? proc.Wss().uss : proc.Usage().uss;
+ if (!show_wss && has_swap) {
+ proc.CalculateSwap(swap_offset_array, zram_compression_ratio);
+ total_swap += proc.Usage().swap;
+ total_pswap += proc.proportional_swap();
+ total_uswap += proc.unique_swap();
+ if (has_zram) {
+ total_zswap += proc.zswap();
+ }
+ }
+
+ print_process_record(ss, proc);
+ ss << proc.cmdline() << std::endl;
+ }
+}
+
+static void print_separator(std::stringstream& ss) {
+ ss << ::android::base::StringPrintf("%5s ", "");
+ if (show_oomadj) {
+ ss << ::android::base::StringPrintf("%5s ", "");
+ }
+
+ if (show_wss) {
+ ss << ::android::base::StringPrintf("%7s %7s %7s ", "", "------", "------");
+ } else {
+ ss << ::android::base::StringPrintf("%8s %7s %7s %7s ", "", "", "------", "------");
+ if (has_swap) {
+ ss << ::android::base::StringPrintf("%7s %7s %7s ", "------", "------", "------");
+ if (has_zram) {
+ ss << ::android::base::StringPrintf("%7s ", "------");
+ }
+ }
+ }
+
+ ss << ::android::base::StringPrintf("%s", "------");
+}
+
+static void print_totals(std::stringstream& ss) {
+ ss << ::android::base::StringPrintf("%5s ", "");
+ if (show_oomadj) {
+ ss << ::android::base::StringPrintf("%5s ", "");
+ }
+
+ if (show_wss) {
+ ss << ::android::base::StringPrintf("%7s %6" PRIu64 "K %6" PRIu64 "K ", "",
+ total_pss / 1024, total_uss / 1024);
+ } else {
+ ss << ::android::base::StringPrintf("%8s %7s %6" PRIu64 "K %6" PRIu64 "K ", "", "",
+ total_pss / 1024, total_uss / 1024);
+ if (has_swap) {
+ ss << ::android::base::StringPrintf("%6" PRIu64 "K ", total_swap / 1024);
+ ss << ::android::base::StringPrintf("%6" PRIu64 "K ", total_pswap / 1024);
+ ss << ::android::base::StringPrintf("%6" PRIu64 "K ", total_uswap / 1024);
+ if (has_zram) {
+ ss << ::android::base::StringPrintf("%6" PRIu64 "K ", total_zswap / 1024);
+ }
+ }
+ }
+ ss << "TOTAL";
+}
+
+static void print_sysmeminfo(std::stringstream& ss, ::android::meminfo::SysMemInfo& smi) {
+ if (has_swap) {
+ ss << ::android::base::StringPrintf("ZRAM: %" PRIu64 "K physical used for %" PRIu64
+ "K in swap "
+ "(%" PRIu64 "K total swap)",
+ smi.mem_zram_kb(),
+ (smi.mem_swap_kb() - smi.mem_swap_free_kb()),
+ smi.mem_swap_kb())
+ << std::endl;
+ }
+
+ ss << ::android::base::StringPrintf(" RAM: %" PRIu64 "K total, %" PRIu64 "K free, %" PRIu64
+ "K buffers, "
+ "%" PRIu64 "K cached, %" PRIu64 "K shmem, %" PRIu64
+ "K slab",
+ smi.mem_total_kb(), smi.mem_free_kb(), smi.mem_buffers_kb(),
+ smi.mem_cached_kb(), smi.mem_shmem_kb(), smi.mem_slab_kb());
+}
+
+int main(int argc, char* argv[]) {
+ auto pss_sort = [](ProcessRecord& a, ProcessRecord& b) {
+ MemUsage stats_a = show_wss ? a.Wss() : a.Usage();
+ MemUsage stats_b = show_wss ? b.Wss() : b.Usage();
+ return reverse_sort ? stats_a.pss < stats_b.pss : stats_a.pss > stats_b.pss;
+ };
+
+ auto uss_sort = [](ProcessRecord& a, ProcessRecord& b) {
+ MemUsage stats_a = show_wss ? a.Wss() : a.Usage();
+ MemUsage stats_b = show_wss ? b.Wss() : b.Usage();
+ return reverse_sort ? stats_a.uss < stats_b.uss : stats_a.uss > stats_b.uss;
+ };
+
+ auto rss_sort = [](ProcessRecord& a, ProcessRecord& b) {
+ MemUsage stats_a = show_wss ? a.Wss() : a.Usage();
+ MemUsage stats_b = show_wss ? b.Wss() : b.Usage();
+ return reverse_sort ? stats_a.rss < stats_b.pss : stats_a.pss > stats_b.pss;
+ };
+
+ auto vss_sort = [](ProcessRecord& a, ProcessRecord& b) {
+ MemUsage stats_a = show_wss ? a.Wss() : a.Usage();
+ MemUsage stats_b = show_wss ? b.Wss() : b.Usage();
+ return reverse_sort ? stats_a.vss < stats_b.vss : stats_a.vss > stats_b.vss;
+ };
+
+ auto swap_sort = [](ProcessRecord& a, ProcessRecord& b) {
+ MemUsage stats_a = show_wss ? a.Wss() : a.Usage();
+ MemUsage stats_b = show_wss ? b.Wss() : b.Usage();
+ return reverse_sort ? stats_a.swap < stats_b.swap : stats_a.swap > stats_b.swap;
+ };
+
+ auto oomadj_sort = [](ProcessRecord& a, ProcessRecord& b) {
+ return reverse_sort ? a.oomadj() < b.oomadj() : a.oomadj() > b.oomadj();
+ };
+
+ // default PSS sort
+ std::function<bool(ProcessRecord & a, ProcessRecord & b)> proc_sort = pss_sort;
+
+ // count all pages by default
+ uint64_t pgflags = 0;
+ uint64_t pgflags_mask = 0;
+
+ int opt;
+ while ((opt = getopt(argc, argv, "cChkoprRsuvwW")) != -1) {
+ switch (opt) {
+ case 'c':
+ pgflags = 0;
+ pgflags_mask = (1 << KPF_SWAPBACKED);
+ break;
+ case 'C':
+ pgflags = (1 << KPF_SWAPBACKED);
+ pgflags_mask = (1 << KPF_SWAPBACKED);
+ break;
+ case 'h':
+ usage(argv[0]);
+ return 0;
+ break;
+ case 'k':
+ pgflags = (1 << KPF_KSM);
+ pgflags_mask = (1 << KPF_KSM);
+ break;
+ case 'o':
+ proc_sort = oomadj_sort;
+ show_oomadj = true;
+ break;
+ case 'p':
+ proc_sort = pss_sort;
+ break;
+ case 'r':
+ proc_sort = rss_sort;
+ break;
+ case 'R':
+ reverse_sort = true;
+ break;
+ case 's':
+ proc_sort = swap_sort;
+ break;
+ case 'u':
+ proc_sort = uss_sort;
+ break;
+ case 'v':
+ proc_sort = vss_sort;
+ break;
+ case 'w':
+ show_wss = true;
+ break;
+ case 'W':
+ reset_wss = true;
+ break;
+ default:
+ abort();
+ }
+ }
+
+ std::vector<pid_t> pids;
+ std::vector<ProcessRecord> procs;
+ if (reset_wss) {
+ if (!read_all_pids(&pids,
+ [&](pid_t pid) -> bool { return ProcMemInfo::ResetWorkingSet(pid); })) {
+ std::cerr << "Failed to reset working set of all processes" << std::endl;
+ exit(EXIT_FAILURE);
+ }
+ // we are done, all other options passed to procrank are ignored in the presence of '-W'
+ return 0;
+ }
+
+ ::android::meminfo::SysMemInfo smi;
+ if (!smi.ReadMemInfo()) {
+ std::cerr << "Failed to get system memory info" << std::endl;
+ exit(EXIT_FAILURE);
+ }
+
+ // Figure out swap and zram
+ uint64_t swap_total = smi.mem_swap_kb() * 1024;
+ has_swap = swap_total > 0;
+ // Allocate the swap array
+ auto swap_offset_array = std::make_unique<uint16_t[]>(swap_total / getpagesize());
+ if (has_swap) {
+ has_zram = smi.mem_zram_kb() > 0;
+ if (has_zram) {
+ zram_compression_ratio = static_cast<float>(smi.mem_zram_kb()) /
+ (smi.mem_swap_kb() - smi.mem_swap_free_kb());
+ }
+ }
+
+ auto mark_swap_usage = [&](pid_t pid) -> bool {
+ ProcessRecord proc(pid, show_wss, pgflags, pgflags_mask);
+ if (!proc.valid()) {
+ std::cerr << "Failed to create process record for: " << pid << std::endl;
+ return false;
+ }
+
+ // Skip processes with no memory mappings
+ uint64_t vss = show_wss ? proc.Wss().vss : proc.Usage().vss;
+ if (vss == 0) return true;
+
+ // collect swap_offset counts from all processes in 1st pass
+ if (!show_wss && has_swap &&
+ !count_swap_offsets(proc, swap_offset_array.get(), swap_total / getpagesize())) {
+ std::cerr << "Failed to count swap offsets for process: " << pid << std::endl;
+ return false;
+ }
+
+ procs.push_back(std::move(proc));
+ return true;
+ };
+
+ // Get a list of all pids currently running in the system in
+ // 1st pass through all processes. Mark each swap offset used by the process as we find them
+ // for calculating proportional swap usage later.
+ if (!read_all_pids(&pids, mark_swap_usage)) {
+ std::cerr << "Failed to read all pids from the system" << std::endl;
+ exit(EXIT_FAILURE);
+ }
+
+ std::stringstream ss;
+ if (procs.empty()) {
+ // This would happen in corner cases where procrank is being run to find KSM usage on a
+ // system with no KSM and combined with working set determination as follows
+ // procrank -w -u -k
+ // procrank -w -s -k
+ // procrank -w -o -k
+ ss << "<empty>" << std::endl << std::endl;
+ print_sysmeminfo(ss, smi);
+ ss << std::endl;
+ std::cout << ss.str();
+ return 0;
+ }
+
+ // Sort all process records, default is PSS descending
+ std::sort(procs.begin(), procs.end(), proc_sort);
+
+ // start dumping output in string stream
+ print_header(ss);
+ ss << std::endl;
+
+ // 2nd pass to calculate and get per process stats to add them up
+ print_processes(ss, procs, swap_offset_array.get());
+
+ // Add separator to output
+ print_separator(ss);
+ ss << std::endl;
+
+ // Add totals to output
+ print_totals(ss);
+ ss << std::endl << std::endl;
+
+ // Add system information at the end
+ print_sysmeminfo(ss, smi);
+ ss << std::endl;
+
+ // dump on the screen
+ std::cout << ss.str();
+
+ return 0;
+}
diff --git a/libprocinfo/include/procinfo/process_map.h b/libprocinfo/include/procinfo/process_map.h
index 0fc4201..981241e 100644
--- a/libprocinfo/include/procinfo/process_map.h
+++ b/libprocinfo/include/procinfo/process_map.h
@@ -17,6 +17,7 @@
#pragma once
#include <stdlib.h>
+#include <string.h>
#include <sys/mman.h>
#include <sys/types.h>
diff --git a/libstats/include/stats_event_list.h b/libstats/include/stats_event_list.h
index a9832db..41ca79b 100644
--- a/libstats/include/stats_event_list.h
+++ b/libstats/include/stats_event_list.h
@@ -26,7 +26,7 @@
int write_to_logger(android_log_context context, log_id_t id);
void note_log_drop();
void stats_log_close();
-
+int android_log_write_char_array(android_log_context ctx, const char* value, size_t len);
#ifdef __cplusplus
}
#endif
@@ -244,6 +244,14 @@
return ret >= 0;
}
+ bool AppendCharArray(const char* value, size_t len) {
+ int retval = android_log_write_char_array(ctx, value, len);
+ if (retval < 0) {
+ ret = retval;
+ }
+ return ret >= 0;
+ }
+
android_log_list_element read() { return android_log_read_next(ctx); }
android_log_list_element peek() { return android_log_peek_next(ctx); }
};
diff --git a/libstats/stats_event_list.c b/libstats/stats_event_list.c
index 72770d4..f4a7e94 100644
--- a/libstats/stats_event_list.c
+++ b/libstats/stats_event_list.c
@@ -193,3 +193,47 @@
errno = save_errno;
return ret;
}
+
+static inline void copy4LE(uint8_t* buf, uint32_t val) {
+ buf[0] = val & 0xFF;
+ buf[1] = (val >> 8) & 0xFF;
+ buf[2] = (val >> 16) & 0xFF;
+ buf[3] = (val >> 24) & 0xFF;
+}
+
+// Note: this function differs from android_log_write_string8_len in that the length passed in
+// should be treated as actual length and not max length.
+int android_log_write_char_array(android_log_context ctx, const char* value, size_t actual_len) {
+ size_t needed;
+ ssize_t len = actual_len;
+ android_log_context_internal* context;
+
+ context = (android_log_context_internal*)ctx;
+ if (!context || (kAndroidLoggerWrite != context->read_write_flag)) {
+ return -EBADF;
+ }
+ if (context->overflow) {
+ return -EIO;
+ }
+ if (!value) {
+ value = "";
+ len = 0;
+ }
+ needed = sizeof(uint8_t) + sizeof(int32_t) + len;
+ if ((context->pos + needed) > MAX_EVENT_PAYLOAD) {
+ /* Truncate string for delivery */
+ len = MAX_EVENT_PAYLOAD - context->pos - 1 - sizeof(int32_t);
+ if (len <= 0) {
+ context->overflow = true;
+ return -EIO;
+ }
+ }
+ context->count[context->list_nest_depth]++;
+ context->storage[context->pos + 0] = EVENT_TYPE_STRING;
+ copy4LE(&context->storage[context->pos + 1], len);
+ if (len) {
+ memcpy(&context->storage[context->pos + 5], value, len);
+ }
+ context->pos += needed;
+ return len;
+}
diff --git a/libunwindstack/Global.cpp b/libunwindstack/Global.cpp
index 7a3de01..fdfd705 100644
--- a/libunwindstack/Global.cpp
+++ b/libunwindstack/Global.cpp
@@ -15,6 +15,7 @@
*/
#include <stdint.h>
+#include <string.h>
#include <sys/mman.h>
#include <string>
diff --git a/libunwindstack/Maps.cpp b/libunwindstack/Maps.cpp
index 8729871..a9fb859 100644
--- a/libunwindstack/Maps.cpp
+++ b/libunwindstack/Maps.cpp
@@ -19,6 +19,7 @@
#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
+#include <string.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <unistd.h>
diff --git a/libunwindstack/Memory.cpp b/libunwindstack/Memory.cpp
index a30d65e..9904fef 100644
--- a/libunwindstack/Memory.cpp
+++ b/libunwindstack/Memory.cpp
@@ -16,6 +16,7 @@
#include <errno.h>
#include <fcntl.h>
+#include <string.h>
#include <sys/mman.h>
#include <sys/ptrace.h>
#include <sys/stat.h>
diff --git a/libunwindstack/RegsArm.cpp b/libunwindstack/RegsArm.cpp
index de22bde..885dc94 100644
--- a/libunwindstack/RegsArm.cpp
+++ b/libunwindstack/RegsArm.cpp
@@ -15,6 +15,7 @@
*/
#include <stdint.h>
+#include <string.h>
#include <functional>
diff --git a/libunwindstack/RegsArm64.cpp b/libunwindstack/RegsArm64.cpp
index a68f6e0..e9787aa 100644
--- a/libunwindstack/RegsArm64.cpp
+++ b/libunwindstack/RegsArm64.cpp
@@ -15,6 +15,7 @@
*/
#include <stdint.h>
+#include <string.h>
#include <functional>
diff --git a/libunwindstack/RegsMips.cpp b/libunwindstack/RegsMips.cpp
index 2e6908c..14a4e31 100644
--- a/libunwindstack/RegsMips.cpp
+++ b/libunwindstack/RegsMips.cpp
@@ -15,6 +15,7 @@
*/
#include <stdint.h>
+#include <string.h>
#include <functional>
diff --git a/libunwindstack/RegsMips64.cpp b/libunwindstack/RegsMips64.cpp
index 0b835a1..3f67d92 100644
--- a/libunwindstack/RegsMips64.cpp
+++ b/libunwindstack/RegsMips64.cpp
@@ -15,6 +15,7 @@
*/
#include <stdint.h>
+#include <string.h>
#include <functional>
diff --git a/libunwindstack/RegsX86_64.cpp b/libunwindstack/RegsX86_64.cpp
index ebad3f4..74cd1cb 100644
--- a/libunwindstack/RegsX86_64.cpp
+++ b/libunwindstack/RegsX86_64.cpp
@@ -15,6 +15,7 @@
*/
#include <stdint.h>
+#include <string.h>
#include <functional>
diff --git a/libunwindstack/Symbols.cpp b/libunwindstack/Symbols.cpp
index 14ebdbb..e3c15a2 100644
--- a/libunwindstack/Symbols.cpp
+++ b/libunwindstack/Symbols.cpp
@@ -17,6 +17,7 @@
#include <elf.h>
#include <stdint.h>
+#include <algorithm>
#include <string>
#include <unwindstack/Memory.h>
diff --git a/rootdir/etc/ld.config.txt b/rootdir/etc/ld.config.txt
index d3e80c9..041f6d8 100644
--- a/rootdir/etc/ld.config.txt
+++ b/rootdir/etc/ld.config.txt
@@ -38,7 +38,8 @@
###############################################################################
namespace.default.isolated = true
-namespace.default.search.paths = /system/${LIB}
+namespace.default.search.paths = /apex/com.android.runtime/${LIB}
+namespace.default.search.paths += /system/${LIB}
namespace.default.search.paths += /%PRODUCT%/${LIB}
namespace.default.search.paths += /%PRODUCT_SERVICES%/${LIB}
@@ -75,6 +76,7 @@
namespace.default.permitted.paths += /mnt/expand
namespace.default.asan.search.paths = /data/asan/system/${LIB}
+namespace.default.asan.search.paths += /apex/com.android.runtime/${LIB}
namespace.default.asan.search.paths += /system/${LIB}
namespace.default.asan.search.paths += /data/asan/product/${LIB}
namespace.default.asan.search.paths += /%PRODUCT%/${LIB}
@@ -337,11 +339,13 @@
###############################################################################
namespace.system.isolated = false
-namespace.system.search.paths = /system/${LIB}
+namespace.system.search.paths = /apex/com.android.runtime/${LIB}
+namespace.system.search.paths += /system/${LIB}
namespace.system.search.paths += /%PRODUCT%/${LIB}
namespace.system.search.paths += /%PRODUCT_SERVICES%/${LIB}
namespace.system.asan.search.paths = /data/asan/system/${LIB}
+namespace.system.asan.search.paths += /apex/com.android.runtime/${LIB}
namespace.system.asan.search.paths += /system/${LIB}
namespace.system.asan.search.paths += /data/asan/product/${LIB}
namespace.system.asan.search.paths += /%PRODUCT%/${LIB}
@@ -358,6 +362,7 @@
###############################################################################
[postinstall]
namespace.default.isolated = false
-namespace.default.search.paths = /system/${LIB}
+namespace.default.search.paths = /apex/com.android.runtime/${LIB}
+namespace.default.search.paths += /system/${LIB}
namespace.default.search.paths += /%PRODUCT%/${LIB}
namespace.default.search.paths += /%PRODUCT_SERVICES%/${LIB}
diff --git a/rootdir/etc/ld.config.vndk_lite.txt b/rootdir/etc/ld.config.vndk_lite.txt
index 7e354ac..6a53b03 100644
--- a/rootdir/etc/ld.config.vndk_lite.txt
+++ b/rootdir/etc/ld.config.vndk_lite.txt
@@ -38,13 +38,15 @@
###############################################################################
namespace.default.isolated = false
-namespace.default.search.paths = /system/${LIB}
+namespace.default.search.paths = /apex/com.android.runtime/${LIB}
+namespace.default.search.paths += /system/${LIB}
namespace.default.search.paths += /odm/${LIB}
namespace.default.search.paths += /vendor/${LIB}
namespace.default.search.paths += /%PRODUCT%/${LIB}
namespace.default.search.paths += /%PRODUCT_SERVICES%/${LIB}
namespace.default.asan.search.paths = /data/asan/system/${LIB}
+namespace.default.asan.search.paths += /apex/com.android.runtime/${LIB}
namespace.default.asan.search.paths += /system/${LIB}
namespace.default.asan.search.paths += /data/asan/odm/${LIB}
namespace.default.asan.search.paths += /odm/${LIB}
@@ -211,6 +213,7 @@
# Access to system libraries are allowed
namespace.default.search.paths += /system/${LIB}/vndk%VNDK_VER%
namespace.default.search.paths += /system/${LIB}/vndk-sp%VNDK_VER%
+namespace.default.search.paths += /apex/com.android.runtime/${LIB}
namespace.default.search.paths += /system/${LIB}
namespace.default.search.paths += /%PRODUCT%/${LIB}
namespace.default.search.paths += /%PRODUCT_SERVICES%/${LIB}
@@ -232,6 +235,7 @@
namespace.default.asan.search.paths += /data/asan/system/${LIB}/vndk-sp%VNDK_VER%
namespace.default.asan.search.paths += /system/${LIB}/vndk-sp%VNDK_VER%
namespace.default.asan.search.paths += /data/asan/system/${LIB}
+namespace.default.asan.search.paths += /apex/com.android.runtime/${LIB}
namespace.default.asan.search.paths += /system/${LIB}
namespace.default.asan.search.paths += /data/asan/product/${LIB}
namespace.default.asan.search.paths += /%PRODUCT%/${LIB}
@@ -248,6 +252,7 @@
###############################################################################
[postinstall]
namespace.default.isolated = false
-namespace.default.search.paths = /system/${LIB}
+namespace.default.search.paths = /apex/com.android.runtime/${LIB}
+namespace.default.search.paths += /system/${LIB}
namespace.default.search.paths += /%PRODUCT%/${LIB}
namespace.default.search.paths += /%PRODUCT_SERVICES%/${LIB}
diff --git a/rootdir/ueventd.rc b/rootdir/ueventd.rc
index d47506c..d90a1ce 100644
--- a/rootdir/ueventd.rc
+++ b/rootdir/ueventd.rc
@@ -1,7 +1,5 @@
firmware_directories /etc/firmware/ /odm/firmware/ /vendor/firmware/ /firmware/image/
-
-subsystem adf
- devname uevent_devname
+uevent_socket_rcvbuf_size 16M
subsystem graphics
devname uevent_devpath
@@ -11,26 +9,10 @@
devname uevent_devpath
dirname /dev/dri
-subsystem oncrpc
- devname uevent_devpath
- dirname /dev/oncrpc
-
-subsystem adsp
- devname uevent_devpath
- dirname /dev/adsp
-
-subsystem msm_camera
- devname uevent_devpath
- dirname /dev/msm_camera
-
subsystem input
devname uevent_devpath
dirname /dev/input
-subsystem mtd
- devname uevent_devpath
- dirname /dev/mtd
-
subsystem sound
devname uevent_devpath
dirname /dev/snd
@@ -58,73 +40,25 @@
/dev/pmsg0 0222 root log
-# the msm hw3d client device node is world writable/readable.
-/dev/msm_hw3dc 0666 root root
-
-# gpu driver for adreno200 is globally accessible
-/dev/kgsl 0666 root root
-
# kms driver for drm based gpu
/dev/dri/* 0666 root graphics
# these should not be world writable
/dev/diag 0660 radio radio
-/dev/diag_arm9 0660 radio radio
/dev/ttyMSM0 0600 bluetooth bluetooth
/dev/uhid 0660 uhid uhid
/dev/uinput 0660 uhid uhid
-/dev/alarm 0664 system radio
/dev/rtc0 0640 system system
/dev/tty0 0660 root system
/dev/graphics/* 0660 root graphics
-/dev/msm_hw3dm 0660 system graphics
/dev/input/* 0660 root input
/dev/v4l-touch* 0660 root input
-/dev/eac 0660 root audio
-/dev/cam 0660 root camera
-/dev/pmem 0660 system graphics
-/dev/pmem_adsp* 0660 system audio
-/dev/pmem_camera* 0660 system camera
-/dev/oncrpc/* 0660 root system
-/dev/adsp/* 0660 system audio
/dev/snd/* 0660 system audio
-/dev/mt9t013 0660 system system
-/dev/msm_camera/* 0660 system system
-/dev/akm8976_daemon 0640 compass system
-/dev/akm8976_aot 0640 compass system
-/dev/akm8973_daemon 0640 compass system
-/dev/akm8973_aot 0640 compass system
-/dev/bma150 0640 compass system
-/dev/cm3602 0640 compass system
-/dev/akm8976_pffd 0640 compass system
-/dev/lightsensor 0640 system system
-/dev/msm_pcm_out* 0660 system audio
-/dev/msm_pcm_in* 0660 system audio
-/dev/msm_pcm_ctl* 0660 system audio
-/dev/msm_snd* 0660 system audio
/dev/msm_mp3* 0660 system audio
-/dev/audience_a1026* 0660 system audio
-/dev/tpa2018d1* 0660 system audio
-/dev/msm_audpre 0660 system audio
-/dev/msm_audio_ctl 0660 system audio
-/dev/htc-acoustic 0660 system audio
-/dev/vdec 0660 system audio
-/dev/q6venc 0660 system audio
-/dev/snd/dsp 0660 system audio
-/dev/snd/dsp1 0660 system audio
-/dev/snd/mixer 0660 system audio
-/dev/smd0 0640 radio radio
-/dev/qmi 0640 radio radio
-/dev/qmi0 0640 radio radio
-/dev/qmi1 0640 radio radio
-/dev/qmi2 0640 radio radio
-/dev/bus/usb/* 0660 root usb
-/dev/mtp_usb 0660 root mtp
/dev/usb_accessory 0660 root usb
/dev/tun 0660 system vpn
# CDMA radio interface MUX
-/dev/ts0710mux* 0640 radio radio
/dev/ppp 0660 radio vpn
# sysfs properties
@@ -134,6 +68,3 @@
/sys/devices/virtual/usb_composite/* enable 0664 root system
/sys/devices/system/cpu/cpu* cpufreq/scaling_max_freq 0664 system system
/sys/devices/system/cpu/cpu* cpufreq/scaling_min_freq 0664 system system
-
-# DVB API device nodes
-/dev/dvb* 0660 root system