Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Further parser cleanups
[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   int core;
17   tmgr_trace_event_t power_event;
18   e_surf_resource_state_t state_current;
19   tmgr_trace_event_t state_event;
20   lmm_constraint_t constraint;
21 } s_cpu_Cas01_t, *cpu_Cas01_t;
22
23 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_cpu, surf,
24                                 "Logging specific to the SURF CPU module");
25
26
27
28 surf_model_t surf_cpu_model = NULL;
29 lmm_system_t cpu_maxmin_system = NULL;
30
31
32 static xbt_swag_t cpu_running_action_set_that_does_not_need_being_checked =
33     NULL;
34
35 static void* cpu_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 {
43
44   cpu_Cas01_t cpu = NULL;
45   xbt_assert(!surf_cpu_resource_by_name(name),
46               "Host '%s' declared several times in the platform file",
47               name);
48   cpu = (cpu_Cas01_t) surf_resource_new(sizeof(s_cpu_Cas01_t),
49           surf_cpu_model, name,cpu_properties);
50   cpu->power_peak = power_peak;
51   xbt_assert(cpu->power_peak > 0, "Power has to be >0");
52   cpu->power_scale = power_scale;
53   cpu->core = core;
54   xbt_assert(core>0,"Invalid number of cores %d",core);
55   if (power_trace)
56     cpu->power_event =
57         tmgr_history_add_trace(history, power_trace, 0.0, 0, cpu);
58
59   cpu->state_current = state_initial;
60   if (state_trace)
61     cpu->state_event =
62         tmgr_history_add_trace(history, state_trace, 0.0, 0, cpu);
63
64   cpu->constraint =
65       lmm_constraint_new(cpu_maxmin_system, cpu,
66                          cpu->core * cpu->power_scale * cpu->power_peak);
67
68   xbt_lib_set(host_lib, name, SURF_CPU_LEVEL, cpu);
69
70   return cpu;
71 }
72
73
74 static void parse_cpu_init(sg_platf_host_cbarg_t host)
75 {
76   if(strcmp(host->V_host_coord,"")) xbt_die("Coordinates not implemented yet!");
77
78   cpu_create_resource(host->V_host_id,
79                   host->V_host_power_peak,
80                   host->V_host_power_scale,
81                   host->V_host_power_trace,
82                   host->V_host_core,
83                   host->V_host_state_initial,
84                   host->V_host_state_trace,
85                   current_property_set);
86   current_property_set = NULL;
87 }
88
89 static void add_traces_cpu(void)
90 {
91   xbt_dict_cursor_t cursor = NULL;
92   char *trace_name, *elm;
93
94   static int called = 0;
95
96   if (called)
97     return;
98   called = 1;
99
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(void)
126 {
127   sg_platf_host_add_cb(parse_cpu_init);
128   surfxml_add_callback(ETag_surfxml_platform_cb_list, &add_traces_cpu);
129 }
130
131 static int cpu_resource_used(void *resource_id)
132 {
133   return lmm_constraint_used(cpu_maxmin_system,
134                              ((cpu_Cas01_t) resource_id)->constraint);
135 }
136
137 static int cpu_action_unref(surf_action_t action)
138 {
139   action->refcount--;
140   if (!action->refcount) {
141     xbt_swag_remove(action, action->state_set);
142     if (((surf_action_cpu_Cas01_t) action)->variable)
143       lmm_variable_free(cpu_maxmin_system,
144                         ((surf_action_cpu_Cas01_t) action)->variable);
145 #ifdef HAVE_TRACING
146     if (action->category)
147       xbt_free(action->category);
148 #endif
149     surf_action_free(&action);
150     return 1;
151   }
152   return 0;
153 }
154
155 static void cpu_action_cancel(surf_action_t action)
156 {
157   surf_action_state_set(action, SURF_ACTION_FAILED);
158   return;
159 }
160
161 static void cpu_action_state_set(surf_action_t action,
162                                  e_surf_action_state_t state)
163 {
164 /*   if((state==SURF_ACTION_DONE) || (state==SURF_ACTION_FAILED)) */
165 /*     if(((surf_action_cpu_Cas01_t)action)->variable) { */
166 /*       lmm_variable_disable(cpu_maxmin_system, ((surf_action_cpu_Cas01_t)action)->variable); */
167 /*       ((surf_action_cpu_Cas01_t)action)->variable = NULL; */
168 /*     } */
169
170   surf_action_state_set(action, state);
171   return;
172 }
173
174 static double cpu_share_resources(double now)
175 {
176   s_surf_action_cpu_Cas01_t action;
177   return generic_maxmin_share_resources(surf_cpu_model->
178                                         states.running_action_set,
179                                         xbt_swag_offset(action, variable),
180                                         cpu_maxmin_system, lmm_solve);
181 }
182
183 static void cpu_update_actions_state(double now, double delta)
184 {
185   surf_action_cpu_Cas01_t action = NULL;
186   surf_action_cpu_Cas01_t next_action = NULL;
187   xbt_swag_t running_actions = surf_cpu_model->states.running_action_set;
188
189   xbt_swag_foreach_safe(action, next_action, running_actions) {
190 #ifdef HAVE_TRACING
191     if (TRACE_is_enabled()) {
192       cpu_Cas01_t x =
193         lmm_constraint_id(lmm_get_cnst_from_var
194                           (cpu_maxmin_system, action->variable, 0));
195
196       TRACE_surf_host_set_utilization(x->generic_resource.name,
197                                       action->generic_action.data,
198                                       (surf_action_t) action,
199                                       lmm_variable_getvalue
200                                       (action->variable), now - delta,
201                                       delta);
202       TRACE_last_timestamp_to_dump = now-delta;
203     }
204 #endif
205     double_update(&(action->generic_action.remains),
206                   lmm_variable_getvalue(action->variable) * delta);
207     if (action->generic_action.max_duration != NO_MAX_DURATION)
208       double_update(&(action->generic_action.max_duration), delta);
209     if ((action->generic_action.remains <= 0) &&
210         (lmm_get_variable_weight(action->variable) > 0)) {
211       action->generic_action.finish = surf_get_clock();
212       cpu_action_state_set((surf_action_t) action, SURF_ACTION_DONE);
213     } else if ((action->generic_action.max_duration != NO_MAX_DURATION) &&
214                (action->generic_action.max_duration <= 0)) {
215       action->generic_action.finish = surf_get_clock();
216       cpu_action_state_set((surf_action_t) action, SURF_ACTION_DONE);
217     }
218   }
219
220   return;
221 }
222
223 static void cpu_update_resource_state(void *id,
224                                       tmgr_trace_event_t event_type,
225                                       double value, double date)
226 {
227   cpu_Cas01_t cpu = id;
228   lmm_variable_t var = NULL;
229   lmm_element_t elem = NULL;
230
231   if (event_type == cpu->power_event) {
232     cpu->power_scale = value;
233     lmm_update_constraint_bound(cpu_maxmin_system, cpu->constraint,
234                                 cpu->core * cpu->power_scale * cpu->power_peak);
235 #ifdef HAVE_TRACING
236     TRACE_surf_host_set_power(date, cpu->generic_resource.name,
237                               cpu->core * cpu->power_scale * cpu->power_peak);
238 #endif
239     while ((var = lmm_get_var_from_cnst
240             (cpu_maxmin_system, cpu->constraint, &elem))) {
241         surf_action_cpu_Cas01_t action = lmm_variable_id(var);
242         lmm_update_variable_bound(cpu_maxmin_system, action->variable,
243                                   cpu->power_scale * cpu->power_peak);
244     }
245     if (tmgr_trace_event_free(event_type))
246       cpu->power_event = NULL;
247   } else if (event_type == cpu->state_event) {
248     if (value > 0)
249       cpu->state_current = SURF_RESOURCE_ON;
250     else {
251       lmm_constraint_t cnst = cpu->constraint;
252       lmm_variable_t var = NULL;
253       lmm_element_t elem = NULL;
254
255       cpu->state_current = SURF_RESOURCE_OFF;
256
257       while ((var = lmm_get_var_from_cnst(cpu_maxmin_system, cnst, &elem))) {
258         surf_action_t action = lmm_variable_id(var);
259
260         if (surf_action_state_get(action) == SURF_ACTION_RUNNING ||
261             surf_action_state_get(action) == SURF_ACTION_READY ||
262             surf_action_state_get(action) ==
263             SURF_ACTION_NOT_IN_THE_SYSTEM) {
264           action->finish = date;
265           cpu_action_state_set(action, SURF_ACTION_FAILED);
266         }
267       }
268     }
269     if (tmgr_trace_event_free(event_type))
270       cpu->state_event = NULL;
271   } else {
272     XBT_CRITICAL("Unknown event ! \n");
273     xbt_abort();
274   }
275
276   return;
277 }
278
279 static surf_action_t cpu_execute(void *cpu, double size)
280 {
281   surf_action_cpu_Cas01_t action = NULL;
282   cpu_Cas01_t CPU = cpu;
283
284   XBT_IN("(%s,%g)", surf_resource_name(CPU), size);
285   action =
286       surf_action_new(sizeof(s_surf_action_cpu_Cas01_t), size,
287                       surf_cpu_model,
288                       CPU->state_current != SURF_RESOURCE_ON);
289
290   action->suspended = 0;        /* Should be useless because of the
291                                    calloc but it seems to help valgrind... */
292
293   action->variable = lmm_variable_new(cpu_maxmin_system, action,
294                                       action->generic_action.priority,
295                                       CPU->power_scale * CPU->power_peak, 1);
296   lmm_expand(cpu_maxmin_system, CPU->constraint, action->variable, 1.0);
297   XBT_OUT();
298   return (surf_action_t) action;
299 }
300
301 static surf_action_t cpu_action_sleep(void *cpu, double duration)
302 {
303   surf_action_cpu_Cas01_t action = NULL;
304
305   if (duration > 0)
306     duration = MAX(duration, MAXMIN_PRECISION);
307
308   XBT_IN("(%s,%g)", surf_resource_name(cpu), duration);
309   action = (surf_action_cpu_Cas01_t) cpu_execute(cpu, 1.0);
310   action->generic_action.max_duration = duration;
311   action->suspended = 2;
312   if (duration == NO_MAX_DURATION) {
313     /* Move to the *end* of the corresponding action set. This convention
314        is used to speed up update_resource_state  */
315     xbt_swag_remove(action, ((surf_action_t) action)->state_set);
316     ((surf_action_t) action)->state_set =
317         cpu_running_action_set_that_does_not_need_being_checked;
318     xbt_swag_insert(action, ((surf_action_t) action)->state_set);
319   }
320
321   lmm_update_variable_weight(cpu_maxmin_system, action->variable, 0.0);
322   XBT_OUT();
323   return (surf_action_t) action;
324 }
325
326 static void cpu_action_suspend(surf_action_t action)
327 {
328   XBT_IN("(%p)", action);
329   if (((surf_action_cpu_Cas01_t) action)->suspended != 2) {
330     lmm_update_variable_weight(cpu_maxmin_system,
331                                ((surf_action_cpu_Cas01_t)
332                                 action)->variable, 0.0);
333     ((surf_action_cpu_Cas01_t) action)->suspended = 1;
334   }
335   XBT_OUT();
336 }
337
338 static void cpu_action_resume(surf_action_t action)
339 {
340   XBT_IN("(%p)", action);
341   if (((surf_action_cpu_Cas01_t) action)->suspended != 2) {
342     lmm_update_variable_weight(cpu_maxmin_system,
343                                ((surf_action_cpu_Cas01_t)
344                                 action)->variable, action->priority);
345     ((surf_action_cpu_Cas01_t) action)->suspended = 0;
346   }
347   XBT_OUT();
348 }
349
350 static int cpu_action_is_suspended(surf_action_t action)
351 {
352   return (((surf_action_cpu_Cas01_t) action)->suspended == 1);
353 }
354
355 static void cpu_action_set_max_duration(surf_action_t action,
356                                         double duration)
357 {
358   XBT_IN("(%p,%g)", action, duration);
359   action->max_duration = duration;
360   XBT_OUT();
361 }
362
363 static void cpu_action_set_priority(surf_action_t action, double priority)
364 {
365   XBT_IN("(%p,%g)", action, priority);
366   action->priority = priority;
367   lmm_update_variable_weight(cpu_maxmin_system,
368                              ((surf_action_cpu_Cas01_t) action)->variable,
369                              priority);
370
371   XBT_OUT();
372 }
373
374 #ifdef HAVE_TRACING
375 static void cpu_action_set_category(surf_action_t action, const char *category)
376 {
377   XBT_IN("(%p,%s)", action, category);
378   action->category = xbt_strdup (category);
379   XBT_OUT();
380 }
381 #endif
382
383 static double cpu_action_get_remains(surf_action_t action)
384 {
385   XBT_IN("(%p)", action);
386   return action->remains;
387   XBT_OUT();
388 }
389
390 static e_surf_resource_state_t cpu_get_state(void *cpu)
391 {
392   return ((cpu_Cas01_t) cpu)->state_current;
393 }
394
395 static double cpu_get_speed(void *cpu, double load)
396 {
397   return load * (((cpu_Cas01_t) cpu)->power_peak);
398 }
399
400 static double cpu_get_available_speed(void *cpu)
401 {
402   /* number between 0 and 1 */
403   return ((cpu_Cas01_t) cpu)->power_scale;
404 }
405
406
407 static void cpu_finalize(void)
408 {
409   lmm_system_free(cpu_maxmin_system);
410   cpu_maxmin_system = NULL;
411
412   surf_model_exit(surf_cpu_model);
413   surf_cpu_model = NULL;
414
415   xbt_swag_free(cpu_running_action_set_that_does_not_need_being_checked);
416   cpu_running_action_set_that_does_not_need_being_checked = NULL;
417 }
418
419 static void surf_cpu_model_init_internal(void)
420 {
421   s_surf_action_t action;
422
423   surf_cpu_model = surf_model_init();
424
425   cpu_running_action_set_that_does_not_need_being_checked =
426       xbt_swag_new(xbt_swag_offset(action, state_hookup));
427
428   surf_cpu_model->name = "CPU";
429
430   surf_cpu_model->action_unref = cpu_action_unref;
431   surf_cpu_model->action_cancel = cpu_action_cancel;
432   surf_cpu_model->action_state_set = cpu_action_state_set;
433
434   surf_cpu_model->model_private->resource_used = cpu_resource_used;
435   surf_cpu_model->model_private->share_resources = cpu_share_resources;
436   surf_cpu_model->model_private->update_actions_state =
437       cpu_update_actions_state;
438   surf_cpu_model->model_private->update_resource_state =
439       cpu_update_resource_state;
440   surf_cpu_model->model_private->finalize = cpu_finalize;
441
442   surf_cpu_model->suspend = cpu_action_suspend;
443   surf_cpu_model->resume = cpu_action_resume;
444   surf_cpu_model->is_suspended = cpu_action_is_suspended;
445   surf_cpu_model->set_max_duration = cpu_action_set_max_duration;
446   surf_cpu_model->set_priority = cpu_action_set_priority;
447 #ifdef HAVE_TRACING
448   surf_cpu_model->set_category = cpu_action_set_category;
449 #endif
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()
482 {
483   if (surf_cpu_model)
484     return;
485   surf_cpu_model_init_internal();
486   cpu_define_callbacks();
487   xbt_dynar_push(model_list, &surf_cpu_model);
488 }