Logo AND Algorithmique Numérique Distribuée

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