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 (c) 2008, 2010, Oracle and/or its affiliates. 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_spinlock_t lcw_lock; /* check or change lcw_list */
48 int lcw_refcount; /* must hold lcw_pending_timers_lock */
49 cfs_timer_t lcw_timer; /* kernel timer */
50 cfs_list_t lcw_list; /* chain on pending list */
51 cfs_time_t lcw_last_touched; /* last touched stamp */
52 cfs_task_t *lcw_task; /* owner task */
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 cfs_completion_t lcw_start_completion;
72 static cfs_completion_t 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 dispatcher.
88 static __u32 lcw_refcount = 0;
89 static CFS_DEFINE_MUTEX(lcw_refcount_mutex);
92 * List of timers that have fired that need their callbacks run by the
96 static cfs_spinlock_t lcw_pending_timers_lock = CFS_SPIN_LOCK_UNLOCKED;
97 static cfs_list_t lcw_pending_timers = \
98 CFS_LIST_HEAD_INIT(lcw_pending_timers);
100 /* Last time a watchdog expired */
101 static cfs_time_t lcw_last_watchdog_time;
102 static int lcw_recent_watchdog_count;
105 lcw_dump(struct lc_watchdog *lcw)
108 #if defined(HAVE_TASKLIST_LOCK)
109 cfs_read_lock(&tasklist_lock);
113 if (lcw->lcw_task == NULL) {
114 LCONSOLE_WARN("Process " LPPID " was not found in the task "
115 "list; watchdog callback may be incomplete\n",
118 libcfs_debug_dumpstack(lcw->lcw_task);
121 #if defined(HAVE_TASKLIST_LOCK)
122 cfs_read_unlock(&tasklist_lock);
129 static void lcw_cb(ulong_ptr_t data)
131 struct lc_watchdog *lcw = (struct lc_watchdog *)data;
134 if (lcw->lcw_state != LC_WATCHDOG_ENABLED) {
139 lcw->lcw_state = LC_WATCHDOG_EXPIRED;
141 cfs_spin_lock_bh(&lcw->lcw_lock);
142 LASSERT(cfs_list_empty(&lcw->lcw_list));
144 cfs_spin_lock_bh(&lcw_pending_timers_lock);
145 lcw->lcw_refcount++; /* +1 for pending list */
146 cfs_list_add(&lcw->lcw_list, &lcw_pending_timers);
147 cfs_waitq_signal(&lcw_event_waitq);
149 cfs_spin_unlock_bh(&lcw_pending_timers_lock);
150 cfs_spin_unlock_bh(&lcw->lcw_lock);
154 static int is_watchdog_fired(void)
158 if (cfs_test_bit(LCW_FLAG_STOP, &lcw_flags))
161 cfs_spin_lock_bh(&lcw_pending_timers_lock);
162 rc = !cfs_list_empty(&lcw_pending_timers);
163 cfs_spin_unlock_bh(&lcw_pending_timers_lock);
167 static void lcw_dump_stack(struct lc_watchdog *lcw)
169 cfs_time_t current_time;
170 cfs_duration_t delta_time;
171 struct timeval timediff;
173 current_time = cfs_time_current();
174 delta_time = cfs_time_sub(current_time, lcw->lcw_last_touched);
175 cfs_duration_usec(delta_time, &timediff);
178 * Check to see if we should throttle the watchdog timer to avoid
179 * too many dumps going to the console thus triggering an NMI.
181 delta_time = cfs_duration_sec(cfs_time_sub(current_time,
182 lcw_last_watchdog_time));
184 if (delta_time < libcfs_watchdog_ratelimit &&
185 lcw_recent_watchdog_count > 3) {
186 LCONSOLE_WARN("Service thread pid %u was inactive for "
187 "%lu.%.02lus. Watchdog stack traces are limited "
188 "to 3 per %d seconds, skipping this one.\n",
191 timediff.tv_usec / 10000,
192 libcfs_watchdog_ratelimit);
194 if (delta_time < libcfs_watchdog_ratelimit) {
195 lcw_recent_watchdog_count++;
197 memcpy(&lcw_last_watchdog_time, ¤t_time,
198 sizeof(current_time));
199 lcw_recent_watchdog_count = 0;
202 LCONSOLE_WARN("Service thread pid %u was inactive for "
203 "%lu.%.02lus. The thread might be hung, or it "
204 "might only be slow and will resume later. "
205 "Dumping the stack trace for debugging purposes:"
209 timediff.tv_usec / 10000);
214 static int lcw_dispatch_main(void *data)
218 struct lc_watchdog *lcw;
219 CFS_LIST_HEAD (zombies);
223 cfs_daemonize("lc_watchdogd");
225 SIGNAL_MASK_LOCK(current, flags);
226 sigfillset(¤t->blocked);
228 SIGNAL_MASK_UNLOCK(current, flags);
230 cfs_complete(&lcw_start_completion);
235 cfs_wait_event_interruptible(lcw_event_waitq,
236 is_watchdog_fired(), rc);
237 CDEBUG(D_INFO, "Watchdog got woken up...\n");
238 if (cfs_test_bit(LCW_FLAG_STOP, &lcw_flags)) {
239 CDEBUG(D_INFO, "LCW_FLAG_STOP was set, shutting down...\n");
241 cfs_spin_lock_bh(&lcw_pending_timers_lock);
242 rc = !cfs_list_empty(&lcw_pending_timers);
243 cfs_spin_unlock_bh(&lcw_pending_timers_lock);
245 CERROR("pending timers list was not empty at "
246 "time of watchdog dispatch shutdown\n");
251 cfs_spin_lock_bh(&lcw_pending_timers_lock);
252 while (!cfs_list_empty(&lcw_pending_timers)) {
255 lcw = cfs_list_entry(lcw_pending_timers.next,
256 struct lc_watchdog, lcw_list);
257 /* +1 ref for callback to make sure lwc wouldn't be
258 * deleted after releasing lcw_pending_timers_lock */
260 cfs_spin_unlock_bh(&lcw_pending_timers_lock);
263 cfs_spin_lock_bh(&lcw->lcw_lock);
264 cfs_spin_lock_bh(&lcw_pending_timers_lock);
266 if (cfs_list_empty(&lcw->lcw_list)) {
267 /* already removed from pending list */
268 lcw->lcw_refcount--; /* -1 ref for callback */
269 if (lcw->lcw_refcount == 0)
270 cfs_list_add(&lcw->lcw_list, &zombies);
271 cfs_spin_unlock_bh(&lcw->lcw_lock);
272 /* still hold lcw_pending_timers_lock */
276 cfs_list_del_init(&lcw->lcw_list);
277 lcw->lcw_refcount--; /* -1 ref for pending list */
279 cfs_spin_unlock_bh(&lcw_pending_timers_lock);
280 cfs_spin_unlock_bh(&lcw->lcw_lock);
282 CDEBUG(D_INFO, "found lcw for pid " LPPID "\n",
286 is_dumplog = lcw->lcw_callback == lc_watchdog_dumplog;
287 if (lcw->lcw_state != LC_WATCHDOG_DISABLED &&
288 (dumplog || !is_dumplog)) {
289 lcw->lcw_callback(lcw->lcw_pid, lcw->lcw_data);
290 if (dumplog && is_dumplog)
294 cfs_spin_lock_bh(&lcw_pending_timers_lock);
295 lcw->lcw_refcount--; /* -1 ref for callback */
296 if (lcw->lcw_refcount == 0)
297 cfs_list_add(&lcw->lcw_list, &zombies);
299 cfs_spin_unlock_bh(&lcw_pending_timers_lock);
301 while (!cfs_list_empty(&zombies)) {
302 lcw = cfs_list_entry(lcw_pending_timers.next,
303 struct lc_watchdog, lcw_list);
304 cfs_list_del(&lcw->lcw_list);
305 LIBCFS_FREE(lcw, sizeof(*lcw));
309 cfs_complete(&lcw_stop_completion);
314 static void lcw_dispatch_start(void)
319 LASSERT(lcw_refcount == 1);
321 cfs_init_completion(&lcw_stop_completion);
322 cfs_init_completion(&lcw_start_completion);
323 cfs_waitq_init(&lcw_event_waitq);
325 CDEBUG(D_INFO, "starting dispatch thread\n");
326 rc = cfs_create_thread(lcw_dispatch_main, NULL, 0);
328 CERROR("error spawning watchdog dispatch thread: %d\n", rc);
332 cfs_wait_for_completion(&lcw_start_completion);
333 CDEBUG(D_INFO, "watchdog dispatcher initialization complete.\n");
338 static void lcw_dispatch_stop(void)
341 LASSERT(lcw_refcount == 0);
343 CDEBUG(D_INFO, "trying to stop watchdog dispatcher.\n");
345 cfs_set_bit(LCW_FLAG_STOP, &lcw_flags);
346 cfs_waitq_signal(&lcw_event_waitq);
348 cfs_wait_for_completion(&lcw_stop_completion);
350 CDEBUG(D_INFO, "watchdog dispatcher has shut down.\n");
355 struct lc_watchdog *lc_watchdog_add(int timeout,
356 void (*callback)(pid_t, void *),
359 struct lc_watchdog *lcw = NULL;
362 LIBCFS_ALLOC(lcw, sizeof(*lcw));
364 CDEBUG(D_INFO, "Could not allocate new lc_watchdog\n");
365 RETURN(ERR_PTR(-ENOMEM));
368 cfs_spin_lock_init(&lcw->lcw_lock);
369 lcw->lcw_refcount = 1; /* refcount for owner */
370 lcw->lcw_task = cfs_current();
371 lcw->lcw_pid = cfs_curproc_pid();
372 lcw->lcw_callback = (callback != NULL) ? callback : lc_watchdog_dumplog;
373 lcw->lcw_data = data;
374 lcw->lcw_state = LC_WATCHDOG_DISABLED;
376 CFS_INIT_LIST_HEAD(&lcw->lcw_list);
377 cfs_timer_init(&lcw->lcw_timer, lcw_cb, lcw);
379 cfs_mutex_lock(&lcw_refcount_mutex);
380 if (++lcw_refcount == 1)
381 lcw_dispatch_start();
382 cfs_mutex_unlock(&lcw_refcount_mutex);
384 /* Keep this working in case we enable them by default */
385 if (lcw->lcw_state == LC_WATCHDOG_ENABLED) {
386 lcw->lcw_last_touched = cfs_time_current();
387 cfs_timer_arm(&lcw->lcw_timer, cfs_time_seconds(timeout) +
393 EXPORT_SYMBOL(lc_watchdog_add);
395 static void lcw_update_time(struct lc_watchdog *lcw, const char *message)
397 cfs_time_t newtime = cfs_time_current();;
399 if (lcw->lcw_state == LC_WATCHDOG_EXPIRED) {
400 struct timeval timediff;
401 cfs_time_t delta_time = cfs_time_sub(newtime,
402 lcw->lcw_last_touched);
403 cfs_duration_usec(delta_time, &timediff);
405 LCONSOLE_WARN("Service thread pid %u %s after %lu.%.02lus. "
406 "This indicates the system was overloaded (too "
407 "many service threads, or there were not enough "
408 "hardware resources).\n",
412 timediff.tv_usec / 10000);
414 lcw->lcw_last_touched = newtime;
417 static void lc_watchdog_del_pending(struct lc_watchdog *lcw)
419 cfs_spin_lock_bh(&lcw->lcw_lock);
420 if (unlikely(!cfs_list_empty(&lcw->lcw_list))) {
421 cfs_spin_lock_bh(&lcw_pending_timers_lock);
422 cfs_list_del_init(&lcw->lcw_list);
423 lcw->lcw_refcount--; /* -1 ref for pending list */
424 cfs_spin_unlock_bh(&lcw_pending_timers_lock);
427 cfs_spin_unlock_bh(&lcw->lcw_lock);
430 void lc_watchdog_touch(struct lc_watchdog *lcw, int timeout)
433 LASSERT(lcw != NULL);
435 lc_watchdog_del_pending(lcw);
437 lcw_update_time(lcw, "resumed");
438 lcw->lcw_state = LC_WATCHDOG_ENABLED;
440 cfs_timer_arm(&lcw->lcw_timer, cfs_time_current() +
441 cfs_time_seconds(timeout));
445 EXPORT_SYMBOL(lc_watchdog_touch);
447 void lc_watchdog_disable(struct lc_watchdog *lcw)
450 LASSERT(lcw != NULL);
452 lc_watchdog_del_pending(lcw);
454 lcw_update_time(lcw, "completed");
455 lcw->lcw_state = LC_WATCHDOG_DISABLED;
459 EXPORT_SYMBOL(lc_watchdog_disable);
461 void lc_watchdog_delete(struct lc_watchdog *lcw)
466 LASSERT(lcw != NULL);
468 cfs_timer_disarm(&lcw->lcw_timer);
470 lcw_update_time(lcw, "stopped");
472 cfs_spin_lock_bh(&lcw->lcw_lock);
473 cfs_spin_lock_bh(&lcw_pending_timers_lock);
474 if (unlikely(!cfs_list_empty(&lcw->lcw_list))) {
475 cfs_list_del_init(&lcw->lcw_list);
476 lcw->lcw_refcount--; /* -1 ref for pending list */
479 lcw->lcw_refcount--; /* -1 ref for owner */
480 dead = lcw->lcw_refcount == 0;
481 cfs_spin_unlock_bh(&lcw_pending_timers_lock);
482 cfs_spin_unlock_bh(&lcw->lcw_lock);
485 LIBCFS_FREE(lcw, sizeof(*lcw));
487 cfs_mutex_lock(&lcw_refcount_mutex);
488 if (--lcw_refcount == 0)
490 cfs_mutex_unlock(&lcw_refcount_mutex);
494 EXPORT_SYMBOL(lc_watchdog_delete);
497 * Provided watchdog handlers
500 void lc_watchdog_dumplog(pid_t pid, void *data)
502 libcfs_debug_dumplog_internal((void *)((long_ptr_t)pid));
504 EXPORT_SYMBOL(lc_watchdog_dumplog);
506 #else /* !defined(WITH_WATCHDOG) */
508 struct lc_watchdog *lc_watchdog_add(int timeout,
509 void (*callback)(pid_t pid, void *),
512 static struct lc_watchdog watchdog;
515 EXPORT_SYMBOL(lc_watchdog_add);
517 void lc_watchdog_touch(struct lc_watchdog *lcw, int timeout)
520 EXPORT_SYMBOL(lc_watchdog_touch);
522 void lc_watchdog_disable(struct lc_watchdog *lcw)
525 EXPORT_SYMBOL(lc_watchdog_disable);
527 void lc_watchdog_delete(struct lc_watchdog *lcw)
530 EXPORT_SYMBOL(lc_watchdog_delete);