[mlpack-svn] r15376 - mlpack/conf/jenkins-conf/benchmark/util
fastlab-svn at coffeetalk-1.cc.gatech.edu
fastlab-svn at coffeetalk-1.cc.gatech.edu
Mon Jul 1 13:28:47 EDT 2013
Author: marcus
Date: Mon Jul 1 13:28:46 2013
New Revision: 15376
Log:
Add valgrind massif memory profiling function.
Added:
mlpack/conf/jenkins-conf/benchmark/util/profiler.py
Added: mlpack/conf/jenkins-conf/benchmark/util/profiler.py
==============================================================================
--- (empty file)
+++ mlpack/conf/jenkins-conf/benchmark/util/profiler.py Mon Jul 1 13:28:46 2013
@@ -0,0 +1,82 @@
+'''
+ @file profiler.py
+ @author Marcus Edel
+
+ Contains functions to get profiling informations.
+'''
+
+import os
+import sys
+import inspect
+
+# import the util path, this method even works if the path contains
+# symlinks to modules.
+cmd_subfolder = os.path.realpath(os.path.abspath(os.path.join(
+ os.path.split(inspect.getfile(inspect.currentframe()))[0], '')))
+if cmd_subfolder not in sys.path:
+ sys.path.insert(0, cmd_subfolder)
+
+from log import *
+
+'''
+This class implements functions the get profiling informations.
+'''
+class Profiler(object):
+
+ '''
+ Use valgrind massif to get memory profiling information and the save the ouput
+ in the specified file.
+
+ @param cmd - Method command line to profile.
+ @param output - Save the report at the output path with the specified name.
+ @param valgrind - Path to the valgrind binary.
+ @param options - Specified massif options.
+ @ return Returns False if the method was not successful, if the method was
+ successful save the report file in the specified file.
+ '''
+ @staticmethod
+ def MassifMemoryUsage(cmd, output, valgrind = "valgrind", options = ""):
+ import shlex, subprocess
+
+ cmd = shlex.split(("%s --tool=massif --massif-out-file=%s %s") %
+ (valgrind, output, options)) + cmd
+ try:
+ s = subprocess.check_output(cmd, stderr=subprocess.STDOUT, shell=False)
+ except Exception:
+ Log.Fatal("Could not execute command: " + str(cmd))
+ return -1
+
+ '''
+ Returns the memory used by a process and his children. We don't know when the
+ process is done so we have to poll to get the memory. To avoid memory overflow
+ we use a ringbuffer to limit the size of the memory values.
+
+ @param process - Popen instance.
+ @param Buffersize - Memory value count.
+ @return List of memory values.
+ '''
+ @staticmethod
+ def SubprocessMemoryUsage(process, Buffersize = 200):
+ import psutil, time, collections
+
+ # Create the process list with the main process and his childrens.
+ p = psutil.Process(process.pid)
+ children = list(p.get_children(recursive=True)) + [p]
+
+ memoryTable = collections.deque(maxlen = Buffersize)
+
+ # We have to poll to get the memory values.
+ while process.poll() == None:
+ try:
+ for p in children:
+ memoryTable.append(int(p.get_memory_info()[0]))
+ # Sometimes a subprocess has terminated in the time between we measure the
+ # memory. In this case, we continue.
+ except psutil.NoSuchProcess:
+ continue
+ except psutil.AccessDenied:
+ continue
+
+ time.sleep(0.01)
+
+ return memoryTable
More information about the mlpack-svn
mailing list