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