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