Logo AND Algorithmique Numérique Distribuée

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