• 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,
18  dataFilterHandler,
19  isFrameContainPoint,
20  Rect,
21  Render,
22  RequestMessage,
23  drawString,
24  drawLoadingFrame,
25} from './ProcedureWorkerCommon';
26import { TraceRow } from '../../component/trace/base/TraceRow';
27import { Utils } from '../../component/trace/base/Utils';
28import { ThreadStruct as BaseThreadStruct } from '../../bean/ThreadStruct';
29import {SpSystemTrace} from "../../component/SpSystemTrace";
30export class ThreadRender extends Render {
31  renderMainThread(
32    threadReq: {
33      context: CanvasRenderingContext2D;
34      useCache: boolean;
35      type: string;
36      translateY: number;
37    },
38    row: TraceRow<ThreadStruct>
39  ) {
40    let threadList = row.dataList;
41    let threadFilter = row.dataListCache;
42    dataFilterHandler(threadList, threadFilter, {
43      startKey: 'startTime',
44      durKey: 'dur',
45      startNS: TraceRow.range?.startNS ?? 0,
46      endNS: TraceRow.range?.endNS ?? 0,
47      totalNS: TraceRow.range?.totalNS ?? 0,
48      frame: row.frame,
49      paddingTop: 5,
50      useCache: threadReq.useCache || !(TraceRow.range?.refresh ?? false),
51    });
52    drawLoadingFrame(threadReq.context, threadFilter, row);
53    threadReq.context.beginPath();
54    for (let re of threadFilter) {
55      re.translateY = threadReq.translateY;
56      ThreadStruct.drawThread(threadReq.context, re);
57      if (row.isHover && re.frame && isFrameContainPoint(re.frame!, row.hoverX, row.hoverY)) {
58        ThreadStruct.hoverThreadStruct = re;
59      }
60    }
61    threadReq.context.closePath();
62  }
63
64  render(threadReq: RequestMessage, threadList: Array<any>, threadFilter: Array<any>) {}
65}
66
67const padding = 3;
68export function ThreadStructOnClick(clickRowType:string,sp:SpSystemTrace,threadClickHandler:any,cpuClickHandler:any){
69  return new Promise((resolve, reject) => {
70    if (clickRowType === TraceRow.ROW_TYPE_THREAD && ThreadStruct.hoverThreadStruct) {
71      sp.removeLinkLinesByBusinessType('thread');
72      ThreadStruct.selectThreadStruct = ThreadStruct.hoverThreadStruct;
73      sp.timerShaftEL?.drawTriangle(ThreadStruct.selectThreadStruct!.startTime || 0, 'inverted');
74      sp.traceSheetEL?.displayThreadData(ThreadStruct.selectThreadStruct, threadClickHandler, cpuClickHandler);
75      sp.timerShaftEL?.modifyFlagList(undefined);
76      reject();
77    }else{
78      resolve(null);
79    }
80  });
81}
82export class ThreadStruct extends BaseThreadStruct {
83  static otherColor = '#673ab7';
84  static uninterruptibleSleepColor = '#f19d38';
85  static uninterruptibleSleepNonIOColor = '#795548';
86  static traceColor = '#0d47a1';
87  static sColor = '#FBFBFB';
88  static hoverThreadStruct: ThreadStruct | undefined;
89  static selectThreadStruct: ThreadStruct | undefined;
90  static selectThreadStructList: Array<ThreadStruct> = new Array<ThreadStruct>();
91  argSetID: number | undefined;
92  translateY: number | undefined;
93  textMetricsWidth: number | undefined;
94
95  static drawThread(threadContext: CanvasRenderingContext2D, data: ThreadStruct) {
96    if (data.frame) {
97      threadContext.globalAlpha = 1;
98      let stateText = ThreadStruct.getEndState(data.state || '');
99      threadContext.fillStyle = Utils.getStateColor(data.state || '');
100      if ('S' === data.state) {
101        threadContext.globalAlpha = 0.2;
102      }
103      threadContext.fillRect(data.frame.x, data.frame.y + padding, data.frame.width, data.frame.height - padding * 2);
104      threadContext.fillStyle = '#fff';
105      threadContext.textBaseline = 'middle';
106      threadContext.font = '8px sans-serif';
107      if ('S' !== data.state) {
108        data.frame.width > 7 && drawString(threadContext, stateText, 2, data.frame, data);
109      }
110      if (
111        ThreadStruct.selectThreadStruct &&
112        ThreadStruct.equals(ThreadStruct.selectThreadStruct, data) &&
113        ThreadStruct.selectThreadStruct.state != 'S'
114      ) {
115        threadContext.strokeStyle = '#232c5d';
116        threadContext.lineWidth = 2;
117        threadContext.strokeRect(
118          data.frame.x,
119          data.frame.y + padding,
120          data.frame.width - 2,
121          data.frame.height - padding * 2
122        );
123      }
124    }
125  }
126}
127