xref: /freebsd/lib/libc/gmon/gmon.c (revision 7b3ea376a27ada7a61eb0c3102f13040fb8c16cb)
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)
31 static char sccsid[] = "@(#)gmon.c	8.1 (Berkeley) 6/4/93";
32 #endif
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
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__))
54 extern char *minbrk __asm (".minbrk");
55 #elif defined(__powerpc64__)
56 extern char *minbrk __asm ("_minbrk");
57 #else
58 extern char *minbrk __asm ("minbrk");
59 #endif
60 
61 struct gmonparam _gmonparam = { GMON_PROF_OFF };
62 
63 static 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 
69 void	moncontrol(int);
70 static int hertz(void);
71 void	_mcleanup(void);
72 
73 void
74 monstartup(u_long lowpc, 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 	s_scale = (p->kcountsize < o) ?
116 	    ((uintmax_t)p->kcountsize << SCALE_SHIFT) / o : (1 << SCALE_SHIFT);
117 	moncontrol(1);
118 }
119 
120 void
121 _mcleanup(void)
122 {
123 	int fd;
124 	int fromindex;
125 	int endfrom;
126 	u_long frompc;
127 	int toindex;
128 	struct rawarc rawarc;
129 	struct gmonparam *p = &_gmonparam;
130 	struct gmonhdr gmonhdr, *hdr;
131 	struct clockinfo clockinfo;
132 	char outname[128];
133 	int mib[2];
134 	size_t size;
135 #ifdef DEBUG
136 	int log, len;
137 	char buf[200];
138 #endif
139 
140 	if (p->state == GMON_PROF_ERROR)
141 		ERR("_mcleanup: tos overflow\n");
142 
143 	size = sizeof(clockinfo);
144 	mib[0] = CTL_KERN;
145 	mib[1] = KERN_CLOCKRATE;
146 	if (sysctl(mib, 2, &clockinfo, &size, NULL, 0) < 0) {
147 		/*
148 		 * Best guess
149 		 */
150 		clockinfo.profhz = hertz();
151 	} else if (clockinfo.profhz == 0) {
152 		if (clockinfo.hz != 0)
153 			clockinfo.profhz = clockinfo.hz;
154 		else
155 			clockinfo.profhz = hertz();
156 	}
157 
158 	moncontrol(0);
159 	if (getenv("PROFIL_USE_PID"))
160 		snprintf(outname, sizeof(outname), "%s.%d.gmon",
161 		    _getprogname(), getpid());
162 	else
163 		snprintf(outname, sizeof(outname), "%s.gmon", _getprogname());
164 
165 	fd = _open(outname, O_CREAT|O_TRUNC|O_WRONLY|O_CLOEXEC, 0666);
166 	if (fd < 0) {
167 		_warn("_mcleanup: %s", outname);
168 		return;
169 	}
170 #ifdef DEBUG
171 	log = _open("gmon.log", O_CREAT|O_TRUNC|O_WRONLY|O_CLOEXEC, 0664);
172 	if (log < 0) {
173 		_warn("_mcleanup: gmon.log");
174 		return;
175 	}
176 	len = sprintf(buf, "[mcleanup1] kcount 0x%p ssiz %lu\n",
177 	    p->kcount, p->kcountsize);
178 	_write(log, buf, len);
179 #endif
180 	hdr = (struct gmonhdr *)&gmonhdr;
181 	bzero(hdr, sizeof(*hdr));
182 	hdr->lpc = p->lowpc;
183 	hdr->hpc = p->highpc;
184 	hdr->ncnt = p->kcountsize + sizeof(gmonhdr);
185 	hdr->version = GMONVERSION;
186 	hdr->profrate = clockinfo.profhz;
187 	_write(fd, (char *)hdr, sizeof *hdr);
188 	_write(fd, p->kcount, p->kcountsize);
189 	endfrom = p->fromssize / sizeof(*p->froms);
190 	for (fromindex = 0; fromindex < endfrom; fromindex++) {
191 		if (p->froms[fromindex] == 0)
192 			continue;
193 
194 		frompc = p->lowpc;
195 		frompc += fromindex * p->hashfraction * sizeof(*p->froms);
196 		for (toindex = p->froms[fromindex]; toindex != 0;
197 		     toindex = p->tos[toindex].link) {
198 #ifdef DEBUG
199 			len = sprintf(buf,
200 			"[mcleanup2] frompc 0x%lx selfpc 0x%lx count %lu\n" ,
201 				frompc, p->tos[toindex].selfpc,
202 				p->tos[toindex].count);
203 			_write(log, buf, len);
204 #endif
205 			rawarc.raw_frompc = frompc;
206 			rawarc.raw_selfpc = p->tos[toindex].selfpc;
207 			rawarc.raw_count = p->tos[toindex].count;
208 			_write(fd, &rawarc, sizeof rawarc);
209 		}
210 	}
211 	_close(fd);
212 }
213 
214 /*
215  * Control profiling
216  *	profiling is what mcount checks to see if
217  *	all the data structures are ready.
218  */
219 void
220 moncontrol(int mode)
221 {
222 	struct gmonparam *p = &_gmonparam;
223 
224 	if (mode) {
225 		/* start */
226 		profil((char *)p->kcount, p->kcountsize, p->lowpc, s_scale);
227 		p->state = GMON_PROF_ON;
228 	} else {
229 		/* stop */
230 		profil((char *)0, 0, 0, 0);
231 		p->state = GMON_PROF_OFF;
232 	}
233 }
234 
235 /*
236  * discover the tick frequency of the machine
237  * if something goes wrong, we return 0, an impossible hertz.
238  */
239 static int
240 hertz(void)
241 {
242 	struct itimerval tim;
243 
244 	tim.it_interval.tv_sec = 0;
245 	tim.it_interval.tv_usec = 1;
246 	tim.it_value.tv_sec = 0;
247 	tim.it_value.tv_usec = 0;
248 	setitimer(ITIMER_REAL, &tim, 0);
249 	setitimer(ITIMER_REAL, 0, &tim);
250 	if (tim.it_interval.tv_usec < 2)
251 		return(0);
252 	return (1000000 / tim.it_interval.tv_usec);
253 }
254