From: =?UTF-8?q?Ji=C5=99=C3=AD=20=C5=BDupka?= Subject: [Autotest][PATCH 3/4] Monitoring numerical information from file and return value of this information. Date: Wed, 13 Oct 2010 16:13:18 +0200 Message-ID: <1286979199-13109-4-git-send-email-jzupka@redhat.com> References: <1286979199-13109-1-git-send-email-jzupka@redhat.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: QUOTED-PRINTABLE Cc: jzupka@redhat.com, akong@redhat.com, ldoktor@redhat.com To: kvm-autotest@redhat.com, kvm@vger.kernel.org Return-path: Received: from mx1.redhat.com ([209.132.183.28]:62423 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752470Ab0JMOMo (ORCPT ); Wed, 13 Oct 2010 10:12:44 -0400 Received: from int-mx03.intmail.prod.int.phx2.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.16]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id o9DECiL5007180 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Wed, 13 Oct 2010 10:12:44 -0400 In-Reply-To: <1286979199-13109-1-git-send-email-jzupka@redhat.com> Sender: kvm-owner@vger.kernel.org List-ID: It gather the informatin at start and stop of the measurement or continuously during the measurement. It work in two modes different and average. Different mode subtract oldvalue from new value. Average mode return average of value. example: data: cpu 90 45 24 5 345 cpu0 14 11 83 34 33 ^^^^ start of line params 0 1 2 3 4 cpu =3D FileFieldMonitor("/proc/stat", [("cpu", 0), ("cpu", 2), True) Work in diff mode. get value 0 and 2 from line started "cpu". cpu.stop() # not necessary cpu.get_status() =3D> return tuple of result. Signed-off-by: Ji=C5=99=C3=AD =C5=BDupka Signed-off-by: Luk=C3=A1=C5=A1 Doktor --- client/common_lib/utils.py | 139 ++++++++++++++++++++++++++++++++++++= ++++++++ 1 files changed, 140 insertions(+), 0 deletions(-) diff --git a/client/common_lib/utils.py b/client/common_lib/utils.py index c126e58..e7e0d02 100644 --- a/client/common_lib/utils.py +++ b/client/common_lib/utils.py @@ -4,6 +4,7 @@ import os, pickle, random, re, resource, select, shutil, signal, Strin= gIO import socket, struct, subprocess, sys, time, textwrap, urlparse import warnings, smtplib, logging, urllib2 +from threading import Thread, Event try: import hashlib except ImportError: @@ -323,6 +324,156 @@ def write_keyval(path, dictionary, type_tag=3DNon= e): keyval.close() =20 =20 +class FileFieldMonitor(): + """ + Monitors the information from the file and reports it's values. + + It gather the information at start and stop of the measurement or + continuously during the measurement. + """ + class Monitor(Thread): + """ + Internal monitor class to ensure continuous monitor of monitor= ed file. + """ + def __init__(self, master): + """ + @param master: Master class which control Monitor + """ + Thread.__init__(self) + self.master =3D master + + def run(self): + """ + Start monitor in thread mode + """ + while not self.master.end_event.isSet(): + self.master._get_value(self.master.logging) + time.sleep(self.master.time_step) + + + def __init__(self, status_file, data_to_read, mode_diff, continuou= sly=3DFalse, + contlogging=3DFalse, separator=3D" +", time_step=3D0.= 1): + """ + Initialize variables. + @param status_file: File contain status. + @param mode_diff: If True make a difference of value, else ave= rage. + @param data_to_read: List of tuples with data position. + format: [(start_of_line,position in params)] + example: + data: + cpu 324 345 34 5 345 + cpu0 34 11 34 34 33 + ^^^^ + start of line + params 0 1 2 3 4 + @param mode_diff: True to subtract old value from new value, + False make average of the values. + @parma continuously: Start the monitoring thread using the tim= e_step + as the measurement period. + @param contlogging: Log data in continuous run. + @param separator: Regular expression of separator. + @param time_step: Time period of the monitoring value. + """ + self.end_event =3D Event() + self.start_time =3D 0 + self.end_time =3D 0 + self.test_time =3D 0 + + self.status_file =3D status_file + self.separator =3D separator + self.data_to_read =3D data_to_read + self.num_of_params =3D len(self.data_to_read) + self.mode_diff =3D mode_diff + self.continoutsly =3D continuously + self.time_step =3D time_step + + self.value =3D [0 for i in range(self.num_of_params)] + self.old_value =3D [0 for i in range(self.num_of_params)] + self.log =3D [] + self.logging =3D contlogging + + self.started =3D False + self.num_of_get_value =3D 0 + self.monitor =3D None + + + def _get_value(self, logging=3DTrue): + """ + Return current values. + @param logging: If true log value in memory. There can be prob= lem + with long run. + """ + data =3D read_file(self.status_file) + value =3D [] + for i in range(self.num_of_params): + value.append(int(get_field(data, + self.data_to_read[i][1], + self.data_to_read[i][0], + self.separator))) + + if logging: + self.log.append(value) + if not self.mode_diff: + value =3D map(lambda x, y: x + y, value, self.old_value) + + self.old_value =3D value + self.num_of_get_value +=3D 1 + return value + + + def start(self): + """ + Start value monitor. + """ + if self.started: + self.stop() + self.old_value =3D [0 for i in range(self.num_of_params)] + self.num_of_get_value =3D 0 + self.log =3D [] + self.end_event.clear() + self.start_time =3D time.time() + self._get_value() + self.started =3D True + if (self.continoutsly): + self.monitor =3D FileFieldMonitor.Monitor(self) + self.monitor.start() + + + def stop(self): + """ + Stop value monitor. + """ + if self.started: + self.started =3D False + self.end_time =3D time.time() + self.test_time =3D self.end_time - self.start_time + self.value =3D self._get_value() + if (self.continoutsly): + self.end_event.set() + self.monitor.join() + if (self.mode_diff): + self.value =3D map(lambda x, y: x - y, self.log[-1], s= elf.log[0]) + else: + self.value =3D map(lambda x: x / self.num_of_get_value= , + self.value) + + + def get_status(self): + """ + @return: Status of monitored process average value, + time of test and array of monitored values and time step o= f + continuous run. + """ + if self.started: + self.stop() + if self.mode_diff: + for i in range(len(self.log) - 1): + self.log[i] =3D (map(lambda x, y: x - y, + self.log[i + 1], self.log[i])) + self.log.pop() + return (self.value, self.test_time, self.log, self.time_step) + + def is_url(path): """Return true if path looks like a URL""" # for now, just handle http and ftp --=20 1.7.2.3