memoptimizer monitors current state of free pages overall and free
pages of each order. Based upon current rate of free pages
consumption and memory fragmentation, it predicts if system is
likely to run out of memory or if memory will become severely
fragmented in near future. If so, it adjusts watermarks to force
memory reclamation if system is about to run out of memory. If
memory is predicted to become severely fragmented, it triggers
compaction in the kernel. The goal is to avert memory shortage
and/or fragmentation by taking proactive measures. To arrive at this
prediction, memoptimizer samples free pages of each order on each
node periodically and fits a straight line using method of least
squares to these sample points. It also computes current reclamation
rate by monitoring /proc/vmstat
. The equation derived for best fit
line is used to compute when free memory exhaustion will occur
taking into account current reclamation rate. If this exhaustion is
imminent in near future, watermarks are adjusted to initiate
reclamation.
memoptimizer samples /proc/buddyinfo
to monitor the total available
free memory and the fraction thereof that, whilst notionally free,
requires compaction before it can be used. At any given moment, the
difference between these two quantities is the amount of memory that
is free for immediate use: once this is exhausted, subsequent
allocations will stall whilst a portion of the fragmented memory is
compacted. The program calculates trends for both the total and
fragmented free memory and, by extrapolation, determines whether
exhaustion is imminent. At the last possible moment, i.e. only once
it is absolutely necessary, the program initiates compaction with a
view to recovering the fragmented memory before it is required by
subsequent allocations. It initiates compaction by writing 1 to
/sys/devices/system/node/node%d/compact
. If number of free pages is
expected to be exhausted, it looks at the number of inactive pages
in cache buffer to determine if changing watermarks can result in
meaningful number of pages reclaimed. It adjusts watermark by
changing watermark scale factor in
/proc/sys/vm/watermark_scale_factor
.
memoptimizer must be run as root and must have access to following files:
/proc/vmstat
/proc/buddyinfo
/proc/zoneinfo
/proc/sys/vm/watermark_scale_factor
/sys/devices/system/node/node%d/compact
memoptimizer daemon can be run standalone or can be started automatically by systemd/init.
memoptimizer supports three levels of aggressiveness - 1 = Low, 2 = Normal (default), 3 = High. Aggressiveness level dictates how often memoptimizer will sample system state and how aggressively it will tune watermark scale factor.
In simplest form, mempoptimizer can be started with:
$ memoptimizer
Aggressiveness level can be changed with:
$ memoptimizer -a 3
If a maximum gap allowed between low and high watermarks as memoptimizer tunes watermarks, is desired, it can be specified with a -m flag where the argument is number of GB:
$ mempotimizer -m 10
To develop for memoptimizer, obtain the source code from git repository at https://github.com/oracle/memoptimizer or from the source package. Source code is broken down into two primary files - memoptimizer.c contains the main driving code for the daemon which does all the initialization and takes action in response to the results from prediction algorithm, predict.c contains the core of prediction algorithm which uses least square fit method to calculate a trend line for memory consumption.
memoptimizer requires only the basic build tools - gcc compiler and linker, and make.
Run make all
to build the memoptimizer daemon.
Run make clean
to remove binaries and intermediate files generated by build process.
Copy memoptimizer binary to a directory appropriate for your system, typically /usr/sbin
. memoptimizer can use a configuration file as well which is /etc/sysconfig/memoptimizer
on rpm based system and /etc/default/memoptimizer
on deb based systems. Here is a sample configuration file:
# Configuration file for memoptimizer
#
# Options in this file can be overridden with command line options
# verbosity level (1-5)
VERBOSE=0
# Maximum gap between low and high watermarks (in GB)
# MAXGAP=5
# Aggressiveness level for memoptimizer (1-3)
AGGRESSIVENESS=2
Source code includes documentation in form of a man page. This man page is contained in file memoptimizer.8
. If memoptimizer was installed as a package on your system, man page should be available as standard man page with man memoptimizer
command. If memoptimizer was not installed as a package, memoptimizer.8
file can be displayed as man page with nroff -man memoptimizer.8
.
memoptimizer source code is maintained in a public git repository. Contributions to the project can be made by either submitting a patch to project admin or by sending a git pull request to project admin. All contributions must be made under GNU Public License version 2. For further details see CONTRIBUTING.md
file in the source code.