Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
plug memleaks
[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 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   free(cpu);
23 }
24
25 static cpu_Cas01_t cpu_new(char *name, double power_scale,
26                            double power_initial,
27                            tmgr_trace_t power_trace,
28                            e_surf_cpu_state_t state_initial,
29                            tmgr_trace_t state_trace,
30                            xbt_dict_t cpu_properties)
31 {
32   cpu_Cas01_t cpu = xbt_new0(s_cpu_Cas01_t, 1);
33   xbt_assert1(!xbt_dict_get_or_null(cpu_set, name),
34               "Host '%s' declared several times in the platform file",name);
35    
36   cpu->model = (surf_model_t) surf_cpu_model;
37   cpu->name = name;
38   cpu->power_scale = power_scale;
39   xbt_assert0(cpu->power_scale > 0, "Power has to be >0");
40   cpu->power_current = power_initial;
41   if (power_trace)
42     cpu->power_event =
43         tmgr_history_add_trace(history, power_trace, 0.0, 0, cpu);
44
45   cpu->state_current = state_initial;
46   if (state_trace)
47     cpu->state_event =
48         tmgr_history_add_trace(history, state_trace, 0.0, 0, cpu);
49
50   cpu->constraint =
51       lmm_constraint_new(cpu_maxmin_system, cpu,
52                          cpu->power_current * cpu->power_scale);
53
54   /*add the property set*/
55   cpu->properties = cpu_properties;
56
57   current_property_set = cpu_properties;
58
59   xbt_dict_set(cpu_set, name, cpu, cpu_free);
60
61   return cpu;
62 }
63
64
65 static void parse_cpu_init(void)
66 {
67   double power_scale = 0.0;
68   double power_initial = 0.0;
69   tmgr_trace_t power_trace = NULL;
70   e_surf_cpu_state_t state_initial = SURF_CPU_OFF;
71   tmgr_trace_t state_trace = NULL;
72
73   power_scale = get_cpu_power(A_surfxml_host_power);
74   surf_parse_get_double(&power_initial, A_surfxml_host_availability);
75   surf_parse_get_trace(&power_trace, A_surfxml_host_availability_file);
76
77   xbt_assert0((A_surfxml_host_state == A_surfxml_host_state_ON) ||
78               (A_surfxml_host_state == A_surfxml_host_state_OFF),
79               "Invalid state");
80   if (A_surfxml_host_state == A_surfxml_host_state_ON)
81     state_initial = SURF_CPU_ON;
82   if (A_surfxml_host_state == A_surfxml_host_state_OFF)
83     state_initial = SURF_CPU_OFF;
84   surf_parse_get_trace(&state_trace, A_surfxml_host_state_file); 
85
86   current_property_set = xbt_dict_new();
87   cpu_new(xbt_strdup(A_surfxml_host_id), power_scale, power_initial, power_trace, state_initial,
88           state_trace,  current_property_set);
89
90 }
91
92 static void add_traces_cpu(void) {
93    xbt_dict_cursor_t cursor=NULL;
94    char *trace_name,*elm;
95
96    static int called = 0;
97    
98    if (called) return;
99    called = 1;
100
101    
102    /* connect all traces relative to hosts */
103    xbt_dict_foreach(trace_connect_list_host_avail, cursor, trace_name, elm) {
104       tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
105       cpu_Cas01_t host = xbt_dict_get_or_null(cpu_set, elm);
106       
107       xbt_assert1(host, "Host %s undefined", elm);
108       xbt_assert1(trace, "Trace %s undefined", trace_name);
109       
110       host->state_event = tmgr_history_add_trace(history, trace, 0.0, 0, host); 
111    }
112
113    xbt_dict_foreach(trace_connect_list_power, cursor, trace_name, elm) {
114       tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
115       cpu_Cas01_t host = xbt_dict_get_or_null(cpu_set, elm);
116       
117       xbt_assert1(host, "Host %s undefined", elm);
118       xbt_assert1(trace, "Trace %s undefined", trace_name);
119       
120       host->power_event = tmgr_history_add_trace(history, trace, 0.0, 0, host); 
121    }
122 }
123
124 static void define_callbacks(const char *file)
125 {
126   surf_parse_reset_parser();
127   surfxml_add_callback(STag_surfxml_host_cb_list, parse_cpu_init);
128   surfxml_add_callback(STag_surfxml_prop_cb_list, parse_properties);
129   surfxml_add_callback(STag_surfxml_random_cb_list, &init_randomness);
130   surfxml_add_callback(ETag_surfxml_random_cb_list, &add_randomness);
131 }
132
133 static void *name_service(const char *name)
134 {
135   return xbt_dict_get_or_null(cpu_set, name);
136 }
137
138 static const char *get_resource_name(void *resource_id)
139 {
140   return ((cpu_Cas01_t) resource_id)->name;
141 }
142
143 static int resource_used(void *resource_id)
144 {
145   return lmm_constraint_used(cpu_maxmin_system,
146                              ((cpu_Cas01_t) resource_id)->constraint);
147 }
148
149 static int action_free(surf_action_t action)
150 {
151   action->using--;
152   if (!action->using) {
153     xbt_swag_remove(action, action->state_set);
154     if (((surf_action_cpu_Cas01_t) action)->variable)
155       lmm_variable_free(cpu_maxmin_system,
156                         ((surf_action_cpu_Cas01_t) action)->variable);
157     free(action);
158     return 1;
159   }
160   return 0;
161 }
162
163 static void action_use(surf_action_t action)
164 {
165   action->using++;
166 }
167
168 static void action_cancel(surf_action_t action)
169 {
170   surf_action_change_state(action, SURF_ACTION_FAILED);
171   return;
172 }
173
174 static void action_recycle(surf_action_t action)
175 {
176   DIE_IMPOSSIBLE;
177 }
178
179 static void action_change_state(surf_action_t action,
180                                 e_surf_action_state_t state)
181 {
182 /*   if((state==SURF_ACTION_DONE) || (state==SURF_ACTION_FAILED)) */
183 /*     if(((surf_action_cpu_Cas01_t)action)->variable) { */
184 /*       lmm_variable_disable(cpu_maxmin_system, ((surf_action_cpu_Cas01_t)action)->variable); */
185 /*       ((surf_action_cpu_Cas01_t)action)->variable = NULL; */
186 /*     } */
187
188   surf_action_change_state(action, state);
189   return;
190 }
191
192 static double share_resources(double now)
193 {
194   s_surf_action_cpu_Cas01_t action;
195   return generic_maxmin_share_resources(surf_cpu_model->common_public->
196                                         states.running_action_set,
197                                         xbt_swag_offset(action, variable),
198                                         cpu_maxmin_system, lmm_solve);
199 }
200
201 static void update_actions_state(double now, double delta)
202 {
203   surf_action_cpu_Cas01_t action = NULL;
204   surf_action_cpu_Cas01_t next_action = NULL;
205   xbt_swag_t running_actions =
206       surf_cpu_model->common_public->states.running_action_set;
207   /* FIXME: UNUSED
208      xbt_swag_t failed_actions =
209      surf_cpu_model->common_public->states.failed_action_set;
210    */
211
212   xbt_swag_foreach_safe(action, next_action, running_actions) {
213     double_update(&(action->generic_action.remains),
214                   lmm_variable_getvalue(action->variable) * delta);
215     if (action->generic_action.max_duration != NO_MAX_DURATION)
216       double_update(&(action->generic_action.max_duration), delta);
217     if ((action->generic_action.remains <= 0) &&
218         (lmm_get_variable_weight(action->variable) > 0)) {
219       action->generic_action.finish = surf_get_clock();
220       action_change_state((surf_action_t) action, SURF_ACTION_DONE);
221     } else if ((action->generic_action.max_duration != NO_MAX_DURATION) &&
222                (action->generic_action.max_duration <= 0)) {
223       action->generic_action.finish = surf_get_clock();
224       action_change_state((surf_action_t) action, SURF_ACTION_DONE);
225     }
226   }
227
228   return;
229 }
230
231 static void update_resource_state(void *id,
232                                   tmgr_trace_event_t event_type,
233                                   double value, double date)
234 {
235   cpu_Cas01_t cpu = id;
236
237   if (event_type == cpu->power_event) {
238     cpu->power_current = value;
239     lmm_update_constraint_bound(cpu_maxmin_system, cpu->constraint,
240                                 cpu->power_current * cpu->power_scale);
241   } else if (event_type == cpu->state_event) {
242     if (value > 0)
243       cpu->state_current = SURF_CPU_ON;
244     else {
245       lmm_constraint_t cnst = cpu->constraint;
246       lmm_variable_t var = NULL;
247       lmm_element_t elem = NULL;
248
249       cpu->state_current = SURF_CPU_OFF;
250
251       while((var = lmm_get_var_from_cnst(cpu_maxmin_system,cnst,&elem))) {
252         surf_action_t action = lmm_variable_id(var) ;
253
254         if(surf_action_get_state(action)==SURF_ACTION_RUNNING ||
255            surf_action_get_state(action)==SURF_ACTION_READY || 
256            surf_action_get_state(action)==SURF_ACTION_NOT_IN_THE_SYSTEM) {
257           action->finish = date;
258           action_change_state( action, SURF_ACTION_FAILED);
259         }
260       }
261     }
262   } else {
263     CRITICAL0("Unknown event ! \n");
264     xbt_abort();
265   }
266
267   return;
268 }
269
270 static surf_action_t execute(void *cpu, double size)
271 {
272   surf_action_cpu_Cas01_t action = NULL;
273   cpu_Cas01_t CPU = cpu;
274
275   XBT_IN2("(%s,%g)", CPU->name, size);
276   action = xbt_new0(s_surf_action_cpu_Cas01_t, 1);
277
278   action->generic_action.using = 1;
279   action->generic_action.cost = size;
280   action->generic_action.remains = size;
281   action->generic_action.priority = 1.0;
282   action->generic_action.max_duration = NO_MAX_DURATION;
283   action->generic_action.start = surf_get_clock();
284   action->generic_action.finish = -1.0;
285   action->generic_action.model_type =
286       (surf_model_t) surf_cpu_model;
287   action->suspended = 0;        /* Should be useless because of the 
288                                    calloc but it seems to help valgrind... */
289
290   if (CPU->state_current == SURF_CPU_ON)
291     action->generic_action.state_set =
292         surf_cpu_model->common_public->states.running_action_set;
293   else
294     action->generic_action.state_set =
295         surf_cpu_model->common_public->states.failed_action_set;
296
297   xbt_swag_insert(action, action->generic_action.state_set);
298
299   action->variable = lmm_variable_new(cpu_maxmin_system, action,
300                                       action->generic_action.priority,
301                                       -1.0, 1);
302   lmm_expand(cpu_maxmin_system, CPU->constraint, action->variable, 1.0);
303   XBT_OUT;
304   return (surf_action_t) action;
305 }
306
307 static surf_action_t action_sleep(void *cpu, double duration)
308 {
309   surf_action_cpu_Cas01_t action = NULL;
310
311   if (duration>0)
312      duration=MAX(duration,MAXMIN_PRECISION);
313
314   XBT_IN2("(%s,%g)", ((cpu_Cas01_t) cpu)->name, duration);
315   action = (surf_action_cpu_Cas01_t) execute(cpu, 1.0);
316   action->generic_action.max_duration = duration;
317   action->suspended = 2;
318   if(duration == NO_MAX_DURATION) {
319     /* Move to the *end* of the corresponding action set. This convention
320        is used to speed up update_resource_state  */
321     xbt_swag_remove(action, ((surf_action_t)action)->state_set);
322     ((surf_action_t)action)->state_set =running_action_set_that_does_not_need_being_checked;
323     xbt_swag_insert(action, ((surf_action_t)action)->state_set);
324   }
325
326   lmm_update_variable_weight(cpu_maxmin_system, action->variable, 0.0);
327   XBT_OUT;
328   return (surf_action_t) action;
329 }
330
331 static void action_suspend(surf_action_t action)
332 {
333   XBT_IN1("(%p)", action);
334   if (((surf_action_cpu_Cas01_t) action)->suspended != 2) {
335     lmm_update_variable_weight(cpu_maxmin_system,
336                                ((surf_action_cpu_Cas01_t) action)->
337                                variable, 0.0);
338     ((surf_action_cpu_Cas01_t) action)->suspended = 1;
339   }
340   XBT_OUT;
341 }
342
343 static void action_resume(surf_action_t action)
344 {
345   XBT_IN1("(%p)", action);
346   if (((surf_action_cpu_Cas01_t) action)->suspended != 2) {
347     lmm_update_variable_weight(cpu_maxmin_system,
348                                ((surf_action_cpu_Cas01_t) action)->
349                                variable, action->priority);
350     ((surf_action_cpu_Cas01_t) action)->suspended = 0;
351   }
352   XBT_OUT;
353 }
354
355 static int action_is_suspended(surf_action_t action)
356 {
357   return (((surf_action_cpu_Cas01_t) action)->suspended == 1);
358 }
359
360 static void action_set_max_duration(surf_action_t action, double duration)
361 {
362   XBT_IN2("(%p,%g)", action, duration);
363   action->max_duration = duration;
364   XBT_OUT;
365 }
366
367 static void action_set_priority(surf_action_t action, double priority)
368 {
369   XBT_IN2("(%p,%g)", action, priority);
370   action->priority = priority;
371   lmm_update_variable_weight(cpu_maxmin_system, ((surf_action_cpu_Cas01_t) action)->variable, priority);
372
373   XBT_OUT;
374 }
375
376 static e_surf_cpu_state_t get_state(void *cpu)
377 {
378   return ((cpu_Cas01_t) cpu)->state_current;
379 }
380
381 static double get_speed(void *cpu, double load)
382 {
383   return load * (((cpu_Cas01_t) cpu)->power_scale);
384 }
385
386 static double get_available_speed(void *cpu)
387 {
388   /* number between 0 and 1 */
389   return ((cpu_Cas01_t) cpu)->power_current;
390 }
391
392 static xbt_dict_t get_properties(void *cpu)
393 {
394  return ((cpu_Cas01_t) cpu)->properties;
395 }
396
397 static void finalize(void)
398 {
399   xbt_dict_free(&cpu_set);
400   lmm_system_free(cpu_maxmin_system);
401   cpu_maxmin_system = NULL;
402   xbt_swag_free(surf_cpu_model->common_public->states.ready_action_set);
403   xbt_swag_free(surf_cpu_model->common_public->states.
404                 running_action_set);
405   xbt_swag_free(surf_cpu_model->common_public->states.
406                 failed_action_set);
407   xbt_swag_free(surf_cpu_model->common_public->states.done_action_set);
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 }