c1ec7e13e78160f87e23c9fb2db07e3183a1ae02
[folly.git] / folly / experimental / io / HugePageUtil.cpp
1 /*
2  * Copyright 2017 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 #include <iostream>
18 #include <stdexcept>
19
20 #include <folly/File.h>
21 #include <folly/Format.h>
22 #include <folly/MemoryMapping.h>
23 #include <folly/Range.h>
24 #include <folly/ScopeGuard.h>
25 #include <folly/experimental/io/HugePages.h>
26 #include <folly/portability/GFlags.h>
27
28 DEFINE_bool(cp, false, "Copy file");
29
30 using namespace folly;
31
32 namespace {
33
34 [[noreturn]] void usage(const char* name) {
35   std::cerr << folly::format(
36       "Usage: {0}\n"
37       "         list all huge page sizes and their mount points\n"
38       "       {0} -cp <src_file> <dest_nameprefix>\n"
39       "         copy src_file to a huge page file\n",
40       name);
41   exit(1);
42 }
43
44 void copy(const char* srcFile, const char* dest) {
45   fs::path destPath(dest);
46   if (!destPath.is_absolute()) {
47     auto hp = getHugePageSize();
48     CHECK(hp) << "no huge pages available";
49     destPath = fs::canonical_parent(destPath, hp->mountPoint);
50   }
51
52   mmapFileCopy(srcFile, destPath.c_str());
53 }
54
55 void list() {
56   for (const auto& p : getHugePageSizes()) {
57     std::cout << p.size << " " << p.mountPoint << "\n";
58   }
59 }
60
61 }  // namespace
62
63
64 int main(int argc, char *argv[]) {
65   gflags::ParseCommandLineFlags(&argc, &argv, true);
66   if (FLAGS_cp) {
67     if (argc != 3) usage(argv[0]);
68     copy(argv[1], argv[2]);
69   } else {
70     if (argc != 1) usage(argv[0]);
71     list();
72   }
73   return 0;
74 }