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