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