Skip to content

Commit

Permalink
[PA] Namespace migration (24 of N)
Browse files Browse the repository at this point in the history
This change migrates //tools/memory/partition_alloc/
from partition_alloc::internal::tools to partition_alloc::tools
because it's not likely that //tools/memory/partition_alloc/
will become part of PA stand-alone library very soon, and
it's also not likely that it uses the (copied, minimum) base
part of the PA library.

Bug: 1288247
Change-Id: I4406191deea6fdf36c18016a021fd6b2b2031461
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3552152
Reviewed-by: Kalvin Lee <kdlee@chromium.org>
Reviewed-by: Bartek Nowierski <bartekn@chromium.org>
Reviewed-by: Benoit Lize <lizeb@chromium.org>
Commit-Queue: Yuki Shiino <yukishiino@chromium.org>
Cr-Commit-Position: refs/heads/main@{#988408}
  • Loading branch information
yuki3 authored and Chromium LUCI CQ committed Apr 4, 2022
1 parent ce3d461 commit 0bb9b1e
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 89 deletions.
7 changes: 3 additions & 4 deletions base/allocator/partition_allocator/thread_cache.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ namespace {
ThreadCacheRegistry g_instance;
} // namespace

namespace internal {

namespace tools {
uintptr_t kThreadCacheNeedleArray[kThreadCacheNeedleArraySize] = {
kNeedle1, reinterpret_cast<uintptr_t>(&g_instance),
Expand All @@ -40,6 +38,8 @@ uintptr_t kThreadCacheNeedleArray[kThreadCacheNeedleArraySize] = {
kNeedle2};
} // namespace tools

namespace internal {

BASE_EXPORT PartitionTlsKey g_thread_cache_key;
#if defined(PA_THREAD_CACHE_FAST_TLS)
BASE_EXPORT
Expand Down Expand Up @@ -428,8 +428,7 @@ ThreadCache* ThreadCache::Create(PartitionRoot<internal::ThreadSafe>* root) {
PA_CHECK(root);
// See comment in thread_cache.h, this is used to make sure
// kThreadCacheNeedleArray is kept in the final binary.
PA_CHECK(internal::tools::kThreadCacheNeedleArray[0] ==
internal::tools::kNeedle1);
PA_CHECK(tools::kThreadCacheNeedleArray[0] == tools::kNeedle1);

// Placement new and RawAlloc() are used, as otherwise when this partition is
// the malloc() implementation, the memory allocated for the new thread cache
Expand Down
14 changes: 7 additions & 7 deletions base/allocator/partition_allocator/thread_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ namespace partition_alloc {

class ThreadCache;

namespace internal {

namespace tools {

// This is used from ThreadCacheInspector, which runs in a different process. It
Expand Down Expand Up @@ -66,6 +64,8 @@ class ThreadCacheInspector;

} // namespace tools

namespace internal {

extern BASE_EXPORT PartitionTlsKey g_thread_cache_key;
// On Android, we have to go through emutls, since this is always a shared
// library, so don't bother.
Expand Down Expand Up @@ -150,8 +150,8 @@ class BASE_EXPORT ThreadCacheRegistry {
static constexpr size_t kMinCachedMemoryForPurging = 500 * 1024;

private:
friend class internal::tools::ThreadCacheInspector;
friend class internal::tools::HeapDumper;
friend class tools::ThreadCacheInspector;
friend class tools::HeapDumper;

// Not using base::Lock as the object's constructor must be constexpr.
internal::Lock lock_;
Expand Down Expand Up @@ -327,8 +327,8 @@ class BASE_EXPORT ThreadCache {
ThreadCacheLimits::kLargeSizeThreshold;

private:
friend class internal::tools::HeapDumper;
friend class internal::tools::ThreadCacheInspector;
friend class tools::HeapDumper;
friend class tools::ThreadCacheInspector;

struct Bucket {
internal::PartitionFreelistEntry* freelist_head = nullptr;
Expand Down Expand Up @@ -417,7 +417,7 @@ class BASE_EXPORT ThreadCache {

friend class ThreadCacheRegistry;
friend class PartitionAllocThreadCacheTest;
friend class internal::tools::ThreadCacheInspector;
friend class tools::ThreadCacheInspector;
FRIEND_TEST_ALL_PREFIXES(PartitionAllocThreadCacheTest, Simple);
FRIEND_TEST_ALL_PREFIXES(PartitionAllocThreadCacheTest,
MultipleObjectsCachedPerBucket);
Expand Down
13 changes: 5 additions & 8 deletions tools/memory/partition_allocator/inspect_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include "base/logging.h"
#include "base/strings/stringprintf.h"

namespace partition_alloc::internal::tools {
namespace partition_alloc::tools {

base::ScopedFD OpenProcMem(pid_t pid) {
std::string path = base::StringPrintf("/proc/%d/mem", pid);
Expand Down Expand Up @@ -75,8 +75,7 @@ char* ReadAtSameAddressInLocalMemory(int fd,

uintptr_t IndexThreadCacheNeedleArray(pid_t pid, int mem_fd, size_t index) {
std::vector<base::debug::MappedMemoryRegion> regions;
DCHECK_LT(index,
partition_alloc::internal::tools::kThreadCacheNeedleArraySize);
DCHECK_LT(index, kThreadCacheNeedleArraySize);

{
// Ensures that the mappings are not going to change.
Expand Down Expand Up @@ -135,10 +134,8 @@ uintptr_t IndexThreadCacheNeedleArray(pid_t pid, int mem_fd, size_t index) {
continue;
}

if (needle_array_candidate[0] ==
partition_alloc::internal::tools::kNeedle1 &&
needle_array_candidate[kThreadCacheNeedleArraySize - 1] ==
partition_alloc::internal::tools::kNeedle2) {
if (needle_array_candidate[0] == kNeedle1 &&
needle_array_candidate[kThreadCacheNeedleArraySize - 1] == kNeedle2) {
LOG(INFO) << "Got it! Address = 0x" << std::hex
<< needle_array_candidate[index];
return needle_array_candidate[index];
Expand All @@ -150,4 +147,4 @@ uintptr_t IndexThreadCacheNeedleArray(pid_t pid, int mem_fd, size_t index) {
return 0;
}

} // namespace partition_alloc::internal::tools
} // namespace partition_alloc::tools
4 changes: 2 additions & 2 deletions tools/memory/partition_allocator/inspect_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include "base/posix/eintr_wrapper.h"
#include "third_party/abseil-cpp/absl/types/optional.h"

namespace partition_alloc::internal::tools {
namespace partition_alloc::tools {

// SIGSTOPs a process.
class ScopedSigStopper {
Expand Down Expand Up @@ -78,6 +78,6 @@ class RawBuffer {
alignas(T) char buffer_[sizeof(T)];
};

} // namespace partition_alloc::internal::tools
} // namespace partition_alloc::tools

#endif // TOOLS_MEMORY_PARTITION_ALLOCATOR_INSPECT_UTILS_H_
38 changes: 20 additions & 18 deletions tools/memory/partition_allocator/pa_buckets_inspect.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

#include "base/allocator/partition_allocator/partition_root.h"
#include "base/allocator/partition_allocator/thread_cache.h"

#include "base/check_op.h"
#include "base/debug/proc_maps_linux.h"
#include "base/files/file.h"
Expand All @@ -34,9 +33,12 @@
#include "third_party/abseil-cpp/absl/types/optional.h"
#include "tools/memory/partition_allocator/inspect_utils.h"

namespace partition_alloc::internal::tools {
namespace partition_alloc::tools {
namespace {

using partition_alloc::internal::BucketIndexLookup;
using partition_alloc::internal::kNumBuckets;

constexpr const char* kDumpName = "dump.dat";
constexpr const char* kTmpDumpName = "dump.dat.tmp";

Expand All @@ -46,7 +48,7 @@ uintptr_t FindAllocInfoAddress(pid_t pid, int mem_fd) {

void DisplayPerBucketData(
const std::unordered_map<uintptr_t, size_t>& live_allocs) {
constexpr base::internal::BucketIndexLookup lookup{};
constexpr BucketIndexLookup lookup{};
std::cout << "Per-bucket stats:"
<< "\nIndex\tBucket Size\t#Allocs\tTotal Size\tFragmentation"
<< std::string(80, '-') << "\n";
Expand All @@ -56,7 +58,7 @@ void DisplayPerBucketData(
size_t total_memory = 0;
for (const auto& pair : live_allocs) {
total_memory += pair.second;
const auto index = base::internal::BucketIndexLookup::GetIndex(pair.second);
const auto index = BucketIndexLookup::GetIndex(pair.second);
alloc_size[index] += pair.second;
alloc_nums[index]++;
}
Expand Down Expand Up @@ -91,9 +93,12 @@ void DisplayPerBucketData(
}

} // namespace
} // namespace partition_alloc::internal::tools
} // namespace partition_alloc::tools

int main(int argc, char** argv) {
using partition_alloc::internal::AllocInfo;
using partition_alloc::internal::kAllocInfoSize;

if (argc < 2) {
LOG(ERROR) << "Usage:" << argv[0] << " <PID> "
<< "[address. 0 to scan the process memory]";
Expand All @@ -103,32 +108,30 @@ int main(int argc, char** argv) {
int pid = atoi(argv[1]);
uintptr_t registry_address = 0;

auto mem_fd = partition_alloc::internal::tools::OpenProcMem(pid);
auto mem_fd = partition_alloc::tools::OpenProcMem(pid);

if (argc == 3) {
uint64_t address;
CHECK(base::StringToUint64(argv[2], &address));
registry_address = static_cast<uintptr_t>(address);
} else {
// Scan the memory.
registry_address = partition_alloc::internal::tools::FindAllocInfoAddress(
pid, mem_fd.get());
registry_address =
partition_alloc::tools::FindAllocInfoAddress(pid, mem_fd.get());
}

CHECK(registry_address);

auto alloc_info = std::make_unique<partition_alloc::internal::AllocInfo>();
partition_alloc::internal::tools::ReadMemory(
mem_fd.get(), registry_address,
sizeof(partition_alloc::internal::AllocInfo),
reinterpret_cast<char*>(alloc_info.get()));
auto alloc_info = std::make_unique<AllocInfo>();
partition_alloc::tools::ReadMemory(mem_fd.get(), registry_address,
sizeof(AllocInfo),
reinterpret_cast<char*>(alloc_info.get()));

size_t old_index = 0;
size_t new_index = alloc_info->index;

std::unordered_map<uintptr_t, size_t> live_allocs = {};
while (true) {
using partition_alloc::internal::kAllocInfoSize;
base::TimeTicks tick = base::TimeTicks::Now();

size_t len = old_index < new_index ? new_index - old_index
Expand All @@ -150,11 +153,10 @@ int main(int argc, char** argv) {
constexpr const char* kClearScreen = "\033[2J\033[1;1H";
std::cout << kClearScreen << "Time to gather data = " << gather_time_ms
<< "ms\n";
partition_alloc::internal::tools::DisplayPerBucketData(live_allocs);
partition_alloc::tools::DisplayPerBucketData(live_allocs);

partition_alloc::internal::tools::ReadMemory(
mem_fd.get(), registry_address,
sizeof(partition_alloc::internal::AllocInfo),
partition_alloc::tools::ReadMemory(
mem_fd.get(), registry_address, sizeof(AllocInfo),
reinterpret_cast<char*>(alloc_info.get()));

old_index = new_index;
Expand Down
33 changes: 22 additions & 11 deletions tools/memory/partition_allocator/pa_dump_heap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
// Dumps PartitionAlloc's heap into a file.

#include <sys/mman.h>

#include <cstdlib>
#include <cstring>
#include <string>

#include "base/allocator/partition_allocator/partition_alloc_config.h"
#include "base/allocator/partition_allocator/partition_ref_count.h"
#include "base/allocator/partition_allocator/partition_root.h"
#include "base/allocator/partition_allocator/thread_cache.h"
#include "base/check.h"
Expand All @@ -22,7 +24,18 @@
#include "base/values.h"
#include "tools/memory/partition_allocator/inspect_utils.h"

namespace partition_alloc::internal::tools {
namespace partition_alloc::tools {

using partition_alloc::internal::kInvalidBucketSize;
using partition_alloc::internal::kSuperPageSize;
using partition_alloc::internal::PartitionPage;
using partition_alloc::internal::PartitionPageSize;
#if BUILDFLAG(USE_BACKUP_REF_PTR)
using partition_alloc::internal::PartitionRefCountPointer;
#endif // BUILDFLAG(USE_BACKUP_REF_PTR)
using partition_alloc::internal::PartitionSuperPageExtentEntry;
using partition_alloc::internal::SystemPageSize;
using partition_alloc::internal::ThreadSafe;

class HeapDumper {
public:
Expand Down Expand Up @@ -260,7 +273,7 @@ class HeapDumper {
}
uintptr_t slot_address =
slot_span_start + slot_index * metadata.bucket->slot_size;
auto* ref_count = internal::PartitionRefCountPointer(slot_address);
auto* ref_count = PartitionRefCountPointer(slot_address);
uint32_t requested_size = ref_count->requested_size();

// Address space dumping is not synchronized with allocation, meaning
Expand Down Expand Up @@ -303,9 +316,8 @@ class HeapDumper {
int mem_fd) NO_THREAD_SAFETY_ANALYSIS {
uintptr_t tcache_registry_address =
IndexThreadCacheNeedleArray(pid, mem_fd, 1);
auto registry =
RawBuffer<base::internal::ThreadCacheRegistry>::ReadFromMemFd(
mem_fd, tcache_registry_address);
auto registry = RawBuffer<ThreadCacheRegistry>::ReadFromMemFd(
mem_fd, tcache_registry_address);
if (!registry)
return 0;

Expand All @@ -314,8 +326,7 @@ class HeapDumper {
if (!tcache_address)
return 0;

auto tcache = RawBuffer<base::internal::ThreadCache>::ReadFromMemFd(
mem_fd, tcache_address);
auto tcache = RawBuffer<ThreadCache>::ReadFromMemFd(mem_fd, tcache_address);
if (!tcache)
return 0;

Expand All @@ -335,7 +346,7 @@ class HeapDumper {
size_t local_root_copy_mapping_size_ = 0;
};

} // namespace partition_alloc::internal::tools
} // namespace partition_alloc::tools

int main(int argc, char** argv) {
base::CommandLine::Init(argc, argv);
Expand All @@ -349,11 +360,11 @@ int main(int argc, char** argv) {
int pid = atoi(command_line->GetSwitchValueASCII("pid").c_str());
LOG(WARNING) << "PID = " << pid;

auto mem_fd = partition_alloc::internal::tools::OpenProcMem(pid);
partition_alloc::internal::tools::HeapDumper dumper{pid, mem_fd.get()};
auto mem_fd = partition_alloc::tools::OpenProcMem(pid);
partition_alloc::tools::HeapDumper dumper{pid, mem_fd.get()};

{
partition_alloc::internal::tools::ScopedSigStopper stopper{pid};
partition_alloc::tools::ScopedSigStopper stopper{pid};
if (!dumper.FindRoot()) {
LOG(WARNING) << "Cannot find (or copy) the root";
return 1;
Expand Down
Loading

0 comments on commit 0bb9b1e

Please sign in to comment.