Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
9d99c6a86a607b432e91b10a734a418a6a48071a
[simgrid.git] / src / surf / cpu_cas01.cpp
1 /* Copyright (c) 2009-2011, 2013. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "cpu_cas01.hpp"
8 #include "cpu_ti.hpp"
9 #include "maxmin_private.hpp"
10 #include "simgrid/sg_config.h"
11
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_cpu_cas, surf_cpu,
13                                 "Logging specific to the SURF CPU IMPROVED module");
14
15 /*************
16  * CallBacks *
17  *************/
18
19 static void parse_cpu_init(sg_platf_host_cbarg_t host){
20   ((CpuCas01ModelPtr)surf_cpu_model_pm)->parseInit(host);
21 }
22
23 static void cpu_add_traces_cpu(){
24   surf_cpu_model_pm->addTraces();
25 }
26
27 static void cpu_define_callbacks()
28 {
29   sg_platf_host_add_cb(parse_cpu_init);
30   sg_platf_postparse_add_cb(cpu_add_traces_cpu);
31 }
32
33 /*********
34  * Model *
35  *********/
36 void surf_cpu_model_init_Cas01()
37 {
38   char *optim = xbt_cfg_get_string(_sg_cfg_set, "cpu/optim");
39
40   xbt_assert(!surf_cpu_model_pm);
41   xbt_assert(!surf_cpu_model_vm);
42
43   if (!strcmp(optim, "TI")) {
44     surf_cpu_model_init_ti();
45     return;
46   }
47
48   surf_cpu_model_pm = new CpuCas01Model();
49   surf_cpu_model_vm  = new CpuCas01Model();
50
51   cpu_define_callbacks();
52   ModelPtr model_pm = static_cast<ModelPtr>(surf_cpu_model_pm);
53   ModelPtr model_vm = static_cast<ModelPtr>(surf_cpu_model_vm);
54   xbt_dynar_push(model_list, &model_pm);
55   xbt_dynar_push(model_list, &model_vm);
56 }
57
58 CpuCas01Model::CpuCas01Model() : CpuModel("cpu")
59 {
60   char *optim = xbt_cfg_get_string(_sg_cfg_set, "cpu/optim");
61   int select = xbt_cfg_get_boolean(_sg_cfg_set, "cpu/maxmin_selective_update");
62
63   if (!strcmp(optim, "Full")) {
64     p_updateMechanism = UM_FULL;
65     m_selectiveUpdate = select;
66   } else if (!strcmp(optim, "Lazy")) {
67     p_updateMechanism = UM_LAZY;
68     m_selectiveUpdate = 1;
69     xbt_assert((select == 1)
70                ||
71                (xbt_cfg_is_default_value
72                 (_sg_cfg_set, "cpu/maxmin_selective_update")),
73                "Disabling selective update while using the lazy update mechanism is dumb!");
74   } else {
75     xbt_die("Unsupported optimization (%s) for this model", optim);
76   }
77
78   p_cpuRunningActionSetThatDoesNotNeedBeingChecked = new ActionList();
79
80   if (getUpdateMechanism() == UM_LAZY) {
81         shareResources = &CpuCas01Model::shareResourcesLazy;
82         updateActionsState = &CpuCas01Model::updateActionsStateLazy;
83
84   } else if (getUpdateMechanism() == UM_FULL) {
85         shareResources = &CpuCas01Model::shareResourcesFull;
86         updateActionsState = &CpuCas01Model::updateActionsStateFull;
87   } else
88     xbt_die("Invalid cpu update mechanism!");
89
90   if (!p_maxminSystem) {
91     p_maxminSystem = lmm_system_new(m_selectiveUpdate);
92   }
93
94   if (getUpdateMechanism() == UM_LAZY) {
95     p_actionHeap = xbt_heap_new(8, NULL);
96     xbt_heap_set_update_callback(p_actionHeap,  surf_action_lmm_update_index_heap);
97     p_modifiedSet = new ActionLmmList();
98     p_maxminSystem->keep_track = p_modifiedSet;
99   }
100 }
101
102 CpuCas01Model::~CpuCas01Model()
103 {
104   lmm_system_free(p_maxminSystem);
105   p_maxminSystem = NULL;
106
107   if (p_actionHeap)
108     xbt_heap_free(p_actionHeap);
109   delete p_modifiedSet;
110
111   surf_cpu_model_pm = NULL;
112
113   delete p_cpuRunningActionSetThatDoesNotNeedBeingChecked;
114 }
115
116 void CpuCas01Model::parseInit(sg_platf_host_cbarg_t host)
117 {
118   createResource(host->id,
119         host->power_peak,
120         host->pstate,
121         host->power_scale,
122         host->power_trace,
123         host->core_amount,
124         host->initial_state,
125         host->state_trace,
126         host->properties);
127 }
128
129 CpuPtr CpuCas01Model::createResource(const char *name, xbt_dynar_t power_peak,
130                                   int pstate, double power_scale,
131                           tmgr_trace_t power_trace, int core,
132                           e_surf_resource_state_t state_initial,
133                           tmgr_trace_t state_trace,
134                           xbt_dict_t cpu_properties)
135 {
136   CpuPtr cpu = NULL;
137   xbt_assert(!surf_cpu_resource_priv(surf_cpu_resource_by_name(name)),
138              "Host '%s' declared several times in the platform file",
139              name);
140   xbt_assert(xbt_dynar_getfirst_as(power_peak, double) > 0.0,
141       "Power has to be >0.0");
142   xbt_assert(core > 0, "Invalid number of cores %d", core);
143
144   cpu = new CpuCas01Lmm(this, name, power_peak, pstate, power_scale, power_trace, core, state_initial, state_trace, cpu_properties);
145   xbt_lib_set(host_lib, name, SURF_CPU_LEVEL, static_cast<ResourcePtr>(cpu));
146
147   return cpu;
148 }
149
150 double CpuCas01Model::shareResourcesFull(double /*now*/)
151 {
152   return Model::shareResourcesMaxMin(getRunningActionSet(),
153                              p_maxminSystem, lmm_solve);
154 }
155
156 void CpuCas01Model::addTraces()
157 {
158   xbt_dict_cursor_t cursor = NULL;
159   char *trace_name, *elm;
160   static int called = 0;
161   if (called)
162     return;
163   called = 1;
164
165   /* connect all traces relative to hosts */
166   xbt_dict_foreach(trace_connect_list_host_avail, cursor, trace_name, elm) {
167     tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
168     CpuCas01LmmPtr host = static_cast<CpuCas01LmmPtr>(surf_cpu_resource_priv(surf_cpu_resource_by_name(elm)));
169
170     xbt_assert(host, "Host %s undefined", elm);
171     xbt_assert(trace, "Trace %s undefined", trace_name);
172
173     host->p_stateEvent = tmgr_history_add_trace(history, trace, 0.0, 0, static_cast<ResourcePtr>(host));
174   }
175
176   xbt_dict_foreach(trace_connect_list_power, cursor, trace_name, elm) {
177     tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
178     CpuCas01LmmPtr host = dynamic_cast<CpuCas01LmmPtr>(static_cast<ResourcePtr>(surf_cpu_resource_priv(surf_cpu_resource_by_name(elm))));
179
180     xbt_assert(host, "Host %s undefined", elm);
181     xbt_assert(trace, "Trace %s undefined", trace_name);
182
183     host->p_powerEvent =
184         tmgr_history_add_trace(history, trace, 0.0, 0, static_cast<ResourcePtr>(host));
185   }
186 }
187
188 /************
189  * Resource *
190  ************/
191 CpuCas01Lmm::CpuCas01Lmm(CpuCas01ModelPtr model_, const char *name, xbt_dynar_t powerPeak,
192                          int pstate, double powerScale, tmgr_trace_t powerTrace, int core,
193                          e_surf_resource_state_t stateInitial, tmgr_trace_t stateTrace,
194                          xbt_dict_t properties)
195 : Resource(model_, name, properties)
196 , CpuLmm(lmm_constraint_new(getModel()->getMaxminSystem(), this, core * powerScale * xbt_dynar_get_as(powerPeak, pstate, double)),
197                  core, xbt_dynar_get_as(powerPeak, pstate, double), powerScale) {
198   p_powerEvent = NULL;
199   p_powerPeakList = powerPeak;
200   m_pstate = pstate;
201
202   p_energy = xbt_new(s_energy_cpu_cas01_t, 1);
203   p_energy->total_energy = 0;
204   p_energy->power_range_watts_list = getWattsRangeList();
205   p_energy->last_updated = surf_get_clock();
206
207   XBT_DEBUG("CPU create: peak=%f, pstate=%d", m_powerPeak, m_pstate);
208
209   m_core = core;
210   m_stateCurrent = stateInitial;
211   if (powerTrace)
212     p_powerEvent = tmgr_history_add_trace(history, powerTrace, 0.0, 0, static_cast<ResourcePtr>(this));
213
214   if (stateTrace)
215     p_stateEvent = tmgr_history_add_trace(history, stateTrace, 0.0, 0, static_cast<ResourcePtr>(this));
216 }
217
218 CpuCas01Lmm::~CpuCas01Lmm(){
219   unsigned int iter;
220   xbt_dynar_t power_tuple = NULL;
221   xbt_dynar_foreach(p_energy->power_range_watts_list, iter, power_tuple)
222     xbt_dynar_free(&power_tuple);
223   xbt_dynar_free(&p_energy->power_range_watts_list);
224   xbt_dynar_free(&p_powerPeakList);
225   xbt_free(p_energy);
226 }
227
228 bool CpuCas01Lmm::isUsed()
229 {
230   return lmm_constraint_used(getModel()->getMaxminSystem(), constraint());
231 }
232
233 void CpuCas01Lmm::updateState(tmgr_trace_event_t event_type, double value, double date)
234 {
235   lmm_variable_t var = NULL;
236   lmm_element_t elem = NULL;
237
238   if (event_type == p_powerEvent) {
239         /* TODO (Hypervisor): do the same thing for constraint_core[i] */
240         xbt_assert(m_core == 1, "FIXME: add power scaling code also for constraint_core[i]");
241
242     m_powerScale = value;
243     lmm_update_constraint_bound(surf_cpu_model_pm->getMaxminSystem(), constraint(),
244                                 m_core * m_powerScale *
245                                 m_powerPeak);
246 #ifdef HAVE_TRACING
247     TRACE_surf_host_set_power(date, getName(),
248                               m_core * m_powerScale *
249                               m_powerPeak);
250 #endif
251     while ((var = lmm_get_var_from_cnst
252             (surf_cpu_model_pm->getMaxminSystem(), constraint(), &elem))) {
253       CpuCas01ActionLmmPtr action = static_cast<CpuCas01ActionLmmPtr>(static_cast<ActionLmmPtr>(lmm_variable_id(var)));
254
255       lmm_update_variable_bound(surf_cpu_model_pm->getMaxminSystem(),
256                                 action->getVariable(),
257                                 m_powerScale * m_powerPeak);
258     }
259     if (tmgr_trace_event_free(event_type))
260       p_powerEvent = NULL;
261   } else if (event_type == p_stateEvent) {
262         /* TODO (Hypervisor): do the same thing for constraint_core[i] */
263     xbt_assert(m_core == 1, "FIXME: add state change code also for constraint_core[i]");
264
265     if (value > 0) {
266       if(m_stateCurrent == SURF_RESOURCE_OFF)
267         xbt_dynar_push_as(host_that_restart, char*, (char *)getName());
268       m_stateCurrent = SURF_RESOURCE_ON;
269     } else {
270       lmm_constraint_t cnst = constraint();
271
272       m_stateCurrent = SURF_RESOURCE_OFF;
273
274       while ((var = lmm_get_var_from_cnst(surf_cpu_model_pm->getMaxminSystem(), cnst, &elem))) {
275         ActionLmmPtr action = static_cast<ActionLmmPtr>(lmm_variable_id(var));
276
277         if (action->getState() == SURF_ACTION_RUNNING ||
278             action->getState() == SURF_ACTION_READY ||
279             action->getState() == SURF_ACTION_NOT_IN_THE_SYSTEM) {
280           action->setFinishTime(date);
281           action->setState(SURF_ACTION_FAILED);
282         }
283       }
284     }
285     if (tmgr_trace_event_free(event_type))
286       p_stateEvent = NULL;
287   } else {
288     XBT_CRITICAL("Unknown event ! \n");
289     xbt_abort();
290   }
291
292   return;
293 }
294
295 CpuActionPtr CpuCas01Lmm::execute(double size)
296 {
297
298   XBT_IN("(%s,%g)", getName(), size);
299   CpuCas01ActionLmmPtr action = new CpuCas01ActionLmm(surf_cpu_model_pm, size, m_stateCurrent != SURF_RESOURCE_ON,
300                                                               m_powerScale * m_powerPeak, constraint());
301
302   XBT_OUT();
303   return action;
304 }
305
306 CpuActionPtr CpuCas01Lmm::sleep(double duration)
307 {
308   if (duration > 0)
309     duration = MAX(duration, MAXMIN_PRECISION);
310
311   XBT_IN("(%s,%g)", getName(), duration);
312   CpuCas01ActionLmmPtr action = new CpuCas01ActionLmm(surf_cpu_model_pm, 1.0, m_stateCurrent != SURF_RESOURCE_ON,
313                                                       m_powerScale * m_powerPeak, constraint());
314
315
316   // FIXME: sleep variables should not consume 1.0 in lmm_expand
317   action->m_maxDuration = duration;
318   action->m_suspended = 2;
319   if (duration == NO_MAX_DURATION) {
320     /* Move to the *end* of the corresponding action set. This convention
321        is used to speed up update_resource_state  */
322         action->getStateSet()->erase(action->getStateSet()->iterator_to(*action));
323     action->p_stateSet = static_cast<CpuCas01ModelPtr>(getModel())->p_cpuRunningActionSetThatDoesNotNeedBeingChecked;
324     action->getStateSet()->push_back(*action);
325   }
326
327   lmm_update_variable_weight(surf_cpu_model_pm->getMaxminSystem(),
328                              action->getVariable(), 0.0);
329   if (surf_cpu_model_pm->getUpdateMechanism() == UM_LAZY) {     // remove action from the heap
330     action->heapRemove(surf_cpu_model_pm->getActionHeap());
331     // this is necessary for a variable with weight 0 since such
332     // variables are ignored in lmm and we need to set its max_duration
333     // correctly at the next call to share_resources
334     surf_cpu_model_pm->getModifiedSet()->push_front(*action);
335   }
336
337   XBT_OUT();
338   return action;
339 }
340
341 xbt_dynar_t CpuCas01Lmm::getWattsRangeList()
342 {
343         xbt_dynar_t power_range_list;
344         xbt_dynar_t power_tuple;
345         int i = 0, pstate_nb=0;
346         xbt_dynar_t current_power_values;
347         double min_power, max_power;
348
349         if (getProperties() == NULL)
350                 return NULL;
351
352         char* all_power_values_str = (char*)xbt_dict_get_or_null(getProperties(), "power_per_state");
353
354         if (all_power_values_str == NULL)
355                 return NULL;
356
357
358         power_range_list = xbt_dynar_new(sizeof(xbt_dynar_t), NULL);
359         xbt_dynar_t all_power_values = xbt_str_split(all_power_values_str, ",");
360
361         pstate_nb = xbt_dynar_length(all_power_values);
362         for (i=0; i< pstate_nb; i++)
363         {
364                 /* retrieve the power values associated with the current pstate */
365                 current_power_values = xbt_str_split(xbt_dynar_get_as(all_power_values, i, char*), ":");
366                 xbt_assert(xbt_dynar_length(current_power_values) > 1,
367                                 "Power properties incorrectly defined - could not retrieve min and max power values for host %s",
368                                 getName());
369
370                 /* min_power corresponds to the idle power (cpu load = 0) */
371                 /* max_power is the power consumed at 100% cpu load       */
372                 min_power = atof(xbt_dynar_get_as(current_power_values, 0, char*));
373                 max_power = atof(xbt_dynar_get_as(current_power_values, 1, char*));
374
375                 power_tuple = xbt_dynar_new(sizeof(double), NULL);
376                 xbt_dynar_push_as(power_tuple, double, min_power);
377                 xbt_dynar_push_as(power_tuple, double, max_power);
378
379                 xbt_dynar_push_as(power_range_list, xbt_dynar_t, power_tuple);
380                 xbt_dynar_free(&current_power_values);
381         }
382         xbt_dynar_free(&all_power_values);
383         return power_range_list;
384 }
385
386 /**
387  * Computes the power consumed by the host according to the current pstate and processor load
388  *
389  */
390 double CpuCas01Lmm::getCurrentWattsValue(double cpu_load)
391 {
392         xbt_dynar_t power_range_list = p_energy->power_range_watts_list;
393
394         if (power_range_list == NULL)
395         {
396                 XBT_DEBUG("No power range properties specified for host %s", getName());
397                 return 0;
398         }
399         xbt_assert(xbt_dynar_length(power_range_list) == xbt_dynar_length(p_powerPeakList),
400                                                 "The number of power ranges in the properties does not match the number of pstates for host %s",
401                                                 getName());
402
403     /* retrieve the power values associated with the current pstate */
404     xbt_dynar_t current_power_values = xbt_dynar_get_as(power_range_list, m_pstate, xbt_dynar_t);
405
406     /* min_power corresponds to the idle power (cpu load = 0) */
407     /* max_power is the power consumed at 100% cpu load       */
408     double min_power = xbt_dynar_get_as(current_power_values, 0, double);
409     double max_power = xbt_dynar_get_as(current_power_values, 1, double);
410     double power_slope = max_power - min_power;
411
412     double current_power = min_power + cpu_load * power_slope;
413
414         XBT_DEBUG("[get_current_watts] min_power=%f, max_power=%f, slope=%f", min_power, max_power, power_slope);
415     XBT_DEBUG("[get_current_watts] Current power (watts) = %f, load = %f", current_power, cpu_load);
416
417         return current_power;
418 }
419
420 /**
421  * Updates the total energy consumed as the sum of the current energy and
422  *                                               the energy consumed by the current action
423  */
424 void CpuCas01Lmm::updateEnergy(double cpu_load)
425 {
426   double start_time = p_energy->last_updated;
427   double finish_time = surf_get_clock();
428
429   XBT_DEBUG("[cpu_update_energy] action time interval=(%f-%f), current power peak=%f, current pstate=%d",
430                   start_time, finish_time, m_powerPeak, m_pstate);
431   double current_energy = p_energy->total_energy;
432   double action_energy = getCurrentWattsValue(cpu_load)*(finish_time-start_time);
433
434   p_energy->total_energy = current_energy + action_energy;
435   p_energy->last_updated = finish_time;
436
437   XBT_DEBUG("[cpu_update_energy] old_energy_value=%f, action_energy_value=%f", current_energy, action_energy);
438 }
439
440 double CpuCas01Lmm::getCurrentPowerPeak()
441 {
442   return m_powerPeak;
443 }
444
445 double CpuCas01Lmm::getPowerPeakAt(int pstate_index)
446 {
447   xbt_dynar_t plist = p_powerPeakList;
448   xbt_assert((pstate_index <= (int)xbt_dynar_length(plist)), "Invalid parameters (pstate index out of bounds)");
449
450   return xbt_dynar_get_as(plist, pstate_index, double);
451 }
452
453 int CpuCas01Lmm::getNbPstates()
454 {
455   return xbt_dynar_length(p_powerPeakList);
456 }
457
458 void CpuCas01Lmm::setPowerPeakAt(int pstate_index)
459 {
460   xbt_dynar_t plist = p_powerPeakList;
461   xbt_assert((pstate_index <= (int)xbt_dynar_length(plist)), "Invalid parameters (pstate index out of bounds)");
462
463   double new_power_peak = xbt_dynar_get_as(plist, pstate_index, double);
464   m_pstate = pstate_index;
465   m_powerPeak = new_power_peak;
466 }
467
468 double CpuCas01Lmm::getConsumedEnergy()
469 {
470   return p_energy->total_energy;
471 }
472
473 /**********
474  * Action *
475  **********/
476
477 CpuCas01ActionLmm::CpuCas01ActionLmm(ModelPtr model_, double cost, bool failed, double power, lmm_constraint_t constraint)
478  : Action(model_, cost, failed)
479  , CpuActionLmm(lmm_variable_new(getModel()->getMaxminSystem(), static_cast<ActionLmmPtr>(this),
480                         m_priority,
481                 power, 1))
482 {
483   m_suspended = 0;     /* Should be useless because of the
484                                                            calloc but it seems to help valgrind... */
485
486   if (getModel()->getUpdateMechanism() == UM_LAZY) {
487     m_indexHeap = -1;
488     m_lastUpdate = surf_get_clock();
489     m_lastValue = 0.0;
490   }
491   lmm_expand(getModel()->getMaxminSystem(), constraint, getVariable(), 1.0);
492 }
493
494
495 /**
496  * Update the CPU total energy for a finished action
497  *
498  */
499 void CpuCas01ActionLmm::updateEnergy()
500 {
501   CpuCas01LmmPtr cpu  = static_cast<CpuCas01LmmPtr>(lmm_constraint_id(lmm_get_cnst_from_var
502                                                                                   (getModel()->getMaxminSystem(),
503                                                                                                   getVariable(), 0)));
504
505   if(cpu->p_energy->last_updated < surf_get_clock()) {
506         double load = lmm_constraint_get_usage(cpu->constraint()) / cpu->m_powerPeak;
507     cpu->updateEnergy(load);
508   }
509 }