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"
11 surf_model_t surf_cpu_model = NULL;
13 #undef GENERIC_LMM_ACTION
16 #define GENERIC_LMM_ACTION(action) action->generic_lmm_action
17 #define GENERIC_ACTION(action) GENERIC_LMM_ACTION(action).generic_action
18 #define ACTION_GET_CPU(action) ((surf_action_cpu_Cas01_t) action)->cpu
20 typedef struct surf_action_cpu_cas01 {
21 s_surf_action_lmm_t generic_lmm_action;
22 } s_surf_action_cpu_Cas01_t, *surf_action_cpu_Cas01_t;
24 typedef struct cpu_Cas01 {
25 s_surf_resource_t generic_resource;
26 s_xbt_swag_hookup_t modified_cpu_hookup;
29 tmgr_trace_event_t power_event;
31 e_surf_resource_state_t state_current;
32 tmgr_trace_event_t state_event;
33 lmm_constraint_t constraint;
34 } s_cpu_Cas01_t, *cpu_Cas01_t;
36 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_cpu, surf,
37 "Logging specific to the SURF CPU IMPROVED module");
42 cpu_running_action_set_that_does_not_need_being_checked = NULL;
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_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);
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 surf_action_cpu_Cas01_t action = NULL;
166 surf_action_cpu_Cas01_t next_action = NULL;
167 xbt_swag_t running_actions = surf_cpu_model->states.running_action_set;
168 xbt_swag_foreach_safe(action, next_action, running_actions) {
170 if (TRACE_is_enabled()) {
172 lmm_constraint_id(lmm_get_cnst_from_var
173 (surf_cpu_model->model_private->maxmin_system,
174 GENERIC_LMM_ACTION(action).variable, 0));
176 TRACE_surf_host_set_utilization(x->generic_resource.name,
177 ((surf_action_t)action)->category,
178 lmm_variable_getvalue(GENERIC_LMM_ACTION(action).
182 TRACE_last_timestamp_to_dump = now - delta;
185 double_update(&(GENERIC_ACTION(action).remains),
186 lmm_variable_getvalue(GENERIC_LMM_ACTION(action).
188 if (GENERIC_LMM_ACTION(action).generic_action.max_duration !=
190 double_update(&(GENERIC_ACTION(action).max_duration), delta);
191 if ((GENERIC_ACTION(action).remains <= 0) &&
192 (lmm_get_variable_weight(GENERIC_LMM_ACTION(action).variable) >
194 GENERIC_ACTION(action).finish = surf_get_clock();
195 surf_action_state_set((surf_action_t) action, SURF_ACTION_DONE);
196 } else if ((GENERIC_ACTION(action).max_duration != NO_MAX_DURATION) &&
197 (GENERIC_ACTION(action).max_duration <= 0)) {
198 GENERIC_ACTION(action).finish = surf_get_clock();
199 surf_action_state_set((surf_action_t) action, SURF_ACTION_DONE);
206 static void cpu_update_resource_state(void *id,
207 tmgr_trace_event_t event_type,
208 double value, double date)
210 cpu_Cas01_t cpu = id;
211 lmm_variable_t var = NULL;
212 lmm_element_t elem = NULL;
214 if (event_type == cpu->power_event) {
215 cpu->power_scale = value;
216 lmm_update_constraint_bound(surf_cpu_model->model_private->maxmin_system, cpu->constraint,
217 cpu->core * cpu->power_scale *
220 TRACE_surf_host_set_power(date, cpu->generic_resource.name,
221 cpu->core * cpu->power_scale *
224 while ((var = lmm_get_var_from_cnst
225 (surf_cpu_model->model_private->maxmin_system, cpu->constraint, &elem))) {
226 surf_action_cpu_Cas01_t action = lmm_variable_id(var);
227 lmm_update_variable_bound(surf_cpu_model->model_private->maxmin_system,
228 GENERIC_LMM_ACTION(action).variable,
229 cpu->power_scale * cpu->power_peak);
231 if (tmgr_trace_event_free(event_type))
232 cpu->power_event = NULL;
233 } else if (event_type == cpu->state_event) {
235 cpu->state_current = SURF_RESOURCE_ON;
237 lmm_constraint_t cnst = cpu->constraint;
239 cpu->state_current = SURF_RESOURCE_OFF;
241 while ((var = lmm_get_var_from_cnst(surf_cpu_model->model_private->maxmin_system, cnst, &elem))) {
242 surf_action_t action = lmm_variable_id(var);
244 if (surf_action_state_get(action) == SURF_ACTION_RUNNING ||
245 surf_action_state_get(action) == SURF_ACTION_READY ||
246 surf_action_state_get(action) ==
247 SURF_ACTION_NOT_IN_THE_SYSTEM) {
248 action->finish = date;
249 surf_action_state_set(action, SURF_ACTION_FAILED);
253 if (tmgr_trace_event_free(event_type))
254 cpu->state_event = NULL;
256 XBT_CRITICAL("Unknown event ! \n");
263 static surf_action_t cpu_execute(void *cpu, double size)
265 surf_action_cpu_Cas01_t action = NULL;
266 cpu_Cas01_t CPU = cpu;
268 XBT_IN("(%s,%g)", surf_resource_name(CPU), size);
270 surf_action_new(sizeof(s_surf_action_cpu_Cas01_t), size,
272 CPU->state_current != SURF_RESOURCE_ON);
274 GENERIC_LMM_ACTION(action).suspended = 0; /* Should be useless because of the
275 calloc but it seems to help valgrind... */
277 GENERIC_LMM_ACTION(action).variable =
278 lmm_variable_new(surf_cpu_model->model_private->maxmin_system, action,
279 GENERIC_ACTION(action).priority,
280 CPU->power_scale * CPU->power_peak, 1);
281 if (surf_cpu_model->model_private->update_mechanism == UM_LAZY) {
282 GENERIC_LMM_ACTION(action).index_heap = -1;
283 GENERIC_LMM_ACTION(action).last_update = surf_get_clock();
284 GENERIC_LMM_ACTION(action).last_value = 0.0;
286 lmm_expand(surf_cpu_model->model_private->maxmin_system, CPU->constraint,
287 GENERIC_LMM_ACTION(action).variable, 1.0);
289 return (surf_action_t) action;
292 static surf_action_t cpu_action_sleep(void *cpu, double duration)
294 surf_action_cpu_Cas01_t action = NULL;
297 duration = MAX(duration, MAXMIN_PRECISION);
299 XBT_IN("(%s,%g)", surf_resource_name(cpu), duration);
300 action = (surf_action_cpu_Cas01_t) cpu_execute(cpu, 1.0);
301 // FIXME: sleep variables should not consume 1.0 in lmm_expand
302 GENERIC_ACTION(action).max_duration = duration;
303 GENERIC_LMM_ACTION(action).suspended = 2;
304 if (duration == NO_MAX_DURATION) {
305 /* Move to the *end* of the corresponding action set. This convention
306 is used to speed up update_resource_state */
307 xbt_swag_remove(action, ((surf_action_t) action)->state_set);
308 ((surf_action_t) action)->state_set =
309 cpu_running_action_set_that_does_not_need_being_checked;
310 xbt_swag_insert(action, ((surf_action_t) action)->state_set);
313 lmm_update_variable_weight(surf_cpu_model->model_private->maxmin_system,
314 GENERIC_LMM_ACTION(action).variable, 0.0);
315 if (surf_cpu_model->model_private->update_mechanism == UM_LAZY) { // remove action from the heap
316 surf_action_lmm_heap_remove(surf_cpu_model->model_private->action_heap,(surf_action_lmm_t)action);
317 // this is necessary for a variable with weight 0 since such
318 // variables are ignored in lmm and we need to set its max_duration
319 // correctly at the next call to share_resources
320 xbt_swag_insert_at_head(action,surf_cpu_model->model_private->modified_set);
324 return (surf_action_t) action;
327 static e_surf_resource_state_t cpu_get_state(void *cpu)
329 return ((cpu_Cas01_t) cpu)->state_current;
332 static double cpu_get_speed(void *cpu, double load)
334 return load * (((cpu_Cas01_t) cpu)->power_peak);
337 static double cpu_get_available_speed(void *cpu)
339 /* number between 0 and 1 */
340 return ((cpu_Cas01_t) cpu)->power_scale;
343 static void cpu_finalize(void)
345 lmm_system_free(surf_cpu_model->model_private->maxmin_system);
346 surf_cpu_model->model_private->maxmin_system = NULL;
348 if (surf_cpu_model->model_private->action_heap)
349 xbt_heap_free(surf_cpu_model->model_private->action_heap);
350 xbt_swag_free(surf_cpu_model->model_private->modified_set);
352 surf_model_exit(surf_cpu_model);
353 surf_cpu_model = NULL;
355 xbt_swag_free(cpu_running_action_set_that_does_not_need_being_checked);
356 cpu_running_action_set_that_does_not_need_being_checked = NULL;
359 static void surf_cpu_model_init_internal()
361 s_surf_action_t action;
362 s_surf_action_cpu_Cas01_t comp;
364 char *optim = xbt_cfg_get_string(_surf_cfg_set, "cpu/optim");
366 xbt_cfg_get_int(_surf_cfg_set, "cpu/maxmin_selective_update");
368 surf_cpu_model = surf_model_init();
370 if (!strcmp(optim, "Full")) {
371 surf_cpu_model->model_private->update_mechanism = UM_FULL;
372 surf_cpu_model->model_private->selective_update = select;
373 } else if (!strcmp(optim, "Lazy")) {
374 surf_cpu_model->model_private->update_mechanism = UM_LAZY;
375 surf_cpu_model->model_private->selective_update = 1;
376 xbt_assert((select == 1)
378 (xbt_cfg_is_default_value
379 (_surf_cfg_set, "cpu/maxmin_selective_update")),
380 "Disabling selective update while using the lazy update mechanism is dumb!");
382 xbt_die("Unsupported optimization (%s) for this model", optim);
385 cpu_running_action_set_that_does_not_need_being_checked =
386 xbt_swag_new(xbt_swag_offset(action, state_hookup));
388 surf_cpu_model->name = "cpu";
390 surf_cpu_model->action_unref = surf_action_unref;
391 surf_cpu_model->action_cancel = surf_action_cancel;
392 surf_cpu_model->action_state_set = surf_action_state_set;
394 surf_cpu_model->model_private->resource_used = cpu_resource_used;
396 if (surf_cpu_model->model_private->update_mechanism == UM_LAZY) {
397 surf_cpu_model->model_private->share_resources =
398 cpu_share_resources_lazy;
399 surf_cpu_model->model_private->update_actions_state =
400 cpu_update_actions_state_lazy;
401 } else if (surf_cpu_model->model_private->update_mechanism == UM_FULL) {
402 surf_cpu_model->model_private->share_resources =
403 cpu_share_resources_full;
404 surf_cpu_model->model_private->update_actions_state =
405 cpu_update_actions_state_full;
407 xbt_die("Invalid cpu update mechanism!");
409 surf_cpu_model->model_private->update_resource_state =
410 cpu_update_resource_state;
411 surf_cpu_model->model_private->finalize = cpu_finalize;
413 surf_cpu_model->suspend = surf_action_suspend;
414 surf_cpu_model->resume = surf_action_resume;
415 surf_cpu_model->is_suspended = surf_action_is_suspended;
416 surf_cpu_model->set_max_duration = surf_action_set_max_duration;
417 surf_cpu_model->set_priority = surf_action_set_priority;
419 surf_cpu_model->set_category = surf_action_set_category;
421 surf_cpu_model->get_remains = surf_action_get_remains;
423 surf_cpu_model->extension.cpu.execute = cpu_execute;
424 surf_cpu_model->extension.cpu.sleep = cpu_action_sleep;
426 surf_cpu_model->extension.cpu.get_state = cpu_get_state;
427 surf_cpu_model->extension.cpu.get_speed = cpu_get_speed;
428 surf_cpu_model->extension.cpu.get_available_speed =
429 cpu_get_available_speed;
430 surf_cpu_model->extension.cpu.create_resource = cpu_create_resource;
431 surf_cpu_model->extension.cpu.add_traces = cpu_add_traces_cpu;
433 if (!surf_cpu_model->model_private->maxmin_system) {
434 surf_cpu_model->model_private->maxmin_system = lmm_system_new(surf_cpu_model->model_private->selective_update);
436 if (surf_cpu_model->model_private->update_mechanism == UM_LAZY) {
437 surf_cpu_model->model_private->action_heap = xbt_heap_new(8, NULL);
438 xbt_heap_set_update_callback(surf_cpu_model->model_private->action_heap,
439 surf_action_lmm_update_index_heap);
440 surf_cpu_model->model_private->modified_set =
441 xbt_swag_new(xbt_swag_offset(comp, generic_lmm_action.action_list_hookup));
442 surf_cpu_model->model_private->maxmin_system->keep_track = surf_cpu_model->model_private->modified_set;
446 /*********************************************************************/
447 /* Basic sharing model for CPU: that is where all this started... ;) */
448 /*********************************************************************/
449 /* @InProceedings{casanova01simgrid, */
450 /* author = "H. Casanova", */
451 /* booktitle = "Proceedings of the IEEE Symposium on Cluster Computing */
452 /* and the Grid (CCGrid'01)", */
453 /* publisher = "IEEE Computer Society", */
454 /* title = "Simgrid: {A} Toolkit for the Simulation of Application */
458 /* note = "Available at */
459 /* \url{http://grail.sdsc.edu/papers/simgrid_ccgrid01.ps.gz}." */
462 void surf_cpu_model_init_Cas01()
464 char *optim = xbt_cfg_get_string(_surf_cfg_set, "cpu/optim");
469 if (!strcmp(optim, "TI")) {
470 surf_cpu_model_init_ti();
474 surf_cpu_model_init_internal();
475 cpu_define_callbacks();
476 xbt_dynar_push(model_list, &surf_cpu_model);