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 { Utils } from '../../base/Utils'; 20import { ColorUtils } from '../../base/ColorUtils'; 21import { CpuFreqLimitsStruct } from '../../../../database/ui-worker/cpu/ProcedureWorkerCpuFreqLimits'; 22import { resizeObserver } from '../SheetUtils'; 23import { getCpuLimitFreqBoxSelect } from "../../../../database/sql/Cpu.sql"; 24 25@element('tabpane-cpu-freq-limits') 26export class TabPaneCpuFreqLimits extends BaseElement { 27 private cpuFreqLimitsTbl: LitTable | null | undefined; 28 private selectionParam: SelectionParam | null | undefined; 29 private cpuFreqLimitSource: CpuFreqLimit[] = []; 30 private cpuFreqLimitSortKey: string = 'cpu'; 31 private cpuFreqLimitSortType: number = 0; 32 33 set data(cpuFreqLimitSelection: SelectionParam | any) { 34 if (cpuFreqLimitSelection == this.selectionParam) { 35 return; 36 } 37 this.selectionParam = cpuFreqLimitSelection; 38 // @ts-ignore 39 this.cpuFreqLimitsTbl!.shadowRoot!.querySelector('.table').style.height = 40 this.parentElement!.clientHeight - 25 + 'px'; 41 let list: any[] = []; 42 getCpuLimitFreqBoxSelect(cpuFreqLimitSelection.cpuFreqLimit, cpuFreqLimitSelection.rightNs).then((res) => { 43 for (let it of res) { 44 if (!it.dur || it.startNs + it.dur > cpuFreqLimitSelection.rightNs) { 45 it.dur = (cpuFreqLimitSelection.rightNs || 0) - (it.startNs || 0); 46 } 47 } 48 this.formatData(res, cpuFreqLimitSelection.leftNs, cpuFreqLimitSelection.rightNs); 49 }); 50 } 51 52 initElements(): void { 53 this.cpuFreqLimitsTbl = this.shadowRoot!.querySelector<LitTable>('#tb-cpu-freq-limit'); 54 this.cpuFreqLimitsTbl!.addEventListener('column-click', (evt) => { 55 // @ts-ignore 56 this.cpuFreqLimitSortKey = evt.detail.key; 57 // @ts-ignore 58 this.cpuFreqLimitSortType = evt.detail.sort; 59 // @ts-ignore 60 this.sortCpuFreqLimitTable(evt.detail.key, evt.detail.sort); 61 }); 62 } 63 64 connectedCallback() { 65 super.connectedCallback(); 66 resizeObserver(this.parentElement!, this.cpuFreqLimitsTbl!, 25); 67 } 68 69 formatData(list: CpuFreqLimitsStruct[], start: number, end: number) { 70 let limitsMap = new Map<string, CpuFreqLimit>(); 71 let groupMapData = (time: number, id: string, item: CpuFreqLimitsStruct) => { 72 if (limitsMap.has(id)) { 73 limitsMap.get(id)!.time += time; 74 } else { 75 let isMax = id.endsWith('max'); 76 let limit = new CpuFreqLimit(); 77 limit.cpu = `Cpu ${item.cpu}`; 78 limit.time = time; 79 limit.type = isMax ? 'Max Freqency' : 'Min Frequency'; 80 limit.value = isMax ? item.max! : item.min!; 81 limitsMap.set(id, limit); 82 } 83 }; 84 list.forEach((item) => { 85 if (item.startNs! > end) { 86 return; 87 } 88 let max = Math.max(item.startNs || 0, start); 89 let min = Math.min((item.startNs || 0) + item.dur, end); 90 if (max < min) { 91 let maxId = `${item.cpu}-${item.max}-max`; 92 let minId = `${item.cpu}-${item.min}-min`; 93 groupMapData(min - max, maxId, item); 94 groupMapData(min - max, minId, item); 95 } 96 }); 97 this.cpuFreqLimitSource = Array.from(limitsMap.values()).map((item) => { 98 item.timeStr = Utils.getProbablyTime(item.time); 99 item.valueStr = `${ColorUtils.formatNumberComma(item.value!)} kHz`; 100 return item; 101 }); 102 this.sortCpuFreqLimitTable(this.cpuFreqLimitSortKey, this.cpuFreqLimitSortType); 103 } 104 105 sortCpuFreqLimitTable(key: string, type: number): void { 106 if (type == 0) { 107 this.cpuFreqLimitsTbl!.recycleDataSource = this.cpuFreqLimitSource; 108 } else { 109 let cpuFreqLimitsArr = Array.from(this.cpuFreqLimitSource); 110 cpuFreqLimitsArr.sort((cpuFreqLimitA, cpuFreqLimitB): number => { 111 if (key == 'timeStr') { 112 return this.compareTime(cpuFreqLimitA, cpuFreqLimitB, type); 113 } else if (key == 'valueStr') { 114 return this.compareValue(cpuFreqLimitA, cpuFreqLimitB, type); 115 } else if (key == 'cpu') { 116 return this.compareCpu(cpuFreqLimitA, cpuFreqLimitB, type); 117 } else if (key == 'type') { 118 return this.compareType(cpuFreqLimitA, cpuFreqLimitB, type); 119 } else { 120 return 0; 121 } 122 }); 123 this.cpuFreqLimitsTbl!.recycleDataSource = cpuFreqLimitsArr; 124 } 125 } 126 127 compareTime(cpuFreqLimitA: any, cpuFreqLimitB: any, type: number): number { 128 if (type == 1) { 129 return cpuFreqLimitA.time - cpuFreqLimitB.time; 130 } else { 131 return cpuFreqLimitB.time - cpuFreqLimitA.time; 132 } 133 } 134 135 compareValue(cpuFreqLimitA: any, cpuFreqLimitB: any, type: number): number { 136 if (type == 1) { 137 return cpuFreqLimitA.value - cpuFreqLimitB.value; 138 } else { 139 return cpuFreqLimitB.value - cpuFreqLimitA.value; 140 } 141 } 142 143 compareCpu(cpuFreqLimitA: any, cpuFreqLimitB: any, type: number): number { 144 if (cpuFreqLimitA.cpu > cpuFreqLimitB.cpu) { 145 return type === 2 ? -1 : 1; 146 } else if (cpuFreqLimitA.cpu == cpuFreqLimitB.cpu) { 147 return 0; 148 } else { 149 return type === 2 ? 1 : -1; 150 } 151 } 152 153 compareType(cpuFreqLimitA: any, cpuFreqLimitB: any, type: number): number { 154 if (cpuFreqLimitA.type > cpuFreqLimitB.type) { 155 return type === 2 ? 1 : -1; 156 } else if (cpuFreqLimitA.type == cpuFreqLimitB.type) { 157 return 0; 158 } else { 159 return type === 2 ? -1 : 1; 160 } 161 } 162 163 initHtml(): string { 164 return ` 165 <style> 166 .cpu-freq-limit-tbl { 167 height: auto; 168 } 169 :host{ 170 display: flex; 171 flex-direction: column; 172 padding: 10px 10px; 173 } 174 </style> 175 <lit-table id="tb-cpu-freq-limit" class="cpu-freq-limit-tbl"> 176 <lit-table-column class="cpu-freq-limit-column" width="20%" title="Cpu" data-index="cpu" key="cpu" align="flex-start" order> 177 </lit-table-column> 178 <lit-table-column class="cpu-freq-limit-column" width="1fr" title="Type" data-index="type" key="type" align="flex-start" order> 179 </lit-table-column> 180 <lit-table-column class="cpu-freq-limit-column" width="1fr" title="Time" data-index="timeStr" key="timeStr" align="flex-start" order> 181 </lit-table-column> 182 <lit-table-column class="cpu-freq-limit-column" width="1fr" title="Value" data-index="valueStr" key="valueStr" align="flex-start" order> 183 </lit-table-column> 184 </lit-table> 185 `; 186 } 187} 188 189class CpuFreqLimit { 190 cpu: string = ''; 191 type: string = ''; 192 time: number = 0; 193 value: number = 0; 194 timeStr: string = ''; 195 valueStr: string = ''; 196} 197