Logo AND Algorithmique Numérique Distribuée

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