Logo AND Algorithmique Numérique Distribuée

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