mirrored from https://gitlab.haskell.org/ghc/ghc.git
/
RetainerProfile.c
2286 lines (2038 loc) · 64.8 KB
/
RetainerProfile.c
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
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -----------------------------------------------------------------------------
*
* (c) The GHC Team, 2001
* Author: Sungwoo Park
*
* Retainer profiling.
*
* ---------------------------------------------------------------------------*/
#ifdef PROFILING
// Turn off inlining when debugging - it obfuscates things
#ifdef DEBUG
#define INLINE
#else
#define INLINE inline
#endif
#include "PosixSource.h"
#include "Rts.h"
#include "RtsUtils.h"
#include "RetainerProfile.h"
#include "RetainerSet.h"
#include "Schedule.h"
#include "Printer.h"
#include "Weak.h"
#include "sm/Sanity.h"
#include "Profiling.h"
#include "Stats.h"
#include "ProfHeap.h"
#include "Apply.h"
#include "sm/Storage.h" // for END_OF_STATIC_LIST
/*
Note: what to change in order to plug-in a new retainer profiling scheme?
(1) type retainer in ../includes/StgRetainerProf.h
(2) retainer function R(), i.e., getRetainerFrom()
(3) the two hashing functions, hashKeySingleton() and hashKeyAddElement(),
in RetainerSet.h, if needed.
(4) printRetainer() and printRetainerSetShort() in RetainerSet.c.
*/
/* -----------------------------------------------------------------------------
* Declarations...
* -------------------------------------------------------------------------- */
static nat retainerGeneration; // generation
static nat numObjectVisited; // total number of objects visited
static nat timesAnyObjectVisited; // number of times any objects are visited
/*
The rs field in the profile header of any object points to its retainer
set in an indirect way: if flip is 0, it points to the retainer set;
if flip is 1, it points to the next byte after the retainer set (even
for NULL pointers). Therefore, with flip 1, (rs ^ 1) is the actual
pointer. See retainerSetOf().
*/
StgWord flip = 0; // flip bit
// must be 0 if DEBUG_RETAINER is on (for static closures)
#define setRetainerSetToNull(c) \
(c)->header.prof.hp.rs = (RetainerSet *)((StgWord)NULL | flip)
static void retainStack(StgClosure *, retainer, StgPtr, StgPtr);
static void retainClosure(StgClosure *, StgClosure *, retainer);
#ifdef DEBUG_RETAINER
static void belongToHeap(StgPtr p);
#endif
#ifdef DEBUG_RETAINER
/*
cStackSize records how many times retainStack() has been invoked recursively,
that is, the number of activation records for retainStack() on the C stack.
maxCStackSize records its max value.
Invariants:
cStackSize <= maxCStackSize
*/
static nat cStackSize, maxCStackSize;
static nat sumOfNewCost; // sum of the cost of each object, computed
// when the object is first visited
static nat sumOfNewCostExtra; // for those objects not visited during
// retainer profiling, e.g., MUT_VAR
static nat costArray[N_CLOSURE_TYPES];
nat sumOfCostLinear; // sum of the costs of all object, computed
// when linearly traversing the heap after
// retainer profiling
nat costArrayLinear[N_CLOSURE_TYPES];
#endif
/* -----------------------------------------------------------------------------
* Retainer stack - header
* Note:
* Although the retainer stack implementation could be separated *
* from the retainer profiling engine, there does not seem to be
* any advantage in doing that; retainer stack is an integral part
* of retainer profiling engine and cannot be use elsewhere at
* all.
* -------------------------------------------------------------------------- */
typedef enum {
posTypeStep,
posTypePtrs,
posTypeSRT,
posTypeLargeSRT,
} nextPosType;
typedef union {
// fixed layout or layout specified by a field in the closure
StgWord step;
// layout.payload
struct {
// See StgClosureInfo in InfoTables.h
#if SIZEOF_VOID_P == 8
StgWord32 pos;
StgWord32 ptrs;
#else
StgWord16 pos;
StgWord16 ptrs;
#endif
StgPtr payload;
} ptrs;
// SRT
struct {
StgClosure **srt;
StgWord srt_bitmap;
} srt;
// Large SRT
struct {
StgLargeSRT *srt;
StgWord offset;
} large_srt;
} nextPos;
typedef struct {
nextPosType type;
nextPos next;
} stackPos;
typedef struct {
StgClosure *c;
retainer c_child_r;
stackPos info;
} stackElement;
/*
Invariants:
firstStack points to the first block group.
currentStack points to the block group currently being used.
currentStack->free == stackLimit.
stackTop points to the topmost byte in the stack of currentStack.
Unless the whole stack is empty, stackTop must point to the topmost
object (or byte) in the whole stack. Thus, it is only when the whole stack
is empty that stackTop == stackLimit (not during the execution of push()
and pop()).
stackBottom == currentStack->start.
stackLimit == currentStack->start + BLOCK_SIZE_W * currentStack->blocks.
Note:
When a current stack becomes empty, stackTop is set to point to
the topmost element on the previous block group so as to satisfy
the invariants described above.
*/
static bdescr *firstStack = NULL;
static bdescr *currentStack;
static stackElement *stackBottom, *stackTop, *stackLimit;
/*
currentStackBoundary is used to mark the current stack chunk.
If stackTop == currentStackBoundary, it means that the current stack chunk
is empty. It is the responsibility of the user to keep currentStackBoundary
valid all the time if it is to be employed.
*/
static stackElement *currentStackBoundary;
/*
stackSize records the current size of the stack.
maxStackSize records its high water mark.
Invariants:
stackSize <= maxStackSize
Note:
stackSize is just an estimate measure of the depth of the graph. The reason
is that some heap objects have only a single child and may not result
in a new element being pushed onto the stack. Therefore, at the end of
retainer profiling, maxStackSize + maxCStackSize is some value no greater
than the actual depth of the graph.
*/
#ifdef DEBUG_RETAINER
static int stackSize, maxStackSize;
#endif
// number of blocks allocated for one stack
#define BLOCKS_IN_STACK 1
/* -----------------------------------------------------------------------------
* Add a new block group to the stack.
* Invariants:
* currentStack->link == s.
* -------------------------------------------------------------------------- */
static INLINE void
newStackBlock( bdescr *bd )
{
currentStack = bd;
stackTop = (stackElement *)(bd->start + BLOCK_SIZE_W * bd->blocks);
stackBottom = (stackElement *)bd->start;
stackLimit = (stackElement *)stackTop;
bd->free = (StgPtr)stackLimit;
}
/* -----------------------------------------------------------------------------
* Return to the previous block group.
* Invariants:
* s->link == currentStack.
* -------------------------------------------------------------------------- */
static INLINE void
returnToOldStack( bdescr *bd )
{
currentStack = bd;
stackTop = (stackElement *)bd->free;
stackBottom = (stackElement *)bd->start;
stackLimit = (stackElement *)(bd->start + BLOCK_SIZE_W * bd->blocks);
bd->free = (StgPtr)stackLimit;
}
/* -----------------------------------------------------------------------------
* Initializes the traverse stack.
* -------------------------------------------------------------------------- */
static void
initializeTraverseStack( void )
{
if (firstStack != NULL) {
freeChain(firstStack);
}
firstStack = allocGroup(BLOCKS_IN_STACK);
firstStack->link = NULL;
firstStack->u.back = NULL;
newStackBlock(firstStack);
}
/* -----------------------------------------------------------------------------
* Frees all the block groups in the traverse stack.
* Invariants:
* firstStack != NULL
* -------------------------------------------------------------------------- */
static void
closeTraverseStack( void )
{
freeChain(firstStack);
firstStack = NULL;
}
/* -----------------------------------------------------------------------------
* Returns rtsTrue if the whole stack is empty.
* -------------------------------------------------------------------------- */
static INLINE rtsBool
isEmptyRetainerStack( void )
{
return (firstStack == currentStack) && stackTop == stackLimit;
}
/* -----------------------------------------------------------------------------
* Returns size of stack
* -------------------------------------------------------------------------- */
#ifdef DEBUG
lnat
retainerStackBlocks( void )
{
bdescr* bd;
lnat res = 0;
for (bd = firstStack; bd != NULL; bd = bd->link)
res += bd->blocks;
return res;
}
#endif
/* -----------------------------------------------------------------------------
* Returns rtsTrue if stackTop is at the stack boundary of the current stack,
* i.e., if the current stack chunk is empty.
* -------------------------------------------------------------------------- */
static INLINE rtsBool
isOnBoundary( void )
{
return stackTop == currentStackBoundary;
}
/* -----------------------------------------------------------------------------
* Initializes *info from ptrs and payload.
* Invariants:
* payload[] begins with ptrs pointers followed by non-pointers.
* -------------------------------------------------------------------------- */
static INLINE void
init_ptrs( stackPos *info, nat ptrs, StgPtr payload )
{
info->type = posTypePtrs;
info->next.ptrs.pos = 0;
info->next.ptrs.ptrs = ptrs;
info->next.ptrs.payload = payload;
}
/* -----------------------------------------------------------------------------
* Find the next object from *info.
* -------------------------------------------------------------------------- */
static INLINE StgClosure *
find_ptrs( stackPos *info )
{
if (info->next.ptrs.pos < info->next.ptrs.ptrs) {
return (StgClosure *)info->next.ptrs.payload[info->next.ptrs.pos++];
} else {
return NULL;
}
}
/* -----------------------------------------------------------------------------
* Initializes *info from SRT information stored in *infoTable.
* -------------------------------------------------------------------------- */
static INLINE void
init_srt_fun( stackPos *info, StgFunInfoTable *infoTable )
{
if (infoTable->i.srt_bitmap == (StgHalfWord)(-1)) {
info->type = posTypeLargeSRT;
info->next.large_srt.srt = (StgLargeSRT *)GET_FUN_SRT(infoTable);
info->next.large_srt.offset = 0;
} else {
info->type = posTypeSRT;
info->next.srt.srt = (StgClosure **)GET_FUN_SRT(infoTable);
info->next.srt.srt_bitmap = infoTable->i.srt_bitmap;
}
}
static INLINE void
init_srt_thunk( stackPos *info, StgThunkInfoTable *infoTable )
{
if (infoTable->i.srt_bitmap == (StgHalfWord)(-1)) {
info->type = posTypeLargeSRT;
info->next.large_srt.srt = (StgLargeSRT *)GET_SRT(infoTable);
info->next.large_srt.offset = 0;
} else {
info->type = posTypeSRT;
info->next.srt.srt = (StgClosure **)GET_SRT(infoTable);
info->next.srt.srt_bitmap = infoTable->i.srt_bitmap;
}
}
/* -----------------------------------------------------------------------------
* Find the next object from *info.
* -------------------------------------------------------------------------- */
static INLINE StgClosure *
find_srt( stackPos *info )
{
StgClosure *c;
StgWord bitmap;
if (info->type == posTypeSRT) {
// Small SRT bitmap
bitmap = info->next.srt.srt_bitmap;
while (bitmap != 0) {
if ((bitmap & 1) != 0) {
#if defined(COMPILING_WINDOWS_DLL)
if ((unsigned long)(*(info->next.srt.srt)) & 0x1)
c = (* (StgClosure **)((unsigned long)*(info->next.srt.srt)) & ~0x1);
else
c = *(info->next.srt.srt);
#else
c = *(info->next.srt.srt);
#endif
bitmap = bitmap >> 1;
info->next.srt.srt++;
info->next.srt.srt_bitmap = bitmap;
return c;
}
bitmap = bitmap >> 1;
info->next.srt.srt++;
}
// bitmap is now zero...
return NULL;
}
else {
// Large SRT bitmap
nat i = info->next.large_srt.offset;
StgWord bitmap;
// Follow the pattern from GC.c:scavenge_large_srt_bitmap().
bitmap = info->next.large_srt.srt->l.bitmap[i / BITS_IN(W_)];
bitmap = bitmap >> (i % BITS_IN(StgWord));
while (i < info->next.large_srt.srt->l.size) {
if ((bitmap & 1) != 0) {
c = ((StgClosure **)info->next.large_srt.srt->srt)[i];
i++;
info->next.large_srt.offset = i;
return c;
}
i++;
if (i % BITS_IN(W_) == 0) {
bitmap = info->next.large_srt.srt->l.bitmap[i / BITS_IN(W_)];
} else {
bitmap = bitmap >> 1;
}
}
// reached the end of this bitmap.
info->next.large_srt.offset = i;
return NULL;
}
}
/* -----------------------------------------------------------------------------
* push() pushes a stackElement representing the next child of *c
* onto the traverse stack. If *c has no child, *first_child is set
* to NULL and nothing is pushed onto the stack. If *c has only one
* child, *c_chlid is set to that child and nothing is pushed onto
* the stack. If *c has more than two children, *first_child is set
* to the first child and a stackElement representing the second
* child is pushed onto the stack.
* Invariants:
* *c_child_r is the most recent retainer of *c's children.
* *c is not any of TSO, AP, PAP, AP_STACK, which means that
* there cannot be any stack objects.
* Note: SRTs are considered to be children as well.
* -------------------------------------------------------------------------- */
static INLINE void
push( StgClosure *c, retainer c_child_r, StgClosure **first_child )
{
stackElement se;
bdescr *nbd; // Next Block Descriptor
#ifdef DEBUG_RETAINER
// debugBelch("push(): stackTop = 0x%x, currentStackBoundary = 0x%x\n", stackTop, currentStackBoundary);
#endif
ASSERT(get_itbl(c)->type != TSO);
ASSERT(get_itbl(c)->type != AP_STACK);
//
// fill in se
//
se.c = c;
se.c_child_r = c_child_r;
// fill in se.info
switch (get_itbl(c)->type) {
// no child, no SRT
case CONSTR_0_1:
case CONSTR_0_2:
case ARR_WORDS:
*first_child = NULL;
return;
// one child (fixed), no SRT
case MUT_VAR_CLEAN:
case MUT_VAR_DIRTY:
*first_child = ((StgMutVar *)c)->var;
return;
case THUNK_SELECTOR:
*first_child = ((StgSelector *)c)->selectee;
return;
case IND_PERM:
case BLACKHOLE:
*first_child = ((StgInd *)c)->indirectee;
return;
case CONSTR_1_0:
case CONSTR_1_1:
*first_child = c->payload[0];
return;
// For CONSTR_2_0 and MVAR, we use se.info.step to record the position
// of the next child. We do not write a separate initialization code.
// Also we do not have to initialize info.type;
// two children (fixed), no SRT
// need to push a stackElement, but nothing to store in se.info
case CONSTR_2_0:
*first_child = c->payload[0]; // return the first pointer
// se.info.type = posTypeStep;
// se.info.next.step = 2; // 2 = second
break;
// three children (fixed), no SRT
// need to push a stackElement
case MVAR_CLEAN:
case MVAR_DIRTY:
// head must be TSO and the head of a linked list of TSOs.
// Shoule it be a child? Seems to be yes.
*first_child = (StgClosure *)((StgMVar *)c)->head;
// se.info.type = posTypeStep;
se.info.next.step = 2; // 2 = second
break;
// three children (fixed), no SRT
case WEAK:
*first_child = ((StgWeak *)c)->key;
// se.info.type = posTypeStep;
se.info.next.step = 2;
break;
// layout.payload.ptrs, no SRT
case CONSTR:
case PRIM:
case MUT_PRIM:
case BCO:
case CONSTR_STATIC:
init_ptrs(&se.info, get_itbl(c)->layout.payload.ptrs,
(StgPtr)c->payload);
*first_child = find_ptrs(&se.info);
if (*first_child == NULL)
return; // no child
break;
// StgMutArrPtr.ptrs, no SRT
case MUT_ARR_PTRS_CLEAN:
case MUT_ARR_PTRS_DIRTY:
case MUT_ARR_PTRS_FROZEN:
case MUT_ARR_PTRS_FROZEN0:
init_ptrs(&se.info, ((StgMutArrPtrs *)c)->ptrs,
(StgPtr)(((StgMutArrPtrs *)c)->payload));
*first_child = find_ptrs(&se.info);
if (*first_child == NULL)
return;
break;
// layout.payload.ptrs, SRT
case FUN: // *c is a heap object.
case FUN_2_0:
init_ptrs(&se.info, get_itbl(c)->layout.payload.ptrs, (StgPtr)c->payload);
*first_child = find_ptrs(&se.info);
if (*first_child == NULL)
// no child from ptrs, so check SRT
goto fun_srt_only;
break;
case THUNK:
case THUNK_2_0:
init_ptrs(&se.info, get_itbl(c)->layout.payload.ptrs,
(StgPtr)((StgThunk *)c)->payload);
*first_child = find_ptrs(&se.info);
if (*first_child == NULL)
// no child from ptrs, so check SRT
goto thunk_srt_only;
break;
// 1 fixed child, SRT
case FUN_1_0:
case FUN_1_1:
*first_child = c->payload[0];
ASSERT(*first_child != NULL);
init_srt_fun(&se.info, get_fun_itbl(c));
break;
case THUNK_1_0:
case THUNK_1_1:
*first_child = ((StgThunk *)c)->payload[0];
ASSERT(*first_child != NULL);
init_srt_thunk(&se.info, get_thunk_itbl(c));
break;
case FUN_STATIC: // *c is a heap object.
ASSERT(get_itbl(c)->srt_bitmap != 0);
case FUN_0_1:
case FUN_0_2:
fun_srt_only:
init_srt_fun(&se.info, get_fun_itbl(c));
*first_child = find_srt(&se.info);
if (*first_child == NULL)
return; // no child
break;
// SRT only
case THUNK_STATIC:
ASSERT(get_itbl(c)->srt_bitmap != 0);
case THUNK_0_1:
case THUNK_0_2:
thunk_srt_only:
init_srt_thunk(&se.info, get_thunk_itbl(c));
*first_child = find_srt(&se.info);
if (*first_child == NULL)
return; // no child
break;
case TREC_CHUNK:
*first_child = (StgClosure *)((StgTRecChunk *)c)->prev_chunk;
se.info.next.step = 0; // entry no.
break;
// cannot appear
case PAP:
case AP:
case AP_STACK:
case TSO:
case STACK:
case IND_STATIC:
case CONSTR_NOCAF_STATIC:
// stack objects
case UPDATE_FRAME:
case CATCH_FRAME:
case UNDERFLOW_FRAME:
case STOP_FRAME:
case RET_DYN:
case RET_BCO:
case RET_SMALL:
case RET_BIG:
// invalid objects
case IND:
case INVALID_OBJECT:
default:
barf("Invalid object *c in push()");
return;
}
if (stackTop - 1 < stackBottom) {
#ifdef DEBUG_RETAINER
// debugBelch("push() to the next stack.\n");
#endif
// currentStack->free is updated when the active stack is switched
// to the next stack.
currentStack->free = (StgPtr)stackTop;
if (currentStack->link == NULL) {
nbd = allocGroup(BLOCKS_IN_STACK);
nbd->link = NULL;
nbd->u.back = currentStack;
currentStack->link = nbd;
} else
nbd = currentStack->link;
newStackBlock(nbd);
}
// adjust stackTop (acutal push)
stackTop--;
// If the size of stackElement was huge, we would better replace the
// following statement by either a memcpy() call or a switch statement
// on the type of the element. Currently, the size of stackElement is
// small enough (5 words) that this direct assignment seems to be enough.
// ToDo: The line below leads to the warning:
// warning: 'se.info.type' may be used uninitialized in this function
// This is caused by the fact that there are execution paths through the
// large switch statement above where some cases do not initialize this
// field. Is this really harmless? Can we avoid the warning?
*stackTop = se;
#ifdef DEBUG_RETAINER
stackSize++;
if (stackSize > maxStackSize) maxStackSize = stackSize;
// ASSERT(stackSize >= 0);
// debugBelch("stackSize = %d\n", stackSize);
#endif
}
/* -----------------------------------------------------------------------------
* popOff() and popOffReal(): Pop a stackElement off the traverse stack.
* Invariants:
* stackTop cannot be equal to stackLimit unless the whole stack is
* empty, in which case popOff() is not allowed.
* Note:
* You can think of popOffReal() as a part of popOff() which is
* executed at the end of popOff() in necessary. Since popOff() is
* likely to be executed quite often while popOffReal() is not, we
* separate popOffReal() from popOff(), which is declared as an
* INLINE function (for the sake of execution speed). popOffReal()
* is called only within popOff() and nowhere else.
* -------------------------------------------------------------------------- */
static void
popOffReal(void)
{
bdescr *pbd; // Previous Block Descriptor
#ifdef DEBUG_RETAINER
// debugBelch("pop() to the previous stack.\n");
#endif
ASSERT(stackTop + 1 == stackLimit);
ASSERT(stackBottom == (stackElement *)currentStack->start);
if (firstStack == currentStack) {
// The stack is completely empty.
stackTop++;
ASSERT(stackTop == stackLimit);
#ifdef DEBUG_RETAINER
stackSize--;
if (stackSize > maxStackSize) maxStackSize = stackSize;
/*
ASSERT(stackSize >= 0);
debugBelch("stackSize = %d\n", stackSize);
*/
#endif
return;
}
// currentStack->free is updated when the active stack is switched back
// to the previous stack.
currentStack->free = (StgPtr)stackLimit;
// find the previous block descriptor
pbd = currentStack->u.back;
ASSERT(pbd != NULL);
returnToOldStack(pbd);
#ifdef DEBUG_RETAINER
stackSize--;
if (stackSize > maxStackSize) maxStackSize = stackSize;
/*
ASSERT(stackSize >= 0);
debugBelch("stackSize = %d\n", stackSize);
*/
#endif
}
static INLINE void
popOff(void) {
#ifdef DEBUG_RETAINER
// debugBelch("\tpopOff(): stackTop = 0x%x, currentStackBoundary = 0x%x\n", stackTop, currentStackBoundary);
#endif
ASSERT(stackTop != stackLimit);
ASSERT(!isEmptyRetainerStack());
// <= (instead of <) is wrong!
if (stackTop + 1 < stackLimit) {
stackTop++;
#ifdef DEBUG_RETAINER
stackSize--;
if (stackSize > maxStackSize) maxStackSize = stackSize;
/*
ASSERT(stackSize >= 0);
debugBelch("stackSize = %d\n", stackSize);
*/
#endif
return;
}
popOffReal();
}
/* -----------------------------------------------------------------------------
* Finds the next object to be considered for retainer profiling and store
* its pointer to *c.
* Test if the topmost stack element indicates that more objects are left,
* and if so, retrieve the first object and store its pointer to *c. Also,
* set *cp and *r appropriately, both of which are stored in the stack element.
* The topmost stack element then is overwritten so as for it to now denote
* the next object.
* If the topmost stack element indicates no more objects are left, pop
* off the stack element until either an object can be retrieved or
* the current stack chunk becomes empty, indicated by rtsTrue returned by
* isOnBoundary(), in which case *c is set to NULL.
* Note:
* It is okay to call this function even when the current stack chunk
* is empty.
* -------------------------------------------------------------------------- */
static INLINE void
pop( StgClosure **c, StgClosure **cp, retainer *r )
{
stackElement *se;
#ifdef DEBUG_RETAINER
// debugBelch("pop(): stackTop = 0x%x, currentStackBoundary = 0x%x\n", stackTop, currentStackBoundary);
#endif
do {
if (isOnBoundary()) { // if the current stack chunk is depleted
*c = NULL;
return;
}
se = stackTop;
switch (get_itbl(se->c)->type) {
// two children (fixed), no SRT
// nothing in se.info
case CONSTR_2_0:
*c = se->c->payload[1];
*cp = se->c;
*r = se->c_child_r;
popOff();
return;
// three children (fixed), no SRT
// need to push a stackElement
case MVAR_CLEAN:
case MVAR_DIRTY:
if (se->info.next.step == 2) {
*c = (StgClosure *)((StgMVar *)se->c)->tail;
se->info.next.step++; // move to the next step
// no popOff
} else {
*c = ((StgMVar *)se->c)->value;
popOff();
}
*cp = se->c;
*r = se->c_child_r;
return;
// three children (fixed), no SRT
case WEAK:
if (se->info.next.step == 2) {
*c = ((StgWeak *)se->c)->value;
se->info.next.step++;
// no popOff
} else {
*c = ((StgWeak *)se->c)->finalizer;
popOff();
}
*cp = se->c;
*r = se->c_child_r;
return;
case TREC_CHUNK: {
// These are pretty complicated: we have N entries, each
// of which contains 3 fields that we want to follow. So
// we divide the step counter: the 2 low bits indicate
// which field, and the rest of the bits indicate the
// entry number (starting from zero).
TRecEntry *entry;
nat entry_no = se->info.next.step >> 2;
nat field_no = se->info.next.step & 3;
if (entry_no == ((StgTRecChunk *)se->c)->next_entry_idx) {
*c = NULL;
popOff();
return;
}
entry = &((StgTRecChunk *)se->c)->entries[entry_no];
if (field_no == 0) {
*c = (StgClosure *)entry->tvar;
} else if (field_no == 1) {
*c = entry->expected_value;
} else {
*c = entry->new_value;
}
*cp = se->c;
*r = se->c_child_r;
se->info.next.step++;
return;
}
case CONSTR:
case PRIM:
case MUT_PRIM:
case BCO:
case CONSTR_STATIC:
// StgMutArrPtr.ptrs, no SRT
case MUT_ARR_PTRS_CLEAN:
case MUT_ARR_PTRS_DIRTY:
case MUT_ARR_PTRS_FROZEN:
case MUT_ARR_PTRS_FROZEN0:
*c = find_ptrs(&se->info);
if (*c == NULL) {
popOff();
break;
}
*cp = se->c;
*r = se->c_child_r;
return;
// layout.payload.ptrs, SRT
case FUN: // always a heap object
case FUN_2_0:
if (se->info.type == posTypePtrs) {
*c = find_ptrs(&se->info);
if (*c != NULL) {
*cp = se->c;
*r = se->c_child_r;
return;
}
init_srt_fun(&se->info, get_fun_itbl(se->c));
}
goto do_srt;
case THUNK:
case THUNK_2_0:
if (se->info.type == posTypePtrs) {
*c = find_ptrs(&se->info);
if (*c != NULL) {
*cp = se->c;
*r = se->c_child_r;
return;
}
init_srt_thunk(&se->info, get_thunk_itbl(se->c));
}
goto do_srt;
// SRT
do_srt:
case THUNK_STATIC:
case FUN_STATIC:
case FUN_0_1:
case FUN_0_2:
case THUNK_0_1:
case THUNK_0_2:
case FUN_1_0:
case FUN_1_1:
case THUNK_1_0:
case THUNK_1_1:
*c = find_srt(&se->info);
if (*c != NULL) {
*cp = se->c;
*r = se->c_child_r;
return;
}
popOff();
break;
// no child (fixed), no SRT
case CONSTR_0_1:
case CONSTR_0_2:
case ARR_WORDS:
// one child (fixed), no SRT
case MUT_VAR_CLEAN:
case MUT_VAR_DIRTY:
case THUNK_SELECTOR:
case IND_PERM:
case CONSTR_1_1:
// cannot appear
case PAP:
case AP:
case AP_STACK:
case TSO:
case STACK:
case IND_STATIC:
case CONSTR_NOCAF_STATIC:
// stack objects
case RET_DYN:
case UPDATE_FRAME:
case CATCH_FRAME:
case UNDERFLOW_FRAME:
case STOP_FRAME:
case RET_BCO:
case RET_SMALL:
case RET_BIG:
// invalid objects
case IND:
case INVALID_OBJECT:
default:
barf("Invalid object *c in pop()");
return;
}
} while (rtsTrue);
}
/* -----------------------------------------------------------------------------
* RETAINER PROFILING ENGINE
* -------------------------------------------------------------------------- */
void
initRetainerProfiling( void )
{
initializeAllRetainerSet();
retainerGeneration = 0;
}
/* -----------------------------------------------------------------------------
* This function must be called before f-closing prof_file.
* -------------------------------------------------------------------------- */
void
endRetainerProfiling( void )
{
#ifdef SECOND_APPROACH
outputAllRetainerSet(prof_file);
#endif
}
/* -----------------------------------------------------------------------------
* Returns the actual pointer to the retainer set of the closure *c.
* It may adjust RSET(c) subject to flip.
* Side effects:
* RSET(c) is initialized to NULL if its current value does not
* conform to flip.
* Note:
* Even though this function has side effects, they CAN be ignored because
* subsequent calls to retainerSetOf() always result in the same return value
* and retainerSetOf() is the only way to retrieve retainerSet of a given
* closure.
* We have to perform an XOR (^) operation each time a closure is examined.
* The reason is that we do not know when a closure is visited last.
* -------------------------------------------------------------------------- */
static INLINE void
maybeInitRetainerSet( StgClosure *c )
{
if (!isRetainerSetFieldValid(c)) {
setRetainerSetToNull(c);
}
}
/* -----------------------------------------------------------------------------
* Returns rtsTrue if *c is a retainer.
* -------------------------------------------------------------------------- */
static INLINE rtsBool
isRetainer( StgClosure *c )