commit 332e9de098ab17e4d8a359e96df40970d9e59e81
Author: Damian Johnson <atagar(a)torproject.org>
Date: Fri Oct 18 14:29:00 2013 -0700
Adding a run_counter() method to the Daemon
Both our connection and resource usage trackers provided a counter for the
number of runs we've made so far (useful to determine if results are fresh or
not). Moving this to their common parent.
---
arm/connections/connPanel.py | 2 +-
arm/headerPanel.py | 4 ++--
arm/util/sysTools.py | 24 ++++++------------------
arm/util/tracker.py | 37 +++++++++++++++++++++++++------------
4 files changed, 34 insertions(+), 33 deletions(-)
diff --git a/arm/connections/connPanel.py b/arm/connections/connPanel.py
index 1086fae..7db0e8e 100644
--- a/arm/connections/connPanel.py
+++ b/arm/connections/connPanel.py
@@ -430,7 +430,7 @@ class ConnectionPanel(panel.Panel, threading.Thread):
if not arm.util.tracker.get_connection_resolver().is_alive(): return
connResolver = arm.util.tracker.get_connection_resolver()
- currentResolutionCount = connResolver.get_resolution_count()
+ currentResolutionCount = connResolver.run_counter()
self.valsLock.acquire()
diff --git a/arm/headerPanel.py b/arm/headerPanel.py
index 55f1727..bf8a98e 100644
--- a/arm/headerPanel.py
+++ b/arm/headerPanel.py
@@ -415,7 +415,7 @@ class HeaderPanel(panel.Panel, threading.Thread):
isChanged = False
if self.vals["tor/pid"]:
resourceTracker = sysTools.getResourceTracker(self.vals["tor/pid"])
- isChanged = self._lastResourceFetch != resourceTracker.getRunCount()
+ isChanged = self._lastResourceFetch != resourceTracker.run_counter()
if isChanged or currentTime - self._lastUpdate >= 20:
self._update()
@@ -568,7 +568,7 @@ class HeaderPanel(panel.Panel, threading.Thread):
self.vals["stat/%mem"] = "0"
else:
cpuUsage, _, memUsage, memUsagePercent = resourceTracker.getResourceUsage()
- self._lastResourceFetch = resourceTracker.getRunCount()
+ self._lastResourceFetch = resourceTracker.run_counter()
self.vals["stat/%torCpu"] = "%0.1f" % (100 * cpuUsage)
self.vals["stat/rss"] = str(memUsage)
self.vals["stat/%mem"] = "%0.1f" % (100 * memUsagePercent)
diff --git a/arm/util/sysTools.py b/arm/util/sysTools.py
index e8a9e49..573b233 100644
--- a/arm/util/sysTools.py
+++ b/arm/util/sysTools.py
@@ -57,7 +57,7 @@ def getResourceTracker(pid, noSpawn = False):
else: del RESOURCE_TRACKERS[pid]
if noSpawn: return None
- tracker = ResourceTracker(pid, CONFIG["queries.resourceUsage.rate"])
+ tracker = ResourceTracker(pid)
RESOURCE_TRACKERS[pid] = tracker
tracker.start()
return tracker
@@ -68,21 +68,18 @@ class ResourceTracker(arm.util.tracker.Daemon):
process.
"""
- def __init__(self, processPid, resolveRate):
+ def __init__(self, processPid):
"""
Initializes a new resolver daemon. When no longer needed it's suggested
that this is stopped.
Arguments:
processPid - pid of the process being tracked
- resolveRate - time between resolving resource usage, resolution is
- disabled if zero
"""
- arm.util.tracker.Daemon.__init__(self, resolveRate)
+ arm.util.tracker.Daemon.__init__(self, CONFIG["queries.resourceUsage.rate"])
self.processPid = processPid
- self.resolveRate = resolveRate
self.cpuSampling = 0.0 # latest cpu usage sampling
self.cpuAvg = 0.0 # total average cpu usage
@@ -98,9 +95,6 @@ class ResourceTracker(arm.util.tracker.Daemon):
self.lastLookup = -1
self._valLock = threading.RLock()
- # number of successful calls we've made
- self._runCount = 0
-
# sequential times we've failed with this method of resolution
self._failureCount = 0
@@ -116,14 +110,6 @@ class ResourceTracker(arm.util.tracker.Daemon):
return results
- def getRunCount(self):
- """
- Provides the number of times we've successfully fetched the resource
- usages.
- """
-
- return self._runCount
-
def lastQueryFailed(self):
"""
Provides true if, since we fetched the currently cached results, we've
@@ -217,6 +203,8 @@ class ResourceTracker(arm.util.tracker.Daemon):
self.memUsagePercentage = newValues["memUsagePercentage"]
self._lastCpuTotal = newValues["_lastCpuTotal"]
self.lastLookup = time.time()
- self._runCount += 1
self._failureCount = 0
self._valLock.release()
+ return True
+ else:
+ return False
diff --git a/arm/util/tracker.py b/arm/util/tracker.py
index d9bfef0..b75e5ff 100644
--- a/arm/util/tracker.py
+++ b/arm/util/tracker.py
@@ -5,6 +5,13 @@ Background tasks for gathering informatino about the tor process.
get_connection_resolver - provides a ConnectionResolver for our tor process
+ Daemon - common parent for resolvers
+ |- run_counter - number of successful runs
+ |- get_rate - provides the rate at which we run
+ |- set_rate - sets the rate at which we run
+ |- set_paused - pauses or continues work
+ +- stop - stops further work by the daemon
+
ConnectionResolver - periodically queries tor's connection information
|- set_process - set the pid and process name used for lookups
|- get_custom_resolver - provide the custom conntion resolver we're using
@@ -49,6 +56,7 @@ class Daemon(threading.Thread):
self._rate = rate
self._last_ran = -1 # time when we last ran
+ self._run_counter = 0 # counter for the number of successful runs
self._is_paused = False
self._halt = False # terminates thread if true
@@ -68,7 +76,11 @@ class Daemon(threading.Thread):
continue # done waiting, try again
- self.task()
+ is_successful = self.task()
+
+ if is_successful:
+ self._run_counter += 1
+
self._last_ran = time.time()
def task(self):
@@ -79,6 +91,14 @@ class Daemon(threading.Thread):
pass
+ def run_counter(self):
+ """
+ Provides the number of successful runs so far. This can be used to
+ determine if the daemon's results are new for the caller or not.
+ """
+
+ return self._run_counter
+
def get_rate(self):
"""
Provides the rate at which we perform our given task.
@@ -128,7 +148,6 @@ class ConnectionResolver(Daemon):
self._resolvers = connection.get_system_resolvers()
self._connections = []
self._custom_resolver = None
- self._resolution_counter = 0 # number of successful connection resolutions
self._process_pid = None
self._process_name = None
@@ -160,8 +179,6 @@ class ConnectionResolver(Daemon):
runtime = time.time() - start_time
- self._resolution_counter += 1
-
if is_default_resolver:
self._failure_count = 0
@@ -180,6 +197,8 @@ class ConnectionResolver(Daemon):
log.debug("connection lookup time increasing to %0.1f seconds per call" % min_rate)
else:
self._rate_too_low_count = 0
+
+ return True
except IOError as exc:
log.info(exc)
@@ -201,6 +220,8 @@ class ConnectionResolver(Daemon):
else:
log.notice(CONFIG['msg.unable_to_use_all_resolvers'])
+ return False
+
def set_process(self, pid, name):
"""
Sets the process we retrieve connections for.
@@ -244,11 +265,3 @@ class ConnectionResolver(Daemon):
return []
else:
return list(self._connections)
-
- def get_resolution_count(self):
- """
- Provides the number of successful resolutions so far. This can be used to
- determine if the connection results are new for the caller or not.
- """
-
- return self._resolution_counter