[PATCH] KVM test: KSM (kernel shared memory) overcommit test

2010-02-10 Thread Jiri Zupka
FIX:
  Patch is based on The previous version [PATCH] KVM test: KSM (kernel shared 
memory) test   
  developed overcommit Lucas Meneghel Rodrigues.
  The only fix is change of behavior with large overcommit. Python has a 
problem in allocating 
  memory even when is still enough space in swap (300MB etc). 
  Change is on line 150 in file ksm_overcommit.py .
diff --git a/client/tests/kvm/kvm_test_utils.py b/client/tests/kvm/kvm_test_utils.py
index 02ec0cf..7d96d6e 100644
--- a/client/tests/kvm/kvm_test_utils.py
+++ b/client/tests/kvm/kvm_test_utils.py
@@ -22,7 +22,8 @@ More specifically:
 
 
 import time, os, logging, re, commands
-from autotest_lib.client.common_lib import utils, error
+from autotest_lib.client.common_lib import error
+from autotest_lib.client.bin import utils
 import kvm_utils, kvm_vm, kvm_subprocess
 
 
@@ -203,3 +204,36 @@ def get_time(session, time_command, time_filter_re, time_format):
 s = re.findall(time_filter_re, s)[0]
 guest_time = time.mktime(time.strptime(s, time_format))
 return (host_time, guest_time)
+
+
+def get_memory_info(lvms):
+
+Get memory information from host and guests in format:
+Host: memfree = XXXM; Guests memsh = {XXX,XXX,...}
+
+@params lvms: List of VM objects
+@return: String with memory info report
+
+if not isinstance(lvms, list):
+raise error.TestError(Invalid list passed to get_stat: %s  % lvms)
+
+try:
+meminfo = Host: memfree = 
+meminfo += str(int(utils.freememtotal()) / 1024) + M; 
+meminfo += swapfree = 
+mf = int(utils.read_from_meminfo(SwapFree)) / 1024
+meminfo += str(mf) + M; 
+except Exception, e:
+raise error.TestFail(Could not fetch host free memory info, 
+ reason: %s % e)
+
+meminfo += Guests memsh = {
+for vm in lvms:
+shm = vm.get_shared_meminfo()
+if shm is None:
+raise error.TestError(Could not get shared meminfo from 
+  VM %s % vm)
+meminfo += %dM;  % shm
+meminfo = meminfo[0:-2] + }
+
+return meminfo
diff --git a/client/tests/kvm/kvm_utils.py b/client/tests/kvm/kvm_utils.py
index df26a77..c9cd2e1 100644
--- a/client/tests/kvm/kvm_utils.py
+++ b/client/tests/kvm/kvm_utils.py
@@ -713,6 +713,22 @@ def generate_random_string(length):
 return str
 
 
+def generate_tmp_file_name(file, ext=None, dir='/tmp/'):
+
+Returns a temporary file name. The file is not created.
+
+while True:
+file_name = (file + '-' + time.strftime(%Y%m%d-%H%M%S-) +
+ generate_random_string(4))
+if ext:
+file_name += '.' + ext
+file_name = os.path.join(dir, file_name)
+if not os.path.exists(file_name):
+break
+
+return file_name
+
+
 def format_str_for_message(str):
 
 Format str so that it can be appended to a message.
diff --git a/client/tests/kvm/kvm_vm.py b/client/tests/kvm/kvm_vm.py
index 6731927..5790dff 100755
--- a/client/tests/kvm/kvm_vm.py
+++ b/client/tests/kvm/kvm_vm.py
@@ -760,6 +760,23 @@ class VM:
 return self.process.get_pid()
 
 
+def get_shared_meminfo(self):
+
+Returns the VM's shared memory information.
+
+@return: Shared memory used by VM (MB)
+
+if self.is_dead():
+logging.error(Could not get shared memory info from dead VM.)
+return None
+
+cmd = cat /proc/%d/statm % self.params.get('pid_' + self.name)
+shm = int(os.popen(cmd).readline().split()[2])
+# statm stores informations in pages, translate it to MB
+shm = shm * 4 / 1024
+return shm
+
+
 def remote_login(self, nic_index=0, timeout=10):
 
 Log into the guest via SSH/Telnet/Netcat.
diff --git a/client/tests/kvm/scripts/allocator.py b/client/tests/kvm/scripts/allocator.py
new file mode 100644
index 000..e0b8c75
--- /dev/null
+++ b/client/tests/kvm/scripts/allocator.py
@@ -0,0 +1,230 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+
+Auxiliary script used to allocate memory on guests.
+
+...@copyright: 2008-2009 Red Hat Inc.
+...@author: Jiri Zupka (jzu...@redhat.com)
+
+
+
+import os, array, sys, struct, random, copy, inspect, tempfile, datetime
+
+PAGE_SIZE = 4096 # machine page size
+
+
+class MemFill(object):
+
+Fills guest memory according to certain patterns.
+
+def __init__(self, mem, static_value, random_key):
+
+Constructor of MemFill class.
+
+@param mem: Amount of test memory in MB.
+@param random_key: Seed of random series used for fill up memory.
+@param static_value: Value used to fill all memory.
+
+if (static_value  0 or static_value  255):
+print (FAIL: Initialization static value
+   can be only in range (0..255))
+return
+
+self.tmpdp = tempfile.mkdtemp()
+ret_code = os.system(mount -o 

Re: [PATCH] KVM test: KSM (kernel shared memory) overcommit test

2010-02-10 Thread Lucas Meneghel Rodrigues
On Wed, 2010-02-10 at 11:13 -0500, Jiri Zupka wrote:
 FIX:
   Patch is based on The previous version [PATCH] KVM test: KSM (kernel shared 
 memory) test   
   developed overcommit Lucas Meneghel Rodrigues.
   The only fix is change of behavior with large overcommit. Python has a 
 problem in allocating 
   memory even when is still enough space in swap (300MB etc). 
   Change is on line 150 in file ksm_overcommit.py .

Ok Jiri, thanks, I am going to focus on this test this week so we can
get it commited ASAP. 

Sorry it's taking long, but it's a large and complex test, and I want to
be as careful as possible.

Cheers,

Lucas

--
To unsubscribe from this list: send the line unsubscribe kvm in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH] KVM test: KSM (kernel shared memory) overcommit test

2010-01-12 Thread Lucas Meneghel Rodrigues
Functionality:
 KSM test start guests. They are connect to guest over ssh.
 Copy and run allocator.py to guests.
 Host can run any python command over allocator.py loop on client side.

 Start run_ksm_overcommit.
 Define host and guest reserve variables (host_reserver,guest_reserver).
 Calculate amount of virtual machine and their memory based on variables
 host_mem and overcommit.
 Check KSM status.
 Create and start virtual guests.
 Test :
  a] serial
   1) initialize, merge all mem to single page
   2) separate first guset mem
   3) separate rest of guest up to fill all mem
   4) kill all guests except for the last
   5) check if mem of last guest is ok
   6) kill guest
  b] parallel
   1) initialize, merge all mem to single page
   2) separate mem of guest
   3) verification of guest mem
   4) merge mem to one block
   5) verification of guests mem
   6) separate mem of guests by 96B
   7) check if mem is all right
   8) kill guest
 allocator.py (client side script)
   After start they wait for command witch they make in client side.
   mem_fill class implement commands to fill, check mem and return
   error to host.

Signed-off-by: Jiri Zupka jzu...@redhat.com
Signed-off-by: Lukáš Doktorldok...@redhat.com
---
 client/tests/kvm/kvm_test_utils.py   |   36 ++-
 client/tests/kvm/kvm_utils.py|   16 +
 client/tests/kvm/kvm_vm.py   |   17 +
 client/tests/kvm/scripts/allocator.py|  230 
 client/tests/kvm/tests/ksm_overcommit.py |  573 ++
 client/tests/kvm/tests_base.cfg.sample   |   18 +
 6 files changed, 889 insertions(+), 1 deletions(-)
 create mode 100644 client/tests/kvm/scripts/allocator.py
 create mode 100644 client/tests/kvm/tests/ksm_overcommit.py

diff --git a/client/tests/kvm/kvm_test_utils.py 
b/client/tests/kvm/kvm_test_utils.py
index 02ec0cf..7d96d6e 100644
--- a/client/tests/kvm/kvm_test_utils.py
+++ b/client/tests/kvm/kvm_test_utils.py
@@ -22,7 +22,8 @@ More specifically:
 
 
 import time, os, logging, re, commands
-from autotest_lib.client.common_lib import utils, error
+from autotest_lib.client.common_lib import error
+from autotest_lib.client.bin import utils
 import kvm_utils, kvm_vm, kvm_subprocess
 
 
@@ -203,3 +204,36 @@ def get_time(session, time_command, time_filter_re, 
time_format):
 s = re.findall(time_filter_re, s)[0]
 guest_time = time.mktime(time.strptime(s, time_format))
 return (host_time, guest_time)
+
+
+def get_memory_info(lvms):
+
+Get memory information from host and guests in format:
+Host: memfree = XXXM; Guests memsh = {XXX,XXX,...}
+
+@params lvms: List of VM objects
+@return: String with memory info report
+
+if not isinstance(lvms, list):
+raise error.TestError(Invalid list passed to get_stat: %s  % lvms)
+
+try:
+meminfo = Host: memfree = 
+meminfo += str(int(utils.freememtotal()) / 1024) + M; 
+meminfo += swapfree = 
+mf = int(utils.read_from_meminfo(SwapFree)) / 1024
+meminfo += str(mf) + M; 
+except Exception, e:
+raise error.TestFail(Could not fetch host free memory info, 
+ reason: %s % e)
+
+meminfo += Guests memsh = {
+for vm in lvms:
+shm = vm.get_shared_meminfo()
+if shm is None:
+raise error.TestError(Could not get shared meminfo from 
+  VM %s % vm)
+meminfo += %dM;  % shm
+meminfo = meminfo[0:-2] + }
+
+return meminfo
diff --git a/client/tests/kvm/kvm_utils.py b/client/tests/kvm/kvm_utils.py
index 2bbbe22..0118742 100644
--- a/client/tests/kvm/kvm_utils.py
+++ b/client/tests/kvm/kvm_utils.py
@@ -713,6 +713,22 @@ def generate_random_string(length):
 return str
 
 
+def generate_tmp_file_name(file, ext=None, dir='/tmp/'):
+
+Returns a temporary file name. The file is not created.
+
+while True:
+file_name = (file + '-' + time.strftime(%Y%m%d-%H%M%S-) +
+ generate_random_string(4))
+if ext:
+file_name += '.' + ext
+file_name = os.path.join(dir, file_name)
+if not os.path.exists(file_name):
+break
+
+return file_name
+
+
 def format_str_for_message(str):
 
 Format str so that it can be appended to a message.
diff --git a/client/tests/kvm/kvm_vm.py b/client/tests/kvm/kvm_vm.py
index 7229b79..e52f826 100755
--- a/client/tests/kvm/kvm_vm.py
+++ b/client/tests/kvm/kvm_vm.py
@@ -690,6 +690,23 @@ class VM:
 return self.process.get_pid()
 
 
+def get_shared_meminfo(self):
+
+Returns the VM's shared memory information.
+
+@return: Shared memory used by VM (MB)
+
+if self.is_dead():
+logging.error(Could not get shared memory info from dead VM.)
+return None
+
+cmd = cat /proc/%d/statm % self.params.get('pid_' + self.name)
+shm = int(os.popen(cmd).readline().split()[2])
+# statm