Search code examples
unixstress-testingstress-ng

Stress-ng stress memory with specific percentage


I am trying to stress a ubuntu container's memory. Typing free in my command terminal provides the following result:

free -m 

          total        used        free      shared  buff/cache   available     

Mem:           7958         585        6246         401        1126        6743                                         
Swap:          2048           0        2048   

I want to stress exactly 10% of the total available memory. Per stress-ng manual:

   -m N, --vm N
          start N workers continuously calling mmap(2)/munmap(2) and writing to the allocated
          memory.  Note  that  this  can cause systems to trip the kernel OOM killer on Linux
          systems if not enough physical memory and swap is not available.

   --vm-bytes N
          mmap N bytes per vm worker, the default is 256MB. One can specify the size as %  of
          total  available  memory  or in units of Bytes, KBytes, MBytes and GBytes using the
          suffix b, k, m or g.

       

Now, on my target container I run two memory stressors to occupy 10% of my memory:

stress-ng -vm 2 --vm-bytes 10% -t 10 

However, the memory usage on the container never reaches 10% no matter how many times I run it. I tried different timeout values, no result. The closet it gets is 8.9% never approaches 10%. I inspect memory usage on my container this way:

 docker stats --no-stream kind_sinoussi
CONTAINER ID   NAME            CPU %     MEM USAGE / LIMIT     MEM %     NET I/O       BLOCK I/O   PIDS
c3fc7a103929   kind_sinoussi   199.01%   638.4MiB / 7.772GiB   8.02%     1.45kB / 0B   0B / 0B     7

In an attempt to understand this behaviour, I tried running the same command with an exact unit of bytes. In my case, I'll opt for 800 mega since 7958m * 0.1 = 795,8 ~ 800m.

stress-ng -vm 2 --vm-bytes 800m -t 15

And, I get 10%!

 docker stats --no-stream kind_sinoussi
CONTAINER ID   NAME            CPU %     MEM USAGE / LIMIT     MEM %     NET I/O       BLOCK I/O   PIDS
c3fc7a103929   kind_sinoussi   198.51%   815.2MiB / 7.772GiB   10.24%    1.45kB / 0B   0B / 0B     7

Can someone explain why this is happening?

Another question, is it possible for stress-ng to stress memory usage to 100%?


Solution

  • stress-ng --vm-bytes 10% will use sysconf(_SC_AVPHYS_PAGES) to determine the available memory. This sysconf() system call will return the number of pages that the application can use without hindering any other process. So this is approximately what the free command is returning for the free memory statistic.

    Note that stress-ng will allocate the memory with mmap, so it may be that during run time mmap'd pages may not necessarily be physically backed at the time you check how much real memory is being used.

    It may be worth trying to also use the --vm-populate option; this will try and ensure the pages are physically populated on the mmap'd memory that stress-ng is exercising. Also try --vm-madvise willneed to use the madvise() system call to hint that the pages will be required fairly soon.