Transparent Huge Page (THP) is a technique to take advantage of large-page (>4K) without (or with little) app modification. Here is a simple C program to illustrate how THP can be used and the effect of different huge-page allocation policy.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <assert.h>
#include <sys/mman.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <stdio.h>

int main(void) {
unsigned char vec[20];
int res;
size_t PS = (size_t)sysconf(_SC_PAGESIZE);
size_t ps_2m = (1 << 21);

void* addr;
// make sure it's 2M-aligned
posix_memalign(&addr, ps_2m, ps_2m);
printf("addr: %p\n", addr);
puts("");

// depending on the huge-page alloc policy:
// /sys/kernel/mm/transparent_hugepage/enabled
// always: no need for madvise
// madvise: needed
// never: no effect either way
//
// res = madvise(addr, ps_2m, MADV_HUGEPAGE);
// assert(res == 0);

res = mincore(addr, 10 * PS, vec);
assert(res == 0);

puts("not committed");
for (int i = 0; i < 10; ++i) {
printf("%d", (vec[i] & 1));
}

puts("");

// write to the first byte
((char *)addr)[0] = 1;

res = mincore(addr, 10 * PS, vec);
assert(res == 0);

puts("");
puts("after writing the first byte");
for (int i = 0; i < 10; ++i) {
printf("%d", (vec[i] & 1));
}

puts("");

return 0;
}

Using the always policy, echo always > /sys/kernel/mm/transparent_hugepage/enabled,:

1
2
3
4
5
6
7
8
addr: 0x7fcf0ae00000

not committed
0000000000

after writing the first byte
1111111111