1 /*
2 * Copyright (C) 2018 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 <unistd.h>
20
21 #include <android-base/logging.h>
22 #include <android-base/unique_fd.h>
23
24 #include "meminfo_private.h"
25
26 using unique_fd = ::android::base::unique_fd;
27
28 namespace android {
29 namespace meminfo {
30
pfn_to_idle_bitmap_offset(uint64_t pfn)31 static inline off64_t pfn_to_idle_bitmap_offset(uint64_t pfn) {
32 return static_cast<off64_t>((pfn >> 6) << 3);
33 }
34
pagesize(void)35 uint64_t pagesize(void) {
36 static uint64_t pagesize = sysconf(_SC_PAGE_SIZE);
37 return pagesize;
38 }
39
InitPageAcct(bool pageidle_enable)40 bool PageAcct::InitPageAcct(bool pageidle_enable) {
41 if (pageidle_enable && !PageAcct::KernelHasPageIdle()) {
42 LOG(ERROR) << "Idle page tracking is not supported by the kernel";
43 return false;
44 }
45
46 if (kpagecount_fd_ < 0) {
47 unique_fd count_fd(TEMP_FAILURE_RETRY(open("/proc/kpagecount", O_RDONLY | O_CLOEXEC)));
48 if (count_fd < 0) {
49 PLOG(ERROR) << "Failed to open /proc/kpagecount";
50 return false;
51 }
52 kpagecount_fd_ = std::move(count_fd);
53 }
54
55 if (kpageflags_fd_ < 0) {
56 unique_fd flags_fd(TEMP_FAILURE_RETRY(open("/proc/kpageflags", O_RDONLY | O_CLOEXEC)));
57 if (flags_fd < 0) {
58 PLOG(ERROR) << "Failed to open /proc/kpageflags";
59 return false;
60 }
61 kpageflags_fd_ = std::move(flags_fd);
62 }
63
64 if (pageidle_enable && pageidle_fd_ < 0) {
65 unique_fd idle_fd(
66 TEMP_FAILURE_RETRY(open("/sys/kernel/mm/page_idle/bitmap", O_RDWR | O_CLOEXEC)));
67 if (idle_fd < 0) {
68 PLOG(ERROR) << "Failed to open page idle bitmap";
69 return false;
70 }
71 pageidle_fd_ = std::move(idle_fd);
72 }
73
74 return true;
75 }
76
PageFlags(uint64_t pfn,uint64_t * flags)77 bool PageAcct::PageFlags(uint64_t pfn, uint64_t* flags) {
78 if (!flags) return false;
79
80 if (kpageflags_fd_ < 0) {
81 if (!InitPageAcct()) return false;
82 }
83
84 if (pread64(kpageflags_fd_, flags, sizeof(uint64_t), pfn * sizeof(uint64_t)) < 0) {
85 PLOG(ERROR) << "Failed to read page flags for page " << pfn;
86 return false;
87 }
88 return true;
89 }
90
PageMapCount(uint64_t pfn,uint64_t * mapcount)91 bool PageAcct::PageMapCount(uint64_t pfn, uint64_t* mapcount) {
92 if (!mapcount) return false;
93
94 if (kpagecount_fd_ < 0) {
95 if (!InitPageAcct()) return false;
96 }
97
98 if (pread64(kpagecount_fd_, mapcount, sizeof(uint64_t), pfn * sizeof(uint64_t)) < 0) {
99 PLOG(ERROR) << "Failed to read map count for page " << pfn;
100 return false;
101 }
102 return true;
103 }
104
IsPageIdle(uint64_t pfn)105 int PageAcct::IsPageIdle(uint64_t pfn) {
106 if (pageidle_fd_ < 0) {
107 if (!InitPageAcct(true)) return -EOPNOTSUPP;
108 }
109
110 int idle_status = MarkPageIdle(pfn);
111 if (idle_status) return idle_status;
112
113 return GetPageIdle(pfn);
114 }
115
MarkPageIdle(uint64_t pfn) const116 int PageAcct::MarkPageIdle(uint64_t pfn) const {
117 off64_t offset = pfn_to_idle_bitmap_offset(pfn);
118 // set the bit corresponding to page frame
119 uint64_t idle_bits = 1ULL << (pfn % 64);
120
121 if (pwrite64(pageidle_fd_, &idle_bits, sizeof(uint64_t), offset) < 0) {
122 PLOG(ERROR) << "Failed to write page idle bitmap for page " << pfn;
123 return -errno;
124 }
125
126 return 0;
127 }
128
GetPageIdle(uint64_t pfn) const129 int PageAcct::GetPageIdle(uint64_t pfn) const {
130 off64_t offset = pfn_to_idle_bitmap_offset(pfn);
131 uint64_t idle_bits;
132
133 if (pread64(pageidle_fd_, &idle_bits, sizeof(uint64_t), offset) < 0) {
134 PLOG(ERROR) << "Failed to read page idle bitmap for page " << pfn;
135 return -errno;
136 }
137
138 return !!(idle_bits & (1ULL << (pfn % 64)));
139 }
140
141 // Public methods
page_present(uint64_t pagemap_val)142 bool page_present(uint64_t pagemap_val) {
143 return PAGE_PRESENT(pagemap_val);
144 }
145
page_swapped(uint64_t pagemap_val)146 bool page_swapped(uint64_t pagemap_val) {
147 return PAGE_SWAPPED(pagemap_val);
148 }
149
page_pfn(uint64_t pagemap_val)150 uint64_t page_pfn(uint64_t pagemap_val) {
151 return PAGE_PFN(pagemap_val);
152 }
153
154 } // namespace meminfo
155 } // namespace android
156