Skip to content

Commit

Permalink
Add metric for counting the number of once-allocated guarded pages.
Browse files Browse the repository at this point in the history
This is in preparation for cl/547474176.

PiperOrigin-RevId: 571121336
Change-Id: I1a0c443d86b3a6871bece9b974267945aa296c02
  • Loading branch information
kda authored and copybara-github committed Oct 5, 2023
1 parent 3b48e5e commit e239ffa
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
24 changes: 23 additions & 1 deletion tcmalloc/guarded_page_allocator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,17 @@

#include <sys/mman.h>

#include <algorithm>
#include <cstdint>

#include "absl/base/internal/sysinfo.h"
#include "absl/debugging/stacktrace.h"
#include "tcmalloc/common.h"
#include "tcmalloc/guarded_allocations.h"
#include "tcmalloc/internal/config.h"
#include "tcmalloc/internal/logging.h"
#include "tcmalloc/internal/page_size.h"
#include "tcmalloc/malloc_extension.h"
#include "tcmalloc/pagemap.h"
#include "tcmalloc/pages.h"
#include "tcmalloc/parameters.h"
Expand Down Expand Up @@ -90,6 +96,15 @@ GuardedAllocWithStatus GuardedPageAllocator::Allocate(size_t size,

// Record stack trace.
SlotMetadata& d = data_[free_slot];
// Count the number of pages that have been used at least once.
if (d.allocation_start == 0) {
absl::base_internal::SpinLockHolder h(&guarded_page_lock_);
++total_pages_used_;
if (total_pages_used_ == total_pages_) {
alloced_page_count_when_all_used_once_ =
num_allocation_requests_ - num_failed_allocations_;
}
}
d.dealloc_trace.depth = 0;
d.alloc_trace.depth = absl::GetStackTrace(d.alloc_trace.stack, kMaxStackDepth,
/*skip_count=*/3);
Expand Down Expand Up @@ -186,14 +201,17 @@ void GuardedPageAllocator::Print(Printer* out) {
"Maximum Slots Allocated: %zu / %zu\n"
"StackTraceFilter Max Slots Used: %zu\n"
"StackTraceFilter Replacement Inserts: %zu\n"
"Total Slots Used Once: %zu / %zu\n"
"Allocation Count When All Slots Used Once: %zu\n"
"PARAMETER tcmalloc_guarded_sample_parameter %d\n"
// TODO(b/263387812): remove when experiment is finished
"PARAMETER tcmalloc_improved_guarded_sampling %d\n",
num_allocation_requests_ - num_failed_allocations_,
num_failed_allocations_, num_alloced_pages_,
total_pages_ - num_alloced_pages_, num_alloced_pages_max_,
max_alloced_pages_, tc_globals.stacktrace_filter().max_slots_used(),
tc_globals.stacktrace_filter().replacement_inserts(), GetChainedRate(),
tc_globals.stacktrace_filter().replacement_inserts(), total_pages_used_,
total_pages_, alloced_page_count_when_all_used_once_, GetChainedRate(),
Parameters::improved_guarded_sampling());
}

Expand All @@ -211,6 +229,10 @@ void GuardedPageAllocator::PrintInPbtxt(PbtxtRegion* gwp_asan) {
tc_globals.stacktrace_filter().max_slots_used());
gwp_asan->PrintI64("stack_trace_filter_replacement_inserts",
tc_globals.stacktrace_filter().replacement_inserts());
gwp_asan->PrintI64("total_pages_used", total_pages_used_);
gwp_asan->PrintI64("total_pages", total_pages_);
gwp_asan->PrintI64("alloced_page_count_when_all_used_once",
alloced_page_count_when_all_used_once_);
gwp_asan->PrintI64("tcmalloc_guarded_sample_parameter", GetChainedRate());
// TODO(b/263387812): remove when experiment is finished
gwp_asan->PrintI64("tcmalloc_improved_guarded_sampling",
Expand Down
8 changes: 8 additions & 0 deletions tcmalloc/guarded_page_allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ class GuardedPageAllocator {
first_page_addr_(0),
max_alloced_pages_(0),
total_pages_(0),
total_pages_used_(0),
alloced_page_count_when_all_used_once_(0),
page_size_(0),
rand_(0),
initialized_(false),
Expand Down Expand Up @@ -272,6 +274,12 @@ class GuardedPageAllocator {
uintptr_t first_page_addr_; // Points to first page returnable by Allocate.
size_t max_alloced_pages_; // Max number of pages to allocate at once.
size_t total_pages_; // Size of the page pool to allocate from.
// Number of pages allocated at least once from page pool.
size_t total_pages_used_ ABSL_GUARDED_BY(guarded_page_lock_);
// The count of allocs when all the pages had been used at least once (i.e.
// when total_pages_used_ == total_pages_).
size_t alloced_page_count_when_all_used_once_
ABSL_GUARDED_BY(guarded_page_lock_);
size_t page_size_; // Size of pages we allocate.
uint64_t rand_; // RNG seed.

Expand Down

0 comments on commit e239ffa

Please sign in to comment.