Logo AND Algorithmique Numérique Distribuée

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