Logo AND Algorithmique Numérique Distribuée

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