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"
56 * Sort the symbol table in by time
58 sortednlp = (nltype **) calloc( nname , sizeof(nltype *) );
59 if ( sortednlp == (nltype **) 0 )
60 errx( 1 , "[printprof] ran out of memory for time sorting" );
61 for ( index = 0 ; index < nname ; index += 1 ) {
62 sortednlp[ index ] = &nl[ index ];
64 qsort( sortednlp , nname , sizeof(nltype *) , timecmp );
65 for ( index = 0 ; index < nname ; index += 1 ) {
66 np = sortednlp[ index ];
74 timecmp( npp1 , npp2 )
75 nltype **npp1, **npp2;
80 timediff = (*npp2) -> time - (*npp1) -> time;
85 calldiff = (*npp2) -> ncall - (*npp1) -> ncall;
90 return( strcmp( (*npp1) -> name , (*npp2) -> name ) );
94 * header for flatprofline
101 printblurb( _PATH_FLAT_BLURB );
103 printf( "\ngranularity: each sample hit covers %g byte(s)" ,
104 scale * HISTORICAL_SCALE_2 );
105 if ( totime > 0.0 ) {
106 printf( " for %.2f%% of %.2f seconds\n\n" ,
107 100.0/totime , totime / hz );
109 printf( " no time accumulated\n\n" );
111 * this doesn't hurt since all the numerators will be zero.
115 printf( "%5.5s %10.10s %8.8s %8.8s %8.8s %8.8s %-8.8s\n" ,
116 "% " , "cumulative" , "self " , "" , "self " , "total " , "" );
117 printf( "%5.5s %10.10s %8.8s %8.8s %8.8s %8.8s %-8.8s\n" ,
118 "time" , "seconds " , "seconds" , "calls" ,
119 hz >= 10000000 ? "ns/call" : hz >= 10000 ? "us/call" : "ms/call" ,
120 hz >= 10000000 ? "ns/call" : hz >= 10000 ? "us/call" : "ms/call" ,
129 if ( zflag == 0 && np -> ncall == 0 && np -> time == 0 &&
130 np -> childtime == 0 ) {
133 actime += np -> time;
135 printf( "%5.1f %10.3f %8.3f" ,
136 100 * np -> time / totime , actime / hz , np -> time / hz );
138 printf( "%5.1f %10.2f %8.2f" ,
139 100 * np -> time / totime , actime / hz , np -> time / hz );
140 if ( np -> ncall != 0 ) {
142 printf( " %8ld %8.0f %8.0f " , np -> ncall ,
143 1e9 * np -> time / hz / np -> ncall ,
144 1e9 * ( np -> time + np -> childtime ) / hz / np -> ncall );
145 else if (hz >= 10000)
146 printf( " %8ld %8.0f %8.0f " , np -> ncall ,
147 1e6 * np -> time / hz / np -> ncall ,
148 1e6 * ( np -> time + np -> childtime ) / hz / np -> ncall );
150 printf( " %8ld %8.2f %8.2f " , np -> ncall ,
151 1000 * np -> time / hz / np -> ncall ,
152 1000 * ( np -> time + np -> childtime ) / hz / np -> ncall );
153 } else if ( np -> time != 0 || np -> childtime != 0 ) {
154 printf( " %8ld %7.2f%% %8.8s " , np -> ncall ,
155 100 * np -> time / ( np -> time + np -> childtime ) , "" );
157 printf( " %8.8s %8.8s %8.8s " , "" , "" , "" );
168 printblurb( _PATH_CALLG_BLURB );
170 printf( "\ngranularity: each sample hit covers %g byte(s)" ,
171 scale * HISTORICAL_SCALE_2 );
172 if ( printtime > 0.0 ) {
173 printf( " for %.2f%% of %.2f seconds\n\n" ,
174 100.0/printtime , printtime / hz );
176 printf( " no time propagated\n\n" );
178 * this doesn't hurt, since all the numerators will be 0.0
182 printf( "%6.6s %5.5s %7.7s %11.11s %7.7s/%-7.7s %-8.8s\n" ,
183 "" , "" , "" , "" , "called" , "total" , "parents");
184 printf( "%-6.6s %5.5s %7.7s %11.11s %7.7s+%-7.7s %-8.8s\t%5.5s\n" ,
185 "index" , "%time" , "self" , "descendents" ,
186 "called" , "self" , "name" , "index" );
187 printf( "%6.6s %5.5s %7.7s %11.11s %7.7s/%-7.7s %-8.8s\n" ,
188 "" , "" , "" , "" , "called" , "total" , "children");
196 char kirkbuffer[ BUFSIZ ];
198 sprintf( kirkbuffer , "[%d]" , np -> index );
199 printf( "%-6.6s %5.1f %7.2f %11.2f" ,
201 100 * ( np -> propself + np -> propchild ) / printtime ,
202 np -> propself / hz ,
203 np -> propchild / hz );
204 if ( ( np -> ncall + np -> selfcalls ) != 0 ) {
205 printf( " %7ld" , np -> npropcall );
206 if ( np -> selfcalls != 0 ) {
207 printf( "+%-7ld " , np -> selfcalls );
209 printf( " %7.7s " , "" );
212 printf( " %7.7s %7.7s " , "" , "" );
219 printgprof(timesortnlp)
220 nltype **timesortnlp;
226 * Print out the structured profiling list
229 for ( index = 0 ; index < nname + ncycle ; index ++ ) {
230 parentp = timesortnlp[ index ];
232 parentp -> ncall == 0 &&
233 parentp -> selfcalls == 0 &&
234 parentp -> propself == 0 &&
235 parentp -> propchild == 0 ) {
238 if ( ! parentp -> printflag ) {
241 if ( parentp -> name == 0 && parentp -> cycleno != 0 ) {
245 printcycle( parentp );
246 printmembers( parentp );
248 printparents( parentp );
249 gprofline( parentp );
250 printchildren( parentp );
253 printf( "-----------------------------------------------\n" );
260 * sort by decreasing propagated time
261 * if times are equal, but one is a cycle header,
262 * say that's first (e.g. less, i.e. -1).
263 * if one's name doesn't have an underscore and the other does,
264 * say the one is first.
265 * all else being equal, sort by names.
268 totalcmp( npp1 , npp2 )
272 register nltype *np1 = *npp1;
273 register nltype *np2 = *npp2;
276 diff = ( np1 -> propself + np1 -> propchild )
277 - ( np2 -> propself + np2 -> propchild );
282 if ( np1 -> name == 0 && np1 -> cycleno != 0 )
284 if ( np2 -> name == 0 && np2 -> cycleno != 0 )
286 if ( np1 -> name == 0 )
288 if ( np2 -> name == 0 )
290 if ( *(np1 -> name) != '_' && *(np2 -> name) == '_' )
292 if ( *(np1 -> name) == '_' && *(np2 -> name) != '_' )
294 if ( np1 -> ncall > np2 -> ncall )
296 if ( np1 -> ncall < np2 -> ncall )
298 return strcmp( np1 -> name , np2 -> name );
302 printparents( 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( parentp )
353 sortchildren( parentp );
354 arcp = parentp -> children;
355 for ( arcp = parentp -> children ; arcp ; arcp = arcp -> arc_childlist ) {
356 childp = arcp -> arc_childp;
357 if ( childp == parentp || ( arcp -> arc_flags & DEADARC ) ||
358 ( childp->cycleno != 0 && childp->cycleno == parentp->cycleno ) ) {
360 * self call or call to sibling
362 printf( "%6.6s %5.5s %7.7s %11.11s %7ld %7.7s " ,
363 "" , "" , "" , "" , arcp -> arc_count , "" );
368 * regular child of parent
370 printf( "%6.6s %5.5s %7.2f %11.2f %7ld/%-7ld " ,
372 arcp -> arc_time / hz , arcp -> arc_childtime / hz ,
373 arcp -> arc_count , childp -> cyclehead -> npropcall );
385 if ( selfp -> name != 0 ) {
386 printf( "%s" , selfp -> name );
388 if ( debug & DFNDEBUG ) {
389 printf( "{%d} " , selfp -> toporder );
391 if ( debug & PROPDEBUG ) {
392 printf( "%5.2f%% " , selfp -> propfraction );
396 if ( selfp -> cycleno != 0 ) {
397 printf( " <cycle %d>" , selfp -> cycleno );
399 if ( selfp -> index != 0 ) {
400 if ( selfp -> printflag ) {
401 printf( " [%d]" , selfp -> index );
403 printf( " (%d)" , selfp -> index );
409 sortchildren( parentp )
418 * unlink children from parent,
419 * then insertion sort back on to sorted's children.
420 * *arcp the arc you have detached and are inserting.
421 * *detachedp the rest of the arcs to be sorted.
422 * sorted arc list onto which you insertion sort.
423 * *prevp arc before the arc you are comparing.
425 sorted.arc_childlist = 0;
426 for ( (arcp = parentp -> children)&&(detachedp = arcp -> arc_childlist);
428 (arcp = detachedp)&&(detachedp = detachedp -> arc_childlist)) {
430 * consider *arcp as disconnected
431 * insert it into sorted
433 for ( prevp = &sorted ;
434 prevp -> arc_childlist ;
435 prevp = prevp -> arc_childlist ) {
436 if ( arccmp( arcp , prevp -> arc_childlist ) != LESSTHAN ) {
440 arcp -> arc_childlist = prevp -> arc_childlist;
441 prevp -> arc_childlist = arcp;
444 * reattach sorted children to parent
446 parentp -> children = sorted.arc_childlist;
450 sortparents( childp )
459 * unlink parents from child,
460 * then insertion sort back on to sorted's parents.
461 * *arcp the arc you have detached and are inserting.
462 * *detachedp the rest of the arcs to be sorted.
463 * sorted arc list onto which you insertion sort.
464 * *prevp arc before the arc you are comparing.
466 sorted.arc_parentlist = 0;
467 for ( (arcp = childp -> parents)&&(detachedp = arcp -> arc_parentlist);
469 (arcp = detachedp)&&(detachedp = detachedp -> arc_parentlist)) {
471 * consider *arcp as disconnected
472 * insert it into sorted
474 for ( prevp = &sorted ;
475 prevp -> arc_parentlist ;
476 prevp = prevp -> arc_parentlist ) {
477 if ( arccmp( arcp , prevp -> arc_parentlist ) != GREATERTHAN ) {
481 arcp -> arc_parentlist = prevp -> arc_parentlist;
482 prevp -> arc_parentlist = arcp;
485 * reattach sorted arcs to child
487 childp -> parents = sorted.arc_parentlist;
491 * print a cycle header
497 char kirkbuffer[ BUFSIZ ];
499 sprintf( kirkbuffer , "[%d]" , cyclep -> index );
500 printf( "%-6.6s %5.1f %7.2f %11.2f %7ld" ,
502 100 * ( cyclep -> propself + cyclep -> propchild ) / printtime ,
503 cyclep -> propself / hz ,
504 cyclep -> propchild / hz ,
505 cyclep -> npropcall );
506 if ( cyclep -> selfcalls != 0 ) {
507 printf( "+%-7ld" , cyclep -> selfcalls );
509 printf( " %7.7s" , "" );
511 printf( " <cycle %d as a whole>\t[%d]\n" ,
512 cyclep -> cycleno , cyclep -> index );
516 * print the members of a cycle
519 printmembers( cyclep )
524 sortmembers( cyclep );
525 for ( memberp = cyclep -> cnext ; memberp ; memberp = memberp -> cnext ) {
526 printf( "%6.6s %5.5s %7.2f %11.2f %7ld" ,
527 "" , "" , memberp -> propself / hz , memberp -> propchild / hz ,
528 memberp -> npropcall );
529 if ( memberp -> selfcalls != 0 ) {
530 printf( "+%-7ld" , memberp -> selfcalls );
532 printf( " %7.7s" , "" );
535 printname( memberp );
541 * sort members of a cycle
544 sortmembers( cyclep )
552 * detach cycle members from cyclehead,
553 * and insertion sort them back on.
555 todo = cyclep -> cnext;
557 for ( (doing = todo)&&(todo = doing -> cnext);
559 (doing = todo )&&(todo = doing -> cnext )){
560 for ( prev = cyclep ; prev -> cnext ; prev = prev -> cnext ) {
561 if ( membercmp( doing , prev -> cnext ) == GREATERTHAN ) {
565 doing -> cnext = prev -> cnext;
566 prev -> cnext = doing;
571 * major sort is on propself + propchild,
572 * next is sort on ncalls + selfcalls.
575 membercmp( this , that )
579 double thistime = this -> propself + this -> propchild;
580 double thattime = that -> propself + that -> propchild;
581 long thiscalls = this -> ncall + this -> selfcalls;
582 long thatcalls = that -> ncall + that -> selfcalls;
584 if ( thistime > thattime ) {
587 if ( thistime < thattime ) {
590 if ( thiscalls > thatcalls ) {
593 if ( thiscalls < thatcalls ) {
599 * compare two arcs to/from the same child/parent.
600 * - if one arc is a self arc, it's least.
601 * - if one arc is within a cycle, it's less than.
602 * - if both arcs are within a cycle, compare arc counts.
603 * - if neither arc is within a cycle, compare with
604 * arc_time + arc_childtime as major key
605 * arc count as minor key
608 arccmp( thisp , thatp )
612 nltype *thisparentp = thisp -> arc_parentp;
613 nltype *thischildp = thisp -> arc_childp;
614 nltype *thatparentp = thatp -> arc_parentp;
615 nltype *thatchildp = thatp -> arc_childp;
620 if ( debug & TIMEDEBUG ) {
621 printf( "[arccmp] " );
622 printname( thisparentp );
624 printname ( thischildp );
625 printf( " %f + %f %ld/%ld\n" ,
626 thisp -> arc_time , thisp -> arc_childtime ,
627 thisp -> arc_count , thischildp -> ncall );
628 printf( "[arccmp] " );
629 printname( thatparentp );
631 printname( thatchildp );
632 printf( " %f + %f %ld/%ld\n" ,
633 thatp -> arc_time , thatp -> arc_childtime ,
634 thatp -> arc_count , thatchildp -> ncall );
638 if ( thisparentp == thischildp ) {
639 /* this is a self call */
642 if ( thatparentp == thatchildp ) {
643 /* that is a self call */
646 if ( thisparentp -> cycleno != 0 && thischildp -> cycleno != 0 &&
647 thisparentp -> cycleno == thischildp -> cycleno ) {
648 /* this is a call within a cycle */
649 if ( thatparentp -> cycleno != 0 && thatchildp -> cycleno != 0 &&
650 thatparentp -> cycleno == thatchildp -> cycleno ) {
651 /* that is a call within the cycle, too */
652 if ( thisp -> arc_count < thatp -> arc_count ) {
655 if ( thisp -> arc_count > thatp -> arc_count ) {
660 /* that isn't a call within the cycle */
664 /* this isn't a call within a cycle */
665 if ( thatparentp -> cycleno != 0 && thatchildp -> cycleno != 0 &&
666 thatparentp -> cycleno == thatchildp -> cycleno ) {
667 /* that is a call within a cycle */
670 /* neither is a call within a cycle */
671 thistime = thisp -> arc_time + thisp -> arc_childtime;
672 thattime = thatp -> arc_time + thatp -> arc_childtime;
673 if ( thistime < thattime )
675 if ( thistime > thattime )
677 if ( thisp -> arc_count < thatp -> arc_count )
679 if ( thisp -> arc_count > thatp -> arc_count )
687 printblurb( blurbname )
693 blurbfile = fopen( blurbname , "r" );
694 if ( blurbfile == NULL ) {
695 warn( "%s" , blurbname );
698 while ( ( input = getc( blurbfile ) ) != EOF ) {
705 namecmp( npp1 , npp2 )
706 nltype **npp1, **npp2;
708 return( strcmp( (*npp1) -> name , (*npp2) -> name ) );
714 nltype **namesortnlp;
715 register nltype *nlp;
716 int index, nnames, todo, i, j;
717 char peterbuffer[ BUFSIZ ];
720 * Now, sort regular function name alphabetically
721 * to create an index.
723 namesortnlp = (nltype **) calloc( nname + ncycle , sizeof(nltype *) );
724 if ( namesortnlp == (nltype **) 0 )
725 errx( 1 , "ran out of memory for sorting");
726 for ( index = 0 , nnames = 0 ; index < nname ; index++ ) {
727 if ( zflag == 0 && nl[index].ncall == 0 && nl[index].time == 0 )
729 namesortnlp[nnames++] = &nl[index];
731 qsort( namesortnlp , nnames , sizeof(nltype *) , namecmp );
732 for ( index = 1 , todo = nnames ; index <= ncycle ; index++ ) {
733 namesortnlp[todo++] = &cyclenl[index];
735 printf( "\f\nIndex by function name\n\n" );
736 index = ( todo + 2 ) / 3;
737 for ( i = 0; i < index ; i++ ) {
738 for ( j = i; j < todo ; j += index ) {
739 nlp = namesortnlp[ j ];
740 if ( nlp -> printflag ) {
741 sprintf( peterbuffer , "[%d]" , nlp -> index );
743 sprintf( peterbuffer , "(%d)" , nlp -> index );
746 printf( "%6.6s %-19.19s" , peterbuffer , nlp -> name );
748 printf( "%6.6s " , peterbuffer );
749 sprintf( peterbuffer , "<cycle %d>" , nlp -> cycleno );
750 printf( "%-19.19s" , peterbuffer );