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 * lnet/libcfs/watchdog.c
38 * Author: Jacob Berkman <jacob@clusterfs.com>
41 #define DEBUG_SUBSYSTEM S_LNET
43 #include <libcfs/kp30.h>
44 #include <libcfs/libcfs.h>
45 #include "tracefile.h"
48 cfs_timer_t lcw_timer; /* kernel timer */
49 struct list_head lcw_list;
50 cfs_time_t lcw_last_touched;
53 void (*lcw_callback)(pid_t, void *);
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 wait_queue_head_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 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;
105 lcw_dump(struct lc_watchdog *lcw)
109 #if defined(HAVE_TASKLIST_LOCK)
110 read_lock(&tasklist_lock);
111 #elif defined(HAVE_TASK_RCU)
114 CERROR("unable to dump stack because of missing export\n");
117 if (lcw->lcw_task == NULL) {
118 CWARN("Process %d was not found in the task list; "
119 "watchdog callback may be incomplete\n",
122 libcfs_debug_dumpstack(lcw->lcw_task);
125 #if defined(HAVE_TASKLIST_LOCK)
126 read_unlock(&tasklist_lock);
127 #elif defined(HAVE_TASK_RCU)
133 static void lcw_cb(unsigned long data)
135 struct lc_watchdog *lcw = (struct lc_watchdog *)data;
136 cfs_time_t current_time;
137 cfs_duration_t delta_time;
138 struct timeval timediff;
142 if (lcw->lcw_state != LC_WATCHDOG_ENABLED) {
147 lcw->lcw_state = LC_WATCHDOG_EXPIRED;
148 current_time = cfs_time_current();
150 delta_time = cfs_time_sub(current_time, lcw->lcw_last_touched);
151 cfs_duration_usec(delta_time, &timediff);
153 /* Check to see if we should throttle the watchdog timer to avoid
154 * too many dumps going to the console thus triggering an NMI.
155 * Normally we would not hold the spin lock over the CWARN but in
156 * this case we hold it to ensure non ratelimited lcw_dumps are not
157 * interleaved on the console making them hard to read. */
158 spin_lock_bh(&lcw_last_watchdog_lock);
159 delta_time = cfs_duration_sec(cfs_time_sub(current_time,
160 lcw_last_watchdog_time));
162 if (delta_time < libcfs_watchdog_ratelimit &&
163 lcw_recent_watchdog_count > 3) {
164 CWARN("Refusing to fire watchdog for pid %d: it was inactive "
165 "for %lu.%.02lus. Rate limiting 1 per %d seconds.\n",
166 (int)lcw->lcw_pid, timediff.tv_sec,
167 timediff.tv_usec / 10000, libcfs_watchdog_ratelimit);
169 if (delta_time < libcfs_watchdog_ratelimit) {
170 lcw_recent_watchdog_count++;
172 memcpy(&lcw_last_watchdog_time, ¤t_time,
173 sizeof(current_time));
174 lcw_recent_watchdog_count = 0;
177 /* This warning should appear on the console, but may not get
178 * into the logs since we're running in a softirq handler */
179 CWARN("Watchdog triggered for pid %d: it was inactive for "
180 "%lu.%.02lus\n", (int)lcw->lcw_pid, timediff.tv_sec,
181 timediff.tv_usec / 10000);
185 spin_unlock_bh(&lcw_last_watchdog_lock);
186 spin_lock_bh(&lcw_pending_timers_lock);
188 if (list_empty(&lcw->lcw_list)) {
189 list_add(&lcw->lcw_list, &lcw_pending_timers);
190 wake_up(&lcw_event_waitq);
193 spin_unlock_bh(&lcw_pending_timers_lock);
198 static int is_watchdog_fired(void)
202 if (test_bit(LCW_FLAG_STOP, &lcw_flags))
205 spin_lock_bh(&lcw_pending_timers_lock);
206 rc = !list_empty(&lcw_pending_timers);
207 spin_unlock_bh(&lcw_pending_timers_lock);
211 static int lcw_dispatch_main(void *data)
215 struct lc_watchdog *lcw;
219 cfs_daemonize("lc_watchdogd");
221 SIGNAL_MASK_LOCK(current, flags);
222 sigfillset(¤t->blocked);
224 SIGNAL_MASK_UNLOCK(current, flags);
226 complete(&lcw_start_completion);
229 wait_event_interruptible(lcw_event_waitq, is_watchdog_fired());
230 CDEBUG(D_INFO, "Watchdog got woken up...\n");
231 if (test_bit(LCW_FLAG_STOP, &lcw_flags)) {
232 CDEBUG(D_INFO, "LCW_FLAG_STOP was set, shutting down...\n");
234 spin_lock_bh(&lcw_pending_timers_lock);
235 rc = !list_empty(&lcw_pending_timers);
236 spin_unlock_bh(&lcw_pending_timers_lock);
238 CERROR("pending timers list was not empty at "
239 "time of watchdog dispatch shutdown\n");
244 spin_lock_bh(&lcw_pending_timers_lock);
245 while (!list_empty(&lcw_pending_timers)) {
247 lcw = list_entry(lcw_pending_timers.next,
250 list_del_init(&lcw->lcw_list);
251 spin_unlock_bh(&lcw_pending_timers_lock);
253 CDEBUG(D_INFO, "found lcw for pid %d\n",
256 if (lcw->lcw_state != LC_WATCHDOG_DISABLED)
257 lcw->lcw_callback(lcw->lcw_pid, lcw->lcw_data);
259 spin_lock_bh(&lcw_pending_timers_lock);
261 spin_unlock_bh(&lcw_pending_timers_lock);
264 complete(&lcw_stop_completion);
269 static void lcw_dispatch_start(void)
274 LASSERT(lcw_refcount == 1);
276 init_completion(&lcw_stop_completion);
277 init_completion(&lcw_start_completion);
278 init_waitqueue_head(&lcw_event_waitq);
280 CDEBUG(D_INFO, "starting dispatch thread\n");
281 rc = kernel_thread(lcw_dispatch_main, NULL, 0);
283 CERROR("error spawning watchdog dispatch thread: %d\n", rc);
287 wait_for_completion(&lcw_start_completion);
288 CDEBUG(D_INFO, "watchdog dispatcher initialization complete.\n");
293 static void lcw_dispatch_stop(void)
296 LASSERT(lcw_refcount == 0);
298 CDEBUG(D_INFO, "trying to stop watchdog dispatcher.\n");
300 set_bit(LCW_FLAG_STOP, &lcw_flags);
301 wake_up(&lcw_event_waitq);
303 wait_for_completion(&lcw_stop_completion);
305 CDEBUG(D_INFO, "watchdog dispatcher has shut down.\n");
310 struct lc_watchdog *lc_watchdog_add(int timeout,
311 void (*callback)(pid_t, void *),
314 struct lc_watchdog *lcw = NULL;
317 LIBCFS_ALLOC(lcw, sizeof(*lcw));
319 CDEBUG(D_INFO, "Could not allocate new lc_watchdog\n");
320 RETURN(ERR_PTR(-ENOMEM));
323 lcw->lcw_task = cfs_current();
324 lcw->lcw_pid = cfs_curproc_pid();
325 lcw->lcw_callback = (callback != NULL) ? callback : lc_watchdog_dumplog;
326 lcw->lcw_data = data;
327 lcw->lcw_state = LC_WATCHDOG_DISABLED;
329 INIT_LIST_HEAD(&lcw->lcw_list);
331 lcw->lcw_timer.function = lcw_cb;
332 lcw->lcw_timer.data = (unsigned long)lcw;
333 lcw->lcw_timer.expires = jiffies + cfs_time_seconds(timeout);
334 init_timer(&lcw->lcw_timer);
336 down(&lcw_refcount_sem);
337 if (++lcw_refcount == 1)
338 lcw_dispatch_start();
339 up(&lcw_refcount_sem);
341 /* Keep this working in case we enable them by default */
342 if (lcw->lcw_state == LC_WATCHDOG_ENABLED) {
343 lcw->lcw_last_touched = cfs_time_current();
344 add_timer(&lcw->lcw_timer);
349 EXPORT_SYMBOL(lc_watchdog_add);
351 static void lcw_update_time(struct lc_watchdog *lcw, const char *message)
353 cfs_time_t newtime = cfs_time_current();;
355 if (lcw->lcw_state == LC_WATCHDOG_EXPIRED) {
356 struct timeval timediff;
357 cfs_time_t delta_time = cfs_time_sub(newtime,
358 lcw->lcw_last_touched);
359 cfs_duration_usec(delta_time, &timediff);
361 CWARN("Expired watchdog for pid %d %s after %lu.%.02lus\n",
362 lcw->lcw_pid, message, timediff.tv_sec,
363 timediff.tv_usec / 10000);
365 lcw->lcw_last_touched = newtime;
368 void lc_watchdog_touch(struct lc_watchdog *lcw, int timeout)
371 LASSERT(lcw != NULL);
373 spin_lock_bh(&lcw_pending_timers_lock);
374 list_del_init(&lcw->lcw_list);
375 spin_unlock_bh(&lcw_pending_timers_lock);
377 lcw_update_time(lcw, "touched");
378 lcw->lcw_state = LC_WATCHDOG_ENABLED;
380 mod_timer(&lcw->lcw_timer, jiffies + cfs_time_seconds(timeout));
384 EXPORT_SYMBOL(lc_watchdog_touch);
386 void lc_watchdog_disable(struct lc_watchdog *lcw)
389 LASSERT(lcw != NULL);
391 spin_lock_bh(&lcw_pending_timers_lock);
392 if (!list_empty(&lcw->lcw_list))
393 list_del_init(&lcw->lcw_list);
394 spin_unlock_bh(&lcw_pending_timers_lock);
396 lcw_update_time(lcw, "disabled");
397 lcw->lcw_state = LC_WATCHDOG_DISABLED;
401 EXPORT_SYMBOL(lc_watchdog_disable);
403 void lc_watchdog_delete(struct lc_watchdog *lcw)
406 LASSERT(lcw != NULL);
408 del_timer(&lcw->lcw_timer);
410 lcw_update_time(lcw, "deleted");
412 spin_lock_bh(&lcw_pending_timers_lock);
413 if (!list_empty(&lcw->lcw_list))
414 list_del_init(&lcw->lcw_list);
415 spin_unlock_bh(&lcw_pending_timers_lock);
417 down(&lcw_refcount_sem);
418 if (--lcw_refcount == 0)
420 up(&lcw_refcount_sem);
422 LIBCFS_FREE(lcw, sizeof(*lcw));
426 EXPORT_SYMBOL(lc_watchdog_delete);
429 * Provided watchdog handlers
432 void lc_watchdog_dumplog(pid_t pid, void *data)
434 libcfs_debug_dumplog_internal((void *)((unsigned long)pid));
436 EXPORT_SYMBOL(lc_watchdog_dumplog);
438 #else /* !defined(WITH_WATCHDOG) */
440 struct lc_watchdog *lc_watchdog_add(int timeout,
441 void (*callback)(pid_t pid, void *),
444 static struct lc_watchdog watchdog;
447 EXPORT_SYMBOL(lc_watchdog_add);
449 void lc_watchdog_touch(struct lc_watchdog *lcw, int timeout)
452 EXPORT_SYMBOL(lc_watchdog_touch);
454 void lc_watchdog_disable(struct lc_watchdog *lcw)
457 EXPORT_SYMBOL(lc_watchdog_disable);
459 void lc_watchdog_delete(struct lc_watchdog *lcw)
462 EXPORT_SYMBOL(lc_watchdog_delete);