subr_prof.c revision 1.22.2.3 1 /* $NetBSD: subr_prof.c,v 1.22.2.3 2001/11/14 19:16:41 nathanw Exp $ */
2
3 /*-
4 * Copyright (c) 1982, 1986, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * @(#)subr_prof.c 8.4 (Berkeley) 2/14/95
36 */
37
38 #include <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: subr_prof.c,v 1.22.2.3 2001/11/14 19:16:41 nathanw Exp $");
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/lwp.h>
45 #include <sys/proc.h>
46 #include <sys/user.h>
47 #include <sys/mount.h>
48 #include <sys/syscallargs.h>
49 #include <sys/sysctl.h>
50
51 #include <machine/cpu.h>
52
53 #ifdef GPROF
54 #include <sys/malloc.h>
55 #include <sys/gmon.h>
56
57 /*
58 * Froms is actually a bunch of unsigned shorts indexing tos
59 */
60 struct gmonparam _gmonparam = { GMON_PROF_OFF };
61
62 /* Actual start of the kernel text segment. */
63 extern char kernel_text[];
64
65 extern char etext[];
66
67
68 void
69 kmstartup()
70 {
71 char *cp;
72 struct gmonparam *p = &_gmonparam;
73 /*
74 * Round lowpc and highpc to multiples of the density we're using
75 * so the rest of the scaling (here and in gprof) stays in ints.
76 */
77 p->lowpc = ROUNDDOWN(((u_long)kernel_text),
78 HISTFRACTION * sizeof(HISTCOUNTER));
79 p->highpc = ROUNDUP((u_long)etext,
80 HISTFRACTION * sizeof(HISTCOUNTER));
81 p->textsize = p->highpc - p->lowpc;
82 printf("Profiling kernel, textsize=%ld [%lx..%lx]\n",
83 p->textsize, p->lowpc, p->highpc);
84 p->kcountsize = p->textsize / HISTFRACTION;
85 p->hashfraction = HASHFRACTION;
86 p->fromssize = p->textsize / HASHFRACTION;
87 p->tolimit = p->textsize * ARCDENSITY / 100;
88 if (p->tolimit < MINARCS)
89 p->tolimit = MINARCS;
90 else if (p->tolimit > MAXARCS)
91 p->tolimit = MAXARCS;
92 p->tossize = p->tolimit * sizeof(struct tostruct);
93 cp = (char *)malloc(p->kcountsize + p->fromssize + p->tossize,
94 M_GPROF, M_NOWAIT);
95 if (cp == 0) {
96 printf("No memory for profiling.\n");
97 return;
98 }
99 memset(cp, 0, p->kcountsize + p->tossize + p->fromssize);
100 p->tos = (struct tostruct *)cp;
101 cp += p->tossize;
102 p->kcount = (u_short *)cp;
103 cp += p->kcountsize;
104 p->froms = (u_short *)cp;
105 }
106
107 /*
108 * Return kernel profiling information.
109 */
110 int
111 sysctl_doprof(name, namelen, oldp, oldlenp, newp, newlen)
112 int *name;
113 u_int namelen;
114 void *oldp;
115 size_t *oldlenp;
116 void *newp;
117 size_t newlen;
118 {
119 struct gmonparam *gp = &_gmonparam;
120 int error;
121
122 /* all sysctl names at this level are terminal */
123 if (namelen != 1)
124 return (ENOTDIR); /* overloaded */
125
126 /* Check we got the necessary memory at startup. */
127 if (gp->kcount == NULL)
128 return (EOPNOTSUPP);
129
130 switch (name[0]) {
131 case GPROF_STATE:
132 error = sysctl_int(oldp, oldlenp, newp, newlen, &gp->state);
133 if (error)
134 return (error);
135 if (gp->state == GMON_PROF_OFF)
136 stopprofclock(&proc0);
137 else
138 startprofclock(&proc0);
139 return (0);
140 case GPROF_COUNT:
141 return (sysctl_struct(oldp, oldlenp, newp, newlen,
142 gp->kcount, gp->kcountsize));
143 case GPROF_FROMS:
144 return (sysctl_struct(oldp, oldlenp, newp, newlen,
145 gp->froms, gp->fromssize));
146 case GPROF_TOS:
147 return (sysctl_struct(oldp, oldlenp, newp, newlen,
148 gp->tos, gp->tossize));
149 case GPROF_GMONPARAM:
150 return (sysctl_rdstruct(oldp, oldlenp, newp, gp, sizeof(*gp)));
151 default:
152 return (EOPNOTSUPP);
153 }
154 /* NOTREACHED */
155 }
156 #endif /* GPROF */
157
158 /*
159 * Profiling system call.
160 *
161 * The scale factor is a fixed point number with 16 bits of fraction, so that
162 * 1.0 is represented as 0x10000. A scale factor of 0 turns off profiling.
163 */
164 /* ARGSUSED */
165 int
166 sys_profil(l, v, retval)
167 struct lwp *l;
168 void *v;
169 register_t *retval;
170 {
171 struct sys_profil_args /* {
172 syscallarg(caddr_t) samples;
173 syscallarg(u_int) size;
174 syscallarg(u_int) offset;
175 syscallarg(u_int) scale;
176 } */ *uap = v;
177 struct proc *p = l->l_proc;
178 struct uprof *upp;
179 int s;
180
181 if (SCARG(uap, scale) > (1 << 16))
182 return (EINVAL);
183 if (SCARG(uap, scale) == 0) {
184 stopprofclock(p);
185 return (0);
186 }
187 upp = &p->p_stats->p_prof;
188
189 /* Block profile interrupts while changing state. */
190 s = splstatclock();
191 upp->pr_off = SCARG(uap, offset);
192 upp->pr_scale = SCARG(uap, scale);
193 upp->pr_base = SCARG(uap, samples);
194 upp->pr_size = SCARG(uap, size);
195 startprofclock(p);
196 splx(s);
197
198 return (0);
199 }
200
201 /*
202 * Scale is a fixed-point number with the binary point 16 bits
203 * into the value, and is <= 1.0. pc is at most 32 bits, so the
204 * intermediate result is at most 48 bits.
205 */
206 #define PC_TO_INDEX(pc, prof) \
207 ((int)(((u_quad_t)((pc) - (prof)->pr_off) * \
208 (u_quad_t)((prof)->pr_scale)) >> 16) & ~1)
209
210 /*
211 * Collect user-level profiling statistics; called on a profiling tick,
212 * when a process is running in user-mode. This routine may be called
213 * from an interrupt context. We try to update the user profiling buffers
214 * cheaply with fuswintr() and suswintr(). If that fails, we revert to
215 * an AST that will vector us to trap() with a context in which copyin
216 * and copyout will work. Trap will then call addupc_task().
217 *
218 * Note that we may (rarely) not get around to the AST soon enough, and
219 * lose profile ticks when the next tick overwrites this one, but in this
220 * case the system is overloaded and the profile is probably already
221 * inaccurate.
222 */
223 void
224 addupc_intr(p, pc)
225 struct proc *p;
226 u_long pc;
227 {
228 struct uprof *prof;
229 caddr_t addr;
230 u_int i;
231 int v;
232
233 prof = &p->p_stats->p_prof;
234 if (pc < prof->pr_off ||
235 (i = PC_TO_INDEX(pc, prof)) >= prof->pr_size)
236 return; /* out of range; ignore */
237
238 addr = prof->pr_base + i;
239 if ((v = fuswintr(addr)) == -1 || suswintr(addr, v + 1) == -1) {
240 prof->pr_addr = pc;
241 prof->pr_ticks++;
242 need_proftick(p);
243 }
244 }
245
246 /*
247 * Much like before, but we can afford to take faults here. If the
248 * update fails, we simply turn off profiling.
249 */
250 void
251 addupc_task(p, pc, ticks)
252 struct proc *p;
253 u_long pc;
254 u_int ticks;
255 {
256 struct uprof *prof;
257 caddr_t addr;
258 u_int i;
259 u_short v;
260
261 /* Testing P_PROFIL may be unnecessary, but is certainly safe. */
262 if ((p->p_flag & P_PROFIL) == 0 || ticks == 0)
263 return;
264
265 prof = &p->p_stats->p_prof;
266 if (pc < prof->pr_off ||
267 (i = PC_TO_INDEX(pc, prof)) >= prof->pr_size)
268 return;
269
270 addr = prof->pr_base + i;
271 if (copyin(addr, (caddr_t)&v, sizeof(v)) == 0) {
272 v += ticks;
273 if (copyout((caddr_t)&v, addr, sizeof(v)) == 0)
274 return;
275 }
276 stopprofclock(p);
277 }
278