1543973a9f70fa8be57a40010d34af1f7c89dde5
[folly.git] / folly / experimental / io / HugePages.h
1 /*
2  * Copyright 2013 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 <cstddef>
21 #include <string>
22 #include <utility>
23 #include <vector>
24
25 #include <boost/operators.hpp>
26
27 #include "folly/Range.h"
28 #include "folly/experimental/io/FsUtil.h"
29
30 namespace folly {
31
32 struct HugePageSize : private boost::totally_ordered<HugePageSize> {
33   HugePageSize() : size(0) { }
34   explicit HugePageSize(size_t s) : size(s) { }
35   size_t size;
36   fs::path mountPoint;
37 };
38
39 inline bool operator<(const HugePageSize& a, const HugePageSize& b) {
40   return a.size < b.size;
41 }
42
43 inline bool operator==(const HugePageSize& a, const HugePageSize& b) {
44   return a.size == b.size;
45 }
46
47 /**
48  * Vector of (huge_page_size, mount_point), sorted by huge_page_size.
49  * mount_point might be empty if no hugetlbfs file system is mounted for
50  * that size.
51  */
52 typedef std::vector<HugePageSize> HugePageSizeVec;
53
54 /**
55  * Class to interface with Linux huge pages (hugetlbfs).
56  */
57 class HugePages {
58  public:
59   HugePages();
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& sizes() const { return sizes_; }
66
67   /**
68    * Return the mount point for the requested huge page size.
69    * 0 = use smallest available.
70    * Throws on error.
71    */
72   const HugePageSize& getSize(size_t hugePageSize = 0) const;
73
74   /**
75    * Create a file on a huge page filesystem containing a copy of the data
76    * from data.  If multiple huge page sizes are allowed, we
77    * pick the smallest huge page size available, unless you request one
78    * explicitly with the hugePageSize argument.
79    *
80    * The "path" argument must be rooted under the mount point for the
81    * selected huge page size.  If relative, it is considered relative to the
82    * mount point.
83    *
84    * We return a struct File structure containing the full path and size
85    * (rounded up to a multiple of the huge page size)
86    */
87   struct File {
88     File() : size(0) { }
89     fs::path path;
90     size_t size;
91   };
92   File create(
93       ByteRange data, const fs::path& path,
94       HugePageSize hugePageSize = HugePageSize()) const;
95
96  private:
97   HugePageSizeVec sizes_;
98 };
99
100 }  // namespace folly
101
102 #endif /* FOLLY_IO_HUGEPAGES_H_ */
103