Logo AND Algorithmique Numérique Distribuée

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