Add an optional outputfiles parameter to the cmdprofile profiler that
lets you override outputfile with a list of outputfiles instead of
just specifying a single file that all the command are written into.

This will allow users to profile multiple commands more easily
without having to add multiple profilers (which technically works,
but more by accident then due to being properly supported).

Risk: Low
Visibility: Adds a new optional parameter to cmdprofile for specifying
    multiple output files.

Signed-off-by: John Admanski <[email protected]>

--- autotest/client/profilers/cmdprofile/cmdprofile.py  2009-12-21 
03:54:43.000000000 -0800
+++ autotest/client/profilers/cmdprofile/cmdprofile.py  2009-12-21 
03:54:43.000000000 -0800
@@ -5,16 +5,34 @@
 
 import time, os, subprocess
 from autotest_lib.client.bin import profiler
-from autotest_lib.client.common_lib import utils
+from autotest_lib.client.common_lib import utils, error
 
 class cmdprofile(profiler.profiler):
-    version = 1
+    version = 2
     supports_reboot = True
 
-    def initialize(self, cmds=['ps'], interval=60, outputfile='cmdprofile'):
+
+    def initialize(self, cmds=['ps'], interval=60, outputfile='cmdprofile',
+                   outputfiles=None):
+
+        # do some basic sanity checking on the parameters
+        if not outputfiles and not outputfile:
+            raise error.TestError(
+                'cmdprofile cannot run if neither outputfile nor outputfile '
+                'is specified')
+        elif outputfiles and len(outputfiles) != len(cmds):
+            raise error.TestError(
+                'cmdprofile paramter outputfiles has length %d and cmds has '
+                'length %d, but both lists must have the same length' %
+                (len(outputfiles), len(cmds)))
+
         self.interval = interval
         self.cmds = cmds
-        self.outputfile = outputfile
+        if outputfiles:
+            # outputfiles overrides outputfile
+            self.outputfiles = outputfiles
+        else:
+            self.outputfiles = [outputfile] * len(cmds)
 
 
     def start(self, test):
@@ -22,13 +40,13 @@
         if self.pid:  # parent
             return
         else:  # child
-            logfile = open(os.path.join(test.profdir, self.outputfile), 'a')
             while True:
-                for cmd in self.cmds:
+                for cmd, outputfile in zip(self.cmds, self.outputfiles):
+                    logfile = open(os.path.join(test.profdir, outputfile), 'a')
                     utils.run(cmd, stdout_tee=logfile, stderr_tee=logfile)
                     logfile.write('\n')
+                    logfile.close()
                 time.sleep(self.interval)
-            logfile.close()
 
 
     def stop(self, test):
_______________________________________________
Autotest mailing list
[email protected]
http://test.kernel.org/cgi-bin/mailman/listinfo/autotest

Reply via email to