gmon.c revision 90667
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
34#if !defined(lint) && defined(LIBC_SCCS)
35static char sccsid[] = "@(#)gmon.c	8.1 (Berkeley) 6/4/93";
36#endif
37#include <sys/cdefs.h>
38__FBSDID("$FreeBSD: head/lib/libc/gmon/gmon.c 90667 2002-02-15 02:28:50Z bde $");
39
40#include "namespace.h"
41#include <sys/param.h>
42#include <sys/time.h>
43#include <sys/gmon.h>
44#include <sys/sysctl.h>
45
46#include "namespace.h"
47#include <err.h>
48#include "un-namespace.h"
49#include <stdio.h>
50#include <fcntl.h>
51#include <string.h>
52#include <unistd.h>
53#include "un-namespace.h"
54
55#if defined(__ELF__) && (defined(i386) || defined(__sparc64__))
56extern char *minbrk asm (".minbrk");
57#else
58extern char *minbrk asm ("minbrk");
59#endif
60
61extern char *__progname;
62
63struct gmonparam _gmonparam = { GMON_PROF_OFF };
64
65static int	s_scale;
66/* see profil(2) where this is describe (incorrectly) */
67#define		SCALE_1_TO_1	0x10000L
68
69#define ERR(s) _write(2, s, sizeof(s))
70
71void	moncontrol(int);
72static int hertz(void);
73
74void
75monstartup(lowpc, highpc)
76	u_long lowpc;
77	u_long highpc;
78{
79	int o;
80	char *cp;
81	struct gmonparam *p = &_gmonparam;
82
83	/*
84	 * round lowpc and highpc to multiples of the density we're using
85	 * so the rest of the scaling (here and in gprof) stays in ints.
86	 */
87	p->lowpc = ROUNDDOWN(lowpc, HISTFRACTION * sizeof(HISTCOUNTER));
88	p->highpc = ROUNDUP(highpc, HISTFRACTION * sizeof(HISTCOUNTER));
89	p->textsize = p->highpc - p->lowpc;
90	p->kcountsize = p->textsize / HISTFRACTION;
91	p->hashfraction = HASHFRACTION;
92	p->fromssize = p->textsize / HASHFRACTION;
93	p->tolimit = p->textsize * ARCDENSITY / 100;
94	if (p->tolimit < MINARCS)
95		p->tolimit = MINARCS;
96	else if (p->tolimit > MAXARCS)
97		p->tolimit = MAXARCS;
98	p->tossize = p->tolimit * sizeof(struct tostruct);
99
100	cp = sbrk(p->kcountsize + p->fromssize + p->tossize);
101	if (cp == (char *)-1) {
102		ERR("monstartup: out of memory\n");
103		return;
104	}
105#ifdef notdef
106	bzero(cp, p->kcountsize + p->fromssize + p->tossize);
107#endif
108	p->tos = (struct tostruct *)cp;
109	cp += p->tossize;
110	p->kcount = (u_short *)cp;
111	cp += p->kcountsize;
112	p->froms = (u_short *)cp;
113
114	minbrk = sbrk(0);
115	p->tos[0].link = 0;
116
117	o = p->highpc - p->lowpc;
118	if (p->kcountsize < o) {
119#ifndef hp300
120		s_scale = ((float)p->kcountsize / o ) * SCALE_1_TO_1;
121#else /* avoid floating point */
122		int quot = o / p->kcountsize;
123
124		if (quot >= 0x10000)
125			s_scale = 1;
126		else if (quot >= 0x100)
127			s_scale = 0x10000 / quot;
128		else if (o >= 0x800000)
129			s_scale = 0x1000000 / (o / (p->kcountsize >> 8));
130		else
131			s_scale = 0x1000000 / ((o << 8) / p->kcountsize);
132#endif
133	} else
134		s_scale = SCALE_1_TO_1;
135
136	moncontrol(1);
137}
138
139void
140_mcleanup()
141{
142	int fd;
143	int fromindex;
144	int endfrom;
145	u_long frompc;
146	int toindex;
147	struct rawarc rawarc;
148	struct gmonparam *p = &_gmonparam;
149	struct gmonhdr gmonhdr, *hdr;
150	struct clockinfo clockinfo;
151	char outname[128];
152	int mib[2];
153	size_t size;
154#ifdef DEBUG
155	int log, len;
156	char buf[200];
157#endif
158
159	if (p->state == GMON_PROF_ERROR)
160		ERR("_mcleanup: tos overflow\n");
161
162	size = sizeof(clockinfo);
163	mib[0] = CTL_KERN;
164	mib[1] = KERN_CLOCKRATE;
165	if (sysctl(mib, 2, &clockinfo, &size, NULL, 0) < 0) {
166		/*
167		 * Best guess
168		 */
169		clockinfo.profhz = hertz();
170	} else if (clockinfo.profhz == 0) {
171		if (clockinfo.hz != 0)
172			clockinfo.profhz = clockinfo.hz;
173		else
174			clockinfo.profhz = hertz();
175	}
176
177	moncontrol(0);
178	snprintf(outname, sizeof(outname), "%s.gmon", __progname);
179	fd = _open(outname, O_CREAT|O_TRUNC|O_WRONLY, 0666);
180	if (fd < 0) {
181		_warn("_mcleanup: %s", outname);
182		return;
183	}
184#ifdef DEBUG
185	log = _open("gmon.log", O_CREAT|O_TRUNC|O_WRONLY, 0664);
186	if (log < 0) {
187		_warn("_mcleanup: gmon.log");
188		return;
189	}
190	len = sprintf(buf, "[mcleanup1] kcount 0x%x ssiz %d\n",
191	    p->kcount, p->kcountsize);
192	_write(log, buf, len);
193#endif
194	hdr = (struct gmonhdr *)&gmonhdr;
195	hdr->lpc = p->lowpc;
196	hdr->hpc = p->highpc;
197	hdr->ncnt = p->kcountsize + sizeof(gmonhdr);
198	hdr->version = GMONVERSION;
199	hdr->profrate = clockinfo.profhz;
200	_write(fd, (char *)hdr, sizeof *hdr);
201	_write(fd, p->kcount, p->kcountsize);
202	endfrom = p->fromssize / sizeof(*p->froms);
203	for (fromindex = 0; fromindex < endfrom; fromindex++) {
204		if (p->froms[fromindex] == 0)
205			continue;
206
207		frompc = p->lowpc;
208		frompc += fromindex * p->hashfraction * sizeof(*p->froms);
209		for (toindex = p->froms[fromindex]; toindex != 0;
210		     toindex = p->tos[toindex].link) {
211#ifdef DEBUG
212			len = sprintf(buf,
213			"[mcleanup2] frompc 0x%x selfpc 0x%x count %d\n" ,
214				frompc, p->tos[toindex].selfpc,
215				p->tos[toindex].count);
216			_write(log, buf, len);
217#endif
218			rawarc.raw_frompc = frompc;
219			rawarc.raw_selfpc = p->tos[toindex].selfpc;
220			rawarc.raw_count = p->tos[toindex].count;
221			_write(fd, &rawarc, sizeof rawarc);
222		}
223	}
224	_close(fd);
225}
226
227/*
228 * Control profiling
229 *	profiling is what mcount checks to see if
230 *	all the data structures are ready.
231 */
232void
233moncontrol(mode)
234	int mode;
235{
236	struct gmonparam *p = &_gmonparam;
237
238	if (mode) {
239		/* start */
240		profil((char *)p->kcount, p->kcountsize, p->lowpc, s_scale);
241		p->state = GMON_PROF_ON;
242	} else {
243		/* stop */
244		profil((char *)0, 0, 0, 0);
245		p->state = GMON_PROF_OFF;
246	}
247}
248
249/*
250 * discover the tick frequency of the machine
251 * if something goes wrong, we return 0, an impossible hertz.
252 */
253static int
254hertz()
255{
256	struct itimerval tim;
257
258	tim.it_interval.tv_sec = 0;
259	tim.it_interval.tv_usec = 1;
260	tim.it_value.tv_sec = 0;
261	tim.it_value.tv_usec = 0;
262	setitimer(ITIMER_REAL, &tim, 0);
263	setitimer(ITIMER_REAL, 0, &tim);
264	if (tim.it_interval.tv_usec < 2)
265		return(0);
266	return (1000000 / tim.it_interval.tv_usec);
267}
268