Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
3f40e53260f9e4bd8abbba57fbd41d99569cb897
[simgrid.git] / src / surf / cpu.c
1 /*      $Id$     */
2
3 /* Copyright (c) 2004 Arnaud Legrand. All rights reserved.                  */
4
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. */
7
8 #include "cpu_private.h"
9 #include "xbt/dict.h"
10
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(cpu, surf,
12                                 "Logging specific to the SURF CPU module");
13
14 surf_cpu_resource_t surf_cpu_resource = NULL;
15
16 static xbt_dict_t cpu_set = NULL;
17
18 static void cpu_free(void *CPU)
19 {
20 /*   cpu_t cpu = CPU; */
21 /* lmm_constraint_free(maxmin_system,cpu->constraint); */
22 /* Clean somewhere else ! */
23
24   xbt_free(CPU);
25 }
26
27 /* power_scale is the basic power of the cpu when the cpu is
28    completely available. power_initial is therefore expected to be
29    comprised between 0.0 and 1.0, just as the values of power_trace.
30    state_trace values mean SURF_CPU_ON if >0 and SURF_CPU_OFF
31    otherwise.
32 */
33 static cpu_t cpu_new(const char *name, xbt_maxmin_float_t power_scale,
34                      xbt_maxmin_float_t power_initial,
35                      tmgr_trace_t power_trace,
36                      e_surf_cpu_state_t state_initial,
37                      tmgr_trace_t state_trace)
38 {
39   cpu_t cpu = xbt_new0(s_cpu_t, 1);
40
41   cpu->resource = (surf_resource_t) surf_cpu_resource;
42   cpu->name = name;
43   cpu->power_scale = power_scale;
44   cpu->power_current = power_initial;
45 /*   cpu->power_trace = power_trace; */
46   if (power_trace)
47     cpu->power_event =
48         tmgr_history_add_trace(history, power_trace, 0.0, 0, cpu);
49
50   cpu->state_current = state_initial;
51 /*   cpu->state_trace = state_trace; */
52   if (state_trace)
53     cpu->state_event =
54         tmgr_history_add_trace(history, state_trace, 0.0, 0, cpu);
55
56   cpu->constraint =
57       lmm_constraint_new(maxmin_system, cpu,
58                          cpu->power_current * cpu->power_scale);
59
60   xbt_dict_set(cpu_set, name, cpu, cpu_free);
61
62   return cpu;
63 }
64
65 /*  
66    Semantic:  name       scale     initial     power     initial     state
67                                     power      trace      state      trace
68    
69    Token:   TOKEN_WORD TOKEN_WORD TOKEN_WORD TOKEN_WORD TOKEN_WORD TOKEN_WORD
70    Type:     string      float      float      string     ON/OFF     string
71 */
72
73 static void parse_cpu(void)
74 {
75   e_surf_token_t token;
76   char *name = NULL;
77   xbt_maxmin_float_t power_scale = 0.0;
78   xbt_maxmin_float_t power_initial = 0.0;
79   tmgr_trace_t power_trace = NULL;;
80   e_surf_cpu_state_t state_initial = SURF_CPU_OFF;
81   tmgr_trace_t state_trace = NULL;
82
83   name = xbt_strdup(surf_parse_text);
84
85   surf_parse_float(&power_scale);
86   surf_parse_float(&power_initial);
87   surf_parse_trace(&power_trace);
88
89   token = surf_parse();         /* state_initial */
90   xbt_assert1((token == TOKEN_WORD), "Parse error line %d", line_pos);
91   if (strcmp(surf_parse_text, "ON") == 0)
92     state_initial = SURF_CPU_ON;
93   else if (strcmp(surf_parse_text, "OFF") == 0)
94     state_initial = SURF_CPU_OFF;
95   else {
96     CRITICAL2("Invalid cpu state (line %d): %s neq ON or OFF\n", line_pos,
97               surf_parse_text);
98     xbt_abort();
99   }
100
101   surf_parse_trace(&state_trace);
102
103   cpu_new(name, power_scale, power_initial, power_trace, state_initial,
104           state_trace);
105 }
106
107 static void parse_file(const char *file)
108 {
109   e_surf_token_t token;
110
111   find_section(file, "CPU");
112
113   while (1) {
114     token = surf_parse();
115
116     if (token == TOKEN_END_SECTION)
117       break;
118     if (token == TOKEN_NEWLINE)
119       continue;
120
121     if (token == TOKEN_WORD)
122       parse_cpu();
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,
149                              ((cpu_t) resource_id)->constraint);
150 }
151
152 static void action_free(surf_action_t action)
153 {
154   surf_action_cpu_t Action = (surf_action_cpu_t) action;
155
156   xbt_swag_remove(action, action->state_set);
157   lmm_variable_free(maxmin_system, Action->variable);
158   xbt_free(action);
159
160   return;
161 }
162
163 static void action_cancel(surf_action_t action)
164 {
165   return;
166 }
167
168 static void action_recycle(surf_action_t action)
169 {
170   return;
171 }
172
173 static void action_change_state(surf_action_t action,
174                                 e_surf_action_state_t state)
175 {
176   surf_action_change_state(action, state);
177   return;
178 }
179
180 static xbt_heap_float_t share_resources(xbt_heap_float_t now)
181 {
182   s_surf_action_cpu_t action;
183   return generic_maxmin_share_resources(surf_cpu_resource->common_public->states.running_action_set,
184                                         xbt_swag_offset(action,variable));
185 }
186
187 static void update_actions_state(xbt_heap_float_t now,
188                                  xbt_heap_float_t delta)
189 {
190   surf_action_cpu_t action = NULL;
191   surf_action_cpu_t next_action = NULL;
192   xbt_swag_t running_actions =
193       surf_cpu_resource->common_public->states.running_action_set;
194   xbt_swag_t failed_actions =
195       surf_cpu_resource->common_public->states.failed_action_set;
196
197   xbt_swag_foreach_safe(action, next_action, running_actions) {
198     action->generic_action.remains -=
199         lmm_variable_getvalue(action->variable) * delta;
200     if(action->generic_action.max_duration!=NO_MAX_DURATION)
201       action->generic_action.max_duration -= delta;
202 /*     if(action->generic_action.remains<.00001) action->generic_action.remains=0; */
203     if (action->generic_action.remains <= 0) {
204       action->generic_action.finish = surf_get_clock();
205       action_change_state((surf_action_t) action, SURF_ACTION_DONE);
206     } else if((action->generic_action.max_duration!=NO_MAX_DURATION) && 
207               (action->generic_action.max_duration<=0)) {
208       action->generic_action.finish = surf_get_clock();
209       action_change_state((surf_action_t) action, SURF_ACTION_DONE);    
210     } else {                    /* Need to check that none of the resource has failed */
211       lmm_constraint_t cnst = NULL;
212       int i = 0;
213       cpu_t cpu = NULL;
214
215       while ((cnst =
216               lmm_get_cnst_from_var(maxmin_system, action->variable,
217                                     i++))) {
218         cpu = lmm_constraint_id(cnst);
219         if (cpu->state_current == SURF_CPU_OFF) {
220           action->generic_action.finish = surf_get_clock();
221           action_change_state((surf_action_t) action, SURF_ACTION_FAILED);
222           break;
223         }
224       }
225     }
226   }
227
228   xbt_swag_foreach_safe(action, next_action, failed_actions) {
229     lmm_variable_disable(maxmin_system, action->variable);
230   }
231
232   return;
233 }
234
235 static void update_resource_state(void *id,
236                                   tmgr_trace_event_t event_type,
237                                   xbt_maxmin_float_t value)
238 {
239   cpu_t cpu = id;
240
241 /*   printf("[" XBT_HEAP_FLOAT_T "] Asking to update CPU \"%s\" with value " */
242 /*       XBT_MAXMIN_FLOAT_T " for event %p\n", surf_get_clock(), cpu->name, */
243 /*       value, event_type); */
244
245   if (event_type == cpu->power_event) {
246     cpu->power_current = value;
247     lmm_update_constraint_bound(maxmin_system,cpu->constraint,
248                                 cpu->power_current * cpu->power_scale);
249   } else if (event_type == cpu->state_event) {
250     if (value > 0)
251       cpu->state_current = SURF_CPU_ON;
252     else
253       cpu->state_current = SURF_CPU_OFF;
254   } else {
255     CRITICAL0("Unknown event ! \n");
256     xbt_abort();
257   }
258
259   return;
260 }
261
262 static surf_action_t execute(void *cpu, xbt_maxmin_float_t size)
263 {
264   surf_action_cpu_t action = NULL;
265   cpu_t CPU = cpu;
266
267   action = xbt_new0(s_surf_action_cpu_t, 1);
268
269   action->generic_action.cost = size;
270   action->generic_action.remains = size;
271   action->generic_action.max_duration = NO_MAX_DURATION;
272   action->generic_action.start = surf_get_clock();
273   action->generic_action.finish = -1.0;
274 /*   action->generic_action.callback = cpu; */
275   action->generic_action.callback = NULL;
276   action->generic_action.resource_type =
277       (surf_resource_t) surf_cpu_resource;
278
279   if (CPU->state_current == SURF_CPU_ON)
280     action->generic_action.state_set =
281         surf_cpu_resource->common_public->states.running_action_set;
282   else
283     action->generic_action.state_set =
284         surf_cpu_resource->common_public->states.failed_action_set;
285   xbt_swag_insert(action, action->generic_action.state_set);
286
287   action->variable = lmm_variable_new(maxmin_system, action, 1.0, -1.0, 1);
288   lmm_expand(maxmin_system, ((cpu_t) cpu)->constraint, action->variable,
289              1.0);
290
291   return (surf_action_t) action;
292 }
293
294 static surf_action_t action_sleep(void *cpu, xbt_maxmin_float_t duration)
295 {
296   surf_action_cpu_t action = NULL;
297
298   action = (surf_action_cpu_t) execute(cpu, 1.0);
299   action->generic_action.max_duration = duration;
300   lmm_update_variable_weight(maxmin_system, action->variable, 0.0);
301
302   return (surf_action_t) action;
303 }
304
305 static void action_suspend(surf_action_t action)
306 {
307   lmm_update_variable_weight(maxmin_system, ((surf_action_cpu_t) action)->variable, 0.0);
308 }
309
310 static void action_resume(surf_action_t action)
311 {
312   lmm_update_variable_weight(maxmin_system, ((surf_action_cpu_t) action)->variable, 1.0);
313 }
314
315 static e_surf_cpu_state_t get_state(void *cpu)
316 {
317   return ((cpu_t) cpu)->state_current;
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.
325                 running_action_set);
326   xbt_swag_free(surf_cpu_resource->common_public->states.
327                 failed_action_set);
328   xbt_swag_free(surf_cpu_resource->common_public->states.done_action_set);
329   xbt_free(surf_cpu_resource->common_public);
330   xbt_free(surf_cpu_resource->common_private);
331   xbt_free(surf_cpu_resource->extension_public);
332
333   xbt_free(surf_cpu_resource);
334   surf_cpu_resource = NULL;
335 }
336
337 static void surf_cpu_resource_init_internal(void)
338 {
339   s_surf_action_t action;
340
341   surf_cpu_resource = xbt_new0(s_surf_cpu_resource_t, 1);
342
343   surf_cpu_resource->common_private =
344       xbt_new0(s_surf_resource_private_t, 1);
345   surf_cpu_resource->common_public = xbt_new0(s_surf_resource_public_t, 1);
346
347   surf_cpu_resource->extension_public =
348       xbt_new0(s_surf_cpu_resource_extension_public_t, 1);
349
350   surf_cpu_resource->common_public->states.ready_action_set =
351       xbt_swag_new(xbt_swag_offset(action, state_hookup));
352   surf_cpu_resource->common_public->states.running_action_set =
353       xbt_swag_new(xbt_swag_offset(action, state_hookup));
354   surf_cpu_resource->common_public->states.failed_action_set =
355       xbt_swag_new(xbt_swag_offset(action, state_hookup));
356   surf_cpu_resource->common_public->states.done_action_set =
357       xbt_swag_new(xbt_swag_offset(action, state_hookup));
358
359   surf_cpu_resource->common_public->name_service = name_service;
360   surf_cpu_resource->common_public->get_resource_name = get_resource_name;
361   surf_cpu_resource->common_public->action_get_state =
362       surf_action_get_state;
363   surf_cpu_resource->common_public->action_free = action_free;
364   surf_cpu_resource->common_public->action_cancel = action_cancel;
365   surf_cpu_resource->common_public->action_recycle = action_recycle;
366   surf_cpu_resource->common_public->action_change_state =
367       action_change_state;
368
369   surf_cpu_resource->common_private->resource_used = resource_used;
370   surf_cpu_resource->common_private->share_resources = share_resources;
371   surf_cpu_resource->common_private->update_actions_state =
372       update_actions_state;
373   surf_cpu_resource->common_private->update_resource_state =
374       update_resource_state;
375   surf_cpu_resource->common_private->finalize = finalize;
376
377   surf_cpu_resource->extension_public->execute = execute;
378   surf_cpu_resource->extension_public->sleep = action_sleep;
379   surf_cpu_resource->extension_public->suspend = action_suspend;
380   surf_cpu_resource->extension_public->resume = action_resume;
381
382   surf_cpu_resource->extension_public->get_state = get_state;
383
384   cpu_set = xbt_dict_new();
385
386   xbt_assert0(maxmin_system, "surf_init has to be called first!");
387 }
388
389 void surf_cpu_resource_init(const char *filename)
390 {
391   surf_cpu_resource_init_internal();
392   parse_file(filename);
393   xbt_dynar_push(resource_list, &surf_cpu_resource);
394 }