Created
March 18, 2015 22:59
-
-
Save mobiusklein/7ba45c4d1b18a3fbf767 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sqlitedict | |
import psutil | |
from uuid import uuid4 | |
class ResourceTracker(object): | |
def __init__(self, cpu_usage_total=0, memory_usage_total=0, runtime_total=0, pid=None): | |
self.cpu_total = cpu_usage_total | |
self.memory_total = memory_usage_total | |
self.runtime_total = runtime_total | |
self.pid = pid | |
def update(self, ps_process, time_elapsed): | |
self.cpu_total += ps_process.cpu_percent() | |
self.memory_used += ps_process.memory_info()[0] | |
self.runtime_total += time_elapsed | |
metrics_store = sqlitedict.open("metrics_store.db") | |
interval = 30 | |
# In process status update loop | |
for process in processes: | |
tracker = metrics_store.get(process.pid, None) # Deserialize existing record or return None | |
if tracker is None: | |
tracker = ResourceTracker() # Make fresh record because no reference was present | |
process.cpu_percent() # Clear the first dummy value returned by cpu_percent | |
tracker.update(process, interval) # Mutate record in memory | |
metrics_store[process.pid] = tracker # Serialize record onto disk | |
if process.is_complete(): # However you decide the process terminated successfully | |
tracker = metrics_store.get(process.pid) | |
metrics_store[process.pid] = None # Clear the pid in case it is re-used | |
metrics_store[str(process.pid) + '-' + uuid4().hex] = tracker # Set up unique storage for this completed process | |
metrics_store.commit() # At the end of a status update loop for all currently running processes | |
# Later, after the run, re-open the database created by a given run and tally up the runtime | |
# statistics | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment