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"
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 if (trace->time_points)
54 xbt_free(trace->time_points);
56 xbt_free(trace->integral);
60 static void surf_cpu_ti_free_tmgr(surf_cpu_ti_tgmr_t trace)
63 surf_cpu_ti_free_trace(trace->trace);
67 static surf_cpu_ti_trace_t surf_cpu_ti_trace_new(tmgr_trace_t power_trace)
69 surf_cpu_ti_trace_t trace;
75 trace = xbt_new0(s_surf_cpu_ti_trace_t, 1);
77 xbt_malloc0(sizeof(double) *
78 (xbt_dynar_length(power_trace->event_list) + 1));
80 xbt_malloc0(sizeof(double) *
81 (xbt_dynar_length(power_trace->event_list) + 1));
82 trace->nb_points = xbt_dynar_length(power_trace->event_list);
83 xbt_dynar_foreach(power_trace->event_list, cpt, val) {
84 trace->time_points[i] = time;
85 trace->integral[i] = integral;
86 integral += val.delta * val.value;
90 trace->time_points[i] = time;
91 trace->integral[i] = integral;
96 * \brief Creates a new integration trace from a tmgr_trace_t
98 * \param power_trace CPU availability trace
99 * \param value Percentage of CPU power available (useful to fixed tracing)
100 * \param spacing Initial spacing
101 * \return Integration trace structure
103 static surf_cpu_ti_tgmr_t cpu_ti_parse_trace(tmgr_trace_t power_trace,
106 surf_cpu_ti_tgmr_t trace;
107 double total_time = 0.0;
110 trace = xbt_new0(s_surf_cpu_ti_tgmr_t, 1);
112 /* no availability file, fixed trace */
114 trace->type = TRACE_FIXED;
115 trace->value = value;
116 XBT_DEBUG("No availabily trace. Constant value = %lf", value);
120 /* only one point available, fixed trace */
121 if (xbt_dynar_length(power_trace->event_list) == 1) {
122 xbt_dynar_get_cpy(power_trace->event_list, 0, &val);
123 trace->type = TRACE_FIXED;
124 trace->value = val.value;
128 trace->type = TRACE_DYNAMIC;
129 trace->power_trace = power_trace;
131 /* count the total time of trace file */
132 xbt_dynar_foreach(power_trace->event_list, cpt, val) {
133 total_time += val.delta;
135 trace->trace = surf_cpu_ti_trace_new(power_trace);
136 trace->last_time = total_time;
138 surf_cpu_ti_integrate_trace_simple(trace->trace, 0, total_time);
140 XBT_DEBUG("Total integral %lf, last_time %lf ",
141 trace->total, trace->last_time);
147 static cpu_ti_t cpu_ti_new(char *name, double power_peak,
149 tmgr_trace_t power_trace,
151 e_surf_resource_state_t state_initial,
152 tmgr_trace_t state_trace,
153 xbt_dict_t cpu_properties)
155 tmgr_trace_t empty_trace;
157 cpu_ti_t cpu = xbt_new0(s_cpu_ti_t, 1);
158 s_surf_action_cpu_ti_t ti_action;
159 xbt_assert(!surf_cpu_resource_by_name(name),
160 "Host '%s' declared several times in the platform file",
162 xbt_assert(core==1,"Multi-core not handled with this model yet");
164 xbt_swag_new(xbt_swag_offset(ti_action, cpu_list_hookup));
165 cpu->generic_resource.model = surf_cpu_model;
166 cpu->generic_resource.name = name;
167 cpu->generic_resource.properties = cpu_properties;
168 cpu->power_peak = power_peak;
169 xbt_assert(cpu->power_peak > 0, "Power has to be >0");
170 XBT_DEBUG("power scale %lf", power_scale);
171 cpu->power_scale = power_scale;
172 cpu->avail_trace = cpu_ti_parse_trace(power_trace, power_scale);
173 cpu->state_current = state_initial;
176 tmgr_history_add_trace(history, state_trace, 0.0, 0, cpu);
177 if (power_trace && xbt_dynar_length(power_trace->event_list) > 1) {
178 /* add a fake trace event if periodicity == 0 */
179 xbt_dynar_get_cpy(power_trace->event_list,
180 xbt_dynar_length(power_trace->event_list) - 1, &val);
181 if (val.delta == 0) {
182 empty_trace = tmgr_empty_trace_new();
184 tmgr_history_add_trace(history, empty_trace,
185 cpu->avail_trace->last_time, 0, cpu);
188 xbt_lib_set(host_lib, name, SURF_CPU_LEVEL, cpu);
194 static void parse_cpu_ti_init(void)
196 double power_peak = 0.0;
197 double power_scale = 0.0;
199 tmgr_trace_t power_trace = NULL;
200 e_surf_resource_state_t state_initial = SURF_RESOURCE_OFF;
201 tmgr_trace_t state_trace = NULL;
203 power_peak = get_cpu_power(A_surfxml_host_power);
204 surf_parse_get_double(&power_scale, A_surfxml_host_availability);
205 power_trace = tmgr_trace_new(A_surfxml_host_availability_file);
206 surf_parse_get_int(&core, A_surfxml_host_core);
208 xbt_assert((A_surfxml_host_state == A_surfxml_host_state_ON) ||
209 (A_surfxml_host_state == A_surfxml_host_state_OFF),
211 if (A_surfxml_host_state == A_surfxml_host_state_ON)
212 state_initial = SURF_RESOURCE_ON;
213 if (A_surfxml_host_state == A_surfxml_host_state_OFF)
214 state_initial = SURF_RESOURCE_OFF;
215 state_trace = tmgr_trace_new(A_surfxml_host_state_file);
217 cpu_ti_new(xbt_strdup(A_surfxml_host_id), power_peak, power_scale,
218 power_trace, core, state_initial, state_trace,
219 current_property_set);
220 current_property_set = NULL;
224 static void add_traces_cpu_ti(void)
226 xbt_dict_cursor_t cursor = NULL;
227 char *trace_name, *elm;
229 static int called = 0;
235 /* connect all traces relative to hosts */
236 xbt_dict_foreach(trace_connect_list_host_avail, cursor, trace_name, elm) {
237 tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
238 cpu_ti_t cpu = surf_cpu_resource_by_name(elm);
240 xbt_assert(cpu, "Host %s undefined", elm);
241 xbt_assert(trace, "Trace %s undefined", trace_name);
243 if (cpu->state_event) {
244 XBT_DEBUG("Trace already configured for this CPU(%s), ignoring it",
248 XBT_DEBUG("Add state trace: %s to CPU(%s)", trace_name, elm);
249 cpu->state_event = tmgr_history_add_trace(history, trace, 0.0, 0, cpu);
252 xbt_dict_foreach(trace_connect_list_power, cursor, trace_name, elm) {
253 tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
254 cpu_ti_t cpu = surf_cpu_resource_by_name(elm);
256 xbt_assert(cpu, "Host %s undefined", elm);
257 xbt_assert(trace, "Trace %s undefined", trace_name);
259 XBT_DEBUG("Add power trace: %s to CPU(%s)", trace_name, elm);
260 if (cpu->avail_trace)
261 surf_cpu_ti_free_tmgr(cpu->avail_trace);
263 cpu->avail_trace = cpu_ti_parse_trace(trace, cpu->power_scale);
265 /* add a fake trace event if periodicity == 0 */
266 if (trace && xbt_dynar_length(trace->event_list) > 1) {
268 xbt_dynar_get_cpy(trace->event_list,
269 xbt_dynar_length(trace->event_list) - 1, &val);
270 if (val.delta == 0) {
271 tmgr_trace_t empty_trace;
272 empty_trace = tmgr_empty_trace_new();
274 tmgr_history_add_trace(history, empty_trace,
275 cpu->avail_trace->last_time, 0, cpu);
281 static void cpu_ti_define_callbacks(const char *file)
283 surfxml_add_callback(ETag_surfxml_host_cb_list, parse_cpu_ti_init);
284 surfxml_add_callback(ETag_surfxml_platform_cb_list, &add_traces_cpu_ti);
287 static int cpu_ti_resource_used(void *resource_id)
289 cpu_ti_t cpu = resource_id;
290 return xbt_swag_size(cpu->action_set);
293 static int cpu_ti_action_unref(surf_action_t action)
296 if (!action->refcount) {
297 xbt_swag_remove(action, action->state_set);
298 /* remove from action_set */
299 xbt_swag_remove(action, ACTION_GET_CPU(action)->action_set);
300 /* remove from heap */
301 xbt_heap_remove(cpu_ti_action_heap,
302 ((surf_action_cpu_ti_t) action)->index_heap);
303 xbt_swag_insert(ACTION_GET_CPU(action), cpu_ti_modified_cpu);
304 surf_action_free(&action);
310 static void cpu_ti_action_cancel(surf_action_t action)
312 surf_action_state_set(action, SURF_ACTION_FAILED);
313 xbt_heap_remove(cpu_ti_action_heap,
314 ((surf_action_cpu_ti_t) action)->index_heap);
315 xbt_swag_insert(ACTION_GET_CPU(action), cpu_ti_modified_cpu);
319 static void cpu_ti_action_state_set(surf_action_t action,
320 e_surf_action_state_t state)
322 surf_action_state_set(action, state);
323 xbt_swag_insert(ACTION_GET_CPU(action), cpu_ti_modified_cpu);
328 * \brief Update the remaining amount of actions
330 * \param cpu Cpu on which the actions are running
331 * \param now Current time
333 static void cpu_ti_update_remaining_amount(cpu_ti_t cpu, double now)
335 #define GENERIC_ACTION(action) action->generic_action
337 surf_action_cpu_ti_t action;
339 /* already updated */
340 if (cpu->last_update >= now)
343 /* calcule the surface */
345 surf_cpu_ti_integrate_trace(cpu->avail_trace, cpu->last_update,
346 now) * cpu->power_peak;
347 XBT_DEBUG("Flops total: %lf, Last update %lf", area_total,
350 xbt_swag_foreach(action, cpu->action_set) {
351 /* action not running, skip it */
352 if (GENERIC_ACTION(action).state_set !=
353 surf_cpu_model->states.running_action_set)
356 /* bogus priority, skip it */
357 if (GENERIC_ACTION(action).priority <= 0)
360 /* action suspended, skip it */
361 if (action->suspended != 0)
364 /* action don't need update */
365 if (GENERIC_ACTION(action).start >= now)
368 /* skip action that are finishing now */
369 if (GENERIC_ACTION(action).finish >= 0
370 && GENERIC_ACTION(action).finish <= now)
373 /* update remaining */
374 double_update(&(GENERIC_ACTION(action).remains),
375 area_total / (cpu->sum_priority *
376 GENERIC_ACTION(action).priority));
377 XBT_DEBUG("Update remaining action(%p) remaining %lf", action,
378 GENERIC_ACTION(action).remains);
380 cpu->last_update = now;
381 #undef GENERIC_ACTION
385 * \brief Update the finish date of action if necessary
387 * \param cpu Cpu on which the actions are running
388 * \param now Current time
390 static void cpu_ti_update_action_finish_date(cpu_ti_t cpu, double now)
392 #define GENERIC_ACTION(action) action->generic_action
393 surf_action_cpu_ti_t action;
394 double sum_priority = 0.0, total_area, min_finish = -1;
396 /* update remaning amount of actions */
397 cpu_ti_update_remaining_amount(cpu, now);
399 xbt_swag_foreach(action, cpu->action_set) {
400 /* action not running, skip it */
401 if (GENERIC_ACTION(action).state_set !=
402 surf_cpu_model->states.running_action_set)
405 /* bogus priority, skip it */
406 if (GENERIC_ACTION(action).priority <= 0)
409 /* action suspended, skip it */
410 if (action->suspended != 0)
413 sum_priority += 1.0 / GENERIC_ACTION(action).priority;
415 cpu->sum_priority = sum_priority;
417 xbt_swag_foreach(action, cpu->action_set) {
419 /* action not running, skip it */
420 if (GENERIC_ACTION(action).state_set !=
421 surf_cpu_model->states.running_action_set)
424 /* verify if the action is really running on cpu */
425 if (action->suspended == 0 && GENERIC_ACTION(action).priority > 0) {
426 /* total area needed to finish the action. Used in trace integration */
428 (GENERIC_ACTION(action).remains) * sum_priority *
429 GENERIC_ACTION(action).priority;
431 total_area /= cpu->power_peak;
433 GENERIC_ACTION(action).finish =
434 surf_cpu_ti_solve_trace(cpu->avail_trace, now, total_area);
435 /* verify which event will happen before (max_duration or finish time) */
436 if ((GENERIC_ACTION(action).max_duration != NO_MAX_DURATION) &&
437 (GENERIC_ACTION(action).start +
438 GENERIC_ACTION(action).max_duration <
439 GENERIC_ACTION(action).finish))
440 min_finish = GENERIC_ACTION(action).start +
441 GENERIC_ACTION(action).max_duration;
443 min_finish = GENERIC_ACTION(action).finish;
445 /* put the max duration time on heap */
446 if (GENERIC_ACTION(action).max_duration != NO_MAX_DURATION)
448 (GENERIC_ACTION(action).start +
449 GENERIC_ACTION(action).max_duration);
451 /* add in action heap */
452 XBT_DEBUG("action(%p) index %d", action, action->index_heap);
453 if (action->index_heap >= 0) {
454 surf_action_cpu_ti_t heap_act =
455 xbt_heap_remove(cpu_ti_action_heap, action->index_heap);
456 if (heap_act != action)
459 if (min_finish != NO_MAX_DURATION)
460 xbt_heap_push(cpu_ti_action_heap, action, min_finish);
463 ("Update finish time: Cpu(%s) Action: %p, Start Time: %lf Finish Time: %lf Max duration %lf",
464 cpu->generic_resource.name, action, GENERIC_ACTION(action).start,
465 GENERIC_ACTION(action).finish,
466 GENERIC_ACTION(action).max_duration);
468 /* remove from modified cpu */
469 xbt_swag_remove(cpu, cpu_ti_modified_cpu);
470 #undef GENERIC_ACTION
473 static double cpu_ti_share_resources(double now)
475 cpu_ti_t cpu, cpu_next;
476 double min_action_duration = -1;
478 /* iterates over modified cpus to update share resources */
479 xbt_swag_foreach_safe(cpu, cpu_next, cpu_ti_modified_cpu) {
480 cpu_ti_update_action_finish_date(cpu, now);
482 /* get the min next event if heap not empty */
483 if (xbt_heap_size(cpu_ti_action_heap) > 0)
484 min_action_duration = xbt_heap_maxkey(cpu_ti_action_heap) - now;
486 XBT_DEBUG("Share resources, min next event date: %lf", min_action_duration);
488 return min_action_duration;
491 static void cpu_ti_update_actions_state(double now, double delta)
493 #define GENERIC_ACTION(action) action->generic_action
494 surf_action_cpu_ti_t action;
495 while ((xbt_heap_size(cpu_ti_action_heap) > 0)
496 && (xbt_heap_maxkey(cpu_ti_action_heap) <= now)) {
497 action = xbt_heap_pop(cpu_ti_action_heap);
498 XBT_DEBUG("Action %p: finish", action);
499 GENERIC_ACTION(action).finish = surf_get_clock();
500 /* set the remains to 0 due to precision problems when updating the remaining amount */
501 GENERIC_ACTION(action).remains = 0;
502 cpu_ti_action_state_set((surf_action_t) action, SURF_ACTION_DONE);
503 /* update remaining amout of all actions */
504 cpu_ti_update_remaining_amount(action->cpu, surf_get_clock());
506 #undef GENERIC_ACTION
509 static void cpu_ti_update_resource_state(void *id,
510 tmgr_trace_event_t event_type,
511 double value, double date)
514 surf_action_cpu_ti_t action;
516 if (event_type == cpu->power_event) {
517 tmgr_trace_t power_trace;
518 surf_cpu_ti_tgmr_t trace;
521 XBT_DEBUG("Finish trace date: %lf value %lf date %lf", surf_get_clock(),
523 /* update remaining of actions and put in modified cpu swag */
524 cpu_ti_update_remaining_amount(cpu, date);
525 xbt_swag_insert(cpu, cpu_ti_modified_cpu);
527 power_trace = cpu->avail_trace->power_trace;
528 xbt_dynar_get_cpy(power_trace->event_list,
529 xbt_dynar_length(power_trace->event_list) - 1, &val);
531 surf_cpu_ti_free_tmgr(cpu->avail_trace);
532 cpu->power_scale = val.value;
534 trace = xbt_new0(s_surf_cpu_ti_tgmr_t, 1);
535 trace->type = TRACE_FIXED;
536 trace->value = val.value;
537 XBT_DEBUG("value %lf", val.value);
539 cpu->avail_trace = trace;
541 if (tmgr_trace_event_free(event_type))
542 cpu->power_event = NULL;
544 } else if (event_type == cpu->state_event) {
546 cpu->state_current = SURF_RESOURCE_ON;
548 cpu->state_current = SURF_RESOURCE_OFF;
550 /* put all action running on cpu to failed */
551 xbt_swag_foreach(action, cpu->action_set) {
552 if (surf_action_state_get((surf_action_t) action) ==
554 || surf_action_state_get((surf_action_t) action) ==
556 || surf_action_state_get((surf_action_t) action) ==
557 SURF_ACTION_NOT_IN_THE_SYSTEM) {
558 action->generic_action.finish = date;
559 cpu_ti_action_state_set((surf_action_t) action,
561 if (action->index_heap >= 0) {
562 surf_action_cpu_ti_t heap_act =
563 xbt_heap_remove(cpu_ti_action_heap, action->index_heap);
564 if (heap_act != action)
570 if (tmgr_trace_event_free(event_type))
571 cpu->state_event = NULL;
573 XBT_CRITICAL("Unknown event ! \n");
580 static surf_action_t cpu_ti_execute(void *cpu, double size)
582 surf_action_cpu_ti_t action = NULL;
585 XBT_IN("(%s,%g)", surf_resource_name(CPU), size);
587 surf_action_new(sizeof(s_surf_action_cpu_ti_t), size, surf_cpu_model,
588 CPU->state_current != SURF_RESOURCE_ON);
590 action->index_heap = -1;
592 xbt_swag_insert(CPU, cpu_ti_modified_cpu);
594 xbt_swag_insert(action, CPU->action_set);
596 action->suspended = 0; /* Should be useless because of the
597 calloc but it seems to help valgrind... */
600 return (surf_action_t) action;
603 static void cpu_ti_action_update_index_heap(void *action, int i)
605 ((surf_action_cpu_ti_t) action)->index_heap = i;
608 static surf_action_t cpu_ti_action_sleep(void *cpu, double duration)
610 surf_action_cpu_ti_t action = NULL;
613 duration = MAX(duration, MAXMIN_PRECISION);
615 XBT_IN("(%s,%g)", surf_resource_name(cpu), duration);
616 action = (surf_action_cpu_ti_t) cpu_ti_execute(cpu, 1.0);
617 action->generic_action.max_duration = duration;
618 action->suspended = 2;
619 if (duration == NO_MAX_DURATION) {
620 /* Move to the *end* of the corresponding action set. This convention
621 is used to speed up update_resource_state */
622 xbt_swag_remove(action, ((surf_action_t) action)->state_set);
623 ((surf_action_t) action)->state_set =
624 cpu_ti_running_action_set_that_does_not_need_being_checked;
625 xbt_swag_insert(action, ((surf_action_t) action)->state_set);
628 return (surf_action_t) action;
631 static void cpu_ti_action_suspend(surf_action_t action)
633 XBT_IN("(%p)", action);
634 if (((surf_action_cpu_ti_t) action)->suspended != 2) {
635 ((surf_action_cpu_ti_t) action)->suspended = 1;
636 xbt_heap_remove(cpu_ti_action_heap,
637 ((surf_action_cpu_ti_t) action)->index_heap);
638 xbt_swag_insert(ACTION_GET_CPU(action), cpu_ti_modified_cpu);
643 static void cpu_ti_action_resume(surf_action_t action)
645 XBT_IN("(%p)", action);
646 if (((surf_action_cpu_ti_t) action)->suspended != 2) {
647 ((surf_action_cpu_ti_t) action)->suspended = 0;
648 xbt_swag_insert(ACTION_GET_CPU(action), cpu_ti_modified_cpu);
653 static int cpu_ti_action_is_suspended(surf_action_t action)
655 return (((surf_action_cpu_ti_t) action)->suspended == 1);
658 static void cpu_ti_action_set_max_duration(surf_action_t action,
661 surf_action_cpu_ti_t ACT = (surf_action_cpu_ti_t) action;
664 XBT_IN("(%p,%g)", action, duration);
666 action->max_duration = duration;
670 (action->start + action->max_duration) <
671 action->finish ? (action->start +
672 action->max_duration) : action->finish;
674 min_finish = action->finish;
676 /* add in action heap */
677 if (ACT->index_heap >= 0) {
678 surf_action_cpu_ti_t heap_act =
679 xbt_heap_remove(cpu_ti_action_heap, ACT->index_heap);
683 xbt_heap_push(cpu_ti_action_heap, ACT, min_finish);
688 static void cpu_ti_action_set_priority(surf_action_t action,
691 XBT_IN("(%p,%g)", action, priority);
692 action->priority = priority;
693 xbt_swag_insert(ACTION_GET_CPU(action), cpu_ti_modified_cpu);
697 static double cpu_ti_action_get_remains(surf_action_t action)
699 XBT_IN("(%p)", action);
700 cpu_ti_update_remaining_amount((cpu_ti_t)
701 ((surf_action_cpu_ti_t) action)->cpu,
703 return action->remains;
707 static e_surf_resource_state_t cpu_ti_get_state(void *cpu)
709 return ((cpu_ti_t) cpu)->state_current;
712 static double cpu_ti_get_speed(void *cpu, double load)
714 return load * (((cpu_ti_t) cpu)->power_peak);
718 * \brief Auxiliary function to update the CPU power scale.
720 * This function uses the trace structure to return the power scale at the determined time a.
721 * \param trace Trace structure to search the updated power scale
723 * \return CPU power scale
725 static double surf_cpu_ti_get_power_scale(surf_cpu_ti_tgmr_t trace,
732 reduced_a = a - floor(a / trace->last_time) * trace->last_time;
734 surf_cpu_ti_binary_search(trace->trace->time_points, reduced_a, 0,
735 trace->trace->nb_points - 1);
736 xbt_dynar_get_cpy(trace->power_trace->event_list, 0, &val);
740 static double cpu_ti_get_available_speed(void *cpu)
744 surf_cpu_ti_get_power_scale(CPU->avail_trace, surf_get_clock());
745 /* number between 0 and 1 */
746 return CPU->power_scale;
749 static void cpu_ti_create_resource(char *name, double power_peak,
751 tmgr_trace_t power_trace,
753 e_surf_resource_state_t state_initial,
754 tmgr_trace_t state_trace,
755 xbt_dict_t cpu_properties)
757 xbt_assert(core==1,"Multi-core not handled with this model yet");
758 cpu_ti_new(name, power_peak, power_scale, power_trace, core,
759 state_initial, state_trace, cpu_properties);
762 static void cpu_ti_finalize(void)
765 xbt_lib_cursor_t cursor;
768 xbt_lib_foreach(host_lib, cursor, key, cpu){
769 if(cpu[SURF_CPU_LEVEL])
771 cpu_ti_t CPU = cpu[SURF_CPU_LEVEL];
772 xbt_swag_free(CPU->action_set);
773 surf_cpu_ti_free_tmgr(CPU->avail_trace);
777 surf_model_exit(surf_cpu_model);
778 surf_cpu_model = NULL;
781 (cpu_ti_running_action_set_that_does_not_need_being_checked);
782 xbt_swag_free(cpu_ti_modified_cpu);
783 cpu_ti_running_action_set_that_does_not_need_being_checked = NULL;
784 xbt_heap_free(cpu_ti_action_heap);
787 static void surf_cpu_ti_model_init_internal(void)
789 s_surf_action_t action;
792 surf_cpu_model = surf_model_init();
794 cpu_ti_running_action_set_that_does_not_need_being_checked =
795 xbt_swag_new(xbt_swag_offset(action, state_hookup));
797 cpu_ti_modified_cpu =
798 xbt_swag_new(xbt_swag_offset(cpu, modified_cpu_hookup));
800 surf_cpu_model->name = "CPU_TI";
802 surf_cpu_model->action_unref = cpu_ti_action_unref;
803 surf_cpu_model->action_cancel = cpu_ti_action_cancel;
804 surf_cpu_model->action_state_set = cpu_ti_action_state_set;
806 surf_cpu_model->model_private->resource_used = cpu_ti_resource_used;
807 surf_cpu_model->model_private->share_resources = cpu_ti_share_resources;
808 surf_cpu_model->model_private->update_actions_state =
809 cpu_ti_update_actions_state;
810 surf_cpu_model->model_private->update_resource_state =
811 cpu_ti_update_resource_state;
812 surf_cpu_model->model_private->finalize = cpu_ti_finalize;
814 surf_cpu_model->suspend = cpu_ti_action_suspend;
815 surf_cpu_model->resume = cpu_ti_action_resume;
816 surf_cpu_model->is_suspended = cpu_ti_action_is_suspended;
817 surf_cpu_model->set_max_duration = cpu_ti_action_set_max_duration;
818 surf_cpu_model->set_priority = cpu_ti_action_set_priority;
819 surf_cpu_model->get_remains = cpu_ti_action_get_remains;
821 surf_cpu_model->extension.cpu.execute = cpu_ti_execute;
822 surf_cpu_model->extension.cpu.sleep = cpu_ti_action_sleep;
824 surf_cpu_model->extension.cpu.get_state = cpu_ti_get_state;
825 surf_cpu_model->extension.cpu.get_speed = cpu_ti_get_speed;
826 surf_cpu_model->extension.cpu.get_available_speed =
827 cpu_ti_get_available_speed;
828 surf_cpu_model->extension.cpu.create_resource = cpu_ti_create_resource;
829 surf_cpu_model->extension.cpu.add_traces = add_traces_cpu_ti;
831 cpu_ti_action_heap = xbt_heap_new(8, NULL);
832 xbt_heap_set_update_callback(cpu_ti_action_heap,
833 cpu_ti_action_update_index_heap);
837 void surf_cpu_model_init_ti(const char *filename)
841 surf_cpu_ti_model_init_internal();
842 cpu_ti_define_callbacks(filename);
843 xbt_dynar_push(model_list, &surf_cpu_model);
848 * \brief Integrate trace
850 * Wrapper around surf_cpu_integrate_trace_simple() to get
853 * \param trace Trace structure.
854 * \param a Begin of interval
855 * \param b End of interval
856 * \return the integrate value. -1 if an error occurs.
858 static double surf_cpu_ti_integrate_trace(surf_cpu_ti_tgmr_t trace,
864 int a_index, b_index;
866 if ((a < 0.0) || (a > b)) {
868 ("Error, invalid integration interval [%.2f,%.2f]. You probably have a task executing with negative computation amount. Check your code.",
875 if (trace->type == TRACE_FIXED) {
876 return ((b - a) * trace->value);
879 if (ceil(a / trace->last_time) == a / trace->last_time)
880 a_index = 1 + (int) (ceil(a / trace->last_time));
882 a_index = (int) (ceil(a / trace->last_time));
884 b_index = (int) (floor(b / trace->last_time));
886 if (a_index > b_index) { /* Same chunk */
887 return surf_cpu_ti_integrate_trace_simple(trace->trace,
889 1) * trace->last_time,
895 first_chunk = surf_cpu_ti_integrate_trace_simple(trace->trace,
900 middle_chunk = (b_index - a_index) * trace->total;
901 last_chunk = surf_cpu_ti_integrate_trace_simple(trace->trace,
907 XBT_DEBUG("first_chunk=%.2f middle_chunk=%.2f last_chunk=%.2f\n",
908 first_chunk, middle_chunk, last_chunk);
910 return (first_chunk + middle_chunk + last_chunk);
914 * \brief Auxiliary function to calculate the integral between a and b.
915 * It simply calculates the integral at point a and b and returns the difference
917 * \param trace Trace structure
918 * \param a Initial point
919 * \param b Final point
922 static double surf_cpu_ti_integrate_trace_simple(surf_cpu_ti_trace_t trace,
925 return surf_cpu_ti_integrate_trace_simple_point(trace,
927 surf_cpu_ti_integrate_trace_simple_point(trace, a);
931 * \brief Auxiliary function to calculate the integral at point a.
932 * \param trace Trace structure
936 static double surf_cpu_ti_integrate_trace_simple_point(surf_cpu_ti_trace_t
943 surf_cpu_ti_binary_search(trace->time_points, a, 0,
944 trace->nb_points - 1);
945 integral += trace->integral[ind];
947 ("a %lf ind %d integral %lf ind + 1 %lf ind %lf time +1 %lf time %lf",
948 a, ind, integral, trace->integral[ind + 1], trace->integral[ind],
949 trace->time_points[ind + 1], trace->time_points[ind]);
950 double_update(&a_aux, trace->time_points[ind]);
953 ((trace->integral[ind + 1] -
954 trace->integral[ind]) / (trace->time_points[ind + 1] -
955 trace->time_points[ind])) * (a -
959 XBT_DEBUG("Integral a %lf = %lf", a, integral);
965 * \brief Calculate the time needed to execute "amount" on cpu.
967 * Here, amount can span multiple trace periods
969 * \param trace CPU trace structure
970 * \param a Initial time
971 * \param amount Amount to be executed
974 static double surf_cpu_ti_solve_trace(surf_cpu_ti_tgmr_t trace, double a,
979 double reduced_amount;
983 /* Fix very small negative numbers */
984 if ((a < 0.0) && (a > -EPSILON)) {
987 if ((amount < 0.0) && (amount > -EPSILON)) {
992 if ((a < 0.0) || (amount < 0.0)) {
994 ("Error, invalid parameters [a = %.2f, amount = %.2f]. You probably have a task executing with negative computation amount. Check your code.",
999 /* At this point, a and amount are positive */
1001 if (amount < EPSILON)
1004 /* Is the trace fixed ? */
1005 if (trace->type == TRACE_FIXED) {
1006 return (a + (amount / trace->value));
1009 XBT_DEBUG("amount %lf total %lf", amount, trace->total);
1010 /* Reduce the problem to one where amount <= trace_total */
1011 quotient = (int) (floor(amount / trace->total));
1012 reduced_amount = (trace->total) * ((amount / trace->total) -
1013 floor(amount / trace->total));
1014 reduced_a = a - (trace->last_time) * (int) (floor(a / trace->last_time));
1016 XBT_DEBUG("Quotient: %d reduced_amount: %lf reduced_a: %lf", quotient,
1017 reduced_amount, reduced_a);
1019 /* Now solve for new_amount which is <= trace_total */
1021 fprintf(stderr,"reduced_a = %.2f\n",reduced_a);
1022 fprintf(stderr,"reduced_amount = %.2f\n",reduced_amount);
1025 surf_cpu_ti_solve_trace_somewhat_simple(trace, reduced_a,
1028 /* Re-map to the original b and amount */
1029 b = (trace->last_time) * (int) (floor(a / trace->last_time)) +
1030 (quotient * trace->last_time) + reduced_b;
1035 * \brief Auxiliary function to solve integral
1037 * Here, amount is <= trace->total
1038 * and a <=trace->last_time
1041 static double surf_cpu_ti_solve_trace_somewhat_simple(surf_cpu_ti_tgmr_t
1045 double amount_till_end;
1048 XBT_DEBUG("Solve integral: [%.2f, amount=%.2f]", a, amount);
1050 surf_cpu_ti_integrate_trace(trace, a, trace->last_time);
1052 fprintf(stderr,"amount_till_end=%.2f\n",amount_till_end);
1055 if (amount_till_end > amount) {
1056 b = surf_cpu_ti_solve_trace_simple(trace->trace, a, amount);
1058 b = trace->last_time +
1059 surf_cpu_ti_solve_trace_simple(trace->trace, 0.0,
1060 amount - amount_till_end);
1066 * \brief Auxiliary function to solve integral.
1067 * It returns the date when the requested amount of flops is available
1068 * \param trace Trace structure
1069 * \param a Initial point
1070 * \param amount Amount of flops
1071 * \return The date when amount is available.
1073 static double surf_cpu_ti_solve_trace_simple(surf_cpu_ti_trace_t trace,
1074 double a, double amount)
1079 integral_a = surf_cpu_ti_integrate_trace_simple_point(trace, a);
1081 surf_cpu_ti_binary_search(trace->integral, integral_a + amount, 0,
1082 trace->nb_points - 1);
1083 time = trace->time_points[ind];
1085 (integral_a + amount -
1086 trace->integral[ind]) / ((trace->integral[ind + 1] -
1087 trace->integral[ind]) /
1088 (trace->time_points[ind + 1] -
1089 trace->time_points[ind]));
1095 * \brief Binary search in array.
1096 * It returns the first point of the interval in which "a" is.
1097 * \param array Array
1098 * \param a Value to search
1099 * \param low Low bound to search in array
1100 * \param high Upper bound to search in array
1101 * \return Index of point
1103 static int surf_cpu_ti_binary_search(double *array, double a, int low,
1106 int mid = low + (high - low) / 2;
1107 XBT_DEBUG("a %lf low %d high %d mid %d value %lf", a, low, high, mid,
1109 /* a == array[mid] */
1110 if (array[mid] == a)
1112 /* a is between mid and mid+1 */
1113 if (array[mid] < a && array[mid + 1] > a)
1117 return surf_cpu_ti_binary_search(array, a, mid + 1, high);
1119 return surf_cpu_ti_binary_search(array, a, low, mid - 1);