• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (C) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16import { ColorUtils } from "../../component/trace/base/ColorUtils.js";
17import {
18    BaseStruct,
19    drawFlagLine,
20    drawLines,
21    drawLoading,
22    drawSelection, drawWakeUp,
23    ns2x, Render,
24    RequestMessage
25} from "./ProcedureWorkerCommon.js";
26
27export class MemoryAbilityRender extends Render{
28    render(req: RequestMessage, list: Array<any>, filter: Array<any>) {
29        if (req.lazyRefresh) {
30            memoryAbility(list, filter, req.startNS, req.endNS, req.totalNS, req.frame, req.useCache || !req.range.refresh);
31        } else {
32            if (!req.useCache) {
33                memoryAbility(list, filter, req.startNS, req.endNS, req.totalNS, req.frame, false);
34            }
35        }
36        if (req.canvas) {
37            req.context.clearRect(0, 0, req.frame.width, req.frame.height);
38            let arr = filter;
39            if (arr.length > 0 && !req.range.refresh && !req.useCache && req.lazyRefresh) {
40                drawLoading(req.context, req.startNS, req.endNS, req.totalNS, req.frame, arr[0].startNS, arr[arr.length - 1].startNS + arr[arr.length - 1].dur)
41            }
42            req.context.beginPath();
43            MemoryAbilityMonitorStruct.maxMemoryByte = req.params.maxMemoryByte;
44            MemoryAbilityMonitorStruct.maxMemoryByteName = req.params.maxMemoryByteName;
45            drawLines(req.context, req.xs, req.frame.height, req.lineColor)
46            MemoryAbilityMonitorStruct.hoverMemoryAbilityStruct = undefined;
47            if (req.isHover) {
48                for (let re of filter) {
49                    if (re.frame && req.hoverX >= re.frame.x && req.hoverX <= re.frame.x + re.frame.width && req.hoverY >= re.frame.y && req.hoverY <= re.frame.y + re.frame.height) {
50                        MemoryAbilityMonitorStruct.hoverMemoryAbilityStruct = re;
51                        break;
52                    }
53                }
54            }
55            MemoryAbilityMonitorStruct.selectMemoryAbilityStruct = req.params.selectMemoryAbilityStruct;
56            for (let re of filter) {
57                MemoryAbilityMonitorStruct.draw(req.context, re)
58            }
59            drawSelection(req.context, req.params);
60            req.context.closePath();
61            let s = MemoryAbilityMonitorStruct.maxMemoryByteName
62            let textMetrics = req.context.measureText(s);
63            req.context.globalAlpha = 0.8
64            req.context.fillStyle = "#f0f0f0"
65            req.context.fillRect(0, 5, textMetrics.width + 8, 18)
66            req.context.globalAlpha = 1
67            req.context.fillStyle = "#333"
68            req.context.textBaseline = "middle"
69            req.context.fillText(s, 4, 5 + 9)
70            drawWakeUp(req.context, req.wakeupBean, req.startNS, req.endNS, req.totalNS, req.frame);
71            drawFlagLine(req.context, req.flagMoveInfo, req.flagSelectedInfo, req.startNS, req.endNS, req.totalNS, req.frame, req.slicesTime);
72        }
73        // @ts-ignore
74        self.postMessage({
75            id: req.id,
76            type: req.type,
77            results: req.canvas ? undefined : filter,
78            hover: MemoryAbilityMonitorStruct.hoverMemoryAbilityStruct
79        });
80    }
81}
82
83export function memoryAbility(list: Array<any>, res: Array<any>, startNS: number, endNS: number, totalNS: number, frame: any, use: boolean) {
84    if (use && res.length > 0) {
85        for (let i = 0; i < res.length; i++) {
86            let item = res[i];
87            if ((item.startNS || 0) + (item.dur || 0) > (startNS || 0) && (item.startNS || 0) < (endNS || 0)) {
88                MemoryAbilityMonitorStruct.setMemoryFrame(item, 5, startNS || 0, endNS || 0, totalNS || 0, frame)
89            } else {
90                item.frame = null;
91            }
92        }
93        return;
94    }
95    res.length = 0;
96    if (list) {
97        for (let index = 0; index < list.length; index++) {
98            let item = list[index];
99            if (index === list.length - 1) {
100                item.dur = (endNS || 0) - (item.startNS || 0)
101            } else {
102                item.dur = (list[index + 1].startNS || 0) - (item.startNS || 0)
103            }
104            if ((item.startNS || 0) + (item.dur || 0) > (startNS || 0) && (item.startNS || 0) < (endNS || 0)) {
105                MemoryAbilityMonitorStruct.setMemoryFrame(list[index], 5, startNS || 0, endNS || 0, totalNS || 0, frame)
106                if (index > 0 && ((list[index - 1].frame?.x || 0) == (list[index].frame?.x || 0) && (list[index - 1].frame?.width || 0) == (list[index].frame?.width || 0))) {
107
108                } else {
109                    res.push(item)
110                }
111            }
112        }
113    }
114}
115
116export class MemoryAbilityMonitorStruct extends BaseStruct {
117    static maxMemoryByte: number = 0
118    static maxMemoryByteName: string = "0 MB"
119    static hoverMemoryAbilityStruct: MemoryAbilityMonitorStruct | undefined;
120    static selectMemoryAbilityStruct: MemoryAbilityMonitorStruct | undefined;
121    cpu: number | undefined
122    value: number | undefined
123    startNS: number | undefined
124    dur: number | undefined
125
126    static draw(context2D: CanvasRenderingContext2D, data: MemoryAbilityMonitorStruct) {
127        if (data.frame) {
128            let width = data.frame.width || 0;
129            let index = 2;
130            context2D.fillStyle = ColorUtils.colorForTid(index)
131            context2D.strokeStyle = ColorUtils.colorForTid(index)
132            if (data.startNS === MemoryAbilityMonitorStruct.hoverMemoryAbilityStruct?.startNS) {
133                context2D.lineWidth = 1;
134                context2D.globalAlpha = 0.6;
135                let drawHeight: number = Math.floor(((data.value || 0) * (data.frame.height || 0) * 1.0) / MemoryAbilityMonitorStruct.maxMemoryByte);
136                context2D.fillRect(data.frame.x, data.frame.y + data.frame.height - drawHeight + 4, width, drawHeight)
137                context2D.beginPath()
138                context2D.arc(data.frame.x, data.frame.y + data.frame.height - drawHeight + 4, 3, 0, 2 * Math.PI, true)
139                context2D.fill()
140                context2D.globalAlpha = 1.0;
141                context2D.stroke();
142                context2D.beginPath()
143                context2D.moveTo(data.frame.x + 3, data.frame.y + data.frame.height - drawHeight + 4);
144                context2D.lineWidth = 3;
145                context2D.lineTo(data.frame.x + width, data.frame.y + data.frame.height - drawHeight + 4)
146                context2D.stroke();
147            } else {
148                context2D.globalAlpha = 0.6;
149                context2D.lineWidth = 1;
150                let drawHeight: number = Math.floor(((data.value || 0) * (data.frame.height || 0)) / MemoryAbilityMonitorStruct.maxMemoryByte);
151                context2D.fillRect(data.frame.x, data.frame.y + data.frame.height - drawHeight + 4, width, drawHeight)
152            }
153        }
154        context2D.globalAlpha = 1.0;
155        context2D.lineWidth = 1;
156    }
157
158    static setMemoryFrame(node: any, padding: number, startNS: number, endNS: number, totalNS: number, frame: any) {
159        let startPointX: number, endPointX: number
160
161        if ((node.startNS || 0) < startNS) {
162            startPointX = 0
163        } else {
164            startPointX = ns2x((node.startNS || 0), startNS, endNS, totalNS, frame);
165        }
166        if ((node.startNS || 0) + (node.dur || 0) > endNS) {
167            endPointX = frame.width;
168        } else {
169            endPointX = ns2x((node.startNS || 0) + (node.dur || 0), startNS, endNS, totalNS, frame);
170        }
171        let frameWidth: number = endPointX - startPointX <= 1 ? 1 : endPointX - startPointX;
172        if (!node.frame) {
173            node.frame = {};
174        }
175        node.frame.x = Math.floor(startPointX);
176        node.frame.y = frame.y + padding;
177        node.frame.width = Math.ceil(frameWidth);
178        node.frame.height = Math.floor(frame.height - padding * 2);
179    }
180}
181
182const textPadding = 2;
183