Logo AND Algorithmique Numérique Distribuée

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