• 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 {BaseStruct, ColorUtils, ns2x} from "./ProcedureWorkerCommon.js";
17import {CpuStruct} from "./ProcedureWorkerCPU.js";
18
19export function proc(list: Array<any>, res: Array<any>, startNS: number, endNS: number, totalNS: number, frame: any,use:boolean) {
20    if(use && res.length > 0){
21        res.forEach(it=>ProcessStruct.setProcessFrame(it, 5, startNS || 0, endNS || 0, totalNS || 0, frame))
22        return;
23    }
24    res.length = 0 ;
25    if (list) {
26        for (let i = 0, len = list.length; i < len; i++) {
27            let it = list[i];
28            if ((it.startTime || 0) + (it.dur || 0) > (startNS || 0) && (it.startTime || 0) < (endNS || 0)) {
29                ProcessStruct.setProcessFrame(list[i], 5, startNS || 0, endNS || 0, totalNS || 0, frame)
30                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))) {
31
32                } else {
33                    res.push(list[i])
34                }
35            }
36        }
37    }
38}
39
40const padding = 1;
41
42export class ProcessStruct extends BaseStruct {
43    cpu: number | undefined
44    dur: number | undefined
45    id: number | undefined
46    pid: number | undefined
47    process: string | undefined
48    startTime: number | undefined
49    state: string | undefined
50    thread: string | undefined
51    tid: number | undefined
52    ts: number | undefined
53    type: string | undefined
54    utid: number | undefined
55
56    static draw(ctx: CanvasRenderingContext2D,path:Path2D, data: ProcessStruct,miniHeight:number) {
57        if (data.frame) {
58            path.rect(data.frame.x, data.frame.y + (data.cpu || 0) * miniHeight + padding, data.frame.width, miniHeight)
59        }
60    }
61
62    static setFrame(node: any, pns: number, startNS: number, endNS: number, frame: any) {
63        if ((node.startTime || 0) < startNS) {
64            node.frame.x = 0;
65        } else {
66            node.frame.x = Math.floor(((node.startTime || 0) - startNS) / pns);
67        }
68        if ((node.startTime || 0) + (node.dur || 0) > endNS) {
69            node.frame.width = frame.width - node.frame.x;
70        } else {
71            node.frame.width = Math.ceil(((node.startTime || 0) + (node.dur || 0) - startNS) / pns - node.frame.x);
72        }
73        if (node.frame.width < 1) {
74            node.frame.width = 1;
75        }
76    }
77
78    static setProcessFrame(node: any, padding: number, startNS: number, endNS: number, totalNS: number, frame: any) {
79        let x1: number;
80        let x2: number;
81        if ((node.startTime || 0) < startNS) {
82            x1 = 0;
83        } else {
84            x1 = ns2x((node.startTime || 0), startNS, endNS, totalNS, frame);
85        }
86        if ((node.startTime || 0) + (node.dur || 0) > endNS) {
87            x2 = frame.width;
88        } else {
89            x2 = ns2x((node.startTime || 0) + (node.dur || 0), startNS, endNS, totalNS, frame);
90        }
91        let getV: number = x2 - x1 <= 1 ? 1 : x2 - x1;
92        if (!node.frame) {
93            node.frame = {};
94        }
95        node.frame.x = Math.floor(x1);
96        node.frame.y = Math.floor(frame.y + 2);
97        node.frame.width = Math.ceil(getV);
98        node.frame.height = Math.floor(frame.height - padding * 2);
99    }
100}