52 long page_size = sysconf(_SC_PAGESIZE);
53 int fd = open(
"/proc/self/pagemap", O_RDONLY);
60 off_t offset = (uintptr_t)virt / page_size *
sizeof(uintptr_t);
61 if (lseek(fd, offset, SEEK_SET) < 0) {
67 if (read(fd, &phy,
sizeof(phy)) < 0) {
78 return (uint64_t)((phy & 0x7fffffffffffffULL) * page_size +
79 ((uintptr_t)virt) % page_size);
88 if (mirror && size % kBufPageSize) {
89 std::cerr <<
"Mirror huge pages must be a multiple of huge page size"
94 fd = open(path.c_str(), O_CREAT | O_RDWR, S_IRWXU);
96 std::cerr <<
"(" << errno <<
") Problem opening huge page file descriptor"
101 if (ftruncate(fd, (off_t)size)) {
102 std::cerr <<
"(" << errno
103 <<
") Could not truncate huge page to size: " << size
106 unlink(path.c_str());
110 void* virt_addr = (
void*)mmap(
nullptr, size * 2, PROT_READ | PROT_WRITE,
111 MAP_SHARED | MAP_HUGETLB, fd, 0);
113 if (virt_addr == (
void*)-1) {
114 std::cerr <<
"(" << errno <<
") Could not mmap huge page" << std::endl;
116 unlink(path.c_str());
123 (
void*)mmap((uint8_t*)virt_addr + size, size, PROT_READ | PROT_WRITE,
124 MAP_FIXED | MAP_SHARED | MAP_HUGETLB, fd, 0);
126 if (ret == (
void*)-1) {
127 std::cerr <<
"(" << errno <<
") Could not mmap second huge page"
130 unlink(path.c_str());
136 if (mlock(virt_addr, size)) {
137 std::cerr <<
"(" << errno <<
") Could not lock huge page" << std::endl;
138 munmap(virt_addr, size);
140 unlink(path.c_str());
Constants used throughout the codebase. Some of these constants need to be kept in sync with the hard...
Helper functions adapted from the ixy driver
void * get_huge_page(const std::string &path, size_t size=0, bool mirror=false)
uint64_t virt_to_phys(void *virt)