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 typedef struct surf_resource_object {
12 surf_resource_t resource;
13 } s_surf_resource_object_t, *surf_resource_object_t;
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_global, surf,
16 "Logging specific to the SURF global module");
19 static double NOW = 0;
21 xbt_dynar_t resource_list = NULL;
22 tmgr_history_t history = NULL;
23 lmm_system_t maxmin_system = NULL;
24 xbt_dynar_t surf_path = NULL;
26 double generic_maxmin_share_resources(xbt_swag_t running_actions,
29 return generic_maxmin_share_resources2(running_actions, offset,
33 double generic_maxmin_share_resources2(xbt_swag_t running_actions,
37 surf_action_t action = NULL;
40 #define VARIABLE(action) (*((lmm_variable_t*)(((char *) (action)) + (offset))))
44 xbt_swag_foreach(action, running_actions) {
45 value = lmm_variable_getvalue(VARIABLE(action));
46 if ((value > 0) || (action->max_duration >= 0))
54 min = value = action->remains / value;
55 if ((action->max_duration >= 0) && (action->max_duration < min))
56 min = action->max_duration;
58 min = action->max_duration;
61 for (action = xbt_swag_getNext(action, running_actions->offset);
63 action = xbt_swag_getNext(action, running_actions->offset)) {
64 value = lmm_variable_getvalue(VARIABLE(action));
66 value = action->remains / value;
70 if ((action->max_duration >= 0) && (action->max_duration < min))
71 min = action->max_duration;
77 e_surf_action_state_t surf_action_get_state(surf_action_t action)
79 surf_action_state_t action_state =
80 &(action->resource_type->common_public->states);
82 if (action->state_set == action_state->ready_action_set)
83 return SURF_ACTION_READY;
84 if (action->state_set == action_state->running_action_set)
85 return SURF_ACTION_RUNNING;
86 if (action->state_set == action_state->failed_action_set)
87 return SURF_ACTION_FAILED;
88 if (action->state_set == action_state->done_action_set)
89 return SURF_ACTION_DONE;
90 return SURF_ACTION_NOT_IN_THE_SYSTEM;
93 void surf_action_free(surf_action_t * action)
95 (*action)->resource_type->common_public->action_cancel(*action);
100 void surf_action_change_state(surf_action_t action,
101 e_surf_action_state_t state)
103 surf_action_state_t action_state =
104 &(action->resource_type->common_public->states);
106 xbt_swag_remove(action, action->state_set);
108 if (state == SURF_ACTION_READY)
109 action->state_set = action_state->ready_action_set;
110 else if (state == SURF_ACTION_RUNNING)
111 action->state_set = action_state->running_action_set;
112 else if (state == SURF_ACTION_FAILED)
113 action->state_set = action_state->failed_action_set;
114 else if (state == SURF_ACTION_DONE)
115 action->state_set = action_state->done_action_set;
117 action->state_set = NULL;
119 if (action->state_set)
120 xbt_swag_insert(action, action->state_set);
123 void surf_action_set_data(surf_action_t action,
129 /* HACKHACK: msg_global must be set to a sensible value (like NULL) to use the logging mecanisme
130 * since log_default_appender use xbt_procname which, in SG, is defined in src/msg/m_process.c
131 * (in RL, xbt_procname is defined in src/gras/Virtu/rl_process.c)
133 extern void *msg_global;
135 void surf_init(int *argc, char **argv)
140 xbt_init(argc, argv);
141 msg_global=NULL; /* see HACKHACK note above */
143 const char *initial_path = "./";
144 surf_path = xbt_dynar_new(sizeof(char*), NULL);
145 xbt_dynar_push(surf_path,&initial_path);
147 for (i=1; i<*argc; i++) {
148 if (!strncmp(argv[i],"--surf-path=",strlen("--surf-path="))) {
149 opt=strchr(argv[i],'=');
151 xbt_dynar_push(surf_path,&opt);
152 /*remove this from argv*/
153 for (j=i+1; j<*argc; j++) {
158 i--; /* compensate effect of next loop incrementation */
163 resource_list = xbt_dynar_new(sizeof(surf_resource_private_t), NULL);
165 history = tmgr_history_new();
167 maxmin_system = lmm_system_new();
170 static char* path_name = NULL;
171 FILE *surf_fopen(const char *name, const char *mode)
176 int path_name_len = 0; /* don't count '\0' */
178 xbt_assert0(name, "Need a non-NULL file name");
180 xbt_assert0(surf_path,"surf_init has to be called before using surf_fopen");
182 if (name[0] == '/') { /* don't mess with absolute file names */
183 return fopen(name,mode);
185 } else { /* search relative files in the path */
188 path_name_len = strlen(name);
189 path_name=xbt_new0(char,path_name_len+1);
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);
197 sprintf(path_name,"%s/%s",path, name);
198 file = fopen(path_name,mode);
199 if (file) return file;
205 void surf_finalize(void)
208 surf_resource_t resource = NULL;
210 xbt_dynar_foreach(resource_list, i, resource) {
211 resource->common_private->finalize();
215 lmm_system_free(maxmin_system);
216 maxmin_system = NULL;
219 tmgr_history_free(history);
223 xbt_dynar_free(&resource_list);
226 xbt_dynar_free(&surf_path);
229 surf_parse_lex_destroy();
236 double surf_solve(void)
238 static int first_run = 1;
241 double next_event_date = -1.0;
242 double resource_next_action_end = -1.0;
244 surf_resource_object_t resource_obj = NULL;
245 surf_resource_t resource = NULL;
246 tmgr_trace_event_t event = NULL;
250 while ((next_event_date = tmgr_history_next_date(history)) != -1.0) {
251 if (next_event_date > NOW)
254 tmgr_history_get_next_event_leq(history, next_event_date,
256 (void **) &resource_obj))) {
257 resource_obj->resource->common_private->
258 update_resource_state(resource_obj, event, value);
261 xbt_dynar_foreach(resource_list, i, resource) {
262 resource->common_private->update_actions_state(NOW, 0.0);
270 xbt_dynar_foreach(resource_list, i, resource) {
271 resource_next_action_end =
272 resource->common_private->share_resources(NOW);
273 if (((min < 0.0) || (resource_next_action_end < min))
274 && (resource_next_action_end >= 0.0))
275 min = resource_next_action_end;
281 while ((next_event_date = tmgr_history_next_date(history)) != -1.0) {
282 if (next_event_date > NOW + min)
285 tmgr_history_get_next_event_leq(history, next_event_date,
287 (void **) &resource_obj))) {
288 if (resource_obj->resource->common_private->
289 resource_used(resource_obj)) {
290 min = next_event_date - NOW;
292 /* update state of resource_obj according to new value. Does not touch lmm.
293 It will be modified if needed when updating actions */
294 resource_obj->resource->common_private->
295 update_resource_state(resource_obj, event, value);
300 xbt_dynar_foreach(resource_list, i, resource) {
301 resource->common_private->update_actions_state(NOW, min);
309 double surf_get_clock(void)