From: Michael Goldish Subject: [KVM-AUTOTEST PATCH 6/6] KVM test: kvm_subprocess: use select() in read_until_output_matches() Date: Tue, 29 Sep 2009 22:04:27 +0200 Message-ID: <1254254667-19385-6-git-send-email-mgoldish@redhat.com> References: <1254254667-19385-1-git-send-email-mgoldish@redhat.com> <1254254667-19385-2-git-send-email-mgoldish@redhat.com> <1254254667-19385-3-git-send-email-mgoldish@redhat.com> <1254254667-19385-4-git-send-email-mgoldish@redhat.com> <1254254667-19385-5-git-send-email-mgoldish@redhat.com> Cc: Michael Goldish To: autotest@test.kernel.org, kvm@vger.kernel.org Return-path: Received: from mx1.redhat.com ([209.132.183.28]:1028 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752901AbZI2UIF (ORCPT ); Tue, 29 Sep 2009 16:08:05 -0400 In-Reply-To: <1254254667-19385-5-git-send-email-mgoldish@redhat.com> Sender: kvm-owner@vger.kernel.org List-ID: Currently read_nonblocking() is called repeatedly until a match is found. This is fine as long as internal_timeout, the timeout parameter passed to read_nonblocking(), is greater than zero. If it equals zero the loop will keep the CPU busy and stress the host. To avoid this, use select() to wait until there's output to read from the child process. Signed-off-by: Michael Goldish --- client/tests/kvm/kvm_subprocess.py | 9 +++++++-- 1 files changed, 7 insertions(+), 2 deletions(-) diff --git a/client/tests/kvm/kvm_subprocess.py b/client/tests/kvm/kvm_subprocess.py index 730f20e..2ac062a 100755 --- a/client/tests/kvm/kvm_subprocess.py +++ b/client/tests/kvm/kvm_subprocess.py @@ -848,8 +848,12 @@ class kvm_expect(kvm_tail): match = None data = "" + fd = self._get_fd("expect") end_time = time.time() + timeout - while time.time() < end_time: + while True: + r, w, x = select.select([fd], [], [], + max(0, end_time - time.time())) + if fd not in r: break # Read data from child newdata = self.read_nonblocking(internal_timeout) # Print it if necessary @@ -868,7 +872,8 @@ class kvm_expect(kvm_tail): done = True # Check if child has died if not self.is_alive(): - logging.debug("Process terminated with status %s" % self.get_status()) + logging.debug("Process terminated with status %s" % + self.get_status()) done = True # Are we done? if done: break -- 1.5.4.1