• 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';
17import {
18  BaseStruct,
19  dataFilterHandler,
20  isFrameContainPoint,
21  ns2x,
22  RequestMessage,
23  Render,
24  drawLoadingFrame,
25} from './ProcedureWorkerCommon';
26import { TraceRow } from '../../component/trace/base/TraceRow';
27
28export class CpuAbilityRender extends Render {
29  renderMainThread(
30    req: {
31      context: CanvasRenderingContext2D;
32      useCache: boolean;
33      type: string;
34      maxCpuUtilization: number;
35      maxCpuUtilizationName: string;
36    },
37    cpuAbilityRow: TraceRow<CpuAbilityMonitorStruct>
38  ): void {
39    let cpuAbilityList = cpuAbilityRow.dataList;
40    let cpuAbilityFilter = cpuAbilityRow.dataListCache;
41    dataFilterHandler(cpuAbilityList, cpuAbilityFilter, {
42      startKey: 'startNS',
43      durKey: 'dur',
44      startNS: TraceRow.range?.startNS ?? 0,
45      endNS: TraceRow.range?.endNS ?? 0,
46      totalNS: TraceRow.range?.totalNS ?? 0,
47      frame: cpuAbilityRow.frame,
48      paddingTop: 5,
49      useCache: req.useCache || !(TraceRow.range?.refresh ?? false),
50    });
51    let find = false;
52    drawLoadingFrame(req.context, cpuAbilityRow.dataListCache, cpuAbilityRow);
53    req.context.beginPath();
54    for (let re of cpuAbilityFilter) {
55      CpuAbilityMonitorStruct.draw(req.context, re, req.maxCpuUtilization, cpuAbilityRow.isHover);
56      if (
57        cpuAbilityRow.isHover &&
58        re.frame &&
59        isFrameContainPoint(re.frame, cpuAbilityRow.hoverX, cpuAbilityRow.hoverY)
60      ) {
61        CpuAbilityMonitorStruct.hoverCpuAbilityStruct = re;
62        find = true;
63      }
64    }
65    if (!find && cpuAbilityRow.isHover) {
66      CpuAbilityMonitorStruct.hoverCpuAbilityStruct = undefined;
67    }
68    req.context.closePath();
69    let textMetrics = req.context.measureText(req.maxCpuUtilizationName);
70    req.context.globalAlpha = 0.8;
71    req.context.fillStyle = '#f0f0f0';
72    req.context.fillRect(0, 5, textMetrics.width + 8, 18);
73    req.context.globalAlpha = 1;
74    req.context.fillStyle = '#333';
75    req.context.textBaseline = 'middle';
76    req.context.fillText(req.maxCpuUtilizationName, 4, 5 + 9);
77  }
78
79  render(req: RequestMessage, list: Array<any>, filter: Array<any>) {}
80}
81
82export class CpuAbilityMonitorStruct extends BaseStruct {
83  static maxCpuUtilization: number = 0;
84  static maxCpuUtilizationName: string = '0 %';
85  static hoverCpuAbilityStruct: CpuAbilityMonitorStruct | undefined;
86  static selectCpuAbilityStruct: CpuAbilityMonitorStruct | undefined;
87
88  type: number | undefined;
89  value: number | undefined;
90  startNS: number | undefined;
91  dur: number | undefined; //自补充,数据库没有返回
92
93  static draw(
94    cpuAbilityContext2D: CanvasRenderingContext2D,
95    cpuAbilityData: CpuAbilityMonitorStruct,
96    maxCpuUtilization: number,
97    isHover: boolean
98  ): void {
99    if (cpuAbilityData.frame) {
100      let width = cpuAbilityData.frame.width || 0;
101      let index = 2;
102      cpuAbilityContext2D.fillStyle = ColorUtils.colorForTid(index);
103      cpuAbilityContext2D.strokeStyle = ColorUtils.colorForTid(index);
104      if (cpuAbilityData.startNS === CpuAbilityMonitorStruct.hoverCpuAbilityStruct?.startNS && isHover) {
105        cpuAbilityContext2D.lineWidth = 1;
106        cpuAbilityContext2D.globalAlpha = 0.6;
107        let drawHeight: number = Math.floor(
108          ((cpuAbilityData.value || 0) * (cpuAbilityData.frame.height || 0) * 1.0) / maxCpuUtilization
109        );
110        let y = cpuAbilityData.frame.y + cpuAbilityData.frame.height - drawHeight + 4;
111        cpuAbilityContext2D.fillRect(cpuAbilityData.frame.x, y, width, drawHeight);
112        cpuAbilityContext2D.beginPath();
113        cpuAbilityContext2D.arc(cpuAbilityData.frame.x, y, 3, 0, 2 * Math.PI, true);
114        cpuAbilityContext2D.fill();
115        cpuAbilityContext2D.globalAlpha = 1.0;
116        cpuAbilityContext2D.stroke();
117        cpuAbilityContext2D.beginPath();
118        cpuAbilityContext2D.moveTo(cpuAbilityData.frame.x + 3, y);
119        cpuAbilityContext2D.lineWidth = 3;
120        cpuAbilityContext2D.lineTo(cpuAbilityData.frame.x + width, y);
121        cpuAbilityContext2D.stroke();
122      } else {
123        cpuAbilityContext2D.globalAlpha = 0.6;
124        cpuAbilityContext2D.lineWidth = 1;
125        let drawHeight: number = Math.floor(
126          ((cpuAbilityData.value || 0) * (cpuAbilityData.frame.height || 0)) / maxCpuUtilization
127        );
128        let rectY = cpuAbilityData.frame.y + cpuAbilityData.frame.height - drawHeight + 4;
129        cpuAbilityContext2D.fillRect(cpuAbilityData.frame.x, rectY, width, drawHeight);
130      }
131    }
132    cpuAbilityContext2D.globalAlpha = 1.0;
133    cpuAbilityContext2D.lineWidth = 1;
134  }
135
136  static setCpuAbilityFrame(
137    cpuAbilityNode: any,
138    padding: number,
139    startNS: number,
140    endNS: number,
141    totalNS: number,
142    frame: any
143  ) {
144    let cpuAbilityStartPointX: number, cpuAbilityEndPointX: number;
145
146    if ((cpuAbilityNode.startNS || 0) < startNS) {
147      cpuAbilityStartPointX = 0;
148    } else {
149      cpuAbilityStartPointX = ns2x(cpuAbilityNode.startNS || 0, startNS, endNS, totalNS, frame);
150    }
151    if ((cpuAbilityNode.startNS || 0) + (cpuAbilityNode.dur || 0) > endNS) {
152      cpuAbilityEndPointX = frame.width;
153    } else {
154      cpuAbilityEndPointX = ns2x(
155        (cpuAbilityNode.startNS || 0) + (cpuAbilityNode.dur || 0),
156        startNS,
157        endNS,
158        totalNS,
159        frame
160      );
161    }
162    let frameWidth: number =
163      cpuAbilityEndPointX - cpuAbilityStartPointX <= 1 ? 1 : cpuAbilityEndPointX - cpuAbilityStartPointX;
164    if (!cpuAbilityNode.frame) {
165      cpuAbilityNode.frame = {};
166    }
167    cpuAbilityNode.frame.x = Math.floor(cpuAbilityStartPointX);
168    cpuAbilityNode.frame.y = frame.y + padding;
169    cpuAbilityNode.frame.width = Math.ceil(frameWidth);
170    cpuAbilityNode.frame.height = Math.floor(frame.height - padding * 2);
171  }
172}
173
174export class CpuAbility {
175  context: any;
176  params: any;
177}
178