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
36 static char sccsid[] = "@(#)printgprof.c 8.1 (Berkeley) 6/6/93";
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
47 #include "pathnames.h"
60 * Sort the symbol table in by time
62 sortednlp = (nltype **) calloc( nname , sizeof(nltype *) );
63 if ( sortednlp == (nltype **) 0 )
64 errx( 1 , "[printprof] ran out of memory for time sorting" );
65 for ( index = 0 ; index < nname ; index += 1 ) {
66 sortednlp[ index ] = &nl[ index ];
68 qsort( sortednlp , nname , sizeof(nltype *) , timecmp );
69 for ( index = 0 ; index < nname ; index += 1 ) {
70 np = sortednlp[ index ];
78 timecmp( npp1 , npp2 )
79 nltype **npp1, **npp2;
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" ,
133 if ( zflag == 0 && np -> ncall == 0 && np -> time == 0 &&
134 np -> childtime == 0 ) {
137 actime += np -> time;
139 printf( "%5.1f %10.3f %8.3f" ,
140 100 * np -> time / totime , actime / hz , np -> time / hz );
142 printf( "%5.1f %10.2f %8.2f" ,
143 100 * np -> time / totime , actime / hz , np -> time / hz );
144 if ( np -> ncall != 0 ) {
146 printf( " %8ld %8.0f %8.0f " , np -> ncall ,
147 1e9 * np -> time / hz / np -> ncall ,
148 1e9 * ( np -> time + np -> childtime ) / hz / np -> ncall );
149 else if (hz >= 10000)
150 printf( " %8ld %8.0f %8.0f " , np -> ncall ,
151 1e6 * np -> time / hz / np -> ncall ,
152 1e6 * ( np -> time + np -> childtime ) / hz / np -> ncall );
154 printf( " %8ld %8.2f %8.2f " , np -> ncall ,
155 1000 * np -> time / hz / np -> ncall ,
156 1000 * ( np -> time + np -> childtime ) / hz / np -> ncall );
157 } else if ( np -> time != 0 || np -> childtime != 0 ) {
158 printf( " %8ld %7.2f%% %8.8s " , np -> ncall ,
159 100 * np -> time / ( np -> time + np -> childtime ) , "" );
161 printf( " %8.8s %8.8s %8.8s " , "" , "" , "" );
172 printblurb( _PATH_CALLG_BLURB );
174 printf( "\ngranularity: each sample hit covers %g byte(s)" ,
175 scale * HISTORICAL_SCALE_2 );
176 if ( printtime > 0.0 ) {
177 printf( " for %.2f%% of %.2f seconds\n\n" ,
178 100.0/printtime , printtime / hz );
180 printf( " no time propagated\n\n" );
182 * this doesn't hurt, since all the numerators will be 0.0
186 printf( "%6.6s %5.5s %7.7s %11.11s %7.7s/%-7.7s %-8.8s\n" ,
187 "" , "" , "" , "" , "called" , "total" , "parents");
188 printf( "%-6.6s %5.5s %7.7s %11.11s %7.7s+%-7.7s %-8.8s\t%5.5s\n" ,
189 "index" , "%time" , "self" , "descendents" ,
190 "called" , "self" , "name" , "index" );
191 printf( "%6.6s %5.5s %7.7s %11.11s %7.7s/%-7.7s %-8.8s\n" ,
192 "" , "" , "" , "" , "called" , "total" , "children");
200 char kirkbuffer[ BUFSIZ ];
202 sprintf( kirkbuffer , "[%d]" , np -> index );
203 printf( "%-6.6s %5.1f %7.2f %11.2f" ,
205 100 * ( np -> propself + np -> propchild ) / printtime ,
206 np -> propself / hz ,
207 np -> propchild / hz );
208 if ( ( np -> ncall + np -> selfcalls ) != 0 ) {
209 printf( " %7ld" , np -> npropcall );
210 if ( np -> selfcalls != 0 ) {
211 printf( "+%-7ld " , np -> selfcalls );
213 printf( " %7.7s " , "" );
216 printf( " %7.7s %7.7s " , "" , "" );
223 printgprof(timesortnlp)
224 nltype **timesortnlp;
230 * Print out the structured profiling list
233 for ( index = 0 ; index < nname + ncycle ; index ++ ) {
234 parentp = timesortnlp[ index ];
236 parentp -> ncall == 0 &&
237 parentp -> selfcalls == 0 &&
238 parentp -> propself == 0 &&
239 parentp -> propchild == 0 ) {
242 if ( ! parentp -> printflag ) {
245 if ( parentp -> name == 0 && parentp -> cycleno != 0 ) {
249 printcycle( parentp );
250 printmembers( parentp );
252 printparents( parentp );
253 gprofline( parentp );
254 printchildren( parentp );
257 printf( "-----------------------------------------------\n" );
264 * sort by decreasing propagated time
265 * if times are equal, but one is a cycle header,
266 * say that's first (e.g. less, i.e. -1).
267 * if one's name doesn't have an underscore and the other does,
268 * say the one is first.
269 * all else being equal, sort by names.
272 totalcmp( npp1 , npp2 )
276 register nltype *np1 = *npp1;
277 register nltype *np2 = *npp2;
280 diff = ( np1 -> propself + np1 -> propchild )
281 - ( np2 -> propself + np2 -> propchild );
286 if ( np1 -> name == 0 && np1 -> cycleno != 0 )
288 if ( np2 -> name == 0 && np2 -> cycleno != 0 )
290 if ( np1 -> name == 0 )
292 if ( np2 -> name == 0 )
294 if ( *(np1 -> name) != '_' && *(np2 -> name) == '_' )
296 if ( *(np1 -> name) == '_' && *(np2 -> name) != '_' )
298 if ( np1 -> ncall > np2 -> ncall )
300 if ( np1 -> ncall < np2 -> ncall )
302 return strcmp( np1 -> name , np2 -> name );
306 printparents( childp )
313 if ( childp -> cyclehead != 0 ) {
314 cycleheadp = childp -> cyclehead;
318 if ( childp -> parents == 0 ) {
319 printf( "%6.6s %5.5s %7.7s %11.11s %7.7s %7.7s <spontaneous>\n" ,
320 "" , "" , "" , "" , "" , "" );
323 sortparents( childp );
324 for ( arcp = childp -> parents ; arcp ; arcp = arcp -> arc_parentlist ) {
325 parentp = arcp -> arc_parentp;
326 if ( childp == parentp || ( arcp -> arc_flags & DEADARC ) ||
327 ( childp->cycleno != 0 && parentp->cycleno == childp->cycleno ) ) {
329 * selfcall or call among siblings
331 printf( "%6.6s %5.5s %7.7s %11.11s %7ld %7.7s " ,
333 arcp -> arc_count , "" );
334 printname( parentp );
338 * regular parent of child
340 printf( "%6.6s %5.5s %7.2f %11.2f %7ld/%-7ld " ,
342 arcp -> arc_time / hz , arcp -> arc_childtime / hz ,
343 arcp -> arc_count , cycleheadp -> npropcall );
344 printname( parentp );
351 printchildren( parentp )
357 sortchildren( parentp );
358 arcp = parentp -> children;
359 for ( arcp = parentp -> children ; arcp ; arcp = arcp -> arc_childlist ) {
360 childp = arcp -> arc_childp;
361 if ( childp == parentp || ( arcp -> arc_flags & DEADARC ) ||
362 ( childp->cycleno != 0 && childp->cycleno == parentp->cycleno ) ) {
364 * self call or call to sibling
366 printf( "%6.6s %5.5s %7.7s %11.11s %7ld %7.7s " ,
367 "" , "" , "" , "" , arcp -> arc_count , "" );
372 * regular child of parent
374 printf( "%6.6s %5.5s %7.2f %11.2f %7ld/%-7ld " ,
376 arcp -> arc_time / hz , arcp -> arc_childtime / hz ,
377 arcp -> arc_count , childp -> cyclehead -> npropcall );
389 if ( selfp -> name != 0 ) {
390 printf( "%s" , selfp -> name );
392 if ( debug & DFNDEBUG ) {
393 printf( "{%d} " , selfp -> toporder );
395 if ( debug & PROPDEBUG ) {
396 printf( "%5.2f%% " , selfp -> propfraction );
400 if ( selfp -> cycleno != 0 ) {
401 printf( " <cycle %d>" , selfp -> cycleno );
403 if ( selfp -> index != 0 ) {
404 if ( selfp -> printflag ) {
405 printf( " [%d]" , selfp -> index );
407 printf( " (%d)" , selfp -> index );
413 sortchildren( parentp )
422 * unlink children from parent,
423 * then insertion sort back on to sorted's children.
424 * *arcp the arc you have detached and are inserting.
425 * *detachedp the rest of the arcs to be sorted.
426 * sorted arc list onto which you insertion sort.
427 * *prevp arc before the arc you are comparing.
429 sorted.arc_childlist = 0;
430 for ( (arcp = parentp -> children)&&(detachedp = arcp -> arc_childlist);
432 (arcp = detachedp)&&(detachedp = detachedp -> arc_childlist)) {
434 * consider *arcp as disconnected
435 * insert it into sorted
437 for ( prevp = &sorted ;
438 prevp -> arc_childlist ;
439 prevp = prevp -> arc_childlist ) {
440 if ( arccmp( arcp , prevp -> arc_childlist ) != LESSTHAN ) {
444 arcp -> arc_childlist = prevp -> arc_childlist;
445 prevp -> arc_childlist = arcp;
448 * reattach sorted children to parent
450 parentp -> children = sorted.arc_childlist;
454 sortparents( childp )
463 * unlink parents from child,
464 * then insertion sort back on to sorted's parents.
465 * *arcp the arc you have detached and are inserting.
466 * *detachedp the rest of the arcs to be sorted.
467 * sorted arc list onto which you insertion sort.
468 * *prevp arc before the arc you are comparing.
470 sorted.arc_parentlist = 0;
471 for ( (arcp = childp -> parents)&&(detachedp = arcp -> arc_parentlist);
473 (arcp = detachedp)&&(detachedp = detachedp -> arc_parentlist)) {
475 * consider *arcp as disconnected
476 * insert it into sorted
478 for ( prevp = &sorted ;
479 prevp -> arc_parentlist ;
480 prevp = prevp -> arc_parentlist ) {
481 if ( arccmp( arcp , prevp -> arc_parentlist ) != GREATERTHAN ) {
485 arcp -> arc_parentlist = prevp -> arc_parentlist;
486 prevp -> arc_parentlist = arcp;
489 * reattach sorted arcs to child
491 childp -> parents = sorted.arc_parentlist;
495 * print a cycle header
501 char kirkbuffer[ BUFSIZ ];
503 sprintf( kirkbuffer , "[%d]" , cyclep -> index );
504 printf( "%-6.6s %5.1f %7.2f %11.2f %7ld" ,
506 100 * ( cyclep -> propself + cyclep -> propchild ) / printtime ,
507 cyclep -> propself / hz ,
508 cyclep -> propchild / hz ,
509 cyclep -> npropcall );
510 if ( cyclep -> selfcalls != 0 ) {
511 printf( "+%-7ld" , cyclep -> selfcalls );
513 printf( " %7.7s" , "" );
515 printf( " <cycle %d as a whole>\t[%d]\n" ,
516 cyclep -> cycleno , cyclep -> index );
520 * print the members of a cycle
523 printmembers( cyclep )
528 sortmembers( cyclep );
529 for ( memberp = cyclep -> cnext ; memberp ; memberp = memberp -> cnext ) {
530 printf( "%6.6s %5.5s %7.2f %11.2f %7ld" ,
531 "" , "" , memberp -> propself / hz , memberp -> propchild / hz ,
532 memberp -> npropcall );
533 if ( memberp -> selfcalls != 0 ) {
534 printf( "+%-7ld" , memberp -> selfcalls );
536 printf( " %7.7s" , "" );
539 printname( memberp );
545 * sort members of a cycle
548 sortmembers( cyclep )
556 * detach cycle members from cyclehead,
557 * and insertion sort them back on.
559 todo = cyclep -> cnext;
561 for ( (doing = todo)&&(todo = doing -> cnext);
563 (doing = todo )&&(todo = doing -> cnext )){
564 for ( prev = cyclep ; prev -> cnext ; prev = prev -> cnext ) {
565 if ( membercmp( doing , prev -> cnext ) == GREATERTHAN ) {
569 doing -> cnext = prev -> cnext;
570 prev -> cnext = doing;
575 * major sort is on propself + propchild,
576 * next is sort on ncalls + selfcalls.
579 membercmp( this , that )
583 double thistime = this -> propself + this -> propchild;
584 double thattime = that -> propself + that -> propchild;
585 long thiscalls = this -> ncall + this -> selfcalls;
586 long thatcalls = that -> ncall + that -> selfcalls;
588 if ( thistime > thattime ) {
591 if ( thistime < thattime ) {
594 if ( thiscalls > thatcalls ) {
597 if ( thiscalls < thatcalls ) {
603 * compare two arcs to/from the same child/parent.
604 * - if one arc is a self arc, it's least.
605 * - if one arc is within a cycle, it's less than.
606 * - if both arcs are within a cycle, compare arc counts.
607 * - if neither arc is within a cycle, compare with
608 * arc_time + arc_childtime as major key
609 * arc count as minor key
612 arccmp( thisp , thatp )
616 nltype *thisparentp = thisp -> arc_parentp;
617 nltype *thischildp = thisp -> arc_childp;
618 nltype *thatparentp = thatp -> arc_parentp;
619 nltype *thatchildp = thatp -> arc_childp;
624 if ( debug & TIMEDEBUG ) {
625 printf( "[arccmp] " );
626 printname( thisparentp );
628 printname ( thischildp );
629 printf( " %f + %f %ld/%ld\n" ,
630 thisp -> arc_time , thisp -> arc_childtime ,
631 thisp -> arc_count , thischildp -> ncall );
632 printf( "[arccmp] " );
633 printname( thatparentp );
635 printname( thatchildp );
636 printf( " %f + %f %ld/%ld\n" ,
637 thatp -> arc_time , thatp -> arc_childtime ,
638 thatp -> arc_count , thatchildp -> ncall );
642 if ( thisparentp == thischildp ) {
643 /* this is a self call */
646 if ( thatparentp == thatchildp ) {
647 /* that is a self call */
650 if ( thisparentp -> cycleno != 0 && thischildp -> cycleno != 0 &&
651 thisparentp -> cycleno == thischildp -> cycleno ) {
652 /* this is a call within a cycle */
653 if ( thatparentp -> cycleno != 0 && thatchildp -> cycleno != 0 &&
654 thatparentp -> cycleno == thatchildp -> cycleno ) {
655 /* that is a call within the cycle, too */
656 if ( thisp -> arc_count < thatp -> arc_count ) {
659 if ( thisp -> arc_count > thatp -> arc_count ) {
664 /* that isn't a call within the cycle */
668 /* this isn't a call within a cycle */
669 if ( thatparentp -> cycleno != 0 && thatchildp -> cycleno != 0 &&
670 thatparentp -> cycleno == thatchildp -> cycleno ) {
671 /* that is a call within a cycle */
674 /* neither is a call within a cycle */
675 thistime = thisp -> arc_time + thisp -> arc_childtime;
676 thattime = thatp -> arc_time + thatp -> arc_childtime;
677 if ( thistime < thattime )
679 if ( thistime > thattime )
681 if ( thisp -> arc_count < thatp -> arc_count )
683 if ( thisp -> arc_count > thatp -> arc_count )
691 printblurb( blurbname )
697 blurbfile = fopen( blurbname , "r" );
698 if ( blurbfile == NULL ) {
699 warn( "%s" , blurbname );
702 while ( ( input = getc( blurbfile ) ) != EOF ) {
709 namecmp( npp1 , npp2 )
710 nltype **npp1, **npp2;
712 return( strcmp( (*npp1) -> name , (*npp2) -> name ) );
718 nltype **namesortnlp;
719 register nltype *nlp;
720 int index, nnames, todo, i, j;
721 char peterbuffer[ BUFSIZ ];
724 * Now, sort regular function name alphabetically
725 * to create an index.
727 namesortnlp = (nltype **) calloc( nname + ncycle , sizeof(nltype *) );
728 if ( namesortnlp == (nltype **) 0 )
729 errx( 1 , "ran out of memory for sorting");
730 for ( index = 0 , nnames = 0 ; index < nname ; index++ ) {
731 if ( zflag == 0 && nl[index].ncall == 0 && nl[index].time == 0 )
733 namesortnlp[nnames++] = &nl[index];
735 qsort( namesortnlp , nnames , sizeof(nltype *) , namecmp );
736 for ( index = 1 , todo = nnames ; index <= ncycle ; index++ ) {
737 namesortnlp[todo++] = &cyclenl[index];
739 printf( "\f\nIndex by function name\n\n" );
740 index = ( todo + 2 ) / 3;
741 for ( i = 0; i < index ; i++ ) {
742 for ( j = i; j < todo ; j += index ) {
743 nlp = namesortnlp[ j ];
744 if ( nlp -> printflag ) {
745 sprintf( peterbuffer , "[%d]" , nlp -> index );
747 sprintf( peterbuffer , "(%d)" , nlp -> index );
750 printf( "%6.6s %-19.19s" , peterbuffer , nlp -> name );
752 printf( "%6.6s " , peterbuffer );
753 sprintf( peterbuffer , "<cycle %d>" , nlp -> cycleno );
754 printf( "%-19.19s" , peterbuffer );