Skip to contentSkip to conversation

tests.cgroup: 3 new subtests

kvm-autotest New Lukas Doktor 1 participant 4 messages Nov 7, 2011 KVM

lore
Share & tools
lore
#0 lore raw

Hi guys,

I have three new subtests for kvm cgroup test. First one tests the freezer cgroup subsystem, the two following tests the memory cgroup subsystem.

* Add TestFreezer subtest (second pass)
* Add MemoryMove subtest
* Add MemoryLimit subtest
* New assign_vm_into_cgroup() function
* Minor code cleanup

Feel free to comment on pull request:
https://github.com/autotest/autotest/pull/59

Regards,
Lukáš Doktor

#1 lore raw

PATCH 1/3 tests.cgroup: Add TestFreezer

This subtest tests the 'freezer.state' cgroup functionality. It assignes the virtual machine into freezer cgroup, schedule cpu-intensive work and verifies the correct function in both states (frozen/thawed) during long and short period of time.

* Adds TestFreezer (freezer) test
* Adds assign_vm_into_cgroup() function
* Code cleanup

---
 client/tests/cgroup/cgroup_common.py |   16 +---
 client/tests/kvm/tests/cgroup.py     |  158 +++++++++++++++++++++++++++++++---
 2 files changed, 145 insertions(+), 29 deletions(-)
client/tests/cgroup/cgroup_common.py +1 -15
index 0c86398..7d5daf2 100755
@@ -6,25 +6,11 @@ Helpers for cgroup testing.
 @copyright: 2011 Red Hat Inc.
 @author: Lukas Doktor <[email protected]>
 """
-import logging, os, shutil, subprocess, time, traceback
+import logging, os, shutil, subprocess, time
 from tempfile import mkdtemp
 from autotest_lib.client.bin import utils
 from autotest_lib.client.common_lib import error
 
-def _traceback(name, exc_info):
-    """
-    Formats traceback into lines "name: line\nname: line"
-    @param name: desired line preposition
-    @param exc_info: sys.exc_info of the exception
-    @return: string which contains beautifully formatted exception
-    """
-    out = "\n"
-    for line in traceback.format_exception(exc_info[0], exc_info[1],
-                                           exc_info[2]):
-        out += "%s: %s" % (name, line)
-    return out
-
-
 class Cgroup(object):
     """
     Cgroup handling class.
client/tests/kvm/tests/cgroup.py +144 -14

204 lines not on this page · raw message

Signature · 1 line
1.7.6.4
#2 lore raw

PATCH 2/3 tests.cgroup: Add MemoryMove test

In reply to Lukas Doktor

This subtest tests the memory.move_charge_at_immigrate cgroup capability. It changes memory cgroup while running memory intense app inside the guest.

* Adds TestMemoryMove test

---
 client/tests/kvm/tests/cgroup.py |   85 +++++++++++++++++++++++++++++++++++++-
 1 files changed, 84 insertions(+), 1 deletions(-)
client/tests/kvm/tests/cgroup.py +84 -1
index fd36f9a..547e4e8 100644
@@ -8,6 +8,7 @@ from random import random
 from autotest_lib.client.common_lib import error
 from autotest_lib.client.bin import utils
 from autotest_lib.client.tests.cgroup.cgroup_common import Cgroup, CgroupModules
+from autotest_lib.client.virt.aexpect import ExpectTimeoutError
 
 def run_cgroup(test, params, env):
     """
@@ -894,6 +895,87 @@ def run_cgroup(test, params, env):
             return ("Freezer works fine")
 
 
+    class TestMemoryMove:
+        """
+        Tests the memory.move_charge_at_immigrate cgroup capability. It changes
+        memory cgroup while running the guest system.
+        """
+        def __init__(self, vms, modules):
+            """
+            Initialization
+            @param vms: list of vms
+            @param modules: initialized cgroup module class
+            """
+            self.vm = vms[0]      # Virt machines
+            self.modules = modules          # cgroup module handler
+            self.cgroup = Cgroup('memory', '')   # cgroup blkio handler
+
+        def cleanup(self):
+            """ Cleanup """
+            err = ""
+            try:
+                del(self.cgroup)
+            except Exception, failure_detail:
+                err += "\nCan't remove Cgroup: %s" % failure_detail
+
+            if err:
+                logging.error("Some cleanup operations failed: %s", err)
+                raise error.TestError("Some cleanup operations failed: %s" %
+                                       err)
+
+
+        def init(self):
+            """ Initialization: prepares two cgroups """
+            self.cgroup.initialize(self.modules)
+            self.cgroup.mk_cgroup()
+            self.cgroup.mk_cgroup()
+            assign_vm_into_cgroup(self.vm, self.cgroup, 0)
+
+            self.cgroup.set_property('memory.move_charge_at_immigrate', '3',
+                                     self.cgroup.cgroups[0])
+            self.cgroup.set_property('memory.move_charge_at_immigrate', '3',
+                                     self.cgroup.cgroups[1])
+
+
+        def run(self):
+            """ Actual test: change cgroup while running test command """
+            sessions = []
+            sessions.append(self.vm.wait_for_login(timeout=30))
+            sessions.append(self.vm.wait_for_login(timeout=30))
+
+            size = int(params.get('mem', 512)) / 2   # Use half of the memory
+            sessions[0].sendline('dd if=/dev/zero of=/dev/null bs=%sM' % size)
+            time.sleep(2)
+
+            sessions[1].cmd('killall -SIGUSR1 dd')
+            for i in range(10):
+                logging.debug("Moving vm into cgroup %s." % (i%2))
+                assign_vm_into_cgroup(self.vm, self.cgroup, i%2)
+                time.sleep(0.1)
+            time.sleep(2)
+            sessions[1].cmd('killall -SIGUSR1 dd')
+            try:
+                out = sessions[0].read_until_output_matches(
+                                                ['(\d+)\+\d records out'])[1]
+                if len(re.findall(r'(\d+)\+\d records out', out)) < 2:
+                    out += sessions[0].read_until_output_matches(
+                                                ['(\d+)\+\d records out'])[1]
+            except ExpectTimeoutError:
+                raise error.TestFail("dd didn't produce expected output: %s" %
+                                      out)
+
+            sessions[1].cmd('killall dd')
+            dd_res = re.findall(r'(\d+)\+(\d+) records in', out)
+            dd_res += re.findall(r'(\d+)\+(\d+) records out', out)
+            dd_res = [int(_[0]) + int(_[1]) for _ in dd_res]
+            if dd_res[1] <= dd_res[0] or dd_res[3] <= dd_res[2]:
+                raise error.TestFail("dd stoped sending bytes: %s..%s, %s..%s" %
+                                      (dd_res[0], dd_res[1], dd_res[2],
+                                       dd_res[3]))
+
+            return ("Memory move succeeded")
+
+
     # Setup
     # TODO: Add all new tests here
     tests = {"blkio_bandwidth_weigth_read"  : TestBlkioBandwidthWeigthRead,
@@ -904,9 +986,10 @@ def run_cgroup(test, params, env):
              "blkio_throttle_multiple_write" : TestBlkioThrottleMultipleWrite,
              "devices_access"               : TestDevicesAccess,
              "freezer"                      : TestFreezer,
+             "memory_move"                  : TestMemoryMove,
             }
     modules = CgroupModules()
-    if (modules.init(['blkio', 'devices', 'freezer']) <= 0):
+    if (modules.init(['blkio', 'devices', 'freezer', 'memory']) <= 0):
         raise error.TestFail('Can\'t mount any cgroup modules')
     # Add all vms
     vms = []
Signature · 1 line
1.7.6.4
#3 lore raw

PATCH 3/3 tests.cgroup: Add MemoryLimit test

In reply to Lukas Doktor

This subtest tests the memory.limit_in_bytes cgroup capability by running VM which demands more memory than permitted by it's cgroup. Everything over the limit should be swapped out.

* Adds TestMemoryLimit test

---
 client/tests/kvm/tests/cgroup.py |  106 ++++++++++++++++++++++++++++++++++++++
 1 files changed, 106 insertions(+), 0 deletions(-)
client/tests/kvm/tests/cgroup.py +106 -0
index 547e4e8..6880c2c 100644
@@ -9,6 +9,8 @@ from autotest_lib.client.common_lib import error
 from autotest_lib.client.bin import utils
 from autotest_lib.client.tests.cgroup.cgroup_common import Cgroup, CgroupModules
 from autotest_lib.client.virt.aexpect import ExpectTimeoutError
+from autotest_lib.client.virt.aexpect import ExpectProcessTerminatedError
+
 
 def run_cgroup(test, params, env):
     """
@@ -976,6 +978,109 @@ def run_cgroup(test, params, env):
             return ("Memory move succeeded")
 
 
+    class TestMemoryLimit:
+        """ Tests the memory.limit_in_bytes by triyng to break the limit """
+        def __init__(self, vms, modules):
+            """
+            Initialization
+            @param vms: list of vms
+            @param modules: initialized cgroup module class
+            """
+            self.vm = vms[0]      # Virt machines
+            self.modules = modules          # cgroup module handler
+            self.cgroup = Cgroup('memory', '')   # cgroup blkio handler
+
+
+        def cleanup(self):
+            """ Cleanup """
+            err = ""
+            try:
+                del(self.cgroup)
+            except Exception, failure_detail:
+                err += "\nCan't remove Cgroup: %s" % failure_detail
+
+            if err:
+                logging.error("Some cleanup operations failed: %s", err)
+                raise error.TestError("Some cleanup operations failed: %s" %
+                                       err)
+
+
+        def init(self):
+            """
+            Initialization: prepares the cgroup and starts new VM inside it.
+            """
+            # Use half of the VM's memory (in KB)
+            mem = int(int(params.get('mem', 1024)) * 512)
+            self.cgroup.initialize(self.modules)
+            self.cgroup.mk_cgroup()
+            self.cgroup.set_property('memory.move_charge_at_immigrate', '3',
+                                     self.cgroup.cgroups[0])
+            self.cgroup.set_property_h('memory.limit_in_bytes', "%sK" % mem,
+                                     self.cgroup.cgroups[0])
+
+            logging.info("Expected VM reload")
+            try:
+                self.vm.create()
+            except Exception, failure_detail:
+                raise error.TestFail("init: Failed to recreate the VM: %s" %
+                                      failure_detail)
+            assign_vm_into_cgroup(self.vm, self.cgroup, 0)
+            timeout = int(params.get("login_timeout", 360))
+            self.vm.wait_for_login(timeout=timeout).close()
+            status = open('/proc/%s/status' % self.vm.get_pid(), 'r').read()
+            rss = int(re.search(r'VmRSS:[\t ]*(\d+) kB', status).group(1))
+            if rss > mem:
+                raise error.TestFail("Init failed to move VM into cgroup, VmRss"
+                                     "=%s, expected=%s" % (rss, mem))
+
+        def run(self):
+            """
+            Run dd with bs > memory limit. Verify that qemu survives and
+            success in executing the command without breaking off the limit.
+            """
+            session = self.vm.wait_for_login(timeout=30)
+
+            # Convert into KB, use 0.6 * guest memory (== * 614.4)
+            mem = int(int(params.get('mem', 1024)) * 615)
+            session.sendline('dd if=/dev/zero of=/dev/null bs=%sK count=1' %mem)
+
+            # Check every 0.1s VM memory usage. Limit the maximum execution time
+            # to mem / 10 (== mem * 0.1 sleeps)
+            max_rss = 0
+            max_swap = 0
+            out = ""
+            for _ in range(int(mem / 1024)):
+                status = open('/proc/%s/status' % self.vm.get_pid(), 'r').read()
+                rss = int(re.search(r'VmRSS:[\t ]*(\d+) kB', status).group(1))
+                max_rss = max(rss, max_rss)
+                swap = int(re.search(r'VmSwap:[\t ]*(\d+) kB', status).group(1))
+                max_swap = max(swap + rss, max_swap)
+                try:
+                    out += session.read_up_to_prompt(timeout=0.1)
+                except ExpectTimeoutError:
+                    #0.1s passed, lets begin the next round
+                    pass
+                except ExpectProcessTerminatedError, failure_detail:
+                    raise error.TestFail("VM failed executing the command: %s" %
+                                          failure_detail)
+                else:
+                    break
+
+            if max_rss > mem:
+                raise error.TestFail("The limit was broken: max_rss=%s, limit="
+                                     "%s" % (max_rss, mem))
+            exit_nr = session.cmd_output("echo $?")[:-1]
+            if exit_nr != '0':
+                raise error.TestFail("dd command failed: %s, output: %s" %
+                                      (exit_nr, out))
+            if (max_rss + max_swap) < mem:
+                raise error.TestFail("VM didn't consume expected amount of "
+                                     "memory. Output of dd cmd: %s" % out)
+
+            return ("Limits were enforced successfully.")
+
+
+
     # Setup
     # TODO: Add all new tests here
     tests = {"blkio_bandwidth_weigth_read"  : TestBlkioBandwidthWeigthRead,
@@ -987,6 +1092,7 @@ def run_cgroup(test, params, env):
              "devices_access"               : TestDevicesAccess,
              "freezer"                      : TestFreezer,
              "memory_move"                  : TestMemoryMove,
+             "memory_limit"                 : TestMemoryLimit,
             }
     modules = CgroupModules()
     if (modules.init(['blkio', 'devices', 'freezer', 'memory']) <= 0):
Signature · 1 line
1.7.6.4