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->common_public.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 =
195 surf_cpu_model->common_public.states.running_action_set;
197 xbt_swag_t failed_actions =
198 surf_cpu_model->common_public.states.failed_action_set;
201 xbt_swag_foreach_safe(action, next_action, running_actions) {
202 double_update(&(action->generic_action.remains),
203 lmm_variable_getvalue(action->variable) * delta);
204 if (action->generic_action.max_duration != NO_MAX_DURATION)
205 double_update(&(action->generic_action.max_duration), delta);
206 if ((action->generic_action.remains <= 0) &&
207 (lmm_get_variable_weight(action->variable) > 0)) {
208 action->generic_action.finish = surf_get_clock();
209 action_change_state((surf_action_t) action, SURF_ACTION_DONE);
210 } else if ((action->generic_action.max_duration != NO_MAX_DURATION) &&
211 (action->generic_action.max_duration <= 0)) {
212 action->generic_action.finish = surf_get_clock();
213 action_change_state((surf_action_t) action, SURF_ACTION_DONE);
220 static void update_resource_state(void *id,
221 tmgr_trace_event_t event_type,
222 double value, double date)
224 cpu_Cas01_t cpu = id;
226 if (event_type == cpu->power_event) {
227 cpu->power_current = value;
228 lmm_update_constraint_bound(cpu_maxmin_system, cpu->constraint,
229 cpu->power_current * cpu->power_scale);
230 } else if (event_type == cpu->state_event) {
232 cpu->state_current = SURF_CPU_ON;
234 lmm_constraint_t cnst = cpu->constraint;
235 lmm_variable_t var = NULL;
236 lmm_element_t elem = NULL;
238 cpu->state_current = SURF_CPU_OFF;
240 while ((var = lmm_get_var_from_cnst(cpu_maxmin_system, cnst, &elem))) {
241 surf_action_t action = lmm_variable_id(var);
243 if (surf_action_get_state(action) == SURF_ACTION_RUNNING ||
244 surf_action_get_state(action) == SURF_ACTION_READY ||
245 surf_action_get_state(action) == SURF_ACTION_NOT_IN_THE_SYSTEM) {
246 action->finish = date;
247 action_change_state(action, SURF_ACTION_FAILED);
252 CRITICAL0("Unknown event ! \n");
259 static surf_action_t execute(void *cpu, double size)
261 surf_action_cpu_Cas01_t action = NULL;
262 cpu_Cas01_t CPU = cpu;
264 XBT_IN2("(%s,%g)", CPU->name, size);
265 action = xbt_new0(s_surf_action_cpu_Cas01_t, 1);
267 action->generic_action.refcount = 1;
268 action->generic_action.cost = size;
269 action->generic_action.remains = size;
270 action->generic_action.priority = 1.0;
271 action->generic_action.max_duration = NO_MAX_DURATION;
272 action->generic_action.start = surf_get_clock();
273 action->generic_action.finish = -1.0;
274 action->generic_action.model_type = surf_cpu_model;
275 action->suspended = 0; /* Should be useless because of the
276 calloc but it seems to help valgrind... */
278 if (CPU->state_current == SURF_CPU_ON)
279 action->generic_action.state_set =
280 surf_cpu_model->common_public.states.running_action_set;
282 action->generic_action.state_set =
283 surf_cpu_model->common_public.states.failed_action_set;
285 xbt_swag_insert(action, action->generic_action.state_set);
287 action->variable = lmm_variable_new(cpu_maxmin_system, action,
288 action->generic_action.priority,
290 lmm_expand(cpu_maxmin_system, CPU->constraint, action->variable, 1.0);
292 return (surf_action_t) action;
295 static surf_action_t action_sleep(void *cpu, double duration)
297 surf_action_cpu_Cas01_t action = NULL;
300 duration = MAX(duration, MAXMIN_PRECISION);
302 XBT_IN2("(%s,%g)", ((cpu_Cas01_t) cpu)->name, duration);
303 action = (surf_action_cpu_Cas01_t) execute(cpu, 1.0);
304 action->generic_action.max_duration = duration;
305 action->suspended = 2;
306 if (duration == NO_MAX_DURATION) {
307 /* Move to the *end* of the corresponding action set. This convention
308 is used to speed up update_resource_state */
309 xbt_swag_remove(action, ((surf_action_t) action)->state_set);
310 ((surf_action_t) action)->state_set =
311 running_action_set_that_does_not_need_being_checked;
312 xbt_swag_insert(action, ((surf_action_t) action)->state_set);
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)->variable,
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)->variable,
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,
361 ((surf_action_cpu_Cas01_t) action)->variable,
367 static e_surf_cpu_state_t get_state(void *cpu)
369 return ((cpu_Cas01_t) cpu)->state_current;
372 static double get_speed(void *cpu, double load)
374 return load * (((cpu_Cas01_t) cpu)->power_scale);
377 static double get_available_speed(void *cpu)
379 /* number between 0 and 1 */
380 return ((cpu_Cas01_t) cpu)->power_current;
383 static xbt_dict_t get_properties(void *cpu)
385 return ((cpu_Cas01_t) cpu)->properties;
388 static void finalize(void)
390 lmm_system_free(cpu_maxmin_system);
391 cpu_maxmin_system = NULL;
393 surf_model_exit(surf_cpu_model);
395 xbt_swag_free(running_action_set_that_does_not_need_being_checked);
396 running_action_set_that_does_not_need_being_checked = NULL;
398 free(surf_cpu_model);
399 surf_cpu_model = NULL;
402 static void surf_cpu_model_init_internal(void)
404 s_surf_action_t action;
406 surf_cpu_model = xbt_new0(s_surf_model_t, 1);
408 surf_model_init(surf_cpu_model);
410 running_action_set_that_does_not_need_being_checked =
411 xbt_swag_new(xbt_swag_offset(action, state_hookup));
413 surf_cpu_model->common_public.get_resource_name = get_resource_name;
414 surf_cpu_model->common_public.action_get_state = surf_action_get_state;
415 surf_cpu_model->common_public.action_get_start_time =
416 surf_action_get_start_time;
417 surf_cpu_model->common_public.action_get_finish_time =
418 surf_action_get_finish_time;
419 surf_cpu_model->common_public.action_free = action_free;
420 surf_cpu_model->common_public.action_use = action_use;
421 surf_cpu_model->common_public.action_cancel = action_cancel;
422 surf_cpu_model->common_public.action_change_state = action_change_state;
423 surf_cpu_model->common_public.action_set_data = surf_action_set_data;
424 surf_cpu_model->common_public.name = "CPU";
426 surf_cpu_model->common_private->resource_used = resource_used;
427 surf_cpu_model->common_private->share_resources = share_resources;
428 surf_cpu_model->common_private->update_actions_state = update_actions_state;
429 surf_cpu_model->common_private->update_resource_state =
430 update_resource_state;
431 surf_cpu_model->common_private->finalize = finalize;
433 surf_cpu_model->common_public.suspend = action_suspend;
434 surf_cpu_model->common_public.resume = action_resume;
435 surf_cpu_model->common_public.is_suspended = action_is_suspended;
436 surf_cpu_model->common_public.set_max_duration = action_set_max_duration;
437 surf_cpu_model->common_public.set_priority = action_set_priority;
438 surf_cpu_model->extension.cpu.execute = execute;
439 surf_cpu_model->extension.cpu.sleep = action_sleep;
441 surf_cpu_model->extension.cpu.get_state = get_state;
442 surf_cpu_model->extension.cpu.get_speed = get_speed;
443 surf_cpu_model->extension.cpu.get_available_speed = get_available_speed;
444 /*manage the properties of the cpu */
445 surf_cpu_model->common_public.get_properties = get_properties;
447 if (!cpu_maxmin_system)
448 cpu_maxmin_system = lmm_system_new();
451 /*********************************************************************/
452 /* Basic sharing model for CPU: that is where all this started... ;) */
453 /*********************************************************************/
454 /* @InProceedings{casanova01simgrid, */
455 /* author = "H. Casanova", */
456 /* booktitle = "Proceedings of the IEEE Symposium on Cluster Computing */
457 /* and the Grid (CCGrid'01)", */
458 /* publisher = "IEEE Computer Society", */
459 /* title = "Simgrid: {A} Toolkit for the Simulation of Application */
463 /* note = "Available at */
464 /* \url{http://grail.sdsc.edu/papers/simgrid_ccgrid01.ps.gz}." */
466 void surf_cpu_model_init_Cas01(const char *filename)
470 surf_cpu_model_init_internal();
471 define_callbacks(filename);
472 xbt_dynar_push(model_list, &surf_cpu_model);