xref: /csrg-svn/sys/kern/subr_prof.c (revision 17094)
1 /*	subr_prof.c	6.4	84/08/29	*/
2 
3 /* last integrated from: gmon.c	4.10 (Berkeley) 1/14/83 */
4 
5 #ifdef GPROF
6 #include "gprof.h"
7 #include "param.h"
8 #include "systm.h"
9 
10 /*
11  * Froms is actually a bunch of unsigned shorts indexing tos
12  */
13 int profiling = 3;
14 u_short *froms;
15 struct tostruct *tos = 0;
16 long tolimit = 0;
17 #ifdef vax
18 char	*s_lowpc = (char *)0x80000000;
19 #endif
20 extern char etext;
21 char *s_highpc = &etext;
22 u_long	s_textsize = 0;
23 int ssiz;
24 u_short	*sbuf;
25 u_short	*kcount;
26 
27 kmstartup()
28 {
29 	u_long	fromssize, tossize;
30 
31 	/*
32 	 *	round lowpc and highpc to multiples of the density we're using
33 	 *	so the rest of the scaling (here and in gprof) stays in ints.
34 	 */
35 	s_lowpc = (char *)
36 	    ROUNDDOWN((unsigned)s_lowpc, HISTFRACTION*sizeof(HISTCOUNTER));
37 	s_highpc = (char *)
38 	    ROUNDUP((unsigned)s_highpc, HISTFRACTION*sizeof(HISTCOUNTER));
39 	s_textsize = s_highpc - s_lowpc;
40 	printf("Profiling kernel, s_textsize=%d [%x..%x]\n",
41 		s_textsize, s_lowpc, s_highpc);
42 	ssiz = (s_textsize / HISTFRACTION) + sizeof(struct phdr);
43 	sbuf = (u_short *)calloc(ssiz);
44 	if (sbuf == 0) {
45 		printf("No space for monitor buffer(s)\n");
46 		return;
47 	}
48 	blkclr((caddr_t)sbuf, ssiz);
49 	fromssize = s_textsize / HASHFRACTION;
50 	froms = (u_short *)calloc(fromssize);
51 	if (froms == 0) {
52 		printf("No space for monitor buffer(s)\n");
53 		cfreemem(sbuf, ssiz);
54 		sbuf = 0;
55 		return;
56 	}
57 	blkclr((caddr_t)froms, fromssize);
58 	tolimit = s_textsize * ARCDENSITY / 100;
59 	if (tolimit < MINARCS) {
60 		tolimit = MINARCS;
61 	} else if (tolimit > 65534) {
62 		tolimit = 65534;
63 	}
64 	tossize = tolimit * sizeof(struct tostruct);
65 	tos = (struct tostruct *)calloc(tossize);
66 	if (tos == 0) {
67 		printf("No space for monitor buffer(s)\n");
68 		cfreemem(sbuf, ssiz);
69 		sbuf = 0;
70 		cfreemem(froms, fromssize);
71 		froms = 0;
72 		return;
73 	}
74 	blkclr((caddr_t)tos, tossize);
75 	tos[0].link = 0;
76 	((struct phdr *)sbuf)->lpc = s_lowpc;
77 	((struct phdr *)sbuf)->hpc = s_highpc;
78 	((struct phdr *)sbuf)->ncnt = ssiz;
79 	kcount = (u_short *)(((int)sbuf) + sizeof(struct phdr));
80 #ifdef notdef
81 	/*
82 	 *	profiling is what mcount checks to see if
83 	 *	all the data structures are ready!!!
84 	 */
85 	profiling = 0;		/* patch by hand when you're ready */
86 #endif
87 }
88 
89 #ifdef vax
90 /*
91  * This routine is massaged so that it may be jsb'ed to
92  */
93 asm(".text");
94 asm("#the beginning of mcount()");
95 asm(".data");
96 mcount()
97 {
98 	register char			*selfpc;	/* r11 => r5 */
99 	register unsigned short		*frompcindex;	/* r10 => r4 */
100 	register struct tostruct	*top;		/* r9  => r3 */
101 	register struct tostruct	*prevtop;	/* r8  => r2 */
102 	register long			toindex;	/* r7  => r1 */
103 	static int s;
104 
105 #ifdef lint
106 	selfpc = (char *)0;
107 	frompcindex = 0;
108 #else not lint
109 	/*
110 	 *	find the return address for mcount,
111 	 *	and the return address for mcount's caller.
112 	 */
113 	asm("	.text");		/* make sure we're in text space */
114 	asm("	movl (sp), r11");	/* selfpc = ... (jsb frame) */
115 	asm("	movl 16(fp), r10");	/* frompcindex =     (calls frame) */
116 #endif not lint
117 	/*
118 	 *	check that we are profiling
119 	 */
120 	if (profiling) {
121 		goto out;
122 	}
123 	/*
124 	 *	insure that we cannot be recursively invoked.
125 	 *	this requires that splhigh() and splx() below
126 	 *	do NOT call mcount!
127 	 */
128 	s = splhigh();
129 	/*
130 	 *	check that frompcindex is a reasonable pc value.
131 	 *	for example:	signal catchers get called from the stack,
132 	 *			not from text space.  too bad.
133 	 */
134 	frompcindex = (unsigned short *)((long)frompcindex - (long)s_lowpc);
135 	if ((unsigned long)frompcindex > s_textsize) {
136 		goto done;
137 	}
138 	frompcindex =
139 	    &froms[((long)frompcindex) / (HASHFRACTION * sizeof(*froms))];
140 	toindex = *frompcindex;
141 	if (toindex == 0) {
142 		/*
143 		 *	first time traversing this arc
144 		 */
145 		toindex = ++tos[0].link;
146 		if (toindex >= tolimit) {
147 			goto overflow;
148 		}
149 		*frompcindex = toindex;
150 		top = &tos[toindex];
151 		top->selfpc = selfpc;
152 		top->count = 1;
153 		top->link = 0;
154 		goto done;
155 	}
156 	top = &tos[toindex];
157 	if (top->selfpc == selfpc) {
158 		/*
159 		 *	arc at front of chain; usual case.
160 		 */
161 		top->count++;
162 		goto done;
163 	}
164 	/*
165 	 *	have to go looking down chain for it.
166 	 *	top points to what we are looking at,
167 	 *	prevtop points to previous top.
168 	 *	we know it is not at the head of the chain.
169 	 */
170 	for (; /* goto done */; ) {
171 		if (top->link == 0) {
172 			/*
173 			 *	top is end of the chain and none of the chain
174 			 *	had top->selfpc == selfpc.
175 			 *	so we allocate a new tostruct
176 			 *	and link it to the head of the chain.
177 			 */
178 			toindex = ++tos[0].link;
179 			if (toindex >= tolimit) {
180 				goto overflow;
181 			}
182 			top = &tos[toindex];
183 			top->selfpc = selfpc;
184 			top->count = 1;
185 			top->link = *frompcindex;
186 			*frompcindex = toindex;
187 			goto done;
188 		}
189 		/*
190 		 *	otherwise, check the next arc on the chain.
191 		 */
192 		prevtop = top;
193 		top = &tos[top->link];
194 		if (top->selfpc == selfpc) {
195 			/*
196 			 *	there it is.
197 			 *	increment its count
198 			 *	move it to the head of the chain.
199 			 */
200 			top->count++;
201 			toindex = prevtop->link;
202 			prevtop->link = top->link;
203 			top->link = *frompcindex;
204 			*frompcindex = toindex;
205 			goto done;
206 		}
207 
208 	}
209 done:
210 	splx(s);
211 	/* and fall through */
212 out:
213 	asm("	rsb");
214 
215 overflow:
216 	profiling = 3;
217 	printf("mcount: tos overflow\n");
218 	goto out;
219 }
220 asm(".text");
221 asm("#the end of mcount()");
222 asm(".data");
223 #endif vax
224 #endif GPROF
225