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