Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
8b42a20b0c8c24b84c415772c940e0bed7f76742
[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
11 static xbt_dict_t cpu_set = NULL;
12 static lmm_system_t sys = NULL;
13
14 /* power_scale is the basic power of the cpu when the cpu is
15    completely available. initial_power is therefore expected to be
16    comprised between 0.0 and 1.0, just as the values of power_trace.
17    state_trace values mean SURF_CPU_ON if >0 and SURF_CPU_OFF
18    otherwise.
19 */
20
21 static void *new_cpu(const char *name, xbt_maxmin_float_t power_scale,
22                       xbt_maxmin_float_t initial_power, tmgr_trace_t power_trace,
23                       e_surf_cpu_state_t initial_state, tmgr_trace_t state_trace)
24 {
25   cpu_t cpu = xbt_new0(s_cpu_t,1);
26
27   cpu->resource = (surf_resource_t) surf_cpu_resource;
28   cpu->name = name;
29   cpu->power_scale = power_scale;
30   cpu->current_power = initial_power;
31 /*   cpu->power_trace = power_trace; */
32   if(power_trace) 
33     cpu->power_event = tmgr_history_add_trace(history, power_trace, 0.0, 0, cpu);
34
35   cpu->current_state = initial_state;
36 /*   cpu->state_trace = state_trace; */
37   if(state_trace) 
38     cpu->state_event = tmgr_history_add_trace(history, state_trace, 0.0, 0, cpu);
39
40   cpu->constraint = lmm_constraint_new(sys, cpu, cpu->current_power * cpu->power_scale);
41
42   xbt_dict_set(cpu_set, name, cpu, NULL);
43
44   return cpu;
45 }
46
47 static void parse_file(const char *file)
48 {
49   tmgr_trace_t trace_A = tmgr_trace_new("trace_A.txt");
50   tmgr_trace_t trace_B = tmgr_trace_new("trace_B.txt");
51
52   new_cpu("Cpu A", 20.0, 1.0, trace_A, SURF_CPU_ON, NULL);
53   new_cpu("Cpu B", 100.0, 1.0, trace_B, SURF_CPU_ON, NULL);
54 }
55
56 static void *name_service(const char *name)
57 {
58   void *cpu=NULL;
59   
60   xbt_dict_get(cpu_set, name, &cpu);
61
62   return cpu;
63 }
64
65 static const char *get_resource_name(void *resource_id)
66 {
67   return ((cpu_t) resource_id)->name;
68 }
69
70 static int resource_used(void *resource_id)
71 {
72   return lmm_constraint_used(sys, ((cpu_t)resource_id)->constraint);
73 }
74
75 static surf_action_t action_new(void *callback)
76 {
77   return NULL;
78 }
79
80 static e_surf_action_state_t action_get_state(surf_action_t action)
81 {
82   return SURF_ACTION_NOT_IN_THE_SYSTEM;
83 }
84
85 static void action_free(surf_action_t * action)
86 {
87   return;
88 }
89
90 static void action_cancel(surf_action_t action)
91 {
92   return;
93 }
94
95 static void action_recycle(surf_action_t action)
96 {
97   return;
98 }
99
100 static void action_change_state(surf_action_t action, e_surf_action_state_t state)
101 {
102   surf_action_change_state(action, state);
103   return;
104 }
105
106 static xbt_heap_float_t share_resources()
107 {
108   surf_action_cpu_t action = NULL;
109   xbt_swag_t running_actions= surf_cpu_resource->common_public->states.running_action_set;
110   xbt_maxmin_float_t min = -1;
111   xbt_maxmin_float_t value = -1;
112   lmm_solve(sys);  
113
114   action = xbt_swag_getFirst(running_actions);
115   if(!action) return 0.0;
116   value = lmm_variable_getvalue(action->variable);
117   min = action->generic_action.remains / value ;
118
119   xbt_swag_foreach(action,running_actions) {
120     value = action->generic_action.remains / 
121       lmm_variable_getvalue(action->variable);
122     if(value<min) min=value;
123   }
124
125   return min;
126 }
127
128 static void update_actions_state(xbt_heap_float_t now,
129                                  xbt_heap_float_t delta)
130 {
131   surf_action_cpu_t action = NULL;
132   surf_action_cpu_t next_action = NULL;
133   xbt_swag_t running_actions= surf_cpu_resource->common_public->states.running_action_set;
134
135   xbt_swag_foreach_safe(action, next_action, running_actions) {
136     action->generic_action.remains -= 
137       lmm_variable_getvalue(action->variable)*delta;
138 /*     if(action->generic_action.remains<.00001) action->generic_action.remains=0; */
139     if(action->generic_action.remains<=0) {
140       action_change_state((surf_action_t)action, SURF_ACTION_DONE);
141     } /* else if(host_failed..) */
142   }
143
144   return;
145 }
146
147 static void update_resource_state(void *id,
148                                   tmgr_trace_event_t event_type, 
149                                   xbt_maxmin_float_t value)
150 {
151   cpu_t cpu = id;
152   
153   printf("Asking to update \"%s\" with value " XBT_MAXMIN_FLOAT_T " for event %p\n",
154          cpu->name, value, event_type);
155   
156   if(event_type==cpu->power_event) {
157     cpu->current_power = value;
158     lmm_update_constraint_bound(cpu->constraint,cpu->current_power * cpu->power_scale);
159   } else if (event_type==cpu->state_event) {
160     if(value>0) cpu->current_state = SURF_CPU_ON;
161     else cpu->current_state = SURF_CPU_OFF;
162   } else abort();
163
164   return;
165 }
166
167 static surf_action_t execute(void *cpu, xbt_maxmin_float_t size)
168 {
169   lmm_variable_t var;
170   surf_action_cpu_t action = NULL;
171
172   action=xbt_new0(s_surf_action_cpu_t,1);
173
174   action->generic_action.cost=size;
175   action->generic_action.remains=size;  
176   action->generic_action.start=-1.0;
177   action->generic_action.finish=-1.0;
178   action->generic_action.callback=cpu;
179   action->generic_action.resource_type=(surf_resource_t)surf_cpu_resource;
180
181   action->generic_action.state_set=surf_cpu_resource->common_public->states.running_action_set;
182   xbt_swag_insert(action,action->generic_action.state_set);
183
184   action->variable = lmm_variable_new(sys, action, 1.0, -1.0, 1);
185   lmm_expand(sys, ((cpu_t)cpu)->constraint, action->variable, 1.0);
186
187   return (surf_action_t) action;
188 }
189
190 static e_surf_cpu_state_t get_state(void *cpu)
191 {
192   return SURF_CPU_OFF;
193 }
194
195 static void surf_cpu_resource_init_internal(void)
196 {
197   s_surf_action_t action;
198
199   surf_cpu_resource = xbt_new0(s_surf_cpu_resource_t,1);
200   
201   surf_cpu_resource->common_private = xbt_new0(s_surf_resource_private_t,1);
202   surf_cpu_resource->common_public = xbt_new0(s_surf_resource_public_t,1);
203 /*   surf_cpu_resource->extension_private = xbt_new0(s_surf_cpu_resource_extension_private_t,1); */
204   surf_cpu_resource->extension_public = xbt_new0(s_surf_cpu_resource_extension_public_t,1);
205
206   surf_cpu_resource->common_public->states.ready_action_set=
207     xbt_swag_new(xbt_swag_offset(action,state_hookup));
208   surf_cpu_resource->common_public->states.running_action_set=
209     xbt_swag_new(xbt_swag_offset(action,state_hookup));
210   surf_cpu_resource->common_public->states.failed_action_set=
211     xbt_swag_new(xbt_swag_offset(action,state_hookup));
212   surf_cpu_resource->common_public->states.done_action_set=
213     xbt_swag_new(xbt_swag_offset(action,state_hookup));
214
215   surf_cpu_resource->common_public->name_service = name_service;
216   surf_cpu_resource->common_public->get_resource_name = get_resource_name;
217   surf_cpu_resource->common_public->resource_used = resource_used;
218   surf_cpu_resource->common_public->action_get_state=surf_action_get_state;
219   surf_cpu_resource->common_public->action_free = action_free;
220   surf_cpu_resource->common_public->action_cancel = action_cancel;
221   surf_cpu_resource->common_public->action_recycle = action_recycle;
222   surf_cpu_resource->common_public->action_change_state = action_change_state;
223   surf_cpu_resource->common_private->share_resources = share_resources;
224   surf_cpu_resource->common_private->update_actions_state = update_actions_state;
225   surf_cpu_resource->common_private->update_resource_state = update_resource_state;
226
227   surf_cpu_resource->extension_public->execute = execute;
228   surf_cpu_resource->extension_public->get_state = get_state;
229
230   cpu_set = xbt_dict_new();
231
232   sys = lmm_system_new();
233 }
234
235 void surf_cpu_resource_init(const char* filename)
236 {
237   surf_cpu_resource_init_internal();
238   parse_file(filename);
239   xbt_dynar_push(resource_list, &surf_cpu_resource);
240 }