xref: /csrg-svn/sys/kern/subr_prof.c (revision 41966)
1 /*
2  * Copyright (c) 1982, 1986 Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  *
7  *	@(#)subr_prof.c	7.7 (Berkeley) 05/15/90
8  */
9 
10 #ifdef GPROF
11 #include "gprof.h"
12 #include "param.h"
13 #include "systm.h"
14 #include "malloc.h"
15 
16 /*
17  * Froms is actually a bunch of unsigned shorts indexing tos
18  */
19 int	profiling = 3;
20 u_short	*froms;
21 struct	tostruct *tos = 0;
22 long	tolimit = 0;
23 #if defined(vax)
24 char	*s_lowpc = (char *)0x80000000;
25 #endif
26 #if defined(tahoe)
27 char	*s_lowpc = (char *)0xc0000000;
28 #endif
29 #if defined(hp300)
30 char	*s_lowpc = (char *)0x00000000;
31 #endif
32 extern	char etext;
33 char	*s_highpc = &etext;
34 u_long	s_textsize = 0;
35 int	ssiz;
36 u_short	*sbuf;
37 u_short	*kcount;
38 
39 kmstartup()
40 {
41 	u_long fromssize, tossize;
42 
43 	/*
44 	 * Round lowpc and highpc to multiples of the density we're using
45 	 * so the rest of the scaling (here and in gprof) stays in ints.
46 	 */
47 	s_lowpc = (char *)
48 	    ROUNDDOWN((unsigned)s_lowpc, HISTFRACTION*sizeof (HISTCOUNTER));
49 	s_highpc = (char *)
50 	    ROUNDUP((unsigned)s_highpc, HISTFRACTION*sizeof (HISTCOUNTER));
51 	s_textsize = s_highpc - s_lowpc;
52 	printf("Profiling kernel, s_textsize=%d [%x..%x]\n",
53 		s_textsize, s_lowpc, s_highpc);
54 	ssiz = (s_textsize / HISTFRACTION) + sizeof (struct phdr);
55 	sbuf = (u_short *)malloc(ssiz, M_GPROF, M_WAITOK);
56 	if (sbuf == 0) {
57 		printf("No space for monitor buffer(s)\n");
58 		return;
59 	}
60 	bzero(sbuf, ssiz);
61 	fromssize = s_textsize / HASHFRACTION;
62 	froms = (u_short *)malloc(fromssize, M_GPROF, M_WAITOK);
63 	if (froms == 0) {
64 		printf("No space for monitor buffer(s)\n");
65 		free(sbuf, M_GPROF);
66 		sbuf = 0;
67 		return;
68 	}
69 	bzero(froms, fromssize);
70 	tolimit = s_textsize * ARCDENSITY / 100;
71 	if (tolimit < MINARCS)
72 		tolimit = MINARCS;
73 	else if (tolimit > (0xffff - 1))
74 		tolimit = 0xffff - 1;
75 	tossize = tolimit * sizeof (struct tostruct);
76 	tos = (struct tostruct *)malloc(tossize, M_GPROF, M_WAITOK);
77 	if (tos == 0) {
78 		printf("No space for monitor buffer(s)\n");
79 		free(sbuf, M_GPROF), sbuf = 0;
80 		free(froms, M_GPROF), froms = 0;
81 		return;
82 	}
83 	bzero(tos, tossize);
84 	tos[0].link = 0;
85 	((struct phdr *)sbuf)->lpc = s_lowpc;
86 	((struct phdr *)sbuf)->hpc = s_highpc;
87 	((struct phdr *)sbuf)->ncnt = ssiz;
88 	kcount = (u_short *)(((int)sbuf) + sizeof (struct phdr));
89 }
90 
91 /*
92  * Special, non-profiled versions
93  */
94 #if defined(hp300)
95 #define splhigh	_splhigh
96 #define splx	_splx
97 #endif
98 
99 /*
100  * This routine is massaged so that it may be jsb'ed to on vax.
101  */
102 asm(".text");
103 asm("#the beginning of mcount()");
104 asm(".data");
105 mcount()
106 {
107 	register char *selfpc;			/* r11 => r5 */
108 	register u_short *frompcindex;		/* r10 => r4 */
109 	register struct tostruct *top;		/* r9  => r3 */
110 	register struct tostruct *prevtop;	/* r8  => r2 */
111 	register long toindex;			/* r7  => r1 */
112 	static int s;
113 
114 	asm("	.text");		/* make sure we're in text space */
115 	/*
116 	 * Check that we are profiling.
117 	 */
118 	if (profiling)
119 		goto out;
120 	/*
121 	 * Find the return address for mcount,
122 	 * and the return address for mcount's caller.
123 	 */
124 #ifdef lint
125 	selfpc = (char *)0;
126 	frompcindex = 0;
127 #else
128 	;				/* avoid label botch */
129 #ifdef __GNUC__
130 #if defined(vax)
131 	Fix Me!!
132 #endif
133 #if defined(tahoe)
134 	Fix Me!!
135 #endif
136 #if defined(hp300)
137 	/*
138 	 * selfpc = pc pushed by mcount jsr,
139 	 * frompcindex = pc pushed by jsr into self.
140 	 * In GCC the caller's stack frame has already been built so we
141 	 * have to chase a6 to find caller's raddr.  This assumes that all
142 	 * routines we are profiling were built with GCC and that all
143 	 * profiled routines use link/unlk.
144 	 */
145 	asm("movl a6@(4),%0" : "=r" (selfpc));
146 	asm("movl a6@(0)@(4),%0" : "=r" (frompcindex));
147 #endif
148 #else
149 #if defined(vax)
150 	asm("	movl (sp), r11");	/* selfpc = ... (jsb frame) */
151 	asm("	movl 16(fp), r10");	/* frompcindex =     (calls frame) */
152 #endif
153 #if defined(tahoe)
154 	asm("	movl -8(fp),r12");	/* selfpc = callf frame */
155 	asm("	movl (fp),r11");
156 	asm("	movl -8(r11),r11");	/* frompcindex = 1 callf frame back */
157 #endif
158 #if defined(hp300)
159 	asm("	.text");		/* make sure we're in text space */
160 	asm("	movl a6@(4),a5");	/* selfpc = pc pushed by mcount jsr */
161 	asm("	movl a6@(8),a4");	/* frompcindex = pc pushed by jsr into
162 					   self, stack frame not yet built */
163 #endif
164 #endif /* not __GNUC__ */
165 #endif /* not lint */
166 	/*
167 	 * Insure that we cannot be recursively invoked.
168 	 * this requires that splhigh() and splx() below
169 	 * do NOT call mcount!
170 	 */
171 	s = splhigh();
172 	/*
173 	 * Check that frompcindex is a reasonable pc value.
174 	 * For example:	signal catchers get called from the stack,
175 	 *	not from text space.  too bad.
176 	 */
177 	frompcindex = (u_short *)((long)frompcindex - (long)s_lowpc);
178 	if ((u_long)frompcindex > s_textsize)
179 		goto done;
180 	frompcindex =
181 	    &froms[((long)frompcindex) / (HASHFRACTION * sizeof (*froms))];
182 	toindex = *frompcindex;
183 	if (toindex == 0) {
184 		/*
185 		 * First time traversing this arc
186 		 */
187 		toindex = ++tos[0].link;
188 		if (toindex >= tolimit)
189 			goto overflow;
190 		*frompcindex = toindex;
191 		top = &tos[toindex];
192 		top->selfpc = selfpc;
193 		top->count = 1;
194 		top->link = 0;
195 		goto done;
196 	}
197 	top = &tos[toindex];
198 	if (top->selfpc == selfpc) {
199 		/*
200 		 * Arc at front of chain; usual case.
201 		 */
202 		top->count++;
203 		goto done;
204 	}
205 	/*
206 	 * Have to go looking down chain for it.
207 	 * Top points to what we are looking at,
208 	 * prevtop points to previous top.
209 	 * We know it is not at the head of the chain.
210 	 */
211 	for (; /* goto done */; ) {
212 		if (top->link == 0) {
213 			/*
214 			 * Top is end of the chain and none of the chain
215 			 * had top->selfpc == selfpc.
216 			 * So we allocate a new tostruct
217 			 * and link it to the head of the chain.
218 			 */
219 			toindex = ++tos[0].link;
220 			if (toindex >= tolimit)
221 				goto overflow;
222 			top = &tos[toindex];
223 			top->selfpc = selfpc;
224 			top->count = 1;
225 			top->link = *frompcindex;
226 			*frompcindex = toindex;
227 			goto done;
228 		}
229 		/*
230 		 * Otherwise, check the next arc on the chain.
231 		 */
232 		prevtop = top;
233 		top = &tos[top->link];
234 		if (top->selfpc == selfpc) {
235 			/*
236 			 * There it is, increment its count and
237 			 * move it to the head of the chain.
238 			 */
239 			top->count++;
240 			toindex = prevtop->link;
241 			prevtop->link = top->link;
242 			top->link = *frompcindex;
243 			*frompcindex = toindex;
244 			goto done;
245 		}
246 
247 	}
248 done:
249 	splx(s);
250 	/* and fall through */
251 out:
252 #if defined(vax)
253 	asm("	rsb");
254 #endif
255 	return;
256 overflow:
257 	profiling = 3;
258 	printf("mcount: tos overflow\n");
259 	goto out;
260 }
261 asm(".text");
262 asm("#the end of mcount()");
263 asm(".data");
264 #endif
265