summaryrefslogtreecommitdiff
path: root/libpagemap
diff options
context:
space:
mode:
Diffstat (limited to 'libpagemap')
-rw-r--r--libpagemap/Android.mk30
-rw-r--r--libpagemap/MODULE_LICENSE_APACHE20
-rw-r--r--libpagemap/NOTICE190
-rw-r--r--libpagemap/include/pagemap/pagemap.h171
-rw-r--r--libpagemap/pm_kernel.c158
-rw-r--r--libpagemap/pm_map.c125
-rw-r--r--libpagemap/pm_map.h24
-rw-r--r--libpagemap/pm_memusage.c28
-rw-r--r--libpagemap/pm_process.c290
9 files changed, 1016 insertions, 0 deletions
diff --git a/libpagemap/Android.mk b/libpagemap/Android.mk
new file mode 100644
index 00000000..b38363c8
--- /dev/null
+++ b/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)
diff --git a/libpagemap/MODULE_LICENSE_APACHE2 b/libpagemap/MODULE_LICENSE_APACHE2
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/libpagemap/MODULE_LICENSE_APACHE2
diff --git a/libpagemap/NOTICE b/libpagemap/NOTICE
new file mode 100644
index 00000000..c5b1efa7
--- /dev/null
+++ b/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
+
diff --git a/libpagemap/include/pagemap/pagemap.h b/libpagemap/include/pagemap/pagemap.h
new file mode 100644
index 00000000..09ff29d8
--- /dev/null
+++ b/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
diff --git a/libpagemap/pm_kernel.c b/libpagemap/pm_kernel.c
new file mode 100644
index 00000000..3615f1ac
--- /dev/null
+++ b/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;
+}
diff --git a/libpagemap/pm_map.c b/libpagemap/pm_map.c
new file mode 100644
index 00000000..f683ba6c
--- /dev/null
+++ b/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;
+}
diff --git a/libpagemap/pm_map.h b/libpagemap/pm_map.h
new file mode 100644
index 00000000..08dc4485
--- /dev/null
+++ b/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
diff --git a/libpagemap/pm_memusage.c b/libpagemap/pm_memusage.c
new file mode 100644
index 00000000..9c5db92e
--- /dev/null
+++ b/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;
+}
diff --git a/libpagemap/pm_process.c b/libpagemap/pm_process.c
new file mode 100644
index 00000000..1ab367d8
--- /dev/null
+++ b/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;
+}