2 * Copyright 2013 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/experimental/io/HugePages.h"
31 #include "folly/Format.h"
32 #include "folly/Range.h"
33 #include "folly/ScopeGuard.h"
35 DEFINE_bool(cp, false, "Copy file");
37 using namespace folly;
41 void usage(const char* name) __attribute__((noreturn));
43 void usage(const char* name) {
44 std::cerr << folly::format(
46 " list all huge page sizes and their mount points\n"
47 " {0} -cp <src_file> <dest_nameprefix>\n"
48 " copy src_file to a huge page file\n",
53 void copy(const char* srcFile, const char* destPrefix) {
54 int srcfd = open(srcFile, O_RDONLY);
56 throw std::system_error(errno, std::system_category(), "open failed");
62 if (fstat(srcfd, &st) == -1) {
63 throw std::system_error(errno, std::system_category(), "fstat failed");
66 void* start = mmap(nullptr, st.st_size, PROT_READ, MAP_SHARED, srcfd, 0);
67 if (start == MAP_FAILED) {
68 throw std::system_error(errno, std::system_category(), "mmap failed");
72 munmap(start, st.st_size);
76 auto f = hp.create(ByteRange(static_cast<const unsigned char*>(start),
79 std::cout << f.path << "\n";
84 for (auto& p : hp.sizes()) {
85 std::cout << p.size << " " << p.mountPoint << "\n";
92 int main(int argc, char *argv[]) {
93 google::ParseCommandLineFlags(&argc, &argv, true);
95 if (argc != 3) usage(argv[0]);
96 copy(argv[1], argv[2]);
98 if (argc != 1) usage(argv[0]);