• 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 {BaseElement, element} from "../../../../../base-ui/BaseElement.js";
17import {LitTable} from "../../../../../base-ui/table/lit-table.js";
18import {SelectionParam} from "../../../../bean/BoxSelection.js";
19import {getTabSmapsData,} from "../../../../database/SqlLite.js";
20import {Utils} from "../../base/Utils.js";
21import {log} from "../../../../../log/Log.js";
22import {Smaps} from "../../../../bean/SmapsStruct.js";
23
24@element('tabpane-smaps-record')
25export class TabPaneSmapsRecord extends BaseElement {
26    private tbl: LitTable | null | undefined;
27    private source: Array<Smaps> = [];
28    private queryResult: Array<Smaps> = []
29
30    set data(val: SelectionParam | any) {
31        // @ts-ignore
32        this.tbl?.shadowRoot?.querySelector(".table").style.height = (this.parentElement.clientHeight - 45) + "px"
33        this.queryDataByDB(val)
34    }
35
36    initElements(): void {
37        this.tbl = this.shadowRoot?.querySelector<LitTable>('#tb-smaps-record');
38        this.tbl!.addEventListener('column-click', (evt) => {
39            // @ts-ignore
40            this.sortByColumn(evt.detail)
41        });
42    }
43
44    connectedCallback() {
45        super.connectedCallback();
46        new ResizeObserver((entries) => {
47            if (this.parentElement?.clientHeight != 0) {
48                // @ts-ignore
49                this.tbl?.shadowRoot.querySelector(".table").style.height = (this.parentElement.clientHeight - 45) + "px"
50                this.tbl?.reMeauseHeight()
51            }
52        }).observe(this.parentElement!);
53    }
54
55    queryDataByDB(val: SelectionParam | any) {
56        getTabSmapsData(val.leftNs, val.rightNs).then(result => {
57            log("getTabSmapsData size :" + result.length);
58            if (result.length != null && result.length > 0) {
59                for (const smaps of result) {
60                    switch (smaps.permission.trim()) {
61                        case "rw-":
62                            smaps.type = "DATA"
63                            break;
64                        case "r-x":
65                            smaps.type = "TEXT"
66                            break;
67                        case "r--":
68                            smaps.type = "CONST"
69                            break;
70                        default:
71                            smaps.type = "OTHER"
72                            break;
73                    }
74                    smaps.address = smaps.start_addr  + " - " + smaps.end_addr
75                    smaps.dirtyStr = Utils.getBinaryByteWithUnit(smaps.dirty * 1024)
76                    smaps.swapperStr = Utils.getBinaryByteWithUnit(smaps.swapper * 1024)
77                    smaps.rssStr = Utils.getBinaryByteWithUnit(smaps.rss * 1024)
78                    smaps.pssStr = Utils.getBinaryByteWithUnit(smaps.pss * 1024)
79                    smaps.sizeStr = Utils.getBinaryByteWithUnit(smaps.size * 1024)
80                    let resideS = smaps.reside.toFixed(2)
81                    if (resideS == "0.00") {
82                        smaps.resideStr = "0 %"
83                    } else {
84                        smaps.resideStr = resideS + "%"
85                    }
86                }
87                this.source = result
88                this.queryResult = result;
89                this.tbl!.recycleDataSource = this.source
90            } else {
91                this.source = []
92                this.queryResult = []
93                this.tbl!.recycleDataSource = []
94            }
95        })
96    }
97
98    initHtml(): string {
99        return `
100        <style>
101        :host{
102            display: flex;
103            flex-direction: column;
104            padding: 10px 10px;
105        }
106
107        </style>
108        <lit-table id="tb-smaps-record" style="height: auto">
109            <lit-table-column order width="80px" title="Type" data-index="type" key="type" align="flex-start" >
110            </lit-table-column>
111            <lit-table-column order width="250px" title="Address Range" data-index="address" key="address" align="flex-start" >
112            </lit-table-column>
113            <lit-table-column order width="0.5fr" title="Dirty Size" data-index="dirtyStr" key="dirtyStr" align="flex-start" >
114            </lit-table-column>
115            <lit-table-column order width="0.5fr" title="Swapped" data-index="swapperStr" key="swapperStr" align="flex-start" >
116            </lit-table-column>
117            <lit-table-column order width="0.5fr" title="Resident Size" data-index="rssStr" key="rssStr" align="flex-start" >
118            </lit-table-column>
119            <lit-table-column order width="0.5fr" title="Virtual Size" data-index="sizeStr" key="sizeStr" align="flex-start" >
120            </lit-table-column>
121              <lit-table-column order width="0.5fr" title="Pss" data-index="pssStr" key="pssStr" align="flex-start" >
122            </lit-table-column>
123            <lit-table-column order width="0.5fr" title="Reside" data-index="resideStr" key="resideStr" align="flex-start" >
124            </lit-table-column>
125            <lit-table-column order width="0.5fr" title="Protection" data-index="permission" key="permission" align="flex-start" >
126            </lit-table-column>
127            <lit-table-column order width="1.5fr" title="Path" data-index="path" key="path" align="flex-start" >
128            </lit-table-column>
129        </lit-table>
130        `;
131    }
132
133    sortByColumn(detail: any) {
134        // @ts-ignore
135        function compare(property, sort, type) {
136            return function (a: Smaps, b: Smaps) {
137                if (type === 'number') {
138                    // @ts-ignore
139                    return sort === 2 ? parseFloat(b[property]) - parseFloat(a[property]) : parseFloat(a[property]) - parseFloat(b[property]);
140                } else {
141                    // @ts-ignore
142                    if (b[property] > a[property]) {
143                        return sort === 2 ? 1 : -1;
144                    } else { // @ts-ignore
145                        if (b[property] == a[property]) {
146                            return 0;
147                        } else {
148                            return sort === 2 ? -1 : 1;
149                        }
150                    }
151                }
152            }
153        }
154
155        if (detail.key === 'dirtyStr' || detail.key === 'swapperStr' || detail.key === 'rssStr' || detail.key === 'sizeStr' || detail.key === 'resideStr') {
156            let key = detail.key.substring(0, detail.key.indexOf("Str"))
157            this.source.sort(compare(key, detail.sort, 'number'))
158        }  else {
159            this.source.sort(compare(detail.key, detail.sort, 'string'))
160        }
161        this.tbl!.recycleDataSource = this.source;
162    }
163}