1 /* Authors: Arnaud Legrand */
3 /* This program is free software; you can redistribute it and/or modify it
4 under the terms of the license (GNU LGPL) which comes with this package. */
6 #include "cpu_private.h"
9 surf_cpu_resource_t surf_cpu_resource = NULL;
10 static xbt_dict_t cpu_set = NULL;
11 static lmm_system_t sys = NULL;
13 /* power_scale is the basic power of the cpu when the cpu is
14 completely available. initial_power is therefore expected to be
15 comprised between 0.0 and 1.0, just as the values of power_trace.
16 state_trace values mean SURF_CPU_ON if >0 and SURF_CPU_OFF
20 static void *new_cpu(const char *name, xbt_maxmin_float_t power_scale,
21 xbt_maxmin_float_t initial_power, tmgr_trace_t power_trace,
22 e_surf_cpu_state_t initial_state, tmgr_trace_t state_trace)
24 cpu_t cpu = xbt_new0(s_cpu_t,1);
27 cpu->power_scale = power_scale;
28 cpu->current_power = initial_power;
29 cpu->power_trace = power_trace;
30 if(power_trace) tmgr_history_add_trace(history, power_trace, 0.0, 0, cpu);
32 cpu->current_state = initial_state;
33 cpu->state_trace = state_trace;
34 if(state_trace) tmgr_history_add_trace(history, state_trace, 0.0, 0, cpu);
36 cpu->constraint = lmm_constraint_new(sys, cpu, cpu->current_power * cpu->power_scale);
38 xbt_dict_set(cpu_set, name, cpu, NULL);
43 static void parse_file(const char *file)
45 tmgr_trace_t trace_A = tmgr_trace_new("trace_A.txt");
46 tmgr_trace_t trace_B = tmgr_trace_new("trace_B.txt");
48 new_cpu("Cpu A", 20.0, 1.0, trace_A, SURF_CPU_ON, NULL);
49 new_cpu("Cpu B", 120.0, 1.0, trace_B, SURF_CPU_ON, NULL);
52 static void *name_service(const char *name)
56 xbt_dict_get(cpu_set, name, &cpu);
61 static const char *get_resource_name(void *resource_id)
63 return ((cpu_t) resource_id)->name;
66 static int resource_used(void *resource_id)
68 return lmm_constraint_used(sys, ((cpu_t)resource_id)->constraint);
71 static surf_action_t action_new(void *callback)
76 static e_surf_action_state_t action_get_state(surf_action_t action)
78 return SURF_ACTION_NOT_IN_THE_SYSTEM;
81 static void action_free(surf_action_t * action)
86 static void action_cancel(surf_action_t action)
91 static void action_recycle(surf_action_t action)
96 static void action_change_state(surf_action_t action, e_surf_action_state_t state)
98 surf_action_change_state(action, state);
102 static xbt_heap_float_t share_resources()
104 surf_action_cpu_t action = NULL;
105 xbt_swag_t running_actions= surf_cpu_resource->resource.states.running_action_set;
106 xbt_maxmin_float_t min = -1;
107 xbt_maxmin_float_t value = -1;
110 action = xbt_swag_getFirst(running_actions);
111 if(!action) return 0.0;
112 value = lmm_variable_getvalue(action->variable);
113 min = action->generic_action.remains / value ;
115 xbt_swag_foreach(action,running_actions) {
116 value = action->generic_action.remains /
117 lmm_variable_getvalue(action->variable);
118 if(value<min) min=value;
124 static void update_state(xbt_heap_float_t now,
125 xbt_heap_float_t delta)
127 surf_action_cpu_t action = NULL;
128 surf_action_cpu_t next_action = NULL;
129 xbt_swag_t running_actions= surf_cpu_resource->resource.states.running_action_set;
131 xbt_swag_foreach_safe(action, next_action, running_actions) {
132 action->generic_action.remains -=
133 lmm_variable_getvalue(action->variable)*delta;
134 /* if(action->generic_action.remains<.00001) action->generic_action.remains=0; */
135 if(action->generic_action.remains<=0) {
136 action_change_state((surf_action_t)action, SURF_ACTION_DONE);
137 } /* else if(host_failed..) */
143 static surf_action_t execute(void *cpu, xbt_maxmin_float_t size)
146 surf_action_cpu_t action = NULL;
148 action=xbt_new0(s_surf_action_cpu_t,1);
150 action->generic_action.cost=size;
151 action->generic_action.remains=size;
152 action->generic_action.start=-1.0;
153 action->generic_action.finish=-1.0;
154 action->generic_action.callback=cpu;
155 action->generic_action.resource_type=(surf_resource_t)surf_cpu_resource;
157 action->generic_action.state_set=surf_cpu_resource->resource.states.running_action_set;
158 xbt_swag_insert(action,action->generic_action.state_set);
160 action->variable = lmm_variable_new(sys, action, 1.0, -1.0, 1);
161 lmm_expand(sys, ((cpu_t)cpu)->constraint, action->variable, 1.0);
163 return (surf_action_t) action;
166 static e_surf_cpu_state_t get_state(void *cpu)
171 static surf_cpu_resource_t surf_cpu_resource_init_internal(void)
173 s_surf_action_t action;
175 surf_cpu_resource = xbt_new0(s_surf_cpu_resource_t,1);
177 surf_cpu_resource->resource.states.ready_action_set=
178 xbt_swag_new(xbt_swag_offset(action,state_hookup));
179 surf_cpu_resource->resource.states.running_action_set=
180 xbt_swag_new(xbt_swag_offset(action,state_hookup));
181 surf_cpu_resource->resource.states.failed_action_set=
182 xbt_swag_new(xbt_swag_offset(action,state_hookup));
183 surf_cpu_resource->resource.states.done_action_set=
184 xbt_swag_new(xbt_swag_offset(action,state_hookup));
186 surf_cpu_resource->resource.name_service = name_service;
187 surf_cpu_resource->resource.get_resource_name = get_resource_name;
188 surf_cpu_resource->resource.resource_used = resource_used;
189 surf_cpu_resource->resource.action_get_state=surf_action_get_state;
190 surf_cpu_resource->resource.action_free = action_free;
191 surf_cpu_resource->resource.action_cancel = action_cancel;
192 surf_cpu_resource->resource.action_recycle = action_recycle;
193 surf_cpu_resource->resource.action_change_state = action_change_state;
194 surf_cpu_resource->resource.share_resources = share_resources;
195 surf_cpu_resource->resource.update_state = update_state;
197 surf_cpu_resource->execute = execute;
198 surf_cpu_resource->get_state = get_state;
200 cpu_set = xbt_dict_new();
202 sys = lmm_system_new();
204 return surf_cpu_resource;
207 void surf_cpu_resource_init(const char* filename)
209 surf_resource_t CPU = NULL;
211 surf_cpu_resource=surf_cpu_resource_init_internal();
212 parse_file(filename);
213 CPU = &(surf_cpu_resource->resource); /* to make short */
214 xbt_dynar_push(resource_list, &CPU);