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 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)printgprof.c 8.1 (Berkeley) 6/6/93
34 * $FreeBSD: src/usr.bin/gprof/printgprof.c,v 1.6 1999/08/28 01:01:56 peter Exp $
35 * $DragonFly: src/usr.bin/gprof/printgprof.c,v 1.2 2003/06/17 04:29:27 dillon Exp $
40 #include "pathnames.h"
52 * Sort the symbol table in by time
54 sortednlp = (nltype **) calloc( nname , sizeof(nltype *) );
55 if ( sortednlp == (nltype **) 0 ) {
56 fprintf( stderr , "[printprof] ran out of memory for time sorting\n" );
58 for ( index = 0 ; index < nname ; index += 1 ) {
59 sortednlp[ index ] = &nl[ index ];
61 qsort( sortednlp , nname , sizeof(nltype *) , timecmp );
62 for ( index = 0 ; index < nname ; index += 1 ) {
63 np = sortednlp[ index ];
70 timecmp( npp1 , npp2 )
71 nltype **npp1, **npp2;
76 timediff = (*npp2) -> time - (*npp1) -> time;
81 calldiff = (*npp2) -> ncall - (*npp1) -> ncall;
86 return( strcmp( (*npp1) -> name , (*npp2) -> name ) );
90 * header for flatprofline
96 printblurb( _PATH_FLAT_BLURB );
98 printf( "\ngranularity: each sample hit covers %d byte(s)" ,
99 (long) scale * sizeof(UNIT) );
100 if ( totime > 0.0 ) {
101 printf( " for %.2f%% of %.2f seconds\n\n" ,
102 100.0/totime , totime / hz );
104 printf( " no time accumulated\n\n" );
106 * this doesn't hurt sinc eall the numerators will be zero.
110 printf( "%5.5s %10.10s %8.8s %8.8s %8.8s %8.8s %-8.8s\n" ,
111 "% " , "cumulative" , "self " , "" , "self " , "total " , "" );
112 printf( "%5.5s %10.10s %8.8s %8.8s %8.8s %8.8s %-8.8s\n" ,
113 "time" , "seconds " , "seconds" , "calls" ,
114 hz >= 10000000 ? "ns/call" : hz >= 10000 ? "us/call" : "ms/call" ,
115 hz >= 10000000 ? "ns/call" : hz >= 10000 ? "us/call" : "ms/call" ,
123 if ( zflag == 0 && np -> ncall == 0 && np -> time == 0 ) {
126 actime += np -> time;
128 printf( "%5.1f %10.3f %8.3f" ,
129 100 * np -> time / totime , actime / hz , np -> time / hz );
131 printf( "%5.1f %10.2f %8.2f" ,
132 100 * np -> time / totime , actime / hz , np -> time / hz );
133 if ( np -> ncall != 0 ) {
135 printf( " %8d %8.0f %8.0f " , np -> ncall ,
136 1e9 * np -> time / hz / np -> ncall ,
137 1e9 * ( np -> time + np -> childtime ) / hz / np -> ncall );
138 else if (hz >= 10000)
139 printf( " %8d %8.0f %8.0f " , np -> ncall ,
140 1e6 * np -> time / hz / np -> ncall ,
141 1e6 * ( np -> time + np -> childtime ) / hz / np -> ncall );
143 printf( " %8d %8.2f %8.2f " , np -> ncall ,
144 1000 * np -> time / hz / np -> ncall ,
145 1000 * ( np -> time + np -> childtime ) / hz / np -> ncall );
147 printf( " %8.8s %8.8s %8.8s " , "" , "" , "" );
157 printblurb( _PATH_CALLG_BLURB );
159 printf( "\ngranularity: each sample hit covers %d byte(s)" ,
160 (long) scale * sizeof(UNIT) );
161 if ( printtime > 0.0 ) {
162 printf( " for %.2f%% of %.2f seconds\n\n" ,
163 100.0/printtime , printtime / hz );
165 printf( " no time propagated\n\n" );
167 * this doesn't hurt, since all the numerators will be 0.0
171 printf( "%6.6s %5.5s %7.7s %11.11s %7.7s/%-7.7s %-8.8s\n" ,
172 "" , "" , "" , "" , "called" , "total" , "parents");
173 printf( "%-6.6s %5.5s %7.7s %11.11s %7.7s+%-7.7s %-8.8s\t%5.5s\n" ,
174 "index" , "%time" , "self" , "descendents" ,
175 "called" , "self" , "name" , "index" );
176 printf( "%6.6s %5.5s %7.7s %11.11s %7.7s/%-7.7s %-8.8s\n" ,
177 "" , "" , "" , "" , "called" , "total" , "children");
184 char kirkbuffer[ BUFSIZ ];
186 sprintf( kirkbuffer , "[%d]" , np -> index );
187 printf( "%-6.6s %5.1f %7.2f %11.2f" ,
189 100 * ( np -> propself + np -> propchild ) / printtime ,
190 np -> propself / hz ,
191 np -> propchild / hz );
192 if ( ( np -> ncall + np -> selfcalls ) != 0 ) {
193 printf( " %7d" , np -> npropcall );
194 if ( np -> selfcalls != 0 ) {
195 printf( "+%-7d " , np -> selfcalls );
197 printf( " %7.7s " , "" );
200 printf( " %7.7s %7.7s " , "" , "" );
206 printgprof(timesortnlp)
207 nltype **timesortnlp;
213 * Print out the structured profiling list
216 for ( index = 0 ; index < nname + ncycle ; index ++ ) {
217 parentp = timesortnlp[ index ];
219 parentp -> ncall == 0 &&
220 parentp -> selfcalls == 0 &&
221 parentp -> propself == 0 &&
222 parentp -> propchild == 0 ) {
225 if ( ! parentp -> printflag ) {
228 if ( parentp -> name == 0 && parentp -> cycleno != 0 ) {
232 printcycle( parentp );
233 printmembers( parentp );
235 printparents( parentp );
236 gprofline( parentp );
237 printchildren( parentp );
240 printf( "-----------------------------------------------\n" );
247 * sort by decreasing propagated time
248 * if times are equal, but one is a cycle header,
249 * say that's first (e.g. less, i.e. -1).
250 * if one's name doesn't have an underscore and the other does,
251 * say the one is first.
252 * all else being equal, sort by names.
255 totalcmp( npp1 , npp2 )
259 register nltype *np1 = *npp1;
260 register nltype *np2 = *npp2;
263 diff = ( np1 -> propself + np1 -> propchild )
264 - ( np2 -> propself + np2 -> propchild );
269 if ( np1 -> name == 0 && np1 -> cycleno != 0 )
271 if ( np2 -> name == 0 && np2 -> cycleno != 0 )
273 if ( np1 -> name == 0 )
275 if ( np2 -> name == 0 )
277 if ( *(np1 -> name) != '_' && *(np2 -> name) == '_' )
279 if ( *(np1 -> name) == '_' && *(np2 -> name) != '_' )
281 if ( np1 -> ncall > np2 -> ncall )
283 if ( np1 -> ncall < np2 -> ncall )
285 return strcmp( np1 -> name , np2 -> name );
288 printparents( childp )
295 if ( childp -> cyclehead != 0 ) {
296 cycleheadp = childp -> cyclehead;
300 if ( childp -> parents == 0 ) {
301 printf( "%6.6s %5.5s %7.7s %11.11s %7.7s %7.7s <spontaneous>\n" ,
302 "" , "" , "" , "" , "" , "" );
305 sortparents( childp );
306 for ( arcp = childp -> parents ; arcp ; arcp = arcp -> arc_parentlist ) {
307 parentp = arcp -> arc_parentp;
308 if ( childp == parentp || ( arcp -> arc_flags & DEADARC ) ||
309 ( childp->cycleno != 0 && parentp->cycleno == childp->cycleno ) ) {
311 * selfcall or call among siblings
313 printf( "%6.6s %5.5s %7.7s %11.11s %7d %7.7s " ,
315 arcp -> arc_count , "" );
316 printname( parentp );
320 * regular parent of child
322 printf( "%6.6s %5.5s %7.2f %11.2f %7d/%-7d " ,
324 arcp -> arc_time / hz , arcp -> arc_childtime / hz ,
325 arcp -> arc_count , cycleheadp -> npropcall );
326 printname( parentp );
332 printchildren( parentp )
338 sortchildren( parentp );
339 arcp = parentp -> children;
340 for ( arcp = parentp -> children ; arcp ; arcp = arcp -> arc_childlist ) {
341 childp = arcp -> arc_childp;
342 if ( childp == parentp || ( arcp -> arc_flags & DEADARC ) ||
343 ( childp->cycleno != 0 && childp->cycleno == parentp->cycleno ) ) {
345 * self call or call to sibling
347 printf( "%6.6s %5.5s %7.7s %11.11s %7d %7.7s " ,
348 "" , "" , "" , "" , arcp -> arc_count , "" );
353 * regular child of parent
355 printf( "%6.6s %5.5s %7.2f %11.2f %7d/%-7d " ,
357 arcp -> arc_time / hz , arcp -> arc_childtime / hz ,
358 arcp -> arc_count , childp -> cyclehead -> npropcall );
369 if ( selfp -> name != 0 ) {
370 printf( "%s" , selfp -> name );
372 if ( debug & DFNDEBUG ) {
373 printf( "{%d} " , selfp -> toporder );
375 if ( debug & PROPDEBUG ) {
376 printf( "%5.2f%% " , selfp -> propfraction );
380 if ( selfp -> cycleno != 0 ) {
381 printf( " <cycle %d>" , selfp -> cycleno );
383 if ( selfp -> index != 0 ) {
384 if ( selfp -> printflag ) {
385 printf( " [%d]" , selfp -> index );
387 printf( " (%d)" , selfp -> index );
392 sortchildren( parentp )
401 * unlink children from parent,
402 * then insertion sort back on to sorted's children.
403 * *arcp the arc you have detached and are inserting.
404 * *detachedp the rest of the arcs to be sorted.
405 * sorted arc list onto which you insertion sort.
406 * *prevp arc before the arc you are comparing.
408 sorted.arc_childlist = 0;
409 for ( (arcp = parentp -> children)&&(detachedp = arcp -> arc_childlist);
411 (arcp = detachedp)&&(detachedp = detachedp -> arc_childlist)) {
413 * consider *arcp as disconnected
414 * insert it into sorted
416 for ( prevp = &sorted ;
417 prevp -> arc_childlist ;
418 prevp = prevp -> arc_childlist ) {
419 if ( arccmp( arcp , prevp -> arc_childlist ) != LESSTHAN ) {
423 arcp -> arc_childlist = prevp -> arc_childlist;
424 prevp -> arc_childlist = arcp;
427 * reattach sorted children to parent
429 parentp -> children = sorted.arc_childlist;
432 sortparents( childp )
441 * unlink parents from child,
442 * then insertion sort back on to sorted's parents.
443 * *arcp the arc you have detached and are inserting.
444 * *detachedp the rest of the arcs to be sorted.
445 * sorted arc list onto which you insertion sort.
446 * *prevp arc before the arc you are comparing.
448 sorted.arc_parentlist = 0;
449 for ( (arcp = childp -> parents)&&(detachedp = arcp -> arc_parentlist);
451 (arcp = detachedp)&&(detachedp = detachedp -> arc_parentlist)) {
453 * consider *arcp as disconnected
454 * insert it into sorted
456 for ( prevp = &sorted ;
457 prevp -> arc_parentlist ;
458 prevp = prevp -> arc_parentlist ) {
459 if ( arccmp( arcp , prevp -> arc_parentlist ) != GREATERTHAN ) {
463 arcp -> arc_parentlist = prevp -> arc_parentlist;
464 prevp -> arc_parentlist = arcp;
467 * reattach sorted arcs to child
469 childp -> parents = sorted.arc_parentlist;
473 * print a cycle header
478 char kirkbuffer[ BUFSIZ ];
480 sprintf( kirkbuffer , "[%d]" , cyclep -> index );
481 printf( "%-6.6s %5.1f %7.2f %11.2f %7d" ,
483 100 * ( cyclep -> propself + cyclep -> propchild ) / printtime ,
484 cyclep -> propself / hz ,
485 cyclep -> propchild / hz ,
486 cyclep -> npropcall );
487 if ( cyclep -> selfcalls != 0 ) {
488 printf( "+%-7d" , cyclep -> selfcalls );
490 printf( " %7.7s" , "" );
492 printf( " <cycle %d as a whole>\t[%d]\n" ,
493 cyclep -> cycleno , cyclep -> index );
497 * print the members of a cycle
499 printmembers( cyclep )
504 sortmembers( cyclep );
505 for ( memberp = cyclep -> cnext ; memberp ; memberp = memberp -> cnext ) {
506 printf( "%6.6s %5.5s %7.2f %11.2f %7d" ,
507 "" , "" , memberp -> propself / hz , memberp -> propchild / hz ,
508 memberp -> npropcall );
509 if ( memberp -> selfcalls != 0 ) {
510 printf( "+%-7d" , memberp -> selfcalls );
512 printf( " %7.7s" , "" );
515 printname( memberp );
521 * sort members of a cycle
523 sortmembers( cyclep )
531 * detach cycle members from cyclehead,
532 * and insertion sort them back on.
534 todo = cyclep -> cnext;
536 for ( (doing = todo)&&(todo = doing -> cnext);
538 (doing = todo )&&(todo = doing -> cnext )){
539 for ( prev = cyclep ; prev -> cnext ; prev = prev -> cnext ) {
540 if ( membercmp( doing , prev -> cnext ) == GREATERTHAN ) {
544 doing -> cnext = prev -> cnext;
545 prev -> cnext = doing;
550 * major sort is on propself + propchild,
551 * next is sort on ncalls + selfcalls.
554 membercmp( this , that )
558 double thistime = this -> propself + this -> propchild;
559 double thattime = that -> propself + that -> propchild;
560 long thiscalls = this -> ncall + this -> selfcalls;
561 long thatcalls = that -> ncall + that -> selfcalls;
563 if ( thistime > thattime ) {
566 if ( thistime < thattime ) {
569 if ( thiscalls > thatcalls ) {
572 if ( thiscalls < thatcalls ) {
578 * compare two arcs to/from the same child/parent.
579 * - if one arc is a self arc, it's least.
580 * - if one arc is within a cycle, it's less than.
581 * - if both arcs are within a cycle, compare arc counts.
582 * - if neither arc is within a cycle, compare with
583 * arc_time + arc_childtime as major key
584 * arc count as minor key
587 arccmp( thisp , thatp )
591 nltype *thisparentp = thisp -> arc_parentp;
592 nltype *thischildp = thisp -> arc_childp;
593 nltype *thatparentp = thatp -> arc_parentp;
594 nltype *thatchildp = thatp -> arc_childp;
599 if ( debug & TIMEDEBUG ) {
600 printf( "[arccmp] " );
601 printname( thisparentp );
603 printname ( thischildp );
604 printf( " %f + %f %d/%d\n" ,
605 thisp -> arc_time , thisp -> arc_childtime ,
606 thisp -> arc_count , thischildp -> ncall );
607 printf( "[arccmp] " );
608 printname( thatparentp );
610 printname( thatchildp );
611 printf( " %f + %f %d/%d\n" ,
612 thatp -> arc_time , thatp -> arc_childtime ,
613 thatp -> arc_count , thatchildp -> ncall );
617 if ( thisparentp == thischildp ) {
618 /* this is a self call */
621 if ( thatparentp == thatchildp ) {
622 /* that is a self call */
625 if ( thisparentp -> cycleno != 0 && thischildp -> cycleno != 0 &&
626 thisparentp -> cycleno == thischildp -> cycleno ) {
627 /* this is a call within a cycle */
628 if ( thatparentp -> cycleno != 0 && thatchildp -> cycleno != 0 &&
629 thatparentp -> cycleno == thatchildp -> cycleno ) {
630 /* that is a call within the cycle, too */
631 if ( thisp -> arc_count < thatp -> arc_count ) {
634 if ( thisp -> arc_count > thatp -> arc_count ) {
639 /* that isn't a call within the cycle */
643 /* this isn't a call within a cycle */
644 if ( thatparentp -> cycleno != 0 && thatchildp -> cycleno != 0 &&
645 thatparentp -> cycleno == thatchildp -> cycleno ) {
646 /* that is a call within a cycle */
649 /* neither is a call within a cycle */
650 thistime = thisp -> arc_time + thisp -> arc_childtime;
651 thattime = thatp -> arc_time + thatp -> arc_childtime;
652 if ( thistime < thattime )
654 if ( thistime > thattime )
656 if ( thisp -> arc_count < thatp -> arc_count )
658 if ( thisp -> arc_count > thatp -> arc_count )
665 printblurb( blurbname )
671 blurbfile = fopen( blurbname , "r" );
672 if ( blurbfile == NULL ) {
676 while ( ( input = getc( blurbfile ) ) != EOF ) {
683 namecmp( npp1 , npp2 )
684 nltype **npp1, **npp2;
686 return( strcmp( (*npp1) -> name , (*npp2) -> name ) );
691 nltype **namesortnlp;
692 register nltype *nlp;
693 int index, nnames, todo, i, j;
694 char peterbuffer[ BUFSIZ ];
697 * Now, sort regular function name alphbetically
698 * to create an index.
700 namesortnlp = (nltype **) calloc( nname + ncycle , sizeof(nltype *) );
701 if ( namesortnlp == (nltype **) 0 ) {
702 warnx("ran out of memory for sorting");
704 for ( index = 0 , nnames = 0 ; index < nname ; index++ ) {
705 if ( zflag == 0 && nl[index].ncall == 0 && nl[index].time == 0 )
707 namesortnlp[nnames++] = &nl[index];
709 qsort( namesortnlp , nnames , sizeof(nltype *) , namecmp );
710 for ( index = 1 , todo = nnames ; index <= ncycle ; index++ ) {
711 namesortnlp[todo++] = &cyclenl[index];
713 printf( "\f\nIndex by function name\n\n" );
714 index = ( todo + 2 ) / 3;
715 for ( i = 0; i < index ; i++ ) {
716 for ( j = i; j < todo ; j += index ) {
717 nlp = namesortnlp[ j ];
718 if ( nlp -> printflag ) {
719 sprintf( peterbuffer , "[%d]" , nlp -> index );
721 sprintf( peterbuffer , "(%d)" , nlp -> index );
724 printf( "%6.6s %-19.19s" , peterbuffer , nlp -> name );
726 printf( "%6.6s " , peterbuffer );
727 sprintf( peterbuffer , "<cycle %d>" , nlp -> cycleno );
728 printf( "%-19.19s" , peterbuffer );