.. _Configuring Automatic Memory Management Policies:

Configuring Automatic Memory Management Policies
------------------------------------------------

Virtuozzo offers several techniques for automatically managing VM and container memory. The management is performed by the ``vcmmd`` daemon by means of policies described below. Setting the correct policy may provide performance and density benefits to your Virtuozzo installation.

The following policies are currently available:

-  ``performance`` (default). Best for hardware nodes with lots of small virtual environments (relative to a NUMA node size).

   .. important:: It is strongly recommended to avoid overcommitting resources when the ``performance`` policy is enabled. For resource overcommit, switch to the ``density`` policy.

-  ``density``. Best for hardware nodes with memory overcommit.

-  ``NoOpPolicy``. This policy is set automatically if no license is installed on a hardware node or if the current license is expired.

You can switch between the policies with the ``prlsrvctl`` tool.

.. note:: Before setting a VCMMD policy, stop all virtual machines and containers on the host or temporarily migrate them live to another host.

For example, to switch to the ``density`` policy, do the following:

1. Make sure that there are no running virtual machines or containers on the host:

   ::

       # prlctl list
       UUID                                   STATUS   IP_ADDR     T  NAME

2. Set the policy with ``prlsrvctl set --vcmmd-policy``:

   ::

       # prlsrvctl set --vcmmd-policy density

To check the currently enabled policy, run

::

    # prlsrvctl info | grep "policy"
    Vcmmd policy: density config=density

.. _Optimizing Virtual Machine Memory with Kernel Same-Page Merging:

Optimizing Virtual Machine Memory with Kernel Same-Page Merging
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

To optimize memory usage by virtual machines, Virtuozzo uses a feature of Linux called Kernel Same-Page Merging (KSM). The KSM daemon ``ksmd`` periodically scans memory for pages with identical content and merges those into a single page. Said page is marked as copy-on-write (COW), so when its contents are changed by a virtual machine, the kernel creates a new copy for that virtual machine.

KSM enables the host to:

-  avoid swapping due to merging of identical pages;

-  run more virtual machines;

-  overcommit virtual machine memory;

-  speed up RAM and hence certain applications and guest operating systems.

In Virtuozzo 7, KSM is managed by ``vcmmd`` and work with both ``performance`` and ``density`` policies. However, with ``performance`` policy enabled, KSM merges identical pages for each NUMA node independently which slows its operation down by as many times as the number of NUMA nodes on the host (e.g., by four times if there are four NUMA nodes).

.. note:: For more details, see :ref:`Configuring Automatic Memory Management Policies`.

.. _Managing Host Services with VCMMD:

Managing Host Services with VCMMD
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

In Virtuozzo 7, you can set memory limits and guarantees managed by ``vcmmd`` to host services. The daemon will take into account the service memory while allocating resources to virtual environments.

.. note:: The setting is removed on hardware node reboot.

For example, to have a service named ``service`` managed by ``vcmmd``, do as follows:

1. Place the service processes in ``/sys/fs/cgroup/memory/service.slice`` directory.

2. Register ``service.slice`` in ``vcmmd`` and set the desired memory parameters. For example:

   ::
       
       # vcmmdctl register SRVC service.slice --guarantee 100000000 --limit 100000000 --swap 100000
	   
3. Activate the service:
	
   ::
       
       # vcmmdctl activate service.slice

To check if the service is managed with ``vcmmd``, run 

::
       
    # vcmmdctl list
    name                                     type  active  guarantee     limit    swap
    <...>
    service.slice                            SRVC     yes      97656     97656      97

You can change the service memory parameters at runtime using the ``vcmmdctl update`` command.

For example, to change the ``service.slice`` parameters, run:

::

    # vcmmdctl update service.slice --guarantee 200000000 --limit 200000000

To check if the parameters have been correctly applied, run
	
::

    # vcmmdctl list
    name                                     type  active  guarantee     limit    swap
    <...>
    service.slice                            SRVC     yes     195312    195312     max

You can unregister the service from ``vcmmd`` at runtime with the ``vcmmdctl unregister`` command. 

.. _Managing Virtuozzo Storage Services with VCMMD:

Managing Virtuozzo Storage Services with VCMMD
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

For Virtuozzo Storage hardware nodes, ``vstorage.slice/vstorage-services.slice`` is automatically created in the memory cgroup on hardware node start.

To check if ``vstorage`` is managed with ``vcmmd``, run

::

    # vcmmdctl list
    name                                     type  active  guarantee     limit    swap
    <...>
    vstorage.slice/vstorage-services.slice   SRVC     yes    4194304 103854973       0

You can temporarily change memory limits and guarantees for ``vstorage`` services at runtime using the ``vcmmdctl update`` command. For example:

::

    # vcmmdctl update vstorage.slice/vstorage-services.slice --guarantee 100000000 --limit 100000000
	
The parameters will be reset to the default values on the next ``vcmmd`` restart or node reboot.
	
To configure limits and guarantees for ``vstorage`` services permanently, do as follows:

1. Edit the memory parameters in the ``/etc/vz/vstorage-limits.conf`` file. (Set ``-1`` for unlimited memory.)

2. Restart ``vcmmd`` to apply changes:

   ::

       # systemctl restart vcmmd

