4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
31 * This file is part of Lustre, http://www.lustre.org/
32 * Lustre is a trademark of Sun Microsystems, Inc.
34 * libcfs/libcfs/watchdog.c
36 * Author: Jacob Berkman <jacob@clusterfs.com>
39 #define DEBUG_SUBSYSTEM S_LNET
41 #include <libcfs/libcfs.h>
42 #include "tracefile.h"
45 cfs_spinlock_t lcw_lock; /* check or change lcw_list */
46 int lcw_refcount; /* must hold lcw_pending_timers_lock */
47 cfs_timer_t lcw_timer; /* kernel timer */
48 cfs_list_t lcw_list; /* chain on pending list */
49 cfs_time_t lcw_last_touched; /* last touched stamp */
50 cfs_task_t *lcw_task; /* owner task */
51 void (*lcw_callback)(pid_t, void *);
65 * The dispatcher will complete lcw_start_completion when it starts,
66 * and lcw_stop_completion when it exits.
67 * Wake lcw_event_waitq to signal timer callback dispatches.
69 static cfs_completion_t lcw_start_completion;
70 static cfs_completion_t lcw_stop_completion;
71 static cfs_waitq_t lcw_event_waitq;
74 * Set this and wake lcw_event_waitq to stop the dispatcher.
79 static unsigned long lcw_flags = 0;
82 * Number of outstanding watchdogs.
83 * When it hits 1, we start the dispatcher.
84 * When it hits 0, we stop the dispatcher.
86 static __u32 lcw_refcount = 0;
87 static CFS_DEFINE_MUTEX(lcw_refcount_mutex);
90 * List of timers that have fired that need their callbacks run by the
94 static DEFINE_SPINLOCK(lcw_pending_timers_lock);
95 static cfs_list_t lcw_pending_timers = CFS_LIST_HEAD_INIT(lcw_pending_timers);
97 /* Last time a watchdog expired */
98 static cfs_time_t lcw_last_watchdog_time;
99 static int lcw_recent_watchdog_count;
102 lcw_dump(struct lc_watchdog *lcw)
105 #if defined(HAVE_TASKLIST_LOCK)
106 cfs_read_lock(&tasklist_lock);
110 if (lcw->lcw_task == NULL) {
111 LCONSOLE_WARN("Process " LPPID " was not found in the task "
112 "list; watchdog callback may be incomplete\n",
115 libcfs_debug_dumpstack(lcw->lcw_task);
118 #if defined(HAVE_TASKLIST_LOCK)
119 cfs_read_unlock(&tasklist_lock);
126 static void lcw_cb(ulong_ptr_t data)
128 struct lc_watchdog *lcw = (struct lc_watchdog *)data;
131 if (lcw->lcw_state != LC_WATCHDOG_ENABLED) {
136 lcw->lcw_state = LC_WATCHDOG_EXPIRED;
138 cfs_spin_lock_bh(&lcw->lcw_lock);
139 LASSERT(cfs_list_empty(&lcw->lcw_list));
141 cfs_spin_lock_bh(&lcw_pending_timers_lock);
142 lcw->lcw_refcount++; /* +1 for pending list */
143 cfs_list_add(&lcw->lcw_list, &lcw_pending_timers);
144 cfs_waitq_signal(&lcw_event_waitq);
146 cfs_spin_unlock_bh(&lcw_pending_timers_lock);
147 cfs_spin_unlock_bh(&lcw->lcw_lock);
151 static int is_watchdog_fired(void)
155 if (cfs_test_bit(LCW_FLAG_STOP, &lcw_flags))
158 cfs_spin_lock_bh(&lcw_pending_timers_lock);
159 rc = !cfs_list_empty(&lcw_pending_timers);
160 cfs_spin_unlock_bh(&lcw_pending_timers_lock);
164 static void lcw_dump_stack(struct lc_watchdog *lcw)
166 cfs_time_t current_time;
167 cfs_duration_t delta_time;
168 struct timeval timediff;
170 current_time = cfs_time_current();
171 delta_time = cfs_time_sub(current_time, lcw->lcw_last_touched);
172 cfs_duration_usec(delta_time, &timediff);
175 * Check to see if we should throttle the watchdog timer to avoid
176 * too many dumps going to the console thus triggering an NMI.
178 delta_time = cfs_duration_sec(cfs_time_sub(current_time,
179 lcw_last_watchdog_time));
181 if (delta_time < libcfs_watchdog_ratelimit &&
182 lcw_recent_watchdog_count > 3) {
183 LCONSOLE_WARN("Service thread pid %u was inactive for "
184 "%lu.%.02lus. Watchdog stack traces are limited "
185 "to 3 per %d seconds, skipping this one.\n",
188 timediff.tv_usec / 10000,
189 libcfs_watchdog_ratelimit);
191 if (delta_time < libcfs_watchdog_ratelimit) {
192 lcw_recent_watchdog_count++;
194 memcpy(&lcw_last_watchdog_time, ¤t_time,
195 sizeof(current_time));
196 lcw_recent_watchdog_count = 0;
199 LCONSOLE_WARN("Service thread pid %u was inactive for "
200 "%lu.%.02lus. The thread might be hung, or it "
201 "might only be slow and will resume later. "
202 "Dumping the stack trace for debugging purposes:"
206 timediff.tv_usec / 10000);
211 static int lcw_dispatch_main(void *data)
215 struct lc_watchdog *lcw;
216 CFS_LIST_HEAD (zombies);
220 cfs_daemonize("lc_watchdogd");
222 SIGNAL_MASK_LOCK(current, flags);
223 sigfillset(¤t->blocked);
225 SIGNAL_MASK_UNLOCK(current, flags);
227 cfs_complete(&lcw_start_completion);
232 cfs_wait_event_interruptible(lcw_event_waitq,
233 is_watchdog_fired(), rc);
234 CDEBUG(D_INFO, "Watchdog got woken up...\n");
235 if (cfs_test_bit(LCW_FLAG_STOP, &lcw_flags)) {
236 CDEBUG(D_INFO, "LCW_FLAG_STOP was set, shutting down...\n");
238 cfs_spin_lock_bh(&lcw_pending_timers_lock);
239 rc = !cfs_list_empty(&lcw_pending_timers);
240 cfs_spin_unlock_bh(&lcw_pending_timers_lock);
242 CERROR("pending timers list was not empty at "
243 "time of watchdog dispatch shutdown\n");
248 cfs_spin_lock_bh(&lcw_pending_timers_lock);
249 while (!cfs_list_empty(&lcw_pending_timers)) {
252 lcw = cfs_list_entry(lcw_pending_timers.next,
253 struct lc_watchdog, lcw_list);
254 /* +1 ref for callback to make sure lwc wouldn't be
255 * deleted after releasing lcw_pending_timers_lock */
257 cfs_spin_unlock_bh(&lcw_pending_timers_lock);
260 cfs_spin_lock_bh(&lcw->lcw_lock);
261 cfs_spin_lock_bh(&lcw_pending_timers_lock);
263 if (cfs_list_empty(&lcw->lcw_list)) {
264 /* already removed from pending list */
265 lcw->lcw_refcount--; /* -1 ref for callback */
266 if (lcw->lcw_refcount == 0)
267 cfs_list_add(&lcw->lcw_list, &zombies);
268 cfs_spin_unlock_bh(&lcw->lcw_lock);
269 /* still hold lcw_pending_timers_lock */
273 cfs_list_del_init(&lcw->lcw_list);
274 lcw->lcw_refcount--; /* -1 ref for pending list */
276 cfs_spin_unlock_bh(&lcw_pending_timers_lock);
277 cfs_spin_unlock_bh(&lcw->lcw_lock);
279 CDEBUG(D_INFO, "found lcw for pid " LPPID "\n",
283 is_dumplog = lcw->lcw_callback == lc_watchdog_dumplog;
284 if (lcw->lcw_state != LC_WATCHDOG_DISABLED &&
285 (dumplog || !is_dumplog)) {
286 lcw->lcw_callback(lcw->lcw_pid, lcw->lcw_data);
287 if (dumplog && is_dumplog)
291 cfs_spin_lock_bh(&lcw_pending_timers_lock);
292 lcw->lcw_refcount--; /* -1 ref for callback */
293 if (lcw->lcw_refcount == 0)
294 cfs_list_add(&lcw->lcw_list, &zombies);
296 cfs_spin_unlock_bh(&lcw_pending_timers_lock);
298 while (!cfs_list_empty(&zombies)) {
299 lcw = cfs_list_entry(lcw_pending_timers.next,
300 struct lc_watchdog, lcw_list);
301 cfs_list_del(&lcw->lcw_list);
302 LIBCFS_FREE(lcw, sizeof(*lcw));
306 cfs_complete(&lcw_stop_completion);
311 static void lcw_dispatch_start(void)
316 LASSERT(lcw_refcount == 1);
318 cfs_init_completion(&lcw_stop_completion);
319 cfs_init_completion(&lcw_start_completion);
320 cfs_waitq_init(&lcw_event_waitq);
322 CDEBUG(D_INFO, "starting dispatch thread\n");
323 rc = cfs_create_thread(lcw_dispatch_main, NULL, 0);
325 CERROR("error spawning watchdog dispatch thread: %d\n", rc);
329 cfs_wait_for_completion(&lcw_start_completion);
330 CDEBUG(D_INFO, "watchdog dispatcher initialization complete.\n");
335 static void lcw_dispatch_stop(void)
338 LASSERT(lcw_refcount == 0);
340 CDEBUG(D_INFO, "trying to stop watchdog dispatcher.\n");
342 cfs_set_bit(LCW_FLAG_STOP, &lcw_flags);
343 cfs_waitq_signal(&lcw_event_waitq);
345 cfs_wait_for_completion(&lcw_stop_completion);
347 CDEBUG(D_INFO, "watchdog dispatcher has shut down.\n");
352 struct lc_watchdog *lc_watchdog_add(int timeout,
353 void (*callback)(pid_t, void *),
356 struct lc_watchdog *lcw = NULL;
359 LIBCFS_ALLOC(lcw, sizeof(*lcw));
361 CDEBUG(D_INFO, "Could not allocate new lc_watchdog\n");
362 RETURN(ERR_PTR(-ENOMEM));
365 cfs_spin_lock_init(&lcw->lcw_lock);
366 lcw->lcw_refcount = 1; /* refcount for owner */
367 lcw->lcw_task = cfs_current();
368 lcw->lcw_pid = cfs_curproc_pid();
369 lcw->lcw_callback = (callback != NULL) ? callback : lc_watchdog_dumplog;
370 lcw->lcw_data = data;
371 lcw->lcw_state = LC_WATCHDOG_DISABLED;
373 CFS_INIT_LIST_HEAD(&lcw->lcw_list);
374 cfs_timer_init(&lcw->lcw_timer, lcw_cb, lcw);
376 cfs_mutex_lock(&lcw_refcount_mutex);
377 if (++lcw_refcount == 1)
378 lcw_dispatch_start();
379 cfs_mutex_unlock(&lcw_refcount_mutex);
381 /* Keep this working in case we enable them by default */
382 if (lcw->lcw_state == LC_WATCHDOG_ENABLED) {
383 lcw->lcw_last_touched = cfs_time_current();
384 cfs_timer_arm(&lcw->lcw_timer, cfs_time_seconds(timeout) +
390 EXPORT_SYMBOL(lc_watchdog_add);
392 static void lcw_update_time(struct lc_watchdog *lcw, const char *message)
394 cfs_time_t newtime = cfs_time_current();;
396 if (lcw->lcw_state == LC_WATCHDOG_EXPIRED) {
397 struct timeval timediff;
398 cfs_time_t delta_time = cfs_time_sub(newtime,
399 lcw->lcw_last_touched);
400 cfs_duration_usec(delta_time, &timediff);
402 LCONSOLE_WARN("Service thread pid %u %s after %lu.%.02lus. "
403 "This indicates the system was overloaded (too "
404 "many service threads, or there were not enough "
405 "hardware resources).\n",
409 timediff.tv_usec / 10000);
411 lcw->lcw_last_touched = newtime;
414 static void lc_watchdog_del_pending(struct lc_watchdog *lcw)
416 cfs_spin_lock_bh(&lcw->lcw_lock);
417 if (unlikely(!cfs_list_empty(&lcw->lcw_list))) {
418 cfs_spin_lock_bh(&lcw_pending_timers_lock);
419 cfs_list_del_init(&lcw->lcw_list);
420 lcw->lcw_refcount--; /* -1 ref for pending list */
421 cfs_spin_unlock_bh(&lcw_pending_timers_lock);
424 cfs_spin_unlock_bh(&lcw->lcw_lock);
427 void lc_watchdog_touch(struct lc_watchdog *lcw, int timeout)
430 LASSERT(lcw != NULL);
432 lc_watchdog_del_pending(lcw);
434 lcw_update_time(lcw, "resumed");
435 lcw->lcw_state = LC_WATCHDOG_ENABLED;
437 cfs_timer_arm(&lcw->lcw_timer, cfs_time_current() +
438 cfs_time_seconds(timeout));
442 EXPORT_SYMBOL(lc_watchdog_touch);
444 void lc_watchdog_disable(struct lc_watchdog *lcw)
447 LASSERT(lcw != NULL);
449 lc_watchdog_del_pending(lcw);
451 lcw_update_time(lcw, "completed");
452 lcw->lcw_state = LC_WATCHDOG_DISABLED;
456 EXPORT_SYMBOL(lc_watchdog_disable);
458 void lc_watchdog_delete(struct lc_watchdog *lcw)
463 LASSERT(lcw != NULL);
465 cfs_timer_disarm(&lcw->lcw_timer);
467 lcw_update_time(lcw, "stopped");
469 cfs_spin_lock_bh(&lcw->lcw_lock);
470 cfs_spin_lock_bh(&lcw_pending_timers_lock);
471 if (unlikely(!cfs_list_empty(&lcw->lcw_list))) {
472 cfs_list_del_init(&lcw->lcw_list);
473 lcw->lcw_refcount--; /* -1 ref for pending list */
476 lcw->lcw_refcount--; /* -1 ref for owner */
477 dead = lcw->lcw_refcount == 0;
478 cfs_spin_unlock_bh(&lcw_pending_timers_lock);
479 cfs_spin_unlock_bh(&lcw->lcw_lock);
482 LIBCFS_FREE(lcw, sizeof(*lcw));
484 cfs_mutex_lock(&lcw_refcount_mutex);
485 if (--lcw_refcount == 0)
487 cfs_mutex_unlock(&lcw_refcount_mutex);
491 EXPORT_SYMBOL(lc_watchdog_delete);
494 * Provided watchdog handlers
497 void lc_watchdog_dumplog(pid_t pid, void *data)
499 libcfs_debug_dumplog_internal((void *)((long_ptr_t)pid));
501 EXPORT_SYMBOL(lc_watchdog_dumplog);
503 #else /* !defined(WITH_WATCHDOG) */
505 struct lc_watchdog *lc_watchdog_add(int timeout,
506 void (*callback)(pid_t pid, void *),
509 static struct lc_watchdog watchdog;
512 EXPORT_SYMBOL(lc_watchdog_add);
514 void lc_watchdog_touch(struct lc_watchdog *lcw, int timeout)
517 EXPORT_SYMBOL(lc_watchdog_touch);
519 void lc_watchdog_disable(struct lc_watchdog *lcw)
522 EXPORT_SYMBOL(lc_watchdog_disable);
524 void lc_watchdog_delete(struct lc_watchdog *lcw)
527 EXPORT_SYMBOL(lc_watchdog_delete);