From eLinux.org
Jump to: navigation, search

This document describes how to test the pretimeout feature with a software watchdog

Prepare the kernel

The patches landed upstream in v4.9-rc1. In case the needed patches are not in your version of renesas-drivers, here is the topic branch [1].

Activate pretimeout in the kernel config. Here is the selection from the watchdog submenu:

--- Watchdog Timer Support
-*-   WatchDog Timer Driver Core
[ ]   Disable watchdog shutdown on close
[*]   Read different watchdog information through sysfs
      *** Watchdog Device Drivers ***
<*>   Software watchdog


      *** Watchdog Pretimeout Governors ***
[*]   Enable watchdog pretimeout governors
        Default Watchdog Pretimeout Governor (panic)  --->
-*-     Panic watchdog pretimeout governor
<*>     Noop watchdog pretimeout governor
<*>     Userspace notifier watchdog pretimeout governor

Prepare the root filesystem for your target

The root filesystem only needs busybox. v1.24.2 was used here in this example. The following patch is currently needed for the '-P' parameter of the watchdog utility. It will be upstreamed when the Linux Kernel parts are upstream.

From: Wolfram Sang <wsa+renesas@sang-engineering.com>
Subject: [PATCH] watchdog: add pretimeout parameter

Signed-off-by: Wolfram Sang <wsa+renesas@sang-engineering.com>
 miscutils/watchdog.c |   12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

Index: b/miscutils/watchdog.c
--- a/miscutils/watchdog.c
+++ b/miscutils/watchdog.c
@@ -10,11 +10,12 @@
 //usage:#define watchdog_trivial_usage
-//usage:       "[-t N[ms]] [-T N[ms]] [-F] DEV"
+//usage:       "[-t N[ms]] [-T N[ms]] [-P N[ms]] [-F] DEV"
 //usage:#define watchdog_full_usage "\n\n"
 //usage:       "Periodically write to watchdog device DEV\n"
 //usage:     "\n	-T N	Reboot after N seconds if not reset (default 60)"
 //usage:     "\n	-t N	Reset every N seconds (default 30)"
+//usage:     "\n	-P N	Pretimeout warning N seconds before reboot (default 0)"
 //usage:     "\n	-F	Run in foreground"
 //usage:     "\n"
 //usage:     "\nUse 500ms to specify period in milliseconds"
@@ -26,6 +27,7 @@
 #define OPT_FOREGROUND  (1 << 0)
 #define OPT_STIMER      (1 << 1)
 #define OPT_HTIMER      (1 << 2)
+#define OPT_PTIMER      (1 << 3)
 static void watchdog_shutdown(int sig UNUSED_PARAM)
@@ -50,11 +52,13 @@ int watchdog_main(int argc, char **argv)
 	unsigned opts;
 	unsigned stimer_duration; /* how often to restart */
 	unsigned htimer_duration = 60000; /* reboots after N ms if not restarted */
+	unsigned ptimer_duration = 0; /* pre-timeout notification N ms before reboot */
 	char *st_arg;
 	char *ht_arg;
+	char *pt_arg;
 	opt_complementary = "=1"; /* must have exactly 1 argument */
-	opts = getopt32(argv, "Ft:T:", &st_arg, &ht_arg);
+	opts = getopt32(argv, "Ft:T:P:", &st_arg, &ht_arg, &pt_arg);
 	/* We need to daemonize *before* opening the watchdog as many drivers
 	 * will only allow one process at a time to do so.  Since daemonizing
@@ -88,6 +92,10 @@ int watchdog_main(int argc, char **argv)
 # endif
 	ioctl_or_warn(3, WDIOC_SETTIMEOUT, &htimer_duration);
+	if (opts & OPT_PTIMER) {
+		ptimer_duration = xatou_sfx(pt_arg, suffixes) / 1000;
+		ioctl_or_warn(3, WDIOC_SETPRETIMEOUT, &ptimer_duration);
+	}
 #if 0

Check if pretimeout govenors are available

After booting the kernel and logging in, list the govenors:

# cat /sys/class/watchdog/watchdog0/pretimeout_available_governors

Note: We assume here watchdog0 is the software watchdog. If this is not the case, please replace all 'watchdog0' with your watchdog device.

Check if pretimeout govenors can be selected

# cat /sys/class/watchdog/watchdog0/pretimeout_governor
# echo -n noop > /sys/class/watchdog/watchdog0/pretimeout_governor
# cat /sys/class/watchdog/watchdog0/pretimeout_governor

Start the watchdog with setting causing a pretimeout

Please remember that the pretimeout is specified in seconds before the timeout. So we will generate a pretimeout here, because: Timeout is 10 seconds (-T 10). Pretimeout is 8 seconds (-P 8), meaning a pretimeout notification will be generated after 2 seconds. A ping will be generated every 3 seconds (-t 3), so it is after the pretimeout notification but before the actual timeout.

# watchdog -t 3 -T 10 -P 8 -F /dev/watchdog
[   57.453927] watchdog0: pretimeout event

Check if proper pretimeout value can be read from sysfs

# cat /sys/class/watchdog/watchdog0/pretimeout