[PULL 06/10] tests/functional: add memlock tests

Thomas Huth posted 10 patches 5 months, 1 week ago
Maintainers: "Alex Bennée" <alex.bennee@linaro.org>, "Philippe Mathieu-Daudé" <philmd@linaro.org>, Thomas Huth <thuth@redhat.com>, Richard Henderson <richard.henderson@linaro.org>, David Hildenbrand <david@redhat.com>, Ilya Leoshkevich <iii@linux.ibm.com>, Halil Pasic <pasic@linux.ibm.com>, Christian Borntraeger <borntraeger@linux.ibm.com>, Eric Farman <farman@linux.ibm.com>, Paolo Bonzini <pbonzini@redhat.com>, "Marc-André Lureau" <marcandre.lureau@redhat.com>, "Daniel P. Berrangé" <berrange@redhat.com>, Michael Rolnik <mrolnik@gmail.com>
[PULL 06/10] tests/functional: add memlock tests
Posted by Thomas Huth 5 months, 1 week ago
From: Alexandr Moshkov <dtalexundeer@yandex-team.ru>

Add new tests to check the correctness of the `-overcommit memlock`
option (possible values: off, on, on-fault) by using
`/proc/{qemu_pid}/status` file to check in VmSize, VmRSS and VmLck
values:

* if `memlock=off`, then VmLck = 0;
* if `memlock=on`, then VmLck > 0 and almost all memory is resident;
* if `memlock=on-fault`, then VmLck > 0 and only few memory is resident.

Signed-off-by: Alexandr Moshkov <dtalexundeer@yandex-team.ru>
Message-ID: <20250605065908.299979-3-dtalexundeer@yandex-team.ru>
Signed-off-by: Thomas Huth <thuth@redhat.com>
---
 tests/functional/meson.build     |  1 +
 tests/functional/test_memlock.py | 79 ++++++++++++++++++++++++++++++++
 2 files changed, 80 insertions(+)
 create mode 100755 tests/functional/test_memlock.py

diff --git a/tests/functional/meson.build b/tests/functional/meson.build
index 557d59ddf4d..c3fca446cff 100644
--- a/tests/functional/meson.build
+++ b/tests/functional/meson.build
@@ -312,6 +312,7 @@ tests_x86_64_system_quick = [
   'virtio_version',
   'x86_cpu_model_versions',
   'vnc',
+  'memlock',
 ]
 
 tests_x86_64_system_thorough = [
diff --git a/tests/functional/test_memlock.py b/tests/functional/test_memlock.py
new file mode 100755
index 00000000000..2b515ff979f
--- /dev/null
+++ b/tests/functional/test_memlock.py
@@ -0,0 +1,79 @@
+#!/usr/bin/env python3
+#
+# Functional test that check overcommit memlock options
+#
+# Copyright (c) Yandex Technologies LLC, 2025
+#
+# Author:
+#  Alexandr Moshkov <dtalexundeer@yandex-team.ru>
+#
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+import re
+
+from typing import Dict
+
+from qemu_test import QemuSystemTest
+from qemu_test import skipLockedMemoryTest
+
+
+STATUS_VALUE_PATTERN = re.compile(r'^(\w+):\s+(\d+) kB', re.MULTILINE)
+
+
+@skipLockedMemoryTest(2_097_152)  # 2GB
+class MemlockTest(QemuSystemTest):
+    """
+    Runs a guest with memlock options.
+    Then verify, that this options is working correctly
+    by checking the status file of the QEMU process.
+    """
+
+    def common_vm_setup_with_memlock(self, memlock):
+        self.vm.add_args('-overcommit', f'mem-lock={memlock}')
+        self.vm.launch()
+
+    def test_memlock_off(self):
+        self.common_vm_setup_with_memlock('off')
+
+        status = self.get_process_status_values(self.vm.get_pid())
+
+        self.assertTrue(status['VmLck'] == 0)
+
+    def test_memlock_on(self):
+        self.common_vm_setup_with_memlock('on')
+
+        status = self.get_process_status_values(self.vm.get_pid())
+
+        # VmLck > 0 kB and almost all memory is resident
+        self.assertTrue(status['VmLck'] > 0)
+        self.assertTrue(status['VmRSS'] >= status['VmSize'] * 0.70)
+
+    def test_memlock_onfault(self):
+        self.common_vm_setup_with_memlock('on-fault')
+
+        status = self.get_process_status_values(self.vm.get_pid())
+
+        # VmLck > 0 kB and only few memory is resident
+        self.assertTrue(status['VmLck'] > 0)
+        self.assertTrue(status['VmRSS'] <= status['VmSize'] * 0.30)
+
+    def get_process_status_values(self, pid: int) -> Dict[str, int]:
+        result = {}
+        raw_status = self._get_raw_process_status(pid)
+
+        for line in raw_status.split('\n'):
+            if m := STATUS_VALUE_PATTERN.match(line):
+                result[m.group(1)] = int(m.group(2))
+
+        return result
+
+    def _get_raw_process_status(self, pid: int) -> str:
+        try:
+            with open(f'/proc/{pid}/status', 'r') as f:
+                return f.read()
+        except FileNotFoundError:
+            self.skipTest("Can't open status file of the process")
+
+
+if __name__ == '__main__':
+    MemlockTest.main()
-- 
2.49.0
Re: [PULL 06/10] tests/functional: add memlock tests
Posted by Philippe Mathieu-Daudé 3 weeks, 1 day ago
Hi Alexandr,

On 11/6/25 14:58, Thomas Huth wrote:
> From: Alexandr Moshkov <dtalexundeer@yandex-team.ru>
> 
> Add new tests to check the correctness of the `-overcommit memlock`
> option (possible values: off, on, on-fault) by using
> `/proc/{qemu_pid}/status` file to check in VmSize, VmRSS and VmLck
> values:
> 
> * if `memlock=off`, then VmLck = 0;
> * if `memlock=on`, then VmLck > 0 and almost all memory is resident;
> * if `memlock=on-fault`, then VmLck > 0 and only few memory is resident.
> 
> Signed-off-by: Alexandr Moshkov <dtalexundeer@yandex-team.ru>
> Message-ID: <20250605065908.299979-3-dtalexundeer@yandex-team.ru>
> Signed-off-by: Thomas Huth <thuth@redhat.com>
> ---
>   tests/functional/meson.build     |  1 +
>   tests/functional/test_memlock.py | 79 ++++++++++++++++++++++++++++++++
>   2 files changed, 80 insertions(+)
>   create mode 100755 tests/functional/test_memlock.py
> 
> diff --git a/tests/functional/meson.build b/tests/functional/meson.build
> index 557d59ddf4d..c3fca446cff 100644
> --- a/tests/functional/meson.build
> +++ b/tests/functional/meson.build
> @@ -312,6 +312,7 @@ tests_x86_64_system_quick = [
>     'virtio_version',
>     'x86_cpu_model_versions',
>     'vnc',
> +  'memlock',
>   ]
>   
>   tests_x86_64_system_thorough = [
> diff --git a/tests/functional/test_memlock.py b/tests/functional/test_memlock.py
> new file mode 100755
> index 00000000000..2b515ff979f
> --- /dev/null
> +++ b/tests/functional/test_memlock.py
> @@ -0,0 +1,79 @@
> +#!/usr/bin/env python3
> +#
> +# Functional test that check overcommit memlock options
> +#
> +# Copyright (c) Yandex Technologies LLC, 2025
> +#
> +# Author:
> +#  Alexandr Moshkov <dtalexundeer@yandex-team.ru>
> +#
> +# SPDX-License-Identifier: GPL-2.0-or-later
> +
> +import re
> +
> +from typing import Dict
> +
> +from qemu_test import QemuSystemTest
> +from qemu_test import skipLockedMemoryTest
> +
> +
> +STATUS_VALUE_PATTERN = re.compile(r'^(\w+):\s+(\d+) kB', re.MULTILINE)
> +
> +
> +@skipLockedMemoryTest(2_097_152)  # 2GB
> +class MemlockTest(QemuSystemTest):
> +    """
> +    Runs a guest with memlock options.
> +    Then verify, that this options is working correctly
> +    by checking the status file of the QEMU process.
> +    """
> +
> +    def common_vm_setup_with_memlock(self, memlock):
> +        self.vm.add_args('-overcommit', f'mem-lock={memlock}')

This test fails on Darwin:

qemu-system-x86_64: mlockall: Function not implemented
qemu-system-x86_64: locking memory failed

Please consider using the @skipIfOperatingSystem("Darwin") decorator,
...

> +        self.vm.launch()
> +
> +    def test_memlock_off(self):
> +        self.common_vm_setup_with_memlock('off')
> +
> +        status = self.get_process_status_values(self.vm.get_pid())
> +
> +        self.assertTrue(status['VmLck'] == 0)
> +
> +    def test_memlock_on(self):
> +        self.common_vm_setup_with_memlock('on')
> +
> +        status = self.get_process_status_values(self.vm.get_pid())
> +
> +        # VmLck > 0 kB and almost all memory is resident
> +        self.assertTrue(status['VmLck'] > 0)
> +        self.assertTrue(status['VmRSS'] >= status['VmSize'] * 0.70)
> +
> +    def test_memlock_onfault(self):
> +        self.common_vm_setup_with_memlock('on-fault')
> +
> +        status = self.get_process_status_values(self.vm.get_pid())
> +
> +        # VmLck > 0 kB and only few memory is resident
> +        self.assertTrue(status['VmLck'] > 0)
> +        self.assertTrue(status['VmRSS'] <= status['VmSize'] * 0.30)
> +
> +    def get_process_status_values(self, pid: int) -> Dict[str, int]:
> +        result = {}
> +        raw_status = self._get_raw_process_status(pid)
> +
> +        for line in raw_status.split('\n'):
> +            if m := STATUS_VALUE_PATTERN.match(line):
> +                result[m.group(1)] = int(m.group(2))
> +
> +        return result
> +
> +    def _get_raw_process_status(self, pid: int) -> str:
> +        try:
> +            with open(f'/proc/{pid}/status', 'r') as f:

... or even better implement skipUntilOperatingSystem() and use
it instead, since this test is clearly Linux-focused.

> +                return f.read()
> +        except FileNotFoundError:
> +            self.skipTest("Can't open status file of the process")
> +
> +
> +if __name__ == '__main__':
> +    MemlockTest.main()
Regards,

Phil.
Re: [PULL 06/10] tests/functional: add memlock tests
Posted by Philippe Mathieu-Daudé 3 weeks, 1 day ago
On 24/10/25 09:45, Philippe Mathieu-Daudé wrote:
> Hi Alexandr,
> 
> On 11/6/25 14:58, Thomas Huth wrote:
>> From: Alexandr Moshkov <dtalexundeer@yandex-team.ru>
>>
>> Add new tests to check the correctness of the `-overcommit memlock`
>> option (possible values: off, on, on-fault) by using
>> `/proc/{qemu_pid}/status` file to check in VmSize, VmRSS and VmLck
>> values:
>>
>> * if `memlock=off`, then VmLck = 0;
>> * if `memlock=on`, then VmLck > 0 and almost all memory is resident;
>> * if `memlock=on-fault`, then VmLck > 0 and only few memory is resident.
>>
>> Signed-off-by: Alexandr Moshkov <dtalexundeer@yandex-team.ru>
>> Message-ID: <20250605065908.299979-3-dtalexundeer@yandex-team.ru>
>> Signed-off-by: Thomas Huth <thuth@redhat.com>
>> ---
>>   tests/functional/meson.build     |  1 +
>>   tests/functional/test_memlock.py | 79 ++++++++++++++++++++++++++++++++
>>   2 files changed, 80 insertions(+)
>>   create mode 100755 tests/functional/test_memlock.py
>>
>> diff --git a/tests/functional/meson.build b/tests/functional/meson.build
>> index 557d59ddf4d..c3fca446cff 100644
>> --- a/tests/functional/meson.build
>> +++ b/tests/functional/meson.build
>> @@ -312,6 +312,7 @@ tests_x86_64_system_quick = [
>>     'virtio_version',
>>     'x86_cpu_model_versions',
>>     'vnc',
>> +  'memlock',
>>   ]
>>   tests_x86_64_system_thorough = [
>> diff --git a/tests/functional/test_memlock.py b/tests/functional/ 
>> test_memlock.py
>> new file mode 100755
>> index 00000000000..2b515ff979f
>> --- /dev/null
>> +++ b/tests/functional/test_memlock.py
>> @@ -0,0 +1,79 @@
>> +#!/usr/bin/env python3
>> +#
>> +# Functional test that check overcommit memlock options
>> +#
>> +# Copyright (c) Yandex Technologies LLC, 2025
>> +#
>> +# Author:
>> +#  Alexandr Moshkov <dtalexundeer@yandex-team.ru>
>> +#
>> +# SPDX-License-Identifier: GPL-2.0-or-later
>> +
>> +import re
>> +
>> +from typing import Dict
>> +
>> +from qemu_test import QemuSystemTest
>> +from qemu_test import skipLockedMemoryTest
>> +
>> +
>> +STATUS_VALUE_PATTERN = re.compile(r'^(\w+):\s+(\d+) kB', re.MULTILINE)
>> +
>> +
>> +@skipLockedMemoryTest(2_097_152)  # 2GB
>> +class MemlockTest(QemuSystemTest):
>> +    """
>> +    Runs a guest with memlock options.
>> +    Then verify, that this options is working correctly
>> +    by checking the status file of the QEMU process.
>> +    """
>> +
>> +    def common_vm_setup_with_memlock(self, memlock):
>> +        self.vm.add_args('-overcommit', f'mem-lock={memlock}')
> 
> This test fails on Darwin:
> 
> qemu-system-x86_64: mlockall: Function not implemented
> qemu-system-x86_64: locking memory failed
> 
> Please consider using the @skipIfOperatingSystem("Darwin") decorator,
> ...
> 
>> +        self.vm.launch()
>> +
>> +    def test_memlock_off(self):
>> +        self.common_vm_setup_with_memlock('off')
>> +
>> +        status = self.get_process_status_values(self.vm.get_pid())
>> +
>> +        self.assertTrue(status['VmLck'] == 0)
>> +
>> +    def test_memlock_on(self):
>> +        self.common_vm_setup_with_memlock('on')
>> +
>> +        status = self.get_process_status_values(self.vm.get_pid())
>> +
>> +        # VmLck > 0 kB and almost all memory is resident
>> +        self.assertTrue(status['VmLck'] > 0)
>> +        self.assertTrue(status['VmRSS'] >= status['VmSize'] * 0.70)
>> +
>> +    def test_memlock_onfault(self):
>> +        self.common_vm_setup_with_memlock('on-fault')
>> +
>> +        status = self.get_process_status_values(self.vm.get_pid())
>> +
>> +        # VmLck > 0 kB and only few memory is resident
>> +        self.assertTrue(status['VmLck'] > 0)
>> +        self.assertTrue(status['VmRSS'] <= status['VmSize'] * 0.30)
>> +
>> +    def get_process_status_values(self, pid: int) -> Dict[str, int]:
>> +        result = {}
>> +        raw_status = self._get_raw_process_status(pid)
>> +
>> +        for line in raw_status.split('\n'):
>> +            if m := STATUS_VALUE_PATTERN.match(line):
>> +                result[m.group(1)] = int(m.group(2))
>> +
>> +        return result
>> +
>> +    def _get_raw_process_status(self, pid: int) -> str:
>> +        try:
>> +            with open(f'/proc/{pid}/status', 'r') as f:
> 
> ... or even better implement skipUntilOperatingSystem() and use

Sorry, I meant:

   skipUntilOperatingSystem -> skipUnlessOperatingSystem

> it instead, since this test is clearly Linux-focused.
> 
>> +                return f.read()
>> +        except FileNotFoundError:
>> +            self.skipTest("Can't open status file of the process")
>> +
>> +
>> +if __name__ == '__main__':
>> +    MemlockTest.main()
> Regards,
> 
> Phil.
>