[2062] | 1 | /* $Id: alloccache.c 3141 2018-03-14 21:58:32Z bird $ */
|
---|
| 2 | /** @file
|
---|
| 3 | * alloccache - Fixed sized allocation cache.
|
---|
| 4 | *
|
---|
| 5 | * The rational for using an allocation cache, is that it is way faster
|
---|
| 6 | * than malloc+free on most systems. It may be more efficient as well,
|
---|
| 7 | * depending on the way the heap implementes small allocations. Also,
|
---|
| 8 | * with the incdep.c code being threaded, all heaps (except for MSC)
|
---|
| 9 | * ran into severe lock contention issues since both the main thread
|
---|
| 10 | * and the incdep worker thread was allocating a crazy amount of tiny
|
---|
| 11 | * allocations (struct dep, struct nameseq, ++).
|
---|
| 12 | *
|
---|
| 13 | * Darwin also showed a significant amount of time spent just executing
|
---|
| 14 | * free(), which is kind of silly. The alloccache helps a bit here too.
|
---|
| 15 | */
|
---|
| 16 |
|
---|
| 17 | /*
|
---|
[2413] | 18 | * Copyright (c) 2008-2010 knut st. osmundsen <bird-kBuild-spamx@anduin.net>
|
---|
[2062] | 19 | *
|
---|
| 20 | * This file is part of kBuild.
|
---|
| 21 | *
|
---|
| 22 | * kBuild is free software; you can redistribute it and/or modify
|
---|
| 23 | * it under the terms of the GNU General Public License as published by
|
---|
| 24 | * the Free Software Foundation; either version 3 of the License, or
|
---|
| 25 | * (at your option) any later version.
|
---|
| 26 | *
|
---|
| 27 | * kBuild is distributed in the hope that it will be useful,
|
---|
| 28 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 29 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 30 | * GNU General Public License for more details.
|
---|
| 31 | *
|
---|
| 32 | * You should have received a copy of the GNU General Public License
|
---|
| 33 | * along with kBuild. If not, see <http://www.gnu.org/licenses/>
|
---|
| 34 | *
|
---|
| 35 | */
|
---|
| 36 |
|
---|
| 37 | /*******************************************************************************
|
---|
| 38 | * Header Files *
|
---|
| 39 | *******************************************************************************/
|
---|
[3140] | 40 | #include "makeint.h"
|
---|
[3141] | 41 | #include "filedef.h"
|
---|
[2062] | 42 | #include "dep.h"
|
---|
| 43 | #include "debug.h"
|
---|
| 44 | #include <assert.h>
|
---|
| 45 |
|
---|
| 46 |
|
---|
| 47 | #ifdef CONFIG_WITH_ALLOC_CACHES
|
---|
| 48 |
|
---|
| 49 | /* Free am item.
|
---|
| 50 | This was not inlined because of aliasing issues arrising with GCC.
|
---|
| 51 | It is also in a separate file for this reason (it used to be in misc.c
|
---|
| 52 | but since free_dep_chain() was using it there, we ran the risk of it
|
---|
| 53 | being inlined and gcc screwing up). */
|
---|
| 54 | void
|
---|
| 55 | alloccache_free (struct alloccache *cache, void *item)
|
---|
| 56 | {
|
---|
[2633] | 57 | #ifndef CONFIG_WITH_ALLOCCACHE_DEBUG
|
---|
[2062] | 58 | struct alloccache_free_ent *f = (struct alloccache_free_ent *)item;
|
---|
[2633] | 59 | # if 0 /*ndef NDEBUG*/
|
---|
[2062] | 60 | struct alloccache_free_ent *c;
|
---|
| 61 | unsigned int i = 0;
|
---|
| 62 | for (c = cache->free_head; c != NULL; c = c->next, i++)
|
---|
| 63 | MY_ASSERT_MSG (c != f && i < 0x10000000,
|
---|
| 64 | ("i=%u total_count=%u\n", i, cache->total_count));
|
---|
[2633] | 65 | # endif
|
---|
[2062] | 66 |
|
---|
| 67 | f->next = cache->free_head;
|
---|
| 68 | cache->free_head = f;
|
---|
| 69 | MAKE_STATS(cache->free_count++;);
|
---|
[3140] | 70 | #else /* CONFIG_WITH_ALLOCCACHE_DEBUG */
|
---|
| 71 |
|
---|
| 72 | struct alloccache **ppcache = (struct alloccache **)item - 1;
|
---|
| 73 | MY_ASSERT_MSG (*ppcache == cache, ("*ppcache=%p cache=%p item=%p\n", *ppcache, cache, item));
|
---|
| 74 | *ppcache = NULL;
|
---|
| 75 | free(ppcache);
|
---|
| 76 | #endif /* CONFIG_WITH_ALLOCCACHE_DEBUG */
|
---|
[2062] | 77 | }
|
---|
| 78 |
|
---|
| 79 | /* Default allocator. */
|
---|
| 80 | static void *
|
---|
| 81 | alloccache_default_grow_alloc(void *ignore, unsigned int size)
|
---|
| 82 | {
|
---|
| 83 | return xmalloc (size);
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | /* Worker for growing the cache. */
|
---|
| 87 | struct alloccache_free_ent *
|
---|
| 88 | alloccache_alloc_grow (struct alloccache *cache)
|
---|
| 89 | {
|
---|
[2633] | 90 | #ifndef CONFIG_WITH_ALLOCCACHE_DEBUG
|
---|
[2062] | 91 | void *item;
|
---|
| 92 | unsigned int items = (64*1024 - 32) / cache->size;
|
---|
| 93 | cache->free_start = cache->grow_alloc (cache->grow_arg, items * cache->size);
|
---|
| 94 | cache->free_end = cache->free_start + items * cache->size;
|
---|
| 95 | cache->total_count+= items;
|
---|
| 96 |
|
---|
[2633] | 97 | # ifndef NDEBUG /* skip the first item so the heap can detect free(). */
|
---|
[2062] | 98 | cache->total_count--;
|
---|
| 99 | cache->free_start += cache->size;
|
---|
[2633] | 100 | # endif
|
---|
[2062] | 101 |
|
---|
| 102 | item = cache->free_start;
|
---|
| 103 | cache->free_start += cache->size;
|
---|
| 104 | /* caller counts */
|
---|
| 105 | return (struct alloccache_free_ent *)item;
|
---|
[3140] | 106 | #else /* CONFIG_WITH_ALLOCCACHE_DEBUG */
|
---|
| 107 |
|
---|
| 108 | /* Prefix the allocation with a cache pointer so alloccahce_free can better
|
---|
| 109 | catch incorrect calls. */
|
---|
| 110 | struct alloccache **ppcache = (struct alloccache **)xmalloc(sizeof(*ppcache) + cache->size);
|
---|
| 111 | *ppcache = cache;
|
---|
| 112 | return (struct alloccache_free_ent *)(ppcache + 1);
|
---|
| 113 | #endif /* CONFIG_WITH_ALLOCCACHE_DEBUG */
|
---|
[2062] | 114 | }
|
---|
| 115 |
|
---|
| 116 | /* List of alloc caches, for printing. */
|
---|
| 117 | static struct alloccache *alloccache_head = NULL;
|
---|
| 118 |
|
---|
| 119 | /* Initializes an alloc cache */
|
---|
| 120 | void
|
---|
| 121 | alloccache_init (struct alloccache *cache, unsigned int size, const char *name,
|
---|
| 122 | void *(*grow_alloc)(void *grow_arg, unsigned int size), void *grow_arg)
|
---|
| 123 | {
|
---|
| 124 | unsigned act_size;
|
---|
| 125 |
|
---|
| 126 | /* ensure OK alignment and min sizeof (struct alloccache_free_ent). */
|
---|
| 127 | if (size <= sizeof (struct alloccache_free_ent))
|
---|
| 128 | act_size = sizeof (struct alloccache_free_ent);
|
---|
| 129 | else if (size <= 32)
|
---|
| 130 | {
|
---|
| 131 | act_size = 4;
|
---|
| 132 | while (act_size < size)
|
---|
| 133 | act_size <<= 1;
|
---|
| 134 | }
|
---|
| 135 | else
|
---|
| 136 | act_size = (size + 31U) & ~(size_t)31;
|
---|
| 137 |
|
---|
| 138 | /* align the structure. */
|
---|
| 139 | cache->free_start = NULL;
|
---|
| 140 | cache->free_end = NULL;
|
---|
| 141 | cache->free_head = NULL;
|
---|
| 142 | cache->size = act_size;
|
---|
| 143 | cache->total_count = 0;
|
---|
| 144 | cache->alloc_count = 0;
|
---|
| 145 | cache->free_count = 0;
|
---|
| 146 | cache->name = name;
|
---|
| 147 | cache->grow_arg = grow_arg;
|
---|
| 148 | cache->grow_alloc = grow_alloc ? grow_alloc : alloccache_default_grow_alloc;
|
---|
| 149 |
|
---|
| 150 | /* link it. */
|
---|
| 151 | cache->next = alloccache_head;
|
---|
| 152 | alloccache_head = cache;
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | /* Terminate an alloc cache, free all the memory it contains. */
|
---|
| 156 | void
|
---|
| 157 | alloccache_term (struct alloccache *cache,
|
---|
| 158 | void (*term_free)(void *term_arg, void *ptr, unsigned int size), void *term_arg)
|
---|
| 159 | {
|
---|
| 160 | /*cache->size = 0;*/
|
---|
| 161 | (void)cache;
|
---|
| 162 | (void)term_free;
|
---|
| 163 | (void)term_arg;
|
---|
| 164 | /* FIXME: Implement memory segment tracking and cleanup. */
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | /* Joins to caches, unlinking the 2nd one. */
|
---|
| 168 | void
|
---|
| 169 | alloccache_join (struct alloccache *cache, struct alloccache *eat)
|
---|
| 170 | {
|
---|
| 171 | assert (cache->size == eat->size);
|
---|
| 172 |
|
---|
| 173 | #if 0 /* probably a waste of time */ /* FIXME: Optimize joining, avoid all list walking. */
|
---|
| 174 | /* add the free list... */
|
---|
| 175 | if (eat->free_head)
|
---|
| 176 | {
|
---|
| 177 | unsigned int eat_in_use = eat->alloc_count - eat->free_count;
|
---|
| 178 | unsigned int dst_in_use = cache->alloc_count - cache->free_count;
|
---|
| 179 | if (!cache->free_head)
|
---|
| 180 | cache->free_head = eat->free_head;
|
---|
| 181 | else if (eat->total_count - eat_in_use < cache->total_count - dst_ins_use)
|
---|
| 182 | {
|
---|
| 183 | struct alloccache_free_ent *last = eat->free_head;
|
---|
| 184 | while (last->next)
|
---|
| 185 | last = last->next;
|
---|
| 186 | last->next = cache->free_head;
|
---|
| 187 | cache->free_head = eat->free_head;
|
---|
| 188 | }
|
---|
| 189 | else
|
---|
| 190 | {
|
---|
| 191 | struct alloccache_free_ent *last = cache->free_head;
|
---|
| 192 | while (last->next)
|
---|
| 193 | last = last->next;
|
---|
| 194 | last->next = eat->free_head;
|
---|
| 195 | }
|
---|
| 196 | }
|
---|
| 197 |
|
---|
| 198 | /* ... and the free space. */
|
---|
| 199 | while (eat->free_start != eat->free_end)
|
---|
| 200 | {
|
---|
| 201 | struct alloccache_free_ent *f = (struct alloccache_free_ent *)eat->free_start;
|
---|
| 202 | eat->free_start += eat->size;
|
---|
| 203 | f->next = cache->free_head;
|
---|
| 204 | cache->free_head = f;
|
---|
| 205 | }
|
---|
| 206 |
|
---|
| 207 | /* and statistics */
|
---|
| 208 | cache->alloc_count += eat->alloc_count;
|
---|
| 209 | cache->free_count += eat->free_count;
|
---|
| 210 | #else
|
---|
| 211 | /* and statistics */
|
---|
| 212 | cache->alloc_count += eat->alloc_count;
|
---|
| 213 | cache->free_count += eat->free_count;
|
---|
| 214 | #endif
|
---|
| 215 | cache->total_count += eat->total_count;
|
---|
| 216 |
|
---|
| 217 | /* unlink and disable the eat cache */
|
---|
| 218 | if (alloccache_head == eat)
|
---|
| 219 | alloccache_head = eat->next;
|
---|
| 220 | else
|
---|
| 221 | {
|
---|
| 222 | struct alloccache *cur = alloccache_head;
|
---|
| 223 | while (cur->next != eat)
|
---|
| 224 | cur = cur->next;
|
---|
| 225 | assert (cur && cur->next == eat);
|
---|
| 226 | cur->next = eat->next;
|
---|
| 227 | }
|
---|
| 228 |
|
---|
| 229 | eat->size = 0;
|
---|
| 230 | eat->free_end = eat->free_start = NULL;
|
---|
| 231 | eat->free_head = NULL;
|
---|
| 232 | }
|
---|
| 233 |
|
---|
| 234 | /* Print one alloc cache. */
|
---|
| 235 | void
|
---|
| 236 | alloccache_print (struct alloccache *cache)
|
---|
| 237 | {
|
---|
| 238 | printf (_("\n# Alloc Cache: %s\n"
|
---|
| 239 | "# Items: size = %-3u total = %-6u"),
|
---|
| 240 | cache->name, cache->size, cache->total_count);
|
---|
| 241 | MAKE_STATS(printf (_(" in-use = %-6lu"),
|
---|
| 242 | cache->alloc_count - cache->free_count););
|
---|
| 243 | MAKE_STATS(printf (_("\n# alloc calls = %-7lu free calls = %-7lu"),
|
---|
| 244 | cache->alloc_count, cache->free_count););
|
---|
| 245 | printf ("\n");
|
---|
| 246 | }
|
---|
| 247 |
|
---|
| 248 | /* Print all alloc caches. */
|
---|
| 249 | void
|
---|
| 250 | alloccache_print_all (void)
|
---|
| 251 | {
|
---|
| 252 | struct alloccache *cur;
|
---|
| 253 | puts ("");
|
---|
| 254 | for (cur = alloccache_head; cur; cur = cur->next)
|
---|
| 255 | alloccache_print (cur);
|
---|
| 256 | }
|
---|
| 257 |
|
---|
| 258 | #endif /* CONFIG_WITH_ALLOC_CACHES */
|
---|
| 259 |
|
---|