Logo AND Algorithmique Numérique Distribuée

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