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 surf_action_t action = NULL;
28 #define VARIABLE(action) (*((lmm_variable_t*)(((char *) (action)) + (offset))))
30 lmm_solve(maxmin_system);
32 xbt_swag_foreach(action, running_actions) {
33 value = lmm_variable_getvalue(VARIABLE(action));
34 if ((value > 0) || (action->max_duration >= 0))
42 min = value = action->remains / value;
43 if ((action->max_duration >= 0) && (action->max_duration < min))
44 min = action->max_duration;
46 min = action->max_duration;
49 for (action = xbt_swag_getNext(action, running_actions->offset);
51 action = xbt_swag_getNext(action, running_actions->offset)) {
52 value = lmm_variable_getvalue(VARIABLE(action));
54 value = action->remains / value;
58 if ((action->max_duration >= 0) && (action->max_duration < min))
59 min = action->max_duration;
65 e_surf_action_state_t surf_action_get_state(surf_action_t action)
67 surf_action_state_t action_state =
68 &(action->resource_type->common_public->states);
70 if (action->state_set == action_state->ready_action_set)
71 return SURF_ACTION_READY;
72 if (action->state_set == action_state->running_action_set)
73 return SURF_ACTION_RUNNING;
74 if (action->state_set == action_state->failed_action_set)
75 return SURF_ACTION_FAILED;
76 if (action->state_set == action_state->done_action_set)
77 return SURF_ACTION_DONE;
78 return SURF_ACTION_NOT_IN_THE_SYSTEM;
81 void surf_action_free(surf_action_t * action)
83 (*action)->resource_type->common_public->action_cancel(*action);
88 void surf_action_change_state(surf_action_t action,
89 e_surf_action_state_t state)
91 surf_action_state_t action_state =
92 &(action->resource_type->common_public->states);
94 xbt_swag_remove(action, action->state_set);
96 if (state == SURF_ACTION_READY)
97 action->state_set = action_state->ready_action_set;
98 else if (state == SURF_ACTION_RUNNING)
99 action->state_set = action_state->running_action_set;
100 else if (state == SURF_ACTION_FAILED)
101 action->state_set = action_state->failed_action_set;
102 else if (state == SURF_ACTION_DONE)
103 action->state_set = action_state->done_action_set;
105 action->state_set = NULL;
107 if (action->state_set)
108 xbt_swag_insert(action, action->state_set);
111 void surf_action_set_data(surf_action_t action,
117 void surf_init(int *argc, char **argv)
122 xbt_init(argc, argv);
124 const char *initial_path = "./";
125 surf_path = xbt_dynar_new(sizeof(char*), NULL);
126 xbt_dynar_push(surf_path,&initial_path);
128 for (i=1; i<*argc; i++) {
129 if (!strncmp(argv[i],"--surf-path=",strlen("--surf-path="))) {
130 opt=strchr(argv[i],'=');
132 xbt_dynar_push(surf_path,&opt);
133 /*remove this from argv*/
134 for (j=i+1; j<*argc; j++) {
139 i--; /* compensate effect of next loop incrementation */
144 resource_list = xbt_dynar_new(sizeof(surf_resource_private_t), NULL);
146 history = tmgr_history_new();
148 maxmin_system = lmm_system_new();
151 static char* path_name = NULL;
152 FILE *surf_fopen(const char *name, const char *mode)
158 xbt_assert0(surf_path,"surf_init has to be called before using surf_fopen");
159 if(!path_name) path_name=xbt_new0(char,strlen(name)+1);
161 xbt_dynar_foreach(surf_path,i,path) {
162 if(strlen(path_name)<strlen(path)+strlen(name)+2)
163 path_name=xbt_realloc(path_name,strlen(path)+strlen(name)+2);
164 strcpy(path_name, path);
165 strcat(path_name,"/");
166 strcat(path_name,name);
167 file = fopen(path_name,mode);
168 if(file) return file;
173 void surf_finalize(void)
176 surf_resource_t resource = NULL;
178 xbt_dynar_foreach(resource_list, i, resource) {
179 resource->common_private->finalize();
183 lmm_system_free(maxmin_system);
184 maxmin_system = NULL;
187 tmgr_history_free(history);
191 xbt_dynar_free(&resource_list);
194 xbt_dynar_free(&surf_path);
197 surf_parse_lex_destroy();
204 double surf_solve(void)
206 static int first_run = 1;
209 double next_event_date = -1.0;
210 double resource_next_action_end = -1.0;
212 surf_resource_object_t resource_obj = NULL;
213 surf_resource_t resource = NULL;
214 tmgr_trace_event_t event = NULL;
218 while ((next_event_date = tmgr_history_next_date(history)) != -1.0) {
219 if (next_event_date > NOW)
222 tmgr_history_get_next_event_leq(history, next_event_date,
224 (void **) &resource_obj))) {
225 resource_obj->resource->common_private->
226 update_resource_state(resource_obj, event, value);
229 xbt_dynar_foreach(resource_list, i, resource) {
230 resource->common_private->update_actions_state(NOW, 0.0);
238 xbt_dynar_foreach(resource_list, i, resource) {
239 resource_next_action_end =
240 resource->common_private->share_resources(NOW);
241 if (((min < 0.0) || (resource_next_action_end < min))
242 && (resource_next_action_end >= 0.0))
243 min = resource_next_action_end;
249 while ((next_event_date = tmgr_history_next_date(history)) != -1.0) {
250 if (next_event_date > NOW + min)
253 tmgr_history_get_next_event_leq(history, next_event_date,
255 (void **) &resource_obj))) {
256 if (resource_obj->resource->common_private->
257 resource_used(resource_obj)) {
258 min = next_event_date - NOW;
260 /* update state of resource_obj according to new value. Does not touch lmm.
261 It will be modified if needed when updating actions */
262 resource_obj->resource->common_private->
263 update_resource_state(resource_obj, event, value);
268 xbt_dynar_foreach(resource_list, i, resource) {
269 resource->common_private->update_actions_state(NOW, min);
277 double surf_get_clock(void)