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 */
49 cfs_time_t lcw_last_touched;
52 void (*lcw_callback)(pid_t, void *);
66 * The dispatcher will complete lcw_start_completion when it starts,
67 * and lcw_stop_completion when it exits.
68 * Wake lcw_event_waitq to signal timer callback dispatches.
70 static cfs_completion_t lcw_start_completion;
71 static cfs_completion_t lcw_stop_completion;
72 static cfs_waitq_t lcw_event_waitq;
75 * Set this and wake lcw_event_waitq to stop the dispatcher.
80 static unsigned long lcw_flags = 0;
83 * Number of outstanding watchdogs.
84 * When it hits 1, we start the dispatcher.
85 * When it hits 0, we stop the distpatcher.
87 static __u32 lcw_refcount = 0;
88 static CFS_DECLARE_MUTEX(lcw_refcount_sem);
91 * List of timers that have fired that need their callbacks run by the
95 static cfs_spinlock_t lcw_pending_timers_lock = CFS_SPIN_LOCK_UNLOCKED;
96 static cfs_list_t 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 cfs_spinlock_t lcw_last_watchdog_lock = CFS_SPIN_LOCK_UNLOCKED;
105 lcw_dump(struct lc_watchdog *lcw)
108 #if defined(HAVE_TASKLIST_LOCK)
109 cfs_read_lock(&tasklist_lock);
110 #elif defined(HAVE_TASK_RCU)
113 CERROR("unable to dump stack because of missing export\n");
116 if (lcw->lcw_task == NULL) {
117 LCONSOLE_WARN("Process " LPPID " was not found in the task "
118 "list; watchdog callback may be incomplete\n",
121 libcfs_debug_dumpstack(lcw->lcw_task);
124 #if defined(HAVE_TASKLIST_LOCK)
125 cfs_read_unlock(&tasklist_lock);
126 #elif defined(HAVE_TASK_RCU)
132 static void lcw_cb(ulong_ptr_t data)
134 struct lc_watchdog *lcw = (struct lc_watchdog *)data;
135 cfs_time_t current_time;
136 cfs_duration_t delta_time;
137 struct timeval timediff;
141 if (lcw->lcw_state != LC_WATCHDOG_ENABLED) {
146 lcw->lcw_state = LC_WATCHDOG_EXPIRED;
147 current_time = cfs_time_current();
149 delta_time = cfs_time_sub(current_time, lcw->lcw_last_touched);
150 cfs_duration_usec(delta_time, &timediff);
152 /* Check to see if we should throttle the watchdog timer to avoid
153 * too many dumps going to the console thus triggering an NMI.
154 * Normally we would not hold the spin lock over the CWARN but in
155 * this case we hold it to ensure non ratelimited lcw_dumps are not
156 * interleaved on the console making them hard to read. */
157 cfs_spin_lock_bh(&lcw_last_watchdog_lock);
158 delta_time = cfs_duration_sec(cfs_time_sub(current_time,
159 lcw_last_watchdog_time));
161 if (delta_time < libcfs_watchdog_ratelimit &&
162 lcw_recent_watchdog_count > 3) {
163 LCONSOLE_WARN("Service thread pid %u was inactive for "
164 "%lu.%.02lus. Watchdog stack traces are limited "
165 "to 3 per %d seconds, skipping this one.\n",
168 timediff.tv_usec / 10000,
169 libcfs_watchdog_ratelimit);
171 if (delta_time < libcfs_watchdog_ratelimit) {
172 lcw_recent_watchdog_count++;
174 memcpy(&lcw_last_watchdog_time, ¤t_time,
175 sizeof(current_time));
176 lcw_recent_watchdog_count = 0;
179 /* This warning should appear on the console, but may not get
180 * into the logs since we're running in a softirq handler */
181 LCONSOLE_WARN("Service thread pid %u was inactive for "
182 "%lu.%.02lus. The thread might be hung, or it "
183 "might only be slow and will resume later. "
184 "Dumping the stack trace for debugging purposes:"
188 timediff.tv_usec / 10000);
192 cfs_spin_unlock_bh(&lcw_last_watchdog_lock);
193 cfs_spin_lock_bh(&lcw_pending_timers_lock);
195 if (cfs_list_empty(&lcw->lcw_list)) {
196 cfs_list_add(&lcw->lcw_list, &lcw_pending_timers);
197 cfs_waitq_signal(&lcw_event_waitq);
200 cfs_spin_unlock_bh(&lcw_pending_timers_lock);
205 static int is_watchdog_fired(void)
209 if (cfs_test_bit(LCW_FLAG_STOP, &lcw_flags))
212 cfs_spin_lock_bh(&lcw_pending_timers_lock);
213 rc = !cfs_list_empty(&lcw_pending_timers);
214 cfs_spin_unlock_bh(&lcw_pending_timers_lock);
218 static int lcw_dispatch_main(void *data)
222 struct lc_watchdog *lcw;
226 cfs_daemonize("lc_watchdogd");
228 SIGNAL_MASK_LOCK(current, flags);
229 sigfillset(¤t->blocked);
231 SIGNAL_MASK_UNLOCK(current, flags);
233 cfs_complete(&lcw_start_completion);
236 cfs_wait_event_interruptible(lcw_event_waitq,
237 is_watchdog_fired(), rc);
238 CDEBUG(D_INFO, "Watchdog got woken up...\n");
239 if (cfs_test_bit(LCW_FLAG_STOP, &lcw_flags)) {
240 CDEBUG(D_INFO, "LCW_FLAG_STOP was set, shutting down...\n");
242 cfs_spin_lock_bh(&lcw_pending_timers_lock);
243 rc = !cfs_list_empty(&lcw_pending_timers);
244 cfs_spin_unlock_bh(&lcw_pending_timers_lock);
246 CERROR("pending timers list was not empty at "
247 "time of watchdog dispatch shutdown\n");
252 cfs_spin_lock_bh(&lcw_pending_timers_lock);
253 while (!cfs_list_empty(&lcw_pending_timers)) {
255 lcw = cfs_list_entry(lcw_pending_timers.next,
258 cfs_list_del_init(&lcw->lcw_list);
259 cfs_spin_unlock_bh(&lcw_pending_timers_lock);
261 CDEBUG(D_INFO, "found lcw for pid " LPPID "\n", lcw->lcw_pid);
263 if (lcw->lcw_state != LC_WATCHDOG_DISABLED)
264 lcw->lcw_callback(lcw->lcw_pid, lcw->lcw_data);
266 cfs_spin_lock_bh(&lcw_pending_timers_lock);
268 cfs_spin_unlock_bh(&lcw_pending_timers_lock);
271 cfs_complete(&lcw_stop_completion);
276 static void lcw_dispatch_start(void)
281 LASSERT(lcw_refcount == 1);
283 cfs_init_completion(&lcw_stop_completion);
284 cfs_init_completion(&lcw_start_completion);
285 cfs_waitq_init(&lcw_event_waitq);
287 CDEBUG(D_INFO, "starting dispatch thread\n");
288 rc = cfs_kernel_thread(lcw_dispatch_main, NULL, 0);
290 CERROR("error spawning watchdog dispatch thread: %d\n", rc);
294 cfs_wait_for_completion(&lcw_start_completion);
295 CDEBUG(D_INFO, "watchdog dispatcher initialization complete.\n");
300 static void lcw_dispatch_stop(void)
303 LASSERT(lcw_refcount == 0);
305 CDEBUG(D_INFO, "trying to stop watchdog dispatcher.\n");
307 cfs_set_bit(LCW_FLAG_STOP, &lcw_flags);
308 cfs_waitq_signal(&lcw_event_waitq);
310 cfs_wait_for_completion(&lcw_stop_completion);
312 CDEBUG(D_INFO, "watchdog dispatcher has shut down.\n");
317 struct lc_watchdog *lc_watchdog_add(int timeout,
318 void (*callback)(pid_t, void *),
321 struct lc_watchdog *lcw = NULL;
324 LIBCFS_ALLOC(lcw, sizeof(*lcw));
326 CDEBUG(D_INFO, "Could not allocate new lc_watchdog\n");
327 RETURN(ERR_PTR(-ENOMEM));
330 lcw->lcw_task = cfs_current();
331 lcw->lcw_pid = cfs_curproc_pid();
332 lcw->lcw_callback = (callback != NULL) ? callback : lc_watchdog_dumplog;
333 lcw->lcw_data = data;
334 lcw->lcw_state = LC_WATCHDOG_DISABLED;
336 CFS_INIT_LIST_HEAD(&lcw->lcw_list);
337 cfs_timer_init(&lcw->lcw_timer, lcw_cb, lcw);
339 cfs_down(&lcw_refcount_sem);
340 if (++lcw_refcount == 1)
341 lcw_dispatch_start();
342 cfs_up(&lcw_refcount_sem);
344 /* Keep this working in case we enable them by default */
345 if (lcw->lcw_state == LC_WATCHDOG_ENABLED) {
346 lcw->lcw_last_touched = cfs_time_current();
347 cfs_timer_arm(&lcw->lcw_timer, cfs_time_seconds(timeout) +
353 EXPORT_SYMBOL(lc_watchdog_add);
355 static void lcw_update_time(struct lc_watchdog *lcw, const char *message)
357 cfs_time_t newtime = cfs_time_current();;
359 if (lcw->lcw_state == LC_WATCHDOG_EXPIRED) {
360 struct timeval timediff;
361 cfs_time_t delta_time = cfs_time_sub(newtime,
362 lcw->lcw_last_touched);
363 cfs_duration_usec(delta_time, &timediff);
365 LCONSOLE_WARN("Service thread pid %u %s after %lu.%.02lus. "
366 "This indicates the system was overloaded (too "
367 "many service threads, or there were not enough "
368 "hardware resources).\n",
372 timediff.tv_usec / 10000);
374 lcw->lcw_last_touched = newtime;
377 void lc_watchdog_touch(struct lc_watchdog *lcw, int timeout)
380 LASSERT(lcw != NULL);
382 cfs_spin_lock_bh(&lcw_pending_timers_lock);
383 cfs_list_del_init(&lcw->lcw_list);
384 cfs_spin_unlock_bh(&lcw_pending_timers_lock);
386 lcw_update_time(lcw, "resumed");
387 lcw->lcw_state = LC_WATCHDOG_ENABLED;
389 cfs_timer_arm(&lcw->lcw_timer, cfs_time_current() +
390 cfs_time_seconds(timeout));
394 EXPORT_SYMBOL(lc_watchdog_touch);
396 void lc_watchdog_disable(struct lc_watchdog *lcw)
399 LASSERT(lcw != NULL);
401 cfs_spin_lock_bh(&lcw_pending_timers_lock);
402 if (!cfs_list_empty(&lcw->lcw_list))
403 cfs_list_del_init(&lcw->lcw_list);
404 cfs_spin_unlock_bh(&lcw_pending_timers_lock);
406 lcw_update_time(lcw, "completed");
407 lcw->lcw_state = LC_WATCHDOG_DISABLED;
411 EXPORT_SYMBOL(lc_watchdog_disable);
413 void lc_watchdog_delete(struct lc_watchdog *lcw)
416 LASSERT(lcw != NULL);
418 cfs_timer_disarm(&lcw->lcw_timer);
420 lcw_update_time(lcw, "stopped");
422 cfs_spin_lock_bh(&lcw_pending_timers_lock);
423 if (!cfs_list_empty(&lcw->lcw_list))
424 cfs_list_del_init(&lcw->lcw_list);
425 cfs_spin_unlock_bh(&lcw_pending_timers_lock);
427 cfs_down(&lcw_refcount_sem);
428 if (--lcw_refcount == 0)
430 cfs_up(&lcw_refcount_sem);
432 LIBCFS_FREE(lcw, sizeof(*lcw));
436 EXPORT_SYMBOL(lc_watchdog_delete);
439 * Provided watchdog handlers
442 void lc_watchdog_dumplog(pid_t pid, void *data)
444 libcfs_debug_dumplog_internal((void *)((long_ptr_t)pid));
446 EXPORT_SYMBOL(lc_watchdog_dumplog);
448 #else /* !defined(WITH_WATCHDOG) */
450 struct lc_watchdog *lc_watchdog_add(int timeout,
451 void (*callback)(pid_t pid, void *),
454 static struct lc_watchdog watchdog;
457 EXPORT_SYMBOL(lc_watchdog_add);
459 void lc_watchdog_touch(struct lc_watchdog *lcw, int timeout)
462 EXPORT_SYMBOL(lc_watchdog_touch);
464 void lc_watchdog_disable(struct lc_watchdog *lcw)
467 EXPORT_SYMBOL(lc_watchdog_disable);
469 void lc_watchdog_delete(struct lc_watchdog *lcw)
472 EXPORT_SYMBOL(lc_watchdog_delete);