blob: 0a26c08189541e9e9284be43ec274325700c5aba [file] [log] [blame]
Sandeep Patil54d87212018-08-29 17:10:47 -07001/*
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
26using unique_fd = ::android::base::unique_fd;
27
28namespace android {
29namespace meminfo {
30
31static inline off64_t pfn_to_idle_bitmap_offset(uint64_t pfn) {
32 return static_cast<off64_t>((pfn >> 6) << 3);
33}
34
35uint64_t pagesize(void) {
36 static uint64_t pagesize = sysconf(_SC_PAGE_SIZE);
37 return pagesize;
38}
39
40bool 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
77bool 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
91bool 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
105int 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
116int 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
129int 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
Sandeep Patilcbc8f122019-01-21 16:47:24 -0800141// Public methods
142bool page_present(uint64_t pagemap_val) {
143 return PAGE_PRESENT(pagemap_val);
144}
145
146bool page_swapped(uint64_t pagemap_val) {
147 return PAGE_SWAPPED(pagemap_val);
148}
149
150uint64_t page_pfn(uint64_t pagemap_val) {
151 return PAGE_PFN(pagemap_val);
152}
153
Sandeep Patil54d87212018-08-29 17:10:47 -0700154} // namespace meminfo
155} // namespace android