+CpuTi::~CpuTi(){
+delete p_availTrace;
+xbt_swag_free(p_actionSet);
+}
+
+void CpuTi::updateState(tmgr_trace_event_t event_type,
+ double value, double date)
+{
+ void *_action;
+ CpuTiActionPtr action;
+
+ if (event_type == p_powerEvent) {
+ tmgr_trace_t power_trace;
+ CpuTiTgmrPtr trace;
+ s_tmgr_event_t val;
+
+ XBT_DEBUG("Finish trace date: %f value %f date %f", surf_get_clock(),
+ value, date);
+ /* update remaining of actions and put in modified cpu swag */
+ updateRemainingAmount(date);
+ xbt_swag_insert(this, reinterpret_cast<CpuTiModelPtr>(getModel())->p_modifiedCpu);
+
+ power_trace = p_availTrace->p_powerTrace;
+ xbt_dynar_get_cpy(power_trace->s_list.event_list,
+ xbt_dynar_length(power_trace->s_list.event_list) - 1, &val);
+ /* free old trace */
+ delete p_availTrace;
+ m_powerScale = val.value;
+
+ trace = new CpuTiTgmr(TRACE_FIXED, val.value);
+ XBT_DEBUG("value %f", val.value);
+
+ p_availTrace = trace;
+
+ if (tmgr_trace_event_free(event_type))
+ p_powerEvent = NULL;
+
+ } else if (event_type == p_stateEvent) {
+ if (value > 0) {
+ if(getState() == SURF_RESOURCE_OFF)
+ xbt_dynar_push_as(host_that_restart, char*, (char *)getName());
+ setState(SURF_RESOURCE_ON);
+ } else {
+ setState(SURF_RESOURCE_OFF);
+
+ /* put all action running on cpu to failed */
+ xbt_swag_foreach(_action, p_actionSet) {
+ action = static_cast<CpuTiActionPtr>(_action);
+ if (action->getState() == SURF_ACTION_RUNNING
+ || action->getState() == SURF_ACTION_READY
+ || action->getState() == SURF_ACTION_NOT_IN_THE_SYSTEM) {
+ action->setFinishTime(date);
+ action->setState(SURF_ACTION_FAILED);
+ if (action->m_indexHeap >= 0) {
+ CpuTiActionPtr heap_act = (CpuTiActionPtr)
+ xbt_heap_remove(reinterpret_cast<CpuTiModelPtr>(getModel())->p_tiActionHeap, action->m_indexHeap);
+ if (heap_act != action)
+ DIE_IMPOSSIBLE;
+ }
+ }
+ }
+ }
+ if (tmgr_trace_event_free(event_type))
+ p_stateEvent = NULL;
+ } else {
+ XBT_CRITICAL("Unknown event ! \n");
+ xbt_abort();
+ }
+
+ return;
+}
+
+void CpuTi::updateActionsFinishTime(double now)
+{
+ void *_action;
+ CpuTiActionPtr action;
+ double sum_priority = 0.0, total_area, min_finish = -1;
+
+/* update remaning amount of actions */
+updateRemainingAmount(now);
+
+ xbt_swag_foreach(_action, p_actionSet) {
+ action = static_cast<CpuTiActionPtr>(_action);
+ /* action not running, skip it */
+ if (action->getStateSet() !=
+ surf_cpu_model_pm->getRunningActionSet())
+ continue;
+
+ /* bogus priority, skip it */
+ if (action->getPriority() <= 0)
+ continue;
+
+ /* action suspended, skip it */
+ if (action->m_suspended != 0)
+ continue;
+
+ sum_priority += 1.0 / action->getPriority();
+ }
+ m_sumPriority = sum_priority;
+
+ xbt_swag_foreach(_action, p_actionSet) {
+ action = static_cast<CpuTiActionPtr>(_action);
+ min_finish = -1;
+ /* action not running, skip it */
+ if (action->getStateSet() !=
+ surf_cpu_model_pm->getRunningActionSet())
+ continue;
+
+ /* verify if the action is really running on cpu */
+ if (action->m_suspended == 0 && action->getPriority() > 0) {
+ /* total area needed to finish the action. Used in trace integration */
+ total_area =
+ (action->getRemains()) * sum_priority *
+ action->getPriority();
+
+ total_area /= m_powerPeak;
+
+ action->setFinishTime(p_availTrace->solve(now, total_area));
+ /* verify which event will happen before (max_duration or finish time) */
+ if (action->getMaxDuration() != NO_MAX_DURATION &&
+ action->getStartTime() + action->getMaxDuration() < action->m_finish)
+ min_finish = action->getStartTime() + action->getMaxDuration();
+ else
+ min_finish = action->m_finish;
+ } else {
+ /* put the max duration time on heap */
+ if (action->getMaxDuration() != NO_MAX_DURATION)
+ min_finish = action->getStartTime() + action->getMaxDuration();
+ }
+ /* add in action heap */
+ XBT_DEBUG("action(%p) index %d", action, action->m_indexHeap);
+ if (action->m_indexHeap >= 0) {
+ CpuTiActionPtr heap_act = (CpuTiActionPtr)
+ xbt_heap_remove(reinterpret_cast<CpuTiModelPtr>(getModel())->p_tiActionHeap, action->m_indexHeap);
+ if (heap_act != action)
+ DIE_IMPOSSIBLE;
+ }
+ if (min_finish != NO_MAX_DURATION)
+ xbt_heap_push(reinterpret_cast<CpuTiModelPtr>(getModel())->p_tiActionHeap, action, min_finish);
+
+ XBT_DEBUG
+ ("Update finish time: Cpu(%s) Action: %p, Start Time: %f Finish Time: %f Max duration %f",
+ getName(), action, action->getStartTime(),
+ action->m_finish,
+ action->getMaxDuration());
+ }
+/* remove from modified cpu */
+ xbt_swag_remove(this, reinterpret_cast<CpuTiModelPtr>(getModel())->p_modifiedCpu);
+}