summaryrefslogtreecommitdiff
path: root/pssbench
diff options
context:
space:
mode:
authorDaniel Colascione <dancol@google.com>2017-12-18 16:54:41 -0800
committerDaniel Colascione <dancol@google.com>2017-12-18 16:54:41 -0800
commitd132063251c4306a3d93c367d048c0263b38a740 (patch)
tree4cfcdd7dc00a83985971ae77c774f96a35b937ca /pssbench
parenta04d8f9b0956392d6c89ac3c9e9d8b0d4c46f28d (diff)
downloadextras-d132063251c4306a3d93c367d048c0263b38a740.tar.gz
Add pssbench tool for benchmarking memory statisics collection.
Test: builds Change-Id: Iced8c4d23d75adb3c3c69ae50252f7c376de56d4
Diffstat (limited to 'pssbench')
-rw-r--r--pssbench/Android.mk16
-rw-r--r--pssbench/main.cpp88
2 files changed, 104 insertions, 0 deletions
diff --git a/pssbench/Android.mk b/pssbench/Android.mk
new file mode 100644
index 00000000..45bab557
--- /dev/null
+++ b/pssbench/Android.mk
@@ -0,0 +1,16 @@
+LOCAL_PATH:= $(call my-dir)
+include $(CLEAR_VARS)
+LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
+LOCAL_MODULE_PATH := $(TARGET_OUT_DATA)/local/tmp
+LOCAL_MODULE:= pssbench
+LOCAL_MODULE_TAGS := optional
+LOCAL_MODULE_CLASS := EXECUTABLES
+
+LOCAL_SRC_FILES += \
+ main.cpp \
+
+LOCAL_SHARED_LIBRARIES := \
+ libutils \
+ liblog \
+
+include $(BUILD_EXECUTABLE)
diff --git a/pssbench/main.cpp b/pssbench/main.cpp
new file mode 100644
index 00000000..160bf6cb
--- /dev/null
+++ b/pssbench/main.cpp
@@ -0,0 +1,88 @@
+#include <stdio.h>
+#include <inttypes.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <getopt.h>
+#include <string.h>
+#include <errno.h>
+
+const char* smaps_file = "smaps";
+bool verbose = false;
+int iterations = 1;
+int bufsz = -1;
+
+int64_t
+get_pss(int pid)
+{
+ char filename[64];
+ snprintf(filename, sizeof(filename), "/proc/%" PRId32 "/%s", pid,
+ smaps_file);
+ if (verbose)
+ fprintf(stderr, "smaps:[%s]\n", filename);
+
+ FILE * file = fopen(filename, "r");
+ if (!file) {
+ return (int64_t) -1;
+ }
+
+ if (bufsz >= 0) {
+ if (setvbuf(file, NULL, _IOFBF, bufsz)) {
+ fprintf(stderr, "setvbuf failed: %s\n", strerror(errno));
+ exit(1);
+ }
+ }
+
+ // Tally up all of the Pss from the various maps
+ char line[256];
+ int64_t pss = 0;
+ while (fgets(line, sizeof(line), file)) {
+ int64_t v;
+ if (sscanf(line, "Pss: %" SCNd64 " kB", &v) == 1) {
+ if (verbose)
+ fprintf(stderr, "pss line: %llu\n", (unsigned long long) v);
+ pss += v;
+ }
+ }
+
+ fclose(file);
+
+ // Return the Pss value in bytes, not kilobytes
+ return pss * 1024;
+}
+
+int
+main(int argc, char** argv)
+{
+ int c;
+ while ((c = getopt(argc, argv, "n:rvb:")) != -1) {
+ switch (c) {
+ case 'r':
+ smaps_file = "smaps_rollup";
+ break;
+ case 'v':
+ verbose = true;
+ break;
+ case 'n':
+ iterations = atoi(optarg);
+ break;
+ case 'b':
+ bufsz = atoi(optarg);
+ break;
+ default:
+ return 1;
+ }
+ }
+
+ if (argv[optind] == NULL) {
+ fprintf(stderr, "pssbench: no PID given\n");
+ return 1;
+ }
+ int pid = atoi(argv[optind]);
+ int64_t pss = 0;
+ for (int i = 0; i < iterations; ++i)
+ pss = get_pss(pid);
+ fflush(NULL);
+
+ printf("iterations:%d pid:%d pss:%lld\n", iterations, pid, (long long)pss);
+ return 0;
+}