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