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