diff options
author | otto <> | 2014-05-26 06:19:07 +0000 |
---|---|---|
committer | otto <> | 2014-05-26 06:19:07 +0000 |
commit | 8ba2fea129dbe500a291768b0aec33a3fd015669 (patch) | |
tree | 0271aed53fd51ff24b80750506655e91821a5421 /src | |
parent | 479c0f2aed2401c3c2377e38a9d1308883fdde31 (diff) | |
download | openbsd-8ba2fea129dbe500a291768b0aec33a3fd015669.tar.gz openbsd-8ba2fea129dbe500a291768b0aec33a3fd015669.tar.bz2 openbsd-8ba2fea129dbe500a291768b0aec33a3fd015669.zip |
move all stats collecting under MALLOC_STATS; ok krw@
Diffstat (limited to 'src')
-rw-r--r-- | src/lib/libc/stdlib/malloc.c | 61 |
1 files changed, 33 insertions, 28 deletions
diff --git a/src/lib/libc/stdlib/malloc.c b/src/lib/libc/stdlib/malloc.c index 54b3cd427d..412d000a11 100644 --- a/src/lib/libc/stdlib/malloc.c +++ b/src/lib/libc/stdlib/malloc.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: malloc.c,v 1.165 2014/05/21 15:47:51 otto Exp $ */ | 1 | /* $OpenBSD: malloc.c,v 1.166 2014/05/26 06:19:07 otto Exp $ */ |
2 | /* | 2 | /* |
3 | * Copyright (c) 2008, 2010, 2011 Otto Moerbeek <otto@drijf.net> | 3 | * Copyright (c) 2008, 2010, 2011 Otto Moerbeek <otto@drijf.net> |
4 | * Copyright (c) 2012 Matthew Dempsky <matthew@openbsd.org> | 4 | * Copyright (c) 2012 Matthew Dempsky <matthew@openbsd.org> |
@@ -127,10 +127,16 @@ struct dir_info { | |||
127 | size_t delete_moves; | 127 | size_t delete_moves; |
128 | size_t cheap_realloc_tries; | 128 | size_t cheap_realloc_tries; |
129 | size_t cheap_reallocs; | 129 | size_t cheap_reallocs; |
130 | #define STATS_INC(x) ((x)++) | 130 | size_t malloc_used; /* bytes allocated */ |
131 | #define STATS_ZERO(x) ((x) = 0) | 131 | size_t malloc_guarded; /* bytes used for guards */ |
132 | #define STATS_SETF(x,y) ((x)->f = (y)) | 132 | #define STATS_ADD(x,y) ((x) += (y)) |
133 | #define STATS_SUB(x,y) ((x) -= (y)) | ||
134 | #define STATS_INC(x) ((x)++) | ||
135 | #define STATS_ZERO(x) ((x) = 0) | ||
136 | #define STATS_SETF(x,y) ((x)->f = (y)) | ||
133 | #else | 137 | #else |
138 | #define STATS_ADD(x,y) /* nothing */ | ||
139 | #define STATS_SUB(x,y) /* nothing */ | ||
134 | #define STATS_INC(x) /* nothing */ | 140 | #define STATS_INC(x) /* nothing */ |
135 | #define STATS_ZERO(x) /* nothing */ | 141 | #define STATS_ZERO(x) /* nothing */ |
136 | #define STATS_SETF(x,y) /* nothing */ | 142 | #define STATS_SETF(x,y) /* nothing */ |
@@ -189,8 +195,6 @@ char *malloc_options; /* compile-time options */ | |||
189 | static char *malloc_func; /* current function */ | 195 | static char *malloc_func; /* current function */ |
190 | static int malloc_active; /* status of malloc */ | 196 | static int malloc_active; /* status of malloc */ |
191 | 197 | ||
192 | static size_t malloc_guarded; /* bytes used for guards */ | ||
193 | static size_t malloc_used; /* bytes allocated */ | ||
194 | 198 | ||
195 | static size_t rbytesused; /* random bytes used */ | 199 | static size_t rbytesused; /* random bytes used */ |
196 | static u_char rbytes[512]; /* random bytes */ | 200 | static u_char rbytes[512]; /* random bytes */ |
@@ -241,7 +245,7 @@ wrterror(char *msg, void *p) | |||
241 | iov[0].iov_base = __progname; | 245 | iov[0].iov_base = __progname; |
242 | iov[0].iov_len = strlen(__progname); | 246 | iov[0].iov_len = strlen(__progname); |
243 | iov[1].iov_base = pidbuf; | 247 | iov[1].iov_base = pidbuf; |
244 | snprintf(pidbuf, sizeof(pidbuf), "(%d)", getpid()); | 248 | snprintf(pidbuf, sizeof(pidbuf), "(%d) in ", getpid()); |
245 | iov[1].iov_len = strlen(pidbuf); | 249 | iov[1].iov_len = strlen(pidbuf); |
246 | iov[2].iov_base = malloc_func; | 250 | iov[2].iov_base = malloc_func; |
247 | iov[2].iov_len = strlen(malloc_func); | 251 | iov[2].iov_len = strlen(malloc_func); |
@@ -311,7 +315,7 @@ unmap(struct dir_info *d, void *p, size_t sz) | |||
311 | if (psz > mopts.malloc_cache) { | 315 | if (psz > mopts.malloc_cache) { |
312 | if (munmap(p, sz)) | 316 | if (munmap(p, sz)) |
313 | wrterror("munmap", p); | 317 | wrterror("munmap", p); |
314 | malloc_used -= sz; | 318 | STATS_SUB(d->malloc_used, sz); |
315 | return; | 319 | return; |
316 | } | 320 | } |
317 | tounmap = 0; | 321 | tounmap = 0; |
@@ -332,7 +336,7 @@ unmap(struct dir_info *d, void *p, size_t sz) | |||
332 | tounmap = 0; | 336 | tounmap = 0; |
333 | d->free_regions_size -= r->size; | 337 | d->free_regions_size -= r->size; |
334 | r->size = 0; | 338 | r->size = 0; |
335 | malloc_used -= rsz; | 339 | STATS_SUB(d->malloc_used, rsz); |
336 | } | 340 | } |
337 | } | 341 | } |
338 | if (tounmap > 0) | 342 | if (tounmap > 0) |
@@ -372,7 +376,7 @@ zapcacheregion(struct dir_info *d, void *p, size_t len) | |||
372 | r->p = NULL; | 376 | r->p = NULL; |
373 | d->free_regions_size -= r->size; | 377 | d->free_regions_size -= r->size; |
374 | r->size = 0; | 378 | r->size = 0; |
375 | malloc_used -= rsz; | 379 | STATS_SUB(d->malloc_used, rsz); |
376 | } | 380 | } |
377 | } | 381 | } |
378 | } | 382 | } |
@@ -395,7 +399,7 @@ map(struct dir_info *d, size_t sz, int zero_fill) | |||
395 | if (psz > d->free_regions_size) { | 399 | if (psz > d->free_regions_size) { |
396 | p = MMAP(sz); | 400 | p = MMAP(sz); |
397 | if (p != MAP_FAILED) | 401 | if (p != MAP_FAILED) |
398 | malloc_used += sz; | 402 | STATS_ADD(d->malloc_used, sz); |
399 | /* zero fill not needed */ | 403 | /* zero fill not needed */ |
400 | return p; | 404 | return p; |
401 | } | 405 | } |
@@ -439,7 +443,7 @@ map(struct dir_info *d, size_t sz, int zero_fill) | |||
439 | } | 443 | } |
440 | p = MMAP(sz); | 444 | p = MMAP(sz); |
441 | if (p != MAP_FAILED) | 445 | if (p != MAP_FAILED) |
442 | malloc_used += sz; | 446 | STATS_ADD(d->malloc_used, sz); |
443 | if (d->free_regions_size > mopts.malloc_cache) | 447 | if (d->free_regions_size > mopts.malloc_cache) |
444 | wrterror("malloc cache", NULL); | 448 | wrterror("malloc cache", NULL); |
445 | /* zero fill not needed */ | 449 | /* zero fill not needed */ |
@@ -625,7 +629,7 @@ omalloc_init(struct dir_info **dp) | |||
625 | for (j = 0; j < MALLOC_CHUNK_LISTS; j++) | 629 | for (j = 0; j < MALLOC_CHUNK_LISTS; j++) |
626 | LIST_INIT(&d->chunk_dir[i][j]); | 630 | LIST_INIT(&d->chunk_dir[i][j]); |
627 | } | 631 | } |
628 | malloc_used += regioninfo_size; | 632 | STATS_ADD(d->malloc_used, regioninfo_size); |
629 | d->canary1 = mopts.malloc_canary ^ (u_int32_t)(uintptr_t)d; | 633 | d->canary1 = mopts.malloc_canary ^ (u_int32_t)(uintptr_t)d; |
630 | d->canary2 = ~d->canary1; | 634 | d->canary2 = ~d->canary1; |
631 | 635 | ||
@@ -661,7 +665,7 @@ omalloc_grow(struct dir_info *d) | |||
661 | if (p == MAP_FAILED) | 665 | if (p == MAP_FAILED) |
662 | return 1; | 666 | return 1; |
663 | 667 | ||
664 | malloc_used += newsize; | 668 | STATS_ADD(d->malloc_used, newsize); |
665 | memset(p, 0, newsize); | 669 | memset(p, 0, newsize); |
666 | STATS_ZERO(d->inserts); | 670 | STATS_ZERO(d->inserts); |
667 | STATS_ZERO(d->insert_collisions); | 671 | STATS_ZERO(d->insert_collisions); |
@@ -681,7 +685,8 @@ omalloc_grow(struct dir_info *d) | |||
681 | if (munmap(d->r, d->regions_total * sizeof(struct region_info))) | 685 | if (munmap(d->r, d->regions_total * sizeof(struct region_info))) |
682 | wrterror("munmap", d->r); | 686 | wrterror("munmap", d->r); |
683 | else | 687 | else |
684 | malloc_used -= d->regions_total * sizeof(struct region_info); | 688 | STATS_SUB(d->malloc_used, |
689 | d->regions_total * sizeof(struct region_info)); | ||
685 | d->regions_free = d->regions_free + d->regions_total; | 690 | d->regions_free = d->regions_free + d->regions_total; |
686 | d->regions_total = newtotal; | 691 | d->regions_total = newtotal; |
687 | d->r = p; | 692 | d->r = p; |
@@ -710,7 +715,7 @@ alloc_chunk_info(struct dir_info *d, int bits) | |||
710 | q = MMAP(MALLOC_PAGESIZE); | 715 | q = MMAP(MALLOC_PAGESIZE); |
711 | if (q == MAP_FAILED) | 716 | if (q == MAP_FAILED) |
712 | return NULL; | 717 | return NULL; |
713 | malloc_used += MALLOC_PAGESIZE; | 718 | STATS_ADD(d->malloc_used, MALLOC_PAGESIZE); |
714 | count = MALLOC_PAGESIZE / size; | 719 | count = MALLOC_PAGESIZE / size; |
715 | for (i = 0; i < count; i++, q += size) | 720 | for (i = 0; i < count; i++, q += size) |
716 | LIST_INSERT_HEAD(&d->chunk_info_list[bits], | 721 | LIST_INSERT_HEAD(&d->chunk_info_list[bits], |
@@ -1067,7 +1072,7 @@ omalloc(size_t sz, int zero_fill, void *f) | |||
1067 | if (mprotect((char *)p + psz - mopts.malloc_guard, | 1072 | if (mprotect((char *)p + psz - mopts.malloc_guard, |
1068 | mopts.malloc_guard, PROT_NONE)) | 1073 | mopts.malloc_guard, PROT_NONE)) |
1069 | wrterror("mprotect", NULL); | 1074 | wrterror("mprotect", NULL); |
1070 | malloc_guarded += mopts.malloc_guard; | 1075 | STATS_ADD(g_pool->malloc_guarded, mopts.malloc_guard); |
1071 | } | 1076 | } |
1072 | 1077 | ||
1073 | if (mopts.malloc_move && | 1078 | if (mopts.malloc_move && |
@@ -1142,7 +1147,7 @@ malloc(size_t size) | |||
1142 | int saved_errno = errno; | 1147 | int saved_errno = errno; |
1143 | 1148 | ||
1144 | _MALLOC_LOCK(); | 1149 | _MALLOC_LOCK(); |
1145 | malloc_func = " in malloc():"; | 1150 | malloc_func = "malloc():"; |
1146 | if (g_pool == NULL) { | 1151 | if (g_pool == NULL) { |
1147 | if (malloc_init() != 0) | 1152 | if (malloc_init() != 0) |
1148 | return NULL; | 1153 | return NULL; |
@@ -1201,7 +1206,7 @@ ofree(void *p) | |||
1201 | PROT_READ | PROT_WRITE)) | 1206 | PROT_READ | PROT_WRITE)) |
1202 | wrterror("mprotect", NULL); | 1207 | wrterror("mprotect", NULL); |
1203 | } | 1208 | } |
1204 | malloc_guarded -= mopts.malloc_guard; | 1209 | STATS_SUB(g_pool->malloc_guarded, mopts.malloc_guard); |
1205 | } | 1210 | } |
1206 | if (mopts.malloc_junk && !mopts.malloc_freeunmap) { | 1211 | if (mopts.malloc_junk && !mopts.malloc_freeunmap) { |
1207 | size_t amt = mopts.malloc_junk == 1 ? MALLOC_MAXCHUNK : | 1212 | size_t amt = mopts.malloc_junk == 1 ? MALLOC_MAXCHUNK : |
@@ -1249,7 +1254,7 @@ free(void *ptr) | |||
1249 | return; | 1254 | return; |
1250 | 1255 | ||
1251 | _MALLOC_LOCK(); | 1256 | _MALLOC_LOCK(); |
1252 | malloc_func = " in free():"; | 1257 | malloc_func = "free():"; |
1253 | if (g_pool == NULL) { | 1258 | if (g_pool == NULL) { |
1254 | _MALLOC_UNLOCK(); | 1259 | _MALLOC_UNLOCK(); |
1255 | wrterror("free() called before allocation", NULL); | 1260 | wrterror("free() called before allocation", NULL); |
@@ -1316,7 +1321,7 @@ orealloc(void *p, size_t newsz, void *f) | |||
1316 | else | 1321 | else |
1317 | q = MAP_FAILED; | 1322 | q = MAP_FAILED; |
1318 | if (q == hint) { | 1323 | if (q == hint) { |
1319 | malloc_used += needed; | 1324 | STATS_ADD(g_pool->malloc_used, needed); |
1320 | if (mopts.malloc_junk == 2) | 1325 | if (mopts.malloc_junk == 2) |
1321 | memset(q, SOME_JUNK, needed); | 1326 | memset(q, SOME_JUNK, needed); |
1322 | r->size = newsz; | 1327 | r->size = newsz; |
@@ -1378,7 +1383,7 @@ realloc(void *ptr, size_t size) | |||
1378 | int saved_errno = errno; | 1383 | int saved_errno = errno; |
1379 | 1384 | ||
1380 | _MALLOC_LOCK(); | 1385 | _MALLOC_LOCK(); |
1381 | malloc_func = " in realloc():"; | 1386 | malloc_func = "realloc():"; |
1382 | if (g_pool == NULL) { | 1387 | if (g_pool == NULL) { |
1383 | if (malloc_init() != 0) | 1388 | if (malloc_init() != 0) |
1384 | return NULL; | 1389 | return NULL; |
@@ -1414,7 +1419,7 @@ calloc(size_t nmemb, size_t size) | |||
1414 | int saved_errno = errno; | 1419 | int saved_errno = errno; |
1415 | 1420 | ||
1416 | _MALLOC_LOCK(); | 1421 | _MALLOC_LOCK(); |
1417 | malloc_func = " in calloc():"; | 1422 | malloc_func = "calloc():"; |
1418 | if (g_pool == NULL) { | 1423 | if (g_pool == NULL) { |
1419 | if (malloc_init() != 0) | 1424 | if (malloc_init() != 0) |
1420 | return NULL; | 1425 | return NULL; |
@@ -1480,7 +1485,7 @@ mapalign(struct dir_info *d, size_t alignment, size_t sz, int zero_fill) | |||
1480 | } | 1485 | } |
1481 | if (munmap(q + sz, alignment - (q - p))) | 1486 | if (munmap(q + sz, alignment - (q - p))) |
1482 | wrterror("munmap", q + sz); | 1487 | wrterror("munmap", q + sz); |
1483 | malloc_used -= alignment; | 1488 | STATS_SUB(d->malloc_used, alignment); |
1484 | 1489 | ||
1485 | return q; | 1490 | return q; |
1486 | } | 1491 | } |
@@ -1525,7 +1530,7 @@ omemalign(size_t alignment, size_t sz, int zero_fill, void *f) | |||
1525 | if (mprotect((char *)p + psz - mopts.malloc_guard, | 1530 | if (mprotect((char *)p + psz - mopts.malloc_guard, |
1526 | mopts.malloc_guard, PROT_NONE)) | 1531 | mopts.malloc_guard, PROT_NONE)) |
1527 | wrterror("mprotect", NULL); | 1532 | wrterror("mprotect", NULL); |
1528 | malloc_guarded += mopts.malloc_guard; | 1533 | STATS_ADD(g_pool->malloc_guarded, mopts.malloc_guard); |
1529 | } | 1534 | } |
1530 | 1535 | ||
1531 | if (mopts.malloc_junk == 2) { | 1536 | if (mopts.malloc_junk == 2) { |
@@ -1550,7 +1555,7 @@ posix_memalign(void **memptr, size_t alignment, size_t size) | |||
1550 | return EINVAL; | 1555 | return EINVAL; |
1551 | 1556 | ||
1552 | _MALLOC_LOCK(); | 1557 | _MALLOC_LOCK(); |
1553 | malloc_func = " in posix_memalign():"; | 1558 | malloc_func = "posix_memalign():"; |
1554 | if (g_pool == NULL) { | 1559 | if (g_pool == NULL) { |
1555 | if (malloc_init() != 0) | 1560 | if (malloc_init() != 0) |
1556 | goto err; | 1561 | goto err; |
@@ -1790,9 +1795,9 @@ malloc_dump1(int fd, struct dir_info *d) | |||
1790 | d->r[i].f, 0); | 1795 | d->r[i].f, 0); |
1791 | } | 1796 | } |
1792 | } | 1797 | } |
1793 | snprintf(buf, sizeof(buf), "In use %zu\n", malloc_used); | 1798 | snprintf(buf, sizeof(buf), "In use %zu\n", d->malloc_used); |
1794 | write(fd, buf, strlen(buf)); | 1799 | write(fd, buf, strlen(buf)); |
1795 | snprintf(buf, sizeof(buf), "Guarded %zu\n", malloc_guarded); | 1800 | snprintf(buf, sizeof(buf), "Guarded %zu\n", d->malloc_guarded); |
1796 | write(fd, buf, strlen(buf)); | 1801 | write(fd, buf, strlen(buf)); |
1797 | dump_leaks(fd); | 1802 | dump_leaks(fd); |
1798 | write(fd, "\n", 1); | 1803 | write(fd, "\n", 1); |