Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge changes of maxmin_selective_update branch into the trunk.
[simgrid.git] / src / surf / cpu_im.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 #define GENERIC_LMM_ACTION(action) action->generic_lmm_action
11 #define GENERIC_ACTION(action) GENERIC_LMM_ACTION(action).generic_action
12 #define ACTION_GET_CPU(action) ((surf_action_cpu_Cas01_im_t) action)->cpu
13
14 typedef struct surf_action_cpu_cas01_im {
15   s_surf_action_lmm_t generic_lmm_action;
16   s_xbt_swag_hookup_t cpu_list_hookup;
17   int index_heap;
18   void *cpu;
19 } s_surf_action_cpu_Cas01_im_t, *surf_action_cpu_Cas01_im_t;
20
21 typedef struct cpu_Cas01_im {
22   s_surf_resource_t generic_resource;
23   s_xbt_swag_hookup_t modified_cpu_hookup;
24   double power_peak;
25   double power_scale;
26   tmgr_trace_event_t power_event;
27   e_surf_resource_state_t state_current;
28   tmgr_trace_event_t state_event;
29   lmm_constraint_t constraint;
30   xbt_swag_t action_set;
31   double last_update;
32 } s_cpu_Cas01_im_t, *cpu_Cas01_im_t;
33
34 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_cpu_im, surf,
35                                 "Logging specific to the SURF CPU IMPROVED module");
36
37
38 lmm_system_t cpu_im_maxmin_system = NULL;
39 static xbt_swag_t modified_cpu = NULL;
40 static xbt_heap_t action_heap = NULL;
41 extern int sg_maxmin_selective_update;
42
43
44 static xbt_swag_t running_action_set_that_does_not_need_being_checked = NULL;
45
46 static cpu_Cas01_im_t cpu_new(char *name, double power_peak,
47                               double power_scale,
48                               tmgr_trace_t power_trace,
49                               e_surf_resource_state_t state_initial,
50                               tmgr_trace_t state_trace,
51                               xbt_dict_t cpu_properties)
52 {
53   cpu_Cas01_im_t cpu = xbt_new0(s_cpu_Cas01_im_t, 1);
54   s_surf_action_cpu_Cas01_im_t action;
55   xbt_assert1(!surf_model_resource_by_name(surf_cpu_model, name),
56               "Host '%s' declared several times in the platform file", name);
57   cpu->generic_resource.model = surf_cpu_model;
58   cpu->generic_resource.name = name;
59   cpu->generic_resource.properties = cpu_properties;
60   cpu->power_peak = power_peak;
61   xbt_assert0(cpu->power_peak > 0, "Power has to be >0");
62   cpu->power_scale = power_scale;
63   if (power_trace)
64     cpu->power_event =
65       tmgr_history_add_trace(history, power_trace, 0.0, 0, cpu);
66
67   cpu->state_current = state_initial;
68   if (state_trace)
69     cpu->state_event =
70       tmgr_history_add_trace(history, state_trace, 0.0, 0, cpu);
71
72   cpu->constraint =
73     lmm_constraint_new(cpu_im_maxmin_system, cpu,
74                        cpu->power_scale * cpu->power_peak);
75
76   xbt_dict_set(surf_model_resource_set(surf_cpu_model), name, cpu,
77                surf_resource_free);
78   cpu->action_set = xbt_swag_new(xbt_swag_offset(action, cpu_list_hookup));
79
80   return cpu;
81 }
82
83
84 static void parse_cpu_init(void)
85 {
86   double power_peak = 0.0;
87   double power_scale = 0.0;
88   tmgr_trace_t power_trace = NULL;
89   e_surf_resource_state_t state_initial = SURF_RESOURCE_OFF;
90   tmgr_trace_t state_trace = NULL;
91
92   power_peak = get_cpu_power(A_surfxml_host_power);
93   surf_parse_get_double(&power_scale, A_surfxml_host_availability);
94   power_trace = tmgr_trace_new(A_surfxml_host_availability_file);
95
96   xbt_assert0((A_surfxml_host_state == A_surfxml_host_state_ON) ||
97               (A_surfxml_host_state == A_surfxml_host_state_OFF),
98               "Invalid state");
99   if (A_surfxml_host_state == A_surfxml_host_state_ON)
100     state_initial = SURF_RESOURCE_ON;
101   if (A_surfxml_host_state == A_surfxml_host_state_OFF)
102     state_initial = SURF_RESOURCE_OFF;
103   state_trace = tmgr_trace_new(A_surfxml_host_state_file);
104
105   current_property_set = xbt_dict_new();
106   cpu_new(xbt_strdup(A_surfxml_host_id), power_peak, power_scale,
107           power_trace, state_initial, state_trace, current_property_set);
108
109 }
110
111 static void add_traces_cpu(void)
112 {
113   xbt_dict_cursor_t cursor = NULL;
114   char *trace_name, *elm;
115
116   static int called = 0;
117
118   if (called)
119     return;
120   called = 1;
121
122
123   /* connect all traces relative to hosts */
124   xbt_dict_foreach(trace_connect_list_host_avail, cursor, trace_name, elm) {
125     tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
126     cpu_Cas01_im_t host = surf_model_resource_by_name(surf_cpu_model, elm);
127
128     xbt_assert1(host, "Host %s undefined", elm);
129     xbt_assert1(trace, "Trace %s undefined", trace_name);
130
131     host->state_event = tmgr_history_add_trace(history, trace, 0.0, 0, host);
132   }
133
134   xbt_dict_foreach(trace_connect_list_power, cursor, trace_name, elm) {
135     tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
136     cpu_Cas01_im_t host = surf_model_resource_by_name(surf_cpu_model, elm);
137
138     xbt_assert1(host, "Host %s undefined", elm);
139     xbt_assert1(trace, "Trace %s undefined", trace_name);
140
141     host->power_event = tmgr_history_add_trace(history, trace, 0.0, 0, host);
142   }
143 }
144
145 static void define_callbacks(const char *file)
146 {
147   surf_parse_reset_parser();
148   surfxml_add_callback(STag_surfxml_host_cb_list, parse_cpu_init);
149   surfxml_add_callback(ETag_surfxml_platform_cb_list, &add_traces_cpu);
150 }
151
152 static int resource_used(void *resource_id)
153 {
154   return lmm_constraint_used(cpu_im_maxmin_system,
155                              ((cpu_Cas01_im_t) resource_id)->constraint);
156 }
157
158 static int action_unref(surf_action_t action)
159 {
160   action->refcount--;
161   if (!action->refcount) {
162     xbt_swag_remove(action, action->state_set);
163     if (((surf_action_lmm_t) action)->variable)
164       lmm_variable_free(cpu_im_maxmin_system,
165                         ((surf_action_lmm_t) action)->variable);
166     /* remove from heap */
167     xbt_heap_remove(action_heap,
168                     ((surf_action_cpu_Cas01_im_t) action)->index_heap);
169     xbt_swag_remove(action,
170                     ((cpu_Cas01_im_t) ACTION_GET_CPU(action))->action_set);
171     xbt_swag_insert(ACTION_GET_CPU(action), modified_cpu);
172     free(action);
173     return 1;
174   }
175   return 0;
176 }
177
178 static void action_cancel(surf_action_t action)
179 {
180   surf_action_state_set(action, SURF_ACTION_FAILED);
181   xbt_heap_remove(action_heap,
182                   ((surf_action_cpu_Cas01_im_t) action)->index_heap);
183   return;
184 }
185
186 static void cpu_action_state_set(surf_action_t action,
187                                  e_surf_action_state_t state)
188 {
189 /*   if((state==SURF_ACTION_DONE) || (state==SURF_ACTION_FAILED)) */
190 /*     if(((surf_action_lmm_t)action)->variable) { */
191 /*       lmm_variable_disable(cpu_im_maxmin_system, ((surf_action_lmm_t)action)->variable); */
192 /*       ((surf_action_lmm_t)action)->variable = NULL; */
193 /*     } */
194
195   surf_action_state_set(action, state);
196   return;
197 }
198
199 static void cpu_update_remains(cpu_Cas01_im_t cpu, double now)
200 {
201   surf_action_cpu_Cas01_im_t action;
202
203   xbt_swag_foreach(action, cpu->action_set) {
204     if (GENERIC_ACTION(action).remains > 0) {
205       double_update(&(GENERIC_ACTION(action).remains),
206                     lmm_variable_getvalue(GENERIC_LMM_ACTION
207                                           (action).variable) * (now -
208                                                                 cpu->
209                                                                 last_update));
210       DEBUG2("Update action(%p) remains %lf", action,
211              GENERIC_ACTION(action).remains);
212     }
213   }
214   cpu->last_update = now;
215 }
216
217 static double share_resources(double now)
218 {
219   surf_action_cpu_Cas01_im_t action;
220   double min;
221   double value;
222   cpu_Cas01_im_t cpu, cpu_next;
223
224   xbt_swag_foreach_safe(cpu, cpu_next, modified_cpu) {
225     cpu_update_remains(cpu, now);
226     xbt_swag_remove(cpu, modified_cpu);
227   }
228
229   lmm_solve(cpu_im_maxmin_system);
230
231   while ((action = lmm_extract_modified_variable(cpu_im_maxmin_system))) {
232     min = -1;
233     value = lmm_variable_getvalue(GENERIC_LMM_ACTION(action).variable);
234     if (value > 0) {
235       if (GENERIC_ACTION(action).remains > 0)
236         value = GENERIC_ACTION(action).remains / value;
237       else
238         value = 0.0;
239     }
240     if (value > 0)
241       min = now + value;
242
243     if ((GENERIC_ACTION(action).max_duration != NO_MAX_DURATION)
244         && (min == -1
245             || GENERIC_ACTION(action).start +
246             GENERIC_ACTION(action).max_duration < min))
247       min =
248         GENERIC_ACTION(action).start + GENERIC_ACTION(action).max_duration;
249
250     DEBUG4("Action(%p) Start %lf Finish %lf Max_duration %lf", action,
251            GENERIC_ACTION(action).start, now + value,
252            GENERIC_ACTION(action).max_duration);
253
254     if (action->index_heap >= 0) {
255       surf_action_cpu_Cas01_im_t heap_act =
256         xbt_heap_remove(action_heap, action->index_heap);
257       if (heap_act != action)
258         DIE_IMPOSSIBLE;
259     }
260     if (min != -1) {
261       xbt_heap_push(action_heap, action, min);
262       DEBUG2("Insert at heap action(%p) min %lf", action, min);
263     }
264   }
265   return xbt_heap_size(action_heap) >
266     0 ? xbt_heap_maxkey(action_heap) - now : -1;
267 }
268
269 static void update_actions_state(double now, double delta)
270 {
271   surf_action_cpu_Cas01_im_t action;
272
273   while ((xbt_heap_size(action_heap) > 0)
274          && (xbt_heap_maxkey(action_heap) <= now)) {
275     action = xbt_heap_pop(action_heap);
276     DEBUG1("Action %p: finish", action);
277     GENERIC_ACTION(action).finish = surf_get_clock();
278     /* set the remains to 0 due to precision problems when updating the remaining amount */
279     GENERIC_ACTION(action).remains = 0;
280     cpu_action_state_set((surf_action_t) action, SURF_ACTION_DONE);
281     xbt_swag_insert(action->cpu, modified_cpu);
282   }
283   return;
284 }
285
286 static void update_resource_state(void *id,
287                                   tmgr_trace_event_t event_type,
288                                   double value, double date)
289 {
290   cpu_Cas01_im_t cpu = id;
291
292   if (event_type == cpu->power_event) {
293     cpu->power_scale = value;
294     lmm_update_constraint_bound(cpu_im_maxmin_system, cpu->constraint,
295                                 cpu->power_scale * cpu->power_peak);
296     xbt_swag_insert(cpu, modified_cpu);
297     if (tmgr_trace_event_free(event_type))
298       cpu->power_event = NULL;
299   } else if (event_type == cpu->state_event) {
300     if (value > 0)
301       cpu->state_current = SURF_RESOURCE_ON;
302     else {
303       lmm_constraint_t cnst = cpu->constraint;
304       lmm_variable_t var = NULL;
305       lmm_element_t elem = NULL;
306
307       cpu->state_current = SURF_RESOURCE_OFF;
308
309       while ((var = lmm_get_var_from_cnst(cpu_im_maxmin_system, cnst, &elem))) {
310         surf_action_t action = lmm_variable_id(var);
311
312         if (surf_action_state_get(action) == SURF_ACTION_RUNNING ||
313             surf_action_state_get(action) == SURF_ACTION_READY ||
314             surf_action_state_get(action) == SURF_ACTION_NOT_IN_THE_SYSTEM) {
315           action->finish = date;
316           cpu_action_state_set(action, SURF_ACTION_FAILED);
317         }
318       }
319     }
320     if (tmgr_trace_event_free(event_type))
321       cpu->state_event = NULL;
322   } else {
323     CRITICAL0("Unknown event ! \n");
324     xbt_abort();
325   }
326
327   return;
328 }
329
330 static surf_action_t execute(void *cpu, double size)
331 {
332   surf_action_cpu_Cas01_im_t action = NULL;
333   cpu_Cas01_im_t CPU = cpu;
334
335   XBT_IN2("(%s,%g)", surf_resource_name(CPU), size);
336   action =
337     surf_action_new(sizeof(s_surf_action_cpu_Cas01_im_t), size,
338                     surf_cpu_model, CPU->state_current != SURF_RESOURCE_ON);
339
340   GENERIC_LMM_ACTION(action).suspended = 0;     /* Should be useless because of the
341                                                    calloc but it seems to help valgrind... */
342
343   GENERIC_LMM_ACTION(action).variable =
344     lmm_variable_new(cpu_im_maxmin_system, action,
345                      GENERIC_ACTION(action).priority, -1.0, 1);
346   action->index_heap = -1;
347   action->cpu = CPU;
348   xbt_swag_insert(CPU, modified_cpu);
349   xbt_swag_insert(action, CPU->action_set);
350   lmm_expand(cpu_im_maxmin_system, CPU->constraint,
351              GENERIC_LMM_ACTION(action).variable, 1.0);
352   XBT_OUT;
353   return (surf_action_t) action;
354 }
355
356 static surf_action_t action_sleep(void *cpu, double duration)
357 {
358   surf_action_cpu_Cas01_im_t action = NULL;
359
360   if (duration > 0)
361     duration = MAX(duration, MAXMIN_PRECISION);
362
363   XBT_IN2("(%s,%g)", surf_resource_name(cpu), duration);
364   action = (surf_action_cpu_Cas01_im_t) execute(cpu, 1.0);
365   GENERIC_ACTION(action).max_duration = duration;
366   GENERIC_LMM_ACTION(action).suspended = 2;
367   if (duration == NO_MAX_DURATION) {
368     /* Move to the *end* of the corresponding action set. This convention
369        is used to speed up update_resource_state  */
370     xbt_swag_remove(action, ((surf_action_t) action)->state_set);
371     ((surf_action_t) action)->state_set =
372       running_action_set_that_does_not_need_being_checked;
373     xbt_swag_insert(action, ((surf_action_t) action)->state_set);
374   }
375
376   lmm_update_variable_weight(cpu_im_maxmin_system,
377                              GENERIC_LMM_ACTION(action).variable, 0.0);
378   xbt_swag_insert(cpu, modified_cpu);
379   XBT_OUT;
380   return (surf_action_t) action;
381 }
382
383 static void action_suspend(surf_action_t action)
384 {
385   XBT_IN1("(%p)", action);
386   if (((surf_action_lmm_t) action)->suspended != 2) {
387     lmm_update_variable_weight(cpu_im_maxmin_system,
388                                ((surf_action_lmm_t) action)->variable, 0.0);
389     ((surf_action_lmm_t) action)->suspended = 1;
390     xbt_swag_insert(ACTION_GET_CPU(action), modified_cpu);
391   }
392   XBT_OUT;
393 }
394
395 static void action_resume(surf_action_t action)
396 {
397   XBT_IN1("(%p)", action);
398   if (((surf_action_lmm_t) action)->suspended != 2) {
399     lmm_update_variable_weight(cpu_im_maxmin_system,
400                                ((surf_action_lmm_t) action)->variable,
401                                action->priority);
402     ((surf_action_lmm_t) action)->suspended = 0;
403     xbt_swag_insert(ACTION_GET_CPU(action), modified_cpu);
404   }
405   XBT_OUT;
406 }
407
408 static int action_is_suspended(surf_action_t action)
409 {
410   return (((surf_action_lmm_t) action)->suspended == 1);
411 }
412
413 static void action_set_max_duration(surf_action_t action, double duration)
414 {
415   surf_action_cpu_Cas01_im_t ACT = (surf_action_cpu_Cas01_im_t) action;
416   double min_finish;
417
418   XBT_IN2("(%p,%g)", action, duration);
419
420   action->max_duration = duration;
421
422   if (duration >= 0)
423     min_finish =
424       (action->start + action->max_duration) <
425       action->finish ? (action->start +
426                         action->max_duration) : action->finish;
427   else
428     min_finish = action->finish;
429
430   /* add in action heap */
431   if (ACT->index_heap >= 0) {
432     surf_action_cpu_Cas01_im_t heap_act =
433       xbt_heap_remove(action_heap, ACT->index_heap);
434     if (heap_act != ACT)
435       DIE_IMPOSSIBLE;
436   }
437   xbt_heap_push(action_heap, ACT, min_finish);
438   XBT_OUT;
439 }
440
441 static void action_set_priority(surf_action_t action, double priority)
442 {
443   XBT_IN2("(%p,%g)", action, priority);
444   action->priority = priority;
445   lmm_update_variable_weight(cpu_im_maxmin_system,
446                              ((surf_action_lmm_t) action)->variable,
447                              priority);
448
449   xbt_swag_insert(ACTION_GET_CPU(action), modified_cpu);
450   XBT_OUT;
451 }
452
453 static double action_get_remains(surf_action_t action)
454 {
455   XBT_IN1("(%p)", action);
456   cpu_update_remains(ACTION_GET_CPU(action), surf_get_clock());
457   return action->remains;
458   XBT_OUT;
459 }
460
461 static e_surf_resource_state_t get_state(void *cpu)
462 {
463   return ((cpu_Cas01_im_t) cpu)->state_current;
464 }
465
466 static double get_speed(void *cpu, double load)
467 {
468   return load * (((cpu_Cas01_im_t) cpu)->power_peak);
469 }
470
471 static double get_available_speed(void *cpu)
472 {
473   /* number between 0 and 1 */
474   return ((cpu_Cas01_im_t) cpu)->power_scale;
475 }
476
477 static void action_update_index_heap(void *action, int i)
478 {
479   ((surf_action_cpu_Cas01_im_t) action)->index_heap = i;
480 }
481
482 static void finalize(void)
483 {
484   void *cpu;
485   xbt_dict_cursor_t cursor;
486   char *key;
487   xbt_dict_foreach(surf_model_resource_set(surf_cpu_model), cursor, key, cpu) {
488     cpu_Cas01_im_t CPU = cpu;
489     xbt_swag_free(CPU->action_set);
490   }
491
492   lmm_system_free(cpu_im_maxmin_system);
493   cpu_im_maxmin_system = NULL;
494
495   surf_model_exit(surf_cpu_model);
496   surf_cpu_model = NULL;
497
498   xbt_swag_free(running_action_set_that_does_not_need_being_checked);
499   running_action_set_that_does_not_need_being_checked = NULL;
500   xbt_heap_free(action_heap);
501   xbt_swag_free(modified_cpu);
502 }
503
504 static void surf_cpu_model_init_internal(void)
505 {
506   s_surf_action_t action;
507   s_cpu_Cas01_im_t cpu;
508
509   surf_cpu_model = surf_model_init();
510
511   running_action_set_that_does_not_need_being_checked =
512     xbt_swag_new(xbt_swag_offset(action, state_hookup));
513
514   surf_cpu_model->name = "CPU_IM";
515
516   surf_cpu_model->action_unref = action_unref;
517   surf_cpu_model->action_cancel = action_cancel;
518   surf_cpu_model->action_state_set = cpu_action_state_set;
519
520   surf_cpu_model->model_private->resource_used = resource_used;
521   surf_cpu_model->model_private->share_resources = share_resources;
522   surf_cpu_model->model_private->update_actions_state = update_actions_state;
523   surf_cpu_model->model_private->update_resource_state =
524     update_resource_state;
525   surf_cpu_model->model_private->finalize = finalize;
526
527   surf_cpu_model->suspend = action_suspend;
528   surf_cpu_model->resume = action_resume;
529   surf_cpu_model->is_suspended = action_is_suspended;
530   surf_cpu_model->set_max_duration = action_set_max_duration;
531   surf_cpu_model->set_priority = action_set_priority;
532   surf_cpu_model->get_remains = action_get_remains;
533
534   surf_cpu_model->extension.cpu.execute = execute;
535   surf_cpu_model->extension.cpu.sleep = action_sleep;
536
537   surf_cpu_model->extension.cpu.get_state = get_state;
538   surf_cpu_model->extension.cpu.get_speed = get_speed;
539   surf_cpu_model->extension.cpu.get_available_speed = get_available_speed;
540
541   if (!cpu_im_maxmin_system) {
542     sg_maxmin_selective_update = 1;
543     cpu_im_maxmin_system = lmm_system_new();
544   }
545   action_heap = xbt_heap_new(8, NULL);
546   xbt_heap_set_update_callback(action_heap, action_update_index_heap);
547   modified_cpu = xbt_swag_new(xbt_swag_offset(cpu, modified_cpu_hookup));
548 }
549
550 /*********************************************************************/
551 /* Basic sharing model for CPU: that is where all this started... ;) */
552 /*********************************************************************/
553 /* @InProceedings{casanova01simgrid, */
554 /*   author =       "H. Casanova", */
555 /*   booktitle =    "Proceedings of the IEEE Symposium on Cluster Computing */
556 /*                  and the Grid (CCGrid'01)", */
557 /*   publisher =    "IEEE Computer Society", */
558 /*   title =        "Simgrid: {A} Toolkit for the Simulation of Application */
559 /*                  Scheduling", */
560 /*   year =         "2001", */
561 /*   month =        may, */
562 /*   note =         "Available at */
563 /*                  \url{http://grail.sdsc.edu/papers/simgrid_ccgrid01.ps.gz}." */
564 /* } */
565 void surf_cpu_model_init_Cas01_im(const char *filename)
566 {
567   if (surf_cpu_model)
568     return;
569   surf_cpu_model_init_internal();
570   define_callbacks(filename);
571   xbt_dynar_push(model_list, &surf_cpu_model);
572 }