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 "surf_private.h"
8 #include "surf/surf_resource.h"
9 #include "maxmin_private.h"
10 #include "simgrid/sg_config.h"
11 #include "surf/cpu_cas01_private.h"
13 /* the model objects for physical machines and virtual machines */
14 surf_model_t surf_cpu_model_pm = NULL;
15 surf_model_t surf_cpu_model_vm = NULL;
17 #undef GENERIC_LMM_ACTION
20 #define GENERIC_LMM_ACTION(action) action->generic_lmm_action
21 #define GENERIC_ACTION(action) GENERIC_LMM_ACTION(action).generic_action
22 #define ACTION_GET_CPU(action) ((surf_action_cpu_Cas01_t) action)->cpu
24 typedef struct surf_action_cpu_cas01 {
25 s_surf_action_lmm_t generic_lmm_action;
26 } s_surf_action_cpu_Cas01_t, *surf_action_cpu_Cas01_t;
28 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_cpu, surf,
29 "Logging specific to the SURF CPU IMPROVED module");
32 cpu_running_action_set_that_does_not_need_being_checked = NULL;
35 void *cpu_cas01_create_resource(const char *name, double power_peak,
37 tmgr_trace_t power_trace,
39 e_surf_resource_state_t state_initial,
40 tmgr_trace_t state_trace,
41 xbt_dict_t cpu_properties,
42 surf_model_t cpu_model)
44 cpu_Cas01_t cpu = NULL;
46 xbt_assert(!surf_cpu_resource_priv(surf_cpu_resource_by_name(name)),
47 "Host '%s' declared several times in the platform file",
49 cpu = (cpu_Cas01_t) surf_resource_new(sizeof(s_cpu_Cas01_t),
52 cpu->power_peak = power_peak;
53 xbt_assert(cpu->power_peak > 0, "Power has to be >0");
54 cpu->power_scale = power_scale;
56 xbt_assert(core > 0, "Invalid number of cores %d", core);
60 tmgr_history_add_trace(history, power_trace, 0.0, 0, cpu);
62 cpu->state_current = state_initial;
65 tmgr_history_add_trace(history, state_trace, 0.0, 0, cpu);
68 lmm_constraint_new(cpu_model->model_private->maxmin_system, cpu,
69 cpu->core * cpu->power_scale * cpu->power_peak);
71 xbt_lib_set(host_lib, name, SURF_CPU_LEVEL, cpu);
73 return xbt_lib_get_elm_or_null(host_lib, name);;
77 static void parse_cpu_init(sg_platf_host_cbarg_t host)
79 /* This function is called when a platform file is parsed. Physical machines
80 * are defined there. Thus, we use the cpu model object for the physical
82 cpu_cas01_create_resource(host->id,
88 host->state_trace, host->properties,
92 static void cpu_add_traces_cpu(void)
94 xbt_dict_cursor_t cursor = NULL;
95 char *trace_name, *elm;
96 static int called = 0;
101 /* connect all traces relative to hosts */
102 xbt_dict_foreach(trace_connect_list_host_avail, cursor, trace_name, elm) {
103 tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
104 cpu_Cas01_t host = surf_cpu_resource_by_name(elm);
106 xbt_assert(host, "Host %s undefined", elm);
107 xbt_assert(trace, "Trace %s undefined", trace_name);
110 tmgr_history_add_trace(history, trace, 0.0, 0, host);
113 xbt_dict_foreach(trace_connect_list_power, cursor, trace_name, elm) {
114 tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
115 cpu_Cas01_t host = surf_cpu_resource_by_name(elm);
117 xbt_assert(host, "Host %s undefined", elm);
118 xbt_assert(trace, "Trace %s undefined", trace_name);
121 tmgr_history_add_trace(history, trace, 0.0, 0, host);
125 static void cpu_define_callbacks_cas01()
127 sg_platf_host_add_cb(parse_cpu_init);
128 sg_platf_postparse_add_cb(cpu_add_traces_cpu);
131 static int cpu_resource_used(void *resource)
133 surf_model_t cpu_model = ((surf_resource_t) resource)->model;
135 return lmm_constraint_used(cpu_model->model_private->maxmin_system,
136 ((cpu_Cas01_t) resource)->constraint);
139 static double cpu_share_resources_lazy(surf_model_t cpu_model, double now)
141 return generic_share_resources_lazy(now, cpu_model);
144 static double cpu_share_resources_full(surf_model_t cpu_model, double now)
146 s_surf_action_cpu_Cas01_t action;
147 return generic_maxmin_share_resources(cpu_model->states.
149 xbt_swag_offset(action,
152 cpu_model->model_private->maxmin_system, lmm_solve);
155 static void cpu_update_actions_state_lazy(surf_model_t cpu_model, double now, double delta)
157 generic_update_actions_state_lazy(now, delta, cpu_model);
160 static void cpu_update_actions_state_full(surf_model_t cpu_model, double now, double delta)
162 generic_update_actions_state_full(now, delta, cpu_model);
165 static void cpu_update_resource_state(void *id,
166 tmgr_trace_event_t event_type,
167 double value, double date)
169 cpu_Cas01_t cpu = id;
170 lmm_variable_t var = NULL;
171 lmm_element_t elem = NULL;
172 surf_model_t cpu_model = ((surf_resource_t) cpu)->model;
174 surf_watched_hosts();
176 if (event_type == cpu->power_event) {
177 cpu->power_scale = value;
178 lmm_update_constraint_bound(cpu_model->model_private->maxmin_system, cpu->constraint,
179 cpu->core * cpu->power_scale *
182 TRACE_surf_host_set_power(date, cpu->generic_resource.name,
183 cpu->core * cpu->power_scale *
186 while ((var = lmm_get_var_from_cnst
187 (cpu_model->model_private->maxmin_system, cpu->constraint, &elem))) {
188 surf_action_cpu_Cas01_t action = lmm_variable_id(var);
189 lmm_update_variable_bound(cpu_model->model_private->maxmin_system,
190 GENERIC_LMM_ACTION(action).variable,
191 cpu->power_scale * cpu->power_peak);
193 if (tmgr_trace_event_free(event_type))
194 cpu->power_event = NULL;
195 } else if (event_type == cpu->state_event) {
197 cpu->state_current = SURF_RESOURCE_ON;
199 lmm_constraint_t cnst = cpu->constraint;
201 cpu->state_current = SURF_RESOURCE_OFF;
203 while ((var = lmm_get_var_from_cnst(cpu_model->model_private->maxmin_system, cnst, &elem))) {
204 surf_action_t action = lmm_variable_id(var);
206 if (surf_action_state_get(action) == SURF_ACTION_RUNNING ||
207 surf_action_state_get(action) == SURF_ACTION_READY ||
208 surf_action_state_get(action) ==
209 SURF_ACTION_NOT_IN_THE_SYSTEM) {
210 action->finish = date;
211 surf_action_state_set(action, SURF_ACTION_FAILED);
215 if (tmgr_trace_event_free(event_type))
216 cpu->state_event = NULL;
218 XBT_CRITICAL("Unknown event ! \n");
225 static surf_action_t cpu_execute(void *cpu, double size)
227 surf_action_cpu_Cas01_t action = NULL;
228 cpu_Cas01_t CPU = surf_cpu_resource_priv(cpu);
229 surf_model_t cpu_model = ((surf_resource_t) CPU)->model;
231 XBT_IN("(%s,%g)", surf_resource_name(CPU), size);
233 surf_action_new(sizeof(s_surf_action_cpu_Cas01_t), size,
235 CPU->state_current != SURF_RESOURCE_ON);
237 GENERIC_LMM_ACTION(action).suspended = 0; /* Should be useless because of the
238 calloc but it seems to help valgrind... */
240 GENERIC_LMM_ACTION(action).variable =
241 lmm_variable_new(cpu_model->model_private->maxmin_system, action,
242 GENERIC_ACTION(action).priority,
243 CPU->power_scale * CPU->power_peak, 1);
244 if (cpu_model->model_private->update_mechanism == UM_LAZY) {
245 GENERIC_LMM_ACTION(action).index_heap = -1;
246 GENERIC_LMM_ACTION(action).last_update = surf_get_clock();
247 GENERIC_LMM_ACTION(action).last_value = 0.0;
249 lmm_expand(cpu_model->model_private->maxmin_system, CPU->constraint,
250 GENERIC_LMM_ACTION(action).variable, 1.0);
252 return (surf_action_t) action;
255 static surf_action_t cpu_action_sleep(void *cpu, double duration)
257 surf_action_cpu_Cas01_t action = NULL;
258 cpu_Cas01_t CPU = surf_cpu_resource_priv(cpu);
259 surf_model_t cpu_model = ((surf_resource_t) CPU)->model;
262 duration = MAX(duration, MAXMIN_PRECISION);
264 XBT_IN("(%s,%g)", surf_resource_name(surf_cpu_resource_priv(cpu)), duration);
265 action = (surf_action_cpu_Cas01_t) cpu_execute(cpu, 1.0);
266 // FIXME: sleep variables should not consume 1.0 in lmm_expand
267 GENERIC_ACTION(action).max_duration = duration;
268 GENERIC_LMM_ACTION(action).suspended = 2;
269 if (duration == NO_MAX_DURATION) {
270 /* Move to the *end* of the corresponding action set. This convention
271 is used to speed up update_resource_state */
272 xbt_swag_remove(action, ((surf_action_t) action)->state_set);
273 ((surf_action_t) action)->state_set =
274 cpu_running_action_set_that_does_not_need_being_checked;
275 xbt_swag_insert(action, ((surf_action_t) action)->state_set);
278 lmm_update_variable_weight(cpu_model->model_private->maxmin_system,
279 GENERIC_LMM_ACTION(action).variable, 0.0);
280 if (cpu_model->model_private->update_mechanism == UM_LAZY) { // remove action from the heap
281 surf_action_lmm_heap_remove(cpu_model->model_private->action_heap,(surf_action_lmm_t)action);
282 // this is necessary for a variable with weight 0 since such
283 // variables are ignored in lmm and we need to set its max_duration
284 // correctly at the next call to share_resources
285 xbt_swag_insert_at_head(action, cpu_model->model_private->modified_set);
289 return (surf_action_t) action;
292 static e_surf_resource_state_t cpu_get_state(void *cpu)
294 return ((cpu_Cas01_t)surf_cpu_resource_priv(cpu))->state_current;
297 static double cpu_get_speed(void *cpu, double load)
299 return load * ((cpu_Cas01_t)surf_cpu_resource_priv(cpu))->power_peak;
302 static double cpu_get_available_speed(void *cpu)
304 /* number between 0 and 1 */
305 return ((cpu_Cas01_t)surf_cpu_resource_priv(cpu))->power_scale;
308 static void cpu_finalize(surf_model_t cpu_model)
310 lmm_system_free(cpu_model->model_private->maxmin_system);
311 cpu_model->model_private->maxmin_system = NULL;
313 if (cpu_model->model_private->action_heap)
314 xbt_heap_free(cpu_model->model_private->action_heap);
315 xbt_swag_free(cpu_model->model_private->modified_set);
317 surf_model_exit(cpu_model);
320 xbt_swag_free(cpu_running_action_set_that_does_not_need_being_checked);
321 cpu_running_action_set_that_does_not_need_being_checked = NULL;
324 static surf_model_t surf_cpu_model_init_cas01(void)
326 s_surf_action_t action;
327 s_surf_action_cpu_Cas01_t comp;
329 char *optim = xbt_cfg_get_string(_sg_cfg_set, "cpu/optim");
331 xbt_cfg_get_int(_sg_cfg_set, "cpu/maxmin_selective_update");
333 surf_model_t cpu_model = surf_model_init();
335 if (!strcmp(optim, "Full")) {
336 cpu_model->model_private->update_mechanism = UM_FULL;
337 cpu_model->model_private->selective_update = select;
338 } else if (!strcmp(optim, "Lazy")) {
339 cpu_model->model_private->update_mechanism = UM_LAZY;
340 cpu_model->model_private->selective_update = 1;
341 xbt_assert((select == 1)
343 (xbt_cfg_is_default_value
344 (_sg_cfg_set, "cpu/maxmin_selective_update")),
345 "Disabling selective update while using the lazy update mechanism is dumb!");
347 xbt_die("Unsupported optimization (%s) for this model", optim);
350 cpu_running_action_set_that_does_not_need_being_checked =
351 xbt_swag_new(xbt_swag_offset(action, state_hookup));
353 cpu_model->name = "cpu";
354 cpu_model->type = SURF_MODEL_TYPE_CPU;
356 cpu_model->action_unref = surf_action_unref;
357 cpu_model->action_cancel = surf_action_cancel;
358 cpu_model->action_state_set = surf_action_state_set;
360 cpu_model->model_private->resource_used = cpu_resource_used;
362 if (cpu_model->model_private->update_mechanism == UM_LAZY) {
363 cpu_model->model_private->share_resources =
364 cpu_share_resources_lazy;
365 cpu_model->model_private->update_actions_state =
366 cpu_update_actions_state_lazy;
367 } else if (cpu_model->model_private->update_mechanism == UM_FULL) {
368 cpu_model->model_private->share_resources =
369 cpu_share_resources_full;
370 cpu_model->model_private->update_actions_state =
371 cpu_update_actions_state_full;
373 xbt_die("Invalid cpu update mechanism!");
375 cpu_model->model_private->update_resource_state =
376 cpu_update_resource_state;
377 cpu_model->model_private->finalize = cpu_finalize;
379 cpu_model->suspend = surf_action_suspend;
380 cpu_model->resume = surf_action_resume;
381 cpu_model->is_suspended = surf_action_is_suspended;
382 cpu_model->set_max_duration = surf_action_set_max_duration;
383 cpu_model->set_priority = surf_action_set_priority;
384 cpu_model->set_bound = surf_action_set_bound;
386 cpu_model->set_category = surf_action_set_category;
388 cpu_model->get_remains = surf_action_get_remains;
390 cpu_model->extension.cpu.execute = cpu_execute;
391 cpu_model->extension.cpu.sleep = cpu_action_sleep;
393 cpu_model->extension.cpu.get_state = cpu_get_state;
394 cpu_model->extension.cpu.get_speed = cpu_get_speed;
395 cpu_model->extension.cpu.get_available_speed =
396 cpu_get_available_speed;
397 cpu_model->extension.cpu.add_traces = cpu_add_traces_cpu;
399 if (!cpu_model->model_private->maxmin_system) {
400 cpu_model->model_private->maxmin_system = lmm_system_new(cpu_model->model_private->selective_update);
402 if (cpu_model->model_private->update_mechanism == UM_LAZY) {
403 cpu_model->model_private->action_heap = xbt_heap_new(8, NULL);
404 xbt_heap_set_update_callback(cpu_model->model_private->action_heap,
405 surf_action_lmm_update_index_heap);
406 cpu_model->model_private->modified_set =
407 xbt_swag_new(xbt_swag_offset(comp, generic_lmm_action.action_list_hookup));
408 cpu_model->model_private->maxmin_system->keep_track = cpu_model->model_private->modified_set;
414 /*********************************************************************/
415 /* Basic sharing model for CPU: that is where all this started... ;) */
416 /*********************************************************************/
417 /* @InProceedings{casanova01simgrid, */
418 /* author = "H. Casanova", */
419 /* booktitle = "Proceedings of the IEEE Symposium on Cluster Computing */
420 /* and the Grid (CCGrid'01)", */
421 /* publisher = "IEEE Computer Society", */
422 /* title = "Simgrid: {A} Toolkit for the Simulation of Application */
426 /* note = "Available at */
427 /* \url{http://grail.sdsc.edu/papers/simgrid_ccgrid01.ps.gz}." */
431 void surf_cpu_model_init_Cas01(void)
433 char *optim = xbt_cfg_get_string(_sg_cfg_set, "cpu/optim");
435 xbt_assert(!surf_cpu_model_pm);
436 xbt_assert(!surf_cpu_model_vm);
438 if (strcmp(optim, "TI") == 0) {
439 /* FIXME: do we have to supprot TI? for VM */
440 surf_cpu_model_pm = surf_cpu_model_init_ti();
441 XBT_INFO("TI model is used (it will crashed since this is the hypervisor branch)");
443 surf_cpu_model_pm = surf_cpu_model_init_cas01();
444 surf_cpu_model_vm = surf_cpu_model_init_cas01();
446 /* cpu_model is registered only to model_list, and not to
447 * model_list_invoke. The shared_resource callback function will be called
448 * from that of the workstation model. */
449 xbt_dynar_push(model_list, &surf_cpu_model_pm);
450 xbt_dynar_push(model_list, &surf_cpu_model_vm);
452 cpu_define_callbacks_cas01();
456 /* TODO: do we address nested virtualization later? */
458 surf_model_t cpu_model_cas01(int level){
459 // TODO this table should be allocated
460 if(!surf_cpu_model[level])
462 return surf_cpu_model[level];