2 * Copyright (c) 1983 Regents of the University of California.
5 * Redistribution and use in source and binary forms are permitted
6 * provided that: (1) source distributions retain this entire copyright
7 * notice and comment, and (2) distributions including binaries display
8 * the following acknowledgement: ``This product includes software
9 * developed by the University of California, Berkeley and its contributors''
10 * in the documentation or other materials provided with the distribution
11 * and in all advertising materials mentioning features or use of this
12 * software. Neither the name of the University nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
21 static char sccsid
[] = "@(#)printgprof.c 5.7 (Berkeley) 6/1/90";
25 #include "pathnames.h"
37 * Sort the symbol table in by time
39 sortednlp
= (nltype
**) calloc( nname
, sizeof(nltype
*) );
40 if ( sortednlp
== (nltype
**) 0 ) {
41 fprintf( stderr
, "[printprof] ran out of memory for time sorting\n" );
43 for ( index
= 0 ; index
< nname
; index
+= 1 ) {
44 sortednlp
[ index
] = &nl
[ index
];
46 qsort( sortednlp
, nname
, sizeof(nltype
*) , timecmp
);
47 for ( index
= 0 ; index
< nname
; index
+= 1 ) {
48 np
= sortednlp
[ index
];
55 timecmp( npp1
, npp2
)
56 nltype
**npp1
, **npp2
;
61 timediff
= (*npp2
) -> time
- (*npp1
) -> time
;
66 calldiff
= (*npp2
) -> ncall
- (*npp1
) -> ncall
;
71 return( strcmp( (*npp1
) -> name
, (*npp2
) -> name
) );
75 * header for flatprofline
81 printblurb( _PATH_FLAT_BLURB
);
83 printf( "\ngranularity: each sample hit covers %d byte(s)" ,
84 (long) scale
* sizeof(UNIT
) );
86 printf( " for %.2f%% of %.2f seconds\n\n" ,
87 100.0/totime
, totime
/ hz
);
89 printf( " no time accumulated\n\n" );
91 * this doesn't hurt sinc eall the numerators will be zero.
95 printf( "%5.5s %10.10s %8.8s %8.8s %8.8s %8.8s %-8.8s\n" ,
96 "% " , "cumulative" , "self " , "" , "self " , "total " , "" );
97 printf( "%5.5s %10.10s %8.8s %8.8s %8.8s %8.8s %-8.8s\n" ,
98 "time" , "seconds " , "seconds" , "calls" ,
99 "ms/call" , "ms/call" , "name" );
106 if ( zflag
== 0 && np
-> ncall
== 0 && np
-> time
== 0 ) {
109 actime
+= np
-> time
;
110 printf( "%5.1f %10.2f %8.2f" ,
111 100 * np
-> time
/ totime
, actime
/ hz
, np
-> time
/ hz
);
112 if ( np
-> ncall
!= 0 ) {
113 printf( " %8d %8.2f %8.2f " , np
-> ncall
,
114 1000 * np
-> time
/ hz
/ np
-> ncall
,
115 1000 * ( np
-> time
+ np
-> childtime
) / hz
/ np
-> ncall
);
117 printf( " %8.8s %8.8s %8.8s " , "" , "" , "" );
127 printblurb( _PATH_CALLG_BLURB
);
129 printf( "\ngranularity: each sample hit covers %d byte(s)" ,
130 (long) scale
* sizeof(UNIT
) );
131 if ( printtime
> 0.0 ) {
132 printf( " for %.2f%% of %.2f seconds\n\n" ,
133 100.0/printtime
, printtime
/ hz
);
135 printf( " no time propagated\n\n" );
137 * this doesn't hurt, since all the numerators will be 0.0
141 printf( "%6.6s %5.5s %7.7s %11.11s %7.7s/%-7.7s %-8.8s\n" ,
142 "" , "" , "" , "" , "called" , "total" , "parents");
143 printf( "%-6.6s %5.5s %7.7s %11.11s %7.7s+%-7.7s %-8.8s\t%5.5s\n" ,
144 "index" , "%time" , "self" , "descendents" ,
145 "called" , "self" , "name" , "index" );
146 printf( "%6.6s %5.5s %7.7s %11.11s %7.7s/%-7.7s %-8.8s\n" ,
147 "" , "" , "" , "" , "called" , "total" , "children");
154 char kirkbuffer
[ BUFSIZ
];
156 sprintf( kirkbuffer
, "[%d]" , np
-> index
);
157 printf( "%-6.6s %5.1f %7.2f %11.2f" ,
159 100 * ( np
-> propself
+ np
-> propchild
) / printtime
,
160 np
-> propself
/ hz
,
161 np
-> propchild
/ hz
);
162 if ( ( np
-> ncall
+ np
-> selfcalls
) != 0 ) {
163 printf( " %7d" , np
-> ncall
);
164 if ( np
-> selfcalls
!= 0 ) {
165 printf( "+%-7d " , np
-> selfcalls
);
167 printf( " %7.7s " , "" );
170 printf( " %7.7s %7.7s " , "" , "" );
176 printgprof(timesortnlp
)
177 nltype
**timesortnlp
;
183 * Print out the structured profiling list
186 for ( index
= 0 ; index
< nname
+ ncycle
; index
++ ) {
187 parentp
= timesortnlp
[ index
];
189 parentp
-> ncall
== 0 &&
190 parentp
-> selfcalls
== 0 &&
191 parentp
-> propself
== 0 &&
192 parentp
-> propchild
== 0 ) {
195 if ( ! parentp
-> printflag
) {
198 if ( parentp
-> name
== 0 && parentp
-> cycleno
!= 0 ) {
202 printcycle( parentp
);
203 printmembers( parentp
);
205 printparents( parentp
);
206 gprofline( parentp
);
207 printchildren( parentp
);
210 printf( "-----------------------------------------------\n" );
213 cfree( timesortnlp
);
217 * sort by decreasing propagated time
218 * if times are equal, but one is a cycle header,
219 * say that's first (e.g. less, i.e. -1).
220 * if one's name doesn't have an underscore and the other does,
221 * say the one is first.
222 * all else being equal, sort by names.
225 totalcmp( npp1
, npp2
)
229 register nltype
*np1
= *npp1
;
230 register nltype
*np2
= *npp2
;
233 diff
= ( np1
-> propself
+ np1
-> propchild
)
234 - ( np2
-> propself
+ np2
-> propchild
);
239 if ( np1
-> name
== 0 && np1
-> cycleno
!= 0 )
241 if ( np2
-> name
== 0 && np2
-> cycleno
!= 0 )
243 if ( np1
-> name
== 0 )
245 if ( np2
-> name
== 0 )
247 if ( *(np1
-> name
) != '_' && *(np2
-> name
) == '_' )
249 if ( *(np1
-> name
) == '_' && *(np2
-> name
) != '_' )
251 if ( np1
-> ncall
> np2
-> ncall
)
253 if ( np1
-> ncall
< np2
-> ncall
)
255 return strcmp( np1
-> name
, np2
-> name
);
258 printparents( childp
)
265 if ( childp
-> cyclehead
!= 0 ) {
266 cycleheadp
= childp
-> cyclehead
;
270 if ( childp
-> parents
== 0 ) {
271 printf( "%6.6s %5.5s %7.7s %11.11s %7.7s %7.7s <spontaneous>\n" ,
272 "" , "" , "" , "" , "" , "" );
275 sortparents( childp
);
276 for ( arcp
= childp
-> parents
; arcp
; arcp
= arcp
-> arc_parentlist
) {
277 parentp
= arcp
-> arc_parentp
;
278 if ( childp
== parentp
||
279 ( childp
->cycleno
!= 0 && parentp
->cycleno
== childp
->cycleno
) ) {
281 * selfcall or call among siblings
283 printf( "%6.6s %5.5s %7.7s %11.11s %7d %7.7s " ,
285 arcp
-> arc_count
, "" );
286 printname( parentp
);
290 * regular parent of child
292 printf( "%6.6s %5.5s %7.2f %11.2f %7d/%-7d " ,
294 arcp
-> arc_time
/ hz
, arcp
-> arc_childtime
/ hz
,
295 arcp
-> arc_count
, cycleheadp
-> ncall
);
296 printname( parentp
);
302 printchildren( parentp
)
308 sortchildren( parentp
);
309 arcp
= parentp
-> children
;
310 for ( arcp
= parentp
-> children
; arcp
; arcp
= arcp
-> arc_childlist
) {
311 childp
= arcp
-> arc_childp
;
312 if ( childp
== parentp
||
313 ( childp
->cycleno
!= 0 && childp
->cycleno
== parentp
->cycleno
) ) {
315 * self call or call to sibling
317 printf( "%6.6s %5.5s %7.7s %11.11s %7d %7.7s " ,
318 "" , "" , "" , "" , arcp
-> arc_count
, "" );
323 * regular child of parent
325 printf( "%6.6s %5.5s %7.2f %11.2f %7d/%-7d " ,
327 arcp
-> arc_time
/ hz
, arcp
-> arc_childtime
/ hz
,
328 arcp
-> arc_count
, childp
-> cyclehead
-> ncall
);
339 if ( selfp
-> name
!= 0 ) {
340 printf( "%s" , selfp
-> name
);
342 if ( debug
& DFNDEBUG
) {
343 printf( "{%d} " , selfp
-> toporder
);
345 if ( debug
& PROPDEBUG
) {
346 printf( "%5.2f%% " , selfp
-> propfraction
);
350 if ( selfp
-> cycleno
!= 0 ) {
351 printf( " <cycle %d>" , selfp
-> cycleno
);
353 if ( selfp
-> index
!= 0 ) {
354 if ( selfp
-> printflag
) {
355 printf( " [%d]" , selfp
-> index
);
357 printf( " (%d)" , selfp
-> index
);
362 sortchildren( parentp
)
371 * unlink children from parent,
372 * then insertion sort back on to sorted's children.
373 * *arcp the arc you have detached and are inserting.
374 * *detachedp the rest of the arcs to be sorted.
375 * sorted arc list onto which you insertion sort.
376 * *prevp arc before the arc you are comparing.
378 sorted
.arc_childlist
= 0;
379 for ( (arcp
= parentp
-> children
)&&(detachedp
= arcp
-> arc_childlist
);
381 (arcp
= detachedp
)&&(detachedp
= detachedp
-> arc_childlist
)) {
383 * consider *arcp as disconnected
384 * insert it into sorted
386 for ( prevp
= &sorted
;
387 prevp
-> arc_childlist
;
388 prevp
= prevp
-> arc_childlist
) {
389 if ( arccmp( arcp
, prevp
-> arc_childlist
) != LESSTHAN
) {
393 arcp
-> arc_childlist
= prevp
-> arc_childlist
;
394 prevp
-> arc_childlist
= arcp
;
397 * reattach sorted children to parent
399 parentp
-> children
= sorted
.arc_childlist
;
402 sortparents( childp
)
411 * unlink parents from child,
412 * then insertion sort back on to sorted's parents.
413 * *arcp the arc you have detached and are inserting.
414 * *detachedp the rest of the arcs to be sorted.
415 * sorted arc list onto which you insertion sort.
416 * *prevp arc before the arc you are comparing.
418 sorted
.arc_parentlist
= 0;
419 for ( (arcp
= childp
-> parents
)&&(detachedp
= arcp
-> arc_parentlist
);
421 (arcp
= detachedp
)&&(detachedp
= detachedp
-> arc_parentlist
)) {
423 * consider *arcp as disconnected
424 * insert it into sorted
426 for ( prevp
= &sorted
;
427 prevp
-> arc_parentlist
;
428 prevp
= prevp
-> arc_parentlist
) {
429 if ( arccmp( arcp
, prevp
-> arc_parentlist
) != GREATERTHAN
) {
433 arcp
-> arc_parentlist
= prevp
-> arc_parentlist
;
434 prevp
-> arc_parentlist
= arcp
;
437 * reattach sorted arcs to child
439 childp
-> parents
= sorted
.arc_parentlist
;
443 * print a cycle header
448 char kirkbuffer
[ BUFSIZ
];
450 sprintf( kirkbuffer
, "[%d]" , cyclep
-> index
);
451 printf( "%-6.6s %5.1f %7.2f %11.2f %7d" ,
453 100 * ( cyclep
-> propself
+ cyclep
-> propchild
) / printtime
,
454 cyclep
-> propself
/ hz
,
455 cyclep
-> propchild
/ hz
,
457 if ( cyclep
-> selfcalls
!= 0 ) {
458 printf( "+%-7d" , cyclep
-> selfcalls
);
460 printf( " %7.7s" , "" );
462 printf( " <cycle %d as a whole>\t[%d]\n" ,
463 cyclep
-> cycleno
, cyclep
-> index
);
467 * print the members of a cycle
469 printmembers( cyclep
)
474 sortmembers( cyclep
);
475 for ( memberp
= cyclep
-> cnext
; memberp
; memberp
= memberp
-> cnext
) {
476 printf( "%6.6s %5.5s %7.2f %11.2f %7d" ,
477 "" , "" , memberp
-> propself
/ hz
, memberp
-> propchild
/ hz
,
479 if ( memberp
-> selfcalls
!= 0 ) {
480 printf( "+%-7d" , memberp
-> selfcalls
);
482 printf( " %7.7s" , "" );
485 printname( memberp
);
491 * sort members of a cycle
493 sortmembers( cyclep
)
501 * detach cycle members from cyclehead,
502 * and insertion sort them back on.
504 todo
= cyclep
-> cnext
;
506 for ( (doing
= todo
)&&(todo
= doing
-> cnext
);
508 (doing
= todo
)&&(todo
= doing
-> cnext
)){
509 for ( prev
= cyclep
; prev
-> cnext
; prev
= prev
-> cnext
) {
510 if ( membercmp( doing
, prev
-> cnext
) == GREATERTHAN
) {
514 doing
-> cnext
= prev
-> cnext
;
515 prev
-> cnext
= doing
;
520 * major sort is on propself + propchild,
521 * next is sort on ncalls + selfcalls.
524 membercmp( this , that
)
528 double thistime
= this -> propself
+ this -> propchild
;
529 double thattime
= that
-> propself
+ that
-> propchild
;
530 long thiscalls
= this -> ncall
+ this -> selfcalls
;
531 long thatcalls
= that
-> ncall
+ that
-> selfcalls
;
533 if ( thistime
> thattime
) {
536 if ( thistime
< thattime
) {
539 if ( thiscalls
> thatcalls
) {
542 if ( thiscalls
< thatcalls
) {
548 * compare two arcs to/from the same child/parent.
549 * - if one arc is a self arc, it's least.
550 * - if one arc is within a cycle, it's less than.
551 * - if both arcs are within a cycle, compare arc counts.
552 * - if neither arc is within a cycle, compare with
553 * arc_time + arc_childtime as major key
554 * arc count as minor key
557 arccmp( thisp
, thatp
)
561 nltype
*thisparentp
= thisp
-> arc_parentp
;
562 nltype
*thischildp
= thisp
-> arc_childp
;
563 nltype
*thatparentp
= thatp
-> arc_parentp
;
564 nltype
*thatchildp
= thatp
-> arc_childp
;
569 if ( debug
& TIMEDEBUG
) {
570 printf( "[arccmp] " );
571 printname( thisparentp
);
573 printname ( thischildp
);
574 printf( " %f + %f %d/%d\n" ,
575 thisp
-> arc_time
, thisp
-> arc_childtime
,
576 thisp
-> arc_count
, thischildp
-> ncall
);
577 printf( "[arccmp] " );
578 printname( thatparentp
);
580 printname( thatchildp
);
581 printf( " %f + %f %d/%d\n" ,
582 thatp
-> arc_time
, thatp
-> arc_childtime
,
583 thatp
-> arc_count
, thatchildp
-> ncall
);
587 if ( thisparentp
== thischildp
) {
588 /* this is a self call */
591 if ( thatparentp
== thatchildp
) {
592 /* that is a self call */
595 if ( thisparentp
-> cycleno
!= 0 && thischildp
-> cycleno
!= 0 &&
596 thisparentp
-> cycleno
== thischildp
-> cycleno
) {
597 /* this is a call within a cycle */
598 if ( thatparentp
-> cycleno
!= 0 && thatchildp
-> cycleno
!= 0 &&
599 thatparentp
-> cycleno
== thatchildp
-> cycleno
) {
600 /* that is a call within the cycle, too */
601 if ( thisp
-> arc_count
< thatp
-> arc_count
) {
604 if ( thisp
-> arc_count
> thatp
-> arc_count
) {
609 /* that isn't a call within the cycle */
613 /* this isn't a call within a cycle */
614 if ( thatparentp
-> cycleno
!= 0 && thatchildp
-> cycleno
!= 0 &&
615 thatparentp
-> cycleno
== thatchildp
-> cycleno
) {
616 /* that is a call within a cycle */
619 /* neither is a call within a cycle */
620 thistime
= thisp
-> arc_time
+ thisp
-> arc_childtime
;
621 thattime
= thatp
-> arc_time
+ thatp
-> arc_childtime
;
622 if ( thistime
< thattime
)
624 if ( thistime
> thattime
)
626 if ( thisp
-> arc_count
< thatp
-> arc_count
)
628 if ( thisp
-> arc_count
> thatp
-> arc_count
)
635 printblurb( blurbname
)
641 blurbfile
= fopen( blurbname
, "r" );
642 if ( blurbfile
== NULL
) {
646 while ( ( input
= getc( blurbfile
) ) != EOF
) {
653 namecmp( npp1
, npp2
)
654 nltype
**npp1
, **npp2
;
656 return( strcmp( (*npp1
) -> name
, (*npp2
) -> name
) );
661 nltype
**namesortnlp
;
662 register nltype
*nlp
;
663 int index
, nnames
, todo
, i
, j
;
664 char peterbuffer
[ BUFSIZ
];
667 * Now, sort regular function name alphbetically
668 * to create an index.
670 namesortnlp
= (nltype
**) calloc( nname
+ ncycle
, sizeof(nltype
*) );
671 if ( namesortnlp
== (nltype
**) 0 ) {
672 fprintf( stderr
, "%s: ran out of memory for sorting\n" , whoami
);
674 for ( index
= 0 , nnames
= 0 ; index
< nname
; index
++ ) {
675 if ( zflag
== 0 && nl
[index
].ncall
== 0 && nl
[index
].time
== 0 )
677 namesortnlp
[nnames
++] = &nl
[index
];
679 qsort( namesortnlp
, nnames
, sizeof(nltype
*) , namecmp
);
680 for ( index
= 1 , todo
= nnames
; index
<= ncycle
; index
++ ) {
681 namesortnlp
[todo
++] = &cyclenl
[index
];
683 printf( "\f\nIndex by function name\n\n" );
684 index
= ( todo
+ 2 ) / 3;
685 for ( i
= 0; i
< index
; i
++ ) {
686 for ( j
= i
; j
< todo
; j
+= index
) {
687 nlp
= namesortnlp
[ j
];
688 if ( nlp
-> printflag
) {
689 sprintf( peterbuffer
, "[%d]" , nlp
-> index
);
691 sprintf( peterbuffer
, "(%d)" , nlp
-> index
);
694 printf( "%6.6s %-19.19s" , peterbuffer
, nlp
-> name
);
696 printf( "%6.6s " , peterbuffer
);
697 sprintf( peterbuffer
, "<cycle %d>" , nlp
-> cycleno
);
698 printf( "%-19.19s" , peterbuffer
);
703 cfree( namesortnlp
);
This page took 0.048541 seconds and 4 git commands to generate.