Logo AND Algorithmique Numérique Distribuée

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