Fix copyright lines
[folly.git] / folly / experimental / io / HugePageUtil.cpp
index 1715da99673b0527d2e6ac49f2adff8c5960b8c4..2bf4273c624d604c9a155d6de969062c4bf84a29 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2013 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/experimental/io/HugePages.h"
-#include "folly/Format.h"
-#include "folly/Range.h"
-#include "folly/ScopeGuard.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");
 
@@ -38,9 +29,7 @@ using namespace folly;
 
 namespace {
 
-void usage(const char* name) __attribute__((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"
@@ -50,54 +39,37 @@ void usage(const char* name) {
   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;
 }
-