gmon.c revision 1.6
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. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34#if !defined(lint) && defined(LIBC_SCCS) 35static char rcsid[] = "$OpenBSD: gmon.c,v 1.6 1996/09/05 12:29:12 deraadt Exp $"; 36#endif 37 38#include <sys/param.h> 39#include <sys/time.h> 40#include <sys/gmon.h> 41#include <sys/sysctl.h> 42 43#include <stdio.h> 44#include <stdlib.h> 45#include <fcntl.h> 46#include <limits.h> 47#include <unistd.h> 48 49extern char *minbrk __asm ("minbrk"); 50 51struct gmonparam _gmonparam = { GMON_PROF_OFF }; 52 53static int s_scale; 54/* see profil(2) where this is describe (incorrectly) */ 55#define SCALE_1_TO_1 0x10000L 56 57#define ERR(s) write(2, s, sizeof(s)) 58 59void moncontrol __P((int)); 60static int hertz __P((void)); 61 62void 63monstartup(lowpc, highpc) 64 u_long lowpc; 65 u_long highpc; 66{ 67 register int o; 68 char *cp; 69 struct gmonparam *p = &_gmonparam; 70 71 /* 72 * round lowpc and highpc to multiples of the density we're using 73 * so the rest of the scaling (here and in gprof) stays in ints. 74 */ 75 p->lowpc = ROUNDDOWN(lowpc, HISTFRACTION * sizeof(HISTCOUNTER)); 76 p->highpc = ROUNDUP(highpc, HISTFRACTION * sizeof(HISTCOUNTER)); 77 p->textsize = p->highpc - p->lowpc; 78 p->kcountsize = p->textsize / HISTFRACTION; 79 p->hashfraction = HASHFRACTION; 80 p->fromssize = p->textsize / p->hashfraction; 81 p->tolimit = p->textsize * ARCDENSITY / 100; 82 if (p->tolimit < MINARCS) 83 p->tolimit = MINARCS; 84 else if (p->tolimit > MAXARCS) 85 p->tolimit = MAXARCS; 86 p->tossize = p->tolimit * sizeof(struct tostruct); 87 88 cp = sbrk(p->kcountsize + p->fromssize + p->tossize); 89 if (cp == (char *)-1) { 90 ERR("monstartup: out of memory\n"); 91 return; 92 } 93#ifdef notdef 94 bzero(cp, p->kcountsize + p->fromssize + p->tossize); 95#endif 96 p->tos = (struct tostruct *)cp; 97 cp += p->tossize; 98 p->kcount = (u_short *)cp; 99 cp += p->kcountsize; 100 p->froms = (u_short *)cp; 101 102 minbrk = sbrk(0); 103 p->tos[0].link = 0; 104 105 o = p->highpc - p->lowpc; 106 if (p->kcountsize < o) { 107#ifndef notdef 108 s_scale = ((float)p->kcountsize / o ) * SCALE_1_TO_1; 109#else /* avoid floating point */ 110 int quot = o / p->kcountsize; 111 112 if (quot >= 0x10000) 113 s_scale = 1; 114 else if (quot >= 0x100) 115 s_scale = 0x10000 / quot; 116 else if (o >= 0x800000) 117 s_scale = 0x1000000 / (o / (p->kcountsize >> 8)); 118 else 119 s_scale = 0x1000000 / ((o << 8) / p->kcountsize); 120#endif 121 } else 122 s_scale = SCALE_1_TO_1; 123 124 moncontrol(1); 125} 126 127void 128_mcleanup() 129{ 130 int fd; 131 int fromindex; 132 int endfrom; 133 u_long frompc; 134 int toindex; 135 struct rawarc rawarc; 136 struct gmonparam *p = &_gmonparam; 137 struct gmonhdr gmonhdr, *hdr; 138 struct clockinfo clockinfo; 139 int mib[2]; 140 size_t size; 141 char *profdir; 142 char *proffile; 143 char buf[PATH_MAX]; 144#ifdef DEBUG 145 int log, len; 146 char dbuf[200]; 147#endif 148 149 if (p->state == GMON_PROF_ERROR) 150 ERR("_mcleanup: tos overflow\n"); 151 152 size = sizeof(clockinfo); 153 mib[0] = CTL_KERN; 154 mib[1] = KERN_CLOCKRATE; 155 if (sysctl(mib, 2, &clockinfo, &size, NULL, 0) < 0) { 156 /* 157 * Best guess 158 */ 159 clockinfo.profhz = hertz(); 160 } else if (clockinfo.profhz == 0) { 161 if (clockinfo.hz != 0) 162 clockinfo.profhz = clockinfo.hz; 163 else 164 clockinfo.profhz = hertz(); 165 } 166 167 moncontrol(0); 168 169 if (issetugid() == 0 && (profdir = getenv("PROFDIR")) != NULL) { 170 extern char *__progname; 171 char *s, *t, *limit; 172 pid_t pid; 173 long divisor; 174 175 /* If PROFDIR contains a null value, no profiling 176 output is produced */ 177 if (*profdir == '\0') { 178 return; 179 } 180 181 limit = buf + sizeof buf - 1 - 10 - 1 - 182 strlen(__progname) - 1; 183 t = buf; 184 s = profdir; 185 while((*t = *s) != '\0' && t < limit) { 186 t++; 187 s++; 188 } 189 *t++ = '/'; 190 191 /* 192 * Copy and convert pid from a pid_t to a string. For 193 * best performance, divisor should be initialized to 194 * the largest power of 10 less than PID_MAX. 195 */ 196 pid = getpid(); 197 divisor=10000; 198 while (divisor > pid) divisor /= 10; /* skip leading zeros */ 199 do { 200 *t++ = (pid/divisor) + '0'; 201 pid %= divisor; 202 } while (divisor /= 10); 203 *t++ = '.'; 204 205 s = __progname; 206 while ((*t++ = *s++) != '\0') 207 ; 208 209 proffile = buf; 210 } else { 211 proffile = "gmon.out"; 212 } 213 214 fd = open(proffile , O_CREAT|O_TRUNC|O_WRONLY, 0666); 215 if (fd < 0) { 216 perror( proffile ); 217 return; 218 } 219#ifdef DEBUG 220 log = open("gmon.log", O_CREAT|O_TRUNC|O_WRONLY, 0664); 221 if (log < 0) { 222 perror("mcount: gmon.log"); 223 return; 224 } 225 len = sprintf(dbuf, "[mcleanup1] kcount 0x%x ssiz %d\n", 226 p->kcount, p->kcountsize); 227 write(log, dbuf, len); 228#endif 229 hdr = (struct gmonhdr *)&gmonhdr; 230 hdr->lpc = p->lowpc; 231 hdr->hpc = p->highpc; 232 hdr->ncnt = p->kcountsize + sizeof(gmonhdr); 233 hdr->version = GMONVERSION; 234 hdr->profrate = clockinfo.profhz; 235 write(fd, (char *)hdr, sizeof *hdr); 236 write(fd, p->kcount, p->kcountsize); 237 endfrom = p->fromssize / sizeof(*p->froms); 238 for (fromindex = 0; fromindex < endfrom; fromindex++) { 239 if (p->froms[fromindex] == 0) 240 continue; 241 242 frompc = p->lowpc; 243 frompc += fromindex * p->hashfraction * sizeof(*p->froms); 244 for (toindex = p->froms[fromindex]; toindex != 0; 245 toindex = p->tos[toindex].link) { 246#ifdef DEBUG 247 len = sprintf(dbuf, 248 "[mcleanup2] frompc 0x%x selfpc 0x%x count %d\n" , 249 frompc, p->tos[toindex].selfpc, 250 p->tos[toindex].count); 251 write(log, dbuf, len); 252#endif 253 rawarc.raw_frompc = frompc; 254 rawarc.raw_selfpc = p->tos[toindex].selfpc; 255 rawarc.raw_count = p->tos[toindex].count; 256 write(fd, &rawarc, sizeof rawarc); 257 } 258 } 259 close(fd); 260} 261 262/* 263 * Control profiling 264 * profiling is what mcount checks to see if 265 * all the data structures are ready. 266 */ 267void 268moncontrol(mode) 269 int mode; 270{ 271 struct gmonparam *p = &_gmonparam; 272 273 if (mode) { 274 /* start */ 275 profil((char *)p->kcount, p->kcountsize, p->lowpc, 276 s_scale); 277 p->state = GMON_PROF_ON; 278 } else { 279 /* stop */ 280 profil((char *)0, 0, 0, 0); 281 p->state = GMON_PROF_OFF; 282 } 283} 284 285/* 286 * discover the tick frequency of the machine 287 * if something goes wrong, we return 0, an impossible hertz. 288 */ 289static int 290hertz() 291{ 292 struct itimerval tim; 293 294 tim.it_interval.tv_sec = 0; 295 tim.it_interval.tv_usec = 1; 296 tim.it_value.tv_sec = 0; 297 tim.it_value.tv_usec = 0; 298 setitimer(ITIMER_REAL, &tim, 0); 299 setitimer(ITIMER_REAL, 0, &tim); 300 if (tim.it_interval.tv_usec < 2) 301 return(0); 302 return (1000000 / tim.it_interval.tv_usec); 303} 304 305 306