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