Skip to content

Commit

Permalink
Prepare for configurable max per-cpu capacity.
Browse files Browse the repository at this point in the history
Currently we have very simple hard-coded max per-cpu per-class capacity:
 - 2048 objects for size class <= 10
 - 152 objects for larger size classes

While it's reasonable on high-level, it's too simplistic and too coarse-grained.
Some of the smaller classes are not as active as others,
and there are very active larger classes as well.

Prepare for externally configurable max capacities.
For now the old behavior is preserved.

PiperOrigin-RevId: 581170836
Change-Id: I1a5de2946b52a26f018177fc48286bc8eb5329c3
  • Loading branch information
dvyukov authored and copybara-github committed Nov 10, 2023
1 parent 905c191 commit c2fc97d
Show file tree
Hide file tree
Showing 7 changed files with 1,348 additions and 1,332 deletions.
268 changes: 134 additions & 134 deletions tcmalloc/experimental_pow2_size_class.cc

Large diffs are not rendered by default.

1,212 changes: 606 additions & 606 deletions tcmalloc/legacy_size_classes.cc

Large diffs are not rendered by default.

11 changes: 8 additions & 3 deletions tcmalloc/size_class_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#define TCMALLOC_SIZE_CLASS_INFO_H_

#include <stddef.h>
#include <stdint.h>

#include "tcmalloc/internal/config.h"

Expand All @@ -27,17 +28,21 @@ namespace tcmalloc_internal {
// Precomputed size class parameters.
struct SizeClassInfo {
// Max size storable in that class
size_t size;
uint32_t size;

// Number of pages to allocate at a time
size_t pages;
uint8_t pages;

// Number of objects to move between a per-thread list and a central list in
// one shot. We want this to be not too small so we can amortize the lock
// overhead for accessing the central list. Making it too big may temporarily
// cause unnecessary memory wastage in the per-thread free list until the
// scavenger cleans up the list.
size_t num_to_move;
uint8_t num_to_move;

// Max per-CPU slab capacity for the default 256KB slab size.
// Scaled up/down for larger/smaller slab sizes.
uint32_t max_capacity;
};

} // namespace tcmalloc_internal
Expand Down
1,172 changes: 586 additions & 586 deletions tcmalloc/size_classes.cc

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tcmalloc/size_classes_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ TEST_F(RunTimeSizeClassesTest, ValidatePageSize) {
};
EXPECT_TRUE(m_.ValidSizeClasses(parsed));

parsed[1].pages = 256;
parsed[1].pages = 255;
EXPECT_FALSE(m_.ValidSizeClasses(parsed));
}

Expand Down
5 changes: 3 additions & 2 deletions tcmalloc/sizemap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ bool SizeMap::IsValidSizeClass(size_t size, size_t pages,
Log(kLog, __FILE__, __LINE__, "pages should not be 0", pages);
return false;
}
if (pages >= 256) {
Log(kLog, __FILE__, __LINE__, "pages limited to 255", pages);
if (pages >= 255) {
Log(kLog, __FILE__, __LINE__, "pages limited to 254", pages);
return false;
}
const size_t objects_per_span = Length(pages).in_bytes() / size;
Expand Down Expand Up @@ -104,6 +104,7 @@ bool SizeMap::SetSizeClasses(absl::Span<const SizeClassInfo> size_classes) {
class_to_size_[curr] = size_classes[c].size;
class_to_pages_[curr] = size_classes[c].pages;
num_objects_to_move_[curr] = size_classes[c].num_to_move;
max_capacity_[curr] = size_classes[c].max_capacity;
++curr;
}

Expand Down
10 changes: 10 additions & 0 deletions tcmalloc/sizemap.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ class SizeMap {
// per-thread free list until the scavenger cleans up the list.
BatchSize num_objects_to_move_[kNumClasses] = {0};

uint32_t max_capacity_[kNumClasses] = {0};

// If size is no more than kMaxSize, compute index of the
// class_array[] entry for it, putting the class index in output
// parameter idx and returning true. Otherwise return false.
Expand Down Expand Up @@ -270,6 +272,14 @@ class SizeMap {
return num_objects_to_move_[size_class];
}

// Max per-CPU slab capacity for the default 256KB slab size.
//
// TODO(b/271598304): Revise this when 512KB slabs are available.
ABSL_ATTRIBUTE_ALWAYS_INLINE size_t max_capacity(size_t size_class) const {
ASSERT(size_class < kNumClasses);
return max_capacity_[size_class];
}

ABSL_ATTRIBUTE_ALWAYS_INLINE absl::Span<const size_t> ColdSizeClasses()
const {
return {cold_sizes_, cold_sizes_count_};
Expand Down

0 comments on commit c2fc97d

Please sign in to comment.