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