Logo AND Algorithmique Numérique Distribuée

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