Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Move clock modification to the right location so that finish_time are correct...
[simgrid.git] / src / surf / surf.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 "surf_private.h"
9 #include "xbt/module.h"
10
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_kernel, surf,
12                                 "Logging specific to SURF (kernel)");
13
14 typedef struct surf_resource_object {
15   surf_resource_t resource;
16 } s_surf_resource_object_t, *surf_resource_object_t;
17
18 static double NOW = 0;
19
20 xbt_dynar_t resource_list = NULL;
21 tmgr_history_t history = NULL;
22 lmm_system_t maxmin_system = NULL;
23 xbt_dynar_t surf_path = NULL;
24 const char *surf_action_state_names[6] = {
25   "SURF_ACTION_READY", 
26   "SURF_ACTION_RUNNING", 
27   "SURF_ACTION_FAILED", 
28   "SURF_ACTION_DONE", 
29   "SURF_ACTION_TO_FREE", 
30   "SURF_ACTION_NOT_IN_THE_SYSTEM"
31 };
32
33 double generic_maxmin_share_resources(xbt_swag_t running_actions,
34                                        size_t offset)
35 {
36   return  generic_maxmin_share_resources2(running_actions, offset,
37                                           maxmin_system);
38 }
39
40 double generic_maxmin_share_resources2(xbt_swag_t running_actions,
41                                        size_t offset,
42                                        lmm_system_t sys)
43 {
44   surf_action_t action = NULL;
45   double min = -1;
46   double value = -1;
47 #define VARIABLE(action) (*((lmm_variable_t*)(((char *) (action)) + (offset))))
48
49   lmm_solve(sys);
50
51   xbt_swag_foreach(action, running_actions) {
52     value = lmm_variable_getvalue(VARIABLE(action));
53     if ((value > 0) || (action->max_duration >= 0))
54       break;
55   }
56
57   if (!action)
58     return -1.0;
59
60   if (value > 0) {
61     min = action->remains / value;
62     if ((action->max_duration >= 0) && (action->max_duration < min))
63       min = action->max_duration;
64   } else
65     min = action->max_duration;
66
67
68   for (action = xbt_swag_getNext(action, running_actions->offset);
69        action;
70        action = xbt_swag_getNext(action, running_actions->offset)) {
71     value = lmm_variable_getvalue(VARIABLE(action));
72     if (value > 0) {
73       value = action->remains / value;
74       if (value < min)
75         min = value;
76     }
77     if ((action->max_duration >= 0) && (action->max_duration < min))
78       min = action->max_duration;
79   }
80 #undef VARIABLE
81   return min;
82 }
83
84 e_surf_action_state_t surf_action_get_state(surf_action_t action)
85 {
86   surf_action_state_t action_state =
87       &(action->resource_type->common_public->states);
88
89   if (action->state_set == action_state->ready_action_set)
90     return SURF_ACTION_READY;
91   if (action->state_set == action_state->running_action_set)
92     return SURF_ACTION_RUNNING;
93   if (action->state_set == action_state->failed_action_set)
94     return SURF_ACTION_FAILED;
95   if (action->state_set == action_state->done_action_set)
96     return SURF_ACTION_DONE;
97   return SURF_ACTION_NOT_IN_THE_SYSTEM;
98 }
99
100 double surf_action_get_start_time(surf_action_t action) {
101   return action->start;
102 }
103
104 double surf_action_get_finish_time(surf_action_t action) {
105   return action->finish;
106 }
107
108 void surf_action_free(surf_action_t * action)
109 {
110   (*action)->resource_type->common_public->action_cancel(*action);
111   free(*action);
112   *action = NULL;
113 }
114
115 void surf_action_change_state(surf_action_t action,
116                               e_surf_action_state_t state)
117 {
118   surf_action_state_t action_state =
119       &(action->resource_type->common_public->states);
120   XBT_IN2("(%p,%s)", action, surf_action_state_names[state]);
121   xbt_swag_remove(action, action->state_set);
122
123   if (state == SURF_ACTION_READY)
124     action->state_set = action_state->ready_action_set;
125   else if (state == SURF_ACTION_RUNNING)
126     action->state_set = action_state->running_action_set;
127   else if (state == SURF_ACTION_FAILED)
128     action->state_set = action_state->failed_action_set;
129   else if (state == SURF_ACTION_DONE)
130     action->state_set = action_state->done_action_set;
131   else
132     action->state_set = NULL;
133
134   if (action->state_set)
135     xbt_swag_insert(action, action->state_set);
136   XBT_OUT;
137 }
138
139 void surf_action_set_data(surf_action_t action,
140                           void *data)
141 {
142   action->data=data;
143 }
144
145 void surf_init(int *argc, char **argv)
146 {
147   int i,j;
148   char *opt;
149
150   xbt_init(argc, argv);
151   if (!surf_path) {
152     const char *initial_path = "./";
153     surf_path = xbt_dynar_new(sizeof(char*), NULL);
154     xbt_dynar_push(surf_path,&initial_path);
155
156     for (i=1; i<*argc; i++) {
157       if (!strncmp(argv[i],"--surf-path=",strlen("--surf-path="))) {
158         opt=strchr(argv[i],'=');
159         opt++;
160         xbt_dynar_push(surf_path,&opt);
161         /*remove this from argv*/
162         for (j=i+1; j<*argc; j++) {
163           argv[j-1] = argv[j];
164         } 
165         argv[j-1] = NULL;
166         (*argc)--;
167         i--; /* compensate effect of next loop incrementation */
168       }
169     }
170   }
171   if (!resource_list)
172     resource_list = xbt_dynar_new(sizeof(surf_resource_private_t), NULL);
173   if (!history)
174     history = tmgr_history_new();
175   if (!maxmin_system)
176     maxmin_system = lmm_system_new();
177 }
178
179 static char* path_name = NULL;
180 FILE *surf_fopen(const char *name, const char *mode)
181 {
182   int i; 
183   char* path = NULL;
184   FILE *file = NULL;
185   int path_name_len = 0; /* don't count '\0' */
186
187   xbt_assert0(name, "Need a non-NULL file name");
188
189   xbt_assert0(surf_path,"surf_init has to be called before using surf_fopen");
190    
191   if (name[0] == '/') { /* don't mess with absolute file names */
192     return fopen(name,mode);
193      
194   } else { /* search relative files in the path */
195    
196     if(!path_name) {
197        path_name_len = strlen(name);
198        path_name=xbt_new0(char,path_name_len+1);
199     }
200
201     xbt_dynar_foreach(surf_path,i,path) {
202       if(path_name_len < strlen(path)+strlen(name)+1) {
203          path_name_len = strlen(path)+strlen(name)+1; /* plus '/' */
204          path_name=xbt_realloc(path_name,path_name_len+1);
205       }
206       sprintf(path_name,"%s/%s",path, name);
207       file = fopen(path_name,mode);
208       if (file) return file;
209     }
210   }
211   return file;
212 }
213
214 void surf_exit(void)
215 {
216   int i;
217   surf_resource_t resource = NULL;
218
219   xbt_dynar_foreach(resource_list, i, resource) {
220     resource->common_private->finalize();
221   }
222
223   if (maxmin_system) {
224     lmm_system_free(maxmin_system);
225     maxmin_system = NULL;
226   }
227   if (history) {
228     tmgr_history_free(history);
229     history = NULL;
230   }
231   if (resource_list)
232     xbt_dynar_free(&resource_list);
233
234   if(surf_path) 
235     xbt_dynar_free(&surf_path);
236
237   tmgr_finalize();
238   surf_parse_lex_destroy();
239   if(path_name) {
240     free(path_name);
241     path_name = NULL;
242   }
243   xbt_exit();
244 }
245
246 double surf_solve(void)
247 {
248   static int first_run = 1;
249
250   double min = -1.0;
251   double next_event_date = -1.0;
252   double resource_next_action_end = -1.0;
253   double value = -1.0;
254   surf_resource_object_t resource_obj = NULL;
255   surf_resource_t resource = NULL;
256   tmgr_trace_event_t event = NULL;
257   int i;
258
259   if (first_run) {
260     DEBUG0("First Run! Let's \"purge\" events and put resources in the right state");
261     while ((next_event_date = tmgr_history_next_date(history)) != -1.0) {
262       if (next_event_date > NOW)
263         break;
264       while ((event =
265               tmgr_history_get_next_event_leq(history, next_event_date,
266                                               &value,
267                                               (void **) &resource_obj))) {
268         resource_obj->resource->common_private->
269             update_resource_state(resource_obj, event, value);
270       }
271     }
272     xbt_dynar_foreach(resource_list, i, resource) {
273       resource->common_private->update_actions_state(NOW, 0.0);
274     }
275     first_run = 0;
276     return 0.0;
277   }
278
279   min = -1.0;
280
281   DEBUG0("Looking for next action end");
282   xbt_dynar_foreach(resource_list, i, resource) {
283     resource_next_action_end =
284         resource->common_private->share_resources(NOW);
285     DEBUG2("Resource [%s] : next action end = %f",resource->common_public->name,
286            resource_next_action_end);
287     if (((min < 0.0) || (resource_next_action_end < min))
288         && (resource_next_action_end >= 0.0))
289       min = resource_next_action_end;
290   }
291   DEBUG1("Next action end : %f", min);
292
293   if (min < 0.0)
294     return -1.0;
295
296   DEBUG0("Looking for next event");
297   while ((next_event_date = tmgr_history_next_date(history)) != -1.0) {
298     DEBUG1("Next event : %f",next_event_date);
299     if (next_event_date > NOW + min)
300       break;
301     DEBUG0("Updating resources");
302     while ((event =
303             tmgr_history_get_next_event_leq(history, next_event_date,
304                                             &value,
305                                             (void **) &resource_obj))) {
306       if (resource_obj->resource->common_private->
307           resource_used(resource_obj)) {
308         min = next_event_date - NOW;
309         DEBUG1("This event will modify resource state. Next event set to %f", min);
310       }
311       /* update state of resource_obj according to new value. Does not touch lmm.
312          It will be modified if needed when updating actions */
313       resource_obj->resource->common_private->
314           update_resource_state(resource_obj, event, value);
315     }
316   }
317
318   DEBUG1("Duration set to %f", min);
319
320   NOW = NOW + min;
321
322   xbt_dynar_foreach(resource_list, i, resource) {
323     resource->common_private->update_actions_state(NOW, min);
324   }
325
326   return min;
327 }
328
329 double surf_get_clock(void)
330 {
331   return NOW;
332 }