Home | History | Annotate | Line # | Download | only in gmon
gmon.c revision 1.21
      1  1.21       agc /*	$NetBSD: gmon.c,v 1.21 2003/08/07 16:43:02 agc Exp $	*/
      2   1.3       cgd 
      3   1.1       cgd /*-
      4   1.1       cgd  * Copyright (c) 1983, 1992, 1993
      5   1.1       cgd  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1       cgd  * modification, are permitted provided that the following conditions
      9   1.1       cgd  * are met:
     10   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15  1.21       agc  * 3. Neither the name of the University nor the names of its contributors
     16   1.1       cgd  *    may be used to endorse or promote products derived from this software
     17   1.1       cgd  *    without specific prior written permission.
     18   1.1       cgd  *
     19   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.1       cgd  * SUCH DAMAGE.
     30   1.1       cgd  */
     31   1.1       cgd 
     32  1.10  christos #include <sys/cdefs.h>
     33   1.1       cgd #if !defined(lint) && defined(LIBC_SCCS)
     34   1.3       cgd #if 0
     35   1.3       cgd static char sccsid[] = "@(#)gmon.c	8.1 (Berkeley) 6/4/93";
     36   1.3       cgd #else
     37  1.21       agc __RCSID("$NetBSD: gmon.c,v 1.21 2003/08/07 16:43:02 agc Exp $");
     38   1.3       cgd #endif
     39   1.1       cgd #endif
     40   1.1       cgd 
     41  1.10  christos #include "namespace.h"
     42   1.1       cgd #include <sys/param.h>
     43   1.1       cgd #include <sys/time.h>
     44   1.1       cgd #include <sys/gmon.h>
     45   1.1       cgd #include <sys/sysctl.h>
     46   1.1       cgd 
     47   1.1       cgd #include <stdio.h>
     48   1.5       jtc #include <stdlib.h>
     49   1.1       cgd #include <fcntl.h>
     50   1.5       jtc #include <limits.h>
     51   1.1       cgd #include <unistd.h>
     52  1.10  christos #include <err.h>
     53  1.15    kleink #include "extern.h"
     54   1.1       cgd 
     55   1.1       cgd struct gmonparam _gmonparam = { GMON_PROF_OFF };
     56   1.1       cgd 
     57  1.14  christos static u_int	s_scale;
     58   1.1       cgd /* see profil(2) where this is describe (incorrectly) */
     59   1.1       cgd #define		SCALE_1_TO_1	0x10000L
     60   1.1       cgd 
     61  1.12    kleink #define ERR(s) write(STDERR_FILENO, s, sizeof(s))
     62   1.1       cgd 
     63   1.1       cgd void	moncontrol __P((int));
     64  1.10  christos void	monstartup __P((u_long, u_long));
     65  1.10  christos void	_mcleanup __P((void));
     66   1.1       cgd static int hertz __P((void));
     67   1.1       cgd 
     68  1.16  christos 
     69   1.1       cgd void
     70   1.1       cgd monstartup(lowpc, highpc)
     71   1.1       cgd 	u_long lowpc;
     72   1.1       cgd 	u_long highpc;
     73   1.1       cgd {
     74  1.19   thorpej 	u_long o;
     75   1.1       cgd 	char *cp;
     76   1.1       cgd 	struct gmonparam *p = &_gmonparam;
     77   1.1       cgd 
     78   1.1       cgd 	/*
     79   1.1       cgd 	 * round lowpc and highpc to multiples of the density we're using
     80   1.1       cgd 	 * so the rest of the scaling (here and in gprof) stays in ints.
     81   1.1       cgd 	 */
     82   1.1       cgd 	p->lowpc = ROUNDDOWN(lowpc, HISTFRACTION * sizeof(HISTCOUNTER));
     83   1.1       cgd 	p->highpc = ROUNDUP(highpc, HISTFRACTION * sizeof(HISTCOUNTER));
     84   1.1       cgd 	p->textsize = p->highpc - p->lowpc;
     85   1.1       cgd 	p->kcountsize = p->textsize / HISTFRACTION;
     86   1.1       cgd 	p->hashfraction = HASHFRACTION;
     87   1.6       cgd 	p->fromssize = p->textsize / p->hashfraction;
     88   1.1       cgd 	p->tolimit = p->textsize * ARCDENSITY / 100;
     89   1.1       cgd 	if (p->tolimit < MINARCS)
     90   1.1       cgd 		p->tolimit = MINARCS;
     91   1.1       cgd 	else if (p->tolimit > MAXARCS)
     92   1.1       cgd 		p->tolimit = MAXARCS;
     93   1.1       cgd 	p->tossize = p->tolimit * sizeof(struct tostruct);
     94   1.1       cgd 
     95  1.16  christos 	cp = sbrk((intptr_t)(p->kcountsize + p->fromssize + p->tossize));
     96   1.1       cgd 	if (cp == (char *)-1) {
     97   1.1       cgd 		ERR("monstartup: out of memory\n");
     98   1.1       cgd 		return;
     99   1.1       cgd 	}
    100   1.1       cgd #ifdef notdef
    101  1.13     perry 	memset(cp, 0, p->kcountsize + p->fromssize + p->tossize);
    102   1.1       cgd #endif
    103  1.14  christos 	p->tos = (struct tostruct *)(void *)cp;
    104  1.14  christos 	cp += (size_t)p->tossize;
    105  1.14  christos 	p->kcount = (u_short *)(void *)cp;
    106  1.14  christos 	cp += (size_t)p->kcountsize;
    107  1.14  christos 	p->froms = (u_short *)(void *)cp;
    108   1.1       cgd 
    109  1.16  christos 	__minbrk = sbrk((intptr_t)0);
    110   1.1       cgd 	p->tos[0].link = 0;
    111   1.1       cgd 
    112   1.1       cgd 	o = p->highpc - p->lowpc;
    113   1.1       cgd 	if (p->kcountsize < o) {
    114   1.2       cgd #ifndef notdef
    115   1.1       cgd 		s_scale = ((float)p->kcountsize / o ) * SCALE_1_TO_1;
    116   1.1       cgd #else /* avoid floating point */
    117  1.19   thorpej 		u_long quot = o / p->kcountsize;
    118   1.1       cgd 
    119   1.1       cgd 		if (quot >= 0x10000)
    120   1.1       cgd 			s_scale = 1;
    121   1.1       cgd 		else if (quot >= 0x100)
    122   1.1       cgd 			s_scale = 0x10000 / quot;
    123   1.1       cgd 		else if (o >= 0x800000)
    124   1.1       cgd 			s_scale = 0x1000000 / (o / (p->kcountsize >> 8));
    125   1.1       cgd 		else
    126   1.1       cgd 			s_scale = 0x1000000 / ((o << 8) / p->kcountsize);
    127   1.1       cgd #endif
    128   1.1       cgd 	} else
    129   1.1       cgd 		s_scale = SCALE_1_TO_1;
    130   1.1       cgd 
    131   1.1       cgd 	moncontrol(1);
    132   1.1       cgd }
    133   1.1       cgd 
    134   1.1       cgd void
    135   1.1       cgd _mcleanup()
    136   1.1       cgd {
    137   1.1       cgd 	int fd;
    138   1.1       cgd 	int fromindex;
    139   1.1       cgd 	int endfrom;
    140   1.1       cgd 	u_long frompc;
    141   1.1       cgd 	int toindex;
    142   1.1       cgd 	struct rawarc rawarc;
    143   1.1       cgd 	struct gmonparam *p = &_gmonparam;
    144   1.1       cgd 	struct gmonhdr gmonhdr, *hdr;
    145   1.1       cgd 	struct clockinfo clockinfo;
    146   1.1       cgd 	int mib[2];
    147   1.1       cgd 	size_t size;
    148   1.5       jtc 	char *profdir;
    149   1.5       jtc 	char *proffile;
    150   1.5       jtc 	char  buf[PATH_MAX];
    151   1.1       cgd #ifdef DEBUG
    152   1.1       cgd 	int log, len;
    153   1.9       mrg 	char buf2[200];
    154   1.1       cgd #endif
    155  1.18  christos 
    156  1.18  christos 	/*
    157  1.18  christos 	 * We disallow writing to the profiling file, if we are a
    158  1.18  christos 	 * set{u,g}id program and our effective {u,g}id does not match
    159  1.18  christos 	 * our real one.
    160  1.18  christos 	 */
    161  1.18  christos 	if (issetugid() && (geteuid() != getuid() || getegid() != getgid())) {
    162  1.18  christos 		warnx("mcount: Profiling of set{u,g}id binaries is not"
    163  1.18  christos 		    " allowed");
    164  1.18  christos 		return;
    165  1.18  christos 	}
    166   1.1       cgd 
    167   1.1       cgd 	if (p->state == GMON_PROF_ERROR)
    168   1.1       cgd 		ERR("_mcleanup: tos overflow\n");
    169   1.1       cgd 
    170   1.1       cgd 	size = sizeof(clockinfo);
    171   1.1       cgd 	mib[0] = CTL_KERN;
    172   1.1       cgd 	mib[1] = KERN_CLOCKRATE;
    173   1.1       cgd 	if (sysctl(mib, 2, &clockinfo, &size, NULL, 0) < 0) {
    174   1.1       cgd 		/*
    175   1.1       cgd 		 * Best guess
    176   1.1       cgd 		 */
    177   1.1       cgd 		clockinfo.profhz = hertz();
    178   1.1       cgd 	} else if (clockinfo.profhz == 0) {
    179   1.1       cgd 		if (clockinfo.hz != 0)
    180   1.1       cgd 			clockinfo.profhz = clockinfo.hz;
    181   1.1       cgd 		else
    182   1.1       cgd 			clockinfo.profhz = hertz();
    183   1.1       cgd 	}
    184   1.1       cgd 
    185   1.1       cgd 	moncontrol(0);
    186   1.5       jtc 
    187   1.5       jtc 	if ((profdir = getenv("PROFDIR")) != NULL) {
    188   1.5       jtc 		/* If PROFDIR contains a null value, no profiling
    189   1.5       jtc 		   output is produced */
    190  1.20       dsl 		if (*profdir == '\0')
    191  1.20       dsl 			return;
    192  1.20       dsl 
    193  1.20       dsl 		if (snprintf(buf, sizeof buf, "%s/%d.%s",
    194  1.20       dsl 			    profdir, getpid(), getprogname()) >= sizeof buf) {
    195  1.20       dsl 			warnx("_mcleanup: internal buffer overflow, PROFDIR too long");
    196   1.5       jtc 			return;
    197   1.5       jtc 		}
    198   1.5       jtc 
    199   1.5       jtc 		proffile = buf;
    200   1.5       jtc 	} else {
    201   1.5       jtc 		proffile = "gmon.out";
    202   1.5       jtc 	}
    203   1.5       jtc 
    204   1.5       jtc 	fd = open(proffile , O_CREAT|O_TRUNC|O_WRONLY, 0666);
    205   1.1       cgd 	if (fd < 0) {
    206  1.10  christos 		warn("mcount: Cannot open `%s'", proffile);
    207   1.1       cgd 		return;
    208   1.1       cgd 	}
    209   1.1       cgd #ifdef DEBUG
    210   1.1       cgd 	log = open("gmon.log", O_CREAT|O_TRUNC|O_WRONLY, 0664);
    211   1.1       cgd 	if (log < 0) {
    212  1.10  christos 		warn("mcount: Cannot open `gmon.log'");
    213   1.1       cgd 		return;
    214   1.1       cgd 	}
    215   1.9       mrg 	len = snprintf(buf2, sizeof buf2, "[mcleanup1] kcount 0x%x ssiz %d\n",
    216   1.1       cgd 	    p->kcount, p->kcountsize);
    217   1.9       mrg 	write(log, buf2, len);
    218   1.1       cgd #endif
    219   1.1       cgd 	hdr = (struct gmonhdr *)&gmonhdr;
    220   1.1       cgd 	hdr->lpc = p->lowpc;
    221   1.1       cgd 	hdr->hpc = p->highpc;
    222  1.14  christos 	hdr->ncnt = (int)(p->kcountsize + sizeof(gmonhdr));
    223   1.1       cgd 	hdr->version = GMONVERSION;
    224   1.1       cgd 	hdr->profrate = clockinfo.profhz;
    225  1.14  christos 	(void)write(fd, hdr, sizeof *hdr);
    226  1.14  christos 	(void)write(fd, p->kcount, (size_t)p->kcountsize);
    227  1.14  christos 	endfrom = (int)(p->fromssize / sizeof(*p->froms));
    228   1.1       cgd 	for (fromindex = 0; fromindex < endfrom; fromindex++) {
    229   1.1       cgd 		if (p->froms[fromindex] == 0)
    230   1.1       cgd 			continue;
    231   1.1       cgd 
    232   1.1       cgd 		frompc = p->lowpc;
    233   1.1       cgd 		frompc += fromindex * p->hashfraction * sizeof(*p->froms);
    234   1.1       cgd 		for (toindex = p->froms[fromindex]; toindex != 0;
    235   1.1       cgd 		     toindex = p->tos[toindex].link) {
    236   1.1       cgd #ifdef DEBUG
    237   1.9       mrg 			len = snprintf(buf2, sizeof buf2,
    238   1.1       cgd 			"[mcleanup2] frompc 0x%x selfpc 0x%x count %d\n" ,
    239   1.1       cgd 				frompc, p->tos[toindex].selfpc,
    240   1.1       cgd 				p->tos[toindex].count);
    241   1.9       mrg 			write(log, buf2, len);
    242   1.1       cgd #endif
    243   1.1       cgd 			rawarc.raw_frompc = frompc;
    244   1.1       cgd 			rawarc.raw_selfpc = p->tos[toindex].selfpc;
    245   1.1       cgd 			rawarc.raw_count = p->tos[toindex].count;
    246   1.1       cgd 			write(fd, &rawarc, sizeof rawarc);
    247   1.1       cgd 		}
    248   1.1       cgd 	}
    249   1.1       cgd 	close(fd);
    250   1.1       cgd }
    251   1.1       cgd 
    252   1.1       cgd /*
    253   1.1       cgd  * Control profiling
    254   1.1       cgd  *	profiling is what mcount checks to see if
    255   1.1       cgd  *	all the data structures are ready.
    256   1.1       cgd  */
    257   1.1       cgd void
    258   1.1       cgd moncontrol(mode)
    259   1.1       cgd 	int mode;
    260   1.1       cgd {
    261   1.1       cgd 	struct gmonparam *p = &_gmonparam;
    262   1.1       cgd 
    263   1.1       cgd 	if (mode) {
    264   1.1       cgd 		/* start */
    265  1.14  christos 		profil((char *)(void *)p->kcount, (size_t)p->kcountsize,
    266  1.14  christos 		    p->lowpc, s_scale);
    267   1.1       cgd 		p->state = GMON_PROF_ON;
    268   1.1       cgd 	} else {
    269   1.1       cgd 		/* stop */
    270  1.14  christos 		profil(NULL, 0, (u_long)0, 0);
    271   1.1       cgd 		p->state = GMON_PROF_OFF;
    272   1.1       cgd 	}
    273   1.1       cgd }
    274   1.1       cgd 
    275   1.1       cgd /*
    276   1.1       cgd  * discover the tick frequency of the machine
    277   1.1       cgd  * if something goes wrong, we return 0, an impossible hertz.
    278   1.1       cgd  */
    279   1.1       cgd static int
    280   1.1       cgd hertz()
    281   1.1       cgd {
    282   1.1       cgd 	struct itimerval tim;
    283   1.1       cgd 
    284   1.1       cgd 	tim.it_interval.tv_sec = 0;
    285   1.1       cgd 	tim.it_interval.tv_usec = 1;
    286   1.1       cgd 	tim.it_value.tv_sec = 0;
    287   1.1       cgd 	tim.it_value.tv_usec = 0;
    288   1.1       cgd 	setitimer(ITIMER_REAL, &tim, 0);
    289   1.1       cgd 	setitimer(ITIMER_REAL, 0, &tim);
    290   1.1       cgd 	if (tim.it_interval.tv_usec < 2)
    291   1.1       cgd 		return(0);
    292  1.14  christos 	return (int)(1000000 / tim.it_interval.tv_usec);
    293   1.1       cgd }
    294