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