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