Whamcloud - gitweb
0c134eead6f5a8e8c3ae2c474adc70a73a1a32f5
[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) { 
78                 prefix = "Lustre"; 
79                 ptype = KERN_INFO; 
80         } 
81         printk("%s%s: %d:%d:(%s:%d:%s()) %.*s", ptype, prefix, hdr->ph_pid, 
82                 hdr->ph_extern_pid, file, hdr->ph_line_num, fn, len, buf);
83         return;
84 }
85
86 int trace_write_daemon_file(struct file *file, const char *buffer, 
87                             unsigned long count, void *data)
88
89         char *name; 
90         unsigned long off; 
91         int rc; 
92         
93         name = kmalloc(count + 1, GFP_KERNEL); 
94         if (name == NULL) 
95                 return -ENOMEM; 
96         
97         if (copy_from_user(name, buffer, count)) { 
98                 rc = -EFAULT; 
99                 goto out; 
100         } 
101         
102         /* be nice and strip out trailing '\n' */ 
103         for (off = count ; off > 2 && isspace(name[off - 1]); off--) 
104                 ; 
105         
106         name[off] = '\0'; 
107         
108         down_write(&tracefile_sem); 
109         if (strcmp(name, "stop") == 0) { 
110                 tracefile = NULL; 
111                 trace_stop_thread(); 
112                 goto out_sem; 
113         } else if (strncmp(name, "size=", 5) == 0) { 
114                 tracefile_size = simple_strtoul(name + 5, NULL, 0); 
115                 if (tracefile_size < 10 || tracefile_size > 20480) 
116                         tracefile_size = TRACEFILE_SIZE; 
117                 else 
118                         tracefile_size <<= 20; 
119                 goto out_sem; 
120         } 
121         
122         if (name[0] != '/') { 
123                 rc = -EINVAL; 
124                 goto out_sem; 
125         } 
126         
127         if (tracefile != NULL) 
128                 kfree(tracefile); 
129         
130         tracefile = name; 
131         name = NULL; 
132         printk(KERN_INFO "Lustre: debug daemon will attempt to start writing " 
133                "to %s (%lukB max)\n", tracefile, (long)(tracefile_size >> 10)); 
134         
135         trace_start_thread(); 
136 out_sem: 
137         up_write(&tracefile_sem); 
138 out: 
139         kfree(name);
140         return count;
141 }
142
143 int trace_read_daemon_file(char *page, char **start, off_t off, int count, 
144                            int *eof, void *data)
145
146         int rc; 
147         
148         down_read(&tracefile_sem); 
149         rc = snprintf(page, count, "%s", tracefile); 
150         up_read(&tracefile_sem); 
151
152         return rc;
153 }
154
155 int trace_write_debug_mb(struct file *file, const char *buffer, 
156                          unsigned long count, void *data)
157
158         char string[32]; 
159         int i; 
160         unsigned max; 
161         
162         if (count >= sizeof(string)) { 
163                 printk(KERN_ERR "Lustre: value too large (length %lu bytes)\n", 
164                        count); 
165                 return -EOVERFLOW; 
166         } 
167         
168         if (copy_from_user(string, buffer, count)) 
169                 return -EFAULT; 
170         
171         max = simple_strtoul(string, NULL, 0); 
172         if (max == 0) 
173                 return -EINVAL;
174
175         if (max > (num_physpages >> (20 - 2 - PAGE_SHIFT)) / 5 || max >= 512) { 
176                 printk(KERN_ERR "Lustre: Refusing to set debug buffer size to " 
177                        "%dMB, which is more than 80%% of available RAM (%lu)\n", 
178                        max, (num_physpages >> (20 - 2 - PAGE_SHIFT)) / 5); 
179                 return -EINVAL; 
180         } 
181
182         max /= smp_num_cpus; 
183         
184         for (i = 0; i < NR_CPUS; i++) { 
185                 struct trace_cpu_data *tcd; 
186                 tcd = &trace_data[i].tcd; 
187                 tcd->tcd_max_pages = max << (20 - PAGE_SHIFT); 
188         } 
189         return count;
190 }
191
192 int trace_read_debug_mb(char *page, char **start, off_t off, int count,
193                                         int *eof, void *data)
194
195         struct trace_cpu_data *tcd; 
196         unsigned long flags; 
197         int rc;
198                                         
199         tcd = trace_get_tcd(flags); 
200         rc = snprintf(page, count, "%lu\n", 
201                       (tcd->tcd_max_pages >> (20 - PAGE_SHIFT)) * smp_num_cpus); 
202         trace_put_tcd(tcd, flags); 
203         return rc;
204 }
205