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_kernel, surf,
12 "Logging specific to SURF (kernel)");
14 typedef struct surf_resource_object {
15 surf_resource_t resource;
16 } s_surf_resource_object_t, *surf_resource_object_t;
18 static double NOW = 0;
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 const char *surf_action_state_names[6] = {
26 "SURF_ACTION_RUNNING",
29 "SURF_ACTION_TO_FREE",
30 "SURF_ACTION_NOT_IN_THE_SYSTEM"
33 double generic_maxmin_share_resources(xbt_swag_t running_actions,
36 return generic_maxmin_share_resources2(running_actions, offset,
40 double generic_maxmin_share_resources2(xbt_swag_t running_actions,
44 surf_action_t action = NULL;
47 #define VARIABLE(action) (*((lmm_variable_t*)(((char *) (action)) + (offset))))
51 xbt_swag_foreach(action, running_actions) {
52 value = lmm_variable_getvalue(VARIABLE(action));
53 if ((value > 0) || (action->max_duration >= 0))
61 min = action->remains / value;
62 if ((action->max_duration >= 0) && (action->max_duration < min))
63 min = action->max_duration;
65 min = action->max_duration;
68 for (action = xbt_swag_getNext(action, running_actions->offset);
70 action = xbt_swag_getNext(action, running_actions->offset)) {
71 value = lmm_variable_getvalue(VARIABLE(action));
73 value = action->remains / value;
77 if ((action->max_duration >= 0) && (action->max_duration < min))
78 min = action->max_duration;
84 e_surf_action_state_t surf_action_get_state(surf_action_t action)
86 surf_action_state_t action_state =
87 &(action->resource_type->common_public->states);
89 if (action->state_set == action_state->ready_action_set)
90 return SURF_ACTION_READY;
91 if (action->state_set == action_state->running_action_set)
92 return SURF_ACTION_RUNNING;
93 if (action->state_set == action_state->failed_action_set)
94 return SURF_ACTION_FAILED;
95 if (action->state_set == action_state->done_action_set)
96 return SURF_ACTION_DONE;
97 return SURF_ACTION_NOT_IN_THE_SYSTEM;
100 void surf_action_free(surf_action_t * action)
102 (*action)->resource_type->common_public->action_cancel(*action);
107 void surf_action_change_state(surf_action_t action,
108 e_surf_action_state_t state)
110 surf_action_state_t action_state =
111 &(action->resource_type->common_public->states);
112 XBT_IN2("(%p,%s)", action, surf_action_state_names[state]);
113 xbt_swag_remove(action, action->state_set);
115 if (state == SURF_ACTION_READY)
116 action->state_set = action_state->ready_action_set;
117 else if (state == SURF_ACTION_RUNNING)
118 action->state_set = action_state->running_action_set;
119 else if (state == SURF_ACTION_FAILED)
120 action->state_set = action_state->failed_action_set;
121 else if (state == SURF_ACTION_DONE)
122 action->state_set = action_state->done_action_set;
124 action->state_set = NULL;
126 if (action->state_set)
127 xbt_swag_insert(action, action->state_set);
131 void surf_action_set_data(surf_action_t action,
137 void surf_init(int *argc, char **argv)
142 xbt_init(argc, argv);
144 const char *initial_path = "./";
145 surf_path = xbt_dynar_new(sizeof(char*), NULL);
146 xbt_dynar_push(surf_path,&initial_path);
148 for (i=1; i<*argc; i++) {
149 if (!strncmp(argv[i],"--surf-path=",strlen("--surf-path="))) {
150 opt=strchr(argv[i],'=');
152 xbt_dynar_push(surf_path,&opt);
153 /*remove this from argv*/
154 for (j=i+1; j<*argc; j++) {
159 i--; /* compensate effect of next loop incrementation */
164 resource_list = xbt_dynar_new(sizeof(surf_resource_private_t), NULL);
166 history = tmgr_history_new();
168 maxmin_system = lmm_system_new();
171 static char* path_name = NULL;
172 FILE *surf_fopen(const char *name, const char *mode)
177 int path_name_len = 0; /* don't count '\0' */
179 xbt_assert0(name, "Need a non-NULL file name");
181 xbt_assert0(surf_path,"surf_init has to be called before using surf_fopen");
183 if (name[0] == '/') { /* don't mess with absolute file names */
184 return fopen(name,mode);
186 } else { /* search relative files in the path */
189 path_name_len = strlen(name);
190 path_name=xbt_new0(char,path_name_len+1);
193 xbt_dynar_foreach(surf_path,i,path) {
194 if(path_name_len < strlen(path)+strlen(name)+1) {
195 path_name_len = strlen(path)+strlen(name)+1; /* plus '/' */
196 path_name=xbt_realloc(path_name,path_name_len+1);
198 sprintf(path_name,"%s/%s",path, name);
199 file = fopen(path_name,mode);
200 if (file) return file;
209 surf_resource_t resource = NULL;
211 xbt_dynar_foreach(resource_list, i, resource) {
212 resource->common_private->finalize();
216 lmm_system_free(maxmin_system);
217 maxmin_system = NULL;
220 tmgr_history_free(history);
224 xbt_dynar_free(&resource_list);
227 xbt_dynar_free(&surf_path);
230 surf_parse_lex_destroy();
238 double surf_solve(void)
240 static int first_run = 1;
243 double next_event_date = -1.0;
244 double resource_next_action_end = -1.0;
246 surf_resource_object_t resource_obj = NULL;
247 surf_resource_t resource = NULL;
248 tmgr_trace_event_t event = NULL;
252 DEBUG0("First Run! Let's \"purge\" events and put resources in the right state");
253 while ((next_event_date = tmgr_history_next_date(history)) != -1.0) {
254 if (next_event_date > NOW)
257 tmgr_history_get_next_event_leq(history, next_event_date,
259 (void **) &resource_obj))) {
260 resource_obj->resource->common_private->
261 update_resource_state(resource_obj, event, value);
264 xbt_dynar_foreach(resource_list, i, resource) {
265 resource->common_private->update_actions_state(NOW, 0.0);
273 DEBUG0("Looking for next action end");
274 xbt_dynar_foreach(resource_list, i, resource) {
275 resource_next_action_end =
276 resource->common_private->share_resources(NOW);
277 DEBUG2("Resource [%s] : next action end = %f",resource->common_public->name,
278 resource_next_action_end);
279 if (((min < 0.0) || (resource_next_action_end < min))
280 && (resource_next_action_end >= 0.0))
281 min = resource_next_action_end;
283 DEBUG1("Next action end : %f", min);
288 DEBUG0("Looking for next event");
289 while ((next_event_date = tmgr_history_next_date(history)) != -1.0) {
290 DEBUG1("Next event : %f",next_event_date);
291 if (next_event_date > NOW + min)
293 DEBUG0("Updating resources");
295 tmgr_history_get_next_event_leq(history, next_event_date,
297 (void **) &resource_obj))) {
298 if (resource_obj->resource->common_private->
299 resource_used(resource_obj)) {
300 min = next_event_date - NOW;
301 DEBUG1("This event will modify resource state. Next event set to %f", min);
303 /* update state of resource_obj according to new value. Does not touch lmm.
304 It will be modified if needed when updating actions */
305 resource_obj->resource->common_private->
306 update_resource_state(resource_obj, event, value);
310 DEBUG1("Duration set to %f", min);
312 xbt_dynar_foreach(resource_list, i, resource) {
313 resource->common_private->update_actions_state(NOW, min);
321 double surf_get_clock(void)