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 cpu->current_state = initial_state;
31 cpu->state_trace = state_trace;
32 cpu->constraint = lmm_constraint_new(sys, cpu, cpu->current_power * cpu->power_scale);
34 xbt_dict_set(cpu_set, name, cpu, NULL);
39 static void parse_file(const char *file)
41 new_cpu("Cpu A", 20.0, 1.0, NULL, SURF_CPU_ON, NULL);
42 new_cpu("Cpu B", 120.0, 1.0, NULL, SURF_CPU_ON, NULL);
45 static void *name_service(const char *name)
49 xbt_dict_get(cpu_set, name, &cpu);
54 static const char *get_resource_name(void *resource_id)
56 return ((cpu_t) resource_id)->name;
59 static int resource_used(void *resource_id)
61 return lmm_constraint_used(sys, ((cpu_t)resource_id)->constraint);
64 static surf_action_t action_new(void *callback)
69 static e_surf_action_state_t action_get_state(surf_action_t action)
71 return SURF_ACTION_NOT_IN_THE_SYSTEM;
74 static void action_free(surf_action_t * action)
79 static void action_cancel(surf_action_t action)
84 static void action_recycle(surf_action_t action)
89 static void action_change_state(surf_action_t action, e_surf_action_state_t state)
91 surf_action_change_state(action, state);
95 static xbt_heap_float_t share_resources()
97 surf_action_cpu_t action = NULL;
98 xbt_swag_t running_actions= surf_cpu_resource->resource.states.running_action_set;
99 xbt_maxmin_float_t min = -1;
100 xbt_maxmin_float_t value = -1;
103 action = xbt_swag_getFirst(running_actions);
104 if(!action) return 0.0;
105 value = lmm_variable_getvalue(action->variable);
106 min = action->generic_action.remains / value ;
108 xbt_swag_foreach(action,running_actions) {
109 /* If everything is stable... */
110 value = action->generic_action.remains /
111 lmm_variable_getvalue(action->variable);
112 if(value<min) min=value;
118 static void update_state(xbt_heap_float_t now,
119 xbt_heap_float_t delta)
121 surf_action_cpu_t action = NULL;
122 surf_action_cpu_t next_action = NULL;
123 xbt_swag_t running_actions= surf_cpu_resource->resource.states.running_action_set;
125 xbt_swag_foreach_safe(action, next_action, running_actions) {
126 action->generic_action.remains -=
127 lmm_variable_getvalue(action->variable)*delta;
128 /* if(action->generic_action.remains<.00001) action->generic_action.remains=0; */
129 if(action->generic_action.remains<=0) {
130 action_change_state((surf_action_t)action, SURF_ACTION_DONE);
131 } /* else if(host_failed..) */
137 static surf_action_t execute(void *cpu, xbt_maxmin_float_t size)
140 surf_action_cpu_t action = NULL;
142 action=xbt_new0(s_surf_action_cpu_t,1);
144 action->generic_action.cost=size;
145 action->generic_action.remains=size;
146 action->generic_action.start=-1.0;
147 action->generic_action.finish=-1.0;
148 action->generic_action.callback=cpu;
149 action->generic_action.resource_type=(surf_resource_t)surf_cpu_resource;
151 action->generic_action.state_set=surf_cpu_resource->resource.states.running_action_set;
152 xbt_swag_insert(action,action->generic_action.state_set);
154 action->variable = lmm_variable_new(sys, action, 1.0, -1.0, 1);
155 lmm_expand(sys, ((cpu_t)cpu)->constraint, action->variable, 1.0);
157 return (surf_action_t) action;
160 static e_surf_cpu_state_t get_state(void *cpu)
165 static surf_cpu_resource_t surf_cpu_resource_init_internal(void)
167 s_surf_action_t action;
169 surf_cpu_resource = xbt_new0(s_surf_cpu_resource_t,1);
171 surf_cpu_resource->resource.states.ready_action_set=
172 xbt_swag_new(xbt_swag_offset(action,state_hookup));
173 surf_cpu_resource->resource.states.running_action_set=
174 xbt_swag_new(xbt_swag_offset(action,state_hookup));
175 surf_cpu_resource->resource.states.failed_action_set=
176 xbt_swag_new(xbt_swag_offset(action,state_hookup));
177 surf_cpu_resource->resource.states.done_action_set=
178 xbt_swag_new(xbt_swag_offset(action,state_hookup));
180 surf_cpu_resource->resource.name_service = name_service;
181 surf_cpu_resource->resource.get_resource_name = get_resource_name;
182 surf_cpu_resource->resource.resource_used = resource_used;
183 surf_cpu_resource->resource.action_get_state=surf_action_get_state;
184 surf_cpu_resource->resource.action_free = action_free;
185 surf_cpu_resource->resource.action_cancel = action_cancel;
186 surf_cpu_resource->resource.action_recycle = action_recycle;
187 surf_cpu_resource->resource.action_change_state = action_change_state;
188 surf_cpu_resource->resource.share_resources = share_resources;
189 surf_cpu_resource->resource.update_state = update_state;
191 surf_cpu_resource->execute = execute;
192 surf_cpu_resource->get_state = get_state;
194 cpu_set = xbt_dict_new();
196 sys = lmm_system_new();
198 return surf_cpu_resource;
201 void surf_cpu_resource_init(const char* filename)
203 surf_resource_t CPU = NULL;
205 surf_cpu_resource=surf_cpu_resource_init_internal();
206 parse_file(filename);
207 CPU = &(surf_cpu_resource->resource); /* to make short */
208 xbt_dynar_push(resource_list, &CPU);