From: =?UTF-8?q?Ji=C5=99=C3=AD=20=C5=BDupka?= Subject: [Autotest][PATCH 4/4] Get system and/or process values and return average value of load cpu or mem. Date: Wed, 13 Oct 2010 16:13:19 +0200 Message-ID: <1286979199-13109-5-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]:41647 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752470Ab0JMOMq (ORCPT ); Wed, 13 Oct 2010 10:12:46 -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 o9DECjGI006563 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Wed, 13 Oct 2010 10:12:46 -0400 In-Reply-To: <1286979199-13109-1-git-send-email-jzupka@redhat.com> Sender: kvm-owner@vger.kernel.org List-ID: =46or gather the information form status files use FileFieldMonitor. Usage: load =3D SystemLoad([0, (1950, "name"), 3982]) PIDs load.start() (work) load.stop() load.start() #Reset monitor values and start monitoring. (work) print load.get_cpu_status_string() #This automatically stops monitor= ing. load.start() (work) load.stop() load.get_mem_status_string() For string output use. print load.get_cpu_status_string() print load.get_mem_status_string() Additionally you can dump all values from monitor using. load.dump() Format: Array of tuples [(PID,([average value],time of measurement,[[measure]],time_step= ])), (PID,([average value],time of measurement,[[measure]],time_step)= )]. Signed-off-by: Ji=C5=99=C3=AD =C5=BDupka Signed-off-by: Luka=C5=A1 Doktor --- client/common_lib/utils.py | 192 ++++++++++++++++++++++++++++++++++++= ++++++++ 1 files changed, 192 insertions(+), 0 deletions(-) diff --git a/client/common_lib/utils.py b/client/common_lib/utils.py index e7e0d02..6fb6ae4 100644 --- a/client/common_lib/utils.py +++ b/client/common_lib/utils.py @@ -969,6 +969,226 @@ def get_cpu_percentage(function, *args, **dargs): return cpu_percent, to_return =20 =20 +class SystemLoad(): + """ + Get system and/or process values and return average value of load. + """ + def __init__(self, pids, advanced=3DFalse, time_step=3D0.1, cpu_co= nt=3DFalse, + use_log=3DFalse): + """ + @param pids: List of pids to be monitored. If pid =3D 0 whole = system will + be monitored. pid =3D=3D 0 means whole system. + @param advanced: monitor add value for system irq count and so= ftirq + for process minor and maior page fault + @param time_step: Time step for continuous monitoring. + @param cpu_cont: If True monitor CPU load continuously. + @param use_log: If true every monitoring is logged for dump. + """ + self.pids =3D [] + self.stats =3D {} + for pid in pids: + if pid =3D=3D 0: + cpu =3D FileFieldMonitor("/proc/stat", + [("cpu", 0), # User Time + ("cpu", 2), # System Time + ("intr", 0), # IRQ Count + ("softirq", 0)], # Soft IRQ Co= unt + True, + cpu_cont, + use_log, + " +", + time_step) + mem =3D FileFieldMonitor("/proc/meminfo", + [("MemTotal:", 0), # Mem Total + ("MemFree:", 0), # Mem Free + ("Buffers:", 0), # Buffers + ("Cached:", 0)], # Cached + False, + True, + use_log, + " +", + time_step) + self.stats[pid] =3D ["TOTAL", cpu, mem] + self.pids.append(pid) + else: + name =3D "" + if (type(pid) is int): + self.pids.append(pid) + name =3D get_process_name(pid) + else: + self.pids.append(pid[0]) + name =3D pid[1] + + cpu =3D FileFieldMonitor("/proc/%d/stat" % + self.pids[-1], + [("", 13), # User Time + ("", 14), # System Time + ("", 9), # Minority Page Fault + ("", 11)], # Majority Page Fau= lt + True, + cpu_cont, + use_log, + " +", + time_step) + mem =3D FileFieldMonitor("/proc/%d/status" % + self.pids[-1], + [("VmSize:", 0), # Virtual Memo= ry Size + ("VmRSS:", 0), # Resident Set = Size + ("VmPeak:", 0), # Peak VM Size + ("VmSwap:", 0)], # VM in Swap + False, + True, + use_log, + " +", + time_step) + self.stats[self.pids[-1]] =3D [name, cpu, mem] + + self.advanced =3D advanced + + + def __str__(self): + """ + Define format how to print + """ + out =3D "" + for pid in self.pids: + for stat in self.stats[pid][1:]: + out +=3D str(stat.get_status()) + "\n" + return out + + + def start(self, pids=3D[]): + """ + Start monitoring of the process system usage. + @param pids: List of PIDs you intend to control. Use pids=3D[]= to control + all defined PIDs. + """ + if pids =3D=3D []: + pids =3D self.pids + + for pid in pids: + for stat in self.stats[pid][1:]: + stat.start() + + + def stop(self, pids=3D[]): + """ + Stop monitoring of the process system usage. + @param pids: List of PIDs you intend to control. Use pids=3D[]= to control + all defined PIDs. + """ + if pids =3D=3D []: + pids =3D self.pids + + for pid in pids: + for stat in self.stats[pid][1:]: + stat.stop() + + + def dump(self, pids=3D[]): + """ + Get the status of monitoring. + @param pids: List of PIDs you intend to control. Use pids=3D[]= to control + all defined PIDs. + @return: + tuple([cpu load], [memory load]): + ([(PID1, (PID1_cpu_meas)), (PID2, (PID2_cpu_meas)), ..= =2E], + [(PID1, (PID1_mem_meas)), (PID2, (PID2_mem_meas)), ..= =2E]) + + PID1_cpu_meas: + average_values[], test_time, cont_meas_values[[]], tim= e_step + PID1_mem_meas: + average_values[], test_time, cont_meas_values[[]], tim= e_step + where average_values[] are the measured values (mem_free,s= wap,...) + which are described in SystemLoad.__init__()-FileFieldMoni= tor. + cont_meas_values[[]] is a list of average_values in the sa= mpling + times. + """ + if pids =3D=3D []: + pids =3D self.pids + + cpus =3D [] + memory =3D [] + for pid in pids: + stat =3D (pid, self.stats[pid][1].get_status()) + cpus.append(stat) + for pid in pids: + stat =3D (pid, self.stats[pid][2].get_status()) + memory.append(stat) + + return (cpus, memory) + + + def get_cpu_status_string(self, pids=3D[]): + """ + Convert status to string array. + @param pids: List of PIDs you intend to control. Use pids=3D[]= to control + all defined PIDs. + @return: String format to table. + """ + if pids =3D=3D []: + pids =3D self.pids + + headers =3D ["NAME", + ("%7s") % "PID", + ("%5s") % "USER", + ("%5s") % "SYS", + ("%5s") % "SUM"] + if self.advanced: + headers.extend(["MINFLT/IRQC", + "MAJFLT/SOFTIRQ"]) + headers.append(("%11s") % "TIME") + textstatus =3D [] + for pid in pids: + stat =3D self.stats[pid][1].get_status() + time =3D stat[1] + stat =3D stat[0] + textstatus.append(["%s" % self.stats[pid][0], + "%7s" % pid, + "%4.0f%%" % (stat[0] / time), + "%4.0f%%" % (stat[1] / time), + "%4.0f%%" % ((stat[0] + stat[1]) / time= ), + "%10.3fs" % time]) + if self.advanced: + textstatus[-1].insert(-1, "%11d" % stat[2]) + textstatus[-1].insert(-1, "%14d" % stat[3]) + + return matrix_to_string(textstatus, tuple(headers)) + + + def get_mem_status_string(self, pids=3D[]): + """ + Convert status to string array. + @param pids: List of PIDs you intend to control. Use pids=3D[]= to control + all defined PIDs. + @return: String format to table. + """ + if pids =3D=3D []: + pids =3D self.pids + + headers =3D ["NAME", + ("%7s") % "PID", + ("%8s") % "TOTAL/VMSIZE", + ("%8s") % "FREE/VMRSS", + ("%8s") % "BUFFERS/VMPEAK", + ("%8s") % "CACHED/VMSWAP", + ("%11s") % "TIME"] + textstatus =3D [] + for pid in pids: + stat =3D self.stats[pid][2].get_status() + time =3D stat[1] + stat =3D stat[0] + textstatus.append(["%s" % self.stats[pid][0], + "%7s" % pid, + "%10dMB" % (stat[0] / 1024), + "%8dMB" % (stat[1] / 1024), + "%12dMB" % (stat[2] / 1024), + "%11dMB" % (stat[3] / 1024), + "%10.3fs" % time]) + + return matrix_to_string(textstatus, tuple(headers)) + + def get_arch(run_function=3Drun): """ Get the hardware architecture of the machine. --=20 1.7.2.3