Skip to content

Commit

Permalink
hugetlb: compacting memory to reduce fragmental pages
Browse files Browse the repository at this point in the history
Kernel memory compaction is a process that helps to reduce fragmentation
and optimize memory allocation by compacting memory pages.

  `echo 1 > /proc/sys/vm/compact_memory`

When 1 is written to the file, all zones are compacted such that free
memory is available in contiguous blocks where possible.

This can be important for example in the allocation of huge pages although
processes will also directly compact memory as required.

As this introduced since v2.6.35, we could safely use it via
SAFE_FILE_PRINT() in LTP lib:

  commit 76ab0f530e4a01d4dc20cdc1d5e87753c579dc18
  Author: Mel Gorman <mel@csn.ul.ie>
  Date:   Mon May 24 14:32:28 2010 -0700

      mm: compaction: add /proc trigger for memory compaction

Signed-off-by: Li Wang <liwang@redhat.com>
  • Loading branch information
wangli5665 committed Jun 22, 2023
1 parent 96553c9 commit acf4611
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/tst_hugepage.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ unsigned long tst_reserve_hugepages(struct tst_hugepage *hp)
}

SAFE_FILE_PRINTF("/proc/sys/vm/drop_caches", "3");
SAFE_FILE_PRINTF("/proc/sys/vm/compact_memory", "1");
if (hp->policy == TST_NEEDS) {
tst_hugepages += SAFE_READ_MEMINFO("HugePages_Total:");
goto set_hugepages;
Expand Down
2 changes: 2 additions & 0 deletions testcases/kernel/mem/hugetlb/hugemmap/hugemmap32.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ static void setup(void)
SAFE_CLOSEDIR(dir);

SAFE_FILE_PRINTF("/proc/sys/vm/drop_caches", "3");
SAFE_FILE_PRINTF("/proc/sys/vm/compact_memory", "1");

if (tst_available_mem() < (long long)hpage_size) {
g_hpage_path[0] = '\0';
tst_brk(TCONF, "No enough memory for gigantic hugepage reservation");
Expand Down

0 comments on commit acf4611

Please sign in to comment.