/*
- * Copyright 2012 Facebook, Inc.
+ * Copyright 2012-present Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* limitations under the License.
*/
-#ifndef FOLLY_IO_HUGEPAGES_H_
-#define FOLLY_IO_HUGEPAGES_H_
+#pragma once
#include <sys/stat.h>
#include <sys/types.h>
-#include <fcntl.h>
-
#include <cstddef>
#include <string>
#include <utility>
#include <vector>
-#include "folly/Range.h"
+#include <boost/operators.hpp>
+
+#include <folly/Range.h>
+#include <folly/experimental/io/FsUtil.h>
+#include <folly/portability/Unistd.h>
namespace folly {
+struct HugePageSize : private boost::totally_ordered<HugePageSize> {
+ explicit HugePageSize(size_t s) : size(s) {}
+
+ fs::path filePath(const fs::path& relpath) const {
+ return mountPoint / relpath;
+ }
+
+ size_t size = 0;
+ fs::path mountPoint;
+ dev_t device = 0;
+};
+
+inline bool operator<(const HugePageSize& a, const HugePageSize& b) {
+ return a.size < b.size;
+}
+
+inline bool operator==(const HugePageSize& a, const HugePageSize& b) {
+ return a.size == b.size;
+}
+
/**
* Vector of (huge_page_size, mount_point), sorted by huge_page_size.
* mount_point might be empty if no hugetlbfs file system is mounted for
* that size.
*/
-typedef std::vector<std::pair<size_t, std::string>> HugePageSizeVec;
+typedef std::vector<HugePageSize> HugePageSizeVec;
/**
- * Class to interface with Linux huge pages (hugetlbfs).
+ * Get list of supported huge page sizes and their mount points, if
+ * hugetlbfs file systems are mounted for those sizes.
*/
-class HugePages {
- public:
- HugePages();
+const HugePageSizeVec& getHugePageSizes();
- /**
- * Get list of supported huge page sizes and their mount points, if
- * hugetlbfs file systems are mounted for those sizes.
- */
- const HugePageSizeVec& sizes() const { return sizes_; }
-
- /**
- * Create a file on a huge page filesystem containing a copy of the data
- * from data. If multiple huge page sizes are allowed, we
- * pick the smallest huge page size available, unless you request one
- * explicitly with the hugePageSize argument.
- *
- * We return a struct File structure containing the full path and size
- * (rounded up to a multiple of the huge page size)
- */
- struct File {
- std::string path;
- size_t size;
- };
- File create(
- ByteRange data, StringPiece baseName, size_t hugePageSize = 0,
- mode_t mode = 0644) const;
-
- private:
- HugePageSizeVec sizes_;
-};
-
-} // namespace folly
+/**
+ * Return the mount point for the requested huge page size.
+ * 0 = use smallest available.
+ * Returns nullptr if the requested huge page size is not available.
+ */
+const HugePageSize* getHugePageSize(size_t size = 0);
-#endif /* FOLLY_IO_HUGEPAGES_H_ */
+/**
+ * Return the huge page size for a device.
+ * returns nullptr if device does not refer to a huge page filesystem.
+ */
+const HugePageSize* getHugePageSizeForDevice(dev_t device);
+} // namespace folly