diff options
-rw-r--r-- | ChangeLog | 16 | ||||
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | csu/Makefile | 25 | ||||
-rw-r--r-- | csu/gmon-start.c | 47 | ||||
-rw-r--r-- | gmon/Makefile | 28 | ||||
-rw-r--r-- | gmon/gmon.c | 232 | ||||
-rw-r--r-- | gmon/mcount.c | 178 | ||||
-rw-r--r-- | gmon/sys/gmon.h | 159 | ||||
-rw-r--r-- | sys/gmon.h | 1 | ||||
-rw-r--r-- | sysdeps/i386/machine-gmon.h | 29 | ||||
-rw-r--r-- | sysdeps/mach/hurd/profil.c | 164 | ||||
-rw-r--r-- | sysdeps/stub/machine-gmon.h | 28 | ||||
-rw-r--r-- | sysdeps/stub/profil.c | 40 | ||||
-rw-r--r-- | sysdeps/unix/bsd/profil.S | 23 |
14 files changed, 963 insertions, 9 deletions
diff --git a/ChangeLog b/ChangeLog index 427ec01b97..bf85dbe212 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,19 @@ +Tue Sep 26 16:50:17 1995 Roland McGrath <roland@churchy.gnu.ai.mit.edu> + + * gmon: New directory of profiling code, incorporated from + 4.4BSD-Lite and modified. + * sysdeps/i386/machine-gmon.h: New file. + * sysdeps/stub/machine-gmon.h: New file. + * sysdeps/mach/hurd/profil.c: New file. + * sysdeps/stub/profil.c: New file. + * sysdeps/unix/bsd/profil.S: New file. + * Makefile (subdirs): Add gmon. + * csu/gmon-start.c: New file. + * csu/Makefile (extra-objs): Add gmon-start.o, + g$(start-installed-name). + (install-lib, omit-deps): Add g$(start-installed-name). + ($(objpfx)g$(start-installed-name)): New target. + Fri Sep 22 14:24:25 1995 Roland McGrath <roland@churchy.gnu.ai.mit.edu> * sysdeps/mach/hurd/Makefile: Elide sunrpc from $(subdirs) for the diff --git a/Makefile b/Makefile index 56601e5282..64cdebfeec 100644 --- a/Makefile +++ b/Makefile @@ -53,7 +53,7 @@ endif # These are the subdirectories containing the library source. subdirs := csu assert ctype locale math setjmp signal stdio stdlib \ malloc string time dirent grp pwd posix io termios resource \ - misc socket sysvipc gnulib $(wildcard crypt) manual \ + misc socket sysvipc gmon gnulib $(wildcard crypt) manual \ $(sysdep-subdirs) elf export subdirs := $(subdirs) # Benign, useless in GNU make before 3.63. diff --git a/csu/Makefile b/csu/Makefile index 8f97624240..4fddba2f3a 100644 --- a/csu/Makefile +++ b/csu/Makefile @@ -20,18 +20,21 @@ # This directory contains the C startup code (that which calls main). This # consists of the startfile, built from start.c and installed as crt0.o -# (traditionally) or crt1.o (for ELF); and some initialization code which -# is in the C library itself. In ELF we also install crti.o and crtn.o, -# special "initializer" and "finalizer" files in used in the link to make -# the .init and .fini sections work right; both these files are built (in -# an arcane manner) from initfini.c. +# (traditionally) or crt1.o (for ELF). In ELF we also install crti.o and +# crtn.o, special "initializer" and "finalizer" files in used in the link +# to make the .init and .fini sections work right; both these files are +# built (in an arcane manner) from initfini.c. subdir := csu csu-dummies = $(filter-out $(start-installed-name),crt1.o Mcrt1.o) -extra-objs = start.o $(start-installed-name) $(csu-dummies) -omit-deps = $(patsubst %.o,%,$(start-installed-name) $(csu-dummies)) -install-lib = $(start-installed-name) $(csu-dummies) +extra-objs = start.o gmon-start.o \ + $(start-installed-name) g$(start-installed-name) \ + $(csu-dummies) +omit-deps = $(patsubst %.o,%,$(start-installed-name) g$(start-installed-name) \ + $(csu-dummies)) +install-lib = $(start-installed-name) g$(start-installed-name) \ + $(csu-dummies) distribute = initfini.c all: # Make this the default target; it will be defined in Rules. @@ -95,6 +98,12 @@ $(objpfx)$(start-installed-name): $(objpfx)start.o ln $< $@ endif +# The profiling startfile is made by linking together the normal +# startfile with gmon-start.o, which defines a constructor function +# to turn on profiling code at startup. +$(objpfx)g$(start-installed-name): $(objpfx)start.o $(objpfx)gmon-start.o + $(CC) -nostdlib -nostartfiles -r -o $@ $^ + # These extra files are sometimes expected by system standard linking # procedures, but we have nothing for them to do. So compile empty files. $(addprefix $(objpfx),$(filter-out $(start-installed-name),$(csu-dummies))): diff --git a/csu/gmon-start.c b/csu/gmon-start.c new file mode 100644 index 0000000000..c9d51b113b --- /dev/null +++ b/csu/gmon-start.c @@ -0,0 +1,47 @@ +/* Code to enable profiling at program startup. +Copyright (C) 1995 Free Software Foundation, Inc. +This file is part of the GNU C Library. + +The GNU C Library is free software; you can redistribute it and/or +modify it under the terms of the GNU Library General Public License as +published by the Free Software Foundation; either version 2 of the +License, or (at your option) any later version. + +The GNU C Library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Library General Public License for more details. + +You should have received a copy of the GNU Library General Public +License along with the GNU C Library; see the file COPYING.LIB. If +not, write to the Free Software Foundation, Inc., 675 Mass Ave, +Cambridge, MA 02139, USA. */ + +#include <sys/types.h> +#include <sys/gmon.h> +#include <stdlib.h> +#include <unistd.h> + +/* Beginning and end of our code segment. */ +extern void _start (), etext (); + +/* These functions are defined in gmon/gmon.c; + they do all the work of setting up and cleaning up profiling. */ +extern void monstartup (u_long, u_long); +extern void _mcleanup (void); + +/* This function gets called at startup by the normal constructor + mechanism. We link this file together with start.o to produce gcrt1.o, + so this constructor will be first in the list. */ +static void gmon_start (void) __attribute__ ((constructor)); +static void +gmon_start (void) +{ + /* Start keeping profiling records. */ + monstartup ((u_long) &_start, (u_long) &etext); + + /* Call _mcleanup before exitting; it will write out gmon.out from the + collected data. */ + atexit (&_mcleanup); +} + diff --git a/gmon/Makefile b/gmon/Makefile new file mode 100644 index 0000000000..3bd4201b72 --- /dev/null +++ b/gmon/Makefile @@ -0,0 +1,28 @@ +# Copyright (C) 1995 Free Software Foundation, Inc. +# This file is part of the GNU C Library. + +# The GNU C Library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Library General Public License as +# published by the Free Software Foundation; either version 2 of the +# License, or (at your option) any later version. + +# The GNU C Library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Library General Public License for more details. + +# You should have received a copy of the GNU Library General Public +# License along with the GNU C Library; see the file COPYING.LIB. If +# not, write to the Free Software Foundation, Inc., 675 Mass Ave, +# Cambridge, MA 02139, USA. + +# +# Sub-makefile for gmon portion of the library. +# +subdir := gmon + +headers := sys/gmon.h machine-gmon.h + +routines := gmon mcount profil + +include ../Rules diff --git a/gmon/gmon.c b/gmon/gmon.c new file mode 100644 index 0000000000..df528e259f --- /dev/null +++ b/gmon/gmon.c @@ -0,0 +1,232 @@ +/*- + * Copyright (c) 1983, 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#if !defined(lint) && defined(LIBC_SCCS) +static char sccsid[] = "@(#)gmon.c 8.1 (Berkeley) 6/4/93"; +#endif + +#include <sys/param.h> +#include <sys/time.h> +#include <sys/gmon.h> + +#include <stdio.h> +#include <fcntl.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +struct gmonparam _gmonparam = { GMON_PROF_OFF }; + +static int s_scale; +/* see profil(2) where this is describe (incorrectly) */ +#define SCALE_1_TO_1 0x10000L + +#define ERR(s) write(2, s, sizeof(s) - 1) + +void moncontrol __P((int)); +static int hertz __P((void)); + +void +monstartup(lowpc, highpc) + u_long lowpc; + u_long highpc; +{ + register int o; + char *cp; + struct gmonparam *p = &_gmonparam; + + /* + * round lowpc and highpc to multiples of the density we're using + * so the rest of the scaling (here and in gprof) stays in ints. + */ + p->lowpc = ROUNDDOWN(lowpc, HISTFRACTION * sizeof(HISTCOUNTER)); + p->highpc = ROUNDUP(highpc, HISTFRACTION * sizeof(HISTCOUNTER)); + p->textsize = p->highpc - p->lowpc; + p->kcountsize = p->textsize / HISTFRACTION; + p->hashfraction = HASHFRACTION; + p->fromssize = p->textsize / HASHFRACTION; + p->tolimit = p->textsize * ARCDENSITY / 100; + if (p->tolimit < MINARCS) + p->tolimit = MINARCS; + else if (p->tolimit > MAXARCS) + p->tolimit = MAXARCS; + p->tossize = p->tolimit * sizeof(struct tostruct); + + cp = malloc (p->kcountsize + p->fromssize + p->tossize); + if (! cp) { + ERR("monstartup: out of memory\n"); + return; + } + bzero(cp, p->kcountsize + p->fromssize + p->tossize); + p->tos = (struct tostruct *)cp; + cp += p->tossize; + p->kcount = (u_short *)cp; + cp += p->kcountsize; + p->froms = (u_short *)cp; + + p->tos[0].link = 0; + + o = p->highpc - p->lowpc; + if (p->kcountsize < o) { +#ifndef hp300 + s_scale = ((float)p->kcountsize / o ) * SCALE_1_TO_1; +#else /* avoid floating point */ + int quot = o / p->kcountsize; + + if (quot >= 0x10000) + s_scale = 1; + else if (quot >= 0x100) + s_scale = 0x10000 / quot; + else if (o >= 0x800000) + s_scale = 0x1000000 / (o / (p->kcountsize >> 8)); + else + s_scale = 0x1000000 / ((o << 8) / p->kcountsize); +#endif + } else + s_scale = SCALE_1_TO_1; + + moncontrol(1); +} + +void +_mcleanup() +{ + int fd; + int fromindex; + int endfrom; + u_long frompc; + int toindex; + struct rawarc rawarc; + struct gmonparam *p = &_gmonparam; + struct gmonhdr gmonhdr, *hdr; +#ifdef DEBUG + int log, len; + char buf[200]; +#endif + + if (p->state == GMON_PROF_ERROR) + ERR("_mcleanup: tos overflow\n"); + + moncontrol(0); + fd = open("gmon.out", O_CREAT|O_TRUNC|O_WRONLY, 0666); + if (fd < 0) { + perror("mcount: gmon.out"); + return; + } +#ifdef DEBUG + log = open("gmon.log", O_CREAT|O_TRUNC|O_WRONLY, 0664); + if (log < 0) { + perror("mcount: gmon.log"); + return; + } + len = sprintf(buf, "[mcleanup1] kcount 0x%x ssiz %d\n", + p->kcount, p->kcountsize); + write(log, buf, len); +#endif + hdr = (struct gmonhdr *)&gmonhdr; + hdr->lpc = p->lowpc; + hdr->hpc = p->highpc; + hdr->ncnt = p->kcountsize + sizeof(gmonhdr); + hdr->version = GMONVERSION; + hdr->profrate = hertz(); + write(fd, (char *)hdr, sizeof *hdr); + write(fd, p->kcount, p->kcountsize); + endfrom = p->fromssize / sizeof(*p->froms); + for (fromindex = 0; fromindex < endfrom; fromindex++) { + if (p->froms[fromindex] == 0) + continue; + + frompc = p->lowpc; + frompc += fromindex * p->hashfraction * sizeof(*p->froms); + for (toindex = p->froms[fromindex]; toindex != 0; + toindex = p->tos[toindex].link) { +#ifdef DEBUG + len = sprintf(buf, + "[mcleanup2] frompc 0x%x selfpc 0x%x count %d\n" , + frompc, p->tos[toindex].selfpc, + p->tos[toindex].count); + write(log, buf, len); +#endif + rawarc.raw_frompc = frompc; + rawarc.raw_selfpc = p->tos[toindex].selfpc; + rawarc.raw_count = p->tos[toindex].count; + write(fd, &rawarc, sizeof rawarc); + } + } + close(fd); +} + +/* + * Control profiling + * profiling is what mcount checks to see if + * all the data structures are ready. + */ +void +moncontrol(mode) + int mode; +{ + struct gmonparam *p = &_gmonparam; + + if (mode) { + /* start */ + profil((char *)p->kcount, p->kcountsize, (int)p->lowpc, + s_scale); + p->state = GMON_PROF_ON; + } else { + /* stop */ + profil((char *)0, 0, 0, 0); + p->state = GMON_PROF_OFF; + } +} + +/* + * discover the tick frequency of the machine + * if something goes wrong, we return 0, an impossible hertz. + */ +static int +hertz() +{ + struct itimerval tim; + + tim.it_interval.tv_sec = 0; + tim.it_interval.tv_usec = 1; + tim.it_value.tv_sec = 0; + tim.it_value.tv_usec = 0; + setitimer(ITIMER_REAL, &tim, 0); + setitimer(ITIMER_REAL, 0, &tim); + if (tim.it_interval.tv_usec < 2) + return(0); + return (1000000 / tim.it_interval.tv_usec); +} + + diff --git a/gmon/mcount.c b/gmon/mcount.c new file mode 100644 index 0000000000..523217d1d2 --- /dev/null +++ b/gmon/mcount.c @@ -0,0 +1,178 @@ +/*- + * Copyright (c) 1983, 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#if !defined(lint) && !defined(KERNEL) && defined(LIBC_SCCS) +static char sccsid[] = "@(#)mcount.c 8.1 (Berkeley) 6/4/93"; +#endif + +#include <sys/param.h> +#include <sys/gmon.h> + +/* + * mcount is called on entry to each function compiled with the profiling + * switch set. _mcount(), which is declared in a machine-dependent way + * with _MCOUNT_DECL, does the actual work and is either inlined into a + * C routine or called by an assembly stub. In any case, this magic is + * taken care of by the MCOUNT definition in <machine/profile.h>. + * + * _mcount updates data structures that represent traversals of the + * program's call graph edges. frompc and selfpc are the return + * address and function address that represents the given call graph edge. + * + * Note: the original BSD code used the same variable (frompcindex) for + * both frompcindex and frompc. Any reasonable, modern compiler will + * perform this optimization. + */ +_MCOUNT_DECL(frompc, selfpc) /* _mcount; may be static, inline, etc */ + register u_long frompc, selfpc; +{ + register u_short *frompcindex; + register struct tostruct *top, *prevtop; + register struct gmonparam *p; + register long toindex; +#ifdef KERNEL + register int s; +#endif + + p = &_gmonparam; + /* + * check that we are profiling + * and that we aren't recursively invoked. + */ + if (p->state != GMON_PROF_ON) + return; +#ifdef KERNEL + MCOUNT_ENTER; +#else + p->state = GMON_PROF_BUSY; +#endif + /* + * check that frompcindex is a reasonable pc value. + * for example: signal catchers get called from the stack, + * not from text space. too bad. + */ + frompc -= p->lowpc; + if (frompc > p->textsize) + goto done; + + frompcindex = &p->froms[frompc / (p->hashfraction * sizeof(*p->froms))]; + toindex = *frompcindex; + if (toindex == 0) { + /* + * first time traversing this arc + */ + toindex = ++p->tos[0].link; + if (toindex >= p->tolimit) + /* halt further profiling */ + goto overflow; + + *frompcindex = toindex; + top = &p->tos[toindex]; + top->selfpc = selfpc; + top->count = 1; + top->link = 0; + goto done; + } + top = &p->tos[toindex]; + if (top->selfpc == selfpc) { + /* + * arc at front of chain; usual case. + */ + top->count++; + goto done; + } + /* + * have to go looking down chain for it. + * top points to what we are looking at, + * prevtop points to previous top. + * we know it is not at the head of the chain. + */ + for (; /* goto done */; ) { + if (top->link == 0) { + /* + * top is end of the chain and none of the chain + * had top->selfpc == selfpc. + * so we allocate a new tostruct + * and link it to the head of the chain. + */ + toindex = ++p->tos[0].link; + if (toindex >= p->tolimit) + goto overflow; + + top = &p->tos[toindex]; + top->selfpc = selfpc; + top->count = 1; + top->link = *frompcindex; + *frompcindex = toindex; + goto done; + } + /* + * otherwise, check the next arc on the chain. + */ + prevtop = top; + top = &p->tos[top->link]; + if (top->selfpc == selfpc) { + /* + * there it is. + * increment its count + * move it to the head of the chain. + */ + top->count++; + toindex = prevtop->link; + prevtop->link = top->link; + top->link = *frompcindex; + *frompcindex = toindex; + goto done; + } + + } +done: +#ifdef KERNEL + MCOUNT_EXIT; +#else + p->state = GMON_PROF_ON; +#endif + return; +overflow: + p->state = GMON_PROF_ERROR; +#ifdef KERNEL + MCOUNT_EXIT; +#endif + return; +} + +/* + * Actual definition of mcount function. Defined in <machine/profile.h>, + * which is included by <sys/gmon.h>. + */ +MCOUNT diff --git a/gmon/sys/gmon.h b/gmon/sys/gmon.h new file mode 100644 index 0000000000..6a682d0b10 --- /dev/null +++ b/gmon/sys/gmon.h @@ -0,0 +1,159 @@ +/*- + * Copyright (c) 1982, 1986, 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * @(#)gmon.h 8.2 (Berkeley) 1/4/94 + */ + +#ifndef _SYS_GMON_H_ +#define _SYS_GMON_H_ + +#include <machine-gmon.h> + +/* + * Structure prepended to gmon.out profiling data file. + */ +struct gmonhdr { + u_long lpc; /* base pc address of sample buffer */ + u_long hpc; /* max pc address of sampled buffer */ + int ncnt; /* size of sample buffer (plus this header) */ + int version; /* version number */ + int profrate; /* profiling clock rate */ + int spare[3]; /* reserved */ +}; +#define GMONVERSION 0x00051879 + +/* + * histogram counters are unsigned shorts (according to the kernel). + */ +#define HISTCOUNTER unsigned short + +/* + * fraction of text space to allocate for histogram counters here, 1/2 + */ +#define HISTFRACTION 2 + +/* + * Fraction of text space to allocate for from hash buckets. + * The value of HASHFRACTION is based on the minimum number of bytes + * of separation between two subroutine call points in the object code. + * Given MIN_SUBR_SEPARATION bytes of separation the value of + * HASHFRACTION is calculated as: + * + * HASHFRACTION = MIN_SUBR_SEPARATION / (2 * sizeof(short) - 1); + * + * For example, on the VAX, the shortest two call sequence is: + * + * calls $0,(r0) + * calls $0,(r0) + * + * which is separated by only three bytes, thus HASHFRACTION is + * calculated as: + * + * HASHFRACTION = 3 / (2 * 2 - 1) = 1 + * + * Note that the division above rounds down, thus if MIN_SUBR_FRACTION + * is less than three, this algorithm will not work! + * + * In practice, however, call instructions are rarely at a minimal + * distance. Hence, we will define HASHFRACTION to be 2 across all + * architectures. This saves a reasonable amount of space for + * profiling data structures without (in practice) sacrificing + * any granularity. + */ +#define HASHFRACTION 2 + +/* + * percent of text space to allocate for tostructs with a minimum. + */ +#define ARCDENSITY 2 +#define MINARCS 50 +#define MAXARCS ((1 << (8 * sizeof(HISTCOUNTER))) - 2) + +struct tostruct { + u_long selfpc; + long count; + u_short link; + u_short pad; +}; + +/* + * a raw arc, with pointers to the calling site and + * the called site and a count. + */ +struct rawarc { + u_long raw_frompc; + u_long raw_selfpc; + long raw_count; +}; + +/* + * general rounding functions. + */ +#define ROUNDDOWN(x,y) (((x)/(y))*(y)) +#define ROUNDUP(x,y) ((((x)+(y)-1)/(y))*(y)) + +/* + * The profiling data structures are housed in this structure. + */ +struct gmonparam { + int state; + u_short *kcount; + u_long kcountsize; + u_short *froms; + u_long fromssize; + struct tostruct *tos; + u_long tossize; + long tolimit; + u_long lowpc; + u_long highpc; + u_long textsize; + u_long hashfraction; +}; +extern struct gmonparam _gmonparam; + +/* + * Possible states of profiling. + */ +#define GMON_PROF_ON 0 +#define GMON_PROF_BUSY 1 +#define GMON_PROF_ERROR 2 +#define GMON_PROF_OFF 3 + +/* + * Sysctl definitions for extracting profiling information from the kernel. + */ +#define GPROF_STATE 0 /* int: profiling enabling variable */ +#define GPROF_COUNT 1 /* struct: profile tick count buffer */ +#define GPROF_FROMS 2 /* struct: from location hash bucket */ +#define GPROF_TOS 3 /* struct: destination/count structure */ +#define GPROF_GMONPARAM 4 /* struct: profiling parameters (see above) */ +#endif /* !_SYS_GMON_H_ */ diff --git a/sys/gmon.h b/sys/gmon.h new file mode 100644 index 0000000000..987589fcfe --- /dev/null +++ b/sys/gmon.h @@ -0,0 +1 @@ +#include <gmon/sys/gmon.h> diff --git a/sysdeps/i386/machine-gmon.h b/sysdeps/i386/machine-gmon.h new file mode 100644 index 0000000000..4128390b8b --- /dev/null +++ b/sysdeps/i386/machine-gmon.h @@ -0,0 +1,29 @@ +/* Machine-specific calling sequence for `mcount' profiling function. i386 +Copyright (C) 1995 Free Software Foundation, Inc. +This file is part of the GNU C Library. + +The GNU C Library is free software; you can redistribute it and/or +modify it under the terms of the GNU Library General Public License as +published by the Free Software Foundation; either version 2 of the +License, or (at your option) any later version. + +The GNU C Library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Library General Public License for more details. + +You should have received a copy of the GNU Library General Public +License along with the GNU C Library; see the file COPYING.LIB. If +not, write to the Free Software Foundation, Inc., 675 Mass Ave, +Cambridge, MA 02139, USA. */ + +#define _MCOUNT_DECL static inline void _mcount + +/* Call _mcount with our the return PC for our caller, + and the return PC our caller will return to. */ +#define MCOUNT \ +void mcount (u_long arg) \ +{ \ + _mcount (((u_long *) (&arg)[-2])[-1], \ + (&arg)[-1]); \ +} diff --git a/sysdeps/mach/hurd/profil.c b/sysdeps/mach/hurd/profil.c new file mode 100644 index 0000000000..4032219a9e --- /dev/null +++ b/sysdeps/mach/hurd/profil.c @@ -0,0 +1,164 @@ +/* Low-level statistical profiling support function. Mach/Hurd version. +Copyright (C) 1995 Free Software Foundation, Inc. +This file is part of the GNU C Library. + +The GNU C Library is free software; you can redistribute it and/or +modify it under the terms of the GNU Library General Public License as +published by the Free Software Foundation; either version 2 of the +License, or (at your option) any later version. + +The GNU C Library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Library General Public License for more details. + +You should have received a copy of the GNU Library General Public +License along with the GNU C Library; see the file COPYING.LIB. If +not, write to the Free Software Foundation, Inc., 675 Mass Ave, +Cambridge, MA 02139, USA. */ + +#include <sys/types.h> +#include <unistd.h> +#include <errno.h> +#include <hurd.h> +#include <mach/mach4.h> +#include <mach/pc_sample.h> +#include <cthreads.h> +#include <assert.h> + +static thread_t profile_thread = MACH_PORT_NULL; +static u_short *samples; +static size_t maxsamples; +static size_t pc_offset; +static size_t sample_scale; +static sampled_pc_seqno_t seqno; +static struct mutex lock = MUTEX_INITIALIZER; + +/* Enable statistical profiling, writing samples of the PC into at most + SIZE bytes of SAMPLE_BUFFER; every processor clock tick while profiling + is enabled, the system examines the user PC and increments + SAMPLE_BUFFER[((PC - OFFSET) / 2) * SCALE / 65536]. If SCALE is zero, + disable profiling. Returns zero on success, -1 on error. */ + +static error_t +update_waiter (u_short *sample_buffer, size_t size, size_t offset, u_int scale) +{ + error_t err; + + if (profile_thread == MACH_PORT_NULL) + { + /* Set up the profiling collector thread. */ + static void profile_waiter (void); + err = __thread_create (__mach_task_self (), &profile_thread); + if (! err) + err = __mach_setup_thread (__mach_task_self (), profile_thread, + &profile_waiter, NULL, NULL); + } + else + err = 0; + + if (! err) + { + if (sample_scale == 0) + err = __thread_resume (profile_thread); + if (! err) + { + samples = sample_buffer; + maxsamples = size / sizeof *sample_buffer; + pc_offset = offset; + sample_scale = scale; + } + } + + return err; +} + +int +profil (u_short *sample_buffer, size_t size, size_t offset, u_int scale) +{ + error_t err; + + __mutex_lock (&lock); + + if (scale == 0) + { + /* Disable profiling. */ + int count; + __thread_suspend (profile_thread); + err = __task_disable_pc_sampling (__mach_task_self (), &count); + sample_scale = 0; + seqno = 0; + } + else + err = update_waiter (sample_buffer, size, offset, scale); + + __mutex_unlock (&lock); + + return err ? __hurd_fail (err) : 0; +} + +static void +profile_waiter (void) +{ + sampled_pc_t pc_samples[512]; + mach_msg_type_number_t nsamples = 512, i; + mach_port_t rcv = __mach_reply_port (); + mach_msg_header_t msg; + const mach_msg_timeout_t timeout = 17; /* ??? XXX */ + error_t err; + + while (1) + { + __mutex_lock (&lock); + + err = __task_get_sampled_pcs (__mach_task_self (), &seqno, + pc_samples, &nsamples); + assert_perror (err); + + for (i = 0; i < nsamples; ++i) + { + size_t idx = (((pc_samples[i].pc - pc_offset) / 2) * + sample_scale / 65536); + if (idx < maxsamples) + ++samples[idx]; + } + + __vm_deallocate (__mach_task_self (), + (vm_address_t) pc_samples, + nsamples * sizeof *pc_samples); + + __mutex_unlock (&lock); + + __mach_msg (&msg, MACH_RCV_MSG|MACH_RCV_TIMEOUT, 0, sizeof msg, + rcv, timeout, MACH_PORT_NULL); + } +} + +data_set_element (_hurd_fork_locks, lock); + +static void +fork_profil (void) +{ + u_short *sb; + size_t n, o, ss; + error_t err; + + if (profile_thread != MACH_PORT_NULL) + { + __mach_port_deallocate (__mach_task_self (), profile_thread); + profile_thread = MACH_PORT_NULL; + } + + sb = samples; + samples = NULL; + n = maxsamples; + maxsamples = 0; + o = pc_offset; + pc_offset = 0; + ss = sample_scale; + sample_scale = 0; + + err = update_waiter (sb, n * sizeof *sb, o, ss); + assert_perror (err); +} +text_set_element (_hurd_fork_child_hook, fork_profil); diff --git a/sysdeps/stub/machine-gmon.h b/sysdeps/stub/machine-gmon.h new file mode 100644 index 0000000000..a6edbc062d --- /dev/null +++ b/sysdeps/stub/machine-gmon.h @@ -0,0 +1,28 @@ +/* Machine-specific calling sequence for `mcount' profiling function. Stub +Copyright (C) 1995 Free Software Foundation, Inc. +This file is part of the GNU C Library. + +The GNU C Library is free software; you can redistribute it and/or +modify it under the terms of the GNU Library General Public License as +published by the Free Software Foundation; either version 2 of the +License, or (at your option) any later version. + +The GNU C Library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Library General Public License for more details. + +You should have received a copy of the GNU Library General Public +License along with the GNU C Library; see the file COPYING.LIB. If +not, write to the Free Software Foundation, Inc., 675 Mass Ave, +Cambridge, MA 02139, USA. */ + +#define _MCOUNT_DECL static inline void _mcount + +/* Call _mcount with our the return PC for our caller, + and the return PC our caller will return to. */ +#define MCOUNT \ +void mcount (u_long arg) \ +{ \ + _mcount (caller return PC, my return PC); \ +} diff --git a/sysdeps/stub/profil.c b/sysdeps/stub/profil.c new file mode 100644 index 0000000000..ab8e1d4ed6 --- /dev/null +++ b/sysdeps/stub/profil.c @@ -0,0 +1,40 @@ +/* Low-level statistical profiling support function. Stub version. +Copyright (C) 1995 Free Software Foundation, Inc. +This file is part of the GNU C Library. + +The GNU C Library is free software; you can redistribute it and/or +modify it under the terms of the GNU Library General Public License as +published by the Free Software Foundation; either version 2 of the +License, or (at your option) any later version. + +The GNU C Library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Library General Public License for more details. + +You should have received a copy of the GNU Library General Public +License along with the GNU C Library; see the file COPYING.LIB. If +not, write to the Free Software Foundation, Inc., 675 Mass Ave, +Cambridge, MA 02139, USA. */ + +#include <sys/types.h> +#include <unistd.h> +#include <errno.h> + +/* Enable statistical profiling, writing samples of the PC into at most + SIZE bytes of SAMPLE_BUFFER; every processor clock tick while profiling + is enabled, the system examines the user PC and increments + SAMPLE_BUFFER[((PC - OFFSET) / 2) * SCALE / 65536]. If SCALE is zero, + disable profiling. Returns zero on success, -1 on error. */ + +int +profil (u_short *sample_buffer, size_t size, size_t offset, u_int scale) +{ + if (scale == 0) + /* Disable profiling. */ + return 0; + + errno = ENOSYS; + return -1; +} +stub_warning (profil) diff --git a/sysdeps/unix/bsd/profil.S b/sysdeps/unix/bsd/profil.S new file mode 100644 index 0000000000..22429bd46b --- /dev/null +++ b/sysdeps/unix/bsd/profil.S @@ -0,0 +1,23 @@ +/* Low-level statistical profiling support function. Unix syscall version. +Copyright (C) 1995 Free Software Foundation, Inc. +This file is part of the GNU C Library. + +The GNU C Library is free software; you can redistribute it and/or +modify it under the terms of the GNU Library General Public License as +published by the Free Software Foundation; either version 2 of the +License, or (at your option) any later version. + +The GNU C Library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Library General Public License for more details. + +You should have received a copy of the GNU Library General Public +License along with the GNU C Library; see the file COPYING.LIB. If +not, write to the Free Software Foundation, Inc., 675 Mass Ave, +Cambridge, MA 02139, USA. */ + +#include <sysdep.h> + +SYSCALL (profil, 4) + ret |