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 surf_model_t surf_cpu_model = NULL;
14 #undef GENERIC_LMM_ACTION
17 #define GENERIC_LMM_ACTION(action) action->generic_lmm_action
18 #define GENERIC_ACTION(action) GENERIC_LMM_ACTION(action).generic_action
19 #define ACTION_GET_CPU(action) ((surf_action_cpu_Cas01_t) action)->cpu
21 typedef struct surf_action_cpu_cas01 {
22 s_surf_action_lmm_t generic_lmm_action;
23 } s_surf_action_cpu_Cas01_t, *surf_action_cpu_Cas01_t;
25 typedef struct cpu_Cas01 {
26 s_surf_resource_t generic_resource;
27 s_xbt_swag_hookup_t modified_cpu_hookup;
30 tmgr_trace_event_t power_event;
32 e_surf_resource_state_t state_current;
33 tmgr_trace_event_t state_event;
34 lmm_constraint_t constraint;
35 } s_cpu_Cas01_t, *cpu_Cas01_t;
37 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_cpu, surf,
38 "Logging specific to the SURF CPU IMPROVED module");
41 cpu_running_action_set_that_does_not_need_being_checked = NULL;
44 /* This function is registered as a callback to sg_platf_new_host() and never called directly */
45 static void *cpu_create_resource(const char *name, double power_peak,
47 tmgr_trace_t power_trace,
49 e_surf_resource_state_t state_initial,
50 tmgr_trace_t state_trace,
51 xbt_dict_t cpu_properties)
53 cpu_Cas01_t cpu = NULL;
55 xbt_assert(!surf_cpu_resource_priv(surf_cpu_resource_by_name(name)),
56 "Host '%s' declared several times in the platform file",
58 cpu = (cpu_Cas01_t) surf_resource_new(sizeof(s_cpu_Cas01_t),
61 cpu->power_peak = power_peak;
62 xbt_assert(cpu->power_peak > 0, "Power has to be >0");
63 cpu->power_scale = power_scale;
65 xbt_assert(core > 0, "Invalid number of cores %d", core);
69 tmgr_history_add_trace(history, power_trace, 0.0, 0, cpu);
71 cpu->state_current = state_initial;
74 tmgr_history_add_trace(history, state_trace, 0.0, 0, cpu);
77 lmm_constraint_new(surf_cpu_model->model_private->maxmin_system, cpu,
78 cpu->core * cpu->power_scale * cpu->power_peak);
80 xbt_lib_set(host_lib, name, SURF_CPU_LEVEL, cpu);
82 return xbt_lib_get_elm_or_null(host_lib, name);;
86 static void parse_cpu_init(sg_platf_host_cbarg_t host)
88 cpu_create_resource(host->id,
94 host->state_trace, host->properties);
97 static void cpu_add_traces_cpu(void)
99 xbt_dict_cursor_t cursor = NULL;
100 char *trace_name, *elm;
101 static int called = 0;
106 /* connect all traces relative to hosts */
107 xbt_dict_foreach(trace_connect_list_host_avail, cursor, trace_name, elm) {
108 tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
109 cpu_Cas01_t host = surf_cpu_resource_by_name(elm);
111 xbt_assert(host, "Host %s undefined", elm);
112 xbt_assert(trace, "Trace %s undefined", trace_name);
115 tmgr_history_add_trace(history, trace, 0.0, 0, host);
118 xbt_dict_foreach(trace_connect_list_power, cursor, trace_name, elm) {
119 tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
120 cpu_Cas01_t host = surf_cpu_resource_by_name(elm);
122 xbt_assert(host, "Host %s undefined", elm);
123 xbt_assert(trace, "Trace %s undefined", trace_name);
126 tmgr_history_add_trace(history, trace, 0.0, 0, host);
130 static void cpu_define_callbacks()
132 sg_platf_host_add_cb(parse_cpu_init);
133 sg_platf_postparse_add_cb(cpu_add_traces_cpu);
136 static int cpu_resource_used(void *resource)
138 return lmm_constraint_used(surf_cpu_model->model_private->maxmin_system,
139 ((cpu_Cas01_t) resource)->constraint);
142 static double cpu_share_resources_lazy(double now)
144 return generic_share_resources_lazy(now, surf_cpu_model);
147 static double cpu_share_resources_full(double now)
149 s_surf_action_cpu_Cas01_t action;
150 return generic_maxmin_share_resources(surf_cpu_model->states.
152 xbt_swag_offset(action,
155 surf_cpu_model->model_private->maxmin_system, lmm_solve);
158 static void cpu_update_actions_state_lazy(double now, double delta)
160 generic_update_actions_state_lazy(now, delta, surf_cpu_model);
163 static void cpu_update_actions_state_full(double now, double delta)
165 generic_update_actions_state_full(now, delta, surf_cpu_model);
168 static void cpu_update_resource_state(void *id,
169 tmgr_trace_event_t event_type,
170 double value, double date)
172 cpu_Cas01_t cpu = id;
173 lmm_variable_t var = NULL;
174 lmm_element_t elem = NULL;
176 surf_watched_hosts();
178 if (event_type == cpu->power_event) {
179 cpu->power_scale = value;
180 lmm_update_constraint_bound(surf_cpu_model->model_private->maxmin_system, cpu->constraint,
181 cpu->core * cpu->power_scale *
184 TRACE_surf_host_set_power(date, cpu->generic_resource.name,
185 cpu->core * cpu->power_scale *
188 while ((var = lmm_get_var_from_cnst
189 (surf_cpu_model->model_private->maxmin_system, cpu->constraint, &elem))) {
190 surf_action_cpu_Cas01_t action = lmm_variable_id(var);
191 lmm_update_variable_bound(surf_cpu_model->model_private->maxmin_system,
192 GENERIC_LMM_ACTION(action).variable,
193 cpu->power_scale * cpu->power_peak);
195 if (tmgr_trace_event_free(event_type))
196 cpu->power_event = NULL;
197 } else if (event_type == cpu->state_event) {
199 cpu->state_current = SURF_RESOURCE_ON;
201 lmm_constraint_t cnst = cpu->constraint;
203 cpu->state_current = SURF_RESOURCE_OFF;
205 while ((var = lmm_get_var_from_cnst(surf_cpu_model->model_private->maxmin_system, cnst, &elem))) {
206 surf_action_t action = lmm_variable_id(var);
208 if (surf_action_state_get(action) == SURF_ACTION_RUNNING ||
209 surf_action_state_get(action) == SURF_ACTION_READY ||
210 surf_action_state_get(action) ==
211 SURF_ACTION_NOT_IN_THE_SYSTEM) {
212 action->finish = date;
213 surf_action_state_set(action, SURF_ACTION_FAILED);
217 if (tmgr_trace_event_free(event_type))
218 cpu->state_event = NULL;
220 XBT_CRITICAL("Unknown event ! \n");
227 static surf_action_t cpu_execute(void *cpu, double size)
229 surf_action_cpu_Cas01_t action = NULL;
230 cpu_Cas01_t CPU = surf_cpu_resource_priv(cpu);
232 XBT_IN("(%s,%g)", surf_resource_name(CPU), size);
234 surf_action_new(sizeof(s_surf_action_cpu_Cas01_t), size,
236 CPU->state_current != SURF_RESOURCE_ON);
238 GENERIC_LMM_ACTION(action).suspended = 0; /* Should be useless because of the
239 calloc but it seems to help valgrind... */
241 GENERIC_LMM_ACTION(action).variable =
242 lmm_variable_new(surf_cpu_model->model_private->maxmin_system, action,
243 GENERIC_ACTION(action).priority,
244 CPU->power_scale * CPU->power_peak, 1);
245 if (surf_cpu_model->model_private->update_mechanism == UM_LAZY) {
246 GENERIC_LMM_ACTION(action).index_heap = -1;
247 GENERIC_LMM_ACTION(action).last_update = surf_get_clock();
248 GENERIC_LMM_ACTION(action).last_value = 0.0;
250 lmm_expand(surf_cpu_model->model_private->maxmin_system, CPU->constraint,
251 GENERIC_LMM_ACTION(action).variable, 1.0);
253 return (surf_action_t) action;
256 static surf_action_t cpu_action_sleep(void *cpu, double duration)
258 surf_action_cpu_Cas01_t action = NULL;
261 duration = MAX(duration, MAXMIN_PRECISION);
263 XBT_IN("(%s,%g)", surf_resource_name(surf_cpu_resource_priv(cpu)), duration);
264 action = (surf_action_cpu_Cas01_t) cpu_execute(cpu, 1.0);
265 // FIXME: sleep variables should not consume 1.0 in lmm_expand
266 GENERIC_ACTION(action).max_duration = duration;
267 GENERIC_LMM_ACTION(action).suspended = 2;
268 if (duration == NO_MAX_DURATION) {
269 /* Move to the *end* of the corresponding action set. This convention
270 is used to speed up update_resource_state */
271 xbt_swag_remove(action, ((surf_action_t) action)->state_set);
272 ((surf_action_t) action)->state_set =
273 cpu_running_action_set_that_does_not_need_being_checked;
274 xbt_swag_insert(action, ((surf_action_t) action)->state_set);
277 lmm_update_variable_weight(surf_cpu_model->model_private->maxmin_system,
278 GENERIC_LMM_ACTION(action).variable, 0.0);
279 if (surf_cpu_model->model_private->update_mechanism == UM_LAZY) { // remove action from the heap
280 surf_action_lmm_heap_remove(surf_cpu_model->model_private->action_heap,(surf_action_lmm_t)action);
281 // this is necessary for a variable with weight 0 since such
282 // variables are ignored in lmm and we need to set its max_duration
283 // correctly at the next call to share_resources
284 xbt_swag_insert_at_head(action,surf_cpu_model->model_private->modified_set);
288 return (surf_action_t) action;
291 static e_surf_resource_state_t cpu_get_state(void *cpu)
293 return ((cpu_Cas01_t)surf_cpu_resource_priv(cpu))->state_current;
296 static double cpu_get_speed(void *cpu, double load)
298 return load * ((cpu_Cas01_t)surf_cpu_resource_priv(cpu))->power_peak;
301 static double cpu_get_available_speed(void *cpu)
303 /* number between 0 and 1 */
304 return ((cpu_Cas01_t)surf_cpu_resource_priv(cpu))->power_scale;
307 static void cpu_finalize(void)
309 lmm_system_free(surf_cpu_model->model_private->maxmin_system);
310 surf_cpu_model->model_private->maxmin_system = NULL;
312 if (surf_cpu_model->model_private->action_heap)
313 xbt_heap_free(surf_cpu_model->model_private->action_heap);
314 xbt_swag_free(surf_cpu_model->model_private->modified_set);
316 surf_model_exit(surf_cpu_model);
317 surf_cpu_model = NULL;
319 xbt_swag_free(cpu_running_action_set_that_does_not_need_being_checked);
320 cpu_running_action_set_that_does_not_need_being_checked = NULL;
323 static void surf_cpu_model_init_internal()
325 s_surf_action_t action;
326 s_surf_action_cpu_Cas01_t comp;
328 char *optim = xbt_cfg_get_string(_sg_cfg_set, "cpu/optim");
330 xbt_cfg_get_int(_sg_cfg_set, "cpu/maxmin_selective_update");
332 surf_cpu_model = surf_model_init();
334 if (!strcmp(optim, "Full")) {
335 surf_cpu_model->model_private->update_mechanism = UM_FULL;
336 surf_cpu_model->model_private->selective_update = select;
337 } else if (!strcmp(optim, "Lazy")) {
338 surf_cpu_model->model_private->update_mechanism = UM_LAZY;
339 surf_cpu_model->model_private->selective_update = 1;
340 xbt_assert((select == 1)
342 (xbt_cfg_is_default_value
343 (_sg_cfg_set, "cpu/maxmin_selective_update")),
344 "Disabling selective update while using the lazy update mechanism is dumb!");
346 xbt_die("Unsupported optimization (%s) for this model", optim);
349 cpu_running_action_set_that_does_not_need_being_checked =
350 xbt_swag_new(xbt_swag_offset(action, state_hookup));
352 surf_cpu_model->name = "cpu";
354 surf_cpu_model->action_unref = surf_action_unref;
355 surf_cpu_model->action_cancel = surf_action_cancel;
356 surf_cpu_model->action_state_set = surf_action_state_set;
358 surf_cpu_model->model_private->resource_used = cpu_resource_used;
360 if (surf_cpu_model->model_private->update_mechanism == UM_LAZY) {
361 surf_cpu_model->model_private->share_resources =
362 cpu_share_resources_lazy;
363 surf_cpu_model->model_private->update_actions_state =
364 cpu_update_actions_state_lazy;
365 } else if (surf_cpu_model->model_private->update_mechanism == UM_FULL) {
366 surf_cpu_model->model_private->share_resources =
367 cpu_share_resources_full;
368 surf_cpu_model->model_private->update_actions_state =
369 cpu_update_actions_state_full;
371 xbt_die("Invalid cpu update mechanism!");
373 surf_cpu_model->model_private->update_resource_state =
374 cpu_update_resource_state;
375 surf_cpu_model->model_private->finalize = cpu_finalize;
377 surf_cpu_model->suspend = surf_action_suspend;
378 surf_cpu_model->resume = surf_action_resume;
379 surf_cpu_model->is_suspended = surf_action_is_suspended;
380 surf_cpu_model->set_max_duration = surf_action_set_max_duration;
381 surf_cpu_model->set_priority = surf_action_set_priority;
383 surf_cpu_model->set_category = surf_action_set_category;
385 surf_cpu_model->get_remains = surf_action_get_remains;
387 surf_cpu_model->extension.cpu.execute = cpu_execute;
388 surf_cpu_model->extension.cpu.sleep = cpu_action_sleep;
390 surf_cpu_model->extension.cpu.get_state = cpu_get_state;
391 surf_cpu_model->extension.cpu.get_speed = cpu_get_speed;
392 surf_cpu_model->extension.cpu.get_available_speed =
393 cpu_get_available_speed;
394 surf_cpu_model->extension.cpu.add_traces = cpu_add_traces_cpu;
396 if (!surf_cpu_model->model_private->maxmin_system) {
397 surf_cpu_model->model_private->maxmin_system = lmm_system_new(surf_cpu_model->model_private->selective_update);
399 if (surf_cpu_model->model_private->update_mechanism == UM_LAZY) {
400 surf_cpu_model->model_private->action_heap = xbt_heap_new(8, NULL);
401 xbt_heap_set_update_callback(surf_cpu_model->model_private->action_heap,
402 surf_action_lmm_update_index_heap);
403 surf_cpu_model->model_private->modified_set =
404 xbt_swag_new(xbt_swag_offset(comp, generic_lmm_action.action_list_hookup));
405 surf_cpu_model->model_private->maxmin_system->keep_track = surf_cpu_model->model_private->modified_set;
409 /*********************************************************************/
410 /* Basic sharing model for CPU: that is where all this started... ;) */
411 /*********************************************************************/
412 /* @InProceedings{casanova01simgrid, */
413 /* author = "H. Casanova", */
414 /* booktitle = "Proceedings of the IEEE Symposium on Cluster Computing */
415 /* and the Grid (CCGrid'01)", */
416 /* publisher = "IEEE Computer Society", */
417 /* title = "Simgrid: {A} Toolkit for the Simulation of Application */
421 /* note = "Available at */
422 /* \url{http://grail.sdsc.edu/papers/simgrid_ccgrid01.ps.gz}." */
425 void surf_cpu_model_init_Cas01()
427 char *optim = xbt_cfg_get_string(_sg_cfg_set, "cpu/optim");
432 if (!strcmp(optim, "TI")) {
433 surf_cpu_model_init_ti();
437 surf_cpu_model_init_internal();
438 cpu_define_callbacks();
439 xbt_dynar_push(model_list, &surf_cpu_model);