Skip to content

Commit d8ade9f

Browse files
committed
Background house-keeping & pre-existing thread
Break apart ProcessBackgroundActions into an "Init" and "Tick" stage and leave periodic invocation to the caller. This lets users reduce the number of threads spun up by 1 if they already have a background house keeping thread that knows when (& perhaps even how much) memory to free.
1 parent 6334d2b commit d8ade9f

4 files changed

+68
-25
lines changed

tcmalloc/background.cc

+35-24
Original file line numberDiff line numberDiff line change
@@ -98,42 +98,53 @@ void ShuffleCpuCaches() {
9898
GOOGLE_MALLOC_SECTION_END
9999

100100
// Release memory to the system at a constant rate.
101-
void MallocExtension_Internal_ProcessBackgroundActions() {
102-
tcmalloc::MallocExtension::MarkThreadIdle();
101+
static absl::Time prev_time;
102+
static absl::Time last_shuffle;
103103

104+
void MallocExtension_Internal_ProcessBackgroundActionsInit() {
104105
// Initialize storage for ReleasePerCpuMemoryToOS().
105106
CPU_ZERO(&tcmalloc::tcmalloc_internal::prev_allowed_cpus);
106107

107-
absl::Time prev_time = absl::Now();
108+
prev_time = absl::Now();
109+
last_shuffle = absl::InfinitePast();
110+
}
111+
112+
void MallocExtension_Internal_ProcessBackgroundActions() {
108113
constexpr absl::Duration kSleepTime = absl::Seconds(1);
109114

115+
tcmalloc::MallocExtension::MarkThreadIdle();
116+
MallocExtension_Internal_ProcessBackgroundActionsInit();
117+
118+
while (true) {
119+
MallocExtension_Internal_ProcessBackgroundActionsTick();
120+
absl::SleepFor(kSleepTime);
121+
}
122+
}
123+
124+
void MallocExtension_Internal_ProcessBackgroundActionsTick() {
110125
// Shuffle per-cpu caches once per kCpuCacheShufflePeriod secs.
111126
constexpr absl::Duration kCpuCacheShufflePeriod = absl::Seconds(5);
112-
absl::Time last_shuffle = absl::InfinitePast();
113127

114-
while (true) {
115-
absl::Time now = absl::Now();
116-
const ssize_t bytes_to_release =
117-
static_cast<size_t>(tcmalloc::tcmalloc_internal::Parameters::
118-
background_release_rate()) *
119-
absl::ToDoubleSeconds(now - prev_time);
120-
if (bytes_to_release > 0) { // may be negative if time goes backwards
121-
tcmalloc::MallocExtension::ReleaseMemoryToSystem(bytes_to_release);
122-
}
128+
absl::Time now = absl::Now();
129+
const ssize_t bytes_to_release =
130+
static_cast<size_t>(tcmalloc::tcmalloc_internal::Parameters::
131+
background_release_rate()) *
132+
absl::ToDoubleSeconds(now - prev_time);
133+
if (bytes_to_release > 0) { // may be negative if time goes backwards
134+
tcmalloc::MallocExtension::ReleaseMemoryToSystem(bytes_to_release);
135+
}
123136

124-
tcmalloc::tcmalloc_internal::ReleasePerCpuMemoryToOS();
137+
tcmalloc::tcmalloc_internal::ReleasePerCpuMemoryToOS();
125138

126-
const bool shuffle_per_cpu_caches =
127-
tcmalloc::tcmalloc_internal::Parameters::shuffle_per_cpu_caches();
139+
const bool shuffle_per_cpu_caches =
140+
tcmalloc::tcmalloc_internal::Parameters::shuffle_per_cpu_caches();
128141

129-
if (shuffle_per_cpu_caches) {
130-
if (now - last_shuffle >= kCpuCacheShufflePeriod) {
131-
tcmalloc::tcmalloc_internal::ShuffleCpuCaches();
132-
last_shuffle = now;
133-
}
142+
if (shuffle_per_cpu_caches) {
143+
if (now - last_shuffle >= kCpuCacheShufflePeriod) {
144+
tcmalloc::tcmalloc_internal::ShuffleCpuCaches();
145+
last_shuffle = now;
134146
}
135-
136-
prev_time = now;
137-
absl::SleepFor(kSleepTime);
138147
}
148+
149+
prev_time = now;
139150
}

tcmalloc/internal_malloc_extension.h

+2
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ ABSL_ATTRIBUTE_WEAK void MallocExtension_Internal_SetProfileSamplingRate(
101101
int64_t);
102102

103103
ABSL_ATTRIBUTE_WEAK void MallocExtension_Internal_ProcessBackgroundActions();
104+
ABSL_ATTRIBUTE_WEAK void MallocExtension_Internal_ProcessBackgroundActionsInit();
105+
ABSL_ATTRIBUTE_WEAK void MallocExtension_Internal_ProcessBackgroundActionsTick();
104106

105107
ABSL_ATTRIBUTE_WEAK tcmalloc::MallocExtension::BytesPerSecond
106108
MallocExtension_Internal_GetBackgroundReleaseRate();

tcmalloc/malloc_extension.cc

+18-1
Original file line numberDiff line numberDiff line change
@@ -425,9 +425,26 @@ void MallocExtension::ProcessBackgroundActions() {
425425
#endif
426426
}
427427

428+
void MallocExtension::ProcessBackgroundActionsInit() {
429+
#if ABSL_INTERNAL_HAVE_WEAK_MALLOCEXTENSION_STUBS
430+
if (NeedsProcessBackgroundActions()) {
431+
MallocExtension_Internal_ProcessBackgroundActionsInit();
432+
}
433+
#endif
434+
}
435+
436+
void MallocExtension::ProcessBackgroundActionsTick() {
437+
#if ABSL_INTERNAL_HAVE_WEAK_MALLOCEXTENSION_STUBS
438+
if (NeedsProcessBackgroundActions()) {
439+
MallocExtension_Internal_ProcessBackgroundActionsTick();
440+
}
441+
#endif
442+
}
443+
428444
bool MallocExtension::NeedsProcessBackgroundActions() {
429445
#if ABSL_INTERNAL_HAVE_WEAK_MALLOCEXTENSION_STUBS
430-
return &MallocExtension_Internal_ProcessBackgroundActions != nullptr;
446+
static bool needed = &MallocExtension_Internal_ProcessBackgroundActions != nullptr;
447+
return needed;
431448
#else
432449
return false;
433450
#endif

tcmalloc/malloc_extension.h

+13
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,19 @@ class MallocExtension final {
433433
// null if the implementation does not support profiling.
434434
static AllocationProfilingToken StartAllocationProfiling();
435435

436+
// Initializes the state needed for this thread to call
437+
// ProcessBackgroundActionsTick. This is the counterpart to
438+
// ProcessBackgroundActions that allows co-operative task keeping on a thread
439+
// that is performing other task keeping.
440+
static void ProcessBackgroundActionsInit();
441+
442+
// This is to be called periodically to run housekeeping actions for the
443+
// allocator off of the main allocation paths of new/delete. Note that unlike
444+
// ProcessBackgroundActions, the thread is not automatically marked as
445+
// idle/busy and the caller is responsible for doing that correctly.
446+
// See ProcessBackgroundActions for details of the actions performed.
447+
static void ProcessBackgroundActionsTick();
448+
436449
// Runs housekeeping actions for the allocator off of the main allocation path
437450
// of new/delete. As of 2020, this includes:
438451
// * Inspecting the current CPU mask and releasing memory from inaccessible

0 commit comments

Comments
 (0)