Home | History | Annotate | Download | only in front_end
      1 /*
      2  * Copyright (C) 2013 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  * @constructor
     33  * @extends {WebInspector.TimelineOverviewBase}
     34  * @param {!WebInspector.TimelineModel} model
     35  */
     36 WebInspector.TimelineMemoryOverview = function(model)
     37 {
     38     WebInspector.TimelineOverviewBase.call(this, model);
     39     this.element.id = "timeline-overview-memory";
     40 
     41     this._maxHeapSizeLabel = this.element.createChild("div", "max memory-graph-label");
     42     this._minHeapSizeLabel = this.element.createChild("div", "min memory-graph-label");
     43 }
     44 
     45 WebInspector.TimelineMemoryOverview.prototype = {
     46     update: function()
     47     {
     48         this.resetCanvas();
     49 
     50         var records = this._model.records;
     51         if (!records.length)
     52             return;
     53 
     54         const lowerOffset = 3;
     55         var maxUsedHeapSize = 0;
     56         var minUsedHeapSize = 100000000000;
     57         var minTime = this._model.minimumRecordTime();
     58         var maxTime = this._model.maximumRecordTime();
     59         WebInspector.TimelinePresentationModel.forAllRecords(records, function(r) {
     60             maxUsedHeapSize = Math.max(maxUsedHeapSize, r.usedHeapSize || maxUsedHeapSize);
     61             minUsedHeapSize = Math.min(minUsedHeapSize, r.usedHeapSize || minUsedHeapSize);
     62         });
     63         minUsedHeapSize = Math.min(minUsedHeapSize, maxUsedHeapSize);
     64 
     65         var width = this._canvas.width;
     66         var height = this._canvas.height - lowerOffset;
     67         var xFactor = width / (maxTime - minTime);
     68         var yFactor = height / Math.max(maxUsedHeapSize - minUsedHeapSize, 1);
     69 
     70         var histogram = new Array(width);
     71         WebInspector.TimelinePresentationModel.forAllRecords(records, function(r) {
     72             if (!r.usedHeapSize)
     73                 return;
     74             var x = Math.round((WebInspector.TimelineModel.endTimeInSeconds(r) - minTime) * xFactor);
     75             var y = Math.round((r.usedHeapSize - minUsedHeapSize) * yFactor);
     76             histogram[x] = Math.max(histogram[x] || 0, y);
     77         });
     78 
     79         height++; // +1 so that the border always fit into the canvas area.
     80 
     81         var y = 0;
     82         var isFirstPoint = true;
     83         var ctx = this._context;
     84         ctx.beginPath();
     85         ctx.moveTo(0, this._canvas.height);
     86         for (var x = 0; x < histogram.length; x++) {
     87             if (typeof histogram[x] === "undefined")
     88                 continue;
     89             if (isFirstPoint) {
     90                 isFirstPoint = false;
     91                 y = histogram[x];
     92                 ctx.lineTo(0, height - y);
     93             }
     94             ctx.lineTo(x, height - y);
     95             y = histogram[x];
     96             ctx.lineTo(x, height - y);
     97         }
     98         ctx.lineTo(width, height - y);
     99         ctx.lineTo(width, this._canvas.height);
    100         ctx.lineTo(0, this._canvas.height);
    101         ctx.closePath();
    102 
    103         ctx.lineWidth = 0.5;
    104         ctx.strokeStyle = "rgba(20,0,0,0.8)";
    105         ctx.stroke();
    106 
    107         ctx.fillStyle = "rgba(214,225,254, 0.8);";
    108         ctx.fill();
    109 
    110         this._maxHeapSizeLabel.textContent = Number.bytesToString(maxUsedHeapSize);
    111         this._minHeapSizeLabel.textContent = Number.bytesToString(minUsedHeapSize);
    112     },
    113 
    114     __proto__: WebInspector.TimelineOverviewBase.prototype
    115 }
    116