Skip to content

Commit

Permalink
Use Profile::Sample::GuardedStatus for guarded_status instead of int.
Browse files Browse the repository at this point in the history
This is the correct type and uses only 1 byte instead of 4. We can then
group `guarded_status` and `allocation_type` into the padding after
`allocation_time` to save 8 bytes.

Cleanup the type casting that is no longer needed.

Before:
struct tcmalloc::tcmalloc_internal::StackTrace needs 7 bytes of padding, non-tail padding is 7 (fields: 18 padded fields: 2) total_size: 616

padding	size	alignment	| Field name
	8	8	| sampled_alloc_handle
	8	8	| proxy
	8	8	| requested_size
	8	8	| requested_alignment
	8	8	| allocated_size
	1	1	| requested_size_returning
	1	1	| access_hint
	1	1	| cold_allocated
	1	1	| has_context
	4	4	| static_initialization_depth
	8	8	| weight
4	12	4	| allocation_time
	8	8	| span_start_address
	8	8	| census_handle
	4	4	| guarded_status
3	1	1	| allocation_type
	8	8	| depth
	512	8	| stack

After:
struct tcmalloc::tcmalloc_internal::StackTrace needs 2 bytes of padding, non-tail padding is 2 (fields: 18 padded fields: 1) total_size: 608

padding	size	alignment	| Field name
	8	8	| sampled_alloc_handle
	8	8	| proxy
	8	8	| requested_size
	8	8	| requested_alignment
	8	8	| allocated_size
	1	1	| requested_size_returning
	1	1	| access_hint
	1	1	| cold_allocated
	1	1	| has_context
	4	4	| static_initialization_depth
	8	8	| weight
	12	4	| allocation_time
	1	1	| guarded_status
2	1	1	| allocation_type
	8	8	| span_start_address
	8	8	| census_handle
	8	8	| depth
	512	8	| stack

PiperOrigin-RevId: 716195113
Change-Id: I766ba0f572c5ebdfc891065ce75aca767b9e2a41
  • Loading branch information
Ken Steele authored and copybara-github committed Jan 16, 2025
1 parent ccd1b2f commit c76cb92
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 10 deletions.
2 changes: 1 addition & 1 deletion tcmalloc/allocation_sampling.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ SampleifyAllocation(Static& state, Policy policy, size_t requested_size,
1;
stack_trace.span_start_address = span->start_address();
stack_trace.allocation_time = absl::Now();
stack_trace.guarded_status = static_cast<int>(alloc_with_status.status);
stack_trace.guarded_status = alloc_with_status.status;
stack_trace.allocation_type = policy.allocation_type();

// How many allocations does this sample represent, given the sampling
Expand Down
12 changes: 5 additions & 7 deletions tcmalloc/internal/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,16 @@ struct StackTrace {
// Timestamp of allocation.
absl::Time allocation_time;

Profile::Sample::GuardedStatus guarded_status;

// How the memory was allocated (new/malloc/etc.)
Profile::Sample::AllocationType allocation_type;

// If not nullptr, this is the start address of the span corresponding to this
// sampled allocation. This may be nullptr for cases where it is not useful
// for residency analysis such as for peakheapz.
void* span_start_address = nullptr;

// An integer representing the guarded status of the allocation.
// The values are from the enum GuardedStatus in ../malloc_extension.h.
int guarded_status;

// How the memory was allocated (new/malloc/etc.)
Profile::Sample::AllocationType allocation_type;

uintptr_t depth; // Number of PC values stored in array below
// Place stack as last member because it might not all be accessed.
void* stack[kMaxStackDepth];
Expand Down
3 changes: 1 addition & 2 deletions tcmalloc/stack_trace_table.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ void StackTraceTable::AddTrace(double sample_weight, const StackTrace& t) {
s->sample.allocation_time = t.allocation_time;

s->sample.span_start_address = t.span_start_address;
s->sample.guarded_status =
static_cast<Profile::Sample::GuardedStatus>(t.guarded_status);
s->sample.guarded_status = t.guarded_status;
s->sample.type = t.allocation_type;

static_assert(kMaxStackDepth <= Profile::Sample::kMaxStackDepth,
Expand Down

0 comments on commit c76cb92

Please sign in to comment.