Quantcast
Channel: Active questions tagged kernel-modules - Unix & Linux Stack Exchange
Viewing all articles
Browse latest Browse all 1181

How to write a program to trigger memory direct reclaim?

$
0
0

First of all, I would like to kindly ask for your assistance in teaching me how to write a program that can trigger direct memory reclamation.

I wrote a program that utilizes mmap to allocate memory as below.

#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <sys/mman.h>#define PAGE_SZ 4096#define GB (1<<30)int random_number(int min, int max) {return min + rand() % (max - min + 1);}int main(int argc, char** argv) {size_t size;size = 5ul * GB;printf("size %lu\n",size);void *mem = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);if (mem == MAP_FAILED) {perror("mmap");exit(EXIT_FAILURE);}size_t num_pages = size / PAGE_SZ;size_t i;for (i = 0; i < num_pages; i++) {*((char *)mem + i * PAGE_SZ) = '6';}srand(time(NULL));while (1) {size_t random_page = random_number(0, num_pages - 1);char first_byte = *((char *)mem + random_page * PAGE_SZ);}return 0;}

However, I noticed that only the available field decreased, while the free field did not show a significant decrease. This made it difficult to trigger direct memory reclamation.

So I am wondering how to make both the free and available fields decrease simultaneously?

I would greatly appreciate any advice or suggestions on how to write testing programs that can effectively trigger direct memory reclamation.

The reason why I want to conduct this test is as follows.

Recently, my server experienced several instances of hang, which lasted from several seconds to even ten minutes.

We have launched a program to monitor the call trace of processes in the 'D' state and the memory status.

Let me explain what I found during the hanging period first.

  1. There were many processes in the 'D' state with a similar call trace: page_fault->...->filmap_fault()->__lock_page_or_retry()->io_schedule().No processes were hung in __alloc_page_slowpath.
  2. The system triggered a direct memory reclaim, as observed through the logging that monitors the change of pgsteal_direct in /proc/vmstat every 2 seconds. The changes can range from tens to even ten million.
  3. The available field and free field of 'free -h' were 15GB and 2GB respectively, before and after the direct reclamation occurred.

And here is some information about the server.The server has 128GB of memory.The value of /proc/sys/vm/min_free_kbytes is set to 2GB.The swap file is disabled.

Based on this information, I believe:

  1. Some processes triggered direct reclamation but did not hang.
  2. The direct reclamation evicted some pages that belonged to other processes. (As far as I know, direct reclamation only evicts clean pages when it occurs.)
  3. A group of processes triggered page faults, causing a high system load and resulting in them getting stuck.

To validate my hypothesis, I need to reproduce direct reclamation.


Viewing all articles
Browse latest Browse all 1181

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>