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