From 21756925d57a6fb6de1dfea9c3e1842d0f101d2c Mon Sep 17 00:00:00 2001 From: cristianrosa Date: Wed, 12 May 2010 09:11:08 +0000 Subject: [PATCH 1/1] Bugfix: fix memory leak related to transition creation/destruction. Instead of using a reference count, associate the transitions to the state that creates them, and delete them only when deleting that state. git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/simgrid/simgrid/trunk@7739 48e7efb5-ca39-0410-a469-dd3cf9ba447f --- src/mc/mc_dpor.c | 3 --- src/mc/mc_global.c | 24 ++++++++++++------------ src/mc/mc_transition.c | 14 ++++++-------- src/mc/private.h | 12 ++++++------ 4 files changed, 24 insertions(+), 29 deletions(-) diff --git a/src/mc/mc_dpor.c b/src/mc/mc_dpor.c index eb4dd9bc1d..53bd9cfb5e 100644 --- a/src/mc/mc_dpor.c +++ b/src/mc/mc_dpor.c @@ -44,8 +44,6 @@ void MC_dpor_init() xbt_setset_set_insert(initial_state->interleave, trans); MC_UNSET_RAW_MEM; - trans->refcount++; - /* Update Statistics */ mc_stats->state_size += xbt_setset_set_size(initial_state->enabled_transitions); } @@ -96,7 +94,6 @@ void MC_dpor(void) xbt_setset_set_remove(mc_current_state->interleave, trans); /* Add the transition in the done set of the current state */ xbt_setset_set_insert(mc_current_state->done, trans); - trans->refcount++; } next_state = MC_state_new(); diff --git a/src/mc/mc_global.c b/src/mc/mc_global.c index 81a1b206d1..959fc73414 100644 --- a/src/mc/mc_global.c +++ b/src/mc/mc_global.c @@ -134,16 +134,12 @@ void MC_replay(xbt_fifo_t stack) void MC_dump_stack(xbt_fifo_t stack) { mc_state_t state; - mc_transition_t trans; + MC_show_stack(stack); + MC_SET_RAW_MEM; - while( (state = (mc_state_t)xbt_fifo_pop(stack)) != NULL ){ - trans = state->executed_transition; - if(trans) - INFO1("%s", trans->name); - + while( (state = (mc_state_t)xbt_fifo_pop(stack)) != NULL ) MC_state_delete(state); - } MC_UNSET_RAW_MEM; } @@ -153,9 +149,8 @@ void MC_show_stack(xbt_fifo_t stack) mc_transition_t trans; xbt_fifo_item_t item; - INFO0("==========================="); for(item=xbt_fifo_get_last_item(stack); - (item?(state=(mc_state_t)(xbt_fifo_get_item_content(item))):(NULL)); \ + (item?(state=(mc_state_t)(xbt_fifo_get_item_content(item))):(NULL)); item=xbt_fifo_get_prev_item(item)){ trans = state->executed_transition; if(trans){ @@ -164,7 +159,6 @@ void MC_show_stack(xbt_fifo_t stack) } } - /** * \brief Schedules all the process that are ready to run * As a side effect it performs some clean-up required by SIMIX @@ -192,6 +186,7 @@ mc_state_t MC_state_new(void) mc_state_t state = NULL; state = xbt_new0(s_mc_state_t, 1); + state->created_transitions = xbt_setset_new_set(mc_setset); state->transitions = xbt_setset_new_set(mc_setset); state->enabled_transitions = xbt_setset_new_set(mc_setset); state->interleave = xbt_setset_new_set(mc_setset); @@ -208,8 +203,13 @@ mc_state_t MC_state_new(void) */ void MC_state_delete(mc_state_t state) { - /*if(state->executed_transition) - MC_transition_delete(state->executed_transition);*/ + xbt_setset_cursor_t cursor; + mc_transition_t trans; + + xbt_setset_foreach(state->created_transitions, cursor, trans){ + MC_transition_delete(trans); + } + xbt_setset_destroy_set(state->transitions); xbt_setset_destroy_set(state->enabled_transitions); xbt_setset_destroy_set(state->interleave); diff --git a/src/mc/mc_transition.c b/src/mc/mc_transition.c index 685e0dd51a..6108b11d35 100644 --- a/src/mc/mc_transition.c +++ b/src/mc/mc_transition.c @@ -14,7 +14,6 @@ mc_transition_t MC_create_transition(mc_trans_type_t type, smx_process_t p, smx_ if(!mc_replay_mode){ MC_SET_RAW_MEM; mc_transition_t trans = xbt_new0(s_mc_transition_t, 1); - trans->refcount = 1; /* Generate a string for the "type" */ switch(type){ @@ -42,8 +41,10 @@ mc_transition_t MC_create_transition(mc_trans_type_t type, smx_process_t p, smx_ trans->rdv = rdv; trans->comm = comm; /* Push it onto the enabled transitions set of the current state */ - current_state = (mc_state_t) + + current_state = (mc_state_t) xbt_fifo_get_item_content(xbt_fifo_get_first_item(mc_stack)); + xbt_setset_set_insert(current_state->created_transitions, trans); xbt_setset_set_insert(current_state->transitions, trans); MC_UNSET_RAW_MEM; return trans; @@ -66,7 +67,6 @@ void MC_random_create(int min, int max) trans->name = bprintf("[%s][%s] mc_random(%d,%d) (%p)", p->smx_host->name, p->name, min, max, trans); xbt_free(type_str); - trans->refcount = 1; trans->type = mc_random ; trans->process = p; trans->min = min; @@ -76,6 +76,7 @@ void MC_random_create(int min, int max) /* Push it onto the enabled transitions set of the current state */ current_state = (mc_state_t) xbt_fifo_get_item_content(xbt_fifo_get_first_item(mc_stack)); + xbt_setset_set_insert(current_state->created_transitions, trans); xbt_setset_set_insert(current_state->transitions, trans); MC_UNSET_RAW_MEM; } @@ -99,11 +100,8 @@ void MC_transition_set_comm(mc_transition_t trans, smx_comm_t comm) */ void MC_transition_delete(mc_transition_t trans) { - /* Only delete it if there are no references, otherwise decrement refcount */ - if(--trans->refcount == 0){ - xbt_free(trans->name); - xbt_free(trans); - } + xbt_free(trans->name); + xbt_free(trans); } /** diff --git a/src/mc/private.h b/src/mc/private.h index 3011c5bd20..42b30001e9 100644 --- a/src/mc/private.h +++ b/src/mc/private.h @@ -66,7 +66,6 @@ void MC_dpor(void); typedef struct s_mc_transition{ XBT_SETSET_HEADERS; char* name; - unsigned int refcount; mc_trans_type_t type; smx_process_t process; smx_rdv_t rdv; @@ -84,11 +83,12 @@ int MC_transition_depend(mc_transition_t, mc_transition_t); /******************************** States **************************************/ typedef struct mc_state{ - xbt_setset_set_t transitions; - xbt_setset_set_t enabled_transitions; - xbt_setset_set_t interleave; - xbt_setset_set_t done; - mc_transition_t executed_transition; + xbt_setset_set_t created_transitions; /* created in this state */ + xbt_setset_set_t transitions; /* created in this state + inherited */ + xbt_setset_set_t enabled_transitions; /* they can be executed by the mc */ + xbt_setset_set_t interleave; /* the ones to be executed by the mc */ + xbt_setset_set_t done; /* already executed transitions */ + mc_transition_t executed_transition; /* last executed transition */ } s_mc_state_t, *mc_state_t; extern xbt_fifo_t mc_stack; -- 2.20.1