From 9fd052e775623581d2d277a1688b9fde94cb18d2 Mon Sep 17 00:00:00 2001 From: Jan Hubicka Date: Fri, 22 Nov 2019 15:37:37 +0100 Subject: [PATCH] Release memory-block-pool memory back to malloc. * ggc-page.c (ggc_collect): Call memory_block_pool::trim. * memory-block.cc (memory_block_pool::clear_free_list): Rename to ... (memory_block_pool::reduce_free_list): ... this one. (memory_block_pool::trim): New static function. * memory-block.h (memory_block_pool::freelist_size): New constant (memory_block_pool::clear_free_list): Rename to ... (memory_block_pool::reduce_free_list): ... this one. (memory_block_pool::trim): Declare. * lto.c (lto_wpa_write_files): Call memory_block_pool::trim. From-SVN: r278616 --- gcc/ChangeLog | 11 +++++++++++ gcc/ggc-page.c | 3 +++ gcc/lto/ChangeLog | 4 ++++ gcc/lto/lto.c | 1 + gcc/memory-block.cc | 34 ++++++++++++++++++++++++++++------ gcc/memory-block.h | 5 ++++- 6 files changed, 51 insertions(+), 7 deletions(-) diff --git a/gcc/ChangeLog b/gcc/ChangeLog index bdd0fc529b7..62ddef2dd31 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,14 @@ +2019-11-22 Jan Hubicka + + * ggc-page.c (ggc_collect): Call memory_block_pool::trim. + * memory-block.cc (memory_block_pool::clear_free_list): Rename to ... + (memory_block_pool::reduce_free_list): ... this one. + (memory_block_pool::trim): New static function. + * memory-block.h (memory_block_pool::freelist_size): New constant + (memory_block_pool::clear_free_list): Rename to ... + (memory_block_pool::reduce_free_list): ... this one. + (memory_block_pool::trim): Declare. + 2019-11-22 Richard Sandiford * tree-vect-stmts.c (vect_model_simple_cost): Take an optional diff --git a/gcc/ggc-page.c b/gcc/ggc-page.c index dde39e680da..4a6a7c9598e 100644 --- a/gcc/ggc-page.c +++ b/gcc/ggc-page.c @@ -2186,6 +2186,9 @@ ggc_collect (void) float allocated_last_gc = MAX (G.allocated_last_gc, (size_t)param_ggc_min_heapsize * 1024); + /* It is also good time to get memory block pool into limits. */ + memory_block_pool::trim (); + float min_expand = allocated_last_gc * param_ggc_min_expand / 100; if (G.allocated < allocated_last_gc + min_expand && !ggc_force_collect) return; diff --git a/gcc/lto/ChangeLog b/gcc/lto/ChangeLog index ba3e38aa364..cdf6964fbb7 100644 --- a/gcc/lto/ChangeLog +++ b/gcc/lto/ChangeLog @@ -1,3 +1,7 @@ +2019-11-22 Jan Hubicka + + * lto.c (lto_wpa_write_files): Call memory_block_pool::trim. + 2019-11-14 Martin Liska * Make-lang.in: Remove wrong dependency diff --git a/gcc/lto/lto.c b/gcc/lto/lto.c index b7e6ffd8c8b..c9e0fcd8a8e 100644 --- a/gcc/lto/lto.c +++ b/gcc/lto/lto.c @@ -387,6 +387,7 @@ lto_wpa_write_files (void) temp_priority.safe_push (part->insns); temp_filenames.safe_push (xstrdup (temp_filename)); } + memory_block_pool::trim (0); for (int set = 0; set < MAX (lto_parallelism, 1); set++) { diff --git a/gcc/memory-block.cc b/gcc/memory-block.cc index 507feaed157..ebf8cdd1e53 100644 --- a/gcc/memory-block.cc +++ b/gcc/memory-block.cc @@ -28,15 +28,30 @@ memory_block_pool memory_block_pool::instance; memory_block_pool::memory_block_pool () : m_blocks (NULL) {} -/* Return all blocks from free list to the OS. */ +/* Reduce free list to NUM blocks and return remaining to malloc. */ void -memory_block_pool::clear_free_list () +memory_block_pool::reduce_free_list (int num) { - while (m_blocks) + block_list **blocks = &m_blocks; + + /* First skip NUM blocks. */ + + for (;num > 0 && *blocks; num--) + blocks = &(*blocks)->m_next; + + if (!*blocks) + return; + + /* And free the remainder of them. */ + + block_list *to_free = *blocks; + *blocks = NULL; + + while (to_free) { - block_list *next = m_blocks->m_next; - XDELETEVEC (m_blocks); - m_blocks = next; + block_list *next = to_free->m_next; + XDELETEVEC (to_free); + to_free = next; } } @@ -62,3 +77,10 @@ mempool_obstack_chunk_free (void *chunk) else XDELETEVEC (chunk); } + +/* Return allocated memory back to malloc (and to system). */ +void +memory_block_pool::trim (int num) +{ + instance.reduce_free_list (num); +} diff --git a/gcc/memory-block.h b/gcc/memory-block.h index 8ebeee0f6ff..a11465b20d6 100644 --- a/gcc/memory-block.h +++ b/gcc/memory-block.h @@ -28,12 +28,15 @@ class memory_block_pool public: /* Blocks have fixed size. This is necessary for sharing. */ static const size_t block_size = 64 * 1024; + /* Number of blocks we keep in the freelists. */ + static const size_t freelist_size = 1024 * 1024 / block_size; memory_block_pool (); static inline void *allocate () ATTRIBUTE_MALLOC; static inline void release (void *); - void clear_free_list (); + static void trim (int nblocks = freelist_size); + void reduce_free_list (int); private: /* memory_block_pool singleton instance, defined in memory-block.cc. */ -- 2.30.2