Home | History | Annotate | Download | only in kvm_stat
      1 """
      2 kvm_stat prints statistics generated by the kvm module.
      3 It depends on debugfs. If no debugfs is mounted, the profiler
      4 will try to mount it so it's possible to proceed.
      5 
      6 @copyright: Red Hat 2010
      7 @author: Lucas Meneghel Rodrigues (lmr (at] redhat.com)
      8 """
      9 import time, os, subprocess, commands, logging
     10 from autotest_lib.client.bin import utils, profiler, os_dep
     11 from autotest_lib.client.common_lib import error
     12 
     13 
     14 class kvm_stat(profiler.profiler):
     15     """
     16     kvm_stat based profiler. Consists on executing kvm_stat -l during a given
     17     test execution, redirecting its output to a file on the profile dir.
     18     """
     19     version = 1
     20     def initialize(self):
     21         """
     22         Gets path of kvm_stat and verifies if debugfs needs to be mounted.
     23         """
     24         self.is_enabled = False
     25 
     26         kvm_stat_installed = False
     27         try:
     28             self.stat_path = os_dep.command('kvm_stat')
     29             kvm_stat_installed = True
     30         except ValueError:
     31             logging.error('Command kvm_stat not present')
     32 
     33         if kvm_stat_installed:
     34             try:
     35                 utils.run("%s --batch" % self.stat_path)
     36                 self.is_enabled = True
     37             except error.CmdError, e:
     38                 if 'debugfs' in str(e):
     39                     try:
     40                         utils.run('mount -t debugfs debugfs /sys/kernel/debug')
     41                     except error.CmdError, e:
     42                         logging.error('Failed to mount debugfs:\n%s', str(e))
     43                 else:
     44                     logging.error('Failed to execute kvm_stat:\n%s', str(e))
     45 
     46 
     47     def start(self, test):
     48         """
     49         Starts kvm_stat subprocess.
     50 
     51         @param test: Autotest test on which this profiler will operate on.
     52         """
     53         if self.is_enabled:
     54             cmd = "%s -l" % self.stat_path
     55             logfile = open(os.path.join(test.profdir, "kvm_stat"), 'w')
     56             p = subprocess.Popen(cmd, shell=True, stdout=logfile,
     57                                  stderr=subprocess.STDOUT)
     58             self.pid = p.pid
     59         else:
     60             logging.error('Asked for kvm_stat profiler, but kvm_stat not '
     61                           'present')
     62 
     63 
     64     def stop(self, test):
     65         """
     66         Stops profiler execution by sending a SIGTERM to kvm_stat process.
     67 
     68         @param test: Autotest test on which this profiler will operate on.
     69         """
     70         if self.is_enabled:
     71             try:
     72                 os.kill(self.pid, 15)
     73             except OSError:
     74                 pass
     75 
     76 
     77     def report(self, test):
     78         """
     79         Report function. Does nothing as there's no postprocesing needed.
     80 
     81         @param test: Autotest test on which this profiler will operate on.
     82         """
     83         return None
     84