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 s_surf_action_lmm_t s_surf_action_cpu_Cas01_t,
11 *surf_action_cpu_Cas01_t;
13 typedef struct cpu_Cas01 {
14 s_surf_resource_t generic_resource;
17 tmgr_trace_event_t power_event;
18 e_surf_resource_state_t state_current;
19 tmgr_trace_event_t state_event;
20 lmm_constraint_t constraint;
21 } s_cpu_Cas01_t, *cpu_Cas01_t;
23 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_cpu, surf,
24 "Logging specific to the SURF CPU module");
28 surf_model_t surf_cpu_model = NULL;
29 lmm_system_t cpu_maxmin_system = NULL;
32 static xbt_swag_t running_action_set_that_does_not_need_being_checked = NULL;
34 static cpu_Cas01_t cpu_new(char *name, double power_peak,
36 tmgr_trace_t power_trace,
37 e_surf_resource_state_t state_initial,
38 tmgr_trace_t state_trace,
39 xbt_dict_t cpu_properties)
41 cpu_Cas01_t cpu = xbt_new0(s_cpu_Cas01_t, 1);
42 xbt_assert1(!surf_model_resource_by_name(surf_cpu_model, name),
43 "Host '%s' declared several times in the platform file", name);
44 cpu->generic_resource.model = surf_cpu_model;
45 cpu->generic_resource.name = name;
46 cpu->generic_resource.properties = cpu_properties;
47 cpu->power_peak = power_peak;
48 xbt_assert0(cpu->power_peak > 0, "Power has to be >0");
49 cpu->power_scale = power_scale;
52 tmgr_history_add_trace(history, power_trace, 0.0, 0, cpu);
54 cpu->state_current = state_initial;
57 tmgr_history_add_trace(history, state_trace, 0.0, 0, cpu);
60 lmm_constraint_new(cpu_maxmin_system, cpu,
61 cpu->power_scale * cpu->power_peak);
63 xbt_dict_set(surf_model_resource_set(surf_cpu_model), name, cpu,
70 static void parse_cpu_init(void)
72 double power_peak = 0.0;
73 double power_scale = 0.0;
74 tmgr_trace_t power_trace = NULL;
75 e_surf_resource_state_t state_initial = SURF_RESOURCE_OFF;
76 tmgr_trace_t state_trace = NULL;
78 power_peak = get_cpu_power(A_surfxml_host_power);
79 surf_parse_get_double(&power_scale, A_surfxml_host_availability);
80 power_trace = tmgr_trace_new(A_surfxml_host_availability_file);
82 xbt_assert0((A_surfxml_host_state == A_surfxml_host_state_ON) ||
83 (A_surfxml_host_state == A_surfxml_host_state_OFF),
85 if (A_surfxml_host_state == A_surfxml_host_state_ON)
86 state_initial = SURF_RESOURCE_ON;
87 if (A_surfxml_host_state == A_surfxml_host_state_OFF)
88 state_initial = SURF_RESOURCE_OFF;
89 state_trace = tmgr_trace_new(A_surfxml_host_state_file);
91 current_property_set = xbt_dict_new();
92 cpu_new(xbt_strdup(A_surfxml_host_id), power_peak, power_scale,
93 power_trace, state_initial, state_trace, current_property_set);
97 static void add_traces_cpu(void)
99 xbt_dict_cursor_t cursor = NULL;
100 char *trace_name, *elm;
102 static int called = 0;
109 /* connect all traces relative to hosts */
110 xbt_dict_foreach(trace_connect_list_host_avail, cursor, trace_name, elm) {
111 tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
112 cpu_Cas01_t host = surf_model_resource_by_name(surf_cpu_model, elm);
114 xbt_assert1(host, "Host %s undefined", elm);
115 xbt_assert1(trace, "Trace %s undefined", trace_name);
117 host->state_event = tmgr_history_add_trace(history, trace, 0.0, 0, host);
120 xbt_dict_foreach(trace_connect_list_power, cursor, trace_name, elm) {
121 tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
122 cpu_Cas01_t host = surf_model_resource_by_name(surf_cpu_model, elm);
124 xbt_assert1(host, "Host %s undefined", elm);
125 xbt_assert1(trace, "Trace %s undefined", trace_name);
127 host->power_event = tmgr_history_add_trace(history, trace, 0.0, 0, host);
131 static void define_callbacks(const char *file)
133 surf_parse_reset_parser();
134 surfxml_add_callback(STag_surfxml_host_cb_list, parse_cpu_init);
135 surfxml_add_callback(ETag_surfxml_platform_cb_list, &add_traces_cpu);
138 static int resource_used(void *resource_id)
140 return lmm_constraint_used(cpu_maxmin_system,
141 ((cpu_Cas01_t) resource_id)->constraint);
144 static int action_unref(surf_action_t action)
147 if (!action->refcount) {
148 xbt_swag_remove(action, action->state_set);
149 if (((surf_action_cpu_Cas01_t) action)->variable)
150 lmm_variable_free(cpu_maxmin_system,
151 ((surf_action_cpu_Cas01_t) action)->variable);
158 static void action_cancel(surf_action_t action)
160 surf_action_state_set(action, SURF_ACTION_FAILED);
164 static void cpu_action_state_set(surf_action_t action,
165 e_surf_action_state_t state)
167 /* if((state==SURF_ACTION_DONE) || (state==SURF_ACTION_FAILED)) */
168 /* if(((surf_action_cpu_Cas01_t)action)->variable) { */
169 /* lmm_variable_disable(cpu_maxmin_system, ((surf_action_cpu_Cas01_t)action)->variable); */
170 /* ((surf_action_cpu_Cas01_t)action)->variable = NULL; */
173 surf_action_state_set(action, state);
177 static double share_resources(double now)
179 s_surf_action_cpu_Cas01_t action;
180 return generic_maxmin_share_resources(surf_cpu_model->
181 states.running_action_set,
182 xbt_swag_offset(action, variable),
183 cpu_maxmin_system, lmm_solve);
186 static void update_actions_state(double now, double delta)
188 surf_action_cpu_Cas01_t action = NULL;
189 surf_action_cpu_Cas01_t next_action = NULL;
190 xbt_swag_t running_actions = surf_cpu_model->states.running_action_set;
192 xbt_swag_foreach_safe(action, next_action, running_actions) {
193 double_update(&(action->generic_action.remains),
194 lmm_variable_getvalue(action->variable) * delta);
195 if (action->generic_action.max_duration != NO_MAX_DURATION)
196 double_update(&(action->generic_action.max_duration), delta);
197 if ((action->generic_action.remains <= 0) &&
198 (lmm_get_variable_weight(action->variable) > 0)) {
199 action->generic_action.finish = surf_get_clock();
200 cpu_action_state_set((surf_action_t) action, SURF_ACTION_DONE);
201 } else if ((action->generic_action.max_duration != NO_MAX_DURATION) &&
202 (action->generic_action.max_duration <= 0)) {
203 action->generic_action.finish = surf_get_clock();
204 cpu_action_state_set((surf_action_t) action, SURF_ACTION_DONE);
211 static void update_resource_state(void *id,
212 tmgr_trace_event_t event_type,
213 double value, double date)
215 cpu_Cas01_t cpu = id;
217 if (event_type == cpu->power_event) {
218 cpu->power_scale = value;
219 lmm_update_constraint_bound(cpu_maxmin_system, cpu->constraint,
220 cpu->power_scale * cpu->power_peak);
221 if (tmgr_trace_event_free(event_type))
222 cpu->power_event = NULL;
223 } else if (event_type == cpu->state_event) {
225 cpu->state_current = SURF_RESOURCE_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_RESOURCE_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_state_get(action) == SURF_ACTION_RUNNING ||
237 surf_action_state_get(action) == SURF_ACTION_READY ||
238 surf_action_state_get(action) == SURF_ACTION_NOT_IN_THE_SYSTEM) {
239 action->finish = date;
240 cpu_action_state_set(action, SURF_ACTION_FAILED);
244 if (tmgr_trace_event_free(event_type))
245 cpu->state_event = NULL;
247 CRITICAL0("Unknown event ! \n");
254 static surf_action_t execute(void *cpu, double size)
256 surf_action_cpu_Cas01_t action = NULL;
257 cpu_Cas01_t CPU = cpu;
259 XBT_IN2("(%s,%g)", surf_resource_name(CPU), size);
261 surf_action_new(sizeof(s_surf_action_cpu_Cas01_t), size, surf_cpu_model,
262 CPU->state_current != SURF_RESOURCE_ON);
264 action->suspended = 0; /* Should be useless because of the
265 calloc but it seems to help valgrind... */
267 action->variable = lmm_variable_new(cpu_maxmin_system, action,
268 action->generic_action.priority,
270 lmm_expand(cpu_maxmin_system, CPU->constraint, action->variable, 1.0);
272 return (surf_action_t) action;
275 static surf_action_t action_sleep(void *cpu, double duration)
277 surf_action_cpu_Cas01_t action = NULL;
280 duration = MAX(duration, MAXMIN_PRECISION);
282 XBT_IN2("(%s,%g)", surf_resource_name(cpu), duration);
283 action = (surf_action_cpu_Cas01_t) execute(cpu, 1.0);
284 action->generic_action.max_duration = duration;
285 action->suspended = 2;
286 if (duration == NO_MAX_DURATION) {
287 /* Move to the *end* of the corresponding action set. This convention
288 is used to speed up update_resource_state */
289 xbt_swag_remove(action, ((surf_action_t) action)->state_set);
290 ((surf_action_t) action)->state_set =
291 running_action_set_that_does_not_need_being_checked;
292 xbt_swag_insert(action, ((surf_action_t) action)->state_set);
295 lmm_update_variable_weight(cpu_maxmin_system, action->variable, 0.0);
297 return (surf_action_t) action;
300 static void action_suspend(surf_action_t action)
302 XBT_IN1("(%p)", action);
303 if (((surf_action_cpu_Cas01_t) action)->suspended != 2) {
304 lmm_update_variable_weight(cpu_maxmin_system,
305 ((surf_action_cpu_Cas01_t) action)->variable,
307 ((surf_action_cpu_Cas01_t) action)->suspended = 1;
312 static void action_resume(surf_action_t action)
314 XBT_IN1("(%p)", action);
315 if (((surf_action_cpu_Cas01_t) action)->suspended != 2) {
316 lmm_update_variable_weight(cpu_maxmin_system,
317 ((surf_action_cpu_Cas01_t) action)->variable,
319 ((surf_action_cpu_Cas01_t) action)->suspended = 0;
324 static int action_is_suspended(surf_action_t action)
326 return (((surf_action_cpu_Cas01_t) action)->suspended == 1);
329 static void action_set_max_duration(surf_action_t action, double duration)
331 XBT_IN2("(%p,%g)", action, duration);
332 action->max_duration = duration;
336 static void action_set_priority(surf_action_t action, double priority)
338 XBT_IN2("(%p,%g)", action, priority);
339 action->priority = priority;
340 lmm_update_variable_weight(cpu_maxmin_system,
341 ((surf_action_cpu_Cas01_t) action)->variable,
347 static double action_get_remains(surf_action_t action)
349 XBT_IN1("(%p)", action);
350 return action->remains;
354 static e_surf_resource_state_t get_state(void *cpu)
356 return ((cpu_Cas01_t) cpu)->state_current;
359 static double get_speed(void *cpu, double load)
361 return load * (((cpu_Cas01_t) cpu)->power_peak);
364 static double get_available_speed(void *cpu)
366 /* number between 0 and 1 */
367 return ((cpu_Cas01_t) cpu)->power_scale;
370 static void finalize(void)
372 lmm_system_free(cpu_maxmin_system);
373 cpu_maxmin_system = NULL;
375 surf_model_exit(surf_cpu_model);
376 surf_cpu_model = NULL;
378 xbt_swag_free(running_action_set_that_does_not_need_being_checked);
379 running_action_set_that_does_not_need_being_checked = NULL;
382 static void surf_cpu_model_init_internal(void)
384 s_surf_action_t action;
386 surf_cpu_model = surf_model_init();
388 running_action_set_that_does_not_need_being_checked =
389 xbt_swag_new(xbt_swag_offset(action, state_hookup));
391 surf_cpu_model->name = "CPU";
393 surf_cpu_model->action_unref = action_unref;
394 surf_cpu_model->action_cancel = action_cancel;
395 surf_cpu_model->action_state_set = cpu_action_state_set;
397 surf_cpu_model->model_private->resource_used = resource_used;
398 surf_cpu_model->model_private->share_resources = share_resources;
399 surf_cpu_model->model_private->update_actions_state = update_actions_state;
400 surf_cpu_model->model_private->update_resource_state =
401 update_resource_state;
402 surf_cpu_model->model_private->finalize = finalize;
404 surf_cpu_model->suspend = action_suspend;
405 surf_cpu_model->resume = action_resume;
406 surf_cpu_model->is_suspended = action_is_suspended;
407 surf_cpu_model->set_max_duration = action_set_max_duration;
408 surf_cpu_model->set_priority = action_set_priority;
409 surf_cpu_model->get_remains = action_get_remains;
411 surf_cpu_model->extension.cpu.execute = execute;
412 surf_cpu_model->extension.cpu.sleep = action_sleep;
414 surf_cpu_model->extension.cpu.get_state = get_state;
415 surf_cpu_model->extension.cpu.get_speed = get_speed;
416 surf_cpu_model->extension.cpu.get_available_speed = get_available_speed;
418 if (!cpu_maxmin_system)
419 cpu_maxmin_system = lmm_system_new();
422 /*********************************************************************/
423 /* Basic sharing model for CPU: that is where all this started... ;) */
424 /*********************************************************************/
425 /* @InProceedings{casanova01simgrid, */
426 /* author = "H. Casanova", */
427 /* booktitle = "Proceedings of the IEEE Symposium on Cluster Computing */
428 /* and the Grid (CCGrid'01)", */
429 /* publisher = "IEEE Computer Society", */
430 /* title = "Simgrid: {A} Toolkit for the Simulation of Application */
434 /* note = "Available at */
435 /* \url{http://grail.sdsc.edu/papers/simgrid_ccgrid01.ps.gz}." */
437 void surf_cpu_model_init_Cas01(const char *filename)
441 surf_cpu_model_init_internal();
442 define_callbacks(filename);
443 xbt_dynar_push(model_list, &surf_cpu_model);