d59d587453e89bb68c48a49369e6a6cc88341d64
[folly.git] / folly / experimental / io / HugePages.cpp
1 /*
2  * Copyright 2017 Facebook, Inc.
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 <folly/experimental/io/HugePages.h>
18
19 #include <fcntl.h>
20 #include <sys/stat.h>
21 #include <sys/types.h>
22
23 #include <cctype>
24 #include <cstring>
25
26 #include <algorithm>
27 #include <stdexcept>
28 #include <system_error>
29
30 #include <boost/regex.hpp>
31
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>
37
38 #include <folly/gen/Base.h>
39 #include <folly/gen/File.h>
40 #include <folly/gen/String.h>
41
42 namespace folly {
43
44 namespace {
45
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)!");
50   size_t pageSize = 0;
51   boost::cmatch match;
52
53   bool error = gen::byLine("/proc/meminfo") |
54     [&] (StringPiece line) -> bool {
55       if (boost::regex_match(line.begin(), line.end(), match, regex)) {
56         StringPiece numStr(
57             line.begin() + match.position(1), size_t(match.length(1)));
58         pageSize = to<size_t>(numStr) * 1024;  // in KiB
59         return false;  // stop
60       }
61       return true;
62     };
63
64   if (error) {
65     throw std::runtime_error("Can't find default huge page size");
66   }
67   return pageSize;
68 }
69
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)!");
74   boost::smatch match;
75   HugePageSizeVec vec;
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)) {
80       StringPiece numStr(
81           filename.data() + match.position(1), size_t(match.length(1)));
82       vec.emplace_back(to<size_t>(numStr) * 1024);
83     }
84   }
85   return vec;
86 }
87
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);
92   boost::cmatch match;
93   if (!boost::regex_match(value.begin(), value.end(), match, regex)) {
94     throw std::runtime_error("Invalid pagesize option");
95   }
96   char c = '\0';
97   if (match.length(2) != 0) {
98     c = char(tolower(value[size_t(match.position(2))]));
99   }
100   StringPiece numStr(value.data() + match.position(1), size_t(match.length(1)));
101   auto const size = to<size_t>(numStr);
102   auto const mult = [c] {
103     switch (c) {
104       case 't':
105         return 1ull << 40;
106       case 'g':
107         return 1ull << 30;
108       case 'm':
109         return 1ull << 20;
110       case 'k':
111         return 1ull << 10;
112       default:
113         return 1ull << 0;
114     }
115   }();
116   return size * mult;
117 }
118
119 /**
120  * Get list of supported huge page sizes and their mount points, if
121  * hugetlbfs file systems are mounted for those sizes.
122  */
123 HugePageSizeVec readHugePageSizes() {
124   HugePageSizeVec sizeVec = readRawHugePageSizes();
125   if (sizeVec.empty()) {
126     return sizeVec;  // nothing to do
127   }
128   std::sort(sizeVec.begin(), sizeVec.end());
129
130   size_t defaultHugePageSize = getDefaultHugePageSize();
131
132   struct PageSizeLess {
133     bool operator()(const HugePageSize& a, size_t b) const {
134       return a.size < b;
135     }
136     bool operator()(size_t a, const HugePageSize& b) const {
137       return a < b.size;
138     }
139   };
140
141   // Read and parse /proc/mounts
142   std::vector<StringPiece> parts;
143   std::vector<StringPiece> options;
144
145   gen::byLine("/proc/mounts") | gen::eachAs<StringPiece>() |
146     [&](StringPiece line) {
147       parts.clear();
148       split(" ", line, parts);
149       // device path fstype options uid gid
150       if (parts.size() != 6) {
151         throw std::runtime_error("Invalid /proc/mounts line");
152       }
153       if (parts[2] != "hugetlbfs") {
154         return;  // we only care about hugetlbfs
155       }
156
157       options.clear();
158       split(",", parts[3], options);
159       size_t pageSize = defaultHugePageSize;
160       // Search for the "pagesize" option, which must have a value
161       for (auto& option : options) {
162         // key=value
163         const char* p = static_cast<const char*>(
164             memchr(option.data(), '=', option.size()));
165         if (!p) {
166           continue;
167         }
168         if (StringPiece(option.data(), p) != "pagesize") {
169           continue;
170         }
171         pageSize = parsePageSizeValue(StringPiece(p + 1, option.end()));
172         break;
173       }
174
175       auto pos = std::lower_bound(sizeVec.begin(), sizeVec.end(), pageSize,
176                                   PageSizeLess());
177       if (pos == sizeVec.end() || pos->size != pageSize) {
178         throw std::runtime_error("Mount page size not found");
179       }
180       if (!pos->mountPoint.empty()) {
181         // Only one mount point per page size is allowed
182         return;
183       }
184
185       // Store mount point
186       fs::path path(parts[1].begin(), parts[1].end());
187       struct stat st;
188       const int ret = stat(path.string().c_str(), &st);
189       if (ret == -1 && errno == ENOENT) {
190         return;
191       }
192       checkUnixError(ret, "stat hugepage mountpoint failed");
193       pos->mountPoint = fs::canonical(path);
194       pos->device = st.st_dev;
195     };
196
197   return sizeVec;
198 }
199
200 }  // namespace
201
202 const HugePageSizeVec& getHugePageSizes() {
203   static HugePageSizeVec sizes = readHugePageSizes();
204   return sizes;
205 }
206
207 const HugePageSize* getHugePageSize(size_t size) {
208   // Linear search is just fine.
209   for (auto& p : getHugePageSizes()) {
210     if (p.mountPoint.empty()) {
211       continue;
212     }
213     if (size == 0 || size == p.size) {
214       return &p;
215     }
216   }
217   return nullptr;
218 }
219
220 const HugePageSize* getHugePageSizeForDevice(dev_t device) {
221   // Linear search is just fine.
222   for (auto& p : getHugePageSizes()) {
223     if (p.mountPoint.empty()) {
224       continue;
225     }
226     if (device == p.device) {
227       return &p;
228     }
229   }
230   return nullptr;
231 }
232
233 }  // namespace folly