From: Robert Haas Date: Wed, 4 Jun 2014 17:42:50 +0000 (-0400) Subject: Better integration for BlockAllocatorGetChunkSpace. X-Git-Url: http://git.postgresql.org/gitweb/static/developers.postgresql.org?a=commitdiff_plain;h=0e9c305276d96c1b327bc225f0d1d0df227c48c4;p=users%2Frhaas%2Fpostgres.git Better integration for BlockAllocatorGetChunkSpace. --- diff --git a/src/backend/utils/mmgr/aregion.c b/src/backend/utils/mmgr/aregion.c index f40c46782d..029ffd023e 100644 --- a/src/backend/utils/mmgr/aregion.c +++ b/src/backend/utils/mmgr/aregion.c @@ -105,7 +105,7 @@ static dlist_head private_freelist[NUM_PRIVATE_FREELISTS]; #define AREGION_INITSIZE (16 * BLOCK_ALLOCATOR_PAGES_PER_CHUNK) #define AREGION_MAXSIZE ((64 * 1024 * 1024) / FPM_PAGE_SIZE) -static Size aregion_private_pages_allocated = 0; +Size aregion_private_pages_allocated = 0; static Size aregion_private_bytes_allocated = 0; static Size aregion_peak_private_bytes_allocated = 0; diff --git a/src/backend/utils/mmgr/balloc.c b/src/backend/utils/mmgr/balloc.c index 16e5763210..2c54cb9b30 100644 --- a/src/backend/utils/mmgr/balloc.c +++ b/src/backend/utils/mmgr/balloc.c @@ -539,19 +539,17 @@ BlockAllocatorGetAllocSpace(Size size) * overhead of its own. */ Size -BlockAllocatorGetChunkSpace(void *ptr) +BlockAllocatorGetChunkSpace(AllocatorRegion *aregion, void *ptr) { - AllocatorRegion *region; char *fpm_base; BlockAllocatorSpan *span; Size pageno; uint16 size_class; /* Locate the containing block. */ - region = LookupAllocatorRegion(ptr); - fpm_base = fpm_segment_base(region->fpm); + fpm_base = fpm_segment_base(aregion->fpm); pageno = fpm_pointer_to_page(fpm_base, ptr); - span = BlockAllocatorMapGet(region->pagemap, pageno); + span = BlockAllocatorMapGet(aregion->pagemap, pageno); /* Work out the size of the allocation. */ size_class = span->size_class; diff --git a/src/backend/utils/mmgr/mcxt.c b/src/backend/utils/mmgr/mcxt.c index 4185a03e9f..7b1cc74a46 100644 --- a/src/backend/utils/mmgr/mcxt.c +++ b/src/backend/utils/mmgr/mcxt.c @@ -25,6 +25,7 @@ #include "postgres.h" #include "miscadmin.h" +#include "utils/aregion.h" #include "utils/memdebug.h" #include "utils/memutils.h" @@ -344,6 +345,20 @@ GetMemoryChunkSpace(void *pointer) Assert(pointer != NULL); Assert(pointer == (void *) MAXALIGN(pointer)); + /* + * If allocator regions are in use, then this pointer might be within + * such a region, in which case it won't have a chunk header. So, we have + * to test for and handle that case first. + */ + if (aregion_private_pages_allocated > 0) + { + AllocatorRegion *aregion; + + aregion = LookupAllocatorRegion(pointer); + if (aregion != NULL) + return BlockAllocatorGetChunkSpace(aregion, pointer); + } + /* * OK, it's probably safe to look at the chunk header. */ diff --git a/src/backend/utils/sort/tuplesort.c b/src/backend/utils/sort/tuplesort.c index f3200fb33d..ad88d1a8e1 100644 --- a/src/backend/utils/sort/tuplesort.c +++ b/src/backend/utils/sort/tuplesort.c @@ -3377,7 +3377,7 @@ writetup_index(Tuplesortstate *state, int tapenum, SortTuple *stup) LogicalTapeWrite(state->tapeset, tapenum, (void *) &tuplen, sizeof(tuplen)); - FREEMEM(state, BlockAllocatorGetChunkSpace(tuple)); + FREEMEM(state, GetMemoryChunkSpace(tuple)); BlockAllocatorFree(tuple); } @@ -3389,7 +3389,7 @@ readtup_index(Tuplesortstate *state, SortTuple *stup, IndexTuple tuple; tuple = (IndexTuple) BlockAllocatorAlloc(state->sortallocator, tuplen); - USEMEM(state, BlockAllocatorGetChunkSpace(tuple)); + USEMEM(state, GetMemoryChunkSpace(tuple)); LogicalTapeReadExact(state->tapeset, tapenum, tuple, tuplen); if (state->randomAccess) /* need trailing length word? */ diff --git a/src/include/utils/aregion.h b/src/include/utils/aregion.h index 2309a38a27..a9d9d4cc13 100644 --- a/src/include/utils/aregion.h +++ b/src/include/utils/aregion.h @@ -67,4 +67,6 @@ extern AllocatorRegion *LookupAllocatorRegion(void *); extern AllocatorRegion *GetRegionForPrivateAllocation(Size npages); extern void ReportRegionContiguousFreespace(AllocatorRegion *, Size npages); +extern Size aregion_private_pages_allocated; + #endif /* AREGION_H */ diff --git a/src/include/utils/balloc.h b/src/include/utils/balloc.h index e970349a20..7758b778ea 100644 --- a/src/include/utils/balloc.h +++ b/src/include/utils/balloc.h @@ -17,6 +17,7 @@ #include "storage/lwlock.h" #include "utils/relptr.h" +struct AllocatorRegion; typedef struct BlockAllocatorContext BlockAllocatorContext; /* Number of pages (see FPM_PAGE_SIZE) per block-allocator chunk. */ @@ -33,7 +34,7 @@ extern void BlockAllocatorFree(void *ptr); /* WRONG SIGNATURE */ extern void BlockAllocatorInit(MemoryContext); extern void BlockAllocatorReset(MemoryContext); extern void BlockAllocatorDelete(MemoryContext); -extern Size BlockAllocatorGetChunkSpace(void *ptr); /* WRONG SIGNATURE */ +extern Size BlockAllocatorGetChunkSpace(struct AllocatorRegion *, void *ptr); extern Size BlockAllocatorGetAllocSpace(Size size); /* EXTRA FUNCTION */ /* IS_EMPTY IS MISSING! */ /* STATS IS MISSING! */