• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// @ts-nocheck
2/*
3 * Copyright (C) 2022 Huawei Device Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17import {BaseElement, element} from "../../../../base-ui/BaseElement.js";
18import {LitTable} from "../../../../base-ui/table/lit-table.js";
19import {SelectionParam} from "../../../bean/BoxSelection.js";
20import {
21    getTabStatesGroupByProcess,
22    getTabStatesGroupByProcessThread,
23    getTabStatesGroupByStatePidTid
24} from "../../../database/SqlLite.js";
25import {StateProcessThread} from "../../../bean/StateProcessThread.js";
26import {Utils} from "../base/Utils.js";
27
28@element('tabpane-pts')
29export class TabPanePTS extends BaseElement {
30    private tbl: LitTable | null | undefined;
31    private range: HTMLLabelElement | null | undefined;
32
33    set data(val: SelectionParam | any) {
34        this.range!.textContent = "Selected range: " + parseFloat(((val.rightNs - val.leftNs) / 1000000.0).toFixed(5)) + " ms"
35        Promise.all([
36            getTabStatesGroupByProcess(val.leftNs, val.rightNs),
37            getTabStatesGroupByProcessThread(val.leftNs, val.rightNs),
38            getTabStatesGroupByStatePidTid(val.leftNs, val.rightNs)]).then((values) => {
39            let processes = values[0];
40            processes.map((spt) => {
41                spt.id = "p" + spt.processId
42                spt.title = (spt.process ?? "Process") + "(" + spt.processId + ")";
43            });
44            let threadMap = this.groupByProcessToMap(values[1]);
45            let stateMap = this.groupByProcessThreadToMap(values[2]);
46            for (let process of processes) {
47                let threads = threadMap.get(process.processId);
48                if (threads != undefined) {
49                    threads!.map((spt) => {
50                        spt.id = "p" + spt.processId + "_" + "t" + spt.threadId;
51                        spt.pid = "p" + spt.processId;
52                        spt.title = (spt.thread ?? "Thread") + "(" + spt.threadId + ")"
53                    })
54                }
55                process.children = threads ?? [];
56                let map = stateMap.get(process.processId);
57                for (let thread of threads!) {
58                    let states = map!.get(thread.threadId);
59                    states!.map((spt) => {
60                        spt.id = "p" + spt.processId + "_" + "t" + spt.threadId + "_" + (spt.state == "R+" ? "RP" : spt.state)
61                        spt.pid = "p" + spt.processId + "_" + "t" + spt.threadId;
62                        spt.title = Utils.getEndState(spt.state)
63                    })
64                    thread.children = states ?? [];
65                }
66            }
67            this.tbl!.dataSource = processes;
68        })
69    }
70
71    initElements(): void {
72        this.tbl = this.shadowRoot?.querySelector<LitTable>('#tb-states');
73        this.range = this.shadowRoot?.querySelector('#time-range')
74    }
75
76    groupByThreadToMap(arr: Array<StateProcessThread>): Map<number, Array<StateProcessThread>> {
77        let map = new Map<number, Array<StateProcessThread>>();
78        for (let spt of arr) {
79            if (map.has(spt.threadId)) {
80                map.get(spt.threadId)!.push(spt);
81            } else {
82                let list: Array<StateProcessThread> = [];
83                list.push(spt);
84                map.set(spt.threadId, list);
85            }
86        }
87        return map;
88    }
89
90    groupByProcessToMap(arr: Array<StateProcessThread>): Map<number, Array<StateProcessThread>> {
91        let map = new Map<number, Array<StateProcessThread>>();
92        for (let spt of arr) {
93            if (map.has(spt.processId)) {
94                map.get(spt.processId)!.push(spt);
95            } else {
96                let list: Array<StateProcessThread> = [];
97                list.push(spt);
98                map.set(spt.processId, list);
99            }
100        }
101        return map;
102    }
103
104    groupByProcessThreadToMap(arr: Array<StateProcessThread>): Map<number, Map<number, Array<StateProcessThread>>> {
105        let map = new Map<number, Map<number, Array<StateProcessThread>>>();
106        let processMap = this.groupByProcessToMap(arr);
107        for (let key of processMap.keys()) {
108            let threadMap = this.groupByThreadToMap(processMap.get(key)!)
109            map.set(key, threadMap);
110        }
111        return map;
112    }
113
114    initHtml(): string {
115        return `
116<style>
117:host{
118    display: flex;
119    flex-direction: column;
120    padding: 10px 10px;
121}
122</style>
123<label id="time-range" style="width: 100%;height: 20px;text-align: end;font-size: 10pt;margin-bottom: 5px">Selected range:0.0 ms</label>
124<lit-table id="tb-states" style="height: auto" tree>
125    <lit-table-column width="27%" title="Process/Thread/State" data-index="title" key="title" align="flex-start"></lit-table-column>
126    <lit-table-column width="1fr" title="Count" data-index="count" key="count" align="flex-start" ></lit-table-column>
127    <lit-table-column width="1fr" title="Duration(ns)" data-index="wallDuration" key="wallDuration" align="flex-start" ></lit-table-column>
128    <lit-table-column width="1fr" title="Min Duration(ns)" data-index="minDuration" key="minDuration" align="flex-start" ></lit-table-column>
129    <lit-table-column width="1fr" title="Avg Duration(ns)" data-index="avgDuration" key="avgDuration" align="flex-start" ></lit-table-column>
130    <lit-table-column width="1fr" title="Max Duration(ns)" data-index="maxDuration" key="maxDuration" align="flex-start" ></lit-table-column>
131</lit-table>
132        `;
133    }
134
135}