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