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