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"
56 /* the GSlice allocator is split up into 4 layers, roughly modelled after the slab
57 * allocator and magazine extensions as outlined in:
58 * + [Bonwick94] Jeff Bonwick, The slab allocator: An object-caching kernel
59 * memory allocator. USENIX 1994, http://citeseer.ist.psu.edu/bonwick94slab.html
60 * + [Bonwick01] Bonwick and Jonathan Adams, Magazines and vmem: Extending the
61 * slab allocator to many cpu's and arbitrary resources.
62 * USENIX 2001, http://citeseer.ist.psu.edu/bonwick01magazines.html
64 * - the thread magazines. for each (aligned) chunk size, a magazine (a list)
65 * of recently freed and soon to be allocated chunks is maintained per thread.
66 * this way, most alloc/free requests can be quickly satisfied from per-thread
67 * free lists which only require one g_private_get() call to retrive the
69 * - the magazine cache. allocating and freeing chunks to/from threads only
70 * occours at magazine sizes from a global depot of magazines. the depot
71 * maintaines a 15 second working set of allocated magazines, so full
72 * magazines are not allocated and released too often.
73 * the chunk size dependent magazine sizes automatically adapt (within limits,
74 * see [3]) to lock contention to properly scale performance across a variety
76 * - the slab allocator. this allocator allocates slabs (blocks of memory) close
77 * to the system page size or multiples thereof which have to be page aligned.
78 * the blocks are divided into smaller chunks which are used to satisfy
79 * allocations from the upper layers. the space provided by the reminder of
80 * the chunk size division is used for cache colorization (random distribution
81 * of chunk addresses) to improve processor cache utilization. multiple slabs
82 * with the same chunk size are kept in a partially sorted ring to allow O(1)
83 * freeing and allocation of chunks (as long as the allocation of an entirely
84 * new slab can be avoided).
85 * - the page allocator. on most modern systems, posix_memalign(3) or
86 * memalign(3) should be available, so this is used to allocate blocks with
87 * system page size based alignments and sizes or multiples thereof.
88 * if no memalign variant is provided, valloc() is used instead and
89 * block sizes are limited to the system page size (no multiples thereof).
90 * as a fallback, on system without even valloc(), a malloc(3)-based page
91 * allocator with alloc-only behaviour is used.
94 * [1] some systems memalign(3) implementations may rely on boundary tagging for
95 * the handed out memory chunks. to avoid excessive page-wise fragmentation,
96 * we reserve 2 * sizeof (void*) per block size for the systems memalign(3),
97 * specified in NATIVE_MALLOC_PADDING.
98 * [2] using the slab allocator alone already provides for a fast and efficient
99 * allocator, it doesn't properly scale beyond single-threaded uses though.
100 * also, the slab allocator implements eager free(3)-ing, i.e. does not
101 * provide any form of caching or working set maintenance. so if used alone,
102 * it's vulnerable to trashing for sequences of balanced (alloc, free) pairs
103 * at certain thresholds.
104 * [3] magazine sizes are bound by an implementation specific minimum size and
105 * a chunk size specific maximum to limit magazine storage sizes to roughly
107 * [4] allocating ca. 8 chunks per block/page keeps a good balance between
108 * external and internal fragmentation (<= 12.5%). [Bonwick94]
111 /* --- macros and constants --- */
112 #define LARGEALIGNMENT (256)
113 #define P2ALIGNMENT (2 * sizeof (gsize)) /* fits 2 pointers (assumed to be 2 * GLIB_SIZEOF_SIZE_T below) */
114 #define ALIGN(size, base) ((base) * (gsize) (((size) + (base) - 1) / (base)))
115 #define NATIVE_MALLOC_PADDING P2ALIGNMENT /* per-page padding left for native malloc(3) see [1] */
116 #define SLAB_INFO_SIZE P2ALIGN (sizeof (SlabInfo) + NATIVE_MALLOC_PADDING)
117 #define MAX_MAGAZINE_SIZE (256) /* see [3] and allocator_get_magazine_threshold() for this */
118 #define MIN_MAGAZINE_SIZE (4)
119 #define MAX_STAMP_COUNTER (7) /* distributes the load of gettimeofday() */
120 #define MAX_SLAB_CHUNK_SIZE(al) (((al)->max_page_size - SLAB_INFO_SIZE) / 8) /* we want at last 8 chunks per page, see [4] */
121 #define MAX_SLAB_INDEX(al) (SLAB_INDEX (al, MAX_SLAB_CHUNK_SIZE (al)) + 1)
122 #define SLAB_INDEX(al, asize) ((asize) / P2ALIGNMENT - 1) /* asize must be P2ALIGNMENT aligned */
123 #define SLAB_CHUNK_SIZE(al, ix) (((ix) + 1) * P2ALIGNMENT)
124 #define SLAB_BPAGE_SIZE(al,csz) (8 * (csz) + SLAB_INFO_SIZE)
126 /* optimized version of ALIGN (size, P2ALIGNMENT) */
127 #if GLIB_SIZEOF_SIZE_T * 2 == 8 /* P2ALIGNMENT */
128 #define P2ALIGN(size) (((size) + 0x7) & ~(gsize) 0x7)
129 #elif GLIB_SIZEOF_SIZE_T * 2 == 16 /* P2ALIGNMENT */
130 #define P2ALIGN(size) (((size) + 0xf) & ~(gsize) 0xf)
132 #define P2ALIGN(size) ALIGN (size, P2ALIGNMENT)
135 /* special helpers to avoid gmessage.c dependency */
136 static void mem_error (const char *format, ...) G_GNUC_PRINTF (1,2);
137 #define mem_assert(cond) do { if (G_LIKELY (cond)) ; else mem_error ("assertion failed: %s", #cond); } while (0)
139 /* --- structures --- */
140 typedef struct _ChunkLink ChunkLink;
141 typedef struct _SlabInfo SlabInfo;
142 typedef struct _CachedMagazine CachedMagazine;
150 SlabInfo *next, *prev;
154 gsize count; /* approximative chunks list length */
157 Magazine *magazine1; /* array of MAX_SLAB_INDEX (allocator) */
158 Magazine *magazine2; /* array of MAX_SLAB_INDEX (allocator) */
161 gboolean always_malloc;
162 gboolean bypass_magazines;
163 gboolean debug_blocks;
164 gsize working_set_msecs;
165 guint color_increment;
168 /* const after initialization */
169 gsize min_page_size, max_page_size;
171 gsize max_slab_chunk_size_for_magazine_cache;
173 GMutex magazine_mutex;
174 ChunkLink **magazines; /* array of MAX_SLAB_INDEX (allocator) */
175 guint *contention_counters; /* array of MAX_SLAB_INDEX (allocator) */
181 SlabInfo **slab_stack; /* array of MAX_SLAB_INDEX (allocator) */
185 /* --- g-slice prototypes --- */
186 static gpointer slab_allocator_alloc_chunk (gsize chunk_size);
187 static void slab_allocator_free_chunk (gsize chunk_size,
189 static void private_thread_memory_cleanup (gpointer data);
190 static gpointer allocator_memalign (gsize alignment,
192 static void allocator_memfree (gsize memsize,
194 static inline void magazine_cache_update_stamp (void);
195 static inline gsize allocator_get_magazine_threshold (Allocator *allocator,
198 /* --- g-slice memory checker --- */
199 static void smc_notify_alloc (void *pointer,
201 static int smc_notify_free (void *pointer,
204 /* --- variables --- */
205 static GPrivate *private_thread_memory = NULL;
206 static gsize sys_page_size = 0;
207 static Allocator allocator[1] = { { 0, }, };
208 static SliceConfig slice_config = {
209 FALSE, /* always_malloc */
210 FALSE, /* bypass_magazines */
211 FALSE, /* debug_blocks */
212 15 * 1000, /* working_set_msecs */
213 1, /* color increment, alt: 0x7fffffff */
215 static GMutex smc_tree_mutex = G_MUTEX_INIT; /* mutex for G_SLICE=debug-blocks */
217 /* --- auxiliary funcitons --- */
219 g_slice_set_config (GSliceConfig ckey,
222 g_return_if_fail (sys_page_size == 0);
225 case G_SLICE_CONFIG_ALWAYS_MALLOC:
226 slice_config.always_malloc = value != 0;
228 case G_SLICE_CONFIG_BYPASS_MAGAZINES:
229 slice_config.bypass_magazines = value != 0;
231 case G_SLICE_CONFIG_WORKING_SET_MSECS:
232 slice_config.working_set_msecs = value;
234 case G_SLICE_CONFIG_COLOR_INCREMENT:
235 slice_config.color_increment = value;
241 g_slice_get_config (GSliceConfig ckey)
245 case G_SLICE_CONFIG_ALWAYS_MALLOC:
246 return slice_config.always_malloc;
247 case G_SLICE_CONFIG_BYPASS_MAGAZINES:
248 return slice_config.bypass_magazines;
249 case G_SLICE_CONFIG_WORKING_SET_MSECS:
250 return slice_config.working_set_msecs;
251 case G_SLICE_CONFIG_CHUNK_SIZES:
252 return MAX_SLAB_INDEX (allocator);
253 case G_SLICE_CONFIG_COLOR_INCREMENT:
254 return slice_config.color_increment;
261 g_slice_get_config_state (GSliceConfig ckey,
266 g_return_val_if_fail (n_values != NULL, NULL);
271 case G_SLICE_CONFIG_CONTENTION_COUNTER:
272 array[i++] = SLAB_CHUNK_SIZE (allocator, address);
273 array[i++] = allocator->contention_counters[address];
274 array[i++] = allocator_get_magazine_threshold (allocator, address);
276 return g_memdup (array, sizeof (array[0]) * *n_values);
283 slice_config_init (SliceConfig *config)
285 /* don't use g_malloc/g_message here */
287 const gchar *val = _g_getenv_nomalloc ("G_SLICE", buffer);
288 const GDebugKey keys[] = {
289 { "always-malloc", 1 << 0 },
290 { "debug-blocks", 1 << 1 },
292 gint flags = !val ? 0 : g_parse_debug_string (val, keys, G_N_ELEMENTS (keys));
293 *config = slice_config;
294 if (flags & (1 << 0)) /* always-malloc */
295 config->always_malloc = TRUE;
296 if (flags & (1 << 1)) /* debug-blocks */
297 config->debug_blocks = TRUE;
301 g_slice_init_nomessage (void)
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
366 allocator_categorize (gsize aligned_chunk_size)
368 /* speed up the likely path */
369 if (G_LIKELY (aligned_chunk_size && aligned_chunk_size <= allocator->max_slab_chunk_size_for_magazine_cache))
370 return 1; /* use magazine cache */
372 /* the above will fail (max_slab_chunk_size_for_magazine_cache == 0) if the
373 * allocator is still uninitialized, or if we are not configured to use the
377 g_slice_init_nomessage ();
378 if (!allocator->config.always_malloc &&
379 aligned_chunk_size &&
380 aligned_chunk_size <= MAX_SLAB_CHUNK_SIZE (allocator))
382 if (allocator->config.bypass_magazines)
383 return 2; /* use slab allocator, see [2] */
384 return 1; /* use magazine cache */
386 return 0; /* use malloc() */
390 _g_slice_thread_init_nomessage (void)
392 /* we may not use g_error() or friends here */
394 g_slice_init_nomessage();
397 /* g_slice_init_nomessage() has been called already, probably due
398 * to a g_slice_alloc1() before g_thread_init().
401 private_thread_memory = g_private_new (private_thread_memory_cleanup);
405 g_mutex_lock_a (GMutex *mutex,
406 guint *contention_counter)
408 gboolean contention = FALSE;
409 if (!g_mutex_trylock (mutex))
411 g_mutex_lock (mutex);
416 allocator->mutex_counter++;
417 if (allocator->mutex_counter >= 1) /* quickly adapt to contention */
419 allocator->mutex_counter = 0;
420 *contention_counter = MIN (*contention_counter + 1, MAX_MAGAZINE_SIZE);
423 else /* !contention */
425 allocator->mutex_counter--;
426 if (allocator->mutex_counter < -11) /* moderately recover magazine sizes */
428 allocator->mutex_counter = 0;
429 *contention_counter = MAX (*contention_counter, 1) - 1;
434 static inline ThreadMemory*
435 thread_memory_from_self (void)
437 ThreadMemory *tmem = g_private_get (private_thread_memory);
438 if (G_UNLIKELY (!tmem))
440 static ThreadMemory *single_thread_memory = NULL; /* remember single-thread info for multi-threaded case */
441 if (single_thread_memory && g_thread_supported ())
443 g_mutex_lock (&allocator->slab_mutex);
444 if (single_thread_memory)
446 /* GSlice has been used before g_thread_init(), and now
447 * we are running threaded. to cope with it, use the saved
448 * thread memory structure from when we weren't threaded.
450 tmem = single_thread_memory;
451 single_thread_memory = NULL; /* slab_mutex protected when multi-threaded */
453 g_mutex_unlock (&allocator->slab_mutex);
457 const guint n_magazines = MAX_SLAB_INDEX (allocator);
458 tmem = g_malloc0 (sizeof (ThreadMemory) + sizeof (Magazine) * 2 * n_magazines);
459 tmem->magazine1 = (Magazine*) (tmem + 1);
460 tmem->magazine2 = &tmem->magazine1[n_magazines];
462 /* g_private_get/g_private_set works in the single-threaded xor the multi-
463 * threaded case. but not *across* g_thread_init(), after multi-thread
464 * initialization it returns NULL for previously set single-thread data.
466 g_private_set (private_thread_memory, tmem);
467 /* save single-thread thread memory structure, in case we need to
468 * pick it up again after multi-thread initialization happened.
470 if (!single_thread_memory && !g_thread_supported ())
471 single_thread_memory = tmem; /* no slab_mutex created yet */
476 static inline ChunkLink*
477 magazine_chain_pop_head (ChunkLink **magazine_chunks)
479 /* magazine chains are linked via ChunkLink->next.
480 * each ChunkLink->data of the toplevel chain may point to a subchain,
481 * linked via ChunkLink->next. ChunkLink->data of the subchains just
482 * contains uninitialized junk.
484 ChunkLink *chunk = (*magazine_chunks)->data;
485 if (G_UNLIKELY (chunk))
487 /* allocating from freed list */
488 (*magazine_chunks)->data = chunk->next;
492 chunk = *magazine_chunks;
493 *magazine_chunks = chunk->next;
498 #if 0 /* useful for debugging */
500 magazine_count (ChunkLink *head)
507 ChunkLink *child = head->data;
509 for (child = head->data; child; child = child->next)
518 allocator_get_magazine_threshold (Allocator *allocator,
521 /* the magazine size calculated here has a lower bound of MIN_MAGAZINE_SIZE,
522 * which is required by the implementation. also, for moderately sized chunks
523 * (say >= 64 bytes), magazine sizes shouldn't be much smaller then the number
524 * of chunks available per page/2 to avoid excessive traffic in the magazine
525 * cache for small to medium sized structures.
526 * the upper bound of the magazine size is effectively provided by
527 * MAX_MAGAZINE_SIZE. for larger chunks, this number is scaled down so that
528 * the content of a single magazine doesn't exceed ca. 16KB.
530 gsize chunk_size = SLAB_CHUNK_SIZE (allocator, ix);
531 guint threshold = MAX (MIN_MAGAZINE_SIZE, allocator->max_page_size / MAX (5 * chunk_size, 5 * 32));
532 guint contention_counter = allocator->contention_counters[ix];
533 if (G_UNLIKELY (contention_counter)) /* single CPU bias */
535 /* adapt contention counter thresholds to chunk sizes */
536 contention_counter = contention_counter * 64 / chunk_size;
537 threshold = MAX (threshold, contention_counter);
542 /* --- magazine cache --- */
544 magazine_cache_update_stamp (void)
546 if (allocator->stamp_counter >= MAX_STAMP_COUNTER)
549 g_get_current_time (&tv);
550 allocator->last_stamp = tv.tv_sec * 1000 + tv.tv_usec / 1000; /* milli seconds */
551 allocator->stamp_counter = 0;
554 allocator->stamp_counter++;
557 static inline ChunkLink*
558 magazine_chain_prepare_fields (ChunkLink *magazine_chunks)
564 /* checked upon initialization: mem_assert (MIN_MAGAZINE_SIZE >= 4); */
565 /* ensure a magazine with at least 4 unused data pointers */
566 chunk1 = magazine_chain_pop_head (&magazine_chunks);
567 chunk2 = magazine_chain_pop_head (&magazine_chunks);
568 chunk3 = magazine_chain_pop_head (&magazine_chunks);
569 chunk4 = magazine_chain_pop_head (&magazine_chunks);
570 chunk4->next = magazine_chunks;
571 chunk3->next = chunk4;
572 chunk2->next = chunk3;
573 chunk1->next = chunk2;
577 /* access the first 3 fields of a specially prepared magazine chain */
578 #define magazine_chain_prev(mc) ((mc)->data)
579 #define magazine_chain_stamp(mc) ((mc)->next->data)
580 #define magazine_chain_uint_stamp(mc) GPOINTER_TO_UINT ((mc)->next->data)
581 #define magazine_chain_next(mc) ((mc)->next->next->data)
582 #define magazine_chain_count(mc) ((mc)->next->next->next->data)
585 magazine_cache_trim (Allocator *allocator,
589 /* g_mutex_lock (allocator->mutex); done by caller */
590 /* trim magazine cache from tail */
591 ChunkLink *current = magazine_chain_prev (allocator->magazines[ix]);
592 ChunkLink *trash = NULL;
593 while (ABS (stamp - magazine_chain_uint_stamp (current)) >= allocator->config.working_set_msecs)
596 ChunkLink *prev = magazine_chain_prev (current);
597 ChunkLink *next = magazine_chain_next (current);
598 magazine_chain_next (prev) = next;
599 magazine_chain_prev (next) = prev;
600 /* clear special fields, put on trash stack */
601 magazine_chain_next (current) = NULL;
602 magazine_chain_count (current) = NULL;
603 magazine_chain_stamp (current) = NULL;
604 magazine_chain_prev (current) = trash;
606 /* fixup list head if required */
607 if (current == allocator->magazines[ix])
609 allocator->magazines[ix] = NULL;
614 g_mutex_unlock (&allocator->magazine_mutex);
618 const gsize chunk_size = SLAB_CHUNK_SIZE (allocator, ix);
619 g_mutex_lock (&allocator->slab_mutex);
623 trash = magazine_chain_prev (current);
624 magazine_chain_prev (current) = NULL; /* clear special field */
627 ChunkLink *chunk = magazine_chain_pop_head (¤t);
628 slab_allocator_free_chunk (chunk_size, chunk);
631 g_mutex_unlock (&allocator->slab_mutex);
636 magazine_cache_push_magazine (guint ix,
637 ChunkLink *magazine_chunks,
638 gsize count) /* must be >= MIN_MAGAZINE_SIZE */
640 ChunkLink *current = magazine_chain_prepare_fields (magazine_chunks);
641 ChunkLink *next, *prev;
642 g_mutex_lock (&allocator->magazine_mutex);
643 /* add magazine at head */
644 next = allocator->magazines[ix];
646 prev = magazine_chain_prev (next);
648 next = prev = current;
649 magazine_chain_next (prev) = current;
650 magazine_chain_prev (next) = current;
651 magazine_chain_prev (current) = prev;
652 magazine_chain_next (current) = next;
653 magazine_chain_count (current) = (gpointer) count;
655 magazine_cache_update_stamp();
656 magazine_chain_stamp (current) = GUINT_TO_POINTER (allocator->last_stamp);
657 allocator->magazines[ix] = current;
658 /* free old magazines beyond a certain threshold */
659 magazine_cache_trim (allocator, ix, allocator->last_stamp);
660 /* g_mutex_unlock (allocator->mutex); was done by magazine_cache_trim() */
664 magazine_cache_pop_magazine (guint ix,
667 g_mutex_lock_a (&allocator->magazine_mutex, &allocator->contention_counters[ix]);
668 if (!allocator->magazines[ix])
670 guint magazine_threshold = allocator_get_magazine_threshold (allocator, ix);
671 gsize i, chunk_size = SLAB_CHUNK_SIZE (allocator, ix);
672 ChunkLink *chunk, *head;
673 g_mutex_unlock (&allocator->magazine_mutex);
674 g_mutex_lock (&allocator->slab_mutex);
675 head = slab_allocator_alloc_chunk (chunk_size);
678 for (i = 1; i < magazine_threshold; i++)
680 chunk->next = slab_allocator_alloc_chunk (chunk_size);
685 g_mutex_unlock (&allocator->slab_mutex);
691 ChunkLink *current = allocator->magazines[ix];
692 ChunkLink *prev = magazine_chain_prev (current);
693 ChunkLink *next = magazine_chain_next (current);
695 magazine_chain_next (prev) = next;
696 magazine_chain_prev (next) = prev;
697 allocator->magazines[ix] = next == current ? NULL : next;
698 g_mutex_unlock (&allocator->magazine_mutex);
699 /* clear special fields and hand out */
700 *countp = (gsize) magazine_chain_count (current);
701 magazine_chain_prev (current) = NULL;
702 magazine_chain_next (current) = NULL;
703 magazine_chain_count (current) = NULL;
704 magazine_chain_stamp (current) = NULL;
709 /* --- thread magazines --- */
711 private_thread_memory_cleanup (gpointer data)
713 ThreadMemory *tmem = data;
714 const guint n_magazines = MAX_SLAB_INDEX (allocator);
716 for (ix = 0; ix < n_magazines; ix++)
720 mags[0] = &tmem->magazine1[ix];
721 mags[1] = &tmem->magazine2[ix];
722 for (j = 0; j < 2; j++)
724 Magazine *mag = mags[j];
725 if (mag->count >= MIN_MAGAZINE_SIZE)
726 magazine_cache_push_magazine (ix, mag->chunks, mag->count);
729 const gsize chunk_size = SLAB_CHUNK_SIZE (allocator, ix);
730 g_mutex_lock (&allocator->slab_mutex);
733 ChunkLink *chunk = magazine_chain_pop_head (&mag->chunks);
734 slab_allocator_free_chunk (chunk_size, chunk);
736 g_mutex_unlock (&allocator->slab_mutex);
744 thread_memory_magazine1_reload (ThreadMemory *tmem,
747 Magazine *mag = &tmem->magazine1[ix];
748 mem_assert (mag->chunks == NULL); /* ensure that we may reset mag->count */
750 mag->chunks = magazine_cache_pop_magazine (ix, &mag->count);
754 thread_memory_magazine2_unload (ThreadMemory *tmem,
757 Magazine *mag = &tmem->magazine2[ix];
758 magazine_cache_push_magazine (ix, mag->chunks, mag->count);
764 thread_memory_swap_magazines (ThreadMemory *tmem,
767 Magazine xmag = tmem->magazine1[ix];
768 tmem->magazine1[ix] = tmem->magazine2[ix];
769 tmem->magazine2[ix] = xmag;
772 static inline gboolean
773 thread_memory_magazine1_is_empty (ThreadMemory *tmem,
776 return tmem->magazine1[ix].chunks == NULL;
779 static inline gboolean
780 thread_memory_magazine2_is_full (ThreadMemory *tmem,
783 return tmem->magazine2[ix].count >= allocator_get_magazine_threshold (allocator, ix);
786 static inline gpointer
787 thread_memory_magazine1_alloc (ThreadMemory *tmem,
790 Magazine *mag = &tmem->magazine1[ix];
791 ChunkLink *chunk = magazine_chain_pop_head (&mag->chunks);
792 if (G_LIKELY (mag->count > 0))
798 thread_memory_magazine2_free (ThreadMemory *tmem,
802 Magazine *mag = &tmem->magazine2[ix];
803 ChunkLink *chunk = mem;
805 chunk->next = mag->chunks;
810 /* --- API functions --- */
812 g_slice_alloc (gsize mem_size)
817 chunk_size = P2ALIGN (mem_size);
818 acat = allocator_categorize (chunk_size);
819 if (G_LIKELY (acat == 1)) /* allocate through magazine layer */
821 ThreadMemory *tmem = thread_memory_from_self();
822 guint ix = SLAB_INDEX (allocator, chunk_size);
823 if (G_UNLIKELY (thread_memory_magazine1_is_empty (tmem, ix)))
825 thread_memory_swap_magazines (tmem, ix);
826 if (G_UNLIKELY (thread_memory_magazine1_is_empty (tmem, ix)))
827 thread_memory_magazine1_reload (tmem, ix);
829 mem = thread_memory_magazine1_alloc (tmem, ix);
831 else if (acat == 2) /* allocate through slab allocator */
833 g_mutex_lock (&allocator->slab_mutex);
834 mem = slab_allocator_alloc_chunk (chunk_size);
835 g_mutex_unlock (&allocator->slab_mutex);
837 else /* delegate to system malloc */
838 mem = g_malloc (mem_size);
839 if (G_UNLIKELY (allocator->config.debug_blocks))
840 smc_notify_alloc (mem, mem_size);
842 TRACE (GLIB_SLICE_ALLOC((void*)mem, mem_size));
848 g_slice_alloc0 (gsize mem_size)
850 gpointer mem = g_slice_alloc (mem_size);
852 memset (mem, 0, mem_size);
857 g_slice_copy (gsize mem_size,
858 gconstpointer mem_block)
860 gpointer mem = g_slice_alloc (mem_size);
862 memcpy (mem, mem_block, mem_size);
867 g_slice_free1 (gsize mem_size,
870 gsize chunk_size = P2ALIGN (mem_size);
871 guint acat = allocator_categorize (chunk_size);
872 if (G_UNLIKELY (!mem_block))
874 if (G_UNLIKELY (allocator->config.debug_blocks) &&
875 !smc_notify_free (mem_block, mem_size))
877 if (G_LIKELY (acat == 1)) /* allocate through magazine layer */
879 ThreadMemory *tmem = thread_memory_from_self();
880 guint ix = SLAB_INDEX (allocator, chunk_size);
881 if (G_UNLIKELY (thread_memory_magazine2_is_full (tmem, ix)))
883 thread_memory_swap_magazines (tmem, ix);
884 if (G_UNLIKELY (thread_memory_magazine2_is_full (tmem, ix)))
885 thread_memory_magazine2_unload (tmem, ix);
887 if (G_UNLIKELY (g_mem_gc_friendly))
888 memset (mem_block, 0, chunk_size);
889 thread_memory_magazine2_free (tmem, ix, mem_block);
891 else if (acat == 2) /* allocate through slab allocator */
893 if (G_UNLIKELY (g_mem_gc_friendly))
894 memset (mem_block, 0, chunk_size);
895 g_mutex_lock (&allocator->slab_mutex);
896 slab_allocator_free_chunk (chunk_size, mem_block);
897 g_mutex_unlock (&allocator->slab_mutex);
899 else /* delegate to system malloc */
901 if (G_UNLIKELY (g_mem_gc_friendly))
902 memset (mem_block, 0, mem_size);
905 TRACE (GLIB_SLICE_FREE((void*)mem_block, mem_size));
909 g_slice_free_chain_with_offset (gsize mem_size,
913 gpointer slice = mem_chain;
914 /* while the thread magazines and the magazine cache are implemented so that
915 * they can easily be extended to allow for free lists containing more free
916 * lists for the first level nodes, which would allow O(1) freeing in this
917 * function, the benefit of such an extension is questionable, because:
918 * - the magazine size counts will become mere lower bounds which confuses
919 * the code adapting to lock contention;
920 * - freeing a single node to the thread magazines is very fast, so this
921 * O(list_length) operation is multiplied by a fairly small factor;
922 * - memory usage histograms on larger applications seem to indicate that
923 * the amount of released multi node lists is negligible in comparison
924 * to single node releases.
925 * - the major performance bottle neck, namely g_private_get() or
926 * g_mutex_lock()/g_mutex_unlock() has already been moved out of the
927 * inner loop for freeing chained slices.
929 gsize chunk_size = P2ALIGN (mem_size);
930 guint acat = allocator_categorize (chunk_size);
931 if (G_LIKELY (acat == 1)) /* allocate through magazine layer */
933 ThreadMemory *tmem = thread_memory_from_self();
934 guint ix = SLAB_INDEX (allocator, chunk_size);
937 guint8 *current = slice;
938 slice = *(gpointer*) (current + next_offset);
939 if (G_UNLIKELY (allocator->config.debug_blocks) &&
940 !smc_notify_free (current, mem_size))
942 if (G_UNLIKELY (thread_memory_magazine2_is_full (tmem, ix)))
944 thread_memory_swap_magazines (tmem, ix);
945 if (G_UNLIKELY (thread_memory_magazine2_is_full (tmem, ix)))
946 thread_memory_magazine2_unload (tmem, ix);
948 if (G_UNLIKELY (g_mem_gc_friendly))
949 memset (current, 0, chunk_size);
950 thread_memory_magazine2_free (tmem, ix, current);
953 else if (acat == 2) /* allocate through slab allocator */
955 g_mutex_lock (&allocator->slab_mutex);
958 guint8 *current = slice;
959 slice = *(gpointer*) (current + next_offset);
960 if (G_UNLIKELY (allocator->config.debug_blocks) &&
961 !smc_notify_free (current, mem_size))
963 if (G_UNLIKELY (g_mem_gc_friendly))
964 memset (current, 0, chunk_size);
965 slab_allocator_free_chunk (chunk_size, current);
967 g_mutex_unlock (&allocator->slab_mutex);
969 else /* delegate to system malloc */
972 guint8 *current = slice;
973 slice = *(gpointer*) (current + next_offset);
974 if (G_UNLIKELY (allocator->config.debug_blocks) &&
975 !smc_notify_free (current, mem_size))
977 if (G_UNLIKELY (g_mem_gc_friendly))
978 memset (current, 0, mem_size);
983 /* --- single page allocator --- */
985 allocator_slab_stack_push (Allocator *allocator,
989 /* insert slab at slab ring head */
990 if (!allocator->slab_stack[ix])
997 SlabInfo *next = allocator->slab_stack[ix], *prev = next->prev;
1003 allocator->slab_stack[ix] = sinfo;
1007 allocator_aligned_page_size (Allocator *allocator,
1010 gsize val = 1 << g_bit_storage (n_bytes - 1);
1011 val = MAX (val, allocator->min_page_size);
1016 allocator_add_slab (Allocator *allocator,
1022 gsize addr, padding, n_chunks, color = 0;
1023 gsize page_size = allocator_aligned_page_size (allocator, SLAB_BPAGE_SIZE (allocator, chunk_size));
1024 /* allocate 1 page for the chunks and the slab */
1025 gpointer aligned_memory = allocator_memalign (page_size, page_size - NATIVE_MALLOC_PADDING);
1026 guint8 *mem = aligned_memory;
1030 const gchar *syserr = "unknown error";
1032 syserr = strerror (errno);
1034 mem_error ("failed to allocate %u bytes (alignment: %u): %s\n",
1035 (guint) (page_size - NATIVE_MALLOC_PADDING), (guint) page_size, syserr);
1037 /* mask page address */
1038 addr = ((gsize) mem / page_size) * page_size;
1039 /* assert alignment */
1040 mem_assert (aligned_memory == (gpointer) addr);
1041 /* basic slab info setup */
1042 sinfo = (SlabInfo*) (mem + page_size - SLAB_INFO_SIZE);
1043 sinfo->n_allocated = 0;
1044 sinfo->chunks = NULL;
1045 /* figure cache colorization */
1046 n_chunks = ((guint8*) sinfo - mem) / chunk_size;
1047 padding = ((guint8*) sinfo - mem) - n_chunks * chunk_size;
1050 color = (allocator->color_accu * P2ALIGNMENT) % padding;
1051 allocator->color_accu += allocator->config.color_increment;
1053 /* add chunks to free list */
1054 chunk = (ChunkLink*) (mem + color);
1055 sinfo->chunks = chunk;
1056 for (i = 0; i < n_chunks - 1; i++)
1058 chunk->next = (ChunkLink*) ((guint8*) chunk + chunk_size);
1059 chunk = chunk->next;
1061 chunk->next = NULL; /* last chunk */
1062 /* add slab to slab ring */
1063 allocator_slab_stack_push (allocator, ix, sinfo);
1067 slab_allocator_alloc_chunk (gsize chunk_size)
1070 guint ix = SLAB_INDEX (allocator, chunk_size);
1071 /* ensure non-empty slab */
1072 if (!allocator->slab_stack[ix] || !allocator->slab_stack[ix]->chunks)
1073 allocator_add_slab (allocator, ix, chunk_size);
1074 /* allocate chunk */
1075 chunk = allocator->slab_stack[ix]->chunks;
1076 allocator->slab_stack[ix]->chunks = chunk->next;
1077 allocator->slab_stack[ix]->n_allocated++;
1078 /* rotate empty slabs */
1079 if (!allocator->slab_stack[ix]->chunks)
1080 allocator->slab_stack[ix] = allocator->slab_stack[ix]->next;
1085 slab_allocator_free_chunk (gsize chunk_size,
1090 guint ix = SLAB_INDEX (allocator, chunk_size);
1091 gsize page_size = allocator_aligned_page_size (allocator, SLAB_BPAGE_SIZE (allocator, chunk_size));
1092 gsize addr = ((gsize) mem / page_size) * page_size;
1093 /* mask page address */
1094 guint8 *page = (guint8*) addr;
1095 SlabInfo *sinfo = (SlabInfo*) (page + page_size - SLAB_INFO_SIZE);
1096 /* assert valid chunk count */
1097 mem_assert (sinfo->n_allocated > 0);
1098 /* add chunk to free list */
1099 was_empty = sinfo->chunks == NULL;
1100 chunk = (ChunkLink*) mem;
1101 chunk->next = sinfo->chunks;
1102 sinfo->chunks = chunk;
1103 sinfo->n_allocated--;
1104 /* keep slab ring partially sorted, empty slabs at end */
1108 SlabInfo *next = sinfo->next, *prev = sinfo->prev;
1111 if (allocator->slab_stack[ix] == sinfo)
1112 allocator->slab_stack[ix] = next == sinfo ? NULL : next;
1113 /* insert slab at head */
1114 allocator_slab_stack_push (allocator, ix, sinfo);
1116 /* eagerly free complete unused slabs */
1117 if (!sinfo->n_allocated)
1120 SlabInfo *next = sinfo->next, *prev = sinfo->prev;
1123 if (allocator->slab_stack[ix] == sinfo)
1124 allocator->slab_stack[ix] = next == sinfo ? NULL : next;
1126 allocator_memfree (page_size, page);
1130 /* --- memalign implementation --- */
1131 #ifdef HAVE_MALLOC_H
1132 #include <malloc.h> /* memalign() */
1136 * define HAVE_POSIX_MEMALIGN 1 // if free(posix_memalign(3)) works, <stdlib.h>
1137 * define HAVE_COMPLIANT_POSIX_MEMALIGN 1 // if free(posix_memalign(3)) works for sizes != 2^n, <stdlib.h>
1138 * define HAVE_MEMALIGN 1 // if free(memalign(3)) works, <malloc.h>
1139 * define HAVE_VALLOC 1 // if free(valloc(3)) works, <stdlib.h> or <malloc.h>
1140 * if none is provided, we implement malloc(3)-based alloc-only page alignment
1143 #if !(HAVE_COMPLIANT_POSIX_MEMALIGN || HAVE_MEMALIGN || HAVE_VALLOC)
1144 static GTrashStack *compat_valloc_trash = NULL;
1148 allocator_memalign (gsize alignment,
1151 gpointer aligned_memory = NULL;
1153 #if HAVE_COMPLIANT_POSIX_MEMALIGN
1154 err = posix_memalign (&aligned_memory, alignment, memsize);
1157 aligned_memory = memalign (alignment, memsize);
1161 aligned_memory = valloc (memsize);
1164 /* simplistic non-freeing page allocator */
1165 mem_assert (alignment == sys_page_size);
1166 mem_assert (memsize <= sys_page_size);
1167 if (!compat_valloc_trash)
1169 const guint n_pages = 16;
1170 guint8 *mem = malloc (n_pages * sys_page_size);
1175 guint8 *amem = (guint8*) ALIGN ((gsize) mem, sys_page_size);
1177 i--; /* mem wasn't page aligned */
1179 g_trash_stack_push (&compat_valloc_trash, amem + i * sys_page_size);
1182 aligned_memory = g_trash_stack_pop (&compat_valloc_trash);
1184 if (!aligned_memory)
1186 return aligned_memory;
1190 allocator_memfree (gsize memsize,
1193 #if HAVE_COMPLIANT_POSIX_MEMALIGN || HAVE_MEMALIGN || HAVE_VALLOC
1196 mem_assert (memsize <= sys_page_size);
1197 g_trash_stack_push (&compat_valloc_trash, mem);
1202 mem_error (const char *format,
1207 /* at least, put out "MEMORY-ERROR", in case we segfault during the rest of the function */
1208 fputs ("\n***MEMORY-ERROR***: ", stderr);
1209 pname = g_get_prgname();
1210 fprintf (stderr, "%s[%ld]: GSlice: ", pname ? pname : "", (long)getpid());
1211 va_start (args, format);
1212 vfprintf (stderr, format, args);
1214 fputs ("\n", stderr);
1219 /* --- g-slice memory checker tree --- */
1220 typedef size_t SmcKType; /* key type */
1221 typedef size_t SmcVType; /* value type */
1226 static void smc_tree_insert (SmcKType key,
1228 static gboolean smc_tree_lookup (SmcKType key,
1230 static gboolean smc_tree_remove (SmcKType key);
1233 /* --- g-slice memory checker implementation --- */
1235 smc_notify_alloc (void *pointer,
1238 size_t adress = (size_t) pointer;
1240 smc_tree_insert (adress, size);
1245 smc_notify_ignore (void *pointer)
1247 size_t adress = (size_t) pointer;
1249 smc_tree_remove (adress);
1254 smc_notify_free (void *pointer,
1257 size_t adress = (size_t) pointer;
1262 return 1; /* ignore */
1263 found_one = smc_tree_lookup (adress, &real_size);
1266 fprintf (stderr, "GSlice: MemChecker: attempt to release non-allocated block: %p size=%" G_GSIZE_FORMAT "\n", pointer, size);
1269 if (real_size != size && (real_size || size))
1271 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);
1274 if (!smc_tree_remove (adress))
1276 fprintf (stderr, "GSlice: MemChecker: attempt to release non-allocated block: %p size=%" G_GSIZE_FORMAT "\n", pointer, size);
1279 return 1; /* all fine */
1282 /* --- g-slice memory checker tree implementation --- */
1283 #define SMC_TRUNK_COUNT (4093 /* 16381 */) /* prime, to distribute trunk collisions (big, allocated just once) */
1284 #define SMC_BRANCH_COUNT (511) /* prime, to distribute branch collisions */
1285 #define SMC_TRUNK_EXTENT (SMC_BRANCH_COUNT * 2039) /* key adress space per trunk, should distribute uniformly across BRANCH_COUNT */
1286 #define SMC_TRUNK_HASH(k) ((k / SMC_TRUNK_EXTENT) % SMC_TRUNK_COUNT) /* generate new trunk hash per megabyte (roughly) */
1287 #define SMC_BRANCH_HASH(k) (k % SMC_BRANCH_COUNT)
1291 unsigned int n_entries;
1294 static SmcBranch **smc_tree_root = NULL;
1297 smc_tree_abort (int errval)
1299 const char *syserr = "unknown error";
1301 syserr = strerror (errval);
1303 mem_error ("MemChecker: failure in debugging tree: %s", syserr);
1306 static inline SmcEntry*
1307 smc_tree_branch_grow_L (SmcBranch *branch,
1310 unsigned int old_size = branch->n_entries * sizeof (branch->entries[0]);
1311 unsigned int new_size = old_size + sizeof (branch->entries[0]);
1313 mem_assert (index <= branch->n_entries);
1314 branch->entries = (SmcEntry*) realloc (branch->entries, new_size);
1315 if (!branch->entries)
1316 smc_tree_abort (errno);
1317 entry = branch->entries + index;
1318 g_memmove (entry + 1, entry, (branch->n_entries - index) * sizeof (entry[0]));
1319 branch->n_entries += 1;
1323 static inline SmcEntry*
1324 smc_tree_branch_lookup_nearest_L (SmcBranch *branch,
1327 unsigned int n_nodes = branch->n_entries, offs = 0;
1328 SmcEntry *check = branch->entries;
1330 while (offs < n_nodes)
1332 unsigned int i = (offs + n_nodes) >> 1;
1333 check = branch->entries + i;
1334 cmp = key < check->key ? -1 : key != check->key;
1336 return check; /* return exact match */
1339 else /* (cmp > 0) */
1342 /* check points at last mismatch, cmp > 0 indicates greater key */
1343 return cmp > 0 ? check + 1 : check; /* return insertion position for inexact match */
1347 smc_tree_insert (SmcKType key,
1350 unsigned int ix0, ix1;
1353 g_mutex_lock (&smc_tree_mutex);
1354 ix0 = SMC_TRUNK_HASH (key);
1355 ix1 = SMC_BRANCH_HASH (key);
1358 smc_tree_root = calloc (SMC_TRUNK_COUNT, sizeof (smc_tree_root[0]));
1360 smc_tree_abort (errno);
1362 if (!smc_tree_root[ix0])
1364 smc_tree_root[ix0] = calloc (SMC_BRANCH_COUNT, sizeof (smc_tree_root[0][0]));
1365 if (!smc_tree_root[ix0])
1366 smc_tree_abort (errno);
1368 entry = smc_tree_branch_lookup_nearest_L (&smc_tree_root[ix0][ix1], key);
1369 if (!entry || /* need create */
1370 entry >= smc_tree_root[ix0][ix1].entries + smc_tree_root[ix0][ix1].n_entries || /* need append */
1371 entry->key != key) /* need insert */
1372 entry = smc_tree_branch_grow_L (&smc_tree_root[ix0][ix1], entry - smc_tree_root[ix0][ix1].entries);
1374 entry->value = value;
1375 g_mutex_unlock (&smc_tree_mutex);
1379 smc_tree_lookup (SmcKType key,
1382 SmcEntry *entry = NULL;
1383 unsigned int ix0 = SMC_TRUNK_HASH (key), ix1 = SMC_BRANCH_HASH (key);
1384 gboolean found_one = FALSE;
1386 g_mutex_lock (&smc_tree_mutex);
1387 if (smc_tree_root && smc_tree_root[ix0])
1389 entry = smc_tree_branch_lookup_nearest_L (&smc_tree_root[ix0][ix1], key);
1391 entry < smc_tree_root[ix0][ix1].entries + smc_tree_root[ix0][ix1].n_entries &&
1395 *value_p = entry->value;
1398 g_mutex_unlock (&smc_tree_mutex);
1403 smc_tree_remove (SmcKType key)
1405 unsigned int ix0 = SMC_TRUNK_HASH (key), ix1 = SMC_BRANCH_HASH (key);
1406 gboolean found_one = FALSE;
1407 g_mutex_lock (&smc_tree_mutex);
1408 if (smc_tree_root && smc_tree_root[ix0])
1410 SmcEntry *entry = smc_tree_branch_lookup_nearest_L (&smc_tree_root[ix0][ix1], key);
1412 entry < smc_tree_root[ix0][ix1].entries + smc_tree_root[ix0][ix1].n_entries &&
1415 unsigned int i = entry - smc_tree_root[ix0][ix1].entries;
1416 smc_tree_root[ix0][ix1].n_entries -= 1;
1417 g_memmove (entry, entry + 1, (smc_tree_root[ix0][ix1].n_entries - i) * sizeof (entry[0]));
1418 if (!smc_tree_root[ix0][ix1].n_entries)
1420 /* avoid useless pressure on the memory system */
1421 free (smc_tree_root[ix0][ix1].entries);
1422 smc_tree_root[ix0][ix1].entries = NULL;
1427 g_mutex_unlock (&smc_tree_mutex);
1431 #ifdef G_ENABLE_DEBUG
1433 g_slice_debug_tree_statistics (void)
1435 g_mutex_lock (&smc_tree_mutex);
1438 unsigned int i, j, t = 0, o = 0, b = 0, su = 0, ex = 0, en = 4294967295u;
1440 for (i = 0; i < SMC_TRUNK_COUNT; i++)
1441 if (smc_tree_root[i])
1444 for (j = 0; j < SMC_BRANCH_COUNT; j++)
1445 if (smc_tree_root[i][j].n_entries)
1448 su += smc_tree_root[i][j].n_entries;
1449 en = MIN (en, smc_tree_root[i][j].n_entries);
1450 ex = MAX (ex, smc_tree_root[i][j].n_entries);
1452 else if (smc_tree_root[i][j].entries)
1453 o++; /* formerly used, now empty */
1456 tf = MAX (t, 1.0); /* max(1) to be a valid divisor */
1457 bf = MAX (b, 1.0); /* max(1) to be a valid divisor */
1458 fprintf (stderr, "GSlice: MemChecker: %u trunks, %u branches, %u old branches\n", t, b, o);
1459 fprintf (stderr, "GSlice: MemChecker: %f branches per trunk, %.2f%% utilization\n",
1461 100.0 - (SMC_BRANCH_COUNT - b / tf) / (0.01 * SMC_BRANCH_COUNT));
1462 fprintf (stderr, "GSlice: MemChecker: %f entries per branch, %u minimum, %u maximum\n",
1466 fprintf (stderr, "GSlice: MemChecker: root=NULL\n");
1467 g_mutex_unlock (&smc_tree_mutex);
1469 /* sample statistics (beast + GSLice + 24h scripted core & GUI activity):
1470 * PID %CPU %MEM VSZ RSS COMMAND
1471 * 8887 30.3 45.8 456068 414856 beast-0.7.1 empty.bse
1472 * $ cat /proc/8887/statm # total-program-size resident-set-size shared-pages text/code data/stack library dirty-pages
1473 * 114017 103714 2354 344 0 108676 0
1474 * $ cat /proc/8887/status
1485 * (gdb) print g_slice_debug_tree_statistics ()
1486 * GSlice: MemChecker: 422 trunks, 213068 branches, 0 old branches
1487 * GSlice: MemChecker: 504.900474 branches per trunk, 98.81% utilization
1488 * GSlice: MemChecker: 4.965039 entries per branch, 1 minimum, 37 maximum
1491 #endif /* G_ENABLE_DEBUG */