Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
941b213a84753475c23a6a35521b77a6665a5f83
[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   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 }
129
130 static void *name_service(const char *name)
131 {
132   return xbt_dict_get_or_null(cpu_set, name);
133 }
134
135 static const char *get_resource_name(void *resource_id)
136 {
137   return ((cpu_Cas01_t) resource_id)->name;
138 }
139
140 static int resource_used(void *resource_id)
141 {
142   return lmm_constraint_used(cpu_maxmin_system,
143                              ((cpu_Cas01_t) resource_id)->constraint);
144 }
145
146 static int action_free(surf_action_t action)
147 {
148   action->using--;
149   if (!action->using) {
150     xbt_swag_remove(action, action->state_set);
151     if (((surf_action_cpu_Cas01_t) action)->variable)
152       lmm_variable_free(cpu_maxmin_system,
153                         ((surf_action_cpu_Cas01_t) action)->variable);
154     free(action);
155     return 1;
156   }
157   return 0;
158 }
159
160 static void action_use(surf_action_t action)
161 {
162   action->using++;
163 }
164
165 static void action_cancel(surf_action_t action)
166 {
167   surf_action_change_state(action, SURF_ACTION_FAILED);
168   return;
169 }
170
171 static void action_recycle(surf_action_t action)
172 {
173   DIE_IMPOSSIBLE;
174 }
175
176 static void action_change_state(surf_action_t action,
177                                 e_surf_action_state_t state)
178 {
179 /*   if((state==SURF_ACTION_DONE) || (state==SURF_ACTION_FAILED)) */
180 /*     if(((surf_action_cpu_Cas01_t)action)->variable) { */
181 /*       lmm_variable_disable(cpu_maxmin_system, ((surf_action_cpu_Cas01_t)action)->variable); */
182 /*       ((surf_action_cpu_Cas01_t)action)->variable = NULL; */
183 /*     } */
184
185   surf_action_change_state(action, state);
186   return;
187 }
188
189 static double share_resources(double now)
190 {
191   s_surf_action_cpu_Cas01_t action;
192   return generic_maxmin_share_resources(surf_cpu_model->common_public->
193                                         states.running_action_set,
194                                         xbt_swag_offset(action, variable),
195                                         cpu_maxmin_system, lmm_solve);
196 }
197
198 static void update_actions_state(double now, double delta)
199 {
200   surf_action_cpu_Cas01_t action = NULL;
201   surf_action_cpu_Cas01_t next_action = NULL;
202   xbt_swag_t running_actions =
203       surf_cpu_model->common_public->states.running_action_set;
204   /* FIXME: UNUSED
205      xbt_swag_t failed_actions =
206      surf_cpu_model->common_public->states.failed_action_set;
207    */
208
209   xbt_swag_foreach_safe(action, next_action, running_actions) {
210     double_update(&(action->generic_action.remains),
211                   lmm_variable_getvalue(action->variable) * delta);
212     if (action->generic_action.max_duration != NO_MAX_DURATION)
213       double_update(&(action->generic_action.max_duration), delta);
214     if ((action->generic_action.remains <= 0) &&
215         (lmm_get_variable_weight(action->variable) > 0)) {
216       action->generic_action.finish = surf_get_clock();
217       action_change_state((surf_action_t) action, SURF_ACTION_DONE);
218     } else if ((action->generic_action.max_duration != NO_MAX_DURATION) &&
219                (action->generic_action.max_duration <= 0)) {
220       action->generic_action.finish = surf_get_clock();
221       action_change_state((surf_action_t) action, SURF_ACTION_DONE);
222     }
223   }
224
225   return;
226 }
227
228 static void update_resource_state(void *id,
229                                   tmgr_trace_event_t event_type,
230                                   double value, double date)
231 {
232   cpu_Cas01_t cpu = id;
233
234   if (event_type == cpu->power_event) {
235     cpu->power_current = value;
236     lmm_update_constraint_bound(cpu_maxmin_system, cpu->constraint,
237                                 cpu->power_current * cpu->power_scale);
238   } else if (event_type == cpu->state_event) {
239     if (value > 0)
240       cpu->state_current = SURF_CPU_ON;
241     else {
242       lmm_constraint_t cnst = cpu->constraint;
243       lmm_variable_t var = NULL;
244       lmm_element_t elem = NULL;
245
246       cpu->state_current = SURF_CPU_OFF;
247
248       while((var = lmm_get_var_from_cnst(cpu_maxmin_system,cnst,&elem))) {
249         surf_action_t action = lmm_variable_id(var) ;
250
251         if(surf_action_get_state(action)==SURF_ACTION_RUNNING ||
252            surf_action_get_state(action)==SURF_ACTION_READY || 
253            surf_action_get_state(action)==SURF_ACTION_NOT_IN_THE_SYSTEM) {
254           action->finish = date;
255           action_change_state( action, SURF_ACTION_FAILED);
256         }
257       }
258     }
259   } else {
260     CRITICAL0("Unknown event ! \n");
261     xbt_abort();
262   }
263
264   return;
265 }
266
267 static surf_action_t execute(void *cpu, double size)
268 {
269   surf_action_cpu_Cas01_t action = NULL;
270   cpu_Cas01_t CPU = cpu;
271
272   XBT_IN2("(%s,%g)", CPU->name, size);
273   action = xbt_new0(s_surf_action_cpu_Cas01_t, 1);
274
275   action->generic_action.using = 1;
276   action->generic_action.cost = size;
277   action->generic_action.remains = size;
278   action->generic_action.priority = 1.0;
279   action->generic_action.max_duration = NO_MAX_DURATION;
280   action->generic_action.start = surf_get_clock();
281   action->generic_action.finish = -1.0;
282   action->generic_action.model_type =
283       (surf_model_t) surf_cpu_model;
284   action->suspended = 0;        /* Should be useless because of the 
285                                    calloc but it seems to help valgrind... */
286
287   if (CPU->state_current == SURF_CPU_ON)
288     action->generic_action.state_set =
289         surf_cpu_model->common_public->states.running_action_set;
290   else
291     action->generic_action.state_set =
292         surf_cpu_model->common_public->states.failed_action_set;
293
294   xbt_swag_insert(action, action->generic_action.state_set);
295
296   action->variable = lmm_variable_new(cpu_maxmin_system, action,
297                                       action->generic_action.priority,
298                                       -1.0, 1);
299   lmm_expand(cpu_maxmin_system, CPU->constraint, action->variable, 1.0);
300   XBT_OUT;
301   return (surf_action_t) action;
302 }
303
304 static surf_action_t action_sleep(void *cpu, double duration)
305 {
306   surf_action_cpu_Cas01_t action = NULL;
307
308   if (duration>0)
309      duration=MAX(duration,MAXMIN_PRECISION);
310
311   XBT_IN2("(%s,%g)", ((cpu_Cas01_t) cpu)->name, duration);
312   action = (surf_action_cpu_Cas01_t) execute(cpu, 1.0);
313   action->generic_action.max_duration = duration;
314   action->suspended = 2;
315   if(duration == NO_MAX_DURATION) {
316     /* Move to the *end* of the corresponding action set. This convention
317        is used to speed up update_resource_state  */
318     xbt_swag_remove(action, ((surf_action_t)action)->state_set);
319     ((surf_action_t)action)->state_set =running_action_set_that_does_not_need_being_checked;
320     xbt_swag_insert(action, ((surf_action_t)action)->state_set);
321   }
322
323   lmm_update_variable_weight(cpu_maxmin_system, action->variable, 0.0);
324   XBT_OUT;
325   return (surf_action_t) action;
326 }
327
328 static void action_suspend(surf_action_t action)
329 {
330   XBT_IN1("(%p)", action);
331   if (((surf_action_cpu_Cas01_t) action)->suspended != 2) {
332     lmm_update_variable_weight(cpu_maxmin_system,
333                                ((surf_action_cpu_Cas01_t) action)->
334                                variable, 0.0);
335     ((surf_action_cpu_Cas01_t) action)->suspended = 1;
336   }
337   XBT_OUT;
338 }
339
340 static void action_resume(surf_action_t action)
341 {
342   XBT_IN1("(%p)", action);
343   if (((surf_action_cpu_Cas01_t) action)->suspended != 2) {
344     lmm_update_variable_weight(cpu_maxmin_system,
345                                ((surf_action_cpu_Cas01_t) action)->
346                                variable, action->priority);
347     ((surf_action_cpu_Cas01_t) action)->suspended = 0;
348   }
349   XBT_OUT;
350 }
351
352 static int action_is_suspended(surf_action_t action)
353 {
354   return (((surf_action_cpu_Cas01_t) action)->suspended == 1);
355 }
356
357 static void action_set_max_duration(surf_action_t action, double duration)
358 {
359   XBT_IN2("(%p,%g)", action, duration);
360   action->max_duration = duration;
361   XBT_OUT;
362 }
363
364 static void action_set_priority(surf_action_t action, double priority)
365 {
366   XBT_IN2("(%p,%g)", action, priority);
367   action->priority = priority;
368   lmm_update_variable_weight(cpu_maxmin_system, ((surf_action_cpu_Cas01_t) action)->variable, priority);
369
370   XBT_OUT;
371 }
372
373 static e_surf_cpu_state_t get_state(void *cpu)
374 {
375   return ((cpu_Cas01_t) cpu)->state_current;
376 }
377
378 static double get_speed(void *cpu, double load)
379 {
380   return load * (((cpu_Cas01_t) cpu)->power_scale);
381 }
382
383 static double get_available_speed(void *cpu)
384 {
385   /* number between 0 and 1 */
386   return ((cpu_Cas01_t) cpu)->power_current;
387 }
388
389 static xbt_dict_t get_properties(void *cpu)
390 {
391  return ((cpu_Cas01_t) cpu)->properties;
392 }
393
394 static void finalize(void)
395 {
396   xbt_dict_free(&cpu_set);
397   lmm_system_free(cpu_maxmin_system);
398   cpu_maxmin_system = NULL;
399   xbt_swag_free(surf_cpu_model->common_public->states.ready_action_set);
400   xbt_swag_free(surf_cpu_model->common_public->states.
401                 running_action_set);
402   xbt_swag_free(surf_cpu_model->common_public->states.
403                 failed_action_set);
404   xbt_swag_free(surf_cpu_model->common_public->states.done_action_set);
405   xbt_swag_free(running_action_set_that_does_not_need_being_checked);
406   running_action_set_that_does_not_need_being_checked = NULL;
407   free(surf_cpu_model->common_public);
408   free(surf_cpu_model->common_private);
409   free(surf_cpu_model->extension_public);
410
411   free(surf_cpu_model);
412   surf_cpu_model = NULL;
413 }
414
415 static void surf_cpu_model_init_internal(void)
416 {
417   s_surf_action_t action;
418
419   surf_cpu_model = xbt_new0(s_surf_cpu_model_t, 1);
420
421   surf_cpu_model->common_private =
422       xbt_new0(s_surf_model_private_t, 1);
423   surf_cpu_model->common_public = xbt_new0(s_surf_model_public_t, 1);
424
425   surf_cpu_model->extension_public =
426       xbt_new0(s_surf_cpu_model_extension_public_t, 1);
427
428   surf_cpu_model->common_public->states.ready_action_set =
429       xbt_swag_new(xbt_swag_offset(action, state_hookup));
430   surf_cpu_model->common_public->states.running_action_set =
431       xbt_swag_new(xbt_swag_offset(action, state_hookup));
432   surf_cpu_model->common_public->states.failed_action_set =
433       xbt_swag_new(xbt_swag_offset(action, state_hookup));
434   surf_cpu_model->common_public->states.done_action_set =
435       xbt_swag_new(xbt_swag_offset(action, state_hookup));
436
437   running_action_set_that_does_not_need_being_checked =
438       xbt_swag_new(xbt_swag_offset(action, state_hookup));
439
440   surf_cpu_model->common_public->name_service = name_service;
441   surf_cpu_model->common_public->get_resource_name = get_resource_name;
442   surf_cpu_model->common_public->action_get_state =
443       surf_action_get_state;
444   surf_cpu_model->common_public->action_get_start_time =
445       surf_action_get_start_time;
446   surf_cpu_model->common_public->action_get_finish_time =
447       surf_action_get_finish_time;
448   surf_cpu_model->common_public->action_free = action_free;
449   surf_cpu_model->common_public->action_use = action_use;
450   surf_cpu_model->common_public->action_cancel = action_cancel;
451   surf_cpu_model->common_public->action_recycle = action_recycle;
452   surf_cpu_model->common_public->action_change_state =
453       action_change_state;
454   surf_cpu_model->common_public->action_set_data = surf_action_set_data;
455   surf_cpu_model->common_public->name = "CPU";
456
457   surf_cpu_model->common_private->resource_used = resource_used;
458   surf_cpu_model->common_private->share_resources = share_resources;
459   surf_cpu_model->common_private->update_actions_state =
460       update_actions_state;
461   surf_cpu_model->common_private->update_resource_state =
462       update_resource_state;
463   surf_cpu_model->common_private->finalize = finalize;
464
465   surf_cpu_model->common_public->suspend = action_suspend;
466   surf_cpu_model->common_public->resume = action_resume;
467   surf_cpu_model->common_public->is_suspended = action_is_suspended;
468   surf_cpu_model->common_public->set_max_duration =
469       action_set_max_duration;
470   surf_cpu_model->common_public->set_priority = action_set_priority;
471   surf_cpu_model->extension_public->execute = execute;
472   surf_cpu_model->extension_public->sleep = action_sleep;
473
474   surf_cpu_model->extension_public->get_state = get_state;
475   surf_cpu_model->extension_public->get_speed = get_speed;
476   surf_cpu_model->extension_public->get_available_speed =
477       get_available_speed;
478   /*manage the properties of the cpu*/
479   surf_cpu_model->common_public->get_properties = get_properties;
480
481   if(!cpu_set) cpu_set = xbt_dict_new();
482   if (!cpu_maxmin_system) cpu_maxmin_system = lmm_system_new();
483 }
484
485 /*********************************************************************/
486 /* Basic sharing model for CPU: that is where all this started... ;) */
487 /*********************************************************************/
488 /* @InProceedings{casanova01simgrid, */
489 /*   author =       "H. Casanova", */
490 /*   booktitle =    "Proceedings of the IEEE Symposium on Cluster Computing */
491 /*                  and the Grid (CCGrid'01)", */
492 /*   publisher =    "IEEE Computer Society", */
493 /*   title =        "Simgrid: {A} Toolkit for the Simulation of Application */
494 /*                  Scheduling", */
495 /*   year =         "2001", */
496 /*   month =        may, */
497 /*   note =         "Available at */
498 /*                  \url{http://grail.sdsc.edu/papers/simgrid_ccgrid01.ps.gz}." */
499 /* } */
500 void surf_cpu_model_init_Cas01(const char *filename)
501 {
502   if (surf_cpu_model)
503     return;
504   surf_cpu_model_init_internal();
505   define_callbacks(filename);
506   xbt_dynar_push(model_list, &surf_cpu_model);
507 }