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