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