Home | History | Annotate | Download | only in browser
      1 /*
      2  * Copyright (C) 2010 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.browser;
     18 
     19 import android.net.WebAddress;
     20 import android.os.Debug;
     21 import android.os.Process;
     22 import android.os.SystemClock;
     23 import android.util.Log;
     24 
     25 /**
     26  * Performance analysis
     27  */
     28 public class Performance {
     29 
     30     private static final String LOGTAG = "browser";
     31 
     32     private final static boolean LOGD_ENABLED =
     33             com.android.browser.Browser.LOGD_ENABLED;
     34 
     35     private static boolean mInTrace;
     36 
     37     // Performance probe
     38     private static final int[] SYSTEM_CPU_FORMAT = new int[] {
     39             Process.PROC_SPACE_TERM | Process.PROC_COMBINE,
     40             Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 1: user time
     41             Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 2: nice time
     42             Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 3: sys time
     43             Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 4: idle time
     44             Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 5: iowait time
     45             Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 6: irq time
     46             Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG  // 7: softirq time
     47     };
     48 
     49     private static long mStart;
     50     private static long mProcessStart;
     51     private static long mUserStart;
     52     private static long mSystemStart;
     53     private static long mIdleStart;
     54     private static long mIrqStart;
     55 
     56     private static long mUiStart;
     57 
     58     static void tracePageStart(String url) {
     59         if (BrowserSettings.getInstance().isTracing()) {
     60             String host;
     61             try {
     62                 WebAddress uri = new WebAddress(url);
     63                 host = uri.getHost();
     64             } catch (android.net.ParseException ex) {
     65                 host = "browser";
     66             }
     67             host = host.replace('.', '_');
     68             host += ".trace";
     69             mInTrace = true;
     70             Debug.startMethodTracing(host, 20 * 1024 * 1024);
     71         }
     72     }
     73 
     74     static void tracePageFinished() {
     75         if (mInTrace) {
     76             mInTrace = false;
     77             Debug.stopMethodTracing();
     78         }
     79     }
     80 
     81     static void onPageStarted() {
     82         mStart = SystemClock.uptimeMillis();
     83         mProcessStart = Process.getElapsedCpuTime();
     84         long[] sysCpu = new long[7];
     85         if (Process.readProcFile("/proc/stat", SYSTEM_CPU_FORMAT, null, sysCpu, null)) {
     86             mUserStart = sysCpu[0] + sysCpu[1];
     87             mSystemStart = sysCpu[2];
     88             mIdleStart = sysCpu[3];
     89             mIrqStart = sysCpu[4] + sysCpu[5] + sysCpu[6];
     90         }
     91         mUiStart = SystemClock.currentThreadTimeMillis();
     92     }
     93 
     94     static void onPageFinished(String url) {
     95         long[] sysCpu = new long[7];
     96         if (Process.readProcFile("/proc/stat", SYSTEM_CPU_FORMAT, null, sysCpu, null)) {
     97             String uiInfo =
     98                     "UI thread used " + (SystemClock.currentThreadTimeMillis() - mUiStart) + " ms";
     99             if (LOGD_ENABLED) {
    100                 Log.d(LOGTAG, uiInfo);
    101             }
    102             // The string that gets written to the log
    103             String performanceString =
    104                     "It took total " + (SystemClock.uptimeMillis() - mStart)
    105                             + " ms clock time to load the page." + "\nbrowser process used "
    106                             + (Process.getElapsedCpuTime() - mProcessStart)
    107                             + " ms, user processes used " + (sysCpu[0] + sysCpu[1] - mUserStart)
    108                             * 10 + " ms, kernel used " + (sysCpu[2] - mSystemStart) * 10
    109                             + " ms, idle took " + (sysCpu[3] - mIdleStart) * 10
    110                             + " ms and irq took " + (sysCpu[4] + sysCpu[5] + sysCpu[6] - mIrqStart)
    111                             * 10 + " ms, " + uiInfo;
    112             if (LOGD_ENABLED) {
    113                 Log.d(LOGTAG, performanceString + "\nWebpage: " + url);
    114             }
    115             if (url != null) {
    116                 // strip the url to maintain consistency
    117                 String newUrl = new String(url);
    118                 if (newUrl.startsWith("http://www.")) {
    119                     newUrl = newUrl.substring(11);
    120                 } else if (newUrl.startsWith("http://")) {
    121                     newUrl = newUrl.substring(7);
    122                 } else if (newUrl.startsWith("https://www.")) {
    123                     newUrl = newUrl.substring(12);
    124                 } else if (newUrl.startsWith("https://")) {
    125                     newUrl = newUrl.substring(8);
    126                 }
    127                 if (LOGD_ENABLED) {
    128                     Log.d(LOGTAG, newUrl + " loaded");
    129                 }
    130             }
    131         }
    132     }
    133 }
    134