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_model_t surf_cpu_model = NULL;
14 lmm_system_t cpu_maxmin_system = NULL;
17 static xbt_swag_t running_action_set_that_does_not_need_being_checked = NULL;
19 static void cpu_free(void *cpu)
21 free(((cpu_Cas01_t) cpu)->name);
22 xbt_dict_free(&(((cpu_Cas01_t) cpu)->properties));
26 static cpu_Cas01_t cpu_new(char *name, double power_scale,
28 tmgr_trace_t power_trace,
29 e_surf_cpu_state_t state_initial,
30 tmgr_trace_t state_trace,
31 xbt_dict_t cpu_properties)
33 cpu_Cas01_t cpu = xbt_new0(s_cpu_Cas01_t, 1);
34 xbt_assert1(!surf_model_resource_by_name(surf_cpu_model, name),
35 "Host '%s' declared several times in the platform file", name);
36 cpu->model = surf_cpu_model;
38 cpu->power_scale = power_scale;
39 xbt_assert0(cpu->power_scale > 0, "Power has to be >0");
40 cpu->power_current = power_initial;
43 tmgr_history_add_trace(history, power_trace, 0.0, 0, cpu);
45 cpu->state_current = state_initial;
48 tmgr_history_add_trace(history, state_trace, 0.0, 0, cpu);
51 lmm_constraint_new(cpu_maxmin_system, cpu,
52 cpu->power_current * cpu->power_scale);
54 /*add the property set */
55 cpu->properties = cpu_properties;
57 current_property_set = cpu_properties;
59 xbt_dict_set(surf_model_resource_set(surf_cpu_model), name, cpu, cpu_free);
65 static void parse_cpu_init(void)
67 double power_scale = 0.0;
68 double power_initial = 0.0;
69 tmgr_trace_t power_trace = NULL;
70 e_surf_cpu_state_t state_initial = SURF_CPU_OFF;
71 tmgr_trace_t state_trace = NULL;
73 power_scale = get_cpu_power(A_surfxml_host_power);
74 surf_parse_get_double(&power_initial, A_surfxml_host_availability);
75 surf_parse_get_trace(&power_trace, A_surfxml_host_availability_file);
77 xbt_assert0((A_surfxml_host_state == A_surfxml_host_state_ON) ||
78 (A_surfxml_host_state == A_surfxml_host_state_OFF),
80 if (A_surfxml_host_state == A_surfxml_host_state_ON)
81 state_initial = SURF_CPU_ON;
82 if (A_surfxml_host_state == A_surfxml_host_state_OFF)
83 state_initial = SURF_CPU_OFF;
84 surf_parse_get_trace(&state_trace, A_surfxml_host_state_file);
86 current_property_set = xbt_dict_new();
87 cpu_new(xbt_strdup(A_surfxml_host_id), power_scale, power_initial,
88 power_trace, state_initial, state_trace, current_property_set);
92 static void add_traces_cpu(void)
94 xbt_dict_cursor_t cursor = NULL;
95 char *trace_name, *elm;
97 static int called = 0;
104 /* connect all traces relative to hosts */
105 xbt_dict_foreach(trace_connect_list_host_avail, cursor, trace_name, elm) {
106 tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
107 cpu_Cas01_t host = surf_model_resource_by_name(surf_cpu_model, elm);
109 xbt_assert1(host, "Host %s undefined", elm);
110 xbt_assert1(trace, "Trace %s undefined", trace_name);
112 host->state_event = tmgr_history_add_trace(history, trace, 0.0, 0, host);
115 xbt_dict_foreach(trace_connect_list_power, cursor, trace_name, elm) {
116 tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
117 cpu_Cas01_t host = surf_model_resource_by_name(surf_cpu_model, elm);
119 xbt_assert1(host, "Host %s undefined", elm);
120 xbt_assert1(trace, "Trace %s undefined", trace_name);
122 host->power_event = tmgr_history_add_trace(history, trace, 0.0, 0, host);
126 static void define_callbacks(const char *file)
128 surf_parse_reset_parser();
129 surfxml_add_callback(STag_surfxml_host_cb_list, parse_cpu_init);
132 static const char *get_resource_name(void *resource_id)
134 return ((cpu_Cas01_t) resource_id)->name;
137 static int resource_used(void *resource_id)
139 return lmm_constraint_used(cpu_maxmin_system,
140 ((cpu_Cas01_t) resource_id)->constraint);
143 static int action_free(surf_action_t action)
146 if (!action->refcount) {
147 xbt_swag_remove(action, action->state_set);
148 if (((surf_action_cpu_Cas01_t) action)->variable)
149 lmm_variable_free(cpu_maxmin_system,
150 ((surf_action_cpu_Cas01_t) action)->variable);
157 static void action_use(surf_action_t action)
162 static void action_cancel(surf_action_t action)
164 surf_action_change_state(action, SURF_ACTION_FAILED);
168 static void action_change_state(surf_action_t action,
169 e_surf_action_state_t state)
171 /* if((state==SURF_ACTION_DONE) || (state==SURF_ACTION_FAILED)) */
172 /* if(((surf_action_cpu_Cas01_t)action)->variable) { */
173 /* lmm_variable_disable(cpu_maxmin_system, ((surf_action_cpu_Cas01_t)action)->variable); */
174 /* ((surf_action_cpu_Cas01_t)action)->variable = NULL; */
177 surf_action_change_state(action, state);
181 static double share_resources(double now)
183 s_surf_action_cpu_Cas01_t action;
184 return generic_maxmin_share_resources(surf_cpu_model->states.
186 xbt_swag_offset(action, variable),
187 cpu_maxmin_system, lmm_solve);
190 static void update_actions_state(double now, double delta)
192 surf_action_cpu_Cas01_t action = NULL;
193 surf_action_cpu_Cas01_t next_action = NULL;
194 xbt_swag_t running_actions = surf_cpu_model->states.running_action_set;
196 xbt_swag_t failed_actions =
197 surf_cpu_model->states.failed_action_set;
200 xbt_swag_foreach_safe(action, next_action, running_actions) {
201 double_update(&(action->generic_action.remains),
202 lmm_variable_getvalue(action->variable) * delta);
203 if (action->generic_action.max_duration != NO_MAX_DURATION)
204 double_update(&(action->generic_action.max_duration), delta);
205 if ((action->generic_action.remains <= 0) &&
206 (lmm_get_variable_weight(action->variable) > 0)) {
207 action->generic_action.finish = surf_get_clock();
208 action_change_state((surf_action_t) action, SURF_ACTION_DONE);
209 } else if ((action->generic_action.max_duration != NO_MAX_DURATION) &&
210 (action->generic_action.max_duration <= 0)) {
211 action->generic_action.finish = surf_get_clock();
212 action_change_state((surf_action_t) action, SURF_ACTION_DONE);
219 static void update_resource_state(void *id,
220 tmgr_trace_event_t event_type,
221 double value, double date)
223 cpu_Cas01_t cpu = id;
225 if (event_type == cpu->power_event) {
226 cpu->power_current = value;
227 lmm_update_constraint_bound(cpu_maxmin_system, cpu->constraint,
228 cpu->power_current * cpu->power_scale);
229 } else if (event_type == cpu->state_event) {
231 cpu->state_current = SURF_CPU_ON;
233 lmm_constraint_t cnst = cpu->constraint;
234 lmm_variable_t var = NULL;
235 lmm_element_t elem = NULL;
237 cpu->state_current = SURF_CPU_OFF;
239 while ((var = lmm_get_var_from_cnst(cpu_maxmin_system, cnst, &elem))) {
240 surf_action_t action = lmm_variable_id(var);
242 if (surf_action_get_state(action) == SURF_ACTION_RUNNING ||
243 surf_action_get_state(action) == SURF_ACTION_READY ||
244 surf_action_get_state(action) == SURF_ACTION_NOT_IN_THE_SYSTEM) {
245 action->finish = date;
246 action_change_state(action, SURF_ACTION_FAILED);
251 CRITICAL0("Unknown event ! \n");
258 static surf_action_t execute(void *cpu, double size)
260 surf_action_cpu_Cas01_t action = NULL;
261 cpu_Cas01_t CPU = cpu;
263 XBT_IN2("(%s,%g)", CPU->name, size);
264 action = xbt_new0(s_surf_action_cpu_Cas01_t, 1);
266 action->generic_action.refcount = 1;
267 action->generic_action.cost = size;
268 action->generic_action.remains = size;
269 action->generic_action.priority = 1.0;
270 action->generic_action.max_duration = NO_MAX_DURATION;
271 action->generic_action.start = surf_get_clock();
272 action->generic_action.finish = -1.0;
273 action->generic_action.model_type = surf_cpu_model;
274 action->suspended = 0; /* Should be useless because of the
275 calloc but it seems to help valgrind... */
277 if (CPU->state_current == SURF_CPU_ON)
278 action->generic_action.state_set =
279 surf_cpu_model->states.running_action_set;
281 action->generic_action.state_set =
282 surf_cpu_model->states.failed_action_set;
284 xbt_swag_insert(action, action->generic_action.state_set);
286 action->variable = lmm_variable_new(cpu_maxmin_system, action,
287 action->generic_action.priority,
289 lmm_expand(cpu_maxmin_system, CPU->constraint, action->variable, 1.0);
291 return (surf_action_t) action;
294 static surf_action_t action_sleep(void *cpu, double duration)
296 surf_action_cpu_Cas01_t action = NULL;
299 duration = MAX(duration, MAXMIN_PRECISION);
301 XBT_IN2("(%s,%g)", ((cpu_Cas01_t) cpu)->name, duration);
302 action = (surf_action_cpu_Cas01_t) execute(cpu, 1.0);
303 action->generic_action.max_duration = duration;
304 action->suspended = 2;
305 if (duration == NO_MAX_DURATION) {
306 /* Move to the *end* of the corresponding action set. This convention
307 is used to speed up update_resource_state */
308 xbt_swag_remove(action, ((surf_action_t) action)->state_set);
309 ((surf_action_t) action)->state_set =
310 running_action_set_that_does_not_need_being_checked;
311 xbt_swag_insert(action, ((surf_action_t) action)->state_set);
314 lmm_update_variable_weight(cpu_maxmin_system, action->variable, 0.0);
316 return (surf_action_t) action;
319 static void action_suspend(surf_action_t action)
321 XBT_IN1("(%p)", action);
322 if (((surf_action_cpu_Cas01_t) action)->suspended != 2) {
323 lmm_update_variable_weight(cpu_maxmin_system,
324 ((surf_action_cpu_Cas01_t) action)->variable,
326 ((surf_action_cpu_Cas01_t) action)->suspended = 1;
331 static void action_resume(surf_action_t action)
333 XBT_IN1("(%p)", action);
334 if (((surf_action_cpu_Cas01_t) action)->suspended != 2) {
335 lmm_update_variable_weight(cpu_maxmin_system,
336 ((surf_action_cpu_Cas01_t) action)->variable,
338 ((surf_action_cpu_Cas01_t) action)->suspended = 0;
343 static int action_is_suspended(surf_action_t action)
345 return (((surf_action_cpu_Cas01_t) action)->suspended == 1);
348 static void action_set_max_duration(surf_action_t action, double duration)
350 XBT_IN2("(%p,%g)", action, duration);
351 action->max_duration = duration;
355 static void action_set_priority(surf_action_t action, double priority)
357 XBT_IN2("(%p,%g)", action, priority);
358 action->priority = priority;
359 lmm_update_variable_weight(cpu_maxmin_system,
360 ((surf_action_cpu_Cas01_t) action)->variable,
366 static e_surf_cpu_state_t get_state(void *cpu)
368 return ((cpu_Cas01_t) cpu)->state_current;
371 static double get_speed(void *cpu, double load)
373 return load * (((cpu_Cas01_t) cpu)->power_scale);
376 static double get_available_speed(void *cpu)
378 /* number between 0 and 1 */
379 return ((cpu_Cas01_t) cpu)->power_current;
382 static xbt_dict_t get_properties(void *cpu)
384 return ((cpu_Cas01_t) cpu)->properties;
387 static void finalize(void)
389 lmm_system_free(cpu_maxmin_system);
390 cpu_maxmin_system = NULL;
392 surf_model_exit(surf_cpu_model);
393 surf_cpu_model = NULL;
395 xbt_swag_free(running_action_set_that_does_not_need_being_checked);
396 running_action_set_that_does_not_need_being_checked = NULL;
399 static void surf_cpu_model_init_internal(void)
401 s_surf_action_t action;
403 surf_cpu_model = surf_model_init();
405 running_action_set_that_does_not_need_being_checked =
406 xbt_swag_new(xbt_swag_offset(action, state_hookup));
408 surf_cpu_model->get_resource_name = get_resource_name;
409 surf_cpu_model->action_get_state = surf_action_get_state;
410 surf_cpu_model->action_get_start_time = surf_action_get_start_time;
411 surf_cpu_model->action_get_finish_time = surf_action_get_finish_time;
412 surf_cpu_model->action_free = action_free;
413 surf_cpu_model->action_use = action_use;
414 surf_cpu_model->action_cancel = action_cancel;
415 surf_cpu_model->action_change_state = action_change_state;
416 surf_cpu_model->action_set_data = surf_action_set_data;
417 surf_cpu_model->name = "CPU";
419 surf_cpu_model->model_private->resource_used = resource_used;
420 surf_cpu_model->model_private->share_resources = share_resources;
421 surf_cpu_model->model_private->update_actions_state = update_actions_state;
422 surf_cpu_model->model_private->update_resource_state =
423 update_resource_state;
424 surf_cpu_model->model_private->finalize = finalize;
426 surf_cpu_model->suspend = action_suspend;
427 surf_cpu_model->resume = action_resume;
428 surf_cpu_model->is_suspended = action_is_suspended;
429 surf_cpu_model->set_max_duration = action_set_max_duration;
430 surf_cpu_model->set_priority = action_set_priority;
431 surf_cpu_model->extension.cpu.execute = execute;
432 surf_cpu_model->extension.cpu.sleep = action_sleep;
434 surf_cpu_model->extension.cpu.get_state = get_state;
435 surf_cpu_model->extension.cpu.get_speed = get_speed;
436 surf_cpu_model->extension.cpu.get_available_speed = get_available_speed;
437 /*manage the properties of the cpu */
438 surf_cpu_model->get_properties = get_properties;
440 if (!cpu_maxmin_system)
441 cpu_maxmin_system = lmm_system_new();
444 /*********************************************************************/
445 /* Basic sharing model for CPU: that is where all this started... ;) */
446 /*********************************************************************/
447 /* @InProceedings{casanova01simgrid, */
448 /* author = "H. Casanova", */
449 /* booktitle = "Proceedings of the IEEE Symposium on Cluster Computing */
450 /* and the Grid (CCGrid'01)", */
451 /* publisher = "IEEE Computer Society", */
452 /* title = "Simgrid: {A} Toolkit for the Simulation of Application */
456 /* note = "Available at */
457 /* \url{http://grail.sdsc.edu/papers/simgrid_ccgrid01.ps.gz}." */
459 void surf_cpu_model_init_Cas01(const char *filename)
463 surf_cpu_model_init_internal();
464 define_callbacks(filename);
465 xbt_dynar_push(model_list, &surf_cpu_model);