2 * Copyright 2016 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <sys/types.h>
26 #include <system_error>
28 #include <gflags/gflags.h>
30 #include <folly/File.h>
31 #include <folly/Format.h>
32 #include <folly/MemoryMapping.h>
33 #include <folly/Portability.h>
34 #include <folly/Range.h>
35 #include <folly/ScopeGuard.h>
36 #include <folly/experimental/io/HugePages.h>
38 DEFINE_bool(cp, false, "Copy file");
40 using namespace folly;
44 FOLLY_NORETURN void usage(const char* name);
46 void usage(const char* name) {
47 std::cerr << folly::format(
49 " list all huge page sizes and their mount points\n"
50 " {0} -cp <src_file> <dest_nameprefix>\n"
51 " copy src_file to a huge page file\n",
56 void copy(const char* srcFile, const char* dest) {
57 fs::path destPath(dest);
58 if (!destPath.is_absolute()) {
59 auto hp = getHugePageSize();
60 CHECK(hp) << "no huge pages available";
61 destPath = fs::canonical_parent(destPath, hp->mountPoint);
64 mmapFileCopy(srcFile, destPath.c_str());
68 for (const auto& p : getHugePageSizes()) {
69 std::cout << p.size << " " << p.mountPoint << "\n";
76 int main(int argc, char *argv[]) {
77 gflags::ParseCommandLineFlags(&argc, &argv, true);
79 if (argc != 3) usage(argv[0]);
80 copy(argv[1], argv[2]);
82 if (argc != 1) usage(argv[0]);