Logo AND Algorithmique Numérique Distribuée

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