Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Sanitize the way surf options are declared: in surf_config not dupplicated in simix...
[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_cpu_model_t surf_cpu_model = NULL;
14 lmm_system_t cpu_maxmin_system = NULL;
15
16 xbt_dict_t cpu_set = NULL;
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(!xbt_dict_get_or_null(cpu_set, name),
35               "Host '%s' declared several times in the platform file", name);
36   cpu->model = (surf_model_t) 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(cpu_set, 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 = xbt_dict_get_or_null(cpu_set, 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 = xbt_dict_get_or_null(cpu_set, 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 void *name_service(const char *name)
133 {
134   return xbt_dict_get_or_null(cpu_set, name);
135 }
136
137 static const char *get_resource_name(void *resource_id)
138 {
139   return ((cpu_Cas01_t) resource_id)->name;
140 }
141
142 static int resource_used(void *resource_id)
143 {
144   return lmm_constraint_used(cpu_maxmin_system,
145                              ((cpu_Cas01_t) resource_id)->constraint);
146 }
147
148 static int action_free(surf_action_t action)
149 {
150   action->refcount--;
151   if (!action->refcount) {
152     xbt_swag_remove(action, action->state_set);
153     if (((surf_action_cpu_Cas01_t) action)->variable)
154       lmm_variable_free(cpu_maxmin_system,
155                         ((surf_action_cpu_Cas01_t) action)->variable);
156     free(action);
157     return 1;
158   }
159   return 0;
160 }
161
162 static void action_use(surf_action_t action)
163 {
164   action->refcount++;
165 }
166
167 static void action_cancel(surf_action_t action)
168 {
169   surf_action_change_state(action, SURF_ACTION_FAILED);
170   return;
171 }
172
173 static void action_recycle(surf_action_t action)
174 {
175   DIE_IMPOSSIBLE;
176 }
177
178 static void action_change_state(surf_action_t action,
179                                 e_surf_action_state_t state)
180 {
181 /*   if((state==SURF_ACTION_DONE) || (state==SURF_ACTION_FAILED)) */
182 /*     if(((surf_action_cpu_Cas01_t)action)->variable) { */
183 /*       lmm_variable_disable(cpu_maxmin_system, ((surf_action_cpu_Cas01_t)action)->variable); */
184 /*       ((surf_action_cpu_Cas01_t)action)->variable = NULL; */
185 /*     } */
186
187   surf_action_change_state(action, state);
188   return;
189 }
190
191 static double share_resources(double now)
192 {
193   s_surf_action_cpu_Cas01_t action;
194   return generic_maxmin_share_resources(surf_cpu_model->common_public->states.
195                                         running_action_set,
196                                         xbt_swag_offset(action, variable),
197                                         cpu_maxmin_system, lmm_solve);
198 }
199
200 static void update_actions_state(double now, double delta)
201 {
202   surf_action_cpu_Cas01_t action = NULL;
203   surf_action_cpu_Cas01_t next_action = NULL;
204   xbt_swag_t running_actions =
205     surf_cpu_model->common_public->states.running_action_set;
206   /* FIXME: UNUSED
207      xbt_swag_t failed_actions =
208      surf_cpu_model->common_public->states.failed_action_set;
209    */
210
211   xbt_swag_foreach_safe(action, next_action, running_actions) {
212     double_update(&(action->generic_action.remains),
213                   lmm_variable_getvalue(action->variable) * delta);
214     if (action->generic_action.max_duration != NO_MAX_DURATION)
215       double_update(&(action->generic_action.max_duration), delta);
216     if ((action->generic_action.remains <= 0) &&
217         (lmm_get_variable_weight(action->variable) > 0)) {
218       action->generic_action.finish = surf_get_clock();
219       action_change_state((surf_action_t) action, SURF_ACTION_DONE);
220     } else if ((action->generic_action.max_duration != NO_MAX_DURATION) &&
221                (action->generic_action.max_duration <= 0)) {
222       action->generic_action.finish = surf_get_clock();
223       action_change_state((surf_action_t) action, SURF_ACTION_DONE);
224     }
225   }
226
227   return;
228 }
229
230 static void update_resource_state(void *id,
231                                   tmgr_trace_event_t event_type,
232                                   double value, double date)
233 {
234   cpu_Cas01_t cpu = id;
235
236   if (event_type == cpu->power_event) {
237     cpu->power_current = value;
238     lmm_update_constraint_bound(cpu_maxmin_system, cpu->constraint,
239                                 cpu->power_current * cpu->power_scale);
240   } else if (event_type == cpu->state_event) {
241     if (value > 0)
242       cpu->state_current = SURF_CPU_ON;
243     else {
244       lmm_constraint_t cnst = cpu->constraint;
245       lmm_variable_t var = NULL;
246       lmm_element_t elem = NULL;
247
248       cpu->state_current = SURF_CPU_OFF;
249
250       while ((var = lmm_get_var_from_cnst(cpu_maxmin_system, cnst, &elem))) {
251         surf_action_t action = lmm_variable_id(var);
252
253         if (surf_action_get_state(action) == SURF_ACTION_RUNNING ||
254             surf_action_get_state(action) == SURF_ACTION_READY ||
255             surf_action_get_state(action) == SURF_ACTION_NOT_IN_THE_SYSTEM) {
256           action->finish = date;
257           action_change_state(action, SURF_ACTION_FAILED);
258         }
259       }
260     }
261   } else {
262     CRITICAL0("Unknown event ! \n");
263     xbt_abort();
264   }
265
266   return;
267 }
268
269 static surf_action_t execute(void *cpu, double size)
270 {
271   surf_action_cpu_Cas01_t action = NULL;
272   cpu_Cas01_t CPU = cpu;
273
274   XBT_IN2("(%s,%g)", CPU->name, size);
275   action = xbt_new0(s_surf_action_cpu_Cas01_t, 1);
276
277   action->generic_action.refcount = 1;
278   action->generic_action.cost = size;
279   action->generic_action.remains = size;
280   action->generic_action.priority = 1.0;
281   action->generic_action.max_duration = NO_MAX_DURATION;
282   action->generic_action.start = surf_get_clock();
283   action->generic_action.finish = -1.0;
284   action->generic_action.model_type = (surf_model_t) surf_cpu_model;
285   action->suspended = 0;        /* Should be useless because of the
286                                    calloc but it seems to help valgrind... */
287
288   if (CPU->state_current == SURF_CPU_ON)
289     action->generic_action.state_set =
290       surf_cpu_model->common_public->states.running_action_set;
291   else
292     action->generic_action.state_set =
293       surf_cpu_model->common_public->states.failed_action_set;
294
295   xbt_swag_insert(action, action->generic_action.state_set);
296
297   action->variable = lmm_variable_new(cpu_maxmin_system, action,
298                                       action->generic_action.priority,
299                                       -1.0, 1);
300   lmm_expand(cpu_maxmin_system, CPU->constraint, action->variable, 1.0);
301   XBT_OUT;
302   return (surf_action_t) action;
303 }
304
305 static surf_action_t action_sleep(void *cpu, double duration)
306 {
307   surf_action_cpu_Cas01_t action = NULL;
308
309   if (duration > 0)
310     duration = MAX(duration, MAXMIN_PRECISION);
311
312   XBT_IN2("(%s,%g)", ((cpu_Cas01_t) cpu)->name, duration);
313   action = (surf_action_cpu_Cas01_t) execute(cpu, 1.0);
314   action->generic_action.max_duration = duration;
315   action->suspended = 2;
316   if (duration == NO_MAX_DURATION) {
317     /* Move to the *end* of the corresponding action set. This convention
318        is used to speed up update_resource_state  */
319     xbt_swag_remove(action, ((surf_action_t) action)->state_set);
320     ((surf_action_t) action)->state_set =
321       running_action_set_that_does_not_need_being_checked;
322     xbt_swag_insert(action, ((surf_action_t) action)->state_set);
323   }
324
325   lmm_update_variable_weight(cpu_maxmin_system, action->variable, 0.0);
326   XBT_OUT;
327   return (surf_action_t) action;
328 }
329
330 static void action_suspend(surf_action_t action)
331 {
332   XBT_IN1("(%p)", action);
333   if (((surf_action_cpu_Cas01_t) action)->suspended != 2) {
334     lmm_update_variable_weight(cpu_maxmin_system,
335                                ((surf_action_cpu_Cas01_t) action)->variable,
336                                0.0);
337     ((surf_action_cpu_Cas01_t) action)->suspended = 1;
338   }
339   XBT_OUT;
340 }
341
342 static void action_resume(surf_action_t action)
343 {
344   XBT_IN1("(%p)", action);
345   if (((surf_action_cpu_Cas01_t) action)->suspended != 2) {
346     lmm_update_variable_weight(cpu_maxmin_system,
347                                ((surf_action_cpu_Cas01_t) action)->variable,
348                                action->priority);
349     ((surf_action_cpu_Cas01_t) action)->suspended = 0;
350   }
351   XBT_OUT;
352 }
353
354 static int action_is_suspended(surf_action_t action)
355 {
356   return (((surf_action_cpu_Cas01_t) action)->suspended == 1);
357 }
358
359 static void action_set_max_duration(surf_action_t action, double duration)
360 {
361   XBT_IN2("(%p,%g)", action, duration);
362   action->max_duration = duration;
363   XBT_OUT;
364 }
365
366 static void action_set_priority(surf_action_t action, double priority)
367 {
368   XBT_IN2("(%p,%g)", action, priority);
369   action->priority = priority;
370   lmm_update_variable_weight(cpu_maxmin_system,
371                              ((surf_action_cpu_Cas01_t) action)->variable,
372                              priority);
373
374   XBT_OUT;
375 }
376
377 static e_surf_cpu_state_t get_state(void *cpu)
378 {
379   return ((cpu_Cas01_t) cpu)->state_current;
380 }
381
382 static double get_speed(void *cpu, double load)
383 {
384   return load * (((cpu_Cas01_t) cpu)->power_scale);
385 }
386
387 static double get_available_speed(void *cpu)
388 {
389   /* number between 0 and 1 */
390   return ((cpu_Cas01_t) cpu)->power_current;
391 }
392
393 static xbt_dict_t get_properties(void *cpu)
394 {
395   return ((cpu_Cas01_t) cpu)->properties;
396 }
397
398 static void finalize(void)
399 {
400   xbt_dict_free(&cpu_set);
401   lmm_system_free(cpu_maxmin_system);
402   cpu_maxmin_system = NULL;
403   xbt_swag_free(surf_cpu_model->common_public->states.ready_action_set);
404   xbt_swag_free(surf_cpu_model->common_public->states.running_action_set);
405   xbt_swag_free(surf_cpu_model->common_public->states.failed_action_set);
406   xbt_swag_free(surf_cpu_model->common_public->states.done_action_set);
407   xbt_swag_free(running_action_set_that_does_not_need_being_checked);
408   running_action_set_that_does_not_need_being_checked = NULL;
409   free(surf_cpu_model->common_public);
410   free(surf_cpu_model->common_private);
411   free(surf_cpu_model->extension_public);
412
413   free(surf_cpu_model);
414   surf_cpu_model = NULL;
415 }
416
417 static void surf_cpu_model_init_internal(void)
418 {
419   s_surf_action_t action;
420
421   surf_cpu_model = xbt_new0(s_surf_cpu_model_t, 1);
422
423   surf_cpu_model->common_private = xbt_new0(s_surf_model_private_t, 1);
424   surf_cpu_model->common_public = xbt_new0(s_surf_model_public_t, 1);
425
426   surf_cpu_model->extension_public =
427     xbt_new0(s_surf_cpu_model_extension_public_t, 1);
428
429   surf_cpu_model->common_public->states.ready_action_set =
430     xbt_swag_new(xbt_swag_offset(action, state_hookup));
431   surf_cpu_model->common_public->states.running_action_set =
432     xbt_swag_new(xbt_swag_offset(action, state_hookup));
433   surf_cpu_model->common_public->states.failed_action_set =
434     xbt_swag_new(xbt_swag_offset(action, state_hookup));
435   surf_cpu_model->common_public->states.done_action_set =
436     xbt_swag_new(xbt_swag_offset(action, state_hookup));
437
438   running_action_set_that_does_not_need_being_checked =
439     xbt_swag_new(xbt_swag_offset(action, state_hookup));
440
441   surf_cpu_model->common_public->name_service = name_service;
442   surf_cpu_model->common_public->get_resource_name = get_resource_name;
443   surf_cpu_model->common_public->action_get_state = surf_action_get_state;
444   surf_cpu_model->common_public->action_get_start_time =
445     surf_action_get_start_time;
446   surf_cpu_model->common_public->action_get_finish_time =
447     surf_action_get_finish_time;
448   surf_cpu_model->common_public->action_free = action_free;
449   surf_cpu_model->common_public->action_use = action_use;
450   surf_cpu_model->common_public->action_cancel = action_cancel;
451   surf_cpu_model->common_public->action_recycle = action_recycle;
452   surf_cpu_model->common_public->action_change_state = action_change_state;
453   surf_cpu_model->common_public->action_set_data = surf_action_set_data;
454   surf_cpu_model->common_public->name = "CPU";
455
456   surf_cpu_model->common_private->resource_used = resource_used;
457   surf_cpu_model->common_private->share_resources = share_resources;
458   surf_cpu_model->common_private->update_actions_state = update_actions_state;
459   surf_cpu_model->common_private->update_resource_state =
460     update_resource_state;
461   surf_cpu_model->common_private->finalize = finalize;
462
463   surf_cpu_model->common_public->suspend = action_suspend;
464   surf_cpu_model->common_public->resume = action_resume;
465   surf_cpu_model->common_public->is_suspended = action_is_suspended;
466   surf_cpu_model->common_public->set_max_duration = action_set_max_duration;
467   surf_cpu_model->common_public->set_priority = action_set_priority;
468   surf_cpu_model->extension_public->execute = execute;
469   surf_cpu_model->extension_public->sleep = action_sleep;
470
471   surf_cpu_model->extension_public->get_state = get_state;
472   surf_cpu_model->extension_public->get_speed = get_speed;
473   surf_cpu_model->extension_public->get_available_speed = get_available_speed;
474   /*manage the properties of the cpu */
475   surf_cpu_model->common_public->get_properties = get_properties;
476
477   if (!cpu_set)
478     cpu_set = xbt_dict_new();
479   if (!cpu_maxmin_system)
480     cpu_maxmin_system = lmm_system_new();
481 }
482
483 /*********************************************************************/
484 /* Basic sharing model for CPU: that is where all this started... ;) */
485 /*********************************************************************/
486 /* @InProceedings{casanova01simgrid, */
487 /*   author =       "H. Casanova", */
488 /*   booktitle =    "Proceedings of the IEEE Symposium on Cluster Computing */
489 /*                  and the Grid (CCGrid'01)", */
490 /*   publisher =    "IEEE Computer Society", */
491 /*   title =        "Simgrid: {A} Toolkit for the Simulation of Application */
492 /*                  Scheduling", */
493 /*   year =         "2001", */
494 /*   month =        may, */
495 /*   note =         "Available at */
496 /*                  \url{http://grail.sdsc.edu/papers/simgrid_ccgrid01.ps.gz}." */
497 /* } */
498 void surf_cpu_model_init_Cas01(const char *filename)
499 {
500   if (surf_cpu_model)
501     return;
502   surf_cpu_model_init_internal();
503   define_callbacks(filename);
504   xbt_dynar_push(model_list, &surf_cpu_model);
505 }