Logo AND Algorithmique Numérique Distribuée

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