Logo AND Algorithmique Numérique Distribuée

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