Whamcloud - gitweb
Land b_smallfix onto HEAD (20040423_1603)
[fs/lustre-release.git] / lnet / libcfs / proc.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Copyright (C) 2001, 2002 Cluster File Systems, Inc.
5  *   Author: Zach Brown <zab@zabbo.net>
6  *   Author: Peter J. Braam <braam@clusterfs.com>
7  *   Author: Phil Schwan <phil@clusterfs.com>
8  *
9  *   This file is part of Lustre, http://www.lustre.org.
10  *
11  *   Lustre is free software; you can redistribute it and/or
12  *   modify it under the terms of version 2 of the GNU General Public
13  *   License as published by the Free Software Foundation.
14  *
15  *   Lustre is distributed in the hope that it will be useful,
16  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *   GNU General Public License for more details.
19  *
20  *   You should have received a copy of the GNU General Public License
21  *   along with Lustre; if not, write to the Free Software
22  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  */
24
25 #ifndef EXPORT_SYMTAB
26 # define EXPORT_SYMTAB
27 #endif
28
29 #include <linux/config.h>
30 #include <linux/module.h>
31 #include <linux/kernel.h>
32 #include <linux/mm.h>
33 #include <linux/string.h>
34 #include <linux/stat.h>
35 #include <linux/errno.h>
36 #include <linux/smp_lock.h>
37 #include <linux/unistd.h>
38 #include <net/sock.h>
39 #include <linux/uio.h>
40
41 #include <asm/system.h>
42 #include <asm/uaccess.h>
43
44 #include <linux/fs.h>
45 #include <linux/file.h>
46 #include <linux/stat.h>
47 #include <linux/list.h>
48 #include <asm/uaccess.h>
49 #include <asm/segment.h>
50
51 #include <linux/proc_fs.h>
52 #include <linux/sysctl.h>
53
54 # define DEBUG_SUBSYSTEM S_PORTALS
55
56 #include <linux/kp30.h>
57 #include <asm/div64.h>
58
59 static struct ctl_table_header *portals_table_header = NULL;
60 extern char debug_file_path[1024];
61 extern char debug_daemon_file_path[1024];
62 extern char portals_upcall[1024];
63
64 #define PSDEV_PORTALS  (0x100)
65 enum {
66         PSDEV_DEBUG = 1,          /* control debugging */
67         PSDEV_SUBSYSTEM_DEBUG,    /* control debugging */
68         PSDEV_PRINTK,             /* force all errors to console */
69         PSDEV_CONSOLE,            /* allow _any_ messages to console */
70         PSDEV_DEBUG_PATH,         /* crashdump log location */
71         PSDEV_DEBUG_DUMP_PATH,    /* crashdump tracelog location */
72         PSDEV_PORTALS_UPCALL,     /* User mode upcall script  */
73         PSDEV_PORTALS_MEMUSED,    /* bytes currently PORTAL_ALLOCated */
74 };
75
76 static struct ctl_table portals_table[] = {
77         {PSDEV_DEBUG, "debug", &portal_debug, sizeof(int), 0644, NULL,
78          &proc_dointvec},
79         {PSDEV_SUBSYSTEM_DEBUG, "subsystem_debug", &portal_subsystem_debug,
80          sizeof(int), 0644, NULL, &proc_dointvec},
81         {PSDEV_PRINTK, "printk", &portal_printk, sizeof(int), 0644, NULL,
82          &proc_dointvec},
83         {PSDEV_CONSOLE, "console", &portal_cerror, sizeof(int), 0644, NULL,
84          &proc_dointvec},
85         {PSDEV_DEBUG_PATH, "debug_path", debug_file_path,
86          sizeof(debug_file_path), 0644, NULL, &proc_dostring, &sysctl_string},
87         {PSDEV_DEBUG_DUMP_PATH, "debug_daemon_path", debug_daemon_file_path,
88          sizeof(debug_daemon_file_path), 0644, NULL, &proc_dostring,
89          &sysctl_string},
90         {PSDEV_PORTALS_UPCALL, "upcall", portals_upcall,
91          sizeof(portals_upcall), 0644, NULL, &proc_dostring,
92          &sysctl_string},
93         {PSDEV_PORTALS_MEMUSED, "memused", (int *)&portal_kmemory.counter,
94          sizeof(int), 0644, NULL, &proc_dointvec},
95         {0}
96 };
97
98 static struct ctl_table top_table[2] = {
99         {PSDEV_PORTALS, "portals", NULL, 0, 0555, portals_table},
100         {0}
101 };
102
103
104 #ifdef PORTALS_PROFILING
105 /*
106  * profiling stuff.  we do this statically for now 'cause its simple,
107  * but we could do some tricks with elf sections to have this array
108  * automatically built.
109  */
110 #define def_prof(FOO) [PROF__##FOO] = {#FOO, 0, }
111
112 struct prof_ent prof_ents[] = {
113         def_prof(our_recvmsg),
114         def_prof(our_sendmsg),
115         def_prof(socknal_recv),
116         def_prof(lib_parse),
117         def_prof(conn_list_walk),
118         def_prof(memcpy),
119         def_prof(lib_finalize),
120         def_prof(pingcli_time),
121         def_prof(gmnal_send),
122         def_prof(gmnal_recv),
123 };
124
125 EXPORT_SYMBOL(prof_ents);
126
127 /*
128  * this function is as crazy as the proc filling api
129  * requires.
130  *
131  * buffer: page allocated for us to scribble in.  the
132  *  data returned to the user will be taken from here.
133  * *start: address of the pointer that will tell the 
134  *  caller where in buffer the data the user wants is.
135  * ppos: offset in the entire /proc file that the user
136  *  currently wants.
137  * wanted: the amount of data the user wants.
138  *
139  * while going, 'curpos' is the offset in the entire
140  * file where we currently are.  We only actually
141  * start filling buffer when we get to a place in
142  * the file that the user cares about.
143  *
144  * we take care to only sprintf when the user cares because
145  * we're holding a lock while we do this.
146  *
147  * we're smart and know that we generate fixed size lines.
148  * we only start writing to the buffer when the user cares.
149  * This is unpredictable because we don't snapshot the
150  * list between calls that are filling in a file from
151  * the list.  The list could change mid read and the
152  * output will look very weird indeed.  oh well.
153  */
154
155 static int prof_read_proc(char *buffer, char **start, off_t ppos, int wanted,
156                           int *eof, void *data)
157 {
158         int len = 0, i;
159         int curpos;
160         char *header = "Interval        Cycles_per (Starts Finishes Total)\n";
161         int header_len = strlen(header);
162         char *format = "%-15s %.12Ld (%.12d %.12d %.12Ld)";
163         int line_len = (15 + 1 + 12 + 2 + 12 + 1 + 12 + 1 + 12 + 1);
164
165         *start = buffer;
166
167         if (ppos < header_len) {
168                 int diff = MIN(header_len, wanted);
169                 memcpy(buffer, header + ppos, diff);
170                 len += diff;
171                 ppos += diff;
172         }
173
174         if (len >= wanted)
175                 goto out;
176
177         curpos = header_len;
178
179         for ( i = 0; i < MAX_PROFS ; i++) {
180                 int copied;
181                 struct prof_ent *pe = &prof_ents[i];
182                 long long cycles_per;
183                 /*
184                  * find the part of the array that the buffer wants
185                  */
186                 if (ppos >= (curpos + line_len))  {
187                         curpos += line_len;
188                         continue;
189                 }
190                 /* the clever caller split a line */
191                 if (ppos > curpos) {
192                         *start = buffer + (ppos - curpos);
193                 }
194
195                 if (pe->finishes == 0)
196                         cycles_per = 0;
197                 else
198                 {
199                         cycles_per = pe->total_cycles;
200                         do_div (cycles_per, pe->finishes);
201                 }
202
203                 copied = sprintf(buffer + len, format, pe->str, cycles_per,
204                                  pe->starts, pe->finishes, pe->total_cycles);
205
206                 len += copied;
207
208                 /* pad to line len, -1 for \n */
209                 if ((copied < line_len-1)) {
210                         int diff = (line_len-1) - copied;
211                         memset(buffer + len, ' ', diff);
212                         len += diff;
213                         copied += diff;
214                 }
215
216                 buffer[len++]= '\n';
217
218                 /* bail if we have enough */
219                 if (((buffer + len) - *start) >= wanted)
220                         break;
221
222                 curpos += line_len;
223         }
224
225         /* lameness */
226         if (i == MAX_PROFS)
227                 *eof = 1;
228  out:
229
230         return MIN(((buffer + len) - *start), wanted);
231 }
232
233 /*
234  * all kids love /proc :/
235  */
236 static unsigned char basedir[]="net/portals";
237 #endif /* PORTALS_PROFILING */
238
239 int insert_proc(void)
240 {
241 #if PORTALS_PROFILING
242         unsigned char dir[128];
243         struct proc_dir_entry *ent;
244
245         if (ARRAY_SIZE(prof_ents) != MAX_PROFS) {
246                 CERROR("profiling enum and array are out of sync.\n");
247                 return -1;
248         }
249
250         /*
251          * This is pretty lame.  assuming that failure just
252          * means that they already existed.
253          */
254         strcat(dir, basedir);
255         create_proc_entry(dir, S_IFDIR, 0);
256
257         strcat(dir, "/cycles");
258         ent = create_proc_entry(dir, 0, 0);
259         if (!ent) {
260                 CERROR("couldn't register %s?\n", dir);
261                 return -1;
262         }
263
264         ent->data = NULL;
265         ent->read_proc = prof_read_proc;
266 #endif /* PORTALS_PROFILING */
267
268 #ifdef CONFIG_SYSCTL
269         if (!portals_table_header)
270                 portals_table_header = register_sysctl_table(top_table, 0);
271 #endif
272
273         return 0;
274 }
275
276 void remove_proc(void)
277 {
278 #if PORTALS_PROFILING
279         unsigned char dir[128];
280         int end;
281
282         dir[0]='\0';
283         strcat(dir, basedir);
284
285         end = strlen(dir);
286
287         strcat(dir, "/cycles");
288         remove_proc_entry(dir,0);
289
290         dir[end] = '\0';
291         remove_proc_entry(dir,0);
292 #endif /* PORTALS_PROFILING */
293
294 #ifdef CONFIG_SYSCTL
295         if (portals_table_header)
296                 unregister_sysctl_table(portals_table_header);
297         portals_table_header = NULL;
298 #endif
299 }