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