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 * 4. 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(LIBC_SCCS) && !defined(lint) 31 static char sccsid[] = "@(#)gmon.c 8.1 (Berkeley) 6/4/93"; 32 #endif 33 #include <sys/cdefs.h> 34 __FBSDID("$FreeBSD$"); 35 36 #include "namespace.h" 37 #include <sys/param.h> 38 #include <sys/time.h> 39 #include <sys/gmon.h> 40 #include <sys/sysctl.h> 41 42 #include <err.h> 43 #include <fcntl.h> 44 #include <stdio.h> 45 #include <stdlib.h> 46 #include <string.h> 47 #include <unistd.h> 48 #include "un-namespace.h" 49 50 #include "libc_private.h" 51 52 #if defined(__i386__) || defined(__sparc64__) || defined(__amd64__) || defined(__powerpc__) 53 extern char *minbrk __asm (".minbrk"); 54 #else 55 extern char *minbrk __asm ("minbrk"); 56 #endif 57 58 struct gmonparam _gmonparam = { GMON_PROF_OFF }; 59 60 static int s_scale; 61 /* see profil(2) where this is describe (incorrectly) */ 62 #define SCALE_1_TO_1 0x10000L 63 64 #define ERR(s) _write(2, s, sizeof(s)) 65 66 void moncontrol(int); 67 static int hertz(void); 68 69 void 70 monstartup(lowpc, highpc) 71 u_long lowpc; 72 u_long highpc; 73 { 74 int o; 75 char *cp; 76 struct gmonparam *p = &_gmonparam; 77 78 /* 79 * round lowpc and highpc to multiples of the density we're using 80 * so the rest of the scaling (here and in gprof) stays in ints. 81 */ 82 p->lowpc = ROUNDDOWN(lowpc, HISTFRACTION * sizeof(HISTCOUNTER)); 83 p->highpc = ROUNDUP(highpc, HISTFRACTION * sizeof(HISTCOUNTER)); 84 p->textsize = p->highpc - p->lowpc; 85 p->kcountsize = p->textsize / HISTFRACTION; 86 p->hashfraction = HASHFRACTION; 87 p->fromssize = p->textsize / HASHFRACTION; 88 p->tolimit = p->textsize * ARCDENSITY / 100; 89 if (p->tolimit < MINARCS) 90 p->tolimit = MINARCS; 91 else if (p->tolimit > MAXARCS) 92 p->tolimit = MAXARCS; 93 p->tossize = p->tolimit * sizeof(struct tostruct); 94 95 cp = sbrk(p->kcountsize + p->fromssize + p->tossize); 96 if (cp == (char *)-1) { 97 ERR("monstartup: out of memory\n"); 98 return; 99 } 100 #ifdef notdef 101 bzero(cp, p->kcountsize + p->fromssize + p->tossize); 102 #endif 103 p->tos = (struct tostruct *)cp; 104 cp += p->tossize; 105 p->kcount = (u_short *)cp; 106 cp += p->kcountsize; 107 p->froms = (u_short *)cp; 108 109 minbrk = sbrk(0); 110 p->tos[0].link = 0; 111 112 o = p->highpc - p->lowpc; 113 if (p->kcountsize < o) { 114 #ifndef hp300 115 s_scale = ((float)p->kcountsize / o ) * SCALE_1_TO_1; 116 #else /* avoid floating point */ 117 int quot = o / p->kcountsize; 118 119 if (quot >= 0x10000) 120 s_scale = 1; 121 else if (quot >= 0x100) 122 s_scale = 0x10000 / quot; 123 else if (o >= 0x800000) 124 s_scale = 0x1000000 / (o / (p->kcountsize >> 8)); 125 else 126 s_scale = 0x1000000 / ((o << 8) / p->kcountsize); 127 #endif 128 } else 129 s_scale = SCALE_1_TO_1; 130 131 moncontrol(1); 132 } 133 134 void 135 _mcleanup() 136 { 137 int fd; 138 int fromindex; 139 int endfrom; 140 u_long frompc; 141 int toindex; 142 struct rawarc rawarc; 143 struct gmonparam *p = &_gmonparam; 144 struct gmonhdr gmonhdr, *hdr; 145 struct clockinfo clockinfo; 146 char outname[128]; 147 int mib[2]; 148 size_t size; 149 #ifdef DEBUG 150 int log, len; 151 char buf[200]; 152 #endif 153 154 if (p->state == GMON_PROF_ERROR) 155 ERR("_mcleanup: tos overflow\n"); 156 157 size = sizeof(clockinfo); 158 mib[0] = CTL_KERN; 159 mib[1] = KERN_CLOCKRATE; 160 if (sysctl(mib, 2, &clockinfo, &size, NULL, 0) < 0) { 161 /* 162 * Best guess 163 */ 164 clockinfo.profhz = hertz(); 165 } else if (clockinfo.profhz == 0) { 166 if (clockinfo.hz != 0) 167 clockinfo.profhz = clockinfo.hz; 168 else 169 clockinfo.profhz = hertz(); 170 } 171 172 moncontrol(0); 173 snprintf(outname, sizeof(outname), "%s.gmon", _getprogname()); 174 fd = _open(outname, O_CREAT|O_TRUNC|O_WRONLY, 0666); 175 if (fd < 0) { 176 _warn("_mcleanup: %s", outname); 177 return; 178 } 179 #ifdef DEBUG 180 log = _open("gmon.log", O_CREAT|O_TRUNC|O_WRONLY, 0664); 181 if (log < 0) { 182 _warn("_mcleanup: gmon.log"); 183 return; 184 } 185 len = sprintf(buf, "[mcleanup1] kcount 0x%p ssiz %lu\n", 186 p->kcount, p->kcountsize); 187 _write(log, buf, len); 188 #endif 189 hdr = (struct gmonhdr *)&gmonhdr; 190 bzero(hdr, sizeof(*hdr)); 191 hdr->lpc = p->lowpc; 192 hdr->hpc = p->highpc; 193 hdr->ncnt = p->kcountsize + sizeof(gmonhdr); 194 hdr->version = GMONVERSION; 195 hdr->profrate = clockinfo.profhz; 196 _write(fd, (char *)hdr, sizeof *hdr); 197 _write(fd, p->kcount, p->kcountsize); 198 endfrom = p->fromssize / sizeof(*p->froms); 199 for (fromindex = 0; fromindex < endfrom; fromindex++) { 200 if (p->froms[fromindex] == 0) 201 continue; 202 203 frompc = p->lowpc; 204 frompc += fromindex * p->hashfraction * sizeof(*p->froms); 205 for (toindex = p->froms[fromindex]; toindex != 0; 206 toindex = p->tos[toindex].link) { 207 #ifdef DEBUG 208 len = sprintf(buf, 209 "[mcleanup2] frompc 0x%lx selfpc 0x%lx count %lu\n" , 210 frompc, p->tos[toindex].selfpc, 211 p->tos[toindex].count); 212 _write(log, buf, len); 213 #endif 214 rawarc.raw_frompc = frompc; 215 rawarc.raw_selfpc = p->tos[toindex].selfpc; 216 rawarc.raw_count = p->tos[toindex].count; 217 _write(fd, &rawarc, sizeof rawarc); 218 } 219 } 220 _close(fd); 221 } 222 223 /* 224 * Control profiling 225 * profiling is what mcount checks to see if 226 * all the data structures are ready. 227 */ 228 void 229 moncontrol(mode) 230 int mode; 231 { 232 struct gmonparam *p = &_gmonparam; 233 234 if (mode) { 235 /* start */ 236 profil((char *)p->kcount, p->kcountsize, p->lowpc, s_scale); 237 p->state = GMON_PROF_ON; 238 } else { 239 /* stop */ 240 profil((char *)0, 0, 0, 0); 241 p->state = GMON_PROF_OFF; 242 } 243 } 244 245 /* 246 * discover the tick frequency of the machine 247 * if something goes wrong, we return 0, an impossible hertz. 248 */ 249 static int 250 hertz() 251 { 252 struct itimerval tim; 253 254 tim.it_interval.tv_sec = 0; 255 tim.it_interval.tv_usec = 1; 256 tim.it_value.tv_sec = 0; 257 tim.it_value.tv_usec = 0; 258 setitimer(ITIMER_REAL, &tim, 0); 259 setitimer(ITIMER_REAL, 0, &tim); 260 if (tim.it_interval.tv_usec < 2) 261 return(0); 262 return (1000000 / tim.it_interval.tv_usec); 263 } 264