2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 static char sccsid[] = "@(#)printgprof.c 8.1 (Berkeley) 6/6/93";
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
45 #include "pathnames.h"
47 int namecmp(const void *, const void *);
48 int timecmp(const void *, const void *);
61 * Sort the symbol table in by time
63 sortednlp = (nltype **) calloc( nname , sizeof(nltype *) );
64 if ( sortednlp == (nltype **) 0 )
65 errx( 1 , "[printprof] ran out of memory for time sorting" );
66 for ( idx = 0 ; idx < nname ; idx += 1 ) {
67 sortednlp[ idx ] = &nl[ idx ];
69 qsort( sortednlp , nname , sizeof(nltype *) , timecmp );
70 for ( idx = 0 ; idx < nname ; idx += 1 ) {
71 np = sortednlp[ idx ];
79 timecmp(const void *v1, const void *v2)
81 const nltype **npp1 = (const nltype **)v1;
82 const nltype **npp2 = (const nltype **)v2;
86 timediff = (*npp2) -> time - (*npp1) -> time;
91 calldiff = (*npp2) -> ncall - (*npp1) -> ncall;
96 return( strcmp( (*npp1) -> name , (*npp2) -> name ) );
100 * header for flatprofline
107 printblurb( _PATH_FLAT_BLURB );
109 printf( "\ngranularity: each sample hit covers %g byte(s)" ,
110 scale * HISTORICAL_SCALE_2 );
111 if ( totime > 0.0 ) {
112 printf( " for %.2f%% of %.2f seconds\n\n" ,
113 100.0/totime , totime / hz );
115 printf( " no time accumulated\n\n" );
117 * this doesn't hurt since all the numerators will be zero.
121 printf( "%5.5s %10.10s %8.8s %8.8s %8.8s %8.8s %-8.8s\n" ,
122 "% " , "cumulative" , "self " , "" , "self " , "total " , "" );
123 printf( "%5.5s %10.10s %8.8s %8.8s %8.8s %8.8s %-8.8s\n" ,
124 "time" , "seconds " , "seconds" , "calls" ,
125 hz >= 10000000 ? "ns/call" : hz >= 10000 ? "us/call" : "ms/call" ,
126 hz >= 10000000 ? "ns/call" : hz >= 10000 ? "us/call" : "ms/call" ,
131 flatprofline(register nltype *np)
134 if ( zflag == 0 && np -> ncall == 0 && np -> time == 0 &&
135 np -> childtime == 0 ) {
138 actime += np -> time;
140 printf( "%5.1f %10.3f %8.3f" ,
141 100 * np -> time / totime , actime / hz , np -> time / hz );
143 printf( "%5.1f %10.2f %8.2f" ,
144 100 * np -> time / totime , actime / hz , np -> time / hz );
145 if ( np -> ncall != 0 ) {
147 printf( " %8ld %8.0f %8.0f " , np -> ncall ,
148 1e9 * np -> time / hz / np -> ncall ,
149 1e9 * ( np -> time + np -> childtime ) / hz / np -> ncall );
150 else if (hz >= 10000)
151 printf( " %8ld %8.0f %8.0f " , np -> ncall ,
152 1e6 * np -> time / hz / np -> ncall ,
153 1e6 * ( np -> time + np -> childtime ) / hz / np -> ncall );
155 printf( " %8ld %8.2f %8.2f " , np -> ncall ,
156 1000 * np -> time / hz / np -> ncall ,
157 1000 * ( np -> time + np -> childtime ) / hz / np -> ncall );
158 } else if ( np -> time != 0 || np -> childtime != 0 ) {
159 printf( " %8ld %7.2f%% %8.8s " , np -> ncall ,
160 100 * np -> time / ( np -> time + np -> childtime ) , "" );
162 printf( " %8.8s %8.8s %8.8s " , "" , "" , "" );
173 printblurb( _PATH_CALLG_BLURB );
175 printf( "\ngranularity: each sample hit covers %g byte(s)" ,
176 scale * HISTORICAL_SCALE_2 );
177 if ( printtime > 0.0 ) {
178 printf( " for %.2f%% of %.2f seconds\n\n" ,
179 100.0/printtime , printtime / hz );
181 printf( " no time propagated\n\n" );
183 * this doesn't hurt, since all the numerators will be 0.0
187 printf( "%6.6s %5.5s %7.7s %11.11s %7.7s/%-7.7s %-8.8s\n" ,
188 "" , "" , "" , "" , "called" , "total" , "parents");
189 printf( "%-6.6s %5.5s %7.7s %11.11s %7.7s+%-7.7s %-8.8s\t%5.5s\n" ,
190 "index" , "%time" , "self" , "descendents" ,
191 "called" , "self" , "name" , "index" );
192 printf( "%6.6s %5.5s %7.7s %11.11s %7.7s/%-7.7s %-8.8s\n" ,
193 "" , "" , "" , "" , "called" , "total" , "children");
198 gprofline(register nltype *np)
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(nltype **timesortnlp)
229 * Print out the structured profiling list
232 for ( idx = 0 ; idx < nname + ncycle ; idx ++ ) {
233 parentp = timesortnlp[ idx ];
235 parentp -> ncall == 0 &&
236 parentp -> selfcalls == 0 &&
237 parentp -> propself == 0 &&
238 parentp -> propchild == 0 ) {
241 if ( ! parentp -> printflag ) {
244 if ( parentp -> name == 0 && parentp -> cycleno != 0 ) {
248 printcycle( parentp );
249 printmembers( parentp );
251 printparents( parentp );
252 gprofline( parentp );
253 printchildren( parentp );
256 printf( "-----------------------------------------------\n" );
263 * sort by decreasing propagated time
264 * if times are equal, but one is a cycle header,
265 * say that's first (e.g. less, i.e. -1).
266 * if one's name doesn't have an underscore and the other does,
267 * say the one is first.
268 * all else being equal, sort by names.
271 totalcmp(const void *v1, const void *v2)
273 const nltype **npp1 = (const nltype **)v1;
274 const nltype **npp2 = (const nltype **)v2;
275 register const nltype *np1 = *npp1;
276 register const nltype *np2 = *npp2;
279 diff = ( np1 -> propself + np1 -> propchild )
280 - ( np2 -> propself + np2 -> propchild );
285 if ( np1 -> name == 0 && np1 -> cycleno != 0 )
287 if ( np2 -> name == 0 && np2 -> cycleno != 0 )
289 if ( np1 -> name == 0 )
291 if ( np2 -> name == 0 )
293 if ( *(np1 -> name) != '_' && *(np2 -> name) == '_' )
295 if ( *(np1 -> name) == '_' && *(np2 -> name) != '_' )
297 if ( np1 -> ncall > np2 -> ncall )
299 if ( np1 -> ncall < np2 -> ncall )
301 return strcmp( np1 -> name , np2 -> name );
305 printparents(nltype *childp)
311 if ( childp -> cyclehead != 0 ) {
312 cycleheadp = childp -> cyclehead;
316 if ( childp -> parents == 0 ) {
317 printf( "%6.6s %5.5s %7.7s %11.11s %7.7s %7.7s <spontaneous>\n" ,
318 "" , "" , "" , "" , "" , "" );
321 sortparents( childp );
322 for ( arcp = childp -> parents ; arcp ; arcp = arcp -> arc_parentlist ) {
323 parentp = arcp -> arc_parentp;
324 if ( childp == parentp || ( arcp -> arc_flags & DEADARC ) ||
325 ( childp->cycleno != 0 && parentp->cycleno == childp->cycleno ) ) {
327 * selfcall or call among siblings
329 printf( "%6.6s %5.5s %7.7s %11.11s %7ld %7.7s " ,
331 arcp -> arc_count , "" );
332 printname( parentp );
336 * regular parent of child
338 printf( "%6.6s %5.5s %7.2f %11.2f %7ld/%-7ld " ,
340 arcp -> arc_time / hz , arcp -> arc_childtime / hz ,
341 arcp -> arc_count , cycleheadp -> npropcall );
342 printname( parentp );
349 printchildren(nltype *parentp)
354 sortchildren( parentp );
355 arcp = parentp -> children;
356 for ( arcp = parentp -> children ; arcp ; arcp = arcp -> arc_childlist ) {
357 childp = arcp -> arc_childp;
358 if ( childp == parentp || ( arcp -> arc_flags & DEADARC ) ||
359 ( childp->cycleno != 0 && childp->cycleno == parentp->cycleno ) ) {
361 * self call or call to sibling
363 printf( "%6.6s %5.5s %7.7s %11.11s %7ld %7.7s " ,
364 "" , "" , "" , "" , arcp -> arc_count , "" );
369 * regular child of parent
371 printf( "%6.6s %5.5s %7.2f %11.2f %7ld/%-7ld " ,
373 arcp -> arc_time / hz , arcp -> arc_childtime / hz ,
374 arcp -> arc_count , childp -> cyclehead -> npropcall );
382 printname(nltype *selfp)
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(nltype *parentp)
417 * unlink children from parent,
418 * then insertion sort back on to sorted's children.
419 * *arcp the arc you have detached and are inserting.
420 * *detachedp the rest of the arcs to be sorted.
421 * sorted arc list onto which you insertion sort.
422 * *prevp arc before the arc you are comparing.
424 sorted.arc_childlist = 0;
425 for ( (arcp = parentp -> children)&&(detachedp = arcp -> arc_childlist);
427 (arcp = detachedp)&&(detachedp = detachedp -> arc_childlist)) {
429 * consider *arcp as disconnected
430 * insert it into sorted
432 for ( prevp = &sorted ;
433 prevp -> arc_childlist ;
434 prevp = prevp -> arc_childlist ) {
435 if ( arccmp( arcp , prevp -> arc_childlist ) != LESSTHAN ) {
439 arcp -> arc_childlist = prevp -> arc_childlist;
440 prevp -> arc_childlist = arcp;
443 * reattach sorted children to parent
445 parentp -> children = sorted.arc_childlist;
449 sortparents(nltype *childp)
457 * unlink parents from child,
458 * then insertion sort back on to sorted's parents.
459 * *arcp the arc you have detached and are inserting.
460 * *detachedp the rest of the arcs to be sorted.
461 * sorted arc list onto which you insertion sort.
462 * *prevp arc before the arc you are comparing.
464 sorted.arc_parentlist = 0;
465 for ( (arcp = childp -> parents)&&(detachedp = arcp -> arc_parentlist);
467 (arcp = detachedp)&&(detachedp = detachedp -> arc_parentlist)) {
469 * consider *arcp as disconnected
470 * insert it into sorted
472 for ( prevp = &sorted ;
473 prevp -> arc_parentlist ;
474 prevp = prevp -> arc_parentlist ) {
475 if ( arccmp( arcp , prevp -> arc_parentlist ) != GREATERTHAN ) {
479 arcp -> arc_parentlist = prevp -> arc_parentlist;
480 prevp -> arc_parentlist = arcp;
483 * reattach sorted arcs to child
485 childp -> parents = sorted.arc_parentlist;
489 * print a cycle header
492 printcycle(nltype *cyclep)
494 char kirkbuffer[ BUFSIZ ];
496 sprintf( kirkbuffer , "[%d]" , cyclep -> index );
497 printf( "%-6.6s %5.1f %7.2f %11.2f %7ld" ,
499 100 * ( cyclep -> propself + cyclep -> propchild ) / printtime ,
500 cyclep -> propself / hz ,
501 cyclep -> propchild / hz ,
502 cyclep -> npropcall );
503 if ( cyclep -> selfcalls != 0 ) {
504 printf( "+%-7ld" , cyclep -> selfcalls );
506 printf( " %7.7s" , "" );
508 printf( " <cycle %d as a whole>\t[%d]\n" ,
509 cyclep -> cycleno , cyclep -> index );
513 * print the members of a cycle
516 printmembers(nltype *cyclep)
520 sortmembers( cyclep );
521 for ( memberp = cyclep -> cnext ; memberp ; memberp = memberp -> cnext ) {
522 printf( "%6.6s %5.5s %7.2f %11.2f %7ld" ,
523 "" , "" , memberp -> propself / hz , memberp -> propchild / hz ,
524 memberp -> npropcall );
525 if ( memberp -> selfcalls != 0 ) {
526 printf( "+%-7ld" , memberp -> selfcalls );
528 printf( " %7.7s" , "" );
531 printname( memberp );
537 * sort members of a cycle
540 sortmembers(nltype *cyclep)
547 * detach cycle members from cyclehead,
548 * and insertion sort them back on.
550 todo = cyclep -> cnext;
552 for ( (doing = todo)&&(todo = doing -> cnext);
554 (doing = todo )&&(todo = doing -> cnext )){
555 for ( prev = cyclep ; prev -> cnext ; prev = prev -> cnext ) {
556 if ( membercmp( doing , prev -> cnext ) == GREATERTHAN ) {
560 doing -> cnext = prev -> cnext;
561 prev -> cnext = doing;
566 * major sort is on propself + propchild,
567 * next is sort on ncalls + selfcalls.
570 membercmp(nltype *this, nltype *that)
572 double thistime = this -> propself + this -> propchild;
573 double thattime = that -> propself + that -> propchild;
574 long thiscalls = this -> ncall + this -> selfcalls;
575 long thatcalls = that -> ncall + that -> selfcalls;
577 if ( thistime > thattime ) {
580 if ( thistime < thattime ) {
583 if ( thiscalls > thatcalls ) {
586 if ( thiscalls < thatcalls ) {
592 * compare two arcs to/from the same child/parent.
593 * - if one arc is a self arc, it's least.
594 * - if one arc is within a cycle, it's less than.
595 * - if both arcs are within a cycle, compare arc counts.
596 * - if neither arc is within a cycle, compare with
597 * arc_time + arc_childtime as major key
598 * arc count as minor key
601 arccmp(arctype *thisp, arctype *thatp)
603 nltype *thisparentp = thisp -> arc_parentp;
604 nltype *thischildp = thisp -> arc_childp;
605 nltype *thatparentp = thatp -> arc_parentp;
606 nltype *thatchildp = thatp -> arc_childp;
611 if ( debug & TIMEDEBUG ) {
612 printf( "[arccmp] " );
613 printname( thisparentp );
615 printname ( thischildp );
616 printf( " %f + %f %ld/%ld\n" ,
617 thisp -> arc_time , thisp -> arc_childtime ,
618 thisp -> arc_count , thischildp -> ncall );
619 printf( "[arccmp] " );
620 printname( thatparentp );
622 printname( thatchildp );
623 printf( " %f + %f %ld/%ld\n" ,
624 thatp -> arc_time , thatp -> arc_childtime ,
625 thatp -> arc_count , thatchildp -> ncall );
629 if ( thisparentp == thischildp ) {
630 /* this is a self call */
633 if ( thatparentp == thatchildp ) {
634 /* that is a self call */
637 if ( thisparentp -> cycleno != 0 && thischildp -> cycleno != 0 &&
638 thisparentp -> cycleno == thischildp -> cycleno ) {
639 /* this is a call within a cycle */
640 if ( thatparentp -> cycleno != 0 && thatchildp -> cycleno != 0 &&
641 thatparentp -> cycleno == thatchildp -> cycleno ) {
642 /* that is a call within the cycle, too */
643 if ( thisp -> arc_count < thatp -> arc_count ) {
646 if ( thisp -> arc_count > thatp -> arc_count ) {
651 /* that isn't a call within the cycle */
655 /* this isn't a call within a cycle */
656 if ( thatparentp -> cycleno != 0 && thatchildp -> cycleno != 0 &&
657 thatparentp -> cycleno == thatchildp -> cycleno ) {
658 /* that is a call within a cycle */
661 /* neither is a call within a cycle */
662 thistime = thisp -> arc_time + thisp -> arc_childtime;
663 thattime = thatp -> arc_time + thatp -> arc_childtime;
664 if ( thistime < thattime )
666 if ( thistime > thattime )
668 if ( thisp -> arc_count < thatp -> arc_count )
670 if ( thisp -> arc_count > thatp -> arc_count )
678 printblurb(const char *blurbname)
683 blurbfile = fopen( blurbname , "r" );
684 if ( blurbfile == NULL ) {
685 warn( "%s" , blurbname );
688 while ( ( input = getc( blurbfile ) ) != EOF ) {
695 namecmp(const void *v1, const void *v2)
697 const nltype **npp1 = (const nltype **)v1;
698 const nltype **npp2 = (const nltype **)v2;
700 return( strcmp( (*npp1) -> name , (*npp2) -> name ) );
706 nltype **namesortnlp;
707 register nltype *nlp;
708 int idx, nnames, todo, i, j;
709 char peterbuffer[ BUFSIZ ];
712 * Now, sort regular function name alphabetically
713 * to create an index.
715 namesortnlp = (nltype **) calloc( nname + ncycle , sizeof(nltype *) );
716 if ( namesortnlp == (nltype **) 0 )
717 errx( 1 , "ran out of memory for sorting");
718 for ( idx = 0 , nnames = 0 ; idx < nname ; idx++ ) {
719 if ( zflag == 0 && nl[idx].ncall == 0 && nl[idx].time == 0 )
721 namesortnlp[nnames++] = &nl[idx];
723 qsort( namesortnlp , nnames , sizeof(nltype *) , namecmp );
724 for ( idx = 1 , todo = nnames ; idx <= ncycle ; idx++ ) {
725 namesortnlp[todo++] = &cyclenl[idx];
727 printf( "\f\nIndex by function name\n\n" );
728 idx = ( todo + 2 ) / 3;
729 for ( i = 0; i < idx ; i++ ) {
730 for ( j = i; j < todo ; j += idx ) {
731 nlp = namesortnlp[ j ];
732 if ( nlp -> printflag ) {
733 sprintf( peterbuffer , "[%d]" , nlp -> index );
735 sprintf( peterbuffer , "(%d)" , nlp -> index );
738 printf( "%6.6s %-19.19s" , peterbuffer , nlp -> name );
740 printf( "%6.6s " , peterbuffer );
741 sprintf( peterbuffer , "<cycle %d>" , nlp -> cycleno );
742 printf( "%-19.19s" , peterbuffer );