Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Renamed resource initialization functions. Now they are postfixed by a alpha-style...
[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   xbt_free(((cpu_t)cpu)->name);
20   xbt_free(cpu);
21 }
22
23 /* power_scale is the basic power of the cpu when the cpu is
24    completely available. power_initial is therefore expected to be
25    comprised between 0.0 and 1.0, just as the values of power_trace.
26    state_trace values mean SURF_CPU_ON if >0 and SURF_CPU_OFF
27    otherwise.
28 */
29 static cpu_t cpu_new(char *name, double power_scale,
30                      double power_initial,
31                      tmgr_trace_t power_trace,
32                      e_surf_cpu_state_t state_initial,
33                      tmgr_trace_t state_trace)
34 {
35   cpu_t cpu = xbt_new0(s_cpu_t, 1);
36
37   cpu->resource = (surf_resource_t) surf_cpu_resource;
38   cpu->name = name;
39   cpu->power_scale = power_scale;
40   cpu->power_current = power_initial;
41 /*   cpu->power_trace = power_trace; */
42   if (power_trace)
43     cpu->power_event =
44         tmgr_history_add_trace(history, power_trace, 0.0, 0, cpu);
45
46   cpu->state_current = state_initial;
47 /*   cpu->state_trace = state_trace; */
48   if (state_trace)
49     cpu->state_event =
50         tmgr_history_add_trace(history, state_trace, 0.0, 0, cpu);
51
52   cpu->constraint =
53       lmm_constraint_new(maxmin_system, cpu,
54                          cpu->power_current * cpu->power_scale);
55
56   xbt_dict_set(cpu_set, name, cpu, cpu_free);
57
58   return cpu;
59 }
60
61 /*  
62    Semantic:  name       scale     initial     power     initial     state
63                                     power      trace      state      trace
64    
65    Token:   TOKEN_WORD TOKEN_WORD TOKEN_WORD TOKEN_WORD TOKEN_WORD TOKEN_WORD
66    Type:     string      double     double     string     ON/OFF     string
67 */
68
69 static void parse_cpu(void)
70 {
71   char *name = NULL;
72   double power_scale = 0.0;
73   double power_initial = 0.0;
74   tmgr_trace_t power_trace = NULL;
75   e_surf_cpu_state_t state_initial = SURF_CPU_OFF;
76   tmgr_trace_t state_trace = NULL;
77
78   name = xbt_strdup(A_cpu_name);
79   surf_parse_get_double(&power_scale,A_cpu_power);
80   surf_parse_get_double(&power_initial,A_cpu_availability);
81   surf_parse_get_trace(&power_trace,A_cpu_availability_file);
82
83   xbt_assert0((A_cpu_state==A_cpu_state_ON)||(A_cpu_state==A_cpu_state_OFF),
84               "Invalid state")
85   if (A_cpu_state==A_cpu_state_ON) state_initial = SURF_CPU_ON;
86   if (A_cpu_state==A_cpu_state_OFF) state_initial = SURF_CPU_OFF;
87   surf_parse_get_trace(&state_trace,A_cpu_state_file);
88
89   cpu_new(name, power_scale, power_initial, power_trace, state_initial,
90           state_trace);
91 }
92
93 static void parse_file(const char *file)
94 {
95   surf_parse_reset_parser();
96   ETag_cpu_fun=parse_cpu;
97   surf_parse_open(file);
98   xbt_assert1((!surf_parse()),"Parse error in %s",file);
99   surf_parse_close();
100 }
101
102 static void *name_service(const char *name)
103 {
104   void *cpu = NULL;
105
106   xbt_dict_get(cpu_set, name, &cpu);
107
108   return cpu;
109 }
110
111 static const char *get_resource_name(void *resource_id)
112 {
113   return ((cpu_t) resource_id)->name;
114 }
115
116 static int resource_used(void *resource_id)
117 {
118   return lmm_constraint_used(maxmin_system,
119                              ((cpu_t) resource_id)->constraint);
120 }
121
122 static void action_free(surf_action_t action)
123 {
124   surf_action_cpu_t Action = (surf_action_cpu_t) action;
125
126   xbt_swag_remove(action, action->state_set);
127   lmm_variable_free(maxmin_system, Action->variable);
128   xbt_free(action);
129
130   return;
131 }
132
133 static void action_cancel(surf_action_t action)
134 {
135   return;
136 }
137
138 static void action_recycle(surf_action_t action)
139 {
140   return;
141 }
142
143 static void action_change_state(surf_action_t action,
144                                 e_surf_action_state_t state)
145 {
146   surf_action_change_state(action, state);
147   return;
148 }
149
150 static double share_resources(double now)
151 {
152   s_surf_action_cpu_t action;
153   return generic_maxmin_share_resources(surf_cpu_resource->common_public->
154                                         states.running_action_set,
155                                         xbt_swag_offset(action, variable));
156 }
157
158 static void update_actions_state(double now, double delta)
159 {
160   surf_action_cpu_t action = NULL;
161   surf_action_cpu_t next_action = NULL;
162   xbt_swag_t running_actions =
163       surf_cpu_resource->common_public->states.running_action_set;
164   xbt_swag_t failed_actions =
165       surf_cpu_resource->common_public->states.failed_action_set;
166
167   xbt_swag_foreach_safe(action, next_action, running_actions) {
168     action->generic_action.remains -=
169         lmm_variable_getvalue(action->variable) * delta;
170     if (action->generic_action.max_duration != NO_MAX_DURATION)
171       action->generic_action.max_duration -= delta;
172 /*     if(action->generic_action.remains<.00001) action->generic_action.remains=0; */
173     if ((action->generic_action.remains <= 0) && 
174         (lmm_get_variable_weight(action->variable)>0)) {
175       action->generic_action.finish = surf_get_clock();
176       action_change_state((surf_action_t) action, SURF_ACTION_DONE);
177     } else if ((action->generic_action.max_duration != NO_MAX_DURATION) &&
178                (action->generic_action.max_duration <= 0)) {
179       action->generic_action.finish = surf_get_clock();
180       action_change_state((surf_action_t) action, SURF_ACTION_DONE);
181     } else {                    /* Need to check that none of the resource has failed */
182       lmm_constraint_t cnst = NULL;
183       int i = 0;
184       cpu_t cpu = NULL;
185
186       while ((cnst =
187               lmm_get_cnst_from_var(maxmin_system, action->variable,
188                                     i++))) {
189         cpu = lmm_constraint_id(cnst);
190         if (cpu->state_current == SURF_CPU_OFF) {
191           action->generic_action.finish = surf_get_clock();
192           action_change_state((surf_action_t) action, SURF_ACTION_FAILED);
193           break;
194         }
195       }
196     }
197   }
198
199   xbt_swag_foreach_safe(action, next_action, failed_actions) {
200     lmm_variable_disable(maxmin_system, action->variable);
201   }
202
203   return;
204 }
205
206 static void update_resource_state(void *id,
207                                   tmgr_trace_event_t event_type,
208                                   double value)
209 {
210   cpu_t cpu = id;
211
212 /*   printf("[" "%lg" "] Asking to update CPU \"%s\" with value " */
213 /*       "%lg" " for event %p\n", surf_get_clock(), cpu->name, */
214 /*       value, event_type); */
215
216   if (event_type == cpu->power_event) {
217     cpu->power_current = value;
218     lmm_update_constraint_bound(maxmin_system, cpu->constraint,
219                                 cpu->power_current * cpu->power_scale);
220   } else if (event_type == cpu->state_event) {
221     if (value > 0)
222       cpu->state_current = SURF_CPU_ON;
223     else
224       cpu->state_current = SURF_CPU_OFF;
225   } else {
226     CRITICAL0("Unknown event ! \n");
227     xbt_abort();
228   }
229
230   return;
231 }
232
233 static surf_action_t execute(void *cpu, double size)
234 {
235   surf_action_cpu_t action = NULL;
236   cpu_t CPU = cpu;
237
238   action = xbt_new0(s_surf_action_cpu_t, 1);
239
240   action->generic_action.cost = size;
241   action->generic_action.remains = size;
242   action->generic_action.max_duration = NO_MAX_DURATION;
243   action->generic_action.start = surf_get_clock();
244   action->generic_action.finish = -1.0;
245   action->generic_action.resource_type =
246       (surf_resource_t) surf_cpu_resource;
247
248   if (CPU->state_current == SURF_CPU_ON)
249     action->generic_action.state_set =
250         surf_cpu_resource->common_public->states.running_action_set;
251   else
252     action->generic_action.state_set =
253         surf_cpu_resource->common_public->states.failed_action_set;
254   xbt_swag_insert(action, action->generic_action.state_set);
255
256   action->variable = lmm_variable_new(maxmin_system, action, 1.0, -1.0, 1);
257   lmm_expand(maxmin_system, ((cpu_t) cpu)->constraint, action->variable,
258              1.0);
259
260   return (surf_action_t) action;
261 }
262
263 static surf_action_t action_sleep(void *cpu, double duration)
264 {
265   surf_action_cpu_t action = NULL;
266
267   action = (surf_action_cpu_t) execute(cpu, 1.0);
268   action->generic_action.max_duration = duration;
269   lmm_update_variable_weight(maxmin_system, action->variable, 0.0);
270
271   return (surf_action_t) action;
272 }
273
274 static void action_suspend(surf_action_t action)
275 {
276   lmm_update_variable_weight(maxmin_system,
277                              ((surf_action_cpu_t) action)->variable, 0.0);
278 }
279
280 static void action_resume(surf_action_t action)
281 {
282   lmm_update_variable_weight(maxmin_system,
283                              ((surf_action_cpu_t) action)->variable, 1.0);
284 }
285
286 static int action_is_suspended(surf_action_t action)
287 {
288   return (lmm_get_variable_weight(((surf_action_cpu_t) action)->variable) == 0.0);
289 }
290
291 static e_surf_cpu_state_t get_state(void *cpu)
292 {
293   return ((cpu_t) cpu)->state_current;
294 }
295
296 static void finalize(void)
297 {
298   xbt_dict_free(&cpu_set);
299   xbt_swag_free(surf_cpu_resource->common_public->states.ready_action_set);
300   xbt_swag_free(surf_cpu_resource->common_public->states.
301                 running_action_set);
302   xbt_swag_free(surf_cpu_resource->common_public->states.
303                 failed_action_set);
304   xbt_swag_free(surf_cpu_resource->common_public->states.done_action_set);
305   xbt_free(surf_cpu_resource->common_public);
306   xbt_free(surf_cpu_resource->common_private);
307   xbt_free(surf_cpu_resource->extension_public);
308
309   xbt_free(surf_cpu_resource);
310   surf_cpu_resource = NULL;
311 }
312
313 static void surf_cpu_resource_init_internal(void)
314 {
315   s_surf_action_t action;
316
317   surf_cpu_resource = xbt_new0(s_surf_cpu_resource_t, 1);
318
319   surf_cpu_resource->common_private =
320       xbt_new0(s_surf_resource_private_t, 1);
321   surf_cpu_resource->common_public = xbt_new0(s_surf_resource_public_t, 1);
322
323   surf_cpu_resource->extension_public =
324       xbt_new0(s_surf_cpu_resource_extension_public_t, 1);
325
326   surf_cpu_resource->common_public->states.ready_action_set =
327       xbt_swag_new(xbt_swag_offset(action, state_hookup));
328   surf_cpu_resource->common_public->states.running_action_set =
329       xbt_swag_new(xbt_swag_offset(action, state_hookup));
330   surf_cpu_resource->common_public->states.failed_action_set =
331       xbt_swag_new(xbt_swag_offset(action, state_hookup));
332   surf_cpu_resource->common_public->states.done_action_set =
333       xbt_swag_new(xbt_swag_offset(action, state_hookup));
334
335   surf_cpu_resource->common_public->name_service = name_service;
336   surf_cpu_resource->common_public->get_resource_name = get_resource_name;
337   surf_cpu_resource->common_public->action_get_state =
338       surf_action_get_state;
339   surf_cpu_resource->common_public->action_free = action_free;
340   surf_cpu_resource->common_public->action_cancel = action_cancel;
341   surf_cpu_resource->common_public->action_recycle = action_recycle;
342   surf_cpu_resource->common_public->action_change_state =
343       action_change_state;
344   surf_cpu_resource->common_public->action_set_data = surf_action_set_data;
345   surf_cpu_resource->common_public->name = "CPU";
346
347   surf_cpu_resource->common_private->resource_used = resource_used;
348   surf_cpu_resource->common_private->share_resources = share_resources;
349   surf_cpu_resource->common_private->update_actions_state =
350       update_actions_state;
351   surf_cpu_resource->common_private->update_resource_state =
352       update_resource_state;
353   surf_cpu_resource->common_private->finalize = finalize;
354
355   surf_cpu_resource->extension_public->execute = execute;
356   surf_cpu_resource->extension_public->sleep = action_sleep;
357   surf_cpu_resource->extension_public->suspend = action_suspend;
358   surf_cpu_resource->extension_public->resume = action_resume;
359   surf_cpu_resource->extension_public->is_suspended = action_is_suspended;
360
361   surf_cpu_resource->extension_public->get_state = get_state;
362
363   cpu_set = xbt_dict_new();
364
365   xbt_assert0(maxmin_system, "surf_init has to be called first!");
366 }
367
368 /*********************************************************************/
369 /* Basic sharing model for CPU: that is all this started... ;)       */
370 /*********************************************************************/
371 /* @InProceedings{casanova01simgrid, */
372 /*   author =       "H. Casanova", */
373 /*   booktitle =    "Proceedings of the IEEE Symposium on Cluster Computing */
374 /*                  and the Grid (CCGrid'01)", */
375 /*   publisher =    "IEEE Computer Society", */
376 /*   title =        "Simgrid: {A} Toolkit for the Simulation of Application */
377 /*                  Scheduling", */
378 /*   year =         "2001", */
379 /*   month =        may, */
380 /*   note =         "Available at */
381 /*                  \url{http://grail.sdsc.edu/papers/simgrid_ccgrid01.ps.gz}." */
382 /* } */
383 void surf_cpu_resource_init_Cas01(const char *filename)
384 {
385   if (surf_cpu_resource)
386     return;
387   surf_cpu_resource_init_internal();
388   parse_file(filename);
389   xbt_dynar_push(resource_list, &surf_cpu_resource);
390 }