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
32 #include <sys/cdefs.h>
37 #include "pathnames.h"
39 int namecmp(const void *, const void *);
40 int timecmp(const void *, const void *);
53 * Sort the symbol table in by time
55 sortednlp = (nltype **) calloc( nname , sizeof(nltype *) );
56 if ( sortednlp == (nltype **) 0 )
57 errx( 1 , "[printprof] ran out of memory for time sorting" );
58 for ( idx = 0 ; idx < nname ; idx += 1 ) {
59 sortednlp[ idx ] = &nl[ idx ];
61 qsort( sortednlp , nname , sizeof(nltype *) , timecmp );
62 for ( idx = 0 ; idx < nname ; idx += 1 ) {
63 np = sortednlp[ idx ];
71 timecmp(const void *v1, const void *v2)
73 const nltype **npp1 = (const nltype **)v1;
74 const nltype **npp2 = (const nltype **)v2;
78 timediff = (*npp2) -> time - (*npp1) -> time;
83 calldiff = (*npp2) -> ncall - (*npp1) -> ncall;
88 return( strcmp( (*npp1) -> name , (*npp2) -> name ) );
92 * header for flatprofline
99 printblurb( _PATH_FLAT_BLURB );
101 printf( "\ngranularity: each sample hit covers %g byte(s)" ,
102 scale * HISTORICAL_SCALE_2 );
103 if ( totime > 0.0 ) {
104 printf( " for %.2f%% of %.2f seconds\n\n" ,
105 100.0/totime , totime / hz );
107 printf( " no time accumulated\n\n" );
109 * this doesn't hurt since all the numerators will be zero.
113 printf( "%5.5s %10.10s %8.8s %8.8s %8.8s %8.8s %-8.8s\n" ,
114 "% " , "cumulative" , "self " , "" , "self " , "total " , "" );
115 printf( "%5.5s %10.10s %8.8s %8.8s %8.8s %8.8s %-8.8s\n" ,
116 "time" , "seconds " , "seconds" , "calls" ,
117 hz >= 10000000 ? "ns/call" : hz >= 10000 ? "us/call" : "ms/call" ,
118 hz >= 10000000 ? "ns/call" : hz >= 10000 ? "us/call" : "ms/call" ,
123 flatprofline(register nltype *np)
126 if ( zflag == 0 && np -> ncall == 0 && np -> time == 0 &&
127 np -> childtime == 0 ) {
130 actime += np -> time;
132 printf( "%5.1f %10.3f %8.3f" ,
133 100 * np -> time / totime , actime / hz , np -> time / hz );
135 printf( "%5.1f %10.2f %8.2f" ,
136 100 * np -> time / totime , actime / hz , np -> time / hz );
137 if ( np -> ncall != 0 ) {
139 printf( " %8ld %8.0f %8.0f " , np -> ncall ,
140 1e9 * np -> time / hz / np -> ncall ,
141 1e9 * ( np -> time + np -> childtime ) / hz / np -> ncall );
142 else if (hz >= 10000)
143 printf( " %8ld %8.0f %8.0f " , np -> ncall ,
144 1e6 * np -> time / hz / np -> ncall ,
145 1e6 * ( np -> time + np -> childtime ) / hz / np -> ncall );
147 printf( " %8ld %8.2f %8.2f " , np -> ncall ,
148 1000 * np -> time / hz / np -> ncall ,
149 1000 * ( np -> time + np -> childtime ) / hz / np -> ncall );
150 } else if ( np -> time != 0 || np -> childtime != 0 ) {
151 printf( " %8ld %7.2f%% %8.8s " , np -> ncall ,
152 100 * np -> time / ( np -> time + np -> childtime ) , "" );
154 printf( " %8.8s %8.8s %8.8s " , "" , "" , "" );
165 printblurb( _PATH_CALLG_BLURB );
167 printf( "\ngranularity: each sample hit covers %g byte(s)" ,
168 scale * HISTORICAL_SCALE_2 );
169 if ( printtime > 0.0 ) {
170 printf( " for %.2f%% of %.2f seconds\n\n" ,
171 100.0/printtime , printtime / hz );
173 printf( " no time propagated\n\n" );
175 * this doesn't hurt, since all the numerators will be 0.0
179 printf( "%6.6s %5.5s %7.7s %11.11s %7.7s/%-7.7s %-8.8s\n" ,
180 "" , "" , "" , "" , "called" , "total" , "parents");
181 printf( "%-6.6s %5.5s %7.7s %11.11s %7.7s+%-7.7s %-8.8s\t%5.5s\n" ,
182 "index" , "%time" , "self" , "descendents" ,
183 "called" , "self" , "name" , "index" );
184 printf( "%6.6s %5.5s %7.7s %11.11s %7.7s/%-7.7s %-8.8s\n" ,
185 "" , "" , "" , "" , "called" , "total" , "children");
190 gprofline(register nltype *np)
192 char kirkbuffer[ BUFSIZ ];
194 sprintf( kirkbuffer , "[%d]" , np -> index );
195 printf( "%-6.6s %5.1f %7.2f %11.2f" ,
197 100 * ( np -> propself + np -> propchild ) / printtime ,
198 np -> propself / hz ,
199 np -> propchild / hz );
200 if ( ( np -> ncall + np -> selfcalls ) != 0 ) {
201 printf( " %7ld" , np -> npropcall );
202 if ( np -> selfcalls != 0 ) {
203 printf( "+%-7ld " , np -> selfcalls );
205 printf( " %7.7s " , "" );
208 printf( " %7.7s %7.7s " , "" , "" );
215 printgprof(nltype **timesortnlp)
221 * Print out the structured profiling list
224 for ( idx = 0 ; idx < nname + ncycle ; idx ++ ) {
225 parentp = timesortnlp[ idx ];
227 parentp -> ncall == 0 &&
228 parentp -> selfcalls == 0 &&
229 parentp -> propself == 0 &&
230 parentp -> propchild == 0 ) {
233 if ( ! parentp -> printflag ) {
236 if ( parentp -> name == 0 && parentp -> cycleno != 0 ) {
240 printcycle( parentp );
241 printmembers( parentp );
243 printparents( parentp );
244 gprofline( parentp );
245 printchildren( parentp );
248 printf( "-----------------------------------------------\n" );
255 * sort by decreasing propagated time
256 * if times are equal, but one is a cycle header,
257 * say that's first (e.g. less, i.e. -1).
258 * if one's name doesn't have an underscore and the other does,
259 * say the one is first.
260 * all else being equal, sort by names.
263 totalcmp(const void *v1, const void *v2)
265 const nltype **npp1 = (const nltype **)v1;
266 const nltype **npp2 = (const nltype **)v2;
267 register const nltype *np1 = *npp1;
268 register const nltype *np2 = *npp2;
271 diff = ( np1 -> propself + np1 -> propchild )
272 - ( np2 -> propself + np2 -> propchild );
277 if ( np1 -> name == 0 && np1 -> cycleno != 0 )
279 if ( np2 -> name == 0 && np2 -> cycleno != 0 )
281 if ( np1 -> name == 0 )
283 if ( np2 -> name == 0 )
285 if ( *(np1 -> name) != '_' && *(np2 -> name) == '_' )
287 if ( *(np1 -> name) == '_' && *(np2 -> name) != '_' )
289 if ( np1 -> ncall > np2 -> ncall )
291 if ( np1 -> ncall < np2 -> ncall )
293 return strcmp( np1 -> name , np2 -> name );
297 printparents(nltype *childp)
303 if ( childp -> cyclehead != 0 ) {
304 cycleheadp = childp -> cyclehead;
308 if ( childp -> parents == 0 ) {
309 printf( "%6.6s %5.5s %7.7s %11.11s %7.7s %7.7s <spontaneous>\n" ,
310 "" , "" , "" , "" , "" , "" );
313 sortparents( childp );
314 for ( arcp = childp -> parents ; arcp ; arcp = arcp -> arc_parentlist ) {
315 parentp = arcp -> arc_parentp;
316 if ( childp == parentp || ( arcp -> arc_flags & DEADARC ) ||
317 ( childp->cycleno != 0 && parentp->cycleno == childp->cycleno ) ) {
319 * selfcall or call among siblings
321 printf( "%6.6s %5.5s %7.7s %11.11s %7ld %7.7s " ,
323 arcp -> arc_count , "" );
324 printname( parentp );
328 * regular parent of child
330 printf( "%6.6s %5.5s %7.2f %11.2f %7ld/%-7ld " ,
332 arcp -> arc_time / hz , arcp -> arc_childtime / hz ,
333 arcp -> arc_count , cycleheadp -> npropcall );
334 printname( parentp );
341 printchildren(nltype *parentp)
346 sortchildren( parentp );
347 arcp = parentp -> children;
348 for ( arcp = parentp -> children ; arcp ; arcp = arcp -> arc_childlist ) {
349 childp = arcp -> arc_childp;
350 if ( childp == parentp || ( arcp -> arc_flags & DEADARC ) ||
351 ( childp->cycleno != 0 && childp->cycleno == parentp->cycleno ) ) {
353 * self call or call to sibling
355 printf( "%6.6s %5.5s %7.7s %11.11s %7ld %7.7s " ,
356 "" , "" , "" , "" , arcp -> arc_count , "" );
361 * regular child of parent
363 printf( "%6.6s %5.5s %7.2f %11.2f %7ld/%-7ld " ,
365 arcp -> arc_time / hz , arcp -> arc_childtime / hz ,
366 arcp -> arc_count , childp -> cyclehead -> npropcall );
374 printname(nltype *selfp)
377 if ( selfp -> name != 0 ) {
378 printf( "%s" , selfp -> name );
380 if ( debug & DFNDEBUG ) {
381 printf( "{%d} " , selfp -> toporder );
383 if ( debug & PROPDEBUG ) {
384 printf( "%5.2f%% " , selfp -> propfraction );
388 if ( selfp -> cycleno != 0 ) {
389 printf( " <cycle %d>" , selfp -> cycleno );
391 if ( selfp -> index != 0 ) {
392 if ( selfp -> printflag ) {
393 printf( " [%d]" , selfp -> index );
395 printf( " (%d)" , selfp -> index );
401 sortchildren(nltype *parentp)
409 * unlink children from parent,
410 * then insertion sort back on to sorted's children.
411 * *arcp the arc you have detached and are inserting.
412 * *detachedp the rest of the arcs to be sorted.
413 * sorted arc list onto which you insertion sort.
414 * *prevp arc before the arc you are comparing.
416 sorted.arc_childlist = 0;
417 for ( (arcp = parentp -> children)&&(detachedp = arcp -> arc_childlist);
419 (arcp = detachedp)&&(detachedp = detachedp -> arc_childlist)) {
421 * consider *arcp as disconnected
422 * insert it into sorted
424 for ( prevp = &sorted ;
425 prevp -> arc_childlist ;
426 prevp = prevp -> arc_childlist ) {
427 if ( arccmp( arcp , prevp -> arc_childlist ) != LESSTHAN ) {
431 arcp -> arc_childlist = prevp -> arc_childlist;
432 prevp -> arc_childlist = arcp;
435 * reattach sorted children to parent
437 parentp -> children = sorted.arc_childlist;
441 sortparents(nltype *childp)
449 * unlink parents from child,
450 * then insertion sort back on to sorted's parents.
451 * *arcp the arc you have detached and are inserting.
452 * *detachedp the rest of the arcs to be sorted.
453 * sorted arc list onto which you insertion sort.
454 * *prevp arc before the arc you are comparing.
456 sorted.arc_parentlist = 0;
457 for ( (arcp = childp -> parents)&&(detachedp = arcp -> arc_parentlist);
459 (arcp = detachedp)&&(detachedp = detachedp -> arc_parentlist)) {
461 * consider *arcp as disconnected
462 * insert it into sorted
464 for ( prevp = &sorted ;
465 prevp -> arc_parentlist ;
466 prevp = prevp -> arc_parentlist ) {
467 if ( arccmp( arcp , prevp -> arc_parentlist ) != GREATERTHAN ) {
471 arcp -> arc_parentlist = prevp -> arc_parentlist;
472 prevp -> arc_parentlist = arcp;
475 * reattach sorted arcs to child
477 childp -> parents = sorted.arc_parentlist;
481 * print a cycle header
484 printcycle(nltype *cyclep)
486 char kirkbuffer[ BUFSIZ ];
488 sprintf( kirkbuffer , "[%d]" , cyclep -> index );
489 printf( "%-6.6s %5.1f %7.2f %11.2f %7ld" ,
491 100 * ( cyclep -> propself + cyclep -> propchild ) / printtime ,
492 cyclep -> propself / hz ,
493 cyclep -> propchild / hz ,
494 cyclep -> npropcall );
495 if ( cyclep -> selfcalls != 0 ) {
496 printf( "+%-7ld" , cyclep -> selfcalls );
498 printf( " %7.7s" , "" );
500 printf( " <cycle %d as a whole>\t[%d]\n" ,
501 cyclep -> cycleno , cyclep -> index );
505 * print the members of a cycle
508 printmembers(nltype *cyclep)
512 sortmembers( cyclep );
513 for ( memberp = cyclep -> cnext ; memberp ; memberp = memberp -> cnext ) {
514 printf( "%6.6s %5.5s %7.2f %11.2f %7ld" ,
515 "" , "" , memberp -> propself / hz , memberp -> propchild / hz ,
516 memberp -> npropcall );
517 if ( memberp -> selfcalls != 0 ) {
518 printf( "+%-7ld" , memberp -> selfcalls );
520 printf( " %7.7s" , "" );
523 printname( memberp );
529 * sort members of a cycle
532 sortmembers(nltype *cyclep)
539 * detach cycle members from cyclehead,
540 * and insertion sort them back on.
542 todo = cyclep -> cnext;
544 for ( (doing = todo)&&(todo = doing -> cnext);
546 (doing = todo )&&(todo = doing -> cnext )){
547 for ( prev = cyclep ; prev -> cnext ; prev = prev -> cnext ) {
548 if ( membercmp( doing , prev -> cnext ) == GREATERTHAN ) {
552 doing -> cnext = prev -> cnext;
553 prev -> cnext = doing;
558 * major sort is on propself + propchild,
559 * next is sort on ncalls + selfcalls.
562 membercmp(nltype *this, nltype *that)
564 double thistime = this -> propself + this -> propchild;
565 double thattime = that -> propself + that -> propchild;
566 long thiscalls = this -> ncall + this -> selfcalls;
567 long thatcalls = that -> ncall + that -> selfcalls;
569 if ( thistime > thattime ) {
572 if ( thistime < thattime ) {
575 if ( thiscalls > thatcalls ) {
578 if ( thiscalls < thatcalls ) {
584 * compare two arcs to/from the same child/parent.
585 * - if one arc is a self arc, it's least.
586 * - if one arc is within a cycle, it's less than.
587 * - if both arcs are within a cycle, compare arc counts.
588 * - if neither arc is within a cycle, compare with
589 * arc_time + arc_childtime as major key
590 * arc count as minor key
593 arccmp(arctype *thisp, arctype *thatp)
595 nltype *thisparentp = thisp -> arc_parentp;
596 nltype *thischildp = thisp -> arc_childp;
597 nltype *thatparentp = thatp -> arc_parentp;
598 nltype *thatchildp = thatp -> arc_childp;
603 if ( debug & TIMEDEBUG ) {
604 printf( "[arccmp] " );
605 printname( thisparentp );
607 printname ( thischildp );
608 printf( " %f + %f %ld/%ld\n" ,
609 thisp -> arc_time , thisp -> arc_childtime ,
610 thisp -> arc_count , thischildp -> ncall );
611 printf( "[arccmp] " );
612 printname( thatparentp );
614 printname( thatchildp );
615 printf( " %f + %f %ld/%ld\n" ,
616 thatp -> arc_time , thatp -> arc_childtime ,
617 thatp -> arc_count , thatchildp -> ncall );
621 if ( thisparentp == thischildp ) {
622 /* this is a self call */
625 if ( thatparentp == thatchildp ) {
626 /* that is a self call */
629 if ( thisparentp -> cycleno != 0 && thischildp -> cycleno != 0 &&
630 thisparentp -> cycleno == thischildp -> cycleno ) {
631 /* this is a call within a cycle */
632 if ( thatparentp -> cycleno != 0 && thatchildp -> cycleno != 0 &&
633 thatparentp -> cycleno == thatchildp -> cycleno ) {
634 /* that is a call within the cycle, too */
635 if ( thisp -> arc_count < thatp -> arc_count ) {
638 if ( thisp -> arc_count > thatp -> arc_count ) {
643 /* that isn't a call within the cycle */
647 /* this isn't a call within a cycle */
648 if ( thatparentp -> cycleno != 0 && thatchildp -> cycleno != 0 &&
649 thatparentp -> cycleno == thatchildp -> cycleno ) {
650 /* that is a call within a cycle */
653 /* neither is a call within a cycle */
654 thistime = thisp -> arc_time + thisp -> arc_childtime;
655 thattime = thatp -> arc_time + thatp -> arc_childtime;
656 if ( thistime < thattime )
658 if ( thistime > thattime )
660 if ( thisp -> arc_count < thatp -> arc_count )
662 if ( thisp -> arc_count > thatp -> arc_count )
670 printblurb(const char *blurbname)
675 blurbfile = fopen( blurbname , "r" );
676 if ( blurbfile == NULL ) {
677 warn( "%s" , blurbname );
680 while ( ( input = getc( blurbfile ) ) != EOF ) {
687 namecmp(const void *v1, const void *v2)
689 const nltype **npp1 = (const nltype **)v1;
690 const nltype **npp2 = (const nltype **)v2;
692 return( strcmp( (*npp1) -> name , (*npp2) -> name ) );
698 nltype **namesortnlp;
699 register nltype *nlp;
700 int idx, nnames, todo, i, j;
701 char peterbuffer[ BUFSIZ ];
704 * Now, sort regular function name alphabetically
705 * to create an index.
707 namesortnlp = (nltype **) calloc( nname + ncycle , sizeof(nltype *) );
708 if ( namesortnlp == (nltype **) 0 )
709 errx( 1 , "ran out of memory for sorting");
710 for ( idx = 0 , nnames = 0 ; idx < nname ; idx++ ) {
711 if ( zflag == 0 && nl[idx].ncall == 0 && nl[idx].time == 0 )
713 namesortnlp[nnames++] = &nl[idx];
715 qsort( namesortnlp , nnames , sizeof(nltype *) , namecmp );
716 for ( idx = 1 , todo = nnames ; idx <= ncycle ; idx++ ) {
717 namesortnlp[todo++] = &cyclenl[idx];
719 printf( "\f\nIndex by function name\n\n" );
720 idx = ( todo + 2 ) / 3;
721 for ( i = 0; i < idx ; i++ ) {
722 for ( j = i; j < todo ; j += idx ) {
723 nlp = namesortnlp[ j ];
724 if ( nlp -> printflag ) {
725 sprintf( peterbuffer , "[%d]" , nlp -> index );
727 sprintf( peterbuffer , "(%d)" , nlp -> index );
730 printf( "%6.6s %-19.19s" , peterbuffer , nlp -> name );
732 printf( "%6.6s " , peterbuffer );
733 sprintf( peterbuffer , "<cycle %d>" , nlp -> cycleno );
734 printf( "%-19.19s" , peterbuffer );