From: thiery Date: Wed, 2 Feb 2011 16:50:16 +0000 (+0000) Subject: Use a mallocator for SIMIX actions X-Git-Tag: v3.6_beta2~362 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/e99350b90a8c0e400db2f43e3093b70afdbd2aac Use a mallocator for SIMIX actions git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/simgrid/simgrid/trunk@9567 48e7efb5-ca39-0410-a469-dd3cf9ba447f --- diff --git a/include/xbt/fifo.h b/include/xbt/fifo.h index 81762b5a5a..5455d697d9 100644 --- a/include/xbt/fifo.h +++ b/include/xbt/fifo.h @@ -31,6 +31,7 @@ typedef struct xbt_fifo *xbt_fifo_t; XBT_PUBLIC(xbt_fifo_t) xbt_fifo_new(void); XBT_PUBLIC(void) xbt_fifo_free(xbt_fifo_t); +XBT_PUBLIC(void) xbt_fifo_reset(xbt_fifo_t); /** @} */ /** @defgroup XBT_fifo_perl Fifo perl-like functions diff --git a/src/simix/private.h b/src/simix/private.h index 5aadc796a3..d63145dbf6 100644 --- a/src/simix/private.h +++ b/src/simix/private.h @@ -12,6 +12,7 @@ #include "xbt/fifo.h" #include "xbt/swag.h" #include "xbt/dict.h" +#include "xbt/mallocator.h" #include "xbt/config.h" #include "xbt/function_types.h" #include "xbt/ex_interface.h" @@ -37,6 +38,7 @@ typedef struct s_smx_global { smx_creation_func_t create_process_function; void_f_pvoid_t kill_process_function; void_pfn_smxprocess_t cleanup_process_function; + xbt_mallocator_t action_mallocator; } s_smx_global_t, *smx_global_t; extern smx_global_t simix_global; diff --git a/src/simix/smx_global.c b/src/simix/smx_global.c index 059130d929..f640d4b426 100644 --- a/src/simix/smx_global.c +++ b/src/simix/smx_global.c @@ -26,6 +26,10 @@ XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_kernel, simix, smx_global_t simix_global = NULL; static xbt_heap_t simix_timers = NULL; +static void* SIMIX_action_mallocator_new_f(void); +static void SIMIX_action_mallocator_free_f(void* action); +static void SIMIX_action_mallocator_reset_f(void* action); + /* FIXME: Yeah, I'll do it in a portable maner one day [Mt] */ #include @@ -79,6 +83,9 @@ void SIMIX_global_init(int *argc, char **argv) simix_global->create_process_function = NULL; simix_global->kill_process_function = NULL; simix_global->cleanup_process_function = SIMIX_process_cleanup; + simix_global->action_mallocator = xbt_mallocator_new(65536, + SIMIX_action_mallocator_new_f, SIMIX_action_mallocator_free_f, + SIMIX_action_mallocator_reset_f); surf_init(argc, argv); /* Initialize SURF structures */ SIMIX_context_mod_init(); @@ -333,3 +340,23 @@ void SIMIX_display_process_status(void) } } } + +static void* SIMIX_action_mallocator_new_f(void) { + smx_action_t action = xbt_new(s_smx_action_t, 1); + action->request_list = xbt_fifo_new(); + return action; +} + +static void SIMIX_action_mallocator_free_f(void* action) { + xbt_fifo_free(((smx_action_t) action)->request_list); + xbt_free(action); +} + +static void SIMIX_action_mallocator_reset_f(void* action) { + + // we also recycle the request list + xbt_fifo_t fifo = ((smx_action_t) action)->request_list; + xbt_fifo_reset(fifo); + memset(action, 0, sizeof(s_smx_action_t)); + ((smx_action_t) action)->request_list = fifo; +} diff --git a/src/simix/smx_host.c b/src/simix/smx_host.c index de979ccc90..630400d4c2 100644 --- a/src/simix/smx_host.c +++ b/src/simix/smx_host.c @@ -182,10 +182,9 @@ smx_action_t SIMIX_host_execute(const char *name, smx_host_t host, double priority) { /* alloc structures and initialize */ - smx_action_t action = xbt_new0(s_smx_action_t, 1); + smx_action_t action = xbt_mallocator_get(simix_global->action_mallocator); action->type = SIMIX_ACTION_EXECUTE; action->name = xbt_strdup(name); - action->request_list = xbt_fifo_new(); action->state = SIMIX_RUNNING; action->execution.host = host; @@ -220,10 +219,9 @@ smx_action_t SIMIX_host_parallel_execute( const char *name, int i; /* alloc structures and initialize */ - smx_action_t action = xbt_new0(s_smx_action_t, 1); + smx_action_t action = xbt_mallocator_get(simix_global->action_mallocator); action->type = SIMIX_ACTION_PARALLEL_EXECUTE; action->name = xbt_strdup(name); - action->request_list = xbt_fifo_new(); action->state = SIMIX_RUNNING; action->execution.host = NULL; /* FIXME: do we need the list of hosts? */ @@ -254,10 +252,7 @@ void SIMIX_host_execution_destroy(smx_action_t action) { DEBUG1("Destroy action %p", action); - if (action->name) - xbt_free(action->name); - - xbt_fifo_free(action->request_list); + xbt_free(action->name); if (action->execution.surf_exec) { surf_workstation_model->action_unref(action->execution.surf_exec); @@ -267,7 +262,7 @@ void SIMIX_host_execution_destroy(smx_action_t action) #ifdef HAVE_TRACING TRACE_smx_action_destroy(action); #endif - xbt_free(action); + xbt_mallocator_release(simix_global->action_mallocator, action); } void SIMIX_host_execution_cancel(smx_action_t action) diff --git a/src/simix/smx_network.c b/src/simix/smx_network.c index 8956b25c74..0fe6778674 100644 --- a/src/simix/smx_network.c +++ b/src/simix/smx_network.c @@ -164,10 +164,9 @@ smx_action_t SIMIX_comm_new(e_smx_comm_type_t type) smx_action_t act; /* alloc structures */ - act = xbt_new0(s_smx_action_t, 1); + act = xbt_mallocator_get(simix_global->action_mallocator); act->type = SIMIX_ACTION_COMMUNICATE; act->state = SIMIX_WAITING; - act->request_list = xbt_fifo_new(); /* set communication */ act->comm.type = type; @@ -212,11 +211,7 @@ void SIMIX_comm_destroy(smx_action_t action) TRACE_smx_action_destroy(action); #endif - if (action->name) - xbt_free(action->name); - - xbt_fifo_free(action->request_list); - + xbt_free(action->name); SIMIX_comm_destroy_internal_actions(action); if (action->comm.detached && action->state != SIMIX_DONE) { @@ -225,7 +220,7 @@ void SIMIX_comm_destroy(smx_action_t action) ((void_f_pvoid_t) action->comm.src_data)(action->comm.src_buff); } - xbt_free(action); + xbt_mallocator_release(simix_global->action_mallocator, action); } void SIMIX_comm_destroy_internal_actions(smx_action_t action) diff --git a/src/simix/smx_process.c b/src/simix/smx_process.c index c24f92204f..01df49c494 100644 --- a/src/simix/smx_process.c +++ b/src/simix/smx_process.c @@ -434,9 +434,8 @@ smx_action_t SIMIX_process_sleep(smx_process_t process, double duration) host->name); } - action = xbt_new0(s_smx_action_t, 1); + action = xbt_mallocator_get(simix_global->action_mallocator); action->type = SIMIX_ACTION_SLEEP; - action->request_list = xbt_fifo_new(); action->name = xbt_strdup("sleep"); #ifdef HAVE_TRACING action->category = NULL; @@ -482,15 +481,13 @@ void SIMIX_post_process_sleep(smx_action_t action) void SIMIX_process_sleep_destroy(smx_action_t action) { DEBUG1("Destroy action %p", action); - if (action->name) - xbt_free(action->name); + xbt_free(action->name); if (action->sleep.surf_sleep) action->sleep.surf_sleep->model_type->action_unref(action->sleep.surf_sleep); #ifdef HAVE_TRACING TRACE_smx_action_destroy(action); #endif - xbt_fifo_free(action->request_list); - xbt_free(action); + xbt_mallocator_release(simix_global->action_mallocator, action); } void SIMIX_process_sleep_suspend(smx_action_t action) diff --git a/src/simix/smx_synchro.c b/src/simix/smx_synchro.c index 16ceb04ab4..d29225573a 100644 --- a/src/simix/smx_synchro.c +++ b/src/simix/smx_synchro.c @@ -24,9 +24,8 @@ static void SIMIX_sem_block_onto(smx_sem_t sem); static smx_action_t SIMIX_synchro_wait(smx_host_t smx_host, double timeout) { smx_action_t action; - action = xbt_new0(s_smx_action_t, 1); + action = xbt_mallocator_get(simix_global->action_mallocator); action->type = SIMIX_ACTION_SYNCHRO; - action->request_list = xbt_fifo_new(); action->name = xbt_strdup("synchro"); action->synchro.sleep = surf_workstation_model->extension.workstation.sleep(smx_host->host, timeout); @@ -68,9 +67,8 @@ void SIMIX_synchro_destroy(smx_action_t action) { DEBUG1("Destroying synchro %p", action); action->synchro.sleep->model_type->action_unref(action->synchro.sleep); - xbt_fifo_free(action->request_list); xbt_free(action->name); - xbt_free(action); + xbt_mallocator_release(simix_global->action_mallocator, action); } void SIMIX_post_synchro(smx_action_t action) diff --git a/src/surf/surf_action.c b/src/surf/surf_action.c index 175fe69c7f..fc5b8d9363 100644 --- a/src/surf/surf_action.c +++ b/src/surf/surf_action.c @@ -26,9 +26,9 @@ const char *surf_action_state_names[6] = { /* Surf actions mallocator */ static xbt_mallocator_t action_mallocator = NULL; static int action_mallocator_allocated_size = 0; -static void* action_mallocator_new_f(void); -static void action_mallocator_free_f(void* action); -static void action_mallocator_reset_f(void* action); +static void* surf_action_mallocator_new_f(void); +static void surf_action_mallocator_free_f(void* action); +static void surf_action_mallocator_reset_f(void* action); /** * \brief Initializes the action module of Surf. @@ -39,8 +39,8 @@ void surf_action_init(void) { * so this size should be set to the maximum size of the surf action structures */ action_mallocator_allocated_size = sizeof(s_surf_action_network_CM02_t); - action_mallocator = xbt_mallocator_new(128, action_mallocator_new_f, - action_mallocator_free_f, action_mallocator_reset_f); + action_mallocator = xbt_mallocator_new(65536, surf_action_mallocator_new_f, + surf_action_mallocator_free_f, surf_action_mallocator_reset_f); } /** @@ -51,15 +51,15 @@ void surf_action_exit(void) { xbt_mallocator_free(action_mallocator); } -static void* action_mallocator_new_f(void) { - return xbt_malloc0(action_mallocator_allocated_size); +static void* surf_action_mallocator_new_f(void) { + return xbt_malloc(action_mallocator_allocated_size); } -static void action_mallocator_free_f(void* action) { +static void surf_action_mallocator_free_f(void* action) { xbt_free(action); } -static void action_mallocator_reset_f(void* action) { +static void surf_action_mallocator_reset_f(void* action) { memset(action, 0, action_mallocator_allocated_size); } diff --git a/src/xbt/fifo.c b/src/xbt/fifo.c index c3d4ffb905..de5e0b7433 100644 --- a/src/xbt/fifo.c +++ b/src/xbt/fifo.c @@ -36,13 +36,24 @@ xbt_fifo_t xbt_fifo_new(void) * Free the fifo structure. None of the objects that was in the fifo is however modified. */ void xbt_fifo_free(xbt_fifo_t l) +{ + xbt_fifo_reset(l); + xbt_free(l); +} + +/** + * \brief Makes a fifo empty. + * \param l a fifo + * + * None of the objects that was in the fifo is however modified. + */ +void xbt_fifo_reset(xbt_fifo_t l) { xbt_fifo_item_t b, tmp; for (b = xbt_fifo_get_first_item(l); b; tmp = b, b = b->next, xbt_fifo_free_item(tmp)); - xbt_free(l); - return; + l->head = l->tail = NULL; } /** Push