Skip to content

Commit 9c59cec

Browse files
committed
[libpas] Specialize page_sharing_pool expirations per participant kind
https://bugs.webkit.org/show_bug.cgi?id=328091 rdar://190441642 Reviewed by Yusuke Suzuki. Previously, participants registered with a page sharing pool ‘expired’ (i.e. become eligible for non-pressure decommit) when their oldest freed memory had been free for at least pas_scavenger_max_epoch_delta. This was static across participant kinds, which means that e.g. small segregated pages would expire at the same rate as large pages. This was less than ideal, since this epoch has proven to be material in benchmark performance, especially Speedometer3. But increasing it across the board is bad for our process’ RSS, esp. for larger size-categories. This patch gets around that by making it possible to specialize the epoch delta by participant-kind, so that e.g. small-segregated pages can be allowed to stay committed for longer than marge-bitfit ones. It doesn't actually set any of the epoch offsets: this will be done in a followon patch. New tests in IsoHeapPageSharingTests. * Source/bmalloc/libpas/src/libpas/pas_page_sharing_participant.c: (apply_epoch_offset): (pas_page_sharing_participant_get_use_epoch): * Source/bmalloc/libpas/src/libpas/pas_page_sharing_participant.h: * Source/bmalloc/libpas/src/test/IsoHeapPageSharingTests.cpp: (std::testSegregatedVariantEpochOffsetsDelayDecommit): (std::testLargeSharingPoolEpochOffsetDelaysDecommit): (std::addAllTests): Canonical link: https://commits.webkit.org/324158@main
1 parent e174317 commit 9c59cec

3 files changed

Lines changed: 175 additions & 6 deletions

File tree

‎Source/bmalloc/libpas/src/libpas/pas_page_sharing_participant.c‎

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,17 @@
3030
#include "pas_page_sharing_participant.h"
3131

3232
#include "pas_bitfit_directory.h"
33+
#include "pas_bitfit_page_config_kind.h"
3334
#include "pas_epoch.h"
3435
#include "pas_heap_lock.h"
3536
#include "pas_large_sharing_pool.h"
3637
#include "pas_page_sharing_pool.h"
38+
#include "pas_segregated_page_config_kind.h"
3739
#include "pas_segregated_size_directory.h"
38-
#include "pas_segregated_size_directory.h"
40+
41+
uint64_t pas_segregated_participant_epoch_offsets[PAS_NUM_SEGREGATED_PAGE_CONFIG_VARIANTS];
42+
uint64_t pas_bitfit_participant_epoch_offsets[PAS_NUM_BITFIT_PAGE_CONFIG_VARIANTS];
43+
uint64_t pas_page_sharing_participant_large_sharing_pool_epoch_offset;
3944

4045
pas_page_sharing_participant
4146
pas_page_sharing_participant_create(void* ptr,
@@ -91,19 +96,46 @@ void pas_page_sharing_participant_payload_with_use_epoch_construct(
9196
*payload = PAS_PAGE_SHARING_PARTICIPANT_PAYLOAD_WITH_USE_EPOCH_INITIALIZER;
9297
}
9398

99+
static uint64_t apply_epoch_offset(uint64_t epoch, uint64_t offset)
100+
{
101+
uint64_t result;
102+
103+
/* Zero means that there is no free memory, so it must not be offset. */
104+
if (epoch == PAS_EPOCH_INVALID)
105+
return PAS_EPOCH_INVALID;
106+
107+
if (__builtin_add_overflow(epoch, offset, &result))
108+
return PAS_EPOCH_MAX;
109+
return result;
110+
}
111+
94112
uint64_t pas_page_sharing_participant_get_use_epoch(pas_page_sharing_participant participant)
95113
{
96114
void* ptr = pas_page_sharing_participant_get_ptr(participant);
97115
switch (pas_page_sharing_participant_get_kind(participant)) {
98116
case pas_page_sharing_participant_null:
99117
PAS_ASSERT(!"Null participant has no use epoch.");
100118
return 0;
101-
case pas_page_sharing_participant_segregated_size_directory:
102-
return pas_segregated_directory_get_use_epoch(ptr);
103-
case pas_page_sharing_participant_bitfit_directory:
104-
return pas_bitfit_directory_get_use_epoch(ptr);
119+
case pas_page_sharing_participant_segregated_size_directory: {
120+
pas_segregated_directory* directory = ptr;
121+
const pas_segregated_page_config* config =
122+
pas_segregated_page_config_kind_get_config(directory->page_config_kind);
123+
return apply_epoch_offset(
124+
pas_segregated_directory_get_use_epoch(directory),
125+
pas_segregated_participant_epoch_offsets[config->variant]);
126+
}
127+
case pas_page_sharing_participant_bitfit_directory: {
128+
pas_bitfit_directory* directory = ptr;
129+
const pas_bitfit_page_config* config =
130+
pas_bitfit_page_config_kind_get_config(directory->config_kind);
131+
return apply_epoch_offset(
132+
pas_bitfit_directory_get_use_epoch(directory),
133+
pas_bitfit_participant_epoch_offsets[config->variant]);
134+
}
105135
case pas_page_sharing_participant_large_sharing_pool:
106-
return pas_large_sharing_participant_payload.use_epoch;
136+
return apply_epoch_offset(
137+
pas_large_sharing_participant_payload.use_epoch,
138+
pas_page_sharing_participant_large_sharing_pool_epoch_offset);
107139
}
108140
PAS_ASSERT(!"Bad participant kind");
109141
return 0;

‎Source/bmalloc/libpas/src/libpas/pas_page_sharing_participant.h‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,12 @@
2727
#define PAS_PAGE_SHARING_PARTICIPANT_H
2828

2929
#include "pas_config.h"
30+
31+
#include "pas_bitfit_page_config_variant.h"
3032
#include "pas_lock.h"
3133
#include "pas_page_sharing_participant_kind.h"
3234
#include "pas_page_sharing_pool_take_result.h"
35+
#include "pas_segregated_page_config_variant.h"
3336
#include "pas_utils.h"
3437

3538
PAS_BEGIN_EXTERN_C;
@@ -116,6 +119,15 @@ PAS_API void pas_page_sharing_participant_payload_construct(
116119
PAS_API void pas_page_sharing_participant_payload_with_use_epoch_construct(
117120
pas_page_sharing_participant_payload_with_use_epoch* payload);
118121

122+
/* How much longer participant's pages should be allowed to stay committed after becoming free,
123+
relative to the baseline pas_scavenger_max_epoch_delta; measured in epoch units.
124+
These values must be set before any participants are registered with the sharing pool. */
125+
PAS_API extern uint64_t pas_segregated_participant_epoch_offsets[PAS_NUM_SEGREGATED_PAGE_CONFIG_VARIANTS];
126+
PAS_API extern uint64_t pas_bitfit_participant_epoch_offsets[PAS_NUM_BITFIT_PAGE_CONFIG_VARIANTS];
127+
PAS_API extern uint64_t pas_page_sharing_participant_large_sharing_pool_epoch_offset;
128+
129+
/* Returns the epoch at which the participant's oldest free memory expires, or PAS_EPOCH_INVALID if
130+
* it has none. This is the use epoch plus the participant's offset. */
119131
PAS_API uint64_t pas_page_sharing_participant_get_use_epoch(pas_page_sharing_participant participant);
120132

121133
PAS_API void pas_page_sharing_participant_set_parent_pool(

‎Source/bmalloc/libpas/src/test/IsoHeapPageSharingTests.cpp‎

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,14 @@
3737
#include <mutex>
3838
#include "pas_all_heaps.h"
3939
#include "pas_baseline_allocator_table.h"
40+
#include "pas_epoch.h"
4041
#include "pas_heap.h"
4142
#include "pas_heap_for_config.h"
4243
#include "pas_large_free_heap_helpers.h"
4344
#include "pas_large_sharing_pool.h"
4445
#include "pas_large_utility_free_heap.h"
4546
#include "pas_page_malloc.h"
47+
#include "pas_page_sharing_participant.h"
4648
#include "pas_scavenger.h"
4749
#include "pas_segregated_size_directory.h"
4850
#include "pas_thread_local_cache.h"
@@ -4245,6 +4247,127 @@ void testNewEligibleHasOlderEpoch()
42454247
assertOnlyDecommitted({ SmallHeapTwo });
42464248
}
42474249

4250+
// Big enough that adding it to any counter epoch the tests produce puts the participant far past
4251+
// whatever max_epoch the tests scavenge with.
4252+
constexpr uint64_t participantEpochOffset = 1ull << 40;
4253+
4254+
// 1. Free older participant
4255+
// 2. Free newer participant
4256+
// 3. Scavenge
4257+
// Sans epoch offsets, both the older and newer participants would be
4258+
// decommitted. But with an offset placed on the older participant,
4259+
// only the newer participant is decommitted.
4260+
void testSegregatedVariantEpochOffsetsDelayDecommit()
4261+
{
4262+
pas_epoch_is_counter = true;
4263+
setupThingy();
4264+
4265+
pas_segregated_participant_epoch_offsets[pas_small_segregated_page_config_variant] = 0;
4266+
pas_segregated_participant_epoch_offsets[pas_medium_segregated_page_config_variant] = participantEpochOffset;
4267+
4268+
pas_heap_ref smallHeapRef = ISO_HEAP_REF_INITIALIZER(32);
4269+
pas_heap_ref mediumHeapRef = ISO_HEAP_REF_INITIALIZER(4096);
4270+
pas_heap* smallHeap = iso_heap_ref_get_heap(&smallHeapRef);
4271+
pas_heap* mediumHeap = iso_heap_ref_get_heap(&mediumHeapRef);
4272+
4273+
vector<void*> smallObjects;
4274+
vector<void*> mediumObjects;
4275+
for (unsigned i = 10000; i--;)
4276+
smallObjects.push_back(checkThingy(iso_try_allocate(&smallHeapRef)));
4277+
for (unsigned i = 300; i--;)
4278+
mediumObjects.push_back(checkThingy(iso_try_allocate(&mediumHeapRef)));
4279+
4280+
auto variantOf = [&] (void* object) {
4281+
return pas_segregated_page_config_kind_get_config(
4282+
pas_segregated_size_directory_for_object(
4283+
reinterpret_cast<uintptr_t>(object), &iso_heap_config)->base.page_config_kind)->variant;
4284+
};
4285+
CHECK_EQUAL(variantOf(smallObjects[0]), pas_small_segregated_page_config_variant);
4286+
CHECK_EQUAL(variantOf(mediumObjects[0]), pas_medium_segregated_page_config_variant);
4287+
4288+
auto freeAll = [&] (vector<void*>& objects) {
4289+
for (void* object : objects)
4290+
iso_deallocate(object);
4291+
objects.clear();
4292+
pas_thread_local_cache_flush_deallocation_log(pas_thread_local_cache_get(&iso_heap_config),
4293+
pas_lock_is_not_held);
4294+
cleanThingy();
4295+
};
4296+
4297+
// The medium heap goes first, so it has the older raw epoch.
4298+
freeAll(mediumObjects);
4299+
freeAll(smallObjects);
4300+
4301+
auto decommitted = [&] (pas_heap* heap) {
4302+
HeapLocker heapLocker;
4303+
return pas_heap_compute_summary(heap, pas_lock_is_held).decommitted;
4304+
};
4305+
4306+
CHECK_EQUAL(decommitted(smallHeap), 0);
4307+
CHECK_EQUAL(decommitted(mediumHeap), 0);
4308+
4309+
pas_physical_page_sharing_pool_scavenge(pas_get_epoch());
4310+
4311+
CHECK_GREATER(decommitted(smallHeap), 0);
4312+
CHECK_EQUAL(decommitted(mediumHeap), 0);
4313+
4314+
pas_physical_page_sharing_pool_scavenge(pas_get_epoch() + participantEpochOffset);
4315+
4316+
CHECK_GREATER(decommitted(mediumHeap), 0);
4317+
}
4318+
4319+
void testLargeSharingPoolEpochOffsetDelaysDecommit()
4320+
{
4321+
pas_epoch_is_counter = true;
4322+
setupThingy();
4323+
4324+
pas_segregated_participant_epoch_offsets[pas_small_segregated_page_config_variant] = 0;
4325+
pas_page_sharing_participant_large_sharing_pool_epoch_offset = participantEpochOffset;
4326+
4327+
pas_heap_ref smallHeapRef = ISO_HEAP_REF_INITIALIZER(32);
4328+
pas_heap_ref largeHeapRef = ISO_HEAP_REF_INITIALIZER(1000000);
4329+
pas_heap* smallHeap = iso_heap_ref_get_heap(&smallHeapRef);
4330+
pas_heap* largeHeap = iso_heap_ref_get_heap(&largeHeapRef);
4331+
4332+
vector<void*> smallObjects;
4333+
vector<void*> largeObjects;
4334+
for (unsigned i = 10000; i--;)
4335+
smallObjects.push_back(checkThingy(iso_try_allocate(&smallHeapRef)));
4336+
for (unsigned i = 5; i--;)
4337+
largeObjects.push_back(checkThingy(iso_try_allocate(&largeHeapRef)));
4338+
4339+
// The large heap goes first, so it has the older raw epoch.
4340+
for (void* object : largeObjects)
4341+
iso_deallocate(object);
4342+
largeObjects.clear();
4343+
pas_thread_local_cache_flush_deallocation_log(pas_thread_local_cache_get(&iso_heap_config),
4344+
pas_lock_is_not_held);
4345+
cleanThingy();
4346+
for (void* object : smallObjects)
4347+
iso_deallocate(object);
4348+
smallObjects.clear();
4349+
pas_thread_local_cache_flush_deallocation_log(pas_thread_local_cache_get(&iso_heap_config),
4350+
pas_lock_is_not_held);
4351+
cleanThingy();
4352+
4353+
auto decommitted = [&] (pas_heap* heap) {
4354+
HeapLocker heapLocker;
4355+
return pas_heap_compute_summary(heap, pas_lock_is_held).decommitted;
4356+
};
4357+
4358+
CHECK_EQUAL(decommitted(smallHeap), 0);
4359+
CHECK_EQUAL(decommitted(largeHeap), 0);
4360+
4361+
pas_physical_page_sharing_pool_scavenge(pas_get_epoch());
4362+
4363+
CHECK_GREATER(decommitted(smallHeap), 0);
4364+
CHECK_EQUAL(decommitted(largeHeap), 0);
4365+
4366+
pas_physical_page_sharing_pool_scavenge(pas_get_epoch() + participantEpochOffset);
4367+
4368+
CHECK_GREATER(decommitted(largeHeap), 0);
4369+
}
4370+
42484371
pas_heap* scavengerCompletionHeap;
42494372
bool scavengerCompletionExpectDone;
42504373
bool scavengerCompletionIsDone;
@@ -4518,6 +4641,8 @@ void addAllTests()
45184641
}
45194642

45204643
ADD_TEST(testNewEligibleHasOlderEpoch());
4644+
ADD_TEST(testSegregatedVariantEpochOffsetsDelayDecommit());
4645+
ADD_TEST(testLargeSharingPoolEpochOffsetDelaysDecommit());
45214646

45224647
ADD_TEST(testScavengerEventuallyReturnsMemory(128, 1));
45234648
ADD_TEST(testScavengerEventuallyReturnsMemory(128, 10000));

0 commit comments

Comments
 (0)