예제 #1
0
/**
 * \brief Extract an object from a mallocator
 * \param m a mallocator
 *
 * Remove an object from the mallocator and return it.
 * This function is designed to be used instead of malloc().
 * If the mallocator is not empty, an object is
 * extracted from the mallocator and no malloc is done.
 *
 * If the mallocator is empty, a new object is created,
 * by calling the function new_f().
 *
 * In both cases, the function reset_f() (if defined) is called on the object.
 *
 * \see xbt_mallocator_release()
 */
void *xbt_mallocator_get(xbt_mallocator_t m)
{
  void *object;

  if (m->objects != NULL) { // this mallocator is active, stop thinking and go for it!
    lock_acquire(m);
    if (m->current_size <= 0) {
      /* No object is ready yet. Create a bunch of them to try to group the
       * mallocs on the same memory pages (to help the cache lines) */

      /* XBT_DEBUG("Create a new object for mallocator %p (size:%d/%d)", */
      /*           m, m->current_size, m->max_size); */
      int i;
      int amount = MIN(m->max_size / 2, 1000);
      for (i = 0; i < amount; i++)
        m->objects[i] = m->new_f();
      m->current_size = amount;
    }

    /* there is at least an available object, now */
    /* XBT_DEBUG("Reuse an old object for mallocator %p (size:%d/%d)", */
    /*           m, m->current_size, m->max_size); */
    object = m->objects[--m->current_size];
    lock_release(m);
  } else {
    if (xbt_mallocator_is_active()) {
      // We have to switch this mallocator from inactive to active (and then get an object)
      m->objects = xbt_new0(void *, m->max_size);
      lock_reset(m);
      return xbt_mallocator_get(m);
    } else {
      object = m->new_f();
    }
  }
예제 #2
0
void *surf_action_new(size_t size, double cost, surf_model_t model,
                      int failed)
{
  xbt_assert(size <= action_mallocator_allocated_size,
      "Cannot create a surf action of size %zu: the mallocator only provides actions of size %d",
      size, action_mallocator_allocated_size);

  surf_action_t action = xbt_mallocator_get(action_mallocator);
  action->refcount = 1;
  action->cost = cost;
  action->remains = cost;
  action->priority = 1.0;
  action->max_duration = NO_MAX_DURATION;
  action->start = surf_get_clock();
  action->finish = -1.0;
  action->model_type = model;
#ifdef HAVE_TRACING
  action->category = NULL;
#endif

  if (failed)
    action->state_set = model->states.failed_action_set;
  else
    action->state_set = model->states.running_action_set;

  xbt_swag_insert(action, action->state_set);

  return action;
}
예제 #3
0
/**
 * \brief Creates a new task.
 *
 * \param name the name of the task (can be \c nullptr)
 * \param data the user data you want to associate with the task (can be \c nullptr)
 * \param amount amount of the task
 * \return the new task
 * \see SD_task_destroy()
 */
SD_task_t SD_task_create(const char *name, void *data, double amount)
{
  SD_task_t task = static_cast<SD_task_t>(xbt_mallocator_get(sd_global->task_mallocator));

  /* general information */
  task->data = data;            /* user data */
  task->name = xbt_strdup(name);
  task->amount = amount;
  task->remains = amount;

  return task;
}
예제 #4
0
static smx_action_t SIMIX_synchro_wait(smx_host_t smx_host, double timeout)
{
  XBT_IN("(%p, %f)",smx_host,timeout);
  smx_action_t action;
  action = xbt_mallocator_get(simix_global->action_mallocator);
  action->type = SIMIX_ACTION_SYNCHRO;
  action->name = xbt_strdup("synchro");
  action->synchro.sleep = 
    surf_workstation_model->extension.workstation.sleep(smx_host->host, timeout);

  surf_workstation_model->action_data_set(action->synchro.sleep, action);
  XBT_OUT();
  return action;
}
예제 #5
0
lmm_variable_t lmm_variable_new(lmm_system_t sys, void *id, double weight, double bound, int number_of_constraints)
{
  lmm_variable_t var = NULL;
  int i;

  XBT_IN("(sys=%p, id=%p, weight=%f, bound=%f, num_cons =%d)", sys, id, weight, bound, number_of_constraints);

  var = (lmm_variable_t) xbt_mallocator_get(sys->variable_mallocator);
  var->id = id;
  var->id_int = Global_debug_id++;
  var->cnsts = (s_lmm_element_t *) xbt_realloc(var->cnsts, number_of_constraints * sizeof(s_lmm_element_t));
  for (i = 0; i < number_of_constraints; i++) {
    var->cnsts[i].enabled_element_set_hookup.next = NULL;
    var->cnsts[i].enabled_element_set_hookup.prev = NULL;
    var->cnsts[i].disabled_element_set_hookup.next = NULL;
    var->cnsts[i].disabled_element_set_hookup.prev = NULL;
    var->cnsts[i].active_element_set_hookup.next = NULL;
    var->cnsts[i].active_element_set_hookup.prev = NULL;
    var->cnsts[i].constraint = NULL;
    var->cnsts[i].variable = NULL;
    var->cnsts[i].value = 0.0;
  }
  var->cnsts_size = number_of_constraints;
  var->cnsts_number = 0;
  var->weight = weight;
  var->staged_weight = 0.0;
  var->bound = bound;
  var->concurrency_share = 1;
  var->value = 0.0;
  var->visited = sys->visited_counter - 1;
  var->mu = 0.0;
  var->new_mu = 0.0;
  var->func_f = func_f_def;
  var->func_fp = func_fp_def;
  var->func_fpi = func_fpi_def;

  var->variable_set_hookup.next = NULL;
  var->variable_set_hookup.prev = NULL;
  var->saturated_variable_set_hookup.next = NULL;
  var->saturated_variable_set_hookup.prev = NULL;

  if (weight)
    xbt_swag_insert_at_head(var, &(sys->variable_set));
  else
    xbt_swag_insert_at_tail(var, &(sys->variable_set));

  XBT_OUT(" returns %p", var);
  return var;
}
예제 #6
0
static smx_synchro_t SIMIX_synchro_wait(smx_host_t smx_host, double timeout)
{
  XBT_IN("(%p, %f)",smx_host,timeout);

  smx_synchro_t sync;
  sync = xbt_mallocator_get(simix_global->synchro_mallocator);
  sync->type = SIMIX_SYNC_SYNCHRO;
  sync->name = xbt_strdup("synchro");
  sync->synchro.sleep = 
    surf_workstation_sleep(smx_host, timeout);

  surf_action_set_data(sync->synchro.sleep, sync);
  XBT_OUT();
  return sync;
}