Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Implement a generic resource; use it as ancestor to specific ones
[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_free(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_use(surf_action_t action)
152 {
153   action->refcount++;
154 }
155
156 static void action_cancel(surf_action_t action)
157 {
158   surf_action_change_state(action, SURF_ACTION_FAILED);
159   return;
160 }
161
162 static void action_change_state(surf_action_t action,
163                                 e_surf_action_state_t state)
164 {
165 /*   if((state==SURF_ACTION_DONE) || (state==SURF_ACTION_FAILED)) */
166 /*     if(((surf_action_cpu_Cas01_t)action)->variable) { */
167 /*       lmm_variable_disable(cpu_maxmin_system, ((surf_action_cpu_Cas01_t)action)->variable); */
168 /*       ((surf_action_cpu_Cas01_t)action)->variable = NULL; */
169 /*     } */
170
171   surf_action_change_state(action, state);
172   return;
173 }
174
175 static double share_resources(double now)
176 {
177   s_surf_action_cpu_Cas01_t action;
178   return generic_maxmin_share_resources(surf_cpu_model->states.
179                                         running_action_set,
180                                         xbt_swag_offset(action, variable),
181                                         cpu_maxmin_system, lmm_solve);
182 }
183
184 static void update_actions_state(double now, double delta)
185 {
186   surf_action_cpu_Cas01_t action = NULL;
187   surf_action_cpu_Cas01_t next_action = NULL;
188   xbt_swag_t running_actions = surf_cpu_model->states.running_action_set;
189   /* FIXME: UNUSED
190      xbt_swag_t failed_actions =
191      surf_cpu_model->states.failed_action_set;
192    */
193
194   xbt_swag_foreach_safe(action, next_action, running_actions) {
195     double_update(&(action->generic_action.remains),
196                   lmm_variable_getvalue(action->variable) * delta);
197     if (action->generic_action.max_duration != NO_MAX_DURATION)
198       double_update(&(action->generic_action.max_duration), delta);
199     if ((action->generic_action.remains <= 0) &&
200         (lmm_get_variable_weight(action->variable) > 0)) {
201       action->generic_action.finish = surf_get_clock();
202       action_change_state((surf_action_t) action, SURF_ACTION_DONE);
203     } else if ((action->generic_action.max_duration != NO_MAX_DURATION) &&
204                (action->generic_action.max_duration <= 0)) {
205       action->generic_action.finish = surf_get_clock();
206       action_change_state((surf_action_t) action, SURF_ACTION_DONE);
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, double date)
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       lmm_constraint_t cnst = cpu->constraint;
228       lmm_variable_t var = NULL;
229       lmm_element_t elem = NULL;
230
231       cpu->state_current = SURF_CPU_OFF;
232
233       while ((var = lmm_get_var_from_cnst(cpu_maxmin_system, cnst, &elem))) {
234         surf_action_t action = lmm_variable_id(var);
235
236         if (surf_action_get_state(action) == SURF_ACTION_RUNNING ||
237             surf_action_get_state(action) == SURF_ACTION_READY ||
238             surf_action_get_state(action) == SURF_ACTION_NOT_IN_THE_SYSTEM) {
239           action->finish = date;
240           action_change_state(action, SURF_ACTION_FAILED);
241         }
242       }
243     }
244   } else {
245     CRITICAL0("Unknown event ! \n");
246     xbt_abort();
247   }
248
249   return;
250 }
251
252 static surf_action_t execute(void *cpu, double size)
253 {
254   surf_action_cpu_Cas01_t action = NULL;
255   cpu_Cas01_t CPU = cpu;
256
257   XBT_IN2("(%s,%g)", CPU->name, size);
258   action = xbt_new0(s_surf_action_cpu_Cas01_t, 1);
259
260   action->generic_action.refcount = 1;
261   action->generic_action.cost = size;
262   action->generic_action.remains = size;
263   action->generic_action.priority = 1.0;
264   action->generic_action.max_duration = NO_MAX_DURATION;
265   action->generic_action.start = surf_get_clock();
266   action->generic_action.finish = -1.0;
267   action->generic_action.model_type = surf_cpu_model;
268   action->suspended = 0;        /* Should be useless because of the
269                                    calloc but it seems to help valgrind... */
270
271   if (CPU->state_current == SURF_CPU_ON)
272     action->generic_action.state_set =
273       surf_cpu_model->states.running_action_set;
274   else
275     action->generic_action.state_set =
276       surf_cpu_model->states.failed_action_set;
277
278   xbt_swag_insert(action, action->generic_action.state_set);
279
280   action->variable = lmm_variable_new(cpu_maxmin_system, action,
281                                       action->generic_action.priority,
282                                       -1.0, 1);
283   lmm_expand(cpu_maxmin_system, CPU->constraint, action->variable, 1.0);
284   XBT_OUT;
285   return (surf_action_t) action;
286 }
287
288 static surf_action_t action_sleep(void *cpu, double duration)
289 {
290   surf_action_cpu_Cas01_t action = NULL;
291
292   if (duration > 0)
293     duration = MAX(duration, MAXMIN_PRECISION);
294
295   XBT_IN2("(%s,%g)", ((cpu_Cas01_t) cpu)->name, duration);
296   action = (surf_action_cpu_Cas01_t) execute(cpu, 1.0);
297   action->generic_action.max_duration = duration;
298   action->suspended = 2;
299   if (duration == NO_MAX_DURATION) {
300     /* Move to the *end* of the corresponding action set. This convention
301        is used to speed up update_resource_state  */
302     xbt_swag_remove(action, ((surf_action_t) action)->state_set);
303     ((surf_action_t) action)->state_set =
304       running_action_set_that_does_not_need_being_checked;
305     xbt_swag_insert(action, ((surf_action_t) action)->state_set);
306   }
307
308   lmm_update_variable_weight(cpu_maxmin_system, action->variable, 0.0);
309   XBT_OUT;
310   return (surf_action_t) action;
311 }
312
313 static void action_suspend(surf_action_t action)
314 {
315   XBT_IN1("(%p)", action);
316   if (((surf_action_cpu_Cas01_t) action)->suspended != 2) {
317     lmm_update_variable_weight(cpu_maxmin_system,
318                                ((surf_action_cpu_Cas01_t) action)->variable,
319                                0.0);
320     ((surf_action_cpu_Cas01_t) action)->suspended = 1;
321   }
322   XBT_OUT;
323 }
324
325 static void action_resume(surf_action_t action)
326 {
327   XBT_IN1("(%p)", action);
328   if (((surf_action_cpu_Cas01_t) action)->suspended != 2) {
329     lmm_update_variable_weight(cpu_maxmin_system,
330                                ((surf_action_cpu_Cas01_t) action)->variable,
331                                action->priority);
332     ((surf_action_cpu_Cas01_t) action)->suspended = 0;
333   }
334   XBT_OUT;
335 }
336
337 static int action_is_suspended(surf_action_t action)
338 {
339   return (((surf_action_cpu_Cas01_t) action)->suspended == 1);
340 }
341
342 static void action_set_max_duration(surf_action_t action, double duration)
343 {
344   XBT_IN2("(%p,%g)", action, duration);
345   action->max_duration = duration;
346   XBT_OUT;
347 }
348
349 static void action_set_priority(surf_action_t action, double priority)
350 {
351   XBT_IN2("(%p,%g)", action, priority);
352   action->priority = priority;
353   lmm_update_variable_weight(cpu_maxmin_system,
354                              ((surf_action_cpu_Cas01_t) action)->variable,
355                              priority);
356
357   XBT_OUT;
358 }
359
360 static e_surf_cpu_state_t get_state(void *cpu)
361 {
362   return ((cpu_Cas01_t) cpu)->state_current;
363 }
364
365 static double get_speed(void *cpu, double load)
366 {
367   return load * (((cpu_Cas01_t) cpu)->power_scale);
368 }
369
370 static double get_available_speed(void *cpu)
371 {
372   /* number between 0 and 1 */
373   return ((cpu_Cas01_t) cpu)->power_current;
374 }
375
376 static xbt_dict_t get_properties(void *cpu)
377 {
378   return ((cpu_Cas01_t) cpu)->properties;
379 }
380
381 static void finalize(void)
382 {
383   lmm_system_free(cpu_maxmin_system);
384   cpu_maxmin_system = NULL;
385
386   surf_model_exit(surf_cpu_model);
387   surf_cpu_model = NULL;
388
389   xbt_swag_free(running_action_set_that_does_not_need_being_checked);
390   running_action_set_that_does_not_need_being_checked = NULL;
391 }
392
393 static void surf_cpu_model_init_internal(void)
394 {
395   s_surf_action_t action;
396
397   surf_cpu_model = surf_model_init();
398
399   running_action_set_that_does_not_need_being_checked =
400     xbt_swag_new(xbt_swag_offset(action, state_hookup));
401
402   surf_cpu_model->action_get_state = surf_action_get_state;
403   surf_cpu_model->action_get_start_time = surf_action_get_start_time;
404   surf_cpu_model->action_get_finish_time = surf_action_get_finish_time;
405   surf_cpu_model->action_free = action_free;
406   surf_cpu_model->action_use = action_use;
407   surf_cpu_model->action_cancel = action_cancel;
408   surf_cpu_model->action_change_state = action_change_state;
409   surf_cpu_model->action_set_data = surf_action_set_data;
410   surf_cpu_model->name = "CPU";
411
412   surf_cpu_model->model_private->resource_used = resource_used;
413   surf_cpu_model->model_private->share_resources = share_resources;
414   surf_cpu_model->model_private->update_actions_state = update_actions_state;
415   surf_cpu_model->model_private->update_resource_state =
416     update_resource_state;
417   surf_cpu_model->model_private->finalize = finalize;
418
419   surf_cpu_model->suspend = action_suspend;
420   surf_cpu_model->resume = action_resume;
421   surf_cpu_model->is_suspended = action_is_suspended;
422   surf_cpu_model->set_max_duration = action_set_max_duration;
423   surf_cpu_model->set_priority = action_set_priority;
424   surf_cpu_model->extension.cpu.execute = execute;
425   surf_cpu_model->extension.cpu.sleep = action_sleep;
426
427   surf_cpu_model->extension.cpu.get_state = get_state;
428   surf_cpu_model->extension.cpu.get_speed = get_speed;
429   surf_cpu_model->extension.cpu.get_available_speed = get_available_speed;
430   /*manage the properties of the cpu */
431   surf_cpu_model->get_properties = get_properties;
432
433   if (!cpu_maxmin_system)
434     cpu_maxmin_system = lmm_system_new();
435 }
436
437 /*********************************************************************/
438 /* Basic sharing model for CPU: that is where all this started... ;) */
439 /*********************************************************************/
440 /* @InProceedings{casanova01simgrid, */
441 /*   author =       "H. Casanova", */
442 /*   booktitle =    "Proceedings of the IEEE Symposium on Cluster Computing */
443 /*                  and the Grid (CCGrid'01)", */
444 /*   publisher =    "IEEE Computer Society", */
445 /*   title =        "Simgrid: {A} Toolkit for the Simulation of Application */
446 /*                  Scheduling", */
447 /*   year =         "2001", */
448 /*   month =        may, */
449 /*   note =         "Available at */
450 /*                  \url{http://grail.sdsc.edu/papers/simgrid_ccgrid01.ps.gz}." */
451 /* } */
452 void surf_cpu_model_init_Cas01(const char *filename)
453 {
454   if (surf_cpu_model)
455     return;
456   surf_cpu_model_init_internal();
457   define_callbacks(filename);
458   xbt_dynar_push(model_list, &surf_cpu_model);
459 }