Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
3ba5717a3965b590aa8295911735af315e98fd8f
[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   power_scale = get_cpu_power(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 add_traces_cpu(void) {
92    xbt_dict_cursor_t cursor=NULL;
93    char *trace_name,*elm;
94
95    static int called = 0;
96    
97    if (called) return;
98    called = 1;
99
100    
101    /* connect all traces relative to hosts */
102    xbt_dict_foreach(trace_connect_list_host_avail, cursor, trace_name, elm) {
103       tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
104       cpu_Cas01_t host = xbt_dict_get_or_null(cpu_set, elm);
105       
106       xbt_assert1(host, "Host %s undefined", elm);
107       xbt_assert1(trace, "Trace %s undefined", trace_name);
108       
109       host->state_event = tmgr_history_add_trace(history, trace, 0.0, 0, host); 
110    }
111
112    xbt_dict_foreach(trace_connect_list_power, cursor, trace_name, elm) {
113       tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
114       cpu_Cas01_t host = xbt_dict_get_or_null(cpu_set, elm);
115       
116       xbt_assert1(host, "Host %s undefined", elm);
117       xbt_assert1(trace, "Trace %s undefined", trace_name);
118       
119       host->power_event = tmgr_history_add_trace(history, trace, 0.0, 0, host); 
120    }
121 }
122
123 static void define_callbacks(const char *file)
124 {
125   surf_parse_reset_parser();
126   surfxml_add_callback(STag_surfxml_host_cb_list, parse_cpu_init);
127   surfxml_add_callback(STag_surfxml_prop_cb_list, parse_properties);
128   surfxml_add_callback(STag_surfxml_random_cb_list, &init_randomness);
129   surfxml_add_callback(ETag_surfxml_random_cb_list, &add_randomness);
130 }
131
132 static void *name_service(const char *name)
133 {
134   return xbt_dict_get_or_null(cpu_set, name);
135 }
136
137 static const char *get_resource_name(void *resource_id)
138 {
139   return ((cpu_Cas01_t) resource_id)->name;
140 }
141
142 static int resource_used(void *resource_id)
143 {
144   return lmm_constraint_used(cpu_maxmin_system,
145                              ((cpu_Cas01_t) resource_id)->constraint);
146 }
147
148 static int action_free(surf_action_t action)
149 {
150   action->using--;
151   if (!action->using) {
152     xbt_swag_remove(action, action->state_set);
153     if (((surf_action_cpu_Cas01_t) action)->variable)
154       lmm_variable_free(cpu_maxmin_system,
155                         ((surf_action_cpu_Cas01_t) action)->variable);
156     free(action);
157     return 1;
158   }
159   return 0;
160 }
161
162 static void action_use(surf_action_t action)
163 {
164   action->using++;
165 }
166
167 static void action_cancel(surf_action_t action)
168 {
169   surf_action_change_state(action, SURF_ACTION_FAILED);
170   return;
171 }
172
173 static void action_recycle(surf_action_t action)
174 {
175   DIE_IMPOSSIBLE;
176 }
177
178 static void action_change_state(surf_action_t action,
179                                 e_surf_action_state_t state)
180 {
181 /*   if((state==SURF_ACTION_DONE) || (state==SURF_ACTION_FAILED)) */
182 /*     if(((surf_action_cpu_Cas01_t)action)->variable) { */
183 /*       lmm_variable_disable(cpu_maxmin_system, ((surf_action_cpu_Cas01_t)action)->variable); */
184 /*       ((surf_action_cpu_Cas01_t)action)->variable = NULL; */
185 /*     } */
186
187   surf_action_change_state(action, state);
188   return;
189 }
190
191 static double share_resources(double now)
192 {
193   s_surf_action_cpu_Cas01_t action;
194   return generic_maxmin_share_resources(surf_cpu_model->common_public->
195                                         states.running_action_set,
196                                         xbt_swag_offset(action, variable),
197                                         cpu_maxmin_system, lmm_solve);
198 }
199
200 static void update_actions_state(double now, double delta)
201 {
202   surf_action_cpu_Cas01_t action = NULL;
203   surf_action_cpu_Cas01_t next_action = NULL;
204   xbt_swag_t running_actions =
205       surf_cpu_model->common_public->states.running_action_set;
206   /* FIXME: UNUSED
207      xbt_swag_t failed_actions =
208      surf_cpu_model->common_public->states.failed_action_set;
209    */
210
211   xbt_swag_foreach_safe(action, next_action, running_actions) {
212     double_update(&(action->generic_action.remains),
213                   lmm_variable_getvalue(action->variable) * delta);
214     if (action->generic_action.max_duration != NO_MAX_DURATION)
215       double_update(&(action->generic_action.max_duration), delta);
216     if ((action->generic_action.remains <= 0) &&
217         (lmm_get_variable_weight(action->variable) > 0)) {
218       action->generic_action.finish = surf_get_clock();
219       action_change_state((surf_action_t) action, SURF_ACTION_DONE);
220     } else if ((action->generic_action.max_duration != NO_MAX_DURATION) &&
221                (action->generic_action.max_duration <= 0)) {
222       action->generic_action.finish = surf_get_clock();
223       action_change_state((surf_action_t) action, SURF_ACTION_DONE);
224     } else {                    /* Need to check that none of the model has failed */
225       lmm_constraint_t cnst = NULL;
226       int i = 0;
227       cpu_Cas01_t cpu = NULL;
228
229       while ((cnst =
230               lmm_get_cnst_from_var(cpu_maxmin_system, action->variable,
231                                     i++))) {
232         cpu = lmm_constraint_id(cnst);
233         if (cpu->state_current == SURF_CPU_OFF) {
234           action->generic_action.finish = surf_get_clock();
235           action_change_state((surf_action_t) action, SURF_ACTION_FAILED);
236           break;
237         }
238       }
239     }
240   }
241
242   return;
243 }
244
245 static void update_resource_state(void *id,
246                                   tmgr_trace_event_t event_type,
247                                   double value)
248 {
249   cpu_Cas01_t cpu = id;
250
251   if (event_type == cpu->power_event) {
252     cpu->power_current = value;
253     lmm_update_constraint_bound(cpu_maxmin_system, cpu->constraint,
254                                 cpu->power_current * cpu->power_scale);
255   } else if (event_type == cpu->state_event) {
256     if (value > 0)
257       cpu->state_current = SURF_CPU_ON;
258     else
259       cpu->state_current = SURF_CPU_OFF;
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   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   duration=MAX(duration,MAXMIN_PRECISION);
308   XBT_IN2("(%s,%g)", ((cpu_Cas01_t) cpu)->name, duration);
309   action = (surf_action_cpu_Cas01_t) execute(cpu, 1.0);
310   action->generic_action.max_duration = duration;
311   action->suspended = 2;
312
313   lmm_update_variable_weight(cpu_maxmin_system, action->variable, 0.0);
314   XBT_OUT;
315   return (surf_action_t) action;
316 }
317
318 static void action_suspend(surf_action_t action)
319 {
320   XBT_IN1("(%p)", action);
321   if (((surf_action_cpu_Cas01_t) action)->suspended != 2) {
322     lmm_update_variable_weight(cpu_maxmin_system,
323                                ((surf_action_cpu_Cas01_t) action)->
324                                variable, 0.0);
325     ((surf_action_cpu_Cas01_t) action)->suspended = 1;
326   }
327   XBT_OUT;
328 }
329
330 static void action_resume(surf_action_t action)
331 {
332   XBT_IN1("(%p)", action);
333   if (((surf_action_cpu_Cas01_t) action)->suspended != 2) {
334     lmm_update_variable_weight(cpu_maxmin_system,
335                                ((surf_action_cpu_Cas01_t) action)->
336                                variable, action->priority);
337     ((surf_action_cpu_Cas01_t) action)->suspended = 0;
338   }
339   XBT_OUT;
340 }
341
342 static int action_is_suspended(surf_action_t action)
343 {
344   return (((surf_action_cpu_Cas01_t) action)->suspended == 1);
345 }
346
347 static void action_set_max_duration(surf_action_t action, double duration)
348 {
349   XBT_IN2("(%p,%g)", action, duration);
350   action->max_duration = duration;
351   XBT_OUT;
352 }
353
354 static void action_set_priority(surf_action_t action, double priority)
355 {
356   XBT_IN2("(%p,%g)", action, priority);
357   action->priority = priority;
358   lmm_update_variable_weight(cpu_maxmin_system, ((surf_action_cpu_Cas01_t) action)->variable, priority);
359
360   XBT_OUT;
361 }
362
363 static e_surf_cpu_state_t get_state(void *cpu)
364 {
365   return ((cpu_Cas01_t) cpu)->state_current;
366 }
367
368 static double get_speed(void *cpu, double load)
369 {
370   return load * (((cpu_Cas01_t) cpu)->power_scale);
371 }
372
373 static double get_available_speed(void *cpu)
374 {
375   /* number between 0 and 1 */
376   return ((cpu_Cas01_t) cpu)->power_current;
377 }
378
379 static xbt_dict_t get_properties(void *cpu)
380 {
381  return ((cpu_Cas01_t) cpu)->properties;
382 }
383
384 static void finalize(void)
385 {
386   xbt_dict_free(&cpu_set);
387   xbt_swag_free(surf_cpu_model->common_public->states.ready_action_set);
388   xbt_swag_free(surf_cpu_model->common_public->states.
389                 running_action_set);
390   xbt_swag_free(surf_cpu_model->common_public->states.
391                 failed_action_set);
392   xbt_swag_free(surf_cpu_model->common_public->states.done_action_set);
393   free(surf_cpu_model->common_public);
394   free(surf_cpu_model->common_private);
395   free(surf_cpu_model->extension_public);
396
397   free(surf_cpu_model);
398   surf_cpu_model = NULL;
399 }
400
401 static void surf_cpu_model_init_internal(void)
402 {
403   s_surf_action_t action;
404
405   surf_cpu_model = xbt_new0(s_surf_cpu_model_t, 1);
406
407   surf_cpu_model->common_private =
408       xbt_new0(s_surf_model_private_t, 1);
409   surf_cpu_model->common_public = xbt_new0(s_surf_model_public_t, 1);
410
411   surf_cpu_model->extension_public =
412       xbt_new0(s_surf_cpu_model_extension_public_t, 1);
413
414   surf_cpu_model->common_public->states.ready_action_set =
415       xbt_swag_new(xbt_swag_offset(action, state_hookup));
416   surf_cpu_model->common_public->states.running_action_set =
417       xbt_swag_new(xbt_swag_offset(action, state_hookup));
418   surf_cpu_model->common_public->states.failed_action_set =
419       xbt_swag_new(xbt_swag_offset(action, state_hookup));
420   surf_cpu_model->common_public->states.done_action_set =
421       xbt_swag_new(xbt_swag_offset(action, state_hookup));
422
423   surf_cpu_model->common_public->name_service = name_service;
424   surf_cpu_model->common_public->get_resource_name = get_resource_name;
425   surf_cpu_model->common_public->action_get_state =
426       surf_action_get_state;
427   surf_cpu_model->common_public->action_get_start_time =
428       surf_action_get_start_time;
429   surf_cpu_model->common_public->action_get_finish_time =
430       surf_action_get_finish_time;
431   surf_cpu_model->common_public->action_free = action_free;
432   surf_cpu_model->common_public->action_use = action_use;
433   surf_cpu_model->common_public->action_cancel = action_cancel;
434   surf_cpu_model->common_public->action_recycle = action_recycle;
435   surf_cpu_model->common_public->action_change_state =
436       action_change_state;
437   surf_cpu_model->common_public->action_set_data = surf_action_set_data;
438   surf_cpu_model->common_public->name = "CPU";
439
440   surf_cpu_model->common_private->resource_used = resource_used;
441   surf_cpu_model->common_private->share_resources = share_resources;
442   surf_cpu_model->common_private->update_actions_state =
443       update_actions_state;
444   surf_cpu_model->common_private->update_resource_state =
445       update_resource_state;
446   surf_cpu_model->common_private->finalize = finalize;
447
448   surf_cpu_model->common_public->suspend = action_suspend;
449   surf_cpu_model->common_public->resume = action_resume;
450   surf_cpu_model->common_public->is_suspended = action_is_suspended;
451   surf_cpu_model->common_public->set_max_duration =
452       action_set_max_duration;
453   surf_cpu_model->common_public->set_priority = action_set_priority;
454   surf_cpu_model->extension_public->execute = execute;
455   surf_cpu_model->extension_public->sleep = action_sleep;
456
457   surf_cpu_model->extension_public->get_state = get_state;
458   surf_cpu_model->extension_public->get_speed = get_speed;
459   surf_cpu_model->extension_public->get_available_speed =
460       get_available_speed;
461   /*manage the properties of the cpu*/
462   surf_cpu_model->common_public->get_properties = get_properties;
463
464   if(!cpu_set) cpu_set = xbt_dict_new();
465   if (!cpu_maxmin_system) cpu_maxmin_system = lmm_system_new();
466 }
467
468 /*********************************************************************/
469 /* Basic sharing model for CPU: that is where all this started... ;) */
470 /*********************************************************************/
471 /* @InProceedings{casanova01simgrid, */
472 /*   author =       "H. Casanova", */
473 /*   booktitle =    "Proceedings of the IEEE Symposium on Cluster Computing */
474 /*                  and the Grid (CCGrid'01)", */
475 /*   publisher =    "IEEE Computer Society", */
476 /*   title =        "Simgrid: {A} Toolkit for the Simulation of Application */
477 /*                  Scheduling", */
478 /*   year =         "2001", */
479 /*   month =        may, */
480 /*   note =         "Available at */
481 /*                  \url{http://grail.sdsc.edu/papers/simgrid_ccgrid01.ps.gz}." */
482 /* } */
483 void surf_cpu_model_init_Cas01(const char *filename)
484 {
485   if (surf_cpu_model)
486     return;
487   surf_cpu_model_init_internal();
488   define_callbacks(filename);
489   xbt_dynar_push(model_list, &surf_cpu_model);
490 }