Home | History | Annotate | Line # | Download | only in gmon
mcount.c revision 1.18
      1  1.18  christos /*	$NetBSD: mcount.c,v 1.18 2024/02/23 13:32:28 christos Exp $	*/
      2   1.1  christos 
      3   1.1  christos /*
      4   1.1  christos  * Copyright (c) 2003, 2004 Wasabi Systems, Inc.
      5   1.1  christos  * All rights reserved.
      6   1.1  christos  *
      7   1.1  christos  * Written by Nathan J. Williams for Wasabi Systems, Inc.
      8   1.1  christos  *
      9   1.1  christos  * Redistribution and use in source and binary forms, with or without
     10   1.1  christos  * modification, are permitted provided that the following conditions
     11   1.1  christos  * are met:
     12   1.1  christos  * 1. Redistributions of source code must retain the above copyright
     13   1.1  christos  *    notice, this list of conditions and the following disclaimer.
     14   1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     15   1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     16   1.1  christos  *    documentation and/or other materials provided with the distribution.
     17   1.1  christos  * 3. All advertising materials mentioning features or use of this software
     18   1.1  christos  *    must display the following acknowledgement:
     19   1.1  christos  *	This product includes software developed for the NetBSD Project by
     20   1.1  christos  *	Wasabi Systems, Inc.
     21   1.1  christos  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     22   1.1  christos  *    or promote products derived from this software without specific prior
     23   1.1  christos  *    written permission.
     24   1.1  christos  *
     25   1.1  christos  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     26   1.1  christos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27   1.1  christos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28   1.1  christos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     29   1.1  christos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30   1.1  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31   1.1  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32   1.1  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33   1.1  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34   1.1  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35   1.1  christos  * POSSIBILITY OF SUCH DAMAGE.
     36   1.1  christos  */
     37   1.1  christos 
     38   1.1  christos /*-
     39   1.1  christos  * Copyright (c) 1983, 1992, 1993
     40   1.1  christos  *	The Regents of the University of California.  All rights reserved.
     41   1.1  christos  *
     42   1.1  christos  * Redistribution and use in source and binary forms, with or without
     43   1.1  christos  * modification, are permitted provided that the following conditions
     44   1.1  christos  * are met:
     45   1.1  christos  * 1. Redistributions of source code must retain the above copyright
     46   1.1  christos  *    notice, this list of conditions and the following disclaimer.
     47   1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     48   1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     49   1.1  christos  *    documentation and/or other materials provided with the distribution.
     50   1.1  christos  * 3. Neither the name of the University nor the names of its contributors
     51   1.1  christos  *    may be used to endorse or promote products derived from this software
     52   1.1  christos  *    without specific prior written permission.
     53   1.1  christos  *
     54   1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     55   1.1  christos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     56   1.1  christos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     57   1.1  christos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     58   1.1  christos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     59   1.1  christos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     60   1.1  christos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     61   1.1  christos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     62   1.1  christos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     63   1.1  christos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     64   1.1  christos  * SUCH DAMAGE.
     65   1.1  christos  */
     66   1.1  christos 
     67   1.1  christos #ifdef _KERNEL_OPT
     68   1.1  christos #include "opt_multiprocessor.h"
     69   1.1  christos #endif
     70   1.1  christos 
     71  1.15       ryo /* If building a standalone libkern, don't include mcount. */
     72  1.15       ryo #if (!defined(_KERNEL) || defined(GPROF)) && !defined(_STANDALONE)
     73  1.15       ryo 
     74   1.1  christos #include <sys/cdefs.h>
     75   1.1  christos #if !defined(lint) && !defined(_KERNEL) && defined(LIBC_SCCS)
     76   1.1  christos #if 0
     77   1.1  christos static char sccsid[] = "@(#)mcount.c	8.1 (Berkeley) 6/4/93";
     78   1.1  christos #else
     79  1.18  christos __RCSID("$NetBSD: mcount.c,v 1.18 2024/02/23 13:32:28 christos Exp $");
     80   1.1  christos #endif
     81   1.1  christos #endif
     82   1.1  christos 
     83   1.1  christos #include <sys/param.h>
     84   1.1  christos #include <sys/gmon.h>
     85  1.11       ryo #include <sys/lock.h>
     86  1.16       ryo #include <sys/proc.h>
     87   1.1  christos 
     88   1.3  christos #ifndef _KERNEL
     89   1.1  christos #include "reentrant.h"
     90   1.3  christos #endif
     91   1.1  christos 
     92  1.14     kamil #if defined(_REENTRANT) && !defined(_RUMPKERNEL)
     93   1.1  christos extern thread_key_t _gmonkey;
     94   1.1  christos extern struct gmonparam _gmondummy;
     95   1.1  christos struct gmonparam *_m_gmon_alloc(void);
     96   1.1  christos #endif
     97   1.1  christos 
     98  1.10      matt _MCOUNT_DECL(u_long, u_long)
     99   1.1  christos #ifdef _KERNEL
    100   1.7       uwe     __attribute__((__no_instrument_function__))
    101   1.1  christos #endif
    102   1.7       uwe     __used;
    103   1.1  christos 
    104  1.12  christos /* XXX: make these interfaces */
    105  1.12  christos #ifdef _RUMPKERNEL
    106  1.12  christos #undef MCOUNT_ENTER
    107  1.12  christos #define MCOUNT_ENTER
    108  1.12  christos #undef MCOUNT_EXIT
    109  1.12  christos #define MCOUNT_EXIT
    110  1.12  christos #undef MCOUNT
    111  1.12  christos #define MCOUNT
    112  1.12  christos #endif
    113  1.12  christos 
    114   1.1  christos /*
    115   1.1  christos  * mcount is called on entry to each function compiled with the profiling
    116   1.1  christos  * switch set.  _mcount(), which is declared in a machine-dependent way
    117   1.1  christos  * with _MCOUNT_DECL, does the actual work and is either inlined into a
    118   1.1  christos  * C routine or called by an assembly stub.  In any case, this magic is
    119   1.1  christos  * taken care of by the MCOUNT definition in <machine/profile.h>.
    120   1.1  christos  *
    121   1.1  christos  * _mcount updates data structures that represent traversals of the
    122   1.1  christos  * program's call graph edges.  frompc and selfpc are the return
    123   1.1  christos  * address and function address that represents the given call graph edge.
    124   1.1  christos  *
    125   1.1  christos  * Note: the original BSD code used the same variable (frompcindex) for
    126   1.1  christos  * both frompcindex and frompc.  Any reasonable, modern compiler will
    127   1.1  christos  * perform this optimization.
    128   1.1  christos  */
    129  1.10      matt /* _mcount; may be static, inline, etc */
    130  1.18  christos /*LINTED unused*/
    131  1.10      matt _MCOUNT_DECL(u_long frompc, u_long selfpc)
    132   1.1  christos {
    133   1.1  christos 	u_short *frompcindex;
    134   1.1  christos 	struct tostruct *top, *prevtop;
    135   1.1  christos 	struct gmonparam *p;
    136   1.1  christos 	long toindex;
    137   1.8     pooka #if defined(_KERNEL) && !defined(_RUMPKERNEL)
    138   1.1  christos 	int s;
    139   1.1  christos #endif
    140   1.1  christos 
    141  1.14     kamil #if defined(_REENTRANT) && !defined(_KERNEL) && !defined(_RUMPKERNEL)
    142   1.1  christos 	if (__isthreaded) {
    143  1.13  christos 		/* prevent re-entry via thr_getspecific */
    144  1.13  christos 		if (_gmonparam.state != GMON_PROF_ON)
    145  1.13  christos 			return;
    146  1.13  christos 		_gmonparam.state = GMON_PROF_BUSY;
    147   1.1  christos 		p = thr_getspecific(_gmonkey);
    148   1.1  christos 		if (p == NULL) {
    149   1.1  christos 			/* Prevent recursive calls while allocating */
    150   1.1  christos 			thr_setspecific(_gmonkey, &_gmondummy);
    151   1.1  christos 			p = _m_gmon_alloc();
    152   1.1  christos 		}
    153  1.13  christos 		_gmonparam.state = GMON_PROF_ON;
    154   1.1  christos 	} else
    155   1.1  christos #endif
    156   1.1  christos 		p = &_gmonparam;
    157   1.1  christos 	/*
    158   1.1  christos 	 * check that we are profiling
    159   1.1  christos 	 * and that we aren't recursively invoked.
    160   1.1  christos 	 */
    161   1.1  christos 	if (p->state != GMON_PROF_ON)
    162   1.1  christos 		return;
    163  1.11       ryo #if defined(_KERNEL) && !defined(_RUMPKERNEL)
    164   1.1  christos 	MCOUNT_ENTER;
    165  1.11       ryo #ifdef MULTIPROCESSOR
    166  1.16       ryo 	p = curcpu()->ci_gmon;
    167  1.16       ryo 	if (p == NULL || p->state != GMON_PROF_ON) {
    168  1.16       ryo 		MCOUNT_EXIT;
    169  1.16       ryo 		return;
    170  1.16       ryo 	}
    171  1.11       ryo #endif
    172   1.1  christos #endif
    173   1.1  christos 	p->state = GMON_PROF_BUSY;
    174   1.1  christos 	/*
    175   1.1  christos 	 * check that frompcindex is a reasonable pc value.
    176   1.1  christos 	 * for example:	signal catchers get called from the stack,
    177   1.1  christos 	 *		not from text space.  too bad.
    178   1.1  christos 	 */
    179   1.1  christos 	frompc -= p->lowpc;
    180   1.1  christos 	if (frompc > p->textsize)
    181   1.1  christos 		goto done;
    182   1.1  christos 
    183   1.1  christos #if (HASHFRACTION & (HASHFRACTION - 1)) == 0
    184   1.1  christos 	if (p->hashfraction == HASHFRACTION)
    185   1.1  christos 		frompcindex =
    186   1.1  christos 		    &p->froms[
    187   1.1  christos 		    (size_t)(frompc / (HASHFRACTION * sizeof(*p->froms)))];
    188   1.1  christos 	else
    189   1.1  christos #endif
    190   1.1  christos 		frompcindex =
    191   1.1  christos 		    &p->froms[
    192   1.1  christos 		    (size_t)(frompc / (p->hashfraction * sizeof(*p->froms)))];
    193   1.1  christos 	toindex = *frompcindex;
    194   1.1  christos 	if (toindex == 0) {
    195   1.1  christos 		/*
    196   1.1  christos 		 *	first time traversing this arc
    197   1.1  christos 		 */
    198   1.1  christos 		toindex = ++p->tos[0].link;
    199   1.1  christos 		if (toindex >= p->tolimit)
    200   1.1  christos 			/* halt further profiling */
    201   1.1  christos 			goto overflow;
    202   1.1  christos 
    203   1.1  christos 		*frompcindex = (u_short)toindex;
    204   1.1  christos 		top = &p->tos[(size_t)toindex];
    205   1.1  christos 		top->selfpc = selfpc;
    206   1.1  christos 		top->count = 1;
    207   1.1  christos 		top->link = 0;
    208   1.1  christos 		goto done;
    209   1.1  christos 	}
    210   1.1  christos 	top = &p->tos[(size_t)toindex];
    211   1.1  christos 	if (top->selfpc == selfpc) {
    212   1.1  christos 		/*
    213   1.1  christos 		 * arc at front of chain; usual case.
    214   1.1  christos 		 */
    215   1.1  christos 		top->count++;
    216   1.1  christos 		goto done;
    217   1.1  christos 	}
    218   1.1  christos 	/*
    219   1.1  christos 	 * have to go looking down chain for it.
    220   1.1  christos 	 * top points to what we are looking at,
    221   1.1  christos 	 * prevtop points to previous top.
    222   1.1  christos 	 * we know it is not at the head of the chain.
    223   1.1  christos 	 */
    224   1.1  christos 	for (; /* goto done */; ) {
    225   1.1  christos 		if (top->link == 0) {
    226   1.1  christos 			/*
    227   1.1  christos 			 * top is end of the chain and none of the chain
    228   1.1  christos 			 * had top->selfpc == selfpc.
    229   1.1  christos 			 * so we allocate a new tostruct
    230   1.1  christos 			 * and link it to the head of the chain.
    231   1.1  christos 			 */
    232   1.1  christos 			toindex = ++p->tos[0].link;
    233   1.1  christos 			if (toindex >= p->tolimit)
    234   1.1  christos 				goto overflow;
    235   1.1  christos 
    236   1.1  christos 			top = &p->tos[(size_t)toindex];
    237   1.1  christos 			top->selfpc = selfpc;
    238   1.1  christos 			top->count = 1;
    239   1.1  christos 			top->link = *frompcindex;
    240   1.1  christos 			*frompcindex = (u_short)toindex;
    241   1.1  christos 			goto done;
    242   1.1  christos 		}
    243   1.1  christos 		/*
    244   1.1  christos 		 * otherwise, check the next arc on the chain.
    245   1.1  christos 		 */
    246   1.1  christos 		prevtop = top;
    247   1.1  christos 		top = &p->tos[top->link];
    248   1.1  christos 		if (top->selfpc == selfpc) {
    249   1.1  christos 			/*
    250   1.1  christos 			 * there it is.
    251   1.1  christos 			 * increment its count
    252   1.1  christos 			 * move it to the head of the chain.
    253   1.1  christos 			 */
    254   1.1  christos 			top->count++;
    255   1.1  christos 			toindex = prevtop->link;
    256   1.1  christos 			prevtop->link = top->link;
    257   1.1  christos 			top->link = *frompcindex;
    258   1.1  christos 			*frompcindex = (u_short)toindex;
    259   1.1  christos 			goto done;
    260   1.1  christos 		}
    261   1.1  christos 	}
    262   1.1  christos done:
    263   1.1  christos 	p->state = GMON_PROF_ON;
    264  1.11       ryo #if defined(_KERNEL) && !defined(_RUMPKERNEL)
    265   1.1  christos 	MCOUNT_EXIT;
    266   1.1  christos #endif
    267   1.1  christos 	return;
    268  1.11       ryo 
    269   1.1  christos overflow:
    270   1.1  christos 	p->state = GMON_PROF_ERROR;
    271  1.11       ryo #if defined(_KERNEL) && !defined(_RUMPKERNEL)
    272   1.1  christos 	MCOUNT_EXIT;
    273   1.1  christos #endif
    274   1.1  christos 	return;
    275   1.1  christos }
    276   1.1  christos 
    277   1.1  christos #ifdef MCOUNT
    278   1.1  christos /*
    279   1.1  christos  * Actual definition of mcount function.  Defined in <machine/profile.h>,
    280   1.1  christos  * which is included by <sys/gmon.h>.
    281   1.1  christos  */
    282   1.1  christos MCOUNT
    283   1.1  christos #endif
    284   1.1  christos 
    285  1.16       ryo #if defined(_KERNEL) && !defined(_RUMPKERNEL) && defined(MULTIPROCESSOR)
    286  1.16       ryo void _gmonparam_merge(struct gmonparam *, struct gmonparam *);
    287  1.16       ryo 
    288  1.16       ryo void
    289  1.16       ryo _gmonparam_merge(struct gmonparam *p, struct gmonparam *q)
    290  1.16       ryo {
    291  1.16       ryo 	u_long fromindex;
    292  1.16       ryo 	u_short *frompcindex, qtoindex, toindex;
    293  1.16       ryo 	u_long selfpc;
    294  1.16       ryo 	u_long endfrom;
    295  1.16       ryo 	long count;
    296  1.16       ryo 	struct tostruct *top;
    297  1.16       ryo 	int i;
    298  1.16       ryo 
    299  1.16       ryo 	count = q->kcountsize / sizeof(*q->kcount);
    300  1.16       ryo 	for (i = 0; i < count; i++)
    301  1.16       ryo 		p->kcount[i] += q->kcount[i];
    302  1.16       ryo 
    303  1.16       ryo 	endfrom = (q->fromssize / sizeof(*q->froms));
    304  1.16       ryo 	for (fromindex = 0; fromindex < endfrom; fromindex++) {
    305  1.16       ryo 		if (q->froms[fromindex] == 0)
    306  1.16       ryo 			continue;
    307  1.16       ryo 		for (qtoindex = q->froms[fromindex]; qtoindex != 0;
    308  1.16       ryo 		     qtoindex = q->tos[qtoindex].link) {
    309  1.16       ryo 			selfpc = q->tos[qtoindex].selfpc;
    310  1.16       ryo 			count = q->tos[qtoindex].count;
    311  1.16       ryo 			/* cribbed from mcount */
    312  1.16       ryo 			frompcindex = &p->froms[fromindex];
    313  1.16       ryo 			toindex = *frompcindex;
    314  1.16       ryo 			if (toindex == 0) {
    315  1.16       ryo 				/*
    316  1.16       ryo 				 * first time traversing this arc
    317  1.16       ryo 				 */
    318  1.16       ryo 				toindex = ++p->tos[0].link;
    319  1.16       ryo 				if (toindex >= p->tolimit)
    320  1.16       ryo 					/* halt further profiling */
    321  1.16       ryo 					goto overflow;
    322  1.16       ryo 
    323  1.16       ryo 				*frompcindex = (u_short)toindex;
    324  1.16       ryo 				top = &p->tos[(size_t)toindex];
    325  1.16       ryo 				top->selfpc = selfpc;
    326  1.16       ryo 				top->count = count;
    327  1.16       ryo 				top->link = 0;
    328  1.16       ryo 				goto done;
    329  1.16       ryo 			}
    330  1.16       ryo 			top = &p->tos[(size_t)toindex];
    331  1.16       ryo 			if (top->selfpc == selfpc) {
    332  1.16       ryo 				/*
    333  1.16       ryo 				 * arc at front of chain; usual case.
    334  1.16       ryo 				 */
    335  1.16       ryo 				top->count+= count;
    336  1.16       ryo 				goto done;
    337  1.16       ryo 			}
    338  1.16       ryo 			/*
    339  1.16       ryo 			 * have to go looking down chain for it.
    340  1.16       ryo 			 * top points to what we are looking at,
    341  1.16       ryo 			 * we know it is not at the head of the chain.
    342  1.16       ryo 			 */
    343  1.16       ryo 			for (; /* goto done */; ) {
    344  1.16       ryo 				if (top->link == 0) {
    345  1.16       ryo 					/*
    346  1.16       ryo 					 * top is end of the chain and
    347  1.16       ryo 					 * none of the chain had
    348  1.16       ryo 					 * top->selfpc == selfpc.  so
    349  1.16       ryo 					 * we allocate a new tostruct
    350  1.16       ryo 					 * and link it to the head of
    351  1.16       ryo 					 * the chain.
    352  1.16       ryo 					 */
    353  1.16       ryo 					toindex = ++p->tos[0].link;
    354  1.16       ryo 					if (toindex >= p->tolimit)
    355  1.16       ryo 						goto overflow;
    356  1.16       ryo 
    357  1.16       ryo 					top = &p->tos[(size_t)toindex];
    358  1.16       ryo 					top->selfpc = selfpc;
    359  1.16       ryo 					top->count = count;
    360  1.16       ryo 					top->link = *frompcindex;
    361  1.16       ryo 					*frompcindex = (u_short)toindex;
    362  1.16       ryo 					goto done;
    363  1.16       ryo 				}
    364  1.16       ryo 				/*
    365  1.16       ryo 				 * otherwise, check the next arc on the chain.
    366  1.16       ryo 				 */
    367  1.16       ryo 				top = &p->tos[top->link];
    368  1.16       ryo 				if (top->selfpc == selfpc) {
    369  1.16       ryo 					/*
    370  1.16       ryo 					 * there it is.
    371  1.16       ryo 					 * add to its count.
    372  1.16       ryo 					 */
    373  1.16       ryo 					top->count += count;
    374  1.16       ryo 					goto done;
    375  1.16       ryo 				}
    376  1.16       ryo 			}
    377  1.16       ryo 
    378  1.16       ryo 		done: ;
    379  1.16       ryo 		}
    380  1.16       ryo 
    381  1.16       ryo 	}
    382  1.16       ryo  overflow: ;
    383  1.16       ryo 
    384  1.16       ryo }
    385  1.16       ryo #endif
    386  1.16       ryo 
    387   1.5  christos #endif /* (!_KERNEL || GPROF) && !_STANDALONE */
    388