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, *surf_action_cpu_Cas01_t;
12 typedef struct cpu_Cas01 {
13 s_surf_resource_t generic_resource;
16 tmgr_trace_event_t power_event;
17 e_surf_resource_state_t state_current;
18 tmgr_trace_event_t state_event;
19 lmm_constraint_t constraint;
20 } s_cpu_Cas01_t, *cpu_Cas01_t;
22 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_cpu, surf,
23 "Logging specific to the SURF CPU module");
27 surf_model_t surf_cpu_model = NULL;
28 lmm_system_t cpu_maxmin_system = NULL;
31 static xbt_swag_t running_action_set_that_does_not_need_being_checked = NULL;
33 static cpu_Cas01_t cpu_new(char *name, double power_scale,
35 tmgr_trace_t power_trace,
36 e_surf_resource_state_t state_initial,
37 tmgr_trace_t state_trace,
38 xbt_dict_t cpu_properties)
40 cpu_Cas01_t cpu = xbt_new0(s_cpu_Cas01_t, 1);
41 xbt_assert1(!surf_model_resource_by_name(surf_cpu_model, name),
42 "Host '%s' declared several times in the platform file", name);
43 cpu->generic_resource.model = surf_cpu_model;
44 cpu->generic_resource.name = name;
45 cpu->generic_resource.properties = cpu_properties;
46 cpu->power_scale = power_scale;
47 xbt_assert0(cpu->power_scale > 0, "Power has to be >0");
48 cpu->power_current = power_initial;
51 tmgr_history_add_trace(history, power_trace, 0.0, 0, cpu);
53 cpu->state_current = state_initial;
56 tmgr_history_add_trace(history, state_trace, 0.0, 0, cpu);
59 lmm_constraint_new(cpu_maxmin_system, cpu,
60 cpu->power_current * cpu->power_scale);
62 xbt_dict_set(surf_model_resource_set(surf_cpu_model), name, cpu, surf_resource_free);
68 static void parse_cpu_init(void)
70 double power_scale = 0.0;
71 double power_initial = 0.0;
72 tmgr_trace_t power_trace = NULL;
73 e_surf_resource_state_t state_initial = SURF_RESOURCE_OFF;
74 tmgr_trace_t state_trace = NULL;
76 power_scale = get_cpu_power(A_surfxml_host_power);
77 surf_parse_get_double(&power_initial, A_surfxml_host_availability);
78 power_trace = tmgr_trace_new(A_surfxml_host_availability_file);
80 xbt_assert0((A_surfxml_host_state == A_surfxml_host_state_ON) ||
81 (A_surfxml_host_state == A_surfxml_host_state_OFF),
83 if (A_surfxml_host_state == A_surfxml_host_state_ON)
84 state_initial = SURF_RESOURCE_ON;
85 if (A_surfxml_host_state == A_surfxml_host_state_OFF)
86 state_initial = SURF_RESOURCE_OFF;
87 state_trace = tmgr_trace_new(A_surfxml_host_state_file);
89 current_property_set = xbt_dict_new();
90 cpu_new(xbt_strdup(A_surfxml_host_id), power_scale, power_initial,
91 power_trace, state_initial, state_trace, current_property_set);
95 static void add_traces_cpu(void)
97 xbt_dict_cursor_t cursor = NULL;
98 char *trace_name, *elm;
100 static int called = 0;
107 /* connect all traces relative to hosts */
108 xbt_dict_foreach(trace_connect_list_host_avail, cursor, trace_name, elm) {
109 tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
110 cpu_Cas01_t host = surf_model_resource_by_name(surf_cpu_model, elm);
112 xbt_assert1(host, "Host %s undefined", elm);
113 xbt_assert1(trace, "Trace %s undefined", trace_name);
115 host->state_event = tmgr_history_add_trace(history, trace, 0.0, 0, host);
118 xbt_dict_foreach(trace_connect_list_power, cursor, trace_name, elm) {
119 tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
120 cpu_Cas01_t host = surf_model_resource_by_name(surf_cpu_model, elm);
122 xbt_assert1(host, "Host %s undefined", elm);
123 xbt_assert1(trace, "Trace %s undefined", trace_name);
125 host->power_event = tmgr_history_add_trace(history, trace, 0.0, 0, host);
129 static void define_callbacks(const char *file)
131 surf_parse_reset_parser();
132 surfxml_add_callback(STag_surfxml_host_cb_list, parse_cpu_init);
135 static int resource_used(void *resource_id)
137 return lmm_constraint_used(cpu_maxmin_system,
138 ((cpu_Cas01_t) resource_id)->constraint);
141 static int action_unref(surf_action_t action)
144 if (!action->refcount) {
145 xbt_swag_remove(action, action->state_set);
146 if (((surf_action_cpu_Cas01_t) action)->variable)
147 lmm_variable_free(cpu_maxmin_system,
148 ((surf_action_cpu_Cas01_t) action)->variable);
155 static void action_cancel(surf_action_t action)
157 surf_action_state_set(action, SURF_ACTION_FAILED);
161 static void cpu_action_state_set(surf_action_t action,
162 e_surf_action_state_t state)
164 /* if((state==SURF_ACTION_DONE) || (state==SURF_ACTION_FAILED)) */
165 /* if(((surf_action_cpu_Cas01_t)action)->variable) { */
166 /* lmm_variable_disable(cpu_maxmin_system, ((surf_action_cpu_Cas01_t)action)->variable); */
167 /* ((surf_action_cpu_Cas01_t)action)->variable = NULL; */
170 surf_action_state_set(action, state);
174 static double share_resources(double now)
176 s_surf_action_cpu_Cas01_t action;
177 return generic_maxmin_share_resources(surf_cpu_model->states.
179 xbt_swag_offset(action, variable),
180 cpu_maxmin_system, lmm_solve);
183 static void update_actions_state(double now, double delta)
185 surf_action_cpu_Cas01_t action = NULL;
186 surf_action_cpu_Cas01_t next_action = NULL;
187 xbt_swag_t running_actions = surf_cpu_model->states.running_action_set;
189 xbt_swag_foreach_safe(action, next_action, running_actions) {
190 double_update(&(action->generic_action.remains),
191 lmm_variable_getvalue(action->variable) * delta);
192 if (action->generic_action.max_duration != NO_MAX_DURATION)
193 double_update(&(action->generic_action.max_duration), delta);
194 if ((action->generic_action.remains <= 0) &&
195 (lmm_get_variable_weight(action->variable) > 0)) {
196 action->generic_action.finish = surf_get_clock();
197 cpu_action_state_set((surf_action_t) action, SURF_ACTION_DONE);
198 } else if ((action->generic_action.max_duration != NO_MAX_DURATION) &&
199 (action->generic_action.max_duration <= 0)) {
200 action->generic_action.finish = surf_get_clock();
201 cpu_action_state_set((surf_action_t) action, SURF_ACTION_DONE);
208 static void update_resource_state(void *id,
209 tmgr_trace_event_t event_type,
210 double value, double date)
212 cpu_Cas01_t cpu = id;
214 if (event_type == cpu->power_event) {
215 cpu->power_current = value;
216 lmm_update_constraint_bound(cpu_maxmin_system, cpu->constraint,
217 cpu->power_current * cpu->power_scale);
218 } else if (event_type == cpu->state_event) {
220 cpu->state_current = SURF_RESOURCE_ON;
222 lmm_constraint_t cnst = cpu->constraint;
223 lmm_variable_t var = NULL;
224 lmm_element_t elem = NULL;
226 cpu->state_current = SURF_RESOURCE_OFF;
228 while ((var = lmm_get_var_from_cnst(cpu_maxmin_system, cnst, &elem))) {
229 surf_action_t action = lmm_variable_id(var);
231 if (surf_action_state_get(action) == SURF_ACTION_RUNNING ||
232 surf_action_state_get(action) == SURF_ACTION_READY ||
233 surf_action_state_get(action) == SURF_ACTION_NOT_IN_THE_SYSTEM) {
234 action->finish = date;
235 cpu_action_state_set(action, SURF_ACTION_FAILED);
240 CRITICAL0("Unknown event ! \n");
247 static surf_action_t execute(void *cpu, double size)
249 surf_action_cpu_Cas01_t action = NULL;
250 cpu_Cas01_t CPU = cpu;
252 XBT_IN2("(%s,%g)", CPU->generic_resource.name, size);
253 action = surf_action_new(sizeof(s_surf_action_cpu_Cas01_t),size,surf_cpu_model,
254 CPU->state_current != SURF_RESOURCE_ON);
256 action->suspended = 0; /* Should be useless because of the
257 calloc but it seems to help valgrind... */
259 action->variable = lmm_variable_new(cpu_maxmin_system, action,
260 action->generic_action.priority,
262 lmm_expand(cpu_maxmin_system, CPU->constraint, action->variable, 1.0);
264 return (surf_action_t) action;
267 static surf_action_t action_sleep(void *cpu, double duration)
269 surf_action_cpu_Cas01_t action = NULL;
272 duration = MAX(duration, MAXMIN_PRECISION);
274 XBT_IN2("(%s,%g)", ((cpu_Cas01_t) cpu)->generic_resource.name, duration);
275 action = (surf_action_cpu_Cas01_t) execute(cpu, 1.0);
276 action->generic_action.max_duration = duration;
277 action->suspended = 2;
278 if (duration == NO_MAX_DURATION) {
279 /* Move to the *end* of the corresponding action set. This convention
280 is used to speed up update_resource_state */
281 xbt_swag_remove(action, ((surf_action_t) action)->state_set);
282 ((surf_action_t) action)->state_set =
283 running_action_set_that_does_not_need_being_checked;
284 xbt_swag_insert(action, ((surf_action_t) action)->state_set);
287 lmm_update_variable_weight(cpu_maxmin_system, action->variable, 0.0);
289 return (surf_action_t) action;
292 static void action_suspend(surf_action_t action)
294 XBT_IN1("(%p)", action);
295 if (((surf_action_cpu_Cas01_t) action)->suspended != 2) {
296 lmm_update_variable_weight(cpu_maxmin_system,
297 ((surf_action_cpu_Cas01_t) action)->variable,
299 ((surf_action_cpu_Cas01_t) action)->suspended = 1;
304 static void action_resume(surf_action_t action)
306 XBT_IN1("(%p)", action);
307 if (((surf_action_cpu_Cas01_t) action)->suspended != 2) {
308 lmm_update_variable_weight(cpu_maxmin_system,
309 ((surf_action_cpu_Cas01_t) action)->variable,
311 ((surf_action_cpu_Cas01_t) action)->suspended = 0;
316 static int action_is_suspended(surf_action_t action)
318 return (((surf_action_cpu_Cas01_t) action)->suspended == 1);
321 static void action_set_max_duration(surf_action_t action, double duration)
323 XBT_IN2("(%p,%g)", action, duration);
324 action->max_duration = duration;
328 static void action_set_priority(surf_action_t action, double priority)
330 XBT_IN2("(%p,%g)", action, priority);
331 action->priority = priority;
332 lmm_update_variable_weight(cpu_maxmin_system,
333 ((surf_action_cpu_Cas01_t) action)->variable,
339 static e_surf_resource_state_t get_state(void *cpu)
341 return ((cpu_Cas01_t) cpu)->state_current;
344 static double get_speed(void *cpu, double load)
346 return load * (((cpu_Cas01_t) cpu)->power_scale);
349 static double get_available_speed(void *cpu)
351 /* number between 0 and 1 */
352 return ((cpu_Cas01_t) cpu)->power_current;
355 static void finalize(void)
357 lmm_system_free(cpu_maxmin_system);
358 cpu_maxmin_system = NULL;
360 surf_model_exit(surf_cpu_model);
361 surf_cpu_model = NULL;
363 xbt_swag_free(running_action_set_that_does_not_need_being_checked);
364 running_action_set_that_does_not_need_being_checked = NULL;
367 static void surf_cpu_model_init_internal(void)
369 s_surf_action_t action;
371 surf_cpu_model = surf_model_init();
373 running_action_set_that_does_not_need_being_checked =
374 xbt_swag_new(xbt_swag_offset(action, state_hookup));
376 surf_cpu_model->name = "CPU";
378 surf_cpu_model->action_unref = action_unref;
379 surf_cpu_model->action_cancel = action_cancel;
380 surf_cpu_model->action_state_set = cpu_action_state_set;
382 surf_cpu_model->model_private->resource_used = resource_used;
383 surf_cpu_model->model_private->share_resources = share_resources;
384 surf_cpu_model->model_private->update_actions_state = update_actions_state;
385 surf_cpu_model->model_private->update_resource_state =
386 update_resource_state;
387 surf_cpu_model->model_private->finalize = finalize;
389 surf_cpu_model->suspend = action_suspend;
390 surf_cpu_model->resume = action_resume;
391 surf_cpu_model->is_suspended = action_is_suspended;
392 surf_cpu_model->set_max_duration = action_set_max_duration;
393 surf_cpu_model->set_priority = action_set_priority;
394 surf_cpu_model->extension.cpu.execute = execute;
395 surf_cpu_model->extension.cpu.sleep = action_sleep;
397 surf_cpu_model->extension.cpu.get_state = get_state;
398 surf_cpu_model->extension.cpu.get_speed = get_speed;
399 surf_cpu_model->extension.cpu.get_available_speed = get_available_speed;
401 if (!cpu_maxmin_system)
402 cpu_maxmin_system = lmm_system_new();
405 /*********************************************************************/
406 /* Basic sharing model for CPU: that is where all this started... ;) */
407 /*********************************************************************/
408 /* @InProceedings{casanova01simgrid, */
409 /* author = "H. Casanova", */
410 /* booktitle = "Proceedings of the IEEE Symposium on Cluster Computing */
411 /* and the Grid (CCGrid'01)", */
412 /* publisher = "IEEE Computer Society", */
413 /* title = "Simgrid: {A} Toolkit for the Simulation of Application */
417 /* note = "Available at */
418 /* \url{http://grail.sdsc.edu/papers/simgrid_ccgrid01.ps.gz}." */
420 void surf_cpu_model_init_Cas01(const char *filename)
424 surf_cpu_model_init_internal();
425 define_callbacks(filename);
426 xbt_dynar_push(model_list, &surf_cpu_model);