1 /* Copyright (c) 2009-2013. 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_obj = 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_obj->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_obj->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_obj;
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_obj;
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_obj;
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_obj;
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_obj;
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_obj;
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);
284 void surf_action_set_bound(surf_action_t action, double bound)
286 surf_model_t model = action->model_obj;
287 XBT_IN("(%p,%g)", action, bound);
288 action->bound = bound;
289 lmm_update_variable_bound(model->model_private->maxmin_system, ((surf_action_lmm_t) action)->variable, bound);
291 if (model->model_private->update_mechanism == UM_LAZY)
292 surf_action_lmm_heap_remove(model->model_private->action_heap, (surf_action_lmm_t) action);
297 void surf_action_set_category(surf_action_t action,
298 const char *category)
300 XBT_IN("(%p,%s)", action, category);
301 action->category = xbt_strdup(category);
306 void generic_update_action_remaining_lazy( surf_action_lmm_t action, double now)
309 surf_model_t model = action->generic_action.model_obj;
311 if(model == surf_network_model)
313 if (action->suspended != 0)
318 xbt_assert(action->generic_action.state_set == model->states.running_action_set,
319 "You're updating an action that is not running.");
321 /* bogus priority, skip it */
322 xbt_assert(action->generic_action.priority > 0,
323 "You're updating an action that seems suspended.");
326 delta = now - action->last_update;
328 if (action->generic_action.remains > 0) {
329 XBT_DEBUG("Updating action(%p): remains was %f, last_update was: %f", action, action->generic_action.remains, action->last_update);
330 double_update(&(action->generic_action.remains),
331 action->last_value * delta);
334 if (model->type == SURF_MODEL_TYPE_CPU && TRACE_is_enabled()) {
335 surf_resource_t cpu =
336 lmm_constraint_id(lmm_get_cnst_from_var
337 (model->model_private->maxmin_system,
338 action->variable, 0));
339 TRACE_surf_host_set_utilization(cpu->name,
340 action->generic_action.category,
343 now - action->last_update);
346 XBT_DEBUG("Updating action(%p): remains is now %f", action,
347 action->generic_action.remains);
350 if(model == surf_network_model)
352 if (((surf_action_t)action)->max_duration != NO_MAX_DURATION)
353 double_update(&(((surf_action_t)action)->max_duration), delta);
355 if ((((surf_action_t)action)->remains <= 0) &&
356 (lmm_get_variable_weight(action->variable) > 0)) {
357 ((surf_action_t)action)->finish = surf_get_clock();
358 model->action_state_set((surf_action_t) action,
361 surf_action_lmm_heap_remove(model->model_private->action_heap,(surf_action_lmm_t)action);
362 } else if (((((surf_action_t)action)->max_duration != NO_MAX_DURATION)
363 && (((surf_action_t)action)->max_duration <= 0))) {
364 ((surf_action_t)action)->finish = surf_get_clock();
365 model->action_state_set((surf_action_t) action,
367 surf_action_lmm_heap_remove(model->model_private->action_heap,(surf_action_lmm_t)action);
371 action->last_update = now;
372 action->last_value = lmm_variable_getvalue(action->variable);
375 double surf_action_get_remains(surf_action_t action)
377 XBT_IN("(%p)", action);
378 surf_model_t model = action->model_obj;
379 /* update remains before return it */
380 if (model->model_private->update_mechanism == UM_LAZY) /* update remains before return it */
381 generic_update_action_remaining_lazy((surf_action_lmm_t)action, surf_get_clock());
383 return action->remains;
387 * Update the CPU total energy for a finished action
390 void update_resource_energy(surf_model_t model, surf_action_lmm_t action)
392 if(model->type == SURF_MODEL_TYPE_CPU){
393 cpu_Cas01_t cpu_model = (cpu_Cas01_t)lmm_constraint_id(lmm_get_cnst_from_var
394 (model->model_private->maxmin_system,
395 action->variable, 0));
397 if( cpu_model->energy->last_updated < surf_get_clock()) {
398 double load = lmm_constraint_get_usage(cpu_model->constraint) / cpu_model->power_peak;
399 cpu_update_energy(cpu_model, load);
407 void generic_update_actions_state_lazy(double now, double delta, surf_model_t model)
409 surf_action_lmm_t action;
410 while ((xbt_heap_size(model->model_private->action_heap) > 0)
411 && (double_equals(xbt_heap_maxkey(model->model_private->action_heap), now))) {
412 action = xbt_heap_pop(model->model_private->action_heap);
413 XBT_DEBUG("Something happened to action %p", action);
415 if (TRACE_is_enabled()) {
416 if(model->type == SURF_MODEL_TYPE_CPU){
417 surf_resource_t cpu =
418 lmm_constraint_id(lmm_get_cnst_from_var
419 (model->model_private->maxmin_system,
420 action->variable, 0));
421 TRACE_surf_host_set_utilization(cpu->name,
422 ((surf_action_t)action)->category,
423 lmm_variable_getvalue(action->variable),
425 now - action->last_update);
428 int n = lmm_get_number_of_cnst_from_var(model->model_private->maxmin_system, action->variable);
430 for (i = 0; i < n; i++){
431 lmm_constraint_t constraint = lmm_get_cnst_from_var(model->model_private->maxmin_system,
434 link_CM02_t link = lmm_constraint_id(constraint);
435 TRACE_surf_link_set_utilization(link->lmm_resource.generic_resource.name,
436 ((surf_action_t)action)->category,
437 (lmm_variable_getvalue(action->variable)*
438 lmm_get_cnst_weight_from_var(model->model_private->maxmin_system,
442 now - action->last_update);
448 if(model->type == SURF_MODEL_TYPE_CPU){
449 action->generic_action.finish = surf_get_clock();
451 update_resource_energy(model, action);
453 /* set the remains to 0 due to precision problems when updating the remaining amount */
454 action->generic_action.remains = 0;
455 surf_action_state_set((surf_action_t) action, SURF_ACTION_DONE);
456 surf_action_lmm_heap_remove(model->model_private->action_heap,action); //FIXME: strange call since action was already popped
459 // if I am wearing a latency hat
460 if (action->hat == LATENCY) {
461 XBT_DEBUG("Latency paid for action %p. Activating", action);
462 lmm_update_variable_weight(model->model_private->maxmin_system, action->variable,
463 ((surf_action_network_CM02_t)(action))->weight);
464 surf_action_lmm_heap_remove(model->model_private->action_heap,action);
465 action->last_update = surf_get_clock();
467 // if I am wearing a max_duration or normal hat
468 } else if (action->hat == MAX_DURATION ||
469 action->hat == NORMAL) {
470 // no need to communicate anymore
471 // assume that flows that reached max_duration have remaining of 0
472 action->generic_action.finish = surf_get_clock();
473 XBT_DEBUG("Action %p finished", action);
474 action->generic_action.remains = 0;
475 ((surf_action_t)action)->finish = surf_get_clock();
476 model->action_state_set((surf_action_t) action,
478 surf_action_lmm_heap_remove(model->model_private->action_heap,action);
480 if (model->gap_remove && model == surf_network_model)
481 model->gap_remove(action);
486 if (TRACE_is_enabled() && model->type == SURF_MODEL_TYPE_CPU) {
487 //defining the last timestamp that we can safely dump to trace file
488 //without losing the event ascending order (considering all CPU's)
490 xbt_swag_t running_actions = model->states.running_action_set;
491 xbt_swag_foreach(action, running_actions) {
493 smaller = action->last_update;
496 if (action->last_update < smaller) {
497 smaller = action->last_update;
501 TRACE_last_timestamp_to_dump = smaller;
508 void generic_update_actions_state_full(double now, double delta, surf_model_t model)
510 surf_action_lmm_t action = NULL;
511 surf_action_lmm_t next_action = NULL;
512 xbt_swag_t running_actions = model->states.running_action_set;
514 xbt_swag_foreach_safe(action, next_action, running_actions) {
516 if(model == surf_network_model)
520 if (((surf_action_network_CM02_t)action)->latency > 0) {
521 if (((surf_action_network_CM02_t)action)->latency > deltap) {
522 double_update(&(((surf_action_network_CM02_t)action)->latency), deltap);
525 double_update(&(deltap), ((surf_action_network_CM02_t)action)->latency);
526 ((surf_action_network_CM02_t)action)->latency = 0.0;
528 if ((((surf_action_network_CM02_t)action)->latency == 0.0) && !(action->suspended))
529 lmm_update_variable_weight(model->model_private->maxmin_system, action->variable,
530 ((surf_action_network_CM02_t)action)->weight);
533 if (TRACE_is_enabled()) {
534 int n = lmm_get_number_of_cnst_from_var(model->model_private->maxmin_system, action->variable);
536 for (i = 0; i < n; i++){
537 lmm_constraint_t constraint = lmm_get_cnst_from_var(model->model_private->maxmin_system,
540 link_CM02_t link = lmm_constraint_id(constraint);
541 TRACE_surf_link_set_utilization(link->lmm_resource.generic_resource.name,
542 ((surf_action_t)action)->category,
543 (lmm_variable_getvalue(action->variable)*
544 lmm_get_cnst_weight_from_var(model->model_private->maxmin_system,
552 if (!lmm_get_number_of_cnst_from_var
553 (model->model_private->maxmin_system, action->variable)) {
554 /* There is actually no link used, hence an infinite bandwidth.
555 * This happens often when using models like vivaldi.
556 * In such case, just make sure that the action completes immediately.
558 double_update(&(action->generic_action.remains),
559 action->generic_action.remains);
565 if (TRACE_is_enabled()) {
567 lmm_constraint_id(lmm_get_cnst_from_var
568 (model->model_private->maxmin_system,
569 action->variable, 0));
571 TRACE_surf_host_set_utilization(x->name,
572 ((surf_action_t)action)->category,
573 lmm_variable_getvalue(action->variable),
576 TRACE_last_timestamp_to_dump = now - delta;
581 double_update(&(action->generic_action.remains),
582 lmm_variable_getvalue(action->variable) * delta);
585 if (action->generic_action.max_duration != NO_MAX_DURATION)
586 double_update(&(action->generic_action.max_duration), delta);
589 if ((action->generic_action.remains <= 0) &&
590 (lmm_get_variable_weight(action->variable) > 0)) {
591 action->generic_action.finish = surf_get_clock();
592 surf_action_state_set((surf_action_t) action, SURF_ACTION_DONE);
594 if (model->gap_remove && model == surf_network_model)
595 model->gap_remove(action);
596 } else if ((action->generic_action.max_duration != NO_MAX_DURATION) &&
597 (action->generic_action.max_duration <= 0)) {
598 action->generic_action.finish = surf_get_clock();
599 surf_action_state_set((surf_action_t) action, SURF_ACTION_DONE);
601 if (model->gap_remove && model == surf_network_model)
602 model->gap_remove(action);
605 update_resource_energy(model, action);