• 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 { TraceRow } from '../../component/trace/base/TraceRow';
17import { BaseStruct, dataFilterHandler, drawLoadingFrame, ns2x, Rect, Render } from './ProcedureWorkerCommon';
18import { ColorUtils } from '../../component/trace/base/ColorUtils';
19
20export class HiSysEventRender extends Render {
21  renderMainThread(
22    req: {
23      useCache: boolean;
24      context: CanvasRenderingContext2D;
25      type: string;
26    },
27    row: TraceRow<HiSysEventStruct>
28  ): void {
29    let hiSysEventList = row.dataList;
30    let hiSysEventFilter = row.dataListCache;
31    let minorFilter: HiSysEventStruct[] = [];
32    let criticalFilter: HiSysEventStruct[] = [];
33    let minorList = hiSysEventFilter.filter((struct) => {
34      return struct.depth === 0;
35    });
36    let criticalList = hiSysEventFilter.filter((struct) => {
37      return struct.depth === 1;
38    });
39    dataFilterHandler(minorList, minorFilter, {
40      startKey: 'ts',
41      durKey: 'dur',
42      startNS: TraceRow.range?.startNS ?? 0,
43      endNS: TraceRow.range?.endNS ?? 0,
44      totalNS: TraceRow.range?.totalNS ?? 0,
45      frame: row.frame,
46      paddingTop: padding * 2,
47      useCache: req.useCache || !(TraceRow.range?.refresh ?? false),
48    });
49    dataFilterHandler(criticalList, criticalFilter, {
50      startKey: 'ts',
51      durKey: 'dur',
52      startNS: TraceRow.range?.startNS ?? 0,
53      endNS: TraceRow.range?.endNS ?? 0,
54      totalNS: TraceRow.range?.totalNS ?? 0,
55      frame: row.frame,
56      paddingTop: rectHeight + padding * 2,
57      useCache: req.useCache || !(TraceRow.range?.refresh ?? false),
58    });
59    hiSysEventFilter = minorFilter.concat(criticalFilter);
60    drawLoadingFrame(req.context, row.dataListCache, row);
61    req.context.beginPath();
62    let find = false;
63    for (let re of hiSysEventFilter) {
64      HiSysEventStruct.draw(req.context, re);
65    }
66    if (!find && row.isHover) {
67      HiSysEventStruct.hoverHiSysEventStruct = undefined;
68    }
69    req.context.closePath();
70  }
71}
72
73export function hiSysEvent(
74  hiSysEventList: Array<HiSysEventStruct>,
75  hiSysEventFilter: Array<HiSysEventStruct>,
76  startNS: number,
77  endNS: number,
78  totalNS: number,
79  row: TraceRow<HiSysEventStruct>,
80  use: boolean
81): void {
82  if (use && hiSysEventFilter.length > 0) {
83    for (let i = 0, len = hiSysEventFilter.length; i < len; i++) {
84      let item = hiSysEventFilter[i];
85      if ((item.startTs || 0) + (item.dur || 0) >= startNS && (item.startTs || 0) <= endNS) {
86        HiSysEventStruct.setSysEventFrame(item, startNS, endNS, totalNS, row.frame);
87      } else {
88        item.frame = undefined;
89      }
90    }
91    return;
92  }
93  hiSysEventFilter.length = 0;
94  if (hiSysEventList) {
95    for (let index = 0; index < hiSysEventList.length; index++) {
96      let item = hiSysEventList[index];
97      if ((item.startTs || 0) + (item.dur || 0) >= startNS && (item.startTs || 0) <= endNS) {
98        HiSysEventStruct.setSysEventFrame(item, startNS, endNS, totalNS, row.frame);
99        hiSysEventFilter.push(item);
100      }
101    }
102  }
103}
104
105export class HiSysEventStruct extends BaseStruct {
106  static hoverHiSysEventStruct: HiSysEventStruct | undefined;
107  static selectHiSysEventStruct: HiSysEventStruct | undefined;
108  id: number | undefined;
109  domain: string | undefined;
110  eventName: string | undefined;
111  eventType: string | undefined;
112  startTs: number | undefined;
113  tz: string | undefined;
114  pid: number | undefined;
115  tid: number | undefined;
116  uid: number | undefined;
117  info: string | undefined;
118  level: string | undefined;
119  seq: number | undefined;
120  contents: string | undefined;
121  dur: number | undefined;
122  depth: number | undefined;
123
124  static setSysEventFrame(
125    sysEventNode: HiSysEventStruct,
126    startNS: number,
127    endNS: number,
128    totalNS: number,
129    frame: Rect
130  ): void {
131    let x1: number, x2: number;
132    if ((sysEventNode.startTs || 0) >= startNS && (sysEventNode.startTs || 0) <= endNS) {
133      x1 = ns2x(sysEventNode.startTs || 0, startNS, endNS, totalNS, frame);
134    } else {
135      x1 = 0;
136    }
137    if (
138      (sysEventNode.startTs || 0) + (sysEventNode.dur || 0) >= startNS &&
139      (sysEventNode.startTs || 0) + (sysEventNode.dur || 0) <= endNS
140    ) {
141      x2 = ns2x((sysEventNode.startTs || 0) + (sysEventNode.dur || 0), startNS, endNS, totalNS, frame);
142    } else {
143      x2 = frame.width;
144    }
145    if (!sysEventNode.frame) {
146      sysEventNode.frame = new Rect(0, 0, 0, 0);
147    }
148    let getV: number = x2 - x1 < 1 ? 1 : x2 - x1;
149    sysEventNode.frame.x = Math.floor(x1);
150    sysEventNode.frame.y = sysEventNode.depth! * rectHeight + padding * 2;
151    sysEventNode.frame.width = Math.ceil(getV);
152    sysEventNode.frame.height = 20;
153  }
154
155  static draw(ctx: CanvasRenderingContext2D, data: HiSysEventStruct): void {
156    if (data.depth === undefined || data.depth === null) {
157      return;
158    }
159    if (data.frame) {
160      ctx.globalAlpha = 1;
161      ctx.fillStyle = ColorUtils.getHisysEventColor(data.depth!);
162      ctx.fillRect(data.frame.x, data.frame.y + padding * data.depth, data.frame.width, rectHeight);
163    }
164  }
165}
166
167const padding = 5;
168const rectHeight = 10;
169