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