Logo AND Algorithmique Numérique Distribuée

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