1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2004 Cluster File Systems, Inc.
5 * Author: Jacob Berkman <jacob@clusterfs.com>
7 * This file is part of Lustre, http://www.lustre.org.
9 * Lustre is free software; you can redistribute it and/or
10 * modify it under the terms of version 2 of the GNU General Public
11 * License as published by the Free Software Foundation.
13 * Lustre is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with Lustre; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #define DEBUG_SUBSYSTEM S_LNET
25 #include <libcfs/kp30.h>
26 #include <libcfs/libcfs.h>
27 #include "tracefile.h"
30 cfs_timer_t lcw_timer; /* kernel timer */
31 struct list_head lcw_list;
32 struct timeval lcw_last_touched;
35 void (*lcw_callback)(pid_t, void *);
39 int lcw_time; /* time until watchdog fires, in ms */
50 * The dispatcher will complete lcw_start_completion when it starts,
51 * and lcw_stop_completion when it exits.
52 * Wake lcw_event_waitq to signal timer callback dispatches.
54 static struct completion lcw_start_completion;
55 static struct completion lcw_stop_completion;
56 static wait_queue_head_t lcw_event_waitq;
59 * Set this and wake lcw_event_waitq to stop the dispatcher.
64 static unsigned long lcw_flags = 0;
67 * Number of outstanding watchdogs.
68 * When it hits 1, we start the dispatcher.
69 * When it hits 0, we stop the distpatcher.
71 static __u32 lcw_refcount = 0;
72 static DECLARE_MUTEX(lcw_refcount_sem);
75 * List of timers that have fired that need their callbacks run by the
78 static spinlock_t lcw_pending_timers_lock = SPIN_LOCK_UNLOCKED; /* BH lock! */
79 static struct list_head lcw_pending_timers = \
80 LIST_HEAD_INIT(lcw_pending_timers);
82 #ifdef HAVE_TASKLIST_LOCK
84 lcw_dump(struct lc_watchdog *lcw)
89 read_lock(&tasklist_lock);
90 tsk = find_task_by_pid(lcw->lcw_pid);
93 CWARN("Process %d was not found in the task list; "
94 "watchdog callback may be incomplete\n", (int)lcw->lcw_pid);
95 } else if (tsk != lcw->lcw_task) {
96 CWARN("The current process %d did not set the watchdog; "
97 "watchdog callback may be incomplete\n", (int)lcw->lcw_pid);
99 libcfs_debug_dumpstack(tsk);
102 read_unlock(&tasklist_lock);
107 lcw_dump(struct lc_watchdog *lcw)
109 CERROR("unable to dump stack because of missing export\n");
113 static void lcw_cb(unsigned long data)
115 struct lc_watchdog *lcw = (struct lc_watchdog *)data;
119 if (lcw->lcw_state != LC_WATCHDOG_ENABLED) {
124 lcw->lcw_state = LC_WATCHDOG_EXPIRED;
126 /* NB this warning should appear on the console, but may not get into
127 * the logs since we're running in a softirq handler */
129 CWARN("Watchdog triggered for pid %d: it was inactive for %ldms\n",
130 (int)lcw->lcw_pid, cfs_duration_sec(lcw->lcw_time) * 1000);
133 spin_lock_bh(&lcw_pending_timers_lock);
135 if (list_empty(&lcw->lcw_list)) {
136 list_add(&lcw->lcw_list, &lcw_pending_timers);
137 wake_up(&lcw_event_waitq);
140 spin_unlock_bh(&lcw_pending_timers_lock);
145 static int is_watchdog_fired(void)
149 if (test_bit(LCW_FLAG_STOP, &lcw_flags))
152 spin_lock_bh(&lcw_pending_timers_lock);
153 rc = !list_empty(&lcw_pending_timers);
154 spin_unlock_bh(&lcw_pending_timers_lock);
158 static int lcw_dispatch_main(void *data)
162 struct lc_watchdog *lcw;
166 cfs_daemonize("lc_watchdogd");
168 SIGNAL_MASK_LOCK(current, flags);
169 sigfillset(¤t->blocked);
171 SIGNAL_MASK_UNLOCK(current, flags);
173 complete(&lcw_start_completion);
176 wait_event_interruptible(lcw_event_waitq, is_watchdog_fired());
177 CDEBUG(D_INFO, "Watchdog got woken up...\n");
178 if (test_bit(LCW_FLAG_STOP, &lcw_flags)) {
179 CDEBUG(D_INFO, "LCW_FLAG_STOP was set, shutting down...\n");
181 spin_lock_bh(&lcw_pending_timers_lock);
182 rc = !list_empty(&lcw_pending_timers);
183 spin_unlock_bh(&lcw_pending_timers_lock);
185 CERROR("pending timers list was not empty at "
186 "time of watchdog dispatch shutdown\n");
191 spin_lock_bh(&lcw_pending_timers_lock);
192 while (!list_empty(&lcw_pending_timers)) {
194 lcw = list_entry(lcw_pending_timers.next,
197 list_del_init(&lcw->lcw_list);
198 spin_unlock_bh(&lcw_pending_timers_lock);
200 CDEBUG(D_INFO, "found lcw for pid %d: inactive for %ldms\n",
201 (int)lcw->lcw_pid, cfs_duration_sec(lcw->lcw_time) * 1000);
203 if (lcw->lcw_state != LC_WATCHDOG_DISABLED)
204 lcw->lcw_callback(lcw->lcw_pid, lcw->lcw_data);
206 spin_lock_bh(&lcw_pending_timers_lock);
208 spin_unlock_bh(&lcw_pending_timers_lock);
211 complete(&lcw_stop_completion);
216 static void lcw_dispatch_start(void)
221 LASSERT(lcw_refcount == 1);
223 init_completion(&lcw_stop_completion);
224 init_completion(&lcw_start_completion);
225 init_waitqueue_head(&lcw_event_waitq);
227 CDEBUG(D_INFO, "starting dispatch thread\n");
228 rc = kernel_thread(lcw_dispatch_main, NULL, 0);
230 CERROR("error spawning watchdog dispatch thread: %d\n", rc);
234 wait_for_completion(&lcw_start_completion);
235 CDEBUG(D_INFO, "watchdog dispatcher initialization complete.\n");
240 static void lcw_dispatch_stop(void)
243 LASSERT(lcw_refcount == 0);
245 CDEBUG(D_INFO, "trying to stop watchdog dispatcher.\n");
247 set_bit(LCW_FLAG_STOP, &lcw_flags);
248 wake_up(&lcw_event_waitq);
250 wait_for_completion(&lcw_stop_completion);
252 CDEBUG(D_INFO, "watchdog dispatcher has shut down.\n");
257 struct lc_watchdog *lc_watchdog_add(int timeout_ms,
258 void (*callback)(pid_t, void *),
261 struct lc_watchdog *lcw = NULL;
264 LIBCFS_ALLOC(lcw, sizeof(*lcw));
266 CDEBUG(D_INFO, "Could not allocate new lc_watchdog\n");
267 RETURN(ERR_PTR(-ENOMEM));
270 lcw->lcw_task = cfs_current();
271 lcw->lcw_pid = cfs_curproc_pid();
272 lcw->lcw_time = cfs_time_seconds(timeout_ms) / 1000;
273 lcw->lcw_callback = (callback != NULL) ? callback : lc_watchdog_dumplog;
274 lcw->lcw_data = data;
275 lcw->lcw_state = LC_WATCHDOG_DISABLED;
277 INIT_LIST_HEAD(&lcw->lcw_list);
279 lcw->lcw_timer.function = lcw_cb;
280 lcw->lcw_timer.data = (unsigned long)lcw;
281 lcw->lcw_timer.expires = jiffies + lcw->lcw_time;
282 init_timer(&lcw->lcw_timer);
284 down(&lcw_refcount_sem);
285 if (++lcw_refcount == 1)
286 lcw_dispatch_start();
287 up(&lcw_refcount_sem);
289 /* Keep this working in case we enable them by default */
290 if (lcw->lcw_state == LC_WATCHDOG_ENABLED) {
291 do_gettimeofday(&lcw->lcw_last_touched);
292 add_timer(&lcw->lcw_timer);
297 EXPORT_SYMBOL(lc_watchdog_add);
299 static void lcw_update_time(struct lc_watchdog *lcw, const char *message)
301 struct timeval newtime;
302 struct timeval timediff;
304 do_gettimeofday(&newtime);
305 if (lcw->lcw_state == LC_WATCHDOG_EXPIRED) {
306 cfs_timeval_sub(&newtime, &lcw->lcw_last_touched, &timediff);
307 CWARN("Expired watchdog for pid %d %s after %lu.%.4lus\n",
311 timediff.tv_usec / 100);
313 lcw->lcw_last_touched = newtime;
316 void lc_watchdog_touch(struct lc_watchdog *lcw)
319 LASSERT(lcw != NULL);
321 spin_lock_bh(&lcw_pending_timers_lock);
322 list_del_init(&lcw->lcw_list);
323 spin_unlock_bh(&lcw_pending_timers_lock);
325 lcw_update_time(lcw, "touched");
326 lcw->lcw_state = LC_WATCHDOG_ENABLED;
328 mod_timer(&lcw->lcw_timer, jiffies + lcw->lcw_time);
332 EXPORT_SYMBOL(lc_watchdog_touch);
334 void lc_watchdog_disable(struct lc_watchdog *lcw)
337 LASSERT(lcw != NULL);
339 spin_lock_bh(&lcw_pending_timers_lock);
340 if (!list_empty(&lcw->lcw_list))
341 list_del_init(&lcw->lcw_list);
342 spin_unlock_bh(&lcw_pending_timers_lock);
344 lcw_update_time(lcw, "disabled");
345 lcw->lcw_state = LC_WATCHDOG_DISABLED;
349 EXPORT_SYMBOL(lc_watchdog_disable);
351 void lc_watchdog_delete(struct lc_watchdog *lcw)
354 LASSERT(lcw != NULL);
356 del_timer(&lcw->lcw_timer);
358 lcw_update_time(lcw, "deleted");
360 spin_lock_bh(&lcw_pending_timers_lock);
361 if (!list_empty(&lcw->lcw_list))
362 list_del_init(&lcw->lcw_list);
363 spin_unlock_bh(&lcw_pending_timers_lock);
365 down(&lcw_refcount_sem);
366 if (--lcw_refcount == 0)
368 up(&lcw_refcount_sem);
370 LIBCFS_FREE(lcw, sizeof(*lcw));
374 EXPORT_SYMBOL(lc_watchdog_delete);
377 * Provided watchdog handlers
380 void lc_watchdog_dumplog(pid_t pid, void *data)
382 libcfs_debug_dumplog_internal((void *)((unsigned long)pid));
384 EXPORT_SYMBOL(lc_watchdog_dumplog);
386 #else /* !defined(WITH_WATCHDOG) */
388 struct lc_watchdog *lc_watchdog_add(int timeout_ms,
389 void (*callback)(pid_t pid, void *),
392 static struct lc_watchdog watchdog;
395 EXPORT_SYMBOL(lc_watchdog_add);
397 void lc_watchdog_touch(struct lc_watchdog *lcw)
400 EXPORT_SYMBOL(lc_watchdog_touch);
402 void lc_watchdog_disable(struct lc_watchdog *lcw)
405 EXPORT_SYMBOL(lc_watchdog_disable);
407 void lc_watchdog_delete(struct lc_watchdog *lcw)
410 EXPORT_SYMBOL(lc_watchdog_delete);