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

#ifdef PROFILING

12 13 14 15 16 17 18
// Turn off inlining when debugging - it obfuscates things
#ifdef DEBUG
#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"
33
#include "Stable.h" /* markStableTables */
Simon Marlow's avatar
Simon Marlow committed
34
#include "sm/Storage.h" // for END_OF_STATIC_LIST
35 36 37 38 39 40 41 42 43 44 45 46 47 48

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

49
static uint32_t retainerGeneration;  // generation
50

51 52 53
static uint32_t numObjectVisited;    // total number of objects visited
static uint32_t timesAnyObjectVisited;  // number of times any objects are
                                        // visited
54 55 56 57 58 59 60 61 62

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

63
StgWord flip = 0;     // flip bit
64 65 66 67 68
                      // must be 0 if DEBUG_RETAINER is on (for static closures)

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

69
static void retainStack(StgClosure *, retainer, StgPtr, StgPtr);
70
static void retainClosure(StgClosure *, StgClosure *, retainer);
71 72 73 74 75 76 77 78 79 80 81 82
#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
 */
83
static uint32_t cStackSize, maxCStackSize;
84

85
static uint32_t sumOfNewCost;        // sum of the cost of each object, computed
86
                                // when the object is first visited
87
static uint32_t sumOfNewCostExtra;   // for those objects not visited during
88
                                // retainer profiling, e.g., MUT_VAR
89
static uint32_t costArray[N_CLOSURE_TYPES];
90

91
uint32_t sumOfCostLinear;            // sum of the costs of all object, computed
92 93
                                // when linearly traversing the heap after
                                // retainer profiling
94
uint32_t costArrayLinear[N_CLOSURE_TYPES];
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
#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,
111
    posTypeLargeSRT,
112 113 114 115 116 117 118 119
} nextPosType;

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

    // layout.payload
    struct {
120 121 122
        // See StgClosureInfo in InfoTables.h
        StgHalfWord pos;
        StgHalfWord ptrs;
123
        StgPtr payload;
124 125 126 127
    } ptrs;

    // SRT
    struct {
128 129
        StgClosure **srt;
        StgWord    srt_bitmap;
130
    } srt;
131 132 133

    // Large SRT
    struct {
134 135
        StgLargeSRT *srt;
        StgWord offset;
136
    } large_srt;
137

138 139 140 141 142 143 144 145 146
} nextPos;

typedef struct {
    nextPosType type;
    nextPos next;
} stackPos;

typedef struct {
    StgClosure *c;
147
    retainer c_child_r;
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
    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.
 */
sof's avatar
sof committed
168
static bdescr *firstStack = NULL;
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
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.
 * -------------------------------------------------------------------------- */
204
static INLINE void
205 206 207 208 209 210 211 212 213 214 215 216 217 218
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.
 * -------------------------------------------------------------------------- */
219
static INLINE void
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
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) {
236
        freeChain(firstStack);
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
    }

    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.
 * -------------------------------------------------------------------------- */
261
static INLINE rtsBool
262 263 264 265 266
isEmptyRetainerStack( void )
{
    return (firstStack == currentStack) && stackTop == stackLimit;
}

sof's avatar
sof committed
267 268 269
/* -----------------------------------------------------------------------------
 * Returns size of stack
 * -------------------------------------------------------------------------- */
270
#ifdef DEBUG
271
W_
272
retainerStackBlocks( void )
sof's avatar
sof committed
273 274
{
    bdescr* bd;
275
    W_ res = 0;
sof's avatar
sof committed
276

277
    for (bd = firstStack; bd != NULL; bd = bd->link)
sof's avatar
sof committed
278 279 280 281
      res += bd->blocks;

    return res;
}
282
#endif
sof's avatar
sof committed
283

284 285 286 287
/* -----------------------------------------------------------------------------
 * Returns rtsTrue if stackTop is at the stack boundary of the current stack,
 * i.e., if the current stack chunk is empty.
 * -------------------------------------------------------------------------- */
288
static INLINE rtsBool
289 290 291 292 293 294 295 296 297 298
isOnBoundary( void )
{
    return stackTop == currentStackBoundary;
}

/* -----------------------------------------------------------------------------
 * Initializes *info from ptrs and payload.
 * Invariants:
 *   payload[] begins with ptrs pointers followed by non-pointers.
 * -------------------------------------------------------------------------- */
299
static INLINE void
300
init_ptrs( stackPos *info, uint32_t ptrs, StgPtr payload )
301 302 303 304 305 306 307 308 309 310
{
    info->type              = posTypePtrs;
    info->next.ptrs.pos     = 0;
    info->next.ptrs.ptrs    = ptrs;
    info->next.ptrs.payload = payload;
}

/* -----------------------------------------------------------------------------
 * Find the next object from *info.
 * -------------------------------------------------------------------------- */
311
static INLINE StgClosure *
312 313 314
find_ptrs( stackPos *info )
{
    if (info->next.ptrs.pos < info->next.ptrs.ptrs) {
315
        return (StgClosure *)info->next.ptrs.payload[info->next.ptrs.pos++];
316
    } else {
317
        return NULL;
318 319 320 321 322 323
    }
}

/* -----------------------------------------------------------------------------
 *  Initializes *info from SRT information stored in *infoTable.
 * -------------------------------------------------------------------------- */
324
static INLINE void
325
init_srt_fun( stackPos *info, StgFunInfoTable *infoTable )
326
{
327
    if (infoTable->i.srt_bitmap == (StgHalfWord)(-1)) {
328 329 330
        info->type = posTypeLargeSRT;
        info->next.large_srt.srt = (StgLargeSRT *)GET_FUN_SRT(infoTable);
        info->next.large_srt.offset = 0;
331
    } else {
332 333 334
        info->type = posTypeSRT;
        info->next.srt.srt = (StgClosure **)GET_FUN_SRT(infoTable);
        info->next.srt.srt_bitmap = infoTable->i.srt_bitmap;
335
    }
336 337
}

338
static INLINE void
339 340
init_srt_thunk( stackPos *info, StgThunkInfoTable *infoTable )
{
341
    if (infoTable->i.srt_bitmap == (StgHalfWord)(-1)) {
342 343 344
        info->type = posTypeLargeSRT;
        info->next.large_srt.srt = (StgLargeSRT *)GET_SRT(infoTable);
        info->next.large_srt.offset = 0;
345
    } else {
346 347 348
        info->type = posTypeSRT;
        info->next.srt.srt = (StgClosure **)GET_SRT(infoTable);
        info->next.srt.srt_bitmap = infoTable->i.srt_bitmap;
349
    }
350 351 352 353 354
}

/* -----------------------------------------------------------------------------
 * Find the next object from *info.
 * -------------------------------------------------------------------------- */
355
static INLINE StgClosure *
356 357 358
find_srt( stackPos *info )
{
    StgClosure *c;
359
    StgWord bitmap;
360

361
    if (info->type == posTypeSRT) {
362 363 364 365
        // Small SRT bitmap
        bitmap = info->next.srt.srt_bitmap;
        while (bitmap != 0) {
            if ((bitmap & 1) != 0) {
366
#if defined(COMPILING_WINDOWS_DLL)
367 368 369 370
                if ((unsigned long)(*(info->next.srt.srt)) & 0x1)
                    c = (* (StgClosure **)((unsigned long)*(info->next.srt.srt)) & ~0x1);
                else
                    c = *(info->next.srt.srt);
371
#else
372
                c = *(info->next.srt.srt);
373
#endif
374 375 376 377 378 379 380 381 382 383
                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;
384 385
    }
    else {
386
        // Large SRT bitmap
387
        uint32_t i = info->next.large_srt.offset;
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
        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;
410 411 412 413 414 415 416 417 418 419 420 421 422 423
    }
}

/* -----------------------------------------------------------------------------
 *  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.
424
 *     *c is not any of TSO, AP, PAP, AP_STACK, which means that
425 426 427
 *        there cannot be any stack objects.
 *  Note: SRTs are considered to  be children as well.
 * -------------------------------------------------------------------------- */
428
static INLINE void
429
push( StgClosure *c, retainer c_child_r, StgClosure **first_child )
430 431 432 433 434
{
    stackElement se;
    bdescr *nbd;      // Next Block Descriptor

#ifdef DEBUG_RETAINER
435
    // debugBelch("push(): stackTop = 0x%x, currentStackBoundary = 0x%x\n", stackTop, currentStackBoundary);
436 437 438
#endif

    ASSERT(get_itbl(c)->type != TSO);
439
    ASSERT(get_itbl(c)->type != AP_STACK);
440 441 442 443 444 445 446 447 448 449

    //
    // fill in se
    //

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

    // fill in se.info
    switch (get_itbl(c)->type) {
450
        // no child, no SRT
451 452 453
    case CONSTR_0_1:
    case CONSTR_0_2:
    case ARR_WORDS:
454 455
        *first_child = NULL;
        return;
456

457
        // one child (fixed), no SRT
458 459
    case MUT_VAR_CLEAN:
    case MUT_VAR_DIRTY:
460 461
        *first_child = ((StgMutVar *)c)->var;
        return;
462
    case THUNK_SELECTOR:
463 464
        *first_child = ((StgSelector *)c)->selectee;
        return;
465
    case BLACKHOLE:
466 467
        *first_child = ((StgInd *)c)->indirectee;
        return;
468 469
    case CONSTR_1_0:
    case CONSTR_1_1:
470 471
        *first_child = c->payload[0];
        return;
472

473 474 475
        // 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;
476

477 478
        // two children (fixed), no SRT
        // need to push a stackElement, but nothing to store in se.info
479
    case CONSTR_2_0:
480 481 482 483
        *first_child = c->payload[0];         // return the first pointer
        // se.info.type = posTypeStep;
        // se.info.next.step = 2;            // 2 = second
        break;
484

485 486
        // three children (fixed), no SRT
        // need to push a stackElement
487 488
    case MVAR_CLEAN:
    case MVAR_DIRTY:
489 490 491 492 493 494 495 496
        // 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
497
    case WEAK:
498 499 500 501
        *first_child = ((StgWeak *)c)->key;
        // se.info.type = posTypeStep;
        se.info.next.step = 2;
        break;
502

503
        // layout.payload.ptrs, no SRT
504
    case TVAR:
505
    case CONSTR:
506
    case PRIM:
507
    case MUT_PRIM:
508 509
    case BCO:
    case CONSTR_STATIC:
510 511 512 513 514 515 516 517
        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
518 519
    case MUT_ARR_PTRS_CLEAN:
    case MUT_ARR_PTRS_DIRTY:
520
    case MUT_ARR_PTRS_FROZEN:
521
    case MUT_ARR_PTRS_FROZEN0:
522 523 524 525 526 527 528 529
        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
530 531 532 533
    case SMALL_MUT_ARR_PTRS_CLEAN:
    case SMALL_MUT_ARR_PTRS_DIRTY:
    case SMALL_MUT_ARR_PTRS_FROZEN:
    case SMALL_MUT_ARR_PTRS_FROZEN0:
534 535 536 537 538 539
        init_ptrs(&se.info, ((StgSmallMutArrPtrs *)c)->ptrs,
                  (StgPtr)(((StgSmallMutArrPtrs *)c)->payload));
        *first_child = find_ptrs(&se.info);
        if (*first_child == NULL)
            return;
        break;
540

541 542 543
    // layout.payload.ptrs, SRT
    case FUN:           // *c is a heap object.
    case FUN_2_0:
544 545 546 547 548 549
        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;
550

551 552
    case THUNK:
    case THUNK_2_0:
553 554 555 556 557 558 559 560 561
        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
562 563
    case FUN_1_0:
    case FUN_1_1:
564 565 566 567
        *first_child = c->payload[0];
        ASSERT(*first_child != NULL);
        init_srt_fun(&se.info, get_fun_itbl(c));
        break;
568

569 570
    case THUNK_1_0:
    case THUNK_1_1:
571 572 573 574
        *first_child = ((StgThunk *)c)->payload[0];
        ASSERT(*first_child != NULL);
        init_srt_thunk(&se.info, get_thunk_itbl(c));
        break;
575 576

    case FUN_STATIC:      // *c is a heap object.
577
        ASSERT(get_itbl(c)->srt_bitmap != 0);
578 579
    case FUN_0_1:
    case FUN_0_2:
580 581
    fun_srt_only:
        init_srt_fun(&se.info, get_fun_itbl(c));
582 583 584 585
        *first_child = find_srt(&se.info);
        if (*first_child == NULL)
            return;     // no child
        break;
586 587 588

    // SRT only
    case THUNK_STATIC:
589
        ASSERT(get_itbl(c)->srt_bitmap != 0);
590 591
    case THUNK_0_1:
    case THUNK_0_2:
592 593
    thunk_srt_only:
        init_srt_thunk(&se.info, get_thunk_itbl(c));
594 595 596 597 598
        *first_child = find_srt(&se.info);
        if (*first_child == NULL)
            return;     // no child
        break;

599
    case TREC_CHUNK:
600 601 602
        *first_child = (StgClosure *)((StgTRecChunk *)c)->prev_chunk;
        se.info.next.step = 0;  // entry no.
        break;
603

604
        // cannot appear
605
    case PAP:
606 607
    case AP:
    case AP_STACK:
608
    case TSO:
609
    case STACK:
610 611
    case IND_STATIC:
    case CONSTR_NOCAF_STATIC:
612
        // stack objects
613 614
    case UPDATE_FRAME:
    case CATCH_FRAME:
615
    case UNDERFLOW_FRAME:
616 617 618 619
    case STOP_FRAME:
    case RET_BCO:
    case RET_SMALL:
    case RET_BIG:
620
        // invalid objects
621 622 623
    case IND:
    case INVALID_OBJECT:
    default:
624 625
        barf("Invalid object *c in push()");
        return;
626 627 628 629
    }

    if (stackTop - 1 < stackBottom) {
#ifdef DEBUG_RETAINER
630
        // debugBelch("push() to the next stack.\n");
631
#endif
632 633 634 635 636 637 638 639 640 641 642 643 644
        // 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);
645 646 647 648 649 650 651 652
    }

    // 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.
653 654 655 656 657 658

    // 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?
659 660 661 662 663 664
    *stackTop = se;

#ifdef DEBUG_RETAINER
    stackSize++;
    if (stackSize > maxStackSize) maxStackSize = stackSize;
    // ASSERT(stackSize >= 0);
665
    // debugBelch("stackSize = %d\n", stackSize);
666 667 668 669 670 671 672 673 674 675 676 677 678
#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
679
 *    INLINE function (for the sake of execution speed).  popOffReal()
680 681 682 683 684 685 686 687
 *    is called only within popOff() and nowhere else.
 * -------------------------------------------------------------------------- */
static void
popOffReal(void)
{
    bdescr *pbd;    // Previous Block Descriptor

#ifdef DEBUG_RETAINER
688
    // debugBelch("pop() to the previous stack.\n");
689 690 691 692 693 694
#endif

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

    if (firstStack == currentStack) {
695 696 697
        // The stack is completely empty.
        stackTop++;
        ASSERT(stackTop == stackLimit);
698
#ifdef DEBUG_RETAINER
699 700 701 702 703 704
        stackSize--;
        if (stackSize > maxStackSize) maxStackSize = stackSize;
        /*
          ASSERT(stackSize >= 0);
          debugBelch("stackSize = %d\n", stackSize);
        */
705
#endif
706
        return;
707 708 709 710 711 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);

#ifdef DEBUG_RETAINER
    stackSize--;
    if (stackSize > maxStackSize) maxStackSize = stackSize;
    /*
      ASSERT(stackSize >= 0);
724
      debugBelch("stackSize = %d\n", stackSize);
725 726 727 728
    */
#endif
}

729
static INLINE void
730 731
popOff(void) {
#ifdef DEBUG_RETAINER
732
    // debugBelch("\tpopOff(): stackTop = 0x%x, currentStackBoundary = 0x%x\n", stackTop, currentStackBoundary);
733 734 735 736 737 738 739
#endif

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

    // <= (instead of <) is wrong!
    if (stackTop + 1 < stackLimit) {
740
        stackTop++;
741
#ifdef DEBUG_RETAINER
742 743 744 745 746 747
        stackSize--;
        if (stackSize > maxStackSize) maxStackSize = stackSize;
        /*
          ASSERT(stackSize >= 0);
          debugBelch("stackSize = %d\n", stackSize);
        */
748
#endif
749
        return;
750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770
    }

    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.
 * -------------------------------------------------------------------------- */
771
static INLINE void
772
pop( StgClosure **c, StgClosure **cp, retainer *r )
773 774 775 776
{
    stackElement *se;

#ifdef DEBUG_RETAINER
777
    // debugBelch("pop(): stackTop = 0x%x, currentStackBoundary = 0x%x\n", stackTop, currentStackBoundary);
778 779 780
#endif

    do {
781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799
        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
800 801
        case MVAR_CLEAN:
        case MVAR_DIRTY:
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
            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;
835 836
            uint32_t entry_no = se->info.next.step >> 2;
            uint32_t field_no = se->info.next.step & 3;
837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854
            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;
        }
855

856 857
        case TVAR:
        case CONSTR:
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
        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 CONSTR_1_1:
            // cannot appear
        case PAP:
        case AP:
        case AP_STACK:
        case TSO:
938 939
        case STACK:
        case IND_STATIC:
940 941
        case CONSTR_NOCAF_STATIC:
            // stack objects
942
        case UPDATE_FRAME:
943
        case CATCH_FRAME:
944 945
        case UNDERFLOW_FRAME:
        case STOP_FRAME:
946 947 948 949 950 951 952 953 954 955
        case RET_BCO:
        case RET_SMALL:
        case RET_BIG:
            // invalid objects
        case IND:
        case INVALID_OBJECT:
        default:
            barf("Invalid object *c in pop()");
            return;
        }
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
    } 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.
 * -------------------------------------------------------------------------- */
995
static INLINE void
996 997 998
maybeInitRetainerSet( StgClosure *c )
{
    if (!isRetainerSetFieldValid(c)) {
999
        setRetainerSetToNull(c);
1000 1001 1002 1003 1004 1005
    }
}

/* -----------------------------------------------------------------------------
 * Returns rtsTrue if *c is a retainer.
 * -------------------------------------------------------------------------- */
1006
static INLINE rtsBool
1007 1008 1009
isRetainer( StgClosure *c )
{
    switch (get_itbl(c)->type) {
1010 1011 1012 1013
        //
        //  True case
        //
        // TSOs MUST be retainers: they constitute the set of roots.
1014
    case TSO:
1015
    case STACK:
1016

1017
        // mutable objects
1018
    case MUT_PRIM:
1019 1020
    case MVAR_CLEAN:
    case MVAR_DIRTY:
1021
    case TVAR:
1022 1023
    case MUT_VAR_CLEAN:
    case MUT_VAR_DIRTY:
1024 1025
    case MUT_ARR_PTRS_CLEAN:
    case MUT_ARR_PTRS_DIRTY:
1026

1027
        // thunks are retainers.
1028 1029 1030 1031 1032 1033 1034
    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:
1035 1036
    case AP:
    case AP_STACK:
1037

1038
        // Static thunks, or CAFS, are obviously retainers.
1039 1040
    case THUNK_STATIC:

1041 1042
        // WEAK objects are roots; there is separate code in which traversing
        // begins from WEAK objects.
1043
    case WEAK:
1044
        return rtsTrue;
1045

1046 1047 1048
        //
        // False case
        //
1049

1050
        // constructors
1051 1052 1053 1054 1055 1056
    case CONSTR:
    case CONSTR_1_0:
    case CONSTR_0_1:
    case CONSTR_2_0:
    case CONSTR_1_1:
    case CONSTR_0_2:
1057
        // functions
1058 1059 1060 1061 1062 1063
    case FUN:
    case FUN_1_0:
    case FUN_0_1:
    case FUN_2_0:
    case FUN_1_1:
    case FUN_0_2:
1064
        // partial applications
1065
    case PAP:
1066
        // indirection
Ian Lynagh's avatar
Ian Lynagh committed
1067 1068 1069 1070
    // 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:
1071
    case BLACKHOLE:
1072
        // static objects
1073 1074
    case CONSTR_STATIC:
    case FUN_STATIC:
1075
        // misc
1076
    case PRIM:
1077 1078
    case BCO:
    case ARR_WORDS:
1079
        // STM
1080
    case TREC_CHUNK:
1081 1082 1083
        // immutable arrays
    case MUT_ARR_PTRS_FROZEN:
    case MUT_ARR_PTRS_FROZEN0:
1084
        return rtsFalse;
1085

1086 1087 1088 1089 1090
        //
        // Error case
        //
        // CONSTR_NOCAF_STATIC
        // cannot be *c, *cp, *r in the retainer profiling loop.
1091
    case CONSTR_NOCAF_STATIC:
1092 1093
        // Stack objects are invalid because they are never treated as
        // legal objects during retainer profiling.
1094 1095
    case UPDATE_FRAME:
    case CATCH_FRAME:
1096
    case UNDERFLOW_FRAME:
1097 1098 1099 1100
    case STOP_FRAME:
    case RET_BCO:
    case RET_SMALL:
    case RET_BIG:
1101
        // other cases
1102 1103 1104
    case IND:
    case INVALID_OBJECT:
    default:
1105 1106
        barf("Invalid object in isRetainer(): %d", get_itbl(c)->type);
        return rtsFalse;
1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118
    }
}

/* -----------------------------------------------------------------------------
 *  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
1119
 *    RetainerSet.c can simply do nothing.
1120 1121 1122 1123
 *    If this is not the case, we can free all the retainer sets and
 *    re-initialize the hash table.
 *    See refreshAllRetainerSet() in RetainerSet.c.
 * -------------------------------------------------------------------------- */
1124
static INLINE retainer
1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147
getRetainerFrom( StgClosure *c )
{
    ASSERT(isRetainer(c));

#if defined(RETAINER_SCHEME_INFO)
    // Retainer scheme 1: retainer = info table
    return get_itbl(c);
#elif defined(RETAINER_SCHEME_CCS)
    // Retainer scheme 2: retainer = cost centre stack
    return c->header.prof.ccs;
#elif defined(RETAINER_SCHEME_CC)
    // Retainer scheme 3: retainer = cost centre
    return c->header.prof.ccs->cc;
#endif
}

/* -----------------------------------------------------------------------------
 *  Associates the retainer set *s with the closure *c, that is, *s becomes
 *  the retainer set of *c.
 *  Invariants:
 *    c != NULL
 *    s != NULL
 * -------------------------------------------------------------------------- */
1148
static INLINE void
1149
associate( StgClosure *c, RetainerSet *s )
1150 1151 1152 1153 1154 1155
{
    // StgWord has the same size as pointers, so the following type
    // casting is okay.
    RSET(c) = (RetainerSet *)((StgWord)s | flip);
}

1156 1157 1158 1159 1160
/* -----------------------------------------------------------------------------
   Call retainClosure for each of the closures covered by a large bitmap.
   -------------------------------------------------------------------------- */

static void
1161
retain_large_bitmap (StgPtr p, StgLargeBitmap *large_bitmap, uint32_t size,
1162
                     StgClosure *c, retainer c_child_r)
1163
{
1164
    uint32_t i, b;
1165
    StgWord bitmap;
1166

1167 1168 1169
    b = 0;
    bitmap = large_bitmap->bitmap[b];
    for (i = 0; i < size; ) {
1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180
        if ((bitmap & 1) == 0) {
            retainClosure((StgClosure *)*p, c, c_child_r);
        }
        i++;
        p++;
        if (i % BITS_IN(W_) == 0) {
            b++;
            bitmap = large_bitmap->bitmap[b];
        } else {
            bitmap = bitmap >> 1;
        }
1181 1182 1183
    }
}

1184
static INLINE StgPtr
1185
retain_small_bitmap (StgPtr p, uint32_t size, StgWord bitmap,
1186
                     StgClosure *c, retainer c_child_r)
1187 1188
{
    while (size > 0) {
1189 1190 1191 1192 1193 1194
        if ((bitmap & 1) == 0) {
            retainClosure((StgClosure *)*p, c, c_child_r);
        }
        p++;
        bitmap = bitmap >> 1;
        size--;
1195 1196 1197 1198
    }
    return p;
}

1199 1200 1201 1202 1203 1204 1205
/* -----------------------------------------------------------------------------
 * Call retainClosure for each of the closures in an SRT.
 * ------------------------------------------------------------------------- */

static void
retain_large_srt_bitmap (StgLargeSRT *srt, StgClosure *c, retainer c_child_r)
{
1206
    uint32_t i, b, size;
1207 1208
    StgWord bitmap;
    StgClosure **p;
1209

1210 1211 1212 1213 1214
    b = 0;
    p = (StgClosure **)srt->srt;
    size   = srt->l.size;
    bitmap = srt->l.bitmap[b];
    for (i = 0; i < size; ) {
1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225
        if ((bitmap & 1) != 0) {
            retainClosure((StgClosure *)*p, c, c_child_r);
        }
        i++;
        p++;
        if (i % BITS_IN(W_) == 0) {
            b++;
            bitmap = srt->l.bitmap[b];
        } else {
            bitmap = bitmap >> 1;
        }
1226 1227 1228 1229
    }
}

static INLINE void
1230 1231
retainSRT (StgClosure **srt, uint32_t srt_bitmap, StgClosure *c,
            retainer c_child_r)
1232
{
1233
  uint32_t bitmap;
1234 1235 1236 1237 1238
  StgClosure **p;

  bitmap = srt_bitmap;
  p = srt;

1239
  if (bitmap == (StgHalfWord)(-1)) {
1240 1241 1242 1243 1244 1245
      retain_large_srt_bitmap( (StgLargeSRT *)srt, c, c_child_r );
      return;
  }

  while (bitmap != 0) {
      if ((bitmap & 1) != 0) {
1246
#if defined(COMPILING_WINDOWS_DLL)
1247 1248 1249 1250 1251 1252
          if ( (unsigned long)(*srt) & 0x1 ) {
              retainClosure(* (StgClosure**) ((unsigned long) (*srt) & ~0x1),
                            c, c_child_r);
          } else {
              retainClosure(*srt,c,c_child_r);
          }
1253
#else
1254
          retainClosure(*srt,c,c_child_r);
1255 1256 1257 1258 1259 1260 1261
#endif
      }
      p++;
      bitmap = bitmap >> 1;
  }
}

1262 1263 1264 1265 1266 1267
/* -----------------------------------------------------------------------------
 *  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:
1268
 *    *c is one of the following: TSO, AP_STACK.
1269 1270 1271 1272 1273
 *    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).
1274
 *    If *c is TSO, its state is not ThreadComplete,or ThreadKilled,
1275
 *    which means that its stack is ready to process.
1276 1277 1278 1279 1280
 *  Note:
 *    This code was almost plagiarzied from GC.c! For each pointer,
 *    retainClosure() is invoked instead of evacuate().
 * -------------------------------------------------------------------------- */
static void
1281
retainStack( StgClosure *c, retainer c_child_r,
1282
             StgPtr stackStart, StgPtr stackEnd )
1283 1284
{
    stackElement *oldStackBoundary;
1285 1286
    StgPtr p;
    StgRetInfoTable *info;
1287
    StgWord bitmap;
1288
    uint32_t size;
1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304

#ifdef DEBUG_RETAINER
    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;

#ifdef DEBUG_RETAINER
1305
    // debugBelch("retainStack() called: oldStackBoundary = 0x%x, currentStackBoundary = 0x%x\n", oldStackBoundary, currentStackBoundary);
1306 1307
#endif

1308 1309
    ASSERT(get_itbl(c)->type == STACK);

1310 1311
    p = stackStart;
    while (p < stackEnd) {
1312
        info = get_ret_itbl((StgClosure *)p);
1313

1314
        switch(info->i.type) {
1315

1316 1317 1318 1319
        case UPDATE_FRAME:
            retainClosure(((StgUpdateFrame *)p)->updatee, c, c_child_r);
            p += sizeofW(StgUpdateFrame);
            continue;
1320

1321 1322
        case UNDERFLOW_FRAME:
        case STOP_FRAME:
1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358
        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:
            retainSRT((StgClosure **)GET_SRT(info), info->i.srt_bitmap, c, c_child_r);
            continue;

        case RET_BCO: {
            StgBCO *bco;

            p++;
            retainClosure((StgClosure *)*p, c, c_child_r);
            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;
1359

1360
        case RET_FUN: {
1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392
            StgRetFun *ret_fun = (StgRetFun *)p;
            StgFunInfoTable *fun_info;

            retainClosure(ret_fun->fun, c, c_child_r);
            fun_info = get_fun_itbl(UNTAG_CLOSURE(ret_fun->fun));

            p = (P_)&ret_fun->payload;
            switch (fun_info->f.fun_type) {
            case ARG_GEN:
                bitmap = BITMAP_BITS(fun_info->f.b.bitmap);
                size = BITMAP_SIZE(fun_info->f.b.bitmap);
                p = retain_small_bitmap(p, size, bitmap, c, c_child_r);
                break;
            case ARG_GEN_BIG:
                size = GET_FUN_LARGE_BITMAP(fun_info)->size;
                retain_large_bitmap(p, GET_FUN_LARGE_BITMAP(fun_info),
                                    size, c, c_child_r);
                p += size;
                break;
            default:
                bitmap = BITMAP_BITS(stg_arg_bitmaps[fun_info->f.fun_type]);
                size = BITMAP_SIZE(stg_arg_bitmaps[fun_info->f.fun_type]);
                p = retain_small_bitmap(p, size, bitmap, c, c_child_r);
                break;
            }
            goto follow_srt;
        }

        default:
            barf("Invalid object found in retainStack(): %d",
                 (int)(info->i.type));
        }
1393 1394 1395 1396 1397
    }

    // restore currentStackBoundary
    currentStackBoundary = oldStackBoundary;
#ifdef DEBUG_RETAINER
1398
    // debugBelch("retainStack() finished: currentStackBoundary = 0x%x\n", currentStackBoundary);
1399 1400 1401 1402 1403 1404 1405
#endif

#ifdef DEBUG_RETAINER
    cStackSize--;
#endif
}

1406 1407 1408 1409
/* ----------------------------------------------------------------------------
 * Call retainClosure for each of the children of a PAP/AP
 * ------------------------------------------------------------------------- */

1410
static INLINE StgPtr
Simon Marlow's avatar
Simon Marlow committed
1411
retain_PAP_payload (StgClosure *pap,    /* NOT tagged */
1412
                    retainer c_child_r, /* NOT tagged */
Simon Marlow's avatar
Simon Marlow committed
1413
                    StgClosure *fun,    /* tagged */
1414
                    StgClosure** payload, StgWord n_args)
1415 1416
{
    StgPtr p;
1417
    StgWord bitmap;
1418 1419
    StgFunInfoTable *fun_info;

1420
    retainClosure(fun, pap, c_child_r);
Simon Marlow's avatar
Simon Marlow committed
1421
    fun = UNTAG_CLOSURE(fun);
1422
    fun_info = get_fun_itbl(fun);
1423 1424
    ASSERT(fun_info->i.type != PAP);

1425
    p = (StgPtr)payload;
1426

1427
    switch (fun_info->f.fun_type) {
1428
    case ARG_GEN:
1429 1430 1431 1432
        bitmap = BITMAP_BITS(fun_info->f.b.bitmap);
        p = retain_small_bitmap(p, n_args, bitmap,
                                pap, c_child_r);
        break;
1433
    case ARG_GEN_BIG:
1434 1435 1436 1437
        retain_large_bitmap(p, GET_FUN_LARGE_BITMAP(fun_info),
                            n_args, pap, c_child_r);
        p += n_args;
        break;
1438
    case ARG_BCO:
1439 1440 1441 1442
        retain_large_bitmap((StgPtr)payload, BCO_BITMAP(fun),
                            n_args, pap, c_child_r);
        p += n_args;
        break;
1443
    default:
1444 1445 1446
        bitmap = BITMAP_BITS(stg_arg_bitmaps[fun_info->f.fun_type]);
        p = retain_small_bitmap(p, n_args, bitmap, pap, c_child_r);
        break;
1447 1448 1449 1450
    }
    return p;
}

1451 1452 1453 1454 1455 1456 1457