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 "surf_private.h"
10 typedef struct surf_action_cpu_Cas01 {
11 s_surf_action_t generic_action;
12 lmm_variable_t variable;
14 } s_surf_action_cpu_Cas01_t, *surf_action_cpu_Cas01_t;
16 typedef struct cpu_Cas01 {
17 s_surf_resource_t generic_resource;
20 tmgr_trace_event_t power_event;
21 e_surf_cpu_state_t state_current;
22 tmgr_trace_event_t state_event;
23 lmm_constraint_t constraint;
24 /*Handles the properties that can be added to cpu's */
25 xbt_dict_t properties;
26 } s_cpu_Cas01_t, *cpu_Cas01_t;
28 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_cpu, surf,
29 "Logging specific to the SURF CPU module");
33 surf_model_t surf_cpu_model = NULL;
34 lmm_system_t cpu_maxmin_system = NULL;
37 static xbt_swag_t running_action_set_that_does_not_need_being_checked = NULL;
39 static void cpu_free(void *cpu)
41 xbt_dict_free(&(((cpu_Cas01_t) cpu)->properties));
42 surf_resource_free(cpu);
45 static cpu_Cas01_t cpu_new(char *name, double power_scale,
47 tmgr_trace_t power_trace,
48 e_surf_cpu_state_t state_initial,
49 tmgr_trace_t state_trace,
50 xbt_dict_t cpu_properties)
52 cpu_Cas01_t cpu = xbt_new0(s_cpu_Cas01_t, 1);
53 xbt_assert1(!surf_model_resource_by_name(surf_cpu_model, name),
54 "Host '%s' declared several times in the platform file", name);
55 cpu->generic_resource.model = surf_cpu_model;
56 cpu->generic_resource.name = name;
57 cpu->power_scale = power_scale;
58 xbt_assert0(cpu->power_scale > 0, "Power has to be >0");
59 cpu->power_current = power_initial;
62 tmgr_history_add_trace(history, power_trace, 0.0, 0, cpu);
64 cpu->state_current = state_initial;
67 tmgr_history_add_trace(history, state_trace, 0.0, 0, cpu);
70 lmm_constraint_new(cpu_maxmin_system, cpu,
71 cpu->power_current * cpu->power_scale);
73 /*add the property set */
74 cpu->properties = cpu_properties;
76 current_property_set = cpu_properties;
78 xbt_dict_set(surf_model_resource_set(surf_cpu_model), name, cpu, cpu_free);
84 static void parse_cpu_init(void)
86 double power_scale = 0.0;
87 double power_initial = 0.0;
88 tmgr_trace_t power_trace = NULL;
89 e_surf_cpu_state_t state_initial = SURF_CPU_OFF;
90 tmgr_trace_t state_trace = NULL;
92 power_scale = get_cpu_power(A_surfxml_host_power);
93 surf_parse_get_double(&power_initial, A_surfxml_host_availability);
94 surf_parse_get_trace(&power_trace, A_surfxml_host_availability_file);
96 xbt_assert0((A_surfxml_host_state == A_surfxml_host_state_ON) ||
97 (A_surfxml_host_state == A_surfxml_host_state_OFF),
99 if (A_surfxml_host_state == A_surfxml_host_state_ON)
100 state_initial = SURF_CPU_ON;
101 if (A_surfxml_host_state == A_surfxml_host_state_OFF)
102 state_initial = SURF_CPU_OFF;
103 surf_parse_get_trace(&state_trace, A_surfxml_host_state_file);
105 current_property_set = xbt_dict_new();
106 cpu_new(xbt_strdup(A_surfxml_host_id), power_scale, power_initial,
107 power_trace, state_initial, state_trace, current_property_set);
111 static void add_traces_cpu(void)
113 xbt_dict_cursor_t cursor = NULL;
114 char *trace_name, *elm;
116 static int called = 0;
123 /* connect all traces relative to hosts */
124 xbt_dict_foreach(trace_connect_list_host_avail, cursor, trace_name, elm) {
125 tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
126 cpu_Cas01_t host = surf_model_resource_by_name(surf_cpu_model, elm);
128 xbt_assert1(host, "Host %s undefined", elm);
129 xbt_assert1(trace, "Trace %s undefined", trace_name);
131 host->state_event = tmgr_history_add_trace(history, trace, 0.0, 0, host);
134 xbt_dict_foreach(trace_connect_list_power, cursor, trace_name, elm) {
135 tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
136 cpu_Cas01_t host = surf_model_resource_by_name(surf_cpu_model, elm);
138 xbt_assert1(host, "Host %s undefined", elm);
139 xbt_assert1(trace, "Trace %s undefined", trace_name);
141 host->power_event = tmgr_history_add_trace(history, trace, 0.0, 0, host);
145 static void define_callbacks(const char *file)
147 surf_parse_reset_parser();
148 surfxml_add_callback(STag_surfxml_host_cb_list, parse_cpu_init);
151 static int resource_used(void *resource_id)
153 return lmm_constraint_used(cpu_maxmin_system,
154 ((cpu_Cas01_t) resource_id)->constraint);
157 static int action_unref(surf_action_t action)
160 if (!action->refcount) {
161 xbt_swag_remove(action, action->state_set);
162 if (((surf_action_cpu_Cas01_t) action)->variable)
163 lmm_variable_free(cpu_maxmin_system,
164 ((surf_action_cpu_Cas01_t) action)->variable);
171 static void action_cancel(surf_action_t action)
173 surf_action_state_set(action, SURF_ACTION_FAILED);
177 static void cpu_action_state_set(surf_action_t action,
178 e_surf_action_state_t state)
180 /* if((state==SURF_ACTION_DONE) || (state==SURF_ACTION_FAILED)) */
181 /* if(((surf_action_cpu_Cas01_t)action)->variable) { */
182 /* lmm_variable_disable(cpu_maxmin_system, ((surf_action_cpu_Cas01_t)action)->variable); */
183 /* ((surf_action_cpu_Cas01_t)action)->variable = NULL; */
186 surf_action_state_set(action, state);
190 static double share_resources(double now)
192 s_surf_action_cpu_Cas01_t action;
193 return generic_maxmin_share_resources(surf_cpu_model->states.
195 xbt_swag_offset(action, variable),
196 cpu_maxmin_system, lmm_solve);
199 static void update_actions_state(double now, double delta)
201 surf_action_cpu_Cas01_t action = NULL;
202 surf_action_cpu_Cas01_t next_action = NULL;
203 xbt_swag_t running_actions = surf_cpu_model->states.running_action_set;
205 xbt_swag_t failed_actions =
206 surf_cpu_model->states.failed_action_set;
209 xbt_swag_foreach_safe(action, next_action, running_actions) {
210 double_update(&(action->generic_action.remains),
211 lmm_variable_getvalue(action->variable) * delta);
212 if (action->generic_action.max_duration != NO_MAX_DURATION)
213 double_update(&(action->generic_action.max_duration), delta);
214 if ((action->generic_action.remains <= 0) &&
215 (lmm_get_variable_weight(action->variable) > 0)) {
216 action->generic_action.finish = surf_get_clock();
217 cpu_action_state_set((surf_action_t) action, SURF_ACTION_DONE);
218 } else if ((action->generic_action.max_duration != NO_MAX_DURATION) &&
219 (action->generic_action.max_duration <= 0)) {
220 action->generic_action.finish = surf_get_clock();
221 cpu_action_state_set((surf_action_t) action, SURF_ACTION_DONE);
228 static void update_resource_state(void *id,
229 tmgr_trace_event_t event_type,
230 double value, double date)
232 cpu_Cas01_t cpu = id;
234 if (event_type == cpu->power_event) {
235 cpu->power_current = value;
236 lmm_update_constraint_bound(cpu_maxmin_system, cpu->constraint,
237 cpu->power_current * cpu->power_scale);
238 } else if (event_type == cpu->state_event) {
240 cpu->state_current = SURF_CPU_ON;
242 lmm_constraint_t cnst = cpu->constraint;
243 lmm_variable_t var = NULL;
244 lmm_element_t elem = NULL;
246 cpu->state_current = SURF_CPU_OFF;
248 while ((var = lmm_get_var_from_cnst(cpu_maxmin_system, cnst, &elem))) {
249 surf_action_t action = lmm_variable_id(var);
251 if (surf_action_state_get(action) == SURF_ACTION_RUNNING ||
252 surf_action_state_get(action) == SURF_ACTION_READY ||
253 surf_action_state_get(action) == SURF_ACTION_NOT_IN_THE_SYSTEM) {
254 action->finish = date;
255 cpu_action_state_set(action, SURF_ACTION_FAILED);
260 CRITICAL0("Unknown event ! \n");
267 static surf_action_t execute(void *cpu, double size)
269 surf_action_cpu_Cas01_t action = NULL;
270 cpu_Cas01_t CPU = cpu;
272 XBT_IN2("(%s,%g)", CPU->generic_resource.name, size);
273 action = xbt_new0(s_surf_action_cpu_Cas01_t, 1);
275 action->generic_action.refcount = 1;
276 action->generic_action.cost = size;
277 action->generic_action.remains = size;
278 action->generic_action.priority = 1.0;
279 action->generic_action.max_duration = NO_MAX_DURATION;
280 action->generic_action.start = surf_get_clock();
281 action->generic_action.finish = -1.0;
282 action->generic_action.model_type = surf_cpu_model;
283 action->suspended = 0; /* Should be useless because of the
284 calloc but it seems to help valgrind... */
286 if (CPU->state_current == SURF_CPU_ON)
287 action->generic_action.state_set =
288 surf_cpu_model->states.running_action_set;
290 action->generic_action.state_set =
291 surf_cpu_model->states.failed_action_set;
293 xbt_swag_insert(action, action->generic_action.state_set);
295 action->variable = lmm_variable_new(cpu_maxmin_system, action,
296 action->generic_action.priority,
298 lmm_expand(cpu_maxmin_system, CPU->constraint, action->variable, 1.0);
300 return (surf_action_t) action;
303 static surf_action_t action_sleep(void *cpu, double duration)
305 surf_action_cpu_Cas01_t action = NULL;
308 duration = MAX(duration, MAXMIN_PRECISION);
310 XBT_IN2("(%s,%g)", ((cpu_Cas01_t) cpu)->generic_resource.name, duration);
311 action = (surf_action_cpu_Cas01_t) execute(cpu, 1.0);
312 action->generic_action.max_duration = duration;
313 action->suspended = 2;
314 if (duration == NO_MAX_DURATION) {
315 /* Move to the *end* of the corresponding action set. This convention
316 is used to speed up update_resource_state */
317 xbt_swag_remove(action, ((surf_action_t) action)->state_set);
318 ((surf_action_t) action)->state_set =
319 running_action_set_that_does_not_need_being_checked;
320 xbt_swag_insert(action, ((surf_action_t) action)->state_set);
323 lmm_update_variable_weight(cpu_maxmin_system, action->variable, 0.0);
325 return (surf_action_t) action;
328 static void action_suspend(surf_action_t action)
330 XBT_IN1("(%p)", action);
331 if (((surf_action_cpu_Cas01_t) action)->suspended != 2) {
332 lmm_update_variable_weight(cpu_maxmin_system,
333 ((surf_action_cpu_Cas01_t) action)->variable,
335 ((surf_action_cpu_Cas01_t) action)->suspended = 1;
340 static void action_resume(surf_action_t action)
342 XBT_IN1("(%p)", action);
343 if (((surf_action_cpu_Cas01_t) action)->suspended != 2) {
344 lmm_update_variable_weight(cpu_maxmin_system,
345 ((surf_action_cpu_Cas01_t) action)->variable,
347 ((surf_action_cpu_Cas01_t) action)->suspended = 0;
352 static int action_is_suspended(surf_action_t action)
354 return (((surf_action_cpu_Cas01_t) action)->suspended == 1);
357 static void action_set_max_duration(surf_action_t action, double duration)
359 XBT_IN2("(%p,%g)", action, duration);
360 action->max_duration = duration;
364 static void action_set_priority(surf_action_t action, double priority)
366 XBT_IN2("(%p,%g)", action, priority);
367 action->priority = priority;
368 lmm_update_variable_weight(cpu_maxmin_system,
369 ((surf_action_cpu_Cas01_t) action)->variable,
375 static e_surf_cpu_state_t get_state(void *cpu)
377 return ((cpu_Cas01_t) cpu)->state_current;
380 static double get_speed(void *cpu, double load)
382 return load * (((cpu_Cas01_t) cpu)->power_scale);
385 static double get_available_speed(void *cpu)
387 /* number between 0 and 1 */
388 return ((cpu_Cas01_t) cpu)->power_current;
391 static xbt_dict_t get_properties(void *cpu)
393 return ((cpu_Cas01_t) cpu)->properties;
396 static void finalize(void)
398 lmm_system_free(cpu_maxmin_system);
399 cpu_maxmin_system = NULL;
401 surf_model_exit(surf_cpu_model);
402 surf_cpu_model = NULL;
404 xbt_swag_free(running_action_set_that_does_not_need_being_checked);
405 running_action_set_that_does_not_need_being_checked = NULL;
408 static void surf_cpu_model_init_internal(void)
410 s_surf_action_t action;
412 surf_cpu_model = surf_model_init();
414 running_action_set_that_does_not_need_being_checked =
415 xbt_swag_new(xbt_swag_offset(action, state_hookup));
417 surf_cpu_model->name = "CPU";
419 surf_cpu_model->action_unref = action_unref;
420 surf_cpu_model->action_cancel = action_cancel;
421 surf_cpu_model->action_state_set = cpu_action_state_set;
423 surf_cpu_model->model_private->resource_used = resource_used;
424 surf_cpu_model->model_private->share_resources = share_resources;
425 surf_cpu_model->model_private->update_actions_state = update_actions_state;
426 surf_cpu_model->model_private->update_resource_state =
427 update_resource_state;
428 surf_cpu_model->model_private->finalize = finalize;
430 surf_cpu_model->suspend = action_suspend;
431 surf_cpu_model->resume = action_resume;
432 surf_cpu_model->is_suspended = action_is_suspended;
433 surf_cpu_model->set_max_duration = action_set_max_duration;
434 surf_cpu_model->set_priority = action_set_priority;
435 surf_cpu_model->extension.cpu.execute = execute;
436 surf_cpu_model->extension.cpu.sleep = action_sleep;
438 surf_cpu_model->extension.cpu.get_state = get_state;
439 surf_cpu_model->extension.cpu.get_speed = get_speed;
440 surf_cpu_model->extension.cpu.get_available_speed = get_available_speed;
441 /*manage the properties of the cpu */
442 surf_cpu_model->get_properties = get_properties;
444 if (!cpu_maxmin_system)
445 cpu_maxmin_system = lmm_system_new();
448 /*********************************************************************/
449 /* Basic sharing model for CPU: that is where all this started... ;) */
450 /*********************************************************************/
451 /* @InProceedings{casanova01simgrid, */
452 /* author = "H. Casanova", */
453 /* booktitle = "Proceedings of the IEEE Symposium on Cluster Computing */
454 /* and the Grid (CCGrid'01)", */
455 /* publisher = "IEEE Computer Society", */
456 /* title = "Simgrid: {A} Toolkit for the Simulation of Application */
460 /* note = "Available at */
461 /* \url{http://grail.sdsc.edu/papers/simgrid_ccgrid01.ps.gz}." */
463 void surf_cpu_model_init_Cas01(const char *filename)
467 surf_cpu_model_init_internal();
468 define_callbacks(filename);
469 xbt_dynar_push(model_list, &surf_cpu_model);