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 xbt_dict_free(&(((cpu_Cas01_t) cpu)->properties));
22 surf_resource_free(cpu);
25 static cpu_Cas01_t cpu_new(char *name, double power_scale,
27 tmgr_trace_t power_trace,
28 e_surf_cpu_state_t state_initial,
29 tmgr_trace_t state_trace,
30 xbt_dict_t cpu_properties)
32 cpu_Cas01_t cpu = xbt_new0(s_cpu_Cas01_t, 1);
33 xbt_assert1(!surf_model_resource_by_name(surf_cpu_model, name),
34 "Host '%s' declared several times in the platform file", name);
35 cpu->generic_resource.model = surf_cpu_model;
36 cpu->generic_resource.name = name;
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(surf_model_resource_set(surf_cpu_model), 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,
87 power_trace, state_initial, state_trace, current_property_set);
91 static void add_traces_cpu(void)
93 xbt_dict_cursor_t cursor = NULL;
94 char *trace_name, *elm;
96 static int called = 0;
103 /* connect all traces relative to hosts */
104 xbt_dict_foreach(trace_connect_list_host_avail, cursor, trace_name, elm) {
105 tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
106 cpu_Cas01_t host = surf_model_resource_by_name(surf_cpu_model, elm);
108 xbt_assert1(host, "Host %s undefined", elm);
109 xbt_assert1(trace, "Trace %s undefined", trace_name);
111 host->state_event = tmgr_history_add_trace(history, trace, 0.0, 0, host);
114 xbt_dict_foreach(trace_connect_list_power, cursor, trace_name, elm) {
115 tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
116 cpu_Cas01_t host = surf_model_resource_by_name(surf_cpu_model, elm);
118 xbt_assert1(host, "Host %s undefined", elm);
119 xbt_assert1(trace, "Trace %s undefined", trace_name);
121 host->power_event = tmgr_history_add_trace(history, trace, 0.0, 0, host);
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);
131 static int resource_used(void *resource_id)
133 return lmm_constraint_used(cpu_maxmin_system,
134 ((cpu_Cas01_t) resource_id)->constraint);
137 static int action_free(surf_action_t action)
140 if (!action->refcount) {
141 xbt_swag_remove(action, action->state_set);
142 if (((surf_action_cpu_Cas01_t) action)->variable)
143 lmm_variable_free(cpu_maxmin_system,
144 ((surf_action_cpu_Cas01_t) action)->variable);
151 static void action_use(surf_action_t action)
156 static void action_cancel(surf_action_t action)
158 surf_action_change_state(action, SURF_ACTION_FAILED);
162 static void action_change_state(surf_action_t action,
163 e_surf_action_state_t state)
165 /* if((state==SURF_ACTION_DONE) || (state==SURF_ACTION_FAILED)) */
166 /* if(((surf_action_cpu_Cas01_t)action)->variable) { */
167 /* lmm_variable_disable(cpu_maxmin_system, ((surf_action_cpu_Cas01_t)action)->variable); */
168 /* ((surf_action_cpu_Cas01_t)action)->variable = NULL; */
171 surf_action_change_state(action, state);
175 static double share_resources(double now)
177 s_surf_action_cpu_Cas01_t action;
178 return generic_maxmin_share_resources(surf_cpu_model->states.
180 xbt_swag_offset(action, variable),
181 cpu_maxmin_system, lmm_solve);
184 static void update_actions_state(double now, double delta)
186 surf_action_cpu_Cas01_t action = NULL;
187 surf_action_cpu_Cas01_t next_action = NULL;
188 xbt_swag_t running_actions = surf_cpu_model->states.running_action_set;
190 xbt_swag_t failed_actions =
191 surf_cpu_model->states.failed_action_set;
194 xbt_swag_foreach_safe(action, next_action, running_actions) {
195 double_update(&(action->generic_action.remains),
196 lmm_variable_getvalue(action->variable) * delta);
197 if (action->generic_action.max_duration != NO_MAX_DURATION)
198 double_update(&(action->generic_action.max_duration), delta);
199 if ((action->generic_action.remains <= 0) &&
200 (lmm_get_variable_weight(action->variable) > 0)) {
201 action->generic_action.finish = surf_get_clock();
202 action_change_state((surf_action_t) action, SURF_ACTION_DONE);
203 } else if ((action->generic_action.max_duration != NO_MAX_DURATION) &&
204 (action->generic_action.max_duration <= 0)) {
205 action->generic_action.finish = surf_get_clock();
206 action_change_state((surf_action_t) action, SURF_ACTION_DONE);
213 static void update_resource_state(void *id,
214 tmgr_trace_event_t event_type,
215 double value, double date)
217 cpu_Cas01_t cpu = id;
219 if (event_type == cpu->power_event) {
220 cpu->power_current = value;
221 lmm_update_constraint_bound(cpu_maxmin_system, cpu->constraint,
222 cpu->power_current * cpu->power_scale);
223 } else if (event_type == cpu->state_event) {
225 cpu->state_current = SURF_CPU_ON;
227 lmm_constraint_t cnst = cpu->constraint;
228 lmm_variable_t var = NULL;
229 lmm_element_t elem = NULL;
231 cpu->state_current = SURF_CPU_OFF;
233 while ((var = lmm_get_var_from_cnst(cpu_maxmin_system, cnst, &elem))) {
234 surf_action_t action = lmm_variable_id(var);
236 if (surf_action_get_state(action) == SURF_ACTION_RUNNING ||
237 surf_action_get_state(action) == SURF_ACTION_READY ||
238 surf_action_get_state(action) == SURF_ACTION_NOT_IN_THE_SYSTEM) {
239 action->finish = date;
240 action_change_state(action, SURF_ACTION_FAILED);
245 CRITICAL0("Unknown event ! \n");
252 static surf_action_t execute(void *cpu, double size)
254 surf_action_cpu_Cas01_t action = NULL;
255 cpu_Cas01_t CPU = cpu;
257 XBT_IN2("(%s,%g)", CPU->name, size);
258 action = xbt_new0(s_surf_action_cpu_Cas01_t, 1);
260 action->generic_action.refcount = 1;
261 action->generic_action.cost = size;
262 action->generic_action.remains = size;
263 action->generic_action.priority = 1.0;
264 action->generic_action.max_duration = NO_MAX_DURATION;
265 action->generic_action.start = surf_get_clock();
266 action->generic_action.finish = -1.0;
267 action->generic_action.model_type = surf_cpu_model;
268 action->suspended = 0; /* Should be useless because of the
269 calloc but it seems to help valgrind... */
271 if (CPU->state_current == SURF_CPU_ON)
272 action->generic_action.state_set =
273 surf_cpu_model->states.running_action_set;
275 action->generic_action.state_set =
276 surf_cpu_model->states.failed_action_set;
278 xbt_swag_insert(action, action->generic_action.state_set);
280 action->variable = lmm_variable_new(cpu_maxmin_system, action,
281 action->generic_action.priority,
283 lmm_expand(cpu_maxmin_system, CPU->constraint, action->variable, 1.0);
285 return (surf_action_t) action;
288 static surf_action_t action_sleep(void *cpu, double duration)
290 surf_action_cpu_Cas01_t action = NULL;
293 duration = MAX(duration, MAXMIN_PRECISION);
295 XBT_IN2("(%s,%g)", ((cpu_Cas01_t) cpu)->name, duration);
296 action = (surf_action_cpu_Cas01_t) execute(cpu, 1.0);
297 action->generic_action.max_duration = duration;
298 action->suspended = 2;
299 if (duration == NO_MAX_DURATION) {
300 /* Move to the *end* of the corresponding action set. This convention
301 is used to speed up update_resource_state */
302 xbt_swag_remove(action, ((surf_action_t) action)->state_set);
303 ((surf_action_t) action)->state_set =
304 running_action_set_that_does_not_need_being_checked;
305 xbt_swag_insert(action, ((surf_action_t) action)->state_set);
308 lmm_update_variable_weight(cpu_maxmin_system, action->variable, 0.0);
310 return (surf_action_t) action;
313 static void action_suspend(surf_action_t action)
315 XBT_IN1("(%p)", action);
316 if (((surf_action_cpu_Cas01_t) action)->suspended != 2) {
317 lmm_update_variable_weight(cpu_maxmin_system,
318 ((surf_action_cpu_Cas01_t) action)->variable,
320 ((surf_action_cpu_Cas01_t) action)->suspended = 1;
325 static void action_resume(surf_action_t action)
327 XBT_IN1("(%p)", action);
328 if (((surf_action_cpu_Cas01_t) action)->suspended != 2) {
329 lmm_update_variable_weight(cpu_maxmin_system,
330 ((surf_action_cpu_Cas01_t) action)->variable,
332 ((surf_action_cpu_Cas01_t) action)->suspended = 0;
337 static int action_is_suspended(surf_action_t action)
339 return (((surf_action_cpu_Cas01_t) action)->suspended == 1);
342 static void action_set_max_duration(surf_action_t action, double duration)
344 XBT_IN2("(%p,%g)", action, duration);
345 action->max_duration = duration;
349 static void action_set_priority(surf_action_t action, double priority)
351 XBT_IN2("(%p,%g)", action, priority);
352 action->priority = priority;
353 lmm_update_variable_weight(cpu_maxmin_system,
354 ((surf_action_cpu_Cas01_t) action)->variable,
360 static e_surf_cpu_state_t get_state(void *cpu)
362 return ((cpu_Cas01_t) cpu)->state_current;
365 static double get_speed(void *cpu, double load)
367 return load * (((cpu_Cas01_t) cpu)->power_scale);
370 static double get_available_speed(void *cpu)
372 /* number between 0 and 1 */
373 return ((cpu_Cas01_t) cpu)->power_current;
376 static xbt_dict_t get_properties(void *cpu)
378 return ((cpu_Cas01_t) cpu)->properties;
381 static void finalize(void)
383 lmm_system_free(cpu_maxmin_system);
384 cpu_maxmin_system = NULL;
386 surf_model_exit(surf_cpu_model);
387 surf_cpu_model = NULL;
389 xbt_swag_free(running_action_set_that_does_not_need_being_checked);
390 running_action_set_that_does_not_need_being_checked = NULL;
393 static void surf_cpu_model_init_internal(void)
395 s_surf_action_t action;
397 surf_cpu_model = surf_model_init();
399 running_action_set_that_does_not_need_being_checked =
400 xbt_swag_new(xbt_swag_offset(action, state_hookup));
402 surf_cpu_model->action_get_state = surf_action_get_state;
403 surf_cpu_model->action_get_start_time = surf_action_get_start_time;
404 surf_cpu_model->action_get_finish_time = surf_action_get_finish_time;
405 surf_cpu_model->action_free = action_free;
406 surf_cpu_model->action_use = action_use;
407 surf_cpu_model->action_cancel = action_cancel;
408 surf_cpu_model->action_change_state = action_change_state;
409 surf_cpu_model->action_set_data = surf_action_set_data;
410 surf_cpu_model->name = "CPU";
412 surf_cpu_model->model_private->resource_used = resource_used;
413 surf_cpu_model->model_private->share_resources = share_resources;
414 surf_cpu_model->model_private->update_actions_state = update_actions_state;
415 surf_cpu_model->model_private->update_resource_state =
416 update_resource_state;
417 surf_cpu_model->model_private->finalize = finalize;
419 surf_cpu_model->suspend = action_suspend;
420 surf_cpu_model->resume = action_resume;
421 surf_cpu_model->is_suspended = action_is_suspended;
422 surf_cpu_model->set_max_duration = action_set_max_duration;
423 surf_cpu_model->set_priority = action_set_priority;
424 surf_cpu_model->extension.cpu.execute = execute;
425 surf_cpu_model->extension.cpu.sleep = action_sleep;
427 surf_cpu_model->extension.cpu.get_state = get_state;
428 surf_cpu_model->extension.cpu.get_speed = get_speed;
429 surf_cpu_model->extension.cpu.get_available_speed = get_available_speed;
430 /*manage the properties of the cpu */
431 surf_cpu_model->get_properties = get_properties;
433 if (!cpu_maxmin_system)
434 cpu_maxmin_system = lmm_system_new();
437 /*********************************************************************/
438 /* Basic sharing model for CPU: that is where all this started... ;) */
439 /*********************************************************************/
440 /* @InProceedings{casanova01simgrid, */
441 /* author = "H. Casanova", */
442 /* booktitle = "Proceedings of the IEEE Symposium on Cluster Computing */
443 /* and the Grid (CCGrid'01)", */
444 /* publisher = "IEEE Computer Society", */
445 /* title = "Simgrid: {A} Toolkit for the Simulation of Application */
449 /* note = "Available at */
450 /* \url{http://grail.sdsc.edu/papers/simgrid_ccgrid01.ps.gz}." */
452 void surf_cpu_model_init_Cas01(const char *filename)
456 surf_cpu_model_init_internal();
457 define_callbacks(filename);
458 xbt_dynar_push(model_list, &surf_cpu_model);