RetainerProfile.c 71.8 KB
Newer Older
1 2 3 4 5 6 7 8 9
/* -----------------------------------------------------------------------------
 *
 * (c) The GHC Team, 2001
 * Author: Sungwoo Park
 *
 * Retainer profiling.
 *
 * ---------------------------------------------------------------------------*/

Ben Gamari's avatar
Ben Gamari committed
10
#if defined(PROFILING)
11

12
// Turn off inlining when debugging - it obfuscates things
Ben Gamari's avatar
Ben Gamari committed
13
#if defined(DEBUG)
14 15 16 17 18
#define INLINE
#else
#define INLINE inline
#endif

Simon Marlow's avatar
Simon Marlow committed
19
#include "PosixSource.h"
20
#include "Rts.h"
Simon Marlow's avatar
Simon Marlow committed
21

22 23 24 25 26 27
#include "RtsUtils.h"
#include "RetainerProfile.h"
#include "RetainerSet.h"
#include "Schedule.h"
#include "Printer.h"
#include "Weak.h"
Simon Marlow's avatar
Simon Marlow committed
28
#include "sm/Sanity.h"
29 30 31
#include "Profiling.h"
#include "Stats.h"
#include "ProfHeap.h"
32
#include "Apply.h"
David Feuer's avatar
David Feuer committed
33 34
#include "StablePtr.h" /* markStablePtrTable */
#include "StableName.h" /* rememberOldStableNameAddresses */
Simon Marlow's avatar
Simon Marlow committed
35
#include "sm/Storage.h" // for END_OF_STATIC_LIST
36

37 38
/* Note [What is a retainer?]
   ~~~~~~~~~~~~~~~~~~~~~~~~~~
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
Retainer profiling is a profiling technique that gives information why
objects can't be freed and lists the consumers that hold pointers to
the heap objects. It does not list all the objects that keeps references
to the other, because then we would keep too much information that will
make the report unusable, for example the cons element of the list would keep
all the tail cells. As a result we are keeping only the objects of the
certain types, see 'isRetainer()' function for more discussion.

More formal definition of the retainer can be given the following way.

An object p is a retainer object of the object l, if all requirements
hold:

  1. p can be a retainer (see `isRetainer()`)
  2. l is reachable from p
  3. There are no other retainers on the path from p to l.

Exact algorithm and additional information can be found the historical
document 'docs/storage-mgt/rp.tex'. Details that are related to the
RTS implementation may be out of date, but the general
information about the retainers is still applicable.
60 61 62
*/


63 64 65 66 67 68 69 70 71 72 73 74 75
/*
  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...
 * -------------------------------------------------------------------------- */

76
static uint32_t retainerGeneration;  // generation
77

78 79 80
static uint32_t numObjectVisited;    // total number of objects visited
static uint32_t timesAnyObjectVisited;  // number of times any objects are
                                        // visited
81 82 83 84 85 86 87 88 89

/*
  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().
 */

90
StgWord flip = 0;     // flip bit
91 92 93 94 95
                      // must be 0 if DEBUG_RETAINER is on (for static closures)

#define setRetainerSetToNull(c)   \
  (c)->header.prof.hp.rs = (RetainerSet *)((StgWord)NULL | flip)

96
static void retainStack(StgClosure *, retainer, StgPtr, StgPtr);
97
static void retainClosure(StgClosure *, StgClosure *, retainer);
Ben Gamari's avatar
Ben Gamari committed
98
#if defined(DEBUG_RETAINER)
99 100
static void belongToHeap(StgPtr p);
#endif
101
static void retainPushClosure( StgClosure *p, StgClosure *c, retainer c_child_r);
102

Ben Gamari's avatar
Ben Gamari committed
103
#if defined(DEBUG_RETAINER)
104 105 106 107 108 109 110
/*
  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
 */
111
static uint32_t cStackSize, maxCStackSize;
112

113
static uint32_t sumOfNewCost;        // sum of the cost of each object, computed
114
                                // when the object is first visited
115
static uint32_t sumOfNewCostExtra;   // for those objects not visited during
116
                                // retainer profiling, e.g., MUT_VAR
117
static uint32_t costArray[N_CLOSURE_TYPES];
118

119
uint32_t sumOfCostLinear;            // sum of the costs of all object, computed
120 121
                                // when linearly traversing the heap after
                                // retainer profiling
122
uint32_t costArrayLinear[N_CLOSURE_TYPES];
123 124 125 126 127 128 129 130 131 132 133 134 135
#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 {
136 137
    // Object with fixed layout. Keeps an information about that
    // element was processed. (stackPos.next.step)
138
    posTypeStep,
139 140
    // Description of the pointers-first heap object. Keeps information
    // about layout. (stackPos.next.ptrs)
141
    posTypePtrs,
142
    // Keeps SRT bitmap (stackPos.next.srt)
143
    posTypeSRT,
144 145 146
    // Keeps a new object that was not inspected yet. Keeps a parent
    // element (stackPos.next.parent)
    posTypeFresh
147 148 149 150 151 152 153 154
} nextPosType;

typedef union {
    // fixed layout or layout specified by a field in the closure
    StgWord step;

    // layout.payload
    struct {
155 156 157
        // See StgClosureInfo in InfoTables.h
        StgHalfWord pos;
        StgHalfWord ptrs;
158
        StgPtr payload;
159 160 161 162
    } ptrs;

    // SRT
    struct {
163
        StgClosure *srt;
164
    } srt;
165 166 167 168

    // parent of the current object, used
    // when posTypeFresh is set
    StgClosure *parent;
169 170
} nextPos;

171 172
// Tagged stack element, that keeps information how to process
// the next element in the traverse stack.
173 174 175 176 177
typedef struct {
    nextPosType type;
    nextPos next;
} stackPos;

178 179
// Element in the traverse stack, keeps the element, information
// how to continue processing the element, and it's retainer set.
180 181
typedef struct {
    StgClosure *c;
182
    retainer c_child_r;
183 184 185
    stackPos info;
} stackElement;

186 187
static void retainActualPush( stackElement *se);

188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
/*
  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.
 */
sof's avatar
sof committed
205
static bdescr *firstStack = NULL;
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
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.
 */
Ben Gamari's avatar
Ben Gamari committed
229
#if defined(DEBUG_RETAINER)
230 231 232 233 234 235 236 237 238 239 240
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.
 * -------------------------------------------------------------------------- */
241
static INLINE void
242 243 244 245 246 247 248 249 250 251 252 253 254 255
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.
 * -------------------------------------------------------------------------- */
256
static INLINE void
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
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) {
273
        freeChain(firstStack);
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
    }

    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;
}

/* -----------------------------------------------------------------------------
Ben Gamari's avatar
Ben Gamari committed
296
 * Returns true if the whole stack is empty.
297
 * -------------------------------------------------------------------------- */
Ben Gamari's avatar
Ben Gamari committed
298
static INLINE bool
299 300 301 302 303
isEmptyRetainerStack( void )
{
    return (firstStack == currentStack) && stackTop == stackLimit;
}

sof's avatar
sof committed
304 305 306
/* -----------------------------------------------------------------------------
 * Returns size of stack
 * -------------------------------------------------------------------------- */
307
W_
308
retainerStackBlocks( void )
sof's avatar
sof committed
309 310
{
    bdescr* bd;
311
    W_ res = 0;
sof's avatar
sof committed
312

313
    for (bd = firstStack; bd != NULL; bd = bd->link)
sof's avatar
sof committed
314 315 316 317 318
      res += bd->blocks;

    return res;
}

319
/* -----------------------------------------------------------------------------
Ben Gamari's avatar
Ben Gamari committed
320
 * Returns true if stackTop is at the stack boundary of the current stack,
321 322
 * i.e., if the current stack chunk is empty.
 * -------------------------------------------------------------------------- */
Ben Gamari's avatar
Ben Gamari committed
323
static INLINE bool
324 325 326 327 328 329 330 331 332 333
isOnBoundary( void )
{
    return stackTop == currentStackBoundary;
}

/* -----------------------------------------------------------------------------
 * Initializes *info from ptrs and payload.
 * Invariants:
 *   payload[] begins with ptrs pointers followed by non-pointers.
 * -------------------------------------------------------------------------- */
334
static INLINE void
335
init_ptrs( stackPos *info, uint32_t ptrs, StgPtr payload )
336 337 338 339 340 341 342 343 344 345
{
    info->type              = posTypePtrs;
    info->next.ptrs.pos     = 0;
    info->next.ptrs.ptrs    = ptrs;
    info->next.ptrs.payload = payload;
}

/* -----------------------------------------------------------------------------
 * Find the next object from *info.
 * -------------------------------------------------------------------------- */
346
static INLINE StgClosure *
347 348 349
find_ptrs( stackPos *info )
{
    if (info->next.ptrs.pos < info->next.ptrs.ptrs) {
350
        return (StgClosure *)info->next.ptrs.payload[info->next.ptrs.pos++];
351
    } else {
352
        return NULL;
353 354 355 356 357 358
    }
}

/* -----------------------------------------------------------------------------
 *  Initializes *info from SRT information stored in *infoTable.
 * -------------------------------------------------------------------------- */
359
static INLINE void
360
init_srt_fun( stackPos *info, const StgFunInfoTable *infoTable )
361
{
362 363 364
    info->type = posTypeSRT;
    if (infoTable->i.srt) {
        info->next.srt.srt = (StgClosure*)GET_FUN_SRT(infoTable);
365
    } else {
366
        info->next.srt.srt = NULL;
367
    }
368 369
}

370
static INLINE void
371
init_srt_thunk( stackPos *info, const StgThunkInfoTable *infoTable )
372
{
Ömer Sinan Ağacan's avatar
Ömer Sinan Ağacan committed
373
    info->type = posTypeSRT;
374 375
    if (infoTable->i.srt) {
        info->next.srt.srt = (StgClosure*)GET_SRT(infoTable);
376
    } else {
377
        info->next.srt.srt = NULL;
378
    }
379 380 381 382 383
}

/* -----------------------------------------------------------------------------
 * Find the next object from *info.
 * -------------------------------------------------------------------------- */
384
static INLINE StgClosure *
385 386 387
find_srt( stackPos *info )
{
    StgClosure *c;
388
    if (info->type == posTypeSRT) {
389 390 391
        c = info->next.srt.srt;
        info->next.srt.srt = NULL;
        return c;
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
/* -----------------------------------------------------------------------------
 * Pushes an element onto traverse stack
 * -------------------------------------------------------------------------- */
static void
retainActualPush(stackElement *se) {
    bdescr *nbd;      // Next Block Descriptor
    if (stackTop - 1 < stackBottom) {
#if defined(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.
    *stackTop = *se;

#if defined(DEBUG_RETAINER)
    stackSize++;
    if (stackSize > maxStackSize) maxStackSize = stackSize;
    // ASSERT(stackSize >= 0);
    // debugBelch("stackSize = %d\n", stackSize);
#endif
}

/* Push an object onto traverse stack. This method can be used anytime
 * instead of calling retainClosure(), it exists in order to use an
 * explicit stack instead of direct recursion.
 *
 *  *p - object's parent
 *  *c - closure
 *  c_child_r - closure retainer.
 */
static INLINE void
retainPushClosure( StgClosure *c, StgClosure *p, retainer c_child_r) {
    stackElement se;

    se.c = c;
    se.c_child_r = c_child_r;
    se.info.next.parent = p;
    se.info.type = posTypeFresh;

    retainActualPush(&se);
};

456 457 458 459
/* -----------------------------------------------------------------------------
 *  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
460
 *  child, *c_child is set to that child and nothing is pushed onto
461 462 463 464 465 466
 *  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.
467
 *     *c is not any of TSO, AP, PAP, AP_STACK, which means that
468 469 470
 *        there cannot be any stack objects.
 *  Note: SRTs are considered to  be children as well.
 * -------------------------------------------------------------------------- */
471
static INLINE void
472
push( StgClosure *c, retainer c_child_r, StgClosure **first_child )
473 474 475 476
{
    stackElement se;
    bdescr *nbd;      // Next Block Descriptor

Ben Gamari's avatar
Ben Gamari committed
477
#if defined(DEBUG_RETAINER)
478
    // debugBelch("push(): stackTop = 0x%x, currentStackBoundary = 0x%x\n", stackTop, currentStackBoundary);
479 480 481
#endif

    ASSERT(get_itbl(c)->type != TSO);
482
    ASSERT(get_itbl(c)->type != AP_STACK);
483 484 485 486 487 488 489 490 491 492

    //
    // fill in se
    //

    se.c = c;
    se.c_child_r = c_child_r;

    // fill in se.info
    switch (get_itbl(c)->type) {
493
        // no child, no SRT
494 495 496
    case CONSTR_0_1:
    case CONSTR_0_2:
    case ARR_WORDS:
gcampax's avatar
gcampax committed
497
    case COMPACT_NFDATA:
498 499
        *first_child = NULL;
        return;
500

501
        // one child (fixed), no SRT
502 503
    case MUT_VAR_CLEAN:
    case MUT_VAR_DIRTY:
504 505
        *first_child = ((StgMutVar *)c)->var;
        return;
506
    case THUNK_SELECTOR:
507 508
        *first_child = ((StgSelector *)c)->selectee;
        return;
509
    case BLACKHOLE:
510 511
        *first_child = ((StgInd *)c)->indirectee;
        return;
512 513
    case CONSTR_1_0:
    case CONSTR_1_1:
514 515
        *first_child = c->payload[0];
        return;
516

517 518 519
        // 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;
520

521 522
        // two children (fixed), no SRT
        // need to push a stackElement, but nothing to store in se.info
523
    case CONSTR_2_0:
524
        *first_child = c->payload[0];         // return the first pointer
525 526
        se.info.type = posTypeStep;
        se.info.next.step = 2;            // 2 = second
527
        break;
528

529 530
        // three children (fixed), no SRT
        // need to push a stackElement
531 532
    case MVAR_CLEAN:
    case MVAR_DIRTY:
533 534 535
        // 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;
536
        se.info.type = posTypeStep;
537 538 539 540
        se.info.next.step = 2;            // 2 = second
        break;

        // three children (fixed), no SRT
541
    case WEAK:
542
        *first_child = ((StgWeak *)c)->key;
543
        se.info.type = posTypeStep;
544 545
        se.info.next.step = 2;
        break;
546

547
        // layout.payload.ptrs, no SRT
548
    case TVAR:
549
    case CONSTR:
Simon Marlow's avatar
Simon Marlow committed
550
    case CONSTR_NOCAF:
551
    case PRIM:
552
    case MUT_PRIM:
553
    case BCO:
554 555 556 557 558 559 560 561
        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
562 563
    case MUT_ARR_PTRS_CLEAN:
    case MUT_ARR_PTRS_DIRTY:
564 565
    case MUT_ARR_PTRS_FROZEN_CLEAN:
    case MUT_ARR_PTRS_FROZEN_DIRTY:
566 567 568 569 570 571 572 573
        init_ptrs(&se.info, ((StgMutArrPtrs *)c)->ptrs,
                  (StgPtr)(((StgMutArrPtrs *)c)->payload));
        *first_child = find_ptrs(&se.info);
        if (*first_child == NULL)
            return;
        break;

        // StgMutArrPtr.ptrs, no SRT
574 575
    case SMALL_MUT_ARR_PTRS_CLEAN:
    case SMALL_MUT_ARR_PTRS_DIRTY:
576 577
    case SMALL_MUT_ARR_PTRS_FROZEN_CLEAN:
    case SMALL_MUT_ARR_PTRS_FROZEN_DIRTY:
578 579 580 581 582 583
        init_ptrs(&se.info, ((StgSmallMutArrPtrs *)c)->ptrs,
                  (StgPtr)(((StgSmallMutArrPtrs *)c)->payload));
        *first_child = find_ptrs(&se.info);
        if (*first_child == NULL)
            return;
        break;
584

585
    // layout.payload.ptrs, SRT
586
    case FUN_STATIC:
587 588
    case FUN:           // *c is a heap object.
    case FUN_2_0:
589 590 591 592 593 594
        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;
595

596 597
    case THUNK:
    case THUNK_2_0:
598 599 600 601 602 603 604 605 606
        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
607 608
    case FUN_1_0:
    case FUN_1_1:
609 610 611 612
        *first_child = c->payload[0];
        ASSERT(*first_child != NULL);
        init_srt_fun(&se.info, get_fun_itbl(c));
        break;
613

614 615
    case THUNK_1_0:
    case THUNK_1_1:
616 617 618 619
        *first_child = ((StgThunk *)c)->payload[0];
        ASSERT(*first_child != NULL);
        init_srt_thunk(&se.info, get_thunk_itbl(c));
        break;
620

621
    case FUN_0_1:      // *c is a heap object.
622
    case FUN_0_2:
623 624
    fun_srt_only:
        init_srt_fun(&se.info, get_fun_itbl(c));
625 626 627 628
        *first_child = find_srt(&se.info);
        if (*first_child == NULL)
            return;     // no child
        break;
629 630 631

    // SRT only
    case THUNK_STATIC:
632
        ASSERT(get_itbl(c)->srt != 0);
633 634
    case THUNK_0_1:
    case THUNK_0_2:
635 636
    thunk_srt_only:
        init_srt_thunk(&se.info, get_thunk_itbl(c));
637 638 639 640 641
        *first_child = find_srt(&se.info);
        if (*first_child == NULL)
            return;     // no child
        break;

642
    case TREC_CHUNK:
643
        *first_child = (StgClosure *)((StgTRecChunk *)c)->prev_chunk;
644
        se.info.type = posTypeStep;
645 646
        se.info.next.step = 0;  // entry no.
        break;
647

648
        // cannot appear
649
    case PAP:
650 651
    case AP:
    case AP_STACK:
652
    case TSO:
653
    case STACK:
654
    case IND_STATIC:
655
        // stack objects
656 657
    case UPDATE_FRAME:
    case CATCH_FRAME:
658
    case UNDERFLOW_FRAME:
659 660 661 662
    case STOP_FRAME:
    case RET_BCO:
    case RET_SMALL:
    case RET_BIG:
663
        // invalid objects
664 665 666
    case IND:
    case INVALID_OBJECT:
    default:
667
        barf("Invalid object *c in push(): %d", get_itbl(c)->type);
668
        return;
669 670
    }

671
    retainActualPush(&se);
672 673 674 675 676 677 678 679 680 681 682 683
}

/* -----------------------------------------------------------------------------
 *  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
684
 *    INLINE function (for the sake of execution speed).  popOffReal()
685 686 687 688 689 690 691
 *    is called only within popOff() and nowhere else.
 * -------------------------------------------------------------------------- */
static void
popOffReal(void)
{
    bdescr *pbd;    // Previous Block Descriptor

Ben Gamari's avatar
Ben Gamari committed
692
#if defined(DEBUG_RETAINER)
693
    // debugBelch("pop() to the previous stack.\n");
694 695 696 697 698 699
#endif

    ASSERT(stackTop + 1 == stackLimit);
    ASSERT(stackBottom == (stackElement *)currentStack->start);

    if (firstStack == currentStack) {
700 701 702
        // The stack is completely empty.
        stackTop++;
        ASSERT(stackTop == stackLimit);
Ben Gamari's avatar
Ben Gamari committed
703
#if defined(DEBUG_RETAINER)
704 705 706 707 708 709
        stackSize--;
        if (stackSize > maxStackSize) maxStackSize = stackSize;
        /*
          ASSERT(stackSize >= 0);
          debugBelch("stackSize = %d\n", stackSize);
        */
710
#endif
711
        return;
712 713 714 715 716 717 718 719 720 721 722 723
    }

    // 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);

Ben Gamari's avatar
Ben Gamari committed
724
#if defined(DEBUG_RETAINER)
725 726 727 728
    stackSize--;
    if (stackSize > maxStackSize) maxStackSize = stackSize;
    /*
      ASSERT(stackSize >= 0);
729
      debugBelch("stackSize = %d\n", stackSize);
730 731 732 733
    */
#endif
}

734
static INLINE void
735
popOff(void) {
Ben Gamari's avatar
Ben Gamari committed
736
#if defined(DEBUG_RETAINER)
737
    // debugBelch("\tpopOff(): stackTop = 0x%x, currentStackBoundary = 0x%x\n", stackTop, currentStackBoundary);
738 739 740 741 742 743 744
#endif

    ASSERT(stackTop != stackLimit);
    ASSERT(!isEmptyRetainerStack());

    // <= (instead of <) is wrong!
    if (stackTop + 1 < stackLimit) {
745
        stackTop++;
Ben Gamari's avatar
Ben Gamari committed
746
#if defined(DEBUG_RETAINER)
747 748 749 750 751 752
        stackSize--;
        if (stackSize > maxStackSize) maxStackSize = stackSize;
        /*
          ASSERT(stackSize >= 0);
          debugBelch("stackSize = %d\n", stackSize);
        */
753
#endif
754
        return;
755 756 757 758 759 760 761 762
    }

    popOffReal();
}

/* -----------------------------------------------------------------------------
 *  Finds the next object to be considered for retainer profiling and store
 *  its pointer to *c.
763 764 765
 *  If the unprocessed object was stored in the stack (posTypeFresh), the
 *  this object is returned as-is. Otherwise Test if the topmost stack
 *  element indicates that more objects are left,
766 767 768 769 770 771
 *  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
Ben Gamari's avatar
Ben Gamari committed
772
 *  the current stack chunk becomes empty, indicated by true returned by
773 774 775 776 777
 *  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.
 * -------------------------------------------------------------------------- */
778
static INLINE void
779
pop( StgClosure **c, StgClosure **cp, retainer *r )
780 781 782
{
    stackElement *se;

Ben Gamari's avatar
Ben Gamari committed
783
#if defined(DEBUG_RETAINER)
784
    // debugBelch("pop(): stackTop = 0x%x, currentStackBoundary = 0x%x\n", stackTop, currentStackBoundary);
785 786 787
#endif

    do {
788 789 790 791 792 793 794
        if (isOnBoundary()) {     // if the current stack chunk is depleted
            *c = NULL;
            return;
        }

        se = stackTop;

795 796 797 798 799 800 801 802 803
        // If this is a top-level element, you should pop that out.
        if (se->info.type == posTypeFresh) {
            *cp = se->info.next.parent;
            *c = se->c;
            *r = se->c_child_r;
            popOff();
            return;
        }

804 805 806 807 808 809 810 811 812 813 814 815
        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
816 817
        case MVAR_CLEAN:
        case MVAR_DIRTY:
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
            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;
851 852
            uint32_t entry_no = se->info.next.step >> 2;
            uint32_t field_no = se->info.next.step & 3;
853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870
            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;
        }
871

872 873
        case TVAR:
        case CONSTR:
874 875 876 877 878 879
        case PRIM:
        case MUT_PRIM:
        case BCO:
            // StgMutArrPtr.ptrs, no SRT
        case MUT_ARR_PTRS_CLEAN:
        case MUT_ARR_PTRS_DIRTY:
880 881
        case MUT_ARR_PTRS_FROZEN_CLEAN:
        case MUT_ARR_PTRS_FROZEN_DIRTY:
882 883 884 885
        case SMALL_MUT_ARR_PTRS_CLEAN:
        case SMALL_MUT_ARR_PTRS_DIRTY:
        case SMALL_MUT_ARR_PTRS_FROZEN_CLEAN:
        case SMALL_MUT_ARR_PTRS_FROZEN_DIRTY:
886 887 888 889 890 891 892 893 894 895 896
            *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
897
        case FUN_STATIC:
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
        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_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 CONSTR_1_1:
            // cannot appear
        case PAP:
        case AP:
        case AP_STACK:
        case TSO:
957 958
        case STACK:
        case IND_STATIC:
Simon Marlow's avatar
Simon Marlow committed
959
        case CONSTR_NOCAF:
960
            // stack objects
961
        case UPDATE_FRAME:
962
        case CATCH_FRAME:
963 964
        case UNDERFLOW_FRAME:
        case STOP_FRAME:
965 966 967 968 969 970 971
        case RET_BCO:
        case RET_SMALL:
        case RET_BIG:
            // invalid objects
        case IND:
        case INVALID_OBJECT:
        default:
972
            barf("Invalid object *c in pop(): %d", get_itbl(se->c)->type);
973 974
            return;
        }
Ben Gamari's avatar
Ben Gamari committed
975
    } while (true);
976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994
}

/* -----------------------------------------------------------------------------
 * RETAINER PROFILING ENGINE
 * -------------------------------------------------------------------------- */

void
initRetainerProfiling( void )
{
    initializeAllRetainerSet();
    retainerGeneration = 0;
}

/* -----------------------------------------------------------------------------
 *  This function must be called before f-closing prof_file.
 * -------------------------------------------------------------------------- */
void
endRetainerProfiling( void )
{
Ben Gamari's avatar
Ben Gamari committed
995
#if defined(SECOND_APPROACH)
996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013
    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.
 * -------------------------------------------------------------------------- */
1014
static INLINE void
1015 1016 1017
maybeInitRetainerSet( StgClosure *c )
{
    if (!isRetainerSetFieldValid(c)) {
1018
        setRetainerSetToNull(c);
1019 1020 1021 1022
    }
}

/* -----------------------------------------------------------------------------
Ben Gamari's avatar
Ben Gamari committed
1023
 * Returns true if *c is a retainer.
1024 1025 1026 1027 1028 1029
 * In general the retainers are the objects that may be the roots of the
 * collection. Basically this roots represents programmers threads
 * (TSO) with their stack and thunks.
 *
 * In addition we mark all mutable objects as a retainers, the reason for
 * that decision is lost in time.
1030
 * -------------------------------------------------------------------------- */
Ben Gamari's avatar
Ben Gamari committed
1031
static INLINE bool
1032 1033 1034
isRetainer( StgClosure *c )
{
    switch (get_itbl(c)->type) {
1035 1036 1037 1038
        //
        //  True case
        //
        // TSOs MUST be retainers: they constitute the set of roots.
1039
    case TSO:
1040
    case STACK:
1041

1042
        // mutable objects
1043
    case MUT_PRIM:
1044 1045
    case MVAR_CLEAN:
    case MVAR_DIRTY:
1046
    case TVAR:
1047 1048
    case MUT_VAR_CLEAN:
    case MUT_VAR_DIRTY:
1049 1050
    case MUT_ARR_PTRS_CLEAN:
    case MUT_ARR_PTRS_DIRTY:
1051 1052 1053
    case SMALL_MUT_ARR_PTRS_CLEAN:
    case SMALL_MUT_ARR_PTRS_DIRTY:
    case BLOCKING_QUEUE:
1054

1055
        // thunks are retainers.
1056 1057 1058 1059 1060 1061 1062
    case THUNK:
    case THUNK_1_0:
    case THUNK_0_1:
    case THUNK_2_0:
    case THUNK_1_1:
    case THUNK_0_2:
    case THUNK_SELECTOR:
1063 1064
    case AP:
    case AP_STACK:
1065

1066
        // Static thunks, or CAFS, are obviously retainers.
1067 1068
    case THUNK_STATIC:

1069 1070
        // WEAK objects are roots; there is separate code in which traversing
        // begins from WEAK objects.
1071
    case WEAK:
Ben Gamari's avatar
Ben Gamari committed
1072
        return true;
1073

1074 1075 1076
        //
        // False case
        //
1077

1078
        // constructors
1079
    case CONSTR:
Simon Marlow's avatar
Simon Marlow committed
1080
    case CONSTR_NOCAF:
1081 1082 1083 1084 1085
    case CONSTR_1_0:
    case CONSTR_0_1:
    case CONSTR_2_0:
    case CONSTR_1_1:
    case CONSTR_0_2:
1086
        // functions
1087 1088 1089 1090 1091 1092
    case FUN:
    case FUN_1_0:
    case FUN_0_1:
    case FUN_2_0:
    case FUN_1_1:
    case FUN_0_2:
1093
        // partial applications
1094
    case PAP:
1095
        // indirection
Ian Lynagh's avatar
Ian Lynagh committed
1096 1097 1098 1099
    // IND_STATIC used to be an error, but at the moment it can happen
    // as isAlive doesn't look through IND_STATIC as it ignores static
    // closures. See trac #3956 for a program that hit this error.
    case IND_STATIC:
1100
    case BLACKHOLE:
1101
    case WHITEHOLE:
1102
        // static objects
1103
    case FUN_STATIC:
1104
        // misc
1105
    case PRIM:
1106 1107
    case BCO:
    case ARR_WORDS:
1108
    case COMPACT_NFDATA:
1109
        // STM
1110
    case TREC_CHUNK:
1111
        // immutable arrays
1112 1113 1114 1115
    case MUT_ARR_PTRS_FROZEN_CLEAN:
    case MUT_ARR_PTRS_FROZEN_DIRTY:
    case SMALL_MUT_ARR_PTRS_FROZEN_CLEAN:
    case SMALL_MUT_ARR_PTRS_FROZEN_DIRTY:
Ben Gamari's avatar
Ben Gamari committed
1116
        return false;
1117

1118 1119 1120 1121 1122
        //
        // Error case
        //
        // Stack objects are invalid because they are never treated as
        // legal objects during retainer profiling.
1123 1124
    case UPDATE_FRAME:
    case CATCH_FRAME:
1125 1126
    case CATCH_RETRY_FRAME:
    case CATCH_STM_FRAME:
1127
    case UNDERFLOW_FRAME:
1128
    case ATOMICALLY_FRAME:
1129 1130 1131 1132
    case STOP_FRAME:
    case RET_BCO:
    case RET_SMALL:
    case RET_BIG:
1133
    case RET_FUN:
1134
        // other cases
1135 1136 1137
    case IND:
    case INVALID_OBJECT:
    default:
1138
        barf("Invalid object in isRetainer(): %d", get_itbl(c)->type);
Ben Gamari's avatar
Ben Gamari committed
1139
        return false;
1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151
    }
}

/* -----------------------------------------------------------------------------
 *  Returns the retainer function value for the closure *c, i.e., R(*c).
 *  This function does NOT return the retainer(s) of *c.
 *  Invariants:
 *    *c must be a retainer.
 *  Note:
 *    Depending on the definition of this function, the maintenance of retainer
 *    sets can be made easier. If most retainer sets are likely to be created
 *    again across garbage collections, refreshAllRetainerSet() in
1152
 *    RetainerSet.c can simply do nothing.
1153 1154 1155 1156
 *    If this is not the case, we can free all the retainer sets and
 *    re-initialize the hash table.
 *    See refreshAllRetainerSet() in RetainerSet.c.
 * -------------------------------------------------------------------------- */
1157
static INLINE retainer
1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171
getRetainerFrom( StgClosure *c )
{
    ASSERT(isRetainer(c));

    return c->header.prof.ccs;
}

/* -----------------------------------------------------------------------------
 *  Associates the retainer set *s with the closure *c, that is, *s becomes
 *  the retainer set of *c.
 *  Invariants:
 *    c != NULL
 *    s != NULL
 * -------------------------------------------------------------------------- */
1172
static INLINE void
1173
associate( StgClosure *c, RetainerSet *s )
1174 1175 1176 1177 1178 1179
{
    // StgWord has the same size as pointers, so the following type
    // casting is okay.
    RSET(c) = (RetainerSet *)((StgWord)s | flip);
}

1180
/* -----------------------------------------------------------------------------
1181
   Call retainPushClosure for each of the closures covered by a large bitmap.
1182 1183 1184
   -------------------------------------------------------------------------- */

static void
1185
retain_large_bitmap (StgPtr p, StgLargeBitmap *large_bitmap, uint32_t size,
1186
                     StgClosure *c, retainer c_child_r)
1187
{
1188
    uint32_t i, b;
1189
    StgWord bitmap;
1190

1191 1192 1193
    b = 0;
    bitmap = large_bitmap->bitmap[b];
    for (i = 0; i < size; ) {
1194
        if ((bitmap & 1) == 0) {
1195
            retainPushClosure((StgClosure *)*p, c, c_child_r);
1196 1197 1198 1199 1200 1201 1202 1203 1204
        }
        i++;
        p++;
        if (i % BITS_IN(W_) == 0) {
            b++;
            bitmap = large_bitmap->bitmap[b];
        } else {
            bitmap = bitmap >> 1;
        }
1205 1206 1207
    }
}

1208
static INLINE StgPtr
1209
retain_small_bitmap (StgPtr p, uint32_t size, StgWord bitmap,
1210
                     StgClosure *c, retainer c_child_r)
1211 1212
{
    while (size > 0) {
1213
        if ((bitmap & 1) == 0) {
1214
            retainPushClosure((StgClosure *)*p, c, c_child_r);
1215 1216 1217 1218
        }
        p++;
        bitmap = bitmap >> 1;
        size--;
1219 1220 1221 1222
    }
    return p;
}

1223 1224 1225 1226 1227 1228
/* -----------------------------------------------------------------------------
 *  Process all the objects in the stack chunk from stackStart to stackEnd
 *  with *c and *c_child_r being their parent and their most recent retainer,
 *  respectively. Treat stackOptionalFun as another child of *c if it is
 *  not NULL.
 *  Invariants:
1229
 *    *c is one of the following: TSO, AP_STACK.
1230 1231 1232 1233 1234
 *    If *c is TSO, c == c_child_r.
 *    stackStart < stackEnd.
 *    RSET(c) and RSET(c_child_r) are valid, i.e., their
 *    interpretation conforms to the current value of flip (even when they
 *    are interpreted to be NULL).
1235
 *    If *c is TSO, its state is not ThreadComplete,or ThreadKilled,
1236
 *    which means that its stack is ready to process.
1237 1238
 *  Note:
 *    This code was almost plagiarzied from GC.c! For each pointer,
1239
 *    retainPushClosure() is invoked instead of evacuate().
1240 1241
 * -------------------------------------------------------------------------- */
static void
1242
retainStack( StgClosure *c, retainer c_child_r,
1243
             StgPtr stackStart, StgPtr stackEnd )
1244 1245
{
    stackElement *oldStackBoundary;
1246
    StgPtr p;
1247
    const StgRetInfoTable *info;
1248
    StgWord bitmap;
1249
    uint32_t size;
1250

Ben Gamari's avatar
Ben Gamari committed
1251
#if defined(DEBUG_RETAINER)
1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264
    cStackSize++;
    if (cStackSize > maxCStackSize) maxCStackSize = cStackSize;
#endif

    /*
      Each invocation of retainStack() creates a new virtual
      stack. Since all such stacks share a single common stack, we
      record the current currentStackBoundary, which will be restored
      at the exit.
    */
    oldStackBoundary = currentStackBoundary;
    currentStackBoundary = stackTop;

Ben Gamari's avatar
Ben Gamari committed
1265
#if defined(DEBUG_RETAINER)
1266
    // debugBelch("retainStack() called: oldStackBoundary = 0x%x, currentStackBoundary = 0x%x\n", oldStackBoundary, currentStackBoundary);
1267 1268
#endif

1269 1270
    ASSERT(get_itbl(c)->type == STACK);

1271 1272
    p = stackStart;
    while (p < stackEnd) {
1273
        info = get_ret_itbl((StgClosure *)p);
1274

1275
        switch(info->i.type) {
1276

1277
        case UPDATE_FRAME:
1278
            retainPushClosure(((StgUpdateFrame *)p)->updatee, c, c_child_r);
1279 1280
            p += sizeofW(StgUpdateFrame);
            continue;
1281

1282 1283
        case UNDERFLOW_FRAME:
        case STOP_FRAME:
1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294
        case CATCH_FRAME:
        case CATCH_STM_FRAME:
        case CATCH_RETRY_FRAME:
        case ATOMICALLY_FRAME:
        case RET_SMALL:
            bitmap = BITMAP_BITS(info->i.layout.bitmap);
            size   = BITMAP_SIZE(info->i.layout.bitmap);
            p++;
            p = retain_small_bitmap(p, size, bitmap, c, c_child_r);

        follow_srt:
1295
            if (info->i.srt) {
1296
                retainPushClosure(GET_SRT(info), c, c_child_r);
1297
            }
1298 1299 1300 1301 1302 1303
            continue;

        case RET_BCO: {
            StgBCO *bco;

            p++;
1304
            retainPushClosure((StgClosure*)*p, c, c_child_r);
1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321
            bco = (StgBCO *)*p;
            p++;
            size = BCO_BITMAP_SIZE(bco);
            retain_large_bitmap(p, BCO_BITMAP(bco), size, c, c_child_r);
            p += size;
            continue;
        }

            // large bitmap (> 32 entries, or > 64 on a 64-bit machine)
        case RET_BIG:
            size = GET_LARGE_BITMAP(&info->i)->size;
            p++;
            retain_large_bitmap(p, GET_LARGE_BITMAP(&info->i),
                                size, c, c_child_r);
            p += size;
            // and don't forget to follow the SRT
            goto follow_srt;
1322

1323
        case RET_FUN: {
1324
            StgRetFun *ret_fun = (StgRetFun *)p;
1325
            const StgFunInfoTable *fun_info;