xref: /dragonfly/lib/libc/gmon/gmon.c (revision e1acdbad)
1 /*-
2  * Copyright (c) 1983, 1992, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * $FreeBSD: src/lib/libc/gmon/gmon.c,v 1.8 2000/01/27 23:06:25 jasone Exp $
34  * $DragonFly: src/lib/libc/gmon/gmon.c,v 1.7 2005/03/09 18:52:21 joerg Exp $
35  *
36  * @(#)gmon.c	8.1 (Berkeley) 6/4/93
37  */
38 
39 #include "namespace.h"
40 #include <sys/param.h>
41 #include <sys/time.h>
42 #include <sys/gmon.h>
43 #include <sys/sysctl.h>
44 
45 #include <err.h>
46 #include <errno.h>
47 #include <stdio.h>
48 #include <fcntl.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <unistd.h>
52 #include "un-namespace.h"
53 
54 #if defined(__i386__) || defined(__amd64__)
55 extern char *minbrk asm (".minbrk");
56 #else
57 extern char *minbrk asm ("minbrk");
58 #endif
59 
60 struct gmonparam _gmonparam = { GMON_PROF_OFF };
61 
62 static int	s_scale;
63 /* see profil(2) where this is describe (incorrectly) */
64 #define		SCALE_1_TO_1	0x10000L
65 
66 #define ERR(s) _write(2, s, sizeof(s))
67 
68 void	moncontrol (int);
69 static int hertz (void);
70 
71 void
72 monstartup(lowpc, highpc)
73 	u_long lowpc;
74 	u_long highpc;
75 {
76 	int o;
77 	char *cp;
78 	struct gmonparam *p = &_gmonparam;
79 
80 	/*
81 	 * round lowpc and highpc to multiples of the density we're using
82 	 * so the rest of the scaling (here and in gprof) stays in ints.
83 	 */
84 	p->lowpc = ROUNDDOWN(lowpc, HISTFRACTION * sizeof(HISTCOUNTER));
85 	p->highpc = ROUNDUP(highpc, HISTFRACTION * sizeof(HISTCOUNTER));
86 	p->textsize = p->highpc - p->lowpc;
87 	p->kcountsize = p->textsize / HISTFRACTION;
88 	p->hashfraction = HASHFRACTION;
89 	p->fromssize = p->textsize / HASHFRACTION;
90 	p->tolimit = p->textsize * ARCDENSITY / 100;
91 	if (p->tolimit < MINARCS)
92 		p->tolimit = MINARCS;
93 	else if (p->tolimit > MAXARCS)
94 		p->tolimit = MAXARCS;
95 	p->tossize = p->tolimit * sizeof(struct tostruct);
96 
97 	cp = sbrk(p->kcountsize + p->fromssize + p->tossize);
98 	if (cp == (char *)-1) {
99 		ERR("monstartup: out of memory\n");
100 		return;
101 	}
102 #ifdef notdef
103 	bzero(cp, p->kcountsize + p->fromssize + p->tossize);
104 #endif
105 	p->tos = (struct tostruct *)cp;
106 	cp += p->tossize;
107 	p->kcount = (u_short *)cp;
108 	cp += p->kcountsize;
109 	p->froms = (u_short *)cp;
110 
111 	minbrk = sbrk(0);
112 	p->tos[0].link = 0;
113 
114 	o = p->highpc - p->lowpc;
115 	if (p->kcountsize < o) {
116 #ifndef hp300
117 		s_scale = ((float)p->kcountsize / o ) * SCALE_1_TO_1;
118 #else /* avoid floating point */
119 		int quot = o / p->kcountsize;
120 
121 		if (quot >= 0x10000)
122 			s_scale = 1;
123 		else if (quot >= 0x100)
124 			s_scale = 0x10000 / quot;
125 		else if (o >= 0x800000)
126 			s_scale = 0x1000000 / (o / (p->kcountsize >> 8));
127 		else
128 			s_scale = 0x1000000 / ((o << 8) / p->kcountsize);
129 #endif
130 	} else
131 		s_scale = SCALE_1_TO_1;
132 
133 	moncontrol(1);
134 }
135 
136 void
137 _mcleanup()
138 {
139 	int fd;
140 	int fromindex;
141 	int endfrom;
142 	u_long frompc;
143 	int toindex;
144 	struct rawarc rawarc;
145 	struct gmonparam *p = &_gmonparam;
146 	struct gmonhdr gmonhdr, *hdr;
147 	struct clockinfo clockinfo;
148 	char outname[128];
149 	int mib[2];
150 	size_t size;
151 #ifdef DEBUG
152 	int log, len;
153 	char buf[200];
154 #endif
155 
156 	if (p->state == GMON_PROF_ERROR)
157 		ERR("_mcleanup: tos overflow\n");
158 
159 	size = sizeof(clockinfo);
160 	mib[0] = CTL_KERN;
161 	mib[1] = KERN_CLOCKRATE;
162 	if (sysctl(mib, 2, &clockinfo, &size, NULL, 0) < 0) {
163 		/*
164 		 * Best guess
165 		 */
166 		clockinfo.profhz = hertz();
167 	} else if (clockinfo.profhz == 0) {
168 		if (clockinfo.hz != 0)
169 			clockinfo.profhz = clockinfo.hz;
170 		else
171 			clockinfo.profhz = hertz();
172 	}
173 
174 	moncontrol(0);
175 	snprintf(outname, sizeof(outname), "%s.gmon", getprogname());
176 	fd = _open(outname, O_CREAT|O_TRUNC|O_WRONLY, 0666);
177 	if (fd < 0) {
178 		warnx("_mcleanup: %s - %s",outname,strerror(errno));
179 		return;
180 	}
181 #ifdef DEBUG
182 	log = _open("gmon.log", O_CREAT|O_TRUNC|O_WRONLY, 0664);
183 	if (log < 0) {
184 		perror("_mcleanup: gmon.log");
185 		return;
186 	}
187 	len = sprintf(buf, "[mcleanup1] kcount 0x%x ssiz %d\n",
188 	    p->kcount, p->kcountsize);
189 	_write(log, buf, len);
190 #endif
191 	hdr = (struct gmonhdr *)&gmonhdr;
192 	hdr->lpc = p->lowpc;
193 	hdr->hpc = p->highpc;
194 	hdr->ncnt = p->kcountsize + sizeof(gmonhdr);
195 	hdr->version = GMONVERSION;
196 	hdr->profrate = clockinfo.profhz;
197 	_write(fd, (char *)hdr, sizeof *hdr);
198 	_write(fd, p->kcount, p->kcountsize);
199 	endfrom = p->fromssize / sizeof(*p->froms);
200 	for (fromindex = 0; fromindex < endfrom; fromindex++) {
201 		if (p->froms[fromindex] == 0)
202 			continue;
203 
204 		frompc = p->lowpc;
205 		frompc += fromindex * p->hashfraction * sizeof(*p->froms);
206 		for (toindex = p->froms[fromindex]; toindex != 0;
207 		     toindex = p->tos[toindex].link) {
208 #ifdef DEBUG
209 			len = sprintf(buf,
210 			"[mcleanup2] frompc 0x%x selfpc 0x%x count %d\n" ,
211 				frompc, p->tos[toindex].selfpc,
212 				p->tos[toindex].count);
213 			_write(log, buf, len);
214 #endif
215 			rawarc.raw_frompc = frompc;
216 			rawarc.raw_selfpc = p->tos[toindex].selfpc;
217 			rawarc.raw_count = p->tos[toindex].count;
218 			_write(fd, &rawarc, sizeof rawarc);
219 		}
220 	}
221 	_close(fd);
222 }
223 
224 /*
225  * Control profiling
226  *	profiling is what mcount checks to see if
227  *	all the data structures are ready.
228  */
229 void
230 moncontrol(mode)
231 	int mode;
232 {
233 	struct gmonparam *p = &_gmonparam;
234 
235 	if (mode) {
236 		/* start */
237 		profil((char *)p->kcount, p->kcountsize, p->lowpc, s_scale);
238 		p->state = GMON_PROF_ON;
239 	} else {
240 		/* stop */
241 		profil((char *)0, 0, 0, 0);
242 		p->state = GMON_PROF_OFF;
243 	}
244 }
245 
246 /*
247  * discover the tick frequency of the machine
248  * if something goes wrong, we return 0, an impossible hertz.
249  */
250 static int
251 hertz()
252 {
253 	struct itimerval tim;
254 
255 	tim.it_interval.tv_sec = 0;
256 	tim.it_interval.tv_usec = 1;
257 	tim.it_value.tv_sec = 0;
258 	tim.it_value.tv_usec = 0;
259 	setitimer(ITIMER_REAL, &tim, 0);
260 	setitimer(ITIMER_REAL, 0, &tim);
261 	if (tim.it_interval.tv_usec < 2)
262 		return(0);
263 	return (1000000 / tim.it_interval.tv_usec);
264 }
265