Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
06b7eaa4d1587da6c855ae51e4f1aaeea63c6a9c
[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   if(power_trace) tmgr_history_add_trace(history, power_trace, 0.0, 0, cpu);
31
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);
35
36   cpu->constraint = lmm_constraint_new(sys, cpu, cpu->current_power * cpu->power_scale);
37
38   xbt_dict_set(cpu_set, name, cpu, NULL);
39
40   return cpu;
41 }
42
43 static void parse_file(const char *file)
44 {
45   tmgr_trace_t trace_A = tmgr_trace_new("trace_A.txt");
46   tmgr_trace_t trace_B = tmgr_trace_new("trace_B.txt");
47
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);
50 }
51
52 static void *name_service(const char *name)
53 {
54   void *cpu=NULL;
55   
56   xbt_dict_get(cpu_set, name, &cpu);
57
58   return cpu;
59 }
60
61 static const char *get_resource_name(void *resource_id)
62 {
63   return ((cpu_t) resource_id)->name;
64 }
65
66 static int resource_used(void *resource_id)
67 {
68   return lmm_constraint_used(sys, ((cpu_t)resource_id)->constraint);
69 }
70
71 static surf_action_t action_new(void *callback)
72 {
73   return NULL;
74 }
75
76 static e_surf_action_state_t action_get_state(surf_action_t action)
77 {
78   return SURF_ACTION_NOT_IN_THE_SYSTEM;
79 }
80
81 static void action_free(surf_action_t * action)
82 {
83   return;
84 }
85
86 static void action_cancel(surf_action_t action)
87 {
88   return;
89 }
90
91 static void action_recycle(surf_action_t action)
92 {
93   return;
94 }
95
96 static void action_change_state(surf_action_t action, e_surf_action_state_t state)
97 {
98   surf_action_change_state(action, state);
99   return;
100 }
101
102 static xbt_heap_float_t share_resources()
103 {
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;
108   lmm_solve(sys);  
109
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 ;
114
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;
119   }
120
121   return min;
122 }
123
124 static void update_state(xbt_heap_float_t now,
125                          xbt_heap_float_t delta)
126 {
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;
130
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..) */
138   }
139
140   return;
141 }
142
143 static surf_action_t execute(void *cpu, xbt_maxmin_float_t size)
144 {
145   lmm_variable_t var;
146   surf_action_cpu_t action = NULL;
147
148   action=xbt_new0(s_surf_action_cpu_t,1);
149
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;
156
157   action->generic_action.state_set=surf_cpu_resource->resource.states.running_action_set;
158   xbt_swag_insert(action,action->generic_action.state_set);
159
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);
162
163   return (surf_action_t) action;
164 }
165
166 static e_surf_cpu_state_t get_state(void *cpu)
167 {
168   return SURF_CPU_OFF;
169 }
170
171 static surf_cpu_resource_t surf_cpu_resource_init_internal(void)
172 {
173   s_surf_action_t action;
174
175   surf_cpu_resource = xbt_new0(s_surf_cpu_resource_t,1);
176
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));
185
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;
196
197   surf_cpu_resource->execute = execute;
198   surf_cpu_resource->get_state = get_state;
199
200   cpu_set = xbt_dict_new();
201
202   sys = lmm_system_new();
203
204   return surf_cpu_resource;
205 }
206
207 void surf_cpu_resource_init(const char* filename)
208 {
209   surf_resource_t CPU = NULL;
210
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);
215 }