/*
- * Copyright 2014 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.
*/
-#include <sys/mman.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <errno.h>
-#include <fcntl.h>
-#include <unistd.h>
-
#include <iostream>
#include <stdexcept>
-#include <system_error>
-
-#include <gflags/gflags.h>
-#include "folly/Format.h"
-#include "folly/Portability.h"
-#include "folly/Range.h"
-#include "folly/ScopeGuard.h"
-#include "folly/experimental/io/HugePages.h"
+#include <folly/Format.h>
+#include <folly/Range.h>
+#include <folly/experimental/io/HugePages.h>
+#include <folly/portability/GFlags.h>
+#include <folly/system/MemoryMapping.h>
DEFINE_bool(cp, false, "Copy file");
namespace {
-void usage(const char* name) FOLLY_NORETURN;
-
-void usage(const char* name) {
+[[noreturn]] void usage(const char* name) {
std::cerr << folly::format(
"Usage: {0}\n"
" list all huge page sizes and their mount points\n"
exit(1);
}
-void copy(const char* srcFile, const char* destPrefix) {
- int srcfd = open(srcFile, O_RDONLY);
- if (srcfd == -1) {
- throw std::system_error(errno, std::system_category(), "open failed");
- }
- SCOPE_EXIT {
- close(srcfd);
- };
- struct stat st;
- if (fstat(srcfd, &st) == -1) {
- throw std::system_error(errno, std::system_category(), "fstat failed");
+void copy(const char* srcFile, const char* dest) {
+ fs::path destPath(dest);
+ if (!destPath.is_absolute()) {
+ auto hp = getHugePageSize();
+ CHECK(hp) << "no huge pages available";
+ destPath = fs::canonical_parent(destPath, hp->mountPoint);
}
- void* start = mmap(nullptr, st.st_size, PROT_READ, MAP_SHARED, srcfd, 0);
- if (start == MAP_FAILED) {
- throw std::system_error(errno, std::system_category(), "mmap failed");
- }
-
- SCOPE_EXIT {
- munmap(start, st.st_size);
- };
-
- HugePages hp;
- auto f = hp.create(ByteRange(static_cast<const unsigned char*>(start),
- st.st_size),
- destPrefix);
- std::cout << f.path << "\n";
+ mmapFileCopy(srcFile, destPath.c_str());
}
void list() {
- HugePages hp;
- for (auto& p : hp.sizes()) {
+ for (const auto& p : getHugePageSizes()) {
std::cout << p.size << " " << p.mountPoint << "\n";
}
}
-} // namespace
+} // namespace
-
-int main(int argc, char *argv[]) {
- google::ParseCommandLineFlags(&argc, &argv, true);
+int main(int argc, char* argv[]) {
+ gflags::ParseCommandLineFlags(&argc, &argv, true);
if (FLAGS_cp) {
- if (argc != 3) usage(argv[0]);
+ if (argc != 3) {
+ usage(argv[0]);
+ }
copy(argv[1], argv[2]);
} else {
- if (argc != 1) usage(argv[0]);
+ if (argc != 1) {
+ usage(argv[0]);
+ }
list();
}
return 0;
}
-