Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2159)

Side by Side Diff: chrome/browser/metrics/thread_watcher.cc

Issue 2300133002: Fix memory leak in ThreadWatcher. (Closed)
Patch Set: Implement isherman's feedback (from comment #6) Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/metrics/thread_watcher.h" 5 #include "chrome/browser/metrics/thread_watcher.h"
6 6
7 #include <math.h> // ceil 7 #include <math.h> // ceil
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/compiler_specific.h" 10 #include "base/compiler_specific.h"
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
72 if (!WatchDogThread::CurrentlyOnWatchDogThread()) { 72 if (!WatchDogThread::CurrentlyOnWatchDogThread()) {
73 WatchDogThread::PostTask( 73 WatchDogThread::PostTask(
74 FROM_HERE, 74 FROM_HERE,
75 base::Bind(&ThreadWatcher::StartWatching, params)); 75 base::Bind(&ThreadWatcher::StartWatching, params));
76 return; 76 return;
77 } 77 }
78 78
79 DCHECK(WatchDogThread::CurrentlyOnWatchDogThread()); 79 DCHECK(WatchDogThread::CurrentlyOnWatchDogThread());
80 80
81 // Create a new thread watcher object for the given thread and activate it. 81 // Create a new thread watcher object for the given thread and activate it.
82 ThreadWatcher* watcher = new ThreadWatcher(params); 82 std::unique_ptr<ThreadWatcher> watcher(new ThreadWatcher(params));
83 83
84 DCHECK(watcher);
85 // If we couldn't register the thread watcher object, we are shutting down, 84 // If we couldn't register the thread watcher object, we are shutting down,
86 // then don't activate thread watching. 85 // so don't activate thread watching.
87 if (!ThreadWatcherList::IsRegistered(params.thread_id)) 86 ThreadWatcher* registered_watcher =
88 return; 87 ThreadWatcherList::Register(std::move(watcher));
89 watcher->ActivateThreadWatching(); 88 if (registered_watcher != nullptr)
89 registered_watcher->ActivateThreadWatching();
90 } 90 }
91 91
92 void ThreadWatcher::ActivateThreadWatching() { 92 void ThreadWatcher::ActivateThreadWatching() {
93 DCHECK(WatchDogThread::CurrentlyOnWatchDogThread()); 93 DCHECK(WatchDogThread::CurrentlyOnWatchDogThread());
94 if (active_) return; 94 if (active_) return;
95 active_ = true; 95 active_ = true;
96 ping_count_ = unresponsive_threshold_; 96 ping_count_ = unresponsive_threshold_;
97 ResetHangCounters(); 97 ResetHangCounters();
98 base::ThreadTaskRunnerHandle::Get()->PostTask( 98 base::ThreadTaskRunnerHandle::Get()->PostTask(
99 FROM_HERE, base::Bind(&ThreadWatcher::PostPingMessage, 99 FROM_HERE, base::Bind(&ThreadWatcher::PostPingMessage,
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
217 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( 217 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
218 FROM_HERE, 218 FROM_HERE,
219 base::Bind(&ThreadWatcher::OnCheckResponsiveness, 219 base::Bind(&ThreadWatcher::OnCheckResponsiveness,
220 weak_ptr_factory_.GetWeakPtr(), ping_sequence_number_), 220 weak_ptr_factory_.GetWeakPtr(), ping_sequence_number_),
221 unresponsive_time_); 221 unresponsive_time_);
222 responsive_ = false; 222 responsive_ = false;
223 } 223 }
224 224
225 void ThreadWatcher::Initialize() { 225 void ThreadWatcher::Initialize() {
226 DCHECK(WatchDogThread::CurrentlyOnWatchDogThread()); 226 DCHECK(WatchDogThread::CurrentlyOnWatchDogThread());
227 ThreadWatcherList::Register(this);
228 227
229 const std::string response_time_histogram_name = 228 const std::string response_time_histogram_name =
230 "ThreadWatcher.ResponseTime." + thread_name_; 229 "ThreadWatcher.ResponseTime." + thread_name_;
231 response_time_histogram_ = base::Histogram::FactoryTimeGet( 230 response_time_histogram_ = base::Histogram::FactoryTimeGet(
232 response_time_histogram_name, 231 response_time_histogram_name,
233 base::TimeDelta::FromMilliseconds(1), 232 base::TimeDelta::FromMilliseconds(1),
234 base::TimeDelta::FromSeconds(100), 50, 233 base::TimeDelta::FromSeconds(100), 50,
235 base::Histogram::kUmaTargetedHistogramFlag); 234 base::Histogram::kUmaTargetedHistogramFlag);
236 235
237 const std::string unresponsive_time_histogram_name = 236 const std::string unresponsive_time_histogram_name =
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after
375 } 374 }
376 375
377 // static 376 // static
378 void ThreadWatcherList::StopWatchingAll() { 377 void ThreadWatcherList::StopWatchingAll() {
379 // TODO(rtenneti): Enable ThreadWatcher. 378 // TODO(rtenneti): Enable ThreadWatcher.
380 ThreadWatcherObserver::RemoveNotifications(); 379 ThreadWatcherObserver::RemoveNotifications();
381 DeleteAll(); 380 DeleteAll();
382 } 381 }
383 382
384 // static 383 // static
385 void ThreadWatcherList::Register(ThreadWatcher* watcher) { 384 ThreadWatcher* ThreadWatcherList::Register(
385 std::unique_ptr<ThreadWatcher> watcher) {
386 DCHECK(WatchDogThread::CurrentlyOnWatchDogThread()); 386 DCHECK(WatchDogThread::CurrentlyOnWatchDogThread());
387 if (!g_thread_watcher_list_) 387 if (!g_thread_watcher_list_)
388 return; 388 return nullptr;
389 DCHECK(!g_thread_watcher_list_->Find(watcher->thread_id())); 389 content::BrowserThread::ID thread_id = watcher->thread_id();
390 g_thread_watcher_list_->registered_[watcher->thread_id()] = watcher; 390 DCHECK(g_thread_watcher_list_->registered_.count(thread_id) == 0);
391 return g_thread_watcher_list_->registered_[thread_id] = watcher.release();
391 } 392 }
392 393
393 // static 394 // static
394 bool ThreadWatcherList::IsRegistered(const BrowserThread::ID thread_id) {
395 DCHECK(WatchDogThread::CurrentlyOnWatchDogThread());
396 return nullptr != ThreadWatcherList::Find(thread_id);
397 }
398
399 // static
400 void ThreadWatcherList::GetStatusOfThreads( 395 void ThreadWatcherList::GetStatusOfThreads(
401 uint32_t* responding_thread_count, 396 uint32_t* responding_thread_count,
402 uint32_t* unresponding_thread_count) { 397 uint32_t* unresponding_thread_count) {
403 DCHECK(WatchDogThread::CurrentlyOnWatchDogThread()); 398 DCHECK(WatchDogThread::CurrentlyOnWatchDogThread());
404 *responding_thread_count = 0; 399 *responding_thread_count = 0;
405 *unresponding_thread_count = 0; 400 *unresponding_thread_count = 0;
406 if (!g_thread_watcher_list_) 401 if (!g_thread_watcher_list_)
407 return; 402 return;
408 403
409 for (RegistrationList::iterator it = 404 for (RegistrationList::iterator it =
(...skipping 594 matching lines...) Expand 10 before | Expand all | Expand 10 after
1004 999
1005 #if defined(OS_WIN) 1000 #if defined(OS_WIN)
1006 // On Windows XP, give twice the time for shutdown. 1001 // On Windows XP, give twice the time for shutdown.
1007 if (base::win::GetVersion() <= base::win::VERSION_XP) 1002 if (base::win::GetVersion() <= base::win::VERSION_XP)
1008 actual_duration *= 2; 1003 actual_duration *= 2;
1009 #endif 1004 #endif
1010 1005
1011 shutdown_watchdog_ = new ShutdownWatchDogThread(actual_duration); 1006 shutdown_watchdog_ = new ShutdownWatchDogThread(actual_duration);
1012 shutdown_watchdog_->Arm(); 1007 shutdown_watchdog_->Arm();
1013 } 1008 }
OLDNEW
« no previous file with comments | « chrome/browser/metrics/thread_watcher.h ('k') | chrome/browser/metrics/thread_watcher_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698