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 void cpu_update_action_remaining_lazy(surf_action_cpu_Cas01_t action, double now)
146 xbt_assert(GENERIC_ACTION(action).state_set == surf_cpu_model->states.running_action_set,
147 "You're updating an action that is not running.");
149 /* bogus priority, skip it */
150 xbt_assert(GENERIC_ACTION(action).priority > 0,
151 "You're updating an action that seems suspended.");
153 delta = now - GENERIC_LMM_ACTION(action).last_update;
154 if (GENERIC_ACTION(action).remains > 0) {
155 XBT_DEBUG("Updating action(%p): remains was %lf, last_update was: %lf", action,
156 GENERIC_ACTION(action).remains,
157 GENERIC_LMM_ACTION(action).last_update);
158 double_update(&(GENERIC_ACTION(action).remains),
159 GENERIC_LMM_ACTION(action).last_value * delta);
162 if (TRACE_is_enabled()) {
164 lmm_constraint_id(lmm_get_cnst_from_var
165 (surf_cpu_model->model_private->maxmin_system,
166 GENERIC_LMM_ACTION(action).variable, 0));
167 TRACE_surf_host_set_utilization(cpu->generic_resource.name,
168 ((surf_action_t)action)->category,
169 GENERIC_LMM_ACTION(action).last_value,
170 GENERIC_LMM_ACTION(action).last_update,
171 now - GENERIC_LMM_ACTION(action).last_update);
174 XBT_DEBUG("Updating action(%p): remains is now %lf", action,
175 GENERIC_ACTION(action).remains);
177 GENERIC_LMM_ACTION(action).last_update = now;
178 GENERIC_LMM_ACTION(action).last_value = lmm_variable_getvalue(GENERIC_LMM_ACTION(action).variable);
181 static double cpu_share_resources_lazy(double now)
183 surf_action_cpu_Cas01_t action = NULL;
188 ("Before share resources, the size of modified actions set is %d",
189 xbt_swag_size(surf_cpu_model->model_private->modified_set));
191 lmm_solve(surf_cpu_model->model_private->maxmin_system);
194 ("After share resources, The size of modified actions set is %d",
195 xbt_swag_size(surf_cpu_model->model_private->modified_set));
197 while((action = xbt_swag_extract(surf_cpu_model->model_private->modified_set))) {
198 int max_dur_flag = 0;
200 if (GENERIC_ACTION(action).state_set !=
201 surf_cpu_model->states.running_action_set)
204 /* bogus priority, skip it */
205 if (GENERIC_ACTION(action).priority <= 0)
208 cpu_update_action_remaining_lazy(action,now);
211 value = lmm_variable_getvalue(GENERIC_LMM_ACTION(action).variable);
213 if (GENERIC_ACTION(action).remains > 0) {
214 value = GENERIC_ACTION(action).remains / value;
222 if ((GENERIC_ACTION(action).max_duration != NO_MAX_DURATION)
224 || GENERIC_ACTION(action).start +
225 GENERIC_ACTION(action).max_duration < min)) {
226 min = GENERIC_ACTION(action).start +
227 GENERIC_ACTION(action).max_duration;
231 XBT_DEBUG("Action(%p) Start %lf Finish %lf Max_duration %lf", action,
232 GENERIC_ACTION(action).start, now + value,
233 GENERIC_ACTION(action).max_duration);
236 surf_action_lmm_heap_remove(surf_cpu_model->model_private->action_heap,(surf_action_lmm_t)action);
237 surf_action_lmm_heap_insert(surf_cpu_model->model_private->action_heap,(surf_action_lmm_t)action, min, max_dur_flag ? MAX_DURATION : NORMAL);
238 XBT_DEBUG("Insert at heap action(%p) min %lf now %lf", action, min,
240 } else DIE_IMPOSSIBLE;
243 //hereafter must have already the min value for this resource model
244 if (xbt_heap_size(surf_cpu_model->model_private->action_heap) > 0)
245 min = xbt_heap_maxkey(surf_cpu_model->model_private->action_heap) - now;
249 XBT_DEBUG("The minimum with the HEAP %lf", min);
254 static double cpu_share_resources_full(double now)
256 s_surf_action_cpu_Cas01_t action;
257 return generic_maxmin_share_resources(surf_cpu_model->states.
259 xbt_swag_offset(action,
262 surf_cpu_model->model_private->maxmin_system, lmm_solve);
265 static void cpu_update_actions_state_lazy(double now, double delta)
267 surf_action_cpu_Cas01_t action;
268 while ((xbt_heap_size(surf_cpu_model->model_private->action_heap) > 0)
269 && (double_equals(xbt_heap_maxkey(surf_cpu_model->model_private->action_heap), now))) {
270 action = xbt_heap_pop(surf_cpu_model->model_private->action_heap);
271 XBT_DEBUG("Action %p: finish", action);
272 GENERIC_ACTION(action).finish = surf_get_clock();
274 if (TRACE_is_enabled()) {
276 lmm_constraint_id(lmm_get_cnst_from_var
277 (surf_cpu_model->model_private->maxmin_system,
278 GENERIC_LMM_ACTION(action).variable, 0));
279 TRACE_surf_host_set_utilization(cpu->generic_resource.name,
280 ((surf_action_t)action)->category,
281 lmm_variable_getvalue(GENERIC_LMM_ACTION(action).variable),
282 GENERIC_LMM_ACTION(action).last_update,
283 now - GENERIC_LMM_ACTION(action).last_update);
286 /* set the remains to 0 due to precision problems when updating the remaining amount */
287 GENERIC_ACTION(action).remains = 0;
288 surf_action_state_set((surf_action_t) action, SURF_ACTION_DONE);
289 surf_action_lmm_heap_remove(surf_cpu_model->model_private->action_heap,(surf_action_lmm_t)action); //FIXME: strange call since action was already popped
292 if (TRACE_is_enabled()) {
293 //defining the last timestamp that we can safely dump to trace file
294 //without losing the event ascending order (considering all CPU's)
296 xbt_swag_t running_actions = surf_cpu_model->states.running_action_set;
297 xbt_swag_foreach(action, running_actions) {
299 smaller = GENERIC_LMM_ACTION(action).last_update;
302 if (GENERIC_LMM_ACTION(action).last_update < smaller) {
303 smaller = GENERIC_LMM_ACTION(action).last_update;
307 TRACE_last_timestamp_to_dump = smaller;
314 static void cpu_update_actions_state_full(double now, double delta)
316 surf_action_cpu_Cas01_t action = NULL;
317 surf_action_cpu_Cas01_t next_action = NULL;
318 xbt_swag_t running_actions = surf_cpu_model->states.running_action_set;
319 xbt_swag_foreach_safe(action, next_action, running_actions) {
321 if (TRACE_is_enabled()) {
323 lmm_constraint_id(lmm_get_cnst_from_var
324 (surf_cpu_model->model_private->maxmin_system,
325 GENERIC_LMM_ACTION(action).variable, 0));
327 TRACE_surf_host_set_utilization(x->generic_resource.name,
328 ((surf_action_t)action)->category,
329 lmm_variable_getvalue(GENERIC_LMM_ACTION(action).
333 TRACE_last_timestamp_to_dump = now - delta;
336 double_update(&(GENERIC_ACTION(action).remains),
337 lmm_variable_getvalue(GENERIC_LMM_ACTION(action).
339 if (GENERIC_LMM_ACTION(action).generic_action.max_duration !=
341 double_update(&(GENERIC_ACTION(action).max_duration), delta);
342 if ((GENERIC_ACTION(action).remains <= 0) &&
343 (lmm_get_variable_weight(GENERIC_LMM_ACTION(action).variable) >
345 GENERIC_ACTION(action).finish = surf_get_clock();
346 surf_action_state_set((surf_action_t) action, SURF_ACTION_DONE);
347 } else if ((GENERIC_ACTION(action).max_duration != NO_MAX_DURATION) &&
348 (GENERIC_ACTION(action).max_duration <= 0)) {
349 GENERIC_ACTION(action).finish = surf_get_clock();
350 surf_action_state_set((surf_action_t) action, SURF_ACTION_DONE);
357 static void cpu_update_resource_state(void *id,
358 tmgr_trace_event_t event_type,
359 double value, double date)
361 cpu_Cas01_t cpu = id;
362 lmm_variable_t var = NULL;
363 lmm_element_t elem = NULL;
365 if (event_type == cpu->power_event) {
366 cpu->power_scale = value;
367 lmm_update_constraint_bound(surf_cpu_model->model_private->maxmin_system, cpu->constraint,
368 cpu->core * cpu->power_scale *
371 TRACE_surf_host_set_power(date, cpu->generic_resource.name,
372 cpu->core * cpu->power_scale *
375 while ((var = lmm_get_var_from_cnst
376 (surf_cpu_model->model_private->maxmin_system, cpu->constraint, &elem))) {
377 surf_action_cpu_Cas01_t action = lmm_variable_id(var);
378 lmm_update_variable_bound(surf_cpu_model->model_private->maxmin_system,
379 GENERIC_LMM_ACTION(action).variable,
380 cpu->power_scale * cpu->power_peak);
382 if (tmgr_trace_event_free(event_type))
383 cpu->power_event = NULL;
384 } else if (event_type == cpu->state_event) {
386 cpu->state_current = SURF_RESOURCE_ON;
388 lmm_constraint_t cnst = cpu->constraint;
390 cpu->state_current = SURF_RESOURCE_OFF;
392 while ((var = lmm_get_var_from_cnst(surf_cpu_model->model_private->maxmin_system, cnst, &elem))) {
393 surf_action_t action = lmm_variable_id(var);
395 if (surf_action_state_get(action) == SURF_ACTION_RUNNING ||
396 surf_action_state_get(action) == SURF_ACTION_READY ||
397 surf_action_state_get(action) ==
398 SURF_ACTION_NOT_IN_THE_SYSTEM) {
399 action->finish = date;
400 surf_action_state_set(action, SURF_ACTION_FAILED);
404 if (tmgr_trace_event_free(event_type))
405 cpu->state_event = NULL;
407 XBT_CRITICAL("Unknown event ! \n");
414 static surf_action_t cpu_execute(void *cpu, double size)
416 surf_action_cpu_Cas01_t action = NULL;
417 cpu_Cas01_t CPU = cpu;
419 XBT_IN("(%s,%g)", surf_resource_name(CPU), size);
421 surf_action_new(sizeof(s_surf_action_cpu_Cas01_t), size,
423 CPU->state_current != SURF_RESOURCE_ON);
425 GENERIC_LMM_ACTION(action).suspended = 0; /* Should be useless because of the
426 calloc but it seems to help valgrind... */
428 GENERIC_LMM_ACTION(action).variable =
429 lmm_variable_new(surf_cpu_model->model_private->maxmin_system, action,
430 GENERIC_ACTION(action).priority,
431 CPU->power_scale * CPU->power_peak, 1);
432 if (surf_cpu_model->model_private->update_mechanism == UM_LAZY) {
433 GENERIC_LMM_ACTION(action).index_heap = -1;
434 GENERIC_LMM_ACTION(action).last_update = surf_get_clock();
435 GENERIC_LMM_ACTION(action).last_value = 0.0;
437 lmm_expand(surf_cpu_model->model_private->maxmin_system, CPU->constraint,
438 GENERIC_LMM_ACTION(action).variable, 1.0);
440 return (surf_action_t) action;
443 static surf_action_t cpu_action_sleep(void *cpu, double duration)
445 surf_action_cpu_Cas01_t action = NULL;
448 duration = MAX(duration, MAXMIN_PRECISION);
450 XBT_IN("(%s,%g)", surf_resource_name(cpu), duration);
451 action = (surf_action_cpu_Cas01_t) cpu_execute(cpu, 1.0);
452 // FIXME: sleep variables should not consume 1.0 in lmm_expand
453 GENERIC_ACTION(action).max_duration = duration;
454 GENERIC_LMM_ACTION(action).suspended = 2;
455 if (duration == NO_MAX_DURATION) {
456 /* Move to the *end* of the corresponding action set. This convention
457 is used to speed up update_resource_state */
458 xbt_swag_remove(action, ((surf_action_t) action)->state_set);
459 ((surf_action_t) action)->state_set =
460 cpu_running_action_set_that_does_not_need_being_checked;
461 xbt_swag_insert(action, ((surf_action_t) action)->state_set);
464 lmm_update_variable_weight(surf_cpu_model->model_private->maxmin_system,
465 GENERIC_LMM_ACTION(action).variable, 0.0);
466 if (surf_cpu_model->model_private->update_mechanism == UM_LAZY) { // remove action from the heap
467 surf_action_lmm_heap_remove(surf_cpu_model->model_private->action_heap,(surf_action_lmm_t)action);
468 // this is necessary for a variable with weight 0 since such
469 // variables are ignored in lmm and we need to set its max_duration
470 // correctly at the next call to share_resources
471 xbt_swag_insert_at_head(action,surf_cpu_model->model_private->modified_set);
475 return (surf_action_t) action;
478 static double cpu_action_get_remains(surf_action_t action)
480 XBT_IN("(%p)", action);
481 /* update remains before return it */
482 if (surf_cpu_model->model_private->update_mechanism == UM_LAZY)
483 cpu_update_action_remaining_lazy((surf_action_cpu_Cas01_t)action,
486 return action->remains;
489 static e_surf_resource_state_t cpu_get_state(void *cpu)
491 return ((cpu_Cas01_t) cpu)->state_current;
494 static double cpu_get_speed(void *cpu, double load)
496 return load * (((cpu_Cas01_t) cpu)->power_peak);
499 static double cpu_get_available_speed(void *cpu)
501 /* number between 0 and 1 */
502 return ((cpu_Cas01_t) cpu)->power_scale;
505 static void cpu_finalize(void)
507 lmm_system_free(surf_cpu_model->model_private->maxmin_system);
508 surf_cpu_model->model_private->maxmin_system = NULL;
510 if (surf_cpu_model->model_private->action_heap)
511 xbt_heap_free(surf_cpu_model->model_private->action_heap);
512 xbt_swag_free(surf_cpu_model->model_private->modified_set);
514 surf_model_exit(surf_cpu_model);
515 surf_cpu_model = NULL;
517 xbt_swag_free(cpu_running_action_set_that_does_not_need_being_checked);
518 cpu_running_action_set_that_does_not_need_being_checked = NULL;
521 static void surf_cpu_model_init_internal()
523 s_surf_action_t action;
524 s_surf_action_cpu_Cas01_t comp;
526 char *optim = xbt_cfg_get_string(_surf_cfg_set, "cpu/optim");
528 xbt_cfg_get_int(_surf_cfg_set, "cpu/maxmin_selective_update");
530 surf_cpu_model = surf_model_init();
532 if (!strcmp(optim, "Full")) {
533 surf_cpu_model->model_private->update_mechanism = UM_FULL;
534 surf_cpu_model->model_private->selective_update = select;
535 } else if (!strcmp(optim, "Lazy")) {
536 surf_cpu_model->model_private->update_mechanism = UM_LAZY;
537 surf_cpu_model->model_private->selective_update = 1;
538 xbt_assert((select == 1)
540 (xbt_cfg_is_default_value
541 (_surf_cfg_set, "cpu/maxmin_selective_update")),
542 "Disabling selective update while using the lazy update mechanism is dumb!");
544 xbt_die("Unsupported optimization (%s) for this model", optim);
547 cpu_running_action_set_that_does_not_need_being_checked =
548 xbt_swag_new(xbt_swag_offset(action, state_hookup));
550 surf_cpu_model->name = "cpu";
552 surf_cpu_model->action_unref = surf_action_unref;
553 surf_cpu_model->action_cancel = surf_action_cancel;
554 surf_cpu_model->action_state_set = surf_action_state_set;
556 surf_cpu_model->model_private->resource_used = cpu_resource_used;
558 if (surf_cpu_model->model_private->update_mechanism == UM_LAZY) {
559 surf_cpu_model->model_private->share_resources =
560 cpu_share_resources_lazy;
561 surf_cpu_model->model_private->update_actions_state =
562 cpu_update_actions_state_lazy;
563 } else if (surf_cpu_model->model_private->update_mechanism == UM_FULL) {
564 surf_cpu_model->model_private->share_resources =
565 cpu_share_resources_full;
566 surf_cpu_model->model_private->update_actions_state =
567 cpu_update_actions_state_full;
569 xbt_die("Invalid cpu update mechanism!");
571 surf_cpu_model->model_private->update_resource_state =
572 cpu_update_resource_state;
573 surf_cpu_model->model_private->finalize = cpu_finalize;
575 surf_cpu_model->suspend = surf_action_suspend;
576 surf_cpu_model->resume = surf_action_resume;
577 surf_cpu_model->is_suspended = surf_action_is_suspended;
578 surf_cpu_model->set_max_duration = surf_action_set_max_duration;
579 surf_cpu_model->set_priority = surf_action_set_priority;
581 surf_cpu_model->set_category = surf_action_set_category;
583 surf_cpu_model->get_remains = cpu_action_get_remains;
585 surf_cpu_model->extension.cpu.execute = cpu_execute;
586 surf_cpu_model->extension.cpu.sleep = cpu_action_sleep;
588 surf_cpu_model->extension.cpu.get_state = cpu_get_state;
589 surf_cpu_model->extension.cpu.get_speed = cpu_get_speed;
590 surf_cpu_model->extension.cpu.get_available_speed =
591 cpu_get_available_speed;
592 surf_cpu_model->extension.cpu.create_resource = cpu_create_resource;
593 surf_cpu_model->extension.cpu.add_traces = cpu_add_traces_cpu;
595 if (!surf_cpu_model->model_private->maxmin_system) {
596 surf_cpu_model->model_private->maxmin_system = lmm_system_new(surf_cpu_model->model_private->selective_update);
598 if (surf_cpu_model->model_private->update_mechanism == UM_LAZY) {
599 surf_cpu_model->model_private->action_heap = xbt_heap_new(8, NULL);
600 xbt_heap_set_update_callback(surf_cpu_model->model_private->action_heap,
601 surf_action_lmm_update_index_heap);
602 surf_cpu_model->model_private->modified_set =
603 xbt_swag_new(xbt_swag_offset(comp, generic_lmm_action.action_list_hookup));
604 surf_cpu_model->model_private->maxmin_system->keep_track = surf_cpu_model->model_private->modified_set;
608 /*********************************************************************/
609 /* Basic sharing model for CPU: that is where all this started... ;) */
610 /*********************************************************************/
611 /* @InProceedings{casanova01simgrid, */
612 /* author = "H. Casanova", */
613 /* booktitle = "Proceedings of the IEEE Symposium on Cluster Computing */
614 /* and the Grid (CCGrid'01)", */
615 /* publisher = "IEEE Computer Society", */
616 /* title = "Simgrid: {A} Toolkit for the Simulation of Application */
620 /* note = "Available at */
621 /* \url{http://grail.sdsc.edu/papers/simgrid_ccgrid01.ps.gz}." */
624 void surf_cpu_model_init_Cas01()
626 char *optim = xbt_cfg_get_string(_surf_cfg_set, "cpu/optim");
631 if (!strcmp(optim, "TI")) {
632 surf_cpu_model_init_ti();
636 surf_cpu_model_init_internal();
637 cpu_define_callbacks();
638 xbt_dynar_push(model_list, &surf_cpu_model);