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