Whamcloud - gitweb
smash the HEAD with the contents of b_cmd. HEAD_PRE_CMD_SMASH and
[fs/lustre-release.git] / lustre / portals / 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 #define PSDEV_DEBUG           1   /* control debugging */
66 #define PSDEV_SUBSYSTEM_DEBUG 2   /* control debugging */
67 #define PSDEV_PRINTK          3   /* force all errors to console */
68 #define PSDEV_CONSOLE         4   /* allow _any_ messages to console */
69 #define PSDEV_DEBUG_PATH      5   /* crashdump log location */
70 #define PSDEV_DEBUG_DUMP_PATH 6   /* crashdump tracelog location */
71 #define PSDEV_PORTALS_UPCALL  7   /* User mode upcall script  */
72
73 #define PORTALS_PRIMARY_CTLCNT 7
74 static struct ctl_table portals_table[PORTALS_PRIMARY_CTLCNT + 1] = {
75         {PSDEV_DEBUG, "debug", &portal_debug, sizeof(int), 0644, NULL,
76          &proc_dointvec},
77         {PSDEV_SUBSYSTEM_DEBUG, "subsystem_debug", &portal_subsystem_debug,
78          sizeof(int), 0644, NULL, &proc_dointvec},
79         {PSDEV_PRINTK, "printk", &portal_printk, sizeof(int), 0644, NULL,
80          &proc_dointvec},
81         {PSDEV_CONSOLE, "console", &portal_cerror, sizeof(int), 0644, NULL,
82          &proc_dointvec},
83         {PSDEV_DEBUG_PATH, "debug_path", debug_file_path,
84          sizeof(debug_file_path), 0644, NULL, &proc_dostring, &sysctl_string},
85         {PSDEV_DEBUG_DUMP_PATH, "debug_daemon_path", debug_daemon_file_path,
86          sizeof(debug_daemon_file_path), 0644, NULL, &proc_dostring,
87          &sysctl_string},
88         {PSDEV_PORTALS_UPCALL, "upcall", portals_upcall,
89          sizeof(portals_upcall), 0644, NULL, &proc_dostring,
90          &sysctl_string},
91         {0}
92 };
93
94 static struct ctl_table top_table[2] = {
95         {PSDEV_PORTALS, "portals", NULL, 0, 0555, portals_table},
96         {0}
97 };
98
99
100 #ifdef PORTALS_PROFILING
101 /*
102  * profiling stuff.  we do this statically for now 'cause its simple,
103  * but we could do some tricks with elf sections to have this array
104  * automatically built.
105  */
106 #define def_prof(FOO) [PROF__##FOO] = {#FOO, 0, }
107
108 struct prof_ent prof_ents[] = {
109         def_prof(our_recvmsg),
110         def_prof(our_sendmsg),
111         def_prof(socknal_recv),
112         def_prof(lib_parse),
113         def_prof(conn_list_walk),
114         def_prof(memcpy),
115         def_prof(lib_finalize),
116         def_prof(pingcli_time),
117         def_prof(gmnal_send),
118         def_prof(gmnal_recv),
119 };
120
121 EXPORT_SYMBOL(prof_ents);
122
123 /*
124  * this function is as crazy as the proc filling api
125  * requires.
126  *
127  * buffer: page allocated for us to scribble in.  the
128  *  data returned to the user will be taken from here.
129  * *start: address of the pointer that will tell the 
130  *  caller where in buffer the data the user wants is.
131  * ppos: offset in the entire /proc file that the user
132  *  currently wants.
133  * wanted: the amount of data the user wants.
134  *
135  * while going, 'curpos' is the offset in the entire
136  * file where we currently are.  We only actually
137  * start filling buffer when we get to a place in
138  * the file that the user cares about.
139  *
140  * we take care to only sprintf when the user cares because
141  * we're holding a lock while we do this.
142  *
143  * we're smart and know that we generate fixed size lines.
144  * we only start writing to the buffer when the user cares.
145  * This is unpredictable because we don't snapshot the
146  * list between calls that are filling in a file from
147  * the list.  The list could change mid read and the
148  * output will look very weird indeed.  oh well.
149  */
150
151 static int prof_read_proc(char *buffer, char **start, off_t ppos, int wanted,
152                           int *eof, void *data)
153 {
154         int len = 0, i;
155         int curpos;
156         char *header = "Interval        Cycles_per (Starts Finishes Total)\n";
157         int header_len = strlen(header);
158         char *format = "%-15s %.12Ld (%.12d %.12d %.12Ld)";
159         int line_len = (15 + 1 + 12 + 2 + 12 + 1 + 12 + 1 + 12 + 1);
160
161         *start = buffer;
162
163         if (ppos < header_len) {
164                 int diff = MIN(header_len, wanted);
165                 memcpy(buffer, header + ppos, diff);
166                 len += diff;
167                 ppos += diff;
168         }
169
170         if (len >= wanted)
171                 goto out;
172
173         curpos = header_len;
174
175         for ( i = 0; i < MAX_PROFS ; i++) {
176                 int copied;
177                 struct prof_ent *pe = &prof_ents[i];
178                 long long cycles_per;
179                 /*
180                  * find the part of the array that the buffer wants
181                  */
182                 if (ppos >= (curpos + line_len))  {
183                         curpos += line_len;
184                         continue;
185                 }
186                 /* the clever caller split a line */
187                 if (ppos > curpos) {
188                         *start = buffer + (ppos - curpos);
189                 }
190
191                 if (pe->finishes == 0)
192                         cycles_per = 0;
193                 else
194                 {
195                         cycles_per = pe->total_cycles;
196                         do_div (cycles_per, pe->finishes);
197                 }
198
199                 copied = sprintf(buffer + len, format, pe->str, cycles_per,
200                                  pe->starts, pe->finishes, pe->total_cycles);
201
202                 len += copied;
203
204                 /* pad to line len, -1 for \n */
205                 if ((copied < line_len-1)) {
206                         int diff = (line_len-1) - copied;
207                         memset(buffer + len, ' ', diff);
208                         len += diff;
209                         copied += diff;
210                 }
211
212                 buffer[len++]= '\n';
213
214                 /* bail if we have enough */
215                 if (((buffer + len) - *start) >= wanted)
216                         break;
217
218                 curpos += line_len;
219         }
220
221         /* lameness */
222         if (i == MAX_PROFS)
223                 *eof = 1;
224  out:
225
226         return MIN(((buffer + len) - *start), wanted);
227 }
228
229 /*
230  * all kids love /proc :/
231  */
232 static unsigned char basedir[]="net/portals";
233 #endif /* PORTALS_PROFILING */
234
235 int insert_proc(void)
236 {
237 #if PORTALS_PROFILING
238         unsigned char dir[128];
239         struct proc_dir_entry *ent;
240
241         if (ARRAY_SIZE(prof_ents) != MAX_PROFS) {
242                 CERROR("profiling enum and array are out of sync.\n");
243                 return -1;
244         }
245
246         /*
247          * This is pretty lame.  assuming that failure just
248          * means that they already existed.
249          */
250         strcat(dir, basedir);
251         create_proc_entry(dir, S_IFDIR, 0);
252
253         strcat(dir, "/cycles");
254         ent = create_proc_entry(dir, 0, 0);
255         if (!ent) {
256                 CERROR("couldn't register %s?\n", dir);
257                 return -1;
258         }
259
260         ent->data = NULL;
261         ent->read_proc = prof_read_proc;
262 #endif /* PORTALS_PROFILING */
263
264 #ifdef CONFIG_SYSCTL
265         if (!portals_table_header)
266                 portals_table_header = register_sysctl_table(top_table, 0);
267 #endif
268
269         return 0;
270 }
271
272 void remove_proc(void)
273 {
274 #if PORTALS_PROFILING
275         unsigned char dir[128];
276         int end;
277
278         dir[0]='\0';
279         strcat(dir, basedir);
280
281         end = strlen(dir);
282
283         strcat(dir, "/cycles");
284         remove_proc_entry(dir,0);
285
286         dir[end] = '\0';
287         remove_proc_entry(dir,0);
288 #endif /* PORTALS_PROFILING */
289
290 #ifdef CONFIG_SYSCTL
291         if (portals_table_header)
292                 unregister_sysctl_table(portals_table_header);
293         portals_table_header = NULL;
294 #endif
295 }