1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
|
/* $OpenBSD: arcs.c,v 1.7 2003/06/03 02:56:08 millert Exp $ */
/* $NetBSD: arcs.c,v 1.6 1995/04/19 07:15:52 cgd Exp $ */
/*
* Copyright (c) 1983, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef lint
#if 0
static char sccsid[] = "@(#)arcs.c 8.1 (Berkeley) 6/6/93";
#else
static char rcsid[] = "$OpenBSD: arcs.c,v 1.7 2003/06/03 02:56:08 millert Exp $";
#endif
#endif /* not lint */
#include "gprof.h"
#ifdef DEBUG
int visited;
int viable;
int newcycle;
int oldcycle;
#endif /* DEBUG */
/*
* add (or just increment) an arc
*/
void
addarc( parentp , childp , count )
nltype *parentp;
nltype *childp;
long count;
{
arctype *arcp;
# ifdef DEBUG
if ( debug & TALLYDEBUG ) {
printf( "[addarc] %d arcs from %s to %s\n" ,
count , parentp -> name , childp -> name );
}
# endif /* DEBUG */
arcp = arclookup( parentp , childp );
if ( arcp != 0 ) {
/*
* a hit: just increment the count.
*/
# ifdef DEBUG
if ( debug & TALLYDEBUG ) {
printf( "[tally] hit %d += %d\n" ,
arcp -> arc_count , count );
}
# endif /* DEBUG */
arcp -> arc_count += count;
return;
}
arcp = (arctype *)calloc( 1 , sizeof *arcp );
arcp -> arc_parentp = parentp;
arcp -> arc_childp = childp;
arcp -> arc_count = count;
/*
* prepend this child to the children of this parent
*/
arcp -> arc_childlist = parentp -> children;
parentp -> children = arcp;
/*
* prepend this parent to the parents of this child
*/
arcp -> arc_parentlist = childp -> parents;
childp -> parents = arcp;
}
/*
* the code below topologically sorts the graph (collapsing cycles),
* and propagates time bottom up and flags top down.
*/
/*
* the topologically sorted name list pointers
*/
nltype **topsortnlp;
int
topcmp( npp1 , npp2 )
nltype **npp1;
nltype **npp2;
{
return (*npp1) -> toporder - (*npp2) -> toporder;
}
nltype **
doarcs()
{
nltype *parentp, **timesortnlp;
arctype *arcp;
long index;
long pass;
/*
* initialize various things:
* zero out child times.
* count self-recursive calls.
* indicate that nothing is on cycles.
*/
for ( parentp = nl ; parentp < npe ; parentp++ ) {
parentp -> childtime = 0.0;
arcp = arclookup( parentp , parentp );
if ( arcp != 0 ) {
parentp -> ncall -= arcp -> arc_count;
parentp -> selfcalls = arcp -> arc_count;
} else {
parentp -> selfcalls = 0;
}
parentp -> npropcall = parentp -> ncall;
parentp -> propfraction = 0.0;
parentp -> propself = 0.0;
parentp -> propchild = 0.0;
parentp -> printflag = FALSE;
parentp -> toporder = DFN_NAN;
parentp -> cycleno = 0;
parentp -> cyclehead = parentp;
parentp -> cnext = 0;
if ( cflag ) {
findcall( parentp , parentp -> value , (parentp+1) -> value );
}
}
for ( pass = 1 ; ; pass++ ) {
/*
* topologically order things
* if any node is unnumbered,
* number it and any of its descendents.
*/
for ( dfn_init() , parentp = nl ; parentp < npe ; parentp++ ) {
if ( parentp -> toporder == DFN_NAN ) {
dfn( parentp );
}
}
/*
* link together nodes on the same cycle
*/
cyclelink();
/*
* if no cycles to break up, proceed
*/
if ( ! Cflag )
break;
/*
* analyze cycles to determine breakup
*/
# ifdef DEBUG
if ( debug & BREAKCYCLE ) {
printf("[doarcs] pass %d, cycle(s) %d\n" , pass , ncycle );
}
# endif /* DEBUG */
if ( pass == 1 ) {
printf( "\n\n%s %s\n%s %d:\n" ,
"The following arcs were deleted" ,
"from the propagation calculation" ,
"to reduce the maximum cycle size to", cyclethreshold );
}
if ( cycleanalyze() )
break;
free ( cyclenl );
ncycle = 0;
for ( parentp = nl ; parentp < npe ; parentp++ ) {
parentp -> toporder = DFN_NAN;
parentp -> cycleno = 0;
parentp -> cyclehead = parentp;
parentp -> cnext = 0;
}
}
if ( pass > 1 ) {
printf( "\f\n" );
} else {
printf( "\tNone\n\n" );
}
/*
* Sort the symbol table in reverse topological order
*/
topsortnlp = (nltype **) calloc( nname , sizeof(nltype *) );
if ( topsortnlp == (nltype **) 0 )
warnx("[doarcs] ran out of memory for topo sorting");
for ( index = 0 ; index < nname ; index += 1 ) {
topsortnlp[ index ] = &nl[ index ];
}
qsort( topsortnlp , nname , sizeof(nltype *) , topcmp );
# ifdef DEBUG
if ( debug & DFNDEBUG ) {
printf( "[doarcs] topological sort listing\n" );
for ( index = 0 ; index < nname ; index += 1 ) {
printf( "[doarcs] " );
printf( "%d:" , topsortnlp[ index ] -> toporder );
printname( topsortnlp[ index ] );
printf( "\n" );
}
}
# endif /* DEBUG */
/*
* starting from the topological top,
* propagate print flags to children.
* also, calculate propagation fractions.
* this happens before time propagation
* since time propagation uses the fractions.
*/
doflags();
/*
* starting from the topological bottom,
* propogate children times up to parents.
*/
dotime();
/*
* Now, sort by propself + propchild.
* sorting both the regular function names
* and cycle headers.
*/
timesortnlp = (nltype **) calloc( nname + ncycle , sizeof(nltype *) );
if ( timesortnlp == (nltype **) 0 )
warnx("ran out of memory for sorting");
for ( index = 0 ; index < nname ; index++ ) {
timesortnlp[index] = &nl[index];
}
for ( index = 1 ; index <= ncycle ; index++ ) {
timesortnlp[nname+index-1] = &cyclenl[index];
}
qsort( timesortnlp , nname + ncycle , sizeof(nltype *) , totalcmp );
for ( index = 0 ; index < nname + ncycle ; index++ ) {
timesortnlp[ index ] -> index = index + 1;
}
return( timesortnlp );
}
void
dotime()
{
int index;
cycletime();
for ( index = 0 ; index < nname ; index += 1 ) {
timepropagate( topsortnlp[ index ] );
}
}
void
timepropagate( parentp )
nltype *parentp;
{
arctype *arcp;
nltype *childp;
double share;
double propshare;
if ( parentp -> propfraction == 0.0 ) {
return;
}
/*
* gather time from children of this parent.
*/
for ( arcp = parentp -> children ; arcp ; arcp = arcp -> arc_childlist ) {
childp = arcp -> arc_childp;
if ( arcp -> arc_flags & DEADARC ) {
continue;
}
if ( arcp -> arc_count == 0 ) {
continue;
}
if ( childp == parentp ) {
continue;
}
if ( childp -> propfraction == 0.0 ) {
continue;
}
if ( childp -> cyclehead != childp ) {
if ( parentp -> cycleno == childp -> cycleno ) {
continue;
}
if ( parentp -> toporder <= childp -> toporder )
warnx("[propagate] toporder botches");
childp = childp -> cyclehead;
} else {
if ( parentp -> toporder <= childp -> toporder ) {
warnx("[propagate] toporder botches");
continue;
}
}
if ( childp -> npropcall == 0 ) {
continue;
}
/*
* distribute time for this arc
*/
arcp -> arc_time = childp -> time
* ( ( (double) arcp -> arc_count ) /
( (double) childp -> npropcall ) );
arcp -> arc_childtime = childp -> childtime
* ( ( (double) arcp -> arc_count ) /
( (double) childp -> npropcall ) );
share = arcp -> arc_time + arcp -> arc_childtime;
parentp -> childtime += share;
/*
* ( 1 - propfraction ) gets lost along the way
*/
propshare = parentp -> propfraction * share;
/*
* fix things for printing
*/
parentp -> propchild += propshare;
arcp -> arc_time *= parentp -> propfraction;
arcp -> arc_childtime *= parentp -> propfraction;
/*
* add this share to the parent's cycle header, if any.
*/
if ( parentp -> cyclehead != parentp ) {
parentp -> cyclehead -> childtime += share;
parentp -> cyclehead -> propchild += propshare;
}
# ifdef DEBUG
if ( debug & PROPDEBUG ) {
printf( "[dotime] child \t" );
printname( childp );
printf( " with %f %f %d/%d\n" ,
childp -> time , childp -> childtime ,
arcp -> arc_count , childp -> npropcall );
printf( "[dotime] parent\t" );
printname( parentp );
printf( "\n[dotime] share %f\n" , share );
}
# endif /* DEBUG */
}
}
void
cyclelink()
{
nltype *nlp;
nltype *cyclenlp;
int cycle;
nltype *memberp;
arctype *arcp;
/*
* Count the number of cycles, and initialze the cycle lists
*/
ncycle = 0;
for ( nlp = nl ; nlp < npe ; nlp++ ) {
/*
* this is how you find unattached cycles
*/
if ( nlp -> cyclehead == nlp && nlp -> cnext != 0 ) {
ncycle += 1;
}
}
/*
* cyclenl is indexed by cycle number:
* i.e. it is origin 1, not origin 0.
*/
cyclenl = (nltype *) calloc( ncycle + 1 , sizeof( nltype ) );
if ( cyclenl == 0 )
errx(0, "No room for %d bytes of cycle headers",
(ncycle + 1) * sizeof(nltype));
/*
* now link cycles to true cycleheads,
* number them, accumulate the data for the cycle
*/
cycle = 0;
for ( nlp = nl ; nlp < npe ; nlp++ ) {
if ( !( nlp -> cyclehead == nlp && nlp -> cnext != 0 ) ) {
continue;
}
cycle += 1;
cyclenlp = &cyclenl[cycle];
cyclenlp -> name = 0; /* the name */
cyclenlp -> value = 0; /* the pc entry point */
cyclenlp -> time = 0.0; /* ticks in this routine */
cyclenlp -> childtime = 0.0; /* cumulative ticks in children */
cyclenlp -> ncall = 0; /* how many times called */
cyclenlp -> selfcalls = 0; /* how many calls to self */
cyclenlp -> propfraction = 0.0; /* what % of time propagates */
cyclenlp -> propself = 0.0; /* how much self time propagates */
cyclenlp -> propchild = 0.0; /* how much child time propagates */
cyclenlp -> printflag = TRUE; /* should this be printed? */
cyclenlp -> index = 0; /* index in the graph list */
cyclenlp -> toporder = DFN_NAN; /* graph call chain top-sort order */
cyclenlp -> cycleno = cycle; /* internal number of cycle on */
cyclenlp -> cyclehead = cyclenlp; /* pointer to head of cycle */
cyclenlp -> cnext = nlp; /* pointer to next member of cycle */
cyclenlp -> parents = 0; /* list of caller arcs */
cyclenlp -> children = 0; /* list of callee arcs */
# ifdef DEBUG
if ( debug & CYCLEDEBUG ) {
printf( "[cyclelink] " );
printname( nlp );
printf( " is the head of cycle %d\n" , cycle );
}
# endif /* DEBUG */
/*
* link members to cycle header
*/
for ( memberp = nlp ; memberp ; memberp = memberp -> cnext ) {
memberp -> cycleno = cycle;
memberp -> cyclehead = cyclenlp;
}
/*
* count calls from outside the cycle
* and those among cycle members
*/
for ( memberp = nlp ; memberp ; memberp = memberp -> cnext ) {
for ( arcp=memberp->parents ; arcp ; arcp=arcp->arc_parentlist ) {
if ( arcp -> arc_parentp == memberp ) {
continue;
}
if ( arcp -> arc_parentp -> cycleno == cycle ) {
cyclenlp -> selfcalls += arcp -> arc_count;
} else {
cyclenlp -> npropcall += arcp -> arc_count;
}
}
}
}
}
/*
* analyze cycles to determine breakup
*/
int
cycleanalyze()
{
arctype **cyclestack;
arctype **stkp;
arctype **arcpp;
arctype **endlist;
arctype *arcp;
nltype *nlp;
cltype *clp;
bool ret;
bool done;
int size;
int cycleno;
/*
* calculate the size of the cycle, and find nodes that
* exit the cycle as they are desirable targets to cut
* some of their parents
*/
for ( done = TRUE , cycleno = 1 ; cycleno <= ncycle ; cycleno++ ) {
size = 0;
for (nlp = cyclenl[ cycleno ] . cnext; nlp; nlp = nlp -> cnext) {
size += 1;
nlp -> parentcnt = 0;
nlp -> flags &= ~HASCYCLEXIT;
for ( arcp = nlp -> parents; arcp; arcp = arcp -> arc_parentlist ) {
nlp -> parentcnt += 1;
if ( arcp -> arc_parentp -> cycleno != cycleno )
nlp -> flags |= HASCYCLEXIT;
}
}
if ( size <= cyclethreshold )
continue;
done = FALSE;
cyclestack = (arctype **) calloc( size + 1 , sizeof( arctype *) );
if ( cyclestack == 0 ) {
warnx("No room for %d bytes of cycle stack" ,
(size + 1) * sizeof(arctype *));
return (done);
}
# ifdef DEBUG
if ( debug & BREAKCYCLE ) {
printf( "[cycleanalyze] starting cycle %d of %d, size %d\n" ,
cycleno , ncycle , size );
}
# endif /* DEBUG */
for ( nlp = cyclenl[ cycleno ] . cnext ; nlp ; nlp = nlp -> cnext ) {
stkp = &cyclestack[0];
nlp -> flags |= CYCLEHEAD;
ret = descend ( nlp , cyclestack , stkp );
nlp -> flags &= ~CYCLEHEAD;
if ( ret == FALSE )
break;
}
free( cyclestack );
if ( cyclecnt > 0 ) {
compresslist();
for ( clp = cyclehead ; clp ; ) {
endlist = &clp -> list[ clp -> size ];
for ( arcpp = clp -> list ; arcpp < endlist ; arcpp++ )
(*arcpp) -> arc_cyclecnt--;
cyclecnt--;
clp = clp -> next;
free( clp );
}
cyclehead = 0;
}
}
# ifdef DEBUG
if ( debug & BREAKCYCLE ) {
printf("%s visited %d, viable %d, newcycle %d, oldcycle %d\n",
"[doarcs]" , visited , viable , newcycle , oldcycle);
}
# endif /* DEBUG */
return (done);
}
int
descend( node , stkstart , stkp )
nltype *node;
arctype **stkstart;
arctype **stkp;
{
arctype *arcp;
bool ret;
for ( arcp = node -> children ; arcp ; arcp = arcp -> arc_childlist ) {
# ifdef DEBUG
visited++;
# endif /* DEBUG */
if ( arcp -> arc_childp -> cycleno != node -> cycleno
|| ( arcp -> arc_childp -> flags & VISITED )
|| ( arcp -> arc_flags & DEADARC ) )
continue;
# ifdef DEBUG
viable++;
# endif /* DEBUG */
*stkp = arcp;
if ( arcp -> arc_childp -> flags & CYCLEHEAD ) {
if ( addcycle( stkstart , stkp ) == FALSE )
return( FALSE );
continue;
}
arcp -> arc_childp -> flags |= VISITED;
ret = descend( arcp -> arc_childp , stkstart , stkp + 1 );
arcp -> arc_childp -> flags &= ~VISITED;
if ( ret == FALSE )
return( FALSE );
}
return (TRUE);
}
int
addcycle( stkstart , stkend )
arctype **stkstart;
arctype **stkend;
{
arctype **arcpp;
arctype **stkloc;
arctype **stkp;
arctype **endlist;
arctype *minarc;
arctype *arcp;
cltype *clp;
int size;
size = stkend - stkstart + 1;
if ( size <= 1 )
return( TRUE );
for ( arcpp = stkstart , minarc = *arcpp ; arcpp <= stkend ; arcpp++ ) {
if ( *arcpp > minarc )
continue;
minarc = *arcpp;
stkloc = arcpp;
}
for ( clp = cyclehead ; clp ; clp = clp -> next ) {
if ( clp -> size != size )
continue;
stkp = stkloc;
endlist = &clp -> list[ size ];
for ( arcpp = clp -> list ; arcpp < endlist ; arcpp++ ) {
if ( *stkp++ != *arcpp )
break;
if ( stkp > stkend )
stkp = stkstart;
}
if ( arcpp == endlist ) {
# ifdef DEBUG
oldcycle++;
# endif /* DEBUG */
return( TRUE );
}
}
clp = (cltype *)
calloc( 1 , sizeof ( cltype ) + ( size - 1 ) * sizeof( arctype * ) );
if ( clp == 0 ) {
warnx("No room for %d bytes of subcycle storage" ,
sizeof(cltype) + (size - 1) * sizeof(arctype *));
return( FALSE );
}
stkp = stkloc;
endlist = &clp -> list[ size ];
for ( arcpp = clp -> list ; arcpp < endlist ; arcpp++ ) {
arcp = *arcpp = *stkp++;
if ( stkp > stkend )
stkp = stkstart;
arcp -> arc_cyclecnt++;
if ( ( arcp -> arc_flags & ONLIST ) == 0 ) {
arcp -> arc_flags |= ONLIST;
arcp -> arc_next = archead;
archead = arcp;
}
}
clp -> size = size;
clp -> next = cyclehead;
cyclehead = clp;
# ifdef DEBUG
newcycle++;
if ( debug & SUBCYCLELIST ) {
printsubcycle( clp );
}
# endif /* DEBUG */
cyclecnt++;
if ( cyclecnt >= CYCLEMAX )
return( FALSE );
return( TRUE );
}
void
compresslist()
{
cltype *clp;
cltype **prev;
arctype **arcpp;
arctype **endlist;
arctype *arcp;
arctype *maxarcp;
arctype *maxexitarcp;
arctype *maxwithparentarcp;
arctype *maxnoparentarcp;
int maxexitcnt;
int maxwithparentcnt;
int maxnoparentcnt;
# ifdef DEBUG
char *type;
# endif
maxexitcnt = 0;
maxwithparentcnt = 0;
maxnoparentcnt = 0;
for ( endlist = &archead , arcp = archead ; arcp ; ) {
if ( arcp -> arc_cyclecnt == 0 ) {
arcp -> arc_flags &= ~ONLIST;
*endlist = arcp -> arc_next;
arcp -> arc_next = 0;
arcp = *endlist;
continue;
}
if ( arcp -> arc_childp -> flags & HASCYCLEXIT ) {
if ( arcp -> arc_cyclecnt > maxexitcnt ||
( arcp -> arc_cyclecnt == maxexitcnt &&
arcp -> arc_cyclecnt < maxexitarcp -> arc_count ) ) {
maxexitcnt = arcp -> arc_cyclecnt;
maxexitarcp = arcp;
}
} else if ( arcp -> arc_childp -> parentcnt > 1 ) {
if ( arcp -> arc_cyclecnt > maxwithparentcnt ||
( arcp -> arc_cyclecnt == maxwithparentcnt &&
arcp -> arc_cyclecnt < maxwithparentarcp -> arc_count ) ) {
maxwithparentcnt = arcp -> arc_cyclecnt;
maxwithparentarcp = arcp;
}
} else {
if ( arcp -> arc_cyclecnt > maxnoparentcnt ||
( arcp -> arc_cyclecnt == maxnoparentcnt &&
arcp -> arc_cyclecnt < maxnoparentarcp -> arc_count ) ) {
maxnoparentcnt = arcp -> arc_cyclecnt;
maxnoparentarcp = arcp;
}
}
endlist = &arcp -> arc_next;
arcp = arcp -> arc_next;
}
if ( maxexitcnt > 0 ) {
/*
* first choice is edge leading to node with out-of-cycle parent
*/
maxarcp = maxexitarcp;
# ifdef DEBUG
type = "exit";
# endif /* DEBUG */
} else if ( maxwithparentcnt > 0 ) {
/*
* second choice is edge leading to node with at least one
* other in-cycle parent
*/
maxarcp = maxwithparentarcp;
# ifdef DEBUG
type = "internal";
# endif /* DEBUG */
} else {
/*
* last choice is edge leading to node with only this arc as
* a parent (as it will now be orphaned)
*/
maxarcp = maxnoparentarcp;
# ifdef DEBUG
type = "orphan";
# endif /* DEBUG */
}
maxarcp -> arc_flags |= DEADARC;
maxarcp -> arc_childp -> parentcnt -= 1;
maxarcp -> arc_childp -> npropcall -= maxarcp -> arc_count;
# ifdef DEBUG
if ( debug & BREAKCYCLE ) {
printf("[compresslist] delete %s arc: "
"%s (%ld) -> %s from %d cycle(s)\n", type,
maxarcp -> arc_parentp -> name, maxarcp -> arc_count,
maxarcp -> arc_childp -> name, maxarcp -> arc_cyclecnt);
}
# endif /* DEBUG */
printf("\t%s to %s with %ld calls\n", maxarcp->arc_parentp -> name,
maxarcp->arc_childp->name, maxarcp->arc_count);
prev = &cyclehead;
for ( clp = cyclehead ; clp ; ) {
endlist = &clp -> list[ clp -> size ];
for ( arcpp = clp -> list ; arcpp < endlist ; arcpp++ )
if ( (*arcpp) -> arc_flags & DEADARC )
break;
if ( arcpp == endlist ) {
prev = &clp -> next;
clp = clp -> next;
continue;
}
for ( arcpp = clp -> list ; arcpp < endlist ; arcpp++ )
(*arcpp) -> arc_cyclecnt--;
cyclecnt--;
*prev = clp -> next;
clp = clp -> next;
free( clp );
}
}
#ifdef DEBUG
printsubcycle( clp )
cltype *clp;
{
arctype **arcpp;
arctype **endlist;
arcpp = clp -> list;
printf( "%s <cycle %d>\n" , (*arcpp) -> arc_parentp -> name ,
(*arcpp) -> arc_parentp -> cycleno ) ;
for ( endlist = &clp -> list[ clp -> size ]; arcpp < endlist ; arcpp++ )
printf( "\t(%d) -> %s\n" , (*arcpp) -> arc_count ,
(*arcpp) -> arc_childp -> name ) ;
}
#endif /* DEBUG */
void
cycletime()
{
int cycle;
nltype *cyclenlp;
nltype *childp;
for ( cycle = 1 ; cycle <= ncycle ; cycle += 1 ) {
cyclenlp = &cyclenl[ cycle ];
for ( childp = cyclenlp -> cnext ; childp ; childp = childp -> cnext ) {
if ( childp -> propfraction == 0.0 ) {
/*
* all members have the same propfraction except those
* that were excluded with -E
*/
continue;
}
cyclenlp -> time += childp -> time;
}
cyclenlp -> propself = cyclenlp -> propfraction * cyclenlp -> time;
}
}
/*
* in one top to bottom pass over the topologically sorted namelist
* propagate:
* printflag as the union of parents' printflags
* propfraction as the sum of fractional parents' propfractions
* and while we're here, sum time for functions.
*/
void
doflags()
{
int index;
nltype *childp;
nltype *oldhead;
oldhead = 0;
for ( index = nname-1 ; index >= 0 ; index -= 1 ) {
childp = topsortnlp[ index ];
/*
* if we haven't done this function or cycle,
* inherit things from parent.
* this way, we are linear in the number of arcs
* since we do all members of a cycle (and the cycle itself)
* as we hit the first member of the cycle.
*/
if ( childp -> cyclehead != oldhead ) {
oldhead = childp -> cyclehead;
inheritflags( childp );
}
# ifdef DEBUG
if ( debug & PROPDEBUG ) {
printf( "[doflags] " );
printname( childp );
printf( " inherits printflag %d and propfraction %f\n" ,
childp -> printflag , childp -> propfraction );
}
# endif /* DEBUG */
if ( ! childp -> printflag ) {
/*
* printflag is off
* it gets turned on by
* being on -f list,
* or there not being any -f list and not being on -e list.
*/
if ( onlist( flist , childp -> name )
|| ( !fflag && !onlist( elist , childp -> name ) ) ) {
childp -> printflag = TRUE;
}
} else {
/*
* this function has printing parents:
* maybe someone wants to shut it up
* by putting it on -e list. (but favor -f over -e)
*/
if ( ( !onlist( flist , childp -> name ) )
&& onlist( elist , childp -> name ) ) {
childp -> printflag = FALSE;
}
}
if ( childp -> propfraction == 0.0 ) {
/*
* no parents to pass time to.
* collect time from children if
* its on -F list,
* or there isn't any -F list and its not on -E list.
*/
if ( onlist( Flist , childp -> name )
|| ( !Fflag && !onlist( Elist , childp -> name ) ) ) {
childp -> propfraction = 1.0;
}
} else {
/*
* it has parents to pass time to,
* but maybe someone wants to shut it up
* by puttting it on -E list. (but favor -F over -E)
*/
if ( !onlist( Flist , childp -> name )
&& onlist( Elist , childp -> name ) ) {
childp -> propfraction = 0.0;
}
}
childp -> propself = childp -> time * childp -> propfraction;
printtime += childp -> propself;
# ifdef DEBUG
if ( debug & PROPDEBUG ) {
printf( "[doflags] " );
printname( childp );
printf( " ends up with printflag %d and propfraction %f\n" ,
childp -> printflag , childp -> propfraction );
printf( "time %f propself %f printtime %f\n" ,
childp -> time , childp -> propself , printtime );
}
# endif /* DEBUG */
}
}
/*
* check if any parent of this child
* (or outside parents of this cycle)
* have their print flags on and set the
* print flag of the child (cycle) appropriately.
* similarly, deal with propagation fractions from parents.
*/
void
inheritflags( childp )
nltype *childp;
{
nltype *headp;
arctype *arcp;
nltype *parentp;
nltype *memp;
headp = childp -> cyclehead;
if ( childp == headp ) {
/*
* just a regular child, check its parents
*/
childp -> printflag = FALSE;
childp -> propfraction = 0.0;
for (arcp = childp -> parents ; arcp ; arcp = arcp -> arc_parentlist) {
parentp = arcp -> arc_parentp;
if ( childp == parentp ) {
continue;
}
childp -> printflag |= parentp -> printflag;
/*
* if the child was never actually called
* (e.g. this arc is static (and all others are, too))
* no time propagates along this arc.
*/
if ( arcp -> arc_flags & DEADARC ) {
continue;
}
if ( childp -> npropcall ) {
childp -> propfraction += parentp -> propfraction
* ( ( (double) arcp -> arc_count )
/ ( (double) childp -> npropcall ) );
}
}
} else {
/*
* its a member of a cycle, look at all parents from
* outside the cycle
*/
headp -> printflag = FALSE;
headp -> propfraction = 0.0;
for ( memp = headp -> cnext ; memp ; memp = memp -> cnext ) {
for (arcp = memp->parents ; arcp ; arcp = arcp->arc_parentlist) {
if ( arcp -> arc_parentp -> cyclehead == headp ) {
continue;
}
parentp = arcp -> arc_parentp;
headp -> printflag |= parentp -> printflag;
/*
* if the cycle was never actually called
* (e.g. this arc is static (and all others are, too))
* no time propagates along this arc.
*/
if ( arcp -> arc_flags & DEADARC ) {
continue;
}
if ( headp -> npropcall ) {
headp -> propfraction += parentp -> propfraction
* ( ( (double) arcp -> arc_count )
/ ( (double) headp -> npropcall ) );
}
}
}
for ( memp = headp ; memp ; memp = memp -> cnext ) {
memp -> printflag = headp -> printflag;
memp -> propfraction = headp -> propfraction;
}
}
}
|