static void arena_commit_test(mps_arena_t arena)
{
  mps_pool_t pool;
  size_t committed;
  size_t reserved;
  size_t limit;
  void *p;
  mps_res_t res;

  committed = mps_arena_committed(arena);
  reserved = mps_arena_reserved(arena);
  cdie(reserved >= committed, "reserved < committed");
  die(mps_pool_create(&pool, arena, mps_class_mv(), 0x1000, 1024, 16384),
      "commit pool create");
  limit = mps_arena_commit_limit(arena);
  die(mps_arena_commit_limit_set(arena, committed), "commit_limit_set before");
  do {
    res = mps_alloc(&p, pool, FILLER_OBJECT_SIZE);
  } while (res == MPS_RES_OK);
  die_expect(res, MPS_RES_COMMIT_LIMIT, "Commit limit allocation");
  die(mps_arena_commit_limit_set(arena, limit), "commit_limit_set after");
  res = mps_alloc(&p, pool, FILLER_OBJECT_SIZE);
  die_expect(res, MPS_RES_OK, "Allocation failed after raising commit_limit");
  mps_pool_destroy(pool);
}
Esempio n. 2
0
/** Shared release method of the commit extension low-memory handlers. */
static Bool mm_commit_release(low_mem_handler_t *handler,
                              corecontext_t *context, low_mem_offer_t *offer)
{
  mm_commit_extension_t *extension = (mm_commit_extension_t *)handler;
  size_t limit;
  mps_res_t res;

  HQASSERT(handler != NULL, "No handler");
  HQASSERT(context != NULL, "No context");
  HQASSERT(offer != NULL, "No offer");
  HQASSERT(offer->next == NULL, "Multiple offers");
  UNUSED_PARAM(corecontext_t*, context);

#ifdef REGAIN_AT_FREE
  if ( !multi_mutex_trylock(&reserve_mutex) )
    return TRUE; /* give up if can't get the lock */
  /* Other threads might have reduced the commit limit since the offer,
     but this thread still needs the same amount memory as before. */
#endif
  HQASSERT(mm_commit_limit == mps_arena_commit_limit(mm_arena),
           "mm_commit_limit out of synch with MPS");
  HQASSERT(mm_commit_limit < extension->limit,
           "Commit limit raised unexpectedly.");
  limit = min(mm_commit_limit + max(offer->taken_size, extension->delta),
              extension->limit);
  res = mps_arena_commit_limit_set(mm_arena, limit);
  HQASSERT(res == MPS_RES_OK, "Failed to set commit limit");
  mm_commit_limit = limit;
  mm_memory_is_low = TRUE;
  MM_LOG(( LOG_CX, "0x%08x", mm_commit_limit ));
#if defined( DEBUG_BUILD )
  { /* If extending without limit, warn the user. */
    mm_commit_extension_t *extension =
      limit <= mm_arena_extension.limit ? &mm_arena_extension : &mm_use_all;

    if ( !extension->reported ) {
      if ( extension != &mm_arena_extension )
        monitorf((uint8 *)"Warning: using all available memory\n");
      extension->reported = TRUE;
    }
  }
#endif
#ifdef REGAIN_AT_FREE
  multi_mutex_unlock(&reserve_mutex);
#endif
  return TRUE;
}
Esempio n. 3
0
/** \brief Reduce the commit limit as far as possible within the
    extension, but only down to the cost given.

  Callers must shrink in the right order, so this will not be called when the
  commit limit is above the range of this extension, unless the cost limit is
  above it as well (this simplifies the callers).
 */
static Bool mm_shrink(mm_commit_extension_t *extension, mm_cost_t cost_limit)
{
  size_t curr_limit;
  Bool enough = TRUE;

  curr_limit = mm_commit_limit;
  HQASSERT(curr_limit == mps_arena_commit_limit( mm_arena ),
           "mm_commit_limit out of synch with MPS");
  HQASSERT(mm_commit_limit >= mm_arena_extension.base
           && mm_commit_limit <= mm_use_all.limit,
           "mm_commit_limit out of range");

  if ( !mm_cost_less_than(cost_limit, extension->cost) )
    return TRUE;

  HQASSERT(curr_limit <= extension->limit,
           "Shrinking using the wrong extension.");
  while ( curr_limit > extension->base ) {
    size_t trylimit;

    /* Now reduce the current limit, capping at the base for this level */
    trylimit = curr_limit - extension->delta;
    if ( trylimit > curr_limit /* wrap around */ || trylimit < extension->base )
      trylimit = extension->base;
    /* Try to set the new limit */
    if ( mps_arena_commit_limit_set( mm_arena, trylimit ) != MPS_RES_OK ) {
      enough = FALSE; break; /* still holding too much memory! */
    }
    curr_limit = trylimit;
  }
  if ( curr_limit != mm_commit_limit ) {
    mm_commit_limit = curr_limit;
    MM_LOG(( LOG_CS, "0x%08x", mm_commit_limit ));
    HQASSERT(mm_reserve_level == 0,
             "Shouldn't get here while using the reserve pool.");
    if ( mm_commit_limit == mm_commit_base )
      mm_memory_is_low = FALSE;
  }
  return enough;
}
Esempio n. 4
0
static void test(void) {

    mycell *p, *q;
    int i;
    mps_res_t res;
    size_t lim0, avail0, lim1, avail1, commit1, lim2, avail2, commit2;
    size_t lim3, avail3, commit3, lim4, avail4, commit4;
    size_t lim5, avail5, commit5, lim6, avail6, commit6;

    cdie(mps_arena_create(&arena, mps_arena_class_vm(), ARENA_SIZE),
         "create arena");

    cdie(mps_thread_reg(&thread, arena), "register thread");

    cdie(mps_root_create_reg(&root, arena, mps_rank_ambig(), 0, thread,
                             mps_stack_scan_ambig, stackpointer, 0), "create stack root");

    cdie(
        mps_fmt_create_A(&format, arena, &fmtA),
        "create format");

    cdie(
        mps_pool_create(&poolsnc, arena, mps_class_snc(), format),
        "create pool");

    cdie(
        mps_ap_create(&apsnc, poolsnc, mps_rank_exact()),
        "create ap");

    report("lim0", "%d", lim0 = mps_reservoir_limit(arena));
    report("avail0",  "%d", avail0 = mps_reservoir_available(arena));
    mps_reservoir_limit_set(arena, (size_t) 0);

    mps_reservoir_limit_set(arena, (size_t) (5ul*1024*1024));
    report("lim1", "%d", lim1 = mps_reservoir_limit(arena));
    report("avail1",  "%d", avail1 = mps_reservoir_available(arena));
    report("commit1", "%d", commit1 = arena_committed_and_used(arena));
    report("defecit1", "%d", lim1-avail1);

    mps_reservoir_limit_set(arena, (size_t) (1045));
    report("lim2", "%d", lim2 = mps_reservoir_limit(arena));
    report("avail2",  "%d", avail2 = mps_reservoir_available(arena));
    report("commit2", "%d", commit2 = arena_committed_and_used(arena));
    report("defecit2", "%d", lim2-avail2);

    /* set commit limit to whatever is currently committed plus 1 MB
    */

    die(mps_arena_commit_limit_set(arena, arena_committed_and_used(arena)+1024*1024), "commit limit set");
    mps_reservoir_limit_set(arena, (size_t) (10ul*1024*1024));
    report("lim3", "%d", lim3 = mps_reservoir_limit(arena));
    report("avail3",  "%d", avail3 = mps_reservoir_available(arena));
    report("commit3", "%d", commit3 = arena_committed_and_used(arena));
    report("defecit3", "%d", lim3-avail3);
    report("spill3", "%d", commit3-mps_arena_commit_limit(arena));

    /* now raise it by 1/2 MB -- reservoir should grow
    */

    die(mps_arena_commit_limit_set(arena, arena_committed_and_used(arena)+512*1024), "commit limit set");
    report("lim4", "%d", lim4 = mps_reservoir_limit(arena));
    report("avail4",  "%d", avail4 = mps_reservoir_available(arena));
    report("commit4", "%d", commit4 = arena_committed_and_used(arena));
    report("grow4", "%d", avail4-avail3);
    report("spill4", "%d", commit4-mps_arena_commit_limit(arena));

    /* try some allocation -- more than a small amount should fail
    */

    i = -1;
    p = NULL;
    res = MPS_RES_OK;
    while (res == MPS_RES_OK) {
        res = allocrone(&q, apsnc, 10, mps_rank_exact());
        if (res == MPS_RES_OK) {
            setref(q, 0, p);
            p = q;
        }
        i++;
    }
    report("allocfail", "%d", i);
    report_res("failres", res);

    /* available shouldn't have changed since before allocation
    */

    report("lim5", "%d", lim5 = mps_reservoir_limit(arena));
    report("avail5",  "%d", avail5 = mps_reservoir_available(arena));
    report("commit5", "%d", commit5 = arena_committed_and_used(arena));
    report("grow5", "%d", avail5-avail4);
    report("spill5", "%d", commit5-mps_arena_commit_limit(arena));

    /* try some allocation from reservoir -- not much should fail
    */

    i = -1;
    res = MPS_RES_OK;
    while (res == MPS_RES_OK) {
        res = reservoir_allocrone(&q, apsnc, 10, mps_rank_exact());
        if (res == MPS_RES_OK) {
            setref(q, 0, p);
            p = q;
        }
        i++;
    }
    report("allocfail2", "%d", i);
    report_res("failres2", res);

    /* available should have changed now
    */

    report("lim6", "%d", lim6 = mps_reservoir_limit(arena));
    report("avail6",  "%d", avail6 = mps_reservoir_available(arena));
    report("commit6", "%d", commit6 = arena_committed_and_used(arena));
    report("spill6", "%d", commit6-mps_arena_commit_limit(arena));
    report("shrink6", "%d", avail5-avail6);

    mps_root_destroy(root);
    comment("Destroyed root.");

    mps_ap_destroy(apsnc);
    comment("Destroyed ap.");

    mps_pool_destroy(poolsnc);
    comment("Destroyed pool.");

    mps_fmt_destroy(format);
    comment("Destroyed format.");

    mps_thread_dereg(thread);
    comment("Deregistered thread.");

    mps_arena_destroy(arena);
    comment("Destroyed arena.");
}
static void test(void) {

 mycell *p, *q;
 int i;
 mps_res_t res;
 size_t lim7, avail7, commit7, lim8, avail8, commit8;
 size_t lim9, avail9, commit9;

 cdie(mps_arena_create(&arena, mps_arena_class_vm(), ARENA_SIZE),
  "create space");

 cdie(mps_thread_reg(&thread, arena), "register thread");

 cdie(mps_root_create_reg(&root, arena, MPS_RANK_AMBIG, 0, thread,
  mps_stack_scan_ambig, stackpointer, 0), "create stack root");

 cdie(
  mps_fmt_create_A(&format, arena, &fmtA),
  "create format");

 cdie(
  mps_pool_create(&poolamc, arena, mps_class_amc(), format),
  "create pool");

 cdie(
  mps_ap_create(&apamc, poolamc, MPS_RANK_EXACT),
  "create ap");

 mps_arena_commit_limit_set(arena, mps_arena_committed(arena)+1024*1024);

 mps_reservoir_limit_set(arena, 0);
 report("lim7", "%d", lim7 = mps_reservoir_limit(arena));
 report("avail7",  "%d", avail7 = mps_reservoir_available(arena));
 report("commit7", "%d", commit7 = mps_arena_committed(arena));
 
/* available should be zero, but should be able to allocate with
   reservoir permit, until commit_limit is reached
*/

 i = -1;
 p = NULL;
 res = MPS_RES_OK;
 while (res == MPS_RES_OK) {
  res = reservoir_allocrone(&q, apamc, 10, MPS_RANK_EXACT);
  if (res == MPS_RES_OK) {
   setref(q, 0, p);
   p = q;
  }
  i++;
 }
 report("allocfail3", "%d", i);
 report_res("failres3", res);

/* should be none left to make available */

 mps_reservoir_limit_set(arena, 10ul*1024*1024);
 report("lim8", "%d", lim8 = mps_reservoir_limit(arena));
 report("avail8",  "%d", avail8 = mps_reservoir_available(arena));
 report("commit8", "%d", commit8 = mps_arena_committed(arena));
 report("spill8", "%d", commit8-mps_arena_commit_limit(arena));

/* throw away objects and collect world */

 p = NULL;
 q = NULL;
 mps_root_destroy(root);
 mps_arena_collect(arena);

/* available should have gone up now */

 report("lim9", "%d", lim9 = mps_reservoir_limit(arena));
 report("avail9",  "%d", avail9 = mps_reservoir_available(arena));
 report("commit9", "%d", commit9 = mps_arena_committed(arena));
 report("grow9", "%d", avail9-avail8);
 report("spill9", "%d", commit9-mps_arena_commit_limit(arena));
 
/* destroy everything remaining
*/

 mps_ap_destroy(apamc);
 comment("Destroyed ap.");

 mps_pool_destroy(poolamc);
 comment("Destroyed pool.");

 mps_fmt_destroy(format);
 comment("Destroyed format.");

 mps_thread_dereg(thread);
 comment("Deregistered thread.");

 mps_arena_destroy(arena);
 comment("Destroyed space.");
}