gmon.c revision 1.1
1/* $NetBSD: gmon.c,v 1.3 1995/02/27 12:54:39 cgd Exp $ */ 2 3/*- 4 * Copyright (c) 1983, 1992, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36#if !defined(lint) && defined(LIBC_SCCS) 37#if 0 38static char sccsid[] = "@(#)gmon.c 8.1 (Berkeley) 6/4/93"; 39#else 40static char rcsid[] = "$NetBSD: gmon.c,v 1.3 1995/02/27 12:54:39 cgd Exp $"; 41#endif 42#endif 43 44#include <sys/param.h> 45#include <sys/time.h> 46#include <sys/gmon.h> 47#include <sys/sysctl.h> 48 49#include <stdio.h> 50#include <fcntl.h> 51#include <unistd.h> 52 53extern char *minbrk asm ("minbrk"); 54 55struct gmonparam _gmonparam = { GMON_PROF_OFF }; 56 57static 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 63void moncontrol __P((int)); 64static int hertz __P((void)); 65 66void 67monstartup(lowpc, highpc) 68 u_long lowpc; 69 u_long highpc; 70{ 71 register int o; 72 char *cp; 73 struct gmonparam *p = &_gmonparam; 74 75 /* 76 * round lowpc and highpc to multiples of the density we're using 77 * so the rest of the scaling (here and in gprof) stays in ints. 78 */ 79 p->lowpc = ROUNDDOWN(lowpc, HISTFRACTION * sizeof(HISTCOUNTER)); 80 p->highpc = ROUNDUP(highpc, HISTFRACTION * sizeof(HISTCOUNTER)); 81 p->textsize = p->highpc - p->lowpc; 82 p->kcountsize = p->textsize / HISTFRACTION; 83 p->hashfraction = HASHFRACTION; 84 p->fromssize = p->textsize / HASHFRACTION; 85 p->tolimit = p->textsize * ARCDENSITY / 100; 86 if (p->tolimit < MINARCS) 87 p->tolimit = MINARCS; 88 else if (p->tolimit > MAXARCS) 89 p->tolimit = MAXARCS; 90 p->tossize = p->tolimit * sizeof(struct tostruct); 91 92 cp = sbrk(p->kcountsize + p->fromssize + p->tossize); 93 if (cp == (char *)-1) { 94 ERR("monstartup: out of memory\n"); 95 return; 96 } 97#ifdef notdef 98 bzero(cp, p->kcountsize + p->fromssize + p->tossize); 99#endif 100 p->tos = (struct tostruct *)cp; 101 cp += p->tossize; 102 p->kcount = (u_short *)cp; 103 cp += p->kcountsize; 104 p->froms = (u_short *)cp; 105 106 minbrk = sbrk(0); 107 p->tos[0].link = 0; 108 109 o = p->highpc - p->lowpc; 110 if (p->kcountsize < o) { 111#ifndef notdef 112 s_scale = ((float)p->kcountsize / o ) * SCALE_1_TO_1; 113#else /* avoid floating point */ 114 int quot = o / p->kcountsize; 115 116 if (quot >= 0x10000) 117 s_scale = 1; 118 else if (quot >= 0x100) 119 s_scale = 0x10000 / quot; 120 else if (o >= 0x800000) 121 s_scale = 0x1000000 / (o / (p->kcountsize >> 8)); 122 else 123 s_scale = 0x1000000 / ((o << 8) / p->kcountsize); 124#endif 125 } else 126 s_scale = SCALE_1_TO_1; 127 128 moncontrol(1); 129} 130 131void 132_mcleanup() 133{ 134 int fd; 135 int fromindex; 136 int endfrom; 137 u_long frompc; 138 int toindex; 139 struct rawarc rawarc; 140 struct gmonparam *p = &_gmonparam; 141 struct gmonhdr gmonhdr, *hdr; 142 struct clockinfo clockinfo; 143 int mib[2]; 144 size_t size; 145#ifdef DEBUG 146 int log, len; 147 char buf[200]; 148#endif 149 150 if (p->state == GMON_PROF_ERROR) 151 ERR("_mcleanup: tos overflow\n"); 152 153 size = sizeof(clockinfo); 154 mib[0] = CTL_KERN; 155 mib[1] = KERN_CLOCKRATE; 156 if (sysctl(mib, 2, &clockinfo, &size, NULL, 0) < 0) { 157 /* 158 * Best guess 159 */ 160 clockinfo.profhz = hertz(); 161 } else if (clockinfo.profhz == 0) { 162 if (clockinfo.hz != 0) 163 clockinfo.profhz = clockinfo.hz; 164 else 165 clockinfo.profhz = hertz(); 166 } 167 168 moncontrol(0); 169 fd = open("gmon.out", O_CREAT|O_TRUNC|O_WRONLY, 0666); 170 if (fd < 0) { 171 perror("mcount: gmon.out"); 172 return; 173 } 174#ifdef DEBUG 175 log = open("gmon.log", O_CREAT|O_TRUNC|O_WRONLY, 0664); 176 if (log < 0) { 177 perror("mcount: gmon.log"); 178 return; 179 } 180 len = sprintf(buf, "[mcleanup1] kcount 0x%x ssiz %d\n", 181 p->kcount, p->kcountsize); 182 write(log, buf, len); 183#endif 184 hdr = (struct gmonhdr *)&gmonhdr; 185 hdr->lpc = p->lowpc; 186 hdr->hpc = p->highpc; 187 hdr->ncnt = p->kcountsize + sizeof(gmonhdr); 188 hdr->version = GMONVERSION; 189 hdr->profrate = clockinfo.profhz; 190 write(fd, (char *)hdr, sizeof *hdr); 191 write(fd, p->kcount, p->kcountsize); 192 endfrom = p->fromssize / sizeof(*p->froms); 193 for (fromindex = 0; fromindex < endfrom; fromindex++) { 194 if (p->froms[fromindex] == 0) 195 continue; 196 197 frompc = p->lowpc; 198 frompc += fromindex * p->hashfraction * sizeof(*p->froms); 199 for (toindex = p->froms[fromindex]; toindex != 0; 200 toindex = p->tos[toindex].link) { 201#ifdef DEBUG 202 len = sprintf(buf, 203 "[mcleanup2] frompc 0x%x selfpc 0x%x count %d\n" , 204 frompc, p->tos[toindex].selfpc, 205 p->tos[toindex].count); 206 write(log, buf, len); 207#endif 208 rawarc.raw_frompc = frompc; 209 rawarc.raw_selfpc = p->tos[toindex].selfpc; 210 rawarc.raw_count = p->tos[toindex].count; 211 write(fd, &rawarc, sizeof rawarc); 212 } 213 } 214 close(fd); 215} 216 217/* 218 * Control profiling 219 * profiling is what mcount checks to see if 220 * all the data structures are ready. 221 */ 222void 223moncontrol(mode) 224 int mode; 225{ 226 struct gmonparam *p = &_gmonparam; 227 228 if (mode) { 229 /* start */ 230 profil((char *)p->kcount, p->kcountsize, (int)p->lowpc, 231 s_scale); 232 p->state = GMON_PROF_ON; 233 } else { 234 /* stop */ 235 profil((char *)0, 0, 0, 0); 236 p->state = GMON_PROF_OFF; 237 } 238} 239 240/* 241 * discover the tick frequency of the machine 242 * if something goes wrong, we return 0, an impossible hertz. 243 */ 244static int 245hertz() 246{ 247 struct itimerval tim; 248 249 tim.it_interval.tv_sec = 0; 250 tim.it_interval.tv_usec = 1; 251 tim.it_value.tv_sec = 0; 252 tim.it_value.tv_usec = 0; 253 setitimer(ITIMER_REAL, &tim, 0); 254 setitimer(ITIMER_REAL, 0, &tim); 255 if (tim.it_interval.tv_usec < 2) 256 return(0); 257 return (1000000 / tim.it_interval.tv_usec); 258} 259 260 261