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