Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
3e34704648c39ce9a69449fa8efe1564398f79ac
[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 static void find_section(const char* file, const char* section_name)
58 {
59   e_surf_token_t token;
60   int found = 0;
61
62   surf_parse_open(file);
63
64   while((token=surf_parse())) {
65     if(token!=TOKEN_BEGIN_SECTION) continue;
66
67     token=surf_parse();
68     xbt_assert1((token==TOKEN_WORD),"Parse error line %d",line_pos);
69     if(strcmp(surf_parse_text,section_name)==0) found=1;
70
71     token=surf_parse();
72     xbt_assert1((token==TOKEN_CLOSURE),"Parse error line %d",line_pos);
73
74     if(found) return;
75   }
76
77   CRITICAL2("Could not find %s section in %s\n",section_name,file);
78 }
79
80 static void close_section(const char* section_name)
81 {
82   e_surf_token_t token;
83
84   token=surf_parse();
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).", 
88               section_name);
89   
90   token=surf_parse();
91   xbt_assert1((token==TOKEN_CLOSURE),"Parse error line %d",line_pos);
92
93   surf_parse_close();
94 }
95
96 /*  
97    Semantic:  name       scale     initial     power     initial     state
98                                     power      trace      state      trace
99    
100    Token:   TOKEN_WORD TOKEN_WORD TOKEN_WORD TOKEN_WORD TOKEN_WORD TOKEN_WORD
101    Type:     string      float      float      string     ON/OFF     string
102 */
103
104 static void parse_host(void)
105 {
106   e_surf_token_t token;
107   char *name = NULL; 
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;
113
114   name=xbt_strdup(surf_parse_text);
115
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);
120
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);
125
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);
130
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, 
136                  surf_parse_text);
137
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);
142
143   cpu_new(name, power_scale, initial_power, power_trace, initial_state, state_trace);
144 }
145
146 static void parse_file(const char *file)
147 {
148   e_surf_token_t token;
149
150   find_section(file,"CPU");
151
152   while(1) {
153     token=surf_parse();
154
155     if(token==TOKEN_END_SECTION) break;
156     if(token==TOKEN_NEWLINE) continue;
157     
158     if(token==TOKEN_WORD) parse_host();
159     else CRITICAL1("Parse error line %d\n",line_pos);
160   }
161
162   close_section("CPU");  
163 }
164
165 static void *name_service(const char *name)
166 {
167   void *cpu=NULL;
168   
169   xbt_dict_get(cpu_set, name, &cpu);
170
171   return cpu;
172 }
173
174 static const char *get_resource_name(void *resource_id)
175 {
176   return ((cpu_t) resource_id)->name;
177 }
178
179 static int resource_used(void *resource_id)
180 {
181   return lmm_constraint_used(maxmin_system, ((cpu_t)resource_id)->constraint);
182 }
183
184 static void action_free(surf_action_t action)
185 {
186   surf_action_cpu_t Action = (surf_action_cpu_t) action;
187
188   xbt_swag_remove(action,action->state_set);
189   lmm_variable_free(maxmin_system,Action->variable);
190   xbt_free(action);
191
192   return;
193 }
194
195 static void action_cancel(surf_action_t action)
196 {
197   return;
198 }
199
200 static void action_recycle(surf_action_t action)
201 {
202   return;
203 }
204
205 static void action_change_state(surf_action_t action, e_surf_action_state_t state)
206 {
207   surf_action_change_state(action, state);
208   return;
209 }
210
211 static xbt_heap_float_t share_resources(xbt_heap_float_t now)
212 {
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);  
218
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 ;
223
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;
228   }
229
230   return min;
231 }
232
233 static void update_actions_state(xbt_heap_float_t now,
234                                  xbt_heap_float_t delta)
235 {
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;
240
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;
249       int i=0;
250       cpu_t cpu = NULL;
251
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);
256           break;
257         }
258       }
259     }
260   }
261
262   xbt_swag_foreach_safe(action, next_action, failed_actions) {
263     lmm_variable_disable(maxmin_system, action->variable);
264   }
265
266   return;
267 }
268
269 static void update_resource_state(void *id,
270                                   tmgr_trace_event_t event_type, 
271                                   xbt_maxmin_float_t value)
272 {
273   cpu_t cpu = id;
274   
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);
277   
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;
284   } else abort();
285
286   return;
287 }
288
289 static surf_action_t execute(void *cpu, xbt_maxmin_float_t size)
290 {
291   surf_action_cpu_t action = NULL;
292   cpu_t CPU = cpu;
293   
294   action=xbt_new0(s_surf_action_cpu_t,1);
295
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;
302
303   if(CPU->current_state==SURF_CPU_ON) 
304     action->generic_action.state_set=surf_cpu_resource->common_public->states.running_action_set;
305   else 
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);
308
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);
311
312   return (surf_action_t) action;
313 }
314
315 static e_surf_cpu_state_t get_state(void *cpu)
316 {
317   return SURF_CPU_OFF;
318 }
319
320 static void finalize(void)
321 {
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);
330
331   xbt_free(surf_cpu_resource);
332   surf_cpu_resource = NULL;
333 }
334
335 static void surf_cpu_resource_init_internal(void)
336 {
337   s_surf_action_t action;
338
339   surf_cpu_resource = xbt_new0(s_surf_cpu_resource_t,1);
340   
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);
345
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));
354
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;
363
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;
368
369   surf_cpu_resource->extension_public->execute = execute;
370   surf_cpu_resource->extension_public->get_state = get_state;
371
372   cpu_set = xbt_dict_new();
373
374   xbt_assert0(maxmin_system,"surf_init has to be called first!");
375 }
376
377 void surf_cpu_resource_init(const char* filename)
378 {
379   surf_cpu_resource_init_internal();
380   parse_file(filename);
381   xbt_dynar_push(resource_list, &surf_cpu_resource);
382 }