[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[tor-commits] [arm/master] Only supporting a single resource tracker
commit 0c36c50b981c4a5d87eb82a26007cf4bd338dd31
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date: Sun Oct 20 19:24:46 2013 -0700
Only supporting a single resource tracker
Swapping us over to a singleton. Code still sucks, but getting closer.
---
arm/graphing/resourceStats.py | 2 +-
arm/headerPanel.py | 6 ++++--
arm/starter.py | 10 +++++-----
arm/util/sysTools.py | 28 +++++++++++-----------------
4 files changed, 21 insertions(+), 25 deletions(-)
diff --git a/arm/graphing/resourceStats.py b/arm/graphing/resourceStats.py
index d4d71c4..843ef41 100644
--- a/arm/graphing/resourceStats.py
+++ b/arm/graphing/resourceStats.py
@@ -42,7 +42,7 @@ class ResourceStats(graphPanel.GraphStats):
primary, secondary = 0, 0
if self.queryPid:
- resourceTracker = sysTools.getResourceTracker(self.queryPid, True)
+ resourceTracker = sysTools.getResourceTracker()
if resourceTracker and not resourceTracker.lastQueryFailed():
primary, _, secondary, _ = resourceTracker.getResourceUsage()
diff --git a/arm/headerPanel.py b/arm/headerPanel.py
index bf8a98e..b27a858 100644
--- a/arm/headerPanel.py
+++ b/arm/headerPanel.py
@@ -414,7 +414,8 @@ class HeaderPanel(panel.Panel, threading.Thread):
isChanged = False
if self.vals["tor/pid"]:
- resourceTracker = sysTools.getResourceTracker(self.vals["tor/pid"])
+ resourceTracker = sysTools.getResourceTracker()
+ resourceTracker.set_process(self.vals["tor/pid"])
isChanged = self._lastResourceFetch != resourceTracker.run_counter()
if isChanged or currentTime - self._lastUpdate >= 20:
@@ -560,7 +561,8 @@ class HeaderPanel(panel.Panel, threading.Thread):
# ps or proc derived resource usage stats
if self.vals["tor/pid"]:
- resourceTracker = sysTools.getResourceTracker(self.vals["tor/pid"])
+ resourceTracker = sysTools.getResourceTracker()
+ resourceTracker.set_process(self.vals["tor/pid"])
if resourceTracker.lastQueryFailed():
self.vals["stat/%torCpu"] = "0"
diff --git a/arm/starter.py b/arm/starter.py
index 16dc348..e728bfb 100644
--- a/arm/starter.py
+++ b/arm/starter.py
@@ -286,17 +286,17 @@ def _shutdown_daemons():
# joins on utility daemon threads - this might take a moment since the
# internal threadpools being joined might be sleeping
- resource_trackers = arm.util.sysTools.RESOURCE_TRACKERS.values()
+ resource_tracker = arm.util.sysTools.getResourceTracker() if arm.util.sysTools.getResourceTracker().is_alive() else None
connection_resolver = arm.util.tracker.get_connection_resolver() if arm.util.tracker.get_connection_resolver().is_alive() else None
- for tracker in resource_trackers:
- tracker.stop()
+ if resource_tracker:
+ resource_tracker.stop()
if connection_resolver:
connection_resolver.stop() # sets halt flag (returning immediately)
- for tracker in resource_trackers:
- tracker.join()
+ if resource_tracker:
+ resource_tracker.join()
if connection_resolver:
connection_resolver.join() # joins on halted resolver
diff --git a/arm/util/sysTools.py b/arm/util/sysTools.py
index d6baef0..e2ec45e 100644
--- a/arm/util/sysTools.py
+++ b/arm/util/sysTools.py
@@ -10,7 +10,7 @@ import arm.util.tracker
from stem.util import conf, log, proc, str_tools, system
-RESOURCE_TRACKERS = {} # mapping of pids to their resource tracker instances
+RESOURCE_TRACKER = None
# Runtimes for system calls, used to estimate cpu usage. Entries are tuples of
# the form:
@@ -56,26 +56,17 @@ def getSysCpuUsage():
runtimeSum = sum([entry[1] for entry in RUNTIMES])
return runtimeSum / SAMPLING_PERIOD
-def getResourceTracker(pid, noSpawn = False):
+def getResourceTracker():
"""
- Provides a running singleton ResourceTracker instance for the given pid.
-
- Arguments:
- pid - pid of the process being tracked
- noSpawn - returns None rather than generating a singleton instance if True
+ Singleton for tracking the resource usage of our tor process.
"""
- if pid in RESOURCE_TRACKERS:
- tracker = RESOURCE_TRACKERS[pid]
- if tracker.isAlive(): return tracker
- else: del RESOURCE_TRACKERS[pid]
+ global RESOURCE_TRACKER
+
+ if RESOURCE_TRACKER is None:
+ RESOURCE_TRACKER = ResourceTracker()
- if noSpawn: return None
- tracker = ResourceTracker()
- tracker.set_process(pid)
- RESOURCE_TRACKERS[pid] = tracker
- tracker.start()
- return tracker
+ return RESOURCE_TRACKER
class ResourceTracker(arm.util.tracker.Daemon):
"""
@@ -140,6 +131,9 @@ class ResourceTracker(arm.util.tracker.Daemon):
return self._failureCount != 0
def task(self):
+ if self._procss_pid is None:
+ return
+
timeSinceReset = time.time() - self.lastLookup
newValues = {}
_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits