Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

auto import from //depot/cupcake/@135843

  • Loading branch information...
commit e16cb84e2324f05334d18dcf5956f20f44262b62 1 parent 19ddb4b
The Android Open Source Project authored
Showing with 10,857 additions and 0 deletions.
  1. +19 −0 Android.mk
  2. +26 −0 latencytop/Android.mk
  3. 0  latencytop/MODULE_LICENSE_APACHE2
  4. +190 −0 latencytop/NOTICE
  5. +442 −0 latencytop/latencytop.c
  6. +30 −0 libpagemap/Android.mk
  7. 0  libpagemap/MODULE_LICENSE_APACHE2
  8. +190 −0 libpagemap/NOTICE
  9. +171 −0 libpagemap/include/pagemap/pagemap.h
  10. +158 −0 libpagemap/pm_kernel.c
  11. +125 −0 libpagemap/pm_map.c
  12. +24 −0 libpagemap/pm_map.h
  13. +28 −0 libpagemap/pm_memusage.c
  14. +290 −0 libpagemap/pm_process.c
  15. +30 −0 librank/Android.mk
  16. 0  librank/MODULE_LICENSE_APACHE2
  17. +190 −0 librank/NOTICE
  18. +349 −0 librank/librank.c
  19. +30 −0 procmem/Android.mk
  20. 0  procmem/MODULE_LICENSE_APACHE2
  21. +190 −0 procmem/NOTICE
  22. +322 −0 procmem/procmem.c
  23. +30 −0 procrank/Android.mk
  24. 0  procrank/MODULE_LICENSE_APACHE2
  25. +190 −0 procrank/NOTICE
  26. +206 −0 procrank/procrank.c
  27. +13 −0 showmap/Android.mk
  28. 0  showmap/MODULE_LICENSE_APACHE2
  29. +190 −0 showmap/NOTICE
  30. +234 −0 showmap/showmap.c
  31. +15 −0 showslab/Android.mk
  32. 0  showslab/MODULE_LICENSE_APACHE2
  33. +190 −0 showslab/NOTICE
  34. +351 −0 showslab/showslab.c
  35. +8 −0 sound/Android.mk
  36. +377 −0 sound/playwav.c
  37. +15 −0 su/Android.mk
  38. 0  su/MODULE_LICENSE_APACHE2
  39. +190 −0 su/NOTICE
  40. +85 −0 su/su.c
  41. +3 −0  tests/Android.mk
  42. +140 −0 tests/bionic/libc/Android.mk
  43. 0  tests/bionic/libc/MODULE_LICENSE_BSD_AND_GPL
  44. +32 −0 tests/bionic/libc/README.TXT
  45. +19 −0 tests/bionic/libc/bionic/lib_relocs.c
  46. +97 −0 tests/bionic/libc/bionic/test_cond.c
  47. +58 −0 tests/bionic/libc/bionic/test_getgrouplist.c
  48. +109 −0 tests/bionic/libc/bionic/test_mutex.c
  49. +8 −0 tests/bionic/libc/bionic/test_netinet_icmp.c
  50. +83 −0 tests/bionic/libc/bionic/test_pthread_cond.c
  51. +30 −0 tests/bionic/libc/bionic/test_pthread_create.c
  52. +25 −0 tests/bionic/libc/bionic/test_relocs.c
  53. +8 −0 tests/bionic/libc/common/hello_world.cpp
  54. +53 −0 tests/bionic/libc/common/test_gethostbyname.c
  55. +20 −0 tests/bionic/libc/common/test_gethostname.c
  56. +221 −0 tests/bionic/libc/common/test_libgen.c
  57. +134 −0 tests/bionic/libc/common/test_pthread_cleanup_push.c
  58. +86 −0 tests/bionic/libc/common/test_pthread_getcpuclockid.c
  59. +76 −0 tests/bionic/libc/common/test_pthread_join.c
  60. +82 −0 tests/bionic/libc/common/test_pthread_once.c
  61. +129 −0 tests/bionic/libc/common/test_semaphore.c
  62. +57 −0 tests/bionic/libc/common/test_seteuid.c
  63. +64 −0 tests/bionic/libc/common/test_static_cpp_mutex.cpp
  64. +55 −0 tests/bionic/libc/common/test_tm_zone.c
  65. +121 −0 tests/bionic/libc/common/test_udp.c
  66. +88 −0 tests/bionic/libc/glibc/assert/test-assert.c
  67. +32 −0 tests/bionic/libc/other/bench_locks.c
  68. +117 −0 tests/bionic/libc/other/test_aligned.c
  69. +40 −0 tests/bionic/libc/other/test_arc4random.c
  70. +17 −0 tests/bionic/libc/other/test_atomics.c
  71. +362 −0 tests/bionic/libc/other/test_jpeg.c
  72. +137 −0 tests/bionic/libc/other/test_sysconf.c
  73. +28 −0 tests/bionic/libc/other/test_system.c
  74. +63 −0 tests/bionic/libc/other/test_thread_max.c
  75. +88 −0 tests/bionic/libc/other/test_timer_create.c
  76. +100 −0 tests/bionic/libc/other/test_timer_create2.c
  77. +107 −0 tests/bionic/libc/other/test_timer_create3.c
  78. +55 −0 tests/bionic/libc/other/test_vfprintf_leak.c
  79. +267 −0 tests/bionic/libc/other/test_zlib.c
  80. +32 −0 tests/cpueater/Android.mk
  81. +83 −0 tests/cpueater/cpueater.c
  82. +130 −0 tests/cpueater/daemonize.c
  83. +33 −0 tests/framebuffer/Android.mk
  84. +232 −0 tests/framebuffer/fb_test.c
  85. +56 −0 tests/framebuffer/minui.h
  86. +169 −0 tests/framebuffer/refresh.c
  87. +43 −0 tests/fstest/Android.mk
  88. +68 −0 tests/fstest/README
  89. +399 −0 tests/fstest/perm_checker.c
  90. +165 −0 tests/fstest/perm_checker.conf
  91. +14 −0 tests/icachetest/Android.mk
  92. +180 −0 tests/icachetest/icache.S
  93. +170 −0 tests/icachetest/icache2.S
  94. +34 −0 tests/icachetest/icache_main.c
  95. +19 −0 tests/memtest/Android.mk
  96. +763 −0 tests/memtest/memtest.cpp
  97. +27 −0 tests/memtest/thumb.cpp
  98. +10 −0 tests/schedtest/Android.mk
  99. +48 −0 tests/schedtest/schedtest.c
  100. +24 −0 timeinfo/Android.mk
  101. +49 −0 timeinfo/timeinfo.cpp
View
19 Android.mk
@@ -0,0 +1,19 @@
+#
+# Copyright (C) 2008 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.
+#
+
+ifneq ($(TARGET_SIMULATOR),true)
+ include $(all-subdir-makefiles)
+endif
View
26 latencytop/Android.mk
@@ -0,0 +1,26 @@
+# Copyright (C) 2008 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.
+
+LOCAL_PATH:= $(call my-dir)
+include $(CLEAR_VARS)
+
+LOCAL_SRC_FILES := latencytop.c
+
+LOCAL_MODULE := latencytop
+
+LOCAL_MODULE_PATH := $(TARGET_OUT_OPTIONAL_EXECUTABLES)
+
+LOCAL_MODULE_TAGS := debug tests
+
+include $(BUILD_EXECUTABLE)
View
0  latencytop/MODULE_LICENSE_APACHE2
No changes.
View
190 latencytop/NOTICE
@@ -0,0 +1,190 @@
+
+ Copyright (c) 2005-2008, 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.
+
+ 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.
+
+
+ Apache License
+ Version 2.0, January 2004
+ http://www.apache.org/licenses/
+
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+ 1. Definitions.
+
+ "License" shall mean the terms and conditions for use, reproduction,
+ and distribution as defined by Sections 1 through 9 of this document.
+
+ "Licensor" shall mean the copyright owner or entity authorized by
+ the copyright owner that is granting the License.
+
+ "Legal Entity" shall mean the union of the acting entity and all
+ other entities that control, are controlled by, or are under common
+ control with that entity. For the purposes of this definition,
+ "control" means (i) the power, direct or indirect, to cause the
+ direction or management of such entity, whether by contract or
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
+ outstanding shares, or (iii) beneficial ownership of such entity.
+
+ "You" (or "Your") shall mean an individual or Legal Entity
+ exercising permissions granted by this License.
+
+ "Source" form shall mean the preferred form for making modifications,
+ including but not limited to software source code, documentation
+ source, and configuration files.
+
+ "Object" form shall mean any form resulting from mechanical
+ transformation or translation of a Source form, including but
+ not limited to compiled object code, generated documentation,
+ and conversions to other media types.
+
+ "Work" shall mean the work of authorship, whether in Source or
+ Object form, made available under the License, as indicated by a
+ copyright notice that is included in or attached to the work
+ (an example is provided in the Appendix below).
+
+ "Derivative Works" shall mean any work, whether in Source or Object
+ form, that is based on (or derived from) the Work and for which the
+ editorial revisions, annotations, elaborations, or other modifications
+ represent, as a whole, an original work of authorship. For the purposes
+ of this License, Derivative Works shall not include works that remain
+ separable from, or merely link (or bind by name) to the interfaces of,
+ the Work and Derivative Works thereof.
+
+ "Contribution" shall mean any work of authorship, including
+ the original version of the Work and any modifications or additions
+ to that Work or Derivative Works thereof, that is intentionally
+ submitted to Licensor for inclusion in the Work by the copyright owner
+ or by an individual or Legal Entity authorized to submit on behalf of
+ the copyright owner. For the purposes of this definition, "submitted"
+ means any form of electronic, verbal, or written communication sent
+ to the Licensor or its representatives, including but not limited to
+ communication on electronic mailing lists, source code control systems,
+ and issue tracking systems that are managed by, or on behalf of, the
+ Licensor for the purpose of discussing and improving the Work, but
+ excluding communication that is conspicuously marked or otherwise
+ designated in writing by the copyright owner as "Not a Contribution."
+
+ "Contributor" shall mean Licensor and any individual or Legal Entity
+ on behalf of whom a Contribution has been received by Licensor and
+ subsequently incorporated within the Work.
+
+ 2. Grant of Copyright License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ copyright license to reproduce, prepare Derivative Works of,
+ publicly display, publicly perform, sublicense, and distribute the
+ Work and such Derivative Works in Source or Object form.
+
+ 3. Grant of Patent License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ (except as stated in this section) patent license to make, have made,
+ use, offer to sell, sell, import, and otherwise transfer the Work,
+ where such license applies only to those patent claims licensable
+ by such Contributor that are necessarily infringed by their
+ Contribution(s) alone or by combination of their Contribution(s)
+ with the Work to which such Contribution(s) was submitted. If You
+ institute patent litigation against any entity (including a
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
+ or a Contribution incorporated within the Work constitutes direct
+ or contributory patent infringement, then any patent licenses
+ granted to You under this License for that Work shall terminate
+ as of the date such litigation is filed.
+
+ 4. Redistribution. You may reproduce and distribute copies of the
+ Work or Derivative Works thereof in any medium, with or without
+ modifications, and in Source or Object form, provided that You
+ meet the following conditions:
+
+ (a) You must give any other recipients of the Work or
+ Derivative Works a copy of this License; and
+
+ (b) You must cause any modified files to carry prominent notices
+ stating that You changed the files; and
+
+ (c) You must retain, in the Source form of any Derivative Works
+ that You distribute, all copyright, patent, trademark, and
+ attribution notices from the Source form of the Work,
+ excluding those notices that do not pertain to any part of
+ the Derivative Works; and
+
+ (d) If the Work includes a "NOTICE" text file as part of its
+ distribution, then any Derivative Works that You distribute must
+ include a readable copy of the attribution notices contained
+ within such NOTICE file, excluding those notices that do not
+ pertain to any part of the Derivative Works, in at least one
+ of the following places: within a NOTICE text file distributed
+ as part of the Derivative Works; within the Source form or
+ documentation, if provided along with the Derivative Works; or,
+ within a display generated by the Derivative Works, if and
+ wherever such third-party notices normally appear. The contents
+ of the NOTICE file are for informational purposes only and
+ do not modify the License. You may add Your own attribution
+ notices within Derivative Works that You distribute, alongside
+ or as an addendum to the NOTICE text from the Work, provided
+ that such additional attribution notices cannot be construed
+ as modifying the License.
+
+ You may add Your own copyright statement to Your modifications and
+ may provide additional or different license terms and conditions
+ for use, reproduction, or distribution of Your modifications, or
+ for any such Derivative Works as a whole, provided Your use,
+ reproduction, and distribution of the Work otherwise complies with
+ the conditions stated in this License.
+
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
+ any Contribution intentionally submitted for inclusion in the Work
+ by You to the Licensor shall be under the terms and conditions of
+ this License, without any additional terms or conditions.
+ Notwithstanding the above, nothing herein shall supersede or modify
+ the terms of any separate license agreement you may have executed
+ with Licensor regarding such Contributions.
+
+ 6. Trademarks. This License does not grant permission to use the trade
+ names, trademarks, service marks, or product names of the Licensor,
+ except as required for reasonable and customary use in describing the
+ origin of the Work and reproducing the content of the NOTICE file.
+
+ 7. Disclaimer of Warranty. Unless required by applicable law or
+ agreed to in writing, Licensor provides the Work (and each
+ Contributor provides its Contributions) on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ implied, including, without limitation, any warranties or conditions
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+ PARTICULAR PURPOSE. You are solely responsible for determining the
+ appropriateness of using or redistributing the Work and assume any
+ risks associated with Your exercise of permissions under this License.
+
+ 8. Limitation of Liability. In no event and under no legal theory,
+ whether in tort (including negligence), contract, or otherwise,
+ unless required by applicable law (such as deliberate and grossly
+ negligent acts) or agreed to in writing, shall any Contributor be
+ liable to You for damages, including any direct, indirect, special,
+ incidental, or consequential damages of any character arising as a
+ result of this License or out of the use or inability to use the
+ Work (including but not limited to damages for loss of goodwill,
+ work stoppage, computer failure or malfunction, or any and all
+ other commercial damages or losses), even if such Contributor
+ has been advised of the possibility of such damages.
+
+ 9. Accepting Warranty or Additional Liability. While redistributing
+ the Work or Derivative Works thereof, You may choose to offer,
+ and charge a fee for, acceptance of support, warranty, indemnity,
+ or other liability obligations and/or rights consistent with this
+ License. However, in accepting such obligations, You may act only
+ on Your own behalf and on Your sole responsibility, not on behalf
+ of any other Contributor, and only if You agree to indemnify,
+ defend, and hold each Contributor harmless for any liability
+ incurred by, or claims asserted against, such Contributor by reason
+ of your accepting any such warranty or additional liability.
+
+ END OF TERMS AND CONDITIONS
+
View
442 latencytop/latencytop.c
@@ -0,0 +1,442 @@
+/*
+ * Copyright (C) 2008 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 <ctype.h>
+#include <dirent.h>
+#include <errno.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#define MAX_LINE 512
+#define MAX_FILENAME 64
+
+const char *EXPECTED_VERSION = "Latency Top version : v0.1\n";
+const char *SYSCTL_FILE = "/proc/sys/kernel/latencytop";
+const char *GLOBAL_STATS_FILE = "/proc/latency_stats";
+const char *THREAD_STATS_FILE_FORMAT = "/proc/%d/task/%d/latency";
+
+struct latency_entry {
+ struct latency_entry *next;
+ unsigned long count;
+ unsigned long max;
+ unsigned long total;
+ char reason[MAX_LINE];
+};
+
+static inline void check_latencytop() { }
+
+static struct latency_entry *read_global_stats(struct latency_entry *list, int erase);
+static struct latency_entry *read_process_stats(struct latency_entry *list, int erase, int pid);
+static struct latency_entry *read_thread_stats(struct latency_entry *list, int erase, int pid, int tid, int fatal);
+
+static struct latency_entry *alloc_latency_entry(void);
+static void free_latency_entry(struct latency_entry *e);
+
+static void set_latencytop(int on);
+static struct latency_entry *read_latency_file(FILE *f, struct latency_entry *list);
+static void erase_latency_file(FILE *f);
+
+static struct latency_entry *find_latency_entry(struct latency_entry *e, char *reason);
+static void print_latency_entries(struct latency_entry *head);
+
+static void signal_handler(int sig);
+static void disable_latencytop(void);
+
+static int numcmp(const long long a, const long long b);
+static int lat_cmp(const void *a, const void *b);
+
+static void clear_screen(void);
+static void usage(const char *cmd);
+
+struct latency_entry *free_entries;
+
+int main(int argc, char *argv[]) {
+ struct latency_entry *e;
+ int delay, iterations;
+ int pid, tid;
+ int count, erase;
+ int i;
+
+ delay = 1;
+ iterations = 0;
+ pid = tid = 0;
+
+ for (i = 1; i < argc; i++) {
+ if (!strcmp(argv[i], "-d")) {
+ if (i >= argc - 1) {
+ fprintf(stderr, "Option -d expects an argument.\n");
+ exit(EXIT_FAILURE);
+ }
+ delay = atoi(argv[++i]);
+ continue;
+ }
+ if (!strcmp(argv[i], "-n")) {
+ if (i >= argc - 1) {
+ fprintf(stderr, "Option -n expects an argument.\n");
+ exit(EXIT_FAILURE);
+ }
+ iterations = atoi(argv[++i]);
+ continue;
+ }
+ if (!strcmp(argv[i], "-h")) {
+ usage(argv[0]);
+ exit(EXIT_SUCCESS);
+ }
+ if (!strcmp(argv[i], "-p")) {
+ if (i >= argc - 1) {
+ fprintf(stderr, "Option -p expects an argument.\n");
+ exit(EXIT_FAILURE);
+ }
+ pid = atoi(argv[++i]);
+ continue;
+ }
+ if (!strcmp(argv[i], "-t")) {
+ if (i >= argc - 1) {
+ fprintf(stderr, "Option -t expects an argument.\n");
+ exit(EXIT_FAILURE);
+ }
+ tid = atoi(argv[++i]);
+ continue;
+ }
+ fprintf(stderr, "Invalid argument \"%s\".\n", argv[i]);
+ usage(argv[0]);
+ exit(EXIT_FAILURE);
+ }
+
+ if (tid && !pid) {
+ fprintf(stderr, "If you provide a thread ID with -t, you must provide a process ID with -p.\n");
+ exit(EXIT_FAILURE);
+ }
+
+ check_latencytop();
+
+ free_entries = NULL;
+
+ signal(SIGINT, &signal_handler);
+ signal(SIGTERM, &signal_handler);
+
+ atexit(&disable_latencytop);
+
+ set_latencytop(1);
+
+ count = 0;
+ erase = 1;
+
+ while ((iterations == 0) || (count++ < iterations)) {
+
+ sleep(delay);
+
+ e = NULL;
+ if (pid) {
+ if (tid) {
+ e = read_thread_stats(e, erase, pid, tid, 1);
+ } else {
+ e = read_process_stats(e, erase, pid);
+ }
+ } else {
+ e = read_global_stats(e, erase);
+ }
+ erase = 0;
+
+ clear_screen();
+ if (pid) {
+ if (tid) {
+ printf("Latencies for thread %d in process %d:\n", tid, pid);
+ } else {
+ printf("Latencies for process %d:\n", pid);
+ }
+ } else {
+ printf("Latencies across all processes:\n");
+ }
+ print_latency_entries(e);
+ }
+
+ set_latencytop(0);
+
+ return 0;
+}
+
+static struct latency_entry *read_global_stats(struct latency_entry *list, int erase) {
+ FILE *f;
+ struct latency_entry *e;
+
+ if (erase) {
+ f = fopen(GLOBAL_STATS_FILE, "w");
+ if (!f) {
+ fprintf(stderr, "Could not open global latency stats file: %s\n", strerror(errno));
+ exit(EXIT_FAILURE);
+ }
+ fprintf(f, "erase\n");
+ fclose(f);
+ }
+
+ f = fopen(GLOBAL_STATS_FILE, "r");
+ if (!f) {
+ fprintf(stderr, "Could not open global latency stats file: %s\n", strerror(errno));
+ exit(EXIT_FAILURE);
+ }
+
+ e = read_latency_file(f, list);
+
+ fclose(f);
+
+ return e;
+}
+
+static struct latency_entry *read_process_stats(struct latency_entry *list, int erase, int pid) {
+ char dirname[MAX_FILENAME];
+ DIR *dir;
+ struct dirent *ent;
+ struct latency_entry *e;
+ int tid;
+
+ sprintf(dirname, "/proc/%d/task", pid);
+ dir = opendir(dirname);
+ if (!dir) {
+ fprintf(stderr, "Could not open task dir for process %d.\n", pid);
+ fprintf(stderr, "Perhaps the process has terminated?\n");
+ exit(EXIT_FAILURE);
+ }
+
+ e = list;
+ while ((ent = readdir(dir))) {
+ if (!isdigit(ent->d_name[0]))
+ continue;
+
+ tid = atoi(ent->d_name);
+
+ e = read_thread_stats(e, erase, pid, tid, 0);
+ }
+
+ closedir(dir);
+
+ return e;
+}
+
+static struct latency_entry *read_thread_stats(struct latency_entry *list, int erase, int pid, int tid, int fatal) {
+ char filename[MAX_FILENAME];
+ FILE *f;
+ struct latency_entry *e;
+
+ sprintf(filename, THREAD_STATS_FILE_FORMAT, pid, tid);
+
+ if (erase) {
+ f = fopen(filename, "w");
+ if (!f) {
+ if (fatal) {
+ fprintf(stderr, "Could not open %s: %s\n", filename, strerror(errno));
+ fprintf(stderr, "Perhaps the process or thread has terminated?\n");
+ exit(EXIT_FAILURE);
+ } else {
+ return list;
+ }
+ }
+ fprintf(f, "erase\n");
+ fclose(f);
+ }
+
+ f = fopen(GLOBAL_STATS_FILE, "r");
+ if (!f) {
+ if (fatal) {
+ fprintf(stderr, "Could not open %s: %s\n", filename, strerror(errno));
+ fprintf(stderr, "Perhaps the process or thread has terminated?\n");
+ exit(EXIT_FAILURE);
+ } else {
+ return list;
+ }
+ }
+
+ e = read_latency_file(f, list);
+
+ fclose(f);
+
+ return e;
+}
+
+static struct latency_entry *alloc_latency_entry(void) {
+ struct latency_entry *e;
+
+ if (free_entries) {
+ e = free_entries;
+ free_entries = free_entries->next;
+ } else {
+ e = calloc(1, sizeof(struct latency_entry));
+ if (!e) {
+ fprintf(stderr, "Could not allocate latency entry: %s\n", strerror(errno));
+ exit(EXIT_FAILURE);
+ }
+ }
+
+ return e;
+}
+
+static void free_latency_entry(struct latency_entry *e) {
+ e->next = free_entries;
+ free_entries = e;
+}
+
+static struct latency_entry *find_latency_entry(struct latency_entry *head, char *reason) {
+ struct latency_entry *e;
+
+ e = head;
+
+ while (e) {
+ if (!strcmp(e->reason, reason))
+ return e;
+ e = e->next;
+ }
+
+ return NULL;
+}
+
+static void set_latencytop(int on) {
+ FILE *f;
+
+ f = fopen(SYSCTL_FILE, "w");
+ if (!f) {
+ fprintf(stderr, "Could not open %s: %s\n", SYSCTL_FILE, strerror(errno));
+ exit(EXIT_FAILURE);
+ }
+
+ fprintf(f, "%d\n", on);
+
+ fclose(f);
+}
+
+static void erase_latency_file(FILE *f) {
+ fprintf(f, "erase\n");
+}
+
+static struct latency_entry *read_latency_file(FILE *f, struct latency_entry *list) {
+ struct latency_entry *e, *head;
+ char line[MAX_LINE];
+ unsigned long count, max, total;
+ char reason[MAX_LINE];
+
+ head = list;
+
+ if (!fgets(line, MAX_LINE, f)) {
+ fprintf(stderr, "Could not read latency file version: %s\n", strerror(errno));
+ exit(EXIT_FAILURE);
+ }
+
+ if (strcmp(line, EXPECTED_VERSION) != 0) {
+ fprintf(stderr, "Expected version: %s\n", EXPECTED_VERSION);
+ fprintf(stderr, "But got version: %s", line);
+ exit(EXIT_FAILURE);
+ }
+
+ while (fgets(line, MAX_LINE, f)) {
+ sscanf(line, "%ld %ld %ld %s", &count, &total, &max, reason);
+ if (max > 0 || total > 0) {
+ e = find_latency_entry(head, reason);
+ if (e) {
+ e->count += count;
+ if (max > e->max)
+ e->max = max;
+ e->total += total;
+ } else {
+ e = alloc_latency_entry();
+ e->count = count;
+ e->max = max;
+ e->total = total;
+ strcpy(e->reason, reason);
+ e->next = head;
+ head = e;
+ }
+ }
+ }
+
+ return head;
+}
+
+static void print_latency_entries(struct latency_entry *head) {
+ struct latency_entry *e, **array;
+ unsigned long average;
+ int i, count;
+
+ e = head;
+ count = 0;
+ while (e) {
+ count++;
+ e = e->next;
+ }
+
+ e = head;
+ array = calloc(count, sizeof(struct latency_entry *));
+ if (!array) {
+ fprintf(stderr, "Error allocating array: %s\n", strerror(errno));
+ exit(EXIT_FAILURE);
+ }
+ for (i = 0; i < count; i++) {
+ array[i] = e;
+ e = e->next;
+ }
+
+ qsort(array, count, sizeof(struct latency_entry *), &lat_cmp);
+
+ printf("%10s %10s %7s %s\n", "Maximum", "Average", "Count", "Reason");
+ for (i = 0; i < count; i++) {
+ e = array[i];
+ average = e->total / e->count;
+ printf("%4lu.%02lu ms %4lu.%02lu ms %7ld %s\n",
+ e->max / 1000, (e->max % 1000) / 10,
+ average / 1000, (average % 1000) / 10,
+ e->count,
+ e->reason);
+ }
+
+ free(array);
+}
+
+static void signal_handler(int sig) {
+ exit(EXIT_SUCCESS);
+}
+
+static void disable_latencytop(void) {
+ set_latencytop(0);
+}
+
+static void clear_screen(void) {
+ printf("\n\n");
+}
+
+static void usage(const char *cmd) {
+ fprintf(stderr, "Usage: %s [ -d delay ] [ -n iterations ] [ -p pid [ -t tid ] ] [ -h ]\n"
+ " -d delay Time to sleep between updates.\n"
+ " -n iterations Number of updates to show (0 = infinite).\n"
+ " -p pid Process to monitor (default is all).\n"
+ " -t tid Thread (within specified process) to monitor (default is all).\n"
+ " -h Display this help screen.\n",
+ cmd);
+}
+
+static int numcmp(const long long a, const long long b) {
+ if (a < b) return -1;
+ if (a > b) return 1;
+ return 0;
+}
+
+static int lat_cmp(const void *a, const void *b) {
+ const struct latency_entry *pa, *pb;
+
+ pa = (*((struct latency_entry **)a));
+ pb = (*((struct latency_entry **)b));
+
+ return numcmp(pb->max, pa->max);
+}
View
30 libpagemap/Android.mk
@@ -0,0 +1,30 @@
+# Copyright (C) 2008 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.
+
+LOCAL_PATH := $(call my-dir)
+include $(CLEAR_VARS)
+
+LOCAL_MODULE := libpagemap
+LOCAL_MODULE_TAGS := debug
+LOCAL_PRELINK_MODULE := false
+
+LOCAL_SRC_FILES := \
+ pm_kernel.c \
+ pm_process.c \
+ pm_map.c \
+ pm_memusage.c
+
+LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
+
+include $(BUILD_SHARED_LIBRARY)
View
0  libpagemap/MODULE_LICENSE_APACHE2
No changes.
View
190 libpagemap/NOTICE
@@ -0,0 +1,190 @@
+
+ Copyright (c) 2005-2008, 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.
+
+ 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.
+
+
+ Apache License
+ Version 2.0, January 2004
+ http://www.apache.org/licenses/
+
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+ 1. Definitions.
+
+ "License" shall mean the terms and conditions for use, reproduction,
+ and distribution as defined by Sections 1 through 9 of this document.
+
+ "Licensor" shall mean the copyright owner or entity authorized by
+ the copyright owner that is granting the License.
+
+ "Legal Entity" shall mean the union of the acting entity and all
+ other entities that control, are controlled by, or are under common
+ control with that entity. For the purposes of this definition,
+ "control" means (i) the power, direct or indirect, to cause the
+ direction or management of such entity, whether by contract or
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
+ outstanding shares, or (iii) beneficial ownership of such entity.
+
+ "You" (or "Your") shall mean an individual or Legal Entity
+ exercising permissions granted by this License.
+
+ "Source" form shall mean the preferred form for making modifications,
+ including but not limited to software source code, documentation
+ source, and configuration files.
+
+ "Object" form shall mean any form resulting from mechanical
+ transformation or translation of a Source form, including but
+ not limited to compiled object code, generated documentation,
+ and conversions to other media types.
+
+ "Work" shall mean the work of authorship, whether in Source or
+ Object form, made available under the License, as indicated by a
+ copyright notice that is included in or attached to the work
+ (an example is provided in the Appendix below).
+
+ "Derivative Works" shall mean any work, whether in Source or Object
+ form, that is based on (or derived from) the Work and for which the
+ editorial revisions, annotations, elaborations, or other modifications
+ represent, as a whole, an original work of authorship. For the purposes
+ of this License, Derivative Works shall not include works that remain
+ separable from, or merely link (or bind by name) to the interfaces of,
+ the Work and Derivative Works thereof.
+
+ "Contribution" shall mean any work of authorship, including
+ the original version of the Work and any modifications or additions
+ to that Work or Derivative Works thereof, that is intentionally
+ submitted to Licensor for inclusion in the Work by the copyright owner
+ or by an individual or Legal Entity authorized to submit on behalf of
+ the copyright owner. For the purposes of this definition, "submitted"
+ means any form of electronic, verbal, or written communication sent
+ to the Licensor or its representatives, including but not limited to
+ communication on electronic mailing lists, source code control systems,
+ and issue tracking systems that are managed by, or on behalf of, the
+ Licensor for the purpose of discussing and improving the Work, but
+ excluding communication that is conspicuously marked or otherwise
+ designated in writing by the copyright owner as "Not a Contribution."
+
+ "Contributor" shall mean Licensor and any individual or Legal Entity
+ on behalf of whom a Contribution has been received by Licensor and
+ subsequently incorporated within the Work.
+
+ 2. Grant of Copyright License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ copyright license to reproduce, prepare Derivative Works of,
+ publicly display, publicly perform, sublicense, and distribute the
+ Work and such Derivative Works in Source or Object form.
+
+ 3. Grant of Patent License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ (except as stated in this section) patent license to make, have made,
+ use, offer to sell, sell, import, and otherwise transfer the Work,
+ where such license applies only to those patent claims licensable
+ by such Contributor that are necessarily infringed by their
+ Contribution(s) alone or by combination of their Contribution(s)
+ with the Work to which such Contribution(s) was submitted. If You
+ institute patent litigation against any entity (including a
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
+ or a Contribution incorporated within the Work constitutes direct
+ or contributory patent infringement, then any patent licenses
+ granted to You under this License for that Work shall terminate
+ as of the date such litigation is filed.
+
+ 4. Redistribution. You may reproduce and distribute copies of the
+ Work or Derivative Works thereof in any medium, with or without
+ modifications, and in Source or Object form, provided that You
+ meet the following conditions:
+
+ (a) You must give any other recipients of the Work or
+ Derivative Works a copy of this License; and
+
+ (b) You must cause any modified files to carry prominent notices
+ stating that You changed the files; and
+
+ (c) You must retain, in the Source form of any Derivative Works
+ that You distribute, all copyright, patent, trademark, and
+ attribution notices from the Source form of the Work,
+ excluding those notices that do not pertain to any part of
+ the Derivative Works; and
+
+ (d) If the Work includes a "NOTICE" text file as part of its
+ distribution, then any Derivative Works that You distribute must
+ include a readable copy of the attribution notices contained
+ within such NOTICE file, excluding those notices that do not
+ pertain to any part of the Derivative Works, in at least one
+ of the following places: within a NOTICE text file distributed
+ as part of the Derivative Works; within the Source form or
+ documentation, if provided along with the Derivative Works; or,
+ within a display generated by the Derivative Works, if and
+ wherever such third-party notices normally appear. The contents
+ of the NOTICE file are for informational purposes only and
+ do not modify the License. You may add Your own attribution
+ notices within Derivative Works that You distribute, alongside
+ or as an addendum to the NOTICE text from the Work, provided
+ that such additional attribution notices cannot be construed
+ as modifying the License.
+
+ You may add Your own copyright statement to Your modifications and
+ may provide additional or different license terms and conditions
+ for use, reproduction, or distribution of Your modifications, or
+ for any such Derivative Works as a whole, provided Your use,
+ reproduction, and distribution of the Work otherwise complies with
+ the conditions stated in this License.
+
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
+ any Contribution intentionally submitted for inclusion in the Work
+ by You to the Licensor shall be under the terms and conditions of
+ this License, without any additional terms or conditions.
+ Notwithstanding the above, nothing herein shall supersede or modify
+ the terms of any separate license agreement you may have executed
+ with Licensor regarding such Contributions.
+
+ 6. Trademarks. This License does not grant permission to use the trade
+ names, trademarks, service marks, or product names of the Licensor,
+ except as required for reasonable and customary use in describing the
+ origin of the Work and reproducing the content of the NOTICE file.
+
+ 7. Disclaimer of Warranty. Unless required by applicable law or
+ agreed to in writing, Licensor provides the Work (and each
+ Contributor provides its Contributions) on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ implied, including, without limitation, any warranties or conditions
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+ PARTICULAR PURPOSE. You are solely responsible for determining the
+ appropriateness of using or redistributing the Work and assume any
+ risks associated with Your exercise of permissions under this License.
+
+ 8. Limitation of Liability. In no event and under no legal theory,
+ whether in tort (including negligence), contract, or otherwise,
+ unless required by applicable law (such as deliberate and grossly
+ negligent acts) or agreed to in writing, shall any Contributor be
+ liable to You for damages, including any direct, indirect, special,
+ incidental, or consequential damages of any character arising as a
+ result of this License or out of the use or inability to use the
+ Work (including but not limited to damages for loss of goodwill,
+ work stoppage, computer failure or malfunction, or any and all
+ other commercial damages or losses), even if such Contributor
+ has been advised of the possibility of such damages.
+
+ 9. Accepting Warranty or Additional Liability. While redistributing
+ the Work or Derivative Works thereof, You may choose to offer,
+ and charge a fee for, acceptance of support, warranty, indemnity,
+ or other liability obligations and/or rights consistent with this
+ License. However, in accepting such obligations, You may act only
+ on Your own behalf and on Your sole responsibility, not on behalf
+ of any other Contributor, and only if You agree to indemnify,
+ defend, and hold each Contributor harmless for any liability
+ incurred by, or claims asserted against, such Contributor by reason
+ of your accepting any such warranty or additional liability.
+
+ END OF TERMS AND CONDITIONS
+
View
171 libpagemap/include/pagemap/pagemap.h
@@ -0,0 +1,171 @@
+/*
+ * Copyright (C) 2008 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.
+ */
+
+#ifndef _PAGEMAP_PAGEMAP_H
+#define _PAGEMAP_PAGEMAP_H
+
+#include <stdint.h>
+#include <stdio.h>
+#include <sys/types.h>
+
+typedef struct pm_memusage pm_memusage_t;
+
+/* Holds the various metrics for memory usage of a process or a mapping. */
+struct pm_memusage {
+ size_t vss;
+ size_t rss;
+ size_t pss;
+ size_t uss;
+};
+
+/* Clears a memusage. */
+void pm_memusage_zero(pm_memusage_t *mu);
+/* Adds one memusage (a) to another (b). */
+void pm_memusage_add(pm_memusage_t *a, pm_memusage_t *b);
+
+typedef struct pm_kernel pm_kernel_t;
+typedef struct pm_process pm_process_t;
+typedef struct pm_map pm_map_t;
+
+/* pm_kernel_t holds the state necessary to interface to the kernel's pagemap
+ * system on a global level. */
+struct pm_kernel {
+ int kpagecount_fd;
+ int kpageflags_fd;
+
+ int pagesize;
+};
+
+/* pm_process_t holds the state necessary to interface to a particular process'
+ * pagemap. */
+struct pm_process {
+ pm_kernel_t *ker;
+
+ pid_t pid;
+
+ pm_map_t **maps;
+ int num_maps;
+
+ int pagemap_fd;
+};
+
+/* pm_map_t holds the state necessary to access information about a particular
+ * mapping in a particular process. */
+struct pm_map {
+ pm_process_t *proc;
+
+ unsigned long start;
+ unsigned long end;
+ unsigned long offset;
+ int flags;
+
+ char *name;
+};
+
+/* Create a pm_kernel_t. */
+int pm_kernel_create(pm_kernel_t **ker_out);
+
+#define pm_kernel_pagesize(ker) ((ker)->pagesize)
+
+/* Get a list of probably-existing PIDs (returned through *pids_out).
+ * Length of the array (in sizeof(pid_t) units) is returned through *len.
+ * The array should be freed by the caller. */
+int pm_kernel_pids(pm_kernel_t *ker, pid_t **pids_out, size_t *len);
+
+/* Get the map count (from /proc/kpagecount) of a physical frame.
+ * The count is returned through *count_out. */
+int pm_kernel_count(pm_kernel_t *ker, unsigned long pfn, uint64_t *count_out);
+
+/* Get the page flags (from /proc/kpageflags) of a physical frame.
+ * The count is returned through *flags_out. */
+int pm_kernel_flags(pm_kernel_t *ker, unsigned long pfn, uint64_t *flags_out);
+
+#define PM_PAGE_LOCKED (1 << 0)
+#define PM_PAGE_ERROR (1 << 1)
+#define PM_PAGE_REFERENCED (1 << 2)
+#define PM_PAGE_UPTODATE (1 << 3)
+#define PM_PAGE_DIRTY (1 << 4)
+#define PM_PAGE_LRU (1 << 5)
+#define PM_PAGE_ACTIVE (1 << 6)
+#define PM_PAGE_SLAB (1 << 7)
+#define PM_PAGE_WRITEBACK (1 << 8)
+#define PM_PAGE_RECLAIM (1 << 9)
+#define PM_PAGE_BUDDY (1 << 10)
+
+/* Destroy a pm_kernel_t. */
+int pm_kernel_destroy(pm_kernel_t *ker);
+
+/* Get the PID of a pm_process_t. */
+#define pm_process_pid(proc) ((proc)->pid)
+
+/* Create a pm_process_t and returns it through *proc_out.
+ * Takes a pm_kernel_t, and the PID of the process. */
+int pm_process_create(pm_kernel_t *ker, pid_t pid, pm_process_t **proc_out);
+
+/* Get the total memory usage of a process and store in *usage_out. */
+int pm_process_usage(pm_process_t *proc, pm_memusage_t *usage_out);
+
+/* Get the working set of a process (if ws_out != NULL), and reset it
+ * (if reset != 0). */
+int pm_process_workingset(pm_process_t *proc, pm_memusage_t *ws_out, int reset);
+
+/* Get the PFNs corresponding to a range of virtual addresses.
+ * The array of PFNs is returned through *range_out, and the caller has the
+ * responsibility to free it. */
+int pm_process_pagemap_range(pm_process_t *proc,
+ unsigned long low, unsigned long hi,
+ uint64_t **range_out, size_t *len);
+
+#define _BITS(x, offset, bits) (((x) >> offset) & ((1LL << ((bits) + 1)) - 1))
+
+#define PM_PAGEMAP_PRESENT(x) (_BITS(x, 63, 1))
+#define PM_PAGEMAP_SWAPPED(x) (!_BITS(x, 62, 1))
+#define PM_PAGEMAP_SHIFT(x) (_BITS(x, 55, 6))
+#define PM_PAGEMAP_PFN(x) (_BITS(x, 0, 55))
+#define PM_PAGEMAP_SWAP_OFFSET(x) (_BITS(x, 5, 50))
+#define PM_PAGEMAP_SWAP_TYPE(x) (_BITS(x, 0, 5))
+
+/* Get the maps in the virtual address space of this process.
+ * Returns an array of pointers to pm_map_t through *maps.
+ * The array should be freed by the caller, but the maps should not be
+ * modified or destroyed. */
+int pm_process_maps(pm_process_t *proc, pm_map_t ***maps_out, size_t *len);
+
+/* Destroy a pm_process_t. */
+int pm_process_destroy(pm_process_t *proc);
+
+/* Get the name, flags, start/end address, or offset of a map. */
+#define pm_map_name(map) ((map)->name)
+#define pm_map_flags(map) ((map)->flags)
+#define PM_MAP_READ 1
+#define PM_MAP_WRITE 2
+#define PM_MAP_EXEC 4
+#define pm_map_start(map) ((map)->start)
+#define pm_map_end(map) ((map)->end)
+#define pm_map_offset(map) ((map)->offset)
+
+/* Get the PFNs of the pages in the virtual address space of this map.
+ * Array of PFNs is returned through *pagemap_out, and should be freed by the
+ * caller. */
+int pm_map_pagemap(pm_map_t *map, uint64_t **pagemap_out, size_t *len);
+
+/* Get the memory usage of this map alone. */
+int pm_map_usage(pm_map_t *map, pm_memusage_t *usage_out);
+
+/* Get the working set of this map alone. */
+int pm_map_workingset(pm_map_t *map, pm_memusage_t *ws_out);
+
+#endif
View
158 libpagemap/pm_kernel.c
@@ -0,0 +1,158 @@
+/*
+ * Copyright (C) 2008 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 <fcntl.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+#include <pagemap/pagemap.h>
+
+int pm_kernel_create(pm_kernel_t **ker_out) {
+ pm_kernel_t *ker;
+ int error;
+
+ if (!ker_out)
+ return 1;
+
+ ker = calloc(1, sizeof(*ker));
+ if (!ker)
+ return errno;
+
+ ker->kpagecount_fd = open("/proc/kpagecount", O_RDONLY);
+ if (ker->kpagecount_fd < 0) {
+ error = errno;
+ free(ker);
+ return error;
+ }
+
+ ker->kpageflags_fd = open("/proc/kpageflags", O_RDONLY);
+ if (ker->kpageflags_fd < 0) {
+ error = errno;
+ close(ker->kpagecount_fd);
+ free(ker);
+ return error;
+ }
+
+ ker->pagesize = getpagesize();
+
+ *ker_out = ker;
+
+ return 0;
+}
+
+#define INIT_PIDS 20
+int pm_kernel_pids(pm_kernel_t *ker, pid_t **pids_out, size_t *len) {
+ DIR *proc;
+ struct dirent *dir;
+ pid_t pid, *pids, *new_pids;
+ size_t pids_count, pids_size;
+ int error;
+
+ proc = opendir("/proc");
+ if (!proc)
+ return errno;
+
+ pids = malloc(INIT_PIDS * sizeof(pid_t));
+ if (!pids) {
+ closedir(proc);
+ return errno;
+ }
+ pids_count = 0; pids_size = INIT_PIDS;
+
+ while ((dir = readdir(proc))) {
+ if (sscanf(dir->d_name, "%d", &pid) < 1)
+ continue;
+
+ if (pids_count >= pids_size) {
+ new_pids = realloc(pids, 2 * pids_size * sizeof(pid_t));
+ if (!new_pids) {
+ error = errno;
+ free(pids);
+ closedir(proc);
+ return error;
+ }
+ pids = new_pids;
+ pids_size = 2 * pids_size;
+ }
+
+ pids[pids_count] = pid;
+
+ pids_count++;
+ }
+
+ closedir(proc);
+
+ new_pids = realloc(pids, pids_count * sizeof(pid_t));
+ if (!new_pids) {
+ error = errno;
+ free(pids);
+ return error;
+ }
+
+ *pids_out = new_pids;
+ *len = pids_count;
+
+ return 0;
+}
+
+int pm_kernel_count(pm_kernel_t *ker, unsigned long pfn, uint64_t *count_out) {
+ off_t off;
+
+ if (!ker || !count_out)
+ return -1;
+
+ off = lseek(ker->kpagecount_fd, pfn * sizeof(uint64_t), SEEK_SET);
+ if (off == (off_t)-1)
+ return errno;
+ if (read(ker->kpagecount_fd, count_out, sizeof(uint64_t)) <
+ (ssize_t)sizeof(uint64_t))
+ return errno;
+
+ return 0;
+}
+
+int pm_kernel_flags(pm_kernel_t *ker, unsigned long pfn, uint64_t *flags_out) {
+ off_t off;
+
+ if (!ker || !flags_out)
+ return -1;
+
+ off = lseek(ker->kpageflags_fd, pfn * sizeof(uint64_t), SEEK_SET);
+ if (off == (off_t)-1)
+ return errno;
+ if (read(ker->kpageflags_fd, flags_out, sizeof(uint64_t)) <
+ (ssize_t)sizeof(uint64_t))
+ return errno;
+
+ return 0;
+}
+
+int pm_kernel_destroy(pm_kernel_t *ker) {
+ if (!ker)
+ return -1;
+
+ close(ker->kpagecount_fd);
+ close(ker->kpageflags_fd);
+
+ free(ker);
+
+ return 0;
+}
View
125 libpagemap/pm_map.c
@@ -0,0 +1,125 @@
+/*
+ * Copyright (C) 2008 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 <stdlib.h>
+#include <string.h>
+
+#include <pagemap/pagemap.h>
+
+int pm_map_pagemap(pm_map_t *map, uint64_t **pagemap_out, size_t *len) {
+ if (!map)
+ return -1;
+
+ return pm_process_pagemap_range(map->proc, map->start, map->end,
+ pagemap_out, len);
+}
+
+int pm_map_usage(pm_map_t *map, pm_memusage_t *usage_out) {
+ uint64_t *pagemap;
+ size_t len, i;
+ uint64_t count;
+ pm_memusage_t usage;
+ int error;
+
+ if (!map || !usage_out)
+ return -1;
+
+ error = pm_map_pagemap(map, &pagemap, &len);
+ if (error) return error;
+
+ pm_memusage_zero(&usage);
+
+ for (i = 0; i < len; i++) {
+ if (!PM_PAGEMAP_PRESENT(pagemap[i]) ||
+ PM_PAGEMAP_SWAPPED(pagemap[i]))
+ continue;
+
+ error = pm_kernel_count(map->proc->ker, PM_PAGEMAP_PFN(pagemap[i]),
+ &count);
+ if (error) goto out;
+
+ usage.vss += map->proc->ker->pagesize;
+ usage.rss += (count >= 1) ? (map->proc->ker->pagesize) : (0);
+ usage.pss += (count >= 1) ? (map->proc->ker->pagesize / count) : (0);
+ usage.uss += (count == 1) ? (map->proc->ker->pagesize) : (0);
+ }
+
+ memcpy(usage_out, &usage, sizeof(usage));
+
+ error = 0;
+
+out:
+ free(pagemap);
+
+ return error;
+}
+
+int pm_map_workingset(pm_map_t *map, pm_memusage_t *ws_out) {
+ uint64_t *pagemap;
+ size_t len, i;
+ uint64_t count, flags;
+ pm_memusage_t ws;
+ int error;
+
+ if (!map || !ws_out)
+ return -1;
+
+ error = pm_map_pagemap(map, &pagemap, &len);
+ if (error) return error;
+
+ pm_memusage_zero(&ws);
+
+ for (i = 0; i < len; i++) {
+ if (!PM_PAGEMAP_PRESENT(pagemap[i]) ||
+ PM_PAGEMAP_SWAPPED(pagemap[i]))
+ continue;
+
+ error = pm_kernel_flags(map->proc->ker, PM_PAGEMAP_PFN(pagemap[i]),
+ &flags);
+ if (error) goto out;
+
+ if (!(flags & PM_PAGE_REFERENCED))
+ continue;
+
+ error = pm_kernel_count(map->proc->ker, PM_PAGEMAP_PFN(pagemap[i]),
+ &count);
+ if (error) goto out;
+
+ ws.vss += map->proc->ker->pagesize;
+ ws.rss += (count >= 1) ? (map->proc->ker->pagesize) : (0);
+ ws.pss += (count >= 1) ? (map->proc->ker->pagesize / count) : (0);
+ ws.uss += (count == 1) ? (map->proc->ker->pagesize) : (0);
+ }
+
+ memcpy(ws_out, &ws, sizeof(ws));
+
+ error = 0;
+
+out:
+ free(pagemap);
+
+ return 0;
+}
+
+int pm_map_destroy(pm_map_t *map) {
+ if (!map)
+ return -1;
+
+ free(map->name);
+ free(map);
+
+ return 0;
+}
View
24 libpagemap/pm_map.h
@@ -0,0 +1,24 @@
+/*
+ * Copyright (C) 2008 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.
+ */
+
+#ifndef _LIBS_PAGEMAP_PM_MAP_H
+#define _LIBS_PAGEMAP_PM_MAP_H
+
+#include <pagemap/pagemap.h>
+
+int pm_map_destroy(pm_map_t *map);
+
+#endif
View
28 libpagemap/pm_memusage.c
@@ -0,0 +1,28 @@
+/*
+ * Copyright (C) 2008 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 <pagemap/pagemap.h>
+
+void pm_memusage_zero(pm_memusage_t *mu) {
+ mu->vss = mu->rss = mu->pss = mu->uss = 0;
+}
+
+void pm_memusage_add(pm_memusage_t *a, pm_memusage_t *b) {
+ a->vss += b->vss;
+ a->rss += b->rss;
+ a->pss += b->pss;
+ a->uss += b->uss;
+}
View
290 libpagemap/pm_process.c
@@ -0,0 +1,290 @@
+/*
+ * Copyright (C) 2008 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 <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <pagemap/pagemap.h>
+
+#include "pm_map.h"
+
+static int read_maps(pm_process_t *proc);
+
+#define MAX_FILENAME 64
+
+int pm_process_create(pm_kernel_t *ker, pid_t pid, pm_process_t **proc_out) {
+ pm_process_t *proc;
+ char filename[MAX_FILENAME];
+ int error;
+
+ if (!ker || !proc_out)
+ return -1;
+
+ proc = calloc(1, sizeof(*proc));
+ if (!proc)
+ return errno;
+
+ proc->ker = ker;
+ proc->pid = pid;
+
+ error = snprintf(filename, MAX_FILENAME, "/proc/%d/pagemap", pid);
+ if (error < 0 || error >= MAX_FILENAME) {
+ error = (error < 0) ? (errno) : (-1);
+ free(proc);
+ return error;
+ }
+
+ proc->pagemap_fd = open(filename, O_RDONLY);
+ if (proc->pagemap_fd < 0) {
+ error = errno;
+ free(proc);
+ return error;
+ }
+
+ error = read_maps(proc);
+ if (error) {
+ free(proc);
+ return error;
+ }
+
+ *proc_out = proc;
+
+ return 0;
+}
+
+int pm_process_usage(pm_process_t *proc, pm_memusage_t *usage_out) {
+ pm_memusage_t usage, map_usage;
+ int error;
+ int i;
+
+ if (!proc || !usage_out)
+ return -1;
+
+ pm_memusage_zero(&usage);
+
+ for (i = 0; i < proc->num_maps; i++) {
+ error = pm_map_usage(proc->maps[i], &map_usage);
+ if (error) return error;
+
+ pm_memusage_add(&usage, &map_usage);
+ }
+
+ memcpy(usage_out, &usage, sizeof(pm_memusage_t));
+
+ return 0;
+}
+
+int pm_process_pagemap_range(pm_process_t *proc,
+ unsigned long low, unsigned long high,
+ uint64_t **range_out, size_t *len) {
+ int firstpage, numpages;
+ uint64_t *range;
+ off_t off;
+ int error;
+
+ if (!proc || (low >= high) || !range_out || !len)
+ return -1;
+
+ firstpage = low / proc->ker->pagesize;
+ numpages = (high - low) / proc->ker->pagesize;
+
+ range = malloc(numpages * sizeof(uint64_t));
+ if (!range)
+ return errno;
+
+ off = lseek(proc->pagemap_fd, firstpage * sizeof(uint64_t), SEEK_SET);
+ if (off == (off_t)-1) {
+ error = errno;
+ free(range);
+ return error;
+ }
+ error = read(proc->pagemap_fd, (char*)range, numpages * sizeof(uint64_t));
+ if (error < numpages * sizeof(uint64_t)) {
+ error = (error < 0) ? errno : -1;
+ free(range);
+ return error;
+ }
+
+ *range_out = range;
+ *len = numpages;
+
+ return 0;
+}
+
+int pm_process_maps(pm_process_t *proc, pm_map_t ***maps_out, size_t *len) {
+ pm_map_t **maps;
+
+ if (!proc || !maps_out || !len)
+ return -1;
+
+ if (proc->num_maps) {
+ maps = malloc(proc->num_maps * sizeof(pm_map_t*));
+ if (!maps)
+ return errno;
+
+ memcpy(maps, proc->maps, proc->num_maps * sizeof(pm_map_t*));
+
+ *maps_out = maps;
+ } else {
+ *maps_out = NULL;
+ }
+ *len = proc->num_maps;
+
+ return 0;
+}
+
+int pm_process_workingset(pm_process_t *proc,
+ pm_memusage_t *ws_out, int reset) {
+ pm_memusage_t ws, map_ws;
+ char filename[MAX_FILENAME];
+ int fd;
+ int i, j;
+ int error;
+
+ if (!proc)
+ return -1;
+
+ if (ws_out) {
+ pm_memusage_zero(&ws);
+ for (i = 0; i < proc->num_maps; i++) {
+ error = pm_map_workingset(proc->maps[i], &map_ws);
+ if (error) return error;
+
+ pm_memusage_add(&ws, &map_ws);
+ }
+
+ memcpy(ws_out, &ws, sizeof(ws));
+ }
+
+ if (reset) {
+ error = snprintf(filename, MAX_FILENAME, "/proc/%d/clear_refs",
+ proc->pid);
+ if (error < 0 || error >= MAX_FILENAME) {
+ return (error < 0) ? (errno) : (-1);
+ }
+
+ fd = open(filename, O_WRONLY);
+ if (fd < 0)
+ return errno;
+
+ write(fd, "1\n", strlen("1\n"));
+
+ close(fd);
+ }
+
+ return 0;
+}
+
+int pm_process_destroy(pm_process_t *proc) {
+ if (!proc)
+ return -1;
+
+ free(proc->maps);
+ close(proc->pagemap_fd);
+ free(proc);
+
+ return 0;
+}
+
+#define INITIAL_MAPS 10
+#define MAX_LINE 256
+#define MAX_PERMS 5
+
+/*
+ * #define FOO 123
+ * S(FOO) => "123"
+ */
+#define _S(n) #n
+#define S(n) _S(n)
+
+static int read_maps(pm_process_t *proc) {
+ char filename[MAX_FILENAME];
+ char line[MAX_LINE], name[MAX_LINE], perms[MAX_PERMS];
+ FILE *maps_f;
+ pm_map_t *map, **maps, **new_maps;
+ int maps_count, maps_size;
+ int error;
+
+ if (!proc)
+ return -1;
+
+ maps = calloc(INITIAL_MAPS, sizeof(pm_map_t*));
+ if (!maps)
+ return errno;
+ maps_count = 0; maps_size = INITIAL_MAPS;
+
+ error = snprintf(filename, MAX_FILENAME, "/proc/%d/maps", proc->pid);
+ if (error < 0 || error >= MAX_FILENAME)
+ return (error < 0) ? (errno) : (-1);
+
+ maps_f = fopen(filename, "r");
+ if (!maps_f)
+ return errno;
+
+ while (fgets(line, MAX_LINE, maps_f)) {
+ if (maps_count >= maps_size) {
+ new_maps = realloc(maps, 2 * maps_size * sizeof(pm_map_t*));
+ if (!new_maps) {
+ error = errno;
+ free(maps);
+ fclose(maps_f);
+ return error;
+ }
+ maps = new_maps;
+ maps_size *= 2;
+ }
+
+ maps[maps_count] = map = calloc(1, sizeof(*map));
+
+ map->proc = proc;
+
+ sscanf(line, "%lx-%lx %s %lx %*s %*d %" S(MAX_LINE) "s",
+ &map->start, &map->end, perms, &map->offset, name);
+
+ map->name = malloc(strlen(name) + 1);
+ if (!map->name) {
+ error = errno;
+ for (; maps_count > 0; maps_count--)
+ pm_map_destroy(maps[maps_count]);
+ free(maps);
+ return error;
+ }
+ strcpy(map->name, name);
+ if (perms[0] == 'r') map->flags |= PM_MAP_READ;
+ if (perms[1] == 'w') map->flags |= PM_MAP_WRITE;
+ if (perms[2] == 'x') map->flags |= PM_MAP_EXEC;
+
+ maps_count++;
+ }
+
+ fclose(maps_f);
+
+ new_maps = realloc(maps, maps_count * sizeof(pm_map_t*));
+ if (maps_count && !new_maps) {
+ error = errno;
+ free(maps);
+ return error;
+ }
+
+ proc->maps = new_maps;
+ proc->num_maps = maps_count;
+
+ return 0;
+}
View
30 librank/Android.mk
@@ -0,0 +1,30 @@
+# Copyright (C) 2008 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.
+
+LOCAL_PATH:= $(call my-dir)
+include $(CLEAR_VARS)
+
+LOCAL_SRC_FILES := librank.c
+
+LOCAL_C_INCLUDES := $(call include-path-for, libpagemap)
+
+LOCAL_SHARED_LIBRARIES := libpagemap
+
+LOCAL_MODULE := librank
+
+LOCAL_MODULE_PATH := $(TARGET_OUT_OPTIONAL_EXECUTABLES)
+
+LOCAL_MODULE_TAGS := debug
+
+include $(BUILD_EXECUTABLE)
View
0  librank/MODULE_LICENSE_APACHE2
No changes.
View
190 librank/NOTICE
@@ -0,0 +1,190 @@
+
+ Copyright (c) 2005-2008, 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.
+
+ 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.
+
+
+ Apache License
+ Version 2.0, January 2004
+ http://www.apache.org/licenses/
+
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+ 1. Definitions.
+
+ "License" shall mean the terms and conditions for use, reproduction,
+ and distribution as defined by Sections 1 through 9 of this document.
+
+ "Licensor" shall mean the copyright owner or entity authorized by
+ the copyright owner that is granting the License.
+
+ "Legal Entity" shall mean the union of the acting entity and all
+ other entities that control, are controlled by, or are under common
+ control with that entity. For the purposes of this definition,
+ "control" means (i) the power, direct or indirect, to cause the
+ direction or management of such entity, whether by contract or
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
+ outstanding shares, or (iii) beneficial ownership of such entity.
+
+ "You" (or "Your") shall mean an individual or Legal Entity
+ exercising permissions granted by this License.
+
+ "Source" form shall mean the preferred form for making modifications,
+ including but not limited to software source code, documentation
+ source, and configuration files.
+
+ "Object" form shall mean any form resulting from mechanical
+ transformation or translation of a Source form, including but
+ not limited to compiled object code, generated documentation,
+ and conversions to other media types.
+
+ "Work" shall mean the work of authorship, whether in Source or
+ Object form, made available under the License, as indicated by a
+ copyright notice that is included in or attached to the work
+ (an example is provided in the Appendix below).
+
+ "Derivative Works" shall mean any work, whether in Source or Object
+ form, that is based on (or derived from) the Work and for which the
+ editorial revisions, annotations, elaborations, or other modifications
+ represent, as a whole, an original work of authorship. For the purposes
+ of this License, Derivative Works shall not include works that remain
+ separable from, or merely link (or bind by name) to the interfaces of,
+ the Work and Derivative Works thereof.
+
+ "Contribution" shall mean any work of authorship, including
+ the original version of the Work and any modifications or additions
+ to that Work or Derivative Works thereof, that is intentionally
+ submitted to Licensor for inclusion in the Work by the copyright owner
+ or by an individual or Legal Entity authorized to submit on behalf of
+ the copyright owner. For the purposes of this definition, "submitted"
+ means any form of electronic, verbal, or written communication sent
+ to the Licensor or its representatives, including but not limited to
+ communication on electronic mailing lists, source code control systems,
+ and issue tracking systems that are managed by, or on behalf of, the
+ Licensor for the purpose of discussing and improving the Work, but
+ excluding communication that is conspicuously marked or otherwise
+ designated in writing by the copyright owner as "Not a Contribution."
+
+ "Contributor" shall mean Licensor and any individual or Legal Entity
+ on behalf of whom a Contribution has been received by Licensor and
+ subsequently incorporated within the Work.
+
+ 2. Grant of Copyright License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ copyright license to reproduce, prepare Derivative Works of,
+ publicly display, publicly perform, sublicense, and distribute the
+ Work and such Derivative Works in Source or Object form.
+
+ 3. Grant of Patent License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ (except as stated in this section) patent license to make, have made,
+ use, offer to sell, sell, import, and otherwise transfer the Work,
+ where such license applies only to those patent claims licensable
+ by such Contributor that are necessarily infringed by their
+ Contribution(s) alone or by combination of their Contribution(s)
+ with the Work to which such Contribution(s) was submitted. If You
+ institute patent litigation against any entity (including a
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
+ or a Contribution incorporated within the Work constitutes direct
+ or contributory patent infringement, then any patent licenses
+ granted to You under this License for that Work shall terminate
+ as of the date such litigation is filed.
+
+ 4. Redistribution. You may reproduce and distribute copies of the
+ Work or Derivative Works thereof in any medium, with or without
+ modifications, and in Source or Object form, provided that You
+ meet the following conditions:
+
+ (a) You must give any other recipients of the Work or
+ Derivative Works a copy of this License; and
+
+ (b) You must cause any modified files to carry prominent notices
+ stating that You changed the files; and
+
+ (c) You must retain, in the Source form of any Derivative Works
+ that You distribute, all copyright, patent, trademark, and
+ attribution notices from the Source form of the Work,
+ excluding those notices that do not pertain to any part of
+ the Derivative Works; and
+
+ (d) If the Work includes a "NOTICE" text file as part of its
+ distribution, then any Derivative Works that You distribute must
+ include a readable copy of the attribution notices contained
+ within such NOTICE file, excluding those notices that do not
+ pertain to any part of the Derivative Works, in at least one
+ of the following places: within a NOTICE text file distributed
+ as part of the Derivative Works; within the Source form or
+ documentation, if provided along with the Derivative Works; or,
+ within a display generated by the Derivative Works, if and
+ wherever such third-party notices normally appear. The contents
+ of the NOTICE file are for informational purposes only and
+ do not modify the License. You may add Your own attribution
+ notices within Derivative Works that You distribute, alongside
+ or as an addendum to the NOTICE text from the Work, provided
+ that such additional attribution notices cannot be construed
+ as modifying the License.
+
+ You may add Your own copyright statement to Your modifications and
+ may provide additional or different license terms and conditions
+ for use, reproduction, or distribution of Your modifications, or
+ for any such Derivative Works as a whole, provided Your use,
+ reproduction, and distribution of the Work otherwise complies with
+ the conditions stated in this License.
+
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
+ any Contribution intentionally submitted for inclusion in the Work
+ by You to the Licensor shall be under the terms and conditions of
+ this License, without any additional terms or conditions.
+ Notwithstanding the above, nothing herein shall supersede or modify
+ the terms of any separate license agreement you may have executed
+ with Licensor regarding such Contributions.
+
+ 6. Trademarks. This License does not grant permission to use the trade
+ names, trademarks, service marks, or product names of the Licensor,
+ except as required for reasonable and customary use in describing the
+ origin of the Work and reproducing the content of the NOTICE file.
+
+ 7. Disclaimer of Warranty. Unless required by applicable law or
+ agreed to in writing, Licensor provides the Work (and each
+ Contributor provides its Contributions) on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ implied, including, without limitation, any warranties or conditions
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+ PARTICULAR PURPOSE. You are solely responsible for determining the
+ appropriateness of using or redistributing the Work and assume any
+ risks associated with Your exercise of permissions under this License.
+
+ 8. Limitation of Liability. In no event and under no legal theory,
+ whether in tort (including negligence), contract, or otherwise,
+ unless required by applicable law (such as deliberate and grossly
+ negligent acts) or agreed to in writing, shall any Contributor be
+ liable to You for damages, including any direct, indirect, special,
+ incidental, or consequential damages of any character arising as a
+ result of this License or out of the use or inability to use the
+ Work (including but not limited to damages for loss of goodwill,
+ work stoppage, computer failure or malfunction, or any and all
+ other commercial damages or losses), even if such Contributor
+ has been advised of the possibility of such damages.
+
+ 9. Accepting Warranty or Additional Liability. While redistributing
+ the Work or Derivative Works thereof, You may choose to offer,
+ and charge a fee for, acceptance of support, warranty, indemnity,
+ or other liability obligations and/or rights consistent with this
+ License. However, in accepting such obligations, You may act only
+ on Your own behalf and on Your sole responsibility, not on behalf
+ of any other Contributor, and only if You agree to indemnify,
+ defend, and hold each Contributor harmless for any liability
+ incurred by, or claims asserted against, such Contributor by reason
+ of your accepting any such warranty or additional liability.
+
+ END OF TERMS AND CONDITIONS
+
View
349 librank/librank.c
@@ -0,0 +1,349 @@
+/*
+ * Copyright (C) 2008 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 <assert.h>
+#include <dirent.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+#include <pagemap/pagemap.h>
+
+#define MAX_CMDLINE 256
+
+struct process_info {
+ pid_t pid;
+ char cmdline[MAX_CMDLINE];
+};
+
+struct mapping_info {
+ struct process_info *proc;
+ pm_memusage_t usage;
+};
+
+struct library_info {
+ struct library_info *next;
+ char *name;
+ struct mapping_info **mappings;
+ int mappings_count;
+ int mappings_size;
+ pm_memusage_t total_usage;
+};
+
+static void usage(char *myname);
+static int getprocname(pid_t pid, char *buf, size_t len);
+static int numcmp(long long a, long long b);
+static int licmp(const void *a, const void *b);
+
+char *library_name_blacklist[] = { "[heap]", "[stack]", "", NULL };
+
+#define declare_sort(field) \
+ static int sort_by_ ## field (const void *a, const void *b)
+
+declare_sort(vss);
+declare_sort(rss);
+declare_sort(pss);
+declare_sort(uss);
+
+#define INIT_LIBRARIES 16
+#define INIT_MAPPINGS 4
+
+static int order;
+
+struct library_info **libraries;
+int libraries_count;
+int libraries_size;
+
+struct library_info *get_library(char *name) {
+ int i;
+ struct library_info *library;
+
+ for (i = 0; library_name_blacklist[i]; i++)
+ if (!strcmp(name, library_name_blacklist[i]))
+ return NULL;
+
+ for (i = 0; i < libraries_count; i++) {
+ if (!strcmp(libraries[i]->name, name))
+ return libraries[i];
+ }
+
+ if (libraries_count >= libraries_size) {
+ libraries = realloc(libraries, 2 * libraries_size * sizeof(struct library_info *));
+ if (!libraries) {
+ fprintf(stderr, "Couldn't resize libraries array: %s\n", strerror(errno));
+ exit(EXIT_FAILURE);
+ }
+ libraries_size = 2 * libraries_size;
+ }
+
+ library = calloc(1, sizeof(*library));
+ if (!library) {
+ fprintf(stderr, "Couldn't allocate space for library struct: %s\n", strerror(errno));
+ exit(EXIT_FAILURE);
+ }
+ library->name = malloc(strlen(name) + 1);
+ if (!library->name) {
+ fprintf(stderr, "Couldn't allocate space for library name: %s\n", strerror(errno));
+ exit(EXIT_FAILURE);
+ }
+ strcpy(library->name, name);
+ library->mappings = malloc(INIT_MAPPINGS * sizeof(struct mapping_info *));
+ if (!library->mappings) {
+ fprintf(stderr, "Couldn't allocate space for library mappings array: %s\n", strerror(errno));
+ exit(EXIT_FAILURE);
+ }
+ library->mappings_count = 0; library->mappings_size = INIT_MAPPINGS;
+ pm_memusage_zero(&library->total_usage);
+
+ libraries[libraries_count++] = library;
+
+ return library;
+}
+
+struct mapping_info *get_mapping(struct library_info *library, struct process_info *proc) {
+ struct mapping_info *mapping;
+ int i;
+
+ for (i = 0; i < library->mappings_count; i++) {
+ if (library->mappings[i]->proc == proc)
+ return library->mappings[i];
+ }
+
+ if (library->mappings_count >= library->mappings_size) {
+ library->mappings = realloc(library->mappings,
+ 2 * library->mappings_size * sizeof(struct mapping*));
+ if (!library->mappings) {
+ fprintf(stderr, "Couldn't resize mappings array: %s\n", strerror(errno));
+ exit(EXIT_FAILURE);
+ }
+ library->mappings_size = 2 * library->mappings_size;
+ }
+
+ mapping = calloc(1, sizeof(*mapping));
+ if (!mapping) {
+ fprintf(stderr, "Couldn't allocate space for mapping struct: %s\n", strerror(errno));
+ exit(EXIT_FAILURE);
+ }
+ mapping->proc = proc;
+ pm_memusage_zero(&mapping->usage);
+
+ library->mappings[library->mappings_count++] = mapping;
+
+ return mapping;
+}
+
+struct process_info *get_process(pid_t pid) {
+ struct process_info *process;
+
+ process = calloc(1, sizeof(*process));
+ if (!process) {
+ fprintf(stderr, "Couldn't allocate space for process struct: %s\n", strerror(errno));
+ exit(EXIT_FAILURE);
+ }
+
+ process->pid = pid;
+ getprocname(pid, process->cmdline, sizeof(process->cmdline));
+
+ return process;
+}
+
+int main(int argc, char *argv[]) {
+ char cmdline[256];
+ char *prefix;
+ size_t prefix_len;
+ int (*compfn)(const void *a, const void *b);
+
+ pm_kernel_t *ker;
+ pm_process_t *proc;
+
+ pid_t *pids;
+ size_t num_procs;
+
+ pm_map_t **maps;
+ size_t num_maps;
+ pm_memusage_t map_usage;
+
+ struct library_info *li, **lis;
+ struct mapping_info *mi, **mis;
+ struct process_info *pi;
+
+ int i, j, error;
+
+ compfn = &sort_by_pss;
+ order = -1;
+ prefix = NULL;
+ prefix_len = 0;
+
+ for (i = 1; i < argc; i++) {
+ if (!strcmp(argv[i], "-P")) {
+ if (i + 1 >= argc) {
+ fprintf(stderr, "Option -P requires an argument.\n");
+ usage(argv[0]);
+ exit(EXIT_FAILURE);
+ }
+ prefix = argv[++i];
+ prefix_len = strlen(prefix);
+ continue;
+ }
+ if (!strcmp(argv[i], "-v")) { compfn = &sort_by_vss; continue; }
+ if (!strcmp(argv[i], "-r")) { compfn = &sort_by_rss; continue; }
+ if (!strcmp(argv[i], "-p")) { compfn = &sort_by_pss; continue; }
+ if (!strcmp(argv[i], "-u")) { compfn = &sort_by_uss; continue; }
+ if (!strcmp(argv[i], "-R")) { order *= -1; continue; }
+ if (!strcmp(argv[i], "-h")) { usage(argv[0]); exit(0); }
+ fprintf(stderr, "Invalid argument \"%s\".\n", argv[i]);
+ usage(argv[0]);
+ exit(EXIT_FAILURE);
+ }
+
+ libraries = malloc(INIT_LIBRARIES * sizeof(struct library_info *));
+ libraries_count = 0; libraries_size = INIT_LIBRARIES;
+
+ error = pm_kernel_create(&ker);
+ if (error) {
+ fprintf(stderr, "Error initializing kernel interface -- "
+ "does this kernel have pagemap?\n");
+ exit(EXIT_FAILURE);
+ }
+
+ error = pm_kernel_pids(ker, &pids, &num_procs);
+ if (error) {
+ fprintf(stderr, "Error listing processes.\n");
+ exit(EXIT_FAILURE);
+ }
+
+ for (i = 0; i < num_procs; i++) {
+ error = pm_process_create(ker, pids[i], &proc);
+ if (error) {
+ fprintf(stderr, "warning: could not create process interface for %d\n", pids[i]);
+ continue;
+ }
+
+ pi = get_process(pids[i]);
+
+ error = pm_process_maps(proc, &maps, &num_maps);
+ if (error) {
+ fprintf(stderr, "Error listing maps for process %d.\n", proc->pid);
+ exit(EXIT_FAILURE);
+ }
+
+ for (j = 0; j < num_maps; j++) {
+ if (prefix && (strncmp(pm_map_name(maps[j]), prefix, prefix_len)))
+ continue;
+
+ li = get_library(pm_map_name(maps[j]));
+ if (!li)
+ continue;
+
+ mi = get_mapping(li, pi);
+
+ error = pm_map_usage(maps[j], &map_usage);
+ if (error) {
+ fprintf(stderr, "Error getting map memory usage of "
+ "map %s in process %d.\n",
+ pm_map_name(maps[j]), proc->pid);
+ exit(EXIT_FAILURE);
+ }
+ pm_memusage_add(&mi->usage, &map_usage);
+ pm_memusage_add(&li->total_usage, &map_usage);
+ }
+ }
+
+ printf( " %6s %6s %6s %6s %6s %s\n", "RSStot", "VSS", "RSS", "PSS", "USS", "Name/PID");
+ fflush(stdout);
+
+ qsort(libraries, libraries_count, sizeof(libraries[0]), &licmp);
+
+ for (i = 0; i < libraries_count; i++) {
+ li = libraries[i];
+
+ printf("%6dK %6s %6s %6s %6s %s\n", li->total_usage.pss / 1024, "", "", "", "", li->name);
+ fflush(stdout);
+
+ qsort(li->mappings, li->mappings_count, sizeof(li->mappings[0]), compfn);
+
+ for (j = 0; j < li->mappings_count; j++) {
+ mi = li->mappings[j];
+ pi = mi->proc;
+ printf( " %6s %6dK %6dK %6dK %6dK %s [%d]\n", "",
+ mi->usage.vss / 1024,
+ mi->usage.rss / 1024,
+ mi->usage.pss / 1024,
+ mi->usage.uss / 1024,
+ pi->cmdline,
+ pi->pid);
+ }
+ printf("\n");
+ fflush(stdout);
+ }
+
+ return 0;
+}
+
+static void usage(char *myname) {
+ fprintf(stderr, "Usage: %s [ -P | -L ] [ -v | -r | -p | -u | -h ]\n"
+ "\n"
+ "Sort options:\n"
+ " -v Sort processes by VSS.\n"
+ " -r Sort processes by RSS.\n"
+ " -p Sort processes by PSS.\n"
+ " -u Sort processes by USS.\n"
+ " (Default sort order is PSS.)\n"
+ " -P /path Limit libraries displayed to those in path.\n"
+ " -R Reverse sort order (default is descending).\n"
+ " -h Display this help screen.\n",
+ myname);
+}
+
+static int getprocname(pid_t pid, char *buf, size_t len) {
+ char filename[20];
+ FILE *f;
+
+ sprintf(filename, "/proc/%d/cmdline", pid);
+ f = fopen(filename, "r");
+ if (!f) { *buf = '\0'; return 1; }
+ if (!fgets(buf, len, f)) { *buf = '\0'; return 2; }
+ fclose(f);
+ return 0;
+}
+
+static int numcmp(long long a, long long b) {
+ if (a < b) return -1;
+ if (a > b) return 1;
+ return 0;
+}
+
+static int licmp(const void *a, const void *b) {
+ return order * numcmp(
+ (*((struct library_info**)a))->total_usage.pss,
+ (*((struct library_info**)b))->total_usage.pss
+ );
+}
+
+#define create_sort(field, compfn) \
+ static int sort_by_ ## field (const void *a, const void *b) { \
+ return order * compfn( \
+ (*((struct mapping_info**)a))->usage.field, \
+ (*((struct mapping_info**)b))->usage.field \
+ ); \
+ }
+
+create_sort(vss, numcmp)
+create_sort(rss, numcmp)
+create_sort(pss, numcmp)
+create_sort(uss, numcmp)
View
30 procmem/Android.mk
@@ -0,0 +1,30 @@
+# Copyright (C) 2008 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.
+
+LOCAL_PATH:= $(call my-dir)
+include $(CLEAR_VARS)
+
+LOCAL_SRC_FILES := procmem.c
+
+LOCAL_C_INCLUDES := $(call include-path-for, libpagemap)
+
+LOCAL_SHARED_LIBRARIES := libpagemap
+
+LOCAL_MODULE := procmem
+
+LOCAL_MODULE_PATH := $(TARGET_OUT_OPTIONAL_EXECUTABLES)
+
+LOCAL_MODULE_TAGS := debug
+
+include $(BUILD_EXECUTABLE)
View
0  procmem/MODULE_LICENSE_APACHE2
No changes.
View
190 procmem/NOTICE
@@ -0,0 +1,190 @@
+
+ Copyright (c) 2005-2008, 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.
+
+ 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.
+
+
+ Apache License
+ Version 2.0, January 2004
+ http://www.apache.org/licenses/
+
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+ 1. Definitions.