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;
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);
33 cpu->model = (surf_model_t) surf_cpu_model;
34 cpu->name = xbt_strdup(name);
35 cpu->power_scale = power_scale;
36 xbt_assert0(cpu->power_scale > 0, "Power has to be >0");
37 cpu->power_current = power_initial;
40 tmgr_history_add_trace(history, power_trace, 0.0, 0, cpu);
42 cpu->state_current = state_initial;
45 tmgr_history_add_trace(history, state_trace, 0.0, 0, cpu);
48 lmm_constraint_new(cpu_maxmin_system, cpu,
49 cpu->power_current * cpu->power_scale);
51 /*add the property set*/
52 cpu->properties = cpu_properties;
54 current_property_set = cpu_properties;
56 xbt_dict_set(cpu_set, name, cpu, cpu_free);
62 static void parse_cpu_init(void)
64 double power_scale = 0.0;
65 double power_initial = 0.0;
66 tmgr_trace_t power_trace = NULL;
67 e_surf_cpu_state_t state_initial = SURF_CPU_OFF;
68 tmgr_trace_t state_trace = NULL;
70 surf_parse_get_double(&power_scale, A_surfxml_host_power);
71 surf_parse_get_double(&power_initial, A_surfxml_host_availability);
72 surf_parse_get_trace(&power_trace, A_surfxml_host_availability_file);
74 xbt_assert0((A_surfxml_host_state == A_surfxml_host_state_ON) ||
75 (A_surfxml_host_state == A_surfxml_host_state_OFF),
77 if (A_surfxml_host_state == A_surfxml_host_state_ON)
78 state_initial = SURF_CPU_ON;
79 if (A_surfxml_host_state == A_surfxml_host_state_OFF)
80 state_initial = SURF_CPU_OFF;
81 surf_parse_get_trace(&state_trace, A_surfxml_host_state_file);
83 current_property_set = xbt_dict_new();
84 cpu_new(A_surfxml_host_id, power_scale, power_initial, power_trace, state_initial,
85 state_trace, /*add the properties*/ current_property_set);
89 static void parse_file(const char *file)
91 surf_parse_reset_parser();
92 surfxml_add_callback(STag_surfxml_host_cb_list, parse_cpu_init);
93 surfxml_add_callback(STag_surfxml_prop_cb_list, &parse_properties);
95 surf_parse_open(file);
96 xbt_assert1((!surf_parse()), "Parse error in %s", file);
100 static void *name_service(const char *name)
102 return xbt_dict_get_or_null(cpu_set, name);
105 static const char *get_resource_name(void *resource_id)
107 return ((cpu_Cas01_t) resource_id)->name;
110 static int resource_used(void *resource_id)
112 return lmm_constraint_used(cpu_maxmin_system,
113 ((cpu_Cas01_t) resource_id)->constraint);
116 static int action_free(surf_action_t action)
119 if (!action->using) {
120 xbt_swag_remove(action, action->state_set);
121 if (((surf_action_cpu_Cas01_t) action)->variable)
122 lmm_variable_free(cpu_maxmin_system,
123 ((surf_action_cpu_Cas01_t) action)->variable);
130 static void action_use(surf_action_t action)
135 static void action_cancel(surf_action_t action)
137 surf_action_change_state(action, SURF_ACTION_FAILED);
141 static void action_recycle(surf_action_t action)
146 static void action_change_state(surf_action_t action,
147 e_surf_action_state_t state)
149 /* if((state==SURF_ACTION_DONE) || (state==SURF_ACTION_FAILED)) */
150 /* if(((surf_action_cpu_Cas01_t)action)->variable) { */
151 /* lmm_variable_disable(cpu_maxmin_system, ((surf_action_cpu_Cas01_t)action)->variable); */
152 /* ((surf_action_cpu_Cas01_t)action)->variable = NULL; */
155 surf_action_change_state(action, state);
159 static double share_resources(double now)
161 s_surf_action_cpu_Cas01_t action;
162 return generic_maxmin_share_resources(surf_cpu_model->common_public->
163 states.running_action_set,
164 xbt_swag_offset(action, variable),
165 cpu_maxmin_system, lmm_solve);
168 static void update_actions_state(double now, double delta)
170 surf_action_cpu_Cas01_t action = NULL;
171 surf_action_cpu_Cas01_t next_action = NULL;
172 xbt_swag_t running_actions =
173 surf_cpu_model->common_public->states.running_action_set;
175 xbt_swag_t failed_actions =
176 surf_cpu_model->common_public->states.failed_action_set;
179 xbt_swag_foreach_safe(action, next_action, running_actions) {
180 double_update(&(action->generic_action.remains),
181 lmm_variable_getvalue(action->variable) * delta);
182 if (action->generic_action.max_duration != NO_MAX_DURATION)
183 double_update(&(action->generic_action.max_duration), delta);
184 if ((action->generic_action.remains <= 0) &&
185 (lmm_get_variable_weight(action->variable) > 0)) {
186 action->generic_action.finish = surf_get_clock();
187 action_change_state((surf_action_t) action, SURF_ACTION_DONE);
188 } else if ((action->generic_action.max_duration != NO_MAX_DURATION) &&
189 (action->generic_action.max_duration <= 0)) {
190 action->generic_action.finish = surf_get_clock();
191 action_change_state((surf_action_t) action, SURF_ACTION_DONE);
192 } else { /* Need to check that none of the model has failed */
193 lmm_constraint_t cnst = NULL;
195 cpu_Cas01_t cpu = NULL;
198 lmm_get_cnst_from_var(cpu_maxmin_system, action->variable,
200 cpu = lmm_constraint_id(cnst);
201 if (cpu->state_current == SURF_CPU_OFF) {
202 action->generic_action.finish = surf_get_clock();
203 action_change_state((surf_action_t) action, SURF_ACTION_FAILED);
213 static void update_resource_state(void *id,
214 tmgr_trace_event_t event_type,
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 cpu->state_current = SURF_CPU_OFF;
229 CRITICAL0("Unknown event ! \n");
236 static surf_action_t execute(void *cpu, double size)
238 surf_action_cpu_Cas01_t action = NULL;
239 cpu_Cas01_t CPU = cpu;
241 XBT_IN2("(%s,%g)", CPU->name, size);
242 action = xbt_new0(s_surf_action_cpu_Cas01_t, 1);
244 action->generic_action.using = 1;
245 action->generic_action.cost = size;
246 action->generic_action.remains = size;
247 action->generic_action.priority = 1.0;
248 action->generic_action.max_duration = NO_MAX_DURATION;
249 action->generic_action.start = surf_get_clock();
250 action->generic_action.finish = -1.0;
251 action->generic_action.model_type =
252 (surf_model_t) surf_cpu_model;
253 action->suspended = 0; /* Should be useless because of the
254 calloc but it seems to help valgrind... */
256 if (CPU->state_current == SURF_CPU_ON)
257 action->generic_action.state_set =
258 surf_cpu_model->common_public->states.running_action_set;
260 action->generic_action.state_set =
261 surf_cpu_model->common_public->states.failed_action_set;
262 xbt_swag_insert(action, action->generic_action.state_set);
264 action->variable = lmm_variable_new(cpu_maxmin_system, action,
265 action->generic_action.priority,
267 lmm_expand(cpu_maxmin_system, CPU->constraint, action->variable, 1.0);
269 return (surf_action_t) action;
272 static surf_action_t action_sleep(void *cpu, double duration)
274 surf_action_cpu_Cas01_t action = NULL;
276 XBT_IN2("(%s,%g)", ((cpu_Cas01_t) cpu)->name, duration);
277 action = (surf_action_cpu_Cas01_t) execute(cpu, 1.0);
278 action->generic_action.max_duration = duration;
279 action->suspended = 2;
280 lmm_update_variable_weight(cpu_maxmin_system, action->variable, 0.0);
282 return (surf_action_t) action;
285 static void action_suspend(surf_action_t action)
287 XBT_IN1("(%p)", action);
288 if (((surf_action_cpu_Cas01_t) action)->suspended != 2) {
289 lmm_update_variable_weight(cpu_maxmin_system,
290 ((surf_action_cpu_Cas01_t) action)->
292 ((surf_action_cpu_Cas01_t) action)->suspended = 1;
297 static void action_resume(surf_action_t action)
299 XBT_IN1("(%p)", action);
300 if (((surf_action_cpu_Cas01_t) action)->suspended != 2) {
301 lmm_update_variable_weight(cpu_maxmin_system,
302 ((surf_action_cpu_Cas01_t) action)->
303 variable, action->priority);
304 ((surf_action_cpu_Cas01_t) action)->suspended = 0;
309 static int action_is_suspended(surf_action_t action)
311 return (((surf_action_cpu_Cas01_t) action)->suspended == 1);
314 static void action_set_max_duration(surf_action_t action, double duration)
316 XBT_IN2("(%p,%g)", action, duration);
317 action->max_duration = duration;
321 static void action_set_priority(surf_action_t action, double priority)
323 XBT_IN2("(%p,%g)", action, priority);
324 action->priority = priority;
325 lmm_update_variable_weight(cpu_maxmin_system, ((surf_action_cpu_Cas01_t) action)->variable, priority);
330 static e_surf_cpu_state_t get_state(void *cpu)
332 return ((cpu_Cas01_t) cpu)->state_current;
335 static double get_speed(void *cpu, double load)
337 return load * (((cpu_Cas01_t) cpu)->power_scale);
340 static double get_available_speed(void *cpu)
342 /* number between 0 and 1 */
343 return ((cpu_Cas01_t) cpu)->power_current;
346 static xbt_dict_t get_properties(void *cpu)
348 return ((cpu_Cas01_t) cpu)->properties;
351 static void finalize(void)
353 xbt_dict_free(&cpu_set);
354 xbt_swag_free(surf_cpu_model->common_public->states.ready_action_set);
355 xbt_swag_free(surf_cpu_model->common_public->states.
357 xbt_swag_free(surf_cpu_model->common_public->states.
359 xbt_swag_free(surf_cpu_model->common_public->states.done_action_set);
360 free(surf_cpu_model->common_public);
361 free(surf_cpu_model->common_private);
362 free(surf_cpu_model->extension_public);
364 free(surf_cpu_model);
365 surf_cpu_model = NULL;
368 static void surf_cpu_model_init_internal(void)
370 s_surf_action_t action;
372 surf_cpu_model = xbt_new0(s_surf_cpu_model_t, 1);
374 surf_cpu_model->common_private =
375 xbt_new0(s_surf_model_private_t, 1);
376 surf_cpu_model->common_public = xbt_new0(s_surf_model_public_t, 1);
378 surf_cpu_model->extension_public =
379 xbt_new0(s_surf_cpu_model_extension_public_t, 1);
381 surf_cpu_model->common_public->states.ready_action_set =
382 xbt_swag_new(xbt_swag_offset(action, state_hookup));
383 surf_cpu_model->common_public->states.running_action_set =
384 xbt_swag_new(xbt_swag_offset(action, state_hookup));
385 surf_cpu_model->common_public->states.failed_action_set =
386 xbt_swag_new(xbt_swag_offset(action, state_hookup));
387 surf_cpu_model->common_public->states.done_action_set =
388 xbt_swag_new(xbt_swag_offset(action, state_hookup));
390 surf_cpu_model->common_public->name_service = name_service;
391 surf_cpu_model->common_public->get_resource_name = get_resource_name;
392 surf_cpu_model->common_public->action_get_state =
393 surf_action_get_state;
394 surf_cpu_model->common_public->action_get_start_time =
395 surf_action_get_start_time;
396 surf_cpu_model->common_public->action_get_finish_time =
397 surf_action_get_finish_time;
398 surf_cpu_model->common_public->action_free = action_free;
399 surf_cpu_model->common_public->action_use = action_use;
400 surf_cpu_model->common_public->action_cancel = action_cancel;
401 surf_cpu_model->common_public->action_recycle = action_recycle;
402 surf_cpu_model->common_public->action_change_state =
404 surf_cpu_model->common_public->action_set_data = surf_action_set_data;
405 surf_cpu_model->common_public->name = "CPU";
407 surf_cpu_model->common_private->resource_used = resource_used;
408 surf_cpu_model->common_private->share_resources = share_resources;
409 surf_cpu_model->common_private->update_actions_state =
410 update_actions_state;
411 surf_cpu_model->common_private->update_resource_state =
412 update_resource_state;
413 surf_cpu_model->common_private->finalize = finalize;
415 surf_cpu_model->common_public->suspend = action_suspend;
416 surf_cpu_model->common_public->resume = action_resume;
417 surf_cpu_model->common_public->is_suspended = action_is_suspended;
418 surf_cpu_model->common_public->set_max_duration =
419 action_set_max_duration;
420 surf_cpu_model->common_public->set_priority = action_set_priority;
421 surf_cpu_model->extension_public->execute = execute;
422 surf_cpu_model->extension_public->sleep = action_sleep;
424 surf_cpu_model->extension_public->get_state = get_state;
425 surf_cpu_model->extension_public->get_speed = get_speed;
426 surf_cpu_model->extension_public->get_available_speed =
428 /*manage the properties of the cpu*/
429 surf_cpu_model->common_public->get_cpu_properties = get_properties;
431 if(!cpu_set) cpu_set = xbt_dict_new();
432 if (!cpu_maxmin_system) cpu_maxmin_system = lmm_system_new();
435 /*********************************************************************/
436 /* Basic sharing model for CPU: that is where all this started... ;) */
437 /*********************************************************************/
438 /* @InProceedings{casanova01simgrid, */
439 /* author = "H. Casanova", */
440 /* booktitle = "Proceedings of the IEEE Symposium on Cluster Computing */
441 /* and the Grid (CCGrid'01)", */
442 /* publisher = "IEEE Computer Society", */
443 /* title = "Simgrid: {A} Toolkit for the Simulation of Application */
447 /* note = "Available at */
448 /* \url{http://grail.sdsc.edu/papers/simgrid_ccgrid01.ps.gz}." */
450 void surf_cpu_model_init_Cas01(const char *filename)
454 surf_cpu_model_init_internal();
455 parse_file(filename);
456 xbt_dynar_push(model_list, &surf_cpu_model);