Logo AND Algorithmique Numérique Distribuée

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