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;
21 tmgr_trace_event_t power_event;
22 e_surf_cpu_state_t state_current;
23 tmgr_trace_event_t state_event;
24 lmm_constraint_t constraint;
25 /*Handles the properties that can be added to cpu's */
26 xbt_dict_t properties;
27 } s_cpu_Cas01_t, *cpu_Cas01_t;
29 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_cpu, surf,
30 "Logging specific to the SURF CPU module");
34 surf_model_t surf_cpu_model = NULL;
35 lmm_system_t cpu_maxmin_system = NULL;
38 static xbt_swag_t running_action_set_that_does_not_need_being_checked = NULL;
40 static void cpu_free(void *cpu)
42 xbt_dict_free(&(((cpu_Cas01_t) cpu)->properties));
43 surf_resource_free(cpu);
46 static cpu_Cas01_t cpu_new(char *name, double power_scale,
48 tmgr_trace_t power_trace,
49 e_surf_cpu_state_t state_initial,
50 tmgr_trace_t state_trace,
51 xbt_dict_t cpu_properties)
53 cpu_Cas01_t cpu = xbt_new0(s_cpu_Cas01_t, 1);
54 xbt_assert1(!surf_model_resource_by_name(surf_cpu_model, name),
55 "Host '%s' declared several times in the platform file", name);
56 cpu->generic_resource.model = surf_cpu_model;
57 cpu->generic_resource.name = name;
58 cpu->power_scale = power_scale;
59 xbt_assert0(cpu->power_scale > 0, "Power has to be >0");
60 cpu->power_current = power_initial;
63 tmgr_history_add_trace(history, power_trace, 0.0, 0, cpu);
65 cpu->state_current = state_initial;
68 tmgr_history_add_trace(history, state_trace, 0.0, 0, cpu);
71 lmm_constraint_new(cpu_maxmin_system, cpu,
72 cpu->power_current * cpu->power_scale);
74 /*add the property set */
75 cpu->properties = cpu_properties;
77 current_property_set = cpu_properties;
79 xbt_dict_set(surf_model_resource_set(surf_cpu_model), name, cpu, cpu_free);
85 static void parse_cpu_init(void)
87 double power_scale = 0.0;
88 double power_initial = 0.0;
89 tmgr_trace_t power_trace = NULL;
90 e_surf_cpu_state_t state_initial = SURF_CPU_OFF;
91 tmgr_trace_t state_trace = NULL;
93 power_scale = get_cpu_power(A_surfxml_host_power);
94 surf_parse_get_double(&power_initial, A_surfxml_host_availability);
95 surf_parse_get_trace(&power_trace, A_surfxml_host_availability_file);
97 xbt_assert0((A_surfxml_host_state == A_surfxml_host_state_ON) ||
98 (A_surfxml_host_state == A_surfxml_host_state_OFF),
100 if (A_surfxml_host_state == A_surfxml_host_state_ON)
101 state_initial = SURF_CPU_ON;
102 if (A_surfxml_host_state == A_surfxml_host_state_OFF)
103 state_initial = SURF_CPU_OFF;
104 surf_parse_get_trace(&state_trace, A_surfxml_host_state_file);
106 current_property_set = xbt_dict_new();
107 cpu_new(xbt_strdup(A_surfxml_host_id), power_scale, power_initial,
108 power_trace, state_initial, state_trace, current_property_set);
112 static void add_traces_cpu(void)
114 xbt_dict_cursor_t cursor = NULL;
115 char *trace_name, *elm;
117 static int called = 0;
124 /* connect all traces relative to hosts */
125 xbt_dict_foreach(trace_connect_list_host_avail, cursor, trace_name, elm) {
126 tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
127 cpu_Cas01_t host = surf_model_resource_by_name(surf_cpu_model, elm);
129 xbt_assert1(host, "Host %s undefined", elm);
130 xbt_assert1(trace, "Trace %s undefined", trace_name);
132 host->state_event = tmgr_history_add_trace(history, trace, 0.0, 0, host);
135 xbt_dict_foreach(trace_connect_list_power, cursor, trace_name, elm) {
136 tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
137 cpu_Cas01_t host = surf_model_resource_by_name(surf_cpu_model, elm);
139 xbt_assert1(host, "Host %s undefined", elm);
140 xbt_assert1(trace, "Trace %s undefined", trace_name);
142 host->power_event = tmgr_history_add_trace(history, trace, 0.0, 0, host);
146 static void define_callbacks(const char *file)
148 surf_parse_reset_parser();
149 surfxml_add_callback(STag_surfxml_host_cb_list, parse_cpu_init);
152 static int resource_used(void *resource_id)
154 return lmm_constraint_used(cpu_maxmin_system,
155 ((cpu_Cas01_t) resource_id)->constraint);
158 static int action_unref(surf_action_t action)
161 if (!action->refcount) {
162 xbt_swag_remove(action, action->state_set);
163 if (((surf_action_cpu_Cas01_t) action)->variable)
164 lmm_variable_free(cpu_maxmin_system,
165 ((surf_action_cpu_Cas01_t) action)->variable);
172 static void action_cancel(surf_action_t action)
174 surf_action_state_set(action, SURF_ACTION_FAILED);
178 static void cpu_action_state_set(surf_action_t action,
179 e_surf_action_state_t state)
181 /* if((state==SURF_ACTION_DONE) || (state==SURF_ACTION_FAILED)) */
182 /* if(((surf_action_cpu_Cas01_t)action)->variable) { */
183 /* lmm_variable_disable(cpu_maxmin_system, ((surf_action_cpu_Cas01_t)action)->variable); */
184 /* ((surf_action_cpu_Cas01_t)action)->variable = NULL; */
187 surf_action_state_set(action, state);
191 static double share_resources(double now)
193 s_surf_action_cpu_Cas01_t action;
194 return generic_maxmin_share_resources(surf_cpu_model->states.
196 xbt_swag_offset(action, variable),
197 cpu_maxmin_system, lmm_solve);
200 static void update_actions_state(double now, double delta)
202 surf_action_cpu_Cas01_t action = NULL;
203 surf_action_cpu_Cas01_t next_action = NULL;
204 xbt_swag_t running_actions = surf_cpu_model->states.running_action_set;
206 xbt_swag_t failed_actions =
207 surf_cpu_model->states.failed_action_set;
210 xbt_swag_foreach_safe(action, next_action, running_actions) {
211 double_update(&(action->generic_action.remains),
212 lmm_variable_getvalue(action->variable) * delta);
213 if (action->generic_action.max_duration != NO_MAX_DURATION)
214 double_update(&(action->generic_action.max_duration), delta);
215 if ((action->generic_action.remains <= 0) &&
216 (lmm_get_variable_weight(action->variable) > 0)) {
217 action->generic_action.finish = surf_get_clock();
218 cpu_action_state_set((surf_action_t) action, SURF_ACTION_DONE);
219 } else if ((action->generic_action.max_duration != NO_MAX_DURATION) &&
220 (action->generic_action.max_duration <= 0)) {
221 action->generic_action.finish = surf_get_clock();
222 cpu_action_state_set((surf_action_t) action, SURF_ACTION_DONE);
229 static void update_resource_state(void *id,
230 tmgr_trace_event_t event_type,
231 double value, double date)
233 cpu_Cas01_t cpu = id;
235 if (event_type == cpu->power_event) {
236 cpu->power_current = value;
237 lmm_update_constraint_bound(cpu_maxmin_system, cpu->constraint,
238 cpu->power_current * cpu->power_scale);
239 } else if (event_type == cpu->state_event) {
241 cpu->state_current = SURF_CPU_ON;
243 lmm_constraint_t cnst = cpu->constraint;
244 lmm_variable_t var = NULL;
245 lmm_element_t elem = NULL;
247 cpu->state_current = SURF_CPU_OFF;
249 while ((var = lmm_get_var_from_cnst(cpu_maxmin_system, cnst, &elem))) {
250 surf_action_t action = lmm_variable_id(var);
252 if (surf_action_state_get(action) == SURF_ACTION_RUNNING ||
253 surf_action_state_get(action) == SURF_ACTION_READY ||
254 surf_action_state_get(action) == SURF_ACTION_NOT_IN_THE_SYSTEM) {
255 action->finish = date;
256 cpu_action_state_set(action, SURF_ACTION_FAILED);
261 CRITICAL0("Unknown event ! \n");
268 static surf_action_t execute(void *cpu, double size)
270 surf_action_cpu_Cas01_t action = NULL;
271 cpu_Cas01_t CPU = cpu;
273 XBT_IN2("(%s,%g)", CPU->name, size);
274 action = xbt_new0(s_surf_action_cpu_Cas01_t, 1);
276 action->generic_action.refcount = 1;
277 action->generic_action.cost = size;
278 action->generic_action.remains = size;
279 action->generic_action.priority = 1.0;
280 action->generic_action.max_duration = NO_MAX_DURATION;
281 action->generic_action.start = surf_get_clock();
282 action->generic_action.finish = -1.0;
283 action->generic_action.model_type = surf_cpu_model;
284 action->suspended = 0; /* Should be useless because of the
285 calloc but it seems to help valgrind... */
287 if (CPU->state_current == SURF_CPU_ON)
288 action->generic_action.state_set =
289 surf_cpu_model->states.running_action_set;
291 action->generic_action.state_set =
292 surf_cpu_model->states.failed_action_set;
294 xbt_swag_insert(action, action->generic_action.state_set);
296 action->variable = lmm_variable_new(cpu_maxmin_system, action,
297 action->generic_action.priority,
299 lmm_expand(cpu_maxmin_system, CPU->constraint, action->variable, 1.0);
301 return (surf_action_t) action;
304 static surf_action_t action_sleep(void *cpu, double duration)
306 surf_action_cpu_Cas01_t action = NULL;
309 duration = MAX(duration, MAXMIN_PRECISION);
311 XBT_IN2("(%s,%g)", ((cpu_Cas01_t) cpu)->name, duration);
312 action = (surf_action_cpu_Cas01_t) execute(cpu, 1.0);
313 action->generic_action.max_duration = duration;
314 action->suspended = 2;
315 if (duration == NO_MAX_DURATION) {
316 /* Move to the *end* of the corresponding action set. This convention
317 is used to speed up update_resource_state */
318 xbt_swag_remove(action, ((surf_action_t) action)->state_set);
319 ((surf_action_t) action)->state_set =
320 running_action_set_that_does_not_need_being_checked;
321 xbt_swag_insert(action, ((surf_action_t) action)->state_set);
324 lmm_update_variable_weight(cpu_maxmin_system, action->variable, 0.0);
326 return (surf_action_t) action;
329 static void action_suspend(surf_action_t action)
331 XBT_IN1("(%p)", action);
332 if (((surf_action_cpu_Cas01_t) action)->suspended != 2) {
333 lmm_update_variable_weight(cpu_maxmin_system,
334 ((surf_action_cpu_Cas01_t) action)->variable,
336 ((surf_action_cpu_Cas01_t) action)->suspended = 1;
341 static void action_resume(surf_action_t action)
343 XBT_IN1("(%p)", action);
344 if (((surf_action_cpu_Cas01_t) action)->suspended != 2) {
345 lmm_update_variable_weight(cpu_maxmin_system,
346 ((surf_action_cpu_Cas01_t) action)->variable,
348 ((surf_action_cpu_Cas01_t) action)->suspended = 0;
353 static int action_is_suspended(surf_action_t action)
355 return (((surf_action_cpu_Cas01_t) action)->suspended == 1);
358 static void action_set_max_duration(surf_action_t action, double duration)
360 XBT_IN2("(%p,%g)", action, duration);
361 action->max_duration = duration;
365 static void action_set_priority(surf_action_t action, double priority)
367 XBT_IN2("(%p,%g)", action, priority);
368 action->priority = priority;
369 lmm_update_variable_weight(cpu_maxmin_system,
370 ((surf_action_cpu_Cas01_t) action)->variable,
376 static e_surf_cpu_state_t get_state(void *cpu)
378 return ((cpu_Cas01_t) cpu)->state_current;
381 static double get_speed(void *cpu, double load)
383 return load * (((cpu_Cas01_t) cpu)->power_scale);
386 static double get_available_speed(void *cpu)
388 /* number between 0 and 1 */
389 return ((cpu_Cas01_t) cpu)->power_current;
392 static xbt_dict_t get_properties(void *cpu)
394 return ((cpu_Cas01_t) cpu)->properties;
397 static void finalize(void)
399 lmm_system_free(cpu_maxmin_system);
400 cpu_maxmin_system = NULL;
402 surf_model_exit(surf_cpu_model);
403 surf_cpu_model = NULL;
405 xbt_swag_free(running_action_set_that_does_not_need_being_checked);
406 running_action_set_that_does_not_need_being_checked = NULL;
409 static void surf_cpu_model_init_internal(void)
411 s_surf_action_t action;
413 surf_cpu_model = surf_model_init();
415 running_action_set_that_does_not_need_being_checked =
416 xbt_swag_new(xbt_swag_offset(action, state_hookup));
418 surf_cpu_model->name = "CPU";
420 surf_cpu_model->action_unref = action_unref;
421 surf_cpu_model->action_cancel = action_cancel;
422 surf_cpu_model->action_state_set = cpu_action_state_set;
424 surf_cpu_model->model_private->resource_used = resource_used;
425 surf_cpu_model->model_private->share_resources = share_resources;
426 surf_cpu_model->model_private->update_actions_state = update_actions_state;
427 surf_cpu_model->model_private->update_resource_state =
428 update_resource_state;
429 surf_cpu_model->model_private->finalize = finalize;
431 surf_cpu_model->suspend = action_suspend;
432 surf_cpu_model->resume = action_resume;
433 surf_cpu_model->is_suspended = action_is_suspended;
434 surf_cpu_model->set_max_duration = action_set_max_duration;
435 surf_cpu_model->set_priority = action_set_priority;
436 surf_cpu_model->extension.cpu.execute = execute;
437 surf_cpu_model->extension.cpu.sleep = action_sleep;
439 surf_cpu_model->extension.cpu.get_state = get_state;
440 surf_cpu_model->extension.cpu.get_speed = get_speed;
441 surf_cpu_model->extension.cpu.get_available_speed = get_available_speed;
442 /*manage the properties of the cpu */
443 surf_cpu_model->get_properties = get_properties;
445 if (!cpu_maxmin_system)
446 cpu_maxmin_system = lmm_system_new();
449 /*********************************************************************/
450 /* Basic sharing model for CPU: that is where all this started... ;) */
451 /*********************************************************************/
452 /* @InProceedings{casanova01simgrid, */
453 /* author = "H. Casanova", */
454 /* booktitle = "Proceedings of the IEEE Symposium on Cluster Computing */
455 /* and the Grid (CCGrid'01)", */
456 /* publisher = "IEEE Computer Society", */
457 /* title = "Simgrid: {A} Toolkit for the Simulation of Application */
461 /* note = "Available at */
462 /* \url{http://grail.sdsc.edu/papers/simgrid_ccgrid01.ps.gz}." */
464 void surf_cpu_model_init_Cas01(const char *filename)
468 surf_cpu_model_init_internal();
469 define_callbacks(filename);
470 xbt_dynar_push(model_list, &surf_cpu_model);