Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
move action_ref one layer up: not a model method anymore, but function surf_action_re...
[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_model_t surf_cpu_model = NULL;
14 lmm_system_t cpu_maxmin_system = NULL;
15
16
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   xbt_dict_free(&(((cpu_Cas01_t) cpu)->properties));
22   surf_resource_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(!surf_model_resource_by_name(surf_cpu_model, name),
34               "Host '%s' declared several times in the platform file", name);
35   cpu->generic_resource.model = surf_cpu_model;
36   cpu->generic_resource.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(surf_model_resource_set(surf_cpu_model), 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,
87           power_trace, state_initial, state_trace, current_property_set);
88
89 }
90
91 static void add_traces_cpu(void)
92 {
93   xbt_dict_cursor_t cursor = NULL;
94   char *trace_name, *elm;
95
96   static int called = 0;
97
98   if (called)
99     return;
100   called = 1;
101
102
103   /* connect all traces relative to hosts */
104   xbt_dict_foreach(trace_connect_list_host_avail, cursor, trace_name, elm) {
105     tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
106     cpu_Cas01_t host = surf_model_resource_by_name(surf_cpu_model, elm);
107
108     xbt_assert1(host, "Host %s undefined", elm);
109     xbt_assert1(trace, "Trace %s undefined", trace_name);
110
111     host->state_event = tmgr_history_add_trace(history, trace, 0.0, 0, host);
112   }
113
114   xbt_dict_foreach(trace_connect_list_power, cursor, trace_name, elm) {
115     tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
116     cpu_Cas01_t host = surf_model_resource_by_name(surf_cpu_model, elm);
117
118     xbt_assert1(host, "Host %s undefined", elm);
119     xbt_assert1(trace, "Trace %s undefined", trace_name);
120
121     host->power_event = tmgr_history_add_trace(history, trace, 0.0, 0, host);
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 }
130
131 static int resource_used(void *resource_id)
132 {
133   return lmm_constraint_used(cpu_maxmin_system,
134                              ((cpu_Cas01_t) resource_id)->constraint);
135 }
136
137 static int action_unref(surf_action_t action)
138 {
139   action->refcount--;
140   if (!action->refcount) {
141     xbt_swag_remove(action, action->state_set);
142     if (((surf_action_cpu_Cas01_t) action)->variable)
143       lmm_variable_free(cpu_maxmin_system,
144                         ((surf_action_cpu_Cas01_t) action)->variable);
145     free(action);
146     return 1;
147   }
148   return 0;
149 }
150
151 static void action_cancel(surf_action_t action)
152 {
153   surf_action_state_set(action, SURF_ACTION_FAILED);
154   return;
155 }
156
157 static void cpu_action_state_set(surf_action_t action,
158                                 e_surf_action_state_t state)
159 {
160 /*   if((state==SURF_ACTION_DONE) || (state==SURF_ACTION_FAILED)) */
161 /*     if(((surf_action_cpu_Cas01_t)action)->variable) { */
162 /*       lmm_variable_disable(cpu_maxmin_system, ((surf_action_cpu_Cas01_t)action)->variable); */
163 /*       ((surf_action_cpu_Cas01_t)action)->variable = NULL; */
164 /*     } */
165
166   surf_action_state_set(action, state);
167   return;
168 }
169
170 static double share_resources(double now)
171 {
172   s_surf_action_cpu_Cas01_t action;
173   return generic_maxmin_share_resources(surf_cpu_model->states.
174                                         running_action_set,
175                                         xbt_swag_offset(action, variable),
176                                         cpu_maxmin_system, lmm_solve);
177 }
178
179 static void update_actions_state(double now, double delta)
180 {
181   surf_action_cpu_Cas01_t action = NULL;
182   surf_action_cpu_Cas01_t next_action = NULL;
183   xbt_swag_t running_actions = surf_cpu_model->states.running_action_set;
184   /* FIXME: UNUSED
185      xbt_swag_t failed_actions =
186      surf_cpu_model->states.failed_action_set;
187    */
188
189   xbt_swag_foreach_safe(action, next_action, running_actions) {
190     double_update(&(action->generic_action.remains),
191                   lmm_variable_getvalue(action->variable) * delta);
192     if (action->generic_action.max_duration != NO_MAX_DURATION)
193       double_update(&(action->generic_action.max_duration), delta);
194     if ((action->generic_action.remains <= 0) &&
195         (lmm_get_variable_weight(action->variable) > 0)) {
196       action->generic_action.finish = surf_get_clock();
197       cpu_action_state_set((surf_action_t) action, SURF_ACTION_DONE);
198     } else if ((action->generic_action.max_duration != NO_MAX_DURATION) &&
199                (action->generic_action.max_duration <= 0)) {
200       action->generic_action.finish = surf_get_clock();
201       cpu_action_state_set((surf_action_t) action, SURF_ACTION_DONE);
202     }
203   }
204
205   return;
206 }
207
208 static void update_resource_state(void *id,
209                                   tmgr_trace_event_t event_type,
210                                   double value, double date)
211 {
212   cpu_Cas01_t cpu = id;
213
214   if (event_type == cpu->power_event) {
215     cpu->power_current = value;
216     lmm_update_constraint_bound(cpu_maxmin_system, cpu->constraint,
217                                 cpu->power_current * cpu->power_scale);
218   } else if (event_type == cpu->state_event) {
219     if (value > 0)
220       cpu->state_current = SURF_CPU_ON;
221     else {
222       lmm_constraint_t cnst = cpu->constraint;
223       lmm_variable_t var = NULL;
224       lmm_element_t elem = NULL;
225
226       cpu->state_current = SURF_CPU_OFF;
227
228       while ((var = lmm_get_var_from_cnst(cpu_maxmin_system, cnst, &elem))) {
229         surf_action_t action = lmm_variable_id(var);
230
231         if (surf_action_state_get(action) == SURF_ACTION_RUNNING ||
232             surf_action_state_get(action) == SURF_ACTION_READY ||
233             surf_action_state_get(action) == SURF_ACTION_NOT_IN_THE_SYSTEM) {
234           action->finish = date;
235           cpu_action_state_set(action, SURF_ACTION_FAILED);
236         }
237       }
238     }
239   } else {
240     CRITICAL0("Unknown event ! \n");
241     xbt_abort();
242   }
243
244   return;
245 }
246
247 static surf_action_t execute(void *cpu, double size)
248 {
249   surf_action_cpu_Cas01_t action = NULL;
250   cpu_Cas01_t CPU = cpu;
251
252   XBT_IN2("(%s,%g)", CPU->name, size);
253   action = xbt_new0(s_surf_action_cpu_Cas01_t, 1);
254
255   action->generic_action.refcount = 1;
256   action->generic_action.cost = size;
257   action->generic_action.remains = size;
258   action->generic_action.priority = 1.0;
259   action->generic_action.max_duration = NO_MAX_DURATION;
260   action->generic_action.start = surf_get_clock();
261   action->generic_action.finish = -1.0;
262   action->generic_action.model_type = surf_cpu_model;
263   action->suspended = 0;        /* Should be useless because of the
264                                    calloc but it seems to help valgrind... */
265
266   if (CPU->state_current == SURF_CPU_ON)
267     action->generic_action.state_set =
268       surf_cpu_model->states.running_action_set;
269   else
270     action->generic_action.state_set =
271       surf_cpu_model->states.failed_action_set;
272
273   xbt_swag_insert(action, action->generic_action.state_set);
274
275   action->variable = lmm_variable_new(cpu_maxmin_system, action,
276                                       action->generic_action.priority,
277                                       -1.0, 1);
278   lmm_expand(cpu_maxmin_system, CPU->constraint, action->variable, 1.0);
279   XBT_OUT;
280   return (surf_action_t) action;
281 }
282
283 static surf_action_t action_sleep(void *cpu, double duration)
284 {
285   surf_action_cpu_Cas01_t action = NULL;
286
287   if (duration > 0)
288     duration = MAX(duration, MAXMIN_PRECISION);
289
290   XBT_IN2("(%s,%g)", ((cpu_Cas01_t) cpu)->name, duration);
291   action = (surf_action_cpu_Cas01_t) execute(cpu, 1.0);
292   action->generic_action.max_duration = duration;
293   action->suspended = 2;
294   if (duration == NO_MAX_DURATION) {
295     /* Move to the *end* of the corresponding action set. This convention
296        is used to speed up update_resource_state  */
297     xbt_swag_remove(action, ((surf_action_t) action)->state_set);
298     ((surf_action_t) action)->state_set =
299       running_action_set_that_does_not_need_being_checked;
300     xbt_swag_insert(action, ((surf_action_t) action)->state_set);
301   }
302
303   lmm_update_variable_weight(cpu_maxmin_system, action->variable, 0.0);
304   XBT_OUT;
305   return (surf_action_t) action;
306 }
307
308 static void action_suspend(surf_action_t action)
309 {
310   XBT_IN1("(%p)", action);
311   if (((surf_action_cpu_Cas01_t) action)->suspended != 2) {
312     lmm_update_variable_weight(cpu_maxmin_system,
313                                ((surf_action_cpu_Cas01_t) action)->variable,
314                                0.0);
315     ((surf_action_cpu_Cas01_t) action)->suspended = 1;
316   }
317   XBT_OUT;
318 }
319
320 static void action_resume(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)->variable,
326                                action->priority);
327     ((surf_action_cpu_Cas01_t) action)->suspended = 0;
328   }
329   XBT_OUT;
330 }
331
332 static int action_is_suspended(surf_action_t action)
333 {
334   return (((surf_action_cpu_Cas01_t) action)->suspended == 1);
335 }
336
337 static void action_set_max_duration(surf_action_t action, double duration)
338 {
339   XBT_IN2("(%p,%g)", action, duration);
340   action->max_duration = duration;
341   XBT_OUT;
342 }
343
344 static void action_set_priority(surf_action_t action, double priority)
345 {
346   XBT_IN2("(%p,%g)", action, priority);
347   action->priority = priority;
348   lmm_update_variable_weight(cpu_maxmin_system,
349                              ((surf_action_cpu_Cas01_t) action)->variable,
350                              priority);
351
352   XBT_OUT;
353 }
354
355 static e_surf_cpu_state_t get_state(void *cpu)
356 {
357   return ((cpu_Cas01_t) cpu)->state_current;
358 }
359
360 static double get_speed(void *cpu, double load)
361 {
362   return load * (((cpu_Cas01_t) cpu)->power_scale);
363 }
364
365 static double get_available_speed(void *cpu)
366 {
367   /* number between 0 and 1 */
368   return ((cpu_Cas01_t) cpu)->power_current;
369 }
370
371 static xbt_dict_t get_properties(void *cpu)
372 {
373   return ((cpu_Cas01_t) cpu)->properties;
374 }
375
376 static void finalize(void)
377 {
378   lmm_system_free(cpu_maxmin_system);
379   cpu_maxmin_system = NULL;
380
381   surf_model_exit(surf_cpu_model);
382   surf_cpu_model = NULL;
383
384   xbt_swag_free(running_action_set_that_does_not_need_being_checked);
385   running_action_set_that_does_not_need_being_checked = NULL;
386 }
387
388 static void surf_cpu_model_init_internal(void)
389 {
390   s_surf_action_t action;
391
392   surf_cpu_model = surf_model_init();
393
394   running_action_set_that_does_not_need_being_checked =
395     xbt_swag_new(xbt_swag_offset(action, state_hookup));
396
397   surf_cpu_model->name = "CPU";
398
399   surf_cpu_model->action_unref = action_unref;
400   surf_cpu_model->action_cancel = action_cancel;
401   surf_cpu_model->action_state_set = cpu_action_state_set;
402
403   surf_cpu_model->model_private->resource_used = resource_used;
404   surf_cpu_model->model_private->share_resources = share_resources;
405   surf_cpu_model->model_private->update_actions_state = update_actions_state;
406   surf_cpu_model->model_private->update_resource_state =
407     update_resource_state;
408   surf_cpu_model->model_private->finalize = finalize;
409
410   surf_cpu_model->suspend = action_suspend;
411   surf_cpu_model->resume = action_resume;
412   surf_cpu_model->is_suspended = action_is_suspended;
413   surf_cpu_model->set_max_duration = action_set_max_duration;
414   surf_cpu_model->set_priority = action_set_priority;
415   surf_cpu_model->extension.cpu.execute = execute;
416   surf_cpu_model->extension.cpu.sleep = action_sleep;
417
418   surf_cpu_model->extension.cpu.get_state = get_state;
419   surf_cpu_model->extension.cpu.get_speed = get_speed;
420   surf_cpu_model->extension.cpu.get_available_speed = get_available_speed;
421   /*manage the properties of the cpu */
422   surf_cpu_model->get_properties = get_properties;
423
424   if (!cpu_maxmin_system)
425     cpu_maxmin_system = lmm_system_new();
426 }
427
428 /*********************************************************************/
429 /* Basic sharing model for CPU: that is where all this started... ;) */
430 /*********************************************************************/
431 /* @InProceedings{casanova01simgrid, */
432 /*   author =       "H. Casanova", */
433 /*   booktitle =    "Proceedings of the IEEE Symposium on Cluster Computing */
434 /*                  and the Grid (CCGrid'01)", */
435 /*   publisher =    "IEEE Computer Society", */
436 /*   title =        "Simgrid: {A} Toolkit for the Simulation of Application */
437 /*                  Scheduling", */
438 /*   year =         "2001", */
439 /*   month =        may, */
440 /*   note =         "Available at */
441 /*                  \url{http://grail.sdsc.edu/papers/simgrid_ccgrid01.ps.gz}." */
442 /* } */
443 void surf_cpu_model_init_Cas01(const char *filename)
444 {
445   if (surf_cpu_model)
446     return;
447   surf_cpu_model_init_internal();
448   define_callbacks(filename);
449   xbt_dynar_push(model_list, &surf_cpu_model);
450 }