Whamcloud - gitweb
b=5684
[fs/lustre-release.git] / lnet / libcfs / darwin / darwin-tracefile.c
1
2 #define DEBUG_SUBSYSTEM S_PORTALS
3 #define LUSTRE_TRACEFILE_PRIVATE
4 #include <libcfs/libcfs.h>
5 #include <libcfs/kp30.h>
6 #include "tracefile.h"
7
8 /*
9  * We can't support smp tracefile currently.
10  * Everything is put on one cpu.
11  */
12
13 #define M_TCD_MAX_PAGES (128 * 1280)
14 extern union trace_data_union trace_data[NR_CPUS];
15 extern char *tracefile;
16 extern long long tracefile_size;
17 extern struct rw_semaphore tracefile_sem;
18 extern int trace_start_thread(void);
19 extern void trace_stop_thread(void);
20
21 long max_debug_mb = M_TCD_MAX_PAGES;
22 static long max_permit_mb = (64 * 1024);
23
24 inline struct trace_cpu_data *
25 __trace_get_tcd (unsigned long *flags)
26 {
27         return &trace_data[0].tcd;
28 }
29
30 inline void
31 __trace_put_tcd (struct trace_cpu_data *tcd, unsigned long flags)
32 {
33         return;
34 }
35
36 void
37 set_ptldebug_header(struct ptldebug_header *header, int subsys, int mask, 
38                     const int line, unsigned long stack)
39
40         struct timeval tv; 
41         
42         do_gettimeofday(&tv); 
43         header->ph_subsys = subsys; 
44         header->ph_mask = mask; 
45         header->ph_cpu_id = smp_processor_id(); 
46         header->ph_sec = (__u32)tv.tv_sec; 
47         header->ph_usec = tv.tv_usec; 
48         header->ph_stack = stack; 
49         header->ph_pid = 0; 
50         header->ph_line_num = line; 
51         header->ph_extern_pid = 0;
52 }
53
54 void print_to_console(struct ptldebug_header *hdr, int mask, char *buf, 
55                              int len, char *file, const char *fn)
56
57         char *prefix = NULL, *ptype = NULL;
58                         
59         if ((mask & D_EMERG) != 0) { 
60                 prefix = "LustreError"; 
61                 ptype = KERN_EMERG; 
62         } else if ((mask & D_ERROR) != 0) { 
63                 prefix = "LustreError"; 
64                 ptype = KERN_ERR; 
65         } else if ((mask & D_WARNING) != 0) { 
66                 prefix = "Lustre"; 
67                 ptype = KERN_WARNING; 
68         } else if (portal_printk != 0 || (mask & D_CONSOLE)) {
69                 prefix = "Lustre"; 
70                 ptype = KERN_INFO; 
71         } 
72
73         if ((mask & D_CONSOLE) != 0) {
74                 printk("%s%s: %.*s", ptype, prefix, len, buf);
75         } else {
76                 printk("%s%s: %d:%d:(%s:%d:%s()) %*s", ptype, prefix, hdr->ph_pid, 
77                        hdr->ph_extern_pid, file, hdr->ph_line_num, fn, len, buf);
78         }
79 }
80
81 /*
82  * Sysctl handle of libcfs
83  */
84 int cfs_trace_daemon SYSCTL_HANDLER_ARGS
85 {
86         int error = 0;
87         char *name = NULL;
88
89         MALLOC(name, char *, req->newlen + 1, M_TEMP, M_WAITOK | M_ZERO);
90         if (name == NULL)
91                 return -ENOMEM;
92         down_write(&tracefile_sem);
93         error = sysctl_handle_string(oidp, name, req->newlen + 1, req);
94         if (!error || req->newptr != NULL) {
95                 /* write */
96                 if (strcmp(name, "stop") == 0) {
97                         /* stop tracefile daemon */
98                         tracefile = NULL;
99                         trace_stop_thread();
100                         goto out; 
101                 }else if (strncmp(name, "size=", 5) == 0) { 
102                         tracefile_size = simple_strtoul(name + 5, NULL, 0); 
103                         if (tracefile_size < 10 || tracefile_size > 20480) 
104                                 tracefile_size = TRACEFILE_SIZE; 
105                         else 
106                                 tracefile_size <<= 20; 
107                         goto out;
108
109                 }
110                 if (name[0] != '/') { 
111                         error = -EINVAL; 
112                         goto out; 
113                 } 
114                 if (tracefile != NULL) 
115                         cfs_free(tracefile);
116                 tracefile = name; 
117                 name = NULL; 
118                 trace_start_thread();
119         } else if (req->newptr != NULL) {
120                 /* Something was wrong with the write request */
121                 printf("sysctl debug daemon failed: %d.\n", error);
122                 goto out;
123         } else {
124                 /* Read request */
125                 SYSCTL_OUT(req, tracefile, sizeof(tracefile));
126         }
127 out:
128         if (name != NULL) 
129                 FREE(name, M_TEMP);
130         up_write(&tracefile_sem);
131         return error;
132 }
133
134
135 int cfs_debug_mb SYSCTL_HANDLER_ARGS
136 {
137         int i;
138         int error = 0;
139
140         error = sysctl_handle_long(oidp, oidp->oid_arg1, oidp->oid_arg2, req);
141         if (!error && req->newptr != NULL) {
142                 /* We have a new value stored in the standard location */
143                 if (max_debug_mb <= 0)
144                         return -EINVAL;
145                 if (max_debug_mb > max_permit_mb) {
146                         printf("sysctl debug_mb is too big: %d.\n", max_debug_mb);
147                         return 0;
148                 } 
149                 for (i = 0; i < NR_CPUS; i++) { 
150                         struct trace_cpu_data *tcd; 
151                         tcd = &trace_data[i].tcd; 
152                         tcd->tcd_max_pages = max_debug_mb;
153                 }
154         } else if (req->newptr != NULL) {
155                 /* Something was wrong with the write request */
156                 printf ("sysctl debug_mb fault: %d.\n", error);
157         } else {
158                 /* Read request */
159                 error = SYSCTL_OUT(req, &max_debug_mb, sizeof max_debug_mb);
160         }
161         return error;
162 }
163
164