Logo AND Algorithmique Numérique Distribuée

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