2017-01-20 13:41:41 +08:00
|
|
|
#define JEMALLOC_BITMAP_C_
|
2017-04-11 09:17:55 +08:00
|
|
|
#include "jemalloc/internal/jemalloc_preamble.h"
|
|
|
|
#include "jemalloc/internal/jemalloc_internal_includes.h"
|
Use bitmaps to track small regions.
The previous free list implementation, which embedded singly linked
lists in available regions, had the unfortunate side effect of causing
many cache misses during thread cache fills. Fix this in two places:
- arena_run_t: Use a new bitmap implementation to track which regions
are available. Furthermore, revert to preferring the
lowest available region (as jemalloc did with its old
bitmap-based approach).
- tcache_t: Move read-only tcache_bin_t metadata into
tcache_bin_info_t, and add a contiguous array of pointers
to tcache_t in order to track cached objects. This
substantially increases the size of tcache_t, but results
in much higher data locality for common tcache operations.
As a side benefit, it is again possible to efficiently
flush the least recently used cached objects, so this
change changes flushing from MRU to LRU.
The new bitmap implementation uses a multi-level summary approach to
make finding the lowest available region very fast. In practice,
bitmaps only have one or two levels, though the implementation is
general enough to handle extremely large bitmaps, mainly so that large
page sizes can still be entertained.
Fix tcache_bin_flush_large() to always flush statistics, in the same way
that tcache_bin_flush_small() was recently fixed.
Use JEMALLOC_DEBUG rather than NDEBUG.
Add dassert(), and use it for debug-only asserts.
2011-03-17 01:30:13 +08:00
|
|
|
|
2017-04-12 05:43:12 +08:00
|
|
|
#include "jemalloc/internal/assert.h"
|
|
|
|
|
Use bitmaps to track small regions.
The previous free list implementation, which embedded singly linked
lists in available regions, had the unfortunate side effect of causing
many cache misses during thread cache fills. Fix this in two places:
- arena_run_t: Use a new bitmap implementation to track which regions
are available. Furthermore, revert to preferring the
lowest available region (as jemalloc did with its old
bitmap-based approach).
- tcache_t: Move read-only tcache_bin_t metadata into
tcache_bin_info_t, and add a contiguous array of pointers
to tcache_t in order to track cached objects. This
substantially increases the size of tcache_t, but results
in much higher data locality for common tcache operations.
As a side benefit, it is again possible to efficiently
flush the least recently used cached objects, so this
change changes flushing from MRU to LRU.
The new bitmap implementation uses a multi-level summary approach to
make finding the lowest available region very fast. In practice,
bitmaps only have one or two levels, though the implementation is
general enough to handle extremely large bitmaps, mainly so that large
page sizes can still be entertained.
Fix tcache_bin_flush_large() to always flush statistics, in the same way
that tcache_bin_flush_small() was recently fixed.
Use JEMALLOC_DEBUG rather than NDEBUG.
Add dassert(), and use it for debug-only asserts.
2011-03-17 01:30:13 +08:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2017-04-17 00:25:56 +08:00
|
|
|
#ifdef BITMAP_USE_TREE
|
|
|
|
|
|
|
|
void
|
|
|
|
bitmap_info_init(bitmap_info_t *binfo, size_t nbits) {
|
|
|
|
unsigned i;
|
|
|
|
size_t group_count;
|
|
|
|
|
|
|
|
assert(nbits > 0);
|
|
|
|
assert(nbits <= (ZU(1) << LG_BITMAP_MAXBITS));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Compute the number of groups necessary to store nbits bits, and
|
|
|
|
* progressively work upward through the levels until reaching a level
|
|
|
|
* that requires only one group.
|
|
|
|
*/
|
|
|
|
binfo->levels[0].group_offset = 0;
|
|
|
|
group_count = BITMAP_BITS2GROUPS(nbits);
|
|
|
|
for (i = 1; group_count > 1; i++) {
|
|
|
|
assert(i < BITMAP_MAX_LEVELS);
|
|
|
|
binfo->levels[i].group_offset = binfo->levels[i-1].group_offset
|
|
|
|
+ group_count;
|
|
|
|
group_count = BITMAP_BITS2GROUPS(group_count);
|
|
|
|
}
|
|
|
|
binfo->levels[i].group_offset = binfo->levels[i-1].group_offset
|
|
|
|
+ group_count;
|
|
|
|
assert(binfo->levels[i].group_offset <= BITMAP_GROUPS_MAX);
|
|
|
|
binfo->nlevels = i;
|
|
|
|
binfo->nbits = nbits;
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t
|
|
|
|
bitmap_info_ngroups(const bitmap_info_t *binfo) {
|
|
|
|
return binfo->levels[binfo->nlevels].group_offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
bitmap_init(bitmap_t *bitmap, const bitmap_info_t *binfo, bool fill) {
|
|
|
|
size_t extra;
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Bits are actually inverted with regard to the external bitmap
|
|
|
|
* interface.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (fill) {
|
|
|
|
/* The "filled" bitmap starts out with all 0 bits. */
|
|
|
|
memset(bitmap, 0, bitmap_size(binfo));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The "empty" bitmap starts out with all 1 bits, except for trailing
|
|
|
|
* unused bits (if any). Note that each group uses bit 0 to correspond
|
|
|
|
* to the first logical bit in the group, so extra bits are the most
|
|
|
|
* significant bits of the last group.
|
|
|
|
*/
|
|
|
|
memset(bitmap, 0xffU, bitmap_size(binfo));
|
|
|
|
extra = (BITMAP_GROUP_NBITS - (binfo->nbits & BITMAP_GROUP_NBITS_MASK))
|
|
|
|
& BITMAP_GROUP_NBITS_MASK;
|
|
|
|
if (extra != 0) {
|
|
|
|
bitmap[binfo->levels[1].group_offset - 1] >>= extra;
|
|
|
|
}
|
|
|
|
for (i = 1; i < binfo->nlevels; i++) {
|
|
|
|
size_t group_count = binfo->levels[i].group_offset -
|
|
|
|
binfo->levels[i-1].group_offset;
|
|
|
|
extra = (BITMAP_GROUP_NBITS - (group_count &
|
|
|
|
BITMAP_GROUP_NBITS_MASK)) & BITMAP_GROUP_NBITS_MASK;
|
|
|
|
if (extra != 0) {
|
|
|
|
bitmap[binfo->levels[i+1].group_offset - 1] >>= extra;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#else /* BITMAP_USE_TREE */
|
|
|
|
|
2016-02-25 00:04:43 +08:00
|
|
|
void
|
2017-01-16 08:56:30 +08:00
|
|
|
bitmap_info_init(bitmap_info_t *binfo, size_t nbits) {
|
2016-02-25 00:04:43 +08:00
|
|
|
assert(nbits > 0);
|
|
|
|
assert(nbits <= (ZU(1) << LG_BITMAP_MAXBITS));
|
|
|
|
|
2016-04-07 01:38:47 +08:00
|
|
|
binfo->ngroups = BITMAP_BITS2GROUPS(nbits);
|
2016-02-25 00:04:43 +08:00
|
|
|
binfo->nbits = nbits;
|
|
|
|
}
|
|
|
|
|
2016-02-27 05:59:41 +08:00
|
|
|
static size_t
|
2017-01-16 08:56:30 +08:00
|
|
|
bitmap_info_ngroups(const bitmap_info_t *binfo) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return binfo->ngroups;
|
2016-02-27 05:59:41 +08:00
|
|
|
}
|
|
|
|
|
2016-02-25 00:04:43 +08:00
|
|
|
void
|
2017-03-24 08:59:47 +08:00
|
|
|
bitmap_init(bitmap_t *bitmap, const bitmap_info_t *binfo, bool fill) {
|
2016-02-25 00:04:43 +08:00
|
|
|
size_t extra;
|
|
|
|
|
2017-03-24 08:59:47 +08:00
|
|
|
if (fill) {
|
|
|
|
memset(bitmap, 0, bitmap_size(binfo));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-02-25 00:04:43 +08:00
|
|
|
memset(bitmap, 0xffU, bitmap_size(binfo));
|
2016-04-07 01:38:47 +08:00
|
|
|
extra = (BITMAP_GROUP_NBITS - (binfo->nbits & BITMAP_GROUP_NBITS_MASK))
|
|
|
|
& BITMAP_GROUP_NBITS_MASK;
|
2017-01-16 08:56:30 +08:00
|
|
|
if (extra != 0) {
|
2016-04-07 01:38:47 +08:00
|
|
|
bitmap[binfo->ngroups - 1] >>= extra;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2016-02-25 00:04:43 +08:00
|
|
|
}
|
|
|
|
|
2017-04-17 00:25:56 +08:00
|
|
|
#endif /* BITMAP_USE_TREE */
|
|
|
|
|
2016-02-27 05:59:41 +08:00
|
|
|
size_t
|
2017-01-16 08:56:30 +08:00
|
|
|
bitmap_size(const bitmap_info_t *binfo) {
|
2016-02-27 05:59:41 +08:00
|
|
|
return (bitmap_info_ngroups(binfo) << LG_SIZEOF_BITMAP);
|
|
|
|
}
|