• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(pm_map_t * map,pm_memusage_t * usage_out)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 
pm_map_workingset(pm_map_t * map,pm_memusage_t * ws_out)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         error = pm_kernel_flags(map->proc->ker, PM_PAGEMAP_PFN(pagemap[i]),
87                                 &flags);
88         if (error) goto out;
89 
90         if (!(flags & PM_PAGE_REFERENCED))
91             continue;
92 
93         error = pm_kernel_count(map->proc->ker, PM_PAGEMAP_PFN(pagemap[i]),
94                                 &count);
95         if (error) goto out;
96 
97         ws.vss += map->proc->ker->pagesize;
98         if( PM_PAGEMAP_SWAPPED(pagemap[i]) ) continue;
99         ws.rss += (count >= 1) ? (map->proc->ker->pagesize) : (0);
100         ws.pss += (count >= 1) ? (map->proc->ker->pagesize / count) : (0);
101         ws.uss += (count == 1) ? (map->proc->ker->pagesize) : (0);
102     }
103 
104     memcpy(ws_out, &ws, sizeof(ws));
105 
106     error = 0;
107 
108 out:
109     free(pagemap);
110 
111     return 0;
112 }
113 
pm_map_destroy(pm_map_t * map)114 int pm_map_destroy(pm_map_t *map) {
115     if (!map)
116         return -1;
117 
118     free(map->name);
119     free(map);
120 
121     return 0;
122 }
123