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"
11 #undef GENERIC_LMM_ACTION
14 #define GENERIC_LMM_ACTION(action) action->generic_lmm_action
15 #define GENERIC_ACTION(action) GENERIC_LMM_ACTION(action).generic_action
16 #define ACTION_GET_CPU(action) ((surf_action_cpu_Cas01_im_t) action)->cpu
18 typedef struct surf_action_cpu_cas01_im {
19 s_surf_action_lmm_t generic_lmm_action;
20 s_xbt_swag_hookup_t cpu_list_hookup;
23 } s_surf_action_cpu_Cas01_im_t, *surf_action_cpu_Cas01_im_t;
25 typedef struct cpu_Cas01_im {
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 xbt_swag_t action_set;
37 } s_cpu_Cas01_im_t, *cpu_Cas01_im_t;
39 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_cpu_im, surf,
40 "Logging specific to the SURF CPU IMPROVED module");
43 lmm_system_t cpu_im_maxmin_system = NULL;
44 static xbt_swag_t cpu_im_modified_cpu = NULL;
45 static xbt_heap_t cpu_im_action_heap = NULL;
46 extern int sg_maxmin_selective_update;
50 cpu_im_running_action_set_that_does_not_need_being_checked = NULL;
52 static void* cpu_im_create_resource(const char *name, double power_peak,
54 tmgr_trace_t power_trace,
56 e_surf_resource_state_t state_initial,
57 tmgr_trace_t state_trace,
58 xbt_dict_t cpu_properties)
60 cpu_Cas01_im_t cpu = NULL;
61 s_surf_action_cpu_Cas01_im_t action;
63 xbt_assert(!surf_cpu_resource_by_name(name),
64 "Host '%s' declared several times in the platform file",
66 cpu = (cpu_Cas01_im_t) surf_resource_new(sizeof(s_cpu_Cas01_im_t),
67 surf_cpu_model, name,cpu_properties);
68 cpu->power_peak = power_peak;
69 xbt_assert(cpu->power_peak > 0, "Power has to be >0");
70 cpu->power_scale = power_scale;
73 tmgr_history_add_trace(history, power_trace, 0.0, 0, cpu);
75 xbt_assert(core>0,"Invalid number of cores %d",core);
77 cpu->state_current = state_initial;
80 tmgr_history_add_trace(history, state_trace, 0.0, 0, cpu);
83 lmm_constraint_new(cpu_im_maxmin_system, cpu,
84 cpu->core * cpu->power_scale * cpu->power_peak);
86 xbt_lib_set(host_lib, name, SURF_CPU_LEVEL, cpu);
87 cpu->action_set = xbt_swag_new(xbt_swag_offset(action, cpu_list_hookup));
93 static void parse_cpu_im_init(sg_platf_host_cbarg_t host)
95 cpu_im_create_resource(host->id,
105 static void cpu_im_add_traces_cpu(void)
107 xbt_dict_cursor_t cursor = NULL;
108 char *trace_name, *elm;
109 static int called = 0;
114 /* connect all traces relative to hosts */
115 xbt_dict_foreach(trace_connect_list_host_avail, cursor, trace_name, elm) {
116 tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
117 cpu_Cas01_im_t host = surf_cpu_resource_by_name(elm);
119 xbt_assert(host, "Host %s undefined", elm);
120 xbt_assert(trace, "Trace %s undefined", trace_name);
123 tmgr_history_add_trace(history, trace, 0.0, 0, host);
126 xbt_dict_foreach(trace_connect_list_power, cursor, trace_name, elm) {
127 tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
128 cpu_Cas01_im_t host = surf_cpu_resource_by_name(elm);
130 xbt_assert(host, "Host %s undefined", elm);
131 xbt_assert(trace, "Trace %s undefined", trace_name);
134 tmgr_history_add_trace(history, trace, 0.0, 0, host);
138 static void cpu_im_define_callbacks()
140 sg_platf_host_add_cb(parse_cpu_im_init);
141 sg_platf_postparse_add_cb(cpu_im_add_traces_cpu);
144 static int cpu_im_resource_used(void *resource)
146 return lmm_constraint_used(cpu_im_maxmin_system,
147 ((cpu_Cas01_im_t) resource)->constraint);
150 static int cpu_im_action_unref(surf_action_t action)
153 if (!action->refcount) {
154 xbt_swag_remove(action, action->state_set);
155 if (((surf_action_lmm_t) action)->variable)
156 lmm_variable_free(cpu_im_maxmin_system,
157 ((surf_action_lmm_t) action)->variable);
158 /* remove from heap */
159 xbt_heap_remove(cpu_im_action_heap,
160 ((surf_action_cpu_Cas01_im_t) action)->index_heap);
161 xbt_swag_remove(action,
162 ((cpu_Cas01_im_t) ACTION_GET_CPU(action))->action_set);
163 xbt_swag_insert(ACTION_GET_CPU(action), cpu_im_modified_cpu);
165 xbt_free(action->category);
167 surf_action_free(&action);
173 static void cpu_im_action_cancel(surf_action_t action)
175 surf_action_state_set(action, SURF_ACTION_FAILED);
176 xbt_heap_remove(cpu_im_action_heap,
177 ((surf_action_cpu_Cas01_im_t) action)->index_heap);
178 xbt_swag_remove(action,
179 ((cpu_Cas01_im_t) ACTION_GET_CPU(action))->action_set);
183 static void cpu_im_cpu_action_state_set(surf_action_t action,
184 e_surf_action_state_t state)
186 /* if((state==SURF_ACTION_DONE) || (state==SURF_ACTION_FAILED)) */
187 /* if(((surf_action_lmm_t)action)->variable) { */
188 /* lmm_variable_disable(cpu_im_maxmin_system, ((surf_action_lmm_t)action)->variable); */
189 /* ((surf_action_lmm_t)action)->variable = NULL; */
192 surf_action_state_set(action, state);
196 static void cpu_im_update_remains(cpu_Cas01_im_t cpu, double now)
198 surf_action_cpu_Cas01_im_t action;
200 if (cpu->last_update >= now)
202 xbt_swag_foreach(action, cpu->action_set) {
203 if (GENERIC_ACTION(action).state_set !=
204 surf_cpu_model->states.running_action_set)
207 /* bogus priority, skip it */
208 if (GENERIC_ACTION(action).priority <= 0)
211 if (GENERIC_ACTION(action).remains > 0) {
212 double_update(&(GENERIC_ACTION(action).remains),
213 lmm_variable_getvalue(GENERIC_LMM_ACTION
214 (action).variable) * (now -
217 if (TRACE_is_enabled()) {
218 TRACE_surf_host_set_utilization(cpu->generic_resource.name,
220 generic_lmm_action.generic_action.
221 data, (surf_action_t) action,
222 lmm_variable_getvalue
226 now - cpu->last_update);
229 XBT_DEBUG("Update action(%p) remains %lf", action,
230 GENERIC_ACTION(action).remains);
233 cpu->last_update = now;
236 static double cpu_im_share_resources(double now)
238 surf_action_cpu_Cas01_im_t action;
241 cpu_Cas01_im_t cpu, cpu_next;
243 xbt_swag_foreach(cpu, cpu_im_modified_cpu)
244 cpu_im_update_remains(cpu, now);
246 lmm_solve(cpu_im_maxmin_system);
248 xbt_swag_foreach_safe(cpu, cpu_next, cpu_im_modified_cpu) {
249 xbt_swag_foreach(action, cpu->action_set) {
250 if (GENERIC_ACTION(action).state_set !=
251 surf_cpu_model->states.running_action_set)
254 /* bogus priority, skip it */
255 if (GENERIC_ACTION(action).priority <= 0)
259 value = lmm_variable_getvalue(GENERIC_LMM_ACTION(action).variable);
261 if (GENERIC_ACTION(action).remains > 0) {
262 value = GENERIC_ACTION(action).remains / value;
270 if ((GENERIC_ACTION(action).max_duration != NO_MAX_DURATION)
272 || GENERIC_ACTION(action).start +
273 GENERIC_ACTION(action).max_duration < min))
275 GENERIC_ACTION(action).start +
276 GENERIC_ACTION(action).max_duration;
278 XBT_DEBUG("Action(%p) Start %lf Finish %lf Max_duration %lf", action,
279 GENERIC_ACTION(action).start, now + value,
280 GENERIC_ACTION(action).max_duration);
282 if (action->index_heap >= 0) {
283 surf_action_cpu_Cas01_im_t heap_act =
284 xbt_heap_remove(cpu_im_action_heap, action->index_heap);
285 if (heap_act != action)
289 xbt_heap_push(cpu_im_action_heap, action, min);
290 XBT_DEBUG("Insert at heap action(%p) min %lf", action, min);
293 xbt_swag_remove(cpu, cpu_im_modified_cpu);
295 return xbt_heap_size(cpu_im_action_heap) >
296 0 ? xbt_heap_maxkey(cpu_im_action_heap) - now : -1;
299 static void cpu_im_update_actions_state(double now, double delta)
301 surf_action_cpu_Cas01_im_t action;
303 while ((xbt_heap_size(cpu_im_action_heap) > 0)
304 && (double_equals(xbt_heap_maxkey(cpu_im_action_heap), now))) {
305 action = xbt_heap_pop(cpu_im_action_heap);
306 XBT_DEBUG("Action %p: finish", action);
307 GENERIC_ACTION(action).finish = surf_get_clock();
308 /* set the remains to 0 due to precision problems when updating the remaining amount */
310 if (TRACE_is_enabled()) {
311 cpu_Cas01_im_t cpu = ((cpu_Cas01_im_t)(action->cpu));
312 TRACE_surf_host_set_utilization(cpu->generic_resource.name,
313 action->generic_lmm_action.generic_action.data,
314 (surf_action_t) action,
315 lmm_variable_getvalue (GENERIC_LMM_ACTION(action).variable),
317 now - cpu->last_update);
320 GENERIC_ACTION(action).remains = 0;
321 cpu_im_cpu_action_state_set((surf_action_t) action, SURF_ACTION_DONE);
322 cpu_im_update_remains(action->cpu, surf_get_clock());
325 if (TRACE_is_enabled()) {
326 //defining the last timestamp that we can safely dump to trace file
327 //without losing the event ascending order (considering all CPU's)
330 xbt_lib_cursor_t cursor;
333 xbt_lib_foreach(host_lib, cursor, key, data){
334 if(data[SURF_CPU_LEVEL]){
335 cpu = data[SURF_CPU_LEVEL];
337 smaller = cpu->last_update;
340 if (cpu->last_update < smaller){
341 smaller = cpu->last_update;
346 TRACE_last_timestamp_to_dump = smaller;
353 static void cpu_im_update_resource_state(void *id,
354 tmgr_trace_event_t event_type,
355 double value, double date)
357 cpu_Cas01_im_t cpu = id;
358 lmm_variable_t var = NULL;
359 lmm_element_t elem = NULL;
361 if (event_type == cpu->power_event) {
362 cpu->power_scale = value;
363 lmm_update_constraint_bound(cpu_im_maxmin_system, cpu->constraint,
364 cpu->core * cpu->power_scale * cpu->power_peak);
366 TRACE_surf_host_set_power(date, cpu->generic_resource.name,
367 cpu->core * cpu->power_scale * cpu->power_peak);
369 while ((var = lmm_get_var_from_cnst
370 (cpu_im_maxmin_system, cpu->constraint, &elem))) {
371 surf_action_cpu_Cas01_im_t action = lmm_variable_id(var);
372 lmm_update_variable_bound(cpu_im_maxmin_system, action->generic_lmm_action.variable,
373 cpu->power_scale * cpu->power_peak);
375 xbt_swag_insert(cpu, cpu_im_modified_cpu);
376 if (tmgr_trace_event_free(event_type))
377 cpu->power_event = NULL;
378 } else if (event_type == cpu->state_event) {
380 cpu->state_current = SURF_RESOURCE_ON;
382 lmm_constraint_t cnst = cpu->constraint;
383 lmm_variable_t var = NULL;
384 lmm_element_t elem = NULL;
386 cpu->state_current = SURF_RESOURCE_OFF;
389 lmm_get_var_from_cnst(cpu_im_maxmin_system, cnst, &elem))) {
390 surf_action_t action = lmm_variable_id(var);
392 if (surf_action_state_get(action) == SURF_ACTION_RUNNING ||
393 surf_action_state_get(action) == SURF_ACTION_READY ||
394 surf_action_state_get(action) ==
395 SURF_ACTION_NOT_IN_THE_SYSTEM) {
396 action->finish = date;
397 cpu_im_cpu_action_state_set(action, SURF_ACTION_FAILED);
401 if (tmgr_trace_event_free(event_type))
402 cpu->state_event = NULL;
404 XBT_CRITICAL("Unknown event ! \n");
411 static surf_action_t cpu_im_execute(void *cpu, double size)
413 surf_action_cpu_Cas01_im_t action = NULL;
414 cpu_Cas01_im_t CPU = cpu;
416 XBT_IN("(%s,%g)", surf_resource_name(CPU), size);
418 surf_action_new(sizeof(s_surf_action_cpu_Cas01_im_t), size,
420 CPU->state_current != SURF_RESOURCE_ON);
422 GENERIC_LMM_ACTION(action).suspended = 0; /* Should be useless because of the
423 calloc but it seems to help valgrind... */
425 GENERIC_LMM_ACTION(action).variable =
426 lmm_variable_new(cpu_im_maxmin_system, action,
427 GENERIC_ACTION(action).priority, CPU->power_scale * CPU->power_peak, 1);
428 action->index_heap = -1;
430 xbt_swag_insert(CPU, cpu_im_modified_cpu);
431 xbt_swag_insert(action, CPU->action_set);
432 lmm_expand(cpu_im_maxmin_system, CPU->constraint,
433 GENERIC_LMM_ACTION(action).variable, 1.0);
435 return (surf_action_t) action;
438 static surf_action_t cpu_im_action_sleep(void *cpu, double duration)
440 surf_action_cpu_Cas01_im_t action = NULL;
443 duration = MAX(duration, MAXMIN_PRECISION);
445 XBT_IN("(%s,%g)", surf_resource_name(cpu), duration);
446 action = (surf_action_cpu_Cas01_im_t) cpu_im_execute(cpu, 1.0);
447 GENERIC_ACTION(action).max_duration = duration;
448 GENERIC_LMM_ACTION(action).suspended = 2;
449 if (duration == NO_MAX_DURATION) {
450 /* Move to the *end* of the corresponding action set. This convention
451 is used to speed up update_resource_state */
452 xbt_swag_remove(action, ((surf_action_t) action)->state_set);
453 ((surf_action_t) action)->state_set =
454 cpu_im_running_action_set_that_does_not_need_being_checked;
455 xbt_swag_insert(action, ((surf_action_t) action)->state_set);
458 lmm_update_variable_weight(cpu_im_maxmin_system,
459 GENERIC_LMM_ACTION(action).variable, 0.0);
460 xbt_swag_insert(cpu, cpu_im_modified_cpu);
462 return (surf_action_t) action;
465 static void cpu_im_action_suspend(surf_action_t action)
467 XBT_IN("(%p)", action);
468 if (((surf_action_lmm_t) action)->suspended != 2) {
469 lmm_update_variable_weight(cpu_im_maxmin_system,
470 ((surf_action_lmm_t) action)->variable,
472 ((surf_action_lmm_t) action)->suspended = 1;
473 xbt_heap_remove(cpu_im_action_heap,
474 ((surf_action_cpu_Cas01_im_t) action)->index_heap);
475 xbt_swag_insert(ACTION_GET_CPU(action), cpu_im_modified_cpu);
480 static void cpu_im_action_resume(surf_action_t action)
483 XBT_IN("(%p)", action);
484 if (((surf_action_lmm_t) action)->suspended != 2) {
485 lmm_update_variable_weight(cpu_im_maxmin_system,
486 ((surf_action_lmm_t) action)->variable,
488 ((surf_action_lmm_t) action)->suspended = 0;
489 xbt_swag_insert(ACTION_GET_CPU(action), cpu_im_modified_cpu);
494 static int cpu_im_action_is_suspended(surf_action_t action)
496 return (((surf_action_lmm_t) action)->suspended == 1);
499 static void cpu_im_action_set_max_duration(surf_action_t action,
502 XBT_IN("(%p,%g)", action, duration);
504 action->max_duration = duration;
505 /* insert cpu in modified_cpu set to notice the max duration change */
506 xbt_swag_insert(ACTION_GET_CPU(action), cpu_im_modified_cpu);
510 static void cpu_im_action_set_priority(surf_action_t action,
513 XBT_IN("(%p,%g)", action, priority);
514 action->priority = priority;
515 lmm_update_variable_weight(cpu_im_maxmin_system,
516 ((surf_action_lmm_t) action)->variable,
519 xbt_swag_insert(ACTION_GET_CPU(action), cpu_im_modified_cpu);
524 static void cpu_im_action_set_category(surf_action_t action,
525 const char *category)
527 XBT_IN("(%p,%s)", action, category);
528 action->category = xbt_strdup (category);
533 static double cpu_im_action_get_remains(surf_action_t action)
535 XBT_IN("(%p)", action);
536 /* update remains before return it */
537 cpu_im_update_remains(ACTION_GET_CPU(action), surf_get_clock());
538 return action->remains;
542 static e_surf_resource_state_t cpu_im_get_state(void *cpu)
544 return ((cpu_Cas01_im_t) cpu)->state_current;
547 static double cpu_im_get_speed(void *cpu, double load)
549 return load * (((cpu_Cas01_im_t) cpu)->power_peak);
552 static double cpu_im_get_available_speed(void *cpu)
554 /* number between 0 and 1 */
555 return ((cpu_Cas01_im_t) cpu)->power_scale;
558 static void cpu_im_action_update_index_heap(void *action, int i)
560 ((surf_action_cpu_Cas01_im_t) action)->index_heap = i;
563 static void cpu_im_finalize(void)
566 xbt_lib_cursor_t cursor;
569 xbt_lib_foreach(host_lib, cursor, key, cpu){
570 if(cpu[SURF_CPU_LEVEL])
572 cpu_Cas01_im_t CPU = cpu[SURF_CPU_LEVEL];
573 xbt_swag_free(CPU->action_set);
577 lmm_system_free(cpu_im_maxmin_system);
578 cpu_im_maxmin_system = NULL;
580 surf_model_exit(surf_cpu_model);
581 surf_cpu_model = NULL;
584 (cpu_im_running_action_set_that_does_not_need_being_checked);
585 cpu_im_running_action_set_that_does_not_need_being_checked = NULL;
586 xbt_heap_free(cpu_im_action_heap);
587 xbt_swag_free(cpu_im_modified_cpu);
590 static void surf_cpu_im_model_init_internal(void)
592 s_surf_action_t action;
593 s_cpu_Cas01_im_t cpu;
595 surf_cpu_model = surf_model_init();
597 cpu_im_running_action_set_that_does_not_need_being_checked =
598 xbt_swag_new(xbt_swag_offset(action, state_hookup));
600 surf_cpu_model->name = "CPU_IM";
602 surf_cpu_model->action_unref = cpu_im_action_unref;
603 surf_cpu_model->action_cancel = cpu_im_action_cancel;
604 surf_cpu_model->action_state_set = cpu_im_cpu_action_state_set;
606 surf_cpu_model->model_private->resource_used = cpu_im_resource_used;
607 surf_cpu_model->model_private->share_resources = cpu_im_share_resources;
608 surf_cpu_model->model_private->update_actions_state =
609 cpu_im_update_actions_state;
610 surf_cpu_model->model_private->update_resource_state =
611 cpu_im_update_resource_state;
612 surf_cpu_model->model_private->finalize = cpu_im_finalize;
614 surf_cpu_model->suspend = cpu_im_action_suspend;
615 surf_cpu_model->resume = cpu_im_action_resume;
616 surf_cpu_model->is_suspended = cpu_im_action_is_suspended;
617 surf_cpu_model->set_max_duration = cpu_im_action_set_max_duration;
618 surf_cpu_model->set_priority = cpu_im_action_set_priority;
620 surf_cpu_model->set_category = cpu_im_action_set_category;
622 surf_cpu_model->get_remains = cpu_im_action_get_remains;
624 surf_cpu_model->extension.cpu.execute = cpu_im_execute;
625 surf_cpu_model->extension.cpu.sleep = cpu_im_action_sleep;
627 surf_cpu_model->extension.cpu.get_state = cpu_im_get_state;
628 surf_cpu_model->extension.cpu.get_speed = cpu_im_get_speed;
629 surf_cpu_model->extension.cpu.get_available_speed =
630 cpu_im_get_available_speed;
631 surf_cpu_model->extension.cpu.create_resource = cpu_im_create_resource;
632 surf_cpu_model->extension.cpu.add_traces = cpu_im_add_traces_cpu;
634 if (!cpu_im_maxmin_system) {
635 sg_maxmin_selective_update = 1;
636 cpu_im_maxmin_system = lmm_system_new();
638 cpu_im_action_heap = xbt_heap_new(8, NULL);
639 xbt_heap_set_update_callback(cpu_im_action_heap,
640 cpu_im_action_update_index_heap);
641 cpu_im_modified_cpu =
642 xbt_swag_new(xbt_swag_offset(cpu, modified_cpu_hookup));
645 /*********************************************************************/
646 /* Basic sharing model for CPU: that is where all this started... ;) */
647 /*********************************************************************/
648 /* @InProceedings{casanova01simgrid, */
649 /* author = "H. Casanova", */
650 /* booktitle = "Proceedings of the IEEE Symposium on Cluster Computing */
651 /* and the Grid (CCGrid'01)", */
652 /* publisher = "IEEE Computer Society", */
653 /* title = "Simgrid: {A} Toolkit for the Simulation of Application */
657 /* note = "Available at */
658 /* \url{http://grail.sdsc.edu/papers/simgrid_ccgrid01.ps.gz}." */
660 void surf_cpu_model_init_Cas01_im()
664 surf_cpu_im_model_init_internal();
665 cpu_im_define_callbacks();
666 xbt_dynar_push(model_list, &surf_cpu_model);