Whamcloud - gitweb
b=10762
[fs/lustre-release.git] / lnet / libcfs / watchdog.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Copyright (C) 2004 Cluster File Systems, Inc.
5  *   Author: Jacob Berkman <jacob@clusterfs.com>
6  *
7  *   This file is part of Lustre, http://www.lustre.org.
8  *
9  *   Lustre is free software; you can redistribute it and/or
10  *   modify it under the terms of version 2 of the GNU General Public
11  *   License as published by the Free Software Foundation.
12  *
13  *   Lustre is distributed in the hope that it will be useful,
14  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *   GNU General Public License for more details.
17  *
18  *   You should have received a copy of the GNU General Public License
19  *   along with Lustre; if not, write to the Free Software
20  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */
22
23 #define DEBUG_SUBSYSTEM S_LNET
24
25 #include <libcfs/kp30.h>
26 #include <libcfs/libcfs.h>
27 #include "tracefile.h"
28
29 struct lc_watchdog {
30         cfs_timer_t       lcw_timer; /* kernel timer */
31         struct list_head  lcw_list;
32         struct timeval    lcw_last_touched;
33         cfs_task_t       *lcw_task;
34
35         void            (*lcw_callback)(pid_t, void *);
36         void             *lcw_data;
37
38         pid_t             lcw_pid;
39         cfs_duration_t    lcw_time; /* time until watchdog fires, jiffies */
40
41         enum {
42                 LC_WATCHDOG_DISABLED,
43                 LC_WATCHDOG_ENABLED,
44                 LC_WATCHDOG_EXPIRED
45         } lcw_state;
46 };
47
48 #ifdef WITH_WATCHDOG
49 /*
50  * The dispatcher will complete lcw_start_completion when it starts,
51  * and lcw_stop_completion when it exits.
52  * Wake lcw_event_waitq to signal timer callback dispatches.
53  */
54 static struct completion lcw_start_completion;
55 static struct completion lcw_stop_completion;
56 static wait_queue_head_t lcw_event_waitq;
57
58 /*
59  * Set this and wake lcw_event_waitq to stop the dispatcher.
60  */
61 enum {
62         LCW_FLAG_STOP = 0
63 };
64 static unsigned long lcw_flags = 0;
65
66 /*
67  * Number of outstanding watchdogs.
68  * When it hits 1, we start the dispatcher.
69  * When it hits 0, we stop the distpatcher.
70  */
71 static __u32         lcw_refcount = 0;
72 static DECLARE_MUTEX(lcw_refcount_sem);
73
74 /*
75  * List of timers that have fired that need their callbacks run by the
76  * dispatcher.
77  */
78 static spinlock_t lcw_pending_timers_lock = SPIN_LOCK_UNLOCKED; /* BH lock! */
79 static struct list_head lcw_pending_timers = \
80         LIST_HEAD_INIT(lcw_pending_timers);
81
82 #ifdef HAVE_TASKLIST_LOCK
83 static void
84 lcw_dump(struct lc_watchdog *lcw)
85 {
86         cfs_task_t *tsk;
87         ENTRY;
88
89         read_lock(&tasklist_lock);
90         tsk = find_task_by_pid(lcw->lcw_pid);
91
92         if (tsk == NULL) {
93                 CWARN("Process %d was not found in the task list; "
94                       "watchdog callback may be incomplete\n", (int)lcw->lcw_pid);
95         } else if (tsk != lcw->lcw_task) {
96                 CWARN("The current process %d did not set the watchdog; "
97                       "watchdog callback may be incomplete\n", (int)lcw->lcw_pid);
98         } else {
99                 libcfs_debug_dumpstack(tsk);
100         }
101         
102         read_unlock(&tasklist_lock);
103         EXIT;
104 }
105 #else
106 static void
107 lcw_dump(struct lc_watchdog *lcw)
108 {
109         CERROR("unable to dump stack because of missing export\n");
110 }
111 #endif
112
113 static void lcw_cb(unsigned long data)
114 {
115         struct lc_watchdog *lcw = (struct lc_watchdog *)data;
116
117         ENTRY;
118
119         if (lcw->lcw_state != LC_WATCHDOG_ENABLED) {
120                 EXIT;
121                 return;
122         }
123
124         lcw->lcw_state = LC_WATCHDOG_EXPIRED;
125
126         /* NB this warning should appear on the console, but may not get into
127          * the logs since we're running in a softirq handler */
128
129         CWARN("Watchdog triggered for pid %d: it was inactive for %lds\n",
130               (int)lcw->lcw_pid, cfs_duration_sec(lcw->lcw_time));
131         lcw_dump(lcw);
132
133         spin_lock_bh(&lcw_pending_timers_lock);
134
135         if (list_empty(&lcw->lcw_list)) {
136                 list_add(&lcw->lcw_list, &lcw_pending_timers);
137                 wake_up(&lcw_event_waitq);
138         }
139
140         spin_unlock_bh(&lcw_pending_timers_lock);
141
142         EXIT;
143 }
144
145 static int is_watchdog_fired(void)
146 {
147         int rc;
148
149         if (test_bit(LCW_FLAG_STOP, &lcw_flags))
150                 return 1;
151
152         spin_lock_bh(&lcw_pending_timers_lock);
153         rc = !list_empty(&lcw_pending_timers);
154         spin_unlock_bh(&lcw_pending_timers_lock);
155         return rc;
156 }
157
158 static int lcw_dispatch_main(void *data)
159 {
160         int                 rc = 0;
161         unsigned long       flags;
162         struct lc_watchdog *lcw;
163
164         ENTRY;
165
166         cfs_daemonize("lc_watchdogd");
167
168         SIGNAL_MASK_LOCK(current, flags);
169         sigfillset(&current->blocked);
170         RECALC_SIGPENDING;
171         SIGNAL_MASK_UNLOCK(current, flags);
172
173         complete(&lcw_start_completion);
174
175         while (1) {
176                 wait_event_interruptible(lcw_event_waitq, is_watchdog_fired());
177                 CDEBUG(D_INFO, "Watchdog got woken up...\n");
178                 if (test_bit(LCW_FLAG_STOP, &lcw_flags)) {
179                         CDEBUG(D_INFO, "LCW_FLAG_STOP was set, shutting down...\n");
180
181                         spin_lock_bh(&lcw_pending_timers_lock);
182                         rc = !list_empty(&lcw_pending_timers);
183                         spin_unlock_bh(&lcw_pending_timers_lock);
184                         if (rc) {
185                                 CERROR("pending timers list was not empty at "
186                                        "time of watchdog dispatch shutdown\n");
187                         }
188                         break;
189                 }
190
191                 spin_lock_bh(&lcw_pending_timers_lock);
192                 while (!list_empty(&lcw_pending_timers)) {
193
194                         lcw = list_entry(lcw_pending_timers.next,
195                                          struct lc_watchdog,
196                                          lcw_list);
197                         list_del_init(&lcw->lcw_list);
198                         spin_unlock_bh(&lcw_pending_timers_lock);
199
200                         CDEBUG(D_INFO, "found lcw for pid %d: inactive for "
201                                "%lds\n", (int)lcw->lcw_pid,
202                                cfs_duration_sec(lcw->lcw_time));
203
204                         if (lcw->lcw_state != LC_WATCHDOG_DISABLED)
205                                 lcw->lcw_callback(lcw->lcw_pid, lcw->lcw_data);
206
207                         spin_lock_bh(&lcw_pending_timers_lock);
208                 }
209                 spin_unlock_bh(&lcw_pending_timers_lock);
210         }
211
212         complete(&lcw_stop_completion);
213
214         RETURN(rc);
215 }
216
217 static void lcw_dispatch_start(void)
218 {
219         int rc;
220
221         ENTRY;
222         LASSERT(lcw_refcount == 1);
223
224         init_completion(&lcw_stop_completion);
225         init_completion(&lcw_start_completion);
226         init_waitqueue_head(&lcw_event_waitq);
227
228         CDEBUG(D_INFO, "starting dispatch thread\n");
229         rc = kernel_thread(lcw_dispatch_main, NULL, 0);
230         if (rc < 0) {
231                 CERROR("error spawning watchdog dispatch thread: %d\n", rc);
232                 EXIT;
233                 return;
234         }
235         wait_for_completion(&lcw_start_completion);
236         CDEBUG(D_INFO, "watchdog dispatcher initialization complete.\n");
237
238         EXIT;
239 }
240
241 static void lcw_dispatch_stop(void)
242 {
243         ENTRY;
244         LASSERT(lcw_refcount == 0);
245
246         CDEBUG(D_INFO, "trying to stop watchdog dispatcher.\n");
247
248         set_bit(LCW_FLAG_STOP, &lcw_flags);
249         wake_up(&lcw_event_waitq);
250
251         wait_for_completion(&lcw_stop_completion);
252
253         CDEBUG(D_INFO, "watchdog dispatcher has shut down.\n");
254
255         EXIT;
256 }
257
258 struct lc_watchdog *lc_watchdog_add(int timeout_ms,
259                                     void (*callback)(pid_t, void *),
260                                     void *data)
261 {
262         struct lc_watchdog *lcw = NULL;
263         ENTRY;
264
265         LIBCFS_ALLOC(lcw, sizeof(*lcw));
266         if (lcw == NULL) {
267                 CDEBUG(D_INFO, "Could not allocate new lc_watchdog\n");
268                 RETURN(ERR_PTR(-ENOMEM));
269         }
270
271         lcw->lcw_task     = cfs_current();
272         lcw->lcw_pid      = cfs_curproc_pid();
273         lcw->lcw_time     = cfs_time_seconds(timeout_ms) / 1000;
274         lcw->lcw_callback = (callback != NULL) ? callback : lc_watchdog_dumplog;
275         lcw->lcw_data     = data;
276         lcw->lcw_state    = LC_WATCHDOG_DISABLED;
277
278         INIT_LIST_HEAD(&lcw->lcw_list);
279
280         lcw->lcw_timer.function = lcw_cb;
281         lcw->lcw_timer.data = (unsigned long)lcw;
282         lcw->lcw_timer.expires = jiffies + lcw->lcw_time;
283         init_timer(&lcw->lcw_timer);
284
285         down(&lcw_refcount_sem);
286         if (++lcw_refcount == 1)
287                 lcw_dispatch_start();
288         up(&lcw_refcount_sem);
289
290         /* Keep this working in case we enable them by default */
291         if (lcw->lcw_state == LC_WATCHDOG_ENABLED) {
292                 do_gettimeofday(&lcw->lcw_last_touched);
293                 add_timer(&lcw->lcw_timer);
294         }
295
296         RETURN(lcw);
297 }
298 EXPORT_SYMBOL(lc_watchdog_add);
299
300 static void lcw_update_time(struct lc_watchdog *lcw, const char *message)
301 {
302         struct timeval newtime;
303         struct timeval timediff;
304
305         do_gettimeofday(&newtime);
306         if (lcw->lcw_state == LC_WATCHDOG_EXPIRED) {
307                 cfs_timeval_sub(&newtime, &lcw->lcw_last_touched, &timediff);
308                 CWARN("Expired watchdog for pid %d %s after %lu.%.4lus\n",
309                       lcw->lcw_pid,
310                       message,
311                       timediff.tv_sec,
312                       timediff.tv_usec / 100);
313         }
314         lcw->lcw_last_touched = newtime;
315 }
316
317 void lc_watchdog_touch_ms(struct lc_watchdog *lcw, int timeout_ms)
318 {
319         ENTRY;
320         LASSERT(lcw != NULL);
321
322         spin_lock_bh(&lcw_pending_timers_lock);
323         list_del_init(&lcw->lcw_list);
324         spin_unlock_bh(&lcw_pending_timers_lock);
325
326         lcw_update_time(lcw, "touched");
327         lcw->lcw_state = LC_WATCHDOG_ENABLED;
328
329         mod_timer(&lcw->lcw_timer, jiffies +
330                   cfs_time_seconds(timeout_ms) / 1000);
331
332         EXIT;
333 }
334 EXPORT_SYMBOL(lc_watchdog_touch_ms);
335
336 /* deprecated - use above instead */
337 void lc_watchdog_touch(struct lc_watchdog *lcw)
338 {
339         lc_watchdog_touch_ms(lcw, cfs_duration_sec(lcw->lcw_time) * 1000);
340 }
341 EXPORT_SYMBOL(lc_watchdog_touch);
342
343 void lc_watchdog_disable(struct lc_watchdog *lcw)
344 {
345         ENTRY;
346         LASSERT(lcw != NULL);
347
348         spin_lock_bh(&lcw_pending_timers_lock);
349         if (!list_empty(&lcw->lcw_list))
350                 list_del_init(&lcw->lcw_list);
351         spin_unlock_bh(&lcw_pending_timers_lock);
352
353         lcw_update_time(lcw, "disabled");
354         lcw->lcw_state = LC_WATCHDOG_DISABLED;
355
356         EXIT;
357 }
358 EXPORT_SYMBOL(lc_watchdog_disable);
359
360 void lc_watchdog_delete(struct lc_watchdog *lcw)
361 {
362         ENTRY;
363         LASSERT(lcw != NULL);
364
365         del_timer(&lcw->lcw_timer);
366
367         lcw_update_time(lcw, "deleted");
368
369         spin_lock_bh(&lcw_pending_timers_lock);
370         if (!list_empty(&lcw->lcw_list))
371                 list_del_init(&lcw->lcw_list);
372         spin_unlock_bh(&lcw_pending_timers_lock);
373
374         down(&lcw_refcount_sem);
375         if (--lcw_refcount == 0)
376                 lcw_dispatch_stop();
377         up(&lcw_refcount_sem);
378
379         LIBCFS_FREE(lcw, sizeof(*lcw));
380
381         EXIT;
382 }
383 EXPORT_SYMBOL(lc_watchdog_delete);
384
385 /*
386  * Provided watchdog handlers
387  */
388
389 void lc_watchdog_dumplog(pid_t pid, void *data)
390 {
391         libcfs_debug_dumplog_internal((void *)((unsigned long)pid));
392 }
393 EXPORT_SYMBOL(lc_watchdog_dumplog);
394
395 #else   /* !defined(WITH_WATCHDOG) */
396
397 struct lc_watchdog *lc_watchdog_add(int timeout_ms,
398                                     void (*callback)(pid_t pid, void *),
399                                     void *data)
400 {
401         static struct lc_watchdog      watchdog;
402         return &watchdog;
403 }
404 EXPORT_SYMBOL(lc_watchdog_add);
405
406 void lc_watchdog_touch_ms(struct lc_watchdog *lcw, int timeout_ms)
407 {
408 }
409 EXPORT_SYMBOL(lc_watchdog_touch_ms);
410
411 void lc_watchdog_touch(struct lc_watchdog *lcw)
412 {
413 }
414 EXPORT_SYMBOL(lc_watchdog_touch);
415
416 void lc_watchdog_disable(struct lc_watchdog *lcw)
417 {
418 }
419 EXPORT_SYMBOL(lc_watchdog_disable);
420
421 void lc_watchdog_delete(struct lc_watchdog *lcw)
422 {
423 }
424 EXPORT_SYMBOL(lc_watchdog_delete);
425
426 #endif
427