gmon.c revision 1.5
1/* $NetBSD: gmon.c,v 1.5 1995/11/21 22:23:47 jtc 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.5 1995/11/21 22:23:47 jtc 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 <stdlib.h> 51#include <fcntl.h> 52#include <limits.h> 53#include <unistd.h> 54 55extern char *minbrk asm ("minbrk"); 56 57struct gmonparam _gmonparam = { GMON_PROF_OFF }; 58 59static int s_scale; 60/* see profil(2) where this is describe (incorrectly) */ 61#define SCALE_1_TO_1 0x10000L 62 63#define ERR(s) write(2, s, sizeof(s)) 64 65void moncontrol __P((int)); 66static int hertz __P((void)); 67 68void 69monstartup(lowpc, highpc) 70 u_long lowpc; 71 u_long highpc; 72{ 73 register int o; 74 char *cp; 75 struct gmonparam *p = &_gmonparam; 76 77 /* 78 * round lowpc and highpc to multiples of the density we're using 79 * so the rest of the scaling (here and in gprof) stays in ints. 80 */ 81 p->lowpc = ROUNDDOWN(lowpc, HISTFRACTION * sizeof(HISTCOUNTER)); 82 p->highpc = ROUNDUP(highpc, HISTFRACTION * sizeof(HISTCOUNTER)); 83 p->textsize = p->highpc - p->lowpc; 84 p->kcountsize = p->textsize / HISTFRACTION; 85 p->hashfraction = HASHFRACTION; 86 p->fromssize = p->textsize / HASHFRACTION; 87 p->tolimit = p->textsize * ARCDENSITY / 100; 88 if (p->tolimit < MINARCS) 89 p->tolimit = MINARCS; 90 else if (p->tolimit > MAXARCS) 91 p->tolimit = MAXARCS; 92 p->tossize = p->tolimit * sizeof(struct tostruct); 93 94 cp = sbrk(p->kcountsize + p->fromssize + p->tossize); 95 if (cp == (char *)-1) { 96 ERR("monstartup: out of memory\n"); 97 return; 98 } 99#ifdef notdef 100 bzero(cp, p->kcountsize + p->fromssize + p->tossize); 101#endif 102 p->tos = (struct tostruct *)cp; 103 cp += p->tossize; 104 p->kcount = (u_short *)cp; 105 cp += p->kcountsize; 106 p->froms = (u_short *)cp; 107 108 minbrk = sbrk(0); 109 p->tos[0].link = 0; 110 111 o = p->highpc - p->lowpc; 112 if (p->kcountsize < o) { 113#ifndef notdef 114 s_scale = ((float)p->kcountsize / o ) * SCALE_1_TO_1; 115#else /* avoid floating point */ 116 int quot = o / p->kcountsize; 117 118 if (quot >= 0x10000) 119 s_scale = 1; 120 else if (quot >= 0x100) 121 s_scale = 0x10000 / quot; 122 else if (o >= 0x800000) 123 s_scale = 0x1000000 / (o / (p->kcountsize >> 8)); 124 else 125 s_scale = 0x1000000 / ((o << 8) / p->kcountsize); 126#endif 127 } else 128 s_scale = SCALE_1_TO_1; 129 130 moncontrol(1); 131} 132 133void 134_mcleanup() 135{ 136 int fd; 137 int fromindex; 138 int endfrom; 139 u_long frompc; 140 int toindex; 141 struct rawarc rawarc; 142 struct gmonparam *p = &_gmonparam; 143 struct gmonhdr gmonhdr, *hdr; 144 struct clockinfo clockinfo; 145 int mib[2]; 146 size_t size; 147 char *profdir; 148 char *proffile; 149 char buf[PATH_MAX]; 150#ifdef DEBUG 151 int log, len; 152 char buf[200]; 153#endif 154 155 if (p->state == GMON_PROF_ERROR) 156 ERR("_mcleanup: tos overflow\n"); 157 158 size = sizeof(clockinfo); 159 mib[0] = CTL_KERN; 160 mib[1] = KERN_CLOCKRATE; 161 if (sysctl(mib, 2, &clockinfo, &size, NULL, 0) < 0) { 162 /* 163 * Best guess 164 */ 165 clockinfo.profhz = hertz(); 166 } else if (clockinfo.profhz == 0) { 167 if (clockinfo.hz != 0) 168 clockinfo.profhz = clockinfo.hz; 169 else 170 clockinfo.profhz = hertz(); 171 } 172 173 moncontrol(0); 174 175 if ((profdir = getenv("PROFDIR")) != NULL) { 176 extern char *__progname; 177 char *s, *t; 178 pid_t pid; 179 long divisor; 180 181 /* If PROFDIR contains a null value, no profiling 182 output is produced */ 183 if (*profdir == '\0') { 184 return; 185 } 186 187 t = buf; 188 s = profdir; 189 while((*t = *s) != '\0') { 190 t++; 191 s++; 192 } 193 *t++ = '/'; 194 195 /* 196 * Copy and convert pid from a pid_t to a string. For 197 * best performance, divisor should be initialized to 198 * the largest power of 10 less than PID_MAX. 199 */ 200 pid = getpid(); 201 divisor=10000; 202 while (divisor > pid) divisor /= 10; /* skip leading zeros */ 203 do { 204 *t++ = (pid/divisor) + '0'; 205 pid %= divisor; 206 } while (divisor /= 10); 207 *t++ = '.'; 208 209 s = __progname; 210 while ((*t++ = *s++) != '\0') 211 ; 212 213 proffile = buf; 214 } else { 215 proffile = "gmon.out"; 216 } 217 218 fd = open(proffile , O_CREAT|O_TRUNC|O_WRONLY, 0666); 219 if (fd < 0) { 220 perror( proffile ); 221 return; 222 } 223#ifdef DEBUG 224 log = open("gmon.log", O_CREAT|O_TRUNC|O_WRONLY, 0664); 225 if (log < 0) { 226 perror("mcount: gmon.log"); 227 return; 228 } 229 len = sprintf(buf, "[mcleanup1] kcount 0x%x ssiz %d\n", 230 p->kcount, p->kcountsize); 231 write(log, buf, len); 232#endif 233 hdr = (struct gmonhdr *)&gmonhdr; 234 hdr->lpc = p->lowpc; 235 hdr->hpc = p->highpc; 236 hdr->ncnt = p->kcountsize + sizeof(gmonhdr); 237 hdr->version = GMONVERSION; 238 hdr->profrate = clockinfo.profhz; 239 write(fd, (char *)hdr, sizeof *hdr); 240 write(fd, p->kcount, p->kcountsize); 241 endfrom = p->fromssize / sizeof(*p->froms); 242 for (fromindex = 0; fromindex < endfrom; fromindex++) { 243 if (p->froms[fromindex] == 0) 244 continue; 245 246 frompc = p->lowpc; 247 frompc += fromindex * p->hashfraction * sizeof(*p->froms); 248 for (toindex = p->froms[fromindex]; toindex != 0; 249 toindex = p->tos[toindex].link) { 250#ifdef DEBUG 251 len = sprintf(buf, 252 "[mcleanup2] frompc 0x%x selfpc 0x%x count %d\n" , 253 frompc, p->tos[toindex].selfpc, 254 p->tos[toindex].count); 255 write(log, buf, len); 256#endif 257 rawarc.raw_frompc = frompc; 258 rawarc.raw_selfpc = p->tos[toindex].selfpc; 259 rawarc.raw_count = p->tos[toindex].count; 260 write(fd, &rawarc, sizeof rawarc); 261 } 262 } 263 close(fd); 264} 265 266/* 267 * Control profiling 268 * profiling is what mcount checks to see if 269 * all the data structures are ready. 270 */ 271void 272moncontrol(mode) 273 int mode; 274{ 275 struct gmonparam *p = &_gmonparam; 276 277 if (mode) { 278 /* start */ 279 profil((char *)p->kcount, p->kcountsize, p->lowpc, 280 s_scale); 281 p->state = GMON_PROF_ON; 282 } else { 283 /* stop */ 284 profil((char *)0, 0, 0, 0); 285 p->state = GMON_PROF_OFF; 286 } 287} 288 289/* 290 * discover the tick frequency of the machine 291 * if something goes wrong, we return 0, an impossible hertz. 292 */ 293static int 294hertz() 295{ 296 struct itimerval tim; 297 298 tim.it_interval.tv_sec = 0; 299 tim.it_interval.tv_usec = 1; 300 tim.it_value.tv_sec = 0; 301 tim.it_value.tv_usec = 0; 302 setitimer(ITIMER_REAL, &tim, 0); 303 setitimer(ITIMER_REAL, 0, &tim); 304 if (tim.it_interval.tv_usec < 2) 305 return(0); 306 return (1000000 / tim.it_interval.tv_usec); 307} 308 309 310