- tmgr_finalize();
- surf_parse_lex_destroy();
- surf_parse_free_callbacks();
-
- NOW = 0; /* Just in case the user plans to restart the simulation afterward */
-}
-
-/*********
- * Model *
- *********/
-
-Model::Model()
- : p_maxminSystem(NULL)
-{
- p_readyActionSet = new ActionList();
- p_runningActionSet = new ActionList();
- p_failedActionSet = new ActionList();
- p_doneActionSet = new ActionList();
-
- p_modifiedSet = NULL;
- p_actionHeap = NULL;
- p_updateMechanism = UM_UNDEFINED;
- m_selectiveUpdate = 0;
-}
-
-Model::~Model(){
- delete p_readyActionSet;
- delete p_runningActionSet;
- delete p_failedActionSet;
- delete p_doneActionSet;
-}
-
-double Model::shareResources(double now)
-{
- //FIXME: set the good function once and for all
- if (p_updateMechanism == UM_LAZY)
- return shareResourcesLazy(now);
- else if (p_updateMechanism == UM_FULL)
- return shareResourcesFull(now);
- else
- xbt_die("Invalid cpu update mechanism!");
-}
-
-double Model::shareResourcesLazy(double now)
-{
- Action *action = NULL;
- double min = -1;
- double share;
-
- XBT_DEBUG
- ("Before share resources, the size of modified actions set is %zd",
- p_modifiedSet->size());
-
- lmm_solve(p_maxminSystem);
-
- XBT_DEBUG
- ("After share resources, The size of modified actions set is %zd",
- p_modifiedSet->size());
-
- while(!p_modifiedSet->empty()) {
- action = &(p_modifiedSet->front());
- p_modifiedSet->pop_front();
- int max_dur_flag = 0;
-
- if (action->getStateSet() != p_runningActionSet)
- continue;
-
- /* bogus priority, skip it */
- if (action->getPriority() <= 0 || action->getHat()==LATENCY)
- continue;
-
- action->updateRemainingLazy(now);
-
- min = -1;
- share = lmm_variable_getvalue(action->getVariable());
-
- if (share > 0) {
- double time_to_completion;
- if (action->getRemains() > 0) {
- time_to_completion = action->getRemainsNoUpdate() / share;
- } else {
- time_to_completion = 0.0;
- }
- min = now + time_to_completion; // when the task will complete if nothing changes
- }
-
- if ((action->getMaxDuration() != NO_MAX_DURATION)
- && (min == -1
- || action->getStartTime() +
- action->getMaxDuration() < min)) {
- min = action->getStartTime() +
- action->getMaxDuration(); // when the task will complete anyway because of the deadline if any
- max_dur_flag = 1;
- }
-
-
- XBT_DEBUG("Action(%p) corresponds to variable %d", action, action->getVariable()->id_int);
-
- XBT_DEBUG("Action(%p) Start %f. May finish at %f (got a share of %f). Max_duration %f", action,
- action->getStartTime(), min, share,
- action->getMaxDuration());
-
- if (min != -1) {
- action->heapUpdate(p_actionHeap, min, max_dur_flag ? MAX_DURATION : NORMAL);
- XBT_DEBUG("Insert at heap action(%p) min %f now %f", action, min,
- now);
- } else DIE_IMPOSSIBLE;
- }
-
- //hereafter must have already the min value for this resource model
- if (xbt_heap_size(p_actionHeap) > 0)
- min = xbt_heap_maxkey(p_actionHeap) - now;
- else
- min = -1;
-
- XBT_DEBUG("The minimum with the HEAP %f", min);
-
- return min;
-}
-
-double Model::shareResourcesFull(double /*now*/) {
- THROW_UNIMPLEMENTED;
-}
-
-double Model::shareResourcesMaxMin(ActionList *running_actions,
- lmm_system_t sys,
- void (*solve) (lmm_system_t))
-{
- Action *action = NULL;
- double min = -1;
- double value = -1;
-
- solve(sys);
-
- ActionList::iterator it(running_actions->begin()), itend(running_actions->end());
- for(; it != itend ; ++it) {
- action = &*it;
- value = lmm_variable_getvalue(action->getVariable());
- if ((value > 0) || (action->getMaxDuration() >= 0))
- break;
- }
-
- if (!action)
- return -1.0;
-
- if (value > 0) {
- if (action->getRemains() > 0)
- min = action->getRemainsNoUpdate() / value;
- else
- min = 0.0;
- if ((action->getMaxDuration() >= 0) && (action->getMaxDuration() < min))
- min = action->getMaxDuration();
- } else
- min = action->getMaxDuration();
-
-
- for (++it; it != itend; ++it) {
- action = &*it;
- value = lmm_variable_getvalue(action->getVariable());
- if (value > 0) {
- if (action->getRemains() > 0)
- value = action->getRemainsNoUpdate() / value;
- else
- value = 0.0;
- if (value < min) {
- min = value;
- XBT_DEBUG("Updating min (value) with %p: %f", action, min);
- }
- }
- if ((action->getMaxDuration() >= 0) && (action->getMaxDuration() < min)) {
- min = action->getMaxDuration();
- XBT_DEBUG("Updating min (duration) with %p: %f", action, min);
- }
- }
- XBT_DEBUG("min value : %f", min);
-
- return min;
-}
-
-void Model::updateActionsState(double now, double delta)
-{
- if (p_updateMechanism == UM_FULL)
- updateActionsStateFull(now, delta);
- else if (p_updateMechanism == UM_LAZY)
- updateActionsStateLazy(now, delta);
- else
- xbt_die("Invalid cpu update mechanism!");
-}
-
-void Model::updateActionsStateLazy(double /*now*/, double /*delta*/)
-{
- THROW_UNIMPLEMENTED;
-}
-
-void Model::updateActionsStateFull(double /*now*/, double /*delta*/)
-{
- THROW_UNIMPLEMENTED;
-}
-
-/************
- * Resource *
- ************/
-
-Resource::Resource()
-: p_name(NULL), p_properties(NULL), p_model(NULL)
-{}
-
-Resource::Resource(Model *model, const char *name, xbt_dict_t props)
- : p_name(xbt_strdup(name)), p_properties(props), p_model(model)
- , m_running(true), m_stateCurrent(SURF_RESOURCE_ON)
-{}
-
-Resource::Resource(Model *model, const char *name, xbt_dict_t props, lmm_constraint_t constraint)
- : p_name(xbt_strdup(name)), p_properties(props), p_model(model)
- , m_running(true), m_stateCurrent(SURF_RESOURCE_ON), p_constraint(constraint)
-{}
-
-Resource::Resource(Model *model, const char *name, xbt_dict_t props, e_surf_resource_state_t stateInit)
- : p_name(xbt_strdup(name)), p_properties(props), p_model(model)
- , m_running(true), m_stateCurrent(stateInit)
-{}
-
-Resource::~Resource() {
- xbt_free((void*)p_name);
- xbt_dict_free(&p_properties);
-}
-
-e_surf_resource_state_t Resource::getState()
-{
- return m_stateCurrent;
-}
-
-void Resource::setState(e_surf_resource_state_t state)
-{
- m_stateCurrent = state;
-}
-
-bool Resource::isOn()
-{
- return m_running;
-}
-
-void Resource::turnOn()
-{
- if (!m_running) {
- m_running = true;
- }
-}
-
-void Resource::turnOff()
-{
- if (m_running) {
- m_running = false;
- }
-}
-
-Model *Resource::getModel() {
- return p_model;
-}
-
-const char *Resource::getName() {
- return p_name;
-}
-
-xbt_dict_t Resource::getProperties() {
- if (p_properties==NULL)
- p_properties = xbt_dict_new();
- return p_properties;
-}
-
-lmm_constraint_t Resource::getConstraint() {
- return p_constraint;
-}
-
-/**********
- * Action *
- **********/
-
-const char *surf_action_state_names[6] = {
- "SURF_ACTION_READY",
- "SURF_ACTION_RUNNING",
- "SURF_ACTION_FAILED",
- "SURF_ACTION_DONE",
- "SURF_ACTION_TO_FREE",
- "SURF_ACTION_NOT_IN_THE_SYSTEM"
-};
-
-void Action::initialize(Model *model, double cost, bool failed,
- lmm_variable_t var)
-{
- m_priority = 1.0;
- m_refcount = 1;
- m_remains = cost;
- m_maxDuration = NO_MAX_DURATION;
- m_finish = -1.0;
- m_start = surf_get_clock();
- m_cost = cost;
- p_model = model;
- p_data = NULL;
- p_variable = var;
- m_lastValue = 0;
- m_lastUpdate = 0;
- m_suspended = false;
- m_hat = NOTSET;
- p_category = NULL;
- p_stateHookup.prev = 0;
- p_stateHookup.next = 0;
- if (failed)
- p_stateSet = getModel()->getFailedActionSet();
- else
- p_stateSet = getModel()->getRunningActionSet();
-
- p_stateSet->push_back(*this);
-}
-
-Action::Action(Model *model, double cost, bool failed)
-{
- initialize(model, cost, failed);
-}
-
-Action::Action(Model *model, double cost, bool failed, lmm_variable_t var)
-{
- initialize(model, cost, failed, var);
-}
-
-Action::~Action() {
- xbt_free(p_category);
-}
-
-void Action::finish() {
- m_finish = surf_get_clock();
-}
-
-e_surf_action_state_t Action::getState()
-{
- if (p_stateSet == getModel()->getReadyActionSet())
- return SURF_ACTION_READY;
- if (p_stateSet == getModel()->getRunningActionSet())
- return SURF_ACTION_RUNNING;
- if (p_stateSet == getModel()->getFailedActionSet())
- return SURF_ACTION_FAILED;
- if (p_stateSet == getModel()->getDoneActionSet())
- return SURF_ACTION_DONE;
- return SURF_ACTION_NOT_IN_THE_SYSTEM;
-}
-
-void Action::setState(e_surf_action_state_t state)
-{
- //surf_action_state_t action_state = &(action->model_type->states);
- XBT_IN("(%p,%s)", this, surf_action_state_names[state]);
- p_stateSet->erase(p_stateSet->iterator_to(*this));
- if (state == SURF_ACTION_READY)
- p_stateSet = getModel()->getReadyActionSet();
- else if (state == SURF_ACTION_RUNNING)
- p_stateSet = getModel()->getRunningActionSet();
- else if (state == SURF_ACTION_FAILED)
- p_stateSet = getModel()->getFailedActionSet();
- else if (state == SURF_ACTION_DONE)
- p_stateSet = getModel()->getDoneActionSet();
- else
- p_stateSet = NULL;
-
- if (p_stateSet)
- p_stateSet->push_back(*this);
- XBT_OUT();
-}
-
-double Action::getBound()
-{
- return (p_variable) ? lmm_variable_getbound(p_variable) : 0;
-}
-
-void Action::setBound(double bound)
-{
- XBT_IN("(%p,%g)", this, bound);
- if (p_variable)
- lmm_update_variable_bound(getModel()->getMaxminSystem(), p_variable, bound);
-
- if (getModel()->getUpdateMechanism() == UM_LAZY && getLastUpdate()!=surf_get_clock())
- heapRemove(getModel()->getActionHeap());
- XBT_OUT();
-}
-
-double Action::getStartTime()
-{
- return m_start;
-}
-
-double Action::getFinishTime()
-{
- /* keep the function behavior, some models (cpu_ti) change the finish time before the action end */
- return m_remains == 0 ? m_finish : -1;
-}
-
-void Action::setData(void* data)
-{
- p_data = data;
-}
-
-void Action::setCategory(const char *category)
-{
- XBT_IN("(%p,%s)", this, category);
- p_category = xbt_strdup(category);
- XBT_OUT();
-}
-
-void Action::ref(){
- m_refcount++;
-}
-
-void Action::setMaxDuration(double duration)
-{
- XBT_IN("(%p,%g)", this, duration);
- m_maxDuration = duration;
- if (getModel()->getUpdateMechanism() == UM_LAZY) // remove action from the heap
- heapRemove(getModel()->getActionHeap());
- XBT_OUT();
-}
-
-void Action::gapRemove() {}
-
-void Action::setPriority(double priority)
-{
- XBT_IN("(%p,%g)", this, priority);
- m_priority = priority;
- lmm_update_variable_weight(getModel()->getMaxminSystem(), getVariable(), priority);
-
- if (getModel()->getUpdateMechanism() == UM_LAZY)
- heapRemove(getModel()->getActionHeap());
- XBT_OUT();
-}
-
-void Action::cancel(){
- setState(SURF_ACTION_FAILED);
- if (getModel()->getUpdateMechanism() == UM_LAZY) {
- if (action_lmm_hook.is_linked())
- getModel()->getModifiedSet()->erase(getModel()->getModifiedSet()->iterator_to(*this));
- heapRemove(getModel()->getActionHeap());
- }
-}
-
-int Action::unref(){
- m_refcount--;
- if (!m_refcount) {
- if (action_hook.is_linked())
- p_stateSet->erase(p_stateSet->iterator_to(*this));
- if (getVariable())
- lmm_variable_free(getModel()->getMaxminSystem(), getVariable());
- if (getModel()->getUpdateMechanism() == UM_LAZY) {
- /* remove from heap */
- heapRemove(getModel()->getActionHeap());
- if (action_lmm_hook.is_linked())
- getModel()->getModifiedSet()->erase(getModel()->getModifiedSet()->iterator_to(*this));
- }
- delete this;
- return 1;
- }
- return 0;
-}