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 static double NOW = 0;
13 xbt_dynar_t resource_list = NULL;
14 tmgr_history_t history = NULL;
15 lmm_system_t maxmin_system = NULL;
17 double generic_maxmin_share_resources(xbt_swag_t running_actions,
20 surf_action_t action = NULL;
23 #define VARIABLE(action) (*((lmm_variable_t*)(((char *) (action)) + (offset))))
25 lmm_solve(maxmin_system);
27 xbt_swag_foreach(action, running_actions) {
28 value = lmm_variable_getvalue(VARIABLE(action));
29 if ((value > 0) || (action->max_duration >= 0))
37 min = value = action->remains / value;
38 if ((action->max_duration >= 0) && (action->max_duration < min))
39 min = action->max_duration;
41 min = action->max_duration;
44 for (action = xbt_swag_getNext(action, running_actions->offset);
46 action = xbt_swag_getNext(action, running_actions->offset)) {
47 value = lmm_variable_getvalue(VARIABLE(action));
49 value = action->remains / value;
53 if ((action->max_duration >= 0) && (action->max_duration < min))
54 min = action->max_duration;
60 e_surf_action_state_t surf_action_get_state(surf_action_t action)
62 surf_action_state_t action_state =
63 &(action->resource_type->common_public->states);
65 if (action->state_set == action_state->ready_action_set)
66 return SURF_ACTION_READY;
67 if (action->state_set == action_state->running_action_set)
68 return SURF_ACTION_RUNNING;
69 if (action->state_set == action_state->failed_action_set)
70 return SURF_ACTION_FAILED;
71 if (action->state_set == action_state->done_action_set)
72 return SURF_ACTION_DONE;
73 return SURF_ACTION_NOT_IN_THE_SYSTEM;
76 void surf_action_free(surf_action_t * action)
78 (*action)->resource_type->common_public->action_cancel(*action);
83 void surf_action_change_state(surf_action_t action,
84 e_surf_action_state_t state)
86 surf_action_state_t action_state =
87 &(action->resource_type->common_public->states);
89 xbt_swag_remove(action, action->state_set);
91 if (state == SURF_ACTION_READY)
92 action->state_set = action_state->ready_action_set;
93 else if (state == SURF_ACTION_RUNNING)
94 action->state_set = action_state->running_action_set;
95 else if (state == SURF_ACTION_FAILED)
96 action->state_set = action_state->failed_action_set;
97 else if (state == SURF_ACTION_DONE)
98 action->state_set = action_state->done_action_set;
100 action->state_set = NULL;
102 if (action->state_set)
103 xbt_swag_insert(action, action->state_set);
106 void surf_action_set_data(surf_action_t action,
112 void surf_init(int *argc, char **argv)
114 xbt_init(argc, argv);
116 resource_list = xbt_dynar_new(sizeof(surf_resource_private_t), NULL);
118 history = tmgr_history_new();
120 maxmin_system = lmm_system_new();
123 void surf_finalize(void)
126 surf_resource_t resource = NULL;
128 xbt_dynar_foreach(resource_list, i, resource) {
129 resource->common_private->finalize();
133 lmm_system_free(maxmin_system);
134 maxmin_system = NULL;
137 tmgr_history_free(history);
141 xbt_dynar_free(&resource_list);
144 surf_parse_lex_destroy();
147 double surf_solve(void)
149 static int first_run = 1;
152 double next_event_date = -1.0;
153 double resource_next_action_end = -1.0;
155 surf_resource_object_t resource_obj = NULL;
156 surf_resource_t resource = NULL;
157 tmgr_trace_event_t event = NULL;
161 while ((next_event_date = tmgr_history_next_date(history)) != -1.0) {
162 if (next_event_date > NOW)
165 tmgr_history_get_next_event_leq(history, next_event_date,
167 (void **) &resource_obj))) {
168 resource_obj->resource->common_private->
169 update_resource_state(resource_obj, event, value);
172 xbt_dynar_foreach(resource_list, i, resource) {
173 resource->common_private->update_actions_state(NOW, 0.0);
181 xbt_dynar_foreach(resource_list, i, resource) {
182 resource_next_action_end =
183 resource->common_private->share_resources(NOW);
184 if (((min < 0.0) || (resource_next_action_end < min))
185 && (resource_next_action_end >= 0.0))
186 min = resource_next_action_end;
192 while ((next_event_date = tmgr_history_next_date(history)) != -1.0) {
193 if (next_event_date > NOW + min)
196 tmgr_history_get_next_event_leq(history, next_event_date,
198 (void **) &resource_obj))) {
199 if (resource_obj->resource->common_private->
200 resource_used(resource_obj)) {
201 min = next_event_date - NOW;
203 /* update state of resource_obj according to new value. Does not touch lmm.
204 It will be modified if needed when updating actions */
205 resource_obj->resource->common_private->
206 update_resource_state(resource_obj, event, value);
211 xbt_dynar_foreach(resource_list, i, resource) {
212 resource->common_private->update_actions_state(NOW, min);
220 double surf_get_clock(void)