[501] | 1 | /* Constant string caching for GNU Make.
|
---|
[3140] | 2 | Copyright (C) 2006-2016 Free Software Foundation, Inc.
|
---|
[501] | 3 | This file is part of GNU Make.
|
---|
| 4 |
|
---|
| 5 | GNU Make is free software; you can redistribute it and/or modify it under the
|
---|
| 6 | terms of the GNU General Public License as published by the Free Software
|
---|
[1993] | 7 | Foundation; either version 3 of the License, or (at your option) any later
|
---|
| 8 | version.
|
---|
[501] | 9 |
|
---|
| 10 | GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
|
---|
| 11 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
---|
| 12 | A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
---|
| 13 |
|
---|
| 14 | You should have received a copy of the GNU General Public License along with
|
---|
[1993] | 15 | this program. If not, see <http://www.gnu.org/licenses/>. */
|
---|
[501] | 16 |
|
---|
[3140] | 17 | #include "makeint.h"
|
---|
[1870] | 18 | #ifndef CONFIG_WITH_STRCACHE2
|
---|
[501] | 19 |
|
---|
[3140] | 20 | #include <stddef.h>
|
---|
[501] | 21 | #include <assert.h>
|
---|
| 22 |
|
---|
| 23 | #include "hash.h"
|
---|
| 24 |
|
---|
| 25 | /* A string cached here will never be freed, so we don't need to worry about
|
---|
| 26 | reference counting. We just store the string, and then remember it in a
|
---|
| 27 | hash so it can be looked up again. */
|
---|
| 28 |
|
---|
[3140] | 29 | typedef unsigned short int sc_buflen_t;
|
---|
| 30 |
|
---|
[501] | 31 | struct strcache {
|
---|
[3140] | 32 | struct strcache *next; /* The next block of strings. Must be first! */
|
---|
| 33 | sc_buflen_t end; /* Offset to the beginning of free space. */
|
---|
| 34 | sc_buflen_t bytesfree; /* Free space left in this buffer. */
|
---|
| 35 | sc_buflen_t count; /* # of strings in this buffer (for stats). */
|
---|
[501] | 36 | char buffer[1]; /* The buffer comes after this. */
|
---|
| 37 | };
|
---|
| 38 |
|
---|
[3140] | 39 | /* The size (in bytes) of each cache buffer.
|
---|
| 40 | Try to pick something that will map well into the heap.
|
---|
| 41 | This must be able to be represented by a short int (<=65535). */
|
---|
| 42 | #define CACHE_BUFFER_BASE (8192)
|
---|
| 43 | #define CACHE_BUFFER_ALLOC(_s) ((_s) - (2 * sizeof (size_t)))
|
---|
| 44 | #define CACHE_BUFFER_OFFSET (offsetof (struct strcache, buffer))
|
---|
| 45 | #define CACHE_BUFFER_SIZE(_s) (CACHE_BUFFER_ALLOC(_s) - CACHE_BUFFER_OFFSET)
|
---|
| 46 | #define BUFSIZE CACHE_BUFFER_SIZE (CACHE_BUFFER_BASE)
|
---|
| 47 |
|
---|
[501] | 48 | static struct strcache *strcache = NULL;
|
---|
[3140] | 49 | static struct strcache *fullcache = NULL;
|
---|
[501] | 50 |
|
---|
[3140] | 51 | static unsigned long total_buffers = 0;
|
---|
| 52 | static unsigned long total_strings = 0;
|
---|
| 53 | static unsigned long total_size = 0;
|
---|
| 54 |
|
---|
[903] | 55 | /* Add a new buffer to the cache. Add it at the front to reduce search time.
|
---|
| 56 | This can also increase the overhead, since it's less likely that older
|
---|
| 57 | buffers will be filled in. However, GNU make has so many smaller strings
|
---|
| 58 | that this doesn't seem to be much of an issue in practice.
|
---|
| 59 | */
|
---|
[501] | 60 | static struct strcache *
|
---|
[3140] | 61 | new_cache (struct strcache **head, sc_buflen_t buflen)
|
---|
[501] | 62 | {
|
---|
[3140] | 63 | struct strcache *new = xmalloc (buflen + CACHE_BUFFER_OFFSET);
|
---|
| 64 | new->end = 0;
|
---|
[501] | 65 | new->count = 0;
|
---|
[3140] | 66 | new->bytesfree = buflen;
|
---|
[501] | 67 |
|
---|
[3140] | 68 | new->next = *head;
|
---|
| 69 | *head = new;
|
---|
[501] | 70 |
|
---|
[3140] | 71 | ++total_buffers;
|
---|
[501] | 72 | return new;
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | static const char *
|
---|
[3140] | 76 | copy_string (struct strcache *sp, const char *str, unsigned int len)
|
---|
[501] | 77 | {
|
---|
[3140] | 78 | /* Add the string to this cache. */
|
---|
| 79 | char *res = &sp->buffer[sp->end];
|
---|
| 80 |
|
---|
| 81 | memmove (res, str, len);
|
---|
| 82 | res[len++] = '\0';
|
---|
| 83 | sp->end += len;
|
---|
| 84 | sp->bytesfree -= len;
|
---|
| 85 | ++sp->count;
|
---|
| 86 |
|
---|
| 87 | return res;
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | static const char *
|
---|
| 91 | add_string (const char *str, unsigned int len)
|
---|
| 92 | {
|
---|
| 93 | const char *res;
|
---|
[501] | 94 | struct strcache *sp;
|
---|
[3140] | 95 | struct strcache **spp = &strcache;
|
---|
| 96 | /* We need space for the nul char. */
|
---|
| 97 | unsigned int sz = len + 1;
|
---|
[501] | 98 |
|
---|
[3140] | 99 | ++total_strings;
|
---|
| 100 | total_size += sz;
|
---|
| 101 |
|
---|
[903] | 102 | /* If the string we want is too large to fit into a single buffer, then
|
---|
[3140] | 103 | no existing cache is large enough. Add it directly to the fullcache. */
|
---|
| 104 | if (sz > BUFSIZE)
|
---|
| 105 | {
|
---|
| 106 | sp = new_cache (&fullcache, sz);
|
---|
| 107 | return copy_string (sp, str, len);
|
---|
| 108 | }
|
---|
[501] | 109 |
|
---|
[3140] | 110 | /* Find the first cache with enough free space. */
|
---|
| 111 | for (; *spp != NULL; spp = &(*spp)->next)
|
---|
| 112 | if ((*spp)->bytesfree > sz)
|
---|
| 113 | break;
|
---|
| 114 | sp = *spp;
|
---|
[501] | 115 |
|
---|
[3140] | 116 | /* If nothing is big enough, make a new cache at the front. */
|
---|
| 117 | if (sp == NULL)
|
---|
| 118 | {
|
---|
| 119 | sp = new_cache (&strcache, BUFSIZE);
|
---|
| 120 | spp = &strcache;
|
---|
| 121 | }
|
---|
[501] | 122 |
|
---|
[3140] | 123 | /* Add the string to this cache. */
|
---|
| 124 | res = copy_string (sp, str, len);
|
---|
[501] | 125 |
|
---|
[3140] | 126 | /* If the amount free in this cache is less than the average string size,
|
---|
| 127 | consider it full and move it to the full list. */
|
---|
| 128 | if (total_strings > 20 && sp->bytesfree < (total_size / total_strings) + 1)
|
---|
| 129 | {
|
---|
| 130 | *spp = sp->next;
|
---|
| 131 | sp->next = fullcache;
|
---|
| 132 | fullcache = sp;
|
---|
| 133 | }
|
---|
[1857] | 134 |
|
---|
[501] | 135 | return res;
|
---|
| 136 | }
|
---|
| 137 |
|
---|
[3140] | 138 | /* For strings too large for the strcache, we just save them in a list. */
|
---|
| 139 | struct hugestring {
|
---|
| 140 | struct hugestring *next; /* The next string. */
|
---|
| 141 | char buffer[1]; /* The string. */
|
---|
| 142 | };
|
---|
[1857] | 143 |
|
---|
[3140] | 144 | static struct hugestring *hugestrings = NULL;
|
---|
| 145 |
|
---|
| 146 | static const char *
|
---|
| 147 | add_hugestring (const char *str, unsigned int len)
|
---|
| 148 | {
|
---|
| 149 | struct hugestring *new = xmalloc (sizeof (struct hugestring) + len);
|
---|
| 150 | memcpy (new->buffer, str, len);
|
---|
| 151 | new->buffer[len] = '\0';
|
---|
| 152 |
|
---|
| 153 | new->next = hugestrings;
|
---|
| 154 | hugestrings = new;
|
---|
| 155 |
|
---|
| 156 | return new->buffer;
|
---|
| 157 | }
|
---|
| 158 |
|
---|
[1857] | 159 | /* Hash table of strings in the cache. */
|
---|
| 160 |
|
---|
[501] | 161 | static unsigned long
|
---|
| 162 | str_hash_1 (const void *key)
|
---|
| 163 | {
|
---|
| 164 | return_ISTRING_HASH_1 ((const char *) key);
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | static unsigned long
|
---|
| 168 | str_hash_2 (const void *key)
|
---|
| 169 | {
|
---|
| 170 | return_ISTRING_HASH_2 ((const char *) key);
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | static int
|
---|
| 174 | str_hash_cmp (const void *x, const void *y)
|
---|
| 175 | {
|
---|
| 176 | return_ISTRING_COMPARE ((const char *) x, (const char *) y);
|
---|
| 177 | }
|
---|
| 178 |
|
---|
| 179 | static struct hash_table strings;
|
---|
[903] | 180 | static unsigned long total_adds = 0;
|
---|
[501] | 181 |
|
---|
| 182 | static const char *
|
---|
[3140] | 183 | add_hash (const char *str, unsigned int len)
|
---|
[501] | 184 | {
|
---|
[3140] | 185 | char *const *slot;
|
---|
| 186 | const char *key;
|
---|
| 187 |
|
---|
| 188 | /* If it's too large for the string cache, just copy it.
|
---|
| 189 | We don't bother trying to match these. */
|
---|
| 190 | if (len > USHRT_MAX - 1)
|
---|
| 191 | return add_hugestring (str, len);
|
---|
| 192 |
|
---|
[501] | 193 | /* Look up the string in the hash. If it's there, return it. */
|
---|
[3140] | 194 | slot = (char *const *) hash_find_slot (&strings, str);
|
---|
| 195 | key = *slot;
|
---|
[1857] | 196 |
|
---|
[3140] | 197 | /* Count the total number of add operations we performed. */
|
---|
[1857] | 198 | ++total_adds;
|
---|
| 199 |
|
---|
| 200 | if (!HASH_VACANT (key))
|
---|
| 201 | return key;
|
---|
| 202 |
|
---|
| 203 | /* Not there yet so add it to a buffer, then into the hash table. */
|
---|
| 204 | key = add_string (str, len);
|
---|
| 205 | hash_insert_at (&strings, key, slot);
|
---|
| 206 | return key;
|
---|
| 207 | }
|
---|
| 208 |
|
---|
[501] | 209 | /* Returns true if the string is in the cache; false if not. */
|
---|
| 210 | int
|
---|
| 211 | strcache_iscached (const char *str)
|
---|
| 212 | {
|
---|
| 213 | struct strcache *sp;
|
---|
| 214 |
|
---|
| 215 | for (sp = strcache; sp != 0; sp = sp->next)
|
---|
[3140] | 216 | if (str >= sp->buffer && str < sp->buffer + sp->end)
|
---|
[501] | 217 | return 1;
|
---|
[3140] | 218 | for (sp = fullcache; sp != 0; sp = sp->next)
|
---|
| 219 | if (str >= sp->buffer && str < sp->buffer + sp->end)
|
---|
| 220 | return 1;
|
---|
[501] | 221 |
|
---|
[3140] | 222 | {
|
---|
| 223 | struct hugestring *hp;
|
---|
| 224 | for (hp = hugestrings; hp != 0; hp = hp->next)
|
---|
| 225 | if (str == hp->buffer)
|
---|
| 226 | return 1;
|
---|
| 227 | }
|
---|
| 228 |
|
---|
[501] | 229 | return 0;
|
---|
| 230 | }
|
---|
| 231 |
|
---|
| 232 | /* If the string is already in the cache, return a pointer to the cached
|
---|
| 233 | version. If not, add it then return a pointer to the cached version.
|
---|
| 234 | Note we do NOT take control of the string passed in. */
|
---|
| 235 | const char *
|
---|
| 236 | strcache_add (const char *str)
|
---|
| 237 | {
|
---|
| 238 | return add_hash (str, strlen (str));
|
---|
| 239 | }
|
---|
| 240 |
|
---|
| 241 | const char *
|
---|
[3140] | 242 | strcache_add_len (const char *str, unsigned int len)
|
---|
[501] | 243 | {
|
---|
[903] | 244 | /* If we're not given a nul-terminated string we have to create one, because
|
---|
| 245 | the hashing functions expect it. */
|
---|
| 246 | if (str[len] != '\0')
|
---|
| 247 | {
|
---|
| 248 | char *key = alloca (len + 1);
|
---|
| 249 | memcpy (key, str, len);
|
---|
| 250 | key[len] = '\0';
|
---|
| 251 | str = key;
|
---|
| 252 | }
|
---|
[501] | 253 |
|
---|
[903] | 254 | return add_hash (str, len);
|
---|
[501] | 255 | }
|
---|
| 256 |
|
---|
| 257 | void
|
---|
| 258 | strcache_init (void)
|
---|
| 259 | {
|
---|
[903] | 260 | hash_init (&strings, 8000, str_hash_1, str_hash_2, str_hash_cmp);
|
---|
[501] | 261 | }
|
---|
| 262 |
|
---|
| 263 |
|
---|
| 264 | /* Generate some stats output. */
|
---|
| 265 |
|
---|
| 266 | void
|
---|
| 267 | strcache_print_stats (const char *prefix)
|
---|
| 268 | {
|
---|
[3140] | 269 | const struct strcache *sp;
|
---|
| 270 | unsigned long numbuffs = 0, fullbuffs = 0;
|
---|
| 271 | unsigned long totfree = 0, maxfree = 0, minfree = BUFSIZE;
|
---|
[501] | 272 |
|
---|
[3140] | 273 | if (! strcache)
|
---|
[501] | 274 | {
|
---|
[3140] | 275 | printf (_("\n%s No strcache buffers\n"), prefix);
|
---|
| 276 | return;
|
---|
| 277 | }
|
---|
[501] | 278 |
|
---|
[3140] | 279 | /* Count the first buffer separately since it's not full. */
|
---|
| 280 | for (sp = strcache->next; sp != NULL; sp = sp->next)
|
---|
| 281 | {
|
---|
| 282 | sc_buflen_t bf = sp->bytesfree;
|
---|
[501] | 283 |
|
---|
[3140] | 284 | totfree += bf;
|
---|
| 285 | maxfree = (bf > maxfree ? bf : maxfree);
|
---|
| 286 | minfree = (bf < minfree ? bf : minfree);
|
---|
[501] | 287 |
|
---|
[3140] | 288 | ++numbuffs;
|
---|
| 289 | }
|
---|
| 290 | for (sp = fullcache; sp != NULL; sp = sp->next)
|
---|
| 291 | {
|
---|
| 292 | sc_buflen_t bf = sp->bytesfree;
|
---|
[903] | 293 |
|
---|
[3140] | 294 | totfree += bf;
|
---|
| 295 | maxfree = (bf > maxfree ? bf : maxfree);
|
---|
| 296 | minfree = (bf < minfree ? bf : minfree);
|
---|
[903] | 297 |
|
---|
[3140] | 298 | ++numbuffs;
|
---|
| 299 | ++fullbuffs;
|
---|
[501] | 300 | }
|
---|
| 301 |
|
---|
[3140] | 302 | /* Make sure we didn't lose any buffers. */
|
---|
| 303 | assert (total_buffers == numbuffs + 1);
|
---|
[501] | 304 |
|
---|
[3140] | 305 | printf (_("\n%s strcache buffers: %lu (%lu) / strings = %lu / storage = %lu B / avg = %lu B\n"),
|
---|
| 306 | prefix, numbuffs + 1, fullbuffs, total_strings, total_size,
|
---|
| 307 | (total_size / total_strings));
|
---|
[903] | 308 |
|
---|
[3140] | 309 | printf (_("%s current buf: size = %hu B / used = %hu B / count = %hu / avg = %hu B\n"),
|
---|
| 310 | prefix, (sc_buflen_t)BUFSIZE, strcache->end, strcache->count,
|
---|
| 311 | (strcache->end / strcache->count));
|
---|
| 312 |
|
---|
| 313 | if (numbuffs)
|
---|
| 314 | {
|
---|
| 315 | /* Show information about non-current buffers. */
|
---|
| 316 | unsigned long sz = total_size - strcache->end;
|
---|
| 317 | unsigned long cnt = total_strings - strcache->count;
|
---|
| 318 | sc_buflen_t avgfree = totfree / numbuffs;
|
---|
| 319 |
|
---|
| 320 | printf (_("%s other used: total = %lu B / count = %lu / avg = %lu B\n"),
|
---|
| 321 | prefix, sz, cnt, sz / cnt);
|
---|
| 322 |
|
---|
| 323 | printf (_("%s other free: total = %lu B / max = %lu B / min = %lu B / avg = %hu B\n"),
|
---|
| 324 | prefix, totfree, maxfree, minfree, avgfree);
|
---|
| 325 | }
|
---|
| 326 |
|
---|
| 327 | printf (_("\n%s strcache performance: lookups = %lu / hit rate = %lu%%\n"),
|
---|
| 328 | prefix, total_adds, (long unsigned)(100.0 * (total_adds - total_strings) / total_adds));
|
---|
| 329 | fputs (_("# hash-table stats:\n# "), stdout);
|
---|
[903] | 330 | hash_print_stats (&strings, stdout);
|
---|
[501] | 331 | }
|
---|
[1870] | 332 |
|
---|
| 333 | #else /* CONFIG_WITH_STRCACHE2 */
|
---|
| 334 |
|
---|
| 335 | #include "strcache2.h"
|
---|
| 336 |
|
---|
[1933] | 337 | const char *suffixes_strcached;
|
---|
| 338 |
|
---|
[1870] | 339 | /* The file string cache. */
|
---|
| 340 | struct strcache2 file_strcache;
|
---|
| 341 |
|
---|
| 342 | void strcache_init (void)
|
---|
| 343 | {
|
---|
| 344 | strcache2_init(&file_strcache,
|
---|
| 345 | "file", /* name */
|
---|
[3090] | 346 | 131072, /* hash size */
|
---|
[1870] | 347 | 0, /* default segment size*/
|
---|
| 348 | #ifdef HAVE_CASE_INSENSITIVE_FS
|
---|
| 349 | 1, /* case insensitive */
|
---|
| 350 | #else
|
---|
| 351 | 0, /* case insensitive */
|
---|
| 352 | #endif
|
---|
| 353 | 0); /* thread safe */
|
---|
[1933] | 354 |
|
---|
| 355 | /* .SUFFIXES is referenced in several loops, keep the added pointer in a
|
---|
| 356 | global var so these can be optimized. */
|
---|
| 357 |
|
---|
| 358 | suffixes_strcached = strcache_add_len (".SUFFIXES", sizeof (".SUFFIXES")-1);
|
---|
[1870] | 359 | }
|
---|
| 360 |
|
---|
| 361 | void
|
---|
| 362 | strcache_print_stats (const char *prefix)
|
---|
| 363 | {
|
---|
| 364 | strcache2_print_stats (&file_strcache, prefix);
|
---|
| 365 | }
|
---|
| 366 |
|
---|
[1895] | 367 | #endif /* CONFIG_WITH_STRCACHE2 */
|
---|
[1870] | 368 |
|
---|