Skip to content

Commit 6631c87

Browse files
ckennellycopybara-github
authored andcommitted
Handle per-core CPUCache stats inside of the CPUCache.
Rather than surface more state throughout tcmalloc.cc during printing, push this logic closer to where that state is actively used. PiperOrigin-RevId: 318524734 Change-Id: Ifd27a1ca2c28f9a7863300df22342584b77a7b99
1 parent 173a1d7 commit 6631c87

File tree

3 files changed

+53
-40
lines changed

3 files changed

+53
-40
lines changed

tcmalloc/cpu_cache.cc

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,53 @@ uint64_t CPUCache::Reclaim(int cpu) {
516516
return ctx.bytes;
517517
}
518518

519+
void CPUCache::Print(TCMalloc_Printer *out) const {
520+
out->printf("------------------------------------------------\n");
521+
out->printf("Bytes in per-CPU caches (per cpu limit: %" PRIu64 " bytes)\n",
522+
Static::cpu_cache()->CacheLimit());
523+
out->printf("------------------------------------------------\n");
524+
525+
cpu_set_t allowed_cpus;
526+
if (sched_getaffinity(0, sizeof(allowed_cpus), &allowed_cpus) != 0) {
527+
CPU_ZERO(&allowed_cpus);
528+
}
529+
530+
for (int cpu = 0, num_cpus = absl::base_internal::NumCPUs(); cpu < num_cpus;
531+
++cpu) {
532+
static constexpr double MiB = 1048576.0;
533+
534+
uint64_t rbytes = UsedBytes(cpu);
535+
bool populated = HasPopulated(cpu);
536+
uint64_t unallocated = Unallocated(cpu);
537+
out->printf("cpu %3d: %12" PRIu64
538+
" bytes (%7.1f MiB) with"
539+
"%12" PRIu64 " bytes unallocated %s%s\n",
540+
cpu, rbytes, rbytes / MiB, unallocated,
541+
CPU_ISSET(cpu, &allowed_cpus) ? " active" : "",
542+
populated ? " populated" : "");
543+
}
544+
}
545+
546+
void CPUCache::PrintInPbtxt(PbtxtRegion *region) const {
547+
cpu_set_t allowed_cpus;
548+
if (sched_getaffinity(0, sizeof(allowed_cpus), &allowed_cpus) != 0) {
549+
CPU_ZERO(&allowed_cpus);
550+
}
551+
552+
for (int cpu = 0, num_cpus = absl::base_internal::NumCPUs(); cpu < num_cpus;
553+
++cpu) {
554+
PbtxtRegion entry = region->CreateSubRegion("cpu_cache");
555+
uint64_t rbytes = UsedBytes(cpu);
556+
bool populated = HasPopulated(cpu);
557+
uint64_t unallocated = Unallocated(cpu);
558+
entry.PrintI64("cpu", uint64_t(cpu));
559+
entry.PrintI64("used", rbytes);
560+
entry.PrintI64("unused", unallocated);
561+
entry.PrintBool("active", CPU_ISSET(cpu, &allowed_cpus));
562+
entry.PrintBool("populated", populated);
563+
}
564+
}
565+
519566
void CPUCache::PerClassResizeInfo::Init() {
520567
state_.store(0, std::memory_order_relaxed);
521568
}

tcmalloc/cpu_cache.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ class CPUCache {
9696
static constexpr size_t kPerCpuShift = 18;
9797
#endif
9898

99+
// Report statistics
100+
void Print(TCMalloc_Printer *out) const;
101+
void PrintInPbtxt(PbtxtRegion *region) const;
102+
99103
private:
100104
// Per-size-class freelist resizing info.
101105
class PerClassResizeInfo {

tcmalloc/tcmalloc.cc

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -395,29 +395,7 @@ static void DumpStats(TCMalloc_Printer* out, int level) {
395395
}
396396

397397
if (tcmalloc::UsePerCpuCache()) {
398-
out->printf("------------------------------------------------\n");
399-
out->printf(
400-
"Bytes in per-CPU caches (per cpu limit: %" PRIu64 " bytes)\n",
401-
Static::cpu_cache()->CacheLimit());
402-
out->printf("------------------------------------------------\n");
403-
404-
cpu_set_t allowed_cpus;
405-
if (sched_getaffinity(0, sizeof(allowed_cpus), &allowed_cpus) != 0) {
406-
CPU_ZERO(&allowed_cpus);
407-
}
408-
409-
for (int cpu = 0, num_cpus = absl::base_internal::NumCPUs();
410-
cpu < num_cpus; ++cpu) {
411-
uint64_t rbytes = Static::cpu_cache()->UsedBytes(cpu);
412-
bool populated = Static::cpu_cache()->HasPopulated(cpu);
413-
uint64_t unallocated = Static::cpu_cache()->Unallocated(cpu);
414-
out->printf("cpu %3d: %12" PRIu64
415-
" bytes (%7.1f MiB) with"
416-
"%12" PRIu64 " bytes unallocated %s%s\n",
417-
cpu, rbytes, rbytes / MiB, unallocated,
418-
CPU_ISSET(cpu, &allowed_cpus) ? " active" : "",
419-
populated ? " populated" : "");
420-
}
398+
Static::cpu_cache()->Print(out);
421399
}
422400

423401
Static::page_allocator()->Print(out, /*tagged=*/false);
@@ -514,23 +492,7 @@ namespace {
514492
}
515493

516494
if (tcmalloc::UsePerCpuCache()) {
517-
cpu_set_t allowed_cpus;
518-
if (sched_getaffinity(0, sizeof(allowed_cpus), &allowed_cpus) != 0) {
519-
CPU_ZERO(&allowed_cpus);
520-
}
521-
522-
for (int cpu = 0, num_cpus = absl::base_internal::NumCPUs();
523-
cpu < num_cpus; ++cpu) {
524-
PbtxtRegion entry = region.CreateSubRegion("cpu_cache");
525-
uint64_t rbytes = Static::cpu_cache()->UsedBytes(cpu);
526-
bool populated = Static::cpu_cache()->HasPopulated(cpu);
527-
uint64_t unallocated = Static::cpu_cache()->Unallocated(cpu);
528-
entry.PrintI64("cpu", uint64_t(cpu));
529-
entry.PrintI64("used", rbytes);
530-
entry.PrintI64("unused", unallocated);
531-
entry.PrintBool("active", CPU_ISSET(cpu, &allowed_cpus));
532-
entry.PrintBool("populated", populated);
533-
}
495+
Static::cpu_cache()->PrintInPbtxt(&region);
534496
}
535497
}
536498
Static::page_allocator()->PrintInPbtxt(&region, /*tagged=*/false);

0 commit comments

Comments
 (0)