Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
don't trust gcc for cleaning your code after you. he's too dumb to be trusted
[simgrid.git] / src / surf / cpu_ti.c
1
2 /*      $Id$     */
3
4 /* Copyright (c) 2004 Arnaud Legrand. All rights reserved.                  */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8
9 /*
10         commit: e2d6799c4182f00443b3013aadb1c2412372460f
11         This commit retrieves the old implementation of CPU_TI with multi-levels.
12 */
13
14 #include "surf_private.h"
15 #include "trace_mgr_private.h"
16 #include "cpu_ti_private.h"
17 #include "xbt/heap.h"
18
19
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_cpu_ti, surf,
21                                 "Logging specific to the SURF CPU TRACE INTEGRATION module");
22
23
24 static xbt_swag_t running_action_set_that_does_not_need_being_checked = NULL;
25 static xbt_swag_t modified_cpu = NULL;
26 static xbt_heap_t action_heap;
27
28 /* prototypes of new trace functions */
29 static double surf_cpu_integrate_trace(surf_cpu_ti_tgmr_t trace, double a,
30                                        double b);
31
32
33 static double surf_cpu_solve_trace(surf_cpu_ti_tgmr_t trace, double a,
34                                    double amount);
35 static double surf_cpu_solve_trace_somewhat_simple(surf_cpu_ti_tgmr_t trace,
36                                                    double a, double amount);
37
38 static void surf_cpu_free_tmgr(surf_cpu_ti_tgmr_t trace);
39
40 static double surf_cpu_integrate_trace_simple(surf_cpu_ti_trace_t trace,
41                                               double a, double b);
42 static double surf_cpu_integrate_trace_simple_point(surf_cpu_ti_trace_t trace,
43                                                     double a);
44 static double surf_cpu_solve_trace_simple(surf_cpu_ti_trace_t trace, double a,
45                                           double amount);
46 static int surf_cpu_binary_search(double *array, double a, int low, int high);
47 /* end prototypes */
48
49 static void surf_cpu_free_trace(surf_cpu_ti_trace_t trace)
50 {
51   if (trace->time_points)
52     xbt_free(trace->time_points);
53   if (trace->integral)
54     xbt_free(trace->integral);
55   xbt_free(trace);
56 }
57
58 static void surf_cpu_free_tmgr(surf_cpu_ti_tgmr_t trace)
59 {
60   if (trace->trace)
61     surf_cpu_free_trace(trace->trace);
62   xbt_free(trace);
63 }
64
65 static surf_cpu_ti_trace_t surf_cpu_trace_new(tmgr_trace_t power_trace)
66 {
67   surf_cpu_ti_trace_t trace;
68   s_tmgr_event_t val;
69   unsigned int cpt;
70   double integral = 0;
71   double time = 0;
72   int i = 0;
73   trace = xbt_new0(s_surf_cpu_ti_trace_t, 1);
74   trace->time_points =
75     xbt_malloc0(sizeof(double) *
76                 (xbt_dynar_length(power_trace->event_list) + 1));
77   trace->integral =
78     xbt_malloc0(sizeof(double) *
79                 (xbt_dynar_length(power_trace->event_list) + 1));
80   trace->nb_points = xbt_dynar_length(power_trace->event_list);
81   xbt_dynar_foreach(power_trace->event_list, cpt, val) {
82     trace->time_points[i] = time;
83     trace->integral[i] = integral;
84     integral += val.delta * val.value;
85     time += val.delta;
86     i++;
87   }
88   trace->time_points[i] = time;
89   trace->integral[i] = integral;
90   return trace;
91 }
92
93 /**
94 * \brief Creates a new integration trace from a tmgr_trace_t
95 *
96 * \param        power_trace             CPU availability trace
97 * \param        value                                   Percentage of CPU power available (useful to fixed tracing)
98 * \param        spacing                         Initial spacing
99 * \return       Integration trace structure
100 */
101 static surf_cpu_ti_tgmr_t cpu_ti_parse_trace(tmgr_trace_t power_trace,
102                                              double value)
103 {
104   surf_cpu_ti_tgmr_t trace;
105   double total_time = 0.0;
106   s_tmgr_event_t val;
107   unsigned int cpt;
108   trace = xbt_new0(s_surf_cpu_ti_tgmr_t, 1);
109
110 /* no availability file, fixed trace */
111   if (!power_trace) {
112     trace->type = TRACE_FIXED;
113     trace->value = value;
114     DEBUG1("No availabily trace. Constant value = %lf", value);
115     return trace;
116   }
117
118   /* only one point available, fixed trace */
119   if (xbt_dynar_length(power_trace->event_list) == 1) {
120     xbt_dynar_get_cpy(power_trace->event_list, 0, &val);
121     trace->type = TRACE_FIXED;
122     trace->value = val.value;
123     return trace;
124   }
125
126   trace->type = TRACE_DYNAMIC;
127   trace->power_trace = power_trace;
128
129   /* count the total time of trace file */
130   xbt_dynar_foreach(power_trace->event_list, cpt, val) {
131     total_time += val.delta;
132   }
133   trace->trace = surf_cpu_trace_new(power_trace);
134   trace->last_time = total_time;
135   trace->total = surf_cpu_integrate_trace_simple(trace->trace, 0, total_time);
136
137   DEBUG2("Total integral %lf, last_time %lf ",
138          trace->total, trace->last_time);
139
140   return trace;
141 }
142
143
144 static cpu_ti_t cpu_new(char *name, double power_peak,
145                         double power_scale,
146                         tmgr_trace_t power_trace,
147                         e_surf_resource_state_t state_initial,
148                         tmgr_trace_t state_trace, xbt_dict_t cpu_properties)
149 {
150   tmgr_trace_t empty_trace;
151   s_tmgr_event_t val;
152   cpu_ti_t cpu = xbt_new0(s_cpu_ti_t, 1);
153   s_surf_action_cpu_ti_t ti_action;
154   xbt_assert1(!surf_model_resource_by_name(surf_cpu_model, name),
155               "Host '%s' declared several times in the platform file", name);
156   cpu->action_set = xbt_swag_new(xbt_swag_offset(ti_action, cpu_list_hookup));
157   cpu->generic_resource.model = surf_cpu_model;
158   cpu->generic_resource.name = name;
159   cpu->generic_resource.properties = cpu_properties;
160   cpu->power_peak = power_peak;
161   xbt_assert0(cpu->power_peak > 0, "Power has to be >0");
162   DEBUG1("power scale %lf", power_scale);
163   cpu->power_scale = power_scale;
164   cpu->avail_trace = cpu_ti_parse_trace(power_trace, power_scale);
165   cpu->state_current = state_initial;
166   if (state_trace)
167     cpu->state_event =
168       tmgr_history_add_trace(history, state_trace, 0.0, 0, cpu);
169   if (power_trace && xbt_dynar_length(power_trace->event_list) > 1) {
170     /* add a fake trace event if periodicity == 0 */
171     xbt_dynar_get_cpy(power_trace->event_list,
172                       xbt_dynar_length(power_trace->event_list) - 1, &val);
173     if (val.delta == 0) {
174       empty_trace = tmgr_empty_trace_new();
175       cpu->power_event =
176         tmgr_history_add_trace(history, empty_trace,
177                                cpu->avail_trace->last_time, 0, cpu);
178     }
179   }
180   xbt_dict_set(surf_model_resource_set(surf_cpu_model), name, cpu,
181                surf_resource_free);
182
183   return cpu;
184 }
185
186
187 static void parse_cpu_init(void)
188 {
189   double power_peak = 0.0;
190   double power_scale = 0.0;
191   tmgr_trace_t power_trace = NULL;
192   e_surf_resource_state_t state_initial = SURF_RESOURCE_OFF;
193   tmgr_trace_t state_trace = NULL;
194
195   power_peak = get_cpu_power(A_surfxml_host_power);
196   surf_parse_get_double(&power_scale, A_surfxml_host_availability);
197   power_trace = tmgr_trace_new(A_surfxml_host_availability_file);
198
199   xbt_assert0((A_surfxml_host_state == A_surfxml_host_state_ON) ||
200               (A_surfxml_host_state == A_surfxml_host_state_OFF),
201               "Invalid state");
202   if (A_surfxml_host_state == A_surfxml_host_state_ON)
203     state_initial = SURF_RESOURCE_ON;
204   if (A_surfxml_host_state == A_surfxml_host_state_OFF)
205     state_initial = SURF_RESOURCE_OFF;
206   state_trace = tmgr_trace_new(A_surfxml_host_state_file);
207
208   current_property_set = xbt_dict_new();
209   cpu_new(xbt_strdup(A_surfxml_host_id), power_peak, power_scale,
210           power_trace, state_initial, state_trace, current_property_set);
211
212 }
213
214 static void add_traces_cpu(void)
215 {
216   xbt_dict_cursor_t cursor = NULL;
217   char *trace_name, *elm;
218
219   static int called = 0;
220
221   if (called)
222     return;
223   called = 1;
224
225 /* connect all traces relative to hosts */
226   xbt_dict_foreach(trace_connect_list_host_avail, cursor, trace_name, elm) {
227     tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
228     cpu_ti_t cpu = surf_model_resource_by_name(surf_cpu_model, elm);
229
230     xbt_assert1(cpu, "Host %s undefined", elm);
231     xbt_assert1(trace, "Trace %s undefined", trace_name);
232
233     if (cpu->state_event) {
234       DEBUG1("Trace already configured for this CPU(%s), ignoring it", elm);
235       continue;
236     }
237     DEBUG2("Add state trace: %s to CPU(%s)", trace_name, elm);
238     cpu->state_event = tmgr_history_add_trace(history, trace, 0.0, 0, cpu);
239   }
240
241   xbt_dict_foreach(trace_connect_list_power, cursor, trace_name, elm) {
242     tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
243     cpu_ti_t cpu = surf_model_resource_by_name(surf_cpu_model, elm);
244
245     xbt_assert1(cpu, "Host %s undefined", elm);
246     xbt_assert1(trace, "Trace %s undefined", trace_name);
247
248     DEBUG2("Add power trace: %s to CPU(%s)", trace_name, elm);
249     if (cpu->avail_trace)
250       surf_cpu_free_tmgr(cpu->avail_trace);
251
252     cpu->avail_trace = cpu_ti_parse_trace(trace, cpu->power_scale);
253
254     /* add a fake trace event if periodicity == 0 */
255     if (trace && xbt_dynar_length(trace->event_list) > 1) {
256       s_tmgr_event_t val;
257       xbt_dynar_get_cpy(trace->event_list,
258                         xbt_dynar_length(trace->event_list) - 1, &val);
259       if (val.delta == 0) {
260         tmgr_trace_t empty_trace;
261         empty_trace = tmgr_empty_trace_new();
262         cpu->power_event =
263           tmgr_history_add_trace(history, empty_trace,
264                                  cpu->avail_trace->last_time, 0, cpu);
265       }
266     }
267   }
268 }
269
270 static void define_callbacks(const char *file)
271 {
272   surf_parse_reset_parser();
273   surfxml_add_callback(STag_surfxml_host_cb_list, parse_cpu_init);
274   surfxml_add_callback(ETag_surfxml_platform_cb_list, &add_traces_cpu);
275 }
276
277 static int resource_used(void *resource_id)
278 {
279   cpu_ti_t cpu = resource_id;
280   return xbt_swag_size(cpu->action_set);
281 }
282
283 static int action_unref(surf_action_t action)
284 {
285   action->refcount--;
286   if (!action->refcount) {
287     xbt_swag_remove(action, action->state_set);
288     /* remove from action_set */
289     xbt_swag_remove(action, ACTION_GET_CPU(action)->action_set);
290     /* remove from heap */
291     xbt_heap_remove(action_heap, ((surf_action_cpu_ti_t) action)->index_heap);
292     xbt_swag_insert(ACTION_GET_CPU(action), modified_cpu);
293     free(action);
294     return 1;
295   }
296   return 0;
297 }
298
299 static void action_cancel(surf_action_t action)
300 {
301   surf_action_state_set(action, SURF_ACTION_FAILED);
302   xbt_heap_remove(action_heap, ((surf_action_cpu_ti_t) action)->index_heap);
303   xbt_swag_insert(ACTION_GET_CPU(action), modified_cpu);
304   return;
305 }
306
307 static void cpu_action_state_set(surf_action_t action,
308                                  e_surf_action_state_t state)
309 {
310   surf_action_state_set(action, state);
311   xbt_swag_insert(ACTION_GET_CPU(action), modified_cpu);
312   return;
313 }
314
315 /**
316 * \brief Update the remaining amount of actions
317 *
318 * \param        cpu             Cpu on which the actions are running
319 * \param        now             Current time
320 */
321 static void cpu_update_remaining_amount(cpu_ti_t cpu, double now)
322 {
323 #define GENERIC_ACTION(action) action->generic_action
324   double area_total;
325   surf_action_cpu_ti_t action;
326
327 /* already updated */
328   if (cpu->last_update >= now)
329     return;
330
331 /* calcule the surface */
332   area_total =
333     surf_cpu_integrate_trace(cpu->avail_trace, cpu->last_update,
334                              now) * cpu->power_peak;
335   DEBUG2("Flops total: %lf, Last update %lf", area_total, cpu->last_update);
336
337   xbt_swag_foreach(action, cpu->action_set) {
338     /* action not running, skip it */
339     if (GENERIC_ACTION(action).state_set !=
340         surf_cpu_model->states.running_action_set)
341       continue;
342
343     /* bogus priority, skip it */
344     if (GENERIC_ACTION(action).priority <= 0)
345       continue;
346
347     /* action suspended, skip it */
348     if (action->suspended != 0)
349       continue;
350
351     /* action don't need update */
352     if (GENERIC_ACTION(action).start >= now)
353       continue;
354
355     /* skip action that are finishing now */
356     if (GENERIC_ACTION(action).finish >= 0
357         && GENERIC_ACTION(action).finish <= now)
358       continue;
359
360     /* update remaining */
361     double_update(&(GENERIC_ACTION(action).remains),
362                   area_total / (cpu->sum_priority *
363                                 GENERIC_ACTION(action).priority));
364     DEBUG2("Update remaining action(%p) remaining %lf", action,
365            GENERIC_ACTION(action).remains);
366   }
367   cpu->last_update = now;
368 #undef GENERIC_ACTION
369 }
370
371 /**
372 * \brief Update the finish date of action if necessary
373 *
374 * \param        cpu             Cpu on which the actions are running
375 * \param        now             Current time
376 */
377 static void cpu_update_action_finish_date(cpu_ti_t cpu, double now)
378 {
379 #define GENERIC_ACTION(action) action->generic_action
380   surf_action_cpu_ti_t action;
381   double sum_priority = 0.0, total_area, min_finish = -1;
382
383 /* update remaning amount of actions */
384   cpu_update_remaining_amount(cpu, now);
385
386   xbt_swag_foreach(action, cpu->action_set) {
387     /* action not running, skip it */
388     if (GENERIC_ACTION(action).state_set !=
389         surf_cpu_model->states.running_action_set)
390       continue;
391
392     /* bogus priority, skip it */
393     if (GENERIC_ACTION(action).priority <= 0)
394       continue;
395
396     /* action suspended, skip it */
397     if (action->suspended != 0)
398       continue;
399
400     sum_priority += 1.0 / GENERIC_ACTION(action).priority;
401   }
402   cpu->sum_priority = sum_priority;
403
404   xbt_swag_foreach(action, cpu->action_set) {
405     /* action not running, skip it */
406     if (GENERIC_ACTION(action).state_set !=
407         surf_cpu_model->states.running_action_set)
408       continue;
409
410     /* verify if the action is really running on cpu */
411     if (action->suspended == 0 && GENERIC_ACTION(action).priority > 0) {
412       /* total area needed to finish the action. Used in trace integration */
413       total_area =
414         (GENERIC_ACTION(action).remains) * sum_priority *
415         GENERIC_ACTION(action).priority;
416
417       total_area /= cpu->power_peak;
418
419       GENERIC_ACTION(action).finish =
420         surf_cpu_solve_trace(cpu->avail_trace, now, total_area);
421       /* verify which event will happen before (max_duration or finish time) */
422       if ((GENERIC_ACTION(action).max_duration != NO_MAX_DURATION) &&
423           (GENERIC_ACTION(action).start +
424            GENERIC_ACTION(action).max_duration <
425            GENERIC_ACTION(action).finish))
426         min_finish = GENERIC_ACTION(action).start +
427           GENERIC_ACTION(action).max_duration;
428       else
429         min_finish = GENERIC_ACTION(action).finish;
430     } else {
431       /* put the max duration time on heap */
432       if (GENERIC_ACTION(action).max_duration != NO_MAX_DURATION)
433         min_finish =
434           (GENERIC_ACTION(action).start +
435            GENERIC_ACTION(action).max_duration);
436     }
437     /* add in action heap */
438     DEBUG2("action(%p) index %d", action, action->index_heap);
439     if (action->index_heap >= 0) {
440       surf_action_cpu_ti_t heap_act =
441         xbt_heap_remove(action_heap, action->index_heap);
442       if (heap_act != action)
443         DIE_IMPOSSIBLE;
444     }
445     if (min_finish != NO_MAX_DURATION)
446       xbt_heap_push(action_heap, action, min_finish);
447
448     DEBUG5
449       ("Update finish time: Cpu(%s) Action: %p, Start Time: %lf Finish Time: %lf Max duration %lf",
450        cpu->generic_resource.name, action, GENERIC_ACTION(action).start,
451        GENERIC_ACTION(action).finish, GENERIC_ACTION(action).max_duration);
452   }
453 /* remove from modified cpu */
454   xbt_swag_remove(cpu, modified_cpu);
455 #undef GENERIC_ACTION
456 }
457
458 static double share_resources(double now)
459 {
460   cpu_ti_t cpu, cpu_next;
461   double min_action_duration = -1;
462
463 /* iterates over modified cpus to update share resources */
464   xbt_swag_foreach_safe(cpu, cpu_next, modified_cpu) {
465     cpu_update_action_finish_date(cpu, now);
466   }
467 /* get the min next event if heap not empty */
468   if (xbt_heap_size(action_heap) > 0)
469     min_action_duration = xbt_heap_maxkey(action_heap) - now;
470
471   DEBUG1("Share resources, min next event date: %lf", min_action_duration);
472
473   return min_action_duration;
474 }
475
476 static void update_actions_state(double now, double delta)
477 {
478 #define GENERIC_ACTION(action) action->generic_action
479   surf_action_cpu_ti_t action;
480   while ((xbt_heap_size(action_heap) > 0)
481          && (xbt_heap_maxkey(action_heap) <= now)) {
482     action = xbt_heap_pop(action_heap);
483     DEBUG1("Action %p: finish", action);
484     GENERIC_ACTION(action).finish = surf_get_clock();
485     /* set the remains to 0 due to precision problems when updating the remaining amount */
486     GENERIC_ACTION(action).remains = 0;
487     cpu_action_state_set((surf_action_t) action, SURF_ACTION_DONE);
488     /* update remaining amout of all actions */
489     cpu_update_remaining_amount(action->cpu, surf_get_clock());
490   }
491 #undef GENERIC_ACTION
492 }
493
494 static void update_resource_state(void *id,
495                                   tmgr_trace_event_t event_type,
496                                   double value, double date)
497 {
498   cpu_ti_t cpu = id;
499   surf_action_cpu_ti_t action;
500
501   if (event_type == cpu->power_event) {
502     tmgr_trace_t power_trace;
503     surf_cpu_ti_tgmr_t trace;
504     s_tmgr_event_t val;
505
506     DEBUG3("Finish trace date: %lf value %lf date %lf", surf_get_clock(),
507            value, date);
508     /* update remaining of actions and put in modified cpu swag */
509     cpu_update_remaining_amount(cpu, date);
510     xbt_swag_insert(cpu, modified_cpu);
511
512     power_trace = cpu->avail_trace->power_trace;
513     xbt_dynar_get_cpy(power_trace->event_list,
514                       xbt_dynar_length(power_trace->event_list) - 1, &val);
515     /* free old trace */
516     surf_cpu_free_tmgr(cpu->avail_trace);
517     cpu->power_scale = val.value;
518
519     trace = xbt_new0(s_surf_cpu_ti_tgmr_t, 1);
520     trace->type = TRACE_FIXED;
521     trace->value = val.value;
522     DEBUG1("value %lf", val.value);
523
524     cpu->avail_trace = trace;
525
526     if (tmgr_trace_event_free(event_type))
527       cpu->power_event = NULL;
528
529   } else if (event_type == cpu->state_event) {
530     if (value > 0)
531       cpu->state_current = SURF_RESOURCE_ON;
532     else {
533       cpu->state_current = SURF_RESOURCE_OFF;
534
535       /* put all action running on cpu to failed */
536       xbt_swag_foreach(action, cpu->action_set) {
537         if (surf_action_state_get((surf_action_t) action) ==
538             SURF_ACTION_RUNNING
539             || surf_action_state_get((surf_action_t) action) ==
540             SURF_ACTION_READY
541             || surf_action_state_get((surf_action_t) action) ==
542             SURF_ACTION_NOT_IN_THE_SYSTEM) {
543           action->generic_action.finish = date;
544           cpu_action_state_set((surf_action_t) action, SURF_ACTION_FAILED);
545           if (action->index_heap >= 0) {
546             surf_action_cpu_ti_t heap_act =
547               xbt_heap_remove(action_heap, action->index_heap);
548             if (heap_act != action)
549               DIE_IMPOSSIBLE;
550           }
551         }
552       }
553     }
554     if (tmgr_trace_event_free(event_type))
555       cpu->state_event = NULL;
556   } else {
557     CRITICAL0("Unknown event ! \n");
558     xbt_abort();
559   }
560
561   return;
562 }
563
564 static surf_action_t execute(void *cpu, double size)
565 {
566   surf_action_cpu_ti_t action = NULL;
567   cpu_ti_t CPU = cpu;
568
569   XBT_IN2("(%s,%g)", surf_resource_name(CPU), size);
570   action =
571     surf_action_new(sizeof(s_surf_action_cpu_ti_t), size, surf_cpu_model,
572                     CPU->state_current != SURF_RESOURCE_ON);
573   action->cpu = cpu;
574   action->index_heap = -1;
575
576   xbt_swag_insert(CPU, modified_cpu);
577
578   xbt_swag_insert(action, CPU->action_set);
579
580   action->suspended = 0;        /* Should be useless because of the
581                                    calloc but it seems to help valgrind... */
582
583   XBT_OUT;
584   return (surf_action_t) action;
585 }
586
587 static void action_update_index_heap(void *action, int i)
588 {
589   ((surf_action_cpu_ti_t) action)->index_heap = i;
590 }
591
592 static surf_action_t action_sleep(void *cpu, double duration)
593 {
594   surf_action_cpu_ti_t action = NULL;
595
596   if (duration > 0)
597     duration = MAX(duration, MAXMIN_PRECISION);
598
599   XBT_IN2("(%s,%g)", surf_resource_name(cpu), duration);
600   action = (surf_action_cpu_ti_t) execute(cpu, 1.0);
601   action->generic_action.max_duration = duration;
602   action->suspended = 2;
603   if (duration == NO_MAX_DURATION) {
604     /* Move to the *end* of the corresponding action set. This convention
605        is used to speed up update_resource_state  */
606     xbt_swag_remove(action, ((surf_action_t) action)->state_set);
607     ((surf_action_t) action)->state_set =
608       running_action_set_that_does_not_need_being_checked;
609     xbt_swag_insert(action, ((surf_action_t) action)->state_set);
610   }
611   XBT_OUT;
612   return (surf_action_t) action;
613 }
614
615 static void action_suspend(surf_action_t action)
616 {
617   XBT_IN1("(%p)", action);
618   if (((surf_action_cpu_ti_t) action)->suspended != 2) {
619     ((surf_action_cpu_ti_t) action)->suspended = 1;
620     xbt_swag_insert(ACTION_GET_CPU(action), modified_cpu);
621   }
622   XBT_OUT;
623 }
624
625 static void action_resume(surf_action_t action)
626 {
627   XBT_IN1("(%p)", action);
628   if (((surf_action_cpu_ti_t) action)->suspended != 2) {
629     ((surf_action_cpu_ti_t) action)->suspended = 0;
630     xbt_swag_insert(ACTION_GET_CPU(action), modified_cpu);
631   }
632   XBT_OUT;
633 }
634
635 static int action_is_suspended(surf_action_t action)
636 {
637   return (((surf_action_cpu_ti_t) action)->suspended == 1);
638 }
639
640 static void action_set_max_duration(surf_action_t action, double duration)
641 {
642   surf_action_cpu_ti_t ACT = (surf_action_cpu_ti_t) action;
643   double min_finish;
644
645   XBT_IN2("(%p,%g)", action, duration);
646
647   action->max_duration = duration;
648
649   if (duration >= 0)
650     min_finish =
651       (action->start + action->max_duration) <
652       action->finish ? (action->start +
653                         action->max_duration) : action->finish;
654   else
655     min_finish = action->finish;
656
657 /* add in action heap */
658   if (ACT->index_heap >= 0) {
659     surf_action_cpu_ti_t heap_act =
660       xbt_heap_remove(action_heap, ACT->index_heap);
661     if (heap_act != ACT)
662       DIE_IMPOSSIBLE;
663   }
664   xbt_heap_push(action_heap, ACT, min_finish);
665
666   XBT_OUT;
667 }
668
669 static void action_set_priority(surf_action_t action, double priority)
670 {
671   XBT_IN2("(%p,%g)", action, priority);
672   action->priority = priority;
673   xbt_swag_insert(ACTION_GET_CPU(action), modified_cpu);
674   XBT_OUT;
675 }
676
677 static double action_get_remains(surf_action_t action)
678 {
679   XBT_IN1("(%p)", action);
680   cpu_update_remaining_amount((cpu_ti_t) ((surf_action_cpu_ti_t) action)->cpu,
681                               surf_get_clock());
682   return action->remains;
683   XBT_OUT;
684 }
685
686 static e_surf_resource_state_t get_state(void *cpu)
687 {
688   return ((cpu_ti_t) cpu)->state_current;
689 }
690
691 static double get_speed(void *cpu, double load)
692 {
693   return load * (((cpu_ti_t) cpu)->power_peak);
694 }
695
696 /**
697 * \brief Auxiliary function to update the CPU power scale.
698 *
699 *       This function uses the trace structure to return the power scale at the determined time a.
700 * \param trace          Trace structure to search the updated power scale
701 * \param a                              Time
702 * \return CPU power scale
703 */
704 static double surf_cpu_get_power_scale(surf_cpu_ti_tgmr_t trace, double a)
705 {
706   double reduced_a;
707   int point;
708   s_tmgr_event_t val;
709
710   reduced_a = a - floor(a / trace->last_time) * trace->last_time;
711   point =
712     surf_cpu_binary_search(trace->trace->time_points, reduced_a, 0,
713                            trace->trace->nb_points - 1);
714   xbt_dynar_get_cpy(trace->power_trace->event_list, 0, &val);
715   return val.value;
716 }
717
718 static double get_available_speed(void *cpu)
719 {
720   cpu_ti_t CPU = cpu;
721   CPU->power_scale =
722     surf_cpu_get_power_scale(CPU->avail_trace, surf_get_clock());
723 /* number between 0 and 1 */
724   return CPU->power_scale;
725 }
726
727 static void finalize(void)
728 {
729   void *cpu;
730   xbt_dict_cursor_t cursor;
731   char *key;
732   xbt_dict_foreach(surf_model_resource_set(surf_cpu_model), cursor, key, cpu) {
733     cpu_ti_t CPU = cpu;
734     xbt_swag_free(CPU->action_set);
735     surf_cpu_free_tmgr(CPU->avail_trace);
736   }
737
738   surf_model_exit(surf_cpu_model);
739   surf_cpu_model = NULL;
740
741   xbt_swag_free(running_action_set_that_does_not_need_being_checked);
742   xbt_swag_free(modified_cpu);
743   running_action_set_that_does_not_need_being_checked = NULL;
744   xbt_heap_free(action_heap);
745 }
746
747 static void surf_cpu_model_init_internal(void)
748 {
749   s_surf_action_t action;
750   s_cpu_ti_t cpu;
751
752   surf_cpu_model = surf_model_init();
753
754   running_action_set_that_does_not_need_being_checked =
755     xbt_swag_new(xbt_swag_offset(action, state_hookup));
756
757   modified_cpu = xbt_swag_new(xbt_swag_offset(cpu, modified_cpu_hookup));
758
759   surf_cpu_model->name = "CPU_TI";
760
761   surf_cpu_model->action_unref = action_unref;
762   surf_cpu_model->action_cancel = action_cancel;
763   surf_cpu_model->action_state_set = cpu_action_state_set;
764
765   surf_cpu_model->model_private->resource_used = resource_used;
766   surf_cpu_model->model_private->share_resources = share_resources;
767   surf_cpu_model->model_private->update_actions_state = update_actions_state;
768   surf_cpu_model->model_private->update_resource_state =
769     update_resource_state;
770   surf_cpu_model->model_private->finalize = finalize;
771
772   surf_cpu_model->suspend = action_suspend;
773   surf_cpu_model->resume = action_resume;
774   surf_cpu_model->is_suspended = action_is_suspended;
775   surf_cpu_model->set_max_duration = action_set_max_duration;
776   surf_cpu_model->set_priority = action_set_priority;
777   surf_cpu_model->get_remains = action_get_remains;
778
779   surf_cpu_model->extension.cpu.execute = execute;
780   surf_cpu_model->extension.cpu.sleep = action_sleep;
781
782   surf_cpu_model->extension.cpu.get_state = get_state;
783   surf_cpu_model->extension.cpu.get_speed = get_speed;
784   surf_cpu_model->extension.cpu.get_available_speed = get_available_speed;
785
786   action_heap = xbt_heap_new(8, NULL);
787   xbt_heap_set_update_callback(action_heap, action_update_index_heap);
788
789 }
790
791 void surf_cpu_model_init_ti(const char *filename)
792 {
793   if (surf_cpu_model)
794     return;
795   surf_cpu_model_init_internal();
796   define_callbacks(filename);
797   xbt_dynar_push(model_list, &surf_cpu_model);
798 }
799
800
801 /**
802 * \brief Integrate trace
803 *
804 * Wrapper around surf_cpu_integrate_trace_simple() to get
805 * the cyclic effect.
806 *
807 * \param trace Trace structure.
808 * \param a                      Begin of interval
809 * \param b                      End of interval
810 * \return the integrate value. -1 if an error occurs.
811 */
812 static double surf_cpu_integrate_trace(surf_cpu_ti_tgmr_t trace, double a,
813                                        double b)
814 {
815   double first_chunk;
816   double middle_chunk;
817   double last_chunk;
818   int a_index, b_index;
819
820   if ((a < 0.0) || (a > b)) {
821     CRITICAL2
822       ("Error, invalid integration interval [%.2f,%.2f]. You probably have a task executing with negative computation amount. Check your code.",
823        a, b);
824     xbt_abort();
825   }
826   if (a == b)
827     return 0.0;
828
829   if (trace->type == TRACE_FIXED) {
830     return ((b - a) * trace->value);
831   }
832
833   if (ceil(a / trace->last_time) == a / trace->last_time)
834     a_index = 1 + (int) (ceil(a / trace->last_time));
835   else
836     a_index = (int) (ceil(a / trace->last_time));
837
838   b_index = (int) (floor(b / trace->last_time));
839
840   if (a_index > b_index) {      /* Same chunk */
841     return surf_cpu_integrate_trace_simple(trace->trace,
842                                            a - (a_index -
843                                                 1) * trace->last_time,
844                                            b - (b_index) * trace->last_time);
845   }
846
847   first_chunk = surf_cpu_integrate_trace_simple(trace->trace,
848                                                 a - (a_index -
849                                                      1) * trace->last_time,
850                                                 trace->last_time);
851   middle_chunk = (b_index - a_index) * trace->total;
852   last_chunk = surf_cpu_integrate_trace_simple(trace->trace,
853                                                0.0,
854                                                b -
855                                                (b_index) * trace->last_time);
856
857   DEBUG3("first_chunk=%.2f  middle_chunk=%.2f  last_chunk=%.2f\n",
858          first_chunk, middle_chunk, last_chunk);
859
860   return (first_chunk + middle_chunk + last_chunk);
861 }
862
863 /**
864  * \brief Auxiliary function to calculate the integral between a and b.
865  *              It simply calculates the integral at point a and b and returns the difference 
866  *      between them.
867  * \param trace         Trace structure
868  * \param a                             Initial point
869  * \param b     Final point
870  * \return      Integral
871 */
872 static double surf_cpu_integrate_trace_simple(surf_cpu_ti_trace_t trace,
873                                               double a, double b)
874 {
875   return surf_cpu_integrate_trace_simple_point(trace,
876                                                b) -
877     surf_cpu_integrate_trace_simple_point(trace, a);
878 }
879
880 /**
881  * \brief Auxiliary function to calculate the integral at point a.
882  * \param trace         Trace structure
883  * \param a                             point
884  * \return      Integral
885 */
886 static double surf_cpu_integrate_trace_simple_point(surf_cpu_ti_trace_t trace,
887                                                     double a)
888 {
889   double integral = 0;
890   int ind;
891   double a_aux = a;
892   ind =
893     surf_cpu_binary_search(trace->time_points, a, 0, trace->nb_points - 1);
894   integral += trace->integral[ind];
895   DEBUG7("a %lf ind %d integral %lf ind + 1 %lf ind %lf time +1 %lf time %lf",
896          a, ind, integral, trace->integral[ind + 1], trace->integral[ind],
897          trace->time_points[ind + 1], trace->time_points[ind]);
898   double_update(&a_aux, trace->time_points[ind]);
899   if (a_aux > 0)
900     integral +=
901       ((trace->integral[ind + 1] -
902         trace->integral[ind]) / (trace->time_points[ind + 1] -
903                                  trace->time_points[ind])) * (a -
904                                                               trace->
905                                                               time_points
906                                                               [ind]);
907   DEBUG2("Integral a %lf = %lf", a, integral);
908
909   return integral;
910 }
911
912 /**
913 * \brief Calculate the time needed to execute "amount" on cpu.
914 *
915 * Here, amount can span multiple trace periods
916 *
917 * \param trace  CPU trace structure
918 * \param a                              Initial time
919 * \param amount Amount to be executed
920 * \return       End time
921 */
922 static double surf_cpu_solve_trace(surf_cpu_ti_tgmr_t trace, double a,
923                                    double amount)
924 {
925   int quotient;
926   double reduced_b;
927   double reduced_amount;
928   double reduced_a;
929   double b;
930
931 /* Fix very small negative numbers */
932   if ((a < 0.0) && (a > -EPSILON)) {
933     a = 0.0;
934   }
935   if ((amount < 0.0) && (amount > -EPSILON)) {
936     amount = 0.0;
937   }
938
939 /* Sanity checks */
940   if ((a < 0.0) || (amount < 0.0)) {
941     CRITICAL2
942       ("Error, invalid parameters [a = %.2f, amount = %.2f]. You probably have a task executing with negative computation amount. Check your code.",
943        a, amount);
944     xbt_abort();
945   }
946
947 /* At this point, a and amount are positive */
948
949   if (amount < EPSILON)
950     return a;
951
952 /* Is the trace fixed ? */
953   if (trace->type == TRACE_FIXED) {
954     return (a + (amount / trace->value));
955   }
956
957   DEBUG2("amount %lf total %lf", amount, trace->total);
958 /* Reduce the problem to one where amount <= trace_total */
959   quotient = (int) (floor(amount / trace->total));
960   reduced_amount = (trace->total) * ((amount / trace->total) -
961                                      floor(amount / trace->total));
962   reduced_a = a - (trace->last_time) * (int) (floor(a / trace->last_time));
963
964   DEBUG3("Quotient: %d reduced_amount: %lf reduced_a: %lf", quotient,
965          reduced_amount, reduced_a);
966
967 /* Now solve for new_amount which is <= trace_total */
968 /*
969          fprintf(stderr,"reduced_a = %.2f\n",reduced_a);
970          fprintf(stderr,"reduced_amount = %.2f\n",reduced_amount);
971  */
972   reduced_b =
973     surf_cpu_solve_trace_somewhat_simple(trace, reduced_a, reduced_amount);
974
975 /* Re-map to the original b and amount */
976   b = (trace->last_time) * (int) (floor(a / trace->last_time)) +
977     (quotient * trace->last_time) + reduced_b;
978   return b;
979 }
980
981 /**
982 * \brief Auxiliary function to solve integral
983 *
984 * Here, amount is <= trace->total
985 * and a <=trace->last_time
986 *
987 */
988 static double surf_cpu_solve_trace_somewhat_simple(surf_cpu_ti_tgmr_t trace,
989                                                    double a, double amount)
990 {
991   double amount_till_end;
992   double b;
993
994   DEBUG2("Solve integral: [%.2f, amount=%.2f]", a, amount);
995   amount_till_end = surf_cpu_integrate_trace(trace, a, trace->last_time);
996 /*
997          fprintf(stderr,"amount_till_end=%.2f\n",amount_till_end);
998  */
999
1000   if (amount_till_end > amount) {
1001     b = surf_cpu_solve_trace_simple(trace->trace, a, amount);
1002   } else {
1003     b = trace->last_time +
1004       surf_cpu_solve_trace_simple(trace->trace, 0.0,
1005                                   amount - amount_till_end);
1006   }
1007   return b;
1008 }
1009
1010 /**
1011  * \brief Auxiliary function to solve integral.
1012  *      It returns the date when the requested amount of flops is available
1013  * \param trace         Trace structure
1014  * \param a                             Initial point
1015  * \param amount        Amount of flops 
1016  * \return The date when amount is available.
1017 */
1018 static double surf_cpu_solve_trace_simple(surf_cpu_ti_trace_t trace, double a,
1019                                           double amount)
1020 {
1021   double integral_a;
1022   int ind;
1023   double time;
1024   integral_a = surf_cpu_integrate_trace_simple_point(trace, a);
1025   ind =
1026     surf_cpu_binary_search(trace->integral, integral_a + amount, 0,
1027                            trace->nb_points - 1);
1028   time = trace->time_points[ind];
1029   time +=
1030     (integral_a + amount -
1031      trace->integral[ind]) / ((trace->integral[ind + 1] -
1032                                trace->integral[ind]) /
1033                               (trace->time_points[ind + 1] -
1034                                trace->time_points[ind]));
1035
1036   return time;
1037 }
1038
1039 /**
1040  * \brief Binary search in array.
1041  *      It returns the first point of the interval in which "a" is. 
1042  * \param array         Array
1043  * \param a                             Value to search
1044  * \param low           Low bound to search in array
1045  * \param high          Upper bound to search in array
1046  * \return Index of point
1047 */
1048 static int surf_cpu_binary_search(double *array, double a, int low, int high)
1049 {
1050   int mid = low + (high - low) / 2;
1051   DEBUG5("a %lf low %d high %d mid %d value %lf", a, low, high, mid,
1052          array[mid]);
1053   /* a == array[mid] */
1054   if (array[mid] == a)
1055     return mid;
1056   /* a is between mid and mid+1 */
1057   if (array[mid] < a && array[mid + 1] > a)
1058     return mid;
1059
1060   if (array[mid] < a)
1061     return surf_cpu_binary_search(array, a, mid + 1, high);
1062   else
1063     return surf_cpu_binary_search(array, a, low, mid - 1);
1064 }