Logo AND Algorithmique Numérique Distribuée

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