1 /* Copyright (c) 2009-2011. The SimGrid Team.
2 * All rights reserved. */
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. */
7 #include "cpu_cas01.hpp"
10 #include "maxmin_private.h"
11 #include "simgrid/sg_config.h"
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_cpu_cas, surf,
15 "Logging specific to the SURF CPU IMPROVED module");
19 cpu_running_action_set_that_does_not_need_being_checked = NULL;
24 void surf_cpu_model_init_Cas01()
26 char *optim = xbt_cfg_get_string(_sg_cfg_set, "cpu/optim");
31 if (!strcmp(optim, "TI")) {
32 surf_cpu_model = new CpuTiModel();
36 surf_cpu_model = new CpuCas01Model();
37 xbt_dynar_push(model_list, &surf_cpu_model);
40 CpuCas01Model::CpuCas01Model() : CpuModel("cpu")
42 CpuCas01ActionLmm action;
44 char *optim = xbt_cfg_get_string(_sg_cfg_set, "cpu/optim");
45 int select = xbt_cfg_get_boolean(_sg_cfg_set, "cpu/maxmin_selective_update");
47 if (!strcmp(optim, "Full")) {
48 p_updateMechanism = UM_FULL;
49 m_selectiveUpdate = select;
50 } else if (!strcmp(optim, "Lazy")) {
51 p_updateMechanism = UM_LAZY;
52 m_selectiveUpdate = 1;
53 xbt_assert((select == 1)
55 (xbt_cfg_is_default_value
56 (_sg_cfg_set, "cpu/maxmin_selective_update")),
57 "Disabling selective update while using the lazy update mechanism is dumb!");
59 xbt_die("Unsupported optimization (%s) for this model", optim);
62 cpu_running_action_set_that_does_not_need_being_checked =
63 xbt_swag_new(xbt_swag_offset(action, p_stateHookup));
65 if (p_updateMechanism == UM_LAZY) {
66 shareResources = &CpuCas01Model::shareResourcesLazy;
67 updateActionsState = &CpuCas01Model::updateActionsStateLazy;
69 } else if (p_updateMechanism == UM_FULL) {
70 shareResources = &CpuCas01Model::shareResourcesFull;
71 updateActionsState = &CpuCas01Model::updateActionsStateFull;
73 xbt_die("Invalid cpu update mechanism!");
75 if (!p_maxminSystem) {
76 p_maxminSystem = lmm_system_new(m_selectiveUpdate);
79 if (p_updateMechanism == UM_LAZY) {
80 p_actionHeap = xbt_heap_new(8, NULL);
81 xbt_heap_set_update_callback(p_actionHeap, surf_action_lmm_update_index_heap);
82 p_modifiedSet = xbt_swag_new(xbt_swag_offset(action, p_actionListHookup));
83 //TOREPAIR: cpu_model->model_private->maxmin_system->m_keepTrack = cpu_model->model_private->modified_set;
85 /* Define callbacks */
86 //TODO sg_platf_host_add_cb(parse_cpu_init);
87 //TODO sg_platf_postparse_add_cb(cpu_add_traces_cpu);
90 CpuCas01Model::~CpuCas01Model()
92 lmm_system_free(p_maxminSystem);
93 p_maxminSystem = NULL;
96 xbt_heap_free(p_actionHeap);
97 xbt_swag_free(p_modifiedSet);
99 surf_cpu_model = NULL;
101 xbt_swag_free(cpu_running_action_set_that_does_not_need_being_checked);
102 cpu_running_action_set_that_does_not_need_being_checked = NULL;
105 void CpuCas01Model::parseInit(sg_platf_host_cbarg_t host)
107 createResource(host->id,
116 CpuCas01LmmPtr CpuCas01Model::createResource(const char *name, double power_peak, double power_scale,
117 tmgr_trace_t power_trace, int core,
118 e_surf_resource_state_t state_initial,
119 tmgr_trace_t state_trace,
120 xbt_dict_t cpu_properties)
122 CpuCas01LmmPtr cpu = NULL;
124 xbt_assert(!surf_cpu_resource_priv(surf_cpu_resource_by_name(name)),
125 "Host '%s' declared several times in the platform file",
127 xbt_assert(power_peak > 0, "Power has to be >0");
128 xbt_assert(core > 0, "Invalid number of cores %d", core);
130 cpu = new CpuCas01Lmm(this, name, power_peak, power_scale, power_trace, core, state_initial, state_trace, cpu_properties);
132 xbt_lib_set(host_lib, name, SURF_CPU_LEVEL, this);
134 return (CpuCas01LmmPtr) xbt_lib_get_elm_or_null(host_lib, name);;
137 double CpuCas01Model::shareResourcesFull(double now)
139 CpuCas01ActionLmm action;
140 Model::shareResourcesMaxMin(p_runningActionSet,
141 xbt_swag_offset(action, p_variable),
142 p_maxminSystem, lmm_solve);
146 void CpuCas01Model::addTraces()
148 xbt_dict_cursor_t cursor = NULL;
149 char *trace_name, *elm;
150 static int called = 0;
155 /* connect all traces relative to hosts */
156 xbt_dict_foreach(trace_connect_list_host_avail, cursor, trace_name, elm) {
157 tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
158 CpuCas01LmmPtr host = (CpuCas01LmmPtr) surf_cpu_resource_by_name(elm);
160 xbt_assert(host, "Host %s undefined", elm);
161 xbt_assert(trace, "Trace %s undefined", trace_name);
164 tmgr_history_add_trace(history, trace, 0.0, 0, host);
167 xbt_dict_foreach(trace_connect_list_power, cursor, trace_name, elm) {
168 tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
169 CpuCas01LmmPtr host = (CpuCas01LmmPtr) surf_cpu_resource_by_name(elm);
171 xbt_assert(host, "Host %s undefined", elm);
172 xbt_assert(trace, "Trace %s undefined", trace_name);
175 tmgr_history_add_trace(history, trace, 0.0, 0, host);
182 CpuCas01Lmm::CpuCas01Lmm(CpuCas01ModelPtr model, const char *name, double powerPeak,
183 double powerScale, tmgr_trace_t powerTrace, int core,
184 e_surf_resource_state_t stateInitial, tmgr_trace_t stateTrace,
185 xbt_dict_t properties) :
186 CpuLmm(model, name, properties) {
187 m_powerPeak = powerPeak;
188 m_powerScale = powerScale;
190 p_stateCurrent = stateInitial;
192 p_powerEvent = tmgr_history_add_trace(history, powerTrace, 0.0, 0, this);
195 p_stateEvent = tmgr_history_add_trace(history, stateTrace, 0.0, 0, this);
197 p_constraint = lmm_constraint_new(p_model->p_maxminSystem, this, m_core * m_powerScale * m_powerPeak);
200 bool CpuCas01Lmm::isUsed()
202 return lmm_constraint_used(p_model->p_maxminSystem, p_constraint);
205 void CpuCas01Lmm::updateState(tmgr_trace_event_t event_type, double value, double date)
207 lmm_variable_t var = NULL;
208 lmm_element_t elem = NULL;
210 surf_watched_hosts();
212 if (event_type == p_powerEvent) {
213 m_powerScale = value;
214 lmm_update_constraint_bound(surf_cpu_model->p_maxminSystem, p_constraint,
215 m_core * m_powerScale *
218 TRACE_surf_host_set_power(date, m_name,
219 m_core * m_powerScale *
222 while ((var = lmm_get_var_from_cnst
223 (surf_cpu_model->p_maxminSystem, p_constraint, &elem))) {
224 CpuCas01ActionLmmPtr action = (CpuCas01ActionLmmPtr) lmm_variable_id(var);
225 lmm_update_variable_bound(surf_cpu_model->p_maxminSystem,
227 m_powerScale * m_powerPeak);
229 if (tmgr_trace_event_free(event_type))
231 } else if (event_type == p_stateEvent) {
233 p_stateCurrent = SURF_RESOURCE_ON;
235 lmm_constraint_t cnst = p_constraint;
237 p_stateCurrent = SURF_RESOURCE_OFF;
239 while ((var = lmm_get_var_from_cnst(surf_cpu_model->p_maxminSystem, cnst, &elem))) {
240 ActionPtr action = (ActionPtr) lmm_variable_id(var);
242 if (action->getState() == SURF_ACTION_RUNNING ||
243 action->getState() == SURF_ACTION_READY ||
244 action->getState() == SURF_ACTION_NOT_IN_THE_SYSTEM) {
245 action->m_finish = date;
246 action->setState(SURF_ACTION_FAILED);
250 if (tmgr_trace_event_free(event_type))
253 XBT_CRITICAL("Unknown event ! \n");
260 CpuActionPtr CpuCas01Lmm::execute(double size)
263 XBT_IN("(%s,%g)", m_name, size);
264 CpuCas01ActionLmmPtr action = new CpuCas01ActionLmm(surf_cpu_model, size, p_stateCurrent != SURF_RESOURCE_ON);
266 action->m_suspended = 0; /* Should be useless because of the
267 calloc but it seems to help valgrind... */
270 lmm_variable_new(surf_cpu_model->p_maxminSystem, action,
272 m_powerScale * m_powerPeak, 1);
273 if (surf_cpu_model->p_updateMechanism == UM_LAZY) {
274 action->m_indexHeap = -1;
275 action->m_lastUpdate = surf_get_clock();
276 action->m_lastValue = 0.0;
278 lmm_expand(surf_cpu_model->p_maxminSystem, p_constraint,
279 action->p_variable, 1.0);
284 CpuActionPtr CpuCas01Lmm::sleep(double duration)
287 duration = MAX(duration, MAXMIN_PRECISION);
289 XBT_IN("(%s,%g)", m_name, duration);
290 CpuCas01ActionLmmPtr action = (CpuCas01ActionLmmPtr) execute(1.0);
292 // FIXME: sleep variables should not consume 1.0 in lmm_expand
293 action->m_maxDuration = duration;
294 action->m_suspended = 2;
295 if (duration == NO_MAX_DURATION) {
296 /* Move to the *end* of the corresponding action set. This convention
297 is used to speed up update_resource_state */
298 xbt_swag_remove(action, action->p_stateSet);
299 action->p_stateSet = cpu_running_action_set_that_does_not_need_being_checked;
300 xbt_swag_insert(action, action->p_stateSet);
303 lmm_update_variable_weight(surf_cpu_model->p_maxminSystem,
304 action->p_variable, 0.0);
305 if (surf_cpu_model->p_updateMechanism == UM_LAZY) { // remove action from the heap
306 action->heapRemove(surf_cpu_model->p_actionHeap);
307 // this is necessary for a variable with weight 0 since such
308 // variables are ignored in lmm and we need to set its max_duration
309 // correctly at the next call to share_resources
310 xbt_swag_insert_at_head(action, surf_cpu_model->p_modifiedSet);