Logo AND Algorithmique Numérique Distribuée

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