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. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 #if !defined(lint) && defined(LIBC_SCCS) 31 static char rcsid[] = "$OpenBSD: gmon.c,v 1.16 2003/06/25 21:16:47 deraadt Exp $"; 32 #endif 33 34 #include <sys/param.h> 35 #include <sys/time.h> 36 #include <sys/gmon.h> 37 #include <sys/sysctl.h> 38 39 #include <stdio.h> 40 #include <stdlib.h> 41 #include <string.h> 42 #include <fcntl.h> 43 #include <limits.h> 44 #include <unistd.h> 45 46 extern char *minbrk __asm ("minbrk"); 47 48 struct gmonparam _gmonparam = { GMON_PROF_OFF }; 49 50 static int s_scale; 51 /* see profil(2) where this is describe (incorrectly) */ 52 #define SCALE_1_TO_1 0x10000L 53 54 #define ERR(s) write(STDERR_FILENO, s, sizeof(s)) 55 56 void moncontrol(int); 57 static int hertz(void); 58 void monstartup(u_long lowpc, u_long highpc); 59 void _mcleanup(void); 60 61 void 62 monstartup(lowpc, highpc) 63 u_long lowpc; 64 u_long highpc; 65 { 66 register int o; 67 char *cp; 68 struct gmonparam *p = &_gmonparam; 69 70 /* 71 * round lowpc and highpc to multiples of the density we're using 72 * so the rest of the scaling (here and in gprof) stays in ints. 73 */ 74 p->lowpc = ROUNDDOWN(lowpc, HISTFRACTION * sizeof(HISTCOUNTER)); 75 p->highpc = ROUNDUP(highpc, HISTFRACTION * sizeof(HISTCOUNTER)); 76 p->textsize = p->highpc - p->lowpc; 77 p->kcountsize = p->textsize / HISTFRACTION; 78 p->hashfraction = HASHFRACTION; 79 p->fromssize = p->textsize / p->hashfraction; 80 p->tolimit = p->textsize * ARCDENSITY / 100; 81 if (p->tolimit < MINARCS) 82 p->tolimit = MINARCS; 83 else if (p->tolimit > MAXARCS) 84 p->tolimit = MAXARCS; 85 p->tossize = p->tolimit * sizeof(struct tostruct); 86 87 cp = sbrk(p->kcountsize + p->fromssize + p->tossize); 88 if (cp == (char *)-1) { 89 ERR("monstartup: out of memory\n"); 90 return; 91 } 92 #ifdef notdef 93 bzero(cp, p->kcountsize + p->fromssize + p->tossize); 94 #endif 95 p->tos = (struct tostruct *)cp; 96 cp += p->tossize; 97 p->kcount = (u_short *)cp; 98 cp += p->kcountsize; 99 p->froms = (u_short *)cp; 100 101 minbrk = sbrk(0); 102 p->tos[0].link = 0; 103 104 o = p->highpc - p->lowpc; 105 if (p->kcountsize < o) { 106 #ifndef notdef 107 s_scale = ((float)p->kcountsize / o ) * SCALE_1_TO_1; 108 #else /* avoid floating point */ 109 int quot = o / p->kcountsize; 110 111 if (quot >= 0x10000) 112 s_scale = 1; 113 else if (quot >= 0x100) 114 s_scale = 0x10000 / quot; 115 else if (o >= 0x800000) 116 s_scale = 0x1000000 / (o / (p->kcountsize >> 8)); 117 else 118 s_scale = 0x1000000 / ((o << 8) / p->kcountsize); 119 #endif 120 } else 121 s_scale = SCALE_1_TO_1; 122 123 moncontrol(1); 124 } 125 126 void 127 _mcleanup(void) 128 { 129 int fd; 130 int fromindex; 131 int endfrom; 132 u_long frompc; 133 int toindex; 134 struct rawarc rawarc; 135 struct gmonparam *p = &_gmonparam; 136 struct gmonhdr gmonhdr, *hdr; 137 struct clockinfo clockinfo; 138 int mib[2]; 139 size_t size; 140 char *profdir; 141 char *proffile; 142 char buf[PATH_MAX]; 143 #ifdef DEBUG 144 int log, len; 145 char dbuf[200]; 146 #endif 147 148 if (p->state == GMON_PROF_ERROR) 149 ERR("_mcleanup: tos overflow\n"); 150 151 size = sizeof(clockinfo); 152 mib[0] = CTL_KERN; 153 mib[1] = KERN_CLOCKRATE; 154 if (sysctl(mib, 2, &clockinfo, &size, NULL, 0) < 0) { 155 /* 156 * Best guess 157 */ 158 clockinfo.profhz = hertz(); 159 } else if (clockinfo.profhz == 0) { 160 if (clockinfo.hz != 0) 161 clockinfo.profhz = clockinfo.hz; 162 else 163 clockinfo.profhz = hertz(); 164 } 165 166 moncontrol(0); 167 168 if (issetugid() == 0 && (profdir = getenv("PROFDIR")) != NULL) { 169 extern char *__progname; 170 char *s, *t, *limit; 171 pid_t pid; 172 long divisor; 173 174 /* If PROFDIR contains a null value, no profiling 175 output is produced */ 176 if (*profdir == '\0') { 177 return; 178 } 179 180 limit = buf + sizeof buf - 1 - 10 - 1 - 181 strlen(__progname) - 1; 182 t = buf; 183 s = profdir; 184 while((*t = *s) != '\0' && t < limit) { 185 t++; 186 s++; 187 } 188 *t++ = '/'; 189 190 /* 191 * Copy and convert pid from a pid_t to a string. For 192 * best performance, divisor should be initialized to 193 * the largest power of 10 less than PID_MAX. 194 */ 195 pid = getpid(); 196 divisor=10000; 197 while (divisor > pid) divisor /= 10; /* skip leading zeros */ 198 do { 199 *t++ = (pid/divisor) + '0'; 200 pid %= divisor; 201 } while (divisor /= 10); 202 *t++ = '.'; 203 204 s = __progname; 205 while ((*t++ = *s++) != '\0') 206 ; 207 208 proffile = buf; 209 } else { 210 proffile = "gmon.out"; 211 } 212 213 fd = open(proffile , O_CREAT|O_TRUNC|O_WRONLY, 0664); 214 if (fd < 0) { 215 perror( proffile ); 216 return; 217 } 218 #ifdef DEBUG 219 log = open("gmon.log", O_CREAT|O_TRUNC|O_WRONLY, 0664); 220 if (log < 0) { 221 perror("mcount: gmon.log"); 222 return; 223 } 224 len = snprintf(dbuf, sizeof dbuf, "[mcleanup1] kcount 0x%x ssiz %d\n", 225 p->kcount, p->kcountsize); 226 write(log, dbuf, len); 227 #endif 228 hdr = (struct gmonhdr *)&gmonhdr; 229 bzero(hdr, sizeof(*hdr)); 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 = snprintf(dbuf, sizeof 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 */ 267 void 268 moncontrol(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 */ 289 static int 290 hertz() 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