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

Side by Side Diff: chrome/browser/jankometer.cc

Issue 6463013: Add support for base::Closure in the MessageLoop. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/base
Patch Set: rebased Created 9 years, 9 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) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 <limits> 5 #include <limits>
6 6
7 #include "chrome/browser/jankometer.h" 7 #include "chrome/browser/jankometer.h"
8 8
9 #include "base/basictypes.h" 9 #include "base/basictypes.h"
10 #include "base/command_line.h" 10 #include "base/command_line.h"
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after
205 events_till_measurement_ = discard_count_; 205 events_till_measurement_ = discard_count_;
206 return measure_current_message_; 206 return measure_current_message_;
207 } 207 }
208 208
209 // static 209 // static
210 int JankObserverHelper::discard_count_ = 99; // Measure only 1 in 100. 210 int JankObserverHelper::discard_count_ = 99; // Measure only 1 in 100.
211 211
212 //------------------------------------------------------------------------------ 212 //------------------------------------------------------------------------------
213 class IOJankObserver : public base::RefCountedThreadSafe<IOJankObserver>, 213 class IOJankObserver : public base::RefCountedThreadSafe<IOJankObserver>,
214 public MessageLoopForIO::IOObserver, 214 public MessageLoopForIO::IOObserver,
215 public MessageLoop::TaskObserver { 215 public MessageLoop::ClosureObserver {
216 public: 216 public:
217 IOJankObserver(const char* thread_name, 217 IOJankObserver(const char* thread_name,
218 TimeDelta excessive_duration, 218 TimeDelta excessive_duration,
219 bool watchdog_enable) 219 bool watchdog_enable)
220 : helper_(thread_name, excessive_duration, watchdog_enable) {} 220 : helper_(thread_name, excessive_duration, watchdog_enable) {}
221 221
222 ~IOJankObserver() {} 222 ~IOJankObserver() {}
223 223
224 // Attaches the observer to the current thread's message loop. You can only 224 // Attaches the observer to the current thread's message loop. You can only
225 // attach to the current thread, so this function can be invoked on another 225 // attach to the current thread, so this function can be invoked on another
226 // thread to attach it. 226 // thread to attach it.
227 void AttachToCurrentThread() { 227 void AttachToCurrentThread() {
228 MessageLoop::current()->AddTaskObserver(this); 228 MessageLoop::current()->AddClosureObserver(this);
229 MessageLoopForIO::current()->AddIOObserver(this); 229 MessageLoopForIO::current()->AddIOObserver(this);
230 } 230 }
231 231
232 // Detaches the observer to the current thread's message loop. 232 // Detaches the observer to the current thread's message loop.
233 void DetachFromCurrentThread() { 233 void DetachFromCurrentThread() {
234 MessageLoopForIO::current()->RemoveIOObserver(this); 234 MessageLoopForIO::current()->RemoveIOObserver(this);
235 MessageLoop::current()->RemoveTaskObserver(this); 235 MessageLoop::current()->RemoveClosureObserver(this);
236 } 236 }
237 237
238 virtual void WillProcessIOEvent() { 238 virtual void WillProcessIOEvent() OVERRIDE {
239 if (!helper_.MessageWillBeMeasured()) 239 if (!helper_.MessageWillBeMeasured())
240 return; 240 return;
241 helper_.StartProcessingTimers(base::TimeDelta()); 241 helper_.StartProcessingTimers(base::TimeDelta());
242 } 242 }
243 243
244 virtual void DidProcessIOEvent() { 244 virtual void DidProcessIOEvent() OVERRIDE {
245 helper_.EndProcessingTimers(); 245 helper_.EndProcessingTimers();
246 } 246 }
247 247
248 virtual void WillProcessTask(const Task* task) { 248 virtual void WillProcessClosure(base::TimeTicks time_posted) OVERRIDE {
249 if (!helper_.MessageWillBeMeasured()) 249 if (!helper_.MessageWillBeMeasured())
250 return; 250 return;
251 base::TimeTicks now = base::TimeTicks::Now(); 251 base::TimeTicks now = base::TimeTicks::Now();
252 const base::TimeDelta queueing_time = now - task->tracked_birth_time(); 252 const base::TimeDelta queueing_time = now - time_posted;
253 helper_.StartProcessingTimers(queueing_time); 253 helper_.StartProcessingTimers(queueing_time);
254 } 254 }
255 255
256 virtual void DidProcessTask(const Task* task) { 256 virtual void DidProcessClosure(base::TimeTicks time_posted) OVERRIDE {
257 helper_.EndProcessingTimers(); 257 helper_.EndProcessingTimers();
258 } 258 }
259 259
260 private: 260 private:
261 friend class base::RefCountedThreadSafe<IOJankObserver>; 261 friend class base::RefCountedThreadSafe<IOJankObserver>;
262 262
263 JankObserverHelper helper_; 263 JankObserverHelper helper_;
264 264
265 DISALLOW_COPY_AND_ASSIGN(IOJankObserver); 265 DISALLOW_COPY_AND_ASSIGN(IOJankObserver);
266 }; 266 };
267 267
268 //------------------------------------------------------------------------------ 268 //------------------------------------------------------------------------------
269 class UIJankObserver : public base::RefCountedThreadSafe<UIJankObserver>, 269 class UIJankObserver : public base::RefCountedThreadSafe<UIJankObserver>,
270 public MessageLoop::TaskObserver, 270 public MessageLoop::ClosureObserver,
271 public MessageLoopForUI::Observer { 271 public MessageLoopForUI::Observer {
272 public: 272 public:
273 UIJankObserver(const char* thread_name, 273 UIJankObserver(const char* thread_name,
274 TimeDelta excessive_duration, 274 TimeDelta excessive_duration,
275 bool watchdog_enable) 275 bool watchdog_enable)
276 : helper_(thread_name, excessive_duration, watchdog_enable) {} 276 : helper_(thread_name, excessive_duration, watchdog_enable) {}
277 277
278 // Attaches the observer to the current thread's message loop. You can only 278 // Attaches the observer to the current thread's message loop. You can only
279 // attach to the current thread, so this function can be invoked on another 279 // attach to the current thread, so this function can be invoked on another
280 // thread to attach it. 280 // thread to attach it.
281 void AttachToCurrentThread() { 281 void AttachToCurrentThread() {
282 DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_UI); 282 DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_UI);
283 MessageLoopForUI::current()->AddObserver(this); 283 MessageLoopForUI::current()->AddObserver(this);
284 MessageLoop::current()->AddTaskObserver(this); 284 MessageLoop::current()->AddClosureObserver(this);
285 } 285 }
286 286
287 // Detaches the observer to the current thread's message loop. 287 // Detaches the observer to the current thread's message loop.
288 void DetachFromCurrentThread() { 288 void DetachFromCurrentThread() {
289 DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_UI); 289 DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_UI);
290 MessageLoop::current()->RemoveTaskObserver(this); 290 MessageLoop::current()->RemoveClosureObserver(this);
291 MessageLoopForUI::current()->RemoveObserver(this); 291 MessageLoopForUI::current()->RemoveObserver(this);
292 } 292 }
293 293
294 virtual void WillProcessTask(const Task* task) { 294 virtual void WillProcessClosure(base::TimeTicks time_posted) OVERRIDE {
295 if (!helper_.MessageWillBeMeasured()) 295 if (!helper_.MessageWillBeMeasured())
296 return; 296 return;
297 base::TimeTicks now = base::TimeTicks::Now(); 297 base::TimeTicks now = base::TimeTicks::Now();
298 const base::TimeDelta queueing_time = now - task->tracked_birth_time(); 298 const base::TimeDelta queueing_time = now - time_posted;
299 helper_.StartProcessingTimers(queueing_time); 299 helper_.StartProcessingTimers(queueing_time);
300 } 300 }
301 301
302 virtual void DidProcessTask(const Task* task) { 302 virtual void DidProcessClosure(base::TimeTicks time_posted) OVERRIDE {
303 helper_.EndProcessingTimers(); 303 helper_.EndProcessingTimers();
304 } 304 }
305 305
306 #if defined(OS_WIN) 306 #if defined(OS_WIN)
307 virtual void WillProcessMessage(const MSG& msg) { 307 virtual void WillProcessMessage(const MSG& msg) {
308 if (!helper_.MessageWillBeMeasured()) 308 if (!helper_.MessageWillBeMeasured())
309 return; 309 return;
310 // GetMessageTime returns a LONG (signed 32-bit) and GetTickCount returns 310 // GetMessageTime returns a LONG (signed 32-bit) and GetTickCount returns
311 // a DWORD (unsigned 32-bit). They both wrap around when the time is longer 311 // a DWORD (unsigned 32-bit). They both wrap around when the time is longer
312 // than they can hold. I'm not sure if GetMessageTime wraps around to 0, 312 // than they can hold. I'm not sure if GetMessageTime wraps around to 0,
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
408 delete ui_observer; 408 delete ui_observer;
409 ui_observer = NULL; 409 ui_observer = NULL;
410 } 410 }
411 if (io_observer) { 411 if (io_observer) {
412 // IO thread can't be running when we remove observers. 412 // IO thread can't be running when we remove observers.
413 DCHECK((!g_browser_process) || !(g_browser_process->io_thread())); 413 DCHECK((!g_browser_process) || !(g_browser_process->io_thread()));
414 delete io_observer; 414 delete io_observer;
415 io_observer = NULL; 415 io_observer = NULL;
416 } 416 }
417 } 417 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698