Logo AND Algorithmique Numérique Distribuée

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