• 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 { BaseStruct } from './BaseStruct';
18import { WakeupBean } from './WakeupBean';
19
20export class CpuStruct extends BaseStruct {
21  static cpuCount: number; //最大cpu数量
22  static hoverCpuStruct: CpuStruct | undefined;
23  static selectCpuStruct: CpuStruct | undefined;
24  static wakeupBean: WakeupBean | null | undefined;
25  cpu: number | undefined;
26  dur: number | undefined;
27  end_state: string | undefined;
28  id: number | undefined;
29  name: string | undefined;
30  priority: number | undefined;
31  processCmdLine: string | undefined;
32  processId: number | undefined;
33  processName: string | undefined;
34  schedId: number | undefined;
35  startTime: number | undefined;
36  tid: number | undefined;
37  type: string | undefined;
38
39  static draw(cpuBeanStructCanvasCtx: CanvasRenderingContext2D, cpuBeanStruct: CpuStruct) {
40    if (cpuBeanStruct.frame) {
41      let cpuBeanStructWidth = cpuBeanStruct.frame.width || 0;
42      if (cpuBeanStruct.processId === CpuStruct.hoverCpuStruct?.processId || !CpuStruct.hoverCpuStruct) {
43        cpuBeanStructCanvasCtx.fillStyle = ColorUtils.colorForTid(
44          (cpuBeanStruct.processId || 0) > 0 ? cpuBeanStruct.processId || 0 : cpuBeanStruct.tid || 0
45        );
46      } else {
47        cpuBeanStructCanvasCtx.fillStyle = '#e0e0e0';
48      }
49      cpuBeanStructCanvasCtx.fillRect(
50        cpuBeanStruct.frame.x,
51        cpuBeanStruct.frame.y,
52        cpuBeanStructWidth,
53        cpuBeanStruct.frame.height
54      );
55      if (cpuBeanStructWidth > textPadding * 2) {
56        let cpuBeanProcess = `${cpuBeanStruct.processName || 'Process'} [${cpuBeanStruct.processId}]`;
57        let cpuBeanThread = `${cpuBeanStruct.name || 'Thread'} [${cpuBeanStruct.tid}]`;
58        let cpuBeanProcessMeasure = cpuBeanStructCanvasCtx.measureText(cpuBeanProcess);
59        let cpuBeanThreadMeasure = cpuBeanStructCanvasCtx.measureText(cpuBeanThread);
60        let cpuBeanProcessCharWidth = Math.round(cpuBeanProcessMeasure.width / cpuBeanProcess.length);
61        let cpuBeanThreadCharWidth = Math.round(cpuBeanThreadMeasure.width / cpuBeanThread.length);
62        cpuBeanStructCanvasCtx.fillStyle = '#ffffff';
63        let y = cpuBeanStruct.frame.height / 2 + cpuBeanStruct.frame.y;
64        if (cpuBeanProcessMeasure.width < cpuBeanStructWidth - textPadding * 2) {
65          let x1 = Math.floor(
66            cpuBeanStructWidth / 2 - cpuBeanProcessMeasure.width / 2 + cpuBeanStruct.frame.x + textPadding
67          );
68          cpuBeanStructCanvasCtx.textBaseline = 'bottom';
69          cpuBeanStructCanvasCtx.fillText(cpuBeanProcess, x1, y, cpuBeanStructWidth - textPadding * 2);
70        } else if (cpuBeanStructWidth - textPadding * 2 > cpuBeanProcessCharWidth * 4) {
71          let chatNum = (cpuBeanStructWidth - textPadding * 2) / cpuBeanProcessCharWidth;
72          let x1 = cpuBeanStruct.frame.x + textPadding;
73          cpuBeanStructCanvasCtx.textBaseline = 'bottom';
74          cpuBeanStructCanvasCtx.fillText(
75            cpuBeanProcess.substring(0, chatNum - 4) + '...',
76            x1,
77            y,
78            cpuBeanStructWidth - textPadding * 2
79          );
80        }
81        if (cpuBeanThreadMeasure.width < cpuBeanStructWidth - textPadding * 2) {
82          cpuBeanStructCanvasCtx.textBaseline = 'top';
83          let x2 = Math.floor(
84            cpuBeanStructWidth / 2 - cpuBeanThreadMeasure.width / 2 + cpuBeanStruct.frame.x + textPadding
85          );
86          cpuBeanStructCanvasCtx.fillText(cpuBeanThread, x2, y + 2, cpuBeanStructWidth - textPadding * 2);
87        } else if (cpuBeanStructWidth - textPadding * 2 > cpuBeanThreadCharWidth * 4) {
88          let chatNum = (cpuBeanStructWidth - textPadding * 2) / cpuBeanThreadCharWidth;
89          let x1 = cpuBeanStruct.frame.x + textPadding;
90          cpuBeanStructCanvasCtx.textBaseline = 'top';
91          cpuBeanStructCanvasCtx.fillText(
92            cpuBeanThread.substring(0, chatNum - 4) + '...',
93            x1,
94            y + 2,
95            cpuBeanStructWidth - textPadding * 2
96          );
97        }
98      }
99      if (CpuStruct.selectCpuStruct && CpuStruct.equals(CpuStruct.selectCpuStruct, cpuBeanStruct)) {
100        cpuBeanStructCanvasCtx.strokeStyle = '#232c5d';
101        cpuBeanStructCanvasCtx.lineWidth = 2;
102        cpuBeanStructCanvasCtx.strokeRect(
103          cpuBeanStruct.frame.x,
104          cpuBeanStruct.frame.y,
105          cpuBeanStructWidth - 2,
106          cpuBeanStruct.frame.height
107        );
108      }
109    }
110  }
111
112  static equals(d1: CpuStruct, d2: CpuStruct): boolean {
113    if (
114      d1 &&
115      d2 &&
116      d1.cpu == d2.cpu &&
117      d1.tid == d2.tid &&
118      d1.processId == d2.processId &&
119      d1.startTime == d2.startTime &&
120      d1.dur == d2.dur
121    ) {
122      return true;
123    } else {
124      return false;
125    }
126  }
127}
128
129const textPadding = 2;
130