Skip to content

Commit da5d2b4

Browse files
mikeashairspeedswift
authored andcommitted
[Runtime] Clean up metadata allocation tracking.
Keep the initial metadata pool to be all zeroes. Remove the hardcoded trailer at the end. Write a trailer into it when we check the environment variable the first time we allocate.
1 parent 8804636 commit da5d2b4

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

stdlib/public/runtime/Metadata.cpp

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5471,13 +5471,17 @@ namespace {
54715471
size_t Remaining;
54725472
};
54735473

5474+
/// The trailer placed at the end of each pool allocation, used when
5475+
/// SWIFT_DEBUG_ENABLE_METADATA_ALLOCATION_ITERATION is on.
54745476
struct PoolTrailer {
54755477
void *PrevTrailer;
54765478
size_t PoolSize;
54775479
};
54785480

5479-
static constexpr size_t InitialPoolSize = 64 * 1024 - sizeof(PoolTrailer);
5481+
static constexpr size_t InitialPoolSize = 64 * 1024;
54805482

5483+
/// The header placed before each allocation, used when
5484+
/// SWIFT_DEBUG_ENABLE_METADATA_ALLOCATION_ITERATION is on.
54815485
struct alignas(void *) AllocationHeader {
54825486
uint16_t Size;
54835487
uint16_t Tag;
@@ -5488,7 +5492,6 @@ namespace {
54885492
// doesn't cost us anything in binary size.
54895493
alignas(void *) static struct {
54905494
char Pool[InitialPoolSize];
5491-
PoolTrailer Trailer{ nullptr, InitialPoolSize };
54925495
} InitialAllocationPool;
54935496
static std::atomic<PoolRange>
54945497
AllocationPool{PoolRange{InitialAllocationPool.Pool,
@@ -5497,18 +5500,31 @@ AllocationPool{PoolRange{InitialAllocationPool.Pool,
54975500
bool swift::_swift_debug_metadataAllocationIterationEnabled = false;
54985501
const void * const swift::_swift_debug_allocationPoolPointer = &AllocationPool;
54995502

5503+
static void checkAllocatorDebugEnvironmentVariable(void *context) {
5504+
const char *value =
5505+
getenv("SWIFT_DEBUG_ENABLE_METADATA_ALLOCATION_ITERATION");
5506+
if (value && (value[0] == '1' || value[0] == 'y' || value[0] == 'Y')) {
5507+
_swift_debug_metadataAllocationIterationEnabled = true;
5508+
// Write a PoolTrailer to the end of InitialAllocationPool and shrink
5509+
// the pool accordingly.
5510+
auto poolCopy = AllocationPool.load(std::memory_order_relaxed);
5511+
assert(poolCopy.Begin == InitialAllocationPool.Pool);
5512+
size_t newPoolSize = InitialPoolSize - sizeof(PoolTrailer);
5513+
PoolTrailer trailer = { nullptr, newPoolSize };
5514+
memcpy(InitialAllocationPool.Pool + newPoolSize, &trailer,
5515+
sizeof(trailer));
5516+
poolCopy.Remaining = newPoolSize;
5517+
AllocationPool.store(poolCopy, std::memory_order_relaxed);
5518+
}
5519+
}
5520+
55005521
void *MetadataAllocator::Allocate(size_t size, size_t alignment) {
55015522
assert(Tag != 0);
55025523
assert(alignment <= alignof(void*));
55035524
assert(size % alignof(void*) == 0);
55045525

55055526
static OnceToken_t getenvToken;
5506-
SWIFT_ONCE_F(getenvToken, [](void *) {
5507-
const char *value =
5508-
getenv("SWIFT_DEBUG_ENABLE_METADATA_ALLOCATION_ITERATION");
5509-
if (value && (value[0] == '1' || value[0] == 'y' || value[0] == 'Y'))
5510-
_swift_debug_metadataAllocationIterationEnabled = true;
5511-
}, nullptr);
5527+
SWIFT_ONCE_F(getenvToken, checkAllocatorDebugEnvironmentVariable, nullptr);
55125528

55135529
// If the size is larger than the maximum, just use malloc.
55145530
if (size > PoolRange::MaxPoolAllocationSize)

0 commit comments

Comments
 (0)