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);
57 static void find_section(const char* file, const char* section_name)
62 surf_parse_open(file);
64 while((token=surf_parse())) {
65 if(token!=TOKEN_BEGIN_SECTION) continue;
68 xbt_assert1((token==TOKEN_WORD),"Parse error line %d",line_pos);
69 if(strcmp(surf_parse_text,section_name)==0) found=1;
72 xbt_assert1((token==TOKEN_CLOSURE),"Parse error line %d",line_pos);
77 CRITICAL2("Could not find %s section in %s\n",section_name,file);
80 static void close_section(const char* section_name)
85 xbt_assert1((token==TOKEN_WORD),"Parse error line %d",line_pos);
86 xbt_assert1((strcmp(surf_parse_text,"CPU")==0),
87 "Closing section does not match the opening one (%s).",
91 xbt_assert1((token==TOKEN_CLOSURE),"Parse error line %d",line_pos);
97 Semantic: name scale initial power initial state
98 power trace state trace
100 Token: TOKEN_WORD TOKEN_WORD TOKEN_WORD TOKEN_WORD TOKEN_WORD TOKEN_WORD
101 Type: string float float string ON/OFF string
104 static void parse_host(void)
106 e_surf_token_t token;
108 xbt_maxmin_float_t power_scale = 0.0;
109 xbt_maxmin_float_t initial_power = 0.0;
110 tmgr_trace_t power_trace = NULL;;
111 e_surf_cpu_state_t initial_state = SURF_CPU_OFF;
112 tmgr_trace_t state_trace = NULL;
114 name=xbt_strdup(surf_parse_text);
116 token=surf_parse(); /* power_scale */
117 xbt_assert1((token==TOKEN_WORD),"Parse error line %d",line_pos);
118 xbt_assert2((sscanf(surf_parse_text,XBT_MAXMIN_FLOAT_T, &power_scale)==1),
119 "Parse error line %d : %s not a number",line_pos,surf_parse_text);
121 token=surf_parse(); /* initial_power */
122 xbt_assert1((token==TOKEN_WORD),"Parse error line %d",line_pos);
123 xbt_assert2((sscanf(surf_parse_text,XBT_MAXMIN_FLOAT_T, &initial_power)==1),
124 "Parse error line %d : %s not a number",line_pos,surf_parse_text);
126 token=surf_parse(); /* power_trace */
127 xbt_assert1((token==TOKEN_WORD),"Parse error line %d",line_pos);
128 if(strcmp(surf_parse_text,"")==0) power_trace = NULL;
129 else power_trace = tmgr_trace_new(surf_parse_text);
131 token=surf_parse(); /* initial_state */
132 xbt_assert1((token==TOKEN_WORD),"Parse error line %d",line_pos);
133 if(strcmp(surf_parse_text,"ON")==0) initial_state = SURF_CPU_ON;
134 else if(strcmp(surf_parse_text,"OFF")==0) initial_state = SURF_CPU_OFF;
135 else CRITICAL2("Invalid cpu state (line %d): %s neq ON or OFF\n",line_pos,
138 token=surf_parse(); /* state_trace */
139 xbt_assert1((token==TOKEN_WORD),"Parse error line %d",line_pos);
140 if(strcmp(surf_parse_text,"")==0) state_trace = NULL;
141 else state_trace = tmgr_trace_new(surf_parse_text);
143 cpu_new(name, power_scale, initial_power, power_trace, initial_state, state_trace);
146 static void parse_file(const char *file)
148 e_surf_token_t token;
150 find_section(file,"CPU");
155 if(token==TOKEN_END_SECTION) break;
156 if(token==TOKEN_NEWLINE) continue;
158 if(token==TOKEN_WORD) parse_host();
159 else CRITICAL1("Parse error line %d\n",line_pos);
162 close_section("CPU");
165 static void *name_service(const char *name)
169 xbt_dict_get(cpu_set, name, &cpu);
174 static const char *get_resource_name(void *resource_id)
176 return ((cpu_t) resource_id)->name;
179 static int resource_used(void *resource_id)
181 return lmm_constraint_used(maxmin_system, ((cpu_t)resource_id)->constraint);
184 static void action_free(surf_action_t action)
186 surf_action_cpu_t Action = (surf_action_cpu_t) action;
188 xbt_swag_remove(action,action->state_set);
189 lmm_variable_free(maxmin_system,Action->variable);
195 static void action_cancel(surf_action_t action)
200 static void action_recycle(surf_action_t action)
205 static void action_change_state(surf_action_t action, e_surf_action_state_t state)
207 surf_action_change_state(action, state);
211 static xbt_heap_float_t share_resources(xbt_heap_float_t now)
213 surf_action_cpu_t action = NULL;
214 xbt_swag_t running_actions= surf_cpu_resource->common_public->states.running_action_set;
215 xbt_maxmin_float_t min = -1;
216 xbt_maxmin_float_t value = -1;
217 lmm_solve(maxmin_system);
219 action = xbt_swag_getFirst(running_actions);
220 if(!action) return 0.0;
221 value = lmm_variable_getvalue(action->variable);
222 min = action->generic_action.remains / value ;
224 xbt_swag_foreach(action,running_actions) {
225 value = action->generic_action.remains /
226 lmm_variable_getvalue(action->variable);
227 if(value<min) min=value;
233 static void update_actions_state(xbt_heap_float_t now,
234 xbt_heap_float_t delta)
236 surf_action_cpu_t action = NULL;
237 surf_action_cpu_t next_action = NULL;
238 xbt_swag_t running_actions= surf_cpu_resource->common_public->states.running_action_set;
239 xbt_swag_t failed_actions= surf_cpu_resource->common_public->states.failed_action_set;
241 xbt_swag_foreach_safe(action, next_action, running_actions) {
242 action->generic_action.remains -=
243 lmm_variable_getvalue(action->variable)*delta;
244 /* if(action->generic_action.remains<.00001) action->generic_action.remains=0; */
245 if(action->generic_action.remains<=0) {
246 action_change_state((surf_action_t)action, SURF_ACTION_DONE);
247 } else { /* Need to check that none of the resource has failed*/
248 lmm_constraint_t cnst = NULL;
252 while((cnst=lmm_get_cnst_from_var(maxmin_system, action->variable, i++))) {
253 cpu = lmm_constraint_id(cnst);
254 if(cpu->current_state==SURF_CPU_OFF) {
255 action_change_state((surf_action_t) action, SURF_ACTION_FAILED);
262 xbt_swag_foreach_safe(action, next_action, failed_actions) {
263 lmm_variable_disable(maxmin_system, action->variable);
269 static void update_resource_state(void *id,
270 tmgr_trace_event_t event_type,
271 xbt_maxmin_float_t value)
275 printf("[" XBT_HEAP_FLOAT_T "] Asking to update \"%s\" with value " XBT_MAXMIN_FLOAT_T " for event %p\n",
276 surf_get_clock(), cpu->name, value, event_type);
278 if(event_type==cpu->power_event) {
279 cpu->current_power = value;
280 lmm_update_constraint_bound(cpu->constraint,cpu->current_power * cpu->power_scale);
281 } else if (event_type==cpu->state_event) {
282 if(value>0) cpu->current_state = SURF_CPU_ON;
283 else cpu->current_state = SURF_CPU_OFF;
289 static surf_action_t execute(void *cpu, xbt_maxmin_float_t size)
291 surf_action_cpu_t action = NULL;
294 action=xbt_new0(s_surf_action_cpu_t,1);
296 action->generic_action.cost=size;
297 action->generic_action.remains=size;
298 action->generic_action.start=-1.0;
299 action->generic_action.finish=-1.0;
300 action->generic_action.callback=cpu;
301 action->generic_action.resource_type=(surf_resource_t)surf_cpu_resource;
303 if(CPU->current_state==SURF_CPU_ON)
304 action->generic_action.state_set=surf_cpu_resource->common_public->states.running_action_set;
306 action->generic_action.state_set=surf_cpu_resource->common_public->states.failed_action_set;
307 xbt_swag_insert(action,action->generic_action.state_set);
309 action->variable = lmm_variable_new(maxmin_system, action, 1.0, -1.0, 1);
310 lmm_expand(maxmin_system, ((cpu_t)cpu)->constraint, action->variable, 1.0);
312 return (surf_action_t) action;
315 static e_surf_cpu_state_t get_state(void *cpu)
320 static void finalize(void)
322 xbt_dict_free(&cpu_set);
323 xbt_swag_free(surf_cpu_resource->common_public->states.ready_action_set);
324 xbt_swag_free(surf_cpu_resource->common_public->states.running_action_set);
325 xbt_swag_free(surf_cpu_resource->common_public->states.failed_action_set);
326 xbt_swag_free(surf_cpu_resource->common_public->states.done_action_set);
327 xbt_free(surf_cpu_resource->common_public);
328 xbt_free(surf_cpu_resource->common_private);
329 xbt_free(surf_cpu_resource->extension_public);
331 xbt_free(surf_cpu_resource);
332 surf_cpu_resource = NULL;
335 static void surf_cpu_resource_init_internal(void)
337 s_surf_action_t action;
339 surf_cpu_resource = xbt_new0(s_surf_cpu_resource_t,1);
341 surf_cpu_resource->common_private = xbt_new0(s_surf_resource_private_t,1);
342 surf_cpu_resource->common_public = xbt_new0(s_surf_resource_public_t,1);
343 /* surf_cpu_resource->extension_private = xbt_new0(s_surf_cpu_resource_extension_private_t,1); */
344 surf_cpu_resource->extension_public = xbt_new0(s_surf_cpu_resource_extension_public_t,1);
346 surf_cpu_resource->common_public->states.ready_action_set=
347 xbt_swag_new(xbt_swag_offset(action,state_hookup));
348 surf_cpu_resource->common_public->states.running_action_set=
349 xbt_swag_new(xbt_swag_offset(action,state_hookup));
350 surf_cpu_resource->common_public->states.failed_action_set=
351 xbt_swag_new(xbt_swag_offset(action,state_hookup));
352 surf_cpu_resource->common_public->states.done_action_set=
353 xbt_swag_new(xbt_swag_offset(action,state_hookup));
355 surf_cpu_resource->common_public->name_service = name_service;
356 surf_cpu_resource->common_public->get_resource_name = get_resource_name;
357 surf_cpu_resource->common_public->resource_used = resource_used;
358 surf_cpu_resource->common_public->action_get_state=surf_action_get_state;
359 surf_cpu_resource->common_public->action_free = action_free;
360 surf_cpu_resource->common_public->action_cancel = action_cancel;
361 surf_cpu_resource->common_public->action_recycle = action_recycle;
362 surf_cpu_resource->common_public->action_change_state = action_change_state;
364 surf_cpu_resource->common_private->share_resources = share_resources;
365 surf_cpu_resource->common_private->update_actions_state = update_actions_state;
366 surf_cpu_resource->common_private->update_resource_state = update_resource_state;
367 surf_cpu_resource->common_private->finalize = finalize;
369 surf_cpu_resource->extension_public->execute = execute;
370 surf_cpu_resource->extension_public->get_state = get_state;
372 cpu_set = xbt_dict_new();
374 xbt_assert0(maxmin_system,"surf_init has to be called first!");
377 void surf_cpu_resource_init(const char* filename)
379 surf_cpu_resource_init_internal();
380 parse_file(filename);
381 xbt_dynar_push(resource_list, &surf_cpu_resource);