Whamcloud - gitweb
fd0e43416f95255ca869e05f711262a8cf4e071f
[fs/lustre-release.git] / libcfs / libcfs / winnt / winnt-tracefile.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  */
30 /*
31  * This file is part of Lustre, http://www.lustre.org/
32  * Lustre is a trademark of Sun Microsystems, Inc.
33  */
34
35 #define DEBUG_SUBSYSTEM S_LNET
36 #define LUSTRE_TRACEFILE_PRIVATE
37
38 #include <libcfs/libcfs.h>
39 #include "tracefile.h"
40
41 /* percents to share the total debug memory for each type */
42 static unsigned int pages_factor[CFS_TCD_TYPE_MAX] = {
43         90,  /* 90% pages for CFS_TCD_TYPE_PASSIVE */
44         10   /* 10% pages for CFS_TCD_TYPE_DISPATCH */
45 };
46
47 char *cfs_trace_console_buffers[CFS_NR_CPUS][CFS_TCD_TYPE_MAX];
48
49 cfs_rw_semaphore_t cfs_tracefile_sem;
50
51 int cfs_tracefile_init_arch()
52 {
53         int    i;
54         int    j;
55         struct cfs_trace_cpu_data *tcd;
56
57         cfs_init_rwsem(&cfs_tracefile_sem);
58
59         /* initialize trace_data */
60         memset(cfs_trace_data, 0, sizeof(cfs_trace_data));
61         for (i = 0; i < CFS_TCD_TYPE_MAX; i++) {
62                 cfs_trace_data[i] =
63                         cfs_alloc(sizeof(union cfs_trace_data_union) * \
64                                   CFS_NR_CPUS, GFP_KERNEL);
65                 if (cfs_trace_data[i] == NULL)
66                         goto out;
67         }
68
69         /* arch related info initialized */
70         cfs_tcd_for_each(tcd, i, j) {
71                 tcd->tcd_pages_factor = (USHORT) pages_factor[i];
72                 tcd->tcd_type = (USHORT) i;
73                 tcd->tcd_cpu = (USHORT)j;
74         }
75
76         for (i = 0; i < cfs_num_possible_cpus(); i++)
77                 for (j = 0; j < CFS_TCD_TYPE_MAX; j++) {
78                         cfs_trace_console_buffers[i][j] =
79                                 cfs_alloc(CFS_TRACE_CONSOLE_BUFFER_SIZE,
80                                           GFP_KERNEL);
81
82                         if (cfs_trace_console_buffers[i][j] == NULL)
83                                 goto out;
84                 }
85
86         return 0;
87
88 out:
89         cfs_tracefile_fini_arch();
90         printk(CFS_KERN_ERR "lnet: Not enough memory\n");
91         return -ENOMEM;
92
93 }
94
95 void cfs_tracefile_fini_arch()
96 {
97         int    i;
98         int    j;
99
100         for (i = 0; i < cfs_num_possible_cpus(); i++) {
101                 for (j = 0; j < CFS_TCD_TYPE_MAX; j++) {
102                         if (cfs_trace_console_buffers[i][j] != NULL) {
103                                 cfs_free(cfs_trace_console_buffers[i][j]);
104                                 cfs_trace_console_buffers[i][j] = NULL;
105                         }
106                 }
107         }
108
109         for (i = 0; cfs_trace_data[i] != NULL; i++) {
110                 cfs_free(cfs_trace_data[i]);
111                 cfs_trace_data[i] = NULL;
112         }
113
114         cfs_fini_rwsem(&cfs_tracefile_sem);
115 }
116
117 void cfs_tracefile_read_lock()
118 {
119         cfs_down_read(&cfs_tracefile_sem);
120 }
121
122 void cfs_tracefile_read_unlock()
123 {
124         cfs_up_read(&cfs_tracefile_sem);
125 }
126
127 void cfs_tracefile_write_lock()
128 {
129         cfs_down_write(&cfs_tracefile_sem);
130 }
131
132 void cfs_tracefile_write_unlock()
133 {
134         cfs_up_write(&cfs_tracefile_sem);
135 }
136
137 cfs_trace_buf_type_t cfs_trace_buf_idx_get()
138 {
139         if (KeGetCurrentIrql() >= DISPATCH_LEVEL)
140                 return CFS_TCD_TYPE_DISPATCH;
141         else
142                 return CFS_TCD_TYPE_PASSIVE;
143 }
144
145 int cfs_trace_lock_tcd(struct cfs_trace_cpu_data *tcd, int walking)
146 {
147         __LASSERT(tcd->tcd_type < CFS_TCD_TYPE_MAX);
148         return 1;
149 }
150
151 void cfs_trace_unlock_tcd(struct cfs_trace_cpu_data *tcd, int walking)
152 {
153         __LASSERT(tcd->tcd_type < CFS_TCD_TYPE_MAX);
154 }
155
156 int cfs_tcd_owns_tage(struct cfs_trace_cpu_data *tcd,
157                       struct cfs_trace_page *tage)
158 {
159         /*
160          * XXX nikita: do NOT call portals_debug_msg() (CDEBUG/ENTRY/EXIT)
161          * from here: this will lead to infinite recursion.
162          */
163         return tcd->tcd_cpu == tage->cpu;
164 }
165
166 void
167 cfs_set_ptldebug_header(struct ptldebug_header *header, int subsys, int mask,
168                         const int line, unsigned long stack)
169 {
170         struct timeval tv;
171
172         cfs_gettimeofday(&tv);
173
174         header->ph_subsys = subsys;
175         header->ph_mask = mask;
176         header->ph_cpu_id = cfs_smp_processor_id();
177         header->ph_type = cfs_trace_buf_idx_get();
178         header->ph_sec = (__u32)tv.tv_sec;
179         header->ph_usec = tv.tv_usec;
180         header->ph_stack = stack;
181         header->ph_pid = (__u32)(ULONG_PTR)current->pid;
182         header->ph_line_num = line;
183         header->ph_extern_pid = 0;
184         return;
185 }
186
187 void cfs_print_to_console(struct ptldebug_header *hdr, int mask,
188                           const char *buf, int len, const char *file,
189                           const char *fn)
190 {
191         char *prefix = "Lustre", *ptype = NULL;
192
193         if ((mask & D_EMERG) != 0) {
194                 prefix = "LustreError";
195                 ptype = CFS_KERN_EMERG;
196         } else if ((mask & D_ERROR) != 0) {
197                 prefix = "LustreError";
198                 ptype = CFS_KERN_ERR;
199         } else if ((mask & D_WARNING) != 0) {
200                 prefix = "Lustre";
201                 ptype = CFS_KERN_WARNING;
202         } else if ((mask & (D_CONSOLE | libcfs_printk)) != 0) {
203                 prefix = "Lustre";
204                 ptype = CFS_KERN_INFO;
205         }
206
207         if ((mask & D_CONSOLE) != 0) {
208                 printk("%s%s: %.*s", ptype, prefix, len, buf);
209         } else {
210                 printk("%s%s: %d:%d:(%s:%d:%s()) %.*s", ptype, prefix, hdr->ph_pid,
211                        hdr->ph_extern_pid, file, hdr->ph_line_num, fn, len, buf);
212         }
213         return;
214 }
215
216 int cfs_trace_max_debug_mb(void)
217 {
218         int  total_mb = (cfs_num_physpages >> (20 - CFS_PAGE_SHIFT));
219         
220         return MAX(512, (total_mb * 80)/100);
221 }