blob: 7c682f97b71c85a0fa0e7a95c1ca2ad2b40b7a19 [file] [log] [blame]
Rom Lemarchand266dde22013-07-08 15:22:10 -07001/*
2 * Copyright (C) 2013 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 <stdbool.h>
19#include <stdlib.h>
20#include <sys/types.h>
21#include <unistd.h>
22#include <string.h>
23#include <fcntl.h>
24#include <stdint.h>
25#include <getopt.h>
26
27#include <pagemap/pagemap.h>
28
29#define MAX_FILENAME 64
30
31#define GROWTH_FACTOR 10
32
33#define NO_PATTERN 0x100
34
Rom Lemarchand2bed1552013-07-08 21:50:24 -070035#define PR_SORTED 1
36#define PR_VERBOSE 2
37
Rom Lemarchand266dde22013-07-08 15:22:10 -070038static void usage(char *myname);
39static int getprocname(pid_t pid, char *buf, int len);
Rom Lemarchand2bed1552013-07-08 21:50:24 -070040static void print_ksm_pages(pm_map_t **maps, size_t num_maps, uint8_t pr_flags);
Rom Lemarchand266dde22013-07-08 15:22:10 -070041static bool is_pattern(uint8_t *data, size_t len);
Rom Lemarchand2bed1552013-07-08 21:50:24 -070042static int cmp_pages(const void *a, const void *b);
Rom Lemarchand266dde22013-07-08 15:22:10 -070043extern uint32_t hashword(const uint32_t *, size_t, int32_t);
44
45struct ksm_page {
46 uint32_t hash;
47 unsigned long *vaddr;
48 size_t vaddr_len, vaddr_size;
49 uint16_t pattern;
50};
51
52int main(int argc, char *argv[]) {
53 pm_kernel_t *ker;
54 pm_process_t *proc;
55 pid_t pid;
56 pm_map_t **maps;
57 size_t num_maps;
58 char cmdline[256]; // this must be within the range of int
59 int error;
60 int rc = EXIT_SUCCESS;
Rom Lemarchand2bed1552013-07-08 21:50:24 -070061 uint8_t pr_flags = 0;
Rom Lemarchand266dde22013-07-08 15:22:10 -070062
63 opterr = 0;
64 do {
Rom Lemarchand2bed1552013-07-08 21:50:24 -070065 int c = getopt(argc, argv, "hvs");
Rom Lemarchand266dde22013-07-08 15:22:10 -070066 if (c == -1)
67 break;
68
69 switch (c) {
Rom Lemarchand2bed1552013-07-08 21:50:24 -070070 case 's':
71 pr_flags |= PR_SORTED;
72 break;
Rom Lemarchand266dde22013-07-08 15:22:10 -070073 case 'v':
Rom Lemarchand2bed1552013-07-08 21:50:24 -070074 pr_flags |= PR_VERBOSE;
Rom Lemarchand266dde22013-07-08 15:22:10 -070075 break;
76 case 'h':
77 usage(argv[0]);
78 exit(EXIT_SUCCESS);
79 case '?':
80 fprintf(stderr, "unknown option: %c\n", optopt);
81 usage(argv[0]);
82 exit(EXIT_FAILURE);
83 }
84 } while (1);
85
86 if (optind != argc - 1) {
87 usage(argv[0]);
88 exit(EXIT_FAILURE);
89 }
90
91 pid = strtoul(argv[optind], NULL, 10);
92 if (pid == 0) {
93 fprintf(stderr, "Invalid PID\n");
94 exit(EXIT_FAILURE);
95 }
96
97 error = pm_kernel_create(&ker);
98 if (error) {
99 fprintf(stderr, "Error creating kernel interface -- "
100 "does this kernel have pagemap?\n");
101 exit(EXIT_FAILURE);
102 }
103
104 error = pm_process_create(ker, pid, &proc);
105 if (error) {
106 fprintf(stderr, "warning: could not create process interface for %d\n", pid);
107 exit(EXIT_FAILURE);
108 }
109
110 error = pm_process_maps(proc, &maps, &num_maps);
111 if (error) {
112 fprintf(stderr, "warning: could not read process map for %d\n", pid);
113 rc = EXIT_FAILURE;
114 goto destroy_proc;
115 }
116
117 if (getprocname(pid, cmdline, sizeof(cmdline)) < 0) {
118 cmdline[0] = '\0';
119 }
120 printf("%s (%u):\n", cmdline, pid);
121 printf("Warning: this tool only compares the KSM CRCs of pages, there is a chance of "
122 "collisions\n");
Rom Lemarchand2bed1552013-07-08 21:50:24 -0700123 print_ksm_pages(maps, num_maps, pr_flags);
Rom Lemarchand266dde22013-07-08 15:22:10 -0700124
125 free(maps);
126destroy_proc:
127 pm_process_destroy(proc);
128 return rc;
129}
130
Rom Lemarchand2bed1552013-07-08 21:50:24 -0700131static void print_ksm_pages(pm_map_t **maps, size_t num_maps, uint8_t pr_flags) {
Rom Lemarchand266dde22013-07-08 15:22:10 -0700132 size_t i, j, k;
133 size_t len;
134 uint64_t *pagemap;
135 size_t map_len;
136 uint64_t flags;
137 pm_kernel_t *ker;
138 int error;
139 unsigned long vaddr;
140 int fd;
141 off_t off;
142 char filename[MAX_FILENAME];
143 uint32_t *data;
144 uint32_t hash;
145 struct ksm_page *pages;
146 size_t pages_len, pages_size;
147
148 if (num_maps <= 0)
149 return;
150
151 ker = maps[0]->proc->ker;
152 error = snprintf(filename, MAX_FILENAME, "/proc/%d/mem", pm_process_pid(maps[0]->proc));
153 if (error < 0 || error >= MAX_FILENAME) {
154 return;
155 }
156
157 data = malloc(pm_kernel_pagesize(ker));
158 if (data == NULL) {
159 fprintf(stderr, "warning: not enough memory to malloc data buffer\n");
160 return;
161 }
162
163 fd = open(filename, O_RDONLY);
164 if (fd < 0) {
165 fprintf(stderr, "warning: could not open %s\n", filename);
166 goto err_open;
167 }
168
169 pages = NULL;
170 pages_size = 0;
171 pages_len = 0;
172
173 for (i = 0; i < num_maps; i++) {
174 error = pm_map_pagemap(maps[i], &pagemap, &map_len);
175 if (error) {
176 fprintf(stderr, "warning: could not read the pagemap of %d\n",
177 pm_process_pid(maps[i]->proc));
178 }
179 for (j = 0; j < map_len; j++) {
180 error = pm_kernel_flags(ker, pagemap[j], &flags);
181 if (error) {
182 fprintf(stderr, "warning: could not read flags for pfn at address 0x%016llx\n",
183 pagemap[i]);
184 continue;
185 }
186 if (!(flags & PM_PAGE_KSM)) {
187 continue;
188 }
189 vaddr = pm_map_start(maps[i]) + j * pm_kernel_pagesize(ker);
190 off = lseek(fd, vaddr, SEEK_SET);
191 if (off == (off_t)-1) {
192 fprintf(stderr, "warning: could not lseek to 0x%08lx\n", vaddr);
193 continue;
194 }
195 len = read(fd, data, pm_kernel_pagesize(ker));
196 if (len != pm_kernel_pagesize(ker)) {
197 fprintf(stderr, "warning: could not read page at 0x%08lx\n", vaddr);
198 continue;
199 }
200
201 hash = hashword(data, pm_kernel_pagesize(ker) / sizeof(*data), 17);
202
203 for (k = 0; k < pages_len; k++) {
204 if (pages[k].hash == hash) break;
205 }
206
207 if (k == pages_len) {
208 if (pages_len == pages_size) {
209 struct ksm_page *tmp = realloc(pages,
210 (pages_size + GROWTH_FACTOR) * sizeof(*pages));
211 if (tmp == NULL) {
212 fprintf(stderr, "warning: not enough memory to realloc pages struct\n");
213 free(pagemap);
214 goto err_realloc;
215 }
216 memset(&tmp[k], 0, sizeof(tmp[k]) * GROWTH_FACTOR);
217 pages = tmp;
218 pages_size += GROWTH_FACTOR;
219 }
220 pages[pages_len].hash = hash;
221 pages[pages_len].pattern = is_pattern((uint8_t *)data, pm_kernel_pagesize(ker)) ?
222 (data[0] & 0xFF) : NO_PATTERN;
223 pages_len++;
224 }
225
Rom Lemarchand2bed1552013-07-08 21:50:24 -0700226 if (pr_flags & PR_VERBOSE) {
Rom Lemarchand266dde22013-07-08 15:22:10 -0700227 if (pages[k].vaddr_len == pages[k].vaddr_size) {
228 unsigned long *tmp = realloc(pages[k].vaddr,
229 (pages[k].vaddr_size + GROWTH_FACTOR) * sizeof(*(pages[k].vaddr)));
230 if (tmp == NULL) {
231 fprintf(stderr, "warning: not enough memory to realloc vaddr array\n");
232 free(pagemap);
233 goto err_realloc;
234 }
235 memset(&tmp[pages[k].vaddr_len], 0, sizeof(tmp[pages[k].vaddr_len]) * GROWTH_FACTOR);
236 pages[k].vaddr = tmp;
237 pages[k].vaddr_size += GROWTH_FACTOR;
238 }
239 pages[k].vaddr[pages[k].vaddr_len] = vaddr;
240 }
241 pages[k].vaddr_len++;
242 }
243 free(pagemap);
244 }
245
Rom Lemarchand2bed1552013-07-08 21:50:24 -0700246 if (pr_flags & PR_SORTED) {
247 qsort(pages, pages_len, sizeof(*pages), cmp_pages);
248 }
249
Rom Lemarchand266dde22013-07-08 15:22:10 -0700250 for (i = 0; i < pages_len; i++) {
251 if (pages[i].pattern != NO_PATTERN) {
252 printf("0x%02x byte pattern: ", pages[i].pattern);
253 } else {
254 printf("KSM CRC 0x%08x:", pages[i].hash);
255 }
256 printf(" %4d page", pages[i].vaddr_len);
257 if (pages[i].vaddr_len > 1) {
258 printf("s");
259 }
260 printf("\n");
261
Rom Lemarchand2bed1552013-07-08 21:50:24 -0700262 if (pr_flags & PR_VERBOSE) {
Rom Lemarchand266dde22013-07-08 15:22:10 -0700263 j = 0;
264 while (j < pages[i].vaddr_len) {
265 printf(" ");
266 for (k = 0; k < 8 && j < pages[i].vaddr_len; k++, j++) {
267 printf(" 0x%08lx", pages[i].vaddr[j]);
268 }
269 printf("\n");
270 }
271 }
272 }
273
274err_realloc:
Rom Lemarchand2bed1552013-07-08 21:50:24 -0700275 if (pr_flags & PR_VERBOSE) {
Rom Lemarchand266dde22013-07-08 15:22:10 -0700276 for (i = 0; i < pages_len; i++) {
277 free(pages[i].vaddr);
278 }
279 }
280 free(pages);
281err_pages:
282 close(fd);
283err_open:
284 free(data);
285}
286
287static void usage(char *myname) {
Rom Lemarchand2bed1552013-07-08 21:50:24 -0700288 fprintf(stderr, "Usage: %s [-s | -v | -h ] <pid>\n"
289 " -s Sort pages by usage count.\n"
Rom Lemarchand266dde22013-07-08 15:22:10 -0700290 " -v Verbose: print virtual addresses.\n"
291 " -h Display this help screen.\n",
292 myname);
293}
294
Rom Lemarchand2bed1552013-07-08 21:50:24 -0700295static int cmp_pages(const void *a, const void *b) {
296 const struct ksm_page *pg_a = a;
297 const struct ksm_page *pg_b = b;
298
299 return pg_b->vaddr_len - pg_a->vaddr_len;
300}
301
Rom Lemarchand266dde22013-07-08 15:22:10 -0700302static bool is_pattern(uint8_t *data, size_t len) {
303 size_t i;
304 uint8_t first_byte = data[0];
305
306 for (i = 1; i < len; i++) {
307 if (first_byte != data[i]) return false;
308 }
309
310 return true;
311}
312
313/*
314 * Get the process name for a given PID. Inserts the process name into buffer
315 * buf of length len. The size of the buffer must be greater than zero to get
316 * any useful output.
317 *
318 * Note that fgets(3) only declares length as an int, so our buffer size is
319 * also declared as an int.
320 *
321 * Returns 0 on success, a positive value on partial success, and -1 on
322 * failure. Other interesting values:
323 * 1 on failure to create string to examine proc cmdline entry
324 * 2 on failure to open proc cmdline entry
325 * 3 on failure to read proc cmdline entry
326 */
327static int getprocname(pid_t pid, char *buf, int len) {
328 char *filename;
329 FILE *f;
330 int rc = 0;
331 static const char* unknown_cmdline = "<unknown>";
332
333 if (len <= 0) {
334 return -1;
335 }
336
337 if (asprintf(&filename, "/proc/%zd/cmdline", pid) < 0) {
338 rc = 1;
339 goto exit;
340 }
341
342 f = fopen(filename, "r");
343 if (f == NULL) {
344 rc = 2;
345 goto releasefilename;
346 }
347
348 if (fgets(buf, len, f) == NULL) {
349 rc = 3;
350 goto closefile;
351 }
352
353closefile:
354 (void) fclose(f);
355releasefilename:
356 free(filename);
357exit:
358 if (rc != 0) {
359 /*
360 * The process went away before we could read its process name. Try
361 * to give the user "<unknown>" here, but otherwise they get to look
362 * at a blank.
363 */
364 if (strlcpy(buf, unknown_cmdline, (size_t)len) >= (size_t)len) {
365 rc = 4;
366 }
367 }
368
369 return rc;
370}
371