Logo AND Algorithmique Numérique Distribuée

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