Whamcloud - gitweb
Land b_release_1_4_3 onto HEAD (20050619_0305)
[fs/lustre-release.git] / lnet / libcfs / debug.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Copyright (C) 2002 Cluster File Systems, Inc.
5  *   Author: Phil Schwan <phil@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 #ifndef EXPORT_SYMTAB
24 # define EXPORT_SYMTAB
25 #endif
26
27 # define DEBUG_SUBSYSTEM S_PORTALS
28
29 #include <libcfs/kp30.h>
30 #include <libcfs/libcfs.h>
31
32 #include "tracefile.h"
33
34 unsigned int portal_subsystem_debug = ~0 - (S_PORTALS);
35 EXPORT_SYMBOL(portal_subsystem_debug);
36
37 unsigned int portal_debug = (D_WARNING | D_DLMTRACE | D_ERROR | D_EMERG | D_HA |
38                              D_RPCTRACE | D_VFSTRACE | D_CONFIG | D_IOCTL |
39                              D_CONSOLE);
40 EXPORT_SYMBOL(portal_debug);
41
42 unsigned int portal_printk;
43 EXPORT_SYMBOL(portal_printk);
44
45 unsigned int portal_stack;
46 EXPORT_SYMBOL(portal_stack);
47
48 unsigned int portals_catastrophe;
49 EXPORT_SYMBOL(portals_catastrophe);
50
51 #ifdef __KERNEL__
52 atomic_t portal_kmemory = ATOMIC_INIT(0);
53 EXPORT_SYMBOL(portal_kmemory);
54 #endif
55
56 static cfs_waitq_t debug_ctlwq;
57
58 char debug_file_path[1024] = "/tmp/lustre-log";
59 static char debug_file_name[1024];
60
61 void portals_debug_dumplog_internal(void *arg)
62 {
63         CFS_DECL_JOURNAL_DATA;
64
65         CFS_PUSH_JOURNAL;
66
67         snprintf(debug_file_name, sizeof(debug_file_path) - 1,
68                  "%s.%ld.%ld", debug_file_path, cfs_time_current_sec(), (long)arg);
69         printk(KERN_ALERT "LustreError: dumping log to %s\n", debug_file_name);
70         tracefile_dump_all_pages(debug_file_name);
71
72         CFS_POP_JOURNAL;
73 }
74
75 int portals_debug_dumplog_thread(void *arg)
76 {
77         kportal_daemonize("");
78         reparent_to_init();
79         portals_debug_dumplog_internal(arg);
80         cfs_waitq_signal(&debug_ctlwq);
81         return 0;
82 }
83
84 void portals_debug_dumplog(void)
85 {
86         int            rc;
87         cfs_waitlink_t wait;
88         ENTRY;
89
90         /* we're being careful to ensure that the kernel thread is
91          * able to set our state to running as it exits before we
92          * get to schedule() */
93         cfs_waitlink_init(&wait);
94         set_current_state(TASK_INTERRUPTIBLE);
95         cfs_waitq_add(&debug_ctlwq, &wait);
96
97         rc = cfs_kernel_thread(portals_debug_dumplog_thread,
98                                (void *)(long)cfs_curproc_pid(),
99                                CLONE_VM | CLONE_FS | CLONE_FILES);
100         if (rc < 0)
101                 printk(KERN_ERR "LustreError: cannot start log dump thread: "
102                        "%d\n", rc);
103         else
104                 schedule();
105
106         /* be sure to teardown if kernel_thread() failed */
107         cfs_waitq_del(&debug_ctlwq, &wait);
108         set_current_state(TASK_RUNNING);
109 }
110
111 #ifdef PORTALS_DUMP_ON_PANIC
112 static int panic_dumplog(struct notifier_block *self, unsigned long unused1,
113                          void *unused2)
114 {
115         static int handled_panic; /* to avoid recursive calls to notifiers */
116
117         if (handled_panic)
118                 return 0;
119         else
120                 handled_panic = 1;
121
122         if (in_interrupt()) {
123                 trace_debug_print();
124                 return 0;
125         }
126
127         while (current->lock_depth >= 0)
128                 unlock_kernel();
129         portals_debug_dumplog();
130         return 0;
131 }
132
133 static struct notifier_block lustre_panic_notifier = {
134         notifier_call :     panic_dumplog,
135         next :              NULL,
136         priority :          10000
137 };
138 #endif
139
140 #ifdef CRAY_PORTALS
141 extern void *lus_portals_debug;
142 #endif
143
144 int portals_debug_init(unsigned long bufsize)
145 {
146         cfs_waitq_init(&debug_ctlwq);
147 #ifdef CRAY_PORTALS
148         lus_portals_debug = &portals_debug_msg;
149 #endif
150 #ifdef PORTALS_DUMP_ON_PANIC
151         /* This is currently disabled because it spews far too much to the
152          * console on the rare cases it is ever triggered. */
153         notifier_chain_register(&panic_notifier_list, &lustre_panic_notifier);
154 #endif
155         return tracefile_init();
156 }
157
158 int portals_debug_cleanup(void)
159 {
160         tracefile_exit();
161 #ifdef PORTALS_DUMP_ON_PANIC
162         notifier_chain_unregister(&panic_notifier_list, &lustre_panic_notifier);
163 #endif
164 #ifdef CRAY_PORTALS
165         lus_portals_debug = NULL;
166 #endif
167         return 0;
168 }
169
170 int portals_debug_clear_buffer(void)
171 {
172         trace_flush_pages();
173         return 0;
174 }
175
176 /* Debug markers, although printed by S_PORTALS
177  * should not be be marked as such. */
178 #undef DEBUG_SUBSYSTEM
179 #define DEBUG_SUBSYSTEM S_UNDEFINED
180 int portals_debug_mark_buffer(char *text)
181 {
182         CDEBUG(D_TRACE,"***************************************************\n");
183         CDEBUG(D_WARNING, "DEBUG MARKER: %s\n", text);
184         CDEBUG(D_TRACE,"***************************************************\n");
185
186         return 0;
187 }
188 #undef DEBUG_SUBSYSTEM
189 #define DEBUG_SUBSYSTEM S_PORTALS
190
191 void portals_debug_set_level(unsigned int debug_level)
192 {
193         printk(KERN_WARNING "Lustre: Setting portals debug level to %08x\n",
194                debug_level);
195         portal_debug = debug_level;
196 }
197
198 char *portals_nid2str(int nal, ptl_nid_t nid, char *str)
199 {
200         if (nid == PTL_NID_ANY) {
201                 snprintf(str, PTL_NALFMT_SIZE, "%s", "PTL_NID_ANY");
202                 return str;
203         }
204
205         switch(nal){
206 /* XXX this could be a nal method of some sort, 'cept it's config
207  * dependent whether (say) socknal NIDs are actually IP addresses... */
208 #if !CRAY_PORTALS
209         case TCPNAL:
210                 /* userspace NAL */
211         case IIBNAL:
212         case VIBNAL:
213         case OPENIBNAL:
214         case RANAL:
215         case SOCKNAL: {
216                 /* HIPQUAD requires __u32, but we can't cast in it */
217                 __u32 nid32 = (__u32)nid;
218                 if ((__u32)(nid >> 32)) {
219                         snprintf(str, PTL_NALFMT_SIZE, "%u:%u.%u.%u.%u",
220                                  (__u32)(nid >> 32), HIPQUAD(nid32));
221                 } else {
222                         snprintf(str, PTL_NALFMT_SIZE, "%u.%u.%u.%u",
223                                  HIPQUAD(nid32));
224                 }
225                 break;
226         }
227         case QSWNAL:
228         case GMNAL:
229         case LONAL:
230                 snprintf(str, PTL_NALFMT_SIZE, "%u:%u",
231                          (__u32)(nid >> 32), (__u32)nid);
232                 break;
233 #endif
234         default:
235                 snprintf(str, PTL_NALFMT_SIZE, "?%x? %llx",
236                          nal, (long long)nid);
237                 break;
238         }
239         return str;
240 }
241
242 char *portals_id2str(int nal, ptl_process_id_t id, char *str)
243 {
244         int   len;
245
246         portals_nid2str(nal, id.nid, str);
247         len = strlen(str);
248         snprintf(str + len, PTL_NALFMT_SIZE - len, "-%u", id.pid);
249         return str;
250 }
251
252 EXPORT_SYMBOL(portals_debug_dumplog);
253 EXPORT_SYMBOL(portals_debug_set_level);
254 EXPORT_SYMBOL(portals_nid2str);
255 EXPORT_SYMBOL(portals_id2str);