16cda32c0SRodney W. Grimes /* 26cda32c0SRodney W. Grimes * Copyright (c) 1992, 1993 36cda32c0SRodney W. Grimes * The Regents of the University of California. All rights reserved. 46cda32c0SRodney W. Grimes * 56cda32c0SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 66cda32c0SRodney W. Grimes * modification, are permitted provided that the following conditions 76cda32c0SRodney W. Grimes * are met: 86cda32c0SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 96cda32c0SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 106cda32c0SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 116cda32c0SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 126cda32c0SRodney W. Grimes * documentation and/or other materials provided with the distribution. 136cda32c0SRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 146cda32c0SRodney W. Grimes * must display the following acknowledgement: 156cda32c0SRodney W. Grimes * This product includes software developed by the University of 166cda32c0SRodney W. Grimes * California, Berkeley and its contributors. 176cda32c0SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 186cda32c0SRodney W. Grimes * may be used to endorse or promote products derived from this software 196cda32c0SRodney W. Grimes * without specific prior written permission. 206cda32c0SRodney W. Grimes * 216cda32c0SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 226cda32c0SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 236cda32c0SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 246cda32c0SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 256cda32c0SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 266cda32c0SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 276cda32c0SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 286cda32c0SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 296cda32c0SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 306cda32c0SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 316cda32c0SRodney W. Grimes * SUCH DAMAGE. 326cda32c0SRodney W. Grimes * 336cda32c0SRodney W. Grimes * @(#)profile.h 8.1 (Berkeley) 6/11/93 34c3aac50fSPeter Wemm * $FreeBSD$ 356cda32c0SRodney W. Grimes */ 366cda32c0SRodney W. Grimes 37912e6037SBruce Evans #ifndef _MACHINE_PROFILE_H_ 38912e6037SBruce Evans #define _MACHINE_PROFILE_H_ 39836dc83bSPaul Richards 40664a31e4SPeter Wemm #ifdef _KERNEL 41930a6423SBruce Evans 42e5171bbeSBruce Evans /* 43a7d00b5bSBruce Evans * Config generates something to tell the compiler to align functions on 16 44a7d00b5bSBruce Evans * byte boundaries. A strict alignment is good for keeping the tables small. 45a7d00b5bSBruce Evans */ 46a7d00b5bSBruce Evans #define FUNCTION_ALIGNMENT 16 47a7d00b5bSBruce Evans 48a7d00b5bSBruce Evans /* 49e5171bbeSBruce Evans * The kernel uses assembler stubs instead of unportable inlines. 50e5171bbeSBruce Evans * This is mainly to save a little time when profiling is not enabled, 51e5171bbeSBruce Evans * which is the usual case for the kernel. 52e5171bbeSBruce Evans */ 53e5171bbeSBruce Evans #define _MCOUNT_DECL void mcount 54e5171bbeSBruce Evans #define MCOUNT 55e5171bbeSBruce Evans 561f403fcfSBruce Evans #ifdef GUPROF 571f403fcfSBruce Evans #define CALIB_SCALE 1000 581f403fcfSBruce Evans #define KCOUNT(p,index) ((p)->kcount[(index) \ 59a7d00b5bSBruce Evans / (HISTFRACTION * sizeof(HISTCOUNTER))]) 601f403fcfSBruce Evans #define MCOUNT_DECL(s) 611f403fcfSBruce Evans #define MCOUNT_ENTER(s) 621f403fcfSBruce Evans #define MCOUNT_EXIT(s) 6337889b39SBruce Evans #define PC_TO_I(p, pc) ((uintfptr_t)(pc) - (uintfptr_t)(p)->lowpc) 641f403fcfSBruce Evans #else 651f403fcfSBruce Evans #define MCOUNT_DECL(s) u_long s; 6678292efeSSteve Passe #ifdef SMP 6792fd4795SBruce Evans extern int mcount_lock; 6825142c5eSJohn Baldwin #define MCOUNT_ENTER(s) { s = read_eflags(); disable_intr(); \ 69ce11a18fSJohn Baldwin while (!atomic_cmpset_acq_int(&mcount_lock, 0, 1)) \ 7025142c5eSJohn Baldwin /* nothing */ ; } 7125142c5eSJohn Baldwin #define MCOUNT_EXIT(s) { atomic_store_rel_int(&mcount_lock, 0); \ 7225142c5eSJohn Baldwin write_eflags(s); } 7378292efeSSteve Passe #else 745c623cb6STor Egge #define MCOUNT_ENTER(s) { s = read_eflags(); disable_intr(); } 751f403fcfSBruce Evans #define MCOUNT_EXIT(s) (write_eflags(s)) 7678292efeSSteve Passe #endif 771f403fcfSBruce Evans #endif /* GUPROF */ 781f403fcfSBruce Evans 79664a31e4SPeter Wemm #else /* !_KERNEL */ 80e5171bbeSBruce Evans 81a7d00b5bSBruce Evans #define FUNCTION_ALIGNMENT 4 82a7d00b5bSBruce Evans 83e5171bbeSBruce Evans #define _MCOUNT_DECL static __inline void _mcount 846cda32c0SRodney W. Grimes 85db8f2e32SMark Murray #ifdef __GNUC__ 866cda32c0SRodney W. Grimes #define MCOUNT \ 87e5171bbeSBruce Evans void \ 88e5171bbeSBruce Evans mcount() \ 89e5171bbeSBruce Evans { \ 9037889b39SBruce Evans uintfptr_t selfpc, frompc; \ 916cda32c0SRodney W. Grimes /* \ 92912e6037SBruce Evans * Find the return address for mcount, \ 936cda32c0SRodney W. Grimes * and the return address for mcount's caller. \ 946cda32c0SRodney W. Grimes * \ 95912e6037SBruce Evans * selfpc = pc pushed by call to mcount \ 966cda32c0SRodney W. Grimes */ \ 9769bb4041SDavid E. O'Brien __asm("movl 4(%%ebp),%0" : "=r" (selfpc)); \ 986cda32c0SRodney W. Grimes /* \ 99912e6037SBruce Evans * frompc = pc pushed by call to mcount's caller. \ 100912e6037SBruce Evans * The caller's stack frame has already been built, so %ebp is \ 101912e6037SBruce Evans * the caller's frame pointer. The caller's raddr is in the \ 102912e6037SBruce Evans * caller's frame following the caller's caller's frame pointer.\ 1036cda32c0SRodney W. Grimes */ \ 10469bb4041SDavid E. O'Brien __asm("movl (%%ebp),%0" : "=r" (frompc)); \ 10537889b39SBruce Evans frompc = ((uintfptr_t *)frompc)[1]; \ 106912e6037SBruce Evans _mcount(frompc, selfpc); \ 1076cda32c0SRodney W. Grimes } 108db8f2e32SMark Murray #else /* __GNUC__ */ 10982e5cdebSMark Murray #define MCOUNT \ 110db8f2e32SMark Murray void \ 111db8f2e32SMark Murray mcount() \ 112db8f2e32SMark Murray { \ 113db8f2e32SMark Murray } 114db8f2e32SMark Murray #endif /* __GNUC__ */ 115930a6423SBruce Evans 11637889b39SBruce Evans typedef unsigned int uintfptr_t; 117930a6423SBruce Evans 118664a31e4SPeter Wemm #endif /* _KERNEL */ 119912e6037SBruce Evans 120912e6037SBruce Evans /* 121912e6037SBruce Evans * An unsigned integral type that can hold non-negative difference between 122912e6037SBruce Evans * function pointers. 123912e6037SBruce Evans */ 1245c623cb6STor Egge typedef u_int fptrdiff_t; 125912e6037SBruce Evans 126664a31e4SPeter Wemm #ifdef _KERNEL 127d6b9e17eSBruce Evans 128b63dc6adSAlfred Perlstein void mcount(uintfptr_t frompc, uintfptr_t selfpc); 129b63dc6adSAlfred Perlstein void kmupetext(uintfptr_t nhighpc); 130912e6037SBruce Evans 131e5171bbeSBruce Evans #ifdef GUPROF 132d6b9e17eSBruce Evans struct gmonparam; 133d6b9e17eSBruce Evans 134b63dc6adSAlfred Perlstein void nullfunc_loop_profiled(void); 135b63dc6adSAlfred Perlstein void nullfunc_profiled(void); 136b63dc6adSAlfred Perlstein void startguprof(struct gmonparam *p); 137b63dc6adSAlfred Perlstein void stopguprof(struct gmonparam *p); 138d6b9e17eSBruce Evans #else 139d6b9e17eSBruce Evans #define startguprof(p) 140d6b9e17eSBruce Evans #define stopguprof(p) 141d6b9e17eSBruce Evans #endif /* GUPROF */ 142d6b9e17eSBruce Evans 143664a31e4SPeter Wemm #else /* !_KERNEL */ 144d6b9e17eSBruce Evans 145d6b9e17eSBruce Evans #include <sys/cdefs.h> 146d6b9e17eSBruce Evans 147d6b9e17eSBruce Evans __BEGIN_DECLS 1487a1a679eSBruce Evans #ifdef __GNUC__ 149b63dc6adSAlfred Perlstein void mcount(void) __asm(".mcount"); 1505584f22bSJohn Polstra #endif 151b63dc6adSAlfred Perlstein static void _mcount(uintfptr_t frompc, uintfptr_t selfpc); 152e5171bbeSBruce Evans __END_DECLS 153e5171bbeSBruce Evans 154664a31e4SPeter Wemm #endif /* _KERNEL */ 155d6b9e17eSBruce Evans 156d6b9e17eSBruce Evans #ifdef GUPROF 157d6b9e17eSBruce Evans /* XXX doesn't quite work outside kernel yet. */ 158d6b9e17eSBruce Evans extern int cputime_bias; 159d6b9e17eSBruce Evans 160d6b9e17eSBruce Evans __BEGIN_DECLS 161b63dc6adSAlfred Perlstein int cputime(void); 162b63dc6adSAlfred Perlstein void empty_loop(void); 163b63dc6adSAlfred Perlstein void mexitcount(uintfptr_t selfpc); 164b63dc6adSAlfred Perlstein void nullfunc(void); 165b63dc6adSAlfred Perlstein void nullfunc_loop(void); 166d6b9e17eSBruce Evans __END_DECLS 167d6b9e17eSBruce Evans #endif 168d6b9e17eSBruce Evans 169e5171bbeSBruce Evans #endif /* !_MACHINE_PROFILE_H_ */ 170