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 int called = 0;
93 static void add_traces_cpu(void)
95 xbt_dynar_t trace_connect = NULL;
97 int connect_element, connect_kind;
98 char *value, *trace_id, *connector_id;
99 cpu_Cas01_t host = NULL;
105 /*for all trace connects parse them and update traces for hosts */
106 xbt_dynar_foreach (traces_connect_list, cpt, value) {
107 trace_connect = xbt_str_split_str(value, "##");
108 trace_id = xbt_dynar_get_as(trace_connect, 0, char*);
109 connect_element = atoi(xbt_dynar_get_as(trace_connect, 1, char*));
110 connect_kind = atoi(xbt_dynar_get_as(trace_connect, 2, char*));
111 connector_id = xbt_dynar_get_as(trace_connect, 3, char*);
113 xbt_assert1((trace = xbt_dict_get_or_null(traces_set_list, trace_id)), "Trace %s undefined", trace_id);
115 if (connect_element == A_surfxml_trace_c_connect_element_HOST) {
116 xbt_assert1((host = xbt_dict_get_or_null(cpu_set, connector_id)), "Host %s undefined", connector_id);
117 switch (connect_kind) {
118 case A_surfxml_trace_c_connect_kind_AVAILABILITY: host->state_event = tmgr_history_add_trace(history, trace, 0.0, 0, host); break;
119 case A_surfxml_trace_c_connect_kind_POWER: host->power_event = tmgr_history_add_trace(history, trace, 0.0, 0, host); break;
125 static void define_callbacks(const char *file)
127 surf_parse_reset_parser();
128 surfxml_add_callback(STag_surfxml_host_cb_list, parse_cpu_init);
129 surfxml_add_callback(STag_surfxml_prop_cb_list, parse_properties);
130 // surfxml_add_callback(ETag_surfxml_platform_cb_list, &add_traces_cpu);
131 surfxml_add_callback(STag_surfxml_random_cb_list, &init_randomness);
132 surfxml_add_callback(ETag_surfxml_random_cb_list, &add_randomness);
135 static void *name_service(const char *name)
137 return xbt_dict_get_or_null(cpu_set, name);
140 static const char *get_resource_name(void *resource_id)
142 return ((cpu_Cas01_t) resource_id)->name;
145 static int resource_used(void *resource_id)
147 return lmm_constraint_used(cpu_maxmin_system,
148 ((cpu_Cas01_t) resource_id)->constraint);
151 static int action_free(surf_action_t action)
154 if (!action->using) {
155 xbt_swag_remove(action, action->state_set);
156 if (((surf_action_cpu_Cas01_t) action)->variable)
157 lmm_variable_free(cpu_maxmin_system,
158 ((surf_action_cpu_Cas01_t) action)->variable);
165 static void action_use(surf_action_t action)
170 static void action_cancel(surf_action_t action)
172 surf_action_change_state(action, SURF_ACTION_FAILED);
176 static void action_recycle(surf_action_t action)
181 static void action_change_state(surf_action_t action,
182 e_surf_action_state_t state)
184 /* if((state==SURF_ACTION_DONE) || (state==SURF_ACTION_FAILED)) */
185 /* if(((surf_action_cpu_Cas01_t)action)->variable) { */
186 /* lmm_variable_disable(cpu_maxmin_system, ((surf_action_cpu_Cas01_t)action)->variable); */
187 /* ((surf_action_cpu_Cas01_t)action)->variable = NULL; */
190 surf_action_change_state(action, state);
194 static double share_resources(double now)
196 s_surf_action_cpu_Cas01_t action;
197 return generic_maxmin_share_resources(surf_cpu_model->common_public->
198 states.running_action_set,
199 xbt_swag_offset(action, variable),
200 cpu_maxmin_system, lmm_solve);
203 static void update_actions_state(double now, double delta)
205 surf_action_cpu_Cas01_t action = NULL;
206 surf_action_cpu_Cas01_t next_action = NULL;
207 xbt_swag_t running_actions =
208 surf_cpu_model->common_public->states.running_action_set;
210 xbt_swag_t failed_actions =
211 surf_cpu_model->common_public->states.failed_action_set;
214 xbt_swag_foreach_safe(action, next_action, running_actions) {
215 double_update(&(action->generic_action.remains),
216 lmm_variable_getvalue(action->variable) * delta);
217 if (action->generic_action.max_duration != NO_MAX_DURATION)
218 double_update(&(action->generic_action.max_duration), delta);
219 if ((action->generic_action.remains <= 0) &&
220 (lmm_get_variable_weight(action->variable) > 0)) {
221 action->generic_action.finish = surf_get_clock();
222 action_change_state((surf_action_t) action, SURF_ACTION_DONE);
223 } else if ((action->generic_action.max_duration != NO_MAX_DURATION) &&
224 (action->generic_action.max_duration <= 0)) {
225 action->generic_action.finish = surf_get_clock();
226 action_change_state((surf_action_t) action, SURF_ACTION_DONE);
227 } else { /* Need to check that none of the model has failed */
228 lmm_constraint_t cnst = NULL;
230 cpu_Cas01_t cpu = NULL;
233 lmm_get_cnst_from_var(cpu_maxmin_system, action->variable,
235 cpu = lmm_constraint_id(cnst);
236 if (cpu->state_current == SURF_CPU_OFF) {
237 action->generic_action.finish = surf_get_clock();
238 action_change_state((surf_action_t) action, SURF_ACTION_FAILED);
248 static void update_resource_state(void *id,
249 tmgr_trace_event_t event_type,
252 cpu_Cas01_t cpu = id;
254 if (event_type == cpu->power_event) {
255 cpu->power_current = value;
256 lmm_update_constraint_bound(cpu_maxmin_system, cpu->constraint,
257 cpu->power_current * cpu->power_scale);
258 } else if (event_type == cpu->state_event) {
260 cpu->state_current = SURF_CPU_ON;
262 cpu->state_current = SURF_CPU_OFF;
264 CRITICAL0("Unknown event ! \n");
271 static surf_action_t execute(void *cpu, double size)
273 surf_action_cpu_Cas01_t action = NULL;
274 cpu_Cas01_t CPU = cpu;
276 XBT_IN2("(%s,%g)", CPU->name, size);
277 action = xbt_new0(s_surf_action_cpu_Cas01_t, 1);
279 action->generic_action.using = 1;
280 action->generic_action.cost = size;
281 action->generic_action.remains = size;
282 action->generic_action.priority = 1.0;
283 action->generic_action.max_duration = NO_MAX_DURATION;
284 action->generic_action.start = surf_get_clock();
285 action->generic_action.finish = -1.0;
286 action->generic_action.model_type =
287 (surf_model_t) surf_cpu_model;
288 action->suspended = 0; /* Should be useless because of the
289 calloc but it seems to help valgrind... */
291 if (CPU->state_current == SURF_CPU_ON)
292 action->generic_action.state_set =
293 surf_cpu_model->common_public->states.running_action_set;
295 action->generic_action.state_set =
296 surf_cpu_model->common_public->states.failed_action_set;
297 xbt_swag_insert(action, action->generic_action.state_set);
299 action->variable = lmm_variable_new(cpu_maxmin_system, action,
300 action->generic_action.priority,
302 lmm_expand(cpu_maxmin_system, CPU->constraint, action->variable, 1.0);
304 return (surf_action_t) action;
307 static surf_action_t action_sleep(void *cpu, double duration)
309 surf_action_cpu_Cas01_t action = NULL;
311 XBT_IN2("(%s,%g)", ((cpu_Cas01_t) cpu)->name, duration);
312 action = (surf_action_cpu_Cas01_t) execute(cpu, 1.0);
313 action->generic_action.max_duration = duration;
314 action->suspended = 2;
315 lmm_update_variable_weight(cpu_maxmin_system, action->variable, 0.0);
317 return (surf_action_t) action;
320 static void action_suspend(surf_action_t action)
322 XBT_IN1("(%p)", action);
323 if (((surf_action_cpu_Cas01_t) action)->suspended != 2) {
324 lmm_update_variable_weight(cpu_maxmin_system,
325 ((surf_action_cpu_Cas01_t) action)->
327 ((surf_action_cpu_Cas01_t) action)->suspended = 1;
332 static void action_resume(surf_action_t action)
334 XBT_IN1("(%p)", action);
335 if (((surf_action_cpu_Cas01_t) action)->suspended != 2) {
336 lmm_update_variable_weight(cpu_maxmin_system,
337 ((surf_action_cpu_Cas01_t) action)->
338 variable, action->priority);
339 ((surf_action_cpu_Cas01_t) action)->suspended = 0;
344 static int action_is_suspended(surf_action_t action)
346 return (((surf_action_cpu_Cas01_t) action)->suspended == 1);
349 static void action_set_max_duration(surf_action_t action, double duration)
351 XBT_IN2("(%p,%g)", action, duration);
352 action->max_duration = duration;
356 static void action_set_priority(surf_action_t action, double priority)
358 XBT_IN2("(%p,%g)", action, priority);
359 action->priority = priority;
360 lmm_update_variable_weight(cpu_maxmin_system, ((surf_action_cpu_Cas01_t) action)->variable, priority);
365 static e_surf_cpu_state_t get_state(void *cpu)
367 return ((cpu_Cas01_t) cpu)->state_current;
370 static double get_speed(void *cpu, double load)
372 return load * (((cpu_Cas01_t) cpu)->power_scale);
375 static double get_available_speed(void *cpu)
377 /* number between 0 and 1 */
378 return ((cpu_Cas01_t) cpu)->power_current;
381 static xbt_dict_t get_properties(void *cpu)
383 return ((cpu_Cas01_t) cpu)->properties;
386 static void finalize(void)
388 xbt_dict_free(&cpu_set);
389 xbt_swag_free(surf_cpu_model->common_public->states.ready_action_set);
390 xbt_swag_free(surf_cpu_model->common_public->states.
392 xbt_swag_free(surf_cpu_model->common_public->states.
394 xbt_swag_free(surf_cpu_model->common_public->states.done_action_set);
395 free(surf_cpu_model->common_public);
396 free(surf_cpu_model->common_private);
397 free(surf_cpu_model->extension_public);
399 free(surf_cpu_model);
400 surf_cpu_model = NULL;
403 static void surf_cpu_model_init_internal(void)
405 s_surf_action_t action;
407 surf_cpu_model = xbt_new0(s_surf_cpu_model_t, 1);
409 surf_cpu_model->common_private =
410 xbt_new0(s_surf_model_private_t, 1);
411 surf_cpu_model->common_public = xbt_new0(s_surf_model_public_t, 1);
413 surf_cpu_model->extension_public =
414 xbt_new0(s_surf_cpu_model_extension_public_t, 1);
416 surf_cpu_model->common_public->states.ready_action_set =
417 xbt_swag_new(xbt_swag_offset(action, state_hookup));
418 surf_cpu_model->common_public->states.running_action_set =
419 xbt_swag_new(xbt_swag_offset(action, state_hookup));
420 surf_cpu_model->common_public->states.failed_action_set =
421 xbt_swag_new(xbt_swag_offset(action, state_hookup));
422 surf_cpu_model->common_public->states.done_action_set =
423 xbt_swag_new(xbt_swag_offset(action, state_hookup));
425 surf_cpu_model->common_public->name_service = name_service;
426 surf_cpu_model->common_public->get_resource_name = get_resource_name;
427 surf_cpu_model->common_public->action_get_state =
428 surf_action_get_state;
429 surf_cpu_model->common_public->action_get_start_time =
430 surf_action_get_start_time;
431 surf_cpu_model->common_public->action_get_finish_time =
432 surf_action_get_finish_time;
433 surf_cpu_model->common_public->action_free = action_free;
434 surf_cpu_model->common_public->action_use = action_use;
435 surf_cpu_model->common_public->action_cancel = action_cancel;
436 surf_cpu_model->common_public->action_recycle = action_recycle;
437 surf_cpu_model->common_public->action_change_state =
439 surf_cpu_model->common_public->action_set_data = surf_action_set_data;
440 surf_cpu_model->common_public->name = "CPU";
442 surf_cpu_model->common_private->resource_used = resource_used;
443 surf_cpu_model->common_private->share_resources = share_resources;
444 surf_cpu_model->common_private->update_actions_state =
445 update_actions_state;
446 surf_cpu_model->common_private->update_resource_state =
447 update_resource_state;
448 surf_cpu_model->common_private->finalize = finalize;
450 surf_cpu_model->common_public->suspend = action_suspend;
451 surf_cpu_model->common_public->resume = action_resume;
452 surf_cpu_model->common_public->is_suspended = action_is_suspended;
453 surf_cpu_model->common_public->set_max_duration =
454 action_set_max_duration;
455 surf_cpu_model->common_public->set_priority = action_set_priority;
456 surf_cpu_model->extension_public->execute = execute;
457 surf_cpu_model->extension_public->sleep = action_sleep;
459 surf_cpu_model->extension_public->get_state = get_state;
460 surf_cpu_model->extension_public->get_speed = get_speed;
461 surf_cpu_model->extension_public->get_available_speed =
463 /*manage the properties of the cpu*/
464 surf_cpu_model->common_public->get_properties = get_properties;
466 if(!cpu_set) cpu_set = xbt_dict_new();
467 if (!cpu_maxmin_system) cpu_maxmin_system = lmm_system_new();
470 /*********************************************************************/
471 /* Basic sharing model for CPU: that is where all this started... ;) */
472 /*********************************************************************/
473 /* @InProceedings{casanova01simgrid, */
474 /* author = "H. Casanova", */
475 /* booktitle = "Proceedings of the IEEE Symposium on Cluster Computing */
476 /* and the Grid (CCGrid'01)", */
477 /* publisher = "IEEE Computer Society", */
478 /* title = "Simgrid: {A} Toolkit for the Simulation of Application */
482 /* note = "Available at */
483 /* \url{http://grail.sdsc.edu/papers/simgrid_ccgrid01.ps.gz}." */
485 void surf_cpu_model_init_Cas01(const char *filename)
489 surf_cpu_model_init_internal();
490 define_callbacks(filename);
491 xbt_dynar_push(model_list, &surf_cpu_model);