1 /* Copyright (c) 2009, 2010. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
7 #include "surf_private.h"
8 #include "network_private.h"
9 #include "xbt/mallocator.h"
11 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_kernel);
17 const char *surf_action_state_names[6] = {
19 "SURF_ACTION_RUNNING",
22 "SURF_ACTION_TO_FREE",
23 "SURF_ACTION_NOT_IN_THE_SYSTEM"
26 /* Surf actions mallocator */
27 static xbt_mallocator_t action_mallocator = NULL;
28 static int action_mallocator_allocated_size = 0;
29 static void* surf_action_mallocator_new_f(void);
30 #define surf_action_mallocator_free_f xbt_free_f
31 static void surf_action_mallocator_reset_f(void* action);
34 * \brief Initializes the action module of Surf.
36 void surf_action_init(void) {
38 /* the action mallocator will always provide actions of the following size,
39 * so this size should be set to the maximum size of the surf action structures
41 action_mallocator_allocated_size = sizeof(s_surf_action_network_CM02_t);
42 action_mallocator = xbt_mallocator_new(65536, surf_action_mallocator_new_f,
43 surf_action_mallocator_free_f, surf_action_mallocator_reset_f);
47 * \brief Uninitializes the action module of Surf.
49 void surf_action_exit(void) {
51 xbt_mallocator_free(action_mallocator);
54 static void* surf_action_mallocator_new_f(void) {
55 return xbt_malloc(action_mallocator_allocated_size);
58 static void surf_action_mallocator_reset_f(void* action) {
59 memset(action, 0, action_mallocator_allocated_size);
62 void *surf_action_new(size_t size, double cost, surf_model_t model,
65 xbt_assert(size <= action_mallocator_allocated_size,
66 "Cannot create a surf action of size %zu: the mallocator only provides actions of size %d",
67 size, action_mallocator_allocated_size);
69 surf_action_t action = xbt_mallocator_get(action_mallocator);
72 action->remains = cost;
73 action->priority = 1.0;
74 action->max_duration = NO_MAX_DURATION;
75 action->start = surf_get_clock();
76 action->finish = -1.0;
77 action->model_type = model;
79 action->category = NULL;
83 action->state_set = model->states.failed_action_set;
85 action->state_set = model->states.running_action_set;
87 xbt_swag_insert(action, action->state_set);
92 e_surf_action_state_t surf_action_state_get(surf_action_t action)
94 surf_action_state_t action_state = &(action->model_type->states);
96 if (action->state_set == action_state->ready_action_set)
97 return SURF_ACTION_READY;
98 if (action->state_set == action_state->running_action_set)
99 return SURF_ACTION_RUNNING;
100 if (action->state_set == action_state->failed_action_set)
101 return SURF_ACTION_FAILED;
102 if (action->state_set == action_state->done_action_set)
103 return SURF_ACTION_DONE;
104 return SURF_ACTION_NOT_IN_THE_SYSTEM;
107 double surf_action_get_start_time(surf_action_t action)
109 return action->start;
112 double surf_action_get_finish_time(surf_action_t action)
114 /* keep the function behavior, some models (cpu_ti) change the finish time before the action end */
115 return action->remains == 0 ? action->finish : -1;
118 XBT_INLINE void surf_action_free(surf_action_t * action)
120 xbt_mallocator_release(action_mallocator, *action);
124 void surf_action_state_set(surf_action_t action,
125 e_surf_action_state_t state)
127 surf_action_state_t action_state = &(action->model_type->states);
128 XBT_IN("(%p,%s)", action, surf_action_state_names[state]);
129 xbt_swag_remove(action, action->state_set);
131 if (state == SURF_ACTION_READY)
132 action->state_set = action_state->ready_action_set;
133 else if (state == SURF_ACTION_RUNNING)
134 action->state_set = action_state->running_action_set;
135 else if (state == SURF_ACTION_FAILED)
136 action->state_set = action_state->failed_action_set;
137 else if (state == SURF_ACTION_DONE)
138 action->state_set = action_state->done_action_set;
140 action->state_set = NULL;
142 if (action->state_set)
143 xbt_swag_insert(action, action->state_set);
147 void surf_action_data_set(surf_action_t action, void *data)
152 XBT_INLINE void surf_action_ref(surf_action_t action)
161 /* added to manage the communication action's heap */
162 void surf_action_lmm_update_index_heap(void *action, int i) {
163 surf_action_lmm_t a = action;
166 /* insert action on heap using a given key and a hat (heap_action_type)
167 * a hat can be of three types for communications:
169 * NORMAL = this is a normal heap entry stating the date to finish transmitting
170 * LATENCY = this is a heap entry to warn us when the latency is payed
171 * MAX_DURATION =this is a heap entry to warn us when the max_duration limit is reached
173 void surf_action_lmm_heap_insert(xbt_heap_t heap, surf_action_lmm_t action, double key,
174 enum heap_action_type hat)
177 xbt_heap_push(heap, action, key);
180 void surf_action_lmm_heap_remove(xbt_heap_t heap, surf_action_lmm_t action)
182 action->hat = NOTSET;
183 if (action->index_heap >= 0) {
184 xbt_heap_remove(heap, action->index_heap);
188 void surf_action_cancel(surf_action_t action)
190 surf_model_t model = action->model_type;
191 surf_action_state_set(action, SURF_ACTION_FAILED);
192 if (model->model_private->update_mechanism == UM_LAZY) {
193 xbt_swag_remove(action, model->model_private->modified_set);
194 surf_action_lmm_heap_remove(model->model_private->action_heap,(surf_action_lmm_t)action);
199 int surf_action_unref(surf_action_t action)
201 surf_model_t model = action->model_type;
203 if (!action->refcount) {
204 xbt_swag_remove(action, action->state_set);
205 if (((surf_action_lmm_t) action)->variable)
206 lmm_variable_free(model->model_private->maxmin_system,
207 ((surf_action_lmm_t) action)->variable);
208 if (model->model_private->update_mechanism == UM_LAZY) {
209 /* remove from heap */
210 surf_action_lmm_heap_remove(model->model_private->action_heap,(surf_action_lmm_t)action);
211 xbt_swag_remove(action, model->model_private->modified_set);
214 xbt_free(action->category);
216 surf_action_free(&action);
222 void surf_action_suspend(surf_action_t action)
224 surf_model_t model = action->model_type;
225 XBT_IN("(%p)", action);
226 if (((surf_action_lmm_t) action)->suspended != 2) {
227 lmm_update_variable_weight(model->model_private->maxmin_system,
228 ((surf_action_lmm_t) action)->variable,
230 ((surf_action_lmm_t) action)->suspended = 1;
231 if (model->model_private->update_mechanism == UM_LAZY)
232 surf_action_lmm_heap_remove(model->model_private->action_heap,(surf_action_lmm_t)action);
237 void surf_action_resume(surf_action_t action)
239 surf_model_t model = action->model_type;
240 XBT_IN("(%p)", action);
241 if (((surf_action_lmm_t) action)->suspended != 2) {
242 lmm_update_variable_weight(model->model_private->maxmin_system,
243 ((surf_action_lmm_t) action)->variable,
245 ((surf_action_lmm_t) action)->suspended = 0;
246 if (model->model_private->update_mechanism == UM_LAZY)
247 surf_action_lmm_heap_remove(model->model_private->action_heap,(surf_action_lmm_t)action);
252 int surf_action_is_suspended(surf_action_t action)
254 return (((surf_action_lmm_t) action)->suspended == 1);
257 void surf_action_set_max_duration(surf_action_t action, double duration)
259 surf_model_t model = action->model_type;
260 XBT_IN("(%p,%g)", action, duration);
261 action->max_duration = duration;
262 if (model->model_private->update_mechanism == UM_LAZY) // remove action from the heap
263 surf_action_lmm_heap_remove(model->model_private->action_heap,(surf_action_lmm_t)action);
267 void surf_action_set_priority(surf_action_t action, double priority)
269 surf_model_t model = action->model_type;
270 XBT_IN("(%p,%g)", action, priority);
271 action->priority = priority;
272 lmm_update_variable_weight(model->model_private->maxmin_system,
273 ((surf_action_lmm_t) action)->variable,
276 if (model->model_private->update_mechanism == UM_LAZY)
277 surf_action_lmm_heap_remove(model->model_private->action_heap,(surf_action_lmm_t)action);
282 void surf_action_set_category(surf_action_t action,
283 const char *category)
285 XBT_IN("(%p,%s)", action, category);
286 action->category = xbt_strdup(category);
291 void generic_update_action_remaining_lazy( surf_action_lmm_t action, double now)
295 if(action->generic_action.model_type == surf_network_model)
297 if (action->suspended != 0)
302 xbt_assert(action->generic_action.state_set == surf_cpu_model->states.running_action_set,
303 "You're updating an action that is not running.");
305 /* bogus priority, skip it */
306 xbt_assert(action->generic_action.priority > 0,
307 "You're updating an action that seems suspended.");
310 delta = now - action->last_update;
312 if (action->generic_action.remains > 0) {
313 XBT_DEBUG("Updating action(%p): remains was %lf, last_update was: %lf", action, action->generic_action.remains, action->last_update);
314 double_update(&(action->generic_action.remains),
315 action->last_value * delta);
318 if (action->generic_action.model_type == surf_cpu_model && TRACE_is_enabled()) {
319 surf_resource_t cpu =
320 lmm_constraint_id(lmm_get_cnst_from_var
321 (surf_cpu_model->model_private->maxmin_system,
322 action->variable, 0));
323 TRACE_surf_host_set_utilization(cpu->name,
324 action->generic_action.category,
327 now - action->last_update);
330 XBT_DEBUG("Updating action(%p): remains is now %lf", action,
331 action->generic_action.remains);
334 if(action->generic_action.model_type == surf_network_model)
336 if (((surf_action_t)action)->max_duration != NO_MAX_DURATION)
337 double_update(&(((surf_action_t)action)->max_duration), delta);
339 if ((((surf_action_t)action)->remains <= 0) &&
340 (lmm_get_variable_weight(action->variable) > 0)) {
341 ((surf_action_t)action)->finish = surf_get_clock();
342 surf_network_model->action_state_set((surf_action_t) action,
345 surf_action_lmm_heap_remove(surf_network_model->model_private->action_heap,(surf_action_lmm_t)action);
346 } else if (((((surf_action_t)action)->max_duration != NO_MAX_DURATION)
347 && (((surf_action_t)action)->max_duration <= 0))) {
348 ((surf_action_t)action)->finish = surf_get_clock();
349 surf_network_model->action_state_set((surf_action_t) action,
351 surf_action_lmm_heap_remove(surf_network_model->model_private->action_heap,(surf_action_lmm_t)action);
355 action->last_update = now;
356 action->last_value = lmm_variable_getvalue(action->variable);
359 double surf_action_get_remains(surf_action_t action)
361 XBT_IN("(%p)", action);
362 surf_model_t model = action->model_type;
363 /* update remains before return it */
364 if (model->model_private->update_mechanism == UM_LAZY) /* update remains before return it */
365 generic_update_action_remaining_lazy((surf_action_lmm_t)action, surf_get_clock());
367 return action->remains;