• 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 {
17    BaseStruct, drawFlagLine,
18    drawLines,
19    drawLoading,
20    drawSelection, drawWakeUp,
21    ns2x,
22    Render,
23    RequestMessage
24} from "./ProcedureWorkerCommon.js";
25import {ColorUtils} from "../../component/trace/base/ColorUtils.js";
26
27export class CpuFreqLimitRender extends Render{
28    render(req: RequestMessage, list: Array<any>, filter: Array<any>) {
29        if (req.lazyRefresh) {
30            freqLimits(list, filter, req.startNS, req.endNS, req.totalNS, req.frame, req.useCache || !req.range.refresh);
31        } else {
32            if (!req.useCache) {
33                freqLimits(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            let maxFreq = req.params.maxFreq;
44            let maxFreqName = req.params.maxFreqName;
45            drawLines(req.context, req.xs, req.frame.height, req.lineColor)
46            CpuFreqLimitsStruct.hoverCpuFreqLimitsStruct = 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                        CpuFreqLimitsStruct.hoverCpuFreqLimitsStruct = re;
51                        break;
52                    }
53                }
54            } else {
55                CpuFreqLimitsStruct.hoverCpuFreqLimitsStruct = req.params.hoverCpuFreqLimitsStruct;
56            }
57            CpuFreqLimitsStruct.selectCpuFreqLimitsStruct = req.params.selectCpuFreqLimitsStruct;
58            for (let re of filter) {
59                CpuFreqLimitsStruct.draw(req.context, re,maxFreq)
60            }
61            drawSelection(req.context, req.params);
62            req.context.closePath();
63            let s = maxFreqName
64            let textMetrics = req.context.measureText(s);
65            req.context.globalAlpha = 0.8
66            req.context.fillStyle = "#f0f0f0"
67            req.context.fillRect(0, 5, textMetrics.width + 8, 18)
68            req.context.globalAlpha = 1
69            req.context.fillStyle = "#333"
70            req.context.textBaseline = "middle"
71            req.context.fillText(s, 4, 5 + 9)
72            drawWakeUp(req.context, req.wakeupBean, req.startNS, req.endNS, req.totalNS, req.frame);
73            drawFlagLine(req.context, req.flagMoveInfo, req.flagSelectedInfo, req.startNS, req.endNS, req.totalNS, req.frame, req.slicesTime);
74        }
75        // @ts-ignore
76        self.postMessage({
77            id: req.id,
78            type: req.type,
79            results: req.canvas ? undefined : filter,
80            hover: CpuFreqLimitsStruct.hoverCpuFreqLimitsStruct
81        });
82    }
83}
84
85export function freqLimits(list: Array<any>, res: Array<any>, startNS: number, endNS: number, totalNS: number, frame: any, use: boolean) {
86    if (use && res.length > 0) {
87        res.forEach(it => CpuFreqLimitsStruct.setFreqLimitFrame(it, 5, startNS || 0, endNS || 0, totalNS || 0, frame));
88        return;
89    }
90    res.length = 0;
91    if (list) {
92        for (let i = 0, len = list.length; i < len; i++) {
93            let it = list[i];
94            if (i === list.length - 1) {
95                it.dur = (endNS || 0) - (it.startNs || 0)
96            } else {
97                it.dur = (list[i + 1].startNs || 0) - (it.startNs || 0)
98            }
99            if ((it.startNs || 0) + (it.dur || 0) > (startNS || 0) && (it.startNs || 0) < (endNS || 0)) {
100                CpuFreqLimitsStruct.setFreqLimitFrame(list[i], 5, startNS || 0, endNS || 0, totalNS || 0, frame)
101                if (i > 0 && ((list[i - 1].frame?.x || 0) == (list[i].frame?.x || 0) && (list[i - 1].frame?.width || 0) == (list[i].frame?.width || 0))) {
102
103                } else {
104                    res.push(it)
105                }
106            }
107        }
108    }
109
110}
111
112export class CpuFreqLimitsStruct extends BaseStruct {
113    static hoverCpuFreqLimitsStruct: CpuFreqLimitsStruct | undefined;
114    static selectCpuFreqLimitsStruct: CpuFreqLimitsStruct | undefined;
115    static minAlpha = 0.4;
116    static maxAlpha = 0.8;
117    startNs: number | undefined;
118    dur:number = 0;
119    max: number | undefined;
120    min: number | undefined;
121    cpu:number = 0;
122
123    static draw(ctx: CanvasRenderingContext2D, data: CpuFreqLimitsStruct,maxFreq:number) {
124        if (data.frame) {
125            let width = data.frame.width || 0;
126            let drawMaxHeight: number = Math.floor(((data.max || 0) * (data.frame.height || 0)) / maxFreq);
127            let drawMinHeight: number = Math.floor(((data.min || 0) * (data.frame.height || 0)) / maxFreq);
128            let index = data.cpu || 0
129            index += 2
130            ctx.fillStyle = ColorUtils.colorForTid(index)
131            ctx.strokeStyle = ColorUtils.colorForTid(index)
132            if (data.startNs === CpuFreqLimitsStruct.hoverCpuFreqLimitsStruct?.startNs||data.startNs === CpuFreqLimitsStruct.selectCpuFreqLimitsStruct?.startNs) {
133                ctx.lineWidth = 1;
134                ctx.globalAlpha = this.minAlpha;
135                this.drawArcLine(ctx,data,drawMaxHeight,drawMaxHeight - drawMinHeight)
136                ctx.globalAlpha = this.maxAlpha;
137                this.drawArcLine(ctx,data,drawMinHeight,drawMinHeight)
138            } else {
139                ctx.globalAlpha = this.minAlpha;
140                ctx.lineWidth = 1;
141                ctx.fillRect(data.frame.x, data.frame.y + data.frame.height - drawMaxHeight, width, drawMaxHeight - drawMinHeight)
142                ctx.globalAlpha = this.maxAlpha;
143                ctx.fillRect(data.frame.x, data.frame.y + data.frame.height - drawMinHeight, width, drawMinHeight)
144            }
145        }
146        ctx.globalAlpha = 1.0;
147        ctx.lineWidth = 1;
148    }
149
150    static drawArcLine(ctx: CanvasRenderingContext2D,data: CpuFreqLimitsStruct,yStartHeight:number,drawHeight:number){
151        if (data.frame) {
152            let width = data.frame.width || 0;
153            ctx.fillRect(data.frame.x, data.frame.y + data.frame.height - yStartHeight, width, drawHeight)
154            ctx.globalAlpha = this.maxAlpha;
155            ctx.beginPath()
156            ctx.arc(data.frame.x, data.frame.y + data.frame.height - yStartHeight, 3, 0, 2 * Math.PI, true)
157            ctx.fill()
158            ctx.stroke();
159            ctx.beginPath()
160            ctx.moveTo(data.frame.x + 3, data.frame.y + data.frame.height - yStartHeight);
161            ctx.lineWidth = 3;
162            ctx.lineTo(data.frame.x + width, data.frame.y + data.frame.height - yStartHeight)
163            ctx.stroke();
164        }
165    }
166
167    static setFreqLimitFrame(node: any, padding: number, startNS: number, endNS: number, totalNS: number, frame: any) {
168        let x1: number, x2: number;
169        if ((node.startNs || 0) < startNS) {
170            x1 = 0;
171        } else {
172            x1 = ns2x((node.startNs || 0), startNS, endNS, totalNS, frame);
173        }
174        if ((node.startNs || 0) + (node.dur || 0) > endNS) {
175            x2 = frame.width;
176        } else {
177            x2 = ns2x((node.startNs || 0) + (node.dur || 0), startNS, endNS, totalNS, frame);
178        }
179        let getV: number = x2 - x1 <= 1 ? 1 : x2 - x1;
180        if (!node.frame) {
181            node.frame = {};
182        }
183        node.frame.x = Math.floor(x1);
184        node.frame.y = frame.y + padding;
185        node.frame.width = Math.ceil(getV);
186        node.frame.height = Math.floor(frame.height - padding * 2);
187    }
188}