[tor-commits] [arm/master] Moving process tracking into the Daemon class
atagar at torproject.org
atagar at torproject.org
Tue Oct 29 03:39:22 UTC 2013
commit cf04513aa02523b319e36ebc520c0c6014a2476a
Author: Damian Johnson <atagar at torproject.org>
Date: Mon Oct 28 11:31:07 2013 -0700
Moving process tracking into the Daemon class
Now that we're using a singleton for just tracking the tor process we can use
the controller singleton within the Daemon class itself. This lets it
transparently keep up to date with tor's pid and process name.
---
arm/controller.py | 11 -----------
arm/headerPanel.py | 2 --
arm/util/tracker.py | 46 +++++++++++++++++++++-------------------------
3 files changed, 21 insertions(+), 38 deletions(-)
diff --git a/arm/controller.py b/arm/controller.py
index 69e52ce..a68fe41 100644
--- a/arm/controller.py
+++ b/arm/controller.py
@@ -118,7 +118,6 @@ def initController(stdscr, startTime):
tor_cmd = "tor"
resolver = arm.util.tracker.get_connection_tracker()
- resolver.set_process(tor_pid, tor_cmd)
log.info("Operating System: %s, Connection Resolvers: %s" % (os.uname()[0], ", ".join(resolver._resolvers)))
resolver.start()
else:
@@ -510,16 +509,6 @@ def connResetListener(controller, eventType, _):
if getController().getPanel("torrc") == None:
torConfig.getTorrc().load(True)
- try:
- tor_cmd = system.get_name_by_pid(tor_pid)
-
- if tor_cmd is None:
- tor_cmd = "tor"
-
- resolver.set_process(controller.get_pid(), tor_cmd)
- except ValueError:
- pass
-
def start_arm(stdscr):
"""
Main draw loop context.
diff --git a/arm/headerPanel.py b/arm/headerPanel.py
index 629c84c..d3a8d9d 100644
--- a/arm/headerPanel.py
+++ b/arm/headerPanel.py
@@ -417,7 +417,6 @@ class HeaderPanel(panel.Panel, threading.Thread):
isChanged = False
if self.vals["tor/pid"]:
resourceTracker = arm.util.tracker.get_resource_tracker()
- resourceTracker.set_process(self.vals["tor/pid"])
isChanged = self._lastResourceFetch != resourceTracker.run_counter()
if isChanged or currentTime - self._lastUpdate >= 20:
@@ -564,7 +563,6 @@ class HeaderPanel(panel.Panel, threading.Thread):
# ps or proc derived resource usage stats
if self.vals["tor/pid"]:
resourceTracker = arm.util.tracker.get_resource_tracker()
- resourceTracker.set_process(self.vals["tor/pid"])
if resourceTracker.last_query_failed():
self.vals["stat/%torCpu"] = "0"
diff --git a/arm/util/tracker.py b/arm/util/tracker.py
index c027975..8e93e58 100644
--- a/arm/util/tracker.py
+++ b/arm/util/tracker.py
@@ -14,13 +14,11 @@ Background tasks for gathering informatino about the tor process.
+- stop - stops further work by the daemon
ConnectionResolver - periodically checks the connections established by tor
- |- set_process - set the pid and process name used for lookups
|- get_custom_resolver - provide the custom conntion resolver we're using
|- set_custom_resolver - overwrites automatic resolver selecion with a custom resolver
+- get_connections - provides our latest connection results
ResourceTracker - periodically checks the resource usage of tor
- |- set_process - set the pid used for lookups
|- get_resource_usage - provides our latest resource usage results
+- last_query_failed - checks if we failed to fetch newer results
"""
@@ -29,6 +27,9 @@ import collections
import time
import threading
+import arm.util.torTools
+
+from stem.control import State
from stem.util import conf, connection, log, proc, str_tools, system
CONFIG = conf.config_dict("arm", {
@@ -90,6 +91,9 @@ class Daemon(threading.Thread):
threading.Thread.__init__(self)
self.daemon = True
+ self._process_name = None
+ self._process_pid = None
+
self._rate = rate
self._last_ran = -1 # time when we last ran
self._run_counter = 0 # counter for the number of successful runs
@@ -98,6 +102,10 @@ class Daemon(threading.Thread):
self._halt = False # terminates thread if true
self._cond = threading.Condition() # used for pausing the thread
+ controller = arm.util.torTools.getConn().controller
+ controller.add_status_listener(self._tor_status_listener)
+ self._tor_status_listener(controller, State.INIT, None)
+
def run(self):
while not self._halt:
time_since_last_ran = time.time() - self._last_ran
@@ -172,6 +180,17 @@ class Daemon(threading.Thread):
self._cond.notifyAll()
self._cond.release()
+ def _tor_status_listener(self, controller, event_type, _):
+ if event_type in (State.INIT, State.RESET):
+ tor_pid = controller.get_pid(None)
+ tor_cmd = system.get_name_by_pid(tor_pid) if tor_pid else None
+
+ if tor_cmd is None:
+ tor_cmd = 'tor'
+
+ self._process_name = tor_cmd
+ self._process_pid = tor_pid
+
class ConnectionResolver(Daemon):
"""
@@ -185,9 +204,6 @@ class ConnectionResolver(Daemon):
self._connections = []
self._custom_resolver = None
- self._process_pid = None
- self._process_name = None
-
# Number of times in a row we've either failed with our current resolver or
# concluded that our rate is too low.
@@ -258,17 +274,6 @@ class ConnectionResolver(Daemon):
return False
- def set_process(self, pid, name):
- """
- Sets the process we retrieve connections for.
-
- :param int pid: process id
- :param str name: name of the process
- """
-
- self._process_pid = pid
- self._process_name = name
-
def get_custom_resolver(self):
"""
Provides the custom resolver the user has selected. This is **None** if
@@ -325,15 +330,6 @@ class ResourceTracker(Daemon):
# sequential times we've failed with this method of resolution
self._failure_count = 0
- def set_process(self, pid):
- """
- Sets the process we retrieve resources for.
-
- :param int pid: process id
- """
-
- self._process_pid = pid
-
def get_resource_usage(self):
"""
Provides the last cached resource usage as a named tuple of the form:
More information about the tor-commits
mailing list