Logo AND Algorithmique Numérique Distribuée

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