diff options
author | Sandeep Patil <sspatil@google.com> | 2019-01-21 13:46:17 -0800 |
---|---|---|
committer | Sandeep Patil <sspatil@google.com> | 2019-01-21 13:47:27 -0800 |
commit | 380995adc7087d05b2fff52878aa9bc69e32f1f7 (patch) | |
tree | aed7175a84a36c3bf8c9329a37edf2fbc7477388 /libmemtrack | |
parent | 763bac7826358c155d4dff6930d83bf3c690efa2 (diff) | |
download | system_core-380995adc7087d05b2fff52878aa9bc69e32f1f7.tar.gz system_core-380995adc7087d05b2fff52878aa9bc69e32f1f7.tar.bz2 system_core-380995adc7087d05b2fff52878aa9bc69e32f1f7.zip |
libmemtrack: Remove libpagemap dependency.
While at it convert the old memtrack_test.c to cpp.
Bug: 111694435
Test: memtrack_test
Change-Id: If297d70cf05b330c578dfabc934fc368d362cc70
Signed-off-by: Sandeep Patil <sspatil@google.com>
Diffstat (limited to 'libmemtrack')
l--------- | libmemtrack/.clang-format | 1 | ||||
-rw-r--r-- | libmemtrack/Android.bp | 5 | ||||
-rw-r--r-- | libmemtrack/memtrack_test.c | 139 | ||||
-rw-r--r-- | libmemtrack/memtrack_test.cpp | 97 |
4 files changed, 101 insertions, 141 deletions
diff --git a/libmemtrack/.clang-format b/libmemtrack/.clang-format new file mode 120000 index 000000000..1af4f51dd --- /dev/null +++ b/libmemtrack/.clang-format @@ -0,0 +1 @@ +../.clang-format-4
\ No newline at end of file diff --git a/libmemtrack/Android.bp b/libmemtrack/Android.bp index 095563384..4e4554a7d 100644 --- a/libmemtrack/Android.bp +++ b/libmemtrack/Android.bp @@ -28,10 +28,11 @@ cc_library_shared { cc_binary { name: "memtrack_test", - srcs: ["memtrack_test.c"], + srcs: ["memtrack_test.cpp"], + static_libs: ["libc++fs"], shared_libs: [ + "libbase", "libmemtrack", - "libpagemap", ], cflags: [ "-Wall", diff --git a/libmemtrack/memtrack_test.c b/libmemtrack/memtrack_test.c deleted file mode 100644 index 77c935e4a..000000000 --- a/libmemtrack/memtrack_test.c +++ /dev/null @@ -1,139 +0,0 @@ -/* - * Copyright (C) 2013 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 <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <sys/types.h> - -#include <memtrack/memtrack.h> - -#include <pagemap/pagemap.h> - -#define DIV_ROUND_UP(x,y) (((x) + (y) - 1) / (y)) - -static int getprocname(pid_t pid, char *buf, int len) { - char *filename; - FILE *f; - int rc = 0; - static const char* unknown_cmdline = "<unknown>"; - - if (len <= 0) { - return -1; - } - - if (asprintf(&filename, "/proc/%d/cmdline", pid) < 0) { - rc = 1; - goto exit; - } - - f = fopen(filename, "r"); - if (f == NULL) { - rc = 2; - goto releasefilename; - } - - if (fgets(buf, len, f) == NULL) { - rc = 3; - goto closefile; - } - -closefile: - (void) fclose(f); -releasefilename: - free(filename); -exit: - if (rc != 0) { - /* - * The process went away before we could read its process name. Try - * to give the user "<unknown>" here, but otherwise they get to look - * at a blank. - */ - if (strlcpy(buf, unknown_cmdline, (size_t)len) >= (size_t)len) { - rc = 4; - } - } - - return rc; -} - -int main(int argc, char *argv[]) -{ - int ret; - pm_kernel_t *ker; - size_t num_procs; - pid_t *pids; - struct memtrack_proc *p; - size_t i; - - (void)argc; - (void)argv; - - ret = pm_kernel_create(&ker); - if (ret) { - fprintf(stderr, "Error creating kernel interface -- " - "does this kernel have pagemap?\n"); - exit(EXIT_FAILURE); - } - - ret = pm_kernel_pids(ker, &pids, &num_procs); - if (ret) { - fprintf(stderr, "Error listing processes.\n"); - exit(EXIT_FAILURE); - } - - p = memtrack_proc_new(); - if (ret) { - fprintf(stderr, "failed to create memtrack process handle\n"); - exit(EXIT_FAILURE); - } - - for (i = 0; i < num_procs; i++) { - pid_t pid = pids[i]; - char cmdline[256]; - size_t v1; - size_t v2; - size_t v3; - size_t v4; - size_t v5; - size_t v6; - - getprocname(pid, cmdline, (int)sizeof(cmdline)); - - ret = memtrack_proc_get(p, pid); - if (ret) { - fprintf(stderr, "failed to get memory info for pid %d: %s (%d)\n", - pid, strerror(-ret), ret); - continue; - } - - v1 = DIV_ROUND_UP(memtrack_proc_graphics_total(p), 1024); - v2 = DIV_ROUND_UP(memtrack_proc_graphics_pss(p), 1024); - v3 = DIV_ROUND_UP(memtrack_proc_gl_total(p), 1024); - v4 = DIV_ROUND_UP(memtrack_proc_gl_pss(p), 1024); - v5 = DIV_ROUND_UP(memtrack_proc_other_total(p), 1024); - v6 = DIV_ROUND_UP(memtrack_proc_other_pss(p), 1024); - - if (v1 | v2 | v3 | v4 | v5 | v6) { - printf("%5d %6zu %6zu %6zu %6zu %6zu %6zu %s\n", pid, - v1, v2, v3, v4, v5, v6, cmdline); - } - } - - memtrack_proc_destroy(p); - - return 0; -} diff --git a/libmemtrack/memtrack_test.cpp b/libmemtrack/memtrack_test.cpp new file mode 100644 index 000000000..aeeaf2467 --- /dev/null +++ b/libmemtrack/memtrack_test.cpp @@ -0,0 +1,97 @@ +/* + * Copyright (C) 2013 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 <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/types.h> + +#include <filesystem> +#include <vector> + +#include <android-base/file.h> +#include <android-base/parseint.h> +#include <android-base/stringprintf.h> +#include <memtrack/memtrack.h> + +#define DIV_ROUND_UP(x, y) (((x) + (y)-1) / (y)) + +static void getprocname(pid_t pid, std::string* name) { + std::string fname = ::android::base::StringPrintf("/proc/%d/cmdline", pid); + if (!::android::base::ReadFileToString(fname, name)) { + fprintf(stderr, "Failed to read cmdline from: %s\n", fname.c_str()); + *name = "<unknown>"; + } +} + +int main(int /* argc */, char** /* argv */) { + int ret; + struct memtrack_proc* p; + std::vector<pid_t> pids; + + p = memtrack_proc_new(); + if (p == nullptr) { + fprintf(stderr, "failed to create memtrack process handle\n"); + exit(EXIT_FAILURE); + } + + for (auto& de : std::filesystem::directory_iterator("/proc")) { + if (!std::filesystem::is_directory(de.status())) { + continue; + } + + pid_t pid; + if (!::android::base::ParseInt(de.path().filename().string(), &pid)) { + continue; + } + pids.emplace_back(pid); + } + + for (auto& pid : pids) { + size_t v1; + size_t v2; + size_t v3; + size_t v4; + size_t v5; + size_t v6; + std::string cmdline; + + getprocname(pid, &cmdline); + + ret = memtrack_proc_get(p, pid); + if (ret) { + fprintf(stderr, "failed to get memory info for pid %d: %s (%d)\n", pid, strerror(-ret), + ret); + continue; + } + + v1 = DIV_ROUND_UP(memtrack_proc_graphics_total(p), 1024); + v2 = DIV_ROUND_UP(memtrack_proc_graphics_pss(p), 1024); + v3 = DIV_ROUND_UP(memtrack_proc_gl_total(p), 1024); + v4 = DIV_ROUND_UP(memtrack_proc_gl_pss(p), 1024); + v5 = DIV_ROUND_UP(memtrack_proc_other_total(p), 1024); + v6 = DIV_ROUND_UP(memtrack_proc_other_pss(p), 1024); + + if (v1 | v2 | v3 | v4 | v5 | v6) { + fprintf(stdout, "%5d %6zu %6zu %6zu %6zu %6zu %6zu %s\n", pid, v1, v2, v3, v4, v5, v6, + cmdline.c_str()); + } + } + + memtrack_proc_destroy(p); + + return ret; +} |