#include "base/allocator/partition_allocator/thread_cache.h"
#include <sys/types.h>
#include <algorithm>
#include <atomic>
#include <cstdint>
#include "base/allocator/partition_allocator/partition_alloc-inl.h"
#include "base/allocator/partition_allocator/partition_alloc_base/component_export.h"
#include "base/allocator/partition_allocator/partition_alloc_base/cxx17_backports.h"
#include "base/allocator/partition_allocator/partition_alloc_base/debug/debugging_buildflags.h"
#include "base/allocator/partition_allocator/partition_alloc_base/immediate_crash.h"
#include "base/allocator/partition_allocator/partition_alloc_buildflags.h"
#include "base/allocator/partition_allocator/partition_alloc_check.h"
#include "base/allocator/partition_allocator/partition_alloc_config.h"
#include "base/allocator/partition_allocator/partition_alloc_constants.h"
#include "base/allocator/partition_allocator/partition_root.h"
#include "build/build_config.h"
#include "base/allocator/partition_allocator/internal_allocator.h"
namespace partition_alloc {
namespace {
ThreadCacheRegistry g_instance;
}
namespace tools {
uintptr_t kThreadCacheNeedleArray[kThreadCacheNeedleArraySize] = {
kNeedle1, reinterpret_cast<uintptr_t>(&g_instance),
#if BUILDFLAG(RECORD_ALLOC_INFO)
reinterpret_cast<uintptr_t>(&internal::g_allocs),
#else
0,
#endif
kNeedle2};
}
namespace internal {
PA_COMPONENT_EXPORT(PARTITION_ALLOC) PartitionTlsKey g_thread_cache_key;
#if PA_CONFIG(THREAD_CACHE_FAST_TLS)
PA_COMPONENT_EXPORT(PARTITION_ALLOC)
thread_local ThreadCache* g_thread_cache;
#endif
}
namespace {
static std::atomic<PartitionRoot<>*> g_thread_cache_root;
#if BUILDFLAG(IS_WIN)
void OnDllProcessDetach() {
g_thread_cache_root.load(std::memory_order_relaxed)->flags.with_thread_cache =
false;
}
#endif
static bool g_thread_cache_key_created = false;
}
constexpr internal::base::TimeDelta ThreadCacheRegistry::kMinPurgeInterval;
constexpr internal::base::TimeDelta ThreadCacheRegistry::kMaxPurgeInterval;
constexpr internal::base::TimeDelta ThreadCacheRegistry::kDefaultPurgeInterval;
constexpr size_t ThreadCacheRegistry::kMinCachedMemoryForPurging;
uint8_t ThreadCache::global_limits_[ThreadCache::kBucketCount];
uint16_t ThreadCache::largest_active_bucket_index_ =
internal::BucketIndexLookup::GetIndex(ThreadCache::kDefaultSizeThreshold);
ThreadCacheRegistry& ThreadCacheRegistry::Instance() {
return g_instance;
}
void ThreadCacheRegistry::RegisterThreadCache(ThreadCache* cache) {
internal::ScopedGuard scoped_locker(GetLock());
cache->next_ = nullptr;
cache->prev_ = nullptr;
ThreadCache* previous_head = list_head_;
list_head_ = cache;
cache->next_ = previous_head;
if (previous_head) {
previous_head->prev_ = cache;
}
}
void ThreadCacheRegistry::UnregisterThreadCache(ThreadCache* cache) {
internal::ScopedGuard scoped_locker(GetLock());
if (cache->prev_) {
cache->prev_->next_ = cache->next_;
}
if (cache->next_) {
cache->next_->prev_ = cache->prev_;
}
if (cache == list_head_) {
list_head_ = cache->next_;
}
}
void ThreadCacheRegistry::DumpStats(bool my_thread_only,
ThreadCacheStats* stats) {
ThreadCache::EnsureThreadSpecificDataInitialized();
memset(reinterpret_cast<void*>(stats), 0, sizeof(ThreadCacheStats));
internal::ScopedGuard scoped_locker(GetLock());
if (my_thread_only) {
auto* tcache = ThreadCache::Get();
if (!ThreadCache::IsValid(tcache)) {
return;
}
tcache->AccumulateStats(stats);
} else {
ThreadCache* tcache = list_head_;
while (tcache) {
tcache->AccumulateStats(stats);
tcache = tcache->next_;
}
}
}
void ThreadCacheRegistry::PurgeAll() {
auto* current_thread_tcache = ThreadCache::Get();
if (ThreadCache::IsValid(current_thread_tcache)) {
current_thread_tcache->Purge();
}
{
internal::ScopedGuard scoped_locker(GetLock());
ThreadCache* tcache = list_head_;
while (tcache) {
PA_DCHECK(ThreadCache::IsValid(tcache));
if (tcache != current_thread_tcache) {
tcache->SetShouldPurge();
}
tcache = tcache->next_;
}
}
}
void ThreadCacheRegistry::ForcePurgeAllThreadAfterForkUnsafe() {
internal::ScopedGuard scoped_locker(GetLock());
ThreadCache* tcache = list_head_;
while (tcache) {
#if BUILDFLAG(PA_DCHECK_IS_ON)
tcache->is_in_thread_cache_ = false;
#endif
tcache->cached_memory_ = tcache->CachedMemory();
tcache = tcache->next_;
}
}
void ThreadCacheRegistry::SetLargestActiveBucketIndex(
uint8_t largest_active_bucket_index) {
largest_active_bucket_index_ = largest_active_bucket_index;
}
void ThreadCacheRegistry::SetThreadCacheMultiplier(float multiplier) {
{
internal::ScopedGuard scoped_locker(GetLock());
ThreadCache* tcache = list_head_;
if (!tcache) {
return;
}
ThreadCache::SetGlobalLimits(tcache->root_, multiplier);
while (tcache) {
PA_DCHECK(ThreadCache::IsValid(tcache));
for (int index = 0; index < ThreadCache::kBucketCount; index++) {
tcache->buckets_[index].limit.store(ThreadCache::global_limits_[index],
std::memory_order_relaxed);
}
tcache = tcache->next_;
}
}
}
void ThreadCacheRegistry::RunPeriodicPurge() {
if (!periodic_purge_is_initialized_) {
ThreadCache::EnsureThreadSpecificDataInitialized();
periodic_purge_is_initialized_ = true;
}
size_t cached_memory_approx = 0;
{
internal::ScopedGuard scoped_locker(GetLock());
ThreadCache* tcache = list_head_;
if (!tcache) {
return;
}
while (tcache) {
cached_memory_approx += tcache->cached_memory_;
tcache = tcache->next_;
}
}
if (cached_memory_approx > 10 * kMinCachedMemoryForPurging) {
periodic_purge_next_interval_ =
std::min(kDefaultPurgeInterval, periodic_purge_next_interval_ / 2);
} else if (cached_memory_approx > 2 * kMinCachedMemoryForPurging) {
periodic_purge_next_interval_ =
std::max(kMinPurgeInterval, periodic_purge_next_interval_ / 2);
} else if (cached_memory_approx < kMinCachedMemoryForPurging) {
periodic_purge_next_interval_ =
std::min(kMaxPurgeInterval, periodic_purge_next_interval_ * 2);
}
periodic_purge_next_interval_ = internal::base::clamp(
periodic_purge_next_interval_, kMinPurgeInterval, kMaxPurgeInterval);
PurgeAll();
}
int64_t ThreadCacheRegistry::GetPeriodicPurgeNextIntervalInMicroseconds()
const {
return periodic_purge_next_interval_.InMicroseconds();
}
void ThreadCacheRegistry::ResetForTesting() {
periodic_purge_next_interval_ = kDefaultPurgeInterval;
}
void ThreadCache::EnsureThreadSpecificDataInitialized() {
internal::ScopedGuard scoped_locker(
ThreadCacheRegistry::Instance().GetLock());
if (g_thread_cache_key_created) {
return;
}
bool ok = internal::PartitionTlsCreate(&internal::g_thread_cache_key, Delete);
PA_CHECK(ok);
g_thread_cache_key_created = true;
}
void ThreadCache::DeleteForTesting(ThreadCache* tcache) {
ThreadCache::Delete(tcache);
}
void ThreadCache::SwapForTesting(PartitionRoot<>* root) {
auto* old_tcache = ThreadCache::Get();
g_thread_cache_root.store(nullptr, std::memory_order_relaxed);
if (old_tcache) {
ThreadCache::DeleteForTesting(old_tcache);
}
if (root) {
Init(root);
Create(root);
} else {
#if BUILDFLAG(IS_WIN)
internal::PartitionTlsSetOnDllProcessDetach(nullptr);
#endif
}
}
void ThreadCache::RemoveTombstoneForTesting() {
PA_CHECK(IsTombstone(Get()));
internal::PartitionTlsSet(internal::g_thread_cache_key, nullptr);
}
void ThreadCache::Init(PartitionRoot<>* root) {
#if BUILDFLAG(IS_NACL)
static_assert(false, "PartitionAlloc isn't supported for NaCl");
#endif
PA_CHECK(root->buckets[kBucketCount - 1].slot_size ==
ThreadCache::kLargeSizeThreshold);
PA_CHECK(root->buckets[largest_active_bucket_index_].slot_size ==
ThreadCache::kDefaultSizeThreshold);
EnsureThreadSpecificDataInitialized();
PartitionRoot<>* expected = nullptr;
if (!g_thread_cache_root.compare_exchange_strong(expected, root,
std::memory_order_seq_cst,
std::memory_order_seq_cst)) {
PA_CHECK(false)
<< "Only one PartitionRoot is allowed to have a thread cache";
}
#if BUILDFLAG(IS_WIN)
internal::PartitionTlsSetOnDllProcessDetach(OnDllProcessDetach);
#endif
SetGlobalLimits(root, kDefaultMultiplier);
}
void ThreadCache::SetGlobalLimits(PartitionRoot<>* root, float multiplier) {
size_t initial_value =
static_cast<size_t>(kSmallBucketBaseCount) * multiplier;
for (int index = 0; index < kBucketCount; index++) {
const auto& root_bucket = root->buckets[index];
if (!root_bucket.active_slot_spans_head) {
global_limits_[index] = 0;
continue;
}
size_t slot_size = root_bucket.slot_size;
size_t value;
if (slot_size <= 128) {
value = initial_value;
} else if (slot_size <= 256) {
value = initial_value / 2;
} else if (slot_size <= 512) {
value = initial_value / 4;
} else {
value = initial_value / 8;
}
constexpr size_t kMinLimit = 1;
constexpr size_t kMaxLimit = std::numeric_limits<uint8_t>::max() - 1;
global_limits_[index] = static_cast<uint8_t>(
internal::base::clamp(value, kMinLimit, kMaxLimit));
PA_DCHECK(global_limits_[index] >= kMinLimit);
PA_DCHECK(global_limits_[index] <= kMaxLimit);
}
}
void ThreadCache::SetLargestCachedSize(size_t size) {
if (size > ThreadCache::kLargeSizeThreshold) {
size = ThreadCache::kLargeSizeThreshold;
}
largest_active_bucket_index_ =
PartitionRoot<internal::ThreadSafe>::SizeToBucketIndex(
size,
PartitionRoot<internal::ThreadSafe>::BucketDistribution::kDefault);
PA_CHECK(largest_active_bucket_index_ < kBucketCount);
ThreadCacheRegistry::Instance().SetLargestActiveBucketIndex(
largest_active_bucket_index_);
}
ThreadCache* ThreadCache::Create(PartitionRoot<internal::ThreadSafe>* root) {
PA_CHECK(root);
PA_CHECK(tools::kThreadCacheNeedleArray[0] == tools::kNeedle1);
ThreadCache* tcache = new ThreadCache(root);
internal::PartitionTlsSet(internal::g_thread_cache_key, tcache);
#if PA_CONFIG(THREAD_CACHE_FAST_TLS)
internal::g_thread_cache = tcache;
#endif
return tcache;
}
ThreadCache::ThreadCache(PartitionRoot<>* root)
: should_purge_(false),
root_(root),
thread_id_(internal::base::PlatformThread::CurrentId()),
next_(nullptr),
prev_(nullptr) {
ThreadCacheRegistry::Instance().RegisterThreadCache(this);
memset(&stats_, 0, sizeof(stats_));
for (int index = 0; index < kBucketCount; index++) {
const auto& root_bucket = root->buckets[index];
Bucket* tcache_bucket = &buckets_[index];
tcache_bucket->freelist_head = nullptr;
tcache_bucket->count = 0;
tcache_bucket->limit.store(global_limits_[index],
std::memory_order_relaxed);
tcache_bucket->slot_size = root_bucket.slot_size;
if (!root_bucket.is_valid()) {
tcache_bucket->limit.store(0, std::memory_order_relaxed);
}
}
}
ThreadCache::~ThreadCache() {
ThreadCacheRegistry::Instance().UnregisterThreadCache(this);
Purge();
}
void ThreadCache::Delete(void* tcache_ptr) {
auto* tcache = static_cast<ThreadCache*>(tcache_ptr);
if (!IsValid(tcache)) {
return;
}
#if PA_CONFIG(THREAD_CACHE_FAST_TLS)
internal::g_thread_cache = nullptr;
#else
internal::PartitionTlsSet(internal::g_thread_cache_key, nullptr);
#endif
delete tcache;
#if BUILDFLAG(IS_WIN)
internal::PartitionTlsSet(internal::g_thread_cache_key,
reinterpret_cast<void*>(kTombstone));
#if PA_CONFIG(THREAD_CACHE_FAST_TLS)
internal::g_thread_cache = reinterpret_cast<ThreadCache*>(kTombstone);
#endif
#endif
}
ThreadCache::Bucket::Bucket() {
limit.store(0, std::memory_order_relaxed);
}
void ThreadCache::FillBucket(size_t bucket_index) {
PA_INCREMENT_COUNTER(stats_.batch_fill_count);
Bucket& bucket = buckets_[bucket_index];
int count = std::max(
1, bucket.limit.load(std::memory_order_relaxed) / kBatchFillRatio);
size_t usable_size;
bool is_already_zeroed;
PA_DCHECK(!root_->buckets[bucket_index].CanStoreRawSize());
PA_DCHECK(!root_->buckets[bucket_index].is_direct_mapped());
size_t allocated_slots = 0;
internal::ScopedGuard guard(root_->lock_);
for (int i = 0; i < count; i++) {
uintptr_t slot_start = root_->AllocFromBucket(
&root_->buckets[bucket_index],
AllocFlags::kFastPathOrReturnNull | AllocFlags::kReturnNull,
root_->buckets[bucket_index].slot_size ,
internal::PartitionPageSize(), &usable_size, &is_already_zeroed);
if (!slot_start) {
break;
}
allocated_slots++;
PutInBucket(bucket, slot_start);
}
cached_memory_ += allocated_slots * bucket.slot_size;
}
void ThreadCache::ClearBucket(Bucket& bucket, size_t limit) {
ClearBucketHelper<true>(bucket, limit);
}
template <bool crash_on_corruption>
void ThreadCache::ClearBucketHelper(Bucket& bucket, size_t limit) {
if (!bucket.count || bucket.count <= limit) {
return;
}
if constexpr (crash_on_corruption) {
bucket.freelist_head->CheckFreeListForThreadCache(bucket.slot_size);
}
uint8_t count_before = bucket.count;
if (limit == 0) {
FreeAfter<crash_on_corruption>(bucket.freelist_head, bucket.slot_size);
bucket.freelist_head = nullptr;
} else {
auto* head = bucket.freelist_head;
size_t items = 1;
while (items < limit) {
head = head->GetNextForThreadCache<crash_on_corruption>(bucket.slot_size);
items++;
}
FreeAfter<crash_on_corruption>(
head->GetNextForThreadCache<crash_on_corruption>(bucket.slot_size),
bucket.slot_size);
head->SetNext(nullptr);
}
bucket.count = limit;
uint8_t count_after = bucket.count;
size_t freed_memory = (count_before - count_after) * bucket.slot_size;
PA_DCHECK(cached_memory_ >= freed_memory);
cached_memory_ -= freed_memory;
PA_DCHECK(cached_memory_ == CachedMemory());
}
void* ThreadCache::operator new(size_t count) {
return InternalAllocatorRoot().Alloc(count, "ThreadCachePA");
}
void ThreadCache::operator delete(void* ptr) {
InternalAllocatorRoot().Free(ptr);
}
template <bool crash_on_corruption>
void ThreadCache::FreeAfter(internal::PartitionFreelistEntry* head,
size_t slot_size) {
internal::ScopedGuard guard(root_->lock_);
while (head) {
uintptr_t slot_start = internal::SlotStartPtr2Addr(head);
head = head->GetNextForThreadCache<crash_on_corruption>(slot_size);
root_->RawFreeLocked(slot_start);
}
}
void ThreadCache::ResetForTesting() {
stats_.alloc_count = 0;
stats_.alloc_hits = 0;
stats_.alloc_misses = 0;
stats_.alloc_miss_empty = 0;
stats_.alloc_miss_too_large = 0;
stats_.cache_fill_count = 0;
stats_.cache_fill_hits = 0;
stats_.cache_fill_misses = 0;
stats_.batch_fill_count = 0;
stats_.bucket_total_memory = 0;
stats_.metadata_overhead = 0;
Purge();
PA_CHECK(cached_memory_ == 0u);
should_purge_.store(false, std::memory_order_relaxed);
}
size_t ThreadCache::CachedMemory() const {
size_t total = 0;
for (const Bucket& bucket : buckets_) {
total += bucket.count * static_cast<size_t>(bucket.slot_size);
}
return total;
}
void ThreadCache::AccumulateStats(ThreadCacheStats* stats) const {
stats->alloc_count += stats_.alloc_count;
stats->alloc_hits += stats_.alloc_hits;
stats->alloc_misses += stats_.alloc_misses;
stats->alloc_miss_empty += stats_.alloc_miss_empty;
stats->alloc_miss_too_large += stats_.alloc_miss_too_large;
stats->cache_fill_count += stats_.cache_fill_count;
stats->cache_fill_hits += stats_.cache_fill_hits;
stats->cache_fill_misses += stats_.cache_fill_misses;
stats->batch_fill_count += stats_.batch_fill_count;
#if PA_CONFIG(THREAD_CACHE_ALLOC_STATS)
for (size_t i = 0; i < internal::kNumBuckets + 1; i++) {
stats->allocs_per_bucket_[i] += stats_.allocs_per_bucket_[i];
}
#endif
stats->bucket_total_memory += cached_memory_;
stats->metadata_overhead += sizeof(*this);
}
void ThreadCache::SetShouldPurge() {
should_purge_.store(true, std::memory_order_relaxed);
}
void ThreadCache::Purge() {
PA_REENTRANCY_GUARD(is_in_thread_cache_);
PurgeInternal();
}
void ThreadCache::TryPurge() {
PA_REENTRANCY_GUARD(is_in_thread_cache_);
PurgeInternalHelper<false>();
}
void ThreadCache::PurgeCurrentThread() {
auto* tcache = Get();
if (IsValid(tcache)) {
tcache->Purge();
}
}
void ThreadCache::PurgeInternal() {
PurgeInternalHelper<true>();
}
void ThreadCache::ResetPerThreadAllocationStatsForTesting() {
thread_alloc_stats_ = {};
}
template <bool crash_on_corruption>
void ThreadCache::PurgeInternalHelper() {
should_purge_.store(false, std::memory_order_relaxed);
for (auto& bucket : buckets_) {
ClearBucketHelper<crash_on_corruption>(bucket, 0);
}
}
}