@@ -99,43 +99,54 @@ void ShuffleCpuCaches() {
9999GOOGLE_MALLOC_SECTION_END
100100
101101// Release memory to the system at a constant rate.
102- void MallocExtension_Internal_ProcessBackgroundActions () {
103- tcmalloc::MallocExtension::MarkThreadIdle () ;
102+ static absl::Time prev_time;
103+ static absl::Time last_shuffle ;
104104
105+ void MallocExtension_Internal_ProcessBackgroundActionsInit () {
105106 // Initialize storage for ReleasePerCpuMemoryToOS().
106107 CPU_ZERO (&tcmalloc::tcmalloc_internal::prev_allowed_cpus);
107108
108- absl::Time prev_time = absl::Now ();
109+ prev_time = absl::Now ();
110+ last_shuffle = absl::InfinitePast ();
111+ }
112+
113+ void MallocExtension_Internal_ProcessBackgroundActions () {
109114 constexpr absl::Duration kSleepTime = absl::Seconds (1 );
110115
116+ tcmalloc::MallocExtension::MarkThreadIdle ();
117+ MallocExtension_Internal_ProcessBackgroundActionsInit ();
118+
119+ while (true ) {
120+ MallocExtension_Internal_ProcessBackgroundActionsTick ();
121+ absl::SleepFor (kSleepTime );
122+ }
123+ }
124+
125+ void MallocExtension_Internal_ProcessBackgroundActionsTick () {
111126 // Shuffle per-cpu caches once per kCpuCacheShufflePeriod secs.
112127 constexpr absl::Duration kCpuCacheShufflePeriod = absl::Seconds (5 );
113- absl::Time last_shuffle = absl::InfinitePast ();
114128
115- while (true ) {
116- absl::Time now = absl::Now ();
117- const ssize_t bytes_to_release =
118- static_cast <size_t >(tcmalloc::tcmalloc_internal::Parameters::
119- background_release_rate ()) *
120- absl::ToDoubleSeconds (now - prev_time);
121- if (bytes_to_release > 0 ) { // may be negative if time goes backwards
122- tcmalloc::MallocExtension::ReleaseMemoryToSystem (bytes_to_release);
123- }
129+ absl::Time now = absl::Now ();
130+ const ssize_t bytes_to_release =
131+ static_cast <size_t >(tcmalloc::tcmalloc_internal::Parameters::
132+ background_release_rate ()) *
133+ absl::ToDoubleSeconds (now - prev_time);
134+ if (bytes_to_release > 0 ) { // may be negative if time goes backwards
135+ tcmalloc::MallocExtension::ReleaseMemoryToSystem (bytes_to_release);
136+ }
124137
125- tcmalloc::tcmalloc_internal::ReleasePerCpuMemoryToOS ();
138+ tcmalloc::tcmalloc_internal::ReleasePerCpuMemoryToOS ();
126139
127- const bool shuffle_per_cpu_caches =
128- tcmalloc::tcmalloc_internal::Parameters::shuffle_per_cpu_caches ();
140+ const bool shuffle_per_cpu_caches =
141+ tcmalloc::tcmalloc_internal::Parameters::shuffle_per_cpu_caches ();
129142
130- if (shuffle_per_cpu_caches) {
131- if (now - last_shuffle >= kCpuCacheShufflePeriod ) {
132- tcmalloc::tcmalloc_internal::ShuffleCpuCaches ();
133- last_shuffle = now;
134- }
143+ if (shuffle_per_cpu_caches) {
144+ if (now - last_shuffle >= kCpuCacheShufflePeriod ) {
145+ tcmalloc::tcmalloc_internal::ShuffleCpuCaches ();
146+ last_shuffle = now;
135147 }
136-
137- tcmalloc::tcmalloc_internal::Static ().sharded_transfer_cache ().Plunder ();
138- prev_time = now;
139- absl::SleepFor (kSleepTime );
140148 }
149+
150+ tcmalloc::tcmalloc_internal::Static ().sharded_transfer_cache ().Plunder ();
151+ prev_time = now;
141152}
0 commit comments