Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
ab00d8bf2ee5d4314fc924e075367902ad7905df
[simgrid.git] / src / surf / cpu.c
1 /* Authors: Arnaud Legrand                                                  */
2
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. */
5
6 #include "cpu_private.h"
7 #include "xbt/dict.h"
8
9 surf_cpu_resource_t surf_cpu_resource = NULL;
10 static xbt_dict_t cpu_set = NULL;
11 static lmm_system_t sys = NULL;
12
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
17    otherwise.
18 */
19
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)
23 {
24   cpu_t cpu = xbt_new0(s_cpu_t,1);
25
26   cpu->name = name;
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);
33
34   xbt_dict_set(cpu_set, name, cpu, NULL);
35
36   return cpu;
37 }
38
39 static void parse_file(const char *file)
40 {
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);
43 }
44
45 static void *name_service(const char *name)
46 {
47   void *cpu=NULL;
48   
49   xbt_dict_get(cpu_set, name, &cpu);
50
51   return cpu;
52 }
53
54 static const char *get_resource_name(void *resource_id)
55 {
56   return ((cpu_t) resource_id)->name;
57 }
58
59 static int resource_used(void *resource_id)
60 {
61   return lmm_constraint_used(sys, ((cpu_t)resource_id)->constraint);
62 }
63
64 static surf_action_t action_new(void *callback)
65 {
66   return NULL;
67 }
68
69 static e_surf_action_state_t action_get_state(surf_action_t action)
70 {
71   return SURF_ACTION_NOT_IN_THE_SYSTEM;
72 }
73
74 static void action_free(surf_action_t * action)
75 {
76   return;
77 }
78
79 static void action_cancel(surf_action_t action)
80 {
81   return;
82 }
83
84 static void action_recycle(surf_action_t action)
85 {
86   return;
87 }
88
89 static void action_change_state(surf_action_t action, e_surf_action_state_t state)
90 {
91   surf_action_change_state(action, state);
92   return;
93 }
94
95 static xbt_heap_float_t share_resources()
96 {
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;
101   lmm_solve(sys);  
102
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 ;
107
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;
113   }
114
115   return min;
116 }
117
118 static void update_state(xbt_heap_float_t now,
119                          xbt_heap_float_t delta)
120 {
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;
124
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..) */
132   }
133
134   return;
135 }
136
137 static surf_action_t execute(void *cpu, xbt_maxmin_float_t size)
138 {
139   lmm_variable_t var;
140   surf_action_cpu_t action = NULL;
141
142   action=xbt_new0(s_surf_action_cpu_t,1);
143
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;
150
151   action->generic_action.state_set=surf_cpu_resource->resource.states.running_action_set;
152   xbt_swag_insert(action,action->generic_action.state_set);
153
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);
156
157   return (surf_action_t) action;
158 }
159
160 static e_surf_cpu_state_t get_state(void *cpu)
161 {
162   return SURF_CPU_OFF;
163 }
164
165 static surf_cpu_resource_t surf_cpu_resource_init_internal(void)
166 {
167   s_surf_action_t action;
168
169   surf_cpu_resource = xbt_new0(s_surf_cpu_resource_t,1);
170
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));
179
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;
190
191   surf_cpu_resource->execute = execute;
192   surf_cpu_resource->get_state = get_state;
193
194   cpu_set = xbt_dict_new();
195
196   sys = lmm_system_new();
197
198   return surf_cpu_resource;
199 }
200
201 void surf_cpu_resource_init(const char* filename)
202 {
203   surf_resource_t CPU = NULL;
204
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);
209 }