gmon.c revision 114320
189857Sobrien/*- 289857Sobrien * Copyright (c) 1983, 1992, 1993 389857Sobrien * The Regents of the University of California. All rights reserved. 489857Sobrien * 589857Sobrien * Redistribution and use in source and binary forms, with or without 689857Sobrien * modification, are permitted provided that the following conditions 789857Sobrien * are met: 889857Sobrien * 1. Redistributions of source code must retain the above copyright 989857Sobrien * notice, this list of conditions and the following disclaimer. 1089857Sobrien * 2. Redistributions in binary form must reproduce the above copyright 11218822Sdim * notice, this list of conditions and the following disclaimer in the 1289857Sobrien * documentation and/or other materials provided with the distribution. 1389857Sobrien * 3. All advertising materials mentioning features or use of this software 1489857Sobrien * must display the following acknowledgement: 1589857Sobrien * This product includes software developed by the University of 1689857Sobrien * California, Berkeley and its contributors. 1789857Sobrien * 4. Neither the name of the University nor the names of its contributors 1889857Sobrien * may be used to endorse or promote products derived from this software 1989857Sobrien * without specific prior written permission. 2089857Sobrien * 2189857Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2289857Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2389857Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2489857Sobrien * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2589857Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2689857Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2789857Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2889857Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2989857Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3089857Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31218822Sdim * SUCH DAMAGE. 3289857Sobrien */ 3389857Sobrien 3489857Sobrien#if !defined(lint) && defined(LIBC_SCCS) 3589857Sobrienstatic char sccsid[] = "@(#)gmon.c 8.1 (Berkeley) 6/4/93"; 3689857Sobrien#endif 37218822Sdim#include <sys/cdefs.h> 38218822Sdim__FBSDID("$FreeBSD: head/lib/libc/gmon/gmon.c 114320 2003-04-30 19:29:02Z peter $"); 39218822Sdim 40218822Sdim#include "namespace.h" 4189857Sobrien#include <sys/param.h> 4289857Sobrien#include <sys/time.h> 43218822Sdim#include <sys/gmon.h> 4489857Sobrien#include <sys/sysctl.h> 45130561Sobrien 4689857Sobrien#include <err.h> 4789857Sobrien#include <fcntl.h> 4889857Sobrien#include <stdio.h> 4989857Sobrien#include <stdlib.h> 50218822Sdim#include <string.h> 5189857Sobrien#include <unistd.h> 52130561Sobrien#include "un-namespace.h" 5389857Sobrien 5489857Sobrien#include "libc_private.h" 5589857Sobrien 5689857Sobrien#if defined(__i386__) || defined(__sparc64__) || defined(__amd64__) 57218822Sdimextern char *minbrk asm (".minbrk"); 5889857Sobrien#else 59130561Sobrienextern char *minbrk asm ("minbrk"); 6089857Sobrien#endif 6189857Sobrien 6289857Sobrienstruct gmonparam _gmonparam = { GMON_PROF_OFF }; 6389857Sobrien 64218822Sdimstatic int s_scale; 6589857Sobrien/* see profil(2) where this is describe (incorrectly) */ 66130561Sobrien#define SCALE_1_TO_1 0x10000L 6789857Sobrien 6889857Sobrien#define ERR(s) _write(2, s, sizeof(s)) 6989857Sobrien 7089857Sobrienvoid moncontrol(int); 71218822Sdimstatic int hertz(void); 7289857Sobrien 73130561Sobrienvoid 7489857Sobrienmonstartup(lowpc, highpc) 7589857Sobrien u_long lowpc; 7689857Sobrien u_long highpc; 7789857Sobrien{ 78218822Sdim int o; 7989857Sobrien char *cp; 80130561Sobrien struct gmonparam *p = &_gmonparam; 8189857Sobrien 8289857Sobrien /* 8389857Sobrien * round lowpc and highpc to multiples of the density we're using 8489857Sobrien * so the rest of the scaling (here and in gprof) stays in ints. 85218822Sdim */ 8689857Sobrien p->lowpc = ROUNDDOWN(lowpc, HISTFRACTION * sizeof(HISTCOUNTER)); 87130561Sobrien p->highpc = ROUNDUP(highpc, HISTFRACTION * sizeof(HISTCOUNTER)); 8889857Sobrien p->textsize = p->highpc - p->lowpc; 8989857Sobrien p->kcountsize = p->textsize / HISTFRACTION; 9089857Sobrien p->hashfraction = HASHFRACTION; 9189857Sobrien p->fromssize = p->textsize / HASHFRACTION; 92218822Sdim p->tolimit = p->textsize * ARCDENSITY / 100; 9389857Sobrien if (p->tolimit < MINARCS) 94130561Sobrien p->tolimit = MINARCS; 9589857Sobrien else if (p->tolimit > MAXARCS) 9689857Sobrien p->tolimit = MAXARCS; 9789857Sobrien p->tossize = p->tolimit * sizeof(struct tostruct); 9889857Sobrien 99218822Sdim cp = sbrk(p->kcountsize + p->fromssize + p->tossize); 10089857Sobrien if (cp == (char *)-1) { 101130561Sobrien ERR("monstartup: out of memory\n"); 10289857Sobrien return; 10389857Sobrien } 10489857Sobrien#ifdef notdef 10589857Sobrien bzero(cp, p->kcountsize + p->fromssize + p->tossize); 106218822Sdim#endif 10789857Sobrien p->tos = (struct tostruct *)cp; 108130561Sobrien cp += p->tossize; 10989857Sobrien p->kcount = (u_short *)cp; 11089857Sobrien cp += p->kcountsize; 11189857Sobrien p->froms = (u_short *)cp; 11289857Sobrien 113218822Sdim minbrk = sbrk(0); 11489857Sobrien p->tos[0].link = 0; 115130561Sobrien 11689857Sobrien o = p->highpc - p->lowpc; 11789857Sobrien if (p->kcountsize < o) { 11889857Sobrien#ifndef hp300 11989857Sobrien s_scale = ((float)p->kcountsize / o ) * SCALE_1_TO_1; 120218822Sdim#else /* avoid floating point */ 12189857Sobrien int quot = o / p->kcountsize; 122130561Sobrien 12389857Sobrien if (quot >= 0x10000) 12489857Sobrien s_scale = 1; 12589857Sobrien else if (quot >= 0x100) 12689857Sobrien s_scale = 0x10000 / quot; 127218822Sdim else if (o >= 0x800000) 12889857Sobrien s_scale = 0x1000000 / (o / (p->kcountsize >> 8)); 129130561Sobrien else 13089857Sobrien s_scale = 0x1000000 / ((o << 8) / p->kcountsize); 13189857Sobrien#endif 13289857Sobrien } else 13389857Sobrien s_scale = SCALE_1_TO_1; 134218822Sdim 13589857Sobrien moncontrol(1); 136218822Sdim} 137218822Sdim 138218822Sdimvoid 139218822Sdim_mcleanup() 140218822Sdim{ 141218822Sdim int fd; 142218822Sdim int fromindex; 143130561Sobrien int endfrom; 14489857Sobrien u_long frompc; 14589857Sobrien int toindex; 14689857Sobrien struct rawarc rawarc; 14789857Sobrien struct gmonparam *p = &_gmonparam; 14889857Sobrien struct gmonhdr gmonhdr, *hdr; 14989857Sobrien struct clockinfo clockinfo; 15089857Sobrien char outname[128]; 15189857Sobrien int mib[2]; 15289857Sobrien size_t size; 15389857Sobrien#ifdef DEBUG 15489857Sobrien int log, len; 15589857Sobrien char buf[200]; 15689857Sobrien#endif 15789857Sobrien 15889857Sobrien if (p->state == GMON_PROF_ERROR) 15989857Sobrien ERR("_mcleanup: tos overflow\n"); 16091041Sobrien 16191041Sobrien size = sizeof(clockinfo); 162218822Sdim mib[0] = CTL_KERN; 16391041Sobrien mib[1] = KERN_CLOCKRATE; 164218822Sdim if (sysctl(mib, 2, &clockinfo, &size, NULL, 0) < 0) { 165218822Sdim /* 166218822Sdim * Best guess 167218822Sdim */ 168218822Sdim clockinfo.profhz = hertz(); 169218822Sdim } else if (clockinfo.profhz == 0) { 170218822Sdim if (clockinfo.hz != 0) 171218822Sdim clockinfo.profhz = clockinfo.hz; 172218822Sdim else 173218822Sdim clockinfo.profhz = hertz(); 174218822Sdim } 175218822Sdim 176218822Sdim moncontrol(0); 177218822Sdim snprintf(outname, sizeof(outname), "%s.gmon", _getprogname()); 178218822Sdim fd = _open(outname, O_CREAT|O_TRUNC|O_WRONLY, 0666); 179218822Sdim if (fd < 0) { 180218822Sdim _warn("_mcleanup: %s", outname); 181218822Sdim return; 182218822Sdim } 183218822Sdim#ifdef DEBUG 184218822Sdim log = _open("gmon.log", O_CREAT|O_TRUNC|O_WRONLY, 0664); 185218822Sdim if (log < 0) { 186218822Sdim _warn("_mcleanup: gmon.log"); 187218822Sdim return; 188218822Sdim } 189218822Sdim len = sprintf(buf, "[mcleanup1] kcount 0x%x ssiz %d\n", 190218822Sdim p->kcount, p->kcountsize); 191218822Sdim _write(log, buf, len); 192218822Sdim#endif 193218822Sdim hdr = (struct gmonhdr *)&gmonhdr; 19489857Sobrien bzero(hdr, sizeof(*hdr)); 19589857Sobrien hdr->lpc = p->lowpc; 19689857Sobrien hdr->hpc = p->highpc; 197218822Sdim hdr->ncnt = p->kcountsize + sizeof(gmonhdr); 198 hdr->version = GMONVERSION; 199 hdr->profrate = clockinfo.profhz; 200 _write(fd, (char *)hdr, sizeof *hdr); 201 _write(fd, p->kcount, p->kcountsize); 202 endfrom = p->fromssize / sizeof(*p->froms); 203 for (fromindex = 0; fromindex < endfrom; fromindex++) { 204 if (p->froms[fromindex] == 0) 205 continue; 206 207 frompc = p->lowpc; 208 frompc += fromindex * p->hashfraction * sizeof(*p->froms); 209 for (toindex = p->froms[fromindex]; toindex != 0; 210 toindex = p->tos[toindex].link) { 211#ifdef DEBUG 212 len = sprintf(buf, 213 "[mcleanup2] frompc 0x%x selfpc 0x%x count %d\n" , 214 frompc, p->tos[toindex].selfpc, 215 p->tos[toindex].count); 216 _write(log, buf, len); 217#endif 218 rawarc.raw_frompc = frompc; 219 rawarc.raw_selfpc = p->tos[toindex].selfpc; 220 rawarc.raw_count = p->tos[toindex].count; 221 _write(fd, &rawarc, sizeof rawarc); 222 } 223 } 224 _close(fd); 225} 226 227/* 228 * Control profiling 229 * profiling is what mcount checks to see if 230 * all the data structures are ready. 231 */ 232void 233moncontrol(mode) 234 int mode; 235{ 236 struct gmonparam *p = &_gmonparam; 237 238 if (mode) { 239 /* start */ 240 profil((char *)p->kcount, p->kcountsize, p->lowpc, s_scale); 241 p->state = GMON_PROF_ON; 242 } else { 243 /* stop */ 244 profil((char *)0, 0, 0, 0); 245 p->state = GMON_PROF_OFF; 246 } 247} 248 249/* 250 * discover the tick frequency of the machine 251 * if something goes wrong, we return 0, an impossible hertz. 252 */ 253static int 254hertz() 255{ 256 struct itimerval tim; 257 258 tim.it_interval.tv_sec = 0; 259 tim.it_interval.tv_usec = 1; 260 tim.it_value.tv_sec = 0; 261 tim.it_value.tv_usec = 0; 262 setitimer(ITIMER_REAL, &tim, 0); 263 setitimer(ITIMER_REAL, 0, &tim); 264 if (tim.it_interval.tv_usec < 2) 265 return(0); 266 return (1000000 / tim.it_interval.tv_usec); 267} 268