Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update workstation handling in SimDag. Change the prefix of SimDag functions with...
[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(surf_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   free(((cpu_Cas01_t)cpu)->name);
20   free(cpu);
21 }
22
23 static cpu_Cas01_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_Cas01_t cpu = xbt_new0(s_cpu_Cas01_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_surfxml_cpu_name);
64   surf_parse_get_double(&power_scale,A_surfxml_cpu_power);
65   surf_parse_get_double(&power_initial,A_surfxml_cpu_availability);
66   surf_parse_get_trace(&power_trace,A_surfxml_cpu_availability_file);
67
68   xbt_assert0((A_surfxml_cpu_state==A_surfxml_cpu_state_ON)||
69               (A_surfxml_cpu_state==A_surfxml_cpu_state_OFF),
70               "Invalid state");
71   if (A_surfxml_cpu_state==A_surfxml_cpu_state_ON) state_initial = SURF_CPU_ON;
72   if (A_surfxml_cpu_state==A_surfxml_cpu_state_OFF) state_initial = SURF_CPU_OFF;
73   surf_parse_get_trace(&state_trace,A_surfxml_cpu_state_file);
74
75   cpu_new(name, power_scale, power_initial, power_trace, state_initial,
76           state_trace);
77 }
78
79 static void parse_file(const char *file)
80 {
81   surf_parse_reset_parser();
82   ETag_surfxml_cpu_fun=parse_cpu;
83   surf_parse_open(file);
84   xbt_assert1((!surf_parse()),"Parse error in %s",file);
85   surf_parse_close();
86 }
87
88 static void *name_service(const char *name)
89 {
90   return xbt_dict_get_or_null(cpu_set, name);
91 }
92
93 static const char *get_resource_name(void *resource_id)
94 {
95   return ((cpu_Cas01_t) resource_id)->name;
96 }
97
98 static int resource_used(void *resource_id)
99 {
100   return lmm_constraint_used(maxmin_system,
101                              ((cpu_Cas01_t) resource_id)->constraint);
102 }
103
104 static int action_free(surf_action_t action)
105 {
106   action->using--;
107   if(!action->using) {
108     xbt_swag_remove(action, action->state_set);
109     if(((surf_action_cpu_Cas01_t)action)->variable)
110       lmm_variable_free(maxmin_system, ((surf_action_cpu_Cas01_t)action)->variable);
111     free(action);
112     return 1;
113   }
114   return 0;
115 }
116
117 static void action_use(surf_action_t action)
118 {
119   action->using++;
120 }
121
122 static void action_cancel(surf_action_t action)
123 {
124   surf_action_change_state(action, SURF_ACTION_FAILED);
125   return;
126 }
127
128 static void action_recycle(surf_action_t action)
129 {
130   DIE_IMPOSSIBLE;
131 }
132
133 static void action_change_state(surf_action_t action,
134                                 e_surf_action_state_t state)
135 {
136 /*   if((state==SURF_ACTION_DONE) || (state==SURF_ACTION_FAILED)) */
137 /*     if(((surf_action_cpu_Cas01_t)action)->variable) { */
138 /*       lmm_variable_disable(maxmin_system, ((surf_action_cpu_Cas01_t)action)->variable); */
139 /*       ((surf_action_cpu_Cas01_t)action)->variable = NULL; */
140 /*     } */
141
142   surf_action_change_state(action, state);
143   return;
144 }
145
146 static double share_resources(double now)
147 {
148   s_surf_action_cpu_Cas01_t action;
149   return generic_maxmin_share_resources(surf_cpu_resource->common_public->
150                                         states.running_action_set,
151                                         xbt_swag_offset(action, variable));
152 }
153
154 static void update_actions_state(double now, double delta)
155 {
156   surf_action_cpu_Cas01_t action = NULL;
157   surf_action_cpu_Cas01_t next_action = NULL;
158   xbt_swag_t running_actions =
159       surf_cpu_resource->common_public->states.running_action_set;
160   /* FIXME: UNUSED
161   xbt_swag_t failed_actions =
162       surf_cpu_resource->common_public->states.failed_action_set;
163   */
164
165   xbt_swag_foreach_safe(action, next_action, running_actions) {
166     surf_double_update(&(action->generic_action.remains),
167         lmm_variable_getvalue(action->variable) * delta);
168     if (action->generic_action.max_duration != NO_MAX_DURATION)
169       surf_double_update(&(action->generic_action.max_duration), delta);
170     if ((action->generic_action.remains <= 0) && 
171         (lmm_get_variable_weight(action->variable)>0)) {
172       action->generic_action.finish = surf_get_clock();
173       action_change_state((surf_action_t) action, SURF_ACTION_DONE);
174     } else if ((action->generic_action.max_duration != NO_MAX_DURATION) &&
175                (action->generic_action.max_duration <= 0)) {
176       action->generic_action.finish = surf_get_clock();
177       action_change_state((surf_action_t) action, SURF_ACTION_DONE);
178     } else {                    /* Need to check that none of the resource has failed */
179       lmm_constraint_t cnst = NULL;
180       int i = 0;
181       cpu_Cas01_t cpu = NULL;
182
183       while ((cnst =
184               lmm_get_cnst_from_var(maxmin_system, action->variable,
185                                     i++))) {
186         cpu = lmm_constraint_id(cnst);
187         if (cpu->state_current == SURF_CPU_OFF) {
188           action->generic_action.finish = surf_get_clock();
189           action_change_state((surf_action_t) action, SURF_ACTION_FAILED);
190           break;
191         }
192       }
193     }
194   }
195
196   return;
197 }
198
199 static void update_resource_state(void *id,
200                                   tmgr_trace_event_t event_type,
201                                   double value)
202 {
203   cpu_Cas01_t cpu = id;
204
205   if (event_type == cpu->power_event) {
206     cpu->power_current = value;
207     lmm_update_constraint_bound(maxmin_system, cpu->constraint,
208                                 cpu->power_current * cpu->power_scale);
209   } else if (event_type == cpu->state_event) {
210     if (value > 0)
211       cpu->state_current = SURF_CPU_ON;
212     else
213       cpu->state_current = SURF_CPU_OFF;
214   } else {
215     CRITICAL0("Unknown event ! \n");
216     xbt_abort();
217   }
218
219   return;
220 }
221
222 static surf_action_t execute(void *cpu, double size)
223 {
224   surf_action_cpu_Cas01_t action = NULL;
225   cpu_Cas01_t CPU = cpu;
226
227   XBT_IN2("(%s,%g)",CPU->name,size);
228   action = xbt_new0(s_surf_action_cpu_Cas01_t, 1);
229
230   action->generic_action.using = 1;
231   action->generic_action.cost = size;
232   action->generic_action.remains = size;
233   action->generic_action.priority = 1.0;
234   action->generic_action.max_duration = NO_MAX_DURATION;
235   action->generic_action.start = surf_get_clock();
236   action->generic_action.finish = -1.0;
237   action->generic_action.resource_type =
238       (surf_resource_t) surf_cpu_resource;
239
240   if (CPU->state_current == SURF_CPU_ON)
241     action->generic_action.state_set =
242         surf_cpu_resource->common_public->states.running_action_set;
243   else
244     action->generic_action.state_set =
245         surf_cpu_resource->common_public->states.failed_action_set;
246   xbt_swag_insert(action, action->generic_action.state_set);
247
248   action->variable = lmm_variable_new(maxmin_system, action, 
249                                       action->generic_action.priority, -1.0, 1);
250   lmm_expand(maxmin_system, CPU->constraint, action->variable,
251              1.0);
252   XBT_OUT;
253   return (surf_action_t) action;
254 }
255
256 static surf_action_t action_sleep(void *cpu, double duration)
257 {
258   surf_action_cpu_Cas01_t action = NULL;
259
260   XBT_IN2("(%s,%g)",((cpu_Cas01_t)cpu)->name,duration);
261   action = (surf_action_cpu_Cas01_t) execute(cpu, 1.0);
262   action->generic_action.max_duration = duration;
263   lmm_update_variable_weight(maxmin_system, action->variable, 0.0);
264   XBT_OUT;
265   return (surf_action_t) action;
266 }
267
268 static void action_suspend(surf_action_t action)
269 {
270   XBT_IN1("(%p)",action);
271   lmm_update_variable_weight(maxmin_system,
272                              ((surf_action_cpu_Cas01_t) action)->variable, 0.0);
273   XBT_OUT;
274 }
275
276 static void action_resume(surf_action_t action)
277 {
278   XBT_IN1("(%p)",action);
279   lmm_update_variable_weight(maxmin_system,
280                              ((surf_action_cpu_Cas01_t) action)->variable, 
281                              action->priority);
282   XBT_OUT;
283 }
284
285 static int action_is_suspended(surf_action_t action)
286 {
287   return (lmm_get_variable_weight(((surf_action_cpu_Cas01_t) action)->variable) == 0.0);
288 }
289
290 static void action_set_max_duration(surf_action_t action, double duration)
291 {
292   XBT_IN2("(%p,%g)",action,duration);
293   action->max_duration = duration;
294   XBT_OUT;
295 }
296
297 static void action_set_priority(surf_action_t action, double priority)
298 {
299   XBT_IN2("(%p,%g)",action,priority);
300   action->priority = priority;
301   XBT_OUT;
302 }
303
304 static e_surf_cpu_state_t get_state(void *cpu)
305 {
306   return ((cpu_Cas01_t) cpu)->state_current;
307 }
308
309 static double get_speed(void *cpu, double load)
310 {
311   return load*(((cpu_Cas01_t) cpu)->power_scale);
312 }
313
314 static void finalize(void)
315 {
316   xbt_dict_free(&cpu_set);
317   xbt_swag_free(surf_cpu_resource->common_public->states.ready_action_set);
318   xbt_swag_free(surf_cpu_resource->common_public->states.
319                 running_action_set);
320   xbt_swag_free(surf_cpu_resource->common_public->states.
321                 failed_action_set);
322   xbt_swag_free(surf_cpu_resource->common_public->states.done_action_set);
323   free(surf_cpu_resource->common_public);
324   free(surf_cpu_resource->common_private);
325   free(surf_cpu_resource->extension_public);
326
327   free(surf_cpu_resource);
328   surf_cpu_resource = NULL;
329 }
330
331 static void surf_cpu_resource_init_internal(void)
332 {
333   s_surf_action_t action;
334
335   surf_cpu_resource = xbt_new0(s_surf_cpu_resource_t, 1);
336
337   surf_cpu_resource->common_private =
338       xbt_new0(s_surf_resource_private_t, 1);
339   surf_cpu_resource->common_public = xbt_new0(s_surf_resource_public_t, 1);
340
341   surf_cpu_resource->extension_public =
342       xbt_new0(s_surf_cpu_resource_extension_public_t, 1);
343
344   surf_cpu_resource->common_public->states.ready_action_set =
345       xbt_swag_new(xbt_swag_offset(action, state_hookup));
346   surf_cpu_resource->common_public->states.running_action_set =
347       xbt_swag_new(xbt_swag_offset(action, state_hookup));
348   surf_cpu_resource->common_public->states.failed_action_set =
349       xbt_swag_new(xbt_swag_offset(action, state_hookup));
350   surf_cpu_resource->common_public->states.done_action_set =
351       xbt_swag_new(xbt_swag_offset(action, state_hookup));
352
353   surf_cpu_resource->common_public->name_service = name_service;
354   surf_cpu_resource->common_public->get_resource_name = get_resource_name;
355   surf_cpu_resource->common_public->action_get_state =
356       surf_action_get_state;
357   surf_cpu_resource->common_public->action_free = action_free;
358   surf_cpu_resource->common_public->action_use = action_use;
359   surf_cpu_resource->common_public->action_cancel = action_cancel;
360   surf_cpu_resource->common_public->action_recycle = action_recycle;
361   surf_cpu_resource->common_public->action_change_state =
362       action_change_state;
363   surf_cpu_resource->common_public->action_set_data = surf_action_set_data;
364   surf_cpu_resource->common_public->name = "CPU";
365
366   surf_cpu_resource->common_private->resource_used = resource_used;
367   surf_cpu_resource->common_private->share_resources = share_resources;
368   surf_cpu_resource->common_private->update_actions_state =
369       update_actions_state;
370   surf_cpu_resource->common_private->update_resource_state =
371       update_resource_state;
372   surf_cpu_resource->common_private->finalize = finalize;
373
374   surf_cpu_resource->common_public->suspend = action_suspend;
375   surf_cpu_resource->common_public->resume = action_resume;
376   surf_cpu_resource->common_public->is_suspended = action_is_suspended;
377   surf_cpu_resource->common_public->set_max_duration = action_set_max_duration;
378   surf_cpu_resource->common_public->set_priority = action_set_priority;
379   surf_cpu_resource->extension_public->execute = execute;
380   surf_cpu_resource->extension_public->sleep = action_sleep;
381
382   surf_cpu_resource->extension_public->get_state = get_state;
383   surf_cpu_resource->extension_public->get_speed = get_speed;
384
385   cpu_set = xbt_dict_new();
386
387   xbt_assert0(maxmin_system, "surf_init has to be called first!");
388 }
389
390 /*********************************************************************/
391 /* Basic sharing model for CPU: that is where all this started... ;) */
392 /*********************************************************************/
393 /* @InProceedings{casanova01simgrid, */
394 /*   author =       "H. Casanova", */
395 /*   booktitle =    "Proceedings of the IEEE Symposium on Cluster Computing */
396 /*                  and the Grid (CCGrid'01)", */
397 /*   publisher =    "IEEE Computer Society", */
398 /*   title =        "Simgrid: {A} Toolkit for the Simulation of Application */
399 /*                  Scheduling", */
400 /*   year =         "2001", */
401 /*   month =        may, */
402 /*   note =         "Available at */
403 /*                  \url{http://grail.sdsc.edu/papers/simgrid_ccgrid01.ps.gz}." */
404 /* } */
405 void surf_cpu_resource_init_Cas01(const char *filename)
406 {
407   if (surf_cpu_resource)
408     return;
409   surf_cpu_resource_init_internal();
410   parse_file(filename);
411   xbt_dynar_push(resource_list, &surf_cpu_resource);
412 }