Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
d748240560ad8e04eb25d2c738238471284084f8
[simgrid.git] / src / surf / cpu.c
1 /*      $Id$     */
2
3 /* Copyright (c) 2004 Arnaud Legrand. All rights reserved.                  */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8 #include "cpu_private.h"
9
10 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(cpu, surf,
11                                 "Logging specific to the SURF CPU module");
12
13 surf_cpu_resource_t surf_cpu_resource = NULL;
14
15 xbt_dict_t cpu_set = NULL;
16
17 static void cpu_free(void *cpu)
18 {
19   xbt_free(((cpu_t)cpu)->name);
20   xbt_free(cpu);
21 }
22
23 static cpu_t cpu_new(char *name, double power_scale,
24                      double power_initial,
25                      tmgr_trace_t power_trace,
26                      e_surf_cpu_state_t state_initial,
27                      tmgr_trace_t state_trace)
28 {
29   cpu_t cpu = xbt_new0(s_cpu_t, 1);
30
31   cpu->resource = (surf_resource_t) surf_cpu_resource;
32   cpu->name = name;
33   cpu->power_scale = power_scale;
34   cpu->power_current = power_initial;
35   if (power_trace)
36     cpu->power_event =
37         tmgr_history_add_trace(history, power_trace, 0.0, 0, cpu);
38
39   cpu->state_current = state_initial;
40   if (state_trace)
41     cpu->state_event =
42         tmgr_history_add_trace(history, state_trace, 0.0, 0, cpu);
43
44   cpu->constraint =
45       lmm_constraint_new(maxmin_system, cpu,
46                          cpu->power_current * cpu->power_scale);
47
48   xbt_dict_set(cpu_set, name, cpu, cpu_free);
49
50   return cpu;
51 }
52
53 static void parse_cpu(void)
54 {
55   char *name = NULL;
56   double power_scale = 0.0;
57   double power_initial = 0.0;
58   tmgr_trace_t power_trace = NULL;
59   e_surf_cpu_state_t state_initial = SURF_CPU_OFF;
60   tmgr_trace_t state_trace = NULL;
61
62   name = xbt_strdup(A_cpu_name);
63   surf_parse_get_double(&power_scale,A_cpu_power);
64   surf_parse_get_double(&power_initial,A_cpu_availability);
65   surf_parse_get_trace(&power_trace,A_cpu_availability_file);
66
67   xbt_assert0((A_cpu_state==A_cpu_state_ON)||(A_cpu_state==A_cpu_state_OFF),
68               "Invalid state")
69   if (A_cpu_state==A_cpu_state_ON) state_initial = SURF_CPU_ON;
70   if (A_cpu_state==A_cpu_state_OFF) state_initial = SURF_CPU_OFF;
71   surf_parse_get_trace(&state_trace,A_cpu_state_file);
72
73   cpu_new(name, power_scale, power_initial, power_trace, state_initial,
74           state_trace);
75 }
76
77 static void parse_file(const char *file)
78 {
79   surf_parse_reset_parser();
80   ETag_cpu_fun=parse_cpu;
81   surf_parse_open(file);
82   xbt_assert1((!surf_parse()),"Parse error in %s",file);
83   surf_parse_close();
84 }
85
86 static void *name_service(const char *name)
87 {
88   void *cpu = NULL;
89
90   xbt_dict_get(cpu_set, name, &cpu);
91
92   return cpu;
93 }
94
95 static const char *get_resource_name(void *resource_id)
96 {
97   return ((cpu_t) resource_id)->name;
98 }
99
100 static int resource_used(void *resource_id)
101 {
102   return lmm_constraint_used(maxmin_system,
103                              ((cpu_t) resource_id)->constraint);
104 }
105
106 static void action_free(surf_action_t action)
107 {
108   surf_action_cpu_t Action = (surf_action_cpu_t) action;
109
110   xbt_swag_remove(action, action->state_set);
111   lmm_variable_free(maxmin_system, Action->variable);
112   xbt_free(action);
113
114   return;
115 }
116
117 static void action_cancel(surf_action_t action)
118 {
119   return;
120 }
121
122 static void action_recycle(surf_action_t action)
123 {
124   return;
125 }
126
127 static void action_change_state(surf_action_t action,
128                                 e_surf_action_state_t state)
129 {
130   surf_action_change_state(action, state);
131   return;
132 }
133
134 static double share_resources(double now)
135 {
136   s_surf_action_cpu_t action;
137   return generic_maxmin_share_resources(surf_cpu_resource->common_public->
138                                         states.running_action_set,
139                                         xbt_swag_offset(action, variable));
140 }
141
142 static void update_actions_state(double now, double delta)
143 {
144   surf_action_cpu_t action = NULL;
145   surf_action_cpu_t next_action = NULL;
146   xbt_swag_t running_actions =
147       surf_cpu_resource->common_public->states.running_action_set;
148   xbt_swag_t failed_actions =
149       surf_cpu_resource->common_public->states.failed_action_set;
150
151   xbt_swag_foreach_safe(action, next_action, running_actions) {
152     action->generic_action.remains -=
153         lmm_variable_getvalue(action->variable) * delta;
154     if (action->generic_action.max_duration != NO_MAX_DURATION)
155       action->generic_action.max_duration -= delta;
156     if ((action->generic_action.remains <= 0) && 
157         (lmm_get_variable_weight(action->variable)>0)) {
158       action->generic_action.finish = surf_get_clock();
159       action_change_state((surf_action_t) action, SURF_ACTION_DONE);
160     } else if ((action->generic_action.max_duration != NO_MAX_DURATION) &&
161                (action->generic_action.max_duration <= 0)) {
162       action->generic_action.finish = surf_get_clock();
163       action_change_state((surf_action_t) action, SURF_ACTION_DONE);
164     } else {                    /* Need to check that none of the resource has failed */
165       lmm_constraint_t cnst = NULL;
166       int i = 0;
167       cpu_t cpu = NULL;
168
169       while ((cnst =
170               lmm_get_cnst_from_var(maxmin_system, action->variable,
171                                     i++))) {
172         cpu = lmm_constraint_id(cnst);
173         if (cpu->state_current == SURF_CPU_OFF) {
174           action->generic_action.finish = surf_get_clock();
175           action_change_state((surf_action_t) action, SURF_ACTION_FAILED);
176           break;
177         }
178       }
179     }
180   }
181
182   xbt_swag_foreach_safe(action, next_action, failed_actions) {
183     lmm_variable_disable(maxmin_system, action->variable);
184   }
185
186   return;
187 }
188
189 static void update_resource_state(void *id,
190                                   tmgr_trace_event_t event_type,
191                                   double value)
192 {
193   cpu_t cpu = id;
194
195   if (event_type == cpu->power_event) {
196     cpu->power_current = value;
197     lmm_update_constraint_bound(maxmin_system, cpu->constraint,
198                                 cpu->power_current * cpu->power_scale);
199   } else if (event_type == cpu->state_event) {
200     if (value > 0)
201       cpu->state_current = SURF_CPU_ON;
202     else
203       cpu->state_current = SURF_CPU_OFF;
204   } else {
205     CRITICAL0("Unknown event ! \n");
206     xbt_abort();
207   }
208
209   return;
210 }
211
212 static surf_action_t execute(void *cpu, double size)
213 {
214   surf_action_cpu_t action = NULL;
215   cpu_t CPU = cpu;
216
217   action = xbt_new0(s_surf_action_cpu_t, 1);
218
219   action->generic_action.cost = size;
220   action->generic_action.remains = size;
221   action->generic_action.max_duration = NO_MAX_DURATION;
222   action->generic_action.start = surf_get_clock();
223   action->generic_action.finish = -1.0;
224   action->generic_action.resource_type =
225       (surf_resource_t) surf_cpu_resource;
226
227   if (CPU->state_current == SURF_CPU_ON)
228     action->generic_action.state_set =
229         surf_cpu_resource->common_public->states.running_action_set;
230   else
231     action->generic_action.state_set =
232         surf_cpu_resource->common_public->states.failed_action_set;
233   xbt_swag_insert(action, action->generic_action.state_set);
234
235   action->variable = lmm_variable_new(maxmin_system, action, 1.0, -1.0, 1);
236   lmm_expand(maxmin_system, CPU->constraint, action->variable,
237              1.0);
238
239   return (surf_action_t) action;
240 }
241
242 static surf_action_t action_sleep(void *cpu, double duration)
243 {
244   surf_action_cpu_t action = NULL;
245
246   action = (surf_action_cpu_t) execute(cpu, 1.0);
247   action->generic_action.max_duration = duration;
248   lmm_update_variable_weight(maxmin_system, action->variable, 0.0);
249
250   return (surf_action_t) action;
251 }
252
253 static void action_suspend(surf_action_t action)
254 {
255   lmm_update_variable_weight(maxmin_system,
256                              ((surf_action_cpu_t) action)->variable, 0.0);
257 }
258
259 static void action_resume(surf_action_t action)
260 {
261   lmm_update_variable_weight(maxmin_system,
262                              ((surf_action_cpu_t) action)->variable, 1.0);
263 }
264
265 static int action_is_suspended(surf_action_t action)
266 {
267   return (lmm_get_variable_weight(((surf_action_cpu_t) action)->variable) == 0.0);
268 }
269
270 static e_surf_cpu_state_t get_state(void *cpu)
271 {
272   return ((cpu_t) cpu)->state_current;
273 }
274
275 static void finalize(void)
276 {
277   xbt_dict_free(&cpu_set);
278   xbt_swag_free(surf_cpu_resource->common_public->states.ready_action_set);
279   xbt_swag_free(surf_cpu_resource->common_public->states.
280                 running_action_set);
281   xbt_swag_free(surf_cpu_resource->common_public->states.
282                 failed_action_set);
283   xbt_swag_free(surf_cpu_resource->common_public->states.done_action_set);
284   xbt_free(surf_cpu_resource->common_public);
285   xbt_free(surf_cpu_resource->common_private);
286   xbt_free(surf_cpu_resource->extension_public);
287
288   xbt_free(surf_cpu_resource);
289   surf_cpu_resource = NULL;
290 }
291
292 static void surf_cpu_resource_init_internal(void)
293 {
294   s_surf_action_t action;
295
296   surf_cpu_resource = xbt_new0(s_surf_cpu_resource_t, 1);
297
298   surf_cpu_resource->common_private =
299       xbt_new0(s_surf_resource_private_t, 1);
300   surf_cpu_resource->common_public = xbt_new0(s_surf_resource_public_t, 1);
301
302   surf_cpu_resource->extension_public =
303       xbt_new0(s_surf_cpu_resource_extension_public_t, 1);
304
305   surf_cpu_resource->common_public->states.ready_action_set =
306       xbt_swag_new(xbt_swag_offset(action, state_hookup));
307   surf_cpu_resource->common_public->states.running_action_set =
308       xbt_swag_new(xbt_swag_offset(action, state_hookup));
309   surf_cpu_resource->common_public->states.failed_action_set =
310       xbt_swag_new(xbt_swag_offset(action, state_hookup));
311   surf_cpu_resource->common_public->states.done_action_set =
312       xbt_swag_new(xbt_swag_offset(action, state_hookup));
313
314   surf_cpu_resource->common_public->name_service = name_service;
315   surf_cpu_resource->common_public->get_resource_name = get_resource_name;
316   surf_cpu_resource->common_public->action_get_state =
317       surf_action_get_state;
318   surf_cpu_resource->common_public->action_free = action_free;
319   surf_cpu_resource->common_public->action_cancel = action_cancel;
320   surf_cpu_resource->common_public->action_recycle = action_recycle;
321   surf_cpu_resource->common_public->action_change_state =
322       action_change_state;
323   surf_cpu_resource->common_public->action_set_data = surf_action_set_data;
324   surf_cpu_resource->common_public->name = "CPU";
325
326   surf_cpu_resource->common_private->resource_used = resource_used;
327   surf_cpu_resource->common_private->share_resources = share_resources;
328   surf_cpu_resource->common_private->update_actions_state =
329       update_actions_state;
330   surf_cpu_resource->common_private->update_resource_state =
331       update_resource_state;
332   surf_cpu_resource->common_private->finalize = finalize;
333
334   surf_cpu_resource->common_public->suspend = action_suspend;
335   surf_cpu_resource->common_public->resume = action_resume;
336   surf_cpu_resource->common_public->is_suspended = action_is_suspended;
337
338   surf_cpu_resource->extension_public->execute = execute;
339   surf_cpu_resource->extension_public->sleep = action_sleep;
340
341   surf_cpu_resource->extension_public->get_state = get_state;
342
343   cpu_set = xbt_dict_new();
344
345   xbt_assert0(maxmin_system, "surf_init has to be called first!");
346 }
347
348 /*********************************************************************/
349 /* Basic sharing model for CPU: that is where all this started... ;) */
350 /*********************************************************************/
351 /* @InProceedings{casanova01simgrid, */
352 /*   author =       "H. Casanova", */
353 /*   booktitle =    "Proceedings of the IEEE Symposium on Cluster Computing */
354 /*                  and the Grid (CCGrid'01)", */
355 /*   publisher =    "IEEE Computer Society", */
356 /*   title =        "Simgrid: {A} Toolkit for the Simulation of Application */
357 /*                  Scheduling", */
358 /*   year =         "2001", */
359 /*   month =        may, */
360 /*   note =         "Available at */
361 /*                  \url{http://grail.sdsc.edu/papers/simgrid_ccgrid01.ps.gz}." */
362 /* } */
363 void surf_cpu_resource_init_Cas01(const char *filename)
364 {
365   if (surf_cpu_resource)
366     return;
367   surf_cpu_resource_init_internal();
368   parse_file(filename);
369   xbt_dynar_push(resource_list, &surf_cpu_resource);
370 }