Introducing 'try to give memory back' callback for heap allocator to use upon allocation request that can not be satisfied by the allocator.

This commit is contained in:
Ruben Ayrapetyan 2014-12-18 21:20:28 +03:00
parent 8febd2bae8
commit 6bb39bb8ea
14 changed files with 308 additions and 105 deletions

View File

@ -0,0 +1,33 @@
/* Copyright 2014 Samsung Electronics Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MEM_ALLOCATOR_INTERNAL_H
#define MEM_ALLOCATOR_INTERNAL_H
#ifndef MEM_ALLOCATOR_INTERNAL
# error "The header is for internal routines of memory allocator component. Please, don't use the routines directly."
#endif /* !MEM_ALLOCATOR_INTERNAL */
/** \addtogroup mem Memory allocation
* @{
*/
extern void mem_run_try_to_give_memory_back_callbacks (mem_try_give_memory_back_severity_t severity);
/**
* @}
*/
#endif /* MEM_ALLOCATOR_INTERNAL_H */

View File

@ -23,15 +23,24 @@
#include "mem-heap.h"
#include "mem-poolman.h"
#define MEM_ALLOCATOR_INTERNAL
#include "mem-allocator-internal.h"
/**
* Check that heap area is less or equal than 64K.
*/
JERRY_STATIC_ASSERT(MEM_HEAP_AREA_SIZE <= 64 * 1024);
/**
* Area for heap
*/
static uint8_t mem_heap_area[ MEM_HEAP_AREA_SIZE ] __attribute__ ((aligned (MEM_ALIGNMENT)));
/**
* Check that heap area is less or equal than 64K.
* The 'try to give memory back' callback
*/
JERRY_STATIC_ASSERT(MEM_HEAP_AREA_SIZE <= 64 * 1024);
static mem_try_give_memory_back_callback_t mem_try_give_memory_back_callback = NULL;
/**
* Initialize memory allocators.
@ -126,6 +135,42 @@ mem_decompress_pointer (uintptr_t compressed_pointer) /**< pointer to decompress
return (void*) int_ptr;
} /* mem_decompress_pointer */
/**
* Register specified 'try to give memory back' callback routine
*/
void
mem_register_a_try_give_memory_back_callback (mem_try_give_memory_back_callback_t callback) /* callback routine */
{
/* Currently only one callback is supported */
JERRY_ASSERT (mem_try_give_memory_back_callback == NULL);
mem_try_give_memory_back_callback = callback;
} /* mem_register_a_try_give_memory_back_callback */
/**
* Unregister specified 'try to give memory back' callback routine
*/
void
mem_unregister_a_try_give_memory_back_callback (mem_try_give_memory_back_callback_t callback) /* callback routine */
{
/* Currently only one callback is supported */
JERRY_ASSERT (mem_try_give_memory_back_callback == callback);
mem_try_give_memory_back_callback = NULL;
} /* mem_unregister_a_try_give_memory_back_callback */
/**
* Run 'try to give memory back' callbacks with specified severity
*/
void
mem_run_try_to_give_memory_back_callbacks (mem_try_give_memory_back_severity_t severity) /**< severity of
the request */
{
JERRY_ASSERT (mem_try_give_memory_back_callback != NULL);
mem_try_give_memory_back_callback (severity);
} /* mem_run_try_to_give_memory_back_callbacks */
#ifndef JERRY_NDEBUG
/**
* Check whether the pointer points to the heap

View File

@ -43,12 +43,37 @@
*/
#define MEM_COMPRESSED_POINTER_WIDTH (MEM_HEAP_OFFSET_LOG - MEM_ALIGNMENT_LOG)
/**
* Severity of a 'try give memory back' request
*
* The request are posted sequentially beginning from
* low to critical until enough memory is freed.
*
* If not enough memory is freed upon a critical request
* then the engine is shut down with ERR_OUT_OF_MEMORY.
*/
typedef enum
{
MEM_TRY_GIVE_MEMORY_BACK_SEVERITY_LOW, /* 'low' severity */
MEM_TRY_GIVE_MEMORY_BACK_SEVERITY_MEDIUM, /* 'medium' severity */
MEM_TRY_GIVE_MEMORY_BACK_SEVERITY_HIGH, /* 'high' severity */
MEM_TRY_GIVE_MEMORY_BACK_SEVERITY_CRITICAL /* 'critical' severity */
} mem_try_give_memory_back_severity_t;
/**
* A 'try give memory back' callback routine type.
*/
typedef void (*mem_try_give_memory_back_callback_t) (mem_try_give_memory_back_severity_t);
extern void mem_init (void);
extern void mem_finalize (bool is_show_mem_stats);
extern uintptr_t mem_compress_pointer (void *pointer);
extern void* mem_decompress_pointer (uintptr_t compressed_pointer);
extern void mem_register_a_try_give_memory_back_callback (mem_try_give_memory_back_callback_t callback);
extern void mem_unregister_a_try_give_memory_back_callback (mem_try_give_memory_back_callback_t callback);
#ifndef JERRY_NDEBUG
extern bool mem_is_heap_pointer (void *pointer);
#endif /* !JERRY_NDEBUG */

View File

@ -30,6 +30,10 @@
#include "mem-config.h"
#include "mem-heap.h"
#define MEM_ALLOCATOR_INTERNAL
#include "mem-allocator-internal.h"
/*
* Valgrind-related options and headers
*/
@ -402,29 +406,22 @@ mem_init_block_header (uint8_t *first_chunk_p, /**< address of the first
/**
* Allocation of memory region.
*
* To reduce heap fragmentation there are two allocation modes - short-term and long-term.
*
* If allocation is short-term then the beginning of the heap is preferred, else - the end of the heap.
*
* It is supposed, that all short-term allocation is used during relatively short discrete sessions.
* After end of the session all short-term allocated regions are supposed to be freed.
* See also:
* mem_heap_alloc_block
*
* @return pointer to allocated memory block - if allocation is successful,
* NULL - if requested region size is zero or if there is not enough memory.
* NULL - if there is not enough memory.
*/
void*
mem_heap_alloc_block (size_t size_in_bytes, /**< size of region to allocate in bytes */
mem_heap_alloc_term_t alloc_term) /**< expected allocation term */
static
void* mem_heap_alloc_block_internal (size_t size_in_bytes, /**< size of region to allocate in bytes */
mem_heap_alloc_term_t alloc_term) /**< expected allocation term */
{
mem_block_header_t *block_p;
mem_direction_t direction;
mem_check_heap ();
JERRY_ASSERT (size_in_bytes != 0);
if (size_in_bytes == 0)
{
return NULL;
}
mem_check_heap ();
if (alloc_term == MEM_HEAP_ALLOC_LONG_TERM)
{
@ -553,15 +550,65 @@ mem_heap_alloc_block (size_t size_in_bytes, /**< size of region to all
VALGRIND_NOACCESS_STRUCT(block_p);
mem_check_heap ();
/* return data space beginning address */
uint8_t *data_space_p = (uint8_t*) (block_p + 1);
JERRY_ASSERT((uintptr_t) data_space_p % MEM_ALIGNMENT == 0);
VALGRIND_UNDEFINED_SPACE(data_space_p, size_in_bytes);
mem_check_heap ();
return data_space_p;
} /* mem_heap_alloc_block_internal */
/**
* Allocation of memory region.
*
* To reduce heap fragmentation there are two allocation modes - short-term and long-term.
*
* If allocation is short-term then the beginning of the heap is preferred, else - the end of the heap.
*
* It is supposed, that all short-term allocation is used during relatively short discrete sessions.
* After end of the session all short-term allocated regions are supposed to be freed.
*
* @return pointer to allocated memory block - if allocation is successful,
* NULL - if requested region size is zero or if there is not enough memory.
*/
void*
mem_heap_alloc_block (size_t size_in_bytes, /**< size of region to allocate in bytes */
mem_heap_alloc_term_t alloc_term) /**< expected allocation term */
{
if (unlikely (size_in_bytes == 0))
{
return NULL;
}
else
{
void *data_space_p = mem_heap_alloc_block_internal (size_in_bytes, alloc_term);
if (likely (data_space_p != NULL))
{
return data_space_p;
}
for (mem_try_give_memory_back_severity_t severity = MEM_TRY_GIVE_MEMORY_BACK_SEVERITY_LOW;
severity <= MEM_TRY_GIVE_MEMORY_BACK_SEVERITY_CRITICAL;
severity++)
{
mem_run_try_to_give_memory_back_callbacks (severity);
data_space_p = mem_heap_alloc_block_internal (size_in_bytes, alloc_term);
if (data_space_p != NULL)
{
return data_space_p;
}
}
JERRY_ASSERT (data_space_p == NULL);
jerry_exit (ERR_OUT_OF_MEMORY);
}
} /* mem_heap_alloc_block */
/**

View File

@ -89,13 +89,7 @@ extern void mem_heap_stats_reset_peak (void);
#define MEM_DEFINE_LOCAL_ARRAY(var_name, number, type) \
{ \
size_t var_name ## ___size = (size_t) (number) * sizeof (type); \
type *var_name = mem_heap_alloc_block (var_name ## ___size, MEM_HEAP_ALLOC_SHORT_TERM); \
\
if (number > 0 \
&& var_name == NULL) \
{ \
jerry_exit (ERR_OUT_OF_MEMORY); \
}
type *var_name = mem_heap_alloc_block (var_name ## ___size, MEM_HEAP_ALLOC_SHORT_TERM);
/**
* Free the previously defined local array variable, freeing corresponding block on the heap,

View File

@ -106,13 +106,7 @@ mem_pools_alloc_longpath (void)
{
mem_pool_state_t *pool_state = (mem_pool_state_t*) mem_heap_alloc_block (MEM_POOL_SIZE, MEM_HEAP_ALLOC_LONG_TERM);
if (pool_state == NULL)
{
/**
* Not enough space for new pool.
*/
return NULL;
}
JERRY_ASSERT (pool_state != NULL);
mem_pool_init (pool_state, MEM_POOL_SIZE);

View File

@ -79,7 +79,8 @@ interp_mem_get_stats (mem_heap_stats_t *out_heap_stats_p,
return;
}
ecma_gc_run (ECMA_GC_GEN_2);
/* Requesting to free as much memory as we currently can */
ecma_try_to_give_back_some_memory (MEM_TRY_GIVE_MEMORY_BACK_SEVERITY_CRITICAL);
if (reset_peak_before)
{
@ -367,7 +368,6 @@ run_int (void)
ecma_deref_object (glob_obj_p);
ecma_deref_object (lex_env_p);
ecma_finalize ();
ecma_gc_run (ECMA_GC_GEN_COUNT - 1);
return ecma_is_value_true (ecma_get_completion_value_value (completion));
}

View File

@ -643,7 +643,7 @@ opfunc_call_n (opcode_t opdata, /**< operation data */
int_data->pos++;
bool this_arg_var_idx_set = false;
idx_t this_arg_var_idx;
idx_t this_arg_var_idx = INVALID_VALUE;
idx_t args_number;
opcode_t next_opcode = read_opcode (int_data->pos);

View File

@ -16,6 +16,7 @@
#include "ecma-alloc.h"
#include "ecma-globals.h"
#include "ecma-gc.h"
#include "ecma-lcache.h"
#include "globals.h"
#include "mem-poolman.h"
@ -59,26 +60,9 @@ JERRY_STATIC_ASSERT (sizeof (ecma_label_descriptor_t) == sizeof (uint64_t));
{ \
ecma_ ## ecma_type ## _t *p ## ecma_type = (ecma_ ## ecma_type ## _t *) mem_pools_alloc (); \
\
if (likely (p ## ecma_type != NULL)) \
{ \
return p ## ecma_type; \
} \
JERRY_ASSERT (p ## ecma_type != NULL); \
\
for (ecma_gc_gen_t gen_id = ECMA_GC_GEN_0; \
gen_id < ECMA_GC_GEN_COUNT; \
gen_id++) \
{ \
ecma_gc_run (gen_id); \
\
p ## ecma_type = (ecma_ ## ecma_type ## _t *) mem_pools_alloc (); \
\
if (likely (p ## ecma_type != NULL)) \
{ \
return p ## ecma_type; \
} \
} \
\
jerry_exit (ERR_OUT_OF_MEMORY); \
return p ## ecma_type; \
}
/**

View File

@ -526,8 +526,6 @@ ecma_gc_sweep (ecma_object_t *object_p) /**< object to free */
void
ecma_gc_run (ecma_gc_gen_t max_gen_to_collect) /**< maximum generation to run collection on */
{
ecma_lcache_invalidate_all ();
JERRY_ASSERT(max_gen_to_collect < ECMA_GC_GEN_COUNT);
/* clearing visited flags for all objects of generations to be processed */
@ -664,6 +662,36 @@ ecma_gc_run (ecma_gc_gen_t max_gen_to_collect) /**< maximum generation to run co
#endif /* !JERRY_NDEBUG */
} /* ecma_gc_run */
/**
* Try to free some memory (depending on severity).
*/
void
ecma_try_to_give_back_some_memory (mem_try_give_memory_back_severity_t severity) /**< severity of
* the request */
{
if (severity == MEM_TRY_GIVE_MEMORY_BACK_SEVERITY_LOW)
{
ecma_gc_run (ECMA_GC_GEN_0);
}
else if (severity == MEM_TRY_GIVE_MEMORY_BACK_SEVERITY_MEDIUM)
{
ecma_gc_run (ECMA_GC_GEN_1);
}
else if (severity == MEM_TRY_GIVE_MEMORY_BACK_SEVERITY_HIGH)
{
ecma_gc_run (ECMA_GC_GEN_2);
}
else
{
JERRY_ASSERT (severity == MEM_TRY_GIVE_MEMORY_BACK_SEVERITY_CRITICAL);
/* Freeing as much memory as we currently can */
ecma_lcache_invalidate_all ();
ecma_gc_run (ECMA_GC_GEN_COUNT - 1);
}
} /* ecma_try_to_give_back_some_memory */
/**
* @}
* @}

View File

@ -24,6 +24,7 @@
#define ECMA_GC_H
#include "ecma-globals.h"
#include "mem-allocator.h"
/**
* GC generation identifier
@ -43,6 +44,7 @@ extern void ecma_deref_object (ecma_object_t *object_p);
extern void ecma_gc_update_may_ref_younger_object_flag_by_value (ecma_object_t *obj_p, ecma_value_t value);
extern void ecma_gc_update_may_ref_younger_object_flag_by_object (ecma_object_t *obj_p, ecma_object_t *ref_obj_p);
extern void ecma_gc_run (ecma_gc_gen_t max_gen_to_collect);
extern void ecma_try_to_give_back_some_memory (mem_try_give_memory_back_severity_t severity);
#endif /* !ECMA_GC_H */

View File

@ -25,6 +25,7 @@
#include "ecma-gc.h"
#include "ecma-globals.h"
#include "ecma-helpers.h"
#include "ecma-lcache.h"
#include "globals.h"
#include "jerry-libc.h"
#include "interpreter.h"
@ -688,6 +689,7 @@ ecma_copy_or_ref_ecma_string (ecma_string_t *string_desc_p) /**< string descript
uint32_t current_refs = string_desc_p->refs;
/* First trying to free unreachable objects that maybe refer to the string */
ecma_lcache_invalidate_all ();
ecma_gc_run (ECMA_GC_GEN_COUNT - 1);
if (current_refs == string_desc_p->refs)

View File

@ -15,8 +15,10 @@
#include "ecma-builtins.h"
#include "ecma-helpers.h"
#include "ecma-gc.h"
#include "ecma-lcache.h"
#include "ecma-operations.h"
#include "mem-allocator.h"
/** \addtogroup ecma ECMA
* @{
@ -34,6 +36,8 @@ ecma_init (void)
ecma_strings_init ();
ecma_init_builtins ();
ecma_lcache_init ();
mem_register_a_try_give_memory_back_callback (ecma_try_to_give_back_some_memory);
} /* ecma_init */
/**
@ -42,8 +46,11 @@ ecma_init (void)
void
ecma_finalize (void)
{
ecma_lcache_invalidate_all ();
mem_unregister_a_try_give_memory_back_callback (ecma_try_to_give_back_some_memory);
ecma_finalize_builtins ();
ecma_lcache_invalidate_all ();
ecma_gc_run (ECMA_GC_GEN_COUNT - 1);
} /* ecma_finalize */
/**

View File

@ -23,16 +23,61 @@ extern int printf (__const char *__restrict __format, ...);
extern void *memset (void *__s, int __c, size_t __n);
// Heap size is 32K
const size_t test_heap_size = 32 * 1024;
#define test_heap_size (32 * 1024)
// Iterations count
const uint32_t test_iters = 64 * 1024;
#define test_iters (64 * 1024)
// Subiterations count
const uint32_t test_sub_iters = 32;
#define test_sub_iters 32
// Threshold size of block to allocate
const uint32_t test_threshold_block_size = 8192;
#define test_threshold_block_size 8192
uint8_t *ptrs[test_sub_iters];
size_t sizes[test_sub_iters];
static void
test_heap_give_some_memory_back (mem_try_give_memory_back_severity_t severity)
{
int p;
if (severity == MEM_TRY_GIVE_MEMORY_BACK_SEVERITY_LOW)
{
p = 8;
}
else if (severity == MEM_TRY_GIVE_MEMORY_BACK_SEVERITY_MEDIUM)
{
p = 4;
}
else if (severity == MEM_TRY_GIVE_MEMORY_BACK_SEVERITY_HIGH)
{
p = 2;
}
else
{
JERRY_ASSERT (severity == MEM_TRY_GIVE_MEMORY_BACK_SEVERITY_CRITICAL);
p = 1;
}
for (int i = 0; i < test_sub_iters; i++)
{
if (rand() % p == 0)
{
if (ptrs[i] != NULL)
{
for( size_t k = 0; k < sizes[i]; k++ )
{
JERRY_ASSERT( ptrs[i][k] == 0 );
}
mem_heap_free_block (ptrs[i]);
ptrs[i] = NULL;
}
}
}
} /* test_heap_give_some_memory_back */
int
main( int __unused argc,
@ -47,58 +92,55 @@ main( int __unused argc,
printf("seed=%d\n", k);
srand((unsigned int) k);
mem_heap_print( true, false, true);
mem_register_a_try_give_memory_back_callback (test_heap_give_some_memory_back);
mem_heap_print (true, false, true);
for ( uint32_t i = 0; i < test_iters; i++ )
{
const uint32_t subiters = test_sub_iters;
uint8_t * ptrs[subiters];
size_t sizes[subiters];
for ( uint32_t j = 0; j < test_sub_iters; j++ )
{
size_t size = (unsigned int) rand() % ( test_threshold_block_size );
ptrs[j] = mem_heap_alloc_block( size, ( rand() % 2 ) ? MEM_HEAP_ALLOC_SHORT_TERM : MEM_HEAP_ALLOC_SHORT_TERM);
sizes[j] = size;
for ( uint32_t j = 0; j < subiters; j++ )
JERRY_ASSERT(size == 0 || ptrs[j] != NULL);
memset(ptrs[j], 0, sizes[j]);
}
// mem_heap_print( true);
for ( uint32_t j = 0; j < test_sub_iters; j++ )
{
if ( ptrs[j] != NULL && (rand () % 2) == 0 )
{
size_t size = (unsigned int) rand() % ( test_threshold_block_size );
ptrs[j] = mem_heap_alloc_block( size, ( rand() % 2 ) ? MEM_HEAP_ALLOC_SHORT_TERM : MEM_HEAP_ALLOC_SHORT_TERM);
sizes[j] = size;
if ( ptrs[j] != NULL )
{
memset(ptrs[j], 0, sizes[j]);
}
// JERRY_ASSERT(ptrs[j] != NULL);
for( size_t k = 0; k < sizes[j]; k++ )
{
JERRY_ASSERT(ptrs[j][k] == 0);
}
size_t new_size = (unsigned int) rand() % ( test_threshold_block_size );
if (mem_heap_try_resize_block (ptrs[j], new_size))
{
sizes[j] = new_size;
memset (ptrs[j], 0, sizes[j]);
}
}
}
// mem_heap_print( true);
for ( uint32_t j = 0; j < subiters; j++ )
for ( uint32_t j = 0; j < test_sub_iters; j++ )
{
if ( ptrs[j] != NULL )
{
if ( ptrs[j] != NULL && (rand () % 2) == 0 )
{
for( size_t k = 0; k < sizes[j]; k++ )
{
JERRY_ASSERT( ptrs[j][k] == 0 );
}
size_t new_size = (unsigned int) rand() % ( test_threshold_block_size );
if (mem_heap_try_resize_block (ptrs[j], new_size))
{
sizes[j] = new_size;
memset (ptrs[j], 0, sizes[j]);
}
}
}
for ( uint32_t j = 0; j < subiters; j++ )
{
if ( ptrs[j] != NULL )
{
for( size_t k = 0; k < sizes[j]; k++ )
{
JERRY_ASSERT( ptrs[j][k] == 0 );
}
mem_heap_free_block( ptrs[j]);
}
for( size_t k = 0; k < sizes[j]; k++ )
{
JERRY_ASSERT( ptrs[j][k] == 0 );
}
mem_heap_free_block (ptrs[j]);
ptrs[j] = NULL;
}
}
}
mem_heap_print( true, false, true);