Logo AND Algorithmique Numérique Distribuée

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