Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
2860616152012ff77dfd7a1cada042bf419b53a1
[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->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 = surf_cpu_model->states.running_action_set;
195   /* FIXME: UNUSED
196      xbt_swag_t failed_actions =
197      surf_cpu_model->states.failed_action_set;
198    */
199
200   xbt_swag_foreach_safe(action, next_action, running_actions) {
201     double_update(&(action->generic_action.remains),
202                   lmm_variable_getvalue(action->variable) * delta);
203     if (action->generic_action.max_duration != NO_MAX_DURATION)
204       double_update(&(action->generic_action.max_duration), delta);
205     if ((action->generic_action.remains <= 0) &&
206         (lmm_get_variable_weight(action->variable) > 0)) {
207       action->generic_action.finish = surf_get_clock();
208       action_change_state((surf_action_t) action, SURF_ACTION_DONE);
209     } else if ((action->generic_action.max_duration != NO_MAX_DURATION) &&
210                (action->generic_action.max_duration <= 0)) {
211       action->generic_action.finish = surf_get_clock();
212       action_change_state((surf_action_t) action, SURF_ACTION_DONE);
213     }
214   }
215
216   return;
217 }
218
219 static void update_resource_state(void *id,
220                                   tmgr_trace_event_t event_type,
221                                   double value, double date)
222 {
223   cpu_Cas01_t cpu = id;
224
225   if (event_type == cpu->power_event) {
226     cpu->power_current = value;
227     lmm_update_constraint_bound(cpu_maxmin_system, cpu->constraint,
228                                 cpu->power_current * cpu->power_scale);
229   } else if (event_type == cpu->state_event) {
230     if (value > 0)
231       cpu->state_current = SURF_CPU_ON;
232     else {
233       lmm_constraint_t cnst = cpu->constraint;
234       lmm_variable_t var = NULL;
235       lmm_element_t elem = NULL;
236
237       cpu->state_current = SURF_CPU_OFF;
238
239       while ((var = lmm_get_var_from_cnst(cpu_maxmin_system, cnst, &elem))) {
240         surf_action_t action = lmm_variable_id(var);
241
242         if (surf_action_get_state(action) == SURF_ACTION_RUNNING ||
243             surf_action_get_state(action) == SURF_ACTION_READY ||
244             surf_action_get_state(action) == SURF_ACTION_NOT_IN_THE_SYSTEM) {
245           action->finish = date;
246           action_change_state(action, SURF_ACTION_FAILED);
247         }
248       }
249     }
250   } else {
251     CRITICAL0("Unknown event ! \n");
252     xbt_abort();
253   }
254
255   return;
256 }
257
258 static surf_action_t execute(void *cpu, double size)
259 {
260   surf_action_cpu_Cas01_t action = NULL;
261   cpu_Cas01_t CPU = cpu;
262
263   XBT_IN2("(%s,%g)", CPU->name, size);
264   action = xbt_new0(s_surf_action_cpu_Cas01_t, 1);
265
266   action->generic_action.refcount = 1;
267   action->generic_action.cost = size;
268   action->generic_action.remains = size;
269   action->generic_action.priority = 1.0;
270   action->generic_action.max_duration = NO_MAX_DURATION;
271   action->generic_action.start = surf_get_clock();
272   action->generic_action.finish = -1.0;
273   action->generic_action.model_type = surf_cpu_model;
274   action->suspended = 0;        /* Should be useless because of the
275                                    calloc but it seems to help valgrind... */
276
277   if (CPU->state_current == SURF_CPU_ON)
278     action->generic_action.state_set =
279       surf_cpu_model->states.running_action_set;
280   else
281     action->generic_action.state_set =
282       surf_cpu_model->states.failed_action_set;
283
284   xbt_swag_insert(action, action->generic_action.state_set);
285
286   action->variable = lmm_variable_new(cpu_maxmin_system, action,
287                                       action->generic_action.priority,
288                                       -1.0, 1);
289   lmm_expand(cpu_maxmin_system, CPU->constraint, action->variable, 1.0);
290   XBT_OUT;
291   return (surf_action_t) action;
292 }
293
294 static surf_action_t action_sleep(void *cpu, double duration)
295 {
296   surf_action_cpu_Cas01_t action = NULL;
297
298   if (duration > 0)
299     duration = MAX(duration, MAXMIN_PRECISION);
300
301   XBT_IN2("(%s,%g)", ((cpu_Cas01_t) cpu)->name, duration);
302   action = (surf_action_cpu_Cas01_t) execute(cpu, 1.0);
303   action->generic_action.max_duration = duration;
304   action->suspended = 2;
305   if (duration == NO_MAX_DURATION) {
306     /* Move to the *end* of the corresponding action set. This convention
307        is used to speed up update_resource_state  */
308     xbt_swag_remove(action, ((surf_action_t) action)->state_set);
309     ((surf_action_t) action)->state_set =
310       running_action_set_that_does_not_need_being_checked;
311     xbt_swag_insert(action, ((surf_action_t) action)->state_set);
312   }
313
314   lmm_update_variable_weight(cpu_maxmin_system, action->variable, 0.0);
315   XBT_OUT;
316   return (surf_action_t) action;
317 }
318
319 static void action_suspend(surf_action_t action)
320 {
321   XBT_IN1("(%p)", action);
322   if (((surf_action_cpu_Cas01_t) action)->suspended != 2) {
323     lmm_update_variable_weight(cpu_maxmin_system,
324                                ((surf_action_cpu_Cas01_t) action)->variable,
325                                0.0);
326     ((surf_action_cpu_Cas01_t) action)->suspended = 1;
327   }
328   XBT_OUT;
329 }
330
331 static void action_resume(surf_action_t action)
332 {
333   XBT_IN1("(%p)", action);
334   if (((surf_action_cpu_Cas01_t) action)->suspended != 2) {
335     lmm_update_variable_weight(cpu_maxmin_system,
336                                ((surf_action_cpu_Cas01_t) action)->variable,
337                                action->priority);
338     ((surf_action_cpu_Cas01_t) action)->suspended = 0;
339   }
340   XBT_OUT;
341 }
342
343 static int action_is_suspended(surf_action_t action)
344 {
345   return (((surf_action_cpu_Cas01_t) action)->suspended == 1);
346 }
347
348 static void action_set_max_duration(surf_action_t action, double duration)
349 {
350   XBT_IN2("(%p,%g)", action, duration);
351   action->max_duration = duration;
352   XBT_OUT;
353 }
354
355 static void action_set_priority(surf_action_t action, double priority)
356 {
357   XBT_IN2("(%p,%g)", action, priority);
358   action->priority = priority;
359   lmm_update_variable_weight(cpu_maxmin_system,
360                              ((surf_action_cpu_Cas01_t) action)->variable,
361                              priority);
362
363   XBT_OUT;
364 }
365
366 static e_surf_cpu_state_t get_state(void *cpu)
367 {
368   return ((cpu_Cas01_t) cpu)->state_current;
369 }
370
371 static double get_speed(void *cpu, double load)
372 {
373   return load * (((cpu_Cas01_t) cpu)->power_scale);
374 }
375
376 static double get_available_speed(void *cpu)
377 {
378   /* number between 0 and 1 */
379   return ((cpu_Cas01_t) cpu)->power_current;
380 }
381
382 static xbt_dict_t get_properties(void *cpu)
383 {
384   return ((cpu_Cas01_t) cpu)->properties;
385 }
386
387 static void finalize(void)
388 {
389   lmm_system_free(cpu_maxmin_system);
390   cpu_maxmin_system = NULL;
391
392   surf_model_exit(surf_cpu_model);
393   surf_cpu_model = NULL;
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
399 static void surf_cpu_model_init_internal(void)
400 {
401   s_surf_action_t action;
402
403   surf_cpu_model = surf_model_init();
404
405   running_action_set_that_does_not_need_being_checked =
406     xbt_swag_new(xbt_swag_offset(action, state_hookup));
407
408   surf_cpu_model->get_resource_name = get_resource_name;
409   surf_cpu_model->action_get_state = surf_action_get_state;
410   surf_cpu_model->action_get_start_time = surf_action_get_start_time;
411   surf_cpu_model->action_get_finish_time = surf_action_get_finish_time;
412   surf_cpu_model->action_free = action_free;
413   surf_cpu_model->action_use = action_use;
414   surf_cpu_model->action_cancel = action_cancel;
415   surf_cpu_model->action_change_state = action_change_state;
416   surf_cpu_model->action_set_data = surf_action_set_data;
417   surf_cpu_model->name = "CPU";
418
419   surf_cpu_model->model_private->resource_used = resource_used;
420   surf_cpu_model->model_private->share_resources = share_resources;
421   surf_cpu_model->model_private->update_actions_state = update_actions_state;
422   surf_cpu_model->model_private->update_resource_state =
423     update_resource_state;
424   surf_cpu_model->model_private->finalize = finalize;
425
426   surf_cpu_model->suspend = action_suspend;
427   surf_cpu_model->resume = action_resume;
428   surf_cpu_model->is_suspended = action_is_suspended;
429   surf_cpu_model->set_max_duration = action_set_max_duration;
430   surf_cpu_model->set_priority = action_set_priority;
431   surf_cpu_model->extension.cpu.execute = execute;
432   surf_cpu_model->extension.cpu.sleep = action_sleep;
433
434   surf_cpu_model->extension.cpu.get_state = get_state;
435   surf_cpu_model->extension.cpu.get_speed = get_speed;
436   surf_cpu_model->extension.cpu.get_available_speed = get_available_speed;
437   /*manage the properties of the cpu */
438   surf_cpu_model->get_properties = get_properties;
439
440   if (!cpu_maxmin_system)
441     cpu_maxmin_system = lmm_system_new();
442 }
443
444 /*********************************************************************/
445 /* Basic sharing model for CPU: that is where all this started... ;) */
446 /*********************************************************************/
447 /* @InProceedings{casanova01simgrid, */
448 /*   author =       "H. Casanova", */
449 /*   booktitle =    "Proceedings of the IEEE Symposium on Cluster Computing */
450 /*                  and the Grid (CCGrid'01)", */
451 /*   publisher =    "IEEE Computer Society", */
452 /*   title =        "Simgrid: {A} Toolkit for the Simulation of Application */
453 /*                  Scheduling", */
454 /*   year =         "2001", */
455 /*   month =        may, */
456 /*   note =         "Available at */
457 /*                  \url{http://grail.sdsc.edu/papers/simgrid_ccgrid01.ps.gz}." */
458 /* } */
459 void surf_cpu_model_init_Cas01(const char *filename)
460 {
461   if (surf_cpu_model)
462     return;
463   surf_cpu_model_init_internal();
464   define_callbacks(filename);
465   xbt_dynar_push(model_list, &surf_cpu_model);
466 }