Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
No more xbt_maxmin_float_t not xbt_heap_float_t. I use double everywhere. In
[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, double power_scale,
33                      double 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      double     double     string     ON/OFF     string
70 */
71
72 static void parse_cpu(void)
73 {
74   e_surf_token_t token;
75   char *name = NULL;
76   double power_scale = 0.0;
77   double 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_double(&power_scale);
85   surf_parse_double(&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 double share_resources(double 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(double now, double delta)
188 {
189   surf_action_cpu_t action = NULL;
190   surf_action_cpu_t next_action = NULL;
191   xbt_swag_t running_actions =
192       surf_cpu_resource->common_public->states.running_action_set;
193   xbt_swag_t failed_actions =
194       surf_cpu_resource->common_public->states.failed_action_set;
195
196   xbt_swag_foreach_safe(action, next_action, running_actions) {
197     action->generic_action.remains -=
198         lmm_variable_getvalue(action->variable) * delta;
199     if (action->generic_action.max_duration != NO_MAX_DURATION)
200       action->generic_action.max_duration -= delta;
201 /*     if(action->generic_action.remains<.00001) action->generic_action.remains=0; */
202     if (action->generic_action.remains <= 0) {
203       action->generic_action.finish = surf_get_clock();
204       action_change_state((surf_action_t) action, SURF_ACTION_DONE);
205     } else if ((action->generic_action.max_duration != NO_MAX_DURATION) &&
206                (action->generic_action.max_duration <= 0)) {
207       action->generic_action.finish = surf_get_clock();
208       action_change_state((surf_action_t) action, SURF_ACTION_DONE);
209     } else {                    /* Need to check that none of the resource has failed */
210       lmm_constraint_t cnst = NULL;
211       int i = 0;
212       cpu_t cpu = NULL;
213
214       while ((cnst =
215               lmm_get_cnst_from_var(maxmin_system, action->variable,
216                                     i++))) {
217         cpu = lmm_constraint_id(cnst);
218         if (cpu->state_current == SURF_CPU_OFF) {
219           action->generic_action.finish = surf_get_clock();
220           action_change_state((surf_action_t) action, SURF_ACTION_FAILED);
221           break;
222         }
223       }
224     }
225   }
226
227   xbt_swag_foreach_safe(action, next_action, failed_actions) {
228     lmm_variable_disable(maxmin_system, action->variable);
229   }
230
231   return;
232 }
233
234 static void update_resource_state(void *id,
235                                   tmgr_trace_event_t event_type,
236                                   double value)
237 {
238   cpu_t cpu = id;
239
240 /*   printf("[" "%lg" "] Asking to update CPU \"%s\" with value " */
241 /*       "%lg" " for event %p\n", surf_get_clock(), cpu->name, */
242 /*       value, event_type); */
243
244   if (event_type == cpu->power_event) {
245     cpu->power_current = value;
246     lmm_update_constraint_bound(maxmin_system, cpu->constraint,
247                                 cpu->power_current * cpu->power_scale);
248   } else if (event_type == cpu->state_event) {
249     if (value > 0)
250       cpu->state_current = SURF_CPU_ON;
251     else
252       cpu->state_current = SURF_CPU_OFF;
253   } else {
254     CRITICAL0("Unknown event ! \n");
255     xbt_abort();
256   }
257
258   return;
259 }
260
261 static surf_action_t execute(void *cpu, double size)
262 {
263   surf_action_cpu_t action = NULL;
264   cpu_t CPU = cpu;
265
266   action = xbt_new0(s_surf_action_cpu_t, 1);
267
268   action->generic_action.cost = size;
269   action->generic_action.remains = size;
270   action->generic_action.max_duration = NO_MAX_DURATION;
271   action->generic_action.start = surf_get_clock();
272   action->generic_action.finish = -1.0;
273 /*   action->generic_action.callback = cpu; */
274   action->generic_action.callback = NULL;
275   action->generic_action.resource_type =
276       (surf_resource_t) surf_cpu_resource;
277
278   if (CPU->state_current == SURF_CPU_ON)
279     action->generic_action.state_set =
280         surf_cpu_resource->common_public->states.running_action_set;
281   else
282     action->generic_action.state_set =
283         surf_cpu_resource->common_public->states.failed_action_set;
284   xbt_swag_insert(action, action->generic_action.state_set);
285
286   action->variable = lmm_variable_new(maxmin_system, action, 1.0, -1.0, 1);
287   lmm_expand(maxmin_system, ((cpu_t) cpu)->constraint, action->variable,
288              1.0);
289
290   return (surf_action_t) action;
291 }
292
293 static surf_action_t action_sleep(void *cpu, double duration)
294 {
295   surf_action_cpu_t action = NULL;
296
297   action = (surf_action_cpu_t) execute(cpu, 1.0);
298   action->generic_action.max_duration = duration;
299   lmm_update_variable_weight(maxmin_system, action->variable, 0.0);
300
301   return (surf_action_t) action;
302 }
303
304 static void action_suspend(surf_action_t action)
305 {
306   lmm_update_variable_weight(maxmin_system,
307                              ((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,
313                              ((surf_action_cpu_t) action)->variable, 1.0);
314 }
315
316 static e_surf_cpu_state_t get_state(void *cpu)
317 {
318   return ((cpu_t) cpu)->state_current;
319 }
320
321 static void finalize(void)
322 {
323   xbt_dict_free(&cpu_set);
324   xbt_swag_free(surf_cpu_resource->common_public->states.ready_action_set);
325   xbt_swag_free(surf_cpu_resource->common_public->states.
326                 running_action_set);
327   xbt_swag_free(surf_cpu_resource->common_public->states.
328                 failed_action_set);
329   xbt_swag_free(surf_cpu_resource->common_public->states.done_action_set);
330   xbt_free(surf_cpu_resource->common_public);
331   xbt_free(surf_cpu_resource->common_private);
332   xbt_free(surf_cpu_resource->extension_public);
333
334   xbt_free(surf_cpu_resource);
335   surf_cpu_resource = NULL;
336 }
337
338 static void surf_cpu_resource_init_internal(void)
339 {
340   s_surf_action_t action;
341
342   surf_cpu_resource = xbt_new0(s_surf_cpu_resource_t, 1);
343
344   surf_cpu_resource->common_private =
345       xbt_new0(s_surf_resource_private_t, 1);
346   surf_cpu_resource->common_public = xbt_new0(s_surf_resource_public_t, 1);
347
348   surf_cpu_resource->extension_public =
349       xbt_new0(s_surf_cpu_resource_extension_public_t, 1);
350
351   surf_cpu_resource->common_public->states.ready_action_set =
352       xbt_swag_new(xbt_swag_offset(action, state_hookup));
353   surf_cpu_resource->common_public->states.running_action_set =
354       xbt_swag_new(xbt_swag_offset(action, state_hookup));
355   surf_cpu_resource->common_public->states.failed_action_set =
356       xbt_swag_new(xbt_swag_offset(action, state_hookup));
357   surf_cpu_resource->common_public->states.done_action_set =
358       xbt_swag_new(xbt_swag_offset(action, state_hookup));
359
360   surf_cpu_resource->common_public->name_service = name_service;
361   surf_cpu_resource->common_public->get_resource_name = get_resource_name;
362   surf_cpu_resource->common_public->action_get_state =
363       surf_action_get_state;
364   surf_cpu_resource->common_public->action_free = action_free;
365   surf_cpu_resource->common_public->action_cancel = action_cancel;
366   surf_cpu_resource->common_public->action_recycle = action_recycle;
367   surf_cpu_resource->common_public->action_change_state =
368       action_change_state;
369   surf_cpu_resource->common_public->name = "CPU";
370
371   surf_cpu_resource->common_private->resource_used = resource_used;
372   surf_cpu_resource->common_private->share_resources = share_resources;
373   surf_cpu_resource->common_private->update_actions_state =
374       update_actions_state;
375   surf_cpu_resource->common_private->update_resource_state =
376       update_resource_state;
377   surf_cpu_resource->common_private->finalize = finalize;
378
379   surf_cpu_resource->extension_public->execute = execute;
380   surf_cpu_resource->extension_public->sleep = action_sleep;
381   surf_cpu_resource->extension_public->suspend = action_suspend;
382   surf_cpu_resource->extension_public->resume = action_resume;
383
384   surf_cpu_resource->extension_public->get_state = get_state;
385
386   cpu_set = xbt_dict_new();
387
388   xbt_assert0(maxmin_system, "surf_init has to be called first!");
389 }
390
391 void surf_cpu_resource_init(const char *filename)
392 {
393   if (surf_cpu_resource)
394     return;
395   surf_cpu_resource_init_internal();
396   parse_file(filename);
397   xbt_dynar_push(resource_list, &surf_cpu_resource);
398 }