blob: 6d750ea81827eec1ed00deb4625856d227eabc06 [file] [log] [blame]
Christopher Ferrise2960912014-03-07 19:42:19 -08001/*
2 * Copyright (C) 2014 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
Christopher Ferrise2960912014-03-07 19:42:19 -080017#include <inttypes.h>
Christopher Ferris2c43cff2015-03-26 19:18:36 -070018#include <stdint.h>
Christopher Ferrise2960912014-03-07 19:42:19 -080019#include <stdio.h>
20#include <stdlib.h>
Christopher Ferrise2960912014-03-07 19:42:19 -080021#include <sys/types.h>
22#include <unistd.h>
23#include <fcntl.h>
24
25// This is an extremely simplified version of libpagemap.
26
Chih-Hung Hsieh67867db2016-05-18 15:53:15 -070027#define _BITS(x, offset, bits) (((x) >> (offset)) & ((1LL << (bits)) - 1))
Christopher Ferrise2960912014-03-07 19:42:19 -080028
29#define PAGEMAP_PRESENT(x) (_BITS(x, 63, 1))
30#define PAGEMAP_SWAPPED(x) (_BITS(x, 62, 1))
31#define PAGEMAP_SHIFT(x) (_BITS(x, 55, 6))
32#define PAGEMAP_PFN(x) (_BITS(x, 0, 55))
33#define PAGEMAP_SWAP_OFFSET(x) (_BITS(x, 5, 50))
34#define PAGEMAP_SWAP_TYPE(x) (_BITS(x, 0, 5))
35
Chih-Hung Hsiehec2ff8c2016-04-18 14:45:46 -070036static bool ReadData(int fd, off_t place, uint64_t *data) {
Christopher Ferrise2960912014-03-07 19:42:19 -080037 if (lseek(fd, place * sizeof(uint64_t), SEEK_SET) < 0) {
38 return false;
39 }
40 if (read(fd, (void*)data, sizeof(uint64_t)) != (ssize_t)sizeof(uint64_t)) {
41 return false;
42 }
43 return true;
44}
45
46size_t GetPssBytes() {
47 FILE* maps = fopen("/proc/self/maps", "r");
Christopher Ferris2c43cff2015-03-26 19:18:36 -070048 if (maps == nullptr) {
49 return 0;
50 }
Christopher Ferrise2960912014-03-07 19:42:19 -080051
52 int pagecount_fd = open("/proc/kpagecount", O_RDONLY);
Christopher Ferris2c43cff2015-03-26 19:18:36 -070053 if (pagecount_fd == -1) {
54 fclose(maps);
55 return 0;
56 }
Christopher Ferrise2960912014-03-07 19:42:19 -080057
58 int pagemap_fd = open("/proc/self/pagemap", O_RDONLY);
Christopher Ferris2c43cff2015-03-26 19:18:36 -070059 if (pagemap_fd == -1) {
60 fclose(maps);
61 close(pagecount_fd);
62 return 0;
63 }
Christopher Ferrise2960912014-03-07 19:42:19 -080064
65 char line[4096];
66 size_t total_pss = 0;
67 int pagesize = getpagesize();
68 while (fgets(line, sizeof(line), maps)) {
69 uintptr_t start, end;
70 if (sscanf(line, "%" SCNxPTR "-%" SCNxPTR " ", &start, &end) != 2) {
71 total_pss = 0;
72 break;
73 }
Chih-Hung Hsiehec2ff8c2016-04-18 14:45:46 -070074 for (off_t page = static_cast<off_t>(start/pagesize);
75 page < static_cast<off_t>(end/pagesize); page++) {
Christopher Ferrise2960912014-03-07 19:42:19 -080076 uint64_t data;
77 if (ReadData(pagemap_fd, page, &data)) {
78 if (PAGEMAP_PRESENT(data) && !PAGEMAP_SWAPPED(data)) {
79 uint64_t count;
Chih-Hung Hsiehec2ff8c2016-04-18 14:45:46 -070080 if (ReadData(pagecount_fd, static_cast<off_t>(PAGEMAP_PFN(data)), &count)) {
Christopher Ferrise2960912014-03-07 19:42:19 -080081 total_pss += (count >= 1) ? pagesize / count : 0;
82 }
83 }
84 }
85 }
86 }
87
88 fclose(maps);
89
90 close(pagecount_fd);
91 close(pagemap_fd);
92
93 return total_pss;
94}