Home | History | Annotate | Line # | Download | only in gmon
gmon.c revision 1.2.4.1
      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. All advertising materials mentioning features or use of this software
     14  *    must display the following acknowledgement:
     15  *	This product includes software developed by the University of
     16  *	California, Berkeley and its contributors.
     17  * 4. Neither the name of the University nor the names of its contributors
     18  *    may be used to endorse or promote products derived from this software
     19  *    without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  * SUCH DAMAGE.
     32  */
     33 
     34 #if !defined(lint) && defined(LIBC_SCCS)
     35 /* from: static char sccsid[] = "@(#)gmon.c	8.1 (Berkeley) 6/4/93"; */
     36 static char *rcsid = "$Id: gmon.c,v 1.2.4.1 1995/05/02 19:35:34 jtc Exp $";
     37 #endif
     38 
     39 #include "namespace.h"
     40 #include <sys/param.h>
     41 #include <sys/time.h>
     42 #include <sys/gmon.h>
     43 #include <sys/sysctl.h>
     44 
     45 #include <stdio.h>
     46 #include <fcntl.h>
     47 #include <unistd.h>
     48 
     49 extern char *minbrk asm ("minbrk");
     50 
     51 struct gmonparam _gmonparam = { GMON_PROF_OFF };
     52 
     53 static int	s_scale;
     54 /* see profil(2) where this is describe (incorrectly) */
     55 #define		SCALE_1_TO_1	0x10000L
     56 
     57 #define ERR(s) write(2, s, sizeof(s))
     58 
     59 void	moncontrol __P((int));
     60 static int hertz __P((void));
     61 
     62 void
     63 monstartup(lowpc, highpc)
     64 	u_long lowpc;
     65 	u_long highpc;
     66 {
     67 	register int o;
     68 	char *cp;
     69 	struct gmonparam *p = &_gmonparam;
     70 
     71 	/*
     72 	 * round lowpc and highpc to multiples of the density we're using
     73 	 * so the rest of the scaling (here and in gprof) stays in ints.
     74 	 */
     75 	p->lowpc = ROUNDDOWN(lowpc, HISTFRACTION * sizeof(HISTCOUNTER));
     76 	p->highpc = ROUNDUP(highpc, HISTFRACTION * sizeof(HISTCOUNTER));
     77 	p->textsize = p->highpc - p->lowpc;
     78 	p->kcountsize = p->textsize / HISTFRACTION;
     79 	p->hashfraction = HASHFRACTION;
     80 	p->fromssize = p->textsize / HASHFRACTION;
     81 	p->tolimit = p->textsize * ARCDENSITY / 100;
     82 	if (p->tolimit < MINARCS)
     83 		p->tolimit = MINARCS;
     84 	else if (p->tolimit > MAXARCS)
     85 		p->tolimit = MAXARCS;
     86 	p->tossize = p->tolimit * sizeof(struct tostruct);
     87 
     88 	cp = sbrk(p->kcountsize + p->fromssize + p->tossize);
     89 	if (cp == (char *)-1) {
     90 		ERR("monstartup: out of memory\n");
     91 		return;
     92 	}
     93 #ifdef notdef
     94 	bzero(cp, p->kcountsize + p->fromssize + p->tossize);
     95 #endif
     96 	p->tos = (struct tostruct *)cp;
     97 	cp += p->tossize;
     98 	p->kcount = (u_short *)cp;
     99 	cp += p->kcountsize;
    100 	p->froms = (u_short *)cp;
    101 
    102 	minbrk = sbrk(0);
    103 	p->tos[0].link = 0;
    104 
    105 	o = p->highpc - p->lowpc;
    106 	if (p->kcountsize < o) {
    107 #ifndef notdef
    108 		s_scale = ((float)p->kcountsize / o ) * SCALE_1_TO_1;
    109 #else /* avoid floating point */
    110 		int quot = o / p->kcountsize;
    111 
    112 		if (quot >= 0x10000)
    113 			s_scale = 1;
    114 		else if (quot >= 0x100)
    115 			s_scale = 0x10000 / quot;
    116 		else if (o >= 0x800000)
    117 			s_scale = 0x1000000 / (o / (p->kcountsize >> 8));
    118 		else
    119 			s_scale = 0x1000000 / ((o << 8) / p->kcountsize);
    120 #endif
    121 	} else
    122 		s_scale = SCALE_1_TO_1;
    123 
    124 	moncontrol(1);
    125 }
    126 
    127 void
    128 _mcleanup()
    129 {
    130 	int fd;
    131 	int fromindex;
    132 	int endfrom;
    133 	u_long frompc;
    134 	int toindex;
    135 	struct rawarc rawarc;
    136 	struct gmonparam *p = &_gmonparam;
    137 	struct gmonhdr gmonhdr, *hdr;
    138 	struct clockinfo clockinfo;
    139 	int mib[2];
    140 	size_t size;
    141 #ifdef DEBUG
    142 	int log, len;
    143 	char buf[200];
    144 #endif
    145 
    146 	if (p->state == GMON_PROF_ERROR)
    147 		ERR("_mcleanup: tos overflow\n");
    148 
    149 	size = sizeof(clockinfo);
    150 	mib[0] = CTL_KERN;
    151 	mib[1] = KERN_CLOCKRATE;
    152 	if (sysctl(mib, 2, &clockinfo, &size, NULL, 0) < 0) {
    153 		/*
    154 		 * Best guess
    155 		 */
    156 		clockinfo.profhz = hertz();
    157 	} else if (clockinfo.profhz == 0) {
    158 		if (clockinfo.hz != 0)
    159 			clockinfo.profhz = clockinfo.hz;
    160 		else
    161 			clockinfo.profhz = hertz();
    162 	}
    163 
    164 	moncontrol(0);
    165 	fd = open("gmon.out", O_CREAT|O_TRUNC|O_WRONLY, 0666);
    166 	if (fd < 0) {
    167 		perror("mcount: gmon.out");
    168 		return;
    169 	}
    170 #ifdef DEBUG
    171 	log = open("gmon.log", O_CREAT|O_TRUNC|O_WRONLY, 0664);
    172 	if (log < 0) {
    173 		perror("mcount: gmon.log");
    174 		return;
    175 	}
    176 	len = sprintf(buf, "[mcleanup1] kcount 0x%x ssiz %d\n",
    177 	    p->kcount, p->kcountsize);
    178 	write(log, buf, len);
    179 #endif
    180 	hdr = (struct gmonhdr *)&gmonhdr;
    181 	hdr->lpc = p->lowpc;
    182 	hdr->hpc = p->highpc;
    183 	hdr->ncnt = p->kcountsize + sizeof(gmonhdr);
    184 	hdr->version = GMONVERSION;
    185 	hdr->profrate = clockinfo.profhz;
    186 	write(fd, (char *)hdr, sizeof *hdr);
    187 	write(fd, p->kcount, p->kcountsize);
    188 	endfrom = p->fromssize / sizeof(*p->froms);
    189 	for (fromindex = 0; fromindex < endfrom; fromindex++) {
    190 		if (p->froms[fromindex] == 0)
    191 			continue;
    192 
    193 		frompc = p->lowpc;
    194 		frompc += fromindex * p->hashfraction * sizeof(*p->froms);
    195 		for (toindex = p->froms[fromindex]; toindex != 0;
    196 		     toindex = p->tos[toindex].link) {
    197 #ifdef DEBUG
    198 			len = sprintf(buf,
    199 			"[mcleanup2] frompc 0x%x selfpc 0x%x count %d\n" ,
    200 				frompc, p->tos[toindex].selfpc,
    201 				p->tos[toindex].count);
    202 			write(log, buf, len);
    203 #endif
    204 			rawarc.raw_frompc = frompc;
    205 			rawarc.raw_selfpc = p->tos[toindex].selfpc;
    206 			rawarc.raw_count = p->tos[toindex].count;
    207 			write(fd, &rawarc, sizeof rawarc);
    208 		}
    209 	}
    210 	close(fd);
    211 }
    212 
    213 /*
    214  * Control profiling
    215  *	profiling is what mcount checks to see if
    216  *	all the data structures are ready.
    217  */
    218 void
    219 moncontrol(mode)
    220 	int mode;
    221 {
    222 	struct gmonparam *p = &_gmonparam;
    223 
    224 	if (mode) {
    225 		/* start */
    226 		profil((char *)p->kcount, p->kcountsize, (int)p->lowpc,
    227 		    s_scale);
    228 		p->state = GMON_PROF_ON;
    229 	} else {
    230 		/* stop */
    231 		profil((char *)0, 0, 0, 0);
    232 		p->state = GMON_PROF_OFF;
    233 	}
    234 }
    235 
    236 /*
    237  * discover the tick frequency of the machine
    238  * if something goes wrong, we return 0, an impossible hertz.
    239  */
    240 static int
    241 hertz()
    242 {
    243 	struct itimerval tim;
    244 
    245 	tim.it_interval.tv_sec = 0;
    246 	tim.it_interval.tv_usec = 1;
    247 	tim.it_value.tv_sec = 0;
    248 	tim.it_value.tv_usec = 0;
    249 	setitimer(ITIMER_REAL, &tim, 0);
    250 	setitimer(ITIMER_REAL, 0, &tim);
    251 	if (tim.it_interval.tv_usec < 2)
    252 		return(0);
    253 	return (1000000 / tim.it_interval.tv_usec);
    254 }
    255 
    256 
    257