1 /* GLIB sliced memory - fast concurrent memory chunk allocator
2 * Copyright (C) 2005 Tim Janik
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
22 #include "glibconfig.h"
24 #if defined HAVE_POSIX_MEMALIGN && defined POSIX_MEMALIGN_WITH_COMPLIANT_ALLOCS
25 # define HAVE_COMPLIANT_POSIX_MEMALIGN 1
28 #if defined(HAVE_COMPLIANT_POSIX_MEMALIGN) && !defined(_XOPEN_SOURCE)
29 #define _XOPEN_SOURCE 600 /* posix_memalign() */
31 #include <stdlib.h> /* posix_memalign() */
36 #include <unistd.h> /* sysconf() */
43 #include <stdio.h> /* fputs/fprintf */
48 #include "gmem.h" /* gslice.h */
49 #include "gstrfuncs.h"
51 #include "gtestutils.h"
53 #include "gthreadprivate.h"
54 #include "glib_trace.h"
55 #include "glib-ctor.h"
57 /* the GSlice allocator is split up into 4 layers, roughly modelled after the slab
58 * allocator and magazine extensions as outlined in:
59 * + [Bonwick94] Jeff Bonwick, The slab allocator: An object-caching kernel
60 * memory allocator. USENIX 1994, http://citeseer.ist.psu.edu/bonwick94slab.html
61 * + [Bonwick01] Bonwick and Jonathan Adams, Magazines and vmem: Extending the
62 * slab allocator to many cpu's and arbitrary resources.
63 * USENIX 2001, http://citeseer.ist.psu.edu/bonwick01magazines.html
65 * - the thread magazines. for each (aligned) chunk size, a magazine (a list)
66 * of recently freed and soon to be allocated chunks is maintained per thread.
67 * this way, most alloc/free requests can be quickly satisfied from per-thread
68 * free lists which only require one g_private_get() call to retrive the
70 * - the magazine cache. allocating and freeing chunks to/from threads only
71 * occours at magazine sizes from a global depot of magazines. the depot
72 * maintaines a 15 second working set of allocated magazines, so full
73 * magazines are not allocated and released too often.
74 * the chunk size dependent magazine sizes automatically adapt (within limits,
75 * see [3]) to lock contention to properly scale performance across a variety
77 * - the slab allocator. this allocator allocates slabs (blocks of memory) close
78 * to the system page size or multiples thereof which have to be page aligned.
79 * the blocks are divided into smaller chunks which are used to satisfy
80 * allocations from the upper layers. the space provided by the reminder of
81 * the chunk size division is used for cache colorization (random distribution
82 * of chunk addresses) to improve processor cache utilization. multiple slabs
83 * with the same chunk size are kept in a partially sorted ring to allow O(1)
84 * freeing and allocation of chunks (as long as the allocation of an entirely
85 * new slab can be avoided).
86 * - the page allocator. on most modern systems, posix_memalign(3) or
87 * memalign(3) should be available, so this is used to allocate blocks with
88 * system page size based alignments and sizes or multiples thereof.
89 * if no memalign variant is provided, valloc() is used instead and
90 * block sizes are limited to the system page size (no multiples thereof).
91 * as a fallback, on system without even valloc(), a malloc(3)-based page
92 * allocator with alloc-only behaviour is used.
95 * [1] some systems memalign(3) implementations may rely on boundary tagging for
96 * the handed out memory chunks. to avoid excessive page-wise fragmentation,
97 * we reserve 2 * sizeof (void*) per block size for the systems memalign(3),
98 * specified in NATIVE_MALLOC_PADDING.
99 * [2] using the slab allocator alone already provides for a fast and efficient
100 * allocator, it doesn't properly scale beyond single-threaded uses though.
101 * also, the slab allocator implements eager free(3)-ing, i.e. does not
102 * provide any form of caching or working set maintenance. so if used alone,
103 * it's vulnerable to trashing for sequences of balanced (alloc, free) pairs
104 * at certain thresholds.
105 * [3] magazine sizes are bound by an implementation specific minimum size and
106 * a chunk size specific maximum to limit magazine storage sizes to roughly
108 * [4] allocating ca. 8 chunks per block/page keeps a good balance between
109 * external and internal fragmentation (<= 12.5%). [Bonwick94]
112 /* --- macros and constants --- */
113 #define LARGEALIGNMENT (256)
114 #define P2ALIGNMENT (2 * sizeof (gsize)) /* fits 2 pointers (assumed to be 2 * GLIB_SIZEOF_SIZE_T below) */
115 #define ALIGN(size, base) ((base) * (gsize) (((size) + (base) - 1) / (base)))
116 #define NATIVE_MALLOC_PADDING P2ALIGNMENT /* per-page padding left for native malloc(3) see [1] */
117 #define SLAB_INFO_SIZE P2ALIGN (sizeof (SlabInfo) + NATIVE_MALLOC_PADDING)
118 #define MAX_MAGAZINE_SIZE (256) /* see [3] and allocator_get_magazine_threshold() for this */
119 #define MIN_MAGAZINE_SIZE (4)
120 #define MAX_STAMP_COUNTER (7) /* distributes the load of gettimeofday() */
121 #define MAX_SLAB_CHUNK_SIZE(al) (((al)->max_page_size - SLAB_INFO_SIZE) / 8) /* we want at last 8 chunks per page, see [4] */
122 #define MAX_SLAB_INDEX(al) (SLAB_INDEX (al, MAX_SLAB_CHUNK_SIZE (al)) + 1)
123 #define SLAB_INDEX(al, asize) ((asize) / P2ALIGNMENT - 1) /* asize must be P2ALIGNMENT aligned */
124 #define SLAB_CHUNK_SIZE(al, ix) (((ix) + 1) * P2ALIGNMENT)
125 #define SLAB_BPAGE_SIZE(al,csz) (8 * (csz) + SLAB_INFO_SIZE)
127 /* optimized version of ALIGN (size, P2ALIGNMENT) */
128 #if GLIB_SIZEOF_SIZE_T * 2 == 8 /* P2ALIGNMENT */
129 #define P2ALIGN(size) (((size) + 0x7) & ~(gsize) 0x7)
130 #elif GLIB_SIZEOF_SIZE_T * 2 == 16 /* P2ALIGNMENT */
131 #define P2ALIGN(size) (((size) + 0xf) & ~(gsize) 0xf)
133 #define P2ALIGN(size) ALIGN (size, P2ALIGNMENT)
136 /* special helpers to avoid gmessage.c dependency */
137 static void mem_error (const char *format, ...) G_GNUC_PRINTF (1,2);
138 #define mem_assert(cond) do { if (G_LIKELY (cond)) ; else mem_error ("assertion failed: %s", #cond); } while (0)
140 /* --- structures --- */
141 typedef struct _ChunkLink ChunkLink;
142 typedef struct _SlabInfo SlabInfo;
143 typedef struct _CachedMagazine CachedMagazine;
151 SlabInfo *next, *prev;
155 gsize count; /* approximative chunks list length */
158 Magazine *magazine1; /* array of MAX_SLAB_INDEX (allocator) */
159 Magazine *magazine2; /* array of MAX_SLAB_INDEX (allocator) */
162 gboolean always_malloc;
163 gboolean bypass_magazines;
164 gboolean debug_blocks;
165 gsize working_set_msecs;
166 guint color_increment;
169 /* const after initialization */
170 gsize min_page_size, max_page_size;
172 gsize max_slab_chunk_size_for_magazine_cache;
174 GMutex magazine_mutex;
175 ChunkLink **magazines; /* array of MAX_SLAB_INDEX (allocator) */
176 guint *contention_counters; /* array of MAX_SLAB_INDEX (allocator) */
182 SlabInfo **slab_stack; /* array of MAX_SLAB_INDEX (allocator) */
186 /* --- g-slice prototypes --- */
187 static gpointer slab_allocator_alloc_chunk (gsize chunk_size);
188 static void slab_allocator_free_chunk (gsize chunk_size,
190 static void private_thread_memory_cleanup (gpointer data);
191 static gpointer allocator_memalign (gsize alignment,
193 static void allocator_memfree (gsize memsize,
195 static inline void magazine_cache_update_stamp (void);
196 static inline gsize allocator_get_magazine_threshold (Allocator *allocator,
199 /* --- g-slice memory checker --- */
200 static void smc_notify_alloc (void *pointer,
202 static int smc_notify_free (void *pointer,
205 /* --- variables --- */
206 static GPrivate private_thread_memory;
207 static gsize sys_page_size = 0;
208 static Allocator allocator[1] = { { 0, }, };
209 static SliceConfig slice_config = {
210 FALSE, /* always_malloc */
211 FALSE, /* bypass_magazines */
212 FALSE, /* debug_blocks */
213 15 * 1000, /* working_set_msecs */
214 1, /* color increment, alt: 0x7fffffff */
216 static GMutex smc_tree_mutex = G_MUTEX_INIT; /* mutex for G_SLICE=debug-blocks */
218 /* --- auxiliary funcitons --- */
220 g_slice_set_config (GSliceConfig ckey,
223 g_return_if_fail (sys_page_size == 0);
226 case G_SLICE_CONFIG_ALWAYS_MALLOC:
227 slice_config.always_malloc = value != 0;
229 case G_SLICE_CONFIG_BYPASS_MAGAZINES:
230 slice_config.bypass_magazines = value != 0;
232 case G_SLICE_CONFIG_WORKING_SET_MSECS:
233 slice_config.working_set_msecs = value;
235 case G_SLICE_CONFIG_COLOR_INCREMENT:
236 slice_config.color_increment = value;
242 g_slice_get_config (GSliceConfig ckey)
246 case G_SLICE_CONFIG_ALWAYS_MALLOC:
247 return slice_config.always_malloc;
248 case G_SLICE_CONFIG_BYPASS_MAGAZINES:
249 return slice_config.bypass_magazines;
250 case G_SLICE_CONFIG_WORKING_SET_MSECS:
251 return slice_config.working_set_msecs;
252 case G_SLICE_CONFIG_CHUNK_SIZES:
253 return MAX_SLAB_INDEX (allocator);
254 case G_SLICE_CONFIG_COLOR_INCREMENT:
255 return slice_config.color_increment;
262 g_slice_get_config_state (GSliceConfig ckey,
267 g_return_val_if_fail (n_values != NULL, NULL);
272 case G_SLICE_CONFIG_CONTENTION_COUNTER:
273 array[i++] = SLAB_CHUNK_SIZE (allocator, address);
274 array[i++] = allocator->contention_counters[address];
275 array[i++] = allocator_get_magazine_threshold (allocator, address);
277 return g_memdup (array, sizeof (array[0]) * *n_values);
284 slice_config_init (SliceConfig *config)
286 /* don't use g_malloc/g_message here */
288 const gchar *val = _g_getenv_nomalloc ("G_SLICE", buffer);
289 const GDebugKey keys[] = {
290 { "always-malloc", 1 << 0 },
291 { "debug-blocks", 1 << 1 },
293 gint flags = !val ? 0 : g_parse_debug_string (val, keys, G_N_ELEMENTS (keys));
294 *config = slice_config;
295 if (flags & (1 << 0)) /* always-malloc */
296 config->always_malloc = TRUE;
297 if (flags & (1 << 1)) /* debug-blocks */
298 config->debug_blocks = TRUE;
301 GLIB_CTOR (g_slice_init_nomessage)
303 /* we may not use g_error() or friends here */
304 mem_assert (sys_page_size == 0);
305 mem_assert (MIN_MAGAZINE_SIZE >= 4);
309 SYSTEM_INFO system_info;
310 GetSystemInfo (&system_info);
311 sys_page_size = system_info.dwPageSize;
314 sys_page_size = sysconf (_SC_PAGESIZE); /* = sysconf (_SC_PAGE_SIZE); = getpagesize(); */
316 mem_assert (sys_page_size >= 2 * LARGEALIGNMENT);
317 mem_assert ((sys_page_size & (sys_page_size - 1)) == 0);
318 slice_config_init (&allocator->config);
319 allocator->min_page_size = sys_page_size;
320 #if HAVE_COMPLIANT_POSIX_MEMALIGN || HAVE_MEMALIGN
321 /* allow allocation of pages up to 8KB (with 8KB alignment).
322 * this is useful because many medium to large sized structures
323 * fit less than 8 times (see [4]) into 4KB pages.
324 * we allow very small page sizes here, to reduce wastage in
325 * threads if only small allocations are required (this does
326 * bear the risk of incresing allocation times and fragmentation
329 allocator->min_page_size = MAX (allocator->min_page_size, 4096);
330 allocator->max_page_size = MAX (allocator->min_page_size, 8192);
331 allocator->min_page_size = MIN (allocator->min_page_size, 128);
333 /* we can only align to system page size */
334 allocator->max_page_size = sys_page_size;
336 if (allocator->config.always_malloc)
338 allocator->contention_counters = NULL;
339 allocator->magazines = NULL;
340 allocator->slab_stack = NULL;
344 allocator->contention_counters = g_new0 (guint, MAX_SLAB_INDEX (allocator));
345 allocator->magazines = g_new0 (ChunkLink*, MAX_SLAB_INDEX (allocator));
346 allocator->slab_stack = g_new0 (SlabInfo*, MAX_SLAB_INDEX (allocator));
349 g_mutex_init (&allocator->magazine_mutex);
350 allocator->mutex_counter = 0;
351 allocator->stamp_counter = MAX_STAMP_COUNTER; /* force initial update */
352 allocator->last_stamp = 0;
353 g_mutex_init (&allocator->slab_mutex);
354 allocator->color_accu = 0;
355 magazine_cache_update_stamp();
356 /* values cached for performance reasons */
357 allocator->max_slab_chunk_size_for_magazine_cache = MAX_SLAB_CHUNK_SIZE (allocator);
358 if (allocator->config.always_malloc || allocator->config.bypass_magazines)
359 allocator->max_slab_chunk_size_for_magazine_cache = 0; /* non-optimized cases */
360 /* at this point, g_mem_gc_friendly() should be initialized, this
361 * should have been accomplished by the above g_malloc/g_new calls
363 g_private_init (&private_thread_memory, private_thread_memory_cleanup);
367 allocator_categorize (gsize aligned_chunk_size)
369 GLIB_ENSURE_CTOR (g_slice_init_nomessage);
371 /* speed up the likely path */
372 if (G_LIKELY (aligned_chunk_size && aligned_chunk_size <= allocator->max_slab_chunk_size_for_magazine_cache))
373 return 1; /* use magazine cache */
375 if (!allocator->config.always_malloc &&
376 aligned_chunk_size &&
377 aligned_chunk_size <= MAX_SLAB_CHUNK_SIZE (allocator))
379 if (allocator->config.bypass_magazines)
380 return 2; /* use slab allocator, see [2] */
381 return 1; /* use magazine cache */
383 return 0; /* use malloc() */
387 g_mutex_lock_a (GMutex *mutex,
388 guint *contention_counter)
390 gboolean contention = FALSE;
391 if (!g_mutex_trylock (mutex))
393 g_mutex_lock (mutex);
398 allocator->mutex_counter++;
399 if (allocator->mutex_counter >= 1) /* quickly adapt to contention */
401 allocator->mutex_counter = 0;
402 *contention_counter = MIN (*contention_counter + 1, MAX_MAGAZINE_SIZE);
405 else /* !contention */
407 allocator->mutex_counter--;
408 if (allocator->mutex_counter < -11) /* moderately recover magazine sizes */
410 allocator->mutex_counter = 0;
411 *contention_counter = MAX (*contention_counter, 1) - 1;
416 static inline ThreadMemory*
417 thread_memory_from_self (void)
419 ThreadMemory *tmem = g_private_get (&private_thread_memory);
420 if (G_UNLIKELY (!tmem))
422 static ThreadMemory *single_thread_memory = NULL; /* remember single-thread info for multi-threaded case */
423 if (single_thread_memory && g_thread_supported ())
425 g_mutex_lock (&allocator->slab_mutex);
426 if (single_thread_memory)
428 /* GSlice has been used before g_thread_init(), and now
429 * we are running threaded. to cope with it, use the saved
430 * thread memory structure from when we weren't threaded.
432 tmem = single_thread_memory;
433 single_thread_memory = NULL; /* slab_mutex protected when multi-threaded */
435 g_mutex_unlock (&allocator->slab_mutex);
439 const guint n_magazines = MAX_SLAB_INDEX (allocator);
440 tmem = g_malloc0 (sizeof (ThreadMemory) + sizeof (Magazine) * 2 * n_magazines);
441 tmem->magazine1 = (Magazine*) (tmem + 1);
442 tmem->magazine2 = &tmem->magazine1[n_magazines];
444 /* g_private_get/g_private_set works in the single-threaded xor the multi-
445 * threaded case. but not *across* g_thread_init(), after multi-thread
446 * initialization it returns NULL for previously set single-thread data.
448 g_private_set (&private_thread_memory, tmem);
449 /* save single-thread thread memory structure, in case we need to
450 * pick it up again after multi-thread initialization happened.
452 if (!single_thread_memory && !g_thread_supported ())
453 single_thread_memory = tmem; /* no slab_mutex created yet */
458 static inline ChunkLink*
459 magazine_chain_pop_head (ChunkLink **magazine_chunks)
461 /* magazine chains are linked via ChunkLink->next.
462 * each ChunkLink->data of the toplevel chain may point to a subchain,
463 * linked via ChunkLink->next. ChunkLink->data of the subchains just
464 * contains uninitialized junk.
466 ChunkLink *chunk = (*magazine_chunks)->data;
467 if (G_UNLIKELY (chunk))
469 /* allocating from freed list */
470 (*magazine_chunks)->data = chunk->next;
474 chunk = *magazine_chunks;
475 *magazine_chunks = chunk->next;
480 #if 0 /* useful for debugging */
482 magazine_count (ChunkLink *head)
489 ChunkLink *child = head->data;
491 for (child = head->data; child; child = child->next)
500 allocator_get_magazine_threshold (Allocator *allocator,
503 /* the magazine size calculated here has a lower bound of MIN_MAGAZINE_SIZE,
504 * which is required by the implementation. also, for moderately sized chunks
505 * (say >= 64 bytes), magazine sizes shouldn't be much smaller then the number
506 * of chunks available per page/2 to avoid excessive traffic in the magazine
507 * cache for small to medium sized structures.
508 * the upper bound of the magazine size is effectively provided by
509 * MAX_MAGAZINE_SIZE. for larger chunks, this number is scaled down so that
510 * the content of a single magazine doesn't exceed ca. 16KB.
512 gsize chunk_size = SLAB_CHUNK_SIZE (allocator, ix);
513 guint threshold = MAX (MIN_MAGAZINE_SIZE, allocator->max_page_size / MAX (5 * chunk_size, 5 * 32));
514 guint contention_counter = allocator->contention_counters[ix];
515 if (G_UNLIKELY (contention_counter)) /* single CPU bias */
517 /* adapt contention counter thresholds to chunk sizes */
518 contention_counter = contention_counter * 64 / chunk_size;
519 threshold = MAX (threshold, contention_counter);
524 /* --- magazine cache --- */
526 magazine_cache_update_stamp (void)
528 if (allocator->stamp_counter >= MAX_STAMP_COUNTER)
531 g_get_current_time (&tv);
532 allocator->last_stamp = tv.tv_sec * 1000 + tv.tv_usec / 1000; /* milli seconds */
533 allocator->stamp_counter = 0;
536 allocator->stamp_counter++;
539 static inline ChunkLink*
540 magazine_chain_prepare_fields (ChunkLink *magazine_chunks)
546 /* checked upon initialization: mem_assert (MIN_MAGAZINE_SIZE >= 4); */
547 /* ensure a magazine with at least 4 unused data pointers */
548 chunk1 = magazine_chain_pop_head (&magazine_chunks);
549 chunk2 = magazine_chain_pop_head (&magazine_chunks);
550 chunk3 = magazine_chain_pop_head (&magazine_chunks);
551 chunk4 = magazine_chain_pop_head (&magazine_chunks);
552 chunk4->next = magazine_chunks;
553 chunk3->next = chunk4;
554 chunk2->next = chunk3;
555 chunk1->next = chunk2;
559 /* access the first 3 fields of a specially prepared magazine chain */
560 #define magazine_chain_prev(mc) ((mc)->data)
561 #define magazine_chain_stamp(mc) ((mc)->next->data)
562 #define magazine_chain_uint_stamp(mc) GPOINTER_TO_UINT ((mc)->next->data)
563 #define magazine_chain_next(mc) ((mc)->next->next->data)
564 #define magazine_chain_count(mc) ((mc)->next->next->next->data)
567 magazine_cache_trim (Allocator *allocator,
571 /* g_mutex_lock (allocator->mutex); done by caller */
572 /* trim magazine cache from tail */
573 ChunkLink *current = magazine_chain_prev (allocator->magazines[ix]);
574 ChunkLink *trash = NULL;
575 while (ABS (stamp - magazine_chain_uint_stamp (current)) >= allocator->config.working_set_msecs)
578 ChunkLink *prev = magazine_chain_prev (current);
579 ChunkLink *next = magazine_chain_next (current);
580 magazine_chain_next (prev) = next;
581 magazine_chain_prev (next) = prev;
582 /* clear special fields, put on trash stack */
583 magazine_chain_next (current) = NULL;
584 magazine_chain_count (current) = NULL;
585 magazine_chain_stamp (current) = NULL;
586 magazine_chain_prev (current) = trash;
588 /* fixup list head if required */
589 if (current == allocator->magazines[ix])
591 allocator->magazines[ix] = NULL;
596 g_mutex_unlock (&allocator->magazine_mutex);
600 const gsize chunk_size = SLAB_CHUNK_SIZE (allocator, ix);
601 g_mutex_lock (&allocator->slab_mutex);
605 trash = magazine_chain_prev (current);
606 magazine_chain_prev (current) = NULL; /* clear special field */
609 ChunkLink *chunk = magazine_chain_pop_head (¤t);
610 slab_allocator_free_chunk (chunk_size, chunk);
613 g_mutex_unlock (&allocator->slab_mutex);
618 magazine_cache_push_magazine (guint ix,
619 ChunkLink *magazine_chunks,
620 gsize count) /* must be >= MIN_MAGAZINE_SIZE */
622 ChunkLink *current = magazine_chain_prepare_fields (magazine_chunks);
623 ChunkLink *next, *prev;
624 g_mutex_lock (&allocator->magazine_mutex);
625 /* add magazine at head */
626 next = allocator->magazines[ix];
628 prev = magazine_chain_prev (next);
630 next = prev = current;
631 magazine_chain_next (prev) = current;
632 magazine_chain_prev (next) = current;
633 magazine_chain_prev (current) = prev;
634 magazine_chain_next (current) = next;
635 magazine_chain_count (current) = (gpointer) count;
637 magazine_cache_update_stamp();
638 magazine_chain_stamp (current) = GUINT_TO_POINTER (allocator->last_stamp);
639 allocator->magazines[ix] = current;
640 /* free old magazines beyond a certain threshold */
641 magazine_cache_trim (allocator, ix, allocator->last_stamp);
642 /* g_mutex_unlock (allocator->mutex); was done by magazine_cache_trim() */
646 magazine_cache_pop_magazine (guint ix,
649 g_mutex_lock_a (&allocator->magazine_mutex, &allocator->contention_counters[ix]);
650 if (!allocator->magazines[ix])
652 guint magazine_threshold = allocator_get_magazine_threshold (allocator, ix);
653 gsize i, chunk_size = SLAB_CHUNK_SIZE (allocator, ix);
654 ChunkLink *chunk, *head;
655 g_mutex_unlock (&allocator->magazine_mutex);
656 g_mutex_lock (&allocator->slab_mutex);
657 head = slab_allocator_alloc_chunk (chunk_size);
660 for (i = 1; i < magazine_threshold; i++)
662 chunk->next = slab_allocator_alloc_chunk (chunk_size);
667 g_mutex_unlock (&allocator->slab_mutex);
673 ChunkLink *current = allocator->magazines[ix];
674 ChunkLink *prev = magazine_chain_prev (current);
675 ChunkLink *next = magazine_chain_next (current);
677 magazine_chain_next (prev) = next;
678 magazine_chain_prev (next) = prev;
679 allocator->magazines[ix] = next == current ? NULL : next;
680 g_mutex_unlock (&allocator->magazine_mutex);
681 /* clear special fields and hand out */
682 *countp = (gsize) magazine_chain_count (current);
683 magazine_chain_prev (current) = NULL;
684 magazine_chain_next (current) = NULL;
685 magazine_chain_count (current) = NULL;
686 magazine_chain_stamp (current) = NULL;
691 /* --- thread magazines --- */
693 private_thread_memory_cleanup (gpointer data)
695 ThreadMemory *tmem = data;
696 const guint n_magazines = MAX_SLAB_INDEX (allocator);
698 for (ix = 0; ix < n_magazines; ix++)
702 mags[0] = &tmem->magazine1[ix];
703 mags[1] = &tmem->magazine2[ix];
704 for (j = 0; j < 2; j++)
706 Magazine *mag = mags[j];
707 if (mag->count >= MIN_MAGAZINE_SIZE)
708 magazine_cache_push_magazine (ix, mag->chunks, mag->count);
711 const gsize chunk_size = SLAB_CHUNK_SIZE (allocator, ix);
712 g_mutex_lock (&allocator->slab_mutex);
715 ChunkLink *chunk = magazine_chain_pop_head (&mag->chunks);
716 slab_allocator_free_chunk (chunk_size, chunk);
718 g_mutex_unlock (&allocator->slab_mutex);
726 thread_memory_magazine1_reload (ThreadMemory *tmem,
729 Magazine *mag = &tmem->magazine1[ix];
730 mem_assert (mag->chunks == NULL); /* ensure that we may reset mag->count */
732 mag->chunks = magazine_cache_pop_magazine (ix, &mag->count);
736 thread_memory_magazine2_unload (ThreadMemory *tmem,
739 Magazine *mag = &tmem->magazine2[ix];
740 magazine_cache_push_magazine (ix, mag->chunks, mag->count);
746 thread_memory_swap_magazines (ThreadMemory *tmem,
749 Magazine xmag = tmem->magazine1[ix];
750 tmem->magazine1[ix] = tmem->magazine2[ix];
751 tmem->magazine2[ix] = xmag;
754 static inline gboolean
755 thread_memory_magazine1_is_empty (ThreadMemory *tmem,
758 return tmem->magazine1[ix].chunks == NULL;
761 static inline gboolean
762 thread_memory_magazine2_is_full (ThreadMemory *tmem,
765 return tmem->magazine2[ix].count >= allocator_get_magazine_threshold (allocator, ix);
768 static inline gpointer
769 thread_memory_magazine1_alloc (ThreadMemory *tmem,
772 Magazine *mag = &tmem->magazine1[ix];
773 ChunkLink *chunk = magazine_chain_pop_head (&mag->chunks);
774 if (G_LIKELY (mag->count > 0))
780 thread_memory_magazine2_free (ThreadMemory *tmem,
784 Magazine *mag = &tmem->magazine2[ix];
785 ChunkLink *chunk = mem;
787 chunk->next = mag->chunks;
792 /* --- API functions --- */
794 g_slice_alloc (gsize mem_size)
799 chunk_size = P2ALIGN (mem_size);
800 acat = allocator_categorize (chunk_size);
801 if (G_LIKELY (acat == 1)) /* allocate through magazine layer */
803 ThreadMemory *tmem = thread_memory_from_self();
804 guint ix = SLAB_INDEX (allocator, chunk_size);
805 if (G_UNLIKELY (thread_memory_magazine1_is_empty (tmem, ix)))
807 thread_memory_swap_magazines (tmem, ix);
808 if (G_UNLIKELY (thread_memory_magazine1_is_empty (tmem, ix)))
809 thread_memory_magazine1_reload (tmem, ix);
811 mem = thread_memory_magazine1_alloc (tmem, ix);
813 else if (acat == 2) /* allocate through slab allocator */
815 g_mutex_lock (&allocator->slab_mutex);
816 mem = slab_allocator_alloc_chunk (chunk_size);
817 g_mutex_unlock (&allocator->slab_mutex);
819 else /* delegate to system malloc */
820 mem = g_malloc (mem_size);
821 if (G_UNLIKELY (allocator->config.debug_blocks))
822 smc_notify_alloc (mem, mem_size);
824 TRACE (GLIB_SLICE_ALLOC((void*)mem, mem_size));
830 g_slice_alloc0 (gsize mem_size)
832 gpointer mem = g_slice_alloc (mem_size);
834 memset (mem, 0, mem_size);
839 g_slice_copy (gsize mem_size,
840 gconstpointer mem_block)
842 gpointer mem = g_slice_alloc (mem_size);
844 memcpy (mem, mem_block, mem_size);
849 g_slice_free1 (gsize mem_size,
852 gsize chunk_size = P2ALIGN (mem_size);
853 guint acat = allocator_categorize (chunk_size);
854 if (G_UNLIKELY (!mem_block))
856 if (G_UNLIKELY (allocator->config.debug_blocks) &&
857 !smc_notify_free (mem_block, mem_size))
859 if (G_LIKELY (acat == 1)) /* allocate through magazine layer */
861 ThreadMemory *tmem = thread_memory_from_self();
862 guint ix = SLAB_INDEX (allocator, chunk_size);
863 if (G_UNLIKELY (thread_memory_magazine2_is_full (tmem, ix)))
865 thread_memory_swap_magazines (tmem, ix);
866 if (G_UNLIKELY (thread_memory_magazine2_is_full (tmem, ix)))
867 thread_memory_magazine2_unload (tmem, ix);
869 if (G_UNLIKELY (g_mem_gc_friendly))
870 memset (mem_block, 0, chunk_size);
871 thread_memory_magazine2_free (tmem, ix, mem_block);
873 else if (acat == 2) /* allocate through slab allocator */
875 if (G_UNLIKELY (g_mem_gc_friendly))
876 memset (mem_block, 0, chunk_size);
877 g_mutex_lock (&allocator->slab_mutex);
878 slab_allocator_free_chunk (chunk_size, mem_block);
879 g_mutex_unlock (&allocator->slab_mutex);
881 else /* delegate to system malloc */
883 if (G_UNLIKELY (g_mem_gc_friendly))
884 memset (mem_block, 0, mem_size);
887 TRACE (GLIB_SLICE_FREE((void*)mem_block, mem_size));
891 g_slice_free_chain_with_offset (gsize mem_size,
895 gpointer slice = mem_chain;
896 /* while the thread magazines and the magazine cache are implemented so that
897 * they can easily be extended to allow for free lists containing more free
898 * lists for the first level nodes, which would allow O(1) freeing in this
899 * function, the benefit of such an extension is questionable, because:
900 * - the magazine size counts will become mere lower bounds which confuses
901 * the code adapting to lock contention;
902 * - freeing a single node to the thread magazines is very fast, so this
903 * O(list_length) operation is multiplied by a fairly small factor;
904 * - memory usage histograms on larger applications seem to indicate that
905 * the amount of released multi node lists is negligible in comparison
906 * to single node releases.
907 * - the major performance bottle neck, namely g_private_get() or
908 * g_mutex_lock()/g_mutex_unlock() has already been moved out of the
909 * inner loop for freeing chained slices.
911 gsize chunk_size = P2ALIGN (mem_size);
912 guint acat = allocator_categorize (chunk_size);
913 if (G_LIKELY (acat == 1)) /* allocate through magazine layer */
915 ThreadMemory *tmem = thread_memory_from_self();
916 guint ix = SLAB_INDEX (allocator, chunk_size);
919 guint8 *current = slice;
920 slice = *(gpointer*) (current + next_offset);
921 if (G_UNLIKELY (allocator->config.debug_blocks) &&
922 !smc_notify_free (current, mem_size))
924 if (G_UNLIKELY (thread_memory_magazine2_is_full (tmem, ix)))
926 thread_memory_swap_magazines (tmem, ix);
927 if (G_UNLIKELY (thread_memory_magazine2_is_full (tmem, ix)))
928 thread_memory_magazine2_unload (tmem, ix);
930 if (G_UNLIKELY (g_mem_gc_friendly))
931 memset (current, 0, chunk_size);
932 thread_memory_magazine2_free (tmem, ix, current);
935 else if (acat == 2) /* allocate through slab allocator */
937 g_mutex_lock (&allocator->slab_mutex);
940 guint8 *current = slice;
941 slice = *(gpointer*) (current + next_offset);
942 if (G_UNLIKELY (allocator->config.debug_blocks) &&
943 !smc_notify_free (current, mem_size))
945 if (G_UNLIKELY (g_mem_gc_friendly))
946 memset (current, 0, chunk_size);
947 slab_allocator_free_chunk (chunk_size, current);
949 g_mutex_unlock (&allocator->slab_mutex);
951 else /* delegate to system malloc */
954 guint8 *current = slice;
955 slice = *(gpointer*) (current + next_offset);
956 if (G_UNLIKELY (allocator->config.debug_blocks) &&
957 !smc_notify_free (current, mem_size))
959 if (G_UNLIKELY (g_mem_gc_friendly))
960 memset (current, 0, mem_size);
965 /* --- single page allocator --- */
967 allocator_slab_stack_push (Allocator *allocator,
971 /* insert slab at slab ring head */
972 if (!allocator->slab_stack[ix])
979 SlabInfo *next = allocator->slab_stack[ix], *prev = next->prev;
985 allocator->slab_stack[ix] = sinfo;
989 allocator_aligned_page_size (Allocator *allocator,
992 gsize val = 1 << g_bit_storage (n_bytes - 1);
993 val = MAX (val, allocator->min_page_size);
998 allocator_add_slab (Allocator *allocator,
1004 gsize addr, padding, n_chunks, color = 0;
1005 gsize page_size = allocator_aligned_page_size (allocator, SLAB_BPAGE_SIZE (allocator, chunk_size));
1006 /* allocate 1 page for the chunks and the slab */
1007 gpointer aligned_memory = allocator_memalign (page_size, page_size - NATIVE_MALLOC_PADDING);
1008 guint8 *mem = aligned_memory;
1012 const gchar *syserr = "unknown error";
1014 syserr = strerror (errno);
1016 mem_error ("failed to allocate %u bytes (alignment: %u): %s\n",
1017 (guint) (page_size - NATIVE_MALLOC_PADDING), (guint) page_size, syserr);
1019 /* mask page address */
1020 addr = ((gsize) mem / page_size) * page_size;
1021 /* assert alignment */
1022 mem_assert (aligned_memory == (gpointer) addr);
1023 /* basic slab info setup */
1024 sinfo = (SlabInfo*) (mem + page_size - SLAB_INFO_SIZE);
1025 sinfo->n_allocated = 0;
1026 sinfo->chunks = NULL;
1027 /* figure cache colorization */
1028 n_chunks = ((guint8*) sinfo - mem) / chunk_size;
1029 padding = ((guint8*) sinfo - mem) - n_chunks * chunk_size;
1032 color = (allocator->color_accu * P2ALIGNMENT) % padding;
1033 allocator->color_accu += allocator->config.color_increment;
1035 /* add chunks to free list */
1036 chunk = (ChunkLink*) (mem + color);
1037 sinfo->chunks = chunk;
1038 for (i = 0; i < n_chunks - 1; i++)
1040 chunk->next = (ChunkLink*) ((guint8*) chunk + chunk_size);
1041 chunk = chunk->next;
1043 chunk->next = NULL; /* last chunk */
1044 /* add slab to slab ring */
1045 allocator_slab_stack_push (allocator, ix, sinfo);
1049 slab_allocator_alloc_chunk (gsize chunk_size)
1052 guint ix = SLAB_INDEX (allocator, chunk_size);
1053 /* ensure non-empty slab */
1054 if (!allocator->slab_stack[ix] || !allocator->slab_stack[ix]->chunks)
1055 allocator_add_slab (allocator, ix, chunk_size);
1056 /* allocate chunk */
1057 chunk = allocator->slab_stack[ix]->chunks;
1058 allocator->slab_stack[ix]->chunks = chunk->next;
1059 allocator->slab_stack[ix]->n_allocated++;
1060 /* rotate empty slabs */
1061 if (!allocator->slab_stack[ix]->chunks)
1062 allocator->slab_stack[ix] = allocator->slab_stack[ix]->next;
1067 slab_allocator_free_chunk (gsize chunk_size,
1072 guint ix = SLAB_INDEX (allocator, chunk_size);
1073 gsize page_size = allocator_aligned_page_size (allocator, SLAB_BPAGE_SIZE (allocator, chunk_size));
1074 gsize addr = ((gsize) mem / page_size) * page_size;
1075 /* mask page address */
1076 guint8 *page = (guint8*) addr;
1077 SlabInfo *sinfo = (SlabInfo*) (page + page_size - SLAB_INFO_SIZE);
1078 /* assert valid chunk count */
1079 mem_assert (sinfo->n_allocated > 0);
1080 /* add chunk to free list */
1081 was_empty = sinfo->chunks == NULL;
1082 chunk = (ChunkLink*) mem;
1083 chunk->next = sinfo->chunks;
1084 sinfo->chunks = chunk;
1085 sinfo->n_allocated--;
1086 /* keep slab ring partially sorted, empty slabs at end */
1090 SlabInfo *next = sinfo->next, *prev = sinfo->prev;
1093 if (allocator->slab_stack[ix] == sinfo)
1094 allocator->slab_stack[ix] = next == sinfo ? NULL : next;
1095 /* insert slab at head */
1096 allocator_slab_stack_push (allocator, ix, sinfo);
1098 /* eagerly free complete unused slabs */
1099 if (!sinfo->n_allocated)
1102 SlabInfo *next = sinfo->next, *prev = sinfo->prev;
1105 if (allocator->slab_stack[ix] == sinfo)
1106 allocator->slab_stack[ix] = next == sinfo ? NULL : next;
1108 allocator_memfree (page_size, page);
1112 /* --- memalign implementation --- */
1113 #ifdef HAVE_MALLOC_H
1114 #include <malloc.h> /* memalign() */
1118 * define HAVE_POSIX_MEMALIGN 1 // if free(posix_memalign(3)) works, <stdlib.h>
1119 * define HAVE_COMPLIANT_POSIX_MEMALIGN 1 // if free(posix_memalign(3)) works for sizes != 2^n, <stdlib.h>
1120 * define HAVE_MEMALIGN 1 // if free(memalign(3)) works, <malloc.h>
1121 * define HAVE_VALLOC 1 // if free(valloc(3)) works, <stdlib.h> or <malloc.h>
1122 * if none is provided, we implement malloc(3)-based alloc-only page alignment
1125 #if !(HAVE_COMPLIANT_POSIX_MEMALIGN || HAVE_MEMALIGN || HAVE_VALLOC)
1126 static GTrashStack *compat_valloc_trash = NULL;
1130 allocator_memalign (gsize alignment,
1133 gpointer aligned_memory = NULL;
1135 #if HAVE_COMPLIANT_POSIX_MEMALIGN
1136 err = posix_memalign (&aligned_memory, alignment, memsize);
1139 aligned_memory = memalign (alignment, memsize);
1143 aligned_memory = valloc (memsize);
1146 /* simplistic non-freeing page allocator */
1147 mem_assert (alignment == sys_page_size);
1148 mem_assert (memsize <= sys_page_size);
1149 if (!compat_valloc_trash)
1151 const guint n_pages = 16;
1152 guint8 *mem = malloc (n_pages * sys_page_size);
1157 guint8 *amem = (guint8*) ALIGN ((gsize) mem, sys_page_size);
1159 i--; /* mem wasn't page aligned */
1161 g_trash_stack_push (&compat_valloc_trash, amem + i * sys_page_size);
1164 aligned_memory = g_trash_stack_pop (&compat_valloc_trash);
1166 if (!aligned_memory)
1168 return aligned_memory;
1172 allocator_memfree (gsize memsize,
1175 #if HAVE_COMPLIANT_POSIX_MEMALIGN || HAVE_MEMALIGN || HAVE_VALLOC
1178 mem_assert (memsize <= sys_page_size);
1179 g_trash_stack_push (&compat_valloc_trash, mem);
1184 mem_error (const char *format,
1189 /* at least, put out "MEMORY-ERROR", in case we segfault during the rest of the function */
1190 fputs ("\n***MEMORY-ERROR***: ", stderr);
1191 pname = g_get_prgname();
1192 fprintf (stderr, "%s[%ld]: GSlice: ", pname ? pname : "", (long)getpid());
1193 va_start (args, format);
1194 vfprintf (stderr, format, args);
1196 fputs ("\n", stderr);
1201 /* --- g-slice memory checker tree --- */
1202 typedef size_t SmcKType; /* key type */
1203 typedef size_t SmcVType; /* value type */
1208 static void smc_tree_insert (SmcKType key,
1210 static gboolean smc_tree_lookup (SmcKType key,
1212 static gboolean smc_tree_remove (SmcKType key);
1215 /* --- g-slice memory checker implementation --- */
1217 smc_notify_alloc (void *pointer,
1220 size_t adress = (size_t) pointer;
1222 smc_tree_insert (adress, size);
1227 smc_notify_ignore (void *pointer)
1229 size_t adress = (size_t) pointer;
1231 smc_tree_remove (adress);
1236 smc_notify_free (void *pointer,
1239 size_t adress = (size_t) pointer;
1244 return 1; /* ignore */
1245 found_one = smc_tree_lookup (adress, &real_size);
1248 fprintf (stderr, "GSlice: MemChecker: attempt to release non-allocated block: %p size=%" G_GSIZE_FORMAT "\n", pointer, size);
1251 if (real_size != size && (real_size || size))
1253 fprintf (stderr, "GSlice: MemChecker: attempt to release block with invalid size: %p size=%" G_GSIZE_FORMAT " invalid-size=%" G_GSIZE_FORMAT "\n", pointer, real_size, size);
1256 if (!smc_tree_remove (adress))
1258 fprintf (stderr, "GSlice: MemChecker: attempt to release non-allocated block: %p size=%" G_GSIZE_FORMAT "\n", pointer, size);
1261 return 1; /* all fine */
1264 /* --- g-slice memory checker tree implementation --- */
1265 #define SMC_TRUNK_COUNT (4093 /* 16381 */) /* prime, to distribute trunk collisions (big, allocated just once) */
1266 #define SMC_BRANCH_COUNT (511) /* prime, to distribute branch collisions */
1267 #define SMC_TRUNK_EXTENT (SMC_BRANCH_COUNT * 2039) /* key adress space per trunk, should distribute uniformly across BRANCH_COUNT */
1268 #define SMC_TRUNK_HASH(k) ((k / SMC_TRUNK_EXTENT) % SMC_TRUNK_COUNT) /* generate new trunk hash per megabyte (roughly) */
1269 #define SMC_BRANCH_HASH(k) (k % SMC_BRANCH_COUNT)
1273 unsigned int n_entries;
1276 static SmcBranch **smc_tree_root = NULL;
1279 smc_tree_abort (int errval)
1281 const char *syserr = "unknown error";
1283 syserr = strerror (errval);
1285 mem_error ("MemChecker: failure in debugging tree: %s", syserr);
1288 static inline SmcEntry*
1289 smc_tree_branch_grow_L (SmcBranch *branch,
1292 unsigned int old_size = branch->n_entries * sizeof (branch->entries[0]);
1293 unsigned int new_size = old_size + sizeof (branch->entries[0]);
1295 mem_assert (index <= branch->n_entries);
1296 branch->entries = (SmcEntry*) realloc (branch->entries, new_size);
1297 if (!branch->entries)
1298 smc_tree_abort (errno);
1299 entry = branch->entries + index;
1300 g_memmove (entry + 1, entry, (branch->n_entries - index) * sizeof (entry[0]));
1301 branch->n_entries += 1;
1305 static inline SmcEntry*
1306 smc_tree_branch_lookup_nearest_L (SmcBranch *branch,
1309 unsigned int n_nodes = branch->n_entries, offs = 0;
1310 SmcEntry *check = branch->entries;
1312 while (offs < n_nodes)
1314 unsigned int i = (offs + n_nodes) >> 1;
1315 check = branch->entries + i;
1316 cmp = key < check->key ? -1 : key != check->key;
1318 return check; /* return exact match */
1321 else /* (cmp > 0) */
1324 /* check points at last mismatch, cmp > 0 indicates greater key */
1325 return cmp > 0 ? check + 1 : check; /* return insertion position for inexact match */
1329 smc_tree_insert (SmcKType key,
1332 unsigned int ix0, ix1;
1335 g_mutex_lock (&smc_tree_mutex);
1336 ix0 = SMC_TRUNK_HASH (key);
1337 ix1 = SMC_BRANCH_HASH (key);
1340 smc_tree_root = calloc (SMC_TRUNK_COUNT, sizeof (smc_tree_root[0]));
1342 smc_tree_abort (errno);
1344 if (!smc_tree_root[ix0])
1346 smc_tree_root[ix0] = calloc (SMC_BRANCH_COUNT, sizeof (smc_tree_root[0][0]));
1347 if (!smc_tree_root[ix0])
1348 smc_tree_abort (errno);
1350 entry = smc_tree_branch_lookup_nearest_L (&smc_tree_root[ix0][ix1], key);
1351 if (!entry || /* need create */
1352 entry >= smc_tree_root[ix0][ix1].entries + smc_tree_root[ix0][ix1].n_entries || /* need append */
1353 entry->key != key) /* need insert */
1354 entry = smc_tree_branch_grow_L (&smc_tree_root[ix0][ix1], entry - smc_tree_root[ix0][ix1].entries);
1356 entry->value = value;
1357 g_mutex_unlock (&smc_tree_mutex);
1361 smc_tree_lookup (SmcKType key,
1364 SmcEntry *entry = NULL;
1365 unsigned int ix0 = SMC_TRUNK_HASH (key), ix1 = SMC_BRANCH_HASH (key);
1366 gboolean found_one = FALSE;
1368 g_mutex_lock (&smc_tree_mutex);
1369 if (smc_tree_root && smc_tree_root[ix0])
1371 entry = smc_tree_branch_lookup_nearest_L (&smc_tree_root[ix0][ix1], key);
1373 entry < smc_tree_root[ix0][ix1].entries + smc_tree_root[ix0][ix1].n_entries &&
1377 *value_p = entry->value;
1380 g_mutex_unlock (&smc_tree_mutex);
1385 smc_tree_remove (SmcKType key)
1387 unsigned int ix0 = SMC_TRUNK_HASH (key), ix1 = SMC_BRANCH_HASH (key);
1388 gboolean found_one = FALSE;
1389 g_mutex_lock (&smc_tree_mutex);
1390 if (smc_tree_root && smc_tree_root[ix0])
1392 SmcEntry *entry = smc_tree_branch_lookup_nearest_L (&smc_tree_root[ix0][ix1], key);
1394 entry < smc_tree_root[ix0][ix1].entries + smc_tree_root[ix0][ix1].n_entries &&
1397 unsigned int i = entry - smc_tree_root[ix0][ix1].entries;
1398 smc_tree_root[ix0][ix1].n_entries -= 1;
1399 g_memmove (entry, entry + 1, (smc_tree_root[ix0][ix1].n_entries - i) * sizeof (entry[0]));
1400 if (!smc_tree_root[ix0][ix1].n_entries)
1402 /* avoid useless pressure on the memory system */
1403 free (smc_tree_root[ix0][ix1].entries);
1404 smc_tree_root[ix0][ix1].entries = NULL;
1409 g_mutex_unlock (&smc_tree_mutex);
1413 #ifdef G_ENABLE_DEBUG
1415 g_slice_debug_tree_statistics (void)
1417 g_mutex_lock (&smc_tree_mutex);
1420 unsigned int i, j, t = 0, o = 0, b = 0, su = 0, ex = 0, en = 4294967295u;
1422 for (i = 0; i < SMC_TRUNK_COUNT; i++)
1423 if (smc_tree_root[i])
1426 for (j = 0; j < SMC_BRANCH_COUNT; j++)
1427 if (smc_tree_root[i][j].n_entries)
1430 su += smc_tree_root[i][j].n_entries;
1431 en = MIN (en, smc_tree_root[i][j].n_entries);
1432 ex = MAX (ex, smc_tree_root[i][j].n_entries);
1434 else if (smc_tree_root[i][j].entries)
1435 o++; /* formerly used, now empty */
1438 tf = MAX (t, 1.0); /* max(1) to be a valid divisor */
1439 bf = MAX (b, 1.0); /* max(1) to be a valid divisor */
1440 fprintf (stderr, "GSlice: MemChecker: %u trunks, %u branches, %u old branches\n", t, b, o);
1441 fprintf (stderr, "GSlice: MemChecker: %f branches per trunk, %.2f%% utilization\n",
1443 100.0 - (SMC_BRANCH_COUNT - b / tf) / (0.01 * SMC_BRANCH_COUNT));
1444 fprintf (stderr, "GSlice: MemChecker: %f entries per branch, %u minimum, %u maximum\n",
1448 fprintf (stderr, "GSlice: MemChecker: root=NULL\n");
1449 g_mutex_unlock (&smc_tree_mutex);
1451 /* sample statistics (beast + GSLice + 24h scripted core & GUI activity):
1452 * PID %CPU %MEM VSZ RSS COMMAND
1453 * 8887 30.3 45.8 456068 414856 beast-0.7.1 empty.bse
1454 * $ cat /proc/8887/statm # total-program-size resident-set-size shared-pages text/code data/stack library dirty-pages
1455 * 114017 103714 2354 344 0 108676 0
1456 * $ cat /proc/8887/status
1467 * (gdb) print g_slice_debug_tree_statistics ()
1468 * GSlice: MemChecker: 422 trunks, 213068 branches, 0 old branches
1469 * GSlice: MemChecker: 504.900474 branches per trunk, 98.81% utilization
1470 * GSlice: MemChecker: 4.965039 entries per branch, 1 minimum, 37 maximum
1473 #endif /* G_ENABLE_DEBUG */