Whamcloud - gitweb
b1bdb71d349693b7448a303f006d343010309932
[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 (c) 2008, 2010, Oracle and/or its affiliates. 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_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 *);
54         void           *lcw_data;
55
56         pid_t           lcw_pid;
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 cfs_completion_t lcw_start_completion;
72 static cfs_completion_t  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 dispatcher.
87  */
88 static __u32         lcw_refcount = 0;
89 static CFS_DEFINE_MUTEX(lcw_refcount_mutex);
90
91 /*
92  * List of timers that have fired that need their callbacks run by the
93  * dispatcher.
94  */
95 /* BH lock! */
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);
99
100 /* Last time a watchdog expired */
101 static cfs_time_t lcw_last_watchdog_time;
102 static int lcw_recent_watchdog_count;
103
104 static void
105 lcw_dump(struct lc_watchdog *lcw)
106 {
107         ENTRY;
108 #if defined(HAVE_TASKLIST_LOCK)
109         cfs_read_lock(&tasklist_lock);
110 #else
111         rcu_read_lock();
112 #endif
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",
116                               (int)lcw->lcw_pid);
117         } else {
118                 libcfs_debug_dumpstack(lcw->lcw_task);
119         }
120
121 #if defined(HAVE_TASKLIST_LOCK)
122         cfs_read_unlock(&tasklist_lock);
123 #else
124         rcu_read_unlock();
125 #endif
126         EXIT;
127 }
128
129 static void lcw_cb(ulong_ptr_t data)
130 {
131         struct lc_watchdog *lcw = (struct lc_watchdog *)data;
132         ENTRY;
133
134         if (lcw->lcw_state != LC_WATCHDOG_ENABLED) {
135                 EXIT;
136                 return;
137         }
138
139         lcw->lcw_state = LC_WATCHDOG_EXPIRED;
140
141         cfs_spin_lock_bh(&lcw->lcw_lock);
142         LASSERT(cfs_list_empty(&lcw->lcw_list));
143
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);
148
149         cfs_spin_unlock_bh(&lcw_pending_timers_lock);
150         cfs_spin_unlock_bh(&lcw->lcw_lock);
151         EXIT;
152 }
153
154 static int is_watchdog_fired(void)
155 {
156         int rc;
157
158         if (cfs_test_bit(LCW_FLAG_STOP, &lcw_flags))
159                 return 1;
160
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);
164         return rc;
165 }
166
167 static void lcw_dump_stack(struct lc_watchdog *lcw)
168 {
169         cfs_time_t      current_time;
170         cfs_duration_t  delta_time;
171         struct timeval  timediff;
172
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);
176
177         /*
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.
180          */
181         delta_time = cfs_duration_sec(cfs_time_sub(current_time,
182                                                    lcw_last_watchdog_time));
183
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",
189                               (int)lcw->lcw_pid,
190                               timediff.tv_sec,
191                               timediff.tv_usec / 10000,
192                               libcfs_watchdog_ratelimit);
193         } else {
194                 if (delta_time < libcfs_watchdog_ratelimit) {
195                         lcw_recent_watchdog_count++;
196                 } else {
197                         memcpy(&lcw_last_watchdog_time, &current_time,
198                                sizeof(current_time));
199                         lcw_recent_watchdog_count = 0;
200                 }
201
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:"
206                               "\n",
207                               (int)lcw->lcw_pid,
208                               timediff.tv_sec,
209                               timediff.tv_usec / 10000);
210                 lcw_dump(lcw);
211         }
212 }
213
214 static int lcw_dispatch_main(void *data)
215 {
216         int                 rc = 0;
217         unsigned long       flags;
218         struct lc_watchdog *lcw;
219         CFS_LIST_HEAD      (zombies);
220
221         ENTRY;
222
223         cfs_daemonize("lc_watchdogd");
224
225         SIGNAL_MASK_LOCK(current, flags);
226         sigfillset(&current->blocked);
227         RECALC_SIGPENDING;
228         SIGNAL_MASK_UNLOCK(current, flags);
229
230         cfs_complete(&lcw_start_completion);
231
232         while (1) {
233                 int dumplog = 1;
234
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");
240
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);
244                         if (rc) {
245                                 CERROR("pending timers list was not empty at "
246                                        "time of watchdog dispatch shutdown\n");
247                         }
248                         break;
249                 }
250
251                 cfs_spin_lock_bh(&lcw_pending_timers_lock);
252                 while (!cfs_list_empty(&lcw_pending_timers)) {
253                         int is_dumplog;
254
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 */
259                         lcw->lcw_refcount++;
260                         cfs_spin_unlock_bh(&lcw_pending_timers_lock);
261
262                         /* lock ordering */
263                         cfs_spin_lock_bh(&lcw->lcw_lock);
264                         cfs_spin_lock_bh(&lcw_pending_timers_lock);
265
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 */
273                                 continue;
274                         }
275
276                         cfs_list_del_init(&lcw->lcw_list);
277                         lcw->lcw_refcount--; /* -1 ref for pending list */
278
279                         cfs_spin_unlock_bh(&lcw_pending_timers_lock);
280                         cfs_spin_unlock_bh(&lcw->lcw_lock);
281
282                         CDEBUG(D_INFO, "found lcw for pid " LPPID "\n",
283                                lcw->lcw_pid);
284                         lcw_dump_stack(lcw);
285
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)
291                                         dumplog = 0;
292                         }
293
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);
298                 }
299                 cfs_spin_unlock_bh(&lcw_pending_timers_lock);
300
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));
306                 }
307         }
308
309         cfs_complete(&lcw_stop_completion);
310
311         RETURN(rc);
312 }
313
314 static void lcw_dispatch_start(void)
315 {
316         int rc;
317
318         ENTRY;
319         LASSERT(lcw_refcount == 1);
320
321         cfs_init_completion(&lcw_stop_completion);
322         cfs_init_completion(&lcw_start_completion);
323         cfs_waitq_init(&lcw_event_waitq);
324
325         CDEBUG(D_INFO, "starting dispatch thread\n");
326         rc = cfs_create_thread(lcw_dispatch_main, NULL, 0);
327         if (rc < 0) {
328                 CERROR("error spawning watchdog dispatch thread: %d\n", rc);
329                 EXIT;
330                 return;
331         }
332         cfs_wait_for_completion(&lcw_start_completion);
333         CDEBUG(D_INFO, "watchdog dispatcher initialization complete.\n");
334
335         EXIT;
336 }
337
338 static void lcw_dispatch_stop(void)
339 {
340         ENTRY;
341         LASSERT(lcw_refcount == 0);
342
343         CDEBUG(D_INFO, "trying to stop watchdog dispatcher.\n");
344
345         cfs_set_bit(LCW_FLAG_STOP, &lcw_flags);
346         cfs_waitq_signal(&lcw_event_waitq);
347
348         cfs_wait_for_completion(&lcw_stop_completion);
349
350         CDEBUG(D_INFO, "watchdog dispatcher has shut down.\n");
351
352         EXIT;
353 }
354
355 struct lc_watchdog *lc_watchdog_add(int timeout,
356                                     void (*callback)(pid_t, void *),
357                                     void *data)
358 {
359         struct lc_watchdog *lcw = NULL;
360         ENTRY;
361
362         LIBCFS_ALLOC(lcw, sizeof(*lcw));
363         if (lcw == NULL) {
364                 CDEBUG(D_INFO, "Could not allocate new lc_watchdog\n");
365                 RETURN(ERR_PTR(-ENOMEM));
366         }
367
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;
375
376         CFS_INIT_LIST_HEAD(&lcw->lcw_list);
377         cfs_timer_init(&lcw->lcw_timer, lcw_cb, lcw);
378
379         cfs_mutex_lock(&lcw_refcount_mutex);
380         if (++lcw_refcount == 1)
381                 lcw_dispatch_start();
382         cfs_mutex_unlock(&lcw_refcount_mutex);
383
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) +
388                               cfs_time_current());
389         }
390
391         RETURN(lcw);
392 }
393 EXPORT_SYMBOL(lc_watchdog_add);
394
395 static void lcw_update_time(struct lc_watchdog *lcw, const char *message)
396 {
397         cfs_time_t newtime = cfs_time_current();;
398
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);
404
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",
409                               lcw->lcw_pid,
410                               message,
411                               timediff.tv_sec,
412                               timediff.tv_usec / 10000);
413         }
414         lcw->lcw_last_touched = newtime;
415 }
416
417 static void lc_watchdog_del_pending(struct lc_watchdog *lcw)
418 {
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);
425         }
426
427         cfs_spin_unlock_bh(&lcw->lcw_lock);
428 }
429
430 void lc_watchdog_touch(struct lc_watchdog *lcw, int timeout)
431 {
432         ENTRY;
433         LASSERT(lcw != NULL);
434
435         lc_watchdog_del_pending(lcw);
436
437         lcw_update_time(lcw, "resumed");
438         lcw->lcw_state = LC_WATCHDOG_ENABLED;
439
440         cfs_timer_arm(&lcw->lcw_timer, cfs_time_current() +
441                       cfs_time_seconds(timeout));
442
443         EXIT;
444 }
445 EXPORT_SYMBOL(lc_watchdog_touch);
446
447 void lc_watchdog_disable(struct lc_watchdog *lcw)
448 {
449         ENTRY;
450         LASSERT(lcw != NULL);
451
452         lc_watchdog_del_pending(lcw);
453
454         lcw_update_time(lcw, "completed");
455         lcw->lcw_state = LC_WATCHDOG_DISABLED;
456
457         EXIT;
458 }
459 EXPORT_SYMBOL(lc_watchdog_disable);
460
461 void lc_watchdog_delete(struct lc_watchdog *lcw)
462 {
463         int dead;
464
465         ENTRY;
466         LASSERT(lcw != NULL);
467
468         cfs_timer_disarm(&lcw->lcw_timer);
469
470         lcw_update_time(lcw, "stopped");
471
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 */
477         }
478
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);
483
484         if (dead)
485                 LIBCFS_FREE(lcw, sizeof(*lcw));
486
487         cfs_mutex_lock(&lcw_refcount_mutex);
488         if (--lcw_refcount == 0)
489                 lcw_dispatch_stop();
490         cfs_mutex_unlock(&lcw_refcount_mutex);
491
492         EXIT;
493 }
494 EXPORT_SYMBOL(lc_watchdog_delete);
495
496 /*
497  * Provided watchdog handlers
498  */
499
500 void lc_watchdog_dumplog(pid_t pid, void *data)
501 {
502         libcfs_debug_dumplog_internal((void *)((long_ptr_t)pid));
503 }
504 EXPORT_SYMBOL(lc_watchdog_dumplog);
505
506 #else   /* !defined(WITH_WATCHDOG) */
507
508 struct lc_watchdog *lc_watchdog_add(int timeout,
509                                     void (*callback)(pid_t pid, void *),
510                                     void *data)
511 {
512         static struct lc_watchdog      watchdog;
513         return &watchdog;
514 }
515 EXPORT_SYMBOL(lc_watchdog_add);
516
517 void lc_watchdog_touch(struct lc_watchdog *lcw, int timeout)
518 {
519 }
520 EXPORT_SYMBOL(lc_watchdog_touch);
521
522 void lc_watchdog_disable(struct lc_watchdog *lcw)
523 {
524 }
525 EXPORT_SYMBOL(lc_watchdog_disable);
526
527 void lc_watchdog_delete(struct lc_watchdog *lcw)
528 {
529 }
530 EXPORT_SYMBOL(lc_watchdog_delete);
531
532 #endif