2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 static char sccsid[] = "@(#)printgprof.c 8.1 (Berkeley) 6/6/93";
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
43 #include "pathnames.h"
45 int namecmp(const void *, const void *);
46 int timecmp(const void *, const void *);
59 * Sort the symbol table in by time
61 sortednlp = (nltype **) calloc( nname , sizeof(nltype *) );
62 if ( sortednlp == (nltype **) 0 )
63 errx( 1 , "[printprof] ran out of memory for time sorting" );
64 for ( idx = 0 ; idx < nname ; idx += 1 ) {
65 sortednlp[ idx ] = &nl[ idx ];
67 qsort( sortednlp , nname , sizeof(nltype *) , timecmp );
68 for ( idx = 0 ; idx < nname ; idx += 1 ) {
69 np = sortednlp[ idx ];
77 timecmp(const void *v1, const void *v2)
79 const nltype **npp1 = (const nltype **)v1;
80 const nltype **npp2 = (const nltype **)v2;
84 timediff = (*npp2) -> time - (*npp1) -> time;
89 calldiff = (*npp2) -> ncall - (*npp1) -> ncall;
94 return( strcmp( (*npp1) -> name , (*npp2) -> name ) );
98 * header for flatprofline
105 printblurb( _PATH_FLAT_BLURB );
107 printf( "\ngranularity: each sample hit covers %g byte(s)" ,
108 scale * HISTORICAL_SCALE_2 );
109 if ( totime > 0.0 ) {
110 printf( " for %.2f%% of %.2f seconds\n\n" ,
111 100.0/totime , totime / hz );
113 printf( " no time accumulated\n\n" );
115 * this doesn't hurt since all the numerators will be zero.
119 printf( "%5.5s %10.10s %8.8s %8.8s %8.8s %8.8s %-8.8s\n" ,
120 "% " , "cumulative" , "self " , "" , "self " , "total " , "" );
121 printf( "%5.5s %10.10s %8.8s %8.8s %8.8s %8.8s %-8.8s\n" ,
122 "time" , "seconds " , "seconds" , "calls" ,
123 hz >= 10000000 ? "ns/call" : hz >= 10000 ? "us/call" : "ms/call" ,
124 hz >= 10000000 ? "ns/call" : hz >= 10000 ? "us/call" : "ms/call" ,
129 flatprofline(register nltype *np)
132 if ( zflag == 0 && np -> ncall == 0 && np -> time == 0 &&
133 np -> childtime == 0 ) {
136 actime += np -> time;
138 printf( "%5.1f %10.3f %8.3f" ,
139 100 * np -> time / totime , actime / hz , np -> time / hz );
141 printf( "%5.1f %10.2f %8.2f" ,
142 100 * np -> time / totime , actime / hz , np -> time / hz );
143 if ( np -> ncall != 0 ) {
145 printf( " %8ld %8.0f %8.0f " , np -> ncall ,
146 1e9 * np -> time / hz / np -> ncall ,
147 1e9 * ( np -> time + np -> childtime ) / hz / np -> ncall );
148 else if (hz >= 10000)
149 printf( " %8ld %8.0f %8.0f " , np -> ncall ,
150 1e6 * np -> time / hz / np -> ncall ,
151 1e6 * ( np -> time + np -> childtime ) / hz / np -> ncall );
153 printf( " %8ld %8.2f %8.2f " , np -> ncall ,
154 1000 * np -> time / hz / np -> ncall ,
155 1000 * ( np -> time + np -> childtime ) / hz / np -> ncall );
156 } else if ( np -> time != 0 || np -> childtime != 0 ) {
157 printf( " %8ld %7.2f%% %8.8s " , np -> ncall ,
158 100 * np -> time / ( np -> time + np -> childtime ) , "" );
160 printf( " %8.8s %8.8s %8.8s " , "" , "" , "" );
171 printblurb( _PATH_CALLG_BLURB );
173 printf( "\ngranularity: each sample hit covers %g byte(s)" ,
174 scale * HISTORICAL_SCALE_2 );
175 if ( printtime > 0.0 ) {
176 printf( " for %.2f%% of %.2f seconds\n\n" ,
177 100.0/printtime , printtime / hz );
179 printf( " no time propagated\n\n" );
181 * this doesn't hurt, since all the numerators will be 0.0
185 printf( "%6.6s %5.5s %7.7s %11.11s %7.7s/%-7.7s %-8.8s\n" ,
186 "" , "" , "" , "" , "called" , "total" , "parents");
187 printf( "%-6.6s %5.5s %7.7s %11.11s %7.7s+%-7.7s %-8.8s\t%5.5s\n" ,
188 "index" , "%time" , "self" , "descendents" ,
189 "called" , "self" , "name" , "index" );
190 printf( "%6.6s %5.5s %7.7s %11.11s %7.7s/%-7.7s %-8.8s\n" ,
191 "" , "" , "" , "" , "called" , "total" , "children");
196 gprofline(register nltype *np)
198 char kirkbuffer[ BUFSIZ ];
200 sprintf( kirkbuffer , "[%d]" , np -> index );
201 printf( "%-6.6s %5.1f %7.2f %11.2f" ,
203 100 * ( np -> propself + np -> propchild ) / printtime ,
204 np -> propself / hz ,
205 np -> propchild / hz );
206 if ( ( np -> ncall + np -> selfcalls ) != 0 ) {
207 printf( " %7ld" , np -> npropcall );
208 if ( np -> selfcalls != 0 ) {
209 printf( "+%-7ld " , np -> selfcalls );
211 printf( " %7.7s " , "" );
214 printf( " %7.7s %7.7s " , "" , "" );
221 printgprof(nltype **timesortnlp)
227 * Print out the structured profiling list
230 for ( idx = 0 ; idx < nname + ncycle ; idx ++ ) {
231 parentp = timesortnlp[ idx ];
233 parentp -> ncall == 0 &&
234 parentp -> selfcalls == 0 &&
235 parentp -> propself == 0 &&
236 parentp -> propchild == 0 ) {
239 if ( ! parentp -> printflag ) {
242 if ( parentp -> name == 0 && parentp -> cycleno != 0 ) {
246 printcycle( parentp );
247 printmembers( parentp );
249 printparents( parentp );
250 gprofline( parentp );
251 printchildren( parentp );
254 printf( "-----------------------------------------------\n" );
261 * sort by decreasing propagated time
262 * if times are equal, but one is a cycle header,
263 * say that's first (e.g. less, i.e. -1).
264 * if one's name doesn't have an underscore and the other does,
265 * say the one is first.
266 * all else being equal, sort by names.
269 totalcmp(const void *v1, const void *v2)
271 const nltype **npp1 = (const nltype **)v1;
272 const nltype **npp2 = (const nltype **)v2;
273 register const nltype *np1 = *npp1;
274 register const nltype *np2 = *npp2;
277 diff = ( np1 -> propself + np1 -> propchild )
278 - ( np2 -> propself + np2 -> propchild );
283 if ( np1 -> name == 0 && np1 -> cycleno != 0 )
285 if ( np2 -> name == 0 && np2 -> cycleno != 0 )
287 if ( np1 -> name == 0 )
289 if ( np2 -> name == 0 )
291 if ( *(np1 -> name) != '_' && *(np2 -> name) == '_' )
293 if ( *(np1 -> name) == '_' && *(np2 -> name) != '_' )
295 if ( np1 -> ncall > np2 -> ncall )
297 if ( np1 -> ncall < np2 -> ncall )
299 return strcmp( np1 -> name , np2 -> name );
303 printparents(nltype *childp)
309 if ( childp -> cyclehead != 0 ) {
310 cycleheadp = childp -> cyclehead;
314 if ( childp -> parents == 0 ) {
315 printf( "%6.6s %5.5s %7.7s %11.11s %7.7s %7.7s <spontaneous>\n" ,
316 "" , "" , "" , "" , "" , "" );
319 sortparents( childp );
320 for ( arcp = childp -> parents ; arcp ; arcp = arcp -> arc_parentlist ) {
321 parentp = arcp -> arc_parentp;
322 if ( childp == parentp || ( arcp -> arc_flags & DEADARC ) ||
323 ( childp->cycleno != 0 && parentp->cycleno == childp->cycleno ) ) {
325 * selfcall or call among siblings
327 printf( "%6.6s %5.5s %7.7s %11.11s %7ld %7.7s " ,
329 arcp -> arc_count , "" );
330 printname( parentp );
334 * regular parent of child
336 printf( "%6.6s %5.5s %7.2f %11.2f %7ld/%-7ld " ,
338 arcp -> arc_time / hz , arcp -> arc_childtime / hz ,
339 arcp -> arc_count , cycleheadp -> npropcall );
340 printname( parentp );
347 printchildren(nltype *parentp)
352 sortchildren( parentp );
353 arcp = parentp -> children;
354 for ( arcp = parentp -> children ; arcp ; arcp = arcp -> arc_childlist ) {
355 childp = arcp -> arc_childp;
356 if ( childp == parentp || ( arcp -> arc_flags & DEADARC ) ||
357 ( childp->cycleno != 0 && childp->cycleno == parentp->cycleno ) ) {
359 * self call or call to sibling
361 printf( "%6.6s %5.5s %7.7s %11.11s %7ld %7.7s " ,
362 "" , "" , "" , "" , arcp -> arc_count , "" );
367 * regular child of parent
369 printf( "%6.6s %5.5s %7.2f %11.2f %7ld/%-7ld " ,
371 arcp -> arc_time / hz , arcp -> arc_childtime / hz ,
372 arcp -> arc_count , childp -> cyclehead -> npropcall );
380 printname(nltype *selfp)
383 if ( selfp -> name != 0 ) {
384 printf( "%s" , selfp -> name );
386 if ( debug & DFNDEBUG ) {
387 printf( "{%d} " , selfp -> toporder );
389 if ( debug & PROPDEBUG ) {
390 printf( "%5.2f%% " , selfp -> propfraction );
394 if ( selfp -> cycleno != 0 ) {
395 printf( " <cycle %d>" , selfp -> cycleno );
397 if ( selfp -> index != 0 ) {
398 if ( selfp -> printflag ) {
399 printf( " [%d]" , selfp -> index );
401 printf( " (%d)" , selfp -> index );
407 sortchildren(nltype *parentp)
415 * unlink children from parent,
416 * then insertion sort back on to sorted's children.
417 * *arcp the arc you have detached and are inserting.
418 * *detachedp the rest of the arcs to be sorted.
419 * sorted arc list onto which you insertion sort.
420 * *prevp arc before the arc you are comparing.
422 sorted.arc_childlist = 0;
423 for ( (arcp = parentp -> children)&&(detachedp = arcp -> arc_childlist);
425 (arcp = detachedp)&&(detachedp = detachedp -> arc_childlist)) {
427 * consider *arcp as disconnected
428 * insert it into sorted
430 for ( prevp = &sorted ;
431 prevp -> arc_childlist ;
432 prevp = prevp -> arc_childlist ) {
433 if ( arccmp( arcp , prevp -> arc_childlist ) != LESSTHAN ) {
437 arcp -> arc_childlist = prevp -> arc_childlist;
438 prevp -> arc_childlist = arcp;
441 * reattach sorted children to parent
443 parentp -> children = sorted.arc_childlist;
447 sortparents(nltype *childp)
455 * unlink parents from child,
456 * then insertion sort back on to sorted's parents.
457 * *arcp the arc you have detached and are inserting.
458 * *detachedp the rest of the arcs to be sorted.
459 * sorted arc list onto which you insertion sort.
460 * *prevp arc before the arc you are comparing.
462 sorted.arc_parentlist = 0;
463 for ( (arcp = childp -> parents)&&(detachedp = arcp -> arc_parentlist);
465 (arcp = detachedp)&&(detachedp = detachedp -> arc_parentlist)) {
467 * consider *arcp as disconnected
468 * insert it into sorted
470 for ( prevp = &sorted ;
471 prevp -> arc_parentlist ;
472 prevp = prevp -> arc_parentlist ) {
473 if ( arccmp( arcp , prevp -> arc_parentlist ) != GREATERTHAN ) {
477 arcp -> arc_parentlist = prevp -> arc_parentlist;
478 prevp -> arc_parentlist = arcp;
481 * reattach sorted arcs to child
483 childp -> parents = sorted.arc_parentlist;
487 * print a cycle header
490 printcycle(nltype *cyclep)
492 char kirkbuffer[ BUFSIZ ];
494 sprintf( kirkbuffer , "[%d]" , cyclep -> index );
495 printf( "%-6.6s %5.1f %7.2f %11.2f %7ld" ,
497 100 * ( cyclep -> propself + cyclep -> propchild ) / printtime ,
498 cyclep -> propself / hz ,
499 cyclep -> propchild / hz ,
500 cyclep -> npropcall );
501 if ( cyclep -> selfcalls != 0 ) {
502 printf( "+%-7ld" , cyclep -> selfcalls );
504 printf( " %7.7s" , "" );
506 printf( " <cycle %d as a whole>\t[%d]\n" ,
507 cyclep -> cycleno , cyclep -> index );
511 * print the members of a cycle
514 printmembers(nltype *cyclep)
518 sortmembers( cyclep );
519 for ( memberp = cyclep -> cnext ; memberp ; memberp = memberp -> cnext ) {
520 printf( "%6.6s %5.5s %7.2f %11.2f %7ld" ,
521 "" , "" , memberp -> propself / hz , memberp -> propchild / hz ,
522 memberp -> npropcall );
523 if ( memberp -> selfcalls != 0 ) {
524 printf( "+%-7ld" , memberp -> selfcalls );
526 printf( " %7.7s" , "" );
529 printname( memberp );
535 * sort members of a cycle
538 sortmembers(nltype *cyclep)
545 * detach cycle members from cyclehead,
546 * and insertion sort them back on.
548 todo = cyclep -> cnext;
550 for ( (doing = todo)&&(todo = doing -> cnext);
552 (doing = todo )&&(todo = doing -> cnext )){
553 for ( prev = cyclep ; prev -> cnext ; prev = prev -> cnext ) {
554 if ( membercmp( doing , prev -> cnext ) == GREATERTHAN ) {
558 doing -> cnext = prev -> cnext;
559 prev -> cnext = doing;
564 * major sort is on propself + propchild,
565 * next is sort on ncalls + selfcalls.
568 membercmp(nltype *this, nltype *that)
570 double thistime = this -> propself + this -> propchild;
571 double thattime = that -> propself + that -> propchild;
572 long thiscalls = this -> ncall + this -> selfcalls;
573 long thatcalls = that -> ncall + that -> selfcalls;
575 if ( thistime > thattime ) {
578 if ( thistime < thattime ) {
581 if ( thiscalls > thatcalls ) {
584 if ( thiscalls < thatcalls ) {
590 * compare two arcs to/from the same child/parent.
591 * - if one arc is a self arc, it's least.
592 * - if one arc is within a cycle, it's less than.
593 * - if both arcs are within a cycle, compare arc counts.
594 * - if neither arc is within a cycle, compare with
595 * arc_time + arc_childtime as major key
596 * arc count as minor key
599 arccmp(arctype *thisp, arctype *thatp)
601 nltype *thisparentp = thisp -> arc_parentp;
602 nltype *thischildp = thisp -> arc_childp;
603 nltype *thatparentp = thatp -> arc_parentp;
604 nltype *thatchildp = thatp -> arc_childp;
609 if ( debug & TIMEDEBUG ) {
610 printf( "[arccmp] " );
611 printname( thisparentp );
613 printname ( thischildp );
614 printf( " %f + %f %ld/%ld\n" ,
615 thisp -> arc_time , thisp -> arc_childtime ,
616 thisp -> arc_count , thischildp -> ncall );
617 printf( "[arccmp] " );
618 printname( thatparentp );
620 printname( thatchildp );
621 printf( " %f + %f %ld/%ld\n" ,
622 thatp -> arc_time , thatp -> arc_childtime ,
623 thatp -> arc_count , thatchildp -> ncall );
627 if ( thisparentp == thischildp ) {
628 /* this is a self call */
631 if ( thatparentp == thatchildp ) {
632 /* that is a self call */
635 if ( thisparentp -> cycleno != 0 && thischildp -> cycleno != 0 &&
636 thisparentp -> cycleno == thischildp -> cycleno ) {
637 /* this is a call within a cycle */
638 if ( thatparentp -> cycleno != 0 && thatchildp -> cycleno != 0 &&
639 thatparentp -> cycleno == thatchildp -> cycleno ) {
640 /* that is a call within the cycle, too */
641 if ( thisp -> arc_count < thatp -> arc_count ) {
644 if ( thisp -> arc_count > thatp -> arc_count ) {
649 /* that isn't a call within the cycle */
653 /* this isn't a call within a cycle */
654 if ( thatparentp -> cycleno != 0 && thatchildp -> cycleno != 0 &&
655 thatparentp -> cycleno == thatchildp -> cycleno ) {
656 /* that is a call within a cycle */
659 /* neither is a call within a cycle */
660 thistime = thisp -> arc_time + thisp -> arc_childtime;
661 thattime = thatp -> arc_time + thatp -> arc_childtime;
662 if ( thistime < thattime )
664 if ( thistime > thattime )
666 if ( thisp -> arc_count < thatp -> arc_count )
668 if ( thisp -> arc_count > thatp -> arc_count )
676 printblurb(const char *blurbname)
681 blurbfile = fopen( blurbname , "r" );
682 if ( blurbfile == NULL ) {
683 warn( "%s" , blurbname );
686 while ( ( input = getc( blurbfile ) ) != EOF ) {
693 namecmp(const void *v1, const void *v2)
695 const nltype **npp1 = (const nltype **)v1;
696 const nltype **npp2 = (const nltype **)v2;
698 return( strcmp( (*npp1) -> name , (*npp2) -> name ) );
704 nltype **namesortnlp;
705 register nltype *nlp;
706 int idx, nnames, todo, i, j;
707 char peterbuffer[ BUFSIZ ];
710 * Now, sort regular function name alphabetically
711 * to create an index.
713 namesortnlp = (nltype **) calloc( nname + ncycle , sizeof(nltype *) );
714 if ( namesortnlp == (nltype **) 0 )
715 errx( 1 , "ran out of memory for sorting");
716 for ( idx = 0 , nnames = 0 ; idx < nname ; idx++ ) {
717 if ( zflag == 0 && nl[idx].ncall == 0 && nl[idx].time == 0 )
719 namesortnlp[nnames++] = &nl[idx];
721 qsort( namesortnlp , nnames , sizeof(nltype *) , namecmp );
722 for ( idx = 1 , todo = nnames ; idx <= ncycle ; idx++ ) {
723 namesortnlp[todo++] = &cyclenl[idx];
725 printf( "\f\nIndex by function name\n\n" );
726 idx = ( todo + 2 ) / 3;
727 for ( i = 0; i < idx ; i++ ) {
728 for ( j = i; j < todo ; j += idx ) {
729 nlp = namesortnlp[ j ];
730 if ( nlp -> printflag ) {
731 sprintf( peterbuffer , "[%d]" , nlp -> index );
733 sprintf( peterbuffer , "(%d)" , nlp -> index );
736 printf( "%6.6s %-19.19s" , peterbuffer , nlp -> name );
738 printf( "%6.6s " , peterbuffer );
739 sprintf( peterbuffer , "<cycle %d>" , nlp -> cycleno );
740 printf( "%-19.19s" , peterbuffer );