Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix Network Constant
[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_init_ti();
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   ActionPtr action;
63   ActionLmmPtr actionlmm;
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   cpu_running_action_set_that_does_not_need_being_checked =
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 = NULL;
118
119   xbt_swag_free(cpu_running_action_set_that_does_not_need_being_checked);
120   cpu_running_action_set_that_does_not_need_being_checked = NULL;
121 }
122
123 void CpuCas01Model::parseInit(sg_platf_host_cbarg_t host)
124 {
125   createResource(host->id,
126         host->power_peak,
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 CpuCas01LmmPtr CpuCas01Model::createResource(const char *name, double power_peak, double power_scale,
136                           tmgr_trace_t power_trace, int core,
137                           e_surf_resource_state_t state_initial,
138                           tmgr_trace_t state_trace,
139                           xbt_dict_t cpu_properties)
140 {
141   CpuPtr cpu = NULL;
142   xbt_assert(!surf_cpu_resource_priv(surf_cpu_resource_by_name(name)),
143              "Host '%s' declared several times in the platform file",
144              name);
145   xbt_assert(power_peak > 0, "Power has to be >0");
146   xbt_assert(core > 0, "Invalid number of cores %d", core);
147
148   cpu = new CpuCas01Lmm(this, name, power_peak, power_scale, power_trace, core, state_initial, state_trace, cpu_properties);
149   xbt_lib_set(host_lib, name, SURF_CPU_LEVEL, static_cast<ResourcePtr>(cpu));
150
151   return (CpuCas01LmmPtr) xbt_lib_get_elm_or_null(host_lib, name);
152 }
153
154 double CpuCas01Model::shareResourcesFull(double now)
155 {
156   return Model::shareResourcesMaxMin(p_runningActionSet,
157                              p_maxminSystem, lmm_solve);
158 }
159
160 void CpuCas01Model::addTraces()
161 {
162   xbt_dict_cursor_t cursor = NULL;
163   char *trace_name, *elm;
164   static int called = 0;
165   if (called)
166     return;
167   called = 1;
168
169   /* connect all traces relative to hosts */
170   xbt_dict_foreach(trace_connect_list_host_avail, cursor, trace_name, elm) {
171     tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
172     CpuCas01LmmPtr host = static_cast<CpuCas01LmmPtr>(surf_cpu_resource_priv(surf_cpu_resource_by_name(elm)));
173
174     xbt_assert(host, "Host %s undefined", elm);
175     xbt_assert(trace, "Trace %s undefined", trace_name);
176
177     host->p_stateEvent =
178         tmgr_history_add_trace(history, trace, 0.0, 0, static_cast<ResourcePtr>(host));
179   }
180
181   xbt_dict_foreach(trace_connect_list_power, cursor, trace_name, elm) {
182     tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
183     CpuCas01LmmPtr host = dynamic_cast<CpuCas01LmmPtr>(static_cast<ResourcePtr>(surf_cpu_resource_priv(surf_cpu_resource_by_name(elm))));
184
185     xbt_assert(host, "Host %s undefined", elm);
186     xbt_assert(trace, "Trace %s undefined", trace_name);
187
188     host->p_powerEvent =
189         tmgr_history_add_trace(history, trace, 0.0, 0, static_cast<ResourcePtr>(host));
190   }
191 }
192
193 /************
194  * Resource *
195  ************/
196 CpuCas01Lmm::CpuCas01Lmm(CpuCas01ModelPtr model, const char *name, double powerPeak,
197         double powerScale, tmgr_trace_t powerTrace, int core,
198         e_surf_resource_state_t stateInitial, tmgr_trace_t stateTrace,
199         xbt_dict_t properties) :
200         CpuLmm(model, name, properties), Resource(model, name, properties) {
201   m_powerPeak = powerPeak;
202   m_powerScale = powerScale;
203   m_core = core;
204   p_stateCurrent = stateInitial;
205   if (powerTrace)
206     p_powerEvent = tmgr_history_add_trace(history, powerTrace, 0.0, 0, static_cast<ResourcePtr>(this));
207
208   if (stateTrace)
209     p_stateEvent = tmgr_history_add_trace(history, stateTrace, 0.0, 0, static_cast<ResourcePtr>(this));
210
211   p_constraint = lmm_constraint_new(p_model->p_maxminSystem, this, m_core * m_powerScale * m_powerPeak);
212 }
213
214 bool CpuCas01Lmm::isUsed()
215 {
216   return lmm_constraint_used(p_model->p_maxminSystem, p_constraint);
217 }
218
219 void CpuCas01Lmm::updateState(tmgr_trace_event_t event_type, double value, double date)
220 {
221   lmm_variable_t var = NULL;
222   lmm_element_t elem = NULL;
223
224   surf_watched_hosts();
225
226   if (event_type == p_powerEvent) {
227     m_powerScale = value;
228     lmm_update_constraint_bound(surf_cpu_model->p_maxminSystem, p_constraint,
229                                 m_core * m_powerScale *
230                                 m_powerPeak);
231 #ifdef HAVE_TRACING
232     TRACE_surf_host_set_power(date, m_name,
233                               m_core * m_powerScale *
234                               m_powerPeak);
235 #endif
236     while ((var = lmm_get_var_from_cnst
237             (surf_cpu_model->p_maxminSystem, p_constraint, &elem))) {
238       CpuCas01ActionLmmPtr action = static_cast<CpuCas01ActionLmmPtr>(static_cast<ActionLmmPtr>(lmm_variable_id(var)));
239
240       lmm_update_variable_bound(surf_cpu_model->p_maxminSystem,
241                                 action->p_variable,
242                                 m_powerScale * m_powerPeak);
243     }
244     if (tmgr_trace_event_free(event_type))
245       p_powerEvent = NULL;
246   } else if (event_type == p_stateEvent) {
247     if (value > 0)
248       p_stateCurrent = SURF_RESOURCE_ON;
249     else {
250       lmm_constraint_t cnst = p_constraint;
251
252       p_stateCurrent = SURF_RESOURCE_OFF;
253
254       while ((var = lmm_get_var_from_cnst(surf_cpu_model->p_maxminSystem, cnst, &elem))) {
255         ActionLmmPtr action = static_cast<ActionLmmPtr>(lmm_variable_id(var));
256
257         if (action->getState() == SURF_ACTION_RUNNING ||
258             action->getState() == SURF_ACTION_READY ||
259             action->getState() == SURF_ACTION_NOT_IN_THE_SYSTEM) {
260           action->m_finish = date;
261           action->setState(SURF_ACTION_FAILED);
262         }
263       }
264     }
265     if (tmgr_trace_event_free(event_type))
266       p_stateEvent = NULL;
267   } else {
268     XBT_CRITICAL("Unknown event ! \n");
269     xbt_abort();
270   }
271
272   return;
273 }
274
275 ActionPtr CpuCas01Lmm::execute(double size)
276 {
277
278   XBT_IN("(%s,%g)", m_name, size);
279   CpuCas01ActionLmmPtr action = new CpuCas01ActionLmm(surf_cpu_model, size, p_stateCurrent != SURF_RESOURCE_ON);
280
281   action->m_suspended = 0;     /* Should be useless because of the
282                                                    calloc but it seems to help valgrind... */
283
284   action->p_variable =
285       lmm_variable_new(surf_cpu_model->p_maxminSystem, static_cast<ActionLmmPtr>(action),
286                        action->m_priority,
287                        m_powerScale * m_powerPeak, 1);
288   if (surf_cpu_model->p_updateMechanism == UM_LAZY) {
289     action->m_indexHeap = -1;
290     action->m_lastUpdate = surf_get_clock();
291     action->m_lastValue = 0.0;
292   }
293   lmm_expand(surf_cpu_model->p_maxminSystem, p_constraint,
294              action->p_variable, 1.0);
295   XBT_OUT();
296   return action;
297 }
298
299 ActionPtr CpuCas01Lmm::sleep(double duration)
300 {
301   if (duration > 0)
302     duration = MAX(duration, MAXMIN_PRECISION);
303
304   XBT_IN("(%s,%g)", m_name, duration);
305   CpuCas01ActionLmmPtr action = dynamic_cast<CpuCas01ActionLmmPtr>(execute(1.0));
306
307   // FIXME: sleep variables should not consume 1.0 in lmm_expand
308   action->m_maxDuration = duration;
309   action->m_suspended = 2;
310   if (duration == NO_MAX_DURATION) {
311     /* Move to the *end* of the corresponding action set. This convention
312        is used to speed up update_resource_state  */
313     xbt_swag_remove(static_cast<ActionPtr>(action), action->p_stateSet);
314     action->p_stateSet = cpu_running_action_set_that_does_not_need_being_checked;
315     xbt_swag_insert(static_cast<ActionPtr>(action), action->p_stateSet);
316   }
317
318   lmm_update_variable_weight(surf_cpu_model->p_maxminSystem,
319                              action->p_variable, 0.0);
320   if (surf_cpu_model->p_updateMechanism == UM_LAZY) {     // remove action from the heap
321     action->heapRemove(surf_cpu_model->p_actionHeap);
322     // this is necessary for a variable with weight 0 since such
323     // variables are ignored in lmm and we need to set its max_duration
324     // correctly at the next call to share_resources
325     xbt_swag_insert_at_head(static_cast<ActionLmmPtr>(action), surf_cpu_model->p_modifiedSet);
326   }
327
328   XBT_OUT();
329   return action;
330 }
331
332
333 /**********
334  * Action *
335  **********/
336
337