2 /* Copyright (c) 2009, 2010. The SimGrid Team.
3 * All rights reserved. */
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. */
9 commit: e2d6799c4182f00443b3013aadb1c2412372460f
10 This commit retrieves the old implementation of CPU_TI with multi-levels.
13 #include "surf_private.h"
14 #include "trace_mgr_private.h"
15 #include "cpu_ti_private.h"
17 #include "surf/surf_resource.h"
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_cpu_ti, surf,
20 "Logging specific to the SURF CPU TRACE INTEGRATION module");
24 cpu_ti_running_action_set_that_does_not_need_being_checked = NULL;
25 static xbt_swag_t cpu_ti_modified_cpu = NULL;
26 static xbt_heap_t cpu_ti_action_heap;
28 /* prototypes of new trace functions */
29 static double surf_cpu_ti_integrate_trace(surf_cpu_ti_tgmr_t trace,
33 static double surf_cpu_ti_solve_trace(surf_cpu_ti_tgmr_t trace, double a,
35 static double surf_cpu_ti_solve_trace_somewhat_simple(surf_cpu_ti_tgmr_t
39 static void surf_cpu_ti_free_tmgr(surf_cpu_ti_tgmr_t trace);
41 static double surf_cpu_ti_integrate_trace_simple(surf_cpu_ti_trace_t trace,
43 static double surf_cpu_ti_integrate_trace_simple_point(surf_cpu_ti_trace_t
45 static double surf_cpu_ti_solve_trace_simple(surf_cpu_ti_trace_t trace,
46 double a, double amount);
47 static int surf_cpu_ti_binary_search(double *array, double a, int low,
51 static void surf_cpu_ti_free_trace(surf_cpu_ti_trace_t trace)
53 xbt_free(trace->time_points);
54 xbt_free(trace->integral);
58 static void surf_cpu_ti_free_tmgr(surf_cpu_ti_tgmr_t trace)
61 surf_cpu_ti_free_trace(trace->trace);
65 static surf_cpu_ti_trace_t surf_cpu_ti_trace_new(tmgr_trace_t power_trace)
67 surf_cpu_ti_trace_t trace;
73 trace = xbt_new0(s_surf_cpu_ti_trace_t, 1);
75 xbt_malloc0(sizeof(double) *
76 (xbt_dynar_length(power_trace->s_list.event_list) + 1));
78 xbt_malloc0(sizeof(double) *
79 (xbt_dynar_length(power_trace->s_list.event_list) + 1));
80 trace->nb_points = xbt_dynar_length(power_trace->s_list.event_list);
81 xbt_dynar_foreach(power_trace->s_list.event_list, cpt, val) {
82 trace->time_points[i] = time;
83 trace->integral[i] = integral;
84 integral += val.delta * val.value;
88 trace->time_points[i] = time;
89 trace->integral[i] = integral;
94 * \brief Creates a new integration trace from a tmgr_trace_t
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
101 static surf_cpu_ti_tgmr_t cpu_ti_parse_trace(tmgr_trace_t power_trace,
104 surf_cpu_ti_tgmr_t trace;
105 double total_time = 0.0;
108 trace = xbt_new0(s_surf_cpu_ti_tgmr_t, 1);
110 /* no availability file, fixed trace */
112 trace->type = TRACE_FIXED;
113 trace->value = value;
114 XBT_DEBUG("No availabily trace. Constant value = %lf", value);
118 /* only one point available, fixed trace */
119 if (xbt_dynar_length(power_trace->s_list.event_list) == 1) {
120 xbt_dynar_get_cpy(power_trace->s_list.event_list, 0, &val);
121 trace->type = TRACE_FIXED;
122 trace->value = val.value;
126 trace->type = TRACE_DYNAMIC;
127 trace->power_trace = power_trace;
129 /* count the total time of trace file */
130 xbt_dynar_foreach(power_trace->s_list.event_list, cpt, val) {
131 total_time += val.delta;
133 trace->trace = surf_cpu_ti_trace_new(power_trace);
134 trace->last_time = total_time;
136 surf_cpu_ti_integrate_trace_simple(trace->trace, 0, total_time);
138 XBT_DEBUG("Total integral %lf, last_time %lf ",
139 trace->total, trace->last_time);
145 static void* cpu_ti_create_resource(const char *name, double power_peak,
147 tmgr_trace_t power_trace,
149 e_surf_resource_state_t state_initial,
150 tmgr_trace_t state_trace,
151 xbt_dict_t cpu_properties)
153 tmgr_trace_t empty_trace;
156 s_surf_action_cpu_ti_t ti_action;
157 xbt_assert(core==1,"Multi-core not handled with this model yet");
158 xbt_assert(!surf_cpu_resource_priv(surf_cpu_resource_by_name(name)),
159 "Host '%s' declared several times in the platform file",
161 xbt_assert(core==1,"Multi-core not handled with this model yet");
162 cpu = (cpu_ti_t) surf_resource_new(sizeof(s_cpu_ti_t),
163 surf_cpu_model, name,cpu_properties);
165 xbt_swag_new(xbt_swag_offset(ti_action, cpu_list_hookup));
166 cpu->power_peak = power_peak;
167 xbt_assert(cpu->power_peak > 0, "Power has to be >0");
168 XBT_DEBUG("power scale %lf", power_scale);
169 cpu->power_scale = power_scale;
170 cpu->avail_trace = cpu_ti_parse_trace(power_trace, power_scale);
171 cpu->state_current = state_initial;
174 tmgr_history_add_trace(history, state_trace, 0.0, 0, cpu);
175 if (power_trace && xbt_dynar_length(power_trace->s_list.event_list) > 1) {
176 /* add a fake trace event if periodicity == 0 */
177 xbt_dynar_get_cpy(power_trace->s_list.event_list,
178 xbt_dynar_length(power_trace->s_list.event_list) - 1, &val);
179 if (val.delta == 0) {
180 empty_trace = tmgr_empty_trace_new();
182 tmgr_history_add_trace(history, empty_trace,
183 cpu->avail_trace->last_time, 0, cpu);
186 xbt_lib_set(host_lib, name, SURF_CPU_LEVEL, cpu);
188 return xbt_lib_get_elm_or_null(host_lib, name);
192 static void parse_cpu_ti_init(sg_platf_host_cbarg_t host)
194 cpu_ti_create_resource(host->id,
205 static void add_traces_cpu_ti(void)
207 xbt_dict_cursor_t cursor = NULL;
208 char *trace_name, *elm;
210 static int called = 0;
216 /* connect all traces relative to hosts */
217 xbt_dict_foreach(trace_connect_list_host_avail, cursor, trace_name, elm) {
218 tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
219 cpu_ti_t cpu = surf_cpu_resource_priv(surf_cpu_resource_by_name(elm));
221 xbt_assert(cpu, "Host %s undefined", elm);
222 xbt_assert(trace, "Trace %s undefined", trace_name);
224 if (cpu->state_event) {
225 XBT_DEBUG("Trace already configured for this CPU(%s), ignoring it",
229 XBT_DEBUG("Add state trace: %s to CPU(%s)", trace_name, elm);
230 cpu->state_event = tmgr_history_add_trace(history, trace, 0.0, 0, cpu);
233 xbt_dict_foreach(trace_connect_list_power, cursor, trace_name, elm) {
234 tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
235 cpu_ti_t cpu = surf_cpu_resource_priv(surf_cpu_resource_by_name(elm));
237 xbt_assert(cpu, "Host %s undefined", elm);
238 xbt_assert(trace, "Trace %s undefined", trace_name);
240 XBT_DEBUG("Add power trace: %s to CPU(%s)", trace_name, elm);
241 if (cpu->avail_trace)
242 surf_cpu_ti_free_tmgr(cpu->avail_trace);
244 cpu->avail_trace = cpu_ti_parse_trace(trace, cpu->power_scale);
246 /* add a fake trace event if periodicity == 0 */
247 if (trace && xbt_dynar_length(trace->s_list.event_list) > 1) {
249 xbt_dynar_get_cpy(trace->s_list.event_list,
250 xbt_dynar_length(trace->s_list.event_list) - 1, &val);
251 if (val.delta == 0) {
252 tmgr_trace_t empty_trace;
253 empty_trace = tmgr_empty_trace_new();
255 tmgr_history_add_trace(history, empty_trace,
256 cpu->avail_trace->last_time, 0, cpu);
262 static void cpu_ti_define_callbacks()
264 sg_platf_host_add_cb(parse_cpu_ti_init);
265 sg_platf_postparse_add_cb(add_traces_cpu_ti);
268 static int cpu_ti_resource_used(void *resource_id)
270 cpu_ti_t cpu = resource_id;
271 return xbt_swag_size(cpu->action_set);
274 static int cpu_ti_action_unref(surf_action_t action)
277 if (!action->refcount) {
278 xbt_swag_remove(action, action->state_set);
279 /* remove from action_set */
280 xbt_swag_remove(action, ((cpu_ti_t)surf_cpu_resource_priv(ACTION_GET_CPU(action)))->action_set);
281 /* remove from heap */
282 xbt_heap_remove(cpu_ti_action_heap,
283 ((surf_action_cpu_ti_t) action)->index_heap);
284 xbt_swag_insert(((cpu_ti_t)surf_cpu_resource_priv(ACTION_GET_CPU(action))), cpu_ti_modified_cpu);
285 surf_action_free(&action);
291 static void cpu_ti_action_cancel(surf_action_t action)
293 surf_action_state_set(action, SURF_ACTION_FAILED);
294 xbt_heap_remove(cpu_ti_action_heap,
295 ((surf_action_cpu_ti_t) action)->index_heap);
296 xbt_swag_insert(surf_cpu_resource_priv(ACTION_GET_CPU(action)), cpu_ti_modified_cpu);
300 static void cpu_ti_action_state_set(surf_action_t action,
301 e_surf_action_state_t state)
303 surf_action_state_set(action, state);
304 xbt_swag_insert(surf_cpu_resource_priv(ACTION_GET_CPU(action)), cpu_ti_modified_cpu);
309 * \brief Update the remaining amount of actions
311 * \param cpu Cpu on which the actions are running
312 * \param now Current time
314 static void cpu_ti_update_remaining_amount(surf_model_t cpu_model, cpu_ti_t cpu, double now)
317 surf_action_cpu_ti_t action;
319 /* already updated */
320 if (cpu->last_update >= now)
323 /* calcule the surface */
325 surf_cpu_ti_integrate_trace(cpu->avail_trace, cpu->last_update,
326 now) * cpu->power_peak;
327 XBT_DEBUG("Flops total: %lf, Last update %lf", area_total,
330 xbt_swag_foreach(action, cpu->action_set) {
331 surf_action_t generic = (surf_action_t)action;
332 /* action not running, skip it */
333 if (generic->state_set !=
334 cpu_model->states.running_action_set)
337 /* bogus priority, skip it */
338 if (generic->priority <= 0)
341 /* action suspended, skip it */
342 if (action->suspended != 0)
345 /* action don't need update */
346 if (generic->start >= now)
349 /* skip action that are finishing now */
350 if (generic->finish >= 0
351 && generic->finish <= now)
354 /* update remaining */
355 double_update(&(generic->remains),
356 area_total / (cpu->sum_priority *
358 XBT_DEBUG("Update remaining action(%p) remaining %lf", action,
361 cpu->last_update = now;
362 #undef GENERIC_ACTION
366 * \brief Update the finish date of action if necessary
368 * \param cpu Cpu on which the actions are running
369 * \param now Current time
371 static void cpu_ti_update_action_finish_date(surf_model_t cpu_model, cpu_ti_t cpu, double now)
373 #define GENERIC_ACTION(action) action->generic_action
374 surf_action_cpu_ti_t action;
375 double sum_priority = 0.0, total_area, min_finish = -1;
377 /* update remaning amount of actions */
378 cpu_ti_update_remaining_amount(cpu_model, cpu, now);
380 xbt_swag_foreach(action, cpu->action_set) {
381 /* action not running, skip it */
382 if (GENERIC_ACTION(action).state_set !=
383 cpu_model->states.running_action_set)
386 /* bogus priority, skip it */
387 if (GENERIC_ACTION(action).priority <= 0)
390 /* action suspended, skip it */
391 if (action->suspended != 0)
394 sum_priority += 1.0 / GENERIC_ACTION(action).priority;
396 cpu->sum_priority = sum_priority;
398 xbt_swag_foreach(action, cpu->action_set) {
400 /* action not running, skip it */
401 if (GENERIC_ACTION(action).state_set !=
402 cpu_model->states.running_action_set)
405 /* verify if the action is really running on cpu */
406 if (action->suspended == 0 && GENERIC_ACTION(action).priority > 0) {
407 /* total area needed to finish the action. Used in trace integration */
409 (GENERIC_ACTION(action).remains) * sum_priority *
410 GENERIC_ACTION(action).priority;
412 total_area /= cpu->power_peak;
414 GENERIC_ACTION(action).finish =
415 surf_cpu_ti_solve_trace(cpu->avail_trace, now, total_area);
416 /* verify which event will happen before (max_duration or finish time) */
417 if ((GENERIC_ACTION(action).max_duration != NO_MAX_DURATION) &&
418 (GENERIC_ACTION(action).start +
419 GENERIC_ACTION(action).max_duration <
420 GENERIC_ACTION(action).finish))
421 min_finish = GENERIC_ACTION(action).start +
422 GENERIC_ACTION(action).max_duration;
424 min_finish = GENERIC_ACTION(action).finish;
426 /* put the max duration time on heap */
427 if (GENERIC_ACTION(action).max_duration != NO_MAX_DURATION)
429 (GENERIC_ACTION(action).start +
430 GENERIC_ACTION(action).max_duration);
432 /* add in action heap */
433 XBT_DEBUG("action(%p) index %d", action, action->index_heap);
434 if (action->index_heap >= 0) {
435 surf_action_cpu_ti_t heap_act =
436 xbt_heap_remove(cpu_ti_action_heap, action->index_heap);
437 if (heap_act != action)
440 if (min_finish != NO_MAX_DURATION)
441 xbt_heap_push(cpu_ti_action_heap, action, min_finish);
444 ("Update finish time: Cpu(%s) Action: %p, Start Time: %lf Finish Time: %lf Max duration %lf",
445 cpu->generic_resource.name, action, GENERIC_ACTION(action).start,
446 GENERIC_ACTION(action).finish,
447 GENERIC_ACTION(action).max_duration);
449 /* remove from modified cpu */
450 xbt_swag_remove(cpu, cpu_ti_modified_cpu);
451 #undef GENERIC_ACTION
454 static double cpu_ti_share_resources(surf_model_t cpu_model, double now)
456 cpu_ti_t cpu, cpu_next;
457 double min_action_duration = -1;
459 /* iterates over modified cpus to update share resources */
460 xbt_swag_foreach_safe(cpu, cpu_next, cpu_ti_modified_cpu) {
461 /* FIXME: cpu_ti_modified_cpu is a global object. But, now we have multiple
462 * cpu_model objects in the system. Do we have to create this
463 * swag for each cpu model object?
465 * We should revisit here after we know what cpu_ti is.
467 cpu_ti_update_action_finish_date(cpu_model, cpu, now);
469 /* get the min next event if heap not empty */
470 if (xbt_heap_size(cpu_ti_action_heap) > 0)
471 min_action_duration = xbt_heap_maxkey(cpu_ti_action_heap) - now;
473 XBT_DEBUG("Share resources, min next event date: %lf", min_action_duration);
475 return min_action_duration;
478 static void cpu_ti_update_actions_state(surf_model_t cpu_model, double now, double delta)
480 /* FIXME: cpu_ti_action_heap is global. Is this okay for VM support? */
482 #define GENERIC_ACTION(action) action->generic_action
483 surf_action_cpu_ti_t action;
484 while ((xbt_heap_size(cpu_ti_action_heap) > 0)
485 && (xbt_heap_maxkey(cpu_ti_action_heap) <= now)) {
486 action = xbt_heap_pop(cpu_ti_action_heap);
487 XBT_DEBUG("Action %p: finish", action);
488 GENERIC_ACTION(action).finish = surf_get_clock();
489 /* set the remains to 0 due to precision problems when updating the remaining amount */
490 GENERIC_ACTION(action).remains = 0;
491 cpu_ti_action_state_set((surf_action_t) action, SURF_ACTION_DONE);
492 /* update remaining amout of all actions */
493 cpu_ti_update_remaining_amount(cpu_model, surf_cpu_resource_priv(action->cpu), surf_get_clock());
495 #undef GENERIC_ACTION
498 static void cpu_ti_update_resource_state(void *id,
499 tmgr_trace_event_t event_type,
500 double value, double date)
503 surf_action_cpu_ti_t action;
505 surf_watched_hosts();
507 if (event_type == cpu->power_event) {
508 tmgr_trace_t power_trace;
509 surf_cpu_ti_tgmr_t trace;
512 XBT_DEBUG("Finish trace date: %lf value %lf date %lf", surf_get_clock(),
514 /* update remaining of actions and put in modified cpu swag */
515 cpu_ti_update_remaining_amount(cpu, date);
516 xbt_swag_insert(cpu, cpu_ti_modified_cpu);
518 power_trace = cpu->avail_trace->power_trace;
519 xbt_dynar_get_cpy(power_trace->s_list.event_list,
520 xbt_dynar_length(power_trace->s_list.event_list) - 1, &val);
522 surf_cpu_ti_free_tmgr(cpu->avail_trace);
523 cpu->power_scale = val.value;
525 trace = xbt_new0(s_surf_cpu_ti_tgmr_t, 1);
526 trace->type = TRACE_FIXED;
527 trace->value = val.value;
528 XBT_DEBUG("value %lf", val.value);
530 cpu->avail_trace = trace;
532 if (tmgr_trace_event_free(event_type))
533 cpu->power_event = NULL;
535 } else if (event_type == cpu->state_event) {
537 cpu->state_current = SURF_RESOURCE_ON;
539 cpu->state_current = SURF_RESOURCE_OFF;
541 /* put all action running on cpu to failed */
542 xbt_swag_foreach(action, cpu->action_set) {
543 if (surf_action_state_get((surf_action_t) action) ==
545 || surf_action_state_get((surf_action_t) action) ==
547 || surf_action_state_get((surf_action_t) action) ==
548 SURF_ACTION_NOT_IN_THE_SYSTEM) {
549 action->generic_action.finish = date;
550 cpu_ti_action_state_set((surf_action_t) action,
552 if (action->index_heap >= 0) {
553 surf_action_cpu_ti_t heap_act =
554 xbt_heap_remove(cpu_ti_action_heap, action->index_heap);
555 if (heap_act != action)
561 if (tmgr_trace_event_free(event_type))
562 cpu->state_event = NULL;
564 XBT_CRITICAL("Unknown event ! \n");
571 static surf_action_t cpu_ti_execute(void *cpu, double size)
573 surf_action_cpu_ti_t action = NULL;
574 cpu_ti_t CPU = surf_cpu_resource_priv(cpu);
576 XBT_IN("(%s,%g)", surf_resource_name(CPU), size);
578 surf_action_new(sizeof(s_surf_action_cpu_ti_t), size, surf_cpu_model,
579 CPU->state_current != SURF_RESOURCE_ON);
581 action->index_heap = -1;
583 xbt_swag_insert(CPU, cpu_ti_modified_cpu);
585 xbt_swag_insert(action, CPU->action_set);
587 action->suspended = 0; /* Should be useless because of the
588 calloc but it seems to help valgrind... */
591 return (surf_action_t) action;
594 static void cpu_ti_action_update_index_heap(void *action, int i)
596 ((surf_action_cpu_ti_t) action)->index_heap = i;
599 static surf_action_t cpu_ti_action_sleep(void *cpu, double duration)
601 surf_action_cpu_ti_t action = NULL;
604 duration = MAX(duration, MAXMIN_PRECISION);
606 XBT_IN("(%s,%g)", surf_resource_name(surf_cpu_resource_priv(cpu)), duration);
607 action = (surf_action_cpu_ti_t) cpu_ti_execute(cpu, 1.0);
608 action->generic_action.max_duration = duration;
609 action->suspended = 2;
610 if (duration == NO_MAX_DURATION) {
611 /* Move to the *end* of the corresponding action set. This convention
612 is used to speed up update_resource_state */
613 xbt_swag_remove(action, ((surf_action_t) action)->state_set);
614 ((surf_action_t) action)->state_set =
615 cpu_ti_running_action_set_that_does_not_need_being_checked;
616 xbt_swag_insert(action, ((surf_action_t) action)->state_set);
619 return (surf_action_t) action;
622 static void cpu_ti_action_suspend(surf_action_t action)
624 XBT_IN("(%p)", action);
625 if (((surf_action_cpu_ti_t) action)->suspended != 2) {
626 ((surf_action_cpu_ti_t) action)->suspended = 1;
627 xbt_heap_remove(cpu_ti_action_heap,
628 ((surf_action_cpu_ti_t) action)->index_heap);
629 xbt_swag_insert(surf_cpu_resource_priv(ACTION_GET_CPU(action)), cpu_ti_modified_cpu);
634 static void cpu_ti_action_resume(surf_action_t action)
636 XBT_IN("(%p)", action);
637 if (((surf_action_cpu_ti_t) action)->suspended != 2) {
638 ((surf_action_cpu_ti_t) action)->suspended = 0;
639 xbt_swag_insert(surf_cpu_resource_priv(ACTION_GET_CPU(action)), cpu_ti_modified_cpu);
644 static int cpu_ti_action_is_suspended(surf_action_t action)
646 return (((surf_action_cpu_ti_t) action)->suspended == 1);
649 static void cpu_ti_action_set_max_duration(surf_action_t action,
652 surf_action_cpu_ti_t ACT = (surf_action_cpu_ti_t) action;
655 XBT_IN("(%p,%g)", action, duration);
657 action->max_duration = duration;
661 (action->start + action->max_duration) <
662 action->finish ? (action->start +
663 action->max_duration) : action->finish;
665 min_finish = action->finish;
667 /* add in action heap */
668 if (ACT->index_heap >= 0) {
669 surf_action_cpu_ti_t heap_act =
670 xbt_heap_remove(cpu_ti_action_heap, ACT->index_heap);
674 xbt_heap_push(cpu_ti_action_heap, ACT, min_finish);
679 static void cpu_ti_action_set_priority(surf_action_t action,
682 XBT_IN("(%p,%g)", action, priority);
683 action->priority = priority;
684 xbt_swag_insert(surf_cpu_resource_priv(ACTION_GET_CPU(action)), cpu_ti_modified_cpu);
688 static double cpu_ti_action_get_remains(surf_action_t action)
690 XBT_IN("(%p)", action);
691 cpu_ti_update_remaining_amount((cpu_ti_t)
692 ((surf_action_cpu_ti_t) action)->cpu,
695 return action->remains;
698 static e_surf_resource_state_t cpu_ti_get_state(void *cpu)
700 return ((cpu_ti_t)surf_cpu_resource_priv(cpu))->state_current;
703 static double cpu_ti_get_speed(void *cpu, double load)
705 return load * ((cpu_ti_t)surf_cpu_resource_priv(cpu))->power_peak;
709 * \brief Auxiliary function to update the CPU power scale.
711 * This function uses the trace structure to return the power scale at the determined time a.
712 * \param trace Trace structure to search the updated power scale
714 * \return CPU power scale
716 static double surf_cpu_ti_get_power_scale(surf_cpu_ti_tgmr_t trace,
723 reduced_a = a - floor(a / trace->last_time) * trace->last_time;
725 surf_cpu_ti_binary_search(trace->trace->time_points, reduced_a, 0,
726 trace->trace->nb_points - 1);
727 xbt_dynar_get_cpy(trace->power_trace->s_list.event_list, point, &val);
731 static double cpu_ti_get_available_speed(void *cpu)
733 cpu_ti_t CPU = surf_cpu_resource_priv(cpu);
735 surf_cpu_ti_get_power_scale(CPU->avail_trace, surf_get_clock());
736 /* number between 0 and 1 */
737 return CPU->power_scale;
740 static void cpu_ti_finalize(surf_model_t cpu_model)
743 xbt_lib_cursor_t cursor;
746 /* FIXME: we should update this code for VM support */
749 xbt_lib_foreach(host_lib, cursor, key, cpu){
750 if(cpu[SURF_CPU_LEVEL])
752 cpu_ti_t CPU = cpu[SURF_CPU_LEVEL];
753 xbt_swag_free(CPU->action_set);
754 surf_cpu_ti_free_tmgr(CPU->avail_trace);
758 surf_model_exit(cpu_model);
762 (cpu_ti_running_action_set_that_does_not_need_being_checked);
763 xbt_swag_free(cpu_ti_modified_cpu);
764 cpu_ti_running_action_set_that_does_not_need_being_checked = NULL;
765 xbt_heap_free(cpu_ti_action_heap);
768 static void surf_cpu_ti_model_init_internal(void)
770 s_surf_action_t action;
773 surf_cpu_model = surf_model_init();
775 cpu_ti_running_action_set_that_does_not_need_being_checked =
776 xbt_swag_new(xbt_swag_offset(action, state_hookup));
778 cpu_ti_modified_cpu =
779 xbt_swag_new(xbt_swag_offset(cpu, modified_cpu_hookup));
781 surf_cpu_model->name = "cpu_ti";
783 surf_cpu_model->action_unref = cpu_ti_action_unref;
784 surf_cpu_model->action_cancel = cpu_ti_action_cancel;
785 surf_cpu_model->action_state_set = cpu_ti_action_state_set;
787 surf_cpu_model->model_private->resource_used = cpu_ti_resource_used;
788 surf_cpu_model->model_private->share_resources = cpu_ti_share_resources;
789 surf_cpu_model->model_private->update_actions_state =
790 cpu_ti_update_actions_state;
791 surf_cpu_model->model_private->update_resource_state =
792 cpu_ti_update_resource_state;
793 surf_cpu_model->model_private->finalize = cpu_ti_finalize;
795 surf_cpu_model->suspend = cpu_ti_action_suspend;
796 surf_cpu_model->resume = cpu_ti_action_resume;
797 surf_cpu_model->is_suspended = cpu_ti_action_is_suspended;
798 surf_cpu_model->set_max_duration = cpu_ti_action_set_max_duration;
799 surf_cpu_model->set_priority = cpu_ti_action_set_priority;
800 surf_cpu_model->get_remains = cpu_ti_action_get_remains;
802 surf_cpu_model->extension.cpu.execute = cpu_ti_execute;
803 surf_cpu_model->extension.cpu.sleep = cpu_ti_action_sleep;
805 surf_cpu_model->extension.cpu.get_state = cpu_ti_get_state;
806 surf_cpu_model->extension.cpu.get_speed = cpu_ti_get_speed;
807 surf_cpu_model->extension.cpu.get_available_speed =
808 cpu_ti_get_available_speed;
809 surf_cpu_model->extension.cpu.add_traces = add_traces_cpu_ti;
811 cpu_ti_action_heap = xbt_heap_new(8, NULL);
812 xbt_heap_set_update_callback(cpu_ti_action_heap,
813 cpu_ti_action_update_index_heap);
817 void surf_cpu_model_init_ti()
819 xbt_assert(!surf_cpu_model,"CPU model already initialized. This should not happen.");
820 surf_cpu_ti_model_init_internal();
821 cpu_ti_define_callbacks();
822 xbt_dynar_push(model_list, &surf_cpu_model);
827 * \brief Integrate trace
829 * Wrapper around surf_cpu_integrate_trace_simple() to get
832 * \param trace Trace structure.
833 * \param a Begin of interval
834 * \param b End of interval
835 * \return the integrate value. -1 if an error occurs.
837 static double surf_cpu_ti_integrate_trace(surf_cpu_ti_tgmr_t trace,
843 int a_index, b_index;
845 if ((a < 0.0) || (a > b)) {
847 ("Error, invalid integration interval [%.2f,%.2f]. You probably have a task executing with negative computation amount. Check your code.",
854 if (trace->type == TRACE_FIXED) {
855 return ((b - a) * trace->value);
858 if (ceil(a / trace->last_time) == a / trace->last_time)
859 a_index = 1 + (int) (ceil(a / trace->last_time));
861 a_index = (int) (ceil(a / trace->last_time));
863 b_index = (int) (floor(b / trace->last_time));
865 if (a_index > b_index) { /* Same chunk */
866 return surf_cpu_ti_integrate_trace_simple(trace->trace,
868 1) * trace->last_time,
874 first_chunk = surf_cpu_ti_integrate_trace_simple(trace->trace,
879 middle_chunk = (b_index - a_index) * trace->total;
880 last_chunk = surf_cpu_ti_integrate_trace_simple(trace->trace,
886 XBT_DEBUG("first_chunk=%.2f middle_chunk=%.2f last_chunk=%.2f\n",
887 first_chunk, middle_chunk, last_chunk);
889 return (first_chunk + middle_chunk + last_chunk);
893 * \brief Auxiliary function to calculate the integral between a and b.
894 * It simply calculates the integral at point a and b and returns the difference
896 * \param trace Trace structure
897 * \param a Initial point
898 * \param b Final point
901 static double surf_cpu_ti_integrate_trace_simple(surf_cpu_ti_trace_t trace,
904 return surf_cpu_ti_integrate_trace_simple_point(trace,
906 surf_cpu_ti_integrate_trace_simple_point(trace, a);
910 * \brief Auxiliary function to calculate the integral at point a.
911 * \param trace Trace structure
915 static double surf_cpu_ti_integrate_trace_simple_point(surf_cpu_ti_trace_t
922 surf_cpu_ti_binary_search(trace->time_points, a, 0,
923 trace->nb_points - 1);
924 integral += trace->integral[ind];
926 ("a %lf ind %d integral %lf ind + 1 %lf ind %lf time +1 %lf time %lf",
927 a, ind, integral, trace->integral[ind + 1], trace->integral[ind],
928 trace->time_points[ind + 1], trace->time_points[ind]);
929 double_update(&a_aux, trace->time_points[ind]);
932 ((trace->integral[ind + 1] -
933 trace->integral[ind]) / (trace->time_points[ind + 1] -
934 trace->time_points[ind])) * (a -
938 XBT_DEBUG("Integral a %lf = %lf", a, integral);
944 * \brief Calculate the time needed to execute "amount" on cpu.
946 * Here, amount can span multiple trace periods
948 * \param trace CPU trace structure
949 * \param a Initial time
950 * \param amount Amount to be executed
953 static double surf_cpu_ti_solve_trace(surf_cpu_ti_tgmr_t trace, double a,
958 double reduced_amount;
962 /* Fix very small negative numbers */
963 if ((a < 0.0) && (a > -EPSILON)) {
966 if ((amount < 0.0) && (amount > -EPSILON)) {
971 if ((a < 0.0) || (amount < 0.0)) {
973 ("Error, invalid parameters [a = %.2f, amount = %.2f]. You probably have a task executing with negative computation amount. Check your code.",
978 /* At this point, a and amount are positive */
980 if (amount < EPSILON)
983 /* Is the trace fixed ? */
984 if (trace->type == TRACE_FIXED) {
985 return (a + (amount / trace->value));
988 XBT_DEBUG("amount %lf total %lf", amount, trace->total);
989 /* Reduce the problem to one where amount <= trace_total */
990 quotient = (int) (floor(amount / trace->total));
991 reduced_amount = (trace->total) * ((amount / trace->total) -
992 floor(amount / trace->total));
993 reduced_a = a - (trace->last_time) * (int) (floor(a / trace->last_time));
995 XBT_DEBUG("Quotient: %d reduced_amount: %lf reduced_a: %lf", quotient,
996 reduced_amount, reduced_a);
998 /* Now solve for new_amount which is <= trace_total */
1000 fprintf(stderr,"reduced_a = %.2f\n",reduced_a);
1001 fprintf(stderr,"reduced_amount = %.2f\n",reduced_amount);
1004 surf_cpu_ti_solve_trace_somewhat_simple(trace, reduced_a,
1007 /* Re-map to the original b and amount */
1008 b = (trace->last_time) * (int) (floor(a / trace->last_time)) +
1009 (quotient * trace->last_time) + reduced_b;
1014 * \brief Auxiliary function to solve integral
1016 * Here, amount is <= trace->total
1017 * and a <=trace->last_time
1020 static double surf_cpu_ti_solve_trace_somewhat_simple(surf_cpu_ti_tgmr_t
1024 double amount_till_end;
1027 XBT_DEBUG("Solve integral: [%.2f, amount=%.2f]", a, amount);
1029 surf_cpu_ti_integrate_trace(trace, a, trace->last_time);
1031 fprintf(stderr,"amount_till_end=%.2f\n",amount_till_end);
1034 if (amount_till_end > amount) {
1035 b = surf_cpu_ti_solve_trace_simple(trace->trace, a, amount);
1037 b = trace->last_time +
1038 surf_cpu_ti_solve_trace_simple(trace->trace, 0.0,
1039 amount - amount_till_end);
1045 * \brief Auxiliary function to solve integral.
1046 * It returns the date when the requested amount of flops is available
1047 * \param trace Trace structure
1048 * \param a Initial point
1049 * \param amount Amount of flops
1050 * \return The date when amount is available.
1052 static double surf_cpu_ti_solve_trace_simple(surf_cpu_ti_trace_t trace,
1053 double a, double amount)
1058 integral_a = surf_cpu_ti_integrate_trace_simple_point(trace, a);
1060 surf_cpu_ti_binary_search(trace->integral, integral_a + amount, 0,
1061 trace->nb_points - 1);
1062 time = trace->time_points[ind];
1064 (integral_a + amount -
1065 trace->integral[ind]) / ((trace->integral[ind + 1] -
1066 trace->integral[ind]) /
1067 (trace->time_points[ind + 1] -
1068 trace->time_points[ind]));
1074 * \brief Binary search in array.
1075 * It returns the first point of the interval in which "a" is.
1076 * \param array Array
1077 * \param a Value to search
1078 * \param low Low bound to search in array
1079 * \param high Upper bound to search in array
1080 * \return Index of point
1082 static int surf_cpu_ti_binary_search(double *array, double a, int low,
1085 xbt_assert(low < high, "Wrong parameters: low (%d) should be smaller than"
1086 " high (%d)", low, high);
1090 mid = low + (high - low) / 2;
1091 XBT_DEBUG("a %lf low %d high %d mid %d value %lf", a, low, high, mid,
1099 while (low < high - 1);