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

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

Issue 14113053: chrome: Use base::MessageLoop. (Part 3) (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 7 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 | Annotate | Revision Log
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 166 matching lines...) Expand 10 before | Expand all | Expand 10 after
177 return; 177 return;
178 watcher->ActivateThreadWatching(); 178 watcher->ActivateThreadWatching();
179 } 179 }
180 180
181 void ThreadWatcher::ActivateThreadWatching() { 181 void ThreadWatcher::ActivateThreadWatching() {
182 DCHECK(WatchDogThread::CurrentlyOnWatchDogThread()); 182 DCHECK(WatchDogThread::CurrentlyOnWatchDogThread());
183 if (active_) return; 183 if (active_) return;
184 active_ = true; 184 active_ = true;
185 ping_count_ = unresponsive_threshold_; 185 ping_count_ = unresponsive_threshold_;
186 ResetHangCounters(); 186 ResetHangCounters();
187 MessageLoop::current()->PostTask( 187 base::MessageLoop::current()->PostTask(
188 FROM_HERE, 188 FROM_HERE,
189 base::Bind(&ThreadWatcher::PostPingMessage, 189 base::Bind(&ThreadWatcher::PostPingMessage,
190 weak_ptr_factory_.GetWeakPtr())); 190 weak_ptr_factory_.GetWeakPtr()));
191 } 191 }
192 192
193 void ThreadWatcher::DeActivateThreadWatching() { 193 void ThreadWatcher::DeActivateThreadWatching() {
194 DCHECK(WatchDogThread::CurrentlyOnWatchDogThread()); 194 DCHECK(WatchDogThread::CurrentlyOnWatchDogThread());
195 active_ = false; 195 active_ = false;
196 ping_count_ = 0; 196 ping_count_ = 0;
197 weak_ptr_factory_.InvalidateWeakPtrs(); 197 weak_ptr_factory_.InvalidateWeakPtrs();
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
233 // Send a ping message to the watched thread. Callback will be called on 233 // Send a ping message to the watched thread. Callback will be called on
234 // the WatchDogThread. 234 // the WatchDogThread.
235 base::Closure callback( 235 base::Closure callback(
236 base::Bind(&ThreadWatcher::OnPongMessage, weak_ptr_factory_.GetWeakPtr(), 236 base::Bind(&ThreadWatcher::OnPongMessage, weak_ptr_factory_.GetWeakPtr(),
237 ping_sequence_number_)); 237 ping_sequence_number_));
238 if (watched_loop_->PostTask( 238 if (watched_loop_->PostTask(
239 FROM_HERE, 239 FROM_HERE,
240 base::Bind(&ThreadWatcher::OnPingMessage, thread_id_, 240 base::Bind(&ThreadWatcher::OnPingMessage, thread_id_,
241 callback))) { 241 callback))) {
242 // Post a task to check the responsiveness of watched thread. 242 // Post a task to check the responsiveness of watched thread.
243 MessageLoop::current()->PostDelayedTask( 243 base::MessageLoop::current()->PostDelayedTask(
244 FROM_HERE, 244 FROM_HERE,
245 base::Bind(&ThreadWatcher::OnCheckResponsiveness, 245 base::Bind(&ThreadWatcher::OnCheckResponsiveness,
246 weak_ptr_factory_.GetWeakPtr(), ping_sequence_number_), 246 weak_ptr_factory_.GetWeakPtr(),
247 unresponsive_time_); 247 ping_sequence_number_),
248 unresponsive_time_);
248 } else { 249 } else {
249 // Watched thread might have gone away, stop watching it. 250 // Watched thread might have gone away, stop watching it.
250 DeActivateThreadWatching(); 251 DeActivateThreadWatching();
251 } 252 }
252 } 253 }
253 254
254 void ThreadWatcher::OnPongMessage(uint64 ping_sequence_number) { 255 void ThreadWatcher::OnPongMessage(uint64 ping_sequence_number) {
255 DCHECK(WatchDogThread::CurrentlyOnWatchDogThread()); 256 DCHECK(WatchDogThread::CurrentlyOnWatchDogThread());
256 257
257 // Record watched thread's response time. 258 // Record watched thread's response time.
(...skipping 11 matching lines...) Expand all
269 270
270 // Increment sequence number for the next ping message to indicate watched 271 // Increment sequence number for the next ping message to indicate watched
271 // thread is responsive. 272 // thread is responsive.
272 ++ping_sequence_number_; 273 ++ping_sequence_number_;
273 274
274 // If we have stopped watching or if the user is idle, then stop sending 275 // If we have stopped watching or if the user is idle, then stop sending
275 // ping messages. 276 // ping messages.
276 if (!active_ || --ping_count_ <= 0) 277 if (!active_ || --ping_count_ <= 0)
277 return; 278 return;
278 279
279 MessageLoop::current()->PostDelayedTask( 280 base::MessageLoop::current()->PostDelayedTask(
280 FROM_HERE, 281 FROM_HERE,
281 base::Bind(&ThreadWatcher::PostPingMessage, 282 base::Bind(&ThreadWatcher::PostPingMessage,
282 weak_ptr_factory_.GetWeakPtr()), 283 weak_ptr_factory_.GetWeakPtr()),
283 sleep_time_); 284 sleep_time_);
284 } 285 }
285 286
286 void ThreadWatcher::OnCheckResponsiveness(uint64 ping_sequence_number) { 287 void ThreadWatcher::OnCheckResponsiveness(uint64 ping_sequence_number) {
287 DCHECK(WatchDogThread::CurrentlyOnWatchDogThread()); 288 DCHECK(WatchDogThread::CurrentlyOnWatchDogThread());
288 // If we have stopped watching then consider thread as responding. 289 // If we have stopped watching then consider thread as responding.
289 if (!active_) { 290 if (!active_) {
290 responsive_ = true; 291 responsive_ = true;
291 return; 292 return;
292 } 293 }
293 // If the latest ping_sequence_number_ is not same as the ping_sequence_number 294 // If the latest ping_sequence_number_ is not same as the ping_sequence_number
294 // that is passed in, then we can assume OnPongMessage was called. 295 // that is passed in, then we can assume OnPongMessage was called.
295 // OnPongMessage increments ping_sequence_number_. 296 // OnPongMessage increments ping_sequence_number_.
296 if (ping_sequence_number_ != ping_sequence_number) { 297 if (ping_sequence_number_ != ping_sequence_number) {
297 // Reset unresponsive_count_ to zero because we got a response from the 298 // Reset unresponsive_count_ to zero because we got a response from the
298 // watched thread. 299 // watched thread.
299 ResetHangCounters(); 300 ResetHangCounters();
300 301
301 responsive_ = true; 302 responsive_ = true;
302 return; 303 return;
303 } 304 }
304 // Record that we got no response from watched thread. 305 // Record that we got no response from watched thread.
305 GotNoResponse(); 306 GotNoResponse();
306 307
307 // Post a task to check the responsiveness of watched thread. 308 // Post a task to check the responsiveness of watched thread.
308 MessageLoop::current()->PostDelayedTask( 309 base::MessageLoop::current()->PostDelayedTask(
309 FROM_HERE, 310 FROM_HERE,
310 base::Bind(&ThreadWatcher::OnCheckResponsiveness, 311 base::Bind(&ThreadWatcher::OnCheckResponsiveness,
311 weak_ptr_factory_.GetWeakPtr(), ping_sequence_number_), 312 weak_ptr_factory_.GetWeakPtr(),
313 ping_sequence_number_),
312 unresponsive_time_); 314 unresponsive_time_);
313 responsive_ = false; 315 responsive_ = false;
314 } 316 }
315 317
316 void ThreadWatcher::Initialize() { 318 void ThreadWatcher::Initialize() {
317 DCHECK(WatchDogThread::CurrentlyOnWatchDogThread()); 319 DCHECK(WatchDogThread::CurrentlyOnWatchDogThread());
318 ThreadWatcherList::Register(this); 320 ThreadWatcherList::Register(this);
319 321
320 const std::string response_time_histogram_name = 322 const std::string response_time_histogram_name =
321 "ThreadWatcher.ResponseTime." + thread_name_; 323 "ThreadWatcher.ResponseTime." + thread_name_;
(...skipping 458 matching lines...) Expand 10 before | Expand all | Expand 10 after
780 } 782 }
781 783
782 WatchDogThread::~WatchDogThread() { 784 WatchDogThread::~WatchDogThread() {
783 Stop(); 785 Stop();
784 } 786 }
785 787
786 // static 788 // static
787 bool WatchDogThread::CurrentlyOnWatchDogThread() { 789 bool WatchDogThread::CurrentlyOnWatchDogThread() {
788 base::AutoLock lock(g_watchdog_lock.Get()); 790 base::AutoLock lock(g_watchdog_lock.Get());
789 return g_watchdog_thread && 791 return g_watchdog_thread &&
790 g_watchdog_thread->message_loop() == MessageLoop::current(); 792 g_watchdog_thread->message_loop() == base::MessageLoop::current();
791 } 793 }
792 794
793 // static 795 // static
794 bool WatchDogThread::PostTask(const tracked_objects::Location& from_here, 796 bool WatchDogThread::PostTask(const tracked_objects::Location& from_here,
795 const base::Closure& task) { 797 const base::Closure& task) {
796 return PostTaskHelper(from_here, task, base::TimeDelta()); 798 return PostTaskHelper(from_here, task, base::TimeDelta());
797 } 799 }
798 800
799 // static 801 // static
800 bool WatchDogThread::PostDelayedTask(const tracked_objects::Location& from_here, 802 bool WatchDogThread::PostDelayedTask(const tracked_objects::Location& from_here,
801 const base::Closure& task, 803 const base::Closure& task,
802 base::TimeDelta delay) { 804 base::TimeDelta delay) {
803 return PostTaskHelper(from_here, task, delay); 805 return PostTaskHelper(from_here, task, delay);
804 } 806 }
805 807
806 // static 808 // static
807 bool WatchDogThread::PostTaskHelper( 809 bool WatchDogThread::PostTaskHelper(
808 const tracked_objects::Location& from_here, 810 const tracked_objects::Location& from_here,
809 const base::Closure& task, 811 const base::Closure& task,
810 base::TimeDelta delay) { 812 base::TimeDelta delay) {
811 { 813 {
812 base::AutoLock lock(g_watchdog_lock.Get()); 814 base::AutoLock lock(g_watchdog_lock.Get());
813 815
814 MessageLoop* message_loop = g_watchdog_thread ? 816 base::MessageLoop* message_loop =
815 g_watchdog_thread->message_loop() : NULL; 817 g_watchdog_thread ? g_watchdog_thread->message_loop() : NULL;
816 if (message_loop) { 818 if (message_loop) {
817 message_loop->PostDelayedTask(from_here, task, delay); 819 message_loop->PostDelayedTask(from_here, task, delay);
818 return true; 820 return true;
819 } 821 }
820 } 822 }
821 823
822 return false; 824 return false;
823 } 825 }
824 826
825 void WatchDogThread::Init() { 827 void WatchDogThread::Init() {
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
926 void StartupTimeBomb::DeleteStartupWatchdog() { 928 void StartupTimeBomb::DeleteStartupWatchdog() {
927 DCHECK_EQ(thread_id_, base::PlatformThread::CurrentId()); 929 DCHECK_EQ(thread_id_, base::PlatformThread::CurrentId());
928 if (startup_watchdog_->IsJoinable()) { 930 if (startup_watchdog_->IsJoinable()) {
929 // Allow the watchdog thread to shutdown on UI. Watchdog thread shutdowns 931 // Allow the watchdog thread to shutdown on UI. Watchdog thread shutdowns
930 // very fast. 932 // very fast.
931 base::ThreadRestrictions::SetIOAllowed(true); 933 base::ThreadRestrictions::SetIOAllowed(true);
932 delete startup_watchdog_; 934 delete startup_watchdog_;
933 startup_watchdog_ = NULL; 935 startup_watchdog_ = NULL;
934 return; 936 return;
935 } 937 }
936 MessageLoop::current()->PostDelayedTask( 938 base::MessageLoop::current()->PostDelayedTask(
937 FROM_HERE, 939 FROM_HERE,
938 base::Bind(&StartupTimeBomb::DeleteStartupWatchdog, 940 base::Bind(&StartupTimeBomb::DeleteStartupWatchdog,
939 base::Unretained(this)), 941 base::Unretained(this)),
940 base::TimeDelta::FromSeconds(10)); 942 base::TimeDelta::FromSeconds(10));
941 } 943 }
942 944
943 // static 945 // static
944 void StartupTimeBomb::DisarmStartupTimeBomb() { 946 void StartupTimeBomb::DisarmStartupTimeBomb() {
945 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 947 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
946 if (g_startup_timebomb_) 948 if (g_startup_timebomb_)
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
980 982
981 #if defined(OS_WIN) 983 #if defined(OS_WIN)
982 // On Windows XP, give twice the time for shutdown. 984 // On Windows XP, give twice the time for shutdown.
983 if (base::win::GetVersion() <= base::win::VERSION_XP) 985 if (base::win::GetVersion() <= base::win::VERSION_XP)
984 actual_duration *= 2; 986 actual_duration *= 2;
985 #endif 987 #endif
986 988
987 shutdown_watchdog_ = new ShutdownWatchDogThread(actual_duration); 989 shutdown_watchdog_ = new ShutdownWatchDogThread(actual_duration);
988 shutdown_watchdog_->Arm(); 990 shutdown_watchdog_->Arm();
989 } 991 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698