2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
25 #include <pagemap/pagemap.h>
29 static int read_maps(pm_process_t *proc);
31 #define MAX_FILENAME 64
33 int pm_process_create(pm_kernel_t *ker, pid_t pid, pm_process_t **proc_out) {
35 char filename[MAX_FILENAME];
38 if (!ker || !proc_out)
41 proc = calloc(1, sizeof(*proc));
48 error = snprintf(filename, MAX_FILENAME, "/proc/%d/pagemap", pid);
49 if (error < 0 || error >= MAX_FILENAME) {
50 error = (error < 0) ? (errno) : (-1);
55 proc->pagemap_fd = open(filename, O_RDONLY);
56 if (proc->pagemap_fd < 0) {
62 error = read_maps(proc);
73 int pm_process_usage_flags(pm_process_t *proc, pm_memusage_t *usage_out,
74 uint64_t flags_mask, uint64_t required_flags)
76 pm_memusage_t usage, map_usage;
80 if (!proc || !usage_out)
83 pm_memusage_zero(&usage);
85 for (i = 0; i < proc->num_maps; i++) {
86 error = pm_map_usage_flags(proc->maps[i], &map_usage, flags_mask,
88 if (error) return error;
90 pm_memusage_add(&usage, &map_usage);
93 memcpy(usage_out, &usage, sizeof(pm_memusage_t));
99 int pm_process_usage(pm_process_t *proc, pm_memusage_t *usage_out) {
100 return pm_process_usage_flags(proc, usage_out, 0, 0);
103 int pm_process_pagemap_range(pm_process_t *proc,
104 uint64_t low, uint64_t high,
105 uint64_t **range_out, size_t *len) {
112 if (!proc || (low > high) || !range_out || !len)
121 firstpage = low / proc->ker->pagesize;
122 numpages = (high - low) / proc->ker->pagesize;
124 range = malloc(numpages * sizeof(uint64_t));
128 off = lseek64(proc->pagemap_fd, firstpage * sizeof(uint64_t), SEEK_SET);
129 if (off == (off_t)-1) {
134 error = read(proc->pagemap_fd, (char*)range, numpages * sizeof(uint64_t));
136 /* EOF, mapping is not in userspace mapping range (probably vectors) */
141 } else if (error < 0 || (error > 0 && error < (int)(numpages * sizeof(uint64_t)))) {
142 error = (error < 0) ? errno : -1;
153 int pm_process_maps(pm_process_t *proc, pm_map_t ***maps_out, size_t *len) {
156 if (!proc || !maps_out || !len)
159 if (proc->num_maps) {
160 maps = malloc(proc->num_maps * sizeof(pm_map_t*));
164 memcpy(maps, proc->maps, proc->num_maps * sizeof(pm_map_t*));
170 *len = proc->num_maps;
175 int pm_process_workingset(pm_process_t *proc,
176 pm_memusage_t *ws_out, int reset) {
177 pm_memusage_t ws, map_ws;
178 char filename[MAX_FILENAME];
187 pm_memusage_zero(&ws);
188 for (i = 0; i < proc->num_maps; i++) {
189 error = pm_map_workingset(proc->maps[i], &map_ws);
190 if (error) return error;
192 pm_memusage_add(&ws, &map_ws);
195 memcpy(ws_out, &ws, sizeof(ws));
199 error = snprintf(filename, MAX_FILENAME, "/proc/%d/clear_refs",
201 if (error < 0 || error >= MAX_FILENAME) {
202 return (error < 0) ? (errno) : (-1);
205 fd = open(filename, O_WRONLY);
209 write(fd, "1\n", strlen("1\n"));
217 int pm_process_destroy(pm_process_t *proc) {
223 for (i = 0; i < proc->num_maps; i++) {
224 pm_map_destroy(proc->maps[i]);
227 close(proc->pagemap_fd);
233 #define INITIAL_MAPS 10
234 #define MAX_LINE 1024
244 static int read_maps(pm_process_t *proc) {
245 char filename[MAX_FILENAME];
246 char line[MAX_LINE], name[MAX_LINE], perms[MAX_PERMS];
248 pm_map_t *map, **maps, **new_maps;
249 int maps_count, maps_size;
255 maps = calloc(INITIAL_MAPS, sizeof(pm_map_t*));
258 maps_count = 0; maps_size = INITIAL_MAPS;
260 error = snprintf(filename, MAX_FILENAME, "/proc/%d/maps", proc->pid);
261 if (error < 0 || error >= MAX_FILENAME) {
263 return (error < 0) ? (errno) : (-1);
266 maps_f = fopen(filename, "r");
272 while (fgets(line, MAX_LINE, maps_f)) {
273 if (maps_count >= maps_size) {
274 new_maps = realloc(maps, 2 * maps_size * sizeof(pm_map_t*));
285 maps[maps_count] = map = calloc(1, sizeof(*map));
290 sscanf(line, "%" SCNx64 "-%" SCNx64 " %s %" SCNx64 " %*s %*d %" S(MAX_LINE) "s",
291 &map->start, &map->end, perms, &map->offset, name);
293 map->name = malloc(strlen(name) + 1);
296 for (; maps_count > 0; maps_count--)
297 pm_map_destroy(maps[maps_count]);
302 strcpy(map->name, name);
303 if (perms[0] == 'r') map->flags |= PM_MAP_READ;
304 if (perms[1] == 'w') map->flags |= PM_MAP_WRITE;
305 if (perms[2] == 'x') map->flags |= PM_MAP_EXEC;
312 new_maps = realloc(maps, maps_count * sizeof(pm_map_t*));
313 if (maps_count && !new_maps) {
319 proc->maps = new_maps;
320 proc->num_maps = maps_count;