@@ -245,13 +245,33 @@ void ActivityProfilerController::profilerLoop() {
245245}
246246
247247void ActivityProfilerController::memoryProfilerLoop () {
248- const std::string& path = asyncRequestConfig_->activitiesLogFile ();
248+ std::string path = asyncRequestConfig_->activitiesLogFile ();
249249 auto profile_time = asyncRequestConfig_->profileMemoryDuration ();
250- profiler_->performMemoryLoop (path, profile_time);
250+ std::unique_ptr<Config> config = asyncRequestConfig_->clone ();
251+ while (!stopRunloop_) {
252+ // Perform Double-checked locking to reduce overhead of taking lock.
253+ if (asyncRequestConfig_ && !profiler_->isActive ()) {
254+ std::lock_guard<std::mutex> lock (asyncConfigLock_);
255+ if (asyncRequestConfig_ && !profiler_->isActive ()) {
256+ logger_ = makeLogger (*asyncRequestConfig_);
257+ path = asyncRequestConfig_->activitiesLogFile ();
258+ profile_time = asyncRequestConfig_->profileMemoryDuration ();
259+ config = asyncRequestConfig_->clone ();
260+ asyncRequestConfig_ = nullptr ;
261+ } else {
262+ continue ;
263+ }
264+ } else {
265+ continue ;
266+ }
267+
268+ profiler_->performMemoryLoop (path, profile_time, logger_.get (), *config);
269+ }
251270}
252271
253272void ActivityProfilerController::step () {
254- // Do not remove this copy to currentIter. Otherwise count is not guaranteed.
273+ // Do not remove this copy to currentIter. Otherwise count is not
274+ // guaranteed.
255275 int64_t currentIter = ++iterationCount_;
256276 VLOG (0 ) << " Step called , iteration = " << currentIter;
257277
@@ -264,7 +284,6 @@ void ActivityProfilerController::step() {
264284 activateConfig (now);
265285 }
266286 }
267-
268287 if (profiler_->isActive ()) {
269288 auto now = system_clock::now ();
270289 auto next_wakeup_time = now + Config::kControllerIntervalMsecs ;
0 commit comments