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