Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge lmm into base to avoid diamond inheritance
[simgrid.git] / src / surf / cpu_interface.cpp
1 #include "cpu_interface.hpp"
2
3 XBT_LOG_EXTERNAL_CATEGORY(surf_kernel);
4 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_cpu, surf,
5                                 "Logging specific to the SURF cpu module");
6
7 CpuModelPtr surf_cpu_model_pm;
8 CpuModelPtr surf_cpu_model_vm;
9
10 /*********
11  * Model *
12  *********/
13
14 void CpuModel::updateActionsStateLazy(double now, double /*delta*/)
15 {
16   CpuActionPtr action;
17   while ((xbt_heap_size(getActionHeap()) > 0)
18          && (double_equals(xbt_heap_maxkey(getActionHeap()), now))) {
19     action = static_cast<CpuActionPtr>(static_cast<ActionPtr>(xbt_heap_pop(getActionHeap())));
20     XBT_CDEBUG(surf_kernel, "Something happened to action %p", action);
21 #ifdef HAVE_TRACING
22     if (TRACE_is_enabled()) {
23       CpuPtr cpu = static_cast<CpuPtr>(lmm_constraint_id(lmm_get_cnst_from_var(getMaxminSystem(), action->getVariable(), 0)));
24       TRACE_surf_host_set_utilization(cpu->getName(), action->getCategory(),
25                                       lmm_variable_getvalue(action->getVariable()),
26                                       action->getLastUpdate(),
27                                       now - action->getLastUpdate());
28     }
29 #endif
30
31     action->finish();
32     XBT_CDEBUG(surf_kernel, "Action %p finished", action);
33
34     action->updateEnergy();
35
36     /* set the remains to 0 due to precision problems when updating the remaining amount */
37     action->setRemains(0);
38     action->setState(SURF_ACTION_DONE);
39     action->heapRemove(getActionHeap()); //FIXME: strange call since action was already popped
40   }
41 #ifdef HAVE_TRACING
42   if (TRACE_is_enabled()) {
43     //defining the last timestamp that we can safely dump to trace file
44     //without losing the event ascending order (considering all CPU's)
45     double smaller = -1;
46     ActionListPtr actionSet = getRunningActionSet();
47     for(ActionList::iterator it(actionSet->begin()), itend(actionSet->end())
48        ; it != itend ; ++it) {
49       action = static_cast<CpuActionPtr>(&*it);
50         if (smaller < 0) {
51           smaller = action->getLastUpdate();
52           continue;
53         }
54         if (action->getLastUpdate() < smaller) {
55           smaller = action->getLastUpdate();
56         }
57     }
58     if (smaller > 0) {
59       TRACE_last_timestamp_to_dump = smaller;
60     }
61   }
62 #endif
63   return;
64 }
65
66 void CpuModel::updateActionsStateFull(double now, double delta)
67 {
68   CpuActionPtr action = NULL;
69   ActionListPtr running_actions = getRunningActionSet();
70
71   for(ActionList::iterator it(running_actions->begin()), itNext=it, itend(running_actions->end())
72      ; it != itend ; it=itNext) {
73         ++itNext;
74     action = static_cast<CpuActionPtr>(&*it);
75 #ifdef HAVE_TRACING
76     if (TRACE_is_enabled()) {
77       CpuPtr x = (CpuPtr) lmm_constraint_id(lmm_get_cnst_from_var
78                               (getMaxminSystem(), action->getVariable(), 0));
79
80       TRACE_surf_host_set_utilization(x->getName(),
81                                       action->getCategory(),
82                                       lmm_variable_getvalue(action->getVariable()),
83                                       now - delta,
84                                       delta);
85       TRACE_last_timestamp_to_dump = now - delta;
86     }
87 #endif
88
89     action->updateRemains(lmm_variable_getvalue(action->getVariable()) * delta);
90
91
92     if (action->getMaxDuration() != NO_MAX_DURATION)
93       action->updateMaxDuration(delta);
94
95
96     if ((action->getRemains() <= 0) &&
97         (lmm_get_variable_weight(action->getVariable()) > 0)) {
98       action->finish();
99       action->setState(SURF_ACTION_DONE);
100     } else if ((action->getMaxDuration() != NO_MAX_DURATION) &&
101                (action->getMaxDuration() <= 0)) {
102       action->finish();
103       action->setState(SURF_ACTION_DONE);
104     }
105     action->updateEnergy();
106   }
107
108   return;
109 }
110
111 /************
112  * Resource *
113  ************/
114
115 Cpu::Cpu(ModelPtr model, const char *name, xbt_dict_t props,
116                  int core, double powerPeak, double powerScale)
117  : Resource(model, name, props)
118  , m_core(core)
119  , m_powerPeak(powerPeak)
120  , m_powerScale(powerScale)
121  , p_constraintCore(NULL)
122  , p_constraintCoreId(NULL)
123 {}
124
125 Cpu::Cpu(ModelPtr model, const char *name, xbt_dict_t props,
126                  lmm_constraint_t constraint, int core, double powerPeak, double powerScale)
127  : Resource(model, name, props, constraint)
128  , m_core(core)
129  , m_powerPeak(powerPeak)
130  , m_powerScale(powerScale)
131 {
132   /* At now, we assume that a VM does not have a multicore CPU. */
133   if (core > 1)
134     xbt_assert(model == surf_cpu_model_pm);
135
136   p_constraintCore = NULL;
137   p_constraintCoreId = NULL;
138   if (model->getUpdateMechanism() != UM_UNDEFINED) {
139         p_constraintCore = xbt_new(lmm_constraint_t, core);
140         p_constraintCoreId = xbt_new(void*, core);
141
142     int i;
143     for (i = 0; i < core; i++) {
144       /* just for a unique id, never used as a string. */
145       p_constraintCoreId[i] = bprintf("%s:%i", name, i);
146       p_constraintCore[i] = lmm_constraint_new(model->getMaxminSystem(), p_constraintCoreId[i], m_powerScale * m_powerPeak);
147     }
148   }
149 }
150
151 Cpu::~Cpu(){
152   if (getModel()->getUpdateMechanism() != UM_UNDEFINED){
153     for (int i = 0; i < m_core; i++) {
154           xbt_free(p_constraintCoreId[i]);
155     }
156     xbt_free(p_constraintCore);
157     xbt_free(p_constraintCoreId);
158   }
159 }
160
161 double Cpu::getSpeed(double load)
162 {
163   return load * m_powerPeak;
164 }
165
166 double Cpu::getAvailableSpeed()
167 {
168 /* number between 0 and 1 */
169   return m_powerScale;
170 }
171
172 int Cpu::getCore()
173 {
174   return m_core;
175 }
176
177 /**********
178  * Action *
179  **********/
180
181 void CpuAction::updateRemainingLazy(double now)
182 {
183   double delta = 0.0;
184
185   xbt_assert(getStateSet() == getModel()->getRunningActionSet(),
186       "You're updating an action that is not running.");
187
188   /* bogus priority, skip it */
189   xbt_assert(getPriority() > 0,
190       "You're updating an action that seems suspended.");
191
192   delta = now - m_lastUpdate;
193
194   if (m_remains > 0) {
195     XBT_CDEBUG(surf_kernel, "Updating action(%p): remains was %lf, last_update was: %lf", this, m_remains, m_lastUpdate);
196     double_update(&(m_remains), m_lastValue * delta);
197
198 #ifdef HAVE_TRACING
199     if (TRACE_is_enabled()) {
200       CpuPtr cpu = static_cast<CpuPtr>(lmm_constraint_id(lmm_get_cnst_from_var(getModel()->getMaxminSystem(), getVariable(), 0)));
201       TRACE_surf_host_set_utilization(cpu->getName(), getCategory(), m_lastValue, m_lastUpdate, now - m_lastUpdate);
202     }
203 #endif
204     XBT_CDEBUG(surf_kernel, "Updating action(%p): remains is now %lf", this, m_remains);
205   }
206
207   m_lastUpdate = now;
208   m_lastValue = lmm_variable_getvalue(getVariable());
209 }
210
211 void CpuAction::setBound(double bound)
212 {
213   XBT_IN("(%p,%g)", this, bound);
214   m_bound = bound;
215   lmm_update_variable_bound(getModel()->getMaxminSystem(), getVariable(), bound);
216
217   if (getModel()->getUpdateMechanism() == UM_LAZY)
218         heapRemove(getModel()->getActionHeap());
219   XBT_OUT();
220 }
221
222 /*
223  *
224  * This function formulates a constraint problem that pins a given task to
225  * particular cores. Currently, it is possible to pin a task to an exactly one
226  * specific core. The system links the variable object of the task to the
227  * per-core constraint object.
228  *
229  * But, the taskset command on Linux takes a mask value specifying a CPU
230  * affinity setting of a given task. If the mask value is 0x03, the given task
231  * will be executed on the first core (CPU0) or the second core (CPU1) on the
232  * given PM. The schedular will determine appropriate placements of tasks,
233  * considering given CPU affinities and task activities.
234  *
235  * How should the system formulate constraint problems for an affinity to
236  * multiple cores?
237  *
238  * The cpu argument must be the host where the task is being executed. The
239  * action object does not have the information about the location where the
240  * action is being executed.
241  */
242 void CpuAction::setAffinity(CpuPtr cpu, unsigned long mask)
243 {
244   lmm_variable_t var_obj = getVariable();
245   XBT_IN("(%p,%lx)", this, mask);
246
247   {
248     unsigned long nbits = 0;
249
250     /* FIXME: There is much faster algorithms doing this. */
251     for (int i = 0; i < cpu->m_core; i++) {
252       unsigned long has_affinity = (1UL << i) & mask;
253       if (has_affinity)
254         nbits += 1;
255     }
256
257     if (nbits > 1) {
258       XBT_CRITICAL("Do not specify multiple cores for an affinity mask.");
259       XBT_CRITICAL("See the comment in cpu_action_set_affinity().");
260       DIE_IMPOSSIBLE;
261     }
262   }
263
264   for (int i = 0; i < cpu->m_core; i++) {
265     XBT_DEBUG("clear affinity %p to cpu-%d@%s", this, i,  cpu->getName());
266     lmm_shrink(cpu->getModel()->getMaxminSystem(), cpu->p_constraintCore[i], var_obj);
267
268     unsigned long has_affinity = (1UL << i) & mask;
269     if (has_affinity) {
270       /* This function only accepts an affinity setting on the host where the
271        * task is now running. In future, a task might move to another host.
272        * But, at this moment, this function cannot take an affinity setting on
273        * that future host.
274        *
275        * It might be possible to extend the code to allow this function to
276        * accept affinity settings on a future host. We might be able to assign
277        * zero to elem->value to maintain such inactive affinity settings in the
278        * system. But, this will make the system complex. */
279       XBT_DEBUG("set affinity %p to cpu-%d@%s", this, i, cpu->getName());
280       lmm_expand(cpu->getModel()->getMaxminSystem(), cpu->p_constraintCore[i], var_obj, 1.0);
281     }
282   }
283
284   if (cpu->getModel()->getUpdateMechanism() == UM_LAZY) {
285     /* FIXME (hypervisor): Do we need to do something for the LAZY mode? */
286   }
287
288   XBT_OUT();
289 }