d1d2317826162404d829c512a33bc45e4fb86986
[folly.git] / folly / experimental / io / HugePages.h
1 /*
2  * Copyright 2016 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 #ifndef FOLLY_IO_HUGEPAGES_H_
18 #define FOLLY_IO_HUGEPAGES_H_
19
20 #include <sys/stat.h>
21 #include <sys/types.h>
22 #include <cstddef>
23 #include <string>
24 #include <unistd.h>
25 #include <utility>
26 #include <vector>
27
28 #include <boost/operators.hpp>
29
30 #include <folly/Range.h>
31 #include <folly/experimental/io/FsUtil.h>
32
33 namespace folly {
34
35 struct HugePageSize : private boost::totally_ordered<HugePageSize> {
36   explicit HugePageSize(size_t s) : size(s) { }
37
38   fs::path filePath(const fs::path& relpath) const {
39     return mountPoint / relpath;
40   }
41
42   size_t size = 0;
43   fs::path mountPoint;
44   dev_t device = 0;
45 };
46
47 inline bool operator<(const HugePageSize& a, const HugePageSize& b) {
48   return a.size < b.size;
49 }
50
51 inline bool operator==(const HugePageSize& a, const HugePageSize& b) {
52   return a.size == b.size;
53 }
54
55 /**
56  * Vector of (huge_page_size, mount_point), sorted by huge_page_size.
57  * mount_point might be empty if no hugetlbfs file system is mounted for
58  * that size.
59  */
60 typedef std::vector<HugePageSize> HugePageSizeVec;
61
62 /**
63  * Get list of supported huge page sizes and their mount points, if
64  * hugetlbfs file systems are mounted for those sizes.
65  */
66 const HugePageSizeVec& getHugePageSizes();
67
68 /**
69  * Return the mount point for the requested huge page size.
70  * 0 = use smallest available.
71  * Returns nullptr if the requested huge page size is not available.
72  */
73 const HugePageSize* getHugePageSize(size_t size = 0);
74
75 /**
76  * Return the huge page size for a device.
77  * returns nullptr if device does not refer to a huge page filesystem.
78  */
79 const HugePageSize* getHugePageSizeForDevice(dev_t device);
80
81 }  // namespace folly
82
83 #endif /* FOLLY_IO_HUGEPAGES_H_ */