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