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 #include <stdlib.h> 18 #include <string.h> 19 20 #include <pagemap/pagemap.h> 21 22 int pm_map_pagemap(pm_map_t *map, uint64_t **pagemap_out, size_t *len) { 23 if (!map) 24 return -1; 25 26 return pm_process_pagemap_range(map->proc, map->start, map->end, 27 pagemap_out, len); 28 } 29 30 int pm_map_usage(pm_map_t *map, pm_memusage_t *usage_out) { 31 uint64_t *pagemap; 32 size_t len, i; 33 uint64_t count; 34 pm_memusage_t usage; 35 int error; 36 37 if (!map || !usage_out) 38 return -1; 39 40 error = pm_map_pagemap(map, &pagemap, &len); 41 if (error) return error; 42 43 pm_memusage_zero(&usage); 44 45 for (i = 0; i < len; i++) { 46 if (!PM_PAGEMAP_PRESENT(pagemap[i]) || 47 PM_PAGEMAP_SWAPPED(pagemap[i])) 48 continue; 49 50 error = pm_kernel_count(map->proc->ker, PM_PAGEMAP_PFN(pagemap[i]), 51 &count); 52 if (error) goto out; 53 54 usage.vss += map->proc->ker->pagesize; 55 usage.rss += (count >= 1) ? (map->proc->ker->pagesize) : (0); 56 usage.pss += (count >= 1) ? (map->proc->ker->pagesize / count) : (0); 57 usage.uss += (count == 1) ? (map->proc->ker->pagesize) : (0); 58 } 59 60 memcpy(usage_out, &usage, sizeof(usage)); 61 62 error = 0; 63 64 out: 65 free(pagemap); 66 67 return error; 68 } 69 70 int pm_map_workingset(pm_map_t *map, pm_memusage_t *ws_out) { 71 uint64_t *pagemap; 72 size_t len, i; 73 uint64_t count, flags; 74 pm_memusage_t ws; 75 int error; 76 77 if (!map || !ws_out) 78 return -1; 79 80 error = pm_map_pagemap(map, &pagemap, &len); 81 if (error) return error; 82 83 pm_memusage_zero(&ws); 84 85 for (i = 0; i < len; i++) { 86 if (!PM_PAGEMAP_PRESENT(pagemap[i]) || 87 PM_PAGEMAP_SWAPPED(pagemap[i])) 88 continue; 89 90 error = pm_kernel_flags(map->proc->ker, PM_PAGEMAP_PFN(pagemap[i]), 91 &flags); 92 if (error) goto out; 93 94 if (!(flags & PM_PAGE_REFERENCED)) 95 continue; 96 97 error = pm_kernel_count(map->proc->ker, PM_PAGEMAP_PFN(pagemap[i]), 98 &count); 99 if (error) goto out; 100 101 ws.vss += map->proc->ker->pagesize; 102 ws.rss += (count >= 1) ? (map->proc->ker->pagesize) : (0); 103 ws.pss += (count >= 1) ? (map->proc->ker->pagesize / count) : (0); 104 ws.uss += (count == 1) ? (map->proc->ker->pagesize) : (0); 105 } 106 107 memcpy(ws_out, &ws, sizeof(ws)); 108 109 error = 0; 110 111 out: 112 free(pagemap); 113 114 return 0; 115 } 116 117 int pm_map_destroy(pm_map_t *map) { 118 if (!map) 119 return -1; 120 121 free(map->name); 122 free(map); 123 124 return 0; 125 } 126