Skip to content

Commit

Permalink
Attempt to enable transparent huge pages when using mmap against a no…
Browse files Browse the repository at this point in the history
…n-durable file.
  • Loading branch information
broneill committed Aug 31, 2024
1 parent db164c1 commit 98a8d26
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions src/main/java/org/cojen/tupl/io/PosixMappedPageArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,7 @@ class PosixMappedPageArray extends MappedPageArray {
long mappingSize = pageSize * pageCount;
long addr = PosixFileIO.mmapFd(mappingSize, prot, flags, -1, 0);

if (mappingSize >= (1L << 30) && Platform.isLinux()) {
try {
PosixFileIO.madvisePtr(addr, mappingSize, 14); // 14 = MADV_HUGEPAGE
} catch (IOException e) {
if (listener != null) {
listener.notify
(EventType.CACHE_INIT_INFO,
"Unable to allocate using transparent huge pages");
}
}
}
hugePages(addr, mappingSize, listener);

setMappingPtr(addr);

Expand Down Expand Up @@ -135,6 +125,12 @@ class PosixMappedPageArray extends MappedPageArray {
if (options.contains(OpenOption.RANDOM_ACCESS)) {
PosixFileIO.madvisePtr(addr, mappingSize, 1); // 1 = POSIX_MADV_RANDOM
}

if (options.contains(OpenOption.NON_DURABLE)) {
// Only works when /sys/kernel/mm/transparent_hugepage/shmem_enabled is set to
// 'advise' or some other appropriate value.
hugePages(addr, mappingSize, listener);
}
} catch (IOException e) {
try {
PosixFileIO.closeFd(fd);
Expand All @@ -149,6 +145,20 @@ class PosixMappedPageArray extends MappedPageArray {
setMappingPtr(addr);
}

private static void hugePages(long addr, long mappingSize, EventListener listener) {
if (mappingSize >= (1L << 30) && Platform.isLinux()) {
try {
PosixFileIO.madvisePtr(addr, mappingSize, 14); // 14 = MADV_HUGEPAGE
} catch (IOException e) {
if (listener != null) {
listener.notify
(EventType.CACHE_INIT_INFO,
"Unable to allocate using transparent huge pages");
}
}
}
}

@Override
public long pageCount() {
return mEmpty ? 0 : super.pageCount();
Expand Down

0 comments on commit 98a8d26

Please sign in to comment.