From f5fd55f293d7e019e8c23e7ff76e2b3743e40cd6 Mon Sep 17 00:00:00 2001 From: thiery Date: Tue, 1 Feb 2011 16:31:14 +0000 Subject: [PATCH] Use a mallocator for the Surf actions git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/simgrid/simgrid/trunk@9557 48e7efb5-ca39-0410-a469-dd3cf9ba447f --- src/surf/cpu.c | 2 +- src/surf/cpu_im.c | 2 +- src/surf/cpu_ti.c | 2 +- src/surf/network.c | 2 +- src/surf/network_constant.c | 2 +- src/surf/network_gtnets.c | 2 +- src/surf/network_im.c | 3 +- src/surf/surf.c | 2 ++ src/surf/surf_action.c | 51 ++++++++++++++++++++++++++++++-- src/surf/surf_private.h | 2 ++ src/surf/workstation_ptask_L07.c | 2 +- 11 files changed, 60 insertions(+), 12 deletions(-) diff --git a/src/surf/cpu.c b/src/surf/cpu.c index 4e3916cb30..dc1165f80e 100644 --- a/src/surf/cpu.c +++ b/src/surf/cpu.c @@ -161,7 +161,7 @@ static int cpu_action_unref(surf_action_t action) if (action->category) xbt_free(action->category); #endif - free(action); + surf_action_free(&action); return 1; } return 0; diff --git a/src/surf/cpu_im.c b/src/surf/cpu_im.c index 7af5accc74..b5d2f8786c 100644 --- a/src/surf/cpu_im.c +++ b/src/surf/cpu_im.c @@ -185,7 +185,7 @@ static int cpu_im_action_unref(surf_action_t action) if (action->category) xbt_free(action->category); #endif - free(action); + surf_action_free(&action); return 1; } return 0; diff --git a/src/surf/cpu_ti.c b/src/surf/cpu_ti.c index b1a06fd299..75d67a9a7f 100644 --- a/src/surf/cpu_ti.c +++ b/src/surf/cpu_ti.c @@ -302,7 +302,7 @@ static int cpu_ti_action_unref(surf_action_t action) xbt_heap_remove(cpu_ti_action_heap, ((surf_action_cpu_ti_t) action)->index_heap); xbt_swag_insert(ACTION_GET_CPU(action), cpu_ti_modified_cpu); - free(action); + surf_action_free(&action); return 1; } return 0; diff --git a/src/surf/network.c b/src/surf/network.c index c69aaa4454..6dee71934e 100644 --- a/src/surf/network.c +++ b/src/surf/network.c @@ -347,7 +347,7 @@ static int net_action_unref(surf_action_t action) if (action->category) xbt_free(action->category); #endif - free(action); + surf_action_free(&action); return 1; } return 0; diff --git a/src/surf/network_constant.c b/src/surf/network_constant.c index bf73baff10..a6978a2280 100644 --- a/src/surf/network_constant.c +++ b/src/surf/network_constant.c @@ -41,7 +41,7 @@ static int netcste_action_unref(surf_action_t action) action->refcount--; if (!action->refcount) { xbt_swag_remove(action, action->state_set); - free(action); + surf_action_free(&action); return 1; } return 0; diff --git a/src/surf/network_gtnets.c b/src/surf/network_gtnets.c index 8d6c9508a9..297560355d 100644 --- a/src/surf/network_gtnets.c +++ b/src/surf/network_gtnets.c @@ -187,7 +187,7 @@ static int action_unref(surf_action_t action) if (action->category) xbt_free(action->category); #endif - free(action); + surf_action_free(&action); return 1; } return 0; diff --git a/src/surf/network_im.c b/src/surf/network_im.c index ce981b50d1..01b323af23 100644 --- a/src/surf/network_im.c +++ b/src/surf/network_im.c @@ -350,8 +350,7 @@ static int im_net_action_unref(surf_action_t action) if (action->category) xbt_free(action->category); #endif - free(action); - action = NULL; + surf_action_free(&action); return 1; } return 0; diff --git a/src/surf/surf.c b/src/surf/surf.c index 26bfeeaf0f..057a81e407 100644 --- a/src/surf/surf.c +++ b/src/surf/surf.c @@ -309,6 +309,7 @@ void surf_init(int *argc, char **argv) history = tmgr_history_new(); surf_config_init(argc, argv); + surf_action_init(); if (MC_IS_ENABLED) MC_memory_init(); } @@ -362,6 +363,7 @@ void surf_exit(void) tmgr_history_free(history); history = NULL; } + surf_action_exit(); if (surf_path) xbt_dynar_free(&surf_path); diff --git a/src/surf/surf_action.c b/src/surf/surf_action.c index 2c34a4e77e..036deea6a8 100644 --- a/src/surf/surf_action.c +++ b/src/surf/surf_action.c @@ -5,6 +5,8 @@ * under the terms of the license (GNU LGPL) which comes with this package. */ #include "surf_private.h" +#include "network_private.h" +#include "xbt/mallocator.h" XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_kernel); @@ -21,10 +23,54 @@ const char *surf_action_state_names[6] = { "SURF_ACTION_NOT_IN_THE_SYSTEM" }; +/* 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); + +/** + * \brief Initializes the action module of Surf. + */ +void surf_action_init(void) { + + /* the action mallocator will always provide actions of the following size, + * 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); +} + +/** + * \brief Uninitializes the action module of Surf. + */ +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 action_mallocator_free_f(void* action) { + xbt_free(action); +} + +static void action_mallocator_reset_f(void* action) { + memset(action, 0, action_mallocator_allocated_size); +} + void *surf_action_new(size_t size, double cost, surf_model_t model, int failed) { - surf_action_t action = xbt_malloc0(size); + xbt_assert2(size <= action_mallocator_allocated_size, + "Cannot create a surf action of size %d: the mallocator only provides actions of size %d", + size, action_mallocator_allocated_size); + + surf_action_t action = xbt_mallocator_get(action_mallocator); action->refcount = 1; action->cost = cost; action->remains = cost; @@ -75,8 +121,7 @@ double surf_action_get_finish_time(surf_action_t action) XBT_INLINE void surf_action_free(surf_action_t * action) { - (*action)->model_type->action_cancel(*action); - free(*action); + xbt_mallocator_release(action_mallocator, *action); *action = NULL; } diff --git a/src/surf/surf_private.h b/src/surf/surf_private.h index cb7b4c4ee0..026ab2f58f 100644 --- a/src/surf/surf_private.h +++ b/src/surf/surf_private.h @@ -50,6 +50,8 @@ double generic_maxmin_share_resources(xbt_swag_t running_actions, void (*solve) (lmm_system_t)); /* Generic functions common to all models */ +void surf_action_init(void); +void surf_action_exit(void); e_surf_action_state_t surf_action_state_get(surf_action_t action); /* cannot declare inline since we use a pointer to it */ double surf_action_get_start_time(surf_action_t action); /* cannot declare inline since we use a pointer to it */ double surf_action_get_finish_time(surf_action_t action); /* cannot declare inline since we use a pointer to it */ diff --git a/src/surf/workstation_ptask_L07.c b/src/surf/workstation_ptask_L07.c index 7a9f6b56c0..a5581e035a 100644 --- a/src/surf/workstation_ptask_L07.c +++ b/src/surf/workstation_ptask_L07.c @@ -128,7 +128,7 @@ static int ptask_action_unref(surf_action_t action) free(((surf_action_workstation_L07_t) action)->workstation_list); free(((surf_action_workstation_L07_t) action)->communication_amount); free(((surf_action_workstation_L07_t) action)->computation_amount); - free(action); + surf_action_free(&action); return 1; } return 0; -- 2.20.1