WebKit/chromium/src/js/ProfilerAgent.js
changeset 0 4f2f89ce4247
equal deleted inserted replaced
-1:000000000000 0:4f2f89ce4247
       
     1 /*
       
     2  * Copyright (C) 2010 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  *     * Redistributions of source code must retain the above copyright
       
     9  * notice, this list of conditions and the following disclaimer.
       
    10  *     * Redistributions in binary form must reproduce the above
       
    11  * copyright notice, this list of conditions and the following disclaimer
       
    12  * in the documentation and/or other materials provided with the
       
    13  * distribution.
       
    14  *     * Neither the name of Google Inc. nor the names of its
       
    15  * contributors may be used to endorse or promote products derived from
       
    16  * this software without specific prior written permission.
       
    17  *
       
    18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
       
    19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
       
    20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
       
    21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
       
    22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
       
    23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
       
    24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
       
    25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
       
    26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
       
    27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
       
    28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
       
    29  */
       
    30 
       
    31 /**
       
    32  * @fileoverview Provides communication interface to remote v8 profiler.
       
    33  */
       
    34 
       
    35 /**
       
    36  * @constructor
       
    37  */
       
    38 devtools.ProfilerAgent = function()
       
    39 {
       
    40     RemoteProfilerAgent.didGetActiveProfilerModules = this._didGetActiveProfilerModules.bind(this);
       
    41     RemoteProfilerAgent.didGetLogLines = this._didGetLogLines.bind(this);
       
    42 
       
    43     /**
       
    44      * Profiler log position.
       
    45      * @type {number}
       
    46      */
       
    47     this._logPosition = 0;
       
    48 
       
    49     /**
       
    50      * Last requested log position.
       
    51      * @type {number}
       
    52      */
       
    53     this._lastRequestedLogPosition = -1;
       
    54 
       
    55     /**
       
    56      * Profiler processor instance.
       
    57      * @type {devtools.profiler.Processor}
       
    58      */
       
    59     this._profilerProcessor = new devtools.profiler.Processor();
       
    60 };
       
    61 
       
    62 
       
    63 /**
       
    64  * A copy of enum from include/v8.h
       
    65  * @enum {number}
       
    66  */
       
    67 devtools.ProfilerAgent.ProfilerModules = {
       
    68     PROFILER_MODULE_NONE: 0,
       
    69     PROFILER_MODULE_CPU: 1,
       
    70     PROFILER_MODULE_HEAP_STATS: 1 << 1,
       
    71     PROFILER_MODULE_JS_CONSTRUCTORS: 1 << 2,
       
    72     PROFILER_MODULE_HEAP_SNAPSHOT: 1 << 16
       
    73 };
       
    74 
       
    75 
       
    76 /**
       
    77  * Initializes profiling state.
       
    78  */
       
    79 devtools.ProfilerAgent.prototype.initializeProfiling = function()
       
    80 {
       
    81     this._getNextLogLines(false);
       
    82 };
       
    83 
       
    84 
       
    85 /**
       
    86  * Requests the next chunk of log lines.
       
    87  * @param {boolean} immediately Do not postpone the request.
       
    88  * @private
       
    89  */
       
    90 devtools.ProfilerAgent.prototype._getNextLogLines = function(immediately)
       
    91 {
       
    92     if (this._lastRequestedLogPosition == this._logPosition)
       
    93         return;
       
    94     var pos = this._lastRequestedLogPosition = this._logPosition;
       
    95     if (immediately)
       
    96         RemoteProfilerAgent.getLogLines(pos);
       
    97     else
       
    98         setTimeout(function() { RemoteProfilerAgent.getLogLines(pos); }, 500);
       
    99 };
       
   100 
       
   101 
       
   102 /**
       
   103  * Starts profiling.
       
   104  * @param {number} modules List of modules to enable.
       
   105  */
       
   106 devtools.ProfilerAgent.prototype.startProfiling = function(modules)
       
   107 {
       
   108     if (modules & devtools.ProfilerAgent.ProfilerModules.PROFILER_MODULE_HEAP_SNAPSHOT) {
       
   109         InspectorBackend.takeHeapSnapshot();
       
   110         // Active modules will not change, instead, a snapshot will be logged.
       
   111         this._getNextLogLines();
       
   112     }
       
   113 };
       
   114 
       
   115 
       
   116 /**
       
   117  * Handles current profiler status.
       
   118  * @param {number} modules List of active (started) modules.
       
   119  */
       
   120 devtools.ProfilerAgent.prototype._didGetActiveProfilerModules = function(modules)
       
   121 {
       
   122 };
       
   123 
       
   124 
       
   125 /**
       
   126  * Handles a portion of a profiler log retrieved by getLogLines call.
       
   127  * @param {number} pos Current position in log.
       
   128  * @param {string} log A portion of profiler log.
       
   129  */
       
   130 devtools.ProfilerAgent.prototype._didGetLogLines = function(pos, log)
       
   131 {
       
   132     this._logPosition = pos;
       
   133     if (log.length > 0) {
       
   134         this._profilerProcessor.processLogChunk(log);
       
   135         this._getNextLogLines();
       
   136     } else {
       
   137         // Allow re-reading from the last position.
       
   138         this._lastRequestedLogPosition = this._logPosition - 1;
       
   139     }
       
   140 };