Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Delete the temporary route table for model Full.
[simgrid.git] / src / surf / cpu.c
1 /* Copyright (c) 2004, 2005, 2006, 2007, 2008, 2009, 2010. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "surf_private.h"
8
9 typedef s_surf_action_lmm_t s_surf_action_cpu_Cas01_t,
10     *surf_action_cpu_Cas01_t;
11
12 typedef struct cpu_Cas01 {
13   s_surf_resource_t generic_resource;
14   double power_peak;
15   double power_scale;
16   tmgr_trace_event_t power_event;
17   e_surf_resource_state_t state_current;
18   tmgr_trace_event_t state_event;
19   lmm_constraint_t constraint;
20 } s_cpu_Cas01_t, *cpu_Cas01_t;
21
22 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_cpu, surf,
23                                 "Logging specific to the SURF CPU module");
24
25
26
27 surf_model_t surf_cpu_model = NULL;
28 lmm_system_t cpu_maxmin_system = NULL;
29
30
31 static xbt_swag_t cpu_running_action_set_that_does_not_need_being_checked =
32     NULL;
33
34 static cpu_Cas01_t cpu_new(char *name, double power_peak,
35                            double power_scale,
36                            tmgr_trace_t power_trace,
37                            e_surf_resource_state_t state_initial,
38                            tmgr_trace_t state_trace,
39                            xbt_dict_t cpu_properties)
40 {
41   cpu_Cas01_t cpu = xbt_new0(s_cpu_Cas01_t, 1);
42   xbt_assert1(!surf_model_resource_by_name(surf_cpu_model, name),
43               "Host '%s' declared several times in the platform file",
44               name);
45   cpu->generic_resource.model = surf_cpu_model;
46   cpu->generic_resource.name = name;
47   cpu->generic_resource.properties = cpu_properties;
48   cpu->power_peak = power_peak;
49   xbt_assert0(cpu->power_peak > 0, "Power has to be >0");
50   cpu->power_scale = power_scale;
51   if (power_trace)
52     cpu->power_event =
53         tmgr_history_add_trace(history, power_trace, 0.0, 0, cpu);
54
55   cpu->state_current = state_initial;
56   if (state_trace)
57     cpu->state_event =
58         tmgr_history_add_trace(history, state_trace, 0.0, 0, cpu);
59
60   cpu->constraint =
61       lmm_constraint_new(cpu_maxmin_system, cpu,
62                          cpu->power_scale * cpu->power_peak);
63
64   xbt_dict_set(surf_model_resource_set(surf_cpu_model), name, cpu,
65                surf_resource_free);
66 #ifdef HAVE_TRACING
67   TRACE_surf_host_declaration(name, cpu->power_scale * cpu->power_peak);
68 #endif
69
70   return cpu;
71 }
72
73
74 static void parse_cpu_init(void)
75 {
76   double power_peak = 0.0;
77   double power_scale = 0.0;
78   tmgr_trace_t power_trace = NULL;
79   e_surf_resource_state_t state_initial = SURF_RESOURCE_OFF;
80   tmgr_trace_t state_trace = NULL;
81
82   power_peak = get_cpu_power(A_surfxml_host_power);
83   surf_parse_get_double(&power_scale, A_surfxml_host_availability);
84   power_trace = tmgr_trace_new(A_surfxml_host_availability_file);
85
86   xbt_assert0((A_surfxml_host_state == A_surfxml_host_state_ON) ||
87               (A_surfxml_host_state == A_surfxml_host_state_OFF),
88               "Invalid state");
89   if (A_surfxml_host_state == A_surfxml_host_state_ON)
90     state_initial = SURF_RESOURCE_ON;
91   if (A_surfxml_host_state == A_surfxml_host_state_OFF)
92     state_initial = SURF_RESOURCE_OFF;
93   state_trace = tmgr_trace_new(A_surfxml_host_state_file);
94
95   current_property_set = xbt_dict_new();
96   cpu_new(xbt_strdup(A_surfxml_host_id), power_peak, power_scale,
97           power_trace, state_initial, state_trace, current_property_set);
98
99 }
100
101 static void add_traces_cpu(void)
102 {
103   xbt_dict_cursor_t cursor = NULL;
104   char *trace_name, *elm;
105
106   static int called = 0;
107
108   if (called)
109     return;
110   called = 1;
111
112
113   /* connect all traces relative to hosts */
114   xbt_dict_foreach(trace_connect_list_host_avail, cursor, trace_name, elm) {
115     tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
116     cpu_Cas01_t host = surf_model_resource_by_name(surf_cpu_model, elm);
117
118     xbt_assert1(host, "Host %s undefined", elm);
119     xbt_assert1(trace, "Trace %s undefined", trace_name);
120
121     host->state_event =
122         tmgr_history_add_trace(history, trace, 0.0, 0, host);
123   }
124
125   xbt_dict_foreach(trace_connect_list_power, cursor, trace_name, elm) {
126     tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
127     cpu_Cas01_t host = surf_model_resource_by_name(surf_cpu_model, elm);
128
129     xbt_assert1(host, "Host %s undefined", elm);
130     xbt_assert1(trace, "Trace %s undefined", trace_name);
131
132     host->power_event =
133         tmgr_history_add_trace(history, trace, 0.0, 0, host);
134   }
135 }
136
137 static void cpu_define_callbacks(const char *file)
138 {
139   surf_parse_reset_parser();
140   surfxml_add_callback(STag_surfxml_host_cb_list, parse_cpu_init);
141   surfxml_add_callback(ETag_surfxml_platform_cb_list, &add_traces_cpu);
142 }
143
144 static int cpu_resource_used(void *resource_id)
145 {
146   return lmm_constraint_used(cpu_maxmin_system,
147                              ((cpu_Cas01_t) resource_id)->constraint);
148 }
149
150 static int cpu_action_unref(surf_action_t action)
151 {
152   action->refcount--;
153   if (!action->refcount) {
154     xbt_swag_remove(action, action->state_set);
155     if (((surf_action_cpu_Cas01_t) action)->variable)
156       lmm_variable_free(cpu_maxmin_system,
157                         ((surf_action_cpu_Cas01_t) action)->variable);
158 #ifdef HAVE_TRACING
159     if (action->category)
160       xbt_free(action->category);
161 #endif
162     free(action);
163     return 1;
164   }
165   return 0;
166 }
167
168 static void cpu_action_cancel(surf_action_t action)
169 {
170   surf_action_state_set(action, SURF_ACTION_FAILED);
171   return;
172 }
173
174 static void cpu_action_state_set(surf_action_t action,
175                                  e_surf_action_state_t state)
176 {
177 /*   if((state==SURF_ACTION_DONE) || (state==SURF_ACTION_FAILED)) */
178 /*     if(((surf_action_cpu_Cas01_t)action)->variable) { */
179 /*       lmm_variable_disable(cpu_maxmin_system, ((surf_action_cpu_Cas01_t)action)->variable); */
180 /*       ((surf_action_cpu_Cas01_t)action)->variable = NULL; */
181 /*     } */
182
183   surf_action_state_set(action, state);
184   return;
185 }
186
187 static double cpu_share_resources(double now)
188 {
189   s_surf_action_cpu_Cas01_t action;
190   return generic_maxmin_share_resources(surf_cpu_model->
191                                         states.running_action_set,
192                                         xbt_swag_offset(action, variable),
193                                         cpu_maxmin_system, lmm_solve);
194 }
195
196 static void cpu_update_actions_state(double now, double delta)
197 {
198   surf_action_cpu_Cas01_t action = NULL;
199   surf_action_cpu_Cas01_t next_action = NULL;
200   xbt_swag_t running_actions = surf_cpu_model->states.running_action_set;
201
202   xbt_swag_foreach_safe(action, next_action, running_actions) {
203 #ifdef HAVE_TRACING
204     cpu_Cas01_t x =
205         lmm_constraint_id(lmm_get_cnst_from_var
206                           (cpu_maxmin_system, action->variable, 0));
207
208     TRACE_surf_host_set_utilization(x->generic_resource.name,
209                                     action->generic_action.data,
210                                     (surf_action_t) action,
211                                     lmm_variable_getvalue
212                                     (action->variable), now - delta,
213                                     delta);
214 #endif
215     double_update(&(action->generic_action.remains),
216                   lmm_variable_getvalue(action->variable) * delta);
217     if (action->generic_action.max_duration != NO_MAX_DURATION)
218       double_update(&(action->generic_action.max_duration), delta);
219     if ((action->generic_action.remains <= 0) &&
220         (lmm_get_variable_weight(action->variable) > 0)) {
221       action->generic_action.finish = surf_get_clock();
222       cpu_action_state_set((surf_action_t) action, SURF_ACTION_DONE);
223     } else if ((action->generic_action.max_duration != NO_MAX_DURATION) &&
224                (action->generic_action.max_duration <= 0)) {
225       action->generic_action.finish = surf_get_clock();
226       cpu_action_state_set((surf_action_t) action, SURF_ACTION_DONE);
227     }
228   }
229
230   return;
231 }
232
233 static void cpu_update_resource_state(void *id,
234                                       tmgr_trace_event_t event_type,
235                                       double value, double date)
236 {
237   cpu_Cas01_t cpu = id;
238
239   if (event_type == cpu->power_event) {
240     cpu->power_scale = value;
241     lmm_update_constraint_bound(cpu_maxmin_system, cpu->constraint,
242                                 cpu->power_scale * cpu->power_peak);
243 #ifdef HAVE_TRACING
244     TRACE_surf_host_set_power(date, cpu->generic_resource.name,
245                               cpu->power_scale * cpu->power_peak);
246 #endif
247     if (tmgr_trace_event_free(event_type))
248       cpu->power_event = NULL;
249   } else if (event_type == cpu->state_event) {
250     if (value > 0)
251       cpu->state_current = SURF_RESOURCE_ON;
252     else {
253       lmm_constraint_t cnst = cpu->constraint;
254       lmm_variable_t var = NULL;
255       lmm_element_t elem = NULL;
256
257       cpu->state_current = SURF_RESOURCE_OFF;
258
259       while ((var = lmm_get_var_from_cnst(cpu_maxmin_system, cnst, &elem))) {
260         surf_action_t action = lmm_variable_id(var);
261
262         if (surf_action_state_get(action) == SURF_ACTION_RUNNING ||
263             surf_action_state_get(action) == SURF_ACTION_READY ||
264             surf_action_state_get(action) ==
265             SURF_ACTION_NOT_IN_THE_SYSTEM) {
266           action->finish = date;
267           cpu_action_state_set(action, SURF_ACTION_FAILED);
268         }
269       }
270     }
271     if (tmgr_trace_event_free(event_type))
272       cpu->state_event = NULL;
273   } else {
274     CRITICAL0("Unknown event ! \n");
275     xbt_abort();
276   }
277
278   return;
279 }
280
281 static surf_action_t cpu_execute(void *cpu, double size)
282 {
283   surf_action_cpu_Cas01_t action = NULL;
284   cpu_Cas01_t CPU = cpu;
285
286   XBT_IN2("(%s,%g)", surf_resource_name(CPU), size);
287   action =
288       surf_action_new(sizeof(s_surf_action_cpu_Cas01_t), size,
289                       surf_cpu_model,
290                       CPU->state_current != SURF_RESOURCE_ON);
291
292   action->suspended = 0;        /* Should be useless because of the
293                                    calloc but it seems to help valgrind... */
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 cpu_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)", surf_resource_name(cpu), duration);
311   action = (surf_action_cpu_Cas01_t) cpu_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         cpu_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 cpu_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)
334                                 action)->variable, 0.0);
335     ((surf_action_cpu_Cas01_t) action)->suspended = 1;
336   }
337   XBT_OUT;
338 }
339
340 static void cpu_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)
346                                 action)->variable, action->priority);
347     ((surf_action_cpu_Cas01_t) action)->suspended = 0;
348   }
349   XBT_OUT;
350 }
351
352 static int cpu_action_is_suspended(surf_action_t action)
353 {
354   return (((surf_action_cpu_Cas01_t) action)->suspended == 1);
355 }
356
357 static void cpu_action_set_max_duration(surf_action_t action,
358                                         double duration)
359 {
360   XBT_IN2("(%p,%g)", action, duration);
361   action->max_duration = duration;
362   XBT_OUT;
363 }
364
365 static void cpu_action_set_priority(surf_action_t action, double priority)
366 {
367   XBT_IN2("(%p,%g)", action, priority);
368   action->priority = priority;
369   lmm_update_variable_weight(cpu_maxmin_system,
370                              ((surf_action_cpu_Cas01_t) action)->variable,
371                              priority);
372
373   XBT_OUT;
374 }
375
376 static double cpu_action_get_remains(surf_action_t action)
377 {
378   XBT_IN1("(%p)", action);
379   return action->remains;
380   XBT_OUT;
381 }
382
383 static e_surf_resource_state_t cpu_get_state(void *cpu)
384 {
385   return ((cpu_Cas01_t) cpu)->state_current;
386 }
387
388 static double cpu_get_speed(void *cpu, double load)
389 {
390   return load * (((cpu_Cas01_t) cpu)->power_peak);
391 }
392
393 static double cpu_get_available_speed(void *cpu)
394 {
395   /* number between 0 and 1 */
396   return ((cpu_Cas01_t) cpu)->power_scale;
397 }
398
399 static void cpu_create_resource(char *name, double power_peak,
400                                 double power_scale,
401                                 tmgr_trace_t power_trace,
402                                 e_surf_resource_state_t state_initial,
403                                 tmgr_trace_t state_trace,
404                                 xbt_dict_t cpu_properties)
405 {
406   cpu_new(name, power_peak, power_scale, power_trace,
407           state_initial, state_trace, cpu_properties);
408 }
409
410 static void cpu_finalize(void)
411 {
412   lmm_system_free(cpu_maxmin_system);
413   cpu_maxmin_system = NULL;
414
415   surf_model_exit(surf_cpu_model);
416   surf_cpu_model = NULL;
417
418   xbt_swag_free(cpu_running_action_set_that_does_not_need_being_checked);
419   cpu_running_action_set_that_does_not_need_being_checked = NULL;
420 }
421
422 static void surf_cpu_model_init_internal(void)
423 {
424   s_surf_action_t action;
425
426   surf_cpu_model = surf_model_init();
427
428   cpu_running_action_set_that_does_not_need_being_checked =
429       xbt_swag_new(xbt_swag_offset(action, state_hookup));
430
431   surf_cpu_model->name = "CPU";
432
433   surf_cpu_model->action_unref = cpu_action_unref;
434   surf_cpu_model->action_cancel = cpu_action_cancel;
435   surf_cpu_model->action_state_set = cpu_action_state_set;
436
437   surf_cpu_model->model_private->resource_used = cpu_resource_used;
438   surf_cpu_model->model_private->share_resources = cpu_share_resources;
439   surf_cpu_model->model_private->update_actions_state =
440       cpu_update_actions_state;
441   surf_cpu_model->model_private->update_resource_state =
442       cpu_update_resource_state;
443   surf_cpu_model->model_private->finalize = cpu_finalize;
444
445   surf_cpu_model->suspend = cpu_action_suspend;
446   surf_cpu_model->resume = cpu_action_resume;
447   surf_cpu_model->is_suspended = cpu_action_is_suspended;
448   surf_cpu_model->set_max_duration = cpu_action_set_max_duration;
449   surf_cpu_model->set_priority = cpu_action_set_priority;
450   surf_cpu_model->get_remains = cpu_action_get_remains;
451
452   surf_cpu_model->extension.cpu.execute = cpu_execute;
453   surf_cpu_model->extension.cpu.sleep = cpu_action_sleep;
454
455   surf_cpu_model->extension.cpu.get_state = cpu_get_state;
456   surf_cpu_model->extension.cpu.get_speed = cpu_get_speed;
457   surf_cpu_model->extension.cpu.get_available_speed =
458       cpu_get_available_speed;
459   surf_cpu_model->extension.cpu.create_resource = cpu_create_resource;
460   surf_cpu_model->extension.cpu.add_traces = add_traces_cpu;
461
462   if (!cpu_maxmin_system)
463     cpu_maxmin_system = lmm_system_new();
464 }
465
466 /*********************************************************************/
467 /* Basic sharing model for CPU: that is where all this started... ;) */
468 /*********************************************************************/
469 /* @InProceedings{casanova01simgrid, */
470 /*   author =       "H. Casanova", */
471 /*   booktitle =    "Proceedings of the IEEE Symposium on Cluster Computing */
472 /*                  and the Grid (CCGrid'01)", */
473 /*   publisher =    "IEEE Computer Society", */
474 /*   title =        "Simgrid: {A} Toolkit for the Simulation of Application */
475 /*                  Scheduling", */
476 /*   year =         "2001", */
477 /*   month =        may, */
478 /*   note =         "Available at */
479 /*                  \url{http://grail.sdsc.edu/papers/simgrid_ccgrid01.ps.gz}." */
480 /* } */
481 void surf_cpu_model_init_Cas01(const char *filename)
482 {
483   if (surf_cpu_model)
484     return;
485   surf_cpu_model_init_internal();
486   cpu_define_callbacks(filename);
487   xbt_dynar_push(model_list, &surf_cpu_model);
488 }