Logo AND Algorithmique Numérique Distribuée

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