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.
// 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("");
return0; }
Using the always policy, echo always > /sys/kernel/mm/transparent_hugepage/enabled,: