⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 arcs.c

📁 早期freebsd实现
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * Copyright (c) 1983, 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. */#ifndef lintstatic char sccsid[] = "@(#)arcs.c	8.1 (Berkeley) 6/6/93";#endif /* not lint */#include "gprof.h"#ifdef DEBUGint visited;int viable;int newcycle;int oldcycle;#endif DEBUG    /*     *	add (or just increment) an arc     */addarc( parentp , childp , count )    nltype	*parentp;    nltype	*childp;    long	count;{    arctype		*arcp;#   ifdef DEBUG	if ( debug & TALLYDEBUG ) {	    printf( "[addarc] %d arcs from %s to %s\n" ,		    count , parentp -> name , childp -> name );	}#   endif DEBUG    arcp = arclookup( parentp , childp );    if ( arcp != 0 ) {	    /*	     *	a hit:  just increment the count.	     */#	ifdef DEBUG	    if ( debug & TALLYDEBUG ) {		printf( "[tally] hit %d += %d\n" ,			arcp -> arc_count , count );	    }#	endif DEBUG	arcp -> arc_count += count;	return;    }    arcp = (arctype *)calloc( 1 , sizeof *arcp );    arcp -> arc_parentp = parentp;    arcp -> arc_childp = childp;    arcp -> arc_count = count;	/*	 *	prepend this child to the children of this parent	 */    arcp -> arc_childlist = parentp -> children;    parentp -> children = arcp;	/*	 *	prepend this parent to the parents of this child	 */    arcp -> arc_parentlist = childp -> parents;    childp -> parents = arcp;}    /*     *	the code below topologically sorts the graph (collapsing cycles),     *	and propagates time bottom up and flags top down.     */    /*     *	the topologically sorted name list pointers     */nltype	**topsortnlp;topcmp( npp1 , npp2 )    nltype	**npp1;    nltype	**npp2;{    return (*npp1) -> toporder - (*npp2) -> toporder;}nltype **doarcs(){    nltype	*parentp, **timesortnlp;    arctype	*arcp;    long	index;    long	pass;	/*	 *	initialize various things:	 *	    zero out child times.	 *	    count self-recursive calls.	 *	    indicate that nothing is on cycles.	 */    for ( parentp = nl ; parentp < npe ; parentp++ ) {	parentp -> childtime = 0.0;	arcp = arclookup( parentp , parentp );	if ( arcp != 0 ) {	    parentp -> ncall -= arcp -> arc_count;	    parentp -> selfcalls = arcp -> arc_count;	} else {	    parentp -> selfcalls = 0;	}	parentp -> npropcall = parentp -> ncall;	parentp -> propfraction = 0.0;	parentp -> propself = 0.0;	parentp -> propchild = 0.0;	parentp -> printflag = FALSE;	parentp -> toporder = DFN_NAN;	parentp -> cycleno = 0;	parentp -> cyclehead = parentp;	parentp -> cnext = 0;	if ( cflag ) {	    findcall( parentp , parentp -> value , (parentp+1) -> value );	}    }    for ( pass = 1 ; ; pass++ ) {	    /*	     *	topologically order things	     *	if any node is unnumbered,	     *	    number it and any of its descendents.	     */	for ( dfn_init() , parentp = nl ; parentp < npe ; parentp++ ) {	    if ( parentp -> toporder == DFN_NAN ) {		dfn( parentp );	    }	}	    /*	     *	link together nodes on the same cycle	     */	cyclelink();	    /*	     *	if no cycles to break up, proceed	     */	if ( ! Cflag )	    break;	    /*	     *	analyze cycles to determine breakup	     */#	ifdef DEBUG	    if ( debug & BREAKCYCLE ) {		printf("[doarcs] pass %d, cycle(s) %d\n" , pass , ncycle );	    }#	endif DEBUG	if ( pass == 1 ) {	    printf( "\n\n%s %s\n%s %d:\n" ,		"The following arcs were deleted" ,		"from the propagation calculation" ,		"to reduce the maximum cycle size to", cyclethreshold );	}	if ( cycleanalyze() )	    break;	free ( cyclenl );	ncycle = 0;	for ( parentp = nl ; parentp < npe ; parentp++ ) {	    parentp -> toporder = DFN_NAN;	    parentp -> cycleno = 0;	    parentp -> cyclehead = parentp;	    parentp -> cnext = 0;	}    }    if ( pass > 1 ) {	printf( "\f\n" );    } else {	printf( "\tNone\n\n" );    }	/*	 *	Sort the symbol table in reverse topological order	 */    topsortnlp = (nltype **) calloc( nname , sizeof(nltype *) );    if ( topsortnlp == (nltype **) 0 ) {	fprintf( stderr , "[doarcs] ran out of memory for topo sorting\n" );    }    for ( index = 0 ; index < nname ; index += 1 ) {	topsortnlp[ index ] = &nl[ index ];    }    qsort( topsortnlp , nname , sizeof(nltype *) , topcmp );#   ifdef DEBUG	if ( debug & DFNDEBUG ) {	    printf( "[doarcs] topological sort listing\n" );	    for ( index = 0 ; index < nname ; index += 1 ) {		printf( "[doarcs] " );		printf( "%d:" , topsortnlp[ index ] -> toporder );		printname( topsortnlp[ index ] );		printf( "\n" );	    }	}#   endif DEBUG	/*	 *	starting from the topological top,	 *	propagate print flags to children.	 *	also, calculate propagation fractions.	 *	this happens before time propagation	 *	since time propagation uses the fractions.	 */    doflags();	/*	 *	starting from the topological bottom, 	 *	propogate children times up to parents.	 */    dotime();	/*	 *	Now, sort by propself + propchild.	 *	sorting both the regular function names	 *	and cycle headers.	 */    timesortnlp = (nltype **) calloc( nname + ncycle , sizeof(nltype *) );    if ( timesortnlp == (nltype **) 0 ) {	fprintf( stderr , "%s: ran out of memory for sorting\n" , whoami );    }    for ( index = 0 ; index < nname ; index++ ) {	timesortnlp[index] = &nl[index];    }    for ( index = 1 ; index <= ncycle ; index++ ) {	timesortnlp[nname+index-1] = &cyclenl[index];    }    qsort( timesortnlp , nname + ncycle , sizeof(nltype *) , totalcmp );    for ( index = 0 ; index < nname + ncycle ; index++ ) {	timesortnlp[ index ] -> index = index + 1;    }    return( timesortnlp );}dotime(){    int	index;    cycletime();    for ( index = 0 ; index < nname ; index += 1 ) {	timepropagate( topsortnlp[ index ] );    }}timepropagate( parentp )    nltype	*parentp;{    arctype	*arcp;    nltype	*childp;    double	share;    double	propshare;    if ( parentp -> propfraction == 0.0 ) {	return;    }	/*	 *	gather time from children of this parent.	 */    for ( arcp = parentp -> children ; arcp ; arcp = arcp -> arc_childlist ) {	childp = arcp -> arc_childp;	if ( arcp -> arc_flags & DEADARC ) {	    continue;	}	if ( arcp -> arc_count == 0 ) {	    continue;	}	if ( childp == parentp ) {	    continue;	}	if ( childp -> propfraction == 0.0 ) {	    continue;	}	if ( childp -> cyclehead != childp ) {	    if ( parentp -> cycleno == childp -> cycleno ) {		continue;	    }	    if ( parentp -> toporder <= childp -> toporder ) {		fprintf( stderr , "[propagate] toporder botches\n" );	    }	    childp = childp -> cyclehead;	} else {	    if ( parentp -> toporder <= childp -> toporder ) {		fprintf( stderr , "[propagate] toporder botches\n" );		continue;	    }	}	if ( childp -> npropcall == 0 ) {	    continue;	}	    /*	     *	distribute time for this arc	     */	arcp -> arc_time = childp -> time			        * ( ( (double) arcp -> arc_count ) /				    ( (double) childp -> npropcall ) );	arcp -> arc_childtime = childp -> childtime			        * ( ( (double) arcp -> arc_count ) /				    ( (double) childp -> npropcall ) );	share = arcp -> arc_time + arcp -> arc_childtime;	parentp -> childtime += share;	    /*	     *	( 1 - propfraction ) gets lost along the way	     */	propshare = parentp -> propfraction * share;	    /*	     *	fix things for printing	     */	parentp -> propchild += propshare;	arcp -> arc_time *= parentp -> propfraction;	arcp -> arc_childtime *= parentp -> propfraction;	    /*	     *	add this share to the parent's cycle header, if any.	     */	if ( parentp -> cyclehead != parentp ) {	    parentp -> cyclehead -> childtime += share;	    parentp -> cyclehead -> propchild += propshare;	}#	ifdef DEBUG	    if ( debug & PROPDEBUG ) {		printf( "[dotime] child \t" );		printname( childp );		printf( " with %f %f %d/%d\n" ,			childp -> time , childp -> childtime ,			arcp -> arc_count , childp -> npropcall );		printf( "[dotime] parent\t" );		printname( parentp );		printf( "\n[dotime] share %f\n" , share );	    }#	endif DEBUG    }}cyclelink(){    register nltype	*nlp;    register nltype	*cyclenlp;    int			cycle;    nltype		*memberp;    arctype		*arcp;	/*	 *	Count the number of cycles, and initialze the cycle lists	 */    ncycle = 0;    for ( nlp = nl ; nlp < npe ; nlp++ ) {	    /*	     *	this is how you find unattached cycles	     */	if ( nlp -> cyclehead == nlp && nlp -> cnext != 0 ) {	    ncycle += 1;	}    }	/*	 *	cyclenl is indexed by cycle number:	 *	i.e. it is origin 1, not origin 0.	 */    cyclenl = (nltype *) calloc( ncycle + 1 , sizeof( nltype ) );    if ( cyclenl == 0 ) {	fprintf( stderr , "%s: No room for %d bytes of cycle headers\n" ,		whoami , ( ncycle + 1 ) * sizeof( nltype ) );	done();    }	/*	 *	now link cycles to true cycleheads,	 *	number them, accumulate the data for the cycle	 */    cycle = 0;    for ( nlp = nl ; nlp < npe ; nlp++ ) {	if ( !( nlp -> cyclehead == nlp && nlp -> cnext != 0 ) ) {	    continue;	}	cycle += 1;	cyclenlp = &cyclenl[cycle];        cyclenlp -> name = 0;		/* the name */        cyclenlp -> value = 0;		/* the pc entry point */        cyclenlp -> time = 0.0;		/* ticks in this routine */        cyclenlp -> childtime = 0.0;	/* cumulative ticks in children */	cyclenlp -> ncall = 0;		/* how many times called */	cyclenlp -> selfcalls = 0;	/* how many calls to self */	cyclenlp -> propfraction = 0.0;	/* what % of time propagates */	cyclenlp -> propself = 0.0;	/* how much self time propagates */	cyclenlp -> propchild = 0.0;	/* how much child time propagates */	cyclenlp -> printflag = TRUE;	/* should this be printed? */	cyclenlp -> index = 0;		/* index in the graph list */	cyclenlp -> toporder = DFN_NAN;	/* graph call chain top-sort order */	cyclenlp -> cycleno = cycle;	/* internal number of cycle on */	cyclenlp -> cyclehead = cyclenlp;	/* pointer to head of cycle */	cyclenlp -> cnext = nlp;	/* pointer to next member of cycle */	cyclenlp -> parents = 0;	/* list of caller arcs */	cyclenlp -> children = 0;	/* list of callee arcs */#	ifdef DEBUG	    if ( debug & CYCLEDEBUG ) {		printf( "[cyclelink] " );		printname( nlp );		printf( " is the head of cycle %d\n" , cycle );	    }#	endif DEBUG	    /*	     *	link members to cycle header	     */	for ( memberp = nlp ; memberp ; memberp = memberp -> cnext ) { 	    memberp -> cycleno = cycle;	    memberp -> cyclehead = cyclenlp;	}	    /*	     *	count calls from outside the cycle	     *	and those among cycle members	     */	for ( memberp = nlp ; memberp ; memberp = memberp -> cnext ) {	    for ( arcp=memberp->parents ; arcp ; arcp=arcp->arc_parentlist ) {		if ( arcp -> arc_parentp == memberp ) {		    continue;		}		if ( arcp -> arc_parentp -> cycleno == cycle ) {		    cyclenlp -> selfcalls += arcp -> arc_count;		} else {		    cyclenlp -> npropcall += arcp -> arc_count;		}	    }	}    }}    /*     *	analyze cycles to determine breakup     */cycleanalyze(){    arctype	**cyclestack;    arctype	**stkp;    arctype	**arcpp;    arctype	**endlist;    arctype	*arcp;    nltype	*nlp;    cltype	*clp;    bool	ret;    bool	done;    int		size;    int		cycleno;	/*	 *	calculate the size of the cycle, and find nodes that	 *	exit the cycle as they are desirable targets to cut	 *	some of their parents	 */    for ( done = TRUE , cycleno = 1 ; cycleno <= ncycle ; cycleno++ ) {	size = 0;	for (nlp = cyclenl[ cycleno ] . cnext; nlp; nlp = nlp -> cnext) {	    size += 1;	    nlp -> parentcnt = 0;	    nlp -> flags &= ~HASCYCLEXIT;	    for ( arcp = nlp -> parents; arcp; arcp = arcp -> arc_parentlist ) {		nlp -> parentcnt += 1;		if ( arcp -> arc_parentp -> cycleno != cycleno )		    nlp -> flags |= HASCYCLEXIT;	    }	}	if ( size <= cyclethreshold )

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -