[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [arm/master] Moving proc resource resolution to a helper too



commit ff27d2ae24afedc603dc519961c81309157fbc31
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Wed Oct 30 09:18:33 2013 -0700

    Moving proc resource resolution to a helper too
    
    On reflection both ps and proc are pretty similar. Moving proc resolution to a
    helper that hides the differences which in turn lets us drop our
    resolver-specific helper methods.
---
 arm/util/tracker.py |  110 ++++++++++++++++++++++-----------------------------
 1 file changed, 47 insertions(+), 63 deletions(-)

diff --git a/arm/util/tracker.py b/arm/util/tracker.py
index ec51af4..0304a6c 100644
--- a/arm/util/tracker.py
+++ b/arm/util/tracker.py
@@ -90,7 +90,7 @@ def get_resource_tracker():
   return RESOURCE_TRACKER
 
 
-def _ps_for_process(pid):
+def _resources_via_ps(pid):
   """
   Fetches resource usage information about a given process via ps. This returns
   a tuple of the form...
@@ -133,6 +133,37 @@ def _ps_for_process(pid):
   raise IOError("unrecognized output from ps: %s" % ps_call)
 
 
+def _resources_via_proc(pid):
+  """
+  Fetches resource usage information about a given process via proc. This
+  returns a tuple of the form...
+
+    (total_cpu_time, uptime, memory_in_bytes, memory_in_percent)
+
+  :param int pid: process to be queried
+
+  :returns: **tuple** with the resource usage information
+
+  :raises: **IOError** if unsuccessful
+  """
+
+  utime, stime, start_time = proc.get_stats(
+    pid,
+    proc.Stat.CPU_UTIME,
+    proc.Stat.CPU_STIME,
+    proc.Stat.START_TIME,
+  )
+
+  total_cpu_time = float(utime) + float(stime)
+  memory_in_bytes = proc.get_memory_usage(process_pid)[0]
+  total_memory = proc.get_physical_memory()
+
+  uptime = time.time() - float(start_time)
+  memory_in_percent = float(mem_usage) / total_memory
+
+  return (total_cpu_time, uptime, memory_in_bytes, memory_in_percent)
+
+
 class Daemon(threading.Thread):
   """
   Daemon that can perform a given action at a set rate. Subclasses are expected
@@ -393,10 +424,22 @@ class ResourceTracker(Daemon):
 
   def _task(self, process_pid, process_name):
     try:
-      if self._use_proc:
-        self._resources = self._proc_results(process_pid)
+      resolver = _resources_via_proc if self._use_proc else _resources_via_ps
+      total_cpu_time, uptime, memory_in_bytes, memory_in_percent = resolver(process_pid)
+
+      if self._resources:
+        cpu_sample = (total_cpu_time - self._resources.cpu_total) / self._resources.cpu_total
       else:
-        self._resources = self._ps_results(process_pid)
+        cpu_sample = 0.0  # we need a prior datapoint to give a sampling
+
+      self._resources = Resources(
+        cpu_sample = cpu_sample,
+        cpu_average = total_cpu_time / uptime,
+        cpu_total = total_cpu_time,
+        memory_bytes = memory_in_bytes,
+        memory_precent = memory_in_percent,
+        timestamp = time.time(),
+      )
 
       self._failure_count = 0
       return True
@@ -433,62 +476,3 @@ class ResourceTracker(Daemon):
           log.debug(CONFIG['msg.unable_to_get_resources'].format(resolver = 'ps', exc = exc))
 
       return False
-
-  def _proc_results(self, process_pid):
-    """
-    Resolves the process resource usage via proc.
-
-    :returns: **Resource** instance for its present resource usage
-
-    :throws: **IOError** if unable to retrieve information from proc
-    """
-
-    utime, stime, start_time = proc.get_stats(
-      process_pid,
-      proc.Stat.CPU_UTIME,
-      proc.Stat.CPU_STIME,
-      proc.Stat.START_TIME,
-    )
-
-    total_cpu_time = float(utime) + float(stime)
-    mem_usage = proc.get_memory_usage(process_pid)[0]
-    total_memory = proc.get_physical_memory()
-
-    if self._resources:
-      cpu_sample = (total_cpu_time - self._resources.cpu_total) / self._resources.cpu_total
-    else:
-      cpu_sample = 0.0  # we need a prior datapoint to give a sampling
-
-    return Resources(
-      cpu_sample = cpu_sample,
-      cpu_average = total_cpu_time / (time.time() - float(start_time)),
-      cpu_total = total_cpu_time,
-      memory_bytes = mem_usage,
-      memory_precent = float(mem_usage) / total_memory,
-      timestamp = time.time(),
-    )
-
-  def _ps_results(self, process_pid):
-    """
-    Resolves the process resource usage via ps.
-
-    :returns: **Resource** instance for its present resource usage
-
-    :throws: **IOError** if unable to retrieve information from proc
-    """
-
-    total_cpu_time, uptime, memory_in_bytes, memory_in_percent = _ps_for_process(process_pid)
-
-    if self._resources:
-      cpu_sample = (total_cpu_time - self._resources.cpu_total) / self._resources.cpu_total
-    else:
-      cpu_sample = 0.0  # we need a prior datapoint to give a sampling
-
-    return Resources(
-      cpu_sample = cpu_sample,
-      cpu_average = total_cpu_time / uptime,
-      cpu_total = total_cpu_time,
-      memory_bytes = memory_in_bytes,
-      memory_precent = memory_in_percent,
-      timestamp = time.time(),
-    )

_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits