Advertisement
Guest User

Untitled

a guest
Apr 7th, 2015
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.36 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. import psutil
  4. import os
  5. import subprocess
  6. import time
  7.  
  8. class ProcessTimer:
  9.   def __init__(self,command,resultsFile):
  10.     self.command = command
  11.     self.resultsFile = resultsFile
  12.     self.execution_state = False
  13.    
  14.     self.results = open(self.resultsFile, 'w')
  15.     self.results.write("{},{},{},{},{}\n".format("time","rss_mem","cpu_percent","mem_percent","threads"))
  16.  
  17.   def execute(self):
  18.  
  19.     self.t1 = None
  20.     self.t0 = time.time()
  21.     self.p = subprocess.Popen(self.command,shell=False)
  22.     self.execution_state = True
  23.  
  24.   def poll(self):
  25.     if not self.check_execution_state():
  26.       return False
  27.  
  28.     self.t1 = time.time()
  29.  
  30.     try:
  31.       pp = psutil.Process(self.p.pid)
  32.  
  33.       #obtain a list of the subprocess and all its descendants
  34.       descendants = list(pp.get_children(recursive=True))
  35.       descendants = descendants + [pp]
  36.  
  37.       rss_memory = 0
  38.  
  39.       #calculate and sum up the memory of the subprocess and all its descendants
  40.       for descendant in descendants:
  41.         try:
  42.      #details: https://code.google.com/p/psutil/wiki/Documentation
  43.           mem_info = descendant.get_memory_info()
  44.  
  45.           rss_memory += (mem_info[0] / 1024 / 1024)
  46.          
  47.         except psutil.NoSuchProcess:
  48.           #sometimes a subprocess descendant will have terminated between the time
  49.           # we obtain a list of descendants, and the time we actually poll this
  50.           # descendant's memory usage.
  51.           pass
  52.       self.results.write("{},{}\n".format(round(self.t1-self.t0,1),round(rss_memory,2)))
  53.       self.results.flush()
  54.       os.fsync(self.results)
  55.  
  56.     except psutil.NoSuchProcess:
  57.       return self.check_execution_state()
  58.  
  59.  
  60.     return self.check_execution_state()
  61.  
  62.   def is_running(self):
  63.     return psutil.pid_exists(self.p.pid) and self.p.poll() == None
  64.   def check_execution_state(self):
  65.     if not self.execution_state:
  66.       return False
  67.     if self.is_running():
  68.       return True
  69.     self.executation_state = False
  70.     self.t1 = time.time()
  71.     return False
  72.  
  73.   def close(self,kill=False):
  74.     try:
  75.       pp = psutil.Process(self.p.pid)
  76.       if kill:
  77.         pp.kill()
  78.       else:
  79.         pp.terminate()
  80.     except psutil.NoSuchProcess:
  81.       print("process already dead")
  82.     except OSError.ProcessLookupError as e:
  83.       print("process already dead")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement