Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
remove the create_resource entry point in resource model APIs
[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 /* 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 {
55   cpu_Cas01_t cpu = NULL;
56
57   xbt_assert(!surf_cpu_resource_by_name(name),
58              "Host '%s' declared several times in the platform file",
59              name);
60   cpu = (cpu_Cas01_t) surf_resource_new(sizeof(s_cpu_Cas01_t),
61                                         surf_cpu_model, name,
62                                         cpu_properties);
63   cpu->power_peak = power_peak;
64   xbt_assert(cpu->power_peak > 0, "Power has to be >0");
65   cpu->power_scale = power_scale;
66   cpu->core = core;
67   xbt_assert(core > 0, "Invalid number of cores %d", core);
68
69   if (power_trace)
70     cpu->power_event =
71         tmgr_history_add_trace(history, power_trace, 0.0, 0, cpu);
72
73   cpu->state_current = state_initial;
74   if (state_trace)
75     cpu->state_event =
76         tmgr_history_add_trace(history, state_trace, 0.0, 0, cpu);
77
78   cpu->constraint =
79       lmm_constraint_new(surf_cpu_model->model_private->maxmin_system, cpu,
80                          cpu->core * cpu->power_scale * cpu->power_peak);
81
82   xbt_lib_set(host_lib, name, SURF_CPU_LEVEL, cpu);
83
84   return cpu;
85 }
86
87
88 static void parse_cpu_init(sg_platf_host_cbarg_t host)
89 {
90   cpu_create_resource(host->id,
91                       host->power_peak,
92                       host->power_scale,
93                       host->power_trace,
94                       host->core_amount,
95                       host->initial_state,
96                       host->state_trace, host->properties);
97 }
98
99 static void cpu_add_traces_cpu(void)
100 {
101   xbt_dict_cursor_t cursor = NULL;
102   char *trace_name, *elm;
103   static int called = 0;
104   if (called)
105     return;
106   called = 1;
107
108   /* connect all traces relative to hosts */
109   xbt_dict_foreach(trace_connect_list_host_avail, cursor, trace_name, elm) {
110     tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
111     cpu_Cas01_t host = surf_cpu_resource_by_name(elm);
112
113     xbt_assert(host, "Host %s undefined", elm);
114     xbt_assert(trace, "Trace %s undefined", trace_name);
115
116     host->state_event =
117         tmgr_history_add_trace(history, trace, 0.0, 0, host);
118   }
119
120   xbt_dict_foreach(trace_connect_list_power, cursor, trace_name, elm) {
121     tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
122     cpu_Cas01_t host = surf_cpu_resource_by_name(elm);
123
124     xbt_assert(host, "Host %s undefined", elm);
125     xbt_assert(trace, "Trace %s undefined", trace_name);
126
127     host->power_event =
128         tmgr_history_add_trace(history, trace, 0.0, 0, host);
129   }
130 }
131
132 static void cpu_define_callbacks()
133 {
134   sg_platf_host_add_cb(parse_cpu_init);
135   sg_platf_postparse_add_cb(cpu_add_traces_cpu);
136 }
137
138 static int cpu_resource_used(void *resource)
139 {
140   return lmm_constraint_used(surf_cpu_model->model_private->maxmin_system,
141                              ((cpu_Cas01_t) resource)->constraint);
142 }
143
144 static double cpu_share_resources_lazy(double now)
145 {
146   return generic_share_resources_lazy(now, surf_cpu_model);
147 }
148
149 static double cpu_share_resources_full(double now)
150 {
151   s_surf_action_cpu_Cas01_t action;
152   return generic_maxmin_share_resources(surf_cpu_model->states.
153                                         running_action_set,
154                                         xbt_swag_offset(action,
155                                                         generic_lmm_action.
156                                                         variable),
157                                         surf_cpu_model->model_private->maxmin_system, lmm_solve);
158 }
159
160 static void cpu_update_actions_state_lazy(double now, double delta)
161 {
162   generic_update_actions_state_lazy(now, delta, surf_cpu_model);
163 }
164
165 static void cpu_update_actions_state_full(double now, double delta)
166 {
167   generic_update_actions_state_full(now, delta, surf_cpu_model);
168 }
169
170 static void cpu_update_resource_state(void *id,
171                                       tmgr_trace_event_t event_type,
172                                       double value, double date)
173 {
174   cpu_Cas01_t cpu = id;
175   lmm_variable_t var = NULL;
176   lmm_element_t elem = NULL;
177
178   surf_watched_hosts();
179
180   if (event_type == cpu->power_event) {
181     cpu->power_scale = value;
182     lmm_update_constraint_bound(surf_cpu_model->model_private->maxmin_system, cpu->constraint,
183                                 cpu->core * cpu->power_scale *
184                                 cpu->power_peak);
185 #ifdef HAVE_TRACING
186     TRACE_surf_host_set_power(date, cpu->generic_resource.name,
187                               cpu->core * cpu->power_scale *
188                               cpu->power_peak);
189 #endif
190     while ((var = lmm_get_var_from_cnst
191             (surf_cpu_model->model_private->maxmin_system, cpu->constraint, &elem))) {
192       surf_action_cpu_Cas01_t action = lmm_variable_id(var);
193       lmm_update_variable_bound(surf_cpu_model->model_private->maxmin_system,
194                                 GENERIC_LMM_ACTION(action).variable,
195                                 cpu->power_scale * cpu->power_peak);
196     }
197     if (tmgr_trace_event_free(event_type))
198       cpu->power_event = NULL;
199   } else if (event_type == cpu->state_event) {
200     if (value > 0)
201       cpu->state_current = SURF_RESOURCE_ON;
202     else {
203       lmm_constraint_t cnst = cpu->constraint;
204
205       cpu->state_current = SURF_RESOURCE_OFF;
206
207       while ((var = lmm_get_var_from_cnst(surf_cpu_model->model_private->maxmin_system, cnst, &elem))) {
208         surf_action_t action = lmm_variable_id(var);
209
210         if (surf_action_state_get(action) == SURF_ACTION_RUNNING ||
211             surf_action_state_get(action) == SURF_ACTION_READY ||
212             surf_action_state_get(action) ==
213             SURF_ACTION_NOT_IN_THE_SYSTEM) {
214           action->finish = date;
215           surf_action_state_set(action, SURF_ACTION_FAILED);
216         }
217       }
218     }
219     if (tmgr_trace_event_free(event_type))
220       cpu->state_event = NULL;
221   } else {
222     XBT_CRITICAL("Unknown event ! \n");
223     xbt_abort();
224   }
225
226   return;
227 }
228
229 static surf_action_t cpu_execute(void *cpu, double size)
230 {
231   surf_action_cpu_Cas01_t action = NULL;
232   cpu_Cas01_t CPU = cpu;
233
234   XBT_IN("(%s,%g)", surf_resource_name(CPU), size);
235   action =
236       surf_action_new(sizeof(s_surf_action_cpu_Cas01_t), size,
237                       surf_cpu_model,
238                       CPU->state_current != SURF_RESOURCE_ON);
239
240   GENERIC_LMM_ACTION(action).suspended = 0;     /* Should be useless because of the
241                                                    calloc but it seems to help valgrind... */
242
243   GENERIC_LMM_ACTION(action).variable =
244       lmm_variable_new(surf_cpu_model->model_private->maxmin_system, action,
245                        GENERIC_ACTION(action).priority,
246                        CPU->power_scale * CPU->power_peak, 1);
247   if (surf_cpu_model->model_private->update_mechanism == UM_LAZY) {
248     GENERIC_LMM_ACTION(action).index_heap = -1;
249     GENERIC_LMM_ACTION(action).last_update = surf_get_clock();
250     GENERIC_LMM_ACTION(action).last_value = 0.0;
251   }
252   lmm_expand(surf_cpu_model->model_private->maxmin_system, CPU->constraint,
253              GENERIC_LMM_ACTION(action).variable, 1.0);
254   XBT_OUT();
255   return (surf_action_t) action;
256 }
257
258 static surf_action_t cpu_action_sleep(void *cpu, double duration)
259 {
260   surf_action_cpu_Cas01_t action = NULL;
261
262   if (duration > 0)
263     duration = MAX(duration, MAXMIN_PRECISION);
264
265   XBT_IN("(%s,%g)", surf_resource_name(cpu), duration);
266   action = (surf_action_cpu_Cas01_t) cpu_execute(cpu, 1.0);
267   // FIXME: sleep variables should not consume 1.0 in lmm_expand
268   GENERIC_ACTION(action).max_duration = duration;
269   GENERIC_LMM_ACTION(action).suspended = 2;
270   if (duration == NO_MAX_DURATION) {
271     /* Move to the *end* of the corresponding action set. This convention
272        is used to speed up update_resource_state  */
273     xbt_swag_remove(action, ((surf_action_t) action)->state_set);
274     ((surf_action_t) action)->state_set =
275         cpu_running_action_set_that_does_not_need_being_checked;
276     xbt_swag_insert(action, ((surf_action_t) action)->state_set);
277   }
278
279   lmm_update_variable_weight(surf_cpu_model->model_private->maxmin_system,
280                              GENERIC_LMM_ACTION(action).variable, 0.0);
281   if (surf_cpu_model->model_private->update_mechanism == UM_LAZY) {     // remove action from the heap
282     surf_action_lmm_heap_remove(surf_cpu_model->model_private->action_heap,(surf_action_lmm_t)action);
283     // this is necessary for a variable with weight 0 since such
284     // variables are ignored in lmm and we need to set its max_duration
285     // correctly at the next call to share_resources
286     xbt_swag_insert_at_head(action,surf_cpu_model->model_private->modified_set);
287   }
288
289   XBT_OUT();
290   return (surf_action_t) action;
291 }
292
293 static e_surf_resource_state_t cpu_get_state(void *cpu)
294 {
295   return ((cpu_Cas01_t) cpu)->state_current;
296 }
297
298 static double cpu_get_speed(void *cpu, double load)
299 {
300   return load * (((cpu_Cas01_t) cpu)->power_peak);
301 }
302
303 static double cpu_get_available_speed(void *cpu)
304 {
305   /* number between 0 and 1 */
306   return ((cpu_Cas01_t) cpu)->power_scale;
307 }
308
309 static void cpu_finalize(void)
310 {
311   lmm_system_free(surf_cpu_model->model_private->maxmin_system);
312   surf_cpu_model->model_private->maxmin_system = NULL;
313
314   if (surf_cpu_model->model_private->action_heap)
315     xbt_heap_free(surf_cpu_model->model_private->action_heap);
316   xbt_swag_free(surf_cpu_model->model_private->modified_set);
317
318   surf_model_exit(surf_cpu_model);
319   surf_cpu_model = NULL;
320
321   xbt_swag_free(cpu_running_action_set_that_does_not_need_being_checked);
322   cpu_running_action_set_that_does_not_need_being_checked = NULL;
323 }
324
325 static void surf_cpu_model_init_internal()
326 {
327   s_surf_action_t action;
328   s_surf_action_cpu_Cas01_t comp;
329
330   char *optim = xbt_cfg_get_string(_sg_cfg_set, "cpu/optim");
331   int select =
332       xbt_cfg_get_int(_sg_cfg_set, "cpu/maxmin_selective_update");
333
334   surf_cpu_model = surf_model_init();
335
336   if (!strcmp(optim, "Full")) {
337     surf_cpu_model->model_private->update_mechanism = UM_FULL;
338     surf_cpu_model->model_private->selective_update = select;
339   } else if (!strcmp(optim, "Lazy")) {
340     surf_cpu_model->model_private->update_mechanism = UM_LAZY;
341     surf_cpu_model->model_private->selective_update = 1;
342     xbt_assert((select == 1)
343                ||
344                (xbt_cfg_is_default_value
345                 (_sg_cfg_set, "cpu/maxmin_selective_update")),
346                "Disabling selective update while using the lazy update mechanism is dumb!");
347   } else {
348     xbt_die("Unsupported optimization (%s) for this model", optim);
349   }
350
351   cpu_running_action_set_that_does_not_need_being_checked =
352       xbt_swag_new(xbt_swag_offset(action, state_hookup));
353
354   surf_cpu_model->name = "cpu";
355
356   surf_cpu_model->action_unref = surf_action_unref;
357   surf_cpu_model->action_cancel = surf_action_cancel;
358   surf_cpu_model->action_state_set = surf_action_state_set;
359
360   surf_cpu_model->model_private->resource_used = cpu_resource_used;
361
362   if (surf_cpu_model->model_private->update_mechanism == UM_LAZY) {
363     surf_cpu_model->model_private->share_resources =
364         cpu_share_resources_lazy;
365     surf_cpu_model->model_private->update_actions_state =
366         cpu_update_actions_state_lazy;
367   } else if (surf_cpu_model->model_private->update_mechanism == UM_FULL) {
368     surf_cpu_model->model_private->share_resources =
369         cpu_share_resources_full;
370     surf_cpu_model->model_private->update_actions_state =
371         cpu_update_actions_state_full;
372   } else
373     xbt_die("Invalid cpu update mechanism!");
374
375   surf_cpu_model->model_private->update_resource_state =
376       cpu_update_resource_state;
377   surf_cpu_model->model_private->finalize = cpu_finalize;
378
379   surf_cpu_model->suspend = surf_action_suspend;
380   surf_cpu_model->resume = surf_action_resume;
381   surf_cpu_model->is_suspended = surf_action_is_suspended;
382   surf_cpu_model->set_max_duration = surf_action_set_max_duration;
383   surf_cpu_model->set_priority = surf_action_set_priority;
384 #ifdef HAVE_TRACING
385   surf_cpu_model->set_category = surf_action_set_category;
386 #endif
387   surf_cpu_model->get_remains = surf_action_get_remains;
388
389   surf_cpu_model->extension.cpu.execute = cpu_execute;
390   surf_cpu_model->extension.cpu.sleep = cpu_action_sleep;
391
392   surf_cpu_model->extension.cpu.get_state = cpu_get_state;
393   surf_cpu_model->extension.cpu.get_speed = cpu_get_speed;
394   surf_cpu_model->extension.cpu.get_available_speed =
395       cpu_get_available_speed;
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 }