Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
task->data (formerly callback) could not be set by end-users.
[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", surf_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", surf_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", surf_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.resource_type =
274       (surf_resource_t) surf_cpu_resource;
275
276   if (CPU->state_current == SURF_CPU_ON)
277     action->generic_action.state_set =
278         surf_cpu_resource->common_public->states.running_action_set;
279   else
280     action->generic_action.state_set =
281         surf_cpu_resource->common_public->states.failed_action_set;
282   xbt_swag_insert(action, action->generic_action.state_set);
283
284   action->variable = lmm_variable_new(maxmin_system, action, 1.0, -1.0, 1);
285   lmm_expand(maxmin_system, ((cpu_t) cpu)->constraint, action->variable,
286              1.0);
287
288   return (surf_action_t) action;
289 }
290
291 static surf_action_t action_sleep(void *cpu, double duration)
292 {
293   surf_action_cpu_t action = NULL;
294
295   action = (surf_action_cpu_t) execute(cpu, 1.0);
296   action->generic_action.max_duration = duration;
297   lmm_update_variable_weight(maxmin_system, action->variable, 0.0);
298
299   return (surf_action_t) action;
300 }
301
302 static void action_suspend(surf_action_t action)
303 {
304   lmm_update_variable_weight(maxmin_system,
305                              ((surf_action_cpu_t) action)->variable, 0.0);
306 }
307
308 static void action_resume(surf_action_t action)
309 {
310   lmm_update_variable_weight(maxmin_system,
311                              ((surf_action_cpu_t) action)->variable, 1.0);
312 }
313
314 static int action_is_suspended(surf_action_t action)
315 {
316   return (lmm_get_variable_weight(maxmin_system, ((surf_action_cpu_t) action)->variable) == 0.0);
317 }
318
319 static e_surf_cpu_state_t get_state(void *cpu)
320 {
321   return ((cpu_t) cpu)->state_current;
322 }
323
324 static void finalize(void)
325 {
326   xbt_dict_free(&cpu_set);
327   xbt_swag_free(surf_cpu_resource->common_public->states.ready_action_set);
328   xbt_swag_free(surf_cpu_resource->common_public->states.
329                 running_action_set);
330   xbt_swag_free(surf_cpu_resource->common_public->states.
331                 failed_action_set);
332   xbt_swag_free(surf_cpu_resource->common_public->states.done_action_set);
333   xbt_free(surf_cpu_resource->common_public);
334   xbt_free(surf_cpu_resource->common_private);
335   xbt_free(surf_cpu_resource->extension_public);
336
337   xbt_free(surf_cpu_resource);
338   surf_cpu_resource = NULL;
339 }
340
341 static void surf_cpu_resource_init_internal(void)
342 {
343   s_surf_action_t action;
344
345   surf_cpu_resource = xbt_new0(s_surf_cpu_resource_t, 1);
346
347   surf_cpu_resource->common_private =
348       xbt_new0(s_surf_resource_private_t, 1);
349   surf_cpu_resource->common_public = xbt_new0(s_surf_resource_public_t, 1);
350
351   surf_cpu_resource->extension_public =
352       xbt_new0(s_surf_cpu_resource_extension_public_t, 1);
353
354   surf_cpu_resource->common_public->states.ready_action_set =
355       xbt_swag_new(xbt_swag_offset(action, state_hookup));
356   surf_cpu_resource->common_public->states.running_action_set =
357       xbt_swag_new(xbt_swag_offset(action, state_hookup));
358   surf_cpu_resource->common_public->states.failed_action_set =
359       xbt_swag_new(xbt_swag_offset(action, state_hookup));
360   surf_cpu_resource->common_public->states.done_action_set =
361       xbt_swag_new(xbt_swag_offset(action, state_hookup));
362
363   surf_cpu_resource->common_public->name_service = name_service;
364   surf_cpu_resource->common_public->get_resource_name = get_resource_name;
365   surf_cpu_resource->common_public->action_get_state =
366       surf_action_get_state;
367   surf_cpu_resource->common_public->action_free = action_free;
368   surf_cpu_resource->common_public->action_cancel = action_cancel;
369   surf_cpu_resource->common_public->action_recycle = action_recycle;
370   surf_cpu_resource->common_public->action_change_state =
371       action_change_state;
372   surf_cpu_resource->common_public->action_set_data = surf_action_set_data;
373   surf_cpu_resource->common_public->name = "CPU";
374
375   surf_cpu_resource->common_private->resource_used = resource_used;
376   surf_cpu_resource->common_private->share_resources = share_resources;
377   surf_cpu_resource->common_private->update_actions_state =
378       update_actions_state;
379   surf_cpu_resource->common_private->update_resource_state =
380       update_resource_state;
381   surf_cpu_resource->common_private->finalize = finalize;
382
383   surf_cpu_resource->extension_public->execute = execute;
384   surf_cpu_resource->extension_public->sleep = action_sleep;
385   surf_cpu_resource->extension_public->suspend = action_suspend;
386   surf_cpu_resource->extension_public->resume = action_resume;
387   surf_cpu_resource->extension_public->is_suspended = action_is_suspended;
388
389   surf_cpu_resource->extension_public->get_state = get_state;
390
391   cpu_set = xbt_dict_new();
392
393   xbt_assert0(maxmin_system, "surf_init has to be called first!");
394 }
395
396 void surf_cpu_resource_init(const char *filename)
397 {
398   if (surf_cpu_resource)
399     return;
400   surf_cpu_resource_init_internal();
401   parse_file(filename);
402   xbt_dynar_push(resource_list, &surf_cpu_resource);
403 }