Huge page library.
[folly.git] / folly / experimental / io / HugePages.h
1 /*
2  * Copyright 2012 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 <fcntl.h>
23
24 #include <cstddef>
25 #include <string>
26 #include <utility>
27 #include <vector>
28
29 #include "folly/Range.h"
30
31 namespace folly {
32
33 /**
34  * Vector of (huge_page_size, mount_point), sorted by huge_page_size.
35  * mount_point might be empty if no hugetlbfs file system is mounted for
36  * that size.
37  */
38 typedef std::vector<std::pair<size_t, std::string>> HugePageSizeVec;
39
40 /**
41  * Class to interface with Linux huge pages (hugetlbfs).
42  */
43 class HugePages {
44  public:
45   HugePages();
46
47   /**
48    * Get list of supported huge page sizes and their mount points, if
49    * hugetlbfs file systems are mounted for those sizes.
50    */
51   const HugePageSizeVec& sizes() const { return sizes_; }
52
53   /**
54    * Create a file on a huge page filesystem containing a copy of the data
55    * from data.  If multiple huge page sizes are allowed, we
56    * pick the smallest huge page size available, unless you request one
57    * explicitly with the hugePageSize argument.
58    *
59    * We return a struct File structure containing the full path and size
60    * (rounded up to a multiple of the huge page size)
61    */
62   struct File {
63     std::string path;
64     size_t size;
65   };
66   File create(
67       ByteRange data, StringPiece baseName, size_t hugePageSize = 0,
68       mode_t mode = 0644) const;
69
70  private:
71   HugePageSizeVec sizes_;
72 };
73
74 }  // namespace folly
75
76 #endif /* FOLLY_IO_HUGEPAGES_H_ */
77