Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
dtd update for new cluster routing options
[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 (p_constraintCoreId){
153     for (int i = 0; i < m_core; i++) {
154           xbt_free(p_constraintCoreId[i]);
155     }
156     xbt_free(p_constraintCore);
157   }
158   if (p_constraintCoreId)
159     xbt_free(p_constraintCoreId);
160 }
161
162 double Cpu::getSpeed(double load)
163 {
164   return load * m_powerPeak;
165 }
166
167 double Cpu::getAvailableSpeed()
168 {
169 /* number between 0 and 1 */
170   return m_powerScale;
171 }
172
173 int Cpu::getCore()
174 {
175   return m_core;
176 }
177
178 /**********
179  * Action *
180  **********/
181
182 void CpuAction::updateRemainingLazy(double now)
183 {
184   double delta = 0.0;
185
186   xbt_assert(getStateSet() == getModel()->getRunningActionSet(),
187       "You're updating an action that is not running.");
188
189   /* bogus priority, skip it */
190   xbt_assert(getPriority() > 0,
191       "You're updating an action that seems suspended.");
192
193   delta = now - m_lastUpdate;
194
195   if (m_remains > 0) {
196     XBT_CDEBUG(surf_kernel, "Updating action(%p): remains was %lf, last_update was: %lf", this, m_remains, m_lastUpdate);
197     double_update(&(m_remains), m_lastValue * delta);
198
199 #ifdef HAVE_TRACING
200     if (TRACE_is_enabled()) {
201       CpuPtr cpu = static_cast<CpuPtr>(lmm_constraint_id(lmm_get_cnst_from_var(getModel()->getMaxminSystem(), getVariable(), 0)));
202       TRACE_surf_host_set_utilization(cpu->getName(), getCategory(), m_lastValue, m_lastUpdate, now - m_lastUpdate);
203     }
204 #endif
205     XBT_CDEBUG(surf_kernel, "Updating action(%p): remains is now %lf", this, m_remains);
206   }
207
208   m_lastUpdate = now;
209   m_lastValue = lmm_variable_getvalue(getVariable());
210 }
211
212 void CpuAction::setBound(double bound)
213 {
214   XBT_IN("(%p,%g)", this, bound);
215   m_bound = bound;
216   lmm_update_variable_bound(getModel()->getMaxminSystem(), getVariable(), bound);
217
218   if (getModel()->getUpdateMechanism() == UM_LAZY)
219         heapRemove(getModel()->getActionHeap());
220   XBT_OUT();
221 }
222
223 /*
224  *
225  * This function formulates a constraint problem that pins a given task to
226  * particular cores. Currently, it is possible to pin a task to an exactly one
227  * specific core. The system links the variable object of the task to the
228  * per-core constraint object.
229  *
230  * But, the taskset command on Linux takes a mask value specifying a CPU
231  * affinity setting of a given task. If the mask value is 0x03, the given task
232  * will be executed on the first core (CPU0) or the second core (CPU1) on the
233  * given PM. The schedular will determine appropriate placements of tasks,
234  * considering given CPU affinities and task activities.
235  *
236  * How should the system formulate constraint problems for an affinity to
237  * multiple cores?
238  *
239  * The cpu argument must be the host where the task is being executed. The
240  * action object does not have the information about the location where the
241  * action is being executed.
242  */
243 void CpuAction::setAffinity(CpuPtr cpu, unsigned long mask)
244 {
245   lmm_variable_t var_obj = getVariable();
246   XBT_IN("(%p,%lx)", this, mask);
247
248   {
249     unsigned long nbits = 0;
250
251     /* FIXME: There is much faster algorithms doing this. */
252     for (int i = 0; i < cpu->m_core; i++) {
253       unsigned long has_affinity = (1UL << i) & mask;
254       if (has_affinity)
255         nbits += 1;
256     }
257
258     if (nbits > 1) {
259       XBT_CRITICAL("Do not specify multiple cores for an affinity mask.");
260       XBT_CRITICAL("See the comment in cpu_action_set_affinity().");
261       DIE_IMPOSSIBLE;
262     }
263   }
264
265   for (int i = 0; i < cpu->m_core; i++) {
266     XBT_DEBUG("clear affinity %p to cpu-%d@%s", this, i,  cpu->getName());
267     lmm_shrink(cpu->getModel()->getMaxminSystem(), cpu->p_constraintCore[i], var_obj);
268
269     unsigned long has_affinity = (1UL << i) & mask;
270     if (has_affinity) {
271       /* This function only accepts an affinity setting on the host where the
272        * task is now running. In future, a task might move to another host.
273        * But, at this moment, this function cannot take an affinity setting on
274        * that future host.
275        *
276        * It might be possible to extend the code to allow this function to
277        * accept affinity settings on a future host. We might be able to assign
278        * zero to elem->value to maintain such inactive affinity settings in the
279        * system. But, this will make the system complex. */
280       XBT_DEBUG("set affinity %p to cpu-%d@%s", this, i, cpu->getName());
281       lmm_expand(cpu->getModel()->getMaxminSystem(), cpu->p_constraintCore[i], var_obj, 1.0);
282     }
283   }
284
285   if (cpu->getModel()->getUpdateMechanism() == UM_LAZY) {
286     /* FIXME (hypervisor): Do we need to do something for the LAZY mode? */
287   }
288
289   XBT_OUT();
290 }