From 16e0efd89b890932ff32fb612228b48dea4371fa Mon Sep 17 00:00:00 2001
From: "evan@chromium.org"
 <evan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>
Date: Wed, 8 Jul 2009 03:58:29 +0000
Subject: linux: port Jankometer

Originally I had split it into multiple files, but I think it's
cleaner to just use one.

(I want to use this for measuring plugin jank.)

BUG=8077

Review URL: http://codereview.chromium.org/155194

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@20130 0039d316-1c4b-4281-b951-d872f2087c98
---
 chrome/browser/jankometer.cc | 81 +++++++++++++++++++++++++++++++++-----------
 chrome/browser/jankometer.h  | 10 +++---
 2 files changed, 67 insertions(+), 24 deletions(-)

(limited to 'chrome/browser')

diff --git a/chrome/browser/jankometer.cc b/chrome/browser/jankometer.cc
index c7bb0a5..1c73d72 100644
--- a/chrome/browser/jankometer.cc
+++ b/chrome/browser/jankometer.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
+// Copyright (c) 2009 The Chromium Authors. All rights reserved.
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
@@ -16,9 +16,14 @@
 #include "base/thread.h"
 #include "base/time.h"
 #include "base/watchdog.h"
+#include "build/build_config.h"
 #include "chrome/browser/browser_process.h"
 #include "chrome/common/chrome_switches.h"
 
+#if defined(OS_LINUX)
+#include "chrome/common/gtk_util.h"
+#endif
+
 using base::TimeDelta;
 using base::TimeTicks;
 
@@ -109,33 +114,24 @@ class JankObserver : public base::RefCountedThreadSafe<JankObserver>,
       MessageLoopForUI::current()->RemoveObserver(this);
   }
 
-  void WillProcessMessage(const MSG& msg) {
-    begin_process_message_ = TimeTicks::Now();
-
-    // GetMessageTime returns a LONG (signed 32-bit) and GetTickCount returns
-    // a DWORD (unsigned 32-bit). They both wrap around when the time is longer
-    // than they can hold. I'm not sure if GetMessageTime wraps around to 0,
-    // or if the original time comes from GetTickCount, it might wrap around
-    // to -1.
-    //
-    // Therefore, I cast to DWORD so if it wraps to -1 we will correct it. If
-    // it doesn't, then our time delta will be negative if a message happens
-    // to straddle the wraparound point, it will still be OK.
-    DWORD cur_message_issue_time = static_cast<DWORD>(msg.time);
-    DWORD cur_time = GetTickCount();
-    queueing_time_ = TimeDelta::FromMilliseconds(cur_time
-                                                 - cur_message_issue_time);
+  // Called when a message has just begun processing, initializes
+  // per-message variables and timers.
+  void StartProcessingTimers() {
     // Simulate arming when the message entered the queue.
     total_time_watchdog_.ArmSomeTimeDeltaAgo(queueing_time_);
     if (queueing_time_ > MaxMessageDelay_) {
       // Message is too delayed.
       queueing_delay_counter_.Increment();
+#if defined(OS_WIN)
       if (kPlaySounds)
         MessageBeep(MB_ICONASTERISK);
+#endif
     }
   }
 
-  void DidProcessMessage(const MSG& msg) {
+  // Called when a message has just finished processing, finalizes
+  // per-message variables and timers.
+  void EndProcessingTimers() {
     total_time_watchdog_.Disarm();
     TimeTicks now = TimeTicks::Now();
     if (begin_process_message_ != TimeTicks()) {
@@ -147,14 +143,61 @@ class JankObserver : public base::RefCountedThreadSafe<JankObserver>,
         TimeDelta::FromMilliseconds(kMaxMessageProcessingMs)) {
       // Message took too long to process.
       slow_processing_counter_.Increment();
+#if defined(OS_WIN)
       if (kPlaySounds)
         MessageBeep(MB_ICONHAND);
+#endif
     }
   }
 
+#if defined(OS_WIN)
+  virtual void WillProcessMessage(const MSG& msg) {
+    begin_process_message_ = TimeTicks::Now();
+
+    // GetMessageTime returns a LONG (signed 32-bit) and GetTickCount returns
+    // a DWORD (unsigned 32-bit). They both wrap around when the time is longer
+    // than they can hold. I'm not sure if GetMessageTime wraps around to 0,
+    // or if the original time comes from GetTickCount, it might wrap around
+    // to -1.
+    //
+    // Therefore, I cast to DWORD so if it wraps to -1 we will correct it. If
+    // it doesn't, then our time delta will be negative if a message happens
+    // to straddle the wraparound point, it will still be OK.
+    DWORD cur_message_issue_time = static_cast<DWORD>(msg.time);
+    DWORD cur_time = GetTickCount();
+    queueing_time_ =
+        base::TimeDelta::FromMilliseconds(cur_time - cur_message_issue_time);
+
+    StartProcessingTimers();
+  }
+
+  virtual void DidProcessMessage(const MSG& msg) {
+    EndProcessingTimers();
+  }
+#elif defined(OS_LINUX)
+  virtual void WillProcessEvent(GdkEvent* event) {
+    begin_process_message_ = TimeTicks::Now();
+    // TODO(evanm): we want to set queueing_time_ using
+    // event_utils::GetGdkEventTime, but how do you convert that info
+    // into a delta?
+    // guint event_time = event_utils::GetGdkEventTime(event);
+    queueing_time_ = base::TimeDelta::FromMilliseconds(0);
+    StartProcessingTimers();
+  }
+
+  virtual void DidProcessEvent(GdkEvent* event) {
+    EndProcessingTimers();
+  }
+#endif
+
  private:
   const TimeDelta MaxMessageDelay_;
+
+  // Time at which the current message processing began.
   TimeTicks begin_process_message_;
+
+  // Time the current message spent in the queue -- delta between message
+  // construction time and message processing time.
   TimeDelta queueing_time_;
 
   // Counters for the two types of jank we measure.
@@ -173,7 +216,7 @@ JankObserver* io_observer = NULL;
 
 }  // namespace
 
-void InstallJankometer(const CommandLine &parsed_command_line) {
+void InstallJankometer(const CommandLine& parsed_command_line) {
   if (ui_observer || io_observer) {
     NOTREACHED() << "Initializing jank-o-meter twice";
     return;
diff --git a/chrome/browser/jankometer.h b/chrome/browser/jankometer.h
index bd2f5d5..fe5d4f6 100644
--- a/chrome/browser/jankometer.h
+++ b/chrome/browser/jankometer.h
@@ -1,9 +1,9 @@
-// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
+// Copyright (c) 2009 The Chromium Authors. All rights reserved.
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
-#ifndef CHROME_BROWSER_JANKOMETER_H__
-#define CHROME_BROWSER_JANKOMETER_H__
+#ifndef CHROME_BROWSER_JANKOMETER_H_
+#define CHROME_BROWSER_JANKOMETER_H_
 
 class CommandLine;
 
@@ -14,9 +14,9 @@ class CommandLine;
 //
 // This function will initialize the service, which will install itself in
 // critical threads. It should be called on the UI thread.
-void InstallJankometer(const CommandLine &parsed_command_line);
+void InstallJankometer(const CommandLine& parsed_command_line);
 
 // Clean up Jank-O-Meter junk
 void UninstallJankometer();
 
-#endif  // CHROME_BROWSER_JANKOMETER_H__
+#endif  // CHROME_BROWSER_JANKOMETER_H_
-- 
cgit v1.1