1 /* Copyright (c) 2009, 2010. 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"
9 #undef GENERIC_LMM_ACTION
12 #define GENERIC_LMM_ACTION(action) action->generic_lmm_action
13 #define GENERIC_ACTION(action) GENERIC_LMM_ACTION(action).generic_action
14 #define ACTION_GET_CPU(action) ((surf_action_cpu_Cas01_im_t) action)->cpu
16 typedef struct surf_action_cpu_cas01_im {
17 s_surf_action_lmm_t generic_lmm_action;
18 s_xbt_swag_hookup_t cpu_list_hookup;
21 } s_surf_action_cpu_Cas01_im_t, *surf_action_cpu_Cas01_im_t;
23 typedef struct cpu_Cas01_im {
24 s_surf_resource_t generic_resource;
25 s_xbt_swag_hookup_t modified_cpu_hookup;
28 tmgr_trace_event_t power_event;
30 e_surf_resource_state_t state_current;
31 tmgr_trace_event_t state_event;
32 lmm_constraint_t constraint;
33 xbt_swag_t action_set;
35 } s_cpu_Cas01_im_t, *cpu_Cas01_im_t;
37 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_cpu_im, surf,
38 "Logging specific to the SURF CPU IMPROVED module");
41 lmm_system_t cpu_im_maxmin_system = NULL;
42 static xbt_swag_t cpu_im_modified_cpu = NULL;
43 static xbt_heap_t cpu_im_action_heap = NULL;
44 extern int sg_maxmin_selective_update;
48 cpu_im_running_action_set_that_does_not_need_being_checked = NULL;
50 static void* cpu_im_create_resource(const char *name, double power_peak,
52 tmgr_trace_t power_trace,
54 e_surf_resource_state_t state_initial,
55 tmgr_trace_t state_trace,
56 xbt_dict_t cpu_properties)
58 cpu_Cas01_im_t cpu = NULL;
59 s_surf_action_cpu_Cas01_im_t action;
61 xbt_assert(!surf_cpu_resource_by_name(name),
62 "Host '%s' declared several times in the platform file",
64 cpu = (cpu_Cas01_im_t) surf_resource_new(sizeof(s_cpu_Cas01_im_t),
65 surf_cpu_model, name,cpu_properties);
66 cpu->power_peak = power_peak;
67 xbt_assert(cpu->power_peak > 0, "Power has to be >0");
68 cpu->power_scale = power_scale;
71 tmgr_history_add_trace(history, power_trace, 0.0, 0, cpu);
73 xbt_assert(core>0,"Invalid number of cores %d",core);
75 cpu->state_current = state_initial;
78 tmgr_history_add_trace(history, state_trace, 0.0, 0, cpu);
81 lmm_constraint_new(cpu_im_maxmin_system, cpu,
82 cpu->core * cpu->power_scale * cpu->power_peak);
84 xbt_lib_set(host_lib, name, SURF_CPU_LEVEL, cpu);
85 cpu->action_set = xbt_swag_new(xbt_swag_offset(action, cpu_list_hookup));
91 static void parse_cpu_im_init(sg_platf_host_cbarg_t host)
93 cpu_im_create_resource(host->V_host_id,
94 host->V_host_power_peak,
95 host->V_host_power_scale,
96 host->V_host_power_trace,
98 host->V_host_state_initial,
99 host->V_host_state_trace,
103 static void cpu_im_add_traces_cpu(void)
105 xbt_dict_cursor_t cursor = NULL;
106 char *trace_name, *elm;
107 static int called = 0;
112 /* connect all traces relative to hosts */
113 xbt_dict_foreach(trace_connect_list_host_avail, cursor, trace_name, elm) {
114 tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
115 cpu_Cas01_im_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);
124 xbt_dict_foreach(trace_connect_list_power, cursor, trace_name, elm) {
125 tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
126 cpu_Cas01_im_t host = surf_cpu_resource_by_name(elm);
128 xbt_assert(host, "Host %s undefined", elm);
129 xbt_assert(trace, "Trace %s undefined", trace_name);
132 tmgr_history_add_trace(history, trace, 0.0, 0, host);
136 static void cpu_im_define_callbacks()
138 sg_platf_host_add_cb(parse_cpu_im_init);
139 sg_platf_postparse_add_cb(cpu_im_add_traces_cpu);
142 static int cpu_im_resource_used(void *resource)
144 return lmm_constraint_used(cpu_im_maxmin_system,
145 ((cpu_Cas01_im_t) resource)->constraint);
148 static int cpu_im_action_unref(surf_action_t action)
151 if (!action->refcount) {
152 xbt_swag_remove(action, action->state_set);
153 if (((surf_action_lmm_t) action)->variable)
154 lmm_variable_free(cpu_im_maxmin_system,
155 ((surf_action_lmm_t) action)->variable);
156 /* remove from heap */
157 xbt_heap_remove(cpu_im_action_heap,
158 ((surf_action_cpu_Cas01_im_t) action)->index_heap);
159 xbt_swag_remove(action,
160 ((cpu_Cas01_im_t) ACTION_GET_CPU(action))->action_set);
161 xbt_swag_insert(ACTION_GET_CPU(action), cpu_im_modified_cpu);
163 if (action->category)
164 xbt_free(action->category);
166 surf_action_free(&action);
172 static void cpu_im_action_cancel(surf_action_t action)
174 surf_action_state_set(action, SURF_ACTION_FAILED);
175 xbt_heap_remove(cpu_im_action_heap,
176 ((surf_action_cpu_Cas01_im_t) action)->index_heap);
177 xbt_swag_remove(action,
178 ((cpu_Cas01_im_t) ACTION_GET_CPU(action))->action_set);
182 static void cpu_im_cpu_action_state_set(surf_action_t action,
183 e_surf_action_state_t state)
185 /* if((state==SURF_ACTION_DONE) || (state==SURF_ACTION_FAILED)) */
186 /* if(((surf_action_lmm_t)action)->variable) { */
187 /* lmm_variable_disable(cpu_im_maxmin_system, ((surf_action_lmm_t)action)->variable); */
188 /* ((surf_action_lmm_t)action)->variable = NULL; */
191 surf_action_state_set(action, state);
195 static void cpu_im_update_remains(cpu_Cas01_im_t cpu, double now)
197 surf_action_cpu_Cas01_im_t action;
199 if (cpu->last_update >= now)
201 xbt_swag_foreach(action, cpu->action_set) {
202 if (GENERIC_ACTION(action).state_set !=
203 surf_cpu_model->states.running_action_set)
206 /* bogus priority, skip it */
207 if (GENERIC_ACTION(action).priority <= 0)
210 if (GENERIC_ACTION(action).remains > 0) {
211 double_update(&(GENERIC_ACTION(action).remains),
212 lmm_variable_getvalue(GENERIC_LMM_ACTION
213 (action).variable) * (now -
216 if (TRACE_is_enabled()) {
217 TRACE_surf_host_set_utilization(cpu->generic_resource.name,
219 generic_lmm_action.generic_action.
220 data, (surf_action_t) action,
221 lmm_variable_getvalue
225 now - cpu->last_update);
228 XBT_DEBUG("Update action(%p) remains %lf", action,
229 GENERIC_ACTION(action).remains);
232 cpu->last_update = now;
235 static double cpu_im_share_resources(double now)
237 surf_action_cpu_Cas01_im_t action;
240 cpu_Cas01_im_t cpu, cpu_next;
242 xbt_swag_foreach(cpu, cpu_im_modified_cpu)
243 cpu_im_update_remains(cpu, now);
245 lmm_solve(cpu_im_maxmin_system);
247 xbt_swag_foreach_safe(cpu, cpu_next, cpu_im_modified_cpu) {
248 xbt_swag_foreach(action, cpu->action_set) {
249 if (GENERIC_ACTION(action).state_set !=
250 surf_cpu_model->states.running_action_set)
253 /* bogus priority, skip it */
254 if (GENERIC_ACTION(action).priority <= 0)
258 value = lmm_variable_getvalue(GENERIC_LMM_ACTION(action).variable);
260 if (GENERIC_ACTION(action).remains > 0) {
261 value = GENERIC_ACTION(action).remains / value;
269 if ((GENERIC_ACTION(action).max_duration != NO_MAX_DURATION)
271 || GENERIC_ACTION(action).start +
272 GENERIC_ACTION(action).max_duration < min))
274 GENERIC_ACTION(action).start +
275 GENERIC_ACTION(action).max_duration;
277 XBT_DEBUG("Action(%p) Start %lf Finish %lf Max_duration %lf", action,
278 GENERIC_ACTION(action).start, now + value,
279 GENERIC_ACTION(action).max_duration);
281 if (action->index_heap >= 0) {
282 surf_action_cpu_Cas01_im_t heap_act =
283 xbt_heap_remove(cpu_im_action_heap, action->index_heap);
284 if (heap_act != action)
288 xbt_heap_push(cpu_im_action_heap, action, min);
289 XBT_DEBUG("Insert at heap action(%p) min %lf", action, min);
292 xbt_swag_remove(cpu, cpu_im_modified_cpu);
294 return xbt_heap_size(cpu_im_action_heap) >
295 0 ? xbt_heap_maxkey(cpu_im_action_heap) - now : -1;
298 static void cpu_im_update_actions_state(double now, double delta)
300 surf_action_cpu_Cas01_im_t action;
302 while ((xbt_heap_size(cpu_im_action_heap) > 0)
303 && (double_equals(xbt_heap_maxkey(cpu_im_action_heap), now))) {
304 action = xbt_heap_pop(cpu_im_action_heap);
305 XBT_DEBUG("Action %p: finish", action);
306 GENERIC_ACTION(action).finish = surf_get_clock();
307 /* set the remains to 0 due to precision problems when updating the remaining amount */
309 if (TRACE_is_enabled()) {
310 cpu_Cas01_im_t cpu = ((cpu_Cas01_im_t)(action->cpu));
311 TRACE_surf_host_set_utilization(cpu->generic_resource.name,
312 action->generic_lmm_action.generic_action.data,
313 (surf_action_t) action,
314 lmm_variable_getvalue (GENERIC_LMM_ACTION(action).variable),
316 now - cpu->last_update);
319 GENERIC_ACTION(action).remains = 0;
320 cpu_im_cpu_action_state_set((surf_action_t) action, SURF_ACTION_DONE);
321 cpu_im_update_remains(action->cpu, surf_get_clock());
324 if (TRACE_is_enabled()) {
325 //defining the last timestamp that we can safely dump to trace file
326 //without losing the event ascending order (considering all CPU's)
329 xbt_lib_cursor_t cursor;
332 xbt_lib_foreach(host_lib, cursor, key, data){
333 if(data[SURF_CPU_LEVEL]){
334 cpu = data[SURF_CPU_LEVEL];
336 smaller = cpu->last_update;
339 if (cpu->last_update < smaller){
340 smaller = cpu->last_update;
345 TRACE_last_timestamp_to_dump = smaller;
352 static void cpu_im_update_resource_state(void *id,
353 tmgr_trace_event_t event_type,
354 double value, double date)
356 cpu_Cas01_im_t cpu = id;
357 lmm_variable_t var = NULL;
358 lmm_element_t elem = NULL;
360 if (event_type == cpu->power_event) {
361 cpu->power_scale = value;
362 lmm_update_constraint_bound(cpu_im_maxmin_system, cpu->constraint,
363 cpu->core * cpu->power_scale * cpu->power_peak);
365 TRACE_surf_host_set_power(date, cpu->generic_resource.name,
366 cpu->core * cpu->power_scale * cpu->power_peak);
368 while ((var = lmm_get_var_from_cnst
369 (cpu_im_maxmin_system, cpu->constraint, &elem))) {
370 surf_action_cpu_Cas01_im_t action = lmm_variable_id(var);
371 lmm_update_variable_bound(cpu_im_maxmin_system, action->generic_lmm_action.variable,
372 cpu->power_scale * cpu->power_peak);
374 xbt_swag_insert(cpu, cpu_im_modified_cpu);
375 if (tmgr_trace_event_free(event_type))
376 cpu->power_event = NULL;
377 } else if (event_type == cpu->state_event) {
379 cpu->state_current = SURF_RESOURCE_ON;
381 lmm_constraint_t cnst = cpu->constraint;
382 lmm_variable_t var = NULL;
383 lmm_element_t elem = NULL;
385 cpu->state_current = SURF_RESOURCE_OFF;
388 lmm_get_var_from_cnst(cpu_im_maxmin_system, cnst, &elem))) {
389 surf_action_t action = lmm_variable_id(var);
391 if (surf_action_state_get(action) == SURF_ACTION_RUNNING ||
392 surf_action_state_get(action) == SURF_ACTION_READY ||
393 surf_action_state_get(action) ==
394 SURF_ACTION_NOT_IN_THE_SYSTEM) {
395 action->finish = date;
396 cpu_im_cpu_action_state_set(action, SURF_ACTION_FAILED);
400 if (tmgr_trace_event_free(event_type))
401 cpu->state_event = NULL;
403 XBT_CRITICAL("Unknown event ! \n");
410 static surf_action_t cpu_im_execute(void *cpu, double size)
412 surf_action_cpu_Cas01_im_t action = NULL;
413 cpu_Cas01_im_t CPU = cpu;
415 XBT_IN("(%s,%g)", surf_resource_name(CPU), size);
417 surf_action_new(sizeof(s_surf_action_cpu_Cas01_im_t), size,
419 CPU->state_current != SURF_RESOURCE_ON);
421 GENERIC_LMM_ACTION(action).suspended = 0; /* Should be useless because of the
422 calloc but it seems to help valgrind... */
424 GENERIC_LMM_ACTION(action).variable =
425 lmm_variable_new(cpu_im_maxmin_system, action,
426 GENERIC_ACTION(action).priority, CPU->power_scale * CPU->power_peak, 1);
427 action->index_heap = -1;
429 xbt_swag_insert(CPU, cpu_im_modified_cpu);
430 xbt_swag_insert(action, CPU->action_set);
431 lmm_expand(cpu_im_maxmin_system, CPU->constraint,
432 GENERIC_LMM_ACTION(action).variable, 1.0);
434 return (surf_action_t) action;
437 static surf_action_t cpu_im_action_sleep(void *cpu, double duration)
439 surf_action_cpu_Cas01_im_t action = NULL;
442 duration = MAX(duration, MAXMIN_PRECISION);
444 XBT_IN("(%s,%g)", surf_resource_name(cpu), duration);
445 action = (surf_action_cpu_Cas01_im_t) cpu_im_execute(cpu, 1.0);
446 GENERIC_ACTION(action).max_duration = duration;
447 GENERIC_LMM_ACTION(action).suspended = 2;
448 if (duration == NO_MAX_DURATION) {
449 /* Move to the *end* of the corresponding action set. This convention
450 is used to speed up update_resource_state */
451 xbt_swag_remove(action, ((surf_action_t) action)->state_set);
452 ((surf_action_t) action)->state_set =
453 cpu_im_running_action_set_that_does_not_need_being_checked;
454 xbt_swag_insert(action, ((surf_action_t) action)->state_set);
457 lmm_update_variable_weight(cpu_im_maxmin_system,
458 GENERIC_LMM_ACTION(action).variable, 0.0);
459 xbt_swag_insert(cpu, cpu_im_modified_cpu);
461 return (surf_action_t) action;
464 static void cpu_im_action_suspend(surf_action_t action)
466 XBT_IN("(%p)", action);
467 if (((surf_action_lmm_t) action)->suspended != 2) {
468 lmm_update_variable_weight(cpu_im_maxmin_system,
469 ((surf_action_lmm_t) action)->variable,
471 ((surf_action_lmm_t) action)->suspended = 1;
472 xbt_heap_remove(cpu_im_action_heap,
473 ((surf_action_cpu_Cas01_im_t) action)->index_heap);
474 xbt_swag_insert(ACTION_GET_CPU(action), cpu_im_modified_cpu);
479 static void cpu_im_action_resume(surf_action_t action)
482 XBT_IN("(%p)", action);
483 if (((surf_action_lmm_t) action)->suspended != 2) {
484 lmm_update_variable_weight(cpu_im_maxmin_system,
485 ((surf_action_lmm_t) action)->variable,
487 ((surf_action_lmm_t) action)->suspended = 0;
488 xbt_swag_insert(ACTION_GET_CPU(action), cpu_im_modified_cpu);
493 static int cpu_im_action_is_suspended(surf_action_t action)
495 return (((surf_action_lmm_t) action)->suspended == 1);
498 static void cpu_im_action_set_max_duration(surf_action_t action,
501 XBT_IN("(%p,%g)", action, duration);
503 action->max_duration = duration;
504 /* insert cpu in modified_cpu set to notice the max duration change */
505 xbt_swag_insert(ACTION_GET_CPU(action), cpu_im_modified_cpu);
509 static void cpu_im_action_set_priority(surf_action_t action,
512 XBT_IN("(%p,%g)", action, priority);
513 action->priority = priority;
514 lmm_update_variable_weight(cpu_im_maxmin_system,
515 ((surf_action_lmm_t) action)->variable,
518 xbt_swag_insert(ACTION_GET_CPU(action), cpu_im_modified_cpu);
523 static void cpu_im_action_set_category(surf_action_t action,
524 const char *category)
526 XBT_IN("(%p,%s)", action, category);
527 action->category = xbt_strdup (category);
532 static double cpu_im_action_get_remains(surf_action_t action)
534 XBT_IN("(%p)", action);
535 /* update remains before return it */
536 cpu_im_update_remains(ACTION_GET_CPU(action), surf_get_clock());
537 return action->remains;
541 static e_surf_resource_state_t cpu_im_get_state(void *cpu)
543 return ((cpu_Cas01_im_t) cpu)->state_current;
546 static double cpu_im_get_speed(void *cpu, double load)
548 return load * (((cpu_Cas01_im_t) cpu)->power_peak);
551 static double cpu_im_get_available_speed(void *cpu)
553 /* number between 0 and 1 */
554 return ((cpu_Cas01_im_t) cpu)->power_scale;
557 static void cpu_im_action_update_index_heap(void *action, int i)
559 ((surf_action_cpu_Cas01_im_t) action)->index_heap = i;
562 static void cpu_im_finalize(void)
565 xbt_lib_cursor_t cursor;
568 xbt_lib_foreach(host_lib, cursor, key, cpu){
569 if(cpu[SURF_CPU_LEVEL])
571 cpu_Cas01_im_t CPU = cpu[SURF_CPU_LEVEL];
572 xbt_swag_free(CPU->action_set);
576 lmm_system_free(cpu_im_maxmin_system);
577 cpu_im_maxmin_system = NULL;
579 surf_model_exit(surf_cpu_model);
580 surf_cpu_model = NULL;
583 (cpu_im_running_action_set_that_does_not_need_being_checked);
584 cpu_im_running_action_set_that_does_not_need_being_checked = NULL;
585 xbt_heap_free(cpu_im_action_heap);
586 xbt_swag_free(cpu_im_modified_cpu);
589 static void surf_cpu_im_model_init_internal(void)
591 s_surf_action_t action;
592 s_cpu_Cas01_im_t cpu;
594 surf_cpu_model = surf_model_init();
596 cpu_im_running_action_set_that_does_not_need_being_checked =
597 xbt_swag_new(xbt_swag_offset(action, state_hookup));
599 surf_cpu_model->name = "CPU_IM";
601 surf_cpu_model->action_unref = cpu_im_action_unref;
602 surf_cpu_model->action_cancel = cpu_im_action_cancel;
603 surf_cpu_model->action_state_set = cpu_im_cpu_action_state_set;
605 surf_cpu_model->model_private->resource_used = cpu_im_resource_used;
606 surf_cpu_model->model_private->share_resources = cpu_im_share_resources;
607 surf_cpu_model->model_private->update_actions_state =
608 cpu_im_update_actions_state;
609 surf_cpu_model->model_private->update_resource_state =
610 cpu_im_update_resource_state;
611 surf_cpu_model->model_private->finalize = cpu_im_finalize;
613 surf_cpu_model->suspend = cpu_im_action_suspend;
614 surf_cpu_model->resume = cpu_im_action_resume;
615 surf_cpu_model->is_suspended = cpu_im_action_is_suspended;
616 surf_cpu_model->set_max_duration = cpu_im_action_set_max_duration;
617 surf_cpu_model->set_priority = cpu_im_action_set_priority;
619 surf_cpu_model->set_category = cpu_im_action_set_category;
621 surf_cpu_model->get_remains = cpu_im_action_get_remains;
623 surf_cpu_model->extension.cpu.execute = cpu_im_execute;
624 surf_cpu_model->extension.cpu.sleep = cpu_im_action_sleep;
626 surf_cpu_model->extension.cpu.get_state = cpu_im_get_state;
627 surf_cpu_model->extension.cpu.get_speed = cpu_im_get_speed;
628 surf_cpu_model->extension.cpu.get_available_speed =
629 cpu_im_get_available_speed;
630 surf_cpu_model->extension.cpu.create_resource = cpu_im_create_resource;
631 surf_cpu_model->extension.cpu.add_traces = cpu_im_add_traces_cpu;
633 if (!cpu_im_maxmin_system) {
634 sg_maxmin_selective_update = 1;
635 cpu_im_maxmin_system = lmm_system_new();
637 cpu_im_action_heap = xbt_heap_new(8, NULL);
638 xbt_heap_set_update_callback(cpu_im_action_heap,
639 cpu_im_action_update_index_heap);
640 cpu_im_modified_cpu =
641 xbt_swag_new(xbt_swag_offset(cpu, modified_cpu_hookup));
644 /*********************************************************************/
645 /* Basic sharing model for CPU: that is where all this started... ;) */
646 /*********************************************************************/
647 /* @InProceedings{casanova01simgrid, */
648 /* author = "H. Casanova", */
649 /* booktitle = "Proceedings of the IEEE Symposium on Cluster Computing */
650 /* and the Grid (CCGrid'01)", */
651 /* publisher = "IEEE Computer Society", */
652 /* title = "Simgrid: {A} Toolkit for the Simulation of Application */
656 /* note = "Available at */
657 /* \url{http://grail.sdsc.edu/papers/simgrid_ccgrid01.ps.gz}." */
659 void surf_cpu_model_init_Cas01_im()
663 surf_cpu_im_model_init_internal();
664 cpu_im_define_callbacks();
665 xbt_dynar_push(model_list, &surf_cpu_model);