Logo AND Algorithmique Numérique Distribuée

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