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 {getTabMemoryAbilityData, queryStartTime} from "../../../database/SqlLite.js"; 20import {SystemMemorySummary} from "../../../bean/AbilityMonitor.js"; 21import {Utils} from "../base/Utils.js"; 22import "../../../component/SpFilter.js"; 23import {log} from "../../../../log/Log.js"; 24 25@element('tabpane-memory-ability') 26export class TabPaneMemoryAbility extends BaseElement { 27 private tbl: LitTable | null | undefined; 28 private source: Array<SystemMemorySummary> = []; 29 private float: HTMLDivElement | null | undefined; 30 private queryResult: Array<SystemMemorySummary> = [] 31 private search: HTMLInputElement | undefined | null 32 33 set data(val: SelectionParam | any) { 34 // @ts-ignore 35 this.tbl?.shadowRoot.querySelector(".table").style.height = (this.parentElement.clientHeight - 45) + "px" 36 this.queryDataByDB(val) 37 } 38 39 initElements(): void { 40 this.tbl = this.shadowRoot?.querySelector<LitTable>('#tb-memory-ability'); 41 new ResizeObserver((entries) => { 42 if (this.parentElement?.clientHeight != 0) { 43 // @ts-ignore 44 this.tbl?.shadowRoot.querySelector(".table").style.height = (this.parentElement.clientHeight - 45) + "px" 45 this.tbl?.reMeauseHeight() 46 } 47 }).observe(this.parentElement!) 48 this.tbl!.addEventListener('column-click', (evt) => { 49 // @ts-ignore 50 this.sortByColumn(evt.detail) 51 }); 52 } 53 54 filterData() { 55 if (this.queryResult.length > 0) { 56 let filter = this.queryResult.filter((item) => { 57 let array = this.toMemoryAbilityArray(item) 58 let isInclude = array.filter(value => value.indexOf(this.search!.value) > -1); 59 return isInclude.length > 0 60 }); 61 if (filter.length > 0) { 62 this.source = filter; 63 this.tbl!.recycleDataSource = this.source; 64 } else { 65 this.source = [] 66 this.tbl!.recycleDataSource = []; 67 } 68 } 69 } 70 71 toMemoryAbilityArray(systemMemorySummary: SystemMemorySummary): any[] { 72 let array: Array<string> = [] 73 array.push(systemMemorySummary.startTimeStr) 74 array.push(systemMemorySummary.durationStr) 75 array.push(systemMemorySummary.memoryTotal) 76 array.push(systemMemorySummary.cached) 77 array.push(systemMemorySummary.swapTotal) 78 return array 79 } 80 81 queryDataByDB(val: SelectionParam | any) { 82 queryStartTime().then(res => { 83 let startTime = res[0].start_ts; 84 getTabMemoryAbilityData(val.leftNs + startTime, val.rightNs + startTime).then(items => { 85 log("getTabMemoryAbilityData result size : " + items.length) 86 this.source = [] 87 this.queryResult = []; 88 if (items.length != null && items.length > 0) { 89 let lastTime = 0; 90 for (const item of items) { 91 let systemMemorySummary = new SystemMemorySummary() 92 if (item.startTime - startTime <= 0) { 93 systemMemorySummary.startTimeStr = '0:000.000.000'; 94 } else { 95 systemMemorySummary.startTimeStr = Utils.getTimeStampHMS(item.startTime - startTime); 96 } 97 if (lastTime !== 0) { 98 systemMemorySummary.durationNumber = item.startTime - lastTime; 99 systemMemorySummary.durationStr = Utils.getDurString(systemMemorySummary.durationNumber); 100 } else { 101 systemMemorySummary.durationNumber = 0; 102 systemMemorySummary.durationStr = '-'; 103 } 104 lastTime = item.startTime; 105 let memorys = item.value.split(","); 106 let names = item.name.split(","); 107 if (memorys.length != names.length) { 108 continue; 109 } 110 for (let i = 0; i < names.length; i++) { 111 switch (names[i]) { 112 case "sys.mem.total": 113 systemMemorySummary.memoryTotal = Utils.getBinaryByteWithUnit(Number(memorys[i])) 114 break; 115 case "sys.mem.free": 116 systemMemorySummary.memFree = Utils.getBinaryByteWithUnit(Number(memorys[i])) 117 break; 118 case "sys.mem.buffers": 119 systemMemorySummary.buffers = Utils.getBinaryByteWithUnit(Number(memorys[i])) 120 break; 121 case "sys.mem.cached": 122 systemMemorySummary.cached = Utils.getBinaryByteWithUnit(Number(memorys[i])) 123 break; 124 case "sys.mem.shmem": 125 systemMemorySummary.shmem = Utils.getBinaryByteWithUnit(Number(memorys[i])) 126 break; 127 case "sys.mem.slab": 128 systemMemorySummary.slab = Utils.getBinaryByteWithUnit(Number(memorys[i])) 129 break; 130 case "sys.mem.swap.total": 131 systemMemorySummary.swapTotal = Utils.getBinaryByteWithUnit(Number(memorys[i])) 132 break; 133 case "sys.mem.swap.free": 134 systemMemorySummary.swapFree = Utils.getBinaryByteWithUnit(Number(memorys[i])) 135 break; 136 case "sys.mem.mapped": 137 systemMemorySummary.mapped = Utils.getBinaryByteWithUnit(Number(memorys[i])) 138 break; 139 case "sys.mem.vmalloc.used": 140 systemMemorySummary.vmallocUsed = Utils.getBinaryByteWithUnit(Number(memorys[i])) 141 break; 142 case "sys.mem.page.tables": 143 systemMemorySummary.pageTables = Utils.getBinaryByteWithUnit(Number(memorys[i])) 144 break; 145 case "sys.mem.kernel.stack": 146 systemMemorySummary.kernelStack = Utils.getBinaryByteWithUnit(Number(memorys[i])) 147 break; 148 case "sys.mem.active": 149 systemMemorySummary.active = Utils.getBinaryByteWithUnit(Number(memorys[i])) 150 break; 151 case "sys.mem.inactive": 152 systemMemorySummary.inactive = Utils.getBinaryByteWithUnit(Number(memorys[i])) 153 break; 154 case "sys.mem.unevictable": 155 systemMemorySummary.unevictable = Utils.getBinaryByteWithUnit(Number(memorys[i])) 156 break; 157 case "sys.mem.vmalloc.total": 158 systemMemorySummary.vmallocTotal = Utils.getBinaryByteWithUnit(Number(memorys[i])) 159 break; 160 case "sys.mem.slab.unreclaimable": 161 systemMemorySummary.sUnreclaim = Utils.getBinaryByteWithUnit(Number(memorys[i])) 162 break; 163 case "sys.mem.cma.total": 164 systemMemorySummary.cmaTotal = Utils.getBinaryByteWithUnit(Number(memorys[i])) 165 break; 166 case "sys.mem.cma.free": 167 systemMemorySummary.cmaFree = Utils.getBinaryByteWithUnit(Number(memorys[i])) 168 break; 169 case "sys.mem.kernel.reclaimable": 170 systemMemorySummary.kReclaimable = Utils.getBinaryByteWithUnit(Number(memorys[i])) 171 break; 172 case "sys.mem.zram": 173 systemMemorySummary.zram = Utils.getBinaryByteWithUnit(Number(memorys[i]) * 1000) 174 break; 175 } 176 } 177 this.source.push(systemMemorySummary); 178 } 179 this.tbl!.recycleDataSource = this.source; 180 } else { 181 this.source = [] 182 this.tbl!.recycleDataSource = []; 183 } 184 }) 185 }); 186 if (this.tbl) { 187 let th = this.tbl.shadowRoot?.querySelector<HTMLDivElement>(".th") 188 th!.style.gridColumnGap = "5px"; 189 } 190 } 191 192 initHtml(): string { 193 return ` 194<style> 195:host{ 196 display: flex; 197 flex-direction: column; 198 padding: 10px 10px; 199} 200#tb-memory-ability{ 201 overflow-x:auto; 202} 203</style> 204 <lit-table id="tb-memory-ability" style="height: auto"> 205 <lit-table-column order width="150px" title="StartTime" data-index="startTimeStr" key="startTimeStr" align="flex-start"></lit-table-column> 206 <lit-table-column order width="100px" title="Duration" data-index="durationStr" key="durationStr" align="flex-start" ></lit-table-column> 207 <lit-table-column order width="100px" title="MemTotal" data-index="memoryTotal" key="memoryTotal" align="flex-start" ></lit-table-column> 208 <lit-table-column order width="100px" title="MemFree" data-index="memFree" key="memFree" align="flex-start" ></lit-table-column> 209 <lit-table-column order width="100px" title="Buffers" data-index="buffers" key="buffers" align="flex-start" ></lit-table-column> 210 <lit-table-column order width="100px" title="Cached" data-index="cached" key="cached" align="flex-start" ></lit-table-column> 211 <lit-table-column order width="100px" title="Shmem" data-index="shmem" key="shmem" align="flex-start" ></lit-table-column> 212 <lit-table-column order width="100px" title="Slab" data-index="slab" key="slab" align="flex-start" ></lit-table-column> 213 <lit-table-column order width="120px" title="SUnreclaim" data-index="sUnreclaim" key="sUnreclaim" align="flex-start" ></lit-table-column> 214 <lit-table-column order width="100px" title="SwapTotal" data-index="swapTotal" key="swapTotal" align="flex-start" ></lit-table-column> 215 <lit-table-column order width="100px" title="SwapFree" data-index="swapFree" key="swapFree" align="flex-start" ></lit-table-column> 216 <lit-table-column order width="100px" title="Mapped" data-index="mapped" key="mapped" align="flex-start" ></lit-table-column> 217 <lit-table-column order width="120px" title="VmallocUsed" data-index="vmallocUsed" key="vmallocUsed" align="flex-start" ></lit-table-column> 218 <lit-table-column order width="100px" title="PageTables" data-index="pageTables" key="pageTables" align="flex-start" ></lit-table-column> 219 <lit-table-column order width="120px" title="KernelStack" data-index="kernelStack" key="kernelStack" align="flex-start" ></lit-table-column> 220 <lit-table-column order width="120px" title="KReclaimable" data-index="kReclaimable" key="kReclaimable" align="flex-start" ></lit-table-column> 221 <lit-table-column order width="100px" title="Active" data-index="active" key="active" align="flex-start" ></lit-table-column> 222 <lit-table-column order width="100px" title="Inactive" data-index="inactive" key="inactive" align="flex-start" ></lit-table-column> 223 <lit-table-column order width="120px" title="Unevictable" data-index="unevictable" key="unevictable" align="flex-start" ></lit-table-column> 224 <lit-table-column order width="120px" title="VmallocTotal" data-index="vmallocTotal" key="vmallocTotal" align="flex-start" ></lit-table-column> 225 <lit-table-column order width="100px" title="CmaTotal" data-index="cmaTotal" key="cmaTotal" align="flex-start" ></lit-table-column> 226 <lit-table-column order width="100px" title="CmaFree" data-index="cmaFree" key="cmaFree" align="flex-start" ></lit-table-column> 227 <lit-table-column order width="100px" title="Zram" data-index="zram" key="zram" align="flex-start" ></lit-table-column> 228</lit-table> 229 `; 230 } 231 232 sortByColumn(detail: any) { 233 // @ts-ignore 234 function compare(property, sort, type) { 235 return function (a: SystemMemorySummary, b: SystemMemorySummary) { 236 if (type === 'number') { 237 // @ts-ignore 238 return sort === 2 ? parseFloat(b[property]) - parseFloat(a[property]) : parseFloat(a[property]) - parseFloat(b[property]); 239 } else if (type === 'durationStr') { 240 return sort === 2 ? b.durationNumber - a.durationNumber : a.durationNumber - b.durationNumber; 241 } else { 242 // @ts-ignore 243 if (b[property] > a[property]) { 244 return sort === 2 ? 1 : -1; 245 } else { // @ts-ignore 246 if (b[property] == a[property]) { 247 return 0; 248 } else { 249 return sort === 2 ? -1 : 1; 250 } 251 } 252 } 253 } 254 } 255 256 if (detail.key === 'startTime') { 257 this.source.sort(compare(detail.key, detail.sort, 'string')) 258 } else if (detail.key === 'durationStr') { 259 this.source.sort(compare(detail.key, detail.sort, 'durationStr')) 260 } else { 261 this.source.sort(compare(detail.key, detail.sort, 'number')) 262 } 263 this.tbl!.recycleDataSource = this.source; 264 } 265} 266 267 268