Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix surf_usage
[simgrid.git] / src / surf / cpu_cas01.cpp
1 /* Copyright (c) 2009-2011. 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,
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)->parseInit(host);
27 }
28
29 static void cpu_add_traces_cpu(){
30   surf_cpu_model->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   if (surf_cpu_model)
47     return;
48
49   if (!strcmp(optim, "TI")) {
50     surf_cpu_model = new CpuTiModel();
51     return;
52   }
53
54   surf_cpu_model = new CpuCas01Model();
55   cpu_define_callbacks();
56   ModelPtr model = static_cast<ModelPtr>(surf_cpu_model);
57   xbt_dynar_push(model_list, &model);
58 }
59
60 CpuCas01Model::CpuCas01Model() : CpuModel("cpu")
61 {
62   CpuCas01ActionLmm action;
63
64   char *optim = xbt_cfg_get_string(_sg_cfg_set, "cpu/optim");
65   int select = xbt_cfg_get_boolean(_sg_cfg_set, "cpu/maxmin_selective_update");
66
67   if (!strcmp(optim, "Full")) {
68     p_updateMechanism = UM_FULL;
69     m_selectiveUpdate = select;
70   } else if (!strcmp(optim, "Lazy")) {
71     p_updateMechanism = UM_LAZY;
72     m_selectiveUpdate = 1;
73     xbt_assert((select == 1)
74                ||
75                (xbt_cfg_is_default_value
76                 (_sg_cfg_set, "cpu/maxmin_selective_update")),
77                "Disabling selective update while using the lazy update mechanism is dumb!");
78   } else {
79     xbt_die("Unsupported optimization (%s) for this model", optim);
80   }
81
82   cpu_running_action_set_that_does_not_need_being_checked =
83       xbt_swag_new(xbt_swag_offset(action, p_stateHookup));
84
85   if (p_updateMechanism == UM_LAZY) {
86         shareResources = &CpuCas01Model::shareResourcesLazy;
87         updateActionsState = &CpuCas01Model::updateActionsStateLazy;
88
89   } else if (p_updateMechanism == UM_FULL) {
90         shareResources = &CpuCas01Model::shareResourcesFull;
91         updateActionsState = &CpuCas01Model::updateActionsStateFull;
92   } else
93     xbt_die("Invalid cpu update mechanism!");
94
95   if (!p_maxminSystem) {
96     p_maxminSystem = lmm_system_new(m_selectiveUpdate);
97   }
98
99   if (p_updateMechanism == UM_LAZY) {
100     p_actionHeap = xbt_heap_new(8, NULL);
101     xbt_heap_set_update_callback(p_actionHeap,  surf_action_lmm_update_index_heap);
102     ActionLmmPtr _actionlmm;
103     CpuCas01ActionLmmPtr _actioncpu;
104     int j = xbt_swag_offset(*_actionlmm, p_actionListHookup);
105     int k = xbt_swag_offset(*_actioncpu, p_actionListHookup);
106     j = ((char *)&( (*_actionlmm).p_actionListHookup ) - (char *)(_actionlmm));
107     k = ((char *)&( (*_actioncpu).p_actionListHookup ) - (char *)(_actioncpu));
108     void *toto = &(*_actionlmm).p_actionListHookup;
109     void *tata = _actionlmm;
110     ActionLmm aieu;
111     ActionLmmPtr actionBase = &aieu;
112     void *actionBaseVoid = actionBase;
113     void *actionBaseCVoid = static_cast<void*>(actionBase);
114     ActionLmmPtr actionBaseVoidBase = (ActionLmmPtr)actionBaseVoid;
115     ActionLmmPtr actionBaseCVoidCBase = static_cast<ActionLmmPtr>(actionBaseCVoid);
116     p_modifiedSet = xbt_swag_new(xbt_swag_offset(*_actionlmm, p_actionListHookup));
117     p_maxminSystem->keep_track = p_modifiedSet;
118   }
119 }
120
121 CpuCas01Model::~CpuCas01Model()
122 {
123   lmm_system_free(p_maxminSystem);
124   p_maxminSystem = NULL;
125
126   if (p_actionHeap)
127     xbt_heap_free(p_actionHeap);
128   xbt_swag_free(p_modifiedSet);
129
130   surf_cpu_model = NULL;
131
132   xbt_swag_free(cpu_running_action_set_that_does_not_need_being_checked);
133   cpu_running_action_set_that_does_not_need_being_checked = NULL;
134 }
135
136 void CpuCas01Model::parseInit(sg_platf_host_cbarg_t host)
137 {
138   createResource(host->id,
139         host->power_peak,
140         host->power_scale,
141         host->power_trace,
142         host->core_amount,
143         host->initial_state,
144         host->state_trace,
145         host->properties);
146 }
147 CpuCas01LmmPtr CpuCas01Model::createResource(const char *name, double power_peak, double power_scale,
148                           tmgr_trace_t power_trace, int core,
149                           e_surf_resource_state_t state_initial,
150                           tmgr_trace_t state_trace,
151                           xbt_dict_t cpu_properties)
152 {
153   CpuPtr cpu = NULL;
154   xbt_assert(!surf_cpu_resource_priv(surf_cpu_resource_by_name(name)),
155              "Host '%s' declared several times in the platform file",
156              name);
157   xbt_assert(power_peak > 0, "Power has to be >0");
158   xbt_assert(core > 0, "Invalid number of cores %d", core);
159
160   cpu = new CpuCas01Lmm(this, name, power_peak, power_scale, power_trace, core, state_initial, state_trace, cpu_properties);
161   xbt_lib_set(host_lib, name, SURF_CPU_LEVEL, cpu);
162
163   return (CpuCas01LmmPtr) xbt_lib_get_elm_or_null(host_lib, name);;
164 }
165
166 double CpuCas01Model::shareResourcesFull(double now)
167 {
168   CpuCas01ActionLmm action;
169   Model::shareResourcesMaxMin(p_runningActionSet, 
170                              xbt_swag_offset(action, p_variable),
171                              p_maxminSystem, lmm_solve);
172   return 0;
173 }
174
175 void CpuCas01Model::addTraces()
176 {
177   xbt_dict_cursor_t cursor = NULL;
178   char *trace_name, *elm;
179   static int called = 0;
180   if (called)
181     return;
182   called = 1;
183
184   /* connect all traces relative to hosts */
185   xbt_dict_foreach(trace_connect_list_host_avail, cursor, trace_name, elm) {
186     tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
187     CpuCas01LmmPtr host = (CpuCas01LmmPtr) surf_cpu_resource_priv(surf_cpu_resource_by_name(elm));
188
189     xbt_assert(host, "Host %s undefined", elm);
190     xbt_assert(trace, "Trace %s undefined", trace_name);
191
192     host->p_stateEvent =
193         tmgr_history_add_trace(history, trace, 0.0, 0, (ResourcePtr) host);
194   }
195
196   xbt_dict_foreach(trace_connect_list_power, cursor, trace_name, elm) {
197     tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
198     CpuCas01LmmPtr host = (CpuCas01LmmPtr) surf_cpu_resource_priv(surf_cpu_resource_by_name(elm));
199
200     xbt_assert(host, "Host %s undefined", elm);
201     xbt_assert(trace, "Trace %s undefined", trace_name);
202
203     host->p_powerEvent =
204         tmgr_history_add_trace(history, trace, 0.0, 0, (ResourcePtr) host);
205   }
206 }
207
208 /************
209  * Resource *
210  ************/
211 CpuCas01Lmm::CpuCas01Lmm(CpuCas01ModelPtr model, const char *name, double powerPeak,
212         double powerScale, tmgr_trace_t powerTrace, int core,
213         e_surf_resource_state_t stateInitial, tmgr_trace_t stateTrace,
214         xbt_dict_t properties) :
215         CpuLmm(model, name, properties), Resource(model, name, properties) {
216   m_powerPeak = powerPeak;
217   m_powerScale = powerScale;
218   m_core = core;
219   p_stateCurrent = stateInitial;
220   if (powerTrace)
221     p_powerEvent = tmgr_history_add_trace(history, powerTrace, 0.0, 0, static_cast<ResourcePtr>(this));
222
223   if (stateTrace)
224     p_stateEvent = tmgr_history_add_trace(history, stateTrace, 0.0, 0, static_cast<ResourcePtr>(this));
225
226   p_constraint = lmm_constraint_new(p_model->p_maxminSystem, this, m_core * m_powerScale * m_powerPeak);
227 }
228
229 bool CpuCas01Lmm::isUsed()
230 {
231   return lmm_constraint_used(p_model->p_maxminSystem, p_constraint);
232 }
233
234 void CpuCas01Lmm::updateState(tmgr_trace_event_t event_type, double value, double date)
235 {
236   lmm_variable_t var = NULL;
237   lmm_element_t elem = NULL;
238
239   surf_watched_hosts();
240
241   if (event_type == p_powerEvent) {
242     m_powerScale = value;
243     lmm_update_constraint_bound(surf_cpu_model->p_maxminSystem, p_constraint,
244                                 m_core * m_powerScale *
245                                 m_powerPeak);
246 #ifdef HAVE_TRACING
247     TRACE_surf_host_set_power(date, m_name,
248                               m_core * m_powerScale *
249                               m_powerPeak);
250 #endif
251     while ((var = lmm_get_var_from_cnst
252             (surf_cpu_model->p_maxminSystem, p_constraint, &elem))) {
253       CpuCas01ActionLmmPtr action = static_cast<CpuCas01ActionLmmPtr>(static_cast<ActionLmmPtr>(lmm_variable_id(var)));
254
255       lmm_update_variable_bound(surf_cpu_model->p_maxminSystem,
256                                 action->p_variable,
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     if (value > 0)
263       p_stateCurrent = SURF_RESOURCE_ON;
264     else {
265       lmm_constraint_t cnst = p_constraint;
266
267       p_stateCurrent = SURF_RESOURCE_OFF;
268
269       while ((var = lmm_get_var_from_cnst(surf_cpu_model->p_maxminSystem, cnst, &elem))) {
270         ActionLmmPtr action = static_cast<ActionLmmPtr>(lmm_variable_id(var));
271
272         if (action->getState() == SURF_ACTION_RUNNING ||
273             action->getState() == SURF_ACTION_READY ||
274             action->getState() == SURF_ACTION_NOT_IN_THE_SYSTEM) {
275           action->m_finish = date;
276           action->setState(SURF_ACTION_FAILED);
277         }
278       }
279     }
280     if (tmgr_trace_event_free(event_type))
281       p_stateEvent = NULL;
282   } else {
283     XBT_CRITICAL("Unknown event ! \n");
284     xbt_abort();
285   }
286
287   return;
288 }
289
290 CpuActionPtr CpuCas01Lmm::execute(double size)
291 {
292
293   XBT_IN("(%s,%g)", m_name, size);
294   CpuCas01ActionLmmPtr action = new CpuCas01ActionLmm(surf_cpu_model, size, p_stateCurrent != SURF_RESOURCE_ON);
295
296   action->m_suspended = 0;     /* Should be useless because of the
297                                                    calloc but it seems to help valgrind... */
298
299   action->p_variable =
300       lmm_variable_new(surf_cpu_model->p_maxminSystem, static_cast<ActionLmmPtr>(action),
301                        action->m_priority,
302                        m_powerScale * m_powerPeak, 1);
303   if (surf_cpu_model->p_updateMechanism == UM_LAZY) {
304     action->m_indexHeap = -1;
305     action->m_lastUpdate = surf_get_clock();
306     action->m_lastValue = 0.0;
307   }
308   lmm_expand(surf_cpu_model->p_maxminSystem, p_constraint,
309              action->p_variable, 1.0);
310   XBT_OUT();
311   return action;
312 }
313
314 CpuActionPtr CpuCas01Lmm::sleep(double duration)
315 {
316   if (duration > 0)
317     duration = MAX(duration, MAXMIN_PRECISION);
318
319   XBT_IN("(%s,%g)", m_name, duration);
320   CpuCas01ActionLmmPtr action = (CpuCas01ActionLmmPtr) execute(1.0);
321
322   // FIXME: sleep variables should not consume 1.0 in lmm_expand
323   action->m_maxDuration = duration;
324   action->m_suspended = 2;
325   if (duration == NO_MAX_DURATION) {
326     /* Move to the *end* of the corresponding action set. This convention
327        is used to speed up update_resource_state  */
328     xbt_swag_remove(static_cast<ActionPtr>(action), action->p_stateSet);
329     action->p_stateSet = cpu_running_action_set_that_does_not_need_being_checked;
330     xbt_swag_insert(static_cast<ActionPtr>(action), action->p_stateSet);
331   }
332
333   lmm_update_variable_weight(surf_cpu_model->p_maxminSystem,
334                              action->p_variable, 0.0);
335   if (surf_cpu_model->p_updateMechanism == UM_LAZY) {     // remove action from the heap
336     action->heapRemove(surf_cpu_model->p_actionHeap);
337     // this is necessary for a variable with weight 0 since such
338     // variables are ignored in lmm and we need to set its max_duration
339     // correctly at the next call to share_resources
340     xbt_swag_insert_at_head(action, surf_cpu_model->p_modifiedSet);
341   }
342
343   XBT_OUT();
344   return action;
345 }
346
347
348 /**********
349  * Action *
350  **********/
351
352