Whamcloud - gitweb
b=5684
[fs/lustre-release.git] / lnet / libcfs / linux / linux-tracefile.c
1 #define DEBUG_SUBSYSTEM S_PORTALS
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 extern union trace_data_union trace_data[NR_CPUS];
14 extern char *tracefile;
15 extern long long tracefile_size;
16 extern struct rw_semaphore tracefile_sem;
17
18 inline struct trace_cpu_data *
19 __trace_get_tcd(unsigned long *flags) 
20 {
21         struct trace_cpu_data *ret;           
22
23         int cpu = get_cpu();                
24         local_irq_save(*flags);               
25         ret = &trace_data[cpu].tcd;     
26
27         return ret;                             
28 }
29
30 inline void 
31 trace_put_tcd (struct trace_cpu_data *tcd, unsigned long flags)
32 {
33         local_irq_restore(flags); 
34         put_cpu();               
35 }
36
37 void
38 set_ptldebug_header(struct ptldebug_header *header, int subsys, int mask, 
39                     const int line, unsigned long stack)
40
41         struct timeval tv; 
42         
43         do_gettimeofday(&tv); 
44         
45         header->ph_subsys = subsys; 
46         header->ph_mask = mask; 
47         header->ph_cpu_id = smp_processor_id(); 
48         header->ph_sec = (__u32)tv.tv_sec; 
49         header->ph_usec = tv.tv_usec; 
50         header->ph_stack = stack; 
51         header->ph_pid = current->pid; 
52         header->ph_line_num = line; 
53 #if defined(__arch_um__) && (LINUX_VERSION_CODE < KERNEL_VERSION(2,4,20)) 
54         header->ph_extern_pid = current->thread.extern_pid;
55 #elif defined(__arch_um__) && (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)) 
56         header->ph_extern_pid = current->thread.mode.tt.extern_pid;
57 #else 
58         header->ph_extern_pid = 0;
59 #endif
60         return;
61 }
62
63 void print_to_console(struct ptldebug_header *hdr, int mask, char *buf, 
64                              int len, char *file, const char *fn)
65
66         char *prefix = NULL, *ptype = NULL; 
67         
68         if ((mask & D_EMERG) != 0) { 
69                 prefix = "LustreError"; 
70                 ptype = KERN_EMERG; 
71         } else if ((mask & D_ERROR) != 0) { 
72                 prefix = "LustreError"; 
73                 ptype = KERN_ERR; 
74         } else if ((mask & D_WARNING) != 0) { 
75                 prefix = "Lustre"; 
76                 ptype = KERN_WARNING; 
77         } else if (portal_printk != 0 || (mask & D_CONSOLE)) {
78                 prefix = "Lustre"; 
79                 ptype = KERN_INFO; 
80         } 
81
82         if ((mask & D_CONSOLE) != 0) {
83                 printk("%s%s: %.*s", ptype, prefix, len, buf);
84         } else {
85                 printk("%s%s: %d:%d:(%s:%d:%s()) %.*s", ptype, prefix, hdr->ph_pid, 
86                        hdr->ph_extern_pid, file, hdr->ph_line_num, fn, len, buf);
87         }
88         return;
89 }
90
91 int trace_write_daemon_file(struct file *file, const char *buffer, 
92                             unsigned long count, void *data)
93
94         char *name; 
95         unsigned long off; 
96         int rc; 
97         
98         name = kmalloc(count + 1, GFP_KERNEL); 
99         if (name == NULL) 
100                 return -ENOMEM; 
101         
102         if (copy_from_user(name, buffer, count)) { 
103                 rc = -EFAULT; 
104                 goto out; 
105         } 
106         
107         /* be nice and strip out trailing '\n' */ 
108         for (off = count ; off > 2 && isspace(name[off - 1]); off--) 
109                 ; 
110         
111         name[off] = '\0'; 
112         
113         down_write(&tracefile_sem); 
114         if (strcmp(name, "stop") == 0) { 
115                 tracefile = NULL; 
116                 trace_stop_thread(); 
117                 goto out_sem; 
118         } else if (strncmp(name, "size=", 5) == 0) { 
119                 tracefile_size = simple_strtoul(name + 5, NULL, 0); 
120                 if (tracefile_size < 10 || tracefile_size > 20480) 
121                         tracefile_size = TRACEFILE_SIZE; 
122                 else 
123                         tracefile_size <<= 20; 
124                 goto out_sem; 
125         } 
126         
127         if (name[0] != '/') { 
128                 rc = -EINVAL; 
129                 goto out_sem; 
130         } 
131         
132         if (tracefile != NULL) 
133                 kfree(tracefile); 
134         
135         tracefile = name; 
136         name = NULL; 
137         printk(KERN_INFO "Lustre: debug daemon will attempt to start writing " 
138                "to %s (%lukB max)\n", tracefile, (long)(tracefile_size >> 10)); 
139         
140         trace_start_thread(); 
141 out_sem: 
142         up_write(&tracefile_sem); 
143 out: 
144         kfree(name);
145         return count;
146 }
147
148 int trace_read_daemon_file(char *page, char **start, off_t off, int count, 
149                            int *eof, void *data)
150
151         int rc; 
152         
153         down_read(&tracefile_sem); 
154         rc = snprintf(page, count, "%s", tracefile); 
155         up_read(&tracefile_sem); 
156
157         return rc;
158 }
159
160 int trace_write_debug_mb(struct file *file, const char *buffer, 
161                          unsigned long count, void *data)
162
163         char string[32]; 
164         int i; 
165         unsigned max; 
166         
167         if (count >= sizeof(string)) { 
168                 printk(KERN_ERR "Lustre: value too large (length %lu bytes)\n", 
169                        count); 
170                 return -EOVERFLOW; 
171         } 
172         
173         if (copy_from_user(string, buffer, count)) 
174                 return -EFAULT; 
175         
176         max = simple_strtoul(string, NULL, 0); 
177         if (max == 0) 
178                 return -EINVAL;
179
180         if (max > (num_physpages >> (20 - 2 - PAGE_SHIFT)) / 5 || max >= 512) { 
181                 printk(KERN_ERR "Lustre: Refusing to set debug buffer size to " 
182                        "%dMB, which is more than 80%% of available RAM (%lu)\n", 
183                        max, (num_physpages >> (20 - 2 - PAGE_SHIFT)) / 5); 
184                 return -EINVAL; 
185         } 
186
187         max /= smp_num_cpus; 
188         
189         for (i = 0; i < NR_CPUS; i++) { 
190                 struct trace_cpu_data *tcd; 
191                 tcd = &trace_data[i].tcd; 
192                 tcd->tcd_max_pages = max << (20 - PAGE_SHIFT); 
193         } 
194         return count;
195 }
196
197 int trace_read_debug_mb(char *page, char **start, off_t off, int count,
198                                         int *eof, void *data)
199
200         struct trace_cpu_data *tcd; 
201         unsigned long flags; 
202         int rc;
203                                         
204         tcd = trace_get_tcd(flags); 
205         rc = snprintf(page, count, "%lu\n", 
206                       (tcd->tcd_max_pages >> (20 - PAGE_SHIFT)) * smp_num_cpus); 
207         trace_put_tcd(tcd, flags); 
208         return rc;
209 }
210