From patchwork Fri Aug 7 21:31:07 2009 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Goldish X-Patchwork-Id: 40026 Received: from vger.kernel.org (vger.kernel.org [209.132.176.167]) by demeter.kernel.org (8.14.2/8.14.2) with ESMTP id n77LSdcK017832 for ; Fri, 7 Aug 2009 21:28:39 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753752AbZHGV2f (ORCPT ); Fri, 7 Aug 2009 17:28:35 -0400 Received: (majordomo@vger.kernel.org) by vger.kernel.org id S1753778AbZHGV2f (ORCPT ); Fri, 7 Aug 2009 17:28:35 -0400 Received: from mx2.redhat.com ([66.187.237.31]:49988 "EHLO mx2.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753649AbZHGV2e (ORCPT ); Fri, 7 Aug 2009 17:28:34 -0400 Received: from int-mx2.corp.redhat.com (int-mx2.corp.redhat.com [172.16.27.26]) by mx2.redhat.com (8.13.8/8.13.8) with ESMTP id n77LSXqR017968; Fri, 7 Aug 2009 17:28:33 -0400 Received: from ns3.rdu.redhat.com (ns3.rdu.redhat.com [10.11.255.199]) by int-mx2.corp.redhat.com (8.13.1/8.13.1) with ESMTP id n77LSW1O007813; Fri, 7 Aug 2009 17:28:32 -0400 Received: from localhost.localdomain (dhcp-1-31.tlv.redhat.com [10.35.1.31]) by ns3.rdu.redhat.com (8.13.8/8.13.8) with ESMTP id n77LSSe2013135; Fri, 7 Aug 2009 17:28:29 -0400 From: Michael Goldish To: autotest@test.kernel.org, kvm@vger.kernel.org Cc: Michael Goldish Subject: [KVM-AUTOTEST PATCH CORRECTION] KVM test: timedrift: Use helper functions to make the code more readable Date: Sat, 8 Aug 2009 00:31:07 +0300 Message-Id: <7a311876172178ea74363ee5ad837813a97aa425.1249680609.git.mgoldish@redhat.com> X-Scanned-By: MIMEDefang 2.58 on 172.16.27.26 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org 1. As suggested by Yolkfull Chow, use a get_time() helper function that returns the current host and guest times. 2. Use helper functions that set and restore the CPU affinity of a process. 3. In these helper functions, set the affinity of all threads of the process, not just the main thread. Signed-off-by: Michael Goldish --- client/tests/kvm/kvm_tests.py | 77 +++++++++++++++++++++++++++++------------ 1 files changed, 55 insertions(+), 22 deletions(-) diff --git a/client/tests/kvm/kvm_tests.py b/client/tests/kvm/kvm_tests.py index 308db97..259555e 100644 --- a/client/tests/kvm/kvm_tests.py +++ b/client/tests/kvm/kvm_tests.py @@ -604,6 +604,51 @@ def run_timedrift(test, params, env): @param params: Dictionary with test parameters. @param env: Dictionary with the test environment. """ + # Helper functions + def set_cpu_affinity(pid, mask): + """ + Set the CPU affinity of all threads of the process with PID pid. + + @param pid: The process ID. + @param mask: The CPU affinity mask. + @return: A dict containing the previous mask for each thread. + """ + tids = commands.getoutput("ps -L --pid=%s -o lwp=" % pid).split() + prev_masks = {} + for tid in tids: + prev_mask = commands.getoutput("taskset -p %s" % tid).split()[-1] + prev_masks[tid] = prev_mask + commands.getoutput("taskset -p %s %s" % (mask, tid)) + return prev_masks + + def restore_cpu_affinity(prev_masks): + """ + Restore the CPU affinity of several threads. + + @param prev_masks: A dict containing TIDs as keys and masks as values. + """ + for tid, mask in prev_masks.items(): + commands.getoutput("taskset -p %s %s" % (mask, tid)) + + def get_time(session, time_command, time_filter_re, time_format): + """ + Returns the host time and guest time. + + @param session: A shell session. + @param time_command: Command to issue to get the current guest time. + @param time_filter_re: Regex filter to apply on the output of + time_command in order to get the current time. + @param time_format: Format string to pass to time.strptime() with the + result of the regex filter. + @return: A tuple containing the host time and guest time. + """ + host_time = time.time() + session.sendline(time_command) + (match, s) = session.read_up_to_prompt() + s = re.findall(time_filter_re, s)[0] + guest_time = time.mktime(time.strptime(s, time_format)) + return (host_time, guest_time) + vm = kvm_utils.env_get_vm(env, params.get("main_vm")) if not vm: raise error.TestError("VM object not found in environment") @@ -641,19 +686,13 @@ def run_timedrift(test, params, env): guest_load_sessions = [] host_load_sessions = [] - # Remember the VM's previous CPU affinity - prev_cpu_mask = commands.getoutput("taskset -p %s" % vm.get_pid()) - prev_cpu_mask = prev_cpu_mask.split()[-1] # Set the VM's CPU affinity - commands.getoutput("taskset -p %s %s" % (cpu_mask, vm.get_pid())) + prev_affinity = set_cpu_affinity(vm.get_pid(), cpu_mask) try: # Get time before load - host_time_0 = time.time() - session.sendline(time_command) - (match, s) = session.read_up_to_prompt() - s = re.findall(time_filter_re, s)[0] - guest_time_0 = time.mktime(time.strptime(s, time_format)) + (host_time_0, guest_time_0) = get_time(session, time_command, + time_filter_re, time_format) # Run some load on the guest logging.info("Starting load on guest...") @@ -676,22 +715,19 @@ def run_timedrift(test, params, env): timeout=0.5)) # Set the CPU affinity of the shell running the load process pid = host_load_sessions[-1].get_shell_pid() - commands.getoutput("taskset -p %s %s" % (cpu_mask, pid)) + set_cpu_affinity(pid, cpu_mask) # Try setting the CPU affinity of the load process itself pid = host_load_sessions[-1].get_pid() if pid: - commands.getoutput("taskset -p %s %s" % (cpu_mask, pid)) + set_cpu_affinity(pid, cpu_mask) # Sleep for a while (during load) logging.info("Sleeping for %s seconds..." % load_duration) time.sleep(load_duration) # Get time delta after load - host_time_1 = time.time() - session.sendline(time_command) - (match, s) = session.read_up_to_prompt() - s = re.findall(time_filter_re, s)[0] - guest_time_1 = time.mktime(time.strptime(s, time_format)) + (host_time_1, guest_time_1) = get_time(session, time_command, + time_filter_re, time_format) # Report results host_delta = host_time_1 - host_time_0 @@ -704,7 +740,7 @@ def run_timedrift(test, params, env): finally: logging.info("Cleaning up...") # Restore the VM's CPU affinity - commands.getoutput("taskset -p %s %s" % (prev_cpu_mask, vm.get_pid())) + restore_cpu_affinity(prev_affinity) # Stop the guest load if guest_load_stop_command: session.get_command_output(guest_load_stop_command) @@ -719,11 +755,8 @@ def run_timedrift(test, params, env): time.sleep(rest_duration) # Get time after rest - host_time_2 = time.time() - session.sendline(time_command) - (match, s) = session.read_up_to_prompt() - s = re.findall(time_filter_re, s)[0] - guest_time_2 = time.mktime(time.strptime(s, time_format)) + (host_time_2, guest_time_2) = get_time(session, time_command, + time_filter_re, time_format) # Report results host_delta_total = host_time_2 - host_time_0