OSDN Git Service

Merge commit 'remotes/korg/cupcake' into merge
[android-x86/system-extras.git] / libpagemap / pm_process.c
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 <errno.h>
18 #include <fcntl.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
23
24 #include <pagemap/pagemap.h>
25
26 #include "pm_map.h"
27
28 static int read_maps(pm_process_t *proc);
29
30 #define MAX_FILENAME 64
31
32 int pm_process_create(pm_kernel_t *ker, pid_t pid, pm_process_t **proc_out) {
33     pm_process_t *proc;
34     char filename[MAX_FILENAME];
35     int error;
36
37     if (!ker || !proc_out)
38         return -1;
39
40     proc = calloc(1, sizeof(*proc));
41     if (!proc)
42         return errno;
43
44     proc->ker = ker;
45     proc->pid = pid;
46
47     error = snprintf(filename, MAX_FILENAME, "/proc/%d/pagemap", pid);
48     if (error < 0 || error >= MAX_FILENAME) {
49         error = (error < 0) ? (errno) : (-1);
50         free(proc);
51         return error;
52     }
53
54     proc->pagemap_fd = open(filename, O_RDONLY);
55     if (proc->pagemap_fd < 0) {
56         error = errno;
57         free(proc);
58         return error;
59     }        
60
61     error = read_maps(proc);
62     if (error) {
63         free(proc);
64         return error;
65     }
66
67     *proc_out = proc;
68
69     return 0;
70 }
71
72 int pm_process_usage(pm_process_t *proc, pm_memusage_t *usage_out) {
73     pm_memusage_t usage, map_usage;
74     int error;
75     int i;
76
77     if (!proc || !usage_out)
78         return -1;
79
80     pm_memusage_zero(&usage);
81
82     for (i = 0; i < proc->num_maps; i++) {
83         error = pm_map_usage(proc->maps[i], &map_usage);
84         if (error) return error;
85
86         pm_memusage_add(&usage, &map_usage);
87     }
88
89     memcpy(usage_out, &usage, sizeof(pm_memusage_t));
90
91     return 0;
92 }
93
94 int pm_process_pagemap_range(pm_process_t *proc,
95                              unsigned long low, unsigned long high,
96                              uint64_t **range_out, size_t *len) {
97     int firstpage, numpages;
98     uint64_t *range;
99     off_t off;
100     int error;
101     ssize_t len_read;
102
103     if (!proc || (low >= high) || !range_out || !len)
104         return -1;
105
106     firstpage = low / proc->ker->pagesize;
107     numpages = (high - low) / proc->ker->pagesize;
108
109     range = malloc(numpages * sizeof(uint64_t));
110     if (!range)
111         return errno;
112
113     off = lseek(proc->pagemap_fd, firstpage * sizeof(uint64_t), SEEK_SET);
114     if (off == (off_t)-1) {
115         error = errno;
116         free(range);
117         return error;
118     }
119     len_read = read(proc->pagemap_fd, (char*)range, numpages * sizeof(uint64_t));
120     if (len_read < (ssize_t)numpages * sizeof(uint64_t)) {
121         error = (error < 0) ? errno : -1;
122         free(range);
123         return error;
124     }
125
126     *range_out = range;
127     *len = numpages;
128
129     return 0;
130 }
131
132 int pm_process_maps(pm_process_t *proc, pm_map_t ***maps_out, int *len) {
133     pm_map_t **maps;
134
135     if (!proc || !maps_out || !len)
136         return -1;
137
138     if (proc->num_maps) {
139         maps = malloc(proc->num_maps * sizeof(pm_map_t*));
140         if (!maps)
141             return errno;
142
143         memcpy(maps, proc->maps, proc->num_maps * sizeof(pm_map_t*));
144     
145         *maps_out = maps;
146     } else {
147         *maps_out = NULL;
148     }
149     *len = proc->num_maps;
150
151     return 0;
152 }
153
154 int pm_process_workingset(pm_process_t *proc,
155                           pm_memusage_t *ws_out, int reset) {
156     pm_memusage_t ws, map_ws;
157     char filename[MAX_FILENAME];
158     int fd;
159     int i, j;
160     int error;
161
162     if (!proc)
163         return -1;
164
165     if (ws_out) {
166         pm_memusage_zero(&ws);
167         for (i = 0; i < proc->num_maps; i++) {
168             error = pm_map_workingset(proc->maps[i], &map_ws);
169             if (error) return error;
170
171             pm_memusage_add(&ws, &map_ws);
172         }
173         
174         memcpy(ws_out, &ws, sizeof(ws));
175     }
176
177     if (reset) {
178         error = snprintf(filename, MAX_FILENAME, "/proc/%d/clear_refs",
179                          proc->pid);
180         if (error < 0 || error >= MAX_FILENAME) {
181             return (error < 0) ? (errno) : (-1);
182         }
183
184         fd = open(filename, O_WRONLY);
185         if (fd < 0)
186             return errno;
187
188         write(fd, "1\n", strlen("1\n"));
189
190         close(fd);
191     }
192
193     return 0;
194 }
195
196 int pm_process_destroy(pm_process_t *proc) {
197     if (!proc)
198         return -1;
199
200     free(proc->maps);
201     close(proc->pagemap_fd);
202     free(proc);
203
204     return 0;
205 }
206
207 #define INITIAL_MAPS 10
208 #define MAX_LINE 256
209 #define MAX_PERMS 5
210
211 /* 
212  * #define FOO 123
213  * S(FOO) => "123"
214  */
215 #define _S(n) #n
216 #define S(n) _S(n)
217
218 static int read_maps(pm_process_t *proc) {
219     char filename[MAX_FILENAME];
220     char line[MAX_LINE], name[MAX_LINE], perms[MAX_PERMS];
221     FILE *maps_f;
222     pm_map_t *map, **maps, **new_maps;
223     int maps_count, maps_size;
224     int error;
225        
226     if (!proc)
227         return -1;
228
229     maps = calloc(INITIAL_MAPS, sizeof(pm_map_t*));
230     if (!maps)
231         return errno;
232     maps_count = 0; maps_size = INITIAL_MAPS;
233
234     error = snprintf(filename, MAX_FILENAME, "/proc/%d/maps", proc->pid);
235     if (error < 0 || error >= MAX_FILENAME)
236         return (error < 0) ? (errno) : (-1);
237
238     maps_f = fopen(filename, "r");
239     if (!maps_f)
240         return errno;
241
242     while (fgets(line, MAX_LINE, maps_f)) {
243         if (maps_count >= maps_size) {
244             new_maps = realloc(maps, 2 * maps_size * sizeof(pm_map_t*));
245             if (!new_maps) {
246                 error = errno;
247                 free(maps);
248                 fclose(maps_f);
249                 return error;
250             }
251             maps = new_maps;
252             maps_size *= 2;
253         }
254
255         maps[maps_count] = map = calloc(1, sizeof(*map));
256
257         map->proc = proc;
258
259         sscanf(line, "%lx-%lx %s %lx %*s %*d %" S(MAX_LINE) "s",
260                &map->start, &map->end, perms, &map->offset, name);
261
262         map->name = malloc(strlen(name) + 1);
263         if (!map->name) {
264             error = errno;
265             for (; maps_count > 0; maps_count--)
266                 pm_map_destroy(maps[maps_count]);
267             free(maps);
268             return error;
269         }
270         strcpy(map->name, name);
271         if (perms[0] == 'r') map->flags |= PM_MAP_READ;
272         if (perms[1] == 'w') map->flags |= PM_MAP_WRITE;
273         if (perms[2] == 'x') map->flags |= PM_MAP_EXEC;
274
275         maps_count++;
276     }
277
278     fclose(maps_f);
279
280     new_maps = realloc(maps, maps_count * sizeof(pm_map_t*));
281     if (maps_count && !new_maps) {
282         error = errno;
283         free(maps);
284         return error;
285     }
286
287     proc->maps = new_maps;
288     proc->num_maps = maps_count;
289
290     return 0;
291 }