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 surf_parse_get_double(&power_scale, 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 define_callbacks(const char *file)
93 surf_parse_reset_parser();
94 surfxml_add_callback(STag_surfxml_host_cb_list, parse_cpu_init);
95 surfxml_add_callback(STag_surfxml_prop_cb_list, parse_properties);
98 static void *name_service(const char *name)
100 return xbt_dict_get_or_null(cpu_set, name);
103 static const char *get_resource_name(void *resource_id)
105 return ((cpu_Cas01_t) resource_id)->name;
108 static int resource_used(void *resource_id)
110 return lmm_constraint_used(cpu_maxmin_system,
111 ((cpu_Cas01_t) resource_id)->constraint);
114 static int action_free(surf_action_t action)
117 if (!action->using) {
118 xbt_swag_remove(action, action->state_set);
119 if (((surf_action_cpu_Cas01_t) action)->variable)
120 lmm_variable_free(cpu_maxmin_system,
121 ((surf_action_cpu_Cas01_t) action)->variable);
128 static void action_use(surf_action_t action)
133 static void action_cancel(surf_action_t action)
135 surf_action_change_state(action, SURF_ACTION_FAILED);
139 static void action_recycle(surf_action_t action)
144 static void action_change_state(surf_action_t action,
145 e_surf_action_state_t state)
147 /* if((state==SURF_ACTION_DONE) || (state==SURF_ACTION_FAILED)) */
148 /* if(((surf_action_cpu_Cas01_t)action)->variable) { */
149 /* lmm_variable_disable(cpu_maxmin_system, ((surf_action_cpu_Cas01_t)action)->variable); */
150 /* ((surf_action_cpu_Cas01_t)action)->variable = NULL; */
153 surf_action_change_state(action, state);
157 static double share_resources(double now)
159 s_surf_action_cpu_Cas01_t action;
160 return generic_maxmin_share_resources(surf_cpu_model->common_public->
161 states.running_action_set,
162 xbt_swag_offset(action, variable),
163 cpu_maxmin_system, lmm_solve);
166 static void update_actions_state(double now, double delta)
168 surf_action_cpu_Cas01_t action = NULL;
169 surf_action_cpu_Cas01_t next_action = NULL;
170 xbt_swag_t running_actions =
171 surf_cpu_model->common_public->states.running_action_set;
173 xbt_swag_t failed_actions =
174 surf_cpu_model->common_public->states.failed_action_set;
177 xbt_swag_foreach_safe(action, next_action, running_actions) {
178 double_update(&(action->generic_action.remains),
179 lmm_variable_getvalue(action->variable) * delta);
180 if (action->generic_action.max_duration != NO_MAX_DURATION)
181 double_update(&(action->generic_action.max_duration), delta);
182 if ((action->generic_action.remains <= 0) &&
183 (lmm_get_variable_weight(action->variable) > 0)) {
184 action->generic_action.finish = surf_get_clock();
185 action_change_state((surf_action_t) action, SURF_ACTION_DONE);
186 } else if ((action->generic_action.max_duration != NO_MAX_DURATION) &&
187 (action->generic_action.max_duration <= 0)) {
188 action->generic_action.finish = surf_get_clock();
189 action_change_state((surf_action_t) action, SURF_ACTION_DONE);
190 } else { /* Need to check that none of the model has failed */
191 lmm_constraint_t cnst = NULL;
193 cpu_Cas01_t cpu = NULL;
196 lmm_get_cnst_from_var(cpu_maxmin_system, action->variable,
198 cpu = lmm_constraint_id(cnst);
199 if (cpu->state_current == SURF_CPU_OFF) {
200 action->generic_action.finish = surf_get_clock();
201 action_change_state((surf_action_t) action, SURF_ACTION_FAILED);
211 static void update_resource_state(void *id,
212 tmgr_trace_event_t event_type,
215 cpu_Cas01_t cpu = id;
217 if (event_type == cpu->power_event) {
218 cpu->power_current = value;
219 lmm_update_constraint_bound(cpu_maxmin_system, cpu->constraint,
220 cpu->power_current * cpu->power_scale);
221 } else if (event_type == cpu->state_event) {
223 cpu->state_current = SURF_CPU_ON;
225 cpu->state_current = SURF_CPU_OFF;
227 CRITICAL0("Unknown event ! \n");
234 static surf_action_t execute(void *cpu, double size)
236 surf_action_cpu_Cas01_t action = NULL;
237 cpu_Cas01_t CPU = cpu;
239 XBT_IN2("(%s,%g)", CPU->name, size);
240 action = xbt_new0(s_surf_action_cpu_Cas01_t, 1);
242 action->generic_action.using = 1;
243 action->generic_action.cost = size;
244 action->generic_action.remains = size;
245 action->generic_action.priority = 1.0;
246 action->generic_action.max_duration = NO_MAX_DURATION;
247 action->generic_action.start = surf_get_clock();
248 action->generic_action.finish = -1.0;
249 action->generic_action.model_type =
250 (surf_model_t) surf_cpu_model;
251 action->suspended = 0; /* Should be useless because of the
252 calloc but it seems to help valgrind... */
254 if (CPU->state_current == SURF_CPU_ON)
255 action->generic_action.state_set =
256 surf_cpu_model->common_public->states.running_action_set;
258 action->generic_action.state_set =
259 surf_cpu_model->common_public->states.failed_action_set;
260 xbt_swag_insert(action, action->generic_action.state_set);
262 action->variable = lmm_variable_new(cpu_maxmin_system, action,
263 action->generic_action.priority,
265 lmm_expand(cpu_maxmin_system, CPU->constraint, action->variable, 1.0);
267 return (surf_action_t) action;
270 static surf_action_t action_sleep(void *cpu, double duration)
272 surf_action_cpu_Cas01_t action = NULL;
274 XBT_IN2("(%s,%g)", ((cpu_Cas01_t) cpu)->name, duration);
275 action = (surf_action_cpu_Cas01_t) execute(cpu, 1.0);
276 action->generic_action.max_duration = duration;
277 action->suspended = 2;
278 lmm_update_variable_weight(cpu_maxmin_system, action->variable, 0.0);
280 return (surf_action_t) action;
283 static void action_suspend(surf_action_t action)
285 XBT_IN1("(%p)", action);
286 if (((surf_action_cpu_Cas01_t) action)->suspended != 2) {
287 lmm_update_variable_weight(cpu_maxmin_system,
288 ((surf_action_cpu_Cas01_t) action)->
290 ((surf_action_cpu_Cas01_t) action)->suspended = 1;
295 static void action_resume(surf_action_t action)
297 XBT_IN1("(%p)", action);
298 if (((surf_action_cpu_Cas01_t) action)->suspended != 2) {
299 lmm_update_variable_weight(cpu_maxmin_system,
300 ((surf_action_cpu_Cas01_t) action)->
301 variable, action->priority);
302 ((surf_action_cpu_Cas01_t) action)->suspended = 0;
307 static int action_is_suspended(surf_action_t action)
309 return (((surf_action_cpu_Cas01_t) action)->suspended == 1);
312 static void action_set_max_duration(surf_action_t action, double duration)
314 XBT_IN2("(%p,%g)", action, duration);
315 action->max_duration = duration;
319 static void action_set_priority(surf_action_t action, double priority)
321 XBT_IN2("(%p,%g)", action, priority);
322 action->priority = priority;
323 lmm_update_variable_weight(cpu_maxmin_system, ((surf_action_cpu_Cas01_t) action)->variable, priority);
328 static e_surf_cpu_state_t get_state(void *cpu)
330 return ((cpu_Cas01_t) cpu)->state_current;
333 static double get_speed(void *cpu, double load)
335 return load * (((cpu_Cas01_t) cpu)->power_scale);
338 static double get_available_speed(void *cpu)
340 /* number between 0 and 1 */
341 return ((cpu_Cas01_t) cpu)->power_current;
344 static xbt_dict_t get_properties(void *cpu)
346 return ((cpu_Cas01_t) cpu)->properties;
349 static void finalize(void)
351 xbt_dict_free(&cpu_set);
352 xbt_swag_free(surf_cpu_model->common_public->states.ready_action_set);
353 xbt_swag_free(surf_cpu_model->common_public->states.
355 xbt_swag_free(surf_cpu_model->common_public->states.
357 xbt_swag_free(surf_cpu_model->common_public->states.done_action_set);
358 free(surf_cpu_model->common_public);
359 free(surf_cpu_model->common_private);
360 free(surf_cpu_model->extension_public);
362 free(surf_cpu_model);
363 surf_cpu_model = NULL;
366 static void surf_cpu_model_init_internal(void)
368 s_surf_action_t action;
370 surf_cpu_model = xbt_new0(s_surf_cpu_model_t, 1);
372 surf_cpu_model->common_private =
373 xbt_new0(s_surf_model_private_t, 1);
374 surf_cpu_model->common_public = xbt_new0(s_surf_model_public_t, 1);
376 surf_cpu_model->extension_public =
377 xbt_new0(s_surf_cpu_model_extension_public_t, 1);
379 surf_cpu_model->common_public->states.ready_action_set =
380 xbt_swag_new(xbt_swag_offset(action, state_hookup));
381 surf_cpu_model->common_public->states.running_action_set =
382 xbt_swag_new(xbt_swag_offset(action, state_hookup));
383 surf_cpu_model->common_public->states.failed_action_set =
384 xbt_swag_new(xbt_swag_offset(action, state_hookup));
385 surf_cpu_model->common_public->states.done_action_set =
386 xbt_swag_new(xbt_swag_offset(action, state_hookup));
388 surf_cpu_model->common_public->name_service = name_service;
389 surf_cpu_model->common_public->get_resource_name = get_resource_name;
390 surf_cpu_model->common_public->action_get_state =
391 surf_action_get_state;
392 surf_cpu_model->common_public->action_get_start_time =
393 surf_action_get_start_time;
394 surf_cpu_model->common_public->action_get_finish_time =
395 surf_action_get_finish_time;
396 surf_cpu_model->common_public->action_free = action_free;
397 surf_cpu_model->common_public->action_use = action_use;
398 surf_cpu_model->common_public->action_cancel = action_cancel;
399 surf_cpu_model->common_public->action_recycle = action_recycle;
400 surf_cpu_model->common_public->action_change_state =
402 surf_cpu_model->common_public->action_set_data = surf_action_set_data;
403 surf_cpu_model->common_public->name = "CPU";
405 surf_cpu_model->common_private->resource_used = resource_used;
406 surf_cpu_model->common_private->share_resources = share_resources;
407 surf_cpu_model->common_private->update_actions_state =
408 update_actions_state;
409 surf_cpu_model->common_private->update_resource_state =
410 update_resource_state;
411 surf_cpu_model->common_private->finalize = finalize;
413 surf_cpu_model->common_public->suspend = action_suspend;
414 surf_cpu_model->common_public->resume = action_resume;
415 surf_cpu_model->common_public->is_suspended = action_is_suspended;
416 surf_cpu_model->common_public->set_max_duration =
417 action_set_max_duration;
418 surf_cpu_model->common_public->set_priority = action_set_priority;
419 surf_cpu_model->extension_public->execute = execute;
420 surf_cpu_model->extension_public->sleep = action_sleep;
422 surf_cpu_model->extension_public->get_state = get_state;
423 surf_cpu_model->extension_public->get_speed = get_speed;
424 surf_cpu_model->extension_public->get_available_speed =
426 /*manage the properties of the cpu*/
427 surf_cpu_model->common_public->get_properties = get_properties;
429 if(!cpu_set) cpu_set = xbt_dict_new();
430 if (!cpu_maxmin_system) cpu_maxmin_system = lmm_system_new();
433 /*********************************************************************/
434 /* Basic sharing model for CPU: that is where all this started... ;) */
435 /*********************************************************************/
436 /* @InProceedings{casanova01simgrid, */
437 /* author = "H. Casanova", */
438 /* booktitle = "Proceedings of the IEEE Symposium on Cluster Computing */
439 /* and the Grid (CCGrid'01)", */
440 /* publisher = "IEEE Computer Society", */
441 /* title = "Simgrid: {A} Toolkit for the Simulation of Application */
445 /* note = "Available at */
446 /* \url{http://grail.sdsc.edu/papers/simgrid_ccgrid01.ps.gz}." */
448 void surf_cpu_model_init_Cas01(const char *filename)
452 surf_cpu_model_init_internal();
453 define_callbacks(filename);
454 xbt_dynar_push(model_list, &surf_cpu_model);