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