aboutsummaryrefslogtreecommitdiffstats
path: root/mpm
diff options
context:
space:
mode:
authorMartin Braun <martin.braun@ettus.com>2018-01-11 18:56:57 -0800
committerMartin Braun <martin.braun@ettus.com>2018-01-15 10:45:07 -0800
commit032e483adcd0084d5479ac36c6586f7518805de3 (patch)
tree822b9fa70b2773197eda57f182c2bdb619dddbe7 /mpm
parenteaed9d5ac511a08a123a28d1c0fc48789ca1480b (diff)
downloaduhd-032e483adcd0084d5479ac36c6586f7518805de3.tar.gz
uhd-032e483adcd0084d5479ac36c6586f7518805de3.tar.bz2
uhd-032e483adcd0084d5479ac36c6586f7518805de3.zip
mpm: sys_utils: Add watchdog module
Reviewed-by: Moritz Fischer <moritz.fischer@ettus.com>
Diffstat (limited to 'mpm')
-rw-r--r--mpm/python/usrp_mpm/sys_utils/CMakeLists.txt1
-rw-r--r--mpm/python/usrp_mpm/sys_utils/watchdog.py63
2 files changed, 64 insertions, 0 deletions
diff --git a/mpm/python/usrp_mpm/sys_utils/CMakeLists.txt b/mpm/python/usrp_mpm/sys_utils/CMakeLists.txt
index bb91418ac..5a9d79034 100644
--- a/mpm/python/usrp_mpm/sys_utils/CMakeLists.txt
+++ b/mpm/python/usrp_mpm/sys_utils/CMakeLists.txt
@@ -13,6 +13,7 @@ SET(USRP_MPM_SYSUTILS_FILES
${CMAKE_CURRENT_SOURCE_DIR}/sysfs_thermal.py
${CMAKE_CURRENT_SOURCE_DIR}/udev.py
${CMAKE_CURRENT_SOURCE_DIR}/uio.py
+ ${CMAKE_CURRENT_SOURCE_DIR}/watchdog.py
)
LIST(APPEND USRP_MPM_FILES ${USRP_MPM_SYSUTILS_FILES})
SET(USRP_MPM_FILES ${USRP_MPM_FILES} PARENT_SCOPE)
diff --git a/mpm/python/usrp_mpm/sys_utils/watchdog.py b/mpm/python/usrp_mpm/sys_utils/watchdog.py
new file mode 100644
index 000000000..125a42c62
--- /dev/null
+++ b/mpm/python/usrp_mpm/sys_utils/watchdog.py
@@ -0,0 +1,63 @@
+#
+# Copyright 2018 Ettus Research, National Instruments Company
+#
+# SPDX-License-Identifier: GPL-3.0
+#
+"""
+systemd watchdog control module
+"""
+
+import os
+import time
+import threading
+from systemd import daemon
+
+MPM_WATCHDOG_DEFAULT_TIMEOUT = 30
+# How often per watchdog interval we send a ping
+MPM_WATCHDOG_TIMEOUT_FRAC = 3.0
+
+def has_watchdog():
+ """Check if the system has a watchdog checking on us.
+
+ We do this by checking on a set value for WATCHDOG_USEC.
+ """
+ return bool(os.environ.get('WATCHDOG_USEC', False))
+
+def watchdog_task(shared_state, log):
+ """
+ Continuously ping the watchdog to tell him that we're still alive.
+
+ This will keep running until the parent thread dies, or
+ shared_state.system_ready gets set to False by someone.
+ """
+ log.info("launching task")
+ watchdog_timeout = \
+ float(os.environ.get(
+ 'WATCHDOG_USEC',
+ MPM_WATCHDOG_DEFAULT_TIMEOUT
+ )) / 1e6
+ watchdog_interval = watchdog_timeout / MPM_WATCHDOG_TIMEOUT_FRAC
+ daemon.notify("READY=1")
+ log.info("READY=1, interval %f", watchdog_interval)
+ while shared_state.system_ready.value:
+ log.trace("Pinging watchdog....")
+ daemon.notify("WATCHDOG=1")
+ time.sleep(watchdog_interval)
+ log.error("Terminating watchdog thread!")
+ return
+
+def spawn_watchdog_task(shared_state, log):
+ """Spawn and return watchdog thread.
+
+ Creates a daemonic thread, because we don't want the watchdog task to
+ outlive the main thread.
+ """
+ task = threading.Thread(
+ target=watchdog_task,
+ args=[shared_state, log],
+ name="MPMWatchdogTask",
+ daemon=True,
+ )
+ task.start()
+ return task
+