Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Kill the useless NS3Sim class
[simgrid.git] / src / surf / cpu_cas01.cpp
1 /* Copyright (c) 2009-2011, 2013-2016. 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_cas01.hpp"
8 #include "cpu_ti.hpp"
9 #include "maxmin_private.hpp"
10 #include "simgrid/sg_config.h"
11
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_cpu_cas, surf_cpu, "Logging specific to the SURF CPU IMPROVED module");
13
14 /*********
15  * Model *
16  *********/
17 void surf_cpu_model_init_Cas01()
18 {
19   xbt_assert(!surf_cpu_model_pm);
20   xbt_assert(!surf_cpu_model_vm);
21
22   char *optim = xbt_cfg_get_string(_sg_cfg_set, "cpu/optim");
23   if (!strcmp(optim, "TI")) {
24     surf_cpu_model_init_ti();
25     return;
26   }
27
28   surf_cpu_model_pm = new simgrid::surf::CpuCas01Model();
29   xbt_dynar_push(all_existing_models, &surf_cpu_model_pm);
30
31   surf_cpu_model_vm  = new simgrid::surf::CpuCas01Model();
32   xbt_dynar_push(all_existing_models, &surf_cpu_model_vm);
33 }
34
35 namespace simgrid {
36 namespace surf {
37
38 CpuCas01Model::CpuCas01Model() : simgrid::surf::CpuModel()
39 {
40   char *optim = xbt_cfg_get_string(_sg_cfg_set, "cpu/optim");
41   int select = xbt_cfg_get_boolean(_sg_cfg_set, "cpu/maxmin_selective_update");
42
43   if (!strcmp(optim, "Full")) {
44     updateMechanism_ = UM_FULL;
45     selectiveUpdate_ = select;
46   } else if (!strcmp(optim, "Lazy")) {
47     updateMechanism_ = UM_LAZY;
48     selectiveUpdate_ = 1;
49     xbt_assert((select == 1)
50                ||
51                (xbt_cfg_is_default_value
52                 (_sg_cfg_set, "cpu/maxmin_selective_update")),
53                "Disabling selective update while using the lazy update mechanism is dumb!");
54   } else {
55     xbt_die("Unsupported optimization (%s) for this model", optim);
56   }
57
58   p_cpuRunningActionSetThatDoesNotNeedBeingChecked = new ActionList();
59
60   maxminSystem_ = lmm_system_new(selectiveUpdate_);
61
62   if (getUpdateMechanism() == UM_LAZY) {
63     actionHeap_ = xbt_heap_new(8, NULL);
64     xbt_heap_set_update_callback(actionHeap_,  surf_action_lmm_update_index_heap);
65     modifiedSet_ = new ActionLmmList();
66     maxminSystem_->keep_track = modifiedSet_;
67   }
68 }
69
70 CpuCas01Model::~CpuCas01Model()
71 {
72   lmm_system_free(maxminSystem_);
73   maxminSystem_ = NULL;
74
75   if (actionHeap_)
76     xbt_heap_free(actionHeap_);
77   delete modifiedSet_;
78
79   surf_cpu_model_pm = NULL;
80
81   delete p_cpuRunningActionSetThatDoesNotNeedBeingChecked;
82 }
83
84 Cpu *CpuCas01Model::createCpu(simgrid::s4u::Host *host, xbt_dynar_t speedPerPstate, int core)
85 {
86   return new CpuCas01(this, host, speedPerPstate, core);
87 }
88
89 double CpuCas01Model::next_occuring_event_full(double /*now*/)
90 {
91   return Model::shareResourcesMaxMin(getRunningActionSet(), maxminSystem_, lmm_solve);
92 }
93
94 /************
95  * Resource *
96  ************/
97 CpuCas01::CpuCas01(CpuCas01Model *model, simgrid::s4u::Host *host, xbt_dynar_t speedPerPstate, int core)
98 : Cpu(model, host,
99     lmm_constraint_new(model->getMaxminSystem(), this, core * xbt_dynar_get_as(speedPerPstate, 0/*pstate*/, double)),
100     speedPerPstate, core)
101 {
102 }
103
104 CpuCas01::~CpuCas01()
105 {
106   if (getModel() == surf_cpu_model_pm)
107     xbt_dynar_free(&speedPerPstate_);
108 }
109
110 xbt_dynar_t CpuCas01::getSpeedPeakList(){
111   return speedPerPstate_;
112 }
113
114 bool CpuCas01::isUsed()
115 {
116   return lmm_constraint_used(getModel()->getMaxminSystem(), getConstraint());
117 }
118
119 /** @brief take into account changes of speed (either load or max) */
120 void CpuCas01::onSpeedChange() {
121   lmm_variable_t var = NULL;
122   lmm_element_t elem = NULL;
123
124     lmm_update_constraint_bound(getModel()->getMaxminSystem(), getConstraint(),
125                                 coresAmount_ * speed_.scale * speed_.peak);
126     while ((var = lmm_get_var_from_cnst
127             (getModel()->getMaxminSystem(), getConstraint(), &elem))) {
128       CpuCas01Action *action = static_cast<CpuCas01Action*>(lmm_variable_id(var));
129
130       lmm_update_variable_bound(getModel()->getMaxminSystem(),
131                                 action->getVariable(),
132                                 speed_.scale * speed_.peak);
133     }
134
135   Cpu::onSpeedChange();
136 }
137
138 void CpuCas01::apply_event(tmgr_trace_iterator_t event, double value)
139 {
140   if (event == speed_.event) {
141     /* TODO (Hypervisor): do the same thing for constraint_core[i] */
142     xbt_assert(coresAmount_ == 1, "FIXME: add speed scaling code also for constraint_core[i]");
143
144     speed_.scale = value;
145     onSpeedChange();
146
147     tmgr_trace_event_unref(&speed_.event);
148   } else if (event == stateEvent_) {
149     /* TODO (Hypervisor): do the same thing for constraint_core[i] */
150     xbt_assert(coresAmount_ == 1, "FIXME: add state change code also for constraint_core[i]");
151
152     if (value > 0) {
153       if(isOff())
154         xbt_dynar_push_as(host_that_restart, char*, (char *)getName());
155       turnOn();
156     } else {
157       lmm_constraint_t cnst = getConstraint();
158       lmm_variable_t var = NULL;
159       lmm_element_t elem = NULL;
160       double date = surf_get_clock();
161
162       turnOff();
163
164       while ((var = lmm_get_var_from_cnst(getModel()->getMaxminSystem(), cnst, &elem))) {
165         Action *action = static_cast<Action*>(lmm_variable_id(var));
166
167         if (action->getState() == Action::State::running ||
168             action->getState() == Action::State::ready ||
169             action->getState() == Action::State::not_in_the_system) {
170           action->setFinishTime(date);
171           action->setState(Action::State::failed);
172         }
173       }
174     }
175     tmgr_trace_event_unref(&stateEvent_);
176
177   } else {
178     xbt_die("Unknown event!\n");
179   }
180 }
181
182 CpuAction *CpuCas01::execution_start(double size)
183 {
184
185   XBT_IN("(%s,%g)", getName(), size);
186   CpuCas01Action *action = new CpuCas01Action(getModel(), size, isOff(),
187       speed_.scale * speed_.peak, getConstraint());
188
189   XBT_OUT();
190   return action;
191 }
192
193 CpuAction *CpuCas01::sleep(double duration)
194 {
195   if (duration > 0)
196     duration = MAX(duration, sg_surf_precision);
197
198   XBT_IN("(%s,%g)", getName(), duration);
199   CpuCas01Action *action = new CpuCas01Action(getModel(), 1.0, isOff(),
200       speed_.scale * speed_.peak, getConstraint());
201
202   // FIXME: sleep variables should not consume 1.0 in lmm_expand
203   action->m_maxDuration = duration;
204   action->m_suspended = 2;
205   if (duration == NO_MAX_DURATION) {
206     /* Move to the *end* of the corresponding action set. This convention
207        is used to speed up update_resource_state  */
208     action->getStateSet()->erase(action->getStateSet()->iterator_to(*action));
209     action->p_stateSet = static_cast<CpuCas01Model*>(getModel())->p_cpuRunningActionSetThatDoesNotNeedBeingChecked;
210     action->getStateSet()->push_back(*action);
211   }
212
213   lmm_update_variable_weight(getModel()->getMaxminSystem(),
214       action->getVariable(), 0.0);
215   if (getModel()->getUpdateMechanism() == UM_LAZY) {     // remove action from the heap
216     action->heapRemove(getModel()->getActionHeap());
217     // this is necessary for a variable with weight 0 since such
218     // variables are ignored in lmm and we need to set its max_duration
219     // correctly at the next call to share_resources
220     getModel()->getModifiedSet()->push_front(*action);
221   }
222
223   XBT_OUT();
224   return action;
225 }
226
227 /**********
228  * Action *
229  **********/
230
231 CpuCas01Action::CpuCas01Action(Model *model, double cost, bool failed, double speed, lmm_constraint_t constraint)
232  : CpuAction(model, cost, failed,
233          lmm_variable_new(model->getMaxminSystem(), this,
234          1.0, speed, 1))
235 {
236   if (model->getUpdateMechanism() == UM_LAZY) {
237     m_indexHeap = -1;
238     m_lastUpdate = surf_get_clock();
239     m_lastValue = 0.0;
240   }
241   lmm_expand(model->getMaxminSystem(), constraint, getVariable(), 1.0);
242 }
243
244 }
245 }