Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Cosmetics
[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,
10                                 "Logging specific to the SURF CPU module");
11
12 surf_cpu_resource_t surf_cpu_resource = NULL;
13
14 static xbt_dict_t cpu_set = NULL;
15
16 /* power_scale is the basic power of the cpu when the cpu is
17    completely available. initial_power is therefore expected to be
18    comprised between 0.0 and 1.0, just as the values of power_trace.
19    state_trace values mean SURF_CPU_ON if >0 and SURF_CPU_OFF
20    otherwise.
21 */
22
23 static void cpu_free(void *CPU)
24 {
25   cpu_t cpu = CPU;
26
27 /* lmm_constraint_free(maxmin_system,cpu->constraint); */
28 /* Clean somewhere else ! */
29
30   xbt_free(cpu);
31 }
32
33 static cpu_t cpu_new(const char *name, xbt_maxmin_float_t power_scale,
34                      xbt_maxmin_float_t initial_power,
35                      tmgr_trace_t power_trace,
36                      e_surf_cpu_state_t initial_state,
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 = initial_power;
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 = initial_state;
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_host(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 initial_power = 0.0;
79   tmgr_trace_t power_trace = NULL;;
80   e_surf_cpu_state_t initial_state = SURF_CPU_OFF;
81   tmgr_trace_t state_trace = NULL;
82
83   name = xbt_strdup(surf_parse_text);
84
85   token = surf_parse();         /* power_scale */
86   xbt_assert1((token == TOKEN_WORD), "Parse error line %d", line_pos);
87   xbt_assert2((sscanf(surf_parse_text, XBT_MAXMIN_FLOAT_T, &power_scale) ==
88                1), "Parse error line %d : %s not a number", line_pos,
89               surf_parse_text);
90
91   token = surf_parse();         /* initial_power */
92   xbt_assert1((token == TOKEN_WORD), "Parse error line %d", line_pos);
93   xbt_assert2((sscanf(surf_parse_text, XBT_MAXMIN_FLOAT_T, &initial_power)
94                == 1), "Parse error line %d : %s not a number", line_pos,
95               surf_parse_text);
96
97   token = surf_parse();         /* power_trace */
98   xbt_assert1((token == TOKEN_WORD), "Parse error line %d", line_pos);
99   if (strcmp(surf_parse_text, "") == 0)
100     power_trace = NULL;
101   else
102     power_trace = tmgr_trace_new(surf_parse_text);
103
104   token = surf_parse();         /* initial_state */
105   xbt_assert1((token == TOKEN_WORD), "Parse error line %d", line_pos);
106   if (strcmp(surf_parse_text, "ON") == 0)
107     initial_state = SURF_CPU_ON;
108   else if (strcmp(surf_parse_text, "OFF") == 0)
109     initial_state = SURF_CPU_OFF;
110   else {
111     CRITICAL2("Invalid cpu state (line %d): %s neq ON or OFF\n", line_pos,
112               surf_parse_text);
113     xbt_abort();
114   }
115
116   token = surf_parse();         /* state_trace */
117   xbt_assert1((token == TOKEN_WORD), "Parse error line %d", line_pos);
118   if (strcmp(surf_parse_text, "") == 0)
119     state_trace = NULL;
120   else
121     state_trace = tmgr_trace_new(surf_parse_text);
122
123   cpu_new(name, power_scale, initial_power, power_trace, initial_state,
124           state_trace);
125 }
126
127 static void parse_file(const char *file)
128 {
129   e_surf_token_t token;
130
131   find_section(file, "CPU");
132
133   while (1) {
134     token = surf_parse();
135
136     if (token == TOKEN_END_SECTION)
137       break;
138     if (token == TOKEN_NEWLINE)
139       continue;
140
141     if (token == TOKEN_WORD)
142       parse_host();
143     else {
144       CRITICAL1("Parse error line %d\n", line_pos);
145       xbt_abort();
146     }
147   }
148
149   close_section("CPU");
150 }
151
152 static void *name_service(const char *name)
153 {
154   void *cpu = NULL;
155
156   xbt_dict_get(cpu_set, name, &cpu);
157
158   return cpu;
159 }
160
161 static const char *get_resource_name(void *resource_id)
162 {
163   return ((cpu_t) resource_id)->name;
164 }
165
166 static int resource_used(void *resource_id)
167 {
168   return lmm_constraint_used(maxmin_system,
169                              ((cpu_t) resource_id)->constraint);
170 }
171
172 static void action_free(surf_action_t action)
173 {
174   surf_action_cpu_t Action = (surf_action_cpu_t) action;
175
176   xbt_swag_remove(action, action->state_set);
177   lmm_variable_free(maxmin_system, Action->variable);
178   xbt_free(action);
179
180   return;
181 }
182
183 static void action_cancel(surf_action_t action)
184 {
185   return;
186 }
187
188 static void action_recycle(surf_action_t action)
189 {
190   return;
191 }
192
193 static void action_change_state(surf_action_t action,
194                                 e_surf_action_state_t state)
195 {
196   surf_action_change_state(action, state);
197   return;
198 }
199
200 static xbt_heap_float_t share_resources(xbt_heap_float_t now)
201 {
202   surf_action_cpu_t action = NULL;
203   xbt_swag_t running_actions =
204       surf_cpu_resource->common_public->states.running_action_set;
205   xbt_maxmin_float_t min = -1;
206   xbt_maxmin_float_t value = -1;
207   lmm_solve(maxmin_system);
208
209   action = xbt_swag_getFirst(running_actions);
210   if (!action)
211     return 0.0;
212   value = lmm_variable_getvalue(action->variable);
213   min = action->generic_action.remains / value;
214
215   xbt_swag_foreach(action, running_actions) {
216     value = action->generic_action.remains /
217         lmm_variable_getvalue(action->variable);
218     if (value < min)
219       min = value;
220   }
221
222   return min;
223 }
224
225 static void update_actions_state(xbt_heap_float_t now,
226                                  xbt_heap_float_t delta)
227 {
228   surf_action_cpu_t action = NULL;
229   surf_action_cpu_t next_action = NULL;
230   xbt_swag_t running_actions =
231       surf_cpu_resource->common_public->states.running_action_set;
232   xbt_swag_t failed_actions =
233       surf_cpu_resource->common_public->states.failed_action_set;
234
235   xbt_swag_foreach_safe(action, next_action, running_actions) {
236     action->generic_action.remains -=
237         lmm_variable_getvalue(action->variable) * delta;
238 /*     if(action->generic_action.remains<.00001) action->generic_action.remains=0; */
239     if (action->generic_action.remains <= 0) {
240       action_change_state((surf_action_t) action, SURF_ACTION_DONE);
241     } else {                    /* Need to check that none of the resource has failed */
242       lmm_constraint_t cnst = NULL;
243       int i = 0;
244       cpu_t cpu = NULL;
245
246       while ((cnst =
247               lmm_get_cnst_from_var(maxmin_system, action->variable,
248                                     i++))) {
249         cpu = lmm_constraint_id(cnst);
250         if (cpu->state_current == SURF_CPU_OFF) {
251           action_change_state((surf_action_t) action, SURF_ACTION_FAILED);
252           break;
253         }
254       }
255     }
256   }
257
258   xbt_swag_foreach_safe(action, next_action, failed_actions) {
259     lmm_variable_disable(maxmin_system, action->variable);
260   }
261
262   return;
263 }
264
265 static void update_resource_state(void *id,
266                                   tmgr_trace_event_t event_type,
267                                   xbt_maxmin_float_t value)
268 {
269   cpu_t cpu = id;
270
271   printf("[" XBT_HEAP_FLOAT_T "] Asking to update \"%s\" with value "
272          XBT_MAXMIN_FLOAT_T " for event %p\n", surf_get_clock(), cpu->name,
273          value, event_type);
274
275   if (event_type == cpu->power_event) {
276     cpu->power_current = value;
277     lmm_update_constraint_bound(cpu->constraint,
278                                 cpu->power_current * cpu->power_scale);
279   } else if (event_type == cpu->state_event) {
280     if (value > 0)
281       cpu->state_current = SURF_CPU_ON;
282     else
283       cpu->state_current = SURF_CPU_OFF;
284   } else {
285     CRITICAL0("Unknown event ! \n");
286     xbt_abort();
287   }
288
289   return;
290 }
291
292 static surf_action_t execute(void *cpu, xbt_maxmin_float_t size)
293 {
294   surf_action_cpu_t action = NULL;
295   cpu_t CPU = cpu;
296
297   action = xbt_new0(s_surf_action_cpu_t, 1);
298
299   action->generic_action.cost = size;
300   action->generic_action.remains = size;
301   action->generic_action.start = -1.0;
302   action->generic_action.finish = -1.0;
303   action->generic_action.callback = cpu;
304   action->generic_action.resource_type =
305       (surf_resource_t) surf_cpu_resource;
306
307   if (CPU->state_current == SURF_CPU_ON)
308     action->generic_action.state_set =
309         surf_cpu_resource->common_public->states.running_action_set;
310   else
311     action->generic_action.state_set =
312         surf_cpu_resource->common_public->states.failed_action_set;
313   xbt_swag_insert(action, action->generic_action.state_set);
314
315   action->variable = lmm_variable_new(maxmin_system, action, 1.0, -1.0, 1);
316   lmm_expand(maxmin_system, ((cpu_t) cpu)->constraint, action->variable,
317              1.0);
318
319   return (surf_action_t) action;
320 }
321
322 static e_surf_cpu_state_t get_state(void *cpu)
323 {
324   return SURF_CPU_OFF;
325 }
326
327 static void finalize(void)
328 {
329   xbt_dict_free(&cpu_set);
330   xbt_swag_free(surf_cpu_resource->common_public->states.ready_action_set);
331   xbt_swag_free(surf_cpu_resource->common_public->states.
332                 running_action_set);
333   xbt_swag_free(surf_cpu_resource->common_public->states.
334                 failed_action_set);
335   xbt_swag_free(surf_cpu_resource->common_public->states.done_action_set);
336   xbt_free(surf_cpu_resource->common_public);
337   xbt_free(surf_cpu_resource->common_private);
338   xbt_free(surf_cpu_resource->extension_public);
339
340   xbt_free(surf_cpu_resource);
341   surf_cpu_resource = NULL;
342 }
343
344 static void surf_cpu_resource_init_internal(void)
345 {
346   s_surf_action_t action;
347
348   surf_cpu_resource = xbt_new0(s_surf_cpu_resource_t, 1);
349
350   surf_cpu_resource->common_private =
351       xbt_new0(s_surf_resource_private_t, 1);
352   surf_cpu_resource->common_public = xbt_new0(s_surf_resource_public_t, 1);
353 /*   surf_cpu_resource->extension_private = xbt_new0(s_surf_cpu_resource_extension_private_t,1); */
354   surf_cpu_resource->extension_public =
355       xbt_new0(s_surf_cpu_resource_extension_public_t, 1);
356
357   surf_cpu_resource->common_public->states.ready_action_set =
358       xbt_swag_new(xbt_swag_offset(action, state_hookup));
359   surf_cpu_resource->common_public->states.running_action_set =
360       xbt_swag_new(xbt_swag_offset(action, state_hookup));
361   surf_cpu_resource->common_public->states.failed_action_set =
362       xbt_swag_new(xbt_swag_offset(action, state_hookup));
363   surf_cpu_resource->common_public->states.done_action_set =
364       xbt_swag_new(xbt_swag_offset(action, state_hookup));
365
366   surf_cpu_resource->common_public->name_service = name_service;
367   surf_cpu_resource->common_public->get_resource_name = get_resource_name;
368   surf_cpu_resource->common_public->resource_used = resource_used;
369   surf_cpu_resource->common_public->action_get_state =
370       surf_action_get_state;
371   surf_cpu_resource->common_public->action_free = action_free;
372   surf_cpu_resource->common_public->action_cancel = action_cancel;
373   surf_cpu_resource->common_public->action_recycle = action_recycle;
374   surf_cpu_resource->common_public->action_change_state =
375       action_change_state;
376
377   surf_cpu_resource->common_private->share_resources = share_resources;
378   surf_cpu_resource->common_private->update_actions_state =
379       update_actions_state;
380   surf_cpu_resource->common_private->update_resource_state =
381       update_resource_state;
382   surf_cpu_resource->common_private->finalize = finalize;
383
384   surf_cpu_resource->extension_public->execute = execute;
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   surf_cpu_resource_init_internal();
395   parse_file(filename);
396   xbt_dynar_push(resource_list, &surf_cpu_resource);
397 }