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