1a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)/*
2a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * Copyright (C) 2013 Google Inc. All rights reserved.
3a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) *
4a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * Redistribution and use in source and binary forms, with or without
5a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * modification, are permitted provided that the following conditions are
6a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * met:
7a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) *
8a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) *     * Redistributions of source code must retain the above copyright
9a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * notice, this list of conditions and the following disclaimer.
10a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) *     * Redistributions in binary form must reproduce the above
11a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * copyright notice, this list of conditions and the following disclaimer
12a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * in the documentation and/or other materials provided with the
13a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * distribution.
14a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) *     * Neither the name of Google Inc. nor the names of its
15a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * contributors may be used to endorse or promote products derived from
16a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * this software without specific prior written permission.
17a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) *
18a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) */
30a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)
31a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)/**
32a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * @constructor
33a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * @extends {WebInspector.TimelineOverviewBase}
34a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * @param {!WebInspector.TimelineModel} model
355d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles) * @param {!WebInspector.TimelineUIUtils} uiUtils
36a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) */
375d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)WebInspector.TimelineMemoryOverview = function(model, uiUtils)
38a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles){
39a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    WebInspector.TimelineOverviewBase.call(this, model);
405d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)    this._uiUtils = uiUtils;
41a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    this.element.id = "timeline-overview-memory";
42a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)
4310f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch    this._heapSizeLabel = this.element.createChild("div", "memory-graph-label");
44a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)}
45a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)
46a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)WebInspector.TimelineMemoryOverview.prototype = {
4709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    resetHeapSizeLabels: function()
4809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    {
4910f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch        this._heapSizeLabel.textContent = "";
5009380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    },
5109380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
52a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    update: function()
53a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    {
54a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)        this.resetCanvas();
556f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch        var ratio = window.devicePixelRatio;
56a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)
57d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        var records = this._model.records();
5809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)        if (!records.length) {
5909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)            this.resetHeapSizeLabels();
60a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)            return;
6109380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)        }
62a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)
636f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch        var lowerOffset = 3 * ratio;
64a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)        var maxUsedHeapSize = 0;
65a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)        var minUsedHeapSize = 100000000000;
66a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)        var minTime = this._model.minimumRecordTime();
67a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)        var maxTime = this._model.maximumRecordTime();
685d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)        var uiUtils = this._uiUtils;
69f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu        /**
70f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu         * @param {!WebInspector.TimelineModel.Record} record
71f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu         */
72f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu        function calculateMinMaxSizes(record)
73f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu        {
745d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)            var counters = uiUtils.countersForRecord(record);
755d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)            if (!counters || !counters.jsHeapSizeUsed)
7610f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch                return;
7710f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch            maxUsedHeapSize = Math.max(maxUsedHeapSize, counters.jsHeapSizeUsed);
7810f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch            minUsedHeapSize = Math.min(minUsedHeapSize, counters.jsHeapSizeUsed);
79f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu        }
80f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu        this._model.forAllRecords(calculateMinMaxSizes);
81a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)        minUsedHeapSize = Math.min(minUsedHeapSize, maxUsedHeapSize);
82a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)
8310f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch        var lineWidth = 1;
84a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)        var width = this._canvas.width;
85a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)        var height = this._canvas.height - lowerOffset;
86a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)        var xFactor = width / (maxTime - minTime);
87a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch        var yFactor = (height - lineWidth) / Math.max(maxUsedHeapSize - minUsedHeapSize, 1);
88a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)
89a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)        var histogram = new Array(width);
90f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu
91f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu        /**
92f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu         * @param {!WebInspector.TimelineModel.Record} record
93f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu         */
94f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu        function buildHistogram(record)
95f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu        {
965d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)            var counters = uiUtils.countersForRecord(record);
975d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)            if (!counters || !counters.jsHeapSizeUsed)
98a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)                return;
99f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu            var x = Math.round((record.endTime() - minTime) * xFactor);
10010f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch            var y = Math.round((counters.jsHeapSizeUsed - minUsedHeapSize) * yFactor);
101a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)            histogram[x] = Math.max(histogram[x] || 0, y);
102f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu        }
103f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu        this._model.forAllRecords(buildHistogram);
104a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)
105a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)        var ctx = this._context;
106a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch        var heightBeyondView = height + lowerOffset + lineWidth;
107a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch
10810f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch        ctx.translate(0.5, 0.5);
10910f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch        ctx.beginPath();
11010f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch        ctx.moveTo(-lineWidth, heightBeyondView);
11110f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch        var y = 0;
11210f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch        var isFirstPoint = true;
11310f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch        var lastX = 0;
11410f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch        for (var x = 0; x < histogram.length; x++) {
11510f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch            if (typeof histogram[x] === "undefined")
11610f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch                continue;
11710f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch            if (isFirstPoint) {
11810f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch                isFirstPoint = false;
11910f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch                y = histogram[x];
12010f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch                ctx.lineTo(-lineWidth, height - y);
121a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)            }
12210f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch            var nextY = histogram[x];
12310f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch            if (Math.abs(nextY - y) > 2 && Math.abs(x - lastX) > 1)
12410f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch                ctx.lineTo(x, height - y);
12510f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch            y = nextY;
12610f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch            ctx.lineTo(x, height - y);
12710f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch            lastX = x;
128a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)        }
12910f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch        ctx.lineTo(width + lineWidth, height - y);
13010f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch        ctx.lineTo(width + lineWidth, heightBeyondView);
13110f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch        ctx.closePath();
13209380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
133a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch        ctx.fillStyle = "hsla(220, 90%, 70%, 0.2)";
134a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch        ctx.fill();
135a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch        ctx.lineWidth = lineWidth;
136a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch        ctx.strokeStyle = "hsl(220, 90%, 70%)";
137a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch        ctx.stroke();
138a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch
13910f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch        this._heapSizeLabel.textContent = WebInspector.UIString("%s \u2013 %s", Number.bytesToString(minUsedHeapSize), Number.bytesToString(maxUsedHeapSize));
140a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    },
141a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)
142a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    __proto__: WebInspector.TimelineOverviewBase.prototype
143a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)}
144