Logo AND Algorithmique Numérique Distribuée

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