2 * Copyright 2016 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <folly/experimental/io/HugePages.h>
20 #include <sys/types.h>
28 #include <system_error>
30 #include <boost/noncopyable.hpp>
31 #include <boost/regex.hpp>
33 #include <glog/logging.h>
35 #include <folly/Conv.h>
36 #include <folly/Format.h>
37 #include <folly/Range.h>
38 #include <folly/ScopeGuard.h>
39 #include <folly/String.h>
41 #include <folly/gen/Base.h>
42 #include <folly/gen/File.h>
43 #include <folly/gen/String.h>
49 // Get the default huge page size
50 size_t getDefaultHugePageSize() {
51 // We need to parse /proc/meminfo
52 static const boost::regex regex(R"!(Hugepagesize:\s*(\d+)\s*kB)!");
56 bool error = gen::byLine("/proc/meminfo") |
57 [&] (StringPiece line) -> bool {
58 if (boost::regex_match(line.begin(), line.end(), match, regex)) {
59 StringPiece numStr(line.begin() + match.position(1), match.length(1));
60 pageSize = to<size_t>(numStr) * 1024; // in KiB
67 throw std::runtime_error("Can't find default huge page size");
72 // Get raw huge page sizes (without mount points, they'll be filled later)
73 HugePageSizeVec readRawHugePageSizes() {
74 // We need to parse file names from /sys/kernel/mm/hugepages
75 static const boost::regex regex(R"!(hugepages-(\d+)kB)!");
78 fs::path path("/sys/kernel/mm/hugepages");
79 for (fs::directory_iterator it(path); it != fs::directory_iterator(); ++it) {
80 std::string filename(it->path().filename().string());
81 if (boost::regex_match(filename, match, regex)) {
82 StringPiece numStr(filename.data() + match.position(1), match.length(1));
83 vec.emplace_back(to<size_t>(numStr) * 1024);
89 // Parse the value of a pagesize mount option
90 // Format: number, optional K/M/G/T suffix, trailing junk allowed
91 size_t parsePageSizeValue(StringPiece value) {
92 static const boost::regex regex(R"!((\d+)([kmgt])?.*)!", boost::regex::icase);
94 if (!boost::regex_match(value.begin(), value.end(), match, regex)) {
95 throw std::runtime_error("Invalid pagesize option");
98 if (match.length(2) != 0) {
99 c = tolower(value[match.position(2)]);
101 StringPiece numStr(value.data() + match.position(1), match.length(1));
102 size_t size = to<size_t>(numStr);
104 case 't': size *= 1024;
105 case 'g': size *= 1024;
106 case 'm': size *= 1024;
107 case 'k': size *= 1024;
113 * Get list of supported huge page sizes and their mount points, if
114 * hugetlbfs file systems are mounted for those sizes.
116 HugePageSizeVec readHugePageSizes() {
117 HugePageSizeVec sizeVec = readRawHugePageSizes();
118 if (sizeVec.empty()) {
119 return sizeVec; // nothing to do
121 std::sort(sizeVec.begin(), sizeVec.end());
123 size_t defaultHugePageSize = getDefaultHugePageSize();
125 struct PageSizeLess {
126 bool operator()(const HugePageSize& a, size_t b) const {
129 bool operator()(size_t a, const HugePageSize& b) const {
134 // Read and parse /proc/mounts
135 std::vector<StringPiece> parts;
136 std::vector<StringPiece> options;
138 gen::byLine("/proc/mounts") | gen::eachAs<StringPiece>() |
139 [&](StringPiece line) {
141 split(" ", line, parts);
142 // device path fstype options uid gid
143 if (parts.size() != 6) {
144 throw std::runtime_error("Invalid /proc/mounts line");
146 if (parts[2] != "hugetlbfs") {
147 return; // we only care about hugetlbfs
151 split(",", parts[3], options);
152 size_t pageSize = defaultHugePageSize;
153 // Search for the "pagesize" option, which must have a value
154 for (auto& option : options) {
156 const char* p = static_cast<const char*>(
157 memchr(option.data(), '=', option.size()));
161 if (StringPiece(option.data(), p) != "pagesize") {
164 pageSize = parsePageSizeValue(StringPiece(p + 1, option.end()));
168 auto pos = std::lower_bound(sizeVec.begin(), sizeVec.end(), pageSize,
170 if (pos == sizeVec.end() || pos->size != pageSize) {
171 throw std::runtime_error("Mount page size not found");
173 if (!pos->mountPoint.empty()) {
174 // Only one mount point per page size is allowed
179 fs::path path(parts[1].begin(), parts[1].end());
181 const int ret = stat(path.string().c_str(), &st);
182 if (ret == -1 && errno == ENOENT) {
185 checkUnixError(ret, "stat hugepage mountpoint failed");
186 pos->mountPoint = fs::canonical(path);
187 pos->device = st.st_dev;
195 const HugePageSizeVec& getHugePageSizes() {
196 static HugePageSizeVec sizes = readHugePageSizes();
200 const HugePageSize* getHugePageSize(size_t size) {
201 // Linear search is just fine.
202 for (auto& p : getHugePageSizes()) {
203 if (p.mountPoint.empty()) {
206 if (size == 0 || size == p.size) {
213 const HugePageSize* getHugePageSizeForDevice(dev_t device) {
214 // Linear search is just fine.
215 for (auto& p : getHugePageSizes()) {
216 if (p.mountPoint.empty()) {
219 if (device == p.device) {