Logo AND Algorithmique Numérique Distribuée

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