Logo AND Algorithmique Numérique Distribuée

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