Skip to content

Commit

Permalink
Merge pull request avocado-framework#3598 from Yingshun/mem_utils
Browse files Browse the repository at this point in the history
Add supports for memory related tests
  • Loading branch information
chloerh authored Jan 17, 2023
2 parents 751289d + a18cdec commit 2203589
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions virttest/utils_libvirt/libvirt_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,51 @@

import logging

from avocado.core import exceptions
from avocado.utils import process

LOG = logging.getLogger('avocado.' + __name__)


def comp_memlock(exp_memlock):
"""
Compare the locked mem with the given value.
:param exp_memlock: The expected locked mem value
:raise: TestError if the actual locked mem is invalid
:return: True on success
"""
LOG.debug("Check if the memlock is %s.", exp_memlock)
tmp_act_memlock = get_qemu_process_memlock_hard_limit()
LOG.debug("Actual memlock is {}.".format(tmp_act_memlock))
try:
act_memlock = int(tmp_act_memlock)
except ValueError as e:
raise exceptions.TestError(e)
return exp_memlock == act_memlock


def get_qemu_process_memlock_hard_limit():
"""
Get qemu process memlock hard limit
"""
cmd = "prlimit -p `pidof qemu-kvm` -l |awk '/MEMLOCK/ {print $7}'"
return process.run(cmd, shell=True).stdout_text.strip()


def normalize_mem_size(mem_size, mem_unit):
"""
Normalize the mem size and convert it to bytes.
:param mem_size: The mem size
:param mem_unit: The mem size unit
:return: Byte format size
"""
try:
mem_size = float(mem_size)
mem_unit_idx = ['B', 'K', 'M', 'G', 'T'].index(mem_unit[0].upper())
except ValueError as e:
raise exceptions.TestError(e)

return int(mem_size * 1024 ** mem_unit_idx)

0 comments on commit 2203589

Please sign in to comment.