xref: /original-bsd/lib/libc/gmon/gmon.c (revision cde01d6c)
1 /*-
2  * Copyright (c) 1983, 1992 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #if !defined(lint) && defined(LIBC_SCCS)
9 static char sccsid[] = "@(#)gmon.c	5.12 (Berkeley) 07/14/92";
10 #endif
11 
12 #include <unistd.h>
13 #include <sys/types.h>
14 #include <sys/time.h>
15 #include <sys/kinfo.h>
16 
17 #ifdef DEBUG
18 #include <stdio.h>
19 #include <fcntl.h>
20 #endif
21 
22 #include <sys/gmon.h>
23 
24 extern char *minbrk asm ("minbrk");
25 
26 struct gmonparam _gmonparam = { GMON_PROF_OFF };
27 
28 static int	ssiz;
29 static char	*sbuf;
30 static int	s_scale;
31 /* see profil(2) where this is describe (incorrectly) */
32 #define		SCALE_1_TO_1	0x10000L
33 
34 #define ERR(s) write(2, s, sizeof(s))
35 
36 static struct gmonhdr gmonhdr;
37 
38 monstartup(lowpc, highpc)
39 	u_long lowpc;
40 	u_long highpc;
41 {
42 	register int o;
43 	struct clockinfo clockinfo;
44 	int tsize, fsize, size;
45 	char *cp;
46 	struct gmonhdr *hdr;
47 	struct gmonparam *p = &_gmonparam;
48 
49 	/*
50 	 * round lowpc and highpc to multiples of the density we're using
51 	 * so the rest of the scaling (here and in gprof) stays in ints.
52 	 */
53 	lowpc = ROUNDDOWN(lowpc, HISTFRACTION * sizeof(HISTCOUNTER));
54 	p->lowpc = lowpc;
55 	highpc = ROUNDUP(highpc, HISTFRACTION * sizeof(HISTCOUNTER));
56 	p->highpc = highpc;
57 	p->textsize = highpc - lowpc;
58 	ssiz = p->textsize / HISTFRACTION;
59 	fsize = p->textsize / HASHFRACTION;
60 	tsize = p->textsize * ARCDENSITY / 100;
61 	if (tsize < MINARCS)
62 		tsize = MINARCS;
63 	else if (tsize > MAXARCS)
64 		tsize = MAXARCS;
65 	p->tolimit = tsize;
66 	tsize *= sizeof(struct tostruct);
67 
68 	cp = sbrk(ssiz + fsize + tsize);
69 	if (cp == (char *)-1) {
70 		ERR("monstartup: out of memory\n");
71 		return;
72 	}
73 #ifdef notdef
74 	bzero(cp, ssiz + fsize + tsize);
75 #endif
76 	p->tos = (struct tostruct *)cp;
77 	cp += tsize;
78 	sbuf = cp;
79 	cp += ssiz;
80 	p->froms = (u_short *)cp;
81 
82 	minbrk = sbrk(0);
83 	p->tos[0].link = 0;
84 
85 	o = highpc - lowpc;
86 	if (ssiz < o) {
87 #ifndef hp300
88 		s_scale = ((float)ssiz / o ) * SCALE_1_TO_1;
89 #else /* avoid floating point */
90 		int quot = o / ssiz;
91 
92 		if (quot >= 0x10000)
93 			s_scale = 1;
94 		else if (quot >= 0x100)
95 			s_scale = 0x10000 / quot;
96 		else if (o >= 0x800000)
97 			s_scale = 0x1000000 / (o / (ssiz >> 8));
98 		else
99 			s_scale = 0x1000000 / ((o << 8) / ssiz);
100 #endif
101 	} else
102 		s_scale = SCALE_1_TO_1;
103 
104 	moncontrol(1);
105 	size = sizeof(clockinfo);
106 	if (getkerninfo(KINFO_CLOCKRATE, &clockinfo, &size, 0) < 0)
107 		/*
108 		 * Best guess
109 		 */
110 		clockinfo.profhz = hertz();
111 	else if (clockinfo.profhz == 0) {
112 		if (clockinfo.hz != 0)
113 			clockinfo.profhz = clockinfo.hz;
114 		else
115 			clockinfo.profhz = hertz();
116 	}
117 	hdr = (struct gmonhdr *)&gmonhdr;
118 	hdr->lpc = lowpc;
119 	hdr->hpc = highpc;
120 	hdr->ncnt = ssiz + sizeof(gmonhdr);
121 	hdr->version = GMONVERSION;
122 	hdr->profrate = clockinfo.profhz;
123 }
124 
125 _mcleanup()
126 {
127 	int fd;
128 	int fromindex;
129 	int endfrom;
130 	u_long frompc;
131 	int toindex;
132 	struct rawarc rawarc;
133 	struct gmonparam *p = &_gmonparam;
134 	int log, len;
135 	char buf[200];
136 
137 	if (p->state == GMON_PROF_ERROR)
138 		ERR("_mcleanup: tos overflow\n");
139 
140 	moncontrol(0);
141 	fd = creat("gmon.out", 0666);
142 	if (fd < 0) {
143 		perror("mcount: gmon.out");
144 		return;
145 	}
146 #ifdef DEBUG
147 	log = open("gmon.log", O_CREAT|O_TRUNC|O_WRONLY, 0664);
148 	if (log < 0) {
149 		perror("mcount: gmon.log");
150 		return;
151 	}
152 	len = sprintf(buf, "[mcleanup1] sbuf 0x%x ssiz %d\n", sbuf, ssiz);
153 	write(log, buf, len);
154 #endif
155 	write(fd, (char *)&gmonhdr, sizeof(gmonhdr));
156 	write(fd, sbuf, ssiz);
157 	endfrom = p->textsize / (HASHFRACTION * sizeof(*p->froms));
158 	for (fromindex = 0; fromindex < endfrom; fromindex++) {
159 		if (p->froms[fromindex] == 0)
160 			continue;
161 
162 		frompc = p->lowpc;
163 		frompc += fromindex * HASHFRACTION * sizeof(*p->froms);
164 		for (toindex = p->froms[fromindex]; toindex != 0;
165 		     toindex = p->tos[toindex].link) {
166 #ifdef DEBUG
167 			len = sprintf(buf,
168 			"[mcleanup2] frompc 0x%x selfpc 0x%x count %d\n" ,
169 				frompc, p->tos[toindex].selfpc,
170 				p->tos[toindex].count);
171 			write(log, buf, len);
172 #endif
173 			rawarc.raw_frompc = frompc;
174 			rawarc.raw_selfpc = p->tos[toindex].selfpc;
175 			rawarc.raw_count = p->tos[toindex].count;
176 			write(fd, &rawarc, sizeof rawarc);
177 		}
178 	}
179 	close(fd);
180 }
181 
182 /*
183  * Control profiling
184  *	profiling is what mcount checks to see if
185  *	all the data structures are ready.
186  */
187 moncontrol(mode)
188 	int mode;
189 {
190 	struct gmonparam *p = &_gmonparam;
191 
192 	if (mode) {
193 		/* start */
194 		profil(sbuf, ssiz, (int)p->lowpc, s_scale);
195 		p->state = GMON_PROF_ON;
196 	} else {
197 		/* stop */
198 		profil((char *)0, 0, 0, 0);
199 		p->state = GMON_PROF_OFF;
200 	}
201 }
202 
203 /*
204  * discover the tick frequency of the machine
205  * if something goes wrong, we return 0, an impossible hertz.
206  */
207 hertz()
208 {
209 	struct itimerval tim;
210 
211 	tim.it_interval.tv_sec = 0;
212 	tim.it_interval.tv_usec = 1;
213 	tim.it_value.tv_sec = 0;
214 	tim.it_value.tv_usec = 0;
215 	setitimer(ITIMER_REAL, &tim, 0);
216 	setitimer(ITIMER_REAL, 0, &tim);
217 	if (tim.it_interval.tv_usec < 2)
218 		return(0);
219 	return (1000000 / tim.it_interval.tv_usec);
220 }
221 
222 
223