Whamcloud - gitweb
LU-502 don't allow to kill service threads by OOM killer.
[fs/lustre-release.git] / libcfs / libcfs / debug.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/debug.c
37  *
38  * Author: Phil Schwan <phil@clusterfs.com>
39  */
40
41 #ifndef EXPORT_SYMTAB
42 # define EXPORT_SYMTAB
43 #endif
44
45 # define DEBUG_SUBSYSTEM S_LNET
46
47 #include <libcfs/libcfs.h>
48 #include "tracefile.h"
49
50 static char debug_file_name[1024];
51
52 unsigned int libcfs_subsystem_debug = ~0;
53 CFS_MODULE_PARM(libcfs_subsystem_debug, "i", int, 0644,
54                 "Lustre kernel debug subsystem mask");
55 EXPORT_SYMBOL(libcfs_subsystem_debug);
56
57 unsigned int libcfs_debug = (D_EMERG | D_ERROR | D_WARNING | D_CONSOLE |
58                              D_NETERROR | D_HA | D_CONFIG | D_IOCTL);
59 CFS_MODULE_PARM(libcfs_debug, "i", int, 0644,
60                 "Lustre kernel debug mask");
61 EXPORT_SYMBOL(libcfs_debug);
62
63 unsigned int libcfs_debug_mb = 0;
64 CFS_MODULE_PARM(libcfs_debug_mb, "i", uint, 0644,
65                 "Total debug buffer size.");
66 EXPORT_SYMBOL(libcfs_debug_mb);
67
68 unsigned int libcfs_printk = D_CANTMASK;
69 CFS_MODULE_PARM(libcfs_printk, "i", uint, 0644,
70                 "Lustre kernel debug console mask");
71 EXPORT_SYMBOL(libcfs_printk);
72
73 unsigned int libcfs_console_ratelimit = 1;
74 CFS_MODULE_PARM(libcfs_console_ratelimit, "i", uint, 0644,
75                 "Lustre kernel debug console ratelimit (0 to disable)");
76 EXPORT_SYMBOL(libcfs_console_ratelimit);
77
78 cfs_duration_t libcfs_console_max_delay;
79 CFS_MODULE_PARM(libcfs_console_max_delay, "l", ulong, 0644,
80                 "Lustre kernel debug console max delay (jiffies)");
81 EXPORT_SYMBOL(libcfs_console_max_delay);
82
83 cfs_duration_t libcfs_console_min_delay;
84 CFS_MODULE_PARM(libcfs_console_min_delay, "l", ulong, 0644,
85                 "Lustre kernel debug console min delay (jiffies)");
86 EXPORT_SYMBOL(libcfs_console_min_delay);
87
88 unsigned int libcfs_console_backoff = CDEBUG_DEFAULT_BACKOFF;
89 CFS_MODULE_PARM(libcfs_console_backoff, "i", uint, 0644,
90                 "Lustre kernel debug console backoff factor");
91 EXPORT_SYMBOL(libcfs_console_backoff);
92
93 unsigned int libcfs_debug_binary = 1;
94 EXPORT_SYMBOL(libcfs_debug_binary);
95
96 unsigned int libcfs_stack;
97 EXPORT_SYMBOL(libcfs_stack);
98
99 unsigned int portal_enter_debugger;
100 EXPORT_SYMBOL(portal_enter_debugger);
101
102 unsigned int libcfs_catastrophe;
103 EXPORT_SYMBOL(libcfs_catastrophe);
104
105 unsigned int libcfs_watchdog_ratelimit = 300;
106 EXPORT_SYMBOL(libcfs_watchdog_ratelimit);
107
108 unsigned int libcfs_panic_on_lbug = 1;
109 CFS_MODULE_PARM(libcfs_panic_on_lbug, "i", uint, 0644,
110                 "Lustre kernel panic on LBUG");
111 EXPORT_SYMBOL(libcfs_panic_on_lbug);
112
113 cfs_atomic_t libcfs_kmemory = CFS_ATOMIC_INIT(0);
114 EXPORT_SYMBOL(libcfs_kmemory);
115
116 static cfs_waitq_t debug_ctlwq;
117
118 char libcfs_debug_file_path_arr[PATH_MAX] = LIBCFS_DEBUG_FILE_PATH_DEFAULT;
119
120 /* We need to pass a pointer here, but elsewhere this must be a const */
121 char *libcfs_debug_file_path;
122 CFS_MODULE_PARM(libcfs_debug_file_path, "s", charp, 0644,
123                 "Path for dumping debug logs, "
124                 "set 'NONE' to prevent log dumping");
125
126 int libcfs_panic_in_progress;
127
128 /* libcfs_debug_token2mask() expects the returned
129  * string in lower-case */
130 const char *
131 libcfs_debug_subsys2str(int subsys)
132 {
133         switch (1 << subsys) {
134         default:
135                 return NULL;
136         case S_UNDEFINED:
137                 return "undefined";
138         case S_MDC:
139                 return "mdc";
140         case S_MDS:
141                 return "mds";
142         case S_OSC:
143                 return "osc";
144         case S_OST:
145                 return "ost";
146         case S_CLASS:
147                 return "class";
148         case S_LOG:
149                 return "log";
150         case S_LLITE:
151                 return "llite";
152         case S_RPC:
153                 return "rpc";
154         case S_LNET:
155                 return "lnet";
156         case S_LND:
157                 return "lnd";
158         case S_PINGER:
159                 return "pinger";
160         case S_FILTER:
161                 return "filter";
162         case S_ECHO:
163                 return "echo";
164         case S_LDLM:
165                 return "ldlm";
166         case S_LOV:
167                 return "lov";
168         case S_LQUOTA:
169                 return "lquota";
170         case S_LMV:
171                 return "lmv";
172         case S_SEC:
173                 return "sec";
174         case S_GSS:
175                 return "gss";
176         case S_MGC:
177                 return "mgc";
178         case S_MGS:
179                 return "mgs";
180         case S_FID:
181                 return "fid";
182         case S_FLD:
183                 return "fld";
184         }
185 }
186
187 /* libcfs_debug_token2mask() expects the returned
188  * string in lower-case */
189 const char *
190 libcfs_debug_dbg2str(int debug)
191 {
192         switch (1 << debug) {
193         default:
194                 return NULL;
195         case D_TRACE:
196                 return "trace";
197         case D_INODE:
198                 return "inode";
199         case D_SUPER:
200                 return "super";
201         case D_EXT2:
202                 return "ext2";
203         case D_MALLOC:
204                 return "malloc";
205         case D_CACHE:
206                 return "cache";
207         case D_INFO:
208                 return "info";
209         case D_IOCTL:
210                 return "ioctl";
211         case D_NETERROR:
212                 return "neterror";
213         case D_NET:
214                 return "net";
215         case D_WARNING:
216                 return "warning";
217         case D_BUFFS:
218                 return "buffs";
219         case D_OTHER:
220                 return "other";
221         case D_DENTRY:
222                 return "dentry";
223         case D_NETTRACE:
224                 return "nettrace";
225         case D_PAGE:
226                 return "page";
227         case D_DLMTRACE:
228                 return "dlmtrace";
229         case D_ERROR:
230                 return "error";
231         case D_EMERG:
232                 return "emerg";
233         case D_HA:
234                 return "ha";
235         case D_RPCTRACE:
236                 return "rpctrace";
237         case D_VFSTRACE:
238                 return "vfstrace";
239         case D_READA:
240                 return "reada";
241         case D_MMAP:
242                 return "mmap";
243         case D_CONFIG:
244                 return "config";
245         case D_CONSOLE:
246                 return "console";
247         case D_QUOTA:
248                 return "quota";
249         case D_SEC:
250                 return "sec";
251         }
252 }
253
254 int
255 libcfs_debug_mask2str(char *str, int size, int mask, int is_subsys)
256 {
257         const char *(*fn)(int bit) = is_subsys ? libcfs_debug_subsys2str :
258                                                  libcfs_debug_dbg2str;
259         int           len = 0;
260         const char   *token;
261         int           i;
262
263         if (mask == 0) {                        /* "0" */
264                 if (size > 0)
265                         str[0] = '0';
266                 len = 1;
267         } else {                                /* space-separated tokens */
268                 for (i = 0; i < 32; i++) {
269                         if ((mask & (1 << i)) == 0)
270                                 continue;
271
272                         token = fn(i);
273                         if (token == NULL)              /* unused bit */
274                                 continue;
275
276                         if (len > 0) {                  /* separator? */
277                                 if (len < size)
278                                         str[len] = ' ';
279                                 len++;
280                         }
281
282                         while (*token != 0) {
283                                 if (len < size)
284                                         str[len] = *token;
285                                 token++;
286                                 len++;
287                         }
288                 }
289         }
290
291         /* terminate 'str' */
292         if (len < size)
293                 str[len] = 0;
294         else
295                 str[size - 1] = 0;
296
297         return len;
298 }
299
300 int
301 libcfs_debug_str2mask(int *mask, const char *str, int is_subsys)
302 {
303         const char *(*fn)(int bit) = is_subsys ? libcfs_debug_subsys2str :
304                                                  libcfs_debug_dbg2str;
305         int         m = 0;
306         int         matched;
307         int         n;
308         int         t;
309
310         /* Allow a number for backwards compatibility */
311
312         for (n = strlen(str); n > 0; n--)
313                 if (!isspace(str[n-1]))
314                         break;
315         matched = n;
316
317         if ((t = sscanf(str, "%i%n", &m, &matched)) >= 1 &&
318             matched == n) {
319                 /* don't print warning for lctl set_param debug=0 */
320                 if (m != 0)
321                         CWARN("You are trying to use a numerical value for the "
322                               "mask - this will be deprecated in a future "
323                               "release.\n");
324                 *mask = m;
325                 return 0;
326         }
327
328         return cfs_str2mask(str, fn, mask, is_subsys ? 0 : D_CANTMASK,
329                             0xffffffff);
330 }
331
332 /**
333  * Dump Lustre log to ::debug_file_path by calling tracefile_dump_all_pages()
334  */
335 void libcfs_debug_dumplog_internal(void *arg)
336 {
337         CFS_DECL_JOURNAL_DATA;
338
339         CFS_PUSH_JOURNAL;
340
341         if (strncmp(libcfs_debug_file_path_arr, "NONE", 4) != 0) {
342                 snprintf(debug_file_name, sizeof(debug_file_name) - 1,
343                          "%s.%ld." LPLD, libcfs_debug_file_path_arr,
344                          cfs_time_current_sec(), (long_ptr_t)arg);
345                 printk(CFS_KERN_ALERT "LustreError: dumping log to %s\n",
346                        debug_file_name);
347                 cfs_tracefile_dump_all_pages(debug_file_name);
348                 libcfs_run_debug_log_upcall(debug_file_name);
349         }
350         CFS_POP_JOURNAL;
351 }
352
353 int libcfs_debug_dumplog_thread(void *arg)
354 {
355         libcfs_debug_dumplog_internal(arg);
356         cfs_waitq_signal(&debug_ctlwq);
357         return 0;
358 }
359
360 void libcfs_debug_dumplog(void)
361 {
362         cfs_waitlink_t wait;
363         cfs_task_t    *dumper;
364         ENTRY;
365
366         /* we're being careful to ensure that the kernel thread is
367          * able to set our state to running as it exits before we
368          * get to schedule() */
369         cfs_waitlink_init(&wait);
370         cfs_set_current_state(CFS_TASK_INTERRUPTIBLE);
371         cfs_waitq_add(&debug_ctlwq, &wait);
372
373         dumper = cfs_kthread_run(libcfs_debug_dumplog_thread,
374                                  (void*)(long)cfs_curproc_pid(),
375                                  "libcfs_debug_dumper");
376         if (IS_ERR(dumper))
377                 printk(CFS_KERN_ERR "LustreError: cannot start log dump thread:"
378                        " %ld\n", PTR_ERR(dumper));
379         else
380                 cfs_waitq_wait(&wait, CFS_TASK_INTERRUPTIBLE);
381
382         /* be sure to teardown if cfs_create_thread() failed */
383         cfs_waitq_del(&debug_ctlwq, &wait);
384         cfs_set_current_state(CFS_TASK_RUNNING);
385 }
386
387 int libcfs_debug_init(unsigned long bufsize)
388 {
389         int    rc = 0;
390         unsigned int max = libcfs_debug_mb;
391
392         cfs_waitq_init(&debug_ctlwq);
393
394         if (libcfs_console_max_delay <= 0 || /* not set by user or */
395             libcfs_console_min_delay <= 0 || /* set to invalid values */
396             libcfs_console_min_delay >= libcfs_console_max_delay) {
397                 libcfs_console_max_delay = CDEBUG_DEFAULT_MAX_DELAY;
398                 libcfs_console_min_delay = CDEBUG_DEFAULT_MIN_DELAY;
399         }
400
401         if (libcfs_debug_file_path != NULL) {
402                 memset(libcfs_debug_file_path_arr, 0, PATH_MAX);
403                 strncpy(libcfs_debug_file_path_arr, 
404                         libcfs_debug_file_path, PATH_MAX-1);
405         }
406
407         /* If libcfs_debug_mb is set to an invalid value or uninitialized
408          * then just make the total buffers smp_num_cpus * TCD_MAX_PAGES */
409         if (max > cfs_trace_max_debug_mb() || max < cfs_num_possible_cpus()) {
410                 max = TCD_MAX_PAGES;
411         } else {
412                 max = (max / cfs_num_possible_cpus());
413                 max = (max << (20 - CFS_PAGE_SHIFT));
414         }
415         rc = cfs_tracefile_init(max);
416
417         if (rc == 0)
418                 libcfs_register_panic_notifier();
419
420         return rc;
421 }
422
423 int libcfs_debug_cleanup(void)
424 {
425         libcfs_unregister_panic_notifier();
426         cfs_tracefile_exit();
427         return 0;
428 }
429
430 int libcfs_debug_clear_buffer(void)
431 {
432         cfs_trace_flush_pages();
433         return 0;
434 }
435
436 /* Debug markers, although printed by S_LNET
437  * should not be be marked as such. */
438 #undef DEBUG_SUBSYSTEM
439 #define DEBUG_SUBSYSTEM S_UNDEFINED
440 int libcfs_debug_mark_buffer(const char *text)
441 {
442         CDEBUG(D_TRACE,"***************************************************\n");
443         LCONSOLE(D_WARNING, "DEBUG MARKER: %s\n", text);
444         CDEBUG(D_TRACE,"***************************************************\n");
445
446         return 0;
447 }
448 #undef DEBUG_SUBSYSTEM
449 #define DEBUG_SUBSYSTEM S_LNET
450
451 void libcfs_debug_set_level(unsigned int debug_level)
452 {
453         printk(CFS_KERN_WARNING "Lustre: Setting portals debug level to %08x\n",
454                debug_level);
455         libcfs_debug = debug_level;
456 }
457
458 EXPORT_SYMBOL(libcfs_debug_dumplog);
459 EXPORT_SYMBOL(libcfs_debug_set_level);