Logo AND Algorithmique Numérique Distribuée

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