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 "maxmin_private.h"
10 #include "surf/datatypes.h"
11 #include "cpu_cas01_private.h"
12 #include "xbt/mallocator.h"
14 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_kernel);
20 const char *surf_action_state_names[6] = {
22 "SURF_ACTION_RUNNING",
25 "SURF_ACTION_TO_FREE",
26 "SURF_ACTION_NOT_IN_THE_SYSTEM"
29 /* Surf actions mallocator */
30 static xbt_mallocator_t action_mallocator = NULL;
31 static int action_mallocator_allocated_size = 0;
32 static void* surf_action_mallocator_new_f(void);
33 #define surf_action_mallocator_free_f xbt_free_f
34 static void surf_action_mallocator_reset_f(void* action);
37 * \brief Initializes the action module of Surf.
39 void surf_action_init(void) {
41 /* the action mallocator will always provide actions of the following size,
42 * so this size should be set to the maximum size of the surf action structures
44 action_mallocator_allocated_size = sizeof(s_surf_action_network_CM02_t);
45 action_mallocator = xbt_mallocator_new(65536, surf_action_mallocator_new_f,
46 surf_action_mallocator_free_f, surf_action_mallocator_reset_f);
50 * \brief Uninitializes the action module of Surf.
52 void surf_action_exit(void) {
54 xbt_mallocator_free(action_mallocator);
57 static void* surf_action_mallocator_new_f(void) {
58 return xbt_malloc(action_mallocator_allocated_size);
61 static void surf_action_mallocator_reset_f(void* action) {
62 memset(action, 0, action_mallocator_allocated_size);
65 void *surf_action_new(size_t size, double cost, surf_model_t model,
68 xbt_assert(size <= action_mallocator_allocated_size,
69 "Cannot create a surf action of size %zu: the mallocator only provides actions of size %d",
70 size, action_mallocator_allocated_size);
72 surf_action_t action = xbt_mallocator_get(action_mallocator);
75 action->remains = cost;
76 action->priority = 1.0;
77 action->max_duration = NO_MAX_DURATION;
78 action->start = surf_get_clock();
79 action->finish = -1.0;
80 action->model_type = model;
82 action->category = NULL;
86 action->state_set = model->states.failed_action_set;
88 action->state_set = model->states.running_action_set;
90 xbt_swag_insert(action, action->state_set);
95 e_surf_action_state_t surf_action_state_get(surf_action_t action)
97 surf_action_state_t action_state = &(action->model_type->states);
99 if (action->state_set == action_state->ready_action_set)
100 return SURF_ACTION_READY;
101 if (action->state_set == action_state->running_action_set)
102 return SURF_ACTION_RUNNING;
103 if (action->state_set == action_state->failed_action_set)
104 return SURF_ACTION_FAILED;
105 if (action->state_set == action_state->done_action_set)
106 return SURF_ACTION_DONE;
107 return SURF_ACTION_NOT_IN_THE_SYSTEM;
110 double surf_action_get_start_time(surf_action_t action)
112 return action->start;
115 double surf_action_get_finish_time(surf_action_t action)
117 /* keep the function behavior, some models (cpu_ti) change the finish time before the action end */
118 return action->remains == 0 ? action->finish : -1;
121 XBT_INLINE void surf_action_free(surf_action_t * action)
123 xbt_mallocator_release(action_mallocator, *action);
127 void surf_action_state_set(surf_action_t action,
128 e_surf_action_state_t state)
130 surf_action_state_t action_state = &(action->model_type->states);
131 XBT_IN("(%p,%s)", action, surf_action_state_names[state]);
132 xbt_swag_remove(action, action->state_set);
134 if (state == SURF_ACTION_READY)
135 action->state_set = action_state->ready_action_set;
136 else if (state == SURF_ACTION_RUNNING)
137 action->state_set = action_state->running_action_set;
138 else if (state == SURF_ACTION_FAILED)
139 action->state_set = action_state->failed_action_set;
140 else if (state == SURF_ACTION_DONE)
141 action->state_set = action_state->done_action_set;
143 action->state_set = NULL;
145 if (action->state_set)
146 xbt_swag_insert(action, action->state_set);
150 void surf_action_data_set(surf_action_t action, void *data)
155 XBT_INLINE void surf_action_ref(surf_action_t action)
164 /* added to manage the communication action's heap */
165 void surf_action_lmm_update_index_heap(void *action, int i) {
166 surf_action_lmm_t a = action;
169 /* insert action on heap using a given key and a hat (heap_action_type)
170 * a hat can be of three types for communications:
172 * NORMAL = this is a normal heap entry stating the date to finish transmitting
173 * LATENCY = this is a heap entry to warn us when the latency is payed
174 * MAX_DURATION =this is a heap entry to warn us when the max_duration limit is reached
176 void surf_action_lmm_heap_insert(xbt_heap_t heap, surf_action_lmm_t action, double key,
177 enum heap_action_type hat)
180 xbt_heap_push(heap, action, key);
183 void surf_action_lmm_heap_remove(xbt_heap_t heap, surf_action_lmm_t action)
185 action->hat = NOTSET;
186 if (action->index_heap >= 0) {
187 xbt_heap_remove(heap, action->index_heap);
191 void surf_action_cancel(surf_action_t action)
193 surf_model_t model = action->model_type;
194 surf_action_state_set(action, SURF_ACTION_FAILED);
195 if (model->model_private->update_mechanism == UM_LAZY) {
196 xbt_swag_remove(action, model->model_private->modified_set);
197 surf_action_lmm_heap_remove(model->model_private->action_heap,(surf_action_lmm_t)action);
202 int surf_action_unref(surf_action_t action)
204 surf_model_t model = action->model_type;
206 if (!action->refcount) {
207 xbt_swag_remove(action, action->state_set);
208 if (((surf_action_lmm_t) action)->variable)
209 lmm_variable_free(model->model_private->maxmin_system,
210 ((surf_action_lmm_t) action)->variable);
211 if (model->model_private->update_mechanism == UM_LAZY) {
212 /* remove from heap */
213 surf_action_lmm_heap_remove(model->model_private->action_heap,(surf_action_lmm_t)action);
214 xbt_swag_remove(action, model->model_private->modified_set);
217 xbt_free(action->category);
219 surf_action_free(&action);
225 void surf_action_suspend(surf_action_t action)
227 surf_model_t model = action->model_type;
228 XBT_IN("(%p)", action);
229 if (((surf_action_lmm_t) action)->suspended != 2) {
230 lmm_update_variable_weight(model->model_private->maxmin_system,
231 ((surf_action_lmm_t) action)->variable,
233 ((surf_action_lmm_t) action)->suspended = 1;
234 if (model->model_private->update_mechanism == UM_LAZY)
235 surf_action_lmm_heap_remove(model->model_private->action_heap,(surf_action_lmm_t)action);
240 void surf_action_resume(surf_action_t action)
242 surf_model_t model = action->model_type;
243 XBT_IN("(%p)", action);
244 if (((surf_action_lmm_t) action)->suspended != 2) {
245 lmm_update_variable_weight(model->model_private->maxmin_system,
246 ((surf_action_lmm_t) action)->variable,
248 ((surf_action_lmm_t) action)->suspended = 0;
249 if (model->model_private->update_mechanism == UM_LAZY)
250 surf_action_lmm_heap_remove(model->model_private->action_heap,(surf_action_lmm_t)action);
255 int surf_action_is_suspended(surf_action_t action)
257 return (((surf_action_lmm_t) action)->suspended == 1);
260 void surf_action_set_max_duration(surf_action_t action, double duration)
262 surf_model_t model = action->model_type;
263 XBT_IN("(%p,%g)", action, duration);
264 action->max_duration = duration;
265 if (model->model_private->update_mechanism == UM_LAZY) // remove action from the heap
266 surf_action_lmm_heap_remove(model->model_private->action_heap,(surf_action_lmm_t)action);
270 void surf_action_set_priority(surf_action_t action, double priority)
272 surf_model_t model = action->model_type;
273 XBT_IN("(%p,%g)", action, priority);
274 action->priority = priority;
275 lmm_update_variable_weight(model->model_private->maxmin_system,
276 ((surf_action_lmm_t) action)->variable,
279 if (model->model_private->update_mechanism == UM_LAZY)
280 surf_action_lmm_heap_remove(model->model_private->action_heap,(surf_action_lmm_t)action);
285 void surf_action_set_category(surf_action_t action,
286 const char *category)
288 XBT_IN("(%p,%s)", action, category);
289 action->category = xbt_strdup(category);
294 void generic_update_action_remaining_lazy( surf_action_lmm_t action, double now)
297 surf_model_t model = action->generic_action.model_type;
299 if(model == surf_network_model)
301 if (action->suspended != 0)
306 xbt_assert(action->generic_action.state_set == model->states.running_action_set,
307 "You're updating an action that is not running.");
309 /* bogus priority, skip it */
310 xbt_assert(action->generic_action.priority > 0,
311 "You're updating an action that seems suspended.");
314 delta = now - action->last_update;
316 if (action->generic_action.remains > 0) {
317 XBT_DEBUG("Updating action(%p): remains was %lf, last_update was: %lf", action, action->generic_action.remains, action->last_update);
318 double_update(&(action->generic_action.remains),
319 action->last_value * delta);
322 if (model == surf_cpu_model && TRACE_is_enabled()) {
323 surf_resource_t cpu =
324 lmm_constraint_id(lmm_get_cnst_from_var
325 (model->model_private->maxmin_system,
326 action->variable, 0));
327 TRACE_surf_host_set_utilization(cpu->name,
328 action->generic_action.category,
331 now - action->last_update);
334 XBT_DEBUG("Updating action(%p): remains is now %lf", action,
335 action->generic_action.remains);
338 if(model == surf_network_model)
340 if (((surf_action_t)action)->max_duration != NO_MAX_DURATION)
341 double_update(&(((surf_action_t)action)->max_duration), delta);
343 if ((((surf_action_t)action)->remains <= 0) &&
344 (lmm_get_variable_weight(action->variable) > 0)) {
345 ((surf_action_t)action)->finish = surf_get_clock();
346 model->action_state_set((surf_action_t) action,
349 surf_action_lmm_heap_remove(model->model_private->action_heap,(surf_action_lmm_t)action);
350 } else if (((((surf_action_t)action)->max_duration != NO_MAX_DURATION)
351 && (((surf_action_t)action)->max_duration <= 0))) {
352 ((surf_action_t)action)->finish = surf_get_clock();
353 model->action_state_set((surf_action_t) action,
355 surf_action_lmm_heap_remove(model->model_private->action_heap,(surf_action_lmm_t)action);
359 action->last_update = now;
360 action->last_value = lmm_variable_getvalue(action->variable);
363 double surf_action_get_remains(surf_action_t action)
365 XBT_IN("(%p)", action);
366 surf_model_t model = action->model_type;
367 /* update remains before return it */
368 if (model->model_private->update_mechanism == UM_LAZY) /* update remains before return it */
369 generic_update_action_remaining_lazy((surf_action_lmm_t)action, surf_get_clock());
371 return action->remains;
375 * Update the CPU total energy for a finished action
378 void update_resource_energy(surf_model_t model, surf_action_lmm_t action)
380 if(model == surf_cpu_model){
381 cpu_Cas01_t cpu_model = (cpu_Cas01_t)lmm_constraint_id(lmm_get_cnst_from_var
382 (model->model_private->maxmin_system,
383 action->variable, 0));
385 double load = lmm_constraint_get_usage(cpu_model->constraint) / cpu_model->power_peak;
386 cpu_update_energy(cpu_model, load);
393 void generic_update_actions_state_lazy(double now, double delta, surf_model_t model)
395 surf_action_lmm_t action;
396 while ((xbt_heap_size(model->model_private->action_heap) > 0)
397 && (double_equals(xbt_heap_maxkey(model->model_private->action_heap), now))) {
398 action = xbt_heap_pop(model->model_private->action_heap);
399 XBT_DEBUG("Something happened to action %p", action);
401 if (TRACE_is_enabled()) {
402 if(model == surf_cpu_model){
403 surf_resource_t cpu =
404 lmm_constraint_id(lmm_get_cnst_from_var
405 (model->model_private->maxmin_system,
406 action->variable, 0));
407 TRACE_surf_host_set_utilization(cpu->name,
408 ((surf_action_t)action)->category,
409 lmm_variable_getvalue(action->variable),
411 now - action->last_update);
414 int n = lmm_get_number_of_cnst_from_var(model->model_private->maxmin_system, action->variable);
416 for (i = 0; i < n; i++){
417 lmm_constraint_t constraint = lmm_get_cnst_from_var(model->model_private->maxmin_system,
420 link_CM02_t link = lmm_constraint_id(constraint);
421 TRACE_surf_link_set_utilization(link->lmm_resource.generic_resource.name,
422 ((surf_action_t)action)->category,
423 (lmm_variable_getvalue(action->variable)*
424 lmm_get_cnst_weight_from_var(model->model_private->maxmin_system,
428 now - action->last_update);
434 if(model == surf_cpu_model){
435 action->generic_action.finish = surf_get_clock();
437 update_resource_energy(model, action);
439 /* set the remains to 0 due to precision problems when updating the remaining amount */
440 action->generic_action.remains = 0;
441 surf_action_state_set((surf_action_t) action, SURF_ACTION_DONE);
442 surf_action_lmm_heap_remove(model->model_private->action_heap,action); //FIXME: strange call since action was already popped
445 // if I am wearing a latency hat
446 if (action->hat == LATENCY) {
447 XBT_DEBUG("Latency paid for action %p. Activating", action);
448 lmm_update_variable_weight(model->model_private->maxmin_system, action->variable,
449 ((surf_action_network_CM02_t)(action))->weight);
450 surf_action_lmm_heap_remove(model->model_private->action_heap,action);
451 action->last_update = surf_get_clock();
453 // if I am wearing a max_duration or normal hat
454 } else if (action->hat == MAX_DURATION ||
455 action->hat == NORMAL) {
456 // no need to communicate anymore
457 // assume that flows that reached max_duration have remaining of 0
458 action->generic_action.finish = surf_get_clock();
459 XBT_DEBUG("Action %p finished", action);
460 action->generic_action.remains = 0;
461 ((surf_action_t)action)->finish = surf_get_clock();
462 model->action_state_set((surf_action_t) action,
464 surf_action_lmm_heap_remove(model->model_private->action_heap,action);
466 if (model->gap_remove && model == surf_network_model)
467 model->gap_remove(action);
472 if (TRACE_is_enabled() && model == surf_cpu_model) {
473 //defining the last timestamp that we can safely dump to trace file
474 //without losing the event ascending order (considering all CPU's)
476 xbt_swag_t running_actions = model->states.running_action_set;
477 xbt_swag_foreach(action, running_actions) {
479 smaller = action->last_update;
482 if (action->last_update < smaller) {
483 smaller = action->last_update;
487 TRACE_last_timestamp_to_dump = smaller;
494 void generic_update_actions_state_full(double now, double delta, surf_model_t model)
496 surf_action_lmm_t action = NULL;
497 surf_action_lmm_t next_action = NULL;
498 xbt_swag_t running_actions = model->states.running_action_set;
500 xbt_swag_foreach_safe(action, next_action, running_actions) {
502 if(model == surf_network_model)
506 if (((surf_action_network_CM02_t)action)->latency > 0) {
507 if (((surf_action_network_CM02_t)action)->latency > deltap) {
508 double_update(&(((surf_action_network_CM02_t)action)->latency), deltap);
511 double_update(&(deltap), ((surf_action_network_CM02_t)action)->latency);
512 ((surf_action_network_CM02_t)action)->latency = 0.0;
514 if ((((surf_action_network_CM02_t)action)->latency == 0.0) && !(action->suspended))
515 lmm_update_variable_weight(model->model_private->maxmin_system, action->variable,
516 ((surf_action_network_CM02_t)action)->weight);
519 if (TRACE_is_enabled()) {
520 int n = lmm_get_number_of_cnst_from_var(model->model_private->maxmin_system, action->variable);
522 for (i = 0; i < n; i++){
523 lmm_constraint_t constraint = lmm_get_cnst_from_var(model->model_private->maxmin_system,
526 link_CM02_t link = lmm_constraint_id(constraint);
527 TRACE_surf_link_set_utilization(link->lmm_resource.generic_resource.name,
528 ((surf_action_t)action)->category,
529 (lmm_variable_getvalue(action->variable)*
530 lmm_get_cnst_weight_from_var(model->model_private->maxmin_system,
538 if (!lmm_get_number_of_cnst_from_var
539 (model->model_private->maxmin_system, action->variable)) {
540 /* There is actually no link used, hence an infinite bandwidth.
541 * This happens often when using models like vivaldi.
542 * In such case, just make sure that the action completes immediately.
544 double_update(&(action->generic_action.remains),
545 action->generic_action.remains);
551 if (TRACE_is_enabled()) {
553 lmm_constraint_id(lmm_get_cnst_from_var
554 (model->model_private->maxmin_system,
555 action->variable, 0));
557 TRACE_surf_host_set_utilization(x->name,
558 ((surf_action_t)action)->category,
559 lmm_variable_getvalue(action->variable),
562 TRACE_last_timestamp_to_dump = now - delta;
567 double_update(&(action->generic_action.remains),
568 lmm_variable_getvalue(action->variable) * delta);
571 if (action->generic_action.max_duration != NO_MAX_DURATION)
572 double_update(&(action->generic_action.max_duration), delta);
575 if ((action->generic_action.remains <= 0) &&
576 (lmm_get_variable_weight(action->variable) > 0)) {
577 action->generic_action.finish = surf_get_clock();
578 surf_action_state_set((surf_action_t) action, SURF_ACTION_DONE);
580 if (model->gap_remove && model == surf_network_model)
581 model->gap_remove(action);
582 } else if ((action->generic_action.max_duration != NO_MAX_DURATION) &&
583 (action->generic_action.max_duration <= 0)) {
584 action->generic_action.finish = surf_get_clock();
585 surf_action_state_set((surf_action_t) action, SURF_ACTION_DONE);
587 if (model->gap_remove && model == surf_network_model)
588 model->gap_remove(action);
591 update_resource_energy(model, action);