3 /* Copyright (c) 2004 Arnaud Legrand. All rights reserved. */
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. */
8 #include "surf_private.h"
9 #include "xbt/module.h"
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_global, surf,
12 "Logging specific to the SURF global module");
15 static double NOW = 0;
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;
22 double generic_maxmin_share_resources(xbt_swag_t running_actions,
25 return generic_maxmin_share_resources2(running_actions, offset,
29 double generic_maxmin_share_resources2(xbt_swag_t running_actions,
33 surf_action_t action = NULL;
36 #define VARIABLE(action) (*((lmm_variable_t*)(((char *) (action)) + (offset))))
40 xbt_swag_foreach(action, running_actions) {
41 value = lmm_variable_getvalue(VARIABLE(action));
42 if ((value > 0) || (action->max_duration >= 0))
50 min = value = action->remains / value;
51 if ((action->max_duration >= 0) && (action->max_duration < min))
52 min = action->max_duration;
54 min = action->max_duration;
57 for (action = xbt_swag_getNext(action, running_actions->offset);
59 action = xbt_swag_getNext(action, running_actions->offset)) {
60 value = lmm_variable_getvalue(VARIABLE(action));
62 value = action->remains / value;
66 if ((action->max_duration >= 0) && (action->max_duration < min))
67 min = action->max_duration;
73 e_surf_action_state_t surf_action_get_state(surf_action_t action)
75 surf_action_state_t action_state =
76 &(action->resource_type->common_public->states);
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;
89 void surf_action_free(surf_action_t * action)
91 (*action)->resource_type->common_public->action_cancel(*action);
96 void surf_action_change_state(surf_action_t action,
97 e_surf_action_state_t state)
99 surf_action_state_t action_state =
100 &(action->resource_type->common_public->states);
102 xbt_swag_remove(action, action->state_set);
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;
113 action->state_set = NULL;
115 if (action->state_set)
116 xbt_swag_insert(action, action->state_set);
119 void surf_action_set_data(surf_action_t action,
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)
129 extern void *msg_global;
131 void surf_init(int *argc, char **argv)
136 xbt_init(argc, argv);
137 msg_global=NULL; /* see HACKHACK note above */
139 const char *initial_path = "./";
140 surf_path = xbt_dynar_new(sizeof(char*), NULL);
141 xbt_dynar_push(surf_path,&initial_path);
143 for (i=1; i<*argc; i++) {
144 if (!strncmp(argv[i],"--surf-path=",strlen("--surf-path="))) {
145 opt=strchr(argv[i],'=');
147 xbt_dynar_push(surf_path,&opt);
148 /*remove this from argv*/
149 for (j=i+1; j<*argc; j++) {
154 i--; /* compensate effect of next loop incrementation */
159 resource_list = xbt_dynar_new(sizeof(surf_resource_private_t), NULL);
161 history = tmgr_history_new();
163 maxmin_system = lmm_system_new();
166 static char* path_name = NULL;
167 FILE *surf_fopen(const char *name, const char *mode)
172 int path_name_len = 0; /* don't count '\0' */
174 xbt_assert0(name, "Need a non-NULL file name");
176 xbt_assert0(surf_path,"surf_init has to be called before using surf_fopen");
178 if (name[0] == '/') { /* don't mess with absolute file names */
179 return fopen(name,mode);
181 } else { /* search relative files in the path */
184 path_name_len = strlen(name);
185 path_name=xbt_new0(char,path_name_len+1);
188 xbt_dynar_foreach(surf_path,i,path) {
189 if(path_name_len < strlen(path)+strlen(name)+1) {
190 path_name_len = strlen(path)+strlen(name)+1; /* plus '/' */
191 path_name=xbt_realloc(path_name,path_name_len+1);
193 sprintf(path_name,"%s/%s",path, name);
194 file = fopen(path_name,mode);
195 if (file) return file;
201 void surf_finalize(void)
204 surf_resource_t resource = NULL;
206 xbt_dynar_foreach(resource_list, i, resource) {
207 resource->common_private->finalize();
211 lmm_system_free(maxmin_system);
212 maxmin_system = NULL;
215 tmgr_history_free(history);
219 xbt_dynar_free(&resource_list);
222 xbt_dynar_free(&surf_path);
225 surf_parse_lex_destroy();
232 double surf_solve(void)
234 static int first_run = 1;
237 double next_event_date = -1.0;
238 double resource_next_action_end = -1.0;
240 surf_resource_object_t resource_obj = NULL;
241 surf_resource_t resource = NULL;
242 tmgr_trace_event_t event = NULL;
246 while ((next_event_date = tmgr_history_next_date(history)) != -1.0) {
247 if (next_event_date > NOW)
250 tmgr_history_get_next_event_leq(history, next_event_date,
252 (void **) &resource_obj))) {
253 resource_obj->resource->common_private->
254 update_resource_state(resource_obj, event, value);
257 xbt_dynar_foreach(resource_list, i, resource) {
258 resource->common_private->update_actions_state(NOW, 0.0);
266 xbt_dynar_foreach(resource_list, i, resource) {
267 resource_next_action_end =
268 resource->common_private->share_resources(NOW);
269 if (((min < 0.0) || (resource_next_action_end < min))
270 && (resource_next_action_end >= 0.0))
271 min = resource_next_action_end;
277 while ((next_event_date = tmgr_history_next_date(history)) != -1.0) {
278 if (next_event_date > NOW + min)
281 tmgr_history_get_next_event_leq(history, next_event_date,
283 (void **) &resource_obj))) {
284 if (resource_obj->resource->common_private->
285 resource_used(resource_obj)) {
286 min = next_event_date - NOW;
288 /* update state of resource_obj according to new value. Does not touch lmm.
289 It will be modified if needed when updating actions */
290 resource_obj->resource->common_private->
291 update_resource_state(resource_obj, event, value);
296 xbt_dynar_foreach(resource_list, i, resource) {
297 resource->common_private->update_actions_state(NOW, min);
305 double surf_get_clock(void)