Logo AND Algorithmique Numérique Distribuée

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