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