1 /* 2 * Copyright (C) 2008 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef _PAGEMAP_PAGEMAP_H 18 #define _PAGEMAP_PAGEMAP_H 19 20 #include <stdint.h> 21 #include <stdio.h> 22 #include <sys/types.h> 23 24 typedef struct pm_memusage pm_memusage_t; 25 26 /* Holds the various metrics for memory usage of a process or a mapping. */ 27 struct pm_memusage { 28 size_t vss; 29 size_t rss; 30 size_t pss; 31 size_t uss; 32 size_t swap; 33 }; 34 35 /* Clears a memusage. */ 36 void pm_memusage_zero(pm_memusage_t *mu); 37 /* Adds one memusage (a) to another (b). */ 38 void pm_memusage_add(pm_memusage_t *a, pm_memusage_t *b); 39 40 typedef struct pm_kernel pm_kernel_t; 41 typedef struct pm_process pm_process_t; 42 typedef struct pm_map pm_map_t; 43 44 /* pm_kernel_t holds the state necessary to interface to the kernel's pagemap 45 * system on a global level. */ 46 struct pm_kernel { 47 int kpagecount_fd; 48 int kpageflags_fd; 49 50 int pagesize; 51 }; 52 53 /* pm_process_t holds the state necessary to interface to a particular process' 54 * pagemap. */ 55 struct pm_process { 56 pm_kernel_t *ker; 57 58 pid_t pid; 59 60 pm_map_t **maps; 61 int num_maps; 62 63 int pagemap_fd; 64 }; 65 66 /* pm_map_t holds the state necessary to access information about a particular 67 * mapping in a particular process. */ 68 struct pm_map { 69 pm_process_t *proc; 70 71 unsigned long start; 72 unsigned long end; 73 unsigned long offset; 74 int flags; 75 76 char *name; 77 }; 78 79 /* Create a pm_kernel_t. */ 80 int pm_kernel_create(pm_kernel_t **ker_out); 81 82 #define pm_kernel_pagesize(ker) ((ker)->pagesize) 83 84 /* Get a list of probably-existing PIDs (returned through *pids_out). 85 * Length of the array (in sizeof(pid_t) units) is returned through *len. 86 * The array should be freed by the caller. */ 87 int pm_kernel_pids(pm_kernel_t *ker, pid_t **pids_out, size_t *len); 88 89 /* Get the map count (from /proc/kpagecount) of a physical frame. 90 * The count is returned through *count_out. */ 91 int pm_kernel_count(pm_kernel_t *ker, unsigned long pfn, uint64_t *count_out); 92 93 /* Get the page flags (from /proc/kpageflags) of a physical frame. 94 * The count is returned through *flags_out. */ 95 int pm_kernel_flags(pm_kernel_t *ker, unsigned long pfn, uint64_t *flags_out); 96 97 #define PM_PAGE_LOCKED (1 << 0) 98 #define PM_PAGE_ERROR (1 << 1) 99 #define PM_PAGE_REFERENCED (1 << 2) 100 #define PM_PAGE_UPTODATE (1 << 3) 101 #define PM_PAGE_DIRTY (1 << 4) 102 #define PM_PAGE_LRU (1 << 5) 103 #define PM_PAGE_ACTIVE (1 << 6) 104 #define PM_PAGE_SLAB (1 << 7) 105 #define PM_PAGE_WRITEBACK (1 << 8) 106 #define PM_PAGE_RECLAIM (1 << 9) 107 #define PM_PAGE_BUDDY (1 << 10) 108 109 /* for kernels >= 2.6.31 */ 110 #define PM_PAGE_MMAP (1 << 11) 111 #define PM_PAGE_ANON (1 << 12) 112 #define PM_PAGE_SWAPCACHE (1 << 13) 113 #define PM_PAGE_SWAPBACKED (1 << 14) 114 #define PM_PAGE_COMPOUND_HEAD (1 << 15) 115 #define PM_PAGE_COMPOUND_TAIL (1 << 16) 116 #define PM_PAGE_HUGE (1 << 17) 117 #define PM_PAGE_UNEVICTABLE (1 << 18) 118 #define PM_PAGE_HWPOISON (1 << 19) 119 #define PM_PAGE_NOPAGE (1 << 20) 120 121 /* for kernels >= 2.6.32 */ 122 #define PM_PAGE_KSM (1 << 21) 123 124 /* for kernels >= 3.4 */ 125 #define PM_PAGE_THP (1 << 22) 126 127 /* Destroy a pm_kernel_t. */ 128 int pm_kernel_destroy(pm_kernel_t *ker); 129 130 /* Get the PID of a pm_process_t. */ 131 #define pm_process_pid(proc) ((proc)->pid) 132 133 /* Create a pm_process_t and returns it through *proc_out. 134 * Takes a pm_kernel_t, and the PID of the process. */ 135 int pm_process_create(pm_kernel_t *ker, pid_t pid, pm_process_t **proc_out); 136 137 /* Get the total memory usage of a process and store in *usage_out. */ 138 int pm_process_usage(pm_process_t *proc, pm_memusage_t *usage_out); 139 140 /* Get the total memory usage of a process and store in *usage_out, only 141 * counting pages with specified flags. */ 142 int pm_process_usage_flags(pm_process_t *proc, pm_memusage_t *usage_out, 143 uint64_t flags_mask, uint64_t required_flags); 144 145 /* Get the working set of a process (if ws_out != NULL), and reset it 146 * (if reset != 0). */ 147 int pm_process_workingset(pm_process_t *proc, pm_memusage_t *ws_out, int reset); 148 149 /* Get the PFNs corresponding to a range of virtual addresses. 150 * The array of PFNs is returned through *range_out, and the caller has the 151 * responsibility to free it. */ 152 int pm_process_pagemap_range(pm_process_t *proc, 153 unsigned long low, unsigned long hi, 154 uint64_t **range_out, size_t *len); 155 156 #define _BITS(x, offset, bits) (((x) >> offset) & ((1LL << (bits)) - 1)) 157 158 #define PM_PAGEMAP_PRESENT(x) (_BITS(x, 63, 1)) 159 #define PM_PAGEMAP_SWAPPED(x) (_BITS(x, 62, 1)) 160 #define PM_PAGEMAP_SHIFT(x) (_BITS(x, 55, 6)) 161 #define PM_PAGEMAP_PFN(x) (_BITS(x, 0, 55)) 162 #define PM_PAGEMAP_SWAP_OFFSET(x) (_BITS(x, 5, 50)) 163 #define PM_PAGEMAP_SWAP_TYPE(x) (_BITS(x, 0, 5)) 164 165 /* Get the maps in the virtual address space of this process. 166 * Returns an array of pointers to pm_map_t through *maps. 167 * The array should be freed by the caller, but the maps should not be 168 * modified or destroyed. */ 169 int pm_process_maps(pm_process_t *proc, pm_map_t ***maps_out, size_t *len); 170 171 /* Destroy a pm_process_t. */ 172 int pm_process_destroy(pm_process_t *proc); 173 174 /* Get the name, flags, start/end address, or offset of a map. */ 175 #define pm_map_name(map) ((map)->name) 176 #define pm_map_flags(map) ((map)->flags) 177 #define PM_MAP_READ 1 178 #define PM_MAP_WRITE 2 179 #define PM_MAP_EXEC 4 180 #define PM_MAP_PERMISSIONS (PM_MAP_READ | PM_MAP_WRITE | PM_MAP_EXEC) 181 #define pm_map_start(map) ((map)->start) 182 #define pm_map_end(map) ((map)->end) 183 #define pm_map_offset(map) ((map)->offset) 184 185 /* Get the PFNs of the pages in the virtual address space of this map. 186 * Array of PFNs is returned through *pagemap_out, and should be freed by the 187 * caller. */ 188 int pm_map_pagemap(pm_map_t *map, uint64_t **pagemap_out, size_t *len); 189 190 /* Get the memory usage of this map alone. */ 191 int pm_map_usage(pm_map_t *map, pm_memusage_t *usage_out); 192 193 /* Get the memory usage of this map alone, only counting pages with specified 194 * flags. */ 195 int pm_map_usage_flags(pm_map_t *map, pm_memusage_t *usage_out, 196 uint64_t flags_mask, uint64_t required_flags); 197 198 /* Get the working set of this map alone. */ 199 int pm_map_workingset(pm_map_t *map, pm_memusage_t *ws_out); 200 201 #endif 202