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

[tor-commits] [stem/master] System utility for getting process start times



commit 24d9377cdc2d1ee3531238b825621934053bae9f
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Tue May 28 09:32:13 2013 -0700

    System utility for getting process start times
    
    Adding a get_start_time() to the system utils to provide the unix timestamp for
    when a given pid started.
---
 stem/util/system.py       |   29 +++++++++++++++++++++++++++++
 test/integ/util/system.py |   29 +++++++++++++++++++++++++++++
 2 files changed, 58 insertions(+)

diff --git a/stem/util/system.py b/stem/util/system.py
index 6b767c7..e760ae5 100644
--- a/stem/util/system.py
+++ b/stem/util/system.py
@@ -20,6 +20,7 @@ best-effort, providing **None** if the lookup fails.
   get_pid_by_port - gets the pid for a process listening to a given port
   get_pid_by_open_file - gets the pid for the process with an open file
   get_cwd - provides the current working directory for a given process
+  get_start_time - provides the unix timestamp when the process started
   get_bsd_jail_id - provides the BSD jail id a given process is running within
   expand_path - expands relative paths and ~ entries
   call - runs the given system command and provides back the results
@@ -530,6 +531,7 @@ def get_cwd(pid):
   Provides the working directory of the given process.
 
   :param int pid: process id of the process to be queried
+
   :returns: **str** with the absolute path for the process' present working
     directory, **None** if it can't be determined
   """
@@ -593,6 +595,33 @@ def get_cwd(pid):
   return None  # all queries failed
 
 
+def get_start_time(pid):
+  """
+  Provides the unix timestamp when the given process started.
+
+  :param int pid: process id of the process to be queried
+
+  :returns: **float** for the unix timestamp when the process began, **None**
+    if it can't be determined
+  """
+
+  if stem.util.proc.is_available():
+    try:
+      return float(stem.util.proc.get_stats(pid, stem.util.proc.Stat.START_TIME)[0])
+    except IOError:
+      pass
+
+  try:
+    ps_results = stem.util.system.call("ps -p %s -o etime" % pid)
+
+    if ps_results and len(ps_results) >= 2:
+      etime = ps_results[1].strip()
+      return time.time() - stem.util.str_tools.parse_short_time_label(etime)
+  except:
+    pass
+
+  return None
+
 def get_bsd_jail_id(pid):
   """
   Gets the jail id for a process. These seem to only exist for FreeBSD (this
diff --git a/test/integ/util/system.py b/test/integ/util/system.py
index 63486a4..1392dfd 100644
--- a/test/integ/util/system.py
+++ b/test/integ/util/system.py
@@ -8,6 +8,7 @@ import os
 import tempfile
 import unittest
 
+import stem.util.proc
 import stem.util.system
 import test.runner
 
@@ -382,6 +383,34 @@ class TestSystem(unittest.TestCase):
     runner_pid, tor_cwd = runner.get_pid(), runner.get_tor_cwd()
     self.assertEquals(tor_cwd, stem.util.system.get_cwd(runner_pid))
 
+  def test_get_start_time_proc(self):
+    """
+    Tests the get_start_time function with a proc response.
+    """
+
+    if not stem.util.proc.is_available():
+      test.runner.skip(self, "(proc unavailable)")
+      return
+
+    mocking.mock(stem.util.system.call, filter_system_call(['ps ']))
+
+    pid = test.runner.get_runner().get_pid()
+    self.assertTrue(stem.util.system.get_start_time(pid) >= 0)
+
+  def test_get_start_time_ps(self):
+    """
+    Tests the get_start_time function with a ps response.
+    """
+
+    if not stem.util.system.is_available("ps"):
+      test.runner.skip(self, "(ps unavailable)")
+      return
+
+    mocking.mock(stem.util.proc.is_available, mocking.return_false())
+
+    pid = test.runner.get_runner().get_pid()
+    self.assertTrue(stem.util.system.get_start_time(pid) >= 0)
+
   def test_get_bsd_jail_id(self):
     """
     Exercises the stem.util.system.get_bsd_jail_id function, running through



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