Logo AND Algorithmique Numérique Distribuée

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