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_DECLARE_MUTEX(lcw_refcount_sem);
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);
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;
137 if (lcw->lcw_state != LC_WATCHDOG_ENABLED) {
142 lcw->lcw_state = LC_WATCHDOG_EXPIRED;
144 cfs_spin_lock_bh(&lcw->lcw_lock);
145 LASSERT(cfs_list_empty(&lcw->lcw_list));
147 cfs_spin_lock_bh(&lcw_pending_timers_lock);
148 lcw->lcw_refcount++; /* +1 for pending list */
149 cfs_list_add(&lcw->lcw_list, &lcw_pending_timers);
150 cfs_waitq_signal(&lcw_event_waitq);
152 cfs_spin_unlock_bh(&lcw_pending_timers_lock);
153 cfs_spin_unlock_bh(&lcw->lcw_lock);
157 static int is_watchdog_fired(void)
161 if (cfs_test_bit(LCW_FLAG_STOP, &lcw_flags))
164 cfs_spin_lock_bh(&lcw_pending_timers_lock);
165 rc = !cfs_list_empty(&lcw_pending_timers);
166 cfs_spin_unlock_bh(&lcw_pending_timers_lock);
170 static void lcw_dump_stack(struct lc_watchdog *lcw)
172 cfs_time_t current_time;
173 cfs_duration_t delta_time;
174 struct timeval timediff;
176 current_time = cfs_time_current();
177 delta_time = cfs_time_sub(current_time, lcw->lcw_last_touched);
178 cfs_duration_usec(delta_time, &timediff);
181 * Check to see if we should throttle the watchdog timer to avoid
182 * too many dumps going to the console thus triggering an NMI.
184 delta_time = cfs_duration_sec(cfs_time_sub(current_time,
185 lcw_last_watchdog_time));
187 if (delta_time < libcfs_watchdog_ratelimit &&
188 lcw_recent_watchdog_count > 3) {
189 LCONSOLE_WARN("Service thread pid %u was inactive for "
190 "%lu.%.02lus. Watchdog stack traces are limited "
191 "to 3 per %d seconds, skipping this one.\n",
194 timediff.tv_usec / 10000,
195 libcfs_watchdog_ratelimit);
197 if (delta_time < libcfs_watchdog_ratelimit) {
198 lcw_recent_watchdog_count++;
200 memcpy(&lcw_last_watchdog_time, ¤t_time,
201 sizeof(current_time));
202 lcw_recent_watchdog_count = 0;
205 LCONSOLE_WARN("Service thread pid %u was inactive for "
206 "%lu.%.02lus. The thread might be hung, or it "
207 "might only be slow and will resume later. "
208 "Dumping the stack trace for debugging purposes:"
212 timediff.tv_usec / 10000);
217 static int lcw_dispatch_main(void *data)
221 struct lc_watchdog *lcw;
222 CFS_LIST_HEAD (zombies);
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);
238 cfs_wait_event_interruptible(lcw_event_waitq,
239 is_watchdog_fired(), rc);
240 CDEBUG(D_INFO, "Watchdog got woken up...\n");
241 if (cfs_test_bit(LCW_FLAG_STOP, &lcw_flags)) {
242 CDEBUG(D_INFO, "LCW_FLAG_STOP was set, shutting down...\n");
244 cfs_spin_lock_bh(&lcw_pending_timers_lock);
245 rc = !cfs_list_empty(&lcw_pending_timers);
246 cfs_spin_unlock_bh(&lcw_pending_timers_lock);
248 CERROR("pending timers list was not empty at "
249 "time of watchdog dispatch shutdown\n");
254 cfs_spin_lock_bh(&lcw_pending_timers_lock);
255 while (!cfs_list_empty(&lcw_pending_timers)) {
258 lcw = cfs_list_entry(lcw_pending_timers.next,
259 struct lc_watchdog, lcw_list);
260 /* +1 ref for callback to make sure lwc wouldn't be
261 * deleted after releasing lcw_pending_timers_lock */
263 cfs_spin_unlock_bh(&lcw_pending_timers_lock);
266 cfs_spin_lock_bh(&lcw->lcw_lock);
267 cfs_spin_lock_bh(&lcw_pending_timers_lock);
269 if (cfs_list_empty(&lcw->lcw_list)) {
270 /* already removed from pending list */
271 lcw->lcw_refcount--; /* -1 ref for callback */
272 if (lcw->lcw_refcount == 0)
273 cfs_list_add(&lcw->lcw_list, &zombies);
274 cfs_spin_unlock_bh(&lcw->lcw_lock);
275 /* still hold lcw_pending_timers_lock */
279 cfs_list_del_init(&lcw->lcw_list);
280 lcw->lcw_refcount--; /* -1 ref for pending list */
282 cfs_spin_unlock_bh(&lcw_pending_timers_lock);
283 cfs_spin_unlock_bh(&lcw->lcw_lock);
285 CDEBUG(D_INFO, "found lcw for pid " LPPID "\n",
289 is_dumplog = lcw->lcw_callback == lc_watchdog_dumplog;
290 if (lcw->lcw_state != LC_WATCHDOG_DISABLED &&
291 (dumplog || !is_dumplog)) {
292 lcw->lcw_callback(lcw->lcw_pid, lcw->lcw_data);
293 if (dumplog && is_dumplog)
297 cfs_spin_lock_bh(&lcw_pending_timers_lock);
298 lcw->lcw_refcount--; /* -1 ref for callback */
299 if (lcw->lcw_refcount == 0)
300 cfs_list_add(&lcw->lcw_list, &zombies);
302 cfs_spin_unlock_bh(&lcw_pending_timers_lock);
304 while (!cfs_list_empty(&zombies)) {
305 lcw = cfs_list_entry(lcw_pending_timers.next,
306 struct lc_watchdog, lcw_list);
307 cfs_list_del(&lcw->lcw_list);
308 LIBCFS_FREE(lcw, sizeof(*lcw));
312 cfs_complete(&lcw_stop_completion);
317 static void lcw_dispatch_start(void)
322 LASSERT(lcw_refcount == 1);
324 cfs_init_completion(&lcw_stop_completion);
325 cfs_init_completion(&lcw_start_completion);
326 cfs_waitq_init(&lcw_event_waitq);
328 CDEBUG(D_INFO, "starting dispatch thread\n");
329 rc = cfs_kernel_thread(lcw_dispatch_main, NULL, 0);
331 CERROR("error spawning watchdog dispatch thread: %d\n", rc);
335 cfs_wait_for_completion(&lcw_start_completion);
336 CDEBUG(D_INFO, "watchdog dispatcher initialization complete.\n");
341 static void lcw_dispatch_stop(void)
344 LASSERT(lcw_refcount == 0);
346 CDEBUG(D_INFO, "trying to stop watchdog dispatcher.\n");
348 cfs_set_bit(LCW_FLAG_STOP, &lcw_flags);
349 cfs_waitq_signal(&lcw_event_waitq);
351 cfs_wait_for_completion(&lcw_stop_completion);
353 CDEBUG(D_INFO, "watchdog dispatcher has shut down.\n");
358 struct lc_watchdog *lc_watchdog_add(int timeout,
359 void (*callback)(pid_t, void *),
362 struct lc_watchdog *lcw = NULL;
365 LIBCFS_ALLOC(lcw, sizeof(*lcw));
367 CDEBUG(D_INFO, "Could not allocate new lc_watchdog\n");
368 RETURN(ERR_PTR(-ENOMEM));
371 cfs_spin_lock_init(&lcw->lcw_lock);
372 lcw->lcw_refcount = 1; /* refcount for owner */
373 lcw->lcw_task = cfs_current();
374 lcw->lcw_pid = cfs_curproc_pid();
375 lcw->lcw_callback = (callback != NULL) ? callback : lc_watchdog_dumplog;
376 lcw->lcw_data = data;
377 lcw->lcw_state = LC_WATCHDOG_DISABLED;
379 CFS_INIT_LIST_HEAD(&lcw->lcw_list);
380 cfs_timer_init(&lcw->lcw_timer, lcw_cb, lcw);
382 cfs_down(&lcw_refcount_sem);
383 if (++lcw_refcount == 1)
384 lcw_dispatch_start();
385 cfs_up(&lcw_refcount_sem);
387 /* Keep this working in case we enable them by default */
388 if (lcw->lcw_state == LC_WATCHDOG_ENABLED) {
389 lcw->lcw_last_touched = cfs_time_current();
390 cfs_timer_arm(&lcw->lcw_timer, cfs_time_seconds(timeout) +
396 EXPORT_SYMBOL(lc_watchdog_add);
398 static void lcw_update_time(struct lc_watchdog *lcw, const char *message)
400 cfs_time_t newtime = cfs_time_current();;
402 if (lcw->lcw_state == LC_WATCHDOG_EXPIRED) {
403 struct timeval timediff;
404 cfs_time_t delta_time = cfs_time_sub(newtime,
405 lcw->lcw_last_touched);
406 cfs_duration_usec(delta_time, &timediff);
408 LCONSOLE_WARN("Service thread pid %u %s after %lu.%.02lus. "
409 "This indicates the system was overloaded (too "
410 "many service threads, or there were not enough "
411 "hardware resources).\n",
415 timediff.tv_usec / 10000);
417 lcw->lcw_last_touched = newtime;
420 static void lc_watchdog_del_pending(struct lc_watchdog *lcw)
422 cfs_spin_lock_bh(&lcw->lcw_lock);
423 if (unlikely(!cfs_list_empty(&lcw->lcw_list))) {
424 cfs_spin_lock_bh(&lcw_pending_timers_lock);
425 cfs_list_del_init(&lcw->lcw_list);
426 lcw->lcw_refcount--; /* -1 ref for pending list */
427 cfs_spin_unlock_bh(&lcw_pending_timers_lock);
430 cfs_spin_unlock_bh(&lcw->lcw_lock);
433 void lc_watchdog_touch(struct lc_watchdog *lcw, int timeout)
436 LASSERT(lcw != NULL);
438 lc_watchdog_del_pending(lcw);
440 lcw_update_time(lcw, "resumed");
441 lcw->lcw_state = LC_WATCHDOG_ENABLED;
443 cfs_timer_arm(&lcw->lcw_timer, cfs_time_current() +
444 cfs_time_seconds(timeout));
448 EXPORT_SYMBOL(lc_watchdog_touch);
450 void lc_watchdog_disable(struct lc_watchdog *lcw)
453 LASSERT(lcw != NULL);
455 lc_watchdog_del_pending(lcw);
457 lcw_update_time(lcw, "completed");
458 lcw->lcw_state = LC_WATCHDOG_DISABLED;
462 EXPORT_SYMBOL(lc_watchdog_disable);
464 void lc_watchdog_delete(struct lc_watchdog *lcw)
469 LASSERT(lcw != NULL);
471 cfs_timer_disarm(&lcw->lcw_timer);
473 lcw_update_time(lcw, "stopped");
475 cfs_spin_lock_bh(&lcw->lcw_lock);
476 cfs_spin_lock_bh(&lcw_pending_timers_lock);
477 if (unlikely(!cfs_list_empty(&lcw->lcw_list))) {
478 cfs_list_del_init(&lcw->lcw_list);
479 lcw->lcw_refcount--; /* -1 ref for pending list */
482 lcw->lcw_refcount--; /* -1 ref for owner */
483 dead = lcw->lcw_refcount == 0;
484 cfs_spin_unlock_bh(&lcw_pending_timers_lock);
485 cfs_spin_unlock_bh(&lcw->lcw_lock);
488 LIBCFS_FREE(lcw, sizeof(*lcw));
490 cfs_down(&lcw_refcount_sem);
491 if (--lcw_refcount == 0)
493 cfs_up(&lcw_refcount_sem);
497 EXPORT_SYMBOL(lc_watchdog_delete);
500 * Provided watchdog handlers
503 void lc_watchdog_dumplog(pid_t pid, void *data)
505 libcfs_debug_dumplog_internal((void *)((long_ptr_t)pid));
507 EXPORT_SYMBOL(lc_watchdog_dumplog);
509 #else /* !defined(WITH_WATCHDOG) */
511 struct lc_watchdog *lc_watchdog_add(int timeout,
512 void (*callback)(pid_t pid, void *),
515 static struct lc_watchdog watchdog;
518 EXPORT_SYMBOL(lc_watchdog_add);
520 void lc_watchdog_touch(struct lc_watchdog *lcw, int timeout)
523 EXPORT_SYMBOL(lc_watchdog_touch);
525 void lc_watchdog_disable(struct lc_watchdog *lcw)
528 EXPORT_SYMBOL(lc_watchdog_disable);
530 void lc_watchdog_delete(struct lc_watchdog *lcw)
533 EXPORT_SYMBOL(lc_watchdog_delete);