Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add MSG_host_on and MSG_host_off
[simgrid.git] / src / surf / cpu_cas01.c
1 /* Copyright (c) 2009-2011. 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 #include "surf/surf_resource.h"
9 #include "maxmin_private.h"
10 #include "simgrid/sg_config.h"
11 #include "surf/cpu_cas01_private.h"
12
13 /* the model objects for physical machines and virtual machines */
14 surf_model_t surf_cpu_model_pm = NULL;
15 surf_model_t surf_cpu_model_vm = NULL;
16
17 #undef GENERIC_LMM_ACTION
18 #undef GENERIC_ACTION
19 #undef ACTION_GET_CPU
20 #define GENERIC_LMM_ACTION(action) action->generic_lmm_action
21 #define GENERIC_ACTION(action) GENERIC_LMM_ACTION(action).generic_action
22 #define ACTION_GET_CPU(action) ((surf_action_cpu_Cas01_t) action)->cpu
23
24 typedef struct surf_action_cpu_cas01 {
25   s_surf_action_lmm_t generic_lmm_action;
26 } s_surf_action_cpu_Cas01_t, *surf_action_cpu_Cas01_t;
27
28 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_cpu, surf,
29                                 "Logging specific to the SURF CPU IMPROVED module");
30
31 static xbt_swag_t
32     cpu_running_action_set_that_does_not_need_being_checked = NULL;
33
34
35 void *cpu_cas01_create_resource(const char *name, double power_peak,
36                                  double power_scale,
37                                  tmgr_trace_t power_trace,
38                                  int core,
39                                  e_surf_resource_state_t state_initial,
40                                  tmgr_trace_t state_trace,
41                                  xbt_dict_t cpu_properties,
42                                  surf_model_t cpu_model)
43 {
44   cpu_Cas01_t cpu = NULL;
45
46   xbt_assert(!surf_cpu_resource_priv(surf_cpu_resource_by_name(name)),
47              "Host '%s' declared several times in the platform file",
48              name);
49   cpu = (cpu_Cas01_t) surf_resource_new(sizeof(s_cpu_Cas01_t),
50                                         cpu_model, name,
51                                         cpu_properties);
52   cpu->power_peak = power_peak;
53   xbt_assert(cpu->power_peak > 0, "Power has to be >0");
54   cpu->power_scale = power_scale;
55   cpu->core = core;
56   xbt_assert(core > 0, "Invalid number of cores %d", core);
57
58   if (power_trace)
59     cpu->power_event =
60         tmgr_history_add_trace(history, power_trace, 0.0, 0, cpu);
61
62   cpu->state_current = state_initial;
63   if (state_trace)
64     cpu->state_event =
65         tmgr_history_add_trace(history, state_trace, 0.0, 0, cpu);
66
67   cpu->constraint =
68       lmm_constraint_new(cpu_model->model_private->maxmin_system, cpu,
69                          cpu->core * cpu->power_scale * cpu->power_peak);
70
71   xbt_lib_set(host_lib, name, SURF_CPU_LEVEL, cpu);
72
73   return xbt_lib_get_elm_or_null(host_lib, name);;
74 }
75
76
77 static void parse_cpu_init(sg_platf_host_cbarg_t host)
78 {
79   /* This function is called when a platform file is parsed. Physical machines
80    * are defined there. Thus, we use the cpu model object for the physical
81    * machine layer. */
82   cpu_cas01_create_resource(host->id,
83                       host->power_peak,
84                       host->power_scale,
85                       host->power_trace,
86                       host->core_amount,
87                       host->initial_state,
88                       host->state_trace, host->properties,
89                       surf_cpu_model_pm);
90 }
91
92 static void cpu_add_traces_cpu(void)
93 {
94   xbt_dict_cursor_t cursor = NULL;
95   char *trace_name, *elm;
96   static int called = 0;
97   if (called)
98     return;
99   called = 1;
100
101   /* connect all traces relative to hosts */
102   xbt_dict_foreach(trace_connect_list_host_avail, cursor, trace_name, elm) {
103     tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
104     cpu_Cas01_t host = surf_cpu_resource_by_name(elm);
105
106     xbt_assert(host, "Host %s undefined", elm);
107     xbt_assert(trace, "Trace %s undefined", trace_name);
108
109     host->state_event =
110         tmgr_history_add_trace(history, trace, 0.0, 0, host);
111   }
112
113   xbt_dict_foreach(trace_connect_list_power, cursor, trace_name, elm) {
114     tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
115     cpu_Cas01_t host = surf_cpu_resource_by_name(elm);
116
117     xbt_assert(host, "Host %s undefined", elm);
118     xbt_assert(trace, "Trace %s undefined", trace_name);
119
120     host->power_event =
121         tmgr_history_add_trace(history, trace, 0.0, 0, host);
122   }
123 }
124
125 static void cpu_define_callbacks_cas01()
126 {
127   sg_platf_host_add_cb(parse_cpu_init);
128   sg_platf_postparse_add_cb(cpu_add_traces_cpu);
129 }
130
131 static int cpu_resource_used(void *resource)
132 {
133   surf_model_t cpu_model = ((surf_resource_t) resource)->model;
134
135   return lmm_constraint_used(cpu_model->model_private->maxmin_system,
136                              ((cpu_Cas01_t) resource)->constraint);
137 }
138
139 static double cpu_share_resources_lazy(surf_model_t cpu_model, double now)
140 {
141   return generic_share_resources_lazy(now, cpu_model);
142 }
143
144 static double cpu_share_resources_full(surf_model_t cpu_model, double now)
145 {
146   s_surf_action_cpu_Cas01_t action;
147   return generic_maxmin_share_resources(cpu_model->states.
148                                         running_action_set,
149                                         xbt_swag_offset(action,
150                                                         generic_lmm_action.
151                                                         variable),
152                                         cpu_model->model_private->maxmin_system, lmm_solve);
153 }
154
155 static void cpu_update_actions_state_lazy(surf_model_t cpu_model, double now, double delta)
156 {
157   generic_update_actions_state_lazy(now, delta, cpu_model);
158 }
159
160 static void cpu_update_actions_state_full(surf_model_t cpu_model, double now, double delta)
161 {
162   generic_update_actions_state_full(now, delta, cpu_model);
163 }
164
165 static void cpu_update_resource_state(void *id,
166                                       tmgr_trace_event_t event_type,
167                                       double value, double date)
168 {
169   cpu_Cas01_t cpu = id;
170   lmm_variable_t var = NULL;
171   lmm_element_t elem = NULL;
172   surf_model_t cpu_model = ((surf_resource_t) cpu)->model;
173
174   surf_watched_hosts();
175
176   if (event_type == cpu->power_event) {
177     cpu->power_scale = value;
178     lmm_update_constraint_bound(cpu_model->model_private->maxmin_system, cpu->constraint,
179                                 cpu->core * cpu->power_scale *
180                                 cpu->power_peak);
181 #ifdef HAVE_TRACING
182     TRACE_surf_host_set_power(date, cpu->generic_resource.name,
183                               cpu->core * cpu->power_scale *
184                               cpu->power_peak);
185 #endif
186     while ((var = lmm_get_var_from_cnst
187             (cpu_model->model_private->maxmin_system, cpu->constraint, &elem))) {
188       surf_action_cpu_Cas01_t action = lmm_variable_id(var);
189       lmm_update_variable_bound(cpu_model->model_private->maxmin_system,
190                                 GENERIC_LMM_ACTION(action).variable,
191                                 cpu->power_scale * cpu->power_peak);
192     }
193     if (tmgr_trace_event_free(event_type))
194       cpu->power_event = NULL;
195   } else if (event_type == cpu->state_event) {
196     if (value > 0)
197       cpu->state_current = SURF_RESOURCE_ON;
198     else {
199       lmm_constraint_t cnst = cpu->constraint;
200
201       cpu->state_current = SURF_RESOURCE_OFF;
202
203       while ((var = lmm_get_var_from_cnst(cpu_model->model_private->maxmin_system, cnst, &elem))) {
204         surf_action_t action = lmm_variable_id(var);
205
206         if (surf_action_state_get(action) == SURF_ACTION_RUNNING ||
207             surf_action_state_get(action) == SURF_ACTION_READY ||
208             surf_action_state_get(action) ==
209             SURF_ACTION_NOT_IN_THE_SYSTEM) {
210           action->finish = date;
211           surf_action_state_set(action, SURF_ACTION_FAILED);
212         }
213       }
214     }
215     if (tmgr_trace_event_free(event_type))
216       cpu->state_event = NULL;
217   } else {
218     XBT_CRITICAL("Unknown event ! \n");
219     xbt_abort();
220   }
221
222   return;
223 }
224
225 static surf_action_t cpu_execute(void *cpu, double size)
226 {
227   surf_action_cpu_Cas01_t action = NULL;
228   cpu_Cas01_t CPU = surf_cpu_resource_priv(cpu);
229   surf_model_t cpu_model = ((surf_resource_t) CPU)->model;
230
231   XBT_IN("(%s,%g)", surf_resource_name(CPU), size);
232   action =
233       surf_action_new(sizeof(s_surf_action_cpu_Cas01_t), size,
234                       cpu_model,
235                       CPU->state_current != SURF_RESOURCE_ON);
236
237   GENERIC_LMM_ACTION(action).suspended = 0;     /* Should be useless because of the
238                                                    calloc but it seems to help valgrind... */
239
240   GENERIC_LMM_ACTION(action).variable =
241       lmm_variable_new(cpu_model->model_private->maxmin_system, action,
242                        GENERIC_ACTION(action).priority,
243                        CPU->power_scale * CPU->power_peak, 1);
244   if (cpu_model->model_private->update_mechanism == UM_LAZY) {
245     GENERIC_LMM_ACTION(action).index_heap = -1;
246     GENERIC_LMM_ACTION(action).last_update = surf_get_clock();
247     GENERIC_LMM_ACTION(action).last_value = 0.0;
248   }
249   lmm_expand(cpu_model->model_private->maxmin_system, CPU->constraint,
250              GENERIC_LMM_ACTION(action).variable, 1.0);
251   XBT_OUT();
252   return (surf_action_t) action;
253 }
254
255 static surf_action_t cpu_action_sleep(void *cpu, double duration)
256 {
257   surf_action_cpu_Cas01_t action = NULL;
258   cpu_Cas01_t CPU = surf_cpu_resource_priv(cpu);
259   surf_model_t cpu_model = ((surf_resource_t) CPU)->model;
260
261   if (duration > 0)
262     duration = MAX(duration, MAXMIN_PRECISION);
263
264   XBT_IN("(%s,%g)", surf_resource_name(surf_cpu_resource_priv(cpu)), duration);
265   action = (surf_action_cpu_Cas01_t) cpu_execute(cpu, 1.0);
266   // FIXME: sleep variables should not consume 1.0 in lmm_expand
267   GENERIC_ACTION(action).max_duration = duration;
268   GENERIC_LMM_ACTION(action).suspended = 2;
269   if (duration == NO_MAX_DURATION) {
270     /* Move to the *end* of the corresponding action set. This convention
271        is used to speed up update_resource_state  */
272     xbt_swag_remove(action, ((surf_action_t) action)->state_set);
273     ((surf_action_t) action)->state_set =
274         cpu_running_action_set_that_does_not_need_being_checked;
275     xbt_swag_insert(action, ((surf_action_t) action)->state_set);
276   }
277
278   lmm_update_variable_weight(cpu_model->model_private->maxmin_system,
279                              GENERIC_LMM_ACTION(action).variable, 0.0);
280   if (cpu_model->model_private->update_mechanism == UM_LAZY) {     // remove action from the heap
281     surf_action_lmm_heap_remove(cpu_model->model_private->action_heap,(surf_action_lmm_t)action);
282     // this is necessary for a variable with weight 0 since such
283     // variables are ignored in lmm and we need to set its max_duration
284     // correctly at the next call to share_resources
285     xbt_swag_insert_at_head(action, cpu_model->model_private->modified_set);
286   }
287
288   XBT_OUT();
289   return (surf_action_t) action;
290 }
291
292 static e_surf_resource_state_t cpu_get_state(void *cpu)
293 {
294   return ((cpu_Cas01_t)surf_cpu_resource_priv(cpu))->state_current;
295 }
296
297 static void cpu_set_state(void *cpu, e_surf_resource_state_t state)
298 {
299   ((cpu_Cas01_t)surf_cpu_resource_priv(cpu))->state_current = state;
300 }
301
302 static double cpu_get_speed(void *cpu, double load)
303 {
304   return load * ((cpu_Cas01_t)surf_cpu_resource_priv(cpu))->power_peak;
305 }
306
307 static double cpu_get_available_speed(void *cpu)
308 {
309   /* number between 0 and 1 */
310   return ((cpu_Cas01_t)surf_cpu_resource_priv(cpu))->power_scale;
311 }
312
313 static void cpu_finalize(surf_model_t cpu_model)
314 {
315   lmm_system_free(cpu_model->model_private->maxmin_system);
316   cpu_model->model_private->maxmin_system = NULL;
317
318   if (cpu_model->model_private->action_heap)
319     xbt_heap_free(cpu_model->model_private->action_heap);
320   xbt_swag_free(cpu_model->model_private->modified_set);
321
322   surf_model_exit(cpu_model);
323   cpu_model = NULL;
324
325   xbt_swag_free(cpu_running_action_set_that_does_not_need_being_checked);
326   cpu_running_action_set_that_does_not_need_being_checked = NULL;
327 }
328
329 static surf_model_t surf_cpu_model_init_cas01(void)
330 {
331   s_surf_action_t action;
332   s_surf_action_cpu_Cas01_t comp;
333
334   char *optim = xbt_cfg_get_string(_sg_cfg_set, "cpu/optim");
335   int select =
336       xbt_cfg_get_int(_sg_cfg_set, "cpu/maxmin_selective_update");
337
338   surf_model_t cpu_model = surf_model_init();
339
340   if (!strcmp(optim, "Full")) {
341     cpu_model->model_private->update_mechanism = UM_FULL;
342     cpu_model->model_private->selective_update = select;
343   } else if (!strcmp(optim, "Lazy")) {
344     cpu_model->model_private->update_mechanism = UM_LAZY;
345     cpu_model->model_private->selective_update = 1;
346     xbt_assert((select == 1)
347                ||
348                (xbt_cfg_is_default_value
349                 (_sg_cfg_set, "cpu/maxmin_selective_update")),
350                "Disabling selective update while using the lazy update mechanism is dumb!");
351   } else {
352     xbt_die("Unsupported optimization (%s) for this model", optim);
353   }
354
355   cpu_running_action_set_that_does_not_need_being_checked =
356       xbt_swag_new(xbt_swag_offset(action, state_hookup));
357
358   cpu_model->name = "cpu";
359   cpu_model->type = SURF_MODEL_TYPE_CPU;
360
361   cpu_model->action_unref = surf_action_unref;
362   cpu_model->action_cancel = surf_action_cancel;
363   cpu_model->action_state_set = surf_action_state_set;
364
365   cpu_model->model_private->resource_used = cpu_resource_used;
366
367   if (cpu_model->model_private->update_mechanism == UM_LAZY) {
368     cpu_model->model_private->share_resources =
369         cpu_share_resources_lazy;
370     cpu_model->model_private->update_actions_state =
371         cpu_update_actions_state_lazy;
372   } else if (cpu_model->model_private->update_mechanism == UM_FULL) {
373     cpu_model->model_private->share_resources =
374         cpu_share_resources_full;
375     cpu_model->model_private->update_actions_state =
376         cpu_update_actions_state_full;
377   } else
378     xbt_die("Invalid cpu update mechanism!");
379
380   cpu_model->model_private->update_resource_state =
381       cpu_update_resource_state;
382   cpu_model->model_private->finalize = cpu_finalize;
383
384   cpu_model->suspend = surf_action_suspend;
385   cpu_model->resume = surf_action_resume;
386   cpu_model->is_suspended = surf_action_is_suspended;
387   cpu_model->set_max_duration = surf_action_set_max_duration;
388   cpu_model->set_priority = surf_action_set_priority;
389   cpu_model->set_bound = surf_action_set_bound;
390 #ifdef HAVE_TRACING
391   cpu_model->set_category = surf_action_set_category;
392 #endif
393   cpu_model->get_remains = surf_action_get_remains;
394
395   cpu_model->extension.cpu.execute = cpu_execute;
396   cpu_model->extension.cpu.sleep = cpu_action_sleep;
397
398   cpu_model->extension.cpu.get_state = cpu_get_state;
399   cpu_model->extension.cpu.set_state = cpu_set_state;
400   cpu_model->extension.cpu.get_speed = cpu_get_speed;
401   cpu_model->extension.cpu.get_available_speed =
402       cpu_get_available_speed;
403   cpu_model->extension.cpu.add_traces = cpu_add_traces_cpu;
404
405   if (!cpu_model->model_private->maxmin_system) {
406     cpu_model->model_private->maxmin_system = lmm_system_new(cpu_model->model_private->selective_update);
407   }
408   if (cpu_model->model_private->update_mechanism == UM_LAZY) {
409     cpu_model->model_private->action_heap = xbt_heap_new(8, NULL);
410     xbt_heap_set_update_callback(cpu_model->model_private->action_heap,
411         surf_action_lmm_update_index_heap);
412     cpu_model->model_private->modified_set =
413         xbt_swag_new(xbt_swag_offset(comp, generic_lmm_action.action_list_hookup));
414     cpu_model->model_private->maxmin_system->keep_track = cpu_model->model_private->modified_set;
415   }
416
417   return cpu_model;
418 }
419
420 /*********************************************************************/
421 /* Basic sharing model for CPU: that is where all this started... ;) */
422 /*********************************************************************/
423 /* @InProceedings{casanova01simgrid, */
424 /*   author =       "H. Casanova", */
425 /*   booktitle =    "Proceedings of the IEEE Symposium on Cluster Computing */
426 /*                  and the Grid (CCGrid'01)", */
427 /*   publisher =    "IEEE Computer Society", */
428 /*   title =        "Simgrid: {A} Toolkit for the Simulation of Application */
429 /*                  Scheduling", */
430 /*   year =         "2001", */
431 /*   month =        may, */
432 /*   note =         "Available at */
433 /*                  \url{http://grail.sdsc.edu/papers/simgrid_ccgrid01.ps.gz}." */
434 /* } */
435
436
437 void surf_cpu_model_init_Cas01(void)
438 {
439   char *optim = xbt_cfg_get_string(_sg_cfg_set, "cpu/optim");
440
441   xbt_assert(!surf_cpu_model_pm);
442   xbt_assert(!surf_cpu_model_vm);
443
444   if (strcmp(optim, "TI") == 0) {
445     /* FIXME: do we have to supprot TI? for VM */
446     surf_cpu_model_pm = surf_cpu_model_init_ti();
447     XBT_INFO("TI model is used (it will crashed since this is the hypervisor branch)");
448   } else {
449     surf_cpu_model_pm  = surf_cpu_model_init_cas01();
450     surf_cpu_model_vm  = surf_cpu_model_init_cas01();
451
452     /* cpu_model is registered only to model_list, and not to
453      * model_list_invoke. The shared_resource callback function will be called
454      * from that of the workstation model. */
455     xbt_dynar_push(model_list, &surf_cpu_model_pm);
456     xbt_dynar_push(model_list, &surf_cpu_model_vm);
457
458     cpu_define_callbacks_cas01();
459   }
460 }
461
462 /* TODO: do we address nested virtualization later? */
463 #if 0
464 surf_model_t cpu_model_cas01(int level){
465         // TODO this table should be allocated
466         if(!surf_cpu_model[level])
467          // allocate it
468         return surf_cpu_model[level];
469 }
470 #endif