Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Change variable waiting_task to waiting_action on msg process control.
[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   xbt_swag_remove(action,
184                   ((cpu_Cas01_im_t) ACTION_GET_CPU(action))->action_set);
185   return;
186 }
187
188 static void cpu_action_state_set(surf_action_t action,
189                                  e_surf_action_state_t state)
190 {
191 /*   if((state==SURF_ACTION_DONE) || (state==SURF_ACTION_FAILED)) */
192 /*     if(((surf_action_lmm_t)action)->variable) { */
193 /*       lmm_variable_disable(cpu_im_maxmin_system, ((surf_action_lmm_t)action)->variable); */
194 /*       ((surf_action_lmm_t)action)->variable = NULL; */
195 /*     } */
196
197   surf_action_state_set(action, state);
198   return;
199 }
200
201 static void cpu_update_remains(cpu_Cas01_im_t cpu, double now)
202 {
203   surf_action_cpu_Cas01_im_t action;
204
205   xbt_swag_foreach(action, cpu->action_set) {
206     if (GENERIC_ACTION(action).remains > 0) {
207       double_update(&(GENERIC_ACTION(action).remains),
208                     lmm_variable_getvalue(GENERIC_LMM_ACTION
209                                           (action).variable) * (now -
210                                                                 cpu->
211                                                                 last_update));
212       DEBUG2("Update action(%p) remains %lf", action,
213              GENERIC_ACTION(action).remains);
214     }
215   }
216   cpu->last_update = now;
217 }
218
219 static double share_resources(double now)
220 {
221   surf_action_cpu_Cas01_im_t action;
222   double min;
223   double value;
224   cpu_Cas01_im_t cpu, cpu_next;
225
226   xbt_swag_foreach(cpu, modified_cpu)
227     cpu_update_remains(cpu, now);
228
229   lmm_solve(cpu_im_maxmin_system);
230
231   xbt_swag_foreach_safe(cpu, cpu_next, modified_cpu) {
232     xbt_swag_foreach(action, cpu->action_set) {
233       min = -1;
234       value = lmm_variable_getvalue(GENERIC_LMM_ACTION(action).variable);
235       if (value > 0) {
236         if (GENERIC_ACTION(action).remains > 0)
237           value = GENERIC_ACTION(action).remains / value;
238         else
239           value = 0.0;
240       }
241       if (value > 0)
242         min = now + value;
243
244       if ((GENERIC_ACTION(action).max_duration != NO_MAX_DURATION)
245           && (min == -1
246               || GENERIC_ACTION(action).start +
247               GENERIC_ACTION(action).max_duration < min))
248         min =
249           GENERIC_ACTION(action).start + GENERIC_ACTION(action).max_duration;
250
251       DEBUG4("Action(%p) Start %lf Finish %lf Max_duration %lf", action,
252              GENERIC_ACTION(action).start, now + value,
253              GENERIC_ACTION(action).max_duration);
254
255       if (action->index_heap >= 0) {
256         surf_action_cpu_Cas01_im_t heap_act =
257           xbt_heap_remove(action_heap, action->index_heap);
258         if (heap_act != action)
259           DIE_IMPOSSIBLE;
260       }
261       if (min != -1) {
262         xbt_heap_push(action_heap, action, min);
263         DEBUG2("Insert at heap action(%p) min %lf", action, min);
264       }
265     }
266     xbt_swag_remove(cpu, modified_cpu);
267   }
268   return xbt_heap_size(action_heap) >
269     0 ? xbt_heap_maxkey(action_heap) - now : -1;
270 }
271
272 static void update_actions_state(double now, double delta)
273 {
274   surf_action_cpu_Cas01_im_t action;
275
276   while ((xbt_heap_size(action_heap) > 0)
277          && (double_equals(xbt_heap_maxkey(action_heap), now))) {
278     action = xbt_heap_pop(action_heap);
279     DEBUG1("Action %p: finish", action);
280     GENERIC_ACTION(action).finish = surf_get_clock();
281     /* set the remains to 0 due to precision problems when updating the remaining amount */
282     GENERIC_ACTION(action).remains = 0;
283     cpu_action_state_set((surf_action_t) action, SURF_ACTION_DONE);
284     xbt_swag_insert(action->cpu, modified_cpu);
285   }
286   return;
287 }
288
289 static void update_resource_state(void *id,
290                                   tmgr_trace_event_t event_type,
291                                   double value, double date)
292 {
293   cpu_Cas01_im_t cpu = id;
294
295   if (event_type == cpu->power_event) {
296     cpu->power_scale = value;
297     lmm_update_constraint_bound(cpu_im_maxmin_system, cpu->constraint,
298                                 cpu->power_scale * cpu->power_peak);
299     xbt_swag_insert(cpu, modified_cpu);
300     if (tmgr_trace_event_free(event_type))
301       cpu->power_event = NULL;
302   } else if (event_type == cpu->state_event) {
303     if (value > 0)
304       cpu->state_current = SURF_RESOURCE_ON;
305     else {
306       lmm_constraint_t cnst = cpu->constraint;
307       lmm_variable_t var = NULL;
308       lmm_element_t elem = NULL;
309
310       cpu->state_current = SURF_RESOURCE_OFF;
311
312       while ((var = lmm_get_var_from_cnst(cpu_im_maxmin_system, cnst, &elem))) {
313         surf_action_t action = lmm_variable_id(var);
314
315         if (surf_action_state_get(action) == SURF_ACTION_RUNNING ||
316             surf_action_state_get(action) == SURF_ACTION_READY ||
317             surf_action_state_get(action) == SURF_ACTION_NOT_IN_THE_SYSTEM) {
318           action->finish = date;
319           cpu_action_state_set(action, SURF_ACTION_FAILED);
320         }
321       }
322     }
323     if (tmgr_trace_event_free(event_type))
324       cpu->state_event = NULL;
325   } else {
326     CRITICAL0("Unknown event ! \n");
327     xbt_abort();
328   }
329
330   return;
331 }
332
333 static surf_action_t execute(void *cpu, double size)
334 {
335   surf_action_cpu_Cas01_im_t action = NULL;
336   cpu_Cas01_im_t CPU = cpu;
337
338   XBT_IN2("(%s,%g)", surf_resource_name(CPU), size);
339   action =
340     surf_action_new(sizeof(s_surf_action_cpu_Cas01_im_t), size,
341                     surf_cpu_model, CPU->state_current != SURF_RESOURCE_ON);
342
343   GENERIC_LMM_ACTION(action).suspended = 0;     /* Should be useless because of the
344                                                    calloc but it seems to help valgrind... */
345
346   GENERIC_LMM_ACTION(action).variable =
347     lmm_variable_new(cpu_im_maxmin_system, action,
348                      GENERIC_ACTION(action).priority, -1.0, 1);
349   action->index_heap = -1;
350   action->cpu = CPU;
351   xbt_swag_insert(CPU, modified_cpu);
352   xbt_swag_insert(action, CPU->action_set);
353   lmm_expand(cpu_im_maxmin_system, CPU->constraint,
354              GENERIC_LMM_ACTION(action).variable, 1.0);
355   XBT_OUT;
356   return (surf_action_t) action;
357 }
358
359 static surf_action_t action_sleep(void *cpu, double duration)
360 {
361   surf_action_cpu_Cas01_im_t action = NULL;
362
363   if (duration > 0)
364     duration = MAX(duration, MAXMIN_PRECISION);
365
366   XBT_IN2("(%s,%g)", surf_resource_name(cpu), duration);
367   action = (surf_action_cpu_Cas01_im_t) execute(cpu, 1.0);
368   GENERIC_ACTION(action).max_duration = duration;
369   GENERIC_LMM_ACTION(action).suspended = 2;
370   if (duration == NO_MAX_DURATION) {
371     /* Move to the *end* of the corresponding action set. This convention
372        is used to speed up update_resource_state  */
373     xbt_swag_remove(action, ((surf_action_t) action)->state_set);
374     ((surf_action_t) action)->state_set =
375       running_action_set_that_does_not_need_being_checked;
376     xbt_swag_insert(action, ((surf_action_t) action)->state_set);
377   }
378
379   lmm_update_variable_weight(cpu_im_maxmin_system,
380                              GENERIC_LMM_ACTION(action).variable, 0.0);
381   xbt_swag_insert(cpu, modified_cpu);
382   XBT_OUT;
383   return (surf_action_t) action;
384 }
385
386 static void action_suspend(surf_action_t action)
387 {
388   XBT_IN1("(%p)", action);
389   if (((surf_action_lmm_t) action)->suspended != 2) {
390     lmm_update_variable_weight(cpu_im_maxmin_system,
391                                ((surf_action_lmm_t) action)->variable, 0.0);
392     ((surf_action_lmm_t) action)->suspended = 1;
393     xbt_swag_insert(ACTION_GET_CPU(action), modified_cpu);
394   }
395   XBT_OUT;
396 }
397
398 static void action_resume(surf_action_t action)
399 {
400   XBT_IN1("(%p)", action);
401   if (((surf_action_lmm_t) action)->suspended != 2) {
402     lmm_update_variable_weight(cpu_im_maxmin_system,
403                                ((surf_action_lmm_t) action)->variable,
404                                action->priority);
405     ((surf_action_lmm_t) action)->suspended = 0;
406     xbt_swag_insert(ACTION_GET_CPU(action), modified_cpu);
407   }
408   XBT_OUT;
409 }
410
411 static int action_is_suspended(surf_action_t action)
412 {
413   return (((surf_action_lmm_t) action)->suspended == 1);
414 }
415
416 static void action_set_max_duration(surf_action_t action, double duration)
417 {
418   XBT_IN2("(%p,%g)", action, duration);
419
420   action->max_duration = duration;
421   /* insert cpu in modified_cpu set to notice the max duration change */
422   xbt_swag_insert(ACTION_GET_CPU(action), modified_cpu);
423   XBT_OUT;
424 }
425
426 static void action_set_priority(surf_action_t action, double priority)
427 {
428   XBT_IN2("(%p,%g)", action, priority);
429   action->priority = priority;
430   lmm_update_variable_weight(cpu_im_maxmin_system,
431                              ((surf_action_lmm_t) action)->variable,
432                              priority);
433
434   xbt_swag_insert(ACTION_GET_CPU(action), modified_cpu);
435   XBT_OUT;
436 }
437
438 static double action_get_remains(surf_action_t action)
439 {
440   XBT_IN1("(%p)", action);
441   /* update remains before return it */
442   cpu_update_remains(ACTION_GET_CPU(action), surf_get_clock());
443   return action->remains;
444   XBT_OUT;
445 }
446
447 static e_surf_resource_state_t get_state(void *cpu)
448 {
449   return ((cpu_Cas01_im_t) cpu)->state_current;
450 }
451
452 static double get_speed(void *cpu, double load)
453 {
454   return load * (((cpu_Cas01_im_t) cpu)->power_peak);
455 }
456
457 static double get_available_speed(void *cpu)
458 {
459   /* number between 0 and 1 */
460   return ((cpu_Cas01_im_t) cpu)->power_scale;
461 }
462
463 static void action_update_index_heap(void *action, int i)
464 {
465   ((surf_action_cpu_Cas01_im_t) action)->index_heap = i;
466 }
467
468 static void finalize(void)
469 {
470   void *cpu;
471   xbt_dict_cursor_t cursor;
472   char *key;
473   xbt_dict_foreach(surf_model_resource_set(surf_cpu_model), cursor, key, cpu) {
474     cpu_Cas01_im_t CPU = cpu;
475     xbt_swag_free(CPU->action_set);
476   }
477
478   lmm_system_free(cpu_im_maxmin_system);
479   cpu_im_maxmin_system = NULL;
480
481   surf_model_exit(surf_cpu_model);
482   surf_cpu_model = NULL;
483
484   xbt_swag_free(running_action_set_that_does_not_need_being_checked);
485   running_action_set_that_does_not_need_being_checked = NULL;
486   xbt_heap_free(action_heap);
487   xbt_swag_free(modified_cpu);
488 }
489
490 static void surf_cpu_model_init_internal(void)
491 {
492   s_surf_action_t action;
493   s_cpu_Cas01_im_t cpu;
494
495   surf_cpu_model = surf_model_init();
496
497   running_action_set_that_does_not_need_being_checked =
498     xbt_swag_new(xbt_swag_offset(action, state_hookup));
499
500   surf_cpu_model->name = "CPU_IM";
501
502   surf_cpu_model->action_unref = action_unref;
503   surf_cpu_model->action_cancel = action_cancel;
504   surf_cpu_model->action_state_set = cpu_action_state_set;
505
506   surf_cpu_model->model_private->resource_used = resource_used;
507   surf_cpu_model->model_private->share_resources = share_resources;
508   surf_cpu_model->model_private->update_actions_state = update_actions_state;
509   surf_cpu_model->model_private->update_resource_state =
510     update_resource_state;
511   surf_cpu_model->model_private->finalize = finalize;
512
513   surf_cpu_model->suspend = action_suspend;
514   surf_cpu_model->resume = action_resume;
515   surf_cpu_model->is_suspended = action_is_suspended;
516   surf_cpu_model->set_max_duration = action_set_max_duration;
517   surf_cpu_model->set_priority = action_set_priority;
518   surf_cpu_model->get_remains = action_get_remains;
519
520   surf_cpu_model->extension.cpu.execute = execute;
521   surf_cpu_model->extension.cpu.sleep = action_sleep;
522
523   surf_cpu_model->extension.cpu.get_state = get_state;
524   surf_cpu_model->extension.cpu.get_speed = get_speed;
525   surf_cpu_model->extension.cpu.get_available_speed = get_available_speed;
526
527   if (!cpu_im_maxmin_system) {
528     sg_maxmin_selective_update = 1;
529     cpu_im_maxmin_system = lmm_system_new();
530   }
531   action_heap = xbt_heap_new(8, NULL);
532   xbt_heap_set_update_callback(action_heap, action_update_index_heap);
533   modified_cpu = xbt_swag_new(xbt_swag_offset(cpu, modified_cpu_hookup));
534 }
535
536 /*********************************************************************/
537 /* Basic sharing model for CPU: that is where all this started... ;) */
538 /*********************************************************************/
539 /* @InProceedings{casanova01simgrid, */
540 /*   author =       "H. Casanova", */
541 /*   booktitle =    "Proceedings of the IEEE Symposium on Cluster Computing */
542 /*                  and the Grid (CCGrid'01)", */
543 /*   publisher =    "IEEE Computer Society", */
544 /*   title =        "Simgrid: {A} Toolkit for the Simulation of Application */
545 /*                  Scheduling", */
546 /*   year =         "2001", */
547 /*   month =        may, */
548 /*   note =         "Available at */
549 /*                  \url{http://grail.sdsc.edu/papers/simgrid_ccgrid01.ps.gz}." */
550 /* } */
551 void surf_cpu_model_init_Cas01_im(const char *filename)
552 {
553   if (surf_cpu_model)
554     return;
555   surf_cpu_model_init_internal();
556   define_callbacks(filename);
557   xbt_dynar_push(model_list, &surf_cpu_model);
558 }