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 cfs_spinlock_t lcw_pending_timers_lock = CFS_SPIN_LOCK_UNLOCKED;
95 static cfs_list_t lcw_pending_timers = \
96 CFS_LIST_HEAD_INIT(lcw_pending_timers);
98 /* Last time a watchdog expired */
99 static cfs_time_t lcw_last_watchdog_time;
100 static int lcw_recent_watchdog_count;
103 lcw_dump(struct lc_watchdog *lcw)
106 #if defined(HAVE_TASKLIST_LOCK)
107 cfs_read_lock(&tasklist_lock);
111 if (lcw->lcw_task == NULL) {
112 LCONSOLE_WARN("Process " LPPID " was not found in the task "
113 "list; watchdog callback may be incomplete\n",
116 libcfs_debug_dumpstack(lcw->lcw_task);
119 #if defined(HAVE_TASKLIST_LOCK)
120 cfs_read_unlock(&tasklist_lock);
127 static void lcw_cb(ulong_ptr_t data)
129 struct lc_watchdog *lcw = (struct lc_watchdog *)data;
132 if (lcw->lcw_state != LC_WATCHDOG_ENABLED) {
137 lcw->lcw_state = LC_WATCHDOG_EXPIRED;
139 cfs_spin_lock_bh(&lcw->lcw_lock);
140 LASSERT(cfs_list_empty(&lcw->lcw_list));
142 cfs_spin_lock_bh(&lcw_pending_timers_lock);
143 lcw->lcw_refcount++; /* +1 for pending list */
144 cfs_list_add(&lcw->lcw_list, &lcw_pending_timers);
145 cfs_waitq_signal(&lcw_event_waitq);
147 cfs_spin_unlock_bh(&lcw_pending_timers_lock);
148 cfs_spin_unlock_bh(&lcw->lcw_lock);
152 static int is_watchdog_fired(void)
156 if (cfs_test_bit(LCW_FLAG_STOP, &lcw_flags))
159 cfs_spin_lock_bh(&lcw_pending_timers_lock);
160 rc = !cfs_list_empty(&lcw_pending_timers);
161 cfs_spin_unlock_bh(&lcw_pending_timers_lock);
165 static void lcw_dump_stack(struct lc_watchdog *lcw)
167 cfs_time_t current_time;
168 cfs_duration_t delta_time;
169 struct timeval timediff;
171 current_time = cfs_time_current();
172 delta_time = cfs_time_sub(current_time, lcw->lcw_last_touched);
173 cfs_duration_usec(delta_time, &timediff);
176 * Check to see if we should throttle the watchdog timer to avoid
177 * too many dumps going to the console thus triggering an NMI.
179 delta_time = cfs_duration_sec(cfs_time_sub(current_time,
180 lcw_last_watchdog_time));
182 if (delta_time < libcfs_watchdog_ratelimit &&
183 lcw_recent_watchdog_count > 3) {
184 LCONSOLE_WARN("Service thread pid %u was inactive for "
185 "%lu.%.02lus. Watchdog stack traces are limited "
186 "to 3 per %d seconds, skipping this one.\n",
189 timediff.tv_usec / 10000,
190 libcfs_watchdog_ratelimit);
192 if (delta_time < libcfs_watchdog_ratelimit) {
193 lcw_recent_watchdog_count++;
195 memcpy(&lcw_last_watchdog_time, ¤t_time,
196 sizeof(current_time));
197 lcw_recent_watchdog_count = 0;
200 LCONSOLE_WARN("Service thread pid %u was inactive for "
201 "%lu.%.02lus. The thread might be hung, or it "
202 "might only be slow and will resume later. "
203 "Dumping the stack trace for debugging purposes:"
207 timediff.tv_usec / 10000);
212 static int lcw_dispatch_main(void *data)
216 struct lc_watchdog *lcw;
217 CFS_LIST_HEAD (zombies);
221 cfs_daemonize("lc_watchdogd");
223 SIGNAL_MASK_LOCK(current, flags);
224 sigfillset(¤t->blocked);
226 SIGNAL_MASK_UNLOCK(current, flags);
228 cfs_complete(&lcw_start_completion);
233 cfs_wait_event_interruptible(lcw_event_waitq,
234 is_watchdog_fired(), rc);
235 CDEBUG(D_INFO, "Watchdog got woken up...\n");
236 if (cfs_test_bit(LCW_FLAG_STOP, &lcw_flags)) {
237 CDEBUG(D_INFO, "LCW_FLAG_STOP was set, shutting down...\n");
239 cfs_spin_lock_bh(&lcw_pending_timers_lock);
240 rc = !cfs_list_empty(&lcw_pending_timers);
241 cfs_spin_unlock_bh(&lcw_pending_timers_lock);
243 CERROR("pending timers list was not empty at "
244 "time of watchdog dispatch shutdown\n");
249 cfs_spin_lock_bh(&lcw_pending_timers_lock);
250 while (!cfs_list_empty(&lcw_pending_timers)) {
253 lcw = cfs_list_entry(lcw_pending_timers.next,
254 struct lc_watchdog, lcw_list);
255 /* +1 ref for callback to make sure lwc wouldn't be
256 * deleted after releasing lcw_pending_timers_lock */
258 cfs_spin_unlock_bh(&lcw_pending_timers_lock);
261 cfs_spin_lock_bh(&lcw->lcw_lock);
262 cfs_spin_lock_bh(&lcw_pending_timers_lock);
264 if (cfs_list_empty(&lcw->lcw_list)) {
265 /* already removed from pending list */
266 lcw->lcw_refcount--; /* -1 ref for callback */
267 if (lcw->lcw_refcount == 0)
268 cfs_list_add(&lcw->lcw_list, &zombies);
269 cfs_spin_unlock_bh(&lcw->lcw_lock);
270 /* still hold lcw_pending_timers_lock */
274 cfs_list_del_init(&lcw->lcw_list);
275 lcw->lcw_refcount--; /* -1 ref for pending list */
277 cfs_spin_unlock_bh(&lcw_pending_timers_lock);
278 cfs_spin_unlock_bh(&lcw->lcw_lock);
280 CDEBUG(D_INFO, "found lcw for pid " LPPID "\n",
284 is_dumplog = lcw->lcw_callback == lc_watchdog_dumplog;
285 if (lcw->lcw_state != LC_WATCHDOG_DISABLED &&
286 (dumplog || !is_dumplog)) {
287 lcw->lcw_callback(lcw->lcw_pid, lcw->lcw_data);
288 if (dumplog && is_dumplog)
292 cfs_spin_lock_bh(&lcw_pending_timers_lock);
293 lcw->lcw_refcount--; /* -1 ref for callback */
294 if (lcw->lcw_refcount == 0)
295 cfs_list_add(&lcw->lcw_list, &zombies);
297 cfs_spin_unlock_bh(&lcw_pending_timers_lock);
299 while (!cfs_list_empty(&zombies)) {
300 lcw = cfs_list_entry(lcw_pending_timers.next,
301 struct lc_watchdog, lcw_list);
302 cfs_list_del(&lcw->lcw_list);
303 LIBCFS_FREE(lcw, sizeof(*lcw));
307 cfs_complete(&lcw_stop_completion);
312 static void lcw_dispatch_start(void)
317 LASSERT(lcw_refcount == 1);
319 cfs_init_completion(&lcw_stop_completion);
320 cfs_init_completion(&lcw_start_completion);
321 cfs_waitq_init(&lcw_event_waitq);
323 CDEBUG(D_INFO, "starting dispatch thread\n");
324 rc = cfs_create_thread(lcw_dispatch_main, NULL, 0);
326 CERROR("error spawning watchdog dispatch thread: %d\n", rc);
330 cfs_wait_for_completion(&lcw_start_completion);
331 CDEBUG(D_INFO, "watchdog dispatcher initialization complete.\n");
336 static void lcw_dispatch_stop(void)
339 LASSERT(lcw_refcount == 0);
341 CDEBUG(D_INFO, "trying to stop watchdog dispatcher.\n");
343 cfs_set_bit(LCW_FLAG_STOP, &lcw_flags);
344 cfs_waitq_signal(&lcw_event_waitq);
346 cfs_wait_for_completion(&lcw_stop_completion);
348 CDEBUG(D_INFO, "watchdog dispatcher has shut down.\n");
353 struct lc_watchdog *lc_watchdog_add(int timeout,
354 void (*callback)(pid_t, void *),
357 struct lc_watchdog *lcw = NULL;
360 LIBCFS_ALLOC(lcw, sizeof(*lcw));
362 CDEBUG(D_INFO, "Could not allocate new lc_watchdog\n");
363 RETURN(ERR_PTR(-ENOMEM));
366 cfs_spin_lock_init(&lcw->lcw_lock);
367 lcw->lcw_refcount = 1; /* refcount for owner */
368 lcw->lcw_task = cfs_current();
369 lcw->lcw_pid = cfs_curproc_pid();
370 lcw->lcw_callback = (callback != NULL) ? callback : lc_watchdog_dumplog;
371 lcw->lcw_data = data;
372 lcw->lcw_state = LC_WATCHDOG_DISABLED;
374 CFS_INIT_LIST_HEAD(&lcw->lcw_list);
375 cfs_timer_init(&lcw->lcw_timer, lcw_cb, lcw);
377 cfs_mutex_lock(&lcw_refcount_mutex);
378 if (++lcw_refcount == 1)
379 lcw_dispatch_start();
380 cfs_mutex_unlock(&lcw_refcount_mutex);
382 /* Keep this working in case we enable them by default */
383 if (lcw->lcw_state == LC_WATCHDOG_ENABLED) {
384 lcw->lcw_last_touched = cfs_time_current();
385 cfs_timer_arm(&lcw->lcw_timer, cfs_time_seconds(timeout) +
391 EXPORT_SYMBOL(lc_watchdog_add);
393 static void lcw_update_time(struct lc_watchdog *lcw, const char *message)
395 cfs_time_t newtime = cfs_time_current();;
397 if (lcw->lcw_state == LC_WATCHDOG_EXPIRED) {
398 struct timeval timediff;
399 cfs_time_t delta_time = cfs_time_sub(newtime,
400 lcw->lcw_last_touched);
401 cfs_duration_usec(delta_time, &timediff);
403 LCONSOLE_WARN("Service thread pid %u %s after %lu.%.02lus. "
404 "This indicates the system was overloaded (too "
405 "many service threads, or there were not enough "
406 "hardware resources).\n",
410 timediff.tv_usec / 10000);
412 lcw->lcw_last_touched = newtime;
415 static void lc_watchdog_del_pending(struct lc_watchdog *lcw)
417 cfs_spin_lock_bh(&lcw->lcw_lock);
418 if (unlikely(!cfs_list_empty(&lcw->lcw_list))) {
419 cfs_spin_lock_bh(&lcw_pending_timers_lock);
420 cfs_list_del_init(&lcw->lcw_list);
421 lcw->lcw_refcount--; /* -1 ref for pending list */
422 cfs_spin_unlock_bh(&lcw_pending_timers_lock);
425 cfs_spin_unlock_bh(&lcw->lcw_lock);
428 void lc_watchdog_touch(struct lc_watchdog *lcw, int timeout)
431 LASSERT(lcw != NULL);
433 lc_watchdog_del_pending(lcw);
435 lcw_update_time(lcw, "resumed");
436 lcw->lcw_state = LC_WATCHDOG_ENABLED;
438 cfs_timer_arm(&lcw->lcw_timer, cfs_time_current() +
439 cfs_time_seconds(timeout));
443 EXPORT_SYMBOL(lc_watchdog_touch);
445 void lc_watchdog_disable(struct lc_watchdog *lcw)
448 LASSERT(lcw != NULL);
450 lc_watchdog_del_pending(lcw);
452 lcw_update_time(lcw, "completed");
453 lcw->lcw_state = LC_WATCHDOG_DISABLED;
457 EXPORT_SYMBOL(lc_watchdog_disable);
459 void lc_watchdog_delete(struct lc_watchdog *lcw)
464 LASSERT(lcw != NULL);
466 cfs_timer_disarm(&lcw->lcw_timer);
468 lcw_update_time(lcw, "stopped");
470 cfs_spin_lock_bh(&lcw->lcw_lock);
471 cfs_spin_lock_bh(&lcw_pending_timers_lock);
472 if (unlikely(!cfs_list_empty(&lcw->lcw_list))) {
473 cfs_list_del_init(&lcw->lcw_list);
474 lcw->lcw_refcount--; /* -1 ref for pending list */
477 lcw->lcw_refcount--; /* -1 ref for owner */
478 dead = lcw->lcw_refcount == 0;
479 cfs_spin_unlock_bh(&lcw_pending_timers_lock);
480 cfs_spin_unlock_bh(&lcw->lcw_lock);
483 LIBCFS_FREE(lcw, sizeof(*lcw));
485 cfs_mutex_lock(&lcw_refcount_mutex);
486 if (--lcw_refcount == 0)
488 cfs_mutex_unlock(&lcw_refcount_mutex);
492 EXPORT_SYMBOL(lc_watchdog_delete);
495 * Provided watchdog handlers
498 void lc_watchdog_dumplog(pid_t pid, void *data)
500 libcfs_debug_dumplog_internal((void *)((long_ptr_t)pid));
502 EXPORT_SYMBOL(lc_watchdog_dumplog);
504 #else /* !defined(WITH_WATCHDOG) */
506 struct lc_watchdog *lc_watchdog_add(int timeout,
507 void (*callback)(pid_t pid, void *),
510 static struct lc_watchdog watchdog;
513 EXPORT_SYMBOL(lc_watchdog_add);
515 void lc_watchdog_touch(struct lc_watchdog *lcw, int timeout)
518 EXPORT_SYMBOL(lc_watchdog_touch);
520 void lc_watchdog_disable(struct lc_watchdog *lcw)
523 EXPORT_SYMBOL(lc_watchdog_disable);
525 void lc_watchdog_delete(struct lc_watchdog *lcw)
528 EXPORT_SYMBOL(lc_watchdog_delete);