Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Change name()/cname() to getName()/getCname() in surf::FileImpl.
[simgrid.git] / src / surf / cpu_ti.cpp
1 /* Copyright (c) 2013-2017. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "cpu_ti.hpp"
8 #include "xbt/heap.h"
9 #include "src/surf/trace_mgr.hpp"
10
11 #ifndef SURF_MODEL_CPUTI_H_
12 #define SURF_MODEL_CPUTI_H_
13
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_cpu_ti, surf_cpu, "Logging specific to the SURF CPU TRACE INTEGRATION module");
15
16 namespace simgrid {
17 namespace surf {
18
19 static inline
20 void cpu_ti_action_update_index_heap(void *action, int i)
21 {
22   (static_cast<simgrid::surf::CpuTiAction*>(action))->updateIndexHeap(i);
23 }
24
25 /*********
26  * Trace *
27  *********/
28
29 CpuTiTrace::CpuTiTrace(tmgr_trace_t speedTrace)
30 {
31   double integral = 0;
32   double time = 0;
33   int i = 0;
34   nbPoints_ = speedTrace->event_list.size() + 1;
35   timePoints_ = new double[nbPoints_];
36   integral_ =  new double[nbPoints_];
37   for (auto const& val : speedTrace->event_list) {
38     timePoints_[i] = time;
39     integral_[i] = integral;
40     integral += val.date_ * val.value_;
41     time += val.date_;
42     i++;
43   }
44   timePoints_[i] = time;
45   integral_[i] = integral;
46 }
47
48 CpuTiTrace::~CpuTiTrace()
49 {
50   delete [] timePoints_;
51   delete [] integral_;
52 }
53
54 CpuTiTgmr::~CpuTiTgmr()
55 {
56   if (trace_)
57     delete trace_;
58 }
59
60 /**
61 * \brief Integrate trace
62 *
63 * Wrapper around surf_cpu_integrate_trace_simple() to get
64 * the cyclic effect.
65 *
66 * \param a      Begin of interval
67 * \param b      End of interval
68 * \return the integrate value. -1 if an error occurs.
69 */
70 double CpuTiTgmr::integrate(double a, double b)
71 {
72   int a_index;
73
74   if ((a < 0.0) || (a > b)) {
75     xbt_die("Error, invalid integration interval [%.2f,%.2f]. "
76         "You probably have a task executing with negative computation amount. Check your code.", a, b);
77   }
78   if (fabs(a -b) < EPSILON)
79     return 0.0;
80
81   if (type_ == TRACE_FIXED) {
82     return ((b - a) * value_);
83   }
84
85   if (fabs(ceil(a / lastTime_) - a / lastTime_) < EPSILON)
86     a_index = 1 + static_cast<int>(ceil(a / lastTime_));
87   else
88     a_index = static_cast<int> (ceil(a / lastTime_));
89
90   int b_index = static_cast<int> (floor(b / lastTime_));
91
92   if (a_index > b_index) {      /* Same chunk */
93     return trace_->integrateSimple(a - (a_index - 1) * lastTime_, b - (b_index) * lastTime_);
94   }
95
96   double first_chunk = trace_->integrateSimple(a - (a_index - 1) * lastTime_, lastTime_);
97   double middle_chunk = (b_index - a_index) * total_;
98   double last_chunk = trace_->integrateSimple(0.0, b - (b_index) * lastTime_);
99
100   XBT_DEBUG("first_chunk=%.2f  middle_chunk=%.2f  last_chunk=%.2f\n", first_chunk, middle_chunk, last_chunk);
101
102   return (first_chunk + middle_chunk + last_chunk);
103 }
104
105 /**
106  * \brief Auxiliary function to compute the integral between a and b.
107  *     It simply computes the integrals at point a and b and returns the difference between them.
108  * \param a  Initial point
109  * \param b  Final point
110 */
111 double CpuTiTrace::integrateSimple(double a, double b)
112 {
113   return integrateSimplePoint(b) - integrateSimplePoint(a);
114 }
115
116 /**
117  * \brief Auxiliary function to compute the integral at point a.
118  * \param a        point
119  */
120 double CpuTiTrace::integrateSimplePoint(double a)
121 {
122   double integral = 0;
123   double a_aux = a;
124   int ind = binarySearch(timePoints_, a, 0, nbPoints_ - 1);
125   integral += integral_[ind];
126
127   XBT_DEBUG("a %f ind %d integral %f ind + 1 %f ind %f time +1 %f time %f",
128        a, ind, integral, integral_[ind + 1], integral_[ind], timePoints_[ind + 1], timePoints_[ind]);
129   double_update(&a_aux, timePoints_[ind], sg_maxmin_precision*sg_surf_precision);
130   if (a_aux > 0)
131     integral += ((integral_[ind + 1] - integral_[ind]) / (timePoints_[ind + 1] - timePoints_[ind])) *
132                 (a - timePoints_[ind]);
133   XBT_DEBUG("Integral a %f = %f", a, integral);
134
135   return integral;
136 }
137
138 /**
139 * \brief Computes the time needed to execute "amount" on cpu.
140 *
141 * Here, amount can span multiple trace periods
142 *
143 * \param a        Initial time
144 * \param amount  Amount to be executed
145 * \return  End time
146 */
147 double CpuTiTgmr::solve(double a, double amount)
148 {
149   /* Fix very small negative numbers */
150   if ((a < 0.0) && (a > -EPSILON)) {
151     a = 0.0;
152   }
153   if ((amount < 0.0) && (amount > -EPSILON)) {
154     amount = 0.0;
155   }
156
157   /* Sanity checks */
158   if ((a < 0.0) || (amount < 0.0)) {
159     XBT_CRITICAL ("Error, invalid parameters [a = %.2f, amount = %.2f]. "
160         "You probably have a task executing with negative computation amount. Check your code.", a, amount);
161     xbt_abort();
162   }
163
164   /* At this point, a and amount are positive */
165   if (amount < EPSILON)
166     return a;
167
168   /* Is the trace fixed ? */
169   if (type_ == TRACE_FIXED) {
170     return (a + (amount / value_));
171   }
172
173   XBT_DEBUG("amount %f total %f", amount, total_);
174   /* Reduce the problem to one where amount <= trace_total */
175   int quotient = static_cast<int>(floor(amount / total_));
176   double reduced_amount = (total_) * ((amount / total_) - floor(amount / total_));
177   double reduced_a = a - (lastTime_) * static_cast<int>(floor(a / lastTime_));
178
179   XBT_DEBUG("Quotient: %d reduced_amount: %f reduced_a: %f", quotient, reduced_amount, reduced_a);
180
181   /* Now solve for new_amount which is <= trace_total */
182   double reduced_b = solveSomewhatSimple(reduced_a, reduced_amount);
183
184 /* Re-map to the original b and amount */
185   double b = (lastTime_) * static_cast<int>(floor(a / lastTime_)) + (quotient * lastTime_) + reduced_b;
186   return b;
187 }
188
189 /**
190 * \brief Auxiliary function to solve integral
191 *
192 * Here, amount is <= trace->total
193 * and a <=trace->last_time
194 *
195 */
196 double CpuTiTgmr::solveSomewhatSimple(double a, double amount)
197 {
198   double b;
199
200   XBT_DEBUG("Solve integral: [%.2f, amount=%.2f]", a, amount);
201   double amount_till_end = integrate(a, lastTime_);
202
203   if (amount_till_end > amount) {
204     b = trace_->solveSimple(a, amount);
205   } else {
206     b = lastTime_ + trace_->solveSimple(0.0, amount - amount_till_end);
207   }
208   return b;
209 }
210
211 /**
212  * \brief Auxiliary function to solve integral.
213  *  It returns the date when the requested amount of flops is available
214  * \param a        Initial point
215  * \param amount  Amount of flops
216  * \return The date when amount is available.
217 */
218 double CpuTiTrace::solveSimple(double a, double amount)
219 {
220   double integral_a = integrateSimplePoint(a);
221   int ind = binarySearch(integral_, integral_a + amount, 0, nbPoints_ - 1);
222   double time = timePoints_[ind];
223   time += (integral_a + amount - integral_[ind]) /
224            ((integral_[ind + 1] - integral_[ind]) / (timePoints_[ind + 1] - timePoints_[ind]));
225
226   return time;
227 }
228
229 /**
230 * \brief Auxiliary function to update the CPU speed scale.
231 *
232 *  This function uses the trace structure to return the speed scale at the determined time a.
233 * \param a        Time
234 * \return CPU speed scale
235 */
236 double CpuTiTgmr::getPowerScale(double a)
237 {
238   double reduced_a = a - floor(a / lastTime_) * lastTime_;
239   int point = trace_->binarySearch(trace_->timePoints_, reduced_a, 0, trace_->nbPoints_ - 1);
240   trace_mgr::DatedValue val = speedTrace_->event_list.at(point);
241   return val.value_;
242 }
243
244 /**
245 * \brief Creates a new integration trace from a tmgr_trace_t
246 *
247 * \param  speedTrace    CPU availability trace
248 * \param  value          Percentage of CPU speed available (useful to fixed tracing)
249 * \return  Integration trace structure
250 */
251 CpuTiTgmr::CpuTiTgmr(tmgr_trace_t speedTrace, double value) :
252     speedTrace_(speedTrace)
253 {
254   double total_time = 0.0;
255   trace_ = 0;
256
257 /* no availability file, fixed trace */
258   if (not speedTrace) {
259     type_ = TRACE_FIXED;
260     value_ = value;
261     XBT_DEBUG("No availability trace. Constant value = %f", value);
262     return;
263   }
264
265   /* only one point available, fixed trace */
266   if (speedTrace->event_list.size() == 1) {
267     trace_mgr::DatedValue val = speedTrace->event_list.front();
268     type_ = TRACE_FIXED;
269     value_                    = val.value_;
270     return;
271   }
272
273   type_ = TRACE_DYNAMIC;
274
275   /* count the total time of trace file */
276   for (auto const& val : speedTrace->event_list)
277     total_time += val.date_;
278
279   trace_ = new CpuTiTrace(speedTrace);
280   lastTime_ = total_time;
281   total_ = trace_->integrateSimple(0, total_time);
282
283   XBT_DEBUG("Total integral %f, last_time %f ", total_, lastTime_);
284 }
285
286 /**
287  * \brief Binary search in array.
288  *  It returns the first point of the interval in which "a" is.
289  * \param array    Array
290  * \param a        Value to search
291  * \param low     Low bound to search in array
292  * \param high    Upper bound to search in array
293  * \return Index of point
294 */
295 int CpuTiTrace::binarySearch(double *array, double a, int low, int high)
296 {
297   xbt_assert(low < high, "Wrong parameters: low (%d) should be smaller than high (%d)", low, high);
298
299   do {
300     int mid = low + (high - low) / 2;
301     XBT_DEBUG("a %f low %d high %d mid %d value %f", a, low, high, mid, array[mid]);
302
303     if (array[mid] > a)
304       high = mid;
305     else
306       low = mid;
307   }
308   while (low < high - 1);
309
310   return low;
311 }
312
313 }
314 }
315
316 /*********
317  * Model *
318  *********/
319
320 void surf_cpu_model_init_ti()
321 {
322   xbt_assert(not surf_cpu_model_pm, "CPU model already initialized. This should not happen.");
323   xbt_assert(not surf_cpu_model_vm, "CPU model already initialized. This should not happen.");
324
325   surf_cpu_model_pm = new simgrid::surf::CpuTiModel();
326   all_existing_models->push_back(surf_cpu_model_pm);
327
328   surf_cpu_model_vm = new simgrid::surf::CpuTiModel();
329   all_existing_models->push_back(surf_cpu_model_vm);
330 }
331
332 namespace simgrid {
333 namespace surf {
334
335 CpuTiModel::CpuTiModel() : CpuModel()
336 {
337   runningActionSetThatDoesNotNeedBeingChecked_ = new ActionList();
338
339   modifiedCpu_ = new CpuTiList();
340
341   tiActionHeap_ = xbt_heap_new(8, nullptr);
342   xbt_heap_set_update_callback(tiActionHeap_, cpu_ti_action_update_index_heap);
343 }
344
345 CpuTiModel::~CpuTiModel()
346 {
347   surf_cpu_model_pm = nullptr;
348   delete runningActionSetThatDoesNotNeedBeingChecked_;
349   delete modifiedCpu_;
350   xbt_heap_free(tiActionHeap_);
351 }
352
353 Cpu *CpuTiModel::createCpu(simgrid::s4u::Host *host, std::vector<double>* speedPerPstate, int core)
354 {
355   return new CpuTi(this, host, speedPerPstate, core);
356 }
357
358 double CpuTiModel::nextOccuringEvent(double now)
359 {
360   double min_action_duration = -1;
361
362 /* iterates over modified cpus to update share resources */
363   for(CpuTiList::iterator it(modifiedCpu_->begin()), itend(modifiedCpu_->end()) ; it != itend ;) {
364     CpuTi *ti = &*it;
365     ++it;
366     ti->updateActionsFinishTime(now);
367   }
368
369 /* get the min next event if heap not empty */
370   if (xbt_heap_size(tiActionHeap_) > 0)
371     min_action_duration = xbt_heap_maxkey(tiActionHeap_) - now;
372
373   XBT_DEBUG("Share resources, min next event date: %f", min_action_duration);
374
375   return min_action_duration;
376 }
377
378 void CpuTiModel::updateActionsState(double now, double /*delta*/)
379 {
380   while ((xbt_heap_size(tiActionHeap_) > 0) && (xbt_heap_maxkey(tiActionHeap_) <= now)) {
381     CpuTiAction *action = static_cast<CpuTiAction*>(xbt_heap_pop(tiActionHeap_));
382     XBT_DEBUG("Action %p: finish", action);
383     action->finish(Action::State::done);
384     /* set the remains to 0 due to precision problems when updating the remaining amount */
385     action->setRemains(0);
386     /* update remaining amount of all actions */
387     action->cpu_->updateRemainingAmount(surf_get_clock());
388   }
389 }
390
391 /************
392  * Resource *
393  ************/
394 CpuTi::CpuTi(CpuTiModel *model, simgrid::s4u::Host *host, std::vector<double> *speedPerPstate, int core)
395   : Cpu(model, host, speedPerPstate, core)
396 {
397   xbt_assert(core==1,"Multi-core not handled by this model yet");
398   coresAmount_ = core;
399
400   actionSet_ = new ActionTiList();
401
402   speed_.peak = speedPerPstate->front();
403   XBT_DEBUG("CPU create: peak=%f", speed_.peak);
404
405   speedIntegratedTrace_ = new CpuTiTgmr(nullptr, 1/*scale*/);
406 }
407
408 CpuTi::~CpuTi()
409 {
410   modified(false);
411   delete speedIntegratedTrace_;
412   delete actionSet_;
413 }
414 void CpuTi::setSpeedTrace(tmgr_trace_t trace)
415 {
416   if (speedIntegratedTrace_)
417     delete speedIntegratedTrace_;
418
419   speedIntegratedTrace_ = new CpuTiTgmr(trace, speed_.scale);
420
421   /* add a fake trace event if periodicity == 0 */
422   if (trace && trace->event_list.size() > 1) {
423     trace_mgr::DatedValue val = trace->event_list.back();
424     if (val.date_ < 1e-12)
425       speed_.event = future_evt_set->add_trace(new simgrid::trace_mgr::trace(), this);
426   }
427 }
428
429 void CpuTi::apply_event(tmgr_trace_event_t event, double value)
430 {
431   if (event == speed_.event) {
432     tmgr_trace_t speedTrace;
433     CpuTiTgmr *trace;
434
435     XBT_DEBUG("Finish trace date: value %f", value);
436     /* update remaining of actions and put in modified cpu swag */
437     updateRemainingAmount(surf_get_clock());
438
439     modified(true);
440
441     speedTrace = speedIntegratedTrace_->speedTrace_;
442     trace_mgr::DatedValue val = speedTrace->event_list.back();
443     delete speedIntegratedTrace_;
444     speed_.scale = val.value_;
445
446     trace = new CpuTiTgmr(TRACE_FIXED, val.value_);
447     XBT_DEBUG("value %f", val.value_);
448
449     speedIntegratedTrace_ = trace;
450
451     tmgr_trace_event_unref(&speed_.event);
452
453   } else if (event == stateEvent_) {
454     if (value > 0) {
455       if(isOff())
456         host_that_restart.push_back(getHost());
457       turnOn();
458     } else {
459       turnOff();
460       double date = surf_get_clock();
461
462       /* put all action running on cpu to failed */
463       for(ActionTiList::iterator it(actionSet_->begin()), itend(actionSet_->end()); it != itend ; ++it) {
464
465         CpuTiAction *action = &*it;
466         if (action->getState() == Action::State::running
467          || action->getState() == Action::State::ready
468          || action->getState() == Action::State::not_in_the_system) {
469           action->setFinishTime(date);
470           action->setState(Action::State::failed);
471           if (action->indexHeap_ >= 0) {
472             CpuTiAction* heap_act = static_cast<CpuTiAction*>(
473                 xbt_heap_remove(static_cast<CpuTiModel*>(model())->tiActionHeap_, action->indexHeap_));
474             if (heap_act != action)
475               DIE_IMPOSSIBLE;
476           }
477         }
478       }
479     }
480     tmgr_trace_event_unref(&stateEvent_);
481
482   } else {
483     xbt_die("Unknown event!\n");
484   }
485 }
486
487 void CpuTi::updateActionsFinishTime(double now)
488 {
489   CpuTiAction *action;
490   double sum_priority = 0.0;
491   double total_area;
492
493   /* update remaining amount of actions */
494   updateRemainingAmount(now);
495
496   for(ActionTiList::iterator it(actionSet_->begin()), itend(actionSet_->end()) ; it != itend ; ++it) {
497     action = &*it;
498     /* action not running, skip it */
499     if (action->getStateSet() != surf_cpu_model_pm->getRunningActionSet())
500       continue;
501
502     /* bogus priority, skip it */
503     if (action->getPriority() <= 0)
504       continue;
505
506     /* action suspended, skip it */
507     if (action->suspended_ != 0)
508       continue;
509
510     sum_priority += 1.0 / action->getPriority();
511   }
512   sumPriority_ = sum_priority;
513
514   for(ActionTiList::iterator it(actionSet_->begin()), itend(actionSet_->end()) ; it != itend ; ++it) {
515     action = &*it;
516     double min_finish = -1;
517     /* action not running, skip it */
518     if (action->getStateSet() !=  surf_cpu_model_pm->getRunningActionSet())
519       continue;
520
521     /* verify if the action is really running on cpu */
522     if (action->suspended_ == 0 && action->getPriority() > 0) {
523       /* total area needed to finish the action. Used in trace integration */
524       total_area = (action->getRemains()) * sum_priority * action->getPriority();
525
526       total_area /= speed_.peak;
527
528       action->setFinishTime(speedIntegratedTrace_->solve(now, total_area));
529       /* verify which event will happen before (max_duration or finish time) */
530       if (action->getMaxDuration() > NO_MAX_DURATION &&
531           action->getStartTime() + action->getMaxDuration() < action->finishTime_)
532         min_finish = action->getStartTime() + action->getMaxDuration();
533       else
534         min_finish = action->finishTime_;
535     } else {
536       /* put the max duration time on heap */
537       if (action->getMaxDuration() > NO_MAX_DURATION)
538         min_finish = action->getStartTime() + action->getMaxDuration();
539     }
540     /* add in action heap */
541     XBT_DEBUG("action(%p) index %d", action, action->indexHeap_);
542     if (action->indexHeap_ >= 0) {
543       CpuTiAction* heap_act = static_cast<CpuTiAction*>(
544           xbt_heap_remove(static_cast<CpuTiModel*>(model())->tiActionHeap_, action->indexHeap_));
545       if (heap_act != action)
546         DIE_IMPOSSIBLE;
547     }
548     if (min_finish > NO_MAX_DURATION)
549       xbt_heap_push(static_cast<CpuTiModel*>(model())->tiActionHeap_, action, min_finish);
550
551     XBT_DEBUG("Update finish time: Cpu(%s) Action: %p, Start Time: %f Finish Time: %f Max duration %f", getCname(),
552               action, action->getStartTime(), action->finishTime_, action->getMaxDuration());
553   }
554   /* remove from modified cpu */
555   modified(false);
556 }
557
558 bool CpuTi::isUsed()
559 {
560   return not actionSet_->empty();
561 }
562
563 double CpuTi::getAvailableSpeed()
564 {
565   speed_.scale = speedIntegratedTrace_->getPowerScale(surf_get_clock());
566   return Cpu::getAvailableSpeed();
567 }
568
569 /** @brief Update the remaining amount of actions */
570 void CpuTi::updateRemainingAmount(double now)
571 {
572
573   /* already updated */
574   if (lastUpdate_ >= now)
575     return;
576
577   /* compute the integration area */
578   double area_total = speedIntegratedTrace_->integrate(lastUpdate_, now) * speed_.peak;
579   XBT_DEBUG("Flops total: %f, Last update %f", area_total, lastUpdate_);
580
581   for(ActionTiList::iterator it(actionSet_->begin()), itend(actionSet_->end()) ; it != itend ; ++it) {
582     CpuTiAction *action = &*it;
583     /* action not running, skip it */
584     if (action->getStateSet() != model()->getRunningActionSet())
585       continue;
586
587     /* bogus priority, skip it */
588     if (action->getPriority() <= 0)
589       continue;
590
591     /* action suspended, skip it */
592     if (action->suspended_ != 0)
593       continue;
594
595     /* action don't need update */
596     if (action->getStartTime() >= now)
597       continue;
598
599     /* skip action that are finishing now */
600     if (action->finishTime_ >= 0 && action->finishTime_ <= now)
601       continue;
602
603     /* update remaining */
604     action->updateRemains(area_total / (sumPriority_ * action->getPriority()));
605     XBT_DEBUG("Update remaining action(%p) remaining %f", action, action->remains_);
606   }
607   lastUpdate_ = now;
608 }
609
610 CpuAction *CpuTi::execution_start(double size)
611 {
612   XBT_IN("(%s,%g)", getCname(), size);
613   CpuTiAction* action = new CpuTiAction(static_cast<CpuTiModel*>(model()), size, isOff(), this);
614
615   actionSet_->push_back(*action);
616
617   XBT_OUT();
618   return action;
619 }
620
621
622 CpuAction *CpuTi::sleep(double duration)
623 {
624   if (duration > 0)
625     duration = MAX(duration, sg_surf_precision);
626
627   XBT_IN("(%s,%g)", getCname(), duration);
628   CpuTiAction* action = new CpuTiAction(static_cast<CpuTiModel*>(model()), 1.0, isOff(), this);
629
630   action->maxDuration_ = duration;
631   action->suspended_ = 2;
632   if (duration == NO_MAX_DURATION) {
633    /* Move to the *end* of the corresponding action set. This convention
634       is used to speed up update_resource_state  */
635   action->getStateSet()->erase(action->getStateSet()->iterator_to(*action));
636   action->stateSet_ = static_cast<CpuTiModel*>(model())->runningActionSetThatDoesNotNeedBeingChecked_;
637   action->getStateSet()->push_back(*action);
638   }
639
640   actionSet_->push_back(*action);
641
642   XBT_OUT();
643   return action;
644 }
645
646 void CpuTi::modified(bool modified){
647   CpuTiList* modifiedCpu = static_cast<CpuTiModel*>(model())->modifiedCpu_;
648   if (modified) {
649     if (not cpu_ti_hook.is_linked()) {
650       modifiedCpu->push_back(*this);
651     }
652   } else {
653     if (cpu_ti_hook.is_linked()) {
654       modifiedCpu->erase(modifiedCpu->iterator_to(*this));
655     }
656   }
657 }
658
659 /**********
660  * Action *
661  **********/
662
663 CpuTiAction::CpuTiAction(CpuTiModel *model_, double cost, bool failed, CpuTi *cpu)
664  : CpuAction(model_, cost, failed)
665  , cpu_(cpu)
666 {
667   indexHeap_ = -1;
668   cpu_->modified(true);
669 }
670
671 void CpuTiAction::updateIndexHeap(int i)
672 {
673   indexHeap_ = i;
674 }
675
676 void CpuTiAction::setState(Action::State state)
677 {
678   CpuAction::setState(state);
679   cpu_->modified(true);
680 }
681
682 int CpuTiAction::unref()
683 {
684   refcount_--;
685   if (not refcount_) {
686     if (action_hook.is_linked())
687       getStateSet()->erase(getStateSet()->iterator_to(*this));
688     /* remove from action_set */
689     if (action_ti_hook.is_linked())
690       cpu_->actionSet_->erase(cpu_->actionSet_->iterator_to(*this));
691     /* remove from heap */
692     xbt_heap_remove(static_cast<CpuTiModel*>(getModel())->tiActionHeap_, this->indexHeap_);
693     cpu_->modified(true);
694     delete this;
695     return 1;
696   }
697   return 0;
698 }
699
700 void CpuTiAction::cancel()
701 {
702   this->setState(Action::State::failed);
703   xbt_heap_remove(getModel()->getActionHeap(), this->indexHeap_);
704   cpu_->modified(true);
705 }
706
707 void CpuTiAction::suspend()
708 {
709   XBT_IN("(%p)", this);
710   if (suspended_ != 2) {
711     suspended_ = 1;
712     xbt_heap_remove(getModel()->getActionHeap(), indexHeap_);
713     cpu_->modified(true);
714   }
715   XBT_OUT();
716 }
717
718 void CpuTiAction::resume()
719 {
720   XBT_IN("(%p)", this);
721   if (suspended_ != 2) {
722     suspended_ = 0;
723     cpu_->modified(true);
724   }
725   XBT_OUT();
726 }
727
728 void CpuTiAction::setMaxDuration(double duration)
729 {
730   double min_finish;
731
732   XBT_IN("(%p,%g)", this, duration);
733
734   maxDuration_ = duration;
735
736   if (duration >= 0)
737     min_finish = (getStartTime() + getMaxDuration()) < getFinishTime() ?
738                  (getStartTime() + getMaxDuration()) : getFinishTime();
739   else
740     min_finish = getFinishTime();
741
742 /* add in action heap */
743   if (indexHeap_ >= 0) {
744     CpuTiAction *heap_act = static_cast<CpuTiAction*>(xbt_heap_remove(getModel()->getActionHeap(), indexHeap_));
745     if (heap_act != this)
746       DIE_IMPOSSIBLE;
747   }
748   xbt_heap_push(getModel()->getActionHeap(), this, min_finish);
749
750   XBT_OUT();
751 }
752
753 void CpuTiAction::setSharingWeight(double priority)
754 {
755   XBT_IN("(%p,%g)", this, priority);
756   sharingWeight_ = priority;
757   cpu_->modified(true);
758   XBT_OUT();
759 }
760
761 double CpuTiAction::getRemains()
762 {
763   XBT_IN("(%p)", this);
764   cpu_->updateRemainingAmount(surf_get_clock());
765   XBT_OUT();
766   return remains_;
767 }
768
769 }
770 }
771
772 #endif /* SURF_MODEL_CPUTI_H_ */