158f0484fSRodney W. Grimes /*- 258f0484fSRodney W. Grimes * Copyright (c) 1983, 1992, 1993 358f0484fSRodney W. Grimes * The Regents of the University of California. All rights reserved. 458f0484fSRodney W. Grimes * 558f0484fSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 658f0484fSRodney W. Grimes * modification, are permitted provided that the following conditions 758f0484fSRodney W. Grimes * are met: 858f0484fSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 958f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 1058f0484fSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 1158f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 1258f0484fSRodney W. Grimes * documentation and/or other materials provided with the distribution. 1358f0484fSRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 1458f0484fSRodney W. Grimes * must display the following acknowledgement: 1558f0484fSRodney W. Grimes * This product includes software developed by the University of 1658f0484fSRodney W. Grimes * California, Berkeley and its contributors. 1758f0484fSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 1858f0484fSRodney W. Grimes * may be used to endorse or promote products derived from this software 1958f0484fSRodney W. Grimes * without specific prior written permission. 2058f0484fSRodney W. Grimes * 2158f0484fSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2258f0484fSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2358f0484fSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2458f0484fSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2558f0484fSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2658f0484fSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2758f0484fSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2858f0484fSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2958f0484fSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3058f0484fSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3158f0484fSRodney W. Grimes * SUCH DAMAGE. 3258f0484fSRodney W. Grimes */ 3358f0484fSRodney W. Grimes 3458f0484fSRodney W. Grimes #if !defined(lint) && defined(LIBC_SCCS) 3558f0484fSRodney W. Grimes static char sccsid[] = "@(#)gmon.c 8.1 (Berkeley) 6/4/93"; 3658f0484fSRodney W. Grimes #endif 37a99d1013SDavid E. O'Brien #include <sys/cdefs.h> 38a99d1013SDavid E. O'Brien __FBSDID("$FreeBSD$"); 3958f0484fSRodney W. Grimes 40d201fe46SDaniel Eischen #include "namespace.h" 4158f0484fSRodney W. Grimes #include <sys/param.h> 4258f0484fSRodney W. Grimes #include <sys/time.h> 4358f0484fSRodney W. Grimes #include <sys/gmon.h> 4458f0484fSRodney W. Grimes #include <sys/sysctl.h> 4558f0484fSRodney W. Grimes 46683728f1SJohn Birrell #include <err.h> 47683728f1SJohn Birrell #include <errno.h> 4858f0484fSRodney W. Grimes #include <stdio.h> 4958f0484fSRodney W. Grimes #include <fcntl.h> 50d201fe46SDaniel Eischen #include <string.h> 5158f0484fSRodney W. Grimes #include <unistd.h> 52d201fe46SDaniel Eischen #include "un-namespace.h" 5358f0484fSRodney W. Grimes 5402467533SJake Burkholder #if defined(__ELF__) && (defined(i386) || defined(__sparc64__)) 55f1703dfbSPeter Wemm extern char *minbrk asm (".minbrk"); 56f1703dfbSPeter Wemm #else 5758f0484fSRodney W. Grimes extern char *minbrk asm ("minbrk"); 58f1703dfbSPeter Wemm #endif 5958f0484fSRodney W. Grimes 60683728f1SJohn Birrell extern char *__progname; 61683728f1SJohn Birrell 6258f0484fSRodney W. Grimes struct gmonparam _gmonparam = { GMON_PROF_OFF }; 6358f0484fSRodney W. Grimes 6458f0484fSRodney W. Grimes static int s_scale; 6558f0484fSRodney W. Grimes /* see profil(2) where this is describe (incorrectly) */ 6658f0484fSRodney W. Grimes #define SCALE_1_TO_1 0x10000L 6758f0484fSRodney W. Grimes 689233c4d9SJason Evans #define ERR(s) _write(2, s, sizeof(s)) 6958f0484fSRodney W. Grimes 70a99d1013SDavid E. O'Brien void moncontrol(int); 71a99d1013SDavid E. O'Brien static int hertz(void); 7258f0484fSRodney W. Grimes 7358f0484fSRodney W. Grimes void 7458f0484fSRodney W. Grimes monstartup(lowpc, highpc) 7558f0484fSRodney W. Grimes u_long lowpc; 7658f0484fSRodney W. Grimes u_long highpc; 7758f0484fSRodney W. Grimes { 78a99d1013SDavid E. O'Brien int o; 7958f0484fSRodney W. Grimes char *cp; 8058f0484fSRodney W. Grimes struct gmonparam *p = &_gmonparam; 8158f0484fSRodney W. Grimes 8258f0484fSRodney W. Grimes /* 8358f0484fSRodney W. Grimes * round lowpc and highpc to multiples of the density we're using 8458f0484fSRodney W. Grimes * so the rest of the scaling (here and in gprof) stays in ints. 8558f0484fSRodney W. Grimes */ 8658f0484fSRodney W. Grimes p->lowpc = ROUNDDOWN(lowpc, HISTFRACTION * sizeof(HISTCOUNTER)); 8758f0484fSRodney W. Grimes p->highpc = ROUNDUP(highpc, HISTFRACTION * sizeof(HISTCOUNTER)); 8858f0484fSRodney W. Grimes p->textsize = p->highpc - p->lowpc; 8958f0484fSRodney W. Grimes p->kcountsize = p->textsize / HISTFRACTION; 9058f0484fSRodney W. Grimes p->hashfraction = HASHFRACTION; 9158f0484fSRodney W. Grimes p->fromssize = p->textsize / HASHFRACTION; 9258f0484fSRodney W. Grimes p->tolimit = p->textsize * ARCDENSITY / 100; 9358f0484fSRodney W. Grimes if (p->tolimit < MINARCS) 9458f0484fSRodney W. Grimes p->tolimit = MINARCS; 9558f0484fSRodney W. Grimes else if (p->tolimit > MAXARCS) 9658f0484fSRodney W. Grimes p->tolimit = MAXARCS; 9758f0484fSRodney W. Grimes p->tossize = p->tolimit * sizeof(struct tostruct); 9858f0484fSRodney W. Grimes 9958f0484fSRodney W. Grimes cp = sbrk(p->kcountsize + p->fromssize + p->tossize); 10058f0484fSRodney W. Grimes if (cp == (char *)-1) { 10158f0484fSRodney W. Grimes ERR("monstartup: out of memory\n"); 10258f0484fSRodney W. Grimes return; 10358f0484fSRodney W. Grimes } 10458f0484fSRodney W. Grimes #ifdef notdef 10558f0484fSRodney W. Grimes bzero(cp, p->kcountsize + p->fromssize + p->tossize); 10658f0484fSRodney W. Grimes #endif 10758f0484fSRodney W. Grimes p->tos = (struct tostruct *)cp; 10858f0484fSRodney W. Grimes cp += p->tossize; 10958f0484fSRodney W. Grimes p->kcount = (u_short *)cp; 11058f0484fSRodney W. Grimes cp += p->kcountsize; 11158f0484fSRodney W. Grimes p->froms = (u_short *)cp; 11258f0484fSRodney W. Grimes 11358f0484fSRodney W. Grimes minbrk = sbrk(0); 11458f0484fSRodney W. Grimes p->tos[0].link = 0; 11558f0484fSRodney W. Grimes 11658f0484fSRodney W. Grimes o = p->highpc - p->lowpc; 11758f0484fSRodney W. Grimes if (p->kcountsize < o) { 11858f0484fSRodney W. Grimes #ifndef hp300 11958f0484fSRodney W. Grimes s_scale = ((float)p->kcountsize / o ) * SCALE_1_TO_1; 12058f0484fSRodney W. Grimes #else /* avoid floating point */ 12158f0484fSRodney W. Grimes int quot = o / p->kcountsize; 12258f0484fSRodney W. Grimes 12358f0484fSRodney W. Grimes if (quot >= 0x10000) 12458f0484fSRodney W. Grimes s_scale = 1; 12558f0484fSRodney W. Grimes else if (quot >= 0x100) 12658f0484fSRodney W. Grimes s_scale = 0x10000 / quot; 12758f0484fSRodney W. Grimes else if (o >= 0x800000) 12858f0484fSRodney W. Grimes s_scale = 0x1000000 / (o / (p->kcountsize >> 8)); 12958f0484fSRodney W. Grimes else 13058f0484fSRodney W. Grimes s_scale = 0x1000000 / ((o << 8) / p->kcountsize); 13158f0484fSRodney W. Grimes #endif 13258f0484fSRodney W. Grimes } else 13358f0484fSRodney W. Grimes s_scale = SCALE_1_TO_1; 13458f0484fSRodney W. Grimes 13558f0484fSRodney W. Grimes moncontrol(1); 13658f0484fSRodney W. Grimes } 13758f0484fSRodney W. Grimes 13858f0484fSRodney W. Grimes void 13958f0484fSRodney W. Grimes _mcleanup() 14058f0484fSRodney W. Grimes { 14158f0484fSRodney W. Grimes int fd; 14258f0484fSRodney W. Grimes int fromindex; 14358f0484fSRodney W. Grimes int endfrom; 14458f0484fSRodney W. Grimes u_long frompc; 14558f0484fSRodney W. Grimes int toindex; 14658f0484fSRodney W. Grimes struct rawarc rawarc; 14758f0484fSRodney W. Grimes struct gmonparam *p = &_gmonparam; 14858f0484fSRodney W. Grimes struct gmonhdr gmonhdr, *hdr; 14958f0484fSRodney W. Grimes struct clockinfo clockinfo; 150683728f1SJohn Birrell char outname[128]; 15158f0484fSRodney W. Grimes int mib[2]; 15258f0484fSRodney W. Grimes size_t size; 15358f0484fSRodney W. Grimes #ifdef DEBUG 15458f0484fSRodney W. Grimes int log, len; 15558f0484fSRodney W. Grimes char buf[200]; 15658f0484fSRodney W. Grimes #endif 15758f0484fSRodney W. Grimes 15858f0484fSRodney W. Grimes if (p->state == GMON_PROF_ERROR) 15958f0484fSRodney W. Grimes ERR("_mcleanup: tos overflow\n"); 16058f0484fSRodney W. Grimes 16158f0484fSRodney W. Grimes size = sizeof(clockinfo); 16258f0484fSRodney W. Grimes mib[0] = CTL_KERN; 16358f0484fSRodney W. Grimes mib[1] = KERN_CLOCKRATE; 16458f0484fSRodney W. Grimes if (sysctl(mib, 2, &clockinfo, &size, NULL, 0) < 0) { 16558f0484fSRodney W. Grimes /* 16658f0484fSRodney W. Grimes * Best guess 16758f0484fSRodney W. Grimes */ 16858f0484fSRodney W. Grimes clockinfo.profhz = hertz(); 16958f0484fSRodney W. Grimes } else if (clockinfo.profhz == 0) { 17058f0484fSRodney W. Grimes if (clockinfo.hz != 0) 17158f0484fSRodney W. Grimes clockinfo.profhz = clockinfo.hz; 17258f0484fSRodney W. Grimes else 17358f0484fSRodney W. Grimes clockinfo.profhz = hertz(); 17458f0484fSRodney W. Grimes } 17558f0484fSRodney W. Grimes 17658f0484fSRodney W. Grimes moncontrol(0); 177683728f1SJohn Birrell snprintf(outname,sizeof(outname),"%s.gmon",__progname); 1789233c4d9SJason Evans fd = _open(outname, O_CREAT|O_TRUNC|O_WRONLY, 0666); 17958f0484fSRodney W. Grimes if (fd < 0) { 180683728f1SJohn Birrell warnx("_mcleanup: %s - %s",outname,strerror(errno)); 18158f0484fSRodney W. Grimes return; 18258f0484fSRodney W. Grimes } 18358f0484fSRodney W. Grimes #ifdef DEBUG 1849233c4d9SJason Evans log = _open("gmon.log", O_CREAT|O_TRUNC|O_WRONLY, 0664); 18558f0484fSRodney W. Grimes if (log < 0) { 186683728f1SJohn Birrell perror("_mcleanup: gmon.log"); 18758f0484fSRodney W. Grimes return; 18858f0484fSRodney W. Grimes } 18958f0484fSRodney W. Grimes len = sprintf(buf, "[mcleanup1] kcount 0x%x ssiz %d\n", 19058f0484fSRodney W. Grimes p->kcount, p->kcountsize); 1919233c4d9SJason Evans _write(log, buf, len); 19258f0484fSRodney W. Grimes #endif 19358f0484fSRodney W. Grimes hdr = (struct gmonhdr *)&gmonhdr; 19458f0484fSRodney W. Grimes hdr->lpc = p->lowpc; 19558f0484fSRodney W. Grimes hdr->hpc = p->highpc; 19658f0484fSRodney W. Grimes hdr->ncnt = p->kcountsize + sizeof(gmonhdr); 19758f0484fSRodney W. Grimes hdr->version = GMONVERSION; 19858f0484fSRodney W. Grimes hdr->profrate = clockinfo.profhz; 1999233c4d9SJason Evans _write(fd, (char *)hdr, sizeof *hdr); 2009233c4d9SJason Evans _write(fd, p->kcount, p->kcountsize); 20158f0484fSRodney W. Grimes endfrom = p->fromssize / sizeof(*p->froms); 20258f0484fSRodney W. Grimes for (fromindex = 0; fromindex < endfrom; fromindex++) { 20358f0484fSRodney W. Grimes if (p->froms[fromindex] == 0) 20458f0484fSRodney W. Grimes continue; 20558f0484fSRodney W. Grimes 20658f0484fSRodney W. Grimes frompc = p->lowpc; 20758f0484fSRodney W. Grimes frompc += fromindex * p->hashfraction * sizeof(*p->froms); 20858f0484fSRodney W. Grimes for (toindex = p->froms[fromindex]; toindex != 0; 20958f0484fSRodney W. Grimes toindex = p->tos[toindex].link) { 21058f0484fSRodney W. Grimes #ifdef DEBUG 21158f0484fSRodney W. Grimes len = sprintf(buf, 21258f0484fSRodney W. Grimes "[mcleanup2] frompc 0x%x selfpc 0x%x count %d\n" , 21358f0484fSRodney W. Grimes frompc, p->tos[toindex].selfpc, 21458f0484fSRodney W. Grimes p->tos[toindex].count); 2159233c4d9SJason Evans _write(log, buf, len); 21658f0484fSRodney W. Grimes #endif 21758f0484fSRodney W. Grimes rawarc.raw_frompc = frompc; 21858f0484fSRodney W. Grimes rawarc.raw_selfpc = p->tos[toindex].selfpc; 21958f0484fSRodney W. Grimes rawarc.raw_count = p->tos[toindex].count; 2209233c4d9SJason Evans _write(fd, &rawarc, sizeof rawarc); 22158f0484fSRodney W. Grimes } 22258f0484fSRodney W. Grimes } 2239233c4d9SJason Evans _close(fd); 22458f0484fSRodney W. Grimes } 22558f0484fSRodney W. Grimes 22658f0484fSRodney W. Grimes /* 22758f0484fSRodney W. Grimes * Control profiling 22858f0484fSRodney W. Grimes * profiling is what mcount checks to see if 22958f0484fSRodney W. Grimes * all the data structures are ready. 23058f0484fSRodney W. Grimes */ 23158f0484fSRodney W. Grimes void 23258f0484fSRodney W. Grimes moncontrol(mode) 23358f0484fSRodney W. Grimes int mode; 23458f0484fSRodney W. Grimes { 23558f0484fSRodney W. Grimes struct gmonparam *p = &_gmonparam; 23658f0484fSRodney W. Grimes 23758f0484fSRodney W. Grimes if (mode) { 23858f0484fSRodney W. Grimes /* start */ 239daa2e8d8SHidetoshi Shimokawa profil((char *)p->kcount, p->kcountsize, p->lowpc, s_scale); 24058f0484fSRodney W. Grimes p->state = GMON_PROF_ON; 24158f0484fSRodney W. Grimes } else { 24258f0484fSRodney W. Grimes /* stop */ 24358f0484fSRodney W. Grimes profil((char *)0, 0, 0, 0); 24458f0484fSRodney W. Grimes p->state = GMON_PROF_OFF; 24558f0484fSRodney W. Grimes } 24658f0484fSRodney W. Grimes } 24758f0484fSRodney W. Grimes 24858f0484fSRodney W. Grimes /* 24958f0484fSRodney W. Grimes * discover the tick frequency of the machine 25058f0484fSRodney W. Grimes * if something goes wrong, we return 0, an impossible hertz. 25158f0484fSRodney W. Grimes */ 25258f0484fSRodney W. Grimes static int 25358f0484fSRodney W. Grimes hertz() 25458f0484fSRodney W. Grimes { 25558f0484fSRodney W. Grimes struct itimerval tim; 25658f0484fSRodney W. Grimes 25758f0484fSRodney W. Grimes tim.it_interval.tv_sec = 0; 25858f0484fSRodney W. Grimes tim.it_interval.tv_usec = 1; 25958f0484fSRodney W. Grimes tim.it_value.tv_sec = 0; 26058f0484fSRodney W. Grimes tim.it_value.tv_usec = 0; 26158f0484fSRodney W. Grimes setitimer(ITIMER_REAL, &tim, 0); 26258f0484fSRodney W. Grimes setitimer(ITIMER_REAL, 0, &tim); 26358f0484fSRodney W. Grimes if (tim.it_interval.tv_usec < 2) 26458f0484fSRodney W. Grimes return(0); 26558f0484fSRodney W. Grimes return (1000000 / tim.it_interval.tv_usec); 26658f0484fSRodney W. Grimes } 267