subr_prof.c revision 1.24 1 /* $NetBSD: subr_prof.c,v 1.24 2001/10/29 07:02:30 simonb 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/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/proc.h>
42 #include <sys/user.h>
43 #include <sys/mount.h>
44 #include <sys/syscallargs.h>
45 #include <sys/sysctl.h>
46
47 #include <machine/cpu.h>
48
49 #ifdef GPROF
50 #include <sys/malloc.h>
51 #include <sys/gmon.h>
52
53 /*
54 * Froms is actually a bunch of unsigned shorts indexing tos
55 */
56 struct gmonparam _gmonparam = { GMON_PROF_OFF };
57
58 /* Actual start of the kernel text segment. */
59 extern char kernel_text[];
60
61 extern char etext[];
62
63
64 void
65 kmstartup()
66 {
67 char *cp;
68 struct gmonparam *p = &_gmonparam;
69 /*
70 * Round lowpc and highpc to multiples of the density we're using
71 * so the rest of the scaling (here and in gprof) stays in ints.
72 */
73 p->lowpc = ROUNDDOWN(((u_long)kernel_text),
74 HISTFRACTION * sizeof(HISTCOUNTER));
75 p->highpc = ROUNDUP((u_long)etext,
76 HISTFRACTION * sizeof(HISTCOUNTER));
77 p->textsize = p->highpc - p->lowpc;
78 printf("Profiling kernel, textsize=%ld [%lx..%lx]\n",
79 p->textsize, p->lowpc, p->highpc);
80 p->kcountsize = p->textsize / HISTFRACTION;
81 p->hashfraction = HASHFRACTION;
82 p->fromssize = p->textsize / HASHFRACTION;
83 p->tolimit = p->textsize * ARCDENSITY / 100;
84 if (p->tolimit < MINARCS)
85 p->tolimit = MINARCS;
86 else if (p->tolimit > MAXARCS)
87 p->tolimit = MAXARCS;
88 p->tossize = p->tolimit * sizeof(struct tostruct);
89 cp = (char *)malloc(p->kcountsize + p->fromssize + p->tossize,
90 M_GPROF, M_NOWAIT);
91 if (cp == 0) {
92 printf("No memory for profiling.\n");
93 return;
94 }
95 memset(cp, 0, p->kcountsize + p->tossize + p->fromssize);
96 p->tos = (struct tostruct *)cp;
97 cp += p->tossize;
98 p->kcount = (u_short *)cp;
99 cp += p->kcountsize;
100 p->froms = (u_short *)cp;
101 }
102
103 /*
104 * Return kernel profiling information.
105 */
106 int
107 sysctl_doprof(name, namelen, oldp, oldlenp, newp, newlen)
108 int *name;
109 u_int namelen;
110 void *oldp;
111 size_t *oldlenp;
112 void *newp;
113 size_t newlen;
114 {
115 struct gmonparam *gp = &_gmonparam;
116 int error;
117
118 /* all sysctl names at this level are terminal */
119 if (namelen != 1)
120 return (ENOTDIR); /* overloaded */
121
122 /* Check we got the necessary memory at startup. */
123 if (gp->kcount == NULL)
124 return (EOPNOTSUPP);
125
126 switch (name[0]) {
127 case GPROF_STATE:
128 error = sysctl_int(oldp, oldlenp, newp, newlen, &gp->state);
129 if (error)
130 return (error);
131 if (gp->state == GMON_PROF_OFF)
132 stopprofclock(&proc0);
133 else
134 startprofclock(&proc0);
135 return (0);
136 case GPROF_COUNT:
137 return (sysctl_struct(oldp, oldlenp, newp, newlen,
138 gp->kcount, gp->kcountsize));
139 case GPROF_FROMS:
140 return (sysctl_struct(oldp, oldlenp, newp, newlen,
141 gp->froms, gp->fromssize));
142 case GPROF_TOS:
143 return (sysctl_struct(oldp, oldlenp, newp, newlen,
144 gp->tos, gp->tossize));
145 case GPROF_GMONPARAM:
146 return (sysctl_rdstruct(oldp, oldlenp, newp, gp, sizeof(*gp)));
147 default:
148 return (EOPNOTSUPP);
149 }
150 /* NOTREACHED */
151 }
152 #endif /* GPROF */
153
154 /*
155 * Profiling system call.
156 *
157 * The scale factor is a fixed point number with 16 bits of fraction, so that
158 * 1.0 is represented as 0x10000. A scale factor of 0 turns off profiling.
159 */
160 /* ARGSUSED */
161 int
162 sys_profil(p, v, retval)
163 struct proc *p;
164 void *v;
165 register_t *retval;
166 {
167 struct sys_profil_args /* {
168 syscallarg(caddr_t) samples;
169 syscallarg(u_int) size;
170 syscallarg(u_int) offset;
171 syscallarg(u_int) scale;
172 } */ *uap = v;
173 struct uprof *upp;
174 int s;
175
176 if (SCARG(uap, scale) > (1 << 16))
177 return (EINVAL);
178 if (SCARG(uap, scale) == 0) {
179 stopprofclock(p);
180 return (0);
181 }
182 upp = &p->p_stats->p_prof;
183
184 /* Block profile interrupts while changing state. */
185 s = splstatclock();
186 upp->pr_off = SCARG(uap, offset);
187 upp->pr_scale = SCARG(uap, scale);
188 upp->pr_base = SCARG(uap, samples);
189 upp->pr_size = SCARG(uap, size);
190 startprofclock(p);
191 splx(s);
192
193 return (0);
194 }
195
196 /*
197 * Scale is a fixed-point number with the binary point 16 bits
198 * into the value, and is <= 1.0. pc is at most 32 bits, so the
199 * intermediate result is at most 48 bits.
200 */
201 #define PC_TO_INDEX(pc, prof) \
202 ((int)(((u_quad_t)((pc) - (prof)->pr_off) * \
203 (u_quad_t)((prof)->pr_scale)) >> 16) & ~1)
204
205 /*
206 * Collect user-level profiling statistics; called on a profiling tick,
207 * when a process is running in user-mode. This routine may be called
208 * from an interrupt context. We try to update the user profiling buffers
209 * cheaply with fuswintr() and suswintr(). If that fails, we revert to
210 * an AST that will vector us to trap() with a context in which copyin
211 * and copyout will work. Trap will then call addupc_task().
212 *
213 * Note that we may (rarely) not get around to the AST soon enough, and
214 * lose profile ticks when the next tick overwrites this one, but in this
215 * case the system is overloaded and the profile is probably already
216 * inaccurate.
217 */
218 void
219 addupc_intr(p, pc)
220 struct proc *p;
221 u_long pc;
222 {
223 struct uprof *prof;
224 caddr_t addr;
225 u_int i;
226 int v;
227
228 prof = &p->p_stats->p_prof;
229 if (pc < prof->pr_off ||
230 (i = PC_TO_INDEX(pc, prof)) >= prof->pr_size)
231 return; /* out of range; ignore */
232
233 addr = prof->pr_base + i;
234 if ((v = fuswintr(addr)) == -1 || suswintr(addr, v + 1) == -1) {
235 prof->pr_addr = pc;
236 prof->pr_ticks++;
237 need_proftick(p);
238 }
239 }
240
241 /*
242 * Much like before, but we can afford to take faults here. If the
243 * update fails, we simply turn off profiling.
244 */
245 void
246 addupc_task(p, pc, ticks)
247 struct proc *p;
248 u_long pc;
249 u_int ticks;
250 {
251 struct uprof *prof;
252 caddr_t addr;
253 u_int i;
254 u_short v;
255
256 /* Testing P_PROFIL may be unnecessary, but is certainly safe. */
257 if ((p->p_flag & P_PROFIL) == 0 || ticks == 0)
258 return;
259
260 prof = &p->p_stats->p_prof;
261 if (pc < prof->pr_off ||
262 (i = PC_TO_INDEX(pc, prof)) >= prof->pr_size)
263 return;
264
265 addr = prof->pr_base + i;
266 if (copyin(addr, (caddr_t)&v, sizeof(v)) == 0) {
267 v += ticks;
268 if (copyout((caddr_t)&v, addr, sizeof(v)) == 0)
269 return;
270 }
271 stopprofclock(p);
272 }
273