Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[trace] cosmetics, remove TRACE_is_active, use TRACE_is_enabled instead
[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 cpu_Cas01_t cpu_new(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   cpu_Cas01_t cpu = xbt_new0(s_cpu_Cas01_t, 1);
44   xbt_assert(!surf_cpu_resource_by_name(name),
45               "Host '%s' declared several times in the platform file",
46               name);
47   cpu->generic_resource.model = surf_cpu_model;
48   cpu->generic_resource.name = name;
49   cpu->generic_resource.properties = 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(void)
75 {
76   double power_peak = 0.0;
77   double power_scale = 0.0;
78   int core = 0;
79   tmgr_trace_t power_trace = NULL;
80   e_surf_resource_state_t state_initial = SURF_RESOURCE_OFF;
81   tmgr_trace_t state_trace = NULL;
82
83   power_peak = get_cpu_power(A_surfxml_host_power);
84   surf_parse_get_double(&power_scale, A_surfxml_host_availability);
85   power_trace = tmgr_trace_new(A_surfxml_host_availability_file);
86   surf_parse_get_int(&core, A_surfxml_host_core);
87
88   xbt_assert((A_surfxml_host_state == A_surfxml_host_state_ON) ||
89               (A_surfxml_host_state == A_surfxml_host_state_OFF),
90               "Invalid state");
91   if (A_surfxml_host_state == A_surfxml_host_state_ON)
92     state_initial = SURF_RESOURCE_ON;
93   if (A_surfxml_host_state == A_surfxml_host_state_OFF)
94     state_initial = SURF_RESOURCE_OFF;
95   state_trace = tmgr_trace_new(A_surfxml_host_state_file);
96
97   cpu_new(xbt_strdup(A_surfxml_host_id), power_peak, power_scale,
98           power_trace, core, state_initial, state_trace, current_property_set);
99   current_property_set = NULL;
100 }
101
102 static void add_traces_cpu(void)
103 {
104   xbt_dict_cursor_t cursor = NULL;
105   char *trace_name, *elm;
106
107   static int called = 0;
108
109   if (called)
110     return;
111   called = 1;
112
113
114   /* connect all traces relative to hosts */
115   xbt_dict_foreach(trace_connect_list_host_avail, cursor, trace_name, elm) {
116     tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
117     cpu_Cas01_t host = surf_cpu_resource_by_name(elm);
118
119     xbt_assert(host, "Host %s undefined", elm);
120     xbt_assert(trace, "Trace %s undefined", trace_name);
121
122     host->state_event =
123         tmgr_history_add_trace(history, trace, 0.0, 0, host);
124   }
125
126   xbt_dict_foreach(trace_connect_list_power, cursor, trace_name, elm) {
127     tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
128     cpu_Cas01_t host = surf_cpu_resource_by_name(elm);
129
130     xbt_assert(host, "Host %s undefined", elm);
131     xbt_assert(trace, "Trace %s undefined", trace_name);
132
133     host->power_event =
134         tmgr_history_add_trace(history, trace, 0.0, 0, host);
135   }
136 }
137
138 static void cpu_define_callbacks(const char *file)
139 {
140   surfxml_add_callback(ETag_surfxml_host_cb_list, parse_cpu_init);
141   surfxml_add_callback(ETag_surfxml_platform_cb_list, &add_traces_cpu);
142 }
143
144 static int cpu_resource_used(void *resource_id)
145 {
146   return lmm_constraint_used(cpu_maxmin_system,
147                              ((cpu_Cas01_t) resource_id)->constraint);
148 }
149
150 static int cpu_action_unref(surf_action_t action)
151 {
152   action->refcount--;
153   if (!action->refcount) {
154     xbt_swag_remove(action, action->state_set);
155     if (((surf_action_cpu_Cas01_t) action)->variable)
156       lmm_variable_free(cpu_maxmin_system,
157                         ((surf_action_cpu_Cas01_t) action)->variable);
158 #ifdef HAVE_TRACING
159     if (action->category)
160       xbt_free(action->category);
161 #endif
162     surf_action_free(&action);
163     return 1;
164   }
165   return 0;
166 }
167
168 static void cpu_action_cancel(surf_action_t action)
169 {
170   surf_action_state_set(action, SURF_ACTION_FAILED);
171   return;
172 }
173
174 static void cpu_action_state_set(surf_action_t action,
175                                  e_surf_action_state_t state)
176 {
177 /*   if((state==SURF_ACTION_DONE) || (state==SURF_ACTION_FAILED)) */
178 /*     if(((surf_action_cpu_Cas01_t)action)->variable) { */
179 /*       lmm_variable_disable(cpu_maxmin_system, ((surf_action_cpu_Cas01_t)action)->variable); */
180 /*       ((surf_action_cpu_Cas01_t)action)->variable = NULL; */
181 /*     } */
182
183   surf_action_state_set(action, state);
184   return;
185 }
186
187 static double cpu_share_resources(double now)
188 {
189   s_surf_action_cpu_Cas01_t action;
190   return generic_maxmin_share_resources(surf_cpu_model->
191                                         states.running_action_set,
192                                         xbt_swag_offset(action, variable),
193                                         cpu_maxmin_system, lmm_solve);
194 }
195
196 static void cpu_update_actions_state(double now, double delta)
197 {
198   surf_action_cpu_Cas01_t action = NULL;
199   surf_action_cpu_Cas01_t next_action = NULL;
200   xbt_swag_t running_actions = surf_cpu_model->states.running_action_set;
201
202   xbt_swag_foreach_safe(action, next_action, running_actions) {
203 #ifdef HAVE_TRACING
204     if (TRACE_is_enabled()) {
205       cpu_Cas01_t x =
206         lmm_constraint_id(lmm_get_cnst_from_var
207                           (cpu_maxmin_system, action->variable, 0));
208
209       TRACE_surf_host_set_utilization(x->generic_resource.name,
210                                       action->generic_action.data,
211                                       (surf_action_t) action,
212                                       lmm_variable_getvalue
213                                       (action->variable), now - delta,
214                                       delta);
215       TRACE_last_timestamp_to_dump = now-delta;
216     }
217 #endif
218     double_update(&(action->generic_action.remains),
219                   lmm_variable_getvalue(action->variable) * delta);
220     if (action->generic_action.max_duration != NO_MAX_DURATION)
221       double_update(&(action->generic_action.max_duration), delta);
222     if ((action->generic_action.remains <= 0) &&
223         (lmm_get_variable_weight(action->variable) > 0)) {
224       action->generic_action.finish = surf_get_clock();
225       cpu_action_state_set((surf_action_t) action, SURF_ACTION_DONE);
226     } else if ((action->generic_action.max_duration != NO_MAX_DURATION) &&
227                (action->generic_action.max_duration <= 0)) {
228       action->generic_action.finish = surf_get_clock();
229       cpu_action_state_set((surf_action_t) action, SURF_ACTION_DONE);
230     }
231   }
232
233   return;
234 }
235
236 static void cpu_update_resource_state(void *id,
237                                       tmgr_trace_event_t event_type,
238                                       double value, double date)
239 {
240   cpu_Cas01_t cpu = id;
241   lmm_variable_t var = NULL;
242   lmm_element_t elem = NULL;
243
244   if (event_type == cpu->power_event) {
245     cpu->power_scale = value;
246     lmm_update_constraint_bound(cpu_maxmin_system, cpu->constraint,
247                                 cpu->core * cpu->power_scale * cpu->power_peak);
248 #ifdef HAVE_TRACING
249     TRACE_surf_host_set_power(date, cpu->generic_resource.name,
250                               cpu->core * cpu->power_scale * cpu->power_peak);
251 #endif
252     while ((var = lmm_get_var_from_cnst
253             (cpu_maxmin_system, cpu->constraint, &elem))) {
254         surf_action_cpu_Cas01_t action = lmm_variable_id(var);
255         lmm_update_variable_bound(cpu_maxmin_system, action->variable,
256                                   cpu->power_scale * cpu->power_peak);
257     }
258     if (tmgr_trace_event_free(event_type))
259       cpu->power_event = NULL;
260   } else if (event_type == cpu->state_event) {
261     if (value > 0)
262       cpu->state_current = SURF_RESOURCE_ON;
263     else {
264       lmm_constraint_t cnst = cpu->constraint;
265       lmm_variable_t var = NULL;
266       lmm_element_t elem = NULL;
267
268       cpu->state_current = SURF_RESOURCE_OFF;
269
270       while ((var = lmm_get_var_from_cnst(cpu_maxmin_system, cnst, &elem))) {
271         surf_action_t action = lmm_variable_id(var);
272
273         if (surf_action_state_get(action) == SURF_ACTION_RUNNING ||
274             surf_action_state_get(action) == SURF_ACTION_READY ||
275             surf_action_state_get(action) ==
276             SURF_ACTION_NOT_IN_THE_SYSTEM) {
277           action->finish = date;
278           cpu_action_state_set(action, SURF_ACTION_FAILED);
279         }
280       }
281     }
282     if (tmgr_trace_event_free(event_type))
283       cpu->state_event = NULL;
284   } else {
285     XBT_CRITICAL("Unknown event ! \n");
286     xbt_abort();
287   }
288
289   return;
290 }
291
292 static surf_action_t cpu_execute(void *cpu, double size)
293 {
294   surf_action_cpu_Cas01_t action = NULL;
295   cpu_Cas01_t CPU = cpu;
296
297   XBT_IN("(%s,%g)", surf_resource_name(CPU), size);
298   action =
299       surf_action_new(sizeof(s_surf_action_cpu_Cas01_t), size,
300                       surf_cpu_model,
301                       CPU->state_current != SURF_RESOURCE_ON);
302
303   action->suspended = 0;        /* Should be useless because of the
304                                    calloc but it seems to help valgrind... */
305
306   action->variable = lmm_variable_new(cpu_maxmin_system, action,
307                                       action->generic_action.priority,
308                                       CPU->power_scale * CPU->power_peak, 1);
309   lmm_expand(cpu_maxmin_system, CPU->constraint, action->variable, 1.0);
310   XBT_OUT();
311   return (surf_action_t) action;
312 }
313
314 static surf_action_t cpu_action_sleep(void *cpu, double duration)
315 {
316   surf_action_cpu_Cas01_t action = NULL;
317
318   if (duration > 0)
319     duration = MAX(duration, MAXMIN_PRECISION);
320
321   XBT_IN("(%s,%g)", surf_resource_name(cpu), duration);
322   action = (surf_action_cpu_Cas01_t) cpu_execute(cpu, 1.0);
323   action->generic_action.max_duration = duration;
324   action->suspended = 2;
325   if (duration == NO_MAX_DURATION) {
326     /* Move to the *end* of the corresponding action set. This convention
327        is used to speed up update_resource_state  */
328     xbt_swag_remove(action, ((surf_action_t) action)->state_set);
329     ((surf_action_t) action)->state_set =
330         cpu_running_action_set_that_does_not_need_being_checked;
331     xbt_swag_insert(action, ((surf_action_t) action)->state_set);
332   }
333
334   lmm_update_variable_weight(cpu_maxmin_system, action->variable, 0.0);
335   XBT_OUT();
336   return (surf_action_t) action;
337 }
338
339 static void cpu_action_suspend(surf_action_t action)
340 {
341   XBT_IN("(%p)", action);
342   if (((surf_action_cpu_Cas01_t) action)->suspended != 2) {
343     lmm_update_variable_weight(cpu_maxmin_system,
344                                ((surf_action_cpu_Cas01_t)
345                                 action)->variable, 0.0);
346     ((surf_action_cpu_Cas01_t) action)->suspended = 1;
347   }
348   XBT_OUT();
349 }
350
351 static void cpu_action_resume(surf_action_t action)
352 {
353   XBT_IN("(%p)", action);
354   if (((surf_action_cpu_Cas01_t) action)->suspended != 2) {
355     lmm_update_variable_weight(cpu_maxmin_system,
356                                ((surf_action_cpu_Cas01_t)
357                                 action)->variable, action->priority);
358     ((surf_action_cpu_Cas01_t) action)->suspended = 0;
359   }
360   XBT_OUT();
361 }
362
363 static int cpu_action_is_suspended(surf_action_t action)
364 {
365   return (((surf_action_cpu_Cas01_t) action)->suspended == 1);
366 }
367
368 static void cpu_action_set_max_duration(surf_action_t action,
369                                         double duration)
370 {
371   XBT_IN("(%p,%g)", action, duration);
372   action->max_duration = duration;
373   XBT_OUT();
374 }
375
376 static void cpu_action_set_priority(surf_action_t action, double priority)
377 {
378   XBT_IN("(%p,%g)", action, priority);
379   action->priority = priority;
380   lmm_update_variable_weight(cpu_maxmin_system,
381                              ((surf_action_cpu_Cas01_t) action)->variable,
382                              priority);
383
384   XBT_OUT();
385 }
386
387 #ifdef HAVE_TRACING
388 static void cpu_action_set_category(surf_action_t action, const char *category)
389 {
390   XBT_IN("(%p,%s)", action, category);
391   action->category = xbt_strdup (category);
392   XBT_OUT();
393 }
394 #endif
395
396 static double cpu_action_get_remains(surf_action_t action)
397 {
398   XBT_IN("(%p)", action);
399   return action->remains;
400   XBT_OUT();
401 }
402
403 static e_surf_resource_state_t cpu_get_state(void *cpu)
404 {
405   return ((cpu_Cas01_t) cpu)->state_current;
406 }
407
408 static double cpu_get_speed(void *cpu, double load)
409 {
410   return load * (((cpu_Cas01_t) cpu)->power_peak);
411 }
412
413 static double cpu_get_available_speed(void *cpu)
414 {
415   /* number between 0 and 1 */
416   return ((cpu_Cas01_t) cpu)->power_scale;
417 }
418
419 static void cpu_create_resource(char *name, double power_peak,
420                                 double power_scale,
421                                 tmgr_trace_t power_trace,
422                                 int core,
423                                 e_surf_resource_state_t state_initial,
424                                 tmgr_trace_t state_trace,
425                                 xbt_dict_t cpu_properties)
426 {
427   cpu_new(name, power_peak, power_scale, power_trace, core,
428           state_initial, state_trace, cpu_properties);
429 }
430
431 static void cpu_finalize(void)
432 {
433   lmm_system_free(cpu_maxmin_system);
434   cpu_maxmin_system = NULL;
435
436   surf_model_exit(surf_cpu_model);
437   surf_cpu_model = NULL;
438
439   xbt_swag_free(cpu_running_action_set_that_does_not_need_being_checked);
440   cpu_running_action_set_that_does_not_need_being_checked = NULL;
441 }
442
443 static void surf_cpu_model_init_internal(void)
444 {
445   s_surf_action_t action;
446
447   surf_cpu_model = surf_model_init();
448
449   cpu_running_action_set_that_does_not_need_being_checked =
450       xbt_swag_new(xbt_swag_offset(action, state_hookup));
451
452   surf_cpu_model->name = "CPU";
453
454   surf_cpu_model->action_unref = cpu_action_unref;
455   surf_cpu_model->action_cancel = cpu_action_cancel;
456   surf_cpu_model->action_state_set = cpu_action_state_set;
457
458   surf_cpu_model->model_private->resource_used = cpu_resource_used;
459   surf_cpu_model->model_private->share_resources = cpu_share_resources;
460   surf_cpu_model->model_private->update_actions_state =
461       cpu_update_actions_state;
462   surf_cpu_model->model_private->update_resource_state =
463       cpu_update_resource_state;
464   surf_cpu_model->model_private->finalize = cpu_finalize;
465
466   surf_cpu_model->suspend = cpu_action_suspend;
467   surf_cpu_model->resume = cpu_action_resume;
468   surf_cpu_model->is_suspended = cpu_action_is_suspended;
469   surf_cpu_model->set_max_duration = cpu_action_set_max_duration;
470   surf_cpu_model->set_priority = cpu_action_set_priority;
471 #ifdef HAVE_TRACING
472   surf_cpu_model->set_category = cpu_action_set_category;
473 #endif
474   surf_cpu_model->get_remains = cpu_action_get_remains;
475
476   surf_cpu_model->extension.cpu.execute = cpu_execute;
477   surf_cpu_model->extension.cpu.sleep = cpu_action_sleep;
478
479   surf_cpu_model->extension.cpu.get_state = cpu_get_state;
480   surf_cpu_model->extension.cpu.get_speed = cpu_get_speed;
481   surf_cpu_model->extension.cpu.get_available_speed =
482       cpu_get_available_speed;
483   surf_cpu_model->extension.cpu.create_resource = cpu_create_resource;
484   surf_cpu_model->extension.cpu.add_traces = add_traces_cpu;
485
486   if (!cpu_maxmin_system)
487     cpu_maxmin_system = lmm_system_new();
488 }
489
490 /*********************************************************************/
491 /* Basic sharing model for CPU: that is where all this started... ;) */
492 /*********************************************************************/
493 /* @InProceedings{casanova01simgrid, */
494 /*   author =       "H. Casanova", */
495 /*   booktitle =    "Proceedings of the IEEE Symposium on Cluster Computing */
496 /*                  and the Grid (CCGrid'01)", */
497 /*   publisher =    "IEEE Computer Society", */
498 /*   title =        "Simgrid: {A} Toolkit for the Simulation of Application */
499 /*                  Scheduling", */
500 /*   year =         "2001", */
501 /*   month =        may, */
502 /*   note =         "Available at */
503 /*                  \url{http://grail.sdsc.edu/papers/simgrid_ccgrid01.ps.gz}." */
504 /* } */
505 void surf_cpu_model_init_Cas01(const char *filename)
506 {
507   if (surf_cpu_model)
508     return;
509   surf_cpu_model_init_internal();
510   cpu_define_callbacks(filename);
511   xbt_dynar_push(model_list, &surf_cpu_model);
512 }