3 /* Copyright (c) 2004 Arnaud Legrand. All rights reserved. */
5 /* This program is free software; you can redistribute it and/or modify it
6 * under the terms of the license (GNU LGPL) which comes with this package. */
8 #include "cpu_private.h"
10 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_cpu, surf,
11 "Logging specific to the SURF CPU module");
13 surf_cpu_model_t surf_cpu_model = NULL;
14 lmm_system_t cpu_maxmin_system = NULL;
16 xbt_dict_t cpu_set = NULL;
18 static void cpu_free(void *cpu)
20 free(((cpu_Cas01_t) cpu)->name);
24 static cpu_Cas01_t cpu_new(char *name, double power_scale,
26 tmgr_trace_t power_trace,
27 e_surf_cpu_state_t state_initial,
28 tmgr_trace_t state_trace,
29 xbt_dict_t cpu_properties)
31 cpu_Cas01_t cpu = xbt_new0(s_cpu_Cas01_t, 1);
32 xbt_assert1(!xbt_dict_get_or_null(cpu_set, name),
33 "Host '%s' declared several times in the platform file",name);
35 cpu->model = (surf_model_t) surf_cpu_model;
37 cpu->power_scale = power_scale;
38 xbt_assert0(cpu->power_scale > 0, "Power has to be >0");
39 cpu->power_current = power_initial;
42 tmgr_history_add_trace(history, power_trace, 0.0, 0, cpu);
44 cpu->state_current = state_initial;
47 tmgr_history_add_trace(history, state_trace, 0.0, 0, cpu);
50 lmm_constraint_new(cpu_maxmin_system, cpu,
51 cpu->power_current * cpu->power_scale);
53 /*add the property set*/
54 cpu->properties = cpu_properties;
56 current_property_set = cpu_properties;
58 xbt_dict_set(cpu_set, name, cpu, cpu_free);
64 static void parse_cpu_init(void)
66 double power_scale = 0.0;
67 double power_initial = 0.0;
68 tmgr_trace_t power_trace = NULL;
69 e_surf_cpu_state_t state_initial = SURF_CPU_OFF;
70 tmgr_trace_t state_trace = NULL;
72 power_scale = get_cpu_power(A_surfxml_host_power);
73 surf_parse_get_double(&power_initial, A_surfxml_host_availability);
74 surf_parse_get_trace(&power_trace, A_surfxml_host_availability_file);
76 xbt_assert0((A_surfxml_host_state == A_surfxml_host_state_ON) ||
77 (A_surfxml_host_state == A_surfxml_host_state_OFF),
79 if (A_surfxml_host_state == A_surfxml_host_state_ON)
80 state_initial = SURF_CPU_ON;
81 if (A_surfxml_host_state == A_surfxml_host_state_OFF)
82 state_initial = SURF_CPU_OFF;
83 surf_parse_get_trace(&state_trace, A_surfxml_host_state_file);
85 current_property_set = xbt_dict_new();
86 cpu_new(xbt_strdup(A_surfxml_host_id), power_scale, power_initial, power_trace, state_initial,
87 state_trace, current_property_set);
91 static void add_traces_cpu(void) {
92 xbt_dict_cursor_t cursor=NULL;
93 char *trace_name,*elm;
95 static int called = 0;
101 /* connect all traces relative to hosts */
102 xbt_dict_foreach(trace_connect_list_host_avail, cursor, trace_name, elm) {
103 tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
104 cpu_Cas01_t host = xbt_dict_get_or_null(cpu_set, elm);
106 xbt_assert1(host, "Host %s undefined", elm);
107 xbt_assert1(trace, "Trace %s undefined", trace_name);
109 host->state_event = tmgr_history_add_trace(history, trace, 0.0, 0, host);
112 xbt_dict_foreach(trace_connect_list_power, cursor, trace_name, elm) {
113 tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
114 cpu_Cas01_t host = xbt_dict_get_or_null(cpu_set, elm);
116 xbt_assert1(host, "Host %s undefined", elm);
117 xbt_assert1(trace, "Trace %s undefined", trace_name);
119 host->power_event = tmgr_history_add_trace(history, trace, 0.0, 0, host);
123 static void define_callbacks(const char *file)
125 surf_parse_reset_parser();
126 surfxml_add_callback(STag_surfxml_host_cb_list, parse_cpu_init);
127 surfxml_add_callback(STag_surfxml_prop_cb_list, parse_properties);
128 surfxml_add_callback(STag_surfxml_random_cb_list, &init_randomness);
129 surfxml_add_callback(ETag_surfxml_random_cb_list, &add_randomness);
132 static void *name_service(const char *name)
134 return xbt_dict_get_or_null(cpu_set, name);
137 static const char *get_resource_name(void *resource_id)
139 return ((cpu_Cas01_t) resource_id)->name;
142 static int resource_used(void *resource_id)
144 return lmm_constraint_used(cpu_maxmin_system,
145 ((cpu_Cas01_t) resource_id)->constraint);
148 static int action_free(surf_action_t action)
151 if (!action->using) {
152 xbt_swag_remove(action, action->state_set);
153 if (((surf_action_cpu_Cas01_t) action)->variable)
154 lmm_variable_free(cpu_maxmin_system,
155 ((surf_action_cpu_Cas01_t) action)->variable);
162 static void action_use(surf_action_t action)
167 static void action_cancel(surf_action_t action)
169 surf_action_change_state(action, SURF_ACTION_FAILED);
173 static void action_recycle(surf_action_t action)
178 static void action_change_state(surf_action_t action,
179 e_surf_action_state_t state)
181 /* if((state==SURF_ACTION_DONE) || (state==SURF_ACTION_FAILED)) */
182 /* if(((surf_action_cpu_Cas01_t)action)->variable) { */
183 /* lmm_variable_disable(cpu_maxmin_system, ((surf_action_cpu_Cas01_t)action)->variable); */
184 /* ((surf_action_cpu_Cas01_t)action)->variable = NULL; */
187 surf_action_change_state(action, state);
191 static double share_resources(double now)
193 s_surf_action_cpu_Cas01_t action;
194 return generic_maxmin_share_resources(surf_cpu_model->common_public->
195 states.running_action_set,
196 xbt_swag_offset(action, variable),
197 cpu_maxmin_system, lmm_solve);
200 static void update_actions_state(double now, double delta)
202 surf_action_cpu_Cas01_t action = NULL;
203 surf_action_cpu_Cas01_t next_action = NULL;
204 xbt_swag_t running_actions =
205 surf_cpu_model->common_public->states.running_action_set;
207 xbt_swag_t failed_actions =
208 surf_cpu_model->common_public->states.failed_action_set;
211 xbt_swag_foreach_safe(action, next_action, running_actions) {
212 if (action->generic_action.max_duration == NO_MAX_DURATION &&
213 action->suspended==2 )
214 /* Rely on the fact that sleep action with no duration are on
215 the end of the queue */
218 double_update(&(action->generic_action.remains),
219 lmm_variable_getvalue(action->variable) * delta);
220 if (action->generic_action.max_duration != NO_MAX_DURATION)
221 double_update(&(action->generic_action.max_duration), delta);
222 if ((action->generic_action.remains <= 0) &&
223 (lmm_get_variable_weight(action->variable) > 0)) {
224 action->generic_action.finish = surf_get_clock();
225 action_change_state((surf_action_t) action, SURF_ACTION_DONE);
226 } else if ((action->generic_action.max_duration != NO_MAX_DURATION) &&
227 (action->generic_action.max_duration <= 0)) {
228 action->generic_action.finish = surf_get_clock();
229 action_change_state((surf_action_t) action, SURF_ACTION_DONE);
236 static void update_resource_state(void *id,
237 tmgr_trace_event_t event_type,
238 double value, double date)
240 cpu_Cas01_t cpu = id;
242 if (event_type == cpu->power_event) {
243 cpu->power_current = value;
244 lmm_update_constraint_bound(cpu_maxmin_system, cpu->constraint,
245 cpu->power_current * cpu->power_scale);
246 } else if (event_type == cpu->state_event) {
248 cpu->state_current = SURF_CPU_ON;
250 lmm_constraint_t cnst = cpu->constraint;
251 lmm_variable_t var = NULL;
252 lmm_element_t elem = NULL;
254 cpu->state_current = SURF_CPU_OFF;
256 while((var = lmm_get_var_from_cnst(cpu_maxmin_system,cnst,&elem))) {
257 surf_action_t action = lmm_variable_id(var) ;
259 if(surf_action_get_state(action)==SURF_ACTION_RUNNING ||
260 surf_action_get_state(action)==SURF_ACTION_READY) {
261 action->finish = date;
262 action_change_state( action, SURF_ACTION_FAILED);
267 CRITICAL0("Unknown event ! \n");
274 static surf_action_t execute(void *cpu, double size)
276 surf_action_cpu_Cas01_t action = NULL;
277 cpu_Cas01_t CPU = cpu;
279 XBT_IN2("(%s,%g)", CPU->name, size);
280 action = xbt_new0(s_surf_action_cpu_Cas01_t, 1);
282 action->generic_action.using = 1;
283 action->generic_action.cost = size;
284 action->generic_action.remains = size;
285 action->generic_action.priority = 1.0;
286 action->generic_action.max_duration = NO_MAX_DURATION;
287 action->generic_action.start = surf_get_clock();
288 action->generic_action.finish = -1.0;
289 action->generic_action.model_type =
290 (surf_model_t) surf_cpu_model;
291 action->suspended = 0; /* Should be useless because of the
292 calloc but it seems to help valgrind... */
294 if (CPU->state_current == SURF_CPU_ON)
295 action->generic_action.state_set =
296 surf_cpu_model->common_public->states.running_action_set;
298 action->generic_action.state_set =
299 surf_cpu_model->common_public->states.failed_action_set;
300 /* Insert at the head by default. This convention is used to speed
301 up update_resource_state */
302 xbt_swag_insert_at_head(action, action->generic_action.state_set);
304 action->variable = lmm_variable_new(cpu_maxmin_system, action,
305 action->generic_action.priority,
307 lmm_expand(cpu_maxmin_system, CPU->constraint, action->variable, 1.0);
309 return (surf_action_t) action;
312 static surf_action_t action_sleep(void *cpu, double duration)
314 surf_action_cpu_Cas01_t action = NULL;
315 xbt_swag_t action_set = NULL;
317 duration=MAX(duration,MAXMIN_PRECISION);
319 XBT_IN2("(%s,%g)", ((cpu_Cas01_t) cpu)->name, duration);
320 action = (surf_action_cpu_Cas01_t) execute(cpu, 1.0);
321 action->generic_action.max_duration = duration;
322 action->suspended = 2;
323 if(duration == NO_MAX_DURATION) {
324 /* Move to the *end* of the corresponding action set. This convention
325 is used to speed up update_resource_state */
326 action_set = action->generic_action.state_set;
327 xbt_swag_remove(action, action_set);
328 xbt_swag_insert(action, action_set);
331 lmm_update_variable_weight(cpu_maxmin_system, action->variable, 0.0);
333 return (surf_action_t) action;
336 static void action_suspend(surf_action_t action)
338 XBT_IN1("(%p)", action);
339 if (((surf_action_cpu_Cas01_t) action)->suspended != 2) {
340 lmm_update_variable_weight(cpu_maxmin_system,
341 ((surf_action_cpu_Cas01_t) action)->
343 ((surf_action_cpu_Cas01_t) action)->suspended = 1;
348 static void action_resume(surf_action_t action)
350 XBT_IN1("(%p)", action);
351 if (((surf_action_cpu_Cas01_t) action)->suspended != 2) {
352 lmm_update_variable_weight(cpu_maxmin_system,
353 ((surf_action_cpu_Cas01_t) action)->
354 variable, action->priority);
355 ((surf_action_cpu_Cas01_t) action)->suspended = 0;
360 static int action_is_suspended(surf_action_t action)
362 return (((surf_action_cpu_Cas01_t) action)->suspended == 1);
365 static void action_set_max_duration(surf_action_t action, double duration)
367 XBT_IN2("(%p,%g)", action, duration);
368 action->max_duration = duration;
372 static void action_set_priority(surf_action_t action, double priority)
374 XBT_IN2("(%p,%g)", action, priority);
375 action->priority = priority;
376 lmm_update_variable_weight(cpu_maxmin_system, ((surf_action_cpu_Cas01_t) action)->variable, priority);
381 static e_surf_cpu_state_t get_state(void *cpu)
383 return ((cpu_Cas01_t) cpu)->state_current;
386 static double get_speed(void *cpu, double load)
388 return load * (((cpu_Cas01_t) cpu)->power_scale);
391 static double get_available_speed(void *cpu)
393 /* number between 0 and 1 */
394 return ((cpu_Cas01_t) cpu)->power_current;
397 static xbt_dict_t get_properties(void *cpu)
399 return ((cpu_Cas01_t) cpu)->properties;
402 static void finalize(void)
404 xbt_dict_free(&cpu_set);
405 xbt_swag_free(surf_cpu_model->common_public->states.ready_action_set);
406 xbt_swag_free(surf_cpu_model->common_public->states.
408 xbt_swag_free(surf_cpu_model->common_public->states.
410 xbt_swag_free(surf_cpu_model->common_public->states.done_action_set);
411 free(surf_cpu_model->common_public);
412 free(surf_cpu_model->common_private);
413 free(surf_cpu_model->extension_public);
415 free(surf_cpu_model);
416 surf_cpu_model = NULL;
419 static void surf_cpu_model_init_internal(void)
421 s_surf_action_t action;
423 surf_cpu_model = xbt_new0(s_surf_cpu_model_t, 1);
425 surf_cpu_model->common_private =
426 xbt_new0(s_surf_model_private_t, 1);
427 surf_cpu_model->common_public = xbt_new0(s_surf_model_public_t, 1);
429 surf_cpu_model->extension_public =
430 xbt_new0(s_surf_cpu_model_extension_public_t, 1);
432 surf_cpu_model->common_public->states.ready_action_set =
433 xbt_swag_new(xbt_swag_offset(action, state_hookup));
434 surf_cpu_model->common_public->states.running_action_set =
435 xbt_swag_new(xbt_swag_offset(action, state_hookup));
436 surf_cpu_model->common_public->states.failed_action_set =
437 xbt_swag_new(xbt_swag_offset(action, state_hookup));
438 surf_cpu_model->common_public->states.done_action_set =
439 xbt_swag_new(xbt_swag_offset(action, state_hookup));
441 surf_cpu_model->common_public->name_service = name_service;
442 surf_cpu_model->common_public->get_resource_name = get_resource_name;
443 surf_cpu_model->common_public->action_get_state =
444 surf_action_get_state;
445 surf_cpu_model->common_public->action_get_start_time =
446 surf_action_get_start_time;
447 surf_cpu_model->common_public->action_get_finish_time =
448 surf_action_get_finish_time;
449 surf_cpu_model->common_public->action_free = action_free;
450 surf_cpu_model->common_public->action_use = action_use;
451 surf_cpu_model->common_public->action_cancel = action_cancel;
452 surf_cpu_model->common_public->action_recycle = action_recycle;
453 surf_cpu_model->common_public->action_change_state =
455 surf_cpu_model->common_public->action_set_data = surf_action_set_data;
456 surf_cpu_model->common_public->name = "CPU";
458 surf_cpu_model->common_private->resource_used = resource_used;
459 surf_cpu_model->common_private->share_resources = share_resources;
460 surf_cpu_model->common_private->update_actions_state =
461 update_actions_state;
462 surf_cpu_model->common_private->update_resource_state =
463 update_resource_state;
464 surf_cpu_model->common_private->finalize = finalize;
466 surf_cpu_model->common_public->suspend = action_suspend;
467 surf_cpu_model->common_public->resume = action_resume;
468 surf_cpu_model->common_public->is_suspended = action_is_suspended;
469 surf_cpu_model->common_public->set_max_duration =
470 action_set_max_duration;
471 surf_cpu_model->common_public->set_priority = action_set_priority;
472 surf_cpu_model->extension_public->execute = execute;
473 surf_cpu_model->extension_public->sleep = action_sleep;
475 surf_cpu_model->extension_public->get_state = get_state;
476 surf_cpu_model->extension_public->get_speed = get_speed;
477 surf_cpu_model->extension_public->get_available_speed =
479 /*manage the properties of the cpu*/
480 surf_cpu_model->common_public->get_properties = get_properties;
482 if(!cpu_set) cpu_set = xbt_dict_new();
483 if (!cpu_maxmin_system) cpu_maxmin_system = lmm_system_new();
486 /*********************************************************************/
487 /* Basic sharing model for CPU: that is where all this started... ;) */
488 /*********************************************************************/
489 /* @InProceedings{casanova01simgrid, */
490 /* author = "H. Casanova", */
491 /* booktitle = "Proceedings of the IEEE Symposium on Cluster Computing */
492 /* and the Grid (CCGrid'01)", */
493 /* publisher = "IEEE Computer Society", */
494 /* title = "Simgrid: {A} Toolkit for the Simulation of Application */
498 /* note = "Available at */
499 /* \url{http://grail.sdsc.edu/papers/simgrid_ccgrid01.ps.gz}." */
501 void surf_cpu_model_init_Cas01(const char *filename)
505 surf_cpu_model_init_internal();
506 define_callbacks(filename);
507 xbt_dynar_push(model_list, &surf_cpu_model);