1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * libcfs/libcfs/watchdog.c
38 * Author: Jacob Berkman <jacob@clusterfs.com>
41 #define DEBUG_SUBSYSTEM S_LNET
43 #include <libcfs/libcfs.h>
44 #include "tracefile.h"
47 cfs_timer_t lcw_timer; /* kernel timer */
48 struct list_head lcw_list;
49 struct timeval lcw_last_touched;
52 void (*lcw_callback)(pid_t, void *);
56 cfs_duration_t lcw_time; /* time until watchdog fires, jiffies */
67 * The dispatcher will complete lcw_start_completion when it starts,
68 * and lcw_stop_completion when it exits.
69 * Wake lcw_event_waitq to signal timer callback dispatches.
71 static struct completion lcw_start_completion;
72 static struct completion lcw_stop_completion;
73 static cfs_waitq_t lcw_event_waitq;
76 * Set this and wake lcw_event_waitq to stop the dispatcher.
81 static unsigned long lcw_flags = 0;
84 * Number of outstanding watchdogs.
85 * When it hits 1, we start the dispatcher.
86 * When it hits 0, we stop the distpatcher.
88 static __u32 lcw_refcount = 0;
89 static DECLARE_MUTEX(lcw_refcount_sem);
92 * List of timers that have fired that need their callbacks run by the
95 static spinlock_t lcw_pending_timers_lock = SPIN_LOCK_UNLOCKED; /* BH lock! */
96 static struct list_head lcw_pending_timers = \
97 CFS_LIST_HEAD_INIT(lcw_pending_timers);
99 /* Last time a watchdog expired */
100 static cfs_time_t lcw_last_watchdog_time;
101 static int lcw_recent_watchdog_count;
102 static spinlock_t lcw_last_watchdog_lock = SPIN_LOCK_UNLOCKED;
104 #ifdef HAVE_TASKLIST_LOCK
106 lcw_dump(struct lc_watchdog *lcw)
111 read_lock(&tasklist_lock);
112 tsk = find_task_by_pid(lcw->lcw_pid);
115 CWARN("Process " LPPID " was not found in the task list; "
116 "watchdog callback may be incomplete\n", lcw->lcw_pid);
117 } else if (tsk != lcw->lcw_task) {
118 CWARN("The current process " LPPID " did not set the watchdog; "
119 "watchdog callback may be incomplete\n", lcw->lcw_pid);
121 libcfs_debug_dumpstack(tsk);
124 read_unlock(&tasklist_lock);
129 lcw_dump(struct lc_watchdog *lcw)
131 CERROR("unable to dump stack because of missing export\n");
135 static void lcw_cb(ulong_ptr_t data)
137 struct lc_watchdog *lcw = (struct lc_watchdog *)data;
138 cfs_time_t current_time;
139 cfs_duration_t delta_time;
143 if (lcw->lcw_state != LC_WATCHDOG_ENABLED) {
148 lcw->lcw_state = LC_WATCHDOG_EXPIRED;
149 current_time = cfs_time_current();
151 /* Check to see if we should throttle the watchdog timer to avoid
152 * too many dumps going to the console thus triggering an NMI.
153 * Normally we would not hold the spin lock over the CWARN but in
154 * this case we hold it to ensure non ratelimited lcw_dumps are not
155 * interleaved on the console making them hard to read. */
156 spin_lock_bh(&lcw_last_watchdog_lock);
157 delta_time = cfs_duration_sec(current_time - lcw_last_watchdog_time);
159 if (delta_time < libcfs_watchdog_ratelimit && lcw_recent_watchdog_count > 3) {
160 CWARN("Refusing to fire watchdog for pid %d: it was inactive "
161 "for %ldms. Rate limiting 1 per %d seconds.\n",
162 (int)lcw->lcw_pid,cfs_duration_sec(lcw->lcw_time) * 1000,
163 libcfs_watchdog_ratelimit);
165 if (delta_time < libcfs_watchdog_ratelimit) {
166 lcw_recent_watchdog_count++;
168 memcpy(&lcw_last_watchdog_time, ¤t_time,
169 sizeof(current_time));
170 lcw_recent_watchdog_count = 0;
173 /* This warning should appear on the console, but may not get
174 * into the logs since we're running in a softirq handler */
175 CWARN("Watchdog triggered for pid %d: it was inactive for %lds\n",
176 (int)lcw->lcw_pid, cfs_duration_sec(lcw->lcw_time));
180 spin_unlock_bh(&lcw_last_watchdog_lock);
181 spin_lock_bh(&lcw_pending_timers_lock);
183 if (list_empty(&lcw->lcw_list)) {
184 list_add(&lcw->lcw_list, &lcw_pending_timers);
185 cfs_waitq_signal(&lcw_event_waitq);
188 spin_unlock_bh(&lcw_pending_timers_lock);
193 static int is_watchdog_fired(void)
197 if (test_bit(LCW_FLAG_STOP, &lcw_flags))
200 spin_lock_bh(&lcw_pending_timers_lock);
201 rc = !list_empty(&lcw_pending_timers);
202 spin_unlock_bh(&lcw_pending_timers_lock);
206 static int lcw_dispatch_main(void *data)
210 struct lc_watchdog *lcw;
214 cfs_daemonize("lc_watchdogd");
216 SIGNAL_MASK_LOCK(current, flags);
217 sigfillset(¤t->blocked);
219 SIGNAL_MASK_UNLOCK(current, flags);
221 complete(&lcw_start_completion);
224 cfs_wait_event_interruptible(lcw_event_waitq, is_watchdog_fired(), rc);
225 CDEBUG(D_INFO, "Watchdog got woken up...\n");
226 if (test_bit(LCW_FLAG_STOP, &lcw_flags)) {
227 CDEBUG(D_INFO, "LCW_FLAG_STOP was set, shutting down...\n");
229 spin_lock_bh(&lcw_pending_timers_lock);
230 rc = !list_empty(&lcw_pending_timers);
231 spin_unlock_bh(&lcw_pending_timers_lock);
233 CERROR("pending timers list was not empty at "
234 "time of watchdog dispatch shutdown\n");
239 spin_lock_bh(&lcw_pending_timers_lock);
240 while (!list_empty(&lcw_pending_timers)) {
242 lcw = list_entry(lcw_pending_timers.next,
245 list_del_init(&lcw->lcw_list);
246 spin_unlock_bh(&lcw_pending_timers_lock);
248 CDEBUG(D_INFO, "found lcw for pid " LPPID ": inactive for "
249 "%lds\n", lcw->lcw_pid, cfs_duration_sec(lcw->lcw_time));
251 if (lcw->lcw_state != LC_WATCHDOG_DISABLED)
252 lcw->lcw_callback(lcw->lcw_pid, lcw->lcw_data);
254 spin_lock_bh(&lcw_pending_timers_lock);
256 spin_unlock_bh(&lcw_pending_timers_lock);
259 complete(&lcw_stop_completion);
264 static void lcw_dispatch_start(void)
269 LASSERT(lcw_refcount == 1);
271 init_completion(&lcw_stop_completion);
272 init_completion(&lcw_start_completion);
273 cfs_waitq_init(&lcw_event_waitq);
275 CDEBUG(D_INFO, "starting dispatch thread\n");
276 rc = kernel_thread(lcw_dispatch_main, NULL, 0);
278 CERROR("error spawning watchdog dispatch thread: %d\n", rc);
282 wait_for_completion(&lcw_start_completion);
283 CDEBUG(D_INFO, "watchdog dispatcher initialization complete.\n");
288 static void lcw_dispatch_stop(void)
291 LASSERT(lcw_refcount == 0);
293 CDEBUG(D_INFO, "trying to stop watchdog dispatcher.\n");
295 set_bit(LCW_FLAG_STOP, &lcw_flags);
296 cfs_waitq_signal(&lcw_event_waitq);
298 wait_for_completion(&lcw_stop_completion);
300 CDEBUG(D_INFO, "watchdog dispatcher has shut down.\n");
305 struct lc_watchdog *lc_watchdog_add(int timeout_ms,
306 void (*callback)(pid_t, void *),
309 struct lc_watchdog *lcw = NULL;
312 LIBCFS_ALLOC(lcw, sizeof(*lcw));
314 CDEBUG(D_INFO, "Could not allocate new lc_watchdog\n");
315 RETURN(ERR_PTR(-ENOMEM));
318 lcw->lcw_task = cfs_current();
319 lcw->lcw_pid = cfs_curproc_pid();
320 lcw->lcw_time = cfs_time_seconds(timeout_ms) / 1000;
321 lcw->lcw_callback = (callback != NULL) ? callback : lc_watchdog_dumplog;
322 lcw->lcw_data = data;
323 lcw->lcw_state = LC_WATCHDOG_DISABLED;
325 CFS_INIT_LIST_HEAD(&lcw->lcw_list);
326 cfs_timer_init(&lcw->lcw_timer, lcw_cb, lcw);
328 down(&lcw_refcount_sem);
329 if (++lcw_refcount == 1)
330 lcw_dispatch_start();
331 up(&lcw_refcount_sem);
333 /* Keep this working in case we enable them by default */
334 if (lcw->lcw_state == LC_WATCHDOG_ENABLED) {
335 do_gettimeofday(&lcw->lcw_last_touched);
336 cfs_timer_arm(&lcw->lcw_timer, lcw->lcw_time +
342 EXPORT_SYMBOL(lc_watchdog_add);
344 static void lcw_update_time(struct lc_watchdog *lcw, const char *message)
346 struct timeval newtime;
347 struct timeval timediff;
349 do_gettimeofday(&newtime);
350 if (lcw->lcw_state == LC_WATCHDOG_EXPIRED) {
351 cfs_timeval_sub(&newtime, &lcw->lcw_last_touched, &timediff);
352 CWARN("Expired watchdog for pid " LPPID " %s after %lu.%.4lus\n",
356 timediff.tv_usec / 100);
358 lcw->lcw_last_touched = newtime;
361 void lc_watchdog_touch_ms(struct lc_watchdog *lcw, int timeout_ms)
364 LASSERT(lcw != NULL);
366 spin_lock_bh(&lcw_pending_timers_lock);
367 list_del_init(&lcw->lcw_list);
368 spin_unlock_bh(&lcw_pending_timers_lock);
370 lcw_update_time(lcw, "touched");
371 lcw->lcw_state = LC_WATCHDOG_ENABLED;
373 cfs_timer_arm(&lcw->lcw_timer, cfs_time_current() +
374 cfs_time_seconds(timeout_ms) / 1000);
378 EXPORT_SYMBOL(lc_watchdog_touch_ms);
380 /* deprecated - use above instead */
381 void lc_watchdog_touch(struct lc_watchdog *lcw)
383 lc_watchdog_touch_ms(lcw, (int)cfs_duration_sec(lcw->lcw_time) * 1000);
385 EXPORT_SYMBOL(lc_watchdog_touch);
387 void lc_watchdog_disable(struct lc_watchdog *lcw)
390 LASSERT(lcw != NULL);
392 spin_lock_bh(&lcw_pending_timers_lock);
393 if (!list_empty(&lcw->lcw_list))
394 list_del_init(&lcw->lcw_list);
395 spin_unlock_bh(&lcw_pending_timers_lock);
397 lcw_update_time(lcw, "disabled");
398 lcw->lcw_state = LC_WATCHDOG_DISABLED;
402 EXPORT_SYMBOL(lc_watchdog_disable);
404 void lc_watchdog_delete(struct lc_watchdog *lcw)
407 LASSERT(lcw != NULL);
409 cfs_timer_disarm(&lcw->lcw_timer);
411 lcw_update_time(lcw, "deleted");
413 spin_lock_bh(&lcw_pending_timers_lock);
414 if (!list_empty(&lcw->lcw_list))
415 list_del_init(&lcw->lcw_list);
416 spin_unlock_bh(&lcw_pending_timers_lock);
418 down(&lcw_refcount_sem);
419 if (--lcw_refcount == 0)
421 up(&lcw_refcount_sem);
423 LIBCFS_FREE(lcw, sizeof(*lcw));
427 EXPORT_SYMBOL(lc_watchdog_delete);
430 * Provided watchdog handlers
433 void lc_watchdog_dumplog(pid_t pid, void *data)
435 libcfs_debug_dumplog_internal((void *)((long_ptr_t)pid));
437 EXPORT_SYMBOL(lc_watchdog_dumplog);
439 #else /* !defined(WITH_WATCHDOG) */
441 struct lc_watchdog *lc_watchdog_add(int timeout_ms,
442 void (*callback)(pid_t pid, void *),
445 static struct lc_watchdog watchdog;
448 EXPORT_SYMBOL(lc_watchdog_add);
450 void lc_watchdog_touch_ms(struct lc_watchdog *lcw, int timeout_ms)
453 EXPORT_SYMBOL(lc_watchdog_touch_ms);
455 void lc_watchdog_touch(struct lc_watchdog *lcw)
458 EXPORT_SYMBOL(lc_watchdog_touch);
460 void lc_watchdog_disable(struct lc_watchdog *lcw)
463 EXPORT_SYMBOL(lc_watchdog_disable);
465 void lc_watchdog_delete(struct lc_watchdog *lcw)
468 EXPORT_SYMBOL(lc_watchdog_delete);