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