1 /* Authors: Arnaud Legrand */
3 /* This program is free software; you can redistribute it and/or modify it
4 under the terms of the license (GNU LGPL) which comes with this package. */
6 #include "cpu_private.h"
9 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(cpu, surf ,"Logging specific to the SURF CPU module");
11 surf_cpu_resource_t surf_cpu_resource = NULL;
13 static xbt_dict_t cpu_set = NULL;
15 /* power_scale is the basic power of the cpu when the cpu is
16 completely available. initial_power is therefore expected to be
17 comprised between 0.0 and 1.0, just as the values of power_trace.
18 state_trace values mean SURF_CPU_ON if >0 and SURF_CPU_OFF
22 static void cpu_free(void *CPU){
25 /* lmm_constraint_free(maxmin_system,cpu->constraint); */
26 /* Clean somewhere else ! */
31 static void *cpu_new(const char *name, xbt_maxmin_float_t power_scale,
32 xbt_maxmin_float_t initial_power, tmgr_trace_t power_trace,
33 e_surf_cpu_state_t initial_state, tmgr_trace_t state_trace)
35 cpu_t cpu = xbt_new0(s_cpu_t,1);
37 cpu->resource = (surf_resource_t) surf_cpu_resource;
39 cpu->power_scale = power_scale;
40 cpu->current_power = initial_power;
41 /* cpu->power_trace = power_trace; */
43 cpu->power_event = tmgr_history_add_trace(history, power_trace, 0.0, 0, cpu);
45 cpu->current_state = initial_state;
46 /* cpu->state_trace = state_trace; */
48 cpu->state_event = tmgr_history_add_trace(history, state_trace, 0.0, 0, cpu);
50 cpu->constraint = lmm_constraint_new(maxmin_system, cpu, cpu->current_power * cpu->power_scale);
52 xbt_dict_set(cpu_set, name, cpu, cpu_free);
58 Semantic: name scale initial power initial state
59 power trace state trace
61 Token: TOKEN_WORD TOKEN_WORD TOKEN_WORD TOKEN_WORD TOKEN_WORD TOKEN_WORD
62 Type: string float float string ON/OFF string
65 static void parse_host(void)
69 xbt_maxmin_float_t power_scale = 0.0;
70 xbt_maxmin_float_t initial_power = 0.0;
71 tmgr_trace_t power_trace = NULL;;
72 e_surf_cpu_state_t initial_state = SURF_CPU_OFF;
73 tmgr_trace_t state_trace = NULL;
75 name=xbt_strdup(surf_parse_text);
77 token=surf_parse(); /* power_scale */
78 xbt_assert1((token==TOKEN_WORD),"Parse error line %d",line_pos);
79 xbt_assert2((sscanf(surf_parse_text,XBT_MAXMIN_FLOAT_T, &power_scale)==1),
80 "Parse error line %d : %s not a number",line_pos,surf_parse_text);
82 token=surf_parse(); /* initial_power */
83 xbt_assert1((token==TOKEN_WORD),"Parse error line %d",line_pos);
84 xbt_assert2((sscanf(surf_parse_text,XBT_MAXMIN_FLOAT_T, &initial_power)==1),
85 "Parse error line %d : %s not a number",line_pos,surf_parse_text);
87 token=surf_parse(); /* power_trace */
88 xbt_assert1((token==TOKEN_WORD),"Parse error line %d",line_pos);
89 if(strcmp(surf_parse_text,"")==0) power_trace = NULL;
90 else power_trace = tmgr_trace_new(surf_parse_text);
92 token=surf_parse(); /* initial_state */
93 xbt_assert1((token==TOKEN_WORD),"Parse error line %d",line_pos);
94 if(strcmp(surf_parse_text,"ON")==0) initial_state = SURF_CPU_ON;
95 else if(strcmp(surf_parse_text,"OFF")==0) initial_state = SURF_CPU_OFF;
97 CRITICAL2("Invalid cpu state (line %d): %s neq ON or OFF\n",line_pos,
102 token=surf_parse(); /* state_trace */
103 xbt_assert1((token==TOKEN_WORD),"Parse error line %d",line_pos);
104 if(strcmp(surf_parse_text,"")==0) state_trace = NULL;
105 else state_trace = tmgr_trace_new(surf_parse_text);
107 cpu_new(name, power_scale, initial_power, power_trace, initial_state, state_trace);
110 static void parse_file(const char *file)
112 e_surf_token_t token;
114 find_section(file,"CPU");
119 if(token==TOKEN_END_SECTION) break;
120 if(token==TOKEN_NEWLINE) continue;
122 if(token==TOKEN_WORD) parse_host();
124 CRITICAL1("Parse error line %d\n",line_pos);
129 close_section("CPU");
132 static void *name_service(const char *name)
136 xbt_dict_get(cpu_set, name, &cpu);
141 static const char *get_resource_name(void *resource_id)
143 return ((cpu_t) resource_id)->name;
146 static int resource_used(void *resource_id)
148 return lmm_constraint_used(maxmin_system, ((cpu_t)resource_id)->constraint);
151 static void action_free(surf_action_t action)
153 surf_action_cpu_t Action = (surf_action_cpu_t) action;
155 xbt_swag_remove(action,action->state_set);
156 lmm_variable_free(maxmin_system,Action->variable);
162 static void action_cancel(surf_action_t action)
167 static void action_recycle(surf_action_t action)
172 static void action_change_state(surf_action_t action, e_surf_action_state_t state)
174 surf_action_change_state(action, state);
178 static xbt_heap_float_t share_resources(xbt_heap_float_t now)
180 surf_action_cpu_t action = NULL;
181 xbt_swag_t running_actions= surf_cpu_resource->common_public->states.running_action_set;
182 xbt_maxmin_float_t min = -1;
183 xbt_maxmin_float_t value = -1;
184 lmm_solve(maxmin_system);
186 action = xbt_swag_getFirst(running_actions);
187 if(!action) return 0.0;
188 value = lmm_variable_getvalue(action->variable);
189 min = action->generic_action.remains / value ;
191 xbt_swag_foreach(action,running_actions) {
192 value = action->generic_action.remains /
193 lmm_variable_getvalue(action->variable);
194 if(value<min) min=value;
200 static void update_actions_state(xbt_heap_float_t now,
201 xbt_heap_float_t delta)
203 surf_action_cpu_t action = NULL;
204 surf_action_cpu_t next_action = NULL;
205 xbt_swag_t running_actions= surf_cpu_resource->common_public->states.running_action_set;
206 xbt_swag_t failed_actions= surf_cpu_resource->common_public->states.failed_action_set;
208 xbt_swag_foreach_safe(action, next_action, running_actions) {
209 action->generic_action.remains -=
210 lmm_variable_getvalue(action->variable)*delta;
211 /* if(action->generic_action.remains<.00001) action->generic_action.remains=0; */
212 if(action->generic_action.remains<=0) {
213 action_change_state((surf_action_t)action, SURF_ACTION_DONE);
214 } else { /* Need to check that none of the resource has failed*/
215 lmm_constraint_t cnst = NULL;
219 while((cnst=lmm_get_cnst_from_var(maxmin_system, action->variable, i++))) {
220 cpu = lmm_constraint_id(cnst);
221 if(cpu->current_state==SURF_CPU_OFF) {
222 action_change_state((surf_action_t) action, SURF_ACTION_FAILED);
229 xbt_swag_foreach_safe(action, next_action, failed_actions) {
230 lmm_variable_disable(maxmin_system, action->variable);
236 static void update_resource_state(void *id,
237 tmgr_trace_event_t event_type,
238 xbt_maxmin_float_t value)
242 printf("[" XBT_HEAP_FLOAT_T "] Asking to update \"%s\" with value " XBT_MAXMIN_FLOAT_T " for event %p\n",
243 surf_get_clock(), cpu->name, value, event_type);
245 if(event_type==cpu->power_event) {
246 cpu->current_power = value;
247 lmm_update_constraint_bound(cpu->constraint,cpu->current_power * cpu->power_scale);
248 } else if (event_type==cpu->state_event) {
249 if(value>0) cpu->current_state = SURF_CPU_ON;
250 else cpu->current_state = SURF_CPU_OFF;
251 } else {CRITICAL0("Unknown event ! \n");xbt_abort();}
256 static surf_action_t execute(void *cpu, xbt_maxmin_float_t size)
258 surf_action_cpu_t action = NULL;
261 action=xbt_new0(s_surf_action_cpu_t,1);
263 action->generic_action.cost=size;
264 action->generic_action.remains=size;
265 action->generic_action.start=-1.0;
266 action->generic_action.finish=-1.0;
267 action->generic_action.callback=cpu;
268 action->generic_action.resource_type=(surf_resource_t)surf_cpu_resource;
270 if(CPU->current_state==SURF_CPU_ON)
271 action->generic_action.state_set=surf_cpu_resource->common_public->states.running_action_set;
273 action->generic_action.state_set=surf_cpu_resource->common_public->states.failed_action_set;
274 xbt_swag_insert(action,action->generic_action.state_set);
276 action->variable = lmm_variable_new(maxmin_system, action, 1.0, -1.0, 1);
277 lmm_expand(maxmin_system, ((cpu_t)cpu)->constraint, action->variable, 1.0);
279 return (surf_action_t) action;
282 static e_surf_cpu_state_t get_state(void *cpu)
287 static void finalize(void)
289 xbt_dict_free(&cpu_set);
290 xbt_swag_free(surf_cpu_resource->common_public->states.ready_action_set);
291 xbt_swag_free(surf_cpu_resource->common_public->states.running_action_set);
292 xbt_swag_free(surf_cpu_resource->common_public->states.failed_action_set);
293 xbt_swag_free(surf_cpu_resource->common_public->states.done_action_set);
294 xbt_free(surf_cpu_resource->common_public);
295 xbt_free(surf_cpu_resource->common_private);
296 xbt_free(surf_cpu_resource->extension_public);
298 xbt_free(surf_cpu_resource);
299 surf_cpu_resource = NULL;
302 static void surf_cpu_resource_init_internal(void)
304 s_surf_action_t action;
306 surf_cpu_resource = xbt_new0(s_surf_cpu_resource_t,1);
308 surf_cpu_resource->common_private = xbt_new0(s_surf_resource_private_t,1);
309 surf_cpu_resource->common_public = xbt_new0(s_surf_resource_public_t,1);
310 /* surf_cpu_resource->extension_private = xbt_new0(s_surf_cpu_resource_extension_private_t,1); */
311 surf_cpu_resource->extension_public = xbt_new0(s_surf_cpu_resource_extension_public_t,1);
313 surf_cpu_resource->common_public->states.ready_action_set=
314 xbt_swag_new(xbt_swag_offset(action,state_hookup));
315 surf_cpu_resource->common_public->states.running_action_set=
316 xbt_swag_new(xbt_swag_offset(action,state_hookup));
317 surf_cpu_resource->common_public->states.failed_action_set=
318 xbt_swag_new(xbt_swag_offset(action,state_hookup));
319 surf_cpu_resource->common_public->states.done_action_set=
320 xbt_swag_new(xbt_swag_offset(action,state_hookup));
322 surf_cpu_resource->common_public->name_service = name_service;
323 surf_cpu_resource->common_public->get_resource_name = get_resource_name;
324 surf_cpu_resource->common_public->resource_used = resource_used;
325 surf_cpu_resource->common_public->action_get_state=surf_action_get_state;
326 surf_cpu_resource->common_public->action_free = action_free;
327 surf_cpu_resource->common_public->action_cancel = action_cancel;
328 surf_cpu_resource->common_public->action_recycle = action_recycle;
329 surf_cpu_resource->common_public->action_change_state = action_change_state;
331 surf_cpu_resource->common_private->share_resources = share_resources;
332 surf_cpu_resource->common_private->update_actions_state = update_actions_state;
333 surf_cpu_resource->common_private->update_resource_state = update_resource_state;
334 surf_cpu_resource->common_private->finalize = finalize;
336 surf_cpu_resource->extension_public->execute = execute;
337 surf_cpu_resource->extension_public->get_state = get_state;
339 cpu_set = xbt_dict_new();
341 xbt_assert0(maxmin_system,"surf_init has to be called first!");
344 void surf_cpu_resource_init(const char* filename)
346 surf_cpu_resource_init_internal();
347 parse_file(filename);
348 xbt_dynar_push(resource_list, &surf_cpu_resource);