From d132063251c4306a3d93c367d048c0263b38a740 Mon Sep 17 00:00:00 2001 From: Daniel Colascione Date: Mon, 18 Dec 2017 16:54:41 -0800 Subject: Add pssbench tool for benchmarking memory statisics collection. Test: builds Change-Id: Iced8c4d23d75adb3c3c69ae50252f7c376de56d4 --- pssbench/Android.mk | 16 ++++++++++ pssbench/main.cpp | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 pssbench/Android.mk create mode 100644 pssbench/main.cpp (limited to 'pssbench') 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 +#include +#include +#include +#include +#include +#include + +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; +} -- cgit v1.2.3