Upstream version 7.35.144.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / devtools / front_end / TimelineMemoryOverview.js
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     resetHeapSizeLabels: function()
47     {
48         this._maxHeapSizeLabel.textContent = "";
49         this._minHeapSizeLabel.textContent = "";
50     },
51
52     update: function()
53     {
54         this.resetCanvas();
55
56         var records = this._model.records();
57         if (!records.length) {
58             this.resetHeapSizeLabels();
59             return;
60         }
61
62         const lowerOffset = 3;
63         var maxUsedHeapSize = 0;
64         var minUsedHeapSize = 100000000000;
65         var minTime = this._model.minimumRecordTime();
66         var maxTime = this._model.maximumRecordTime();
67         this._model.forAllRecords(function(r) {
68             if (!r.counters || !r.counters.jsHeapSizeUsed)
69                 return;
70             maxUsedHeapSize = Math.max(maxUsedHeapSize, r.counters.jsHeapSizeUsed);
71             minUsedHeapSize = Math.min(minUsedHeapSize, r.counters.jsHeapSizeUsed);
72         });
73         minUsedHeapSize = Math.min(minUsedHeapSize, maxUsedHeapSize);
74
75         var width = this._canvas.width;
76         var height = this._canvas.height - lowerOffset;
77         var xFactor = width / (maxTime - minTime);
78         var yFactor = height / Math.max(maxUsedHeapSize - minUsedHeapSize, 1);
79
80         var histogram = new Array(width);
81         this._model.forAllRecords(function(r) {
82             if (!r.counters || !r.counters.jsHeapSizeUsed)
83                 return;
84             var x = Math.round((r.endTime - minTime) * xFactor);
85             var y = (r.counters.jsHeapSizeUsed - minUsedHeapSize) * yFactor;
86             histogram[x] = Math.max(histogram[x] || 0, y);
87         });
88
89         var y = 0;
90         var isFirstPoint = true;
91         var ctx = this._context;
92         ctx.save();
93         ctx.translate(0.5, 0.5);
94         ctx.beginPath();
95         ctx.moveTo(-1, this._canvas.height);
96         for (var x = 0; x < histogram.length; x++) {
97             if (typeof histogram[x] === "undefined")
98                 continue;
99             if (isFirstPoint) {
100                 isFirstPoint = false;
101                 y = histogram[x];
102                 ctx.lineTo(-1, height - y);
103             }
104             ctx.lineTo(x, height - y);
105             y = histogram[x];
106             ctx.lineTo(x, height - y);
107         }
108         ctx.lineTo(width, height - y);
109         ctx.lineTo(width, this._canvas.height);
110         ctx.lineTo(-1, this._canvas.height);
111         ctx.closePath();
112
113         var gradient = ctx.createLinearGradient(0, 0, 0, height);
114         gradient.addColorStop(0, "rgba(192,204,255,1)");
115         gradient.addColorStop(1, "rgba(192,204,255,0.4)");
116         ctx.fillStyle = gradient;
117         ctx.fill();
118
119         ctx.lineWidth = 0.5;
120         ctx.strokeStyle = "#666";
121         ctx.stroke();
122         ctx.restore();
123
124         this._maxHeapSizeLabel.textContent = Number.bytesToString(maxUsedHeapSize);
125         this._minHeapSizeLabel.textContent = Number.bytesToString(minUsedHeapSize);
126     },
127
128     __proto__: WebInspector.TimelineOverviewBase.prototype
129 }