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, getTabSmapsMaxRss,} from "../../../../database/SqlLite.js"; 20import {Smaps, SmapsTreeObj} from "../../../../bean/SmapsStruct.js"; 21import {Utils} from "../../base/Utils.js"; 22 23@element('tabpane-smaps-statistics') 24export class TabPaneSmapsStatistics extends BaseElement { 25 private tbl: LitTable | null | undefined; 26 27 set data(val: SelectionParam | any) { 28 // @ts-ignore 29 this.tbl?.shadowRoot?.querySelector(".table").style.height = (this.parentElement.clientHeight - 45) + "px" 30 this.queryDataByDB(val) 31 } 32 33 initElements(): void { 34 this.tbl = this.shadowRoot?.querySelector<LitTable>('#tb-smaps-statistics'); 35 } 36 37 connectedCallback() { 38 super.connectedCallback(); 39 new ResizeObserver((entries) => { 40 if (this.parentElement?.clientHeight != 0) { 41 // @ts-ignore 42 this.tbl?.shadowRoot.querySelector(".table").style.height = (this.parentElement.clientHeight - 45) + "px" 43 this.tbl?.reMeauseHeight() 44 } 45 }).observe(this.parentElement!); 46 } 47 48 49 queryDataByDB(val: SelectionParam | any) { 50 getTabSmapsMaxRss(val.leftNs, val.rightNs).then(maxRes => { 51 let sumRss = maxRes[0].max_value 52 let allTree: SmapsTreeObj = new SmapsTreeObj("All", "", "*All*"); 53 let dataTree: SmapsTreeObj = new SmapsTreeObj("DATA", "", "DATA"); 54 let textTree: SmapsTreeObj = new SmapsTreeObj("TEXT", "", "TEXT"); 55 let constTree: SmapsTreeObj = new SmapsTreeObj("CONST", "", "CONST"); 56 let otherTree: SmapsTreeObj = new SmapsTreeObj("OTHER", "", "OTHER"); 57 getTabSmapsData(val.leftNs, val.rightNs).then(result => { 58 if (result.length != null && result.length > 0) { 59 for (let id = 0; id < result.length; id++) { 60 let smaps = result[id] 61 switch (smaps.permission.trim()) { 62 case "rw-": 63 smaps.type = "DATA" 64 this.handleTree(smaps, id, "DATA", dataTree, sumRss); 65 break; 66 case "r-x": 67 smaps.type = "TEXT" 68 this.handleTree(smaps, id, "TEXT", textTree, sumRss); 69 break; 70 case "r--": 71 smaps.type = "CONST" 72 this.handleTree(smaps, id, "CONST", constTree, sumRss); 73 break; 74 default: 75 smaps.type = "OTHER" 76 this.handleTree(smaps, id, "OTHER", otherTree, sumRss); 77 break; 78 } 79 this.handleAllDataTree(smaps, id, "All", allTree, sumRss); 80 if (id == result.length - 1) { 81 this.handleSmapsTreeObj(dataTree, sumRss) 82 this.handleSmapsTreeObj(textTree, sumRss) 83 this.handleSmapsTreeObj(constTree, sumRss) 84 this.handleSmapsTreeObj(otherTree, sumRss) 85 this.handleSmapsTreeObj(allTree, sumRss) 86 } 87 } 88 this.tbl!.recycleDataSource = [allTree, dataTree, textTree, constTree, otherTree] 89 } else { 90 this.tbl!.recycleDataSource = [] 91 } 92 }) 93 }) 94 95 } 96 97 private handleSmapsTreeObj(smapsTreeObj: SmapsTreeObj, sumRss: number) { 98 smapsTreeObj.rsspro = (smapsTreeObj.rss / sumRss) * 100 99 smapsTreeObj.rssproStr = smapsTreeObj.rsspro.toFixed(2) + "%" 100 smapsTreeObj.regStr = smapsTreeObj.reg + "" 101 smapsTreeObj.rssStr = Utils.getBinaryByteWithUnit(smapsTreeObj.rss * 1024) 102 smapsTreeObj.dirtyStr = Utils.getBinaryByteWithUnit(smapsTreeObj.dirty * 1024) 103 smapsTreeObj.swapperStr = Utils.getBinaryByteWithUnit(smapsTreeObj.swapper * 1024) 104 smapsTreeObj.sizeStr = Utils.getBinaryByteWithUnit(smapsTreeObj.size * 1024) 105 smapsTreeObj.respro = smapsTreeObj.rss / smapsTreeObj.size * 100 106 smapsTreeObj.pssStr = Utils.getBinaryByteWithUnit(smapsTreeObj.pss * 1024) 107 smapsTreeObj.resproStr = smapsTreeObj.respro.toFixed(2) + "%" 108 } 109 110 111 private handleAllDataTree(smaps: Smaps, id: number, parentId: string, dataTree: SmapsTreeObj, sumRss: number) { 112 let type = smaps.type 113 let obj = new SmapsTreeObj(id + "", parentId, type); 114 obj.path = smaps.path 115 obj.rss = smaps.rss 116 obj.rsspro = (smaps.rss / sumRss) * 100 117 obj.rssproStr = obj.rsspro.toFixed(2) + "%" 118 obj.rssStr = Utils.getBinaryByteWithUnit(smaps.rss * 1024) 119 obj.dirty = smaps.dirty 120 obj.dirtyStr = Utils.getBinaryByteWithUnit(smaps.dirty * 1024) 121 obj.swapper = smaps.swapper 122 obj.swapperStr = Utils.getBinaryByteWithUnit(smaps.swapper * 1024) 123 obj.size = smaps.size 124 obj.sizeStr = Utils.getBinaryByteWithUnit(smaps.size * 1024) 125 obj.pss = smaps.pss 126 obj.pssStr = Utils.getBinaryByteWithUnit(smaps.pss * 1024) 127 obj.respro = smaps.reside 128 obj.resproStr = smaps.reside.toFixed(2) + "%" 129 dataTree.reg += 1 130 if (dataTree.children.length > 1 && dataTree.path != "< multiple >") { 131 dataTree.path = "< multiple >" 132 } 133 dataTree.rss += smaps.rss 134 dataTree.dirty += smaps.dirty 135 dataTree.swapper += smaps.swapper 136 dataTree.size += smaps.size 137 dataTree.respro += smaps.reside 138 dataTree.pss += smaps.pss 139 dataTree.children.push(obj) 140 } 141 142 private handleTree(smaps: Smaps, id: number, parentId: string, dataTree: SmapsTreeObj, sumRss: number) { 143 let type = smaps.start_addr + " (" + smaps.size / 4 + " pages)" 144 let obj = new SmapsTreeObj(id + "", parentId, type); 145 obj.path = smaps.path 146 obj.rss = smaps.rss 147 obj.rsspro = (smaps.rss / sumRss) * 100 148 obj.rssproStr = obj.rsspro.toFixed(2) + "%" 149 obj.rssStr = Utils.getBinaryByteWithUnit(smaps.rss * 1024) 150 obj.dirty = smaps.dirty 151 obj.dirtyStr = Utils.getBinaryByteWithUnit(smaps.dirty * 1024) 152 obj.swapper = smaps.swapper 153 obj.swapperStr = Utils.getBinaryByteWithUnit(smaps.swapper * 1024) 154 obj.size = smaps.size 155 obj.sizeStr = Utils.getBinaryByteWithUnit(smaps.size * 1024) 156 obj.pss = smaps.pss 157 obj.pssStr = Utils.getBinaryByteWithUnit(smaps.pss * 1024) 158 obj.respro = smaps.reside 159 obj.resproStr = smaps.reside.toFixed(2) + "%" 160 dataTree.reg += 1 161 if (dataTree.children.length > 1 && dataTree.path != "< multiple >") { 162 dataTree.path = "< multiple >" 163 } 164 dataTree.rss += smaps.rss 165 dataTree.dirty += smaps.dirty 166 dataTree.swapper += smaps.swapper 167 dataTree.size += smaps.size 168 dataTree.pss += smaps.pss 169 dataTree.children.push(obj) 170 } 171 172 initHtml(): string { 173 return ` 174 <style> 175 :host{ 176 display: flex; 177 flex-direction: column; 178 padding: 10px 10px; 179 } 180 </style> 181 <lit-table id="tb-smaps-statistics" style="height: auto" tree> 182 <lit-table-column width="250px" title="Type" data-index="type" key="type" align="flex-start" > 183 </lit-table-column> 184 <lit-table-column width="0.3fr" title="% of Res." data-index="rssproStr" key="rssproStr" align="flex-start" > 185 </lit-table-column> 186 <lit-table-column width="0.5fr" title="# Regs" data-index="regStr" key="regStr" align="flex-start" > 187 </lit-table-column> 188 <lit-table-column width="2fr" title="Path" data-index="path" key="path" align="flex-start" > 189 </lit-table-column> 190 <lit-table-column width="0.5fr" title="Resident Size" data-index="rssStr" key="rssStr" align="flex-start" > 191 </lit-table-column> 192 <lit-table-column width="0.5fr" title="Dirty Size" data-index="dirtyStr" key="dirtyStr" align="flex-start" > 193 </lit-table-column> 194 <lit-table-column width="0.5fr" title="Swapped" data-index="swapperStr" key="swapperStr" align="flex-start" > 195 </lit-table-column> 196 <lit-table-column width="0.5fr" title="Virtual Size" data-index="sizeStr" key="sizeStr" align="flex-start" > 197 </lit-table-column> 198 <lit-table-column width="0.5fr" title="Pss" data-index="pssStr" key="pssStr" align="flex-start" > 199 </lit-table-column> 200 <lit-table-column width="0.5fr" title="Res. %" data-index="resproStr" key="resproStr" align="flex-start" > 201 </lit-table-column> 202 </lit-table> 203 `; 204 } 205}