profile.h revision 92761
11543Srgrimes/*
21543Srgrimes * Copyright (c) 1992, 1993
31543Srgrimes *	The Regents of the University of California.  All rights reserved.
41543Srgrimes *
51543Srgrimes * Redistribution and use in source and binary forms, with or without
61543Srgrimes * modification, are permitted provided that the following conditions
71543Srgrimes * are met:
81543Srgrimes * 1. Redistributions of source code must retain the above copyright
91543Srgrimes *    notice, this list of conditions and the following disclaimer.
101543Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111543Srgrimes *    notice, this list of conditions and the following disclaimer in the
121543Srgrimes *    documentation and/or other materials provided with the distribution.
131543Srgrimes * 3. All advertising materials mentioning features or use of this software
141543Srgrimes *    must display the following acknowledgement:
151543Srgrimes *	This product includes software developed by the University of
161543Srgrimes *	California, Berkeley and its contributors.
171543Srgrimes * 4. Neither the name of the University nor the names of its contributors
181543Srgrimes *    may be used to endorse or promote products derived from this software
191543Srgrimes *    without specific prior written permission.
201543Srgrimes *
211543Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221543Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231543Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241543Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251543Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261543Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271543Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281543Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291543Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301543Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311543Srgrimes * SUCH DAMAGE.
321543Srgrimes *
331543Srgrimes *	@(#)profile.h	8.1 (Berkeley) 6/11/93
3450477Speter * $FreeBSD: head/sys/amd64/include/profile.h 92761 2002-03-20 05:48:58Z alfred $
351543Srgrimes */
361543Srgrimes
3713107Sbde#ifndef _MACHINE_PROFILE_H_
3813107Sbde#define	_MACHINE_PROFILE_H_
392166Spaul
4055205Speter#ifdef _KERNEL
4137542Sbde
4213157Sbde/*
4322639Sbde * Config generates something to tell the compiler to align functions on 16
4422639Sbde * byte boundaries.  A strict alignment is good for keeping the tables small.
4522639Sbde */
4622639Sbde#define	FUNCTION_ALIGNMENT	16
4722639Sbde
4822639Sbde/*
4913157Sbde * The kernel uses assembler stubs instead of unportable inlines.
5013157Sbde * This is mainly to save a little time when profiling is not enabled,
5113157Sbde * which is the usual case for the kernel.
5213157Sbde */
5313157Sbde#define	_MCOUNT_DECL void mcount
5413157Sbde#define	MCOUNT
551543Srgrimes
5617879Sbde#ifdef GUPROF
5717879Sbde#define	CALIB_SCALE	1000
5817879Sbde#define	KCOUNT(p,index)	((p)->kcount[(index) \
5922639Sbde			 / (HISTFRACTION * sizeof(HISTCOUNTER))])
6017879Sbde#define	MCOUNT_DECL(s)
6117879Sbde#define	MCOUNT_ENTER(s)
6217879Sbde#define	MCOUNT_EXIT(s)
6337629Sbde#define	PC_TO_I(p, pc)	((uintfptr_t)(pc) - (uintfptr_t)(p)->lowpc)
6417879Sbde#else
6517879Sbde#define	MCOUNT_DECL(s)	u_long s;
6628921Sfsmp#ifdef SMP
6790024Sbdeextern int	mcount_lock;
6878908Sjhb#define	MCOUNT_ENTER(s)	{ s = read_eflags(); disable_intr(); \
6979734Sjhb 			  while (!atomic_cmpset_acq_int(&mcount_lock, 0, 1)) \
7078908Sjhb			  	/* nothing */ ; }
7178908Sjhb#define	MCOUNT_EXIT(s)	{ atomic_store_rel_int(&mcount_lock, 0); \
7278908Sjhb			  write_eflags(s); }
7328921Sfsmp#else
7431723Stegge#define	MCOUNT_ENTER(s)	{ s = read_eflags(); disable_intr(); }
7517879Sbde#define	MCOUNT_EXIT(s)	(write_eflags(s))
7628921Sfsmp#endif
7717879Sbde#endif /* GUPROF */
7817879Sbde
7955205Speter#else /* !_KERNEL */
8013157Sbde
8122639Sbde#define	FUNCTION_ALIGNMENT	4
8222639Sbde
8313157Sbde#define	_MCOUNT_DECL static __inline void _mcount
8413157Sbde
851543Srgrimes#define	MCOUNT \
8613157Sbdevoid \
8713157Sbdemcount() \
8813157Sbde{ \
8937629Sbde	uintfptr_t selfpc, frompc; \
901543Srgrimes	/* \
9113107Sbde	 * Find the return address for mcount, \
921543Srgrimes	 * and the return address for mcount's caller. \
931543Srgrimes	 * \
9413107Sbde	 * selfpc = pc pushed by call to mcount \
951543Srgrimes	 */ \
961543Srgrimes	asm("movl 4(%%ebp),%0" : "=r" (selfpc)); \
971543Srgrimes	/* \
9813107Sbde	 * frompc = pc pushed by call to mcount's caller. \
9913107Sbde	 * The caller's stack frame has already been built, so %ebp is \
10013107Sbde	 * the caller's frame pointer.  The caller's raddr is in the \
10113107Sbde	 * caller's frame following the caller's caller's frame pointer. \
1021543Srgrimes	 */ \
10313107Sbde	asm("movl (%%ebp),%0" : "=r" (frompc)); \
10437629Sbde	frompc = ((uintfptr_t *)frompc)[1]; \
10513107Sbde	_mcount(frompc, selfpc); \
1061543Srgrimes}
10737542Sbde
10837629Sbdetypedef	unsigned int	uintfptr_t;
10937542Sbde
11055205Speter#endif /* _KERNEL */
1112166Spaul
11213107Sbde/*
11313107Sbde * An unsigned integral type that can hold non-negative difference between
11413107Sbde * function pointers.
11513107Sbde */
11631723Steggetypedef	u_int	fptrdiff_t;
11713107Sbde
11855205Speter#ifdef _KERNEL
11919000Sbde
12092761Salfredvoid	mcount(uintfptr_t frompc, uintfptr_t selfpc);
12192761Salfredvoid	kmupetext(uintfptr_t nhighpc);
12219000Sbde
12319000Sbde#ifdef GUPROF
12419000Sbdestruct gmonparam;
12519000Sbde
12692761Salfredvoid	nullfunc_loop_profiled(void);
12792761Salfredvoid	nullfunc_profiled(void);
12892761Salfredvoid	startguprof(struct gmonparam *p);
12992761Salfredvoid	stopguprof(struct gmonparam *p);
13013157Sbde#else
13119000Sbde#define	startguprof(p)
13219000Sbde#define	stopguprof(p)
13319000Sbde#endif /* GUPROF */
13419000Sbde
13555205Speter#else /* !_KERNEL */
13619000Sbde
13719000Sbde#include <sys/cdefs.h>
13819000Sbde
13919000Sbde__BEGIN_DECLS
14033047Sbde#ifdef __GNUC__
14138928Sjdp#ifdef __ELF__
14292761Salfredvoid	mcount(void) __asm(".mcount");
14338928Sjdp#else
14492761Salfredvoid	mcount(void) __asm("mcount");
14533047Sbde#endif
14638928Sjdp#endif
14792761Salfredstatic void	_mcount(uintfptr_t frompc, uintfptr_t selfpc);
14819000Sbde__END_DECLS
14913157Sbde
15055205Speter#endif /* _KERNEL */
15119000Sbde
15213157Sbde#ifdef GUPROF
15319000Sbde/* XXX doesn't quite work outside kernel yet. */
15419000Sbdeextern int	cputime_bias;
15519000Sbde
15619000Sbde__BEGIN_DECLS
15792761Salfredint	cputime(void);
15892761Salfredvoid	empty_loop(void);
15992761Salfredvoid	mexitcount(uintfptr_t selfpc);
16092761Salfredvoid	nullfunc(void);
16192761Salfredvoid	nullfunc_loop(void);
16219000Sbde__END_DECLS
16313157Sbde#endif
16413107Sbde
16513157Sbde#endif /* !_MACHINE_PROFILE_H_ */
166