2 * Copyright 2017 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>
21 #include <sys/types.h>
28 #include <system_error>
30 #include <boost/regex.hpp>
32 #include <folly/Conv.h>
33 #include <folly/CppAttributes.h>
34 #include <folly/Format.h>
35 #include <folly/Range.h>
36 #include <folly/String.h>
38 #include <folly/gen/Base.h>
39 #include <folly/gen/File.h>
40 #include <folly/gen/String.h>
46 // Get the default huge page size
47 size_t getDefaultHugePageSize() {
48 // We need to parse /proc/meminfo
49 static const boost::regex regex(R"!(Hugepagesize:\s*(\d+)\s*kB)!");
53 bool error = gen::byLine("/proc/meminfo") |
54 [&] (StringPiece line) -> bool {
55 if (boost::regex_match(line.begin(), line.end(), match, regex)) {
57 line.begin() + match.position(1), size_t(match.length(1)));
58 pageSize = to<size_t>(numStr) * 1024; // in KiB
65 throw std::runtime_error("Can't find default huge page size");
70 // Get raw huge page sizes (without mount points, they'll be filled later)
71 HugePageSizeVec readRawHugePageSizes() {
72 // We need to parse file names from /sys/kernel/mm/hugepages
73 static const boost::regex regex(R"!(hugepages-(\d+)kB)!");
76 fs::path path("/sys/kernel/mm/hugepages");
77 for (fs::directory_iterator it(path); it != fs::directory_iterator(); ++it) {
78 std::string filename(it->path().filename().string());
79 if (boost::regex_match(filename, match, regex)) {
81 filename.data() + match.position(1), size_t(match.length(1)));
82 vec.emplace_back(to<size_t>(numStr) * 1024);
88 // Parse the value of a pagesize mount option
89 // Format: number, optional K/M/G/T suffix, trailing junk allowed
90 size_t parsePageSizeValue(StringPiece value) {
91 static const boost::regex regex(R"!((\d+)([kmgt])?.*)!", boost::regex::icase);
93 if (!boost::regex_match(value.begin(), value.end(), match, regex)) {
94 throw std::runtime_error("Invalid pagesize option");
97 if (match.length(2) != 0) {
98 c = char(tolower(value[size_t(match.position(2))]));
100 StringPiece numStr(value.data() + match.position(1), size_t(match.length(1)));
101 size_t size = to<size_t>(numStr);
103 case 't': size *= 1024; FOLLY_FALLTHROUGH;
104 case 'g': size *= 1024; FOLLY_FALLTHROUGH;
105 case 'm': size *= 1024; FOLLY_FALLTHROUGH;
106 case 'k': size *= 1024;
112 * Get list of supported huge page sizes and their mount points, if
113 * hugetlbfs file systems are mounted for those sizes.
115 HugePageSizeVec readHugePageSizes() {
116 HugePageSizeVec sizeVec = readRawHugePageSizes();
117 if (sizeVec.empty()) {
118 return sizeVec; // nothing to do
120 std::sort(sizeVec.begin(), sizeVec.end());
122 size_t defaultHugePageSize = getDefaultHugePageSize();
124 struct PageSizeLess {
125 bool operator()(const HugePageSize& a, size_t b) const {
128 bool operator()(size_t a, const HugePageSize& b) const {
133 // Read and parse /proc/mounts
134 std::vector<StringPiece> parts;
135 std::vector<StringPiece> options;
137 gen::byLine("/proc/mounts") | gen::eachAs<StringPiece>() |
138 [&](StringPiece line) {
140 split(" ", line, parts);
141 // device path fstype options uid gid
142 if (parts.size() != 6) {
143 throw std::runtime_error("Invalid /proc/mounts line");
145 if (parts[2] != "hugetlbfs") {
146 return; // we only care about hugetlbfs
150 split(",", parts[3], options);
151 size_t pageSize = defaultHugePageSize;
152 // Search for the "pagesize" option, which must have a value
153 for (auto& option : options) {
155 const char* p = static_cast<const char*>(
156 memchr(option.data(), '=', option.size()));
160 if (StringPiece(option.data(), p) != "pagesize") {
163 pageSize = parsePageSizeValue(StringPiece(p + 1, option.end()));
167 auto pos = std::lower_bound(sizeVec.begin(), sizeVec.end(), pageSize,
169 if (pos == sizeVec.end() || pos->size != pageSize) {
170 throw std::runtime_error("Mount page size not found");
172 if (!pos->mountPoint.empty()) {
173 // Only one mount point per page size is allowed
178 fs::path path(parts[1].begin(), parts[1].end());
180 const int ret = stat(path.string().c_str(), &st);
181 if (ret == -1 && errno == ENOENT) {
184 checkUnixError(ret, "stat hugepage mountpoint failed");
185 pos->mountPoint = fs::canonical(path);
186 pos->device = st.st_dev;
194 const HugePageSizeVec& getHugePageSizes() {
195 static HugePageSizeVec sizes = readHugePageSizes();
199 const HugePageSize* getHugePageSize(size_t size) {
200 // Linear search is just fine.
201 for (auto& p : getHugePageSizes()) {
202 if (p.mountPoint.empty()) {
205 if (size == 0 || size == p.size) {
212 const HugePageSize* getHugePageSizeForDevice(dev_t device) {
213 // Linear search is just fine.
214 for (auto& p : getHugePageSizes()) {
215 if (p.mountPoint.empty()) {
218 if (device == p.device) {