Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Check that the platform file does not define the same element (host or link) twice...
[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_model_t surf_cpu_model = NULL;
14 lmm_system_t cpu_maxmin_system = NULL;
15
16 xbt_dict_t cpu_set = NULL;
17
18 static void cpu_free(void *cpu)
19 {
20   free(((cpu_Cas01_t) cpu)->name);
21   free(cpu);
22 }
23
24 static cpu_Cas01_t cpu_new(char *name, double power_scale,
25                            double power_initial,
26                            tmgr_trace_t power_trace,
27                            e_surf_cpu_state_t state_initial,
28                            tmgr_trace_t state_trace,
29                            xbt_dict_t cpu_properties)
30 {
31   cpu_Cas01_t cpu = xbt_new0(s_cpu_Cas01_t, 1);
32   xbt_assert1(!xbt_dict_get_or_null(cpu_set, name),
33               "Host '%s' declared several times in the platform file",name);
34    
35   cpu->model = (surf_model_t) surf_cpu_model;
36   cpu->name = name;
37   cpu->power_scale = power_scale;
38   xbt_assert0(cpu->power_scale > 0, "Power has to be >0");
39   cpu->power_current = power_initial;
40   if (power_trace)
41     cpu->power_event =
42         tmgr_history_add_trace(history, power_trace, 0.0, 0, cpu);
43
44   cpu->state_current = state_initial;
45   if (state_trace)
46     cpu->state_event =
47         tmgr_history_add_trace(history, state_trace, 0.0, 0, cpu);
48
49   cpu->constraint =
50       lmm_constraint_new(cpu_maxmin_system, cpu,
51                          cpu->power_current * cpu->power_scale);
52
53   /*add the property set*/
54   cpu->properties = cpu_properties;
55
56   current_property_set = cpu_properties;
57
58   xbt_dict_set(cpu_set, name, cpu, cpu_free);
59
60   return cpu;
61 }
62
63
64 static void parse_cpu_init(void)
65 {
66   double power_scale = 0.0;
67   double power_initial = 0.0;
68   tmgr_trace_t power_trace = NULL;
69   e_surf_cpu_state_t state_initial = SURF_CPU_OFF;
70   tmgr_trace_t state_trace = NULL;
71
72   surf_parse_get_double(&power_scale, A_surfxml_host_power);
73   surf_parse_get_double(&power_initial, A_surfxml_host_availability);
74   surf_parse_get_trace(&power_trace, A_surfxml_host_availability_file);
75
76   xbt_assert0((A_surfxml_host_state == A_surfxml_host_state_ON) ||
77               (A_surfxml_host_state == A_surfxml_host_state_OFF),
78               "Invalid state");
79   if (A_surfxml_host_state == A_surfxml_host_state_ON)
80     state_initial = SURF_CPU_ON;
81   if (A_surfxml_host_state == A_surfxml_host_state_OFF)
82     state_initial = SURF_CPU_OFF;
83   surf_parse_get_trace(&state_trace, A_surfxml_host_state_file); 
84
85   current_property_set = xbt_dict_new();
86   cpu_new(xbt_strdup(A_surfxml_host_id), power_scale, power_initial, power_trace, state_initial,
87           state_trace,  current_property_set);
88
89 }
90
91 static void parse_file(const char *file)
92 {
93   surf_parse_reset_parser();
94   surfxml_add_callback(STag_surfxml_host_cb_list, parse_cpu_init);
95   surfxml_add_callback(STag_surfxml_prop_cb_list, &parse_properties);
96
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   return xbt_dict_get_or_null(cpu_set, name);
105 }
106
107 static const char *get_resource_name(void *resource_id)
108 {
109   return ((cpu_Cas01_t) resource_id)->name;
110 }
111
112 static int resource_used(void *resource_id)
113 {
114   return lmm_constraint_used(cpu_maxmin_system,
115                              ((cpu_Cas01_t) resource_id)->constraint);
116 }
117
118 static int action_free(surf_action_t action)
119 {
120   action->using--;
121   if (!action->using) {
122     xbt_swag_remove(action, action->state_set);
123     if (((surf_action_cpu_Cas01_t) action)->variable)
124       lmm_variable_free(cpu_maxmin_system,
125                         ((surf_action_cpu_Cas01_t) action)->variable);
126     free(action);
127     return 1;
128   }
129   return 0;
130 }
131
132 static void action_use(surf_action_t action)
133 {
134   action->using++;
135 }
136
137 static void action_cancel(surf_action_t action)
138 {
139   surf_action_change_state(action, SURF_ACTION_FAILED);
140   return;
141 }
142
143 static void action_recycle(surf_action_t action)
144 {
145   DIE_IMPOSSIBLE;
146 }
147
148 static void action_change_state(surf_action_t action,
149                                 e_surf_action_state_t state)
150 {
151 /*   if((state==SURF_ACTION_DONE) || (state==SURF_ACTION_FAILED)) */
152 /*     if(((surf_action_cpu_Cas01_t)action)->variable) { */
153 /*       lmm_variable_disable(cpu_maxmin_system, ((surf_action_cpu_Cas01_t)action)->variable); */
154 /*       ((surf_action_cpu_Cas01_t)action)->variable = NULL; */
155 /*     } */
156
157   surf_action_change_state(action, state);
158   return;
159 }
160
161 static double share_resources(double now)
162 {
163   s_surf_action_cpu_Cas01_t action;
164   return generic_maxmin_share_resources(surf_cpu_model->common_public->
165                                         states.running_action_set,
166                                         xbt_swag_offset(action, variable),
167                                         cpu_maxmin_system, lmm_solve);
168 }
169
170 static void update_actions_state(double now, double delta)
171 {
172   surf_action_cpu_Cas01_t action = NULL;
173   surf_action_cpu_Cas01_t next_action = NULL;
174   xbt_swag_t running_actions =
175       surf_cpu_model->common_public->states.running_action_set;
176   /* FIXME: UNUSED
177      xbt_swag_t failed_actions =
178      surf_cpu_model->common_public->states.failed_action_set;
179    */
180
181   xbt_swag_foreach_safe(action, next_action, running_actions) {
182     double_update(&(action->generic_action.remains),
183                   lmm_variable_getvalue(action->variable) * delta);
184     if (action->generic_action.max_duration != NO_MAX_DURATION)
185       double_update(&(action->generic_action.max_duration), delta);
186     if ((action->generic_action.remains <= 0) &&
187         (lmm_get_variable_weight(action->variable) > 0)) {
188       action->generic_action.finish = surf_get_clock();
189       action_change_state((surf_action_t) action, SURF_ACTION_DONE);
190     } else if ((action->generic_action.max_duration != NO_MAX_DURATION) &&
191                (action->generic_action.max_duration <= 0)) {
192       action->generic_action.finish = surf_get_clock();
193       action_change_state((surf_action_t) action, SURF_ACTION_DONE);
194     } else {                    /* Need to check that none of the model has failed */
195       lmm_constraint_t cnst = NULL;
196       int i = 0;
197       cpu_Cas01_t cpu = NULL;
198
199       while ((cnst =
200               lmm_get_cnst_from_var(cpu_maxmin_system, action->variable,
201                                     i++))) {
202         cpu = lmm_constraint_id(cnst);
203         if (cpu->state_current == SURF_CPU_OFF) {
204           action->generic_action.finish = surf_get_clock();
205           action_change_state((surf_action_t) action, SURF_ACTION_FAILED);
206           break;
207         }
208       }
209     }
210   }
211
212   return;
213 }
214
215 static void update_resource_state(void *id,
216                                   tmgr_trace_event_t event_type,
217                                   double value)
218 {
219   cpu_Cas01_t cpu = id;
220
221   if (event_type == cpu->power_event) {
222     cpu->power_current = value;
223     lmm_update_constraint_bound(cpu_maxmin_system, cpu->constraint,
224                                 cpu->power_current * cpu->power_scale);
225   } else if (event_type == cpu->state_event) {
226     if (value > 0)
227       cpu->state_current = SURF_CPU_ON;
228     else
229       cpu->state_current = SURF_CPU_OFF;
230   } else {
231     CRITICAL0("Unknown event ! \n");
232     xbt_abort();
233   }
234
235   return;
236 }
237
238 static surf_action_t execute(void *cpu, double size)
239 {
240   surf_action_cpu_Cas01_t action = NULL;
241   cpu_Cas01_t CPU = cpu;
242
243   XBT_IN2("(%s,%g)", CPU->name, size);
244   action = xbt_new0(s_surf_action_cpu_Cas01_t, 1);
245
246   action->generic_action.using = 1;
247   action->generic_action.cost = size;
248   action->generic_action.remains = size;
249   action->generic_action.priority = 1.0;
250   action->generic_action.max_duration = NO_MAX_DURATION;
251   action->generic_action.start = surf_get_clock();
252   action->generic_action.finish = -1.0;
253   action->generic_action.model_type =
254       (surf_model_t) surf_cpu_model;
255   action->suspended = 0;        /* Should be useless because of the 
256                                    calloc but it seems to help valgrind... */
257
258   if (CPU->state_current == SURF_CPU_ON)
259     action->generic_action.state_set =
260         surf_cpu_model->common_public->states.running_action_set;
261   else
262     action->generic_action.state_set =
263         surf_cpu_model->common_public->states.failed_action_set;
264   xbt_swag_insert(action, action->generic_action.state_set);
265
266   action->variable = lmm_variable_new(cpu_maxmin_system, action,
267                                       action->generic_action.priority,
268                                       -1.0, 1);
269   lmm_expand(cpu_maxmin_system, CPU->constraint, action->variable, 1.0);
270   XBT_OUT;
271   return (surf_action_t) action;
272 }
273
274 static surf_action_t action_sleep(void *cpu, double duration)
275 {
276   surf_action_cpu_Cas01_t action = NULL;
277
278   XBT_IN2("(%s,%g)", ((cpu_Cas01_t) cpu)->name, duration);
279   action = (surf_action_cpu_Cas01_t) execute(cpu, 1.0);
280   action->generic_action.max_duration = duration;
281   action->suspended = 2;
282   lmm_update_variable_weight(cpu_maxmin_system, action->variable, 0.0);
283   XBT_OUT;
284   return (surf_action_t) action;
285 }
286
287 static void action_suspend(surf_action_t action)
288 {
289   XBT_IN1("(%p)", action);
290   if (((surf_action_cpu_Cas01_t) action)->suspended != 2) {
291     lmm_update_variable_weight(cpu_maxmin_system,
292                                ((surf_action_cpu_Cas01_t) action)->
293                                variable, 0.0);
294     ((surf_action_cpu_Cas01_t) action)->suspended = 1;
295   }
296   XBT_OUT;
297 }
298
299 static void action_resume(surf_action_t action)
300 {
301   XBT_IN1("(%p)", action);
302   if (((surf_action_cpu_Cas01_t) action)->suspended != 2) {
303     lmm_update_variable_weight(cpu_maxmin_system,
304                                ((surf_action_cpu_Cas01_t) action)->
305                                variable, action->priority);
306     ((surf_action_cpu_Cas01_t) action)->suspended = 0;
307   }
308   XBT_OUT;
309 }
310
311 static int action_is_suspended(surf_action_t action)
312 {
313   return (((surf_action_cpu_Cas01_t) action)->suspended == 1);
314 }
315
316 static void action_set_max_duration(surf_action_t action, double duration)
317 {
318   XBT_IN2("(%p,%g)", action, duration);
319   action->max_duration = duration;
320   XBT_OUT;
321 }
322
323 static void action_set_priority(surf_action_t action, double priority)
324 {
325   XBT_IN2("(%p,%g)", action, priority);
326   action->priority = priority;
327   lmm_update_variable_weight(cpu_maxmin_system, ((surf_action_cpu_Cas01_t) action)->variable, priority);
328
329   XBT_OUT;
330 }
331
332 static e_surf_cpu_state_t get_state(void *cpu)
333 {
334   return ((cpu_Cas01_t) cpu)->state_current;
335 }
336
337 static double get_speed(void *cpu, double load)
338 {
339   return load * (((cpu_Cas01_t) cpu)->power_scale);
340 }
341
342 static double get_available_speed(void *cpu)
343 {
344   /* number between 0 and 1 */
345   return ((cpu_Cas01_t) cpu)->power_current;
346 }
347
348 static xbt_dict_t get_properties(void *cpu)
349 {
350  return ((cpu_Cas01_t) cpu)->properties;
351 }
352
353 static void finalize(void)
354 {
355   xbt_dict_free(&cpu_set);
356   xbt_swag_free(surf_cpu_model->common_public->states.ready_action_set);
357   xbt_swag_free(surf_cpu_model->common_public->states.
358                 running_action_set);
359   xbt_swag_free(surf_cpu_model->common_public->states.
360                 failed_action_set);
361   xbt_swag_free(surf_cpu_model->common_public->states.done_action_set);
362   free(surf_cpu_model->common_public);
363   free(surf_cpu_model->common_private);
364   free(surf_cpu_model->extension_public);
365
366   free(surf_cpu_model);
367   surf_cpu_model = NULL;
368 }
369
370 static void surf_cpu_model_init_internal(void)
371 {
372   s_surf_action_t action;
373
374   surf_cpu_model = xbt_new0(s_surf_cpu_model_t, 1);
375
376   surf_cpu_model->common_private =
377       xbt_new0(s_surf_model_private_t, 1);
378   surf_cpu_model->common_public = xbt_new0(s_surf_model_public_t, 1);
379
380   surf_cpu_model->extension_public =
381       xbt_new0(s_surf_cpu_model_extension_public_t, 1);
382
383   surf_cpu_model->common_public->states.ready_action_set =
384       xbt_swag_new(xbt_swag_offset(action, state_hookup));
385   surf_cpu_model->common_public->states.running_action_set =
386       xbt_swag_new(xbt_swag_offset(action, state_hookup));
387   surf_cpu_model->common_public->states.failed_action_set =
388       xbt_swag_new(xbt_swag_offset(action, state_hookup));
389   surf_cpu_model->common_public->states.done_action_set =
390       xbt_swag_new(xbt_swag_offset(action, state_hookup));
391
392   surf_cpu_model->common_public->name_service = name_service;
393   surf_cpu_model->common_public->get_resource_name = get_resource_name;
394   surf_cpu_model->common_public->action_get_state =
395       surf_action_get_state;
396   surf_cpu_model->common_public->action_get_start_time =
397       surf_action_get_start_time;
398   surf_cpu_model->common_public->action_get_finish_time =
399       surf_action_get_finish_time;
400   surf_cpu_model->common_public->action_free = action_free;
401   surf_cpu_model->common_public->action_use = action_use;
402   surf_cpu_model->common_public->action_cancel = action_cancel;
403   surf_cpu_model->common_public->action_recycle = action_recycle;
404   surf_cpu_model->common_public->action_change_state =
405       action_change_state;
406   surf_cpu_model->common_public->action_set_data = surf_action_set_data;
407   surf_cpu_model->common_public->name = "CPU";
408
409   surf_cpu_model->common_private->resource_used = resource_used;
410   surf_cpu_model->common_private->share_resources = share_resources;
411   surf_cpu_model->common_private->update_actions_state =
412       update_actions_state;
413   surf_cpu_model->common_private->update_resource_state =
414       update_resource_state;
415   surf_cpu_model->common_private->finalize = finalize;
416
417   surf_cpu_model->common_public->suspend = action_suspend;
418   surf_cpu_model->common_public->resume = action_resume;
419   surf_cpu_model->common_public->is_suspended = action_is_suspended;
420   surf_cpu_model->common_public->set_max_duration =
421       action_set_max_duration;
422   surf_cpu_model->common_public->set_priority = action_set_priority;
423   surf_cpu_model->extension_public->execute = execute;
424   surf_cpu_model->extension_public->sleep = action_sleep;
425
426   surf_cpu_model->extension_public->get_state = get_state;
427   surf_cpu_model->extension_public->get_speed = get_speed;
428   surf_cpu_model->extension_public->get_available_speed =
429       get_available_speed;
430   /*manage the properties of the cpu*/
431   surf_cpu_model->common_public->get_properties = get_properties;
432
433   if(!cpu_set) cpu_set = xbt_dict_new();
434   if (!cpu_maxmin_system) cpu_maxmin_system = lmm_system_new();
435 }
436
437 /*********************************************************************/
438 /* Basic sharing model for CPU: that is where all this started... ;) */
439 /*********************************************************************/
440 /* @InProceedings{casanova01simgrid, */
441 /*   author =       "H. Casanova", */
442 /*   booktitle =    "Proceedings of the IEEE Symposium on Cluster Computing */
443 /*                  and the Grid (CCGrid'01)", */
444 /*   publisher =    "IEEE Computer Society", */
445 /*   title =        "Simgrid: {A} Toolkit for the Simulation of Application */
446 /*                  Scheduling", */
447 /*   year =         "2001", */
448 /*   month =        may, */
449 /*   note =         "Available at */
450 /*                  \url{http://grail.sdsc.edu/papers/simgrid_ccgrid01.ps.gz}." */
451 /* } */
452 void surf_cpu_model_init_Cas01(const char *filename)
453 {
454   if (surf_cpu_model)
455     return;
456   surf_cpu_model_init_internal();
457   parse_file(filename);
458   xbt_dynar_push(model_list, &surf_cpu_model);
459 }