Whamcloud - gitweb
Branch HEAD
[fs/lustre-release.git] / libcfs / libcfs / linux / linux-tracefile.c
1 #define DEBUG_SUBSYSTEM S_LNET
2 #define LUSTRE_TRACEFILE_PRIVATE
3
4 #include <libcfs/libcfs.h>
5 #include "tracefile.h"
6
7 #ifndef get_cpu
8 #define get_cpu() smp_processor_id()
9 #define put_cpu() do { } while (0)
10 #endif
11
12 /* three types of trace_data in linux */
13 enum {
14         TCD_TYPE_PROC = 0,
15         TCD_TYPE_SOFTIRQ,
16         TCD_TYPE_IRQ,
17         TCD_TYPE_MAX
18 };
19
20 /* percents to share the total debug memory for each type */
21 static unsigned int pages_factor[TCD_TYPE_MAX] = {
22         80,  /* 80% pages for TCD_TYPE_PROC */
23         10,  /* 10% pages for TCD_TYPE_SOFTIRQ */
24         10   /* 10% pages for TCD_TYPE_IRQ */
25 };
26
27 char *trace_console_buffers[NR_CPUS][3];
28
29 struct rw_semaphore tracefile_sem;
30
31 int tracefile_init_arch()
32 {
33         int    i;
34         int    j;
35         struct trace_cpu_data *tcd;
36
37         init_rwsem(&tracefile_sem);
38
39         /* initialize trace_data */
40         memset(trace_data, 0, sizeof(trace_data));
41         for (i = 0; i < TCD_TYPE_MAX; i++) {
42                 trace_data[i]=kmalloc(sizeof(union trace_data_union)*NR_CPUS,
43                                                           GFP_KERNEL);
44                 if (trace_data[i] == NULL)
45                         goto out;
46
47         }
48
49         /* arch related info initialized */
50         tcd_for_each(tcd, i, j) {
51                 tcd->tcd_pages_factor = pages_factor[i];
52                 tcd->tcd_type = i;
53                 tcd->tcd_cpu = j;
54         }
55
56         for (i = 0; i < num_possible_cpus(); i++)
57                 for (j = 0; j < 3; j++) {
58                         trace_console_buffers[i][j] =
59                                 kmalloc(TRACE_CONSOLE_BUFFER_SIZE,
60                                         GFP_KERNEL);
61
62                         if (trace_console_buffers[i][j] == NULL)
63                                 goto out;
64                 }
65
66         return 0;
67
68 out:
69         tracefile_fini_arch();
70         printk(KERN_ERR "lnet: No enough memory\n");
71         return -ENOMEM;
72
73 }
74
75 void tracefile_fini_arch()
76 {
77         int    i;
78         int    j;
79
80         for (i = 0; i < num_possible_cpus(); i++)
81                 for (j = 0; j < 3; j++)
82                         if (trace_console_buffers[i][j] != NULL) {
83                                 kfree(trace_console_buffers[i][j]);
84                                 trace_console_buffers[i][j] = NULL;
85                         }
86
87         for (i = 0; trace_data[i] != NULL; i++) {
88                 kfree(trace_data[i]);
89                 trace_data[i] = NULL;
90         }
91 }
92
93 void tracefile_read_lock()
94 {
95         down_read(&tracefile_sem);
96 }
97
98 void tracefile_read_unlock()
99 {
100         up_read(&tracefile_sem);
101 }
102
103 void tracefile_write_lock()
104 {
105         down_write(&tracefile_sem);
106 }
107
108 void tracefile_write_unlock()
109 {
110         up_write(&tracefile_sem);
111 }
112
113 char *
114 trace_get_console_buffer(void)
115 {
116         int  cpu = get_cpu();
117         int  idx;
118
119         if (in_irq()) {
120                 idx = 0;
121         } else if (in_softirq()) {
122                 idx = 1;
123         } else {
124                 idx = 2;
125         }
126
127         return trace_console_buffers[cpu][idx];
128 }
129
130 void
131 trace_put_console_buffer(char *buffer)
132 {
133         put_cpu();
134 }
135
136 struct trace_cpu_data *
137 trace_get_tcd(void)
138 {
139         int cpu;
140
141         cpu = get_cpu();
142         if (in_irq())
143                 return &(*trace_data[TCD_TYPE_IRQ])[cpu].tcd;
144         else if (in_softirq())
145                 return &(*trace_data[TCD_TYPE_SOFTIRQ])[cpu].tcd;
146         return &(*trace_data[TCD_TYPE_PROC])[cpu].tcd;
147 }
148
149 void
150 trace_put_tcd (struct trace_cpu_data *tcd)
151 {
152         put_cpu();
153 }
154
155 int trace_lock_tcd(struct trace_cpu_data *tcd)
156 {
157         __LASSERT(tcd->tcd_type < TCD_TYPE_MAX);
158         if (tcd->tcd_type == TCD_TYPE_IRQ)
159                 local_irq_disable();
160         else if (tcd->tcd_type == TCD_TYPE_SOFTIRQ)
161                 local_bh_disable();
162         return 1;
163 }
164
165 void trace_unlock_tcd(struct trace_cpu_data *tcd)
166 {
167         __LASSERT(tcd->tcd_type < TCD_TYPE_MAX);
168         if (tcd->tcd_type == TCD_TYPE_IRQ)
169                 local_irq_enable();
170         else if (tcd->tcd_type == TCD_TYPE_SOFTIRQ)
171                 local_bh_enable();
172 }
173
174 int tcd_owns_tage(struct trace_cpu_data *tcd, struct trace_page *tage)
175 {
176         /*
177          * XXX nikita: do NOT call portals_debug_msg() (CDEBUG/ENTRY/EXIT)
178          * from here: this will lead to infinite recursion.
179          */
180         return tcd->tcd_cpu == tage->cpu;
181 }
182
183 void
184 set_ptldebug_header(struct ptldebug_header *header, int subsys, int mask,
185                     const int line, unsigned long stack)
186 {
187         struct timeval tv;
188
189         do_gettimeofday(&tv);
190
191         header->ph_subsys = subsys;
192         header->ph_mask = mask;
193         header->ph_cpu_id = smp_processor_id();
194         header->ph_sec = (__u32)tv.tv_sec;
195         header->ph_usec = tv.tv_usec;
196         header->ph_stack = stack;
197         header->ph_pid = current->pid;
198         header->ph_line_num = line;
199 #if defined(__arch_um__) && (LINUX_VERSION_CODE < KERNEL_VERSION(2,4,20))
200         header->ph_extern_pid = current->thread.extern_pid;
201 #elif defined(__arch_um__) && (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
202         header->ph_extern_pid = current->thread.mode.tt.extern_pid;
203 #else
204         header->ph_extern_pid = 0;
205 #endif
206         return;
207 }
208
209 void print_to_console(struct ptldebug_header *hdr, int mask, const char *buf,
210                              int len, const char *file, const char *fn)
211 {
212         char *prefix = "Lustre", *ptype = NULL;
213
214         if ((mask & D_EMERG) != 0) {
215                 prefix = "LustreError";
216                 ptype = KERN_EMERG;
217         } else if ((mask & D_ERROR) != 0) {
218                 prefix = "LustreError";
219                 ptype = KERN_ERR;
220         } else if ((mask & D_WARNING) != 0) {
221                 prefix = "Lustre";
222                 ptype = KERN_WARNING;
223         } else if ((mask & (D_CONSOLE | libcfs_printk)) != 0) {
224                 prefix = "Lustre";
225                 ptype = KERN_INFO;
226         }
227
228         if ((mask & D_CONSOLE) != 0) {
229                 printk("%s%s: %.*s", ptype, prefix, len, buf);
230         } else {
231                 printk("%s%s: %d:%d:(%s:%d:%s()) %.*s", ptype, prefix, hdr->ph_pid,
232                        hdr->ph_extern_pid, file, hdr->ph_line_num, fn, len, buf);
233         }
234         return;
235 }
236
237 int trace_max_debug_mb(void)
238 {
239         int  total_mb = (num_physpages >> (20 - CFS_PAGE_SHIFT));
240         
241         return MAX(512, (total_mb * 80)/100);
242 }
243
244 void
245 trace_call_on_all_cpus(void (*fn)(void *arg), void *arg)
246 {
247         cpumask_t cpus_allowed = current->cpus_allowed;
248         /* use cpus_allowed to quiet 2.4 UP kernel warning only */
249         cpumask_t m = cpus_allowed;
250         int       cpu;
251
252         /* Run the given routine on every CPU in thread context */
253         for (cpu = 0; cpu < num_possible_cpus(); cpu++) {
254                 if (!cpu_online(cpu))
255                         continue;
256
257                 cpus_clear(m);
258                 cpu_set(cpu, m);
259                 set_cpus_allowed(current, m);
260
261                 fn(arg);
262
263                 set_cpus_allowed(current, cpus_allowed);
264         }
265 }