Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Taking in account the fact that some resource may have no action terminated.
[simgrid.git] / src / surf / cpu.c
1 /* Authors: Arnaud Legrand                                                  */
2
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. */
5
6 #include "cpu_private.h"
7 #include "xbt/dict.h"
8
9 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(cpu, surf ,"Logging specific to the SURF CPU module");
10
11 surf_cpu_resource_t surf_cpu_resource = NULL;
12
13 static xbt_dict_t cpu_set = NULL;
14
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
19    otherwise.
20 */
21
22 static void cpu_free(void *CPU){
23   cpu_t cpu = CPU;
24
25 /* lmm_constraint_free(maxmin_system,cpu->constraint); */
26 /* Clean somewhere else ! */
27
28   xbt_free(cpu);
29 }
30
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)
34 {
35   cpu_t cpu = xbt_new0(s_cpu_t,1);
36
37   cpu->resource = (surf_resource_t) surf_cpu_resource;
38   cpu->name = name;
39   cpu->power_scale = power_scale;
40   cpu->current_power = initial_power;
41 /*   cpu->power_trace = power_trace; */
42   if(power_trace) 
43     cpu->power_event = tmgr_history_add_trace(history, power_trace, 0.0, 0, cpu);
44
45   cpu->current_state = initial_state;
46 /*   cpu->state_trace = state_trace; */
47   if(state_trace) 
48     cpu->state_event = tmgr_history_add_trace(history, state_trace, 0.0, 0, cpu);
49
50   cpu->constraint = lmm_constraint_new(maxmin_system, cpu, cpu->current_power * cpu->power_scale);
51
52   xbt_dict_set(cpu_set, name, cpu, cpu_free);
53
54   return cpu;
55 }
56
57 /*  
58    Semantic:  name       scale     initial     power     initial     state
59                                     power      trace      state      trace
60    
61    Token:   TOKEN_WORD TOKEN_WORD TOKEN_WORD TOKEN_WORD TOKEN_WORD TOKEN_WORD
62    Type:     string      float      float      string     ON/OFF     string
63 */
64
65 static void parse_host(void)
66 {
67   e_surf_token_t token;
68   char *name = NULL; 
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;
74
75   name=xbt_strdup(surf_parse_text);
76
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);
81
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);
86
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);
91
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;
96   else {
97     CRITICAL2("Invalid cpu state (line %d): %s neq ON or OFF\n",line_pos, 
98               surf_parse_text);
99     xbt_abort();
100   }
101
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);
106
107   cpu_new(name, power_scale, initial_power, power_trace, initial_state, state_trace);
108 }
109
110 static void parse_file(const char *file)
111 {
112   e_surf_token_t token;
113
114   find_section(file,"CPU");
115
116   while(1) {
117     token=surf_parse();
118
119     if(token==TOKEN_END_SECTION) break;
120     if(token==TOKEN_NEWLINE) continue;
121     
122     if(token==TOKEN_WORD) parse_host();
123     else {
124       CRITICAL1("Parse error line %d\n",line_pos);
125       xbt_abort();
126     }
127   }
128
129   close_section("CPU");  
130 }
131
132 static void *name_service(const char *name)
133 {
134   void *cpu=NULL;
135   
136   xbt_dict_get(cpu_set, name, &cpu);
137
138   return cpu;
139 }
140
141 static const char *get_resource_name(void *resource_id)
142 {
143   return ((cpu_t) resource_id)->name;
144 }
145
146 static int resource_used(void *resource_id)
147 {
148   return lmm_constraint_used(maxmin_system, ((cpu_t)resource_id)->constraint);
149 }
150
151 static void action_free(surf_action_t action)
152 {
153   surf_action_cpu_t Action = (surf_action_cpu_t) action;
154
155   xbt_swag_remove(action,action->state_set);
156   lmm_variable_free(maxmin_system,Action->variable);
157   xbt_free(action);
158
159   return;
160 }
161
162 static void action_cancel(surf_action_t action)
163 {
164   return;
165 }
166
167 static void action_recycle(surf_action_t action)
168 {
169   return;
170 }
171
172 static void action_change_state(surf_action_t action, e_surf_action_state_t state)
173 {
174   surf_action_change_state(action, state);
175   return;
176 }
177
178 static xbt_heap_float_t share_resources(xbt_heap_float_t now)
179 {
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);  
185
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 ;
190
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;
195   }
196
197   return min;
198 }
199
200 static void update_actions_state(xbt_heap_float_t now,
201                                  xbt_heap_float_t delta)
202 {
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;
207
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;
216       int i=0;
217       cpu_t cpu = NULL;
218
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);
223           break;
224         }
225       }
226     }
227   }
228
229   xbt_swag_foreach_safe(action, next_action, failed_actions) {
230     lmm_variable_disable(maxmin_system, action->variable);
231   }
232
233   return;
234 }
235
236 static void update_resource_state(void *id,
237                                   tmgr_trace_event_t event_type, 
238                                   xbt_maxmin_float_t value)
239 {
240   cpu_t cpu = id;
241   
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);
244   
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();}
252
253   return;
254 }
255
256 static surf_action_t execute(void *cpu, xbt_maxmin_float_t size)
257 {
258   surf_action_cpu_t action = NULL;
259   cpu_t CPU = cpu;
260   
261   action=xbt_new0(s_surf_action_cpu_t,1);
262
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;
269
270   if(CPU->current_state==SURF_CPU_ON) 
271     action->generic_action.state_set=surf_cpu_resource->common_public->states.running_action_set;
272   else 
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);
275
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);
278
279   return (surf_action_t) action;
280 }
281
282 static e_surf_cpu_state_t get_state(void *cpu)
283 {
284   return SURF_CPU_OFF;
285 }
286
287 static void finalize(void)
288 {
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);
297
298   xbt_free(surf_cpu_resource);
299   surf_cpu_resource = NULL;
300 }
301
302 static void surf_cpu_resource_init_internal(void)
303 {
304   s_surf_action_t action;
305
306   surf_cpu_resource = xbt_new0(s_surf_cpu_resource_t,1);
307   
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);
312
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));
321
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;
330
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;
335
336   surf_cpu_resource->extension_public->execute = execute;
337   surf_cpu_resource->extension_public->get_state = get_state;
338
339   cpu_set = xbt_dict_new();
340
341   xbt_assert0(maxmin_system,"surf_init has to be called first!");
342 }
343
344 void surf_cpu_resource_init(const char* filename)
345 {
346   surf_cpu_resource_init_internal();
347   parse_file(filename);
348   xbt_dynar_push(resource_list, &surf_cpu_resource);
349 }