Commit 16a22edb authored by Benoit Lize's avatar Benoit Lize Committed by Chromium LUCI CQ

[PA-E] Disable the thread cache on low-end Android at runtime.

PartitionAlloc's thread cache improves performance (up to ~3% on
speedometer 2.0 vs jemalloc) on Android Go, but at a memory cost. Since
periodic purge is still disabled, this commit disables the thread cache
on these configurations to recover.

This CL introduces extended_api.h for PartitionAlloc, which is intended
to become the central place to provide finer-grained control when PA-E
is enabled, such as adjusting thread cache behavior (size, reclaiming
behavior, or disabling it), and other allocator tuning parameters.

As such, even if complete thread cache disabling is intended to be
temporary, having an extended API is not.

Bug: 1167218, 998048
Change-Id: I9c9801e41f1a0ed4dab6b3d96647540356428a50
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2637640
Commit-Queue: Benoit L <lizeb@chromium.org>
Reviewed-by: default avatarBartek Nowierski <bartekn@chromium.org>
Reviewed-by: default avatarArthur Sonzogni <arthursonzogni@chromium.org>
Reviewed-by: default avatarKentaro Hara <haraken@chromium.org>
Cr-Commit-Position: refs/heads/master@{#846097}
parent b373a47d
......@@ -1780,6 +1780,8 @@ component("base") {
"allocator/partition_allocator/address_space_randomization.cc",
"allocator/partition_allocator/address_space_randomization.h",
"allocator/partition_allocator/checked_ptr_support.h",
"allocator/partition_allocator/extended_api.cc",
"allocator/partition_allocator/extended_api.h",
"allocator/partition_allocator/memory_reclaimer.cc",
"allocator/partition_allocator/memory_reclaimer.h",
"allocator/partition_allocator/object_bitmap.h",
......
// Copyright 2021 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/allocator/partition_allocator/extended_api.h"
#include "base/allocator/allocator_shim_default_dispatch_to_partition_alloc.h"
#include "base/allocator/buildflags.h"
#include "base/allocator/partition_allocator/thread_cache.h"
namespace base {
void DisablePartitionAllocThreadCacheForProcess() {
#if BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC)
auto* root = base::internal::PartitionAllocMalloc::Allocator();
// Some platforms don't have a thread cache, or it could already have been
// disabled.
if (root->with_thread_cache) {
internal::ThreadCacheRegistry::Instance().PurgeAll();
root->with_thread_cache = false;
// Doesn't destroy the thread cache object(s). For background threads, they
// will be collected (and free cached memory) at thread destruction
// time. For the main thread, we leak it.
}
#endif // BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC)
}
} // namespace base
// Copyright 2021 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef BASE_ALLOCATOR_PARTITION_ALLOCATOR_EXTENDED_API_H_
#define BASE_ALLOCATOR_PARTITION_ALLOCATOR_EXTENDED_API_H_
#include "base/base_export.h"
namespace base {
// Disables the thread cache for the entire process.
//
// Saves memory but slows down the allocator *significantly*. Only use for
// configurations that are very memory-constrained or performance-insensitive
// processes.
//
// Must preferably be called from the main thread, when no/few threads have
// been started.
//
// Otherwise, there are several things that can happen:
// 1. Another thread is currently temporarily disabling the thread cache, and
// will re-enable it, negating this call's effect.
// 2. Other threads' caches cannot be purged from here, and would retain their
// cached memory until thread destruction (where it is reclaimed).
//
// These are not correctness issues, at worst in the first case, memory is not
// saved, and in the second one, *some* of the memory is leaked.
BASE_EXPORT void DisablePartitionAllocThreadCacheForProcess();
} // namespace base
#endif // BASE_ALLOCATOR_PARTITION_ALLOCATOR_EXTENDED_API_H_
......@@ -168,11 +168,13 @@
#endif
#if defined(OS_ANDROID)
#include "base/system/sys_info.h"
#include "content/browser/android/browser_startup_controller.h"
#include "content/common/android/cpu_affinity.h"
#endif
#if BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC)
#include "base/allocator/partition_allocator/extended_api.h"
#include "base/allocator/partition_allocator/thread_cache.h"
#endif
......@@ -922,6 +924,13 @@ int ContentMainRunnerImpl::Run(bool start_minimal_browser) {
RegisterMainThreadFactories();
#if BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC) && defined(OS_ANDROID)
// The thread cache consumes more memory, especially as long as periodic purge
// above is disabled. Don't use one on low-memory devices.
if (base::SysInfo::IsLowEndDevice())
base::DisablePartitionAllocThreadCacheForProcess();
#endif // BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC) && defined(OS_ANDROID)
if (process_type.empty())
return RunBrowser(main_params, start_minimal_browser);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment