Home | History | Annotate | Download | only in sdk
      1 /*
      2  * Copyright (C) 2014 Google Inc. All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions are
      6  * met:
      7  *
      8  * 1. Redistributions of source code must retain the above copyright
      9  * notice, this list of conditions and the following disclaimer.
     10  *
     11  * 2. Redistributions in binary form must reproduce the above
     12  * copyright notice, this list of conditions and the following disclaimer
     13  * in the documentation and/or other materials provided with the
     14  * distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY GOOGLE INC. AND ITS CONTRIBUTORS
     17  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     18  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     19  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GOOGLE INC.
     20  * OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 /**
     30  * @constructor
     31  * @extends {WebInspector.SDKModel}
     32  * @param {!WebInspector.Target} target
     33  * @implements {ProfilerAgent.Dispatcher}
     34  */
     35 WebInspector.CPUProfilerModel = function(target)
     36 {
     37     WebInspector.SDKModel.call(this, WebInspector.CPUProfilerModel, target);
     38     this._isRecording = false;
     39     target.registerProfilerDispatcher(this);
     40     target.profilerAgent().enable();
     41 
     42     this._configureCpuProfilerSamplingInterval();
     43     WebInspector.settings.highResolutionCpuProfiling.addChangeListener(this._configureCpuProfilerSamplingInterval, this);
     44 }
     45 
     46 WebInspector.CPUProfilerModel.EventTypes = {
     47     ProfileStarted: "ProfileStarted",
     48     ProfileStopped: "ProfileStopped",
     49     ConsoleProfileStarted: "ConsoleProfileStarted",
     50     ConsoleProfileFinished: "ConsoleProfileFinished"
     51 };
     52 
     53 WebInspector.CPUProfilerModel.prototype = {
     54 
     55     _configureCpuProfilerSamplingInterval: function()
     56     {
     57         var intervalUs = WebInspector.settings.highResolutionCpuProfiling.get() ? 100 : 1000;
     58         this.target().profilerAgent().setSamplingInterval(intervalUs, didChangeInterval);
     59         function didChangeInterval(error)
     60         {
     61             if (error)
     62                 WebInspector.console.error(error);
     63         }
     64     },
     65 
     66     /**
     67      * @param {string} id
     68      * @param {!DebuggerAgent.Location} scriptLocation
     69      * @param {!ProfilerAgent.CPUProfile} cpuProfile
     70      * @param {string=} title
     71      */
     72     consoleProfileFinished: function(id, scriptLocation, cpuProfile, title)
     73     {
     74         // Make sure ProfilesPanel is initialized and CPUProfileType is created.
     75         self.runtime.loadModule("profiler");
     76         var debuggerLocation = WebInspector.DebuggerModel.Location.fromPayload(this.target(), scriptLocation);
     77         this.dispatchEventToListeners(WebInspector.CPUProfilerModel.EventTypes.ConsoleProfileFinished, {protocolId: id, scriptLocation: debuggerLocation, cpuProfile: cpuProfile, title: title});
     78     },
     79 
     80     /**
     81      * @param {string} id
     82      * @param {!DebuggerAgent.Location} scriptLocation
     83      * @param {string=} title
     84      */
     85     consoleProfileStarted: function(id, scriptLocation, title)
     86     {
     87         // Make sure ProfilesPanel is initialized and CPUProfileType is created.
     88         self.runtime.loadModule("profiler");
     89         var debuggerLocation = WebInspector.DebuggerModel.Location.fromPayload(this.target(), scriptLocation)
     90         this.dispatchEventToListeners(WebInspector.CPUProfilerModel.EventTypes.ConsoleProfileStarted, {protocolId: id, scriptLocation: debuggerLocation, title: title});
     91     },
     92 
     93     /**
     94       * @return {boolean}
     95       */
     96     isRecordingProfile: function()
     97     {
     98         return this._isRecording;
     99     },
    100 
    101     startRecording: function()
    102     {
    103         this._isRecording = true;
    104         this.target().profilerAgent().start();
    105         this.dispatchEventToListeners(WebInspector.CPUProfilerModel.EventTypes.ProfileStarted);
    106         WebInspector.userMetrics.ProfilesCPUProfileTaken.record();
    107     },
    108 
    109     /**
    110      * @param {!function(?string,?ProfilerAgent.CPUProfile)} callback
    111      */
    112     stopRecording: function(callback)
    113     {
    114         this._isRecording = false;
    115         this.target().profilerAgent().stop(callback);
    116         this.dispatchEventToListeners(WebInspector.CPUProfilerModel.EventTypes.ProfileStopped);
    117     },
    118 
    119     dispose: function()
    120     {
    121         WebInspector.settings.highResolutionCpuProfiling.removeChangeListener(this._configureCpuProfilerSamplingInterval, this);
    122     },
    123 
    124 
    125     __proto__: WebInspector.SDKModel.prototype
    126 }
    127 
    128 /**
    129  * @type {!WebInspector.CPUProfilerModel}
    130  */
    131 WebInspector.cpuProfilerModel;
    132