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"
12 /* the model objects for physical machines and virtual machines */
13 surf_model_t surf_cpu_model_pm = NULL;
14 surf_model_t surf_cpu_model_vm = NULL;
16 #undef GENERIC_LMM_ACTION
19 #define GENERIC_LMM_ACTION(action) action->generic_lmm_action
20 #define GENERIC_ACTION(action) GENERIC_LMM_ACTION(action).generic_action
21 #define ACTION_GET_CPU(action) ((surf_action_cpu_Cas01_t) action)->cpu
23 typedef struct surf_action_cpu_cas01 {
24 s_surf_action_lmm_t generic_lmm_action;
25 } s_surf_action_cpu_Cas01_t, *surf_action_cpu_Cas01_t;
27 typedef struct cpu_Cas01 {
28 s_surf_resource_t generic_resource;
29 s_xbt_swag_hookup_t modified_cpu_hookup;
32 tmgr_trace_event_t power_event;
34 e_surf_resource_state_t state_current;
35 tmgr_trace_event_t state_event;
36 lmm_constraint_t constraint;
37 } s_cpu_Cas01_t, *cpu_Cas01_t;
39 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_cpu, surf,
40 "Logging specific to the SURF CPU IMPROVED module");
43 cpu_running_action_set_that_does_not_need_being_checked = NULL;
46 /* This function is registered as a callback to sg_platf_new_host() and never called directly */
47 static void *cpu_create_resource(const char *name, double power_peak,
49 tmgr_trace_t power_trace,
51 e_surf_resource_state_t state_initial,
52 tmgr_trace_t state_trace,
53 xbt_dict_t cpu_properties,
54 surf_model_t cpu_model)
56 cpu_Cas01_t cpu = NULL;
58 xbt_assert(!surf_cpu_resource_priv(surf_cpu_resource_by_name(name)),
59 "Host '%s' declared several times in the platform file",
61 cpu = (cpu_Cas01_t) surf_resource_new(sizeof(s_cpu_Cas01_t),
64 cpu->power_peak = power_peak;
65 xbt_assert(cpu->power_peak > 0, "Power has to be >0");
66 cpu->power_scale = power_scale;
68 xbt_assert(core > 0, "Invalid number of cores %d", core);
72 tmgr_history_add_trace(history, power_trace, 0.0, 0, cpu);
74 cpu->state_current = state_initial;
77 tmgr_history_add_trace(history, state_trace, 0.0, 0, cpu);
80 lmm_constraint_new(cpu_model->model_private->maxmin_system, cpu,
81 cpu->core * cpu->power_scale * cpu->power_peak);
83 xbt_lib_set(host_lib, name, SURF_CPU_LEVEL, cpu);
85 return xbt_lib_get_elm_or_null(host_lib, name);;
89 static void parse_cpu_init(sg_platf_host_cbarg_t host)
91 /* This function is called when a platform file is parsed. Physical machines
92 * are defined there. Thus, we use the cpu model object for the physical
94 cpu_create_resource(host->id,
100 host->state_trace, host->properties,
104 static void cpu_add_traces_cpu(void)
106 xbt_dict_cursor_t cursor = NULL;
107 char *trace_name, *elm;
108 static int called = 0;
113 /* connect all traces relative to hosts */
114 xbt_dict_foreach(trace_connect_list_host_avail, cursor, trace_name, elm) {
115 tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
116 cpu_Cas01_t host = surf_cpu_resource_by_name(elm);
118 xbt_assert(host, "Host %s undefined", elm);
119 xbt_assert(trace, "Trace %s undefined", trace_name);
122 tmgr_history_add_trace(history, trace, 0.0, 0, host);
125 xbt_dict_foreach(trace_connect_list_power, cursor, trace_name, elm) {
126 tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
127 cpu_Cas01_t host = surf_cpu_resource_by_name(elm);
129 xbt_assert(host, "Host %s undefined", elm);
130 xbt_assert(trace, "Trace %s undefined", trace_name);
133 tmgr_history_add_trace(history, trace, 0.0, 0, host);
137 static void cpu_define_callbacks()
139 sg_platf_host_add_cb(parse_cpu_init);
140 sg_platf_postparse_add_cb(cpu_add_traces_cpu);
143 static int cpu_resource_used(void *resource)
145 surf_model_t cpu_model = ((surf_resource_t) resource)->model;
147 return lmm_constraint_used(cpu_model->model_private->maxmin_system,
148 ((cpu_Cas01_t) resource)->constraint);
151 static double cpu_share_resources_lazy(surf_model_t cpu_model, double now)
153 return generic_share_resources_lazy(now, cpu_model);
156 static double cpu_share_resources_full(surf_model_t cpu_model, double now)
158 s_surf_action_cpu_Cas01_t action;
159 return generic_maxmin_share_resources(cpu_model->states.
161 xbt_swag_offset(action,
164 cpu_model->model_private->maxmin_system, lmm_solve);
167 static void cpu_update_actions_state_lazy(surf_model_t cpu_model, double now, double delta)
169 generic_update_actions_state_lazy(now, delta, cpu_model);
172 static void cpu_update_actions_state_full(surf_model_t cpu_model, double now, double delta)
174 generic_update_actions_state_full(now, delta, cpu_model);
177 static void cpu_update_resource_state(void *id,
178 tmgr_trace_event_t event_type,
179 double value, double date)
181 cpu_Cas01_t cpu = id;
182 lmm_variable_t var = NULL;
183 lmm_element_t elem = NULL;
184 surf_model_t cpu_model = ((surf_resource_t) cpu)->model;
186 surf_watched_hosts();
188 if (event_type == cpu->power_event) {
189 cpu->power_scale = value;
190 lmm_update_constraint_bound(cpu_model->model_private->maxmin_system, cpu->constraint,
191 cpu->core * cpu->power_scale *
194 TRACE_surf_host_set_power(date, cpu->generic_resource.name,
195 cpu->core * cpu->power_scale *
198 while ((var = lmm_get_var_from_cnst
199 (cpu_model->model_private->maxmin_system, cpu->constraint, &elem))) {
200 surf_action_cpu_Cas01_t action = lmm_variable_id(var);
201 lmm_update_variable_bound(cpu_model->model_private->maxmin_system,
202 GENERIC_LMM_ACTION(action).variable,
203 cpu->power_scale * cpu->power_peak);
205 if (tmgr_trace_event_free(event_type))
206 cpu->power_event = NULL;
207 } else if (event_type == cpu->state_event) {
209 cpu->state_current = SURF_RESOURCE_ON;
211 lmm_constraint_t cnst = cpu->constraint;
213 cpu->state_current = SURF_RESOURCE_OFF;
215 while ((var = lmm_get_var_from_cnst(cpu_model->model_private->maxmin_system, cnst, &elem))) {
216 surf_action_t action = lmm_variable_id(var);
218 if (surf_action_state_get(action) == SURF_ACTION_RUNNING ||
219 surf_action_state_get(action) == SURF_ACTION_READY ||
220 surf_action_state_get(action) ==
221 SURF_ACTION_NOT_IN_THE_SYSTEM) {
222 action->finish = date;
223 surf_action_state_set(action, SURF_ACTION_FAILED);
227 if (tmgr_trace_event_free(event_type))
228 cpu->state_event = NULL;
230 XBT_CRITICAL("Unknown event ! \n");
237 static surf_action_t cpu_execute(void *cpu, double size)
239 surf_action_cpu_Cas01_t action = NULL;
240 cpu_Cas01_t CPU = surf_cpu_resource_priv(cpu);
241 surf_model_t cpu_model = ((surf_resource_t) CPU)->model;
243 XBT_IN("(%s,%g)", surf_resource_name(CPU), size);
245 surf_action_new(sizeof(s_surf_action_cpu_Cas01_t), size,
247 CPU->state_current != SURF_RESOURCE_ON);
249 GENERIC_LMM_ACTION(action).suspended = 0; /* Should be useless because of the
250 calloc but it seems to help valgrind... */
252 GENERIC_LMM_ACTION(action).variable =
253 lmm_variable_new(cpu_model->model_private->maxmin_system, action,
254 GENERIC_ACTION(action).priority,
255 CPU->power_scale * CPU->power_peak, 1);
256 if (cpu_model->model_private->update_mechanism == UM_LAZY) {
257 GENERIC_LMM_ACTION(action).index_heap = -1;
258 GENERIC_LMM_ACTION(action).last_update = surf_get_clock();
259 GENERIC_LMM_ACTION(action).last_value = 0.0;
261 lmm_expand(cpu_model->model_private->maxmin_system, CPU->constraint,
262 GENERIC_LMM_ACTION(action).variable, 1.0);
264 return (surf_action_t) action;
267 static surf_action_t cpu_action_sleep(void *cpu, double duration)
269 surf_action_cpu_Cas01_t action = NULL;
270 cpu_Cas01_t CPU = surf_cpu_resource_priv(cpu);
271 surf_model_t cpu_model = ((surf_resource_t) CPU)->model;
274 duration = MAX(duration, MAXMIN_PRECISION);
276 XBT_IN("(%s,%g)", surf_resource_name(surf_cpu_resource_priv(cpu)), duration);
277 action = (surf_action_cpu_Cas01_t) cpu_execute(cpu, 1.0);
278 // FIXME: sleep variables should not consume 1.0 in lmm_expand
279 GENERIC_ACTION(action).max_duration = duration;
280 GENERIC_LMM_ACTION(action).suspended = 2;
281 if (duration == NO_MAX_DURATION) {
282 /* Move to the *end* of the corresponding action set. This convention
283 is used to speed up update_resource_state */
284 xbt_swag_remove(action, ((surf_action_t) action)->state_set);
285 ((surf_action_t) action)->state_set =
286 cpu_running_action_set_that_does_not_need_being_checked;
287 xbt_swag_insert(action, ((surf_action_t) action)->state_set);
290 lmm_update_variable_weight(cpu_model->model_private->maxmin_system,
291 GENERIC_LMM_ACTION(action).variable, 0.0);
292 if (cpu_model->model_private->update_mechanism == UM_LAZY) { // remove action from the heap
293 surf_action_lmm_heap_remove(cpu_model->model_private->action_heap,(surf_action_lmm_t)action);
294 // this is necessary for a variable with weight 0 since such
295 // variables are ignored in lmm and we need to set its max_duration
296 // correctly at the next call to share_resources
297 xbt_swag_insert_at_head(action, cpu_model->model_private->modified_set);
301 return (surf_action_t) action;
304 static e_surf_resource_state_t cpu_get_state(void *cpu)
306 return ((cpu_Cas01_t)surf_cpu_resource_priv(cpu))->state_current;
309 static double cpu_get_speed(void *cpu, double load)
311 return load * ((cpu_Cas01_t)surf_cpu_resource_priv(cpu))->power_peak;
314 static double cpu_get_available_speed(void *cpu)
316 /* number between 0 and 1 */
317 return ((cpu_Cas01_t)surf_cpu_resource_priv(cpu))->power_scale;
320 static void cpu_finalize(surf_model_t cpu_model)
322 lmm_system_free(cpu_model->model_private->maxmin_system);
323 cpu_model->model_private->maxmin_system = NULL;
325 if (cpu_model->model_private->action_heap)
326 xbt_heap_free(cpu_model->model_private->action_heap);
327 xbt_swag_free(cpu_model->model_private->modified_set);
329 surf_model_exit(cpu_model);
332 xbt_swag_free(cpu_running_action_set_that_does_not_need_being_checked);
333 cpu_running_action_set_that_does_not_need_being_checked = NULL;
336 static void surf_cpu_model_init_internal(surf_model_t cpu_model)
338 s_surf_action_t action;
339 s_surf_action_cpu_Cas01_t comp;
341 char *optim = xbt_cfg_get_string(_sg_cfg_set, "cpu/optim");
343 xbt_cfg_get_int(_sg_cfg_set, "cpu/maxmin_selective_update");
345 cpu_model = surf_model_init();
347 if (!strcmp(optim, "Full")) {
348 cpu_model->model_private->update_mechanism = UM_FULL;
349 cpu_model->model_private->selective_update = select;
350 } else if (!strcmp(optim, "Lazy")) {
351 cpu_model->model_private->update_mechanism = UM_LAZY;
352 cpu_model->model_private->selective_update = 1;
353 xbt_assert((select == 1)
355 (xbt_cfg_is_default_value
356 (_sg_cfg_set, "cpu/maxmin_selective_update")),
357 "Disabling selective update while using the lazy update mechanism is dumb!");
359 xbt_die("Unsupported optimization (%s) for this model", optim);
362 cpu_running_action_set_that_does_not_need_being_checked =
363 xbt_swag_new(xbt_swag_offset(action, state_hookup));
365 cpu_model->name = "cpu";
366 cpu_model->type = SURF_MODEL_TYPE_CPU;
368 cpu_model->action_unref = surf_action_unref;
369 cpu_model->action_cancel = surf_action_cancel;
370 cpu_model->action_state_set = surf_action_state_set;
372 cpu_model->model_private->resource_used = cpu_resource_used;
374 if (cpu_model->model_private->update_mechanism == UM_LAZY) {
375 cpu_model->model_private->share_resources =
376 cpu_share_resources_lazy;
377 cpu_model->model_private->update_actions_state =
378 cpu_update_actions_state_lazy;
379 } else if (cpu_model->model_private->update_mechanism == UM_FULL) {
380 cpu_model->model_private->share_resources =
381 cpu_share_resources_full;
382 cpu_model->model_private->update_actions_state =
383 cpu_update_actions_state_full;
385 xbt_die("Invalid cpu update mechanism!");
387 cpu_model->model_private->update_resource_state =
388 cpu_update_resource_state;
389 cpu_model->model_private->finalize = cpu_finalize;
391 cpu_model->suspend = surf_action_suspend;
392 cpu_model->resume = surf_action_resume;
393 cpu_model->is_suspended = surf_action_is_suspended;
394 cpu_model->set_max_duration = surf_action_set_max_duration;
395 cpu_model->set_priority = surf_action_set_priority;
397 cpu_model->set_category = surf_action_set_category;
399 cpu_model->get_remains = surf_action_get_remains;
401 cpu_model->extension.cpu.execute = cpu_execute;
402 cpu_model->extension.cpu.sleep = cpu_action_sleep;
404 cpu_model->extension.cpu.get_state = cpu_get_state;
405 cpu_model->extension.cpu.get_speed = cpu_get_speed;
406 cpu_model->extension.cpu.get_available_speed =
407 cpu_get_available_speed;
408 cpu_model->extension.cpu.add_traces = cpu_add_traces_cpu;
410 if (!cpu_model->model_private->maxmin_system) {
411 cpu_model->model_private->maxmin_system = lmm_system_new(cpu_model->model_private->selective_update);
413 if (cpu_model->model_private->update_mechanism == UM_LAZY) {
414 cpu_model->model_private->action_heap = xbt_heap_new(8, NULL);
415 xbt_heap_set_update_callback(cpu_model->model_private->action_heap,
416 surf_action_lmm_update_index_heap);
417 cpu_model->model_private->modified_set =
418 xbt_swag_new(xbt_swag_offset(comp, generic_lmm_action.action_list_hookup));
419 cpu_model->model_private->maxmin_system->keep_track = cpu_model->model_private->modified_set;
423 /*********************************************************************/
424 /* Basic sharing model for CPU: that is where all this started... ;) */
425 /*********************************************************************/
426 /* @InProceedings{casanova01simgrid, */
427 /* author = "H. Casanova", */
428 /* booktitle = "Proceedings of the IEEE Symposium on Cluster Computing */
429 /* and the Grid (CCGrid'01)", */
430 /* publisher = "IEEE Computer Society", */
431 /* title = "Simgrid: {A} Toolkit for the Simulation of Application */
435 /* note = "Available at */
436 /* \url{http://grail.sdsc.edu/papers/simgrid_ccgrid01.ps.gz}." */
440 static void create_cpu_model_object(surf_model_t cpu_model)
442 char *optim = xbt_cfg_get_string(_sg_cfg_set, "cpu/optim");
444 xbt_assert(cpu_model == NULL, "wrong intialization");
446 if (!strcmp(optim, "TI")) {
447 surf_cpu_model_init_ti(cpu_model);
451 surf_cpu_model_init_internal(cpu_model);
452 cpu_define_callbacks();
454 /* cpu_model is registered only to model_list, and not to
455 * model_list_invoke. The shared_resource callback function will be called
456 * from that of the workstation model. */
457 xbt_dynar_push(model_list, &cpu_model);
460 void surf_cpu_model_init_Cas01(void)
462 create_cpu_model_object(surf_cpu_model_pm);
463 create_cpu_model_object(surf_cpu_model_vm);
466 /* TODO: do we address nested virtualization later? */
468 surf_model_t cpu_model_cas01(int level){
469 // TODO this table should be allocated
470 if(!surf_cpu_model[level])
472 return surf_cpu_model[level];