This patch does the following:

1) In older versions of sar, specifying an interval and
a count of 0 means that reports will be generated
continuously. For newer versions, just ommiting count
does it, a count of 0 is invalid. Handle this difference
testing the command during the initialize() method.

2) Try to terminate the process using the subprocess
method terminate() (which only sends a sigterm to the
process and does not raise exceptions if the process
was already terminated).

3) Add some docstring documenation for the profiler.

Signed-off-by: Feng Yang <[email protected]>
Signed-off-by: Lucas Meneghel Rodrigues <[email protected]>
---
 client/profilers/sar/sar.py |   63 +++++++++++++++++++++++++++++++++++-------
 1 files changed, 52 insertions(+), 11 deletions(-)

diff --git a/client/profilers/sar/sar.py b/client/profilers/sar/sar.py
index fbe0639..2618646 100644
--- a/client/profilers/sar/sar.py
+++ b/client/profilers/sar/sar.py
@@ -4,35 +4,76 @@ Sets up a subprocess to run sar from the sysstat suite
 Default options:
 sar -A -f
 """
-
-
-import os, shutil, subprocess
-from autotest_lib.client.bin import utils, profiler
+import os, shutil, subprocess, time
+from autotest_lib.client.bin import utils, profiler, os_dep
 
 
 class sar(profiler.profiler):
+    """
+    The sar command writes to standard output the contents of selected
+    cumulative activity counters in the operating system. This profiler
+    executes sar and redirects its output in a file located in the profiler
+    results dir.
+    """
     version = 1
 
-    def initialize(self, interval = 1):
+    def initialize(self, interval=1):
+        """
+        Set sar interval and verify what flags the installed sar supports.
+
+        @param interval: Interval used by sar to produce system data.
+        """
         self.interval = interval
+        self.sar_path = os_dep.command('sar')
+        # If using older versions of sar, command below means: Measure default
+        # params using interval of 1 second continuously. For newer versions,
+        # behavior has changed - to generate reports continuously just omit the
+        # count parameter.
+        t_cmd = self.sar_path + " 1 0"
+        t_process = subprocess.Popen(t_cmd, shell=True,
+                                     stdout=subprocess.PIPE,
+                                     stderr=subprocess.PIPE)
+        # Wait a little to see if process is going to fail or work
+        time.sleep(3)
+        if t_process.poll():
+            # Sar process returned, so 0 doesn't mean generate continuously
+            self.cmd = self.sar_path + " -o %s %d"
+        else:
+            # Sar process didn't return, so 0 means generate continuously
+            # Just terminate the process
+            self.cmd = self.sar_path + "-o %s %d 0"
+            t_process.terminate()
 
 
     def start(self, test):
+        """
+        Starts sar subprocess.
+
+        @param test: Autotest test on which this profiler will operate on.
+        """
         logfile = open(os.path.join(test.profdir, "sar"), 'w')
         # Save the sar data as binary, convert to text after the test.
         raw = os.path.join(test.profdir, "sar.raw")
-        cmd = "/usr/bin/sar -o %s %d 0" % (raw, self.interval)
-        p = subprocess.Popen(cmd, shell=True, stdout=logfile, \
-                             stderr=subprocess.STDOUT)
-        self.pid = p.pid
+        cmd = self.cmd % (raw, self.interval)
+        self.sar_process = subprocess.Popen(cmd, shell=True, stdout=logfile,
+                                            stderr=subprocess.STDOUT)
 
 
     def stop(self, test):
-        os.kill(self.pid, 15)
+        """
+        Stops profiler execution by sending a SIGTERM to sar process.
+
+        @param test: Autotest test on which this profiler will operate on.
+        """
+        self.sar_process.terminate()
 
 
     def report(self, test):
-        # Convert the binary sar data to text.
+        """
+        Report function. Convert the binary sar data to text.
+
+        @param test: Autotest test on which this profiler will operate on.
+        """
         raw = os.path.join(test.profdir, "sar.raw")
         output = os.path.join(test.profdir, "sar")
         utils.system('/usr/bin/sar -A -f %s > %s' % (raw, output))
-- 
1.6.6.1

_______________________________________________
Autotest mailing list
[email protected]
http://test.kernel.org/cgi-bin/mailman/listinfo/autotest

Reply via email to