@@ -99,43 +99,54 @@ void ShuffleCpuCaches() {
99
99
GOOGLE_MALLOC_SECTION_END
100
100
101
101
// 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 ;
104
104
105
+ void MallocExtension_Internal_ProcessBackgroundActionsInit () {
105
106
// Initialize storage for ReleasePerCpuMemoryToOS().
106
107
CPU_ZERO (&tcmalloc::tcmalloc_internal::prev_allowed_cpus);
107
108
108
- absl::Time prev_time = absl::Now ();
109
+ prev_time = absl::Now ();
110
+ last_shuffle = absl::InfinitePast ();
111
+ }
112
+
113
+ void MallocExtension_Internal_ProcessBackgroundActions () {
109
114
constexpr absl::Duration kSleepTime = absl::Seconds (1 );
110
115
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 () {
111
126
// Shuffle per-cpu caches once per kCpuCacheShufflePeriod secs.
112
127
constexpr absl::Duration kCpuCacheShufflePeriod = absl::Seconds (5 );
113
- absl::Time last_shuffle = absl::InfinitePast ();
114
128
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
+ }
124
137
125
- tcmalloc::tcmalloc_internal::ReleasePerCpuMemoryToOS ();
138
+ tcmalloc::tcmalloc_internal::ReleasePerCpuMemoryToOS ();
126
139
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 ();
129
142
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;
135
147
}
136
-
137
- tcmalloc::tcmalloc_internal::Static ().sharded_transfer_cache ().Plunder ();
138
- prev_time = now;
139
- absl::SleepFor (kSleepTime );
140
148
}
149
+
150
+ tcmalloc::tcmalloc_internal::Static ().sharded_transfer_cache ().Plunder ();
151
+ prev_time = now;
141
152
}
0 commit comments