Logo AND Algorithmique Numérique Distribuée

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