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 if( cpu_model->energy->last_updated < surf_get_clock()) {
386 double load = lmm_constraint_get_usage(cpu_model->constraint) / cpu_model->power_peak;
387 cpu_update_energy(cpu_model, load);
395 void generic_update_actions_state_lazy(double now, double delta, surf_model_t model)
397 surf_action_lmm_t action;
398 while ((xbt_heap_size(model->model_private->action_heap) > 0)
399 && (double_equals(xbt_heap_maxkey(model->model_private->action_heap), now))) {
400 action = xbt_heap_pop(model->model_private->action_heap);
401 XBT_DEBUG("Something happened to action %p", action);
403 if (TRACE_is_enabled()) {
404 if(model == surf_cpu_model){
405 surf_resource_t cpu =
406 lmm_constraint_id(lmm_get_cnst_from_var
407 (model->model_private->maxmin_system,
408 action->variable, 0));
409 TRACE_surf_host_set_utilization(cpu->name,
410 ((surf_action_t)action)->category,
411 lmm_variable_getvalue(action->variable),
413 now - action->last_update);
416 int n = lmm_get_number_of_cnst_from_var(model->model_private->maxmin_system, action->variable);
418 for (i = 0; i < n; i++){
419 lmm_constraint_t constraint = lmm_get_cnst_from_var(model->model_private->maxmin_system,
422 link_CM02_t link = lmm_constraint_id(constraint);
423 TRACE_surf_link_set_utilization(link->lmm_resource.generic_resource.name,
424 ((surf_action_t)action)->category,
425 (lmm_variable_getvalue(action->variable)*
426 lmm_get_cnst_weight_from_var(model->model_private->maxmin_system,
430 now - action->last_update);
436 if(model == surf_cpu_model){
437 action->generic_action.finish = surf_get_clock();
439 update_resource_energy(model, action);
441 /* set the remains to 0 due to precision problems when updating the remaining amount */
442 action->generic_action.remains = 0;
443 surf_action_state_set((surf_action_t) action, SURF_ACTION_DONE);
444 surf_action_lmm_heap_remove(model->model_private->action_heap,action); //FIXME: strange call since action was already popped
447 // if I am wearing a latency hat
448 if (action->hat == LATENCY) {
449 XBT_DEBUG("Latency paid for action %p. Activating", action);
450 lmm_update_variable_weight(model->model_private->maxmin_system, action->variable,
451 ((surf_action_network_CM02_t)(action))->weight);
452 surf_action_lmm_heap_remove(model->model_private->action_heap,action);
453 action->last_update = surf_get_clock();
455 // if I am wearing a max_duration or normal hat
456 } else if (action->hat == MAX_DURATION ||
457 action->hat == NORMAL) {
458 // no need to communicate anymore
459 // assume that flows that reached max_duration have remaining of 0
460 action->generic_action.finish = surf_get_clock();
461 XBT_DEBUG("Action %p finished", action);
462 action->generic_action.remains = 0;
463 ((surf_action_t)action)->finish = surf_get_clock();
464 model->action_state_set((surf_action_t) action,
466 surf_action_lmm_heap_remove(model->model_private->action_heap,action);
468 if (model->gap_remove && model == surf_network_model)
469 model->gap_remove(action);
474 if (TRACE_is_enabled() && model == surf_cpu_model) {
475 //defining the last timestamp that we can safely dump to trace file
476 //without losing the event ascending order (considering all CPU's)
478 xbt_swag_t running_actions = model->states.running_action_set;
479 xbt_swag_foreach(action, running_actions) {
481 smaller = action->last_update;
484 if (action->last_update < smaller) {
485 smaller = action->last_update;
489 TRACE_last_timestamp_to_dump = smaller;
496 void generic_update_actions_state_full(double now, double delta, surf_model_t model)
498 surf_action_lmm_t action = NULL;
499 surf_action_lmm_t next_action = NULL;
500 xbt_swag_t running_actions = model->states.running_action_set;
502 xbt_swag_foreach_safe(action, next_action, running_actions) {
504 if(model == surf_network_model)
508 if (((surf_action_network_CM02_t)action)->latency > 0) {
509 if (((surf_action_network_CM02_t)action)->latency > deltap) {
510 double_update(&(((surf_action_network_CM02_t)action)->latency), deltap);
513 double_update(&(deltap), ((surf_action_network_CM02_t)action)->latency);
514 ((surf_action_network_CM02_t)action)->latency = 0.0;
516 if ((((surf_action_network_CM02_t)action)->latency == 0.0) && !(action->suspended))
517 lmm_update_variable_weight(model->model_private->maxmin_system, action->variable,
518 ((surf_action_network_CM02_t)action)->weight);
521 if (TRACE_is_enabled()) {
522 int n = lmm_get_number_of_cnst_from_var(model->model_private->maxmin_system, action->variable);
524 for (i = 0; i < n; i++){
525 lmm_constraint_t constraint = lmm_get_cnst_from_var(model->model_private->maxmin_system,
528 link_CM02_t link = lmm_constraint_id(constraint);
529 TRACE_surf_link_set_utilization(link->lmm_resource.generic_resource.name,
530 ((surf_action_t)action)->category,
531 (lmm_variable_getvalue(action->variable)*
532 lmm_get_cnst_weight_from_var(model->model_private->maxmin_system,
540 if (!lmm_get_number_of_cnst_from_var
541 (model->model_private->maxmin_system, action->variable)) {
542 /* There is actually no link used, hence an infinite bandwidth.
543 * This happens often when using models like vivaldi.
544 * In such case, just make sure that the action completes immediately.
546 double_update(&(action->generic_action.remains),
547 action->generic_action.remains);
553 if (TRACE_is_enabled()) {
555 lmm_constraint_id(lmm_get_cnst_from_var
556 (model->model_private->maxmin_system,
557 action->variable, 0));
559 TRACE_surf_host_set_utilization(x->name,
560 ((surf_action_t)action)->category,
561 lmm_variable_getvalue(action->variable),
564 TRACE_last_timestamp_to_dump = now - delta;
569 double_update(&(action->generic_action.remains),
570 lmm_variable_getvalue(action->variable) * delta);
573 if (action->generic_action.max_duration != NO_MAX_DURATION)
574 double_update(&(action->generic_action.max_duration), delta);
577 if ((action->generic_action.remains <= 0) &&
578 (lmm_get_variable_weight(action->variable) > 0)) {
579 action->generic_action.finish = surf_get_clock();
580 surf_action_state_set((surf_action_t) action, SURF_ACTION_DONE);
582 if (model->gap_remove && model == surf_network_model)
583 model->gap_remove(action);
584 } else if ((action->generic_action.max_duration != NO_MAX_DURATION) &&
585 (action->generic_action.max_duration <= 0)) {
586 action->generic_action.finish = surf_get_clock();
587 surf_action_state_set((surf_action_t) action, SURF_ACTION_DONE);
589 if (model->gap_remove && model == surf_network_model)
590 model->gap_remove(action);
593 update_resource_energy(model, action);