Logo AND Algorithmique Numérique Distribuée

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