Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
768df15f91ac4b54a5c858b939e9ca1f54a4ae9a
[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
10 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(cpu, surf,
11                                 "Logging specific to the SURF CPU module");
12
13 surf_cpu_resource_t surf_cpu_resource = NULL;
14
15 xbt_dict_t cpu_set = NULL;
16
17 static void cpu_free(void *CPU)
18 {
19 /*   cpu_t cpu = CPU; */
20 /* lmm_constraint_free(maxmin_system,cpu->constraint); */
21 /* Clean somewhere else ! */
22
23   xbt_free(CPU);
24 }
25
26 /* power_scale is the basic power of the cpu when the cpu is
27    completely available. power_initial is therefore expected to be
28    comprised between 0.0 and 1.0, just as the values of power_trace.
29    state_trace values mean SURF_CPU_ON if >0 and SURF_CPU_OFF
30    otherwise.
31 */
32 static cpu_t cpu_new(const char *name, xbt_maxmin_float_t power_scale,
33                      xbt_maxmin_float_t power_initial,
34                      tmgr_trace_t power_trace,
35                      e_surf_cpu_state_t state_initial,
36                      tmgr_trace_t state_trace)
37 {
38   cpu_t cpu = xbt_new0(s_cpu_t, 1);
39
40   cpu->resource = (surf_resource_t) surf_cpu_resource;
41   cpu->name = name;
42   cpu->power_scale = power_scale;
43   cpu->power_current = power_initial;
44 /*   cpu->power_trace = power_trace; */
45   if (power_trace)
46     cpu->power_event =
47         tmgr_history_add_trace(history, power_trace, 0.0, 0, cpu);
48
49   cpu->state_current = state_initial;
50 /*   cpu->state_trace = state_trace; */
51   if (state_trace)
52     cpu->state_event =
53         tmgr_history_add_trace(history, state_trace, 0.0, 0, cpu);
54
55   cpu->constraint =
56       lmm_constraint_new(maxmin_system, cpu,
57                          cpu->power_current * cpu->power_scale);
58
59   xbt_dict_set(cpu_set, name, cpu, cpu_free);
60
61   return cpu;
62 }
63
64 /*  
65    Semantic:  name       scale     initial     power     initial     state
66                                     power      trace      state      trace
67    
68    Token:   TOKEN_WORD TOKEN_WORD TOKEN_WORD TOKEN_WORD TOKEN_WORD TOKEN_WORD
69    Type:     string      float      float      string     ON/OFF     string
70 */
71
72 static void parse_cpu(void)
73 {
74   e_surf_token_t token;
75   char *name = NULL;
76   xbt_maxmin_float_t power_scale = 0.0;
77   xbt_maxmin_float_t power_initial = 0.0;
78   tmgr_trace_t power_trace = NULL;;
79   e_surf_cpu_state_t state_initial = SURF_CPU_OFF;
80   tmgr_trace_t state_trace = NULL;
81
82   name = xbt_strdup(surf_parse_text);
83
84   surf_parse_float(&power_scale);
85   surf_parse_float(&power_initial);
86   surf_parse_trace(&power_trace);
87
88   token = surf_parse();         /* state_initial */
89   xbt_assert1((token == TOKEN_WORD), "Parse error line %d", line_pos);
90   if (strcmp(surf_parse_text, "ON") == 0)
91     state_initial = SURF_CPU_ON;
92   else if (strcmp(surf_parse_text, "OFF") == 0)
93     state_initial = SURF_CPU_OFF;
94   else {
95     CRITICAL2("Invalid cpu state (line %d): %s neq ON or OFF\n", line_pos,
96               surf_parse_text);
97     xbt_abort();
98   }
99
100   surf_parse_trace(&state_trace);
101
102   cpu_new(name, power_scale, power_initial, power_trace, state_initial,
103           state_trace);
104 }
105
106 static void parse_file(const char *file)
107 {
108   e_surf_token_t token;
109
110   find_section(file, "CPU");
111
112   while (1) {
113     token = surf_parse();
114
115     if (token == TOKEN_END_SECTION)
116       break;
117     if (token == TOKEN_NEWLINE)
118       continue;
119
120     if (token == TOKEN_WORD)
121       parse_cpu();
122     else {
123       CRITICAL1("Parse error line %d\n", line_pos);
124       xbt_abort();
125     }
126   }
127
128   close_section("CPU");
129 }
130
131 static void *name_service(const char *name)
132 {
133   void *cpu = NULL;
134
135   xbt_dict_get(cpu_set, name, &cpu);
136
137   return cpu;
138 }
139
140 static const char *get_resource_name(void *resource_id)
141 {
142   return ((cpu_t) resource_id)->name;
143 }
144
145 static int resource_used(void *resource_id)
146 {
147   return lmm_constraint_used(maxmin_system,
148                              ((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,
173                                 e_surf_action_state_t state)
174 {
175   surf_action_change_state(action, state);
176   return;
177 }
178
179 static xbt_heap_float_t share_resources(xbt_heap_float_t now)
180 {
181   s_surf_action_cpu_t action;
182   return generic_maxmin_share_resources(surf_cpu_resource->common_public->
183                                         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,
308                              ((surf_action_cpu_t) action)->variable, 0.0);
309 }
310
311 static void action_resume(surf_action_t action)
312 {
313   lmm_update_variable_weight(maxmin_system,
314                              ((surf_action_cpu_t) action)->variable, 1.0);
315 }
316
317 static e_surf_cpu_state_t get_state(void *cpu)
318 {
319   return ((cpu_t) cpu)->state_current;
320 }
321
322 static void finalize(void)
323 {
324   xbt_dict_free(&cpu_set);
325   xbt_swag_free(surf_cpu_resource->common_public->states.ready_action_set);
326   xbt_swag_free(surf_cpu_resource->common_public->states.
327                 running_action_set);
328   xbt_swag_free(surf_cpu_resource->common_public->states.
329                 failed_action_set);
330   xbt_swag_free(surf_cpu_resource->common_public->states.done_action_set);
331   xbt_free(surf_cpu_resource->common_public);
332   xbt_free(surf_cpu_resource->common_private);
333   xbt_free(surf_cpu_resource->extension_public);
334
335   xbt_free(surf_cpu_resource);
336   surf_cpu_resource = NULL;
337 }
338
339 static void surf_cpu_resource_init_internal(void)
340 {
341   s_surf_action_t action;
342
343   surf_cpu_resource = xbt_new0(s_surf_cpu_resource_t, 1);
344
345   surf_cpu_resource->common_private =
346       xbt_new0(s_surf_resource_private_t, 1);
347   surf_cpu_resource->common_public = xbt_new0(s_surf_resource_public_t, 1);
348
349   surf_cpu_resource->extension_public =
350       xbt_new0(s_surf_cpu_resource_extension_public_t, 1);
351
352   surf_cpu_resource->common_public->states.ready_action_set =
353       xbt_swag_new(xbt_swag_offset(action, state_hookup));
354   surf_cpu_resource->common_public->states.running_action_set =
355       xbt_swag_new(xbt_swag_offset(action, state_hookup));
356   surf_cpu_resource->common_public->states.failed_action_set =
357       xbt_swag_new(xbt_swag_offset(action, state_hookup));
358   surf_cpu_resource->common_public->states.done_action_set =
359       xbt_swag_new(xbt_swag_offset(action, state_hookup));
360
361   surf_cpu_resource->common_public->name_service = name_service;
362   surf_cpu_resource->common_public->get_resource_name = get_resource_name;
363   surf_cpu_resource->common_public->action_get_state =
364       surf_action_get_state;
365   surf_cpu_resource->common_public->action_free = action_free;
366   surf_cpu_resource->common_public->action_cancel = action_cancel;
367   surf_cpu_resource->common_public->action_recycle = action_recycle;
368   surf_cpu_resource->common_public->action_change_state =
369       action_change_state;
370   surf_cpu_resource->common_public->name = "CPU";
371
372   surf_cpu_resource->common_private->resource_used = resource_used;
373   surf_cpu_resource->common_private->share_resources = share_resources;
374   surf_cpu_resource->common_private->update_actions_state =
375       update_actions_state;
376   surf_cpu_resource->common_private->update_resource_state =
377       update_resource_state;
378   surf_cpu_resource->common_private->finalize = finalize;
379
380   surf_cpu_resource->extension_public->execute = execute;
381   surf_cpu_resource->extension_public->sleep = action_sleep;
382   surf_cpu_resource->extension_public->suspend = action_suspend;
383   surf_cpu_resource->extension_public->resume = action_resume;
384
385   surf_cpu_resource->extension_public->get_state = get_state;
386
387   cpu_set = xbt_dict_new();
388
389   xbt_assert0(maxmin_system, "surf_init has to be called first!");
390 }
391
392 void surf_cpu_resource_init(const char *filename)
393 {
394   if (surf_cpu_resource)
395     return;
396   surf_cpu_resource_init_internal();
397   parse_file(filename);
398   xbt_dynar_push(resource_list, &surf_cpu_resource);
399 }