Whamcloud - gitweb
Branch HEAD
[fs/lustre-release.git] / libcfs / libcfs / watchdog.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
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.
11  *
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).
17  *
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
21  *
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
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * libcfs/libcfs/watchdog.c
37  *
38  * Author: Jacob Berkman <jacob@clusterfs.com>
39  */
40
41 #define DEBUG_SUBSYSTEM S_LNET
42
43 #include <libcfs/libcfs.h>
44 #include "tracefile.h"
45
46 struct lc_watchdog {
47         cfs_timer_t       lcw_timer; /* kernel timer */
48         struct list_head  lcw_list;
49         cfs_time_t        lcw_last_touched;
50         cfs_task_t       *lcw_task;
51
52         void            (*lcw_callback)(pid_t, void *);
53         void             *lcw_data;
54
55         pid_t             lcw_pid;
56         cfs_duration_t    lcw_time; /* time until watchdog fires, jiffies */
57
58         enum {
59                 LC_WATCHDOG_DISABLED,
60                 LC_WATCHDOG_ENABLED,
61                 LC_WATCHDOG_EXPIRED
62         } lcw_state;
63 };
64
65 #ifdef WITH_WATCHDOG
66 /*
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.
70  */
71 static struct completion lcw_start_completion;
72 static struct completion lcw_stop_completion;
73 static cfs_waitq_t lcw_event_waitq;
74
75 /*
76  * Set this and wake lcw_event_waitq to stop the dispatcher.
77  */
78 enum {
79         LCW_FLAG_STOP = 0
80 };
81 static unsigned long lcw_flags = 0;
82
83 /*
84  * Number of outstanding watchdogs.
85  * When it hits 1, we start the dispatcher.
86  * When it hits 0, we stop the distpatcher.
87  */
88 static __u32         lcw_refcount = 0;
89 static DECLARE_MUTEX(lcw_refcount_sem);
90
91 /*
92  * List of timers that have fired that need their callbacks run by the
93  * dispatcher.
94  */
95 static spinlock_t lcw_pending_timers_lock = SPIN_LOCK_UNLOCKED; /* BH lock! */
96 static struct list_head lcw_pending_timers = \
97         CFS_LIST_HEAD_INIT(lcw_pending_timers);
98
99 /* Last time a watchdog expired */
100 static cfs_time_t lcw_last_watchdog_time;
101 static int lcw_recent_watchdog_count;
102 static spinlock_t lcw_last_watchdog_lock = SPIN_LOCK_UNLOCKED;
103
104 static void
105 lcw_dump(struct lc_watchdog *lcw)
106 {
107         cfs_task_t *tsk;
108 #if defined(HAVE_TASKLIST_LOCK)
109         read_lock(&tasklist_lock);
110 #elif defined(HAVE_TASK_RCU)
111         rcu_read_lock();
112 #else
113         CERROR("unable to dump stack because of missing export\n"); 
114         return;
115 #endif
116         ENTRY;
117
118         tsk = find_task_by_pid(lcw->lcw_pid);
119
120         if (tsk == NULL) {
121                 CWARN("Process " LPPID " was not found in the task list; "
122                       "watchdog callback may be incomplete\n", lcw->lcw_pid);
123         } else if (tsk != lcw->lcw_task) {
124                 CWARN("The current process " LPPID " did not set the watchdog; "
125                       "watchdog callback may be incomplete\n", lcw->lcw_pid);
126         } else {
127                 libcfs_debug_dumpstack(tsk);
128         }
129
130 #if defined(HAVE_TASKLIST_LOCK)
131         read_unlock(&tasklist_lock);
132 #elif defined(HAVE_TASK_RCU)
133         rcu_read_unlock();
134 #endif
135         EXIT;
136 }
137
138 static void lcw_cb(ulong_ptr_t data)
139 {
140         struct lc_watchdog *lcw = (struct lc_watchdog *)data;
141         cfs_time_t current_time;
142         cfs_duration_t delta_time;
143         struct timeval timediff;
144
145         ENTRY;
146
147         if (lcw->lcw_state != LC_WATCHDOG_ENABLED) {
148                 EXIT;
149                 return;
150         }
151
152         lcw->lcw_state = LC_WATCHDOG_EXPIRED;
153         current_time = cfs_time_current();
154
155         delta_time = cfs_time_sub(current_time, lcw->lcw_last_touched);
156         cfs_duration_usec(delta_time, &timediff);
157
158         /* Check to see if we should throttle the watchdog timer to avoid
159          * too many dumps going to the console thus triggering an NMI.
160          * Normally we would not hold the spin lock over the CWARN but in
161          * this case we hold it to ensure non ratelimited lcw_dumps are not
162          * interleaved on the console making them hard to read. */
163         spin_lock_bh(&lcw_last_watchdog_lock);
164         delta_time = cfs_duration_sec(cfs_time_sub(current_time,
165                                                    lcw_last_watchdog_time));
166
167         if (delta_time < libcfs_watchdog_ratelimit &&
168             lcw_recent_watchdog_count > 3) {
169                 CWARN("Refusing to fire watchdog for pid %d: it was inactive "
170                       "for %lu.%.02lus. Rate limiting 1 per %d seconds.\n",
171                       (int)lcw->lcw_pid, timediff.tv_sec,
172                       timediff.tv_usec / 10000, libcfs_watchdog_ratelimit);
173         } else {
174                 if (delta_time < libcfs_watchdog_ratelimit) {
175                         lcw_recent_watchdog_count++;
176                 } else {
177                         memcpy(&lcw_last_watchdog_time, &current_time,
178                                sizeof(current_time));
179                         lcw_recent_watchdog_count = 0;
180                 }
181
182                 /* This warning should appear on the console, but may not get
183                  * into the logs since we're running in a softirq handler */
184                 CWARN("Watchdog triggered for pid %d: it was inactive for "
185                       "%lu.%.02lus\n", (int)lcw->lcw_pid, timediff.tv_sec,
186                       timediff.tv_usec / 10000);
187                 lcw_dump(lcw);
188         }
189
190         spin_unlock_bh(&lcw_last_watchdog_lock);
191         spin_lock_bh(&lcw_pending_timers_lock);
192
193         if (list_empty(&lcw->lcw_list)) {
194                 list_add(&lcw->lcw_list, &lcw_pending_timers);
195                 cfs_waitq_signal(&lcw_event_waitq);
196         }
197
198         spin_unlock_bh(&lcw_pending_timers_lock);
199
200         EXIT;
201 }
202
203 static int is_watchdog_fired(void)
204 {
205         int rc;
206
207         if (test_bit(LCW_FLAG_STOP, &lcw_flags))
208                 return 1;
209
210         spin_lock_bh(&lcw_pending_timers_lock);
211         rc = !list_empty(&lcw_pending_timers);
212         spin_unlock_bh(&lcw_pending_timers_lock);
213         return rc;
214 }
215
216 static int lcw_dispatch_main(void *data)
217 {
218         int                 rc = 0;
219         unsigned long       flags;
220         struct lc_watchdog *lcw;
221
222         ENTRY;
223
224         cfs_daemonize("lc_watchdogd");
225
226         SIGNAL_MASK_LOCK(current, flags);
227         sigfillset(&current->blocked);
228         RECALC_SIGPENDING;
229         SIGNAL_MASK_UNLOCK(current, flags);
230
231         complete(&lcw_start_completion);
232
233         while (1) {
234                 cfs_wait_event_interruptible(lcw_event_waitq, is_watchdog_fired(), rc);
235                 CDEBUG(D_INFO, "Watchdog got woken up...\n");
236                 if (test_bit(LCW_FLAG_STOP, &lcw_flags)) {
237                         CDEBUG(D_INFO, "LCW_FLAG_STOP was set, shutting down...\n");
238
239                         spin_lock_bh(&lcw_pending_timers_lock);
240                         rc = !list_empty(&lcw_pending_timers);
241                         spin_unlock_bh(&lcw_pending_timers_lock);
242                         if (rc) {
243                                 CERROR("pending timers list was not empty at "
244                                        "time of watchdog dispatch shutdown\n");
245                         }
246                         break;
247                 }
248
249                 spin_lock_bh(&lcw_pending_timers_lock);
250                 while (!list_empty(&lcw_pending_timers)) {
251
252                         lcw = list_entry(lcw_pending_timers.next,
253                                          struct lc_watchdog,
254                                          lcw_list);
255                         list_del_init(&lcw->lcw_list);
256                         spin_unlock_bh(&lcw_pending_timers_lock);
257
258                         CDEBUG(D_INFO, "found lcw for pid " LPPID ": inactive for "
259                                "%lds\n", lcw->lcw_pid, cfs_duration_sec(lcw->lcw_time));
260
261                         if (lcw->lcw_state != LC_WATCHDOG_DISABLED)
262                                 lcw->lcw_callback(lcw->lcw_pid, lcw->lcw_data);
263
264                         spin_lock_bh(&lcw_pending_timers_lock);
265                 }
266                 spin_unlock_bh(&lcw_pending_timers_lock);
267         }
268
269         complete(&lcw_stop_completion);
270
271         RETURN(rc);
272 }
273
274 static void lcw_dispatch_start(void)
275 {
276         int rc;
277
278         ENTRY;
279         LASSERT(lcw_refcount == 1);
280
281         init_completion(&lcw_stop_completion);
282         init_completion(&lcw_start_completion);
283         cfs_waitq_init(&lcw_event_waitq);
284
285         CDEBUG(D_INFO, "starting dispatch thread\n");
286         rc = kernel_thread(lcw_dispatch_main, NULL, 0);
287         if (rc < 0) {
288                 CERROR("error spawning watchdog dispatch thread: %d\n", rc);
289                 EXIT;
290                 return;
291         }
292         wait_for_completion(&lcw_start_completion);
293         CDEBUG(D_INFO, "watchdog dispatcher initialization complete.\n");
294
295         EXIT;
296 }
297
298 static void lcw_dispatch_stop(void)
299 {
300         ENTRY;
301         LASSERT(lcw_refcount == 0);
302
303         CDEBUG(D_INFO, "trying to stop watchdog dispatcher.\n");
304
305         set_bit(LCW_FLAG_STOP, &lcw_flags);
306         cfs_waitq_signal(&lcw_event_waitq);
307
308         wait_for_completion(&lcw_stop_completion);
309
310         CDEBUG(D_INFO, "watchdog dispatcher has shut down.\n");
311
312         EXIT;
313 }
314
315 struct lc_watchdog *lc_watchdog_add(int timeout_ms,
316                                     void (*callback)(pid_t, void *),
317                                     void *data)
318 {
319         struct lc_watchdog *lcw = NULL;
320         ENTRY;
321
322         LIBCFS_ALLOC(lcw, sizeof(*lcw));
323         if (lcw == NULL) {
324                 CDEBUG(D_INFO, "Could not allocate new lc_watchdog\n");
325                 RETURN(ERR_PTR(-ENOMEM));
326         }
327
328         lcw->lcw_task     = cfs_current();
329         lcw->lcw_pid      = cfs_curproc_pid();
330         lcw->lcw_time     = cfs_time_seconds(timeout_ms) / 1000;
331         lcw->lcw_callback = (callback != NULL) ? callback : lc_watchdog_dumplog;
332         lcw->lcw_data     = data;
333         lcw->lcw_state    = LC_WATCHDOG_DISABLED;
334
335         CFS_INIT_LIST_HEAD(&lcw->lcw_list);
336         cfs_timer_init(&lcw->lcw_timer, lcw_cb, lcw);
337
338         down(&lcw_refcount_sem);
339         if (++lcw_refcount == 1)
340                 lcw_dispatch_start();
341         up(&lcw_refcount_sem);
342
343         /* Keep this working in case we enable them by default */
344         if (lcw->lcw_state == LC_WATCHDOG_ENABLED) {
345                 lcw->lcw_last_touched = cfs_time_current();
346                 cfs_timer_arm(&lcw->lcw_timer, lcw->lcw_time + 
347                               cfs_time_current());
348         }
349
350         RETURN(lcw);
351 }
352 EXPORT_SYMBOL(lc_watchdog_add);
353
354 static void lcw_update_time(struct lc_watchdog *lcw, const char *message)
355 {
356         cfs_time_t newtime = cfs_time_current();;
357
358         if (lcw->lcw_state == LC_WATCHDOG_EXPIRED) {
359                 struct timeval timediff;
360                 cfs_time_t delta_time = cfs_time_sub(newtime,
361                                                      lcw->lcw_last_touched);
362                 cfs_duration_usec(delta_time, &timediff);
363
364                 CWARN("Expired watchdog for pid " LPPID " %s after %lu.%.02lus\n",
365                       lcw->lcw_pid, message, timediff.tv_sec,
366                       timediff.tv_usec / 10000);
367         }
368         lcw->lcw_last_touched = newtime;
369 }
370
371 void lc_watchdog_touch_ms(struct lc_watchdog *lcw, int timeout_ms)
372 {
373         ENTRY;
374         LASSERT(lcw != NULL);
375
376         spin_lock_bh(&lcw_pending_timers_lock);
377         list_del_init(&lcw->lcw_list);
378         spin_unlock_bh(&lcw_pending_timers_lock);
379
380         lcw_update_time(lcw, "touched");
381         lcw->lcw_state = LC_WATCHDOG_ENABLED;
382
383         cfs_timer_arm(&lcw->lcw_timer, cfs_time_current() +
384                       cfs_time_seconds(timeout_ms) / 1000);
385
386         EXIT;
387 }
388 EXPORT_SYMBOL(lc_watchdog_touch_ms);
389
390 /* deprecated - use above instead */
391 void lc_watchdog_touch(struct lc_watchdog *lcw)
392 {
393         lc_watchdog_touch_ms(lcw, (int)cfs_duration_sec(lcw->lcw_time) * 1000);
394 }
395 EXPORT_SYMBOL(lc_watchdog_touch);
396
397 void lc_watchdog_disable(struct lc_watchdog *lcw)
398 {
399         ENTRY;
400         LASSERT(lcw != NULL);
401
402         spin_lock_bh(&lcw_pending_timers_lock);
403         if (!list_empty(&lcw->lcw_list))
404                 list_del_init(&lcw->lcw_list);
405         spin_unlock_bh(&lcw_pending_timers_lock);
406
407         lcw_update_time(lcw, "disabled");
408         lcw->lcw_state = LC_WATCHDOG_DISABLED;
409
410         EXIT;
411 }
412 EXPORT_SYMBOL(lc_watchdog_disable);
413
414 void lc_watchdog_delete(struct lc_watchdog *lcw)
415 {
416         ENTRY;
417         LASSERT(lcw != NULL);
418
419         cfs_timer_disarm(&lcw->lcw_timer);
420
421         lcw_update_time(lcw, "deleted");
422
423         spin_lock_bh(&lcw_pending_timers_lock);
424         if (!list_empty(&lcw->lcw_list))
425                 list_del_init(&lcw->lcw_list);
426         spin_unlock_bh(&lcw_pending_timers_lock);
427
428         down(&lcw_refcount_sem);
429         if (--lcw_refcount == 0)
430                 lcw_dispatch_stop();
431         up(&lcw_refcount_sem);
432
433         LIBCFS_FREE(lcw, sizeof(*lcw));
434
435         EXIT;
436 }
437 EXPORT_SYMBOL(lc_watchdog_delete);
438
439 /*
440  * Provided watchdog handlers
441  */
442
443 void lc_watchdog_dumplog(pid_t pid, void *data)
444 {
445         libcfs_debug_dumplog_internal((void *)((long_ptr_t)pid));
446 }
447 EXPORT_SYMBOL(lc_watchdog_dumplog);
448
449 #else   /* !defined(WITH_WATCHDOG) */
450
451 struct lc_watchdog *lc_watchdog_add(int timeout_ms,
452                                     void (*callback)(pid_t pid, void *),
453                                     void *data)
454 {
455         static struct lc_watchdog      watchdog;
456         return &watchdog;
457 }
458 EXPORT_SYMBOL(lc_watchdog_add);
459
460 void lc_watchdog_touch_ms(struct lc_watchdog *lcw, int timeout_ms)
461 {
462 }
463 EXPORT_SYMBOL(lc_watchdog_touch_ms);
464
465 void lc_watchdog_touch(struct lc_watchdog *lcw)
466 {
467 }
468 EXPORT_SYMBOL(lc_watchdog_touch);
469
470 void lc_watchdog_disable(struct lc_watchdog *lcw)
471 {
472 }
473 EXPORT_SYMBOL(lc_watchdog_disable);
474
475 void lc_watchdog_delete(struct lc_watchdog *lcw)
476 {
477 }
478 EXPORT_SYMBOL(lc_watchdog_delete);
479
480 #endif