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 { SpSchedulingAnalysis } from './SpSchedulingAnalysis'; 18import { DrawerCpuTabs } from './DrawerCpuTabs'; 19import { LitChartPie } from '../../../base-ui/chart/pie/LitChartPie'; 20import { LitDrawer } from '../../../base-ui/drawer/LitDrawer'; 21import '../../../base-ui/drawer/LitDrawer'; 22import './DrawerCpuTabs'; 23import { procedurePool } from '../../database/Procedure'; 24import { info } from '../../../log/Log'; 25import { LitSelect } from '../../../base-ui/select/LitSelect'; 26import '../../../base-ui/progress-bar/LitProgressBar'; 27import { LitProgressBar } from '../../../base-ui/progress-bar/LitProgressBar'; 28import { pieChartColors } from '../../../base-ui/chart/pie/LitChartPieData'; 29import { SpStatisticsHttpUtil } from '../../../statistics/util/SpStatisticsHttpUtil'; 30import { TabCpuAnalysisHtml } from './TabCpuAnalysis.html'; 31 32@element('tab-cpu-analysis') 33export class TabCpuAnalysis extends BaseElement { 34 private cpuUsageGrid: HTMLDivElement | undefined; 35 private cpuUsageChart: HTMLDivElement | undefined; 36 private drawer: LitDrawer | undefined | null; 37 private cpuPieMap: Map<number, LitChartPie> = new Map<number, LitChartPie>(); 38 private schedulingSelect: LitSelect | undefined | null; 39 private drawerCpuTabs: DrawerCpuTabs | undefined | null; 40 private progress: LitProgressBar | null | undefined; 41 private loadingUsage: boolean = false; 42 private loadingPieData: boolean = false; 43 44 initElements(): void { 45 this.progress = this.shadowRoot!.querySelector<LitProgressBar>('#loading'); 46 this.cpuUsageGrid = this.shadowRoot?.querySelector('#cpu_usage_table') as HTMLDivElement; 47 this.cpuUsageChart = this.shadowRoot?.querySelector('#cpu_usage_chart') as HTMLDivElement; 48 this.schedulingSelect = this.shadowRoot?.querySelector<LitSelect>('#scheduling_select'); 49 this.drawer = this.shadowRoot!.querySelector<LitDrawer>('#drawer-right'); 50 this.drawerCpuTabs = this.shadowRoot?.querySelector<DrawerCpuTabs>('#drawer-cpu-tabs'); 51 this.schedulingSelect!.onchange = (e) => { 52 this.loadingPieData = true; 53 this.progress!.loading = this.loadingUsage || this.loadingPieData; 54 this.queryPieChartDataByType((e as any).detail.text); 55 }; 56 this.drawer!.onClose = (e: any) => { 57 this.drawerCpuTabs!.clearData(); 58 }; 59 } 60 61 init() { 62 this.cpuPieMap.clear(); 63 this.cpuUsageGrid!.innerHTML = ''; 64 this.cpuUsageChart!.innerHTML = ''; 65 this.schedulingSelect!.value = '1'; 66 this.cpuUsageGrid!.append(this.createUsageItem('usage', '%')); 67 for (let i = 0; i < SpSchedulingAnalysis.cpuCount; i++) { 68 let cpuPie = new LitChartPie(); 69 cpuPie.className = 'pie-chart'; 70 this.cpuPieMap.set(i, cpuPie); 71 this.cpuUsageGrid!.append(this.createUsageItem(`CPU: ${i}`, 0)); 72 this.cpuUsageChart!.append(this.createUsageChartItem(i, cpuPie)); 73 } 74 this.loadingUsage = true; 75 this.loadingPieData = true; 76 this.progress!.loading = this.loadingUsage || this.loadingPieData; 77 this.queryLogicWorker('scheduling-getCpuUsage', 'query Cpu Usage Time:', (res) => { 78 if (res && res.length > 0) { 79 this.cpuUsageGrid!.innerHTML = ''; 80 this.cpuUsageGrid!.append(this.createUsageItem('usage', '%')); 81 if (res instanceof Array) { 82 for (let re of res) { 83 this.cpuUsageGrid!.append(this.createUsageItem(`CPU: ${re.cpu}`, ((re.usage || 0) * 100).toFixed(2))); 84 } 85 } 86 } 87 this.loadingUsage = false; 88 this.progress!.loading = this.loadingUsage || this.loadingPieData; 89 }); 90 this.queryPieChartDataByType('CPU Idle'); 91 SpStatisticsHttpUtil.addOrdinaryVisitAction({ 92 event: 'CPU Data', 93 action: 'trace_tab', 94 }); 95 } 96 97 queryPieChartDataByType(type: string) { 98 SpStatisticsHttpUtil.addOrdinaryVisitAction({ 99 event: 'Analysis ' + type, 100 action: 'scheduling_analysis', 101 }); 102 let tip = ''; 103 if (type === 'CPU Frequency') { 104 tip = 'freq:'; 105 } else if (type === 'CPU Idle') { 106 tip = 'idle:'; 107 } else { 108 tip = 'irq:'; 109 } 110 this.queryLogicWorker(`scheduling-${type}`, `query ${type} Analysis Time:`, (res) => { 111 for (let key of this.cpuPieMap.keys()) { 112 this.cpuPieMap.get(key)!.config = { 113 appendPadding: 10, 114 data: res.get(key) || [], 115 angleField: 'sum', 116 colorField: 'value', 117 radius: 0.8, 118 tip: (obj) => { 119 return `<div> 120 <div>${tip}${obj.obj.value}</div> 121 <div>ratio:${obj.obj.ratio}%</div> 122 </div> 123 `; 124 }, 125 label: { 126 type: 'outer', 127 color: 128 type !== 'CPU Idle' 129 ? undefined 130 : (it) => { 131 return pieChartColors[(it as any).value]; 132 }, 133 }, 134 interactions: [ 135 { 136 type: 'element-active', 137 }, 138 ], 139 }; 140 } 141 this.loadingPieData = false; 142 this.progress!.loading = this.loadingUsage || this.loadingPieData; 143 }); 144 } 145 146 queryLogicWorker(cpuAnalysisType: string, log: string, handler: (res: any) => void) { 147 let cpuAnalysisTime = new Date().getTime(); 148 procedurePool.submitWithName( 149 'logic0', 150 cpuAnalysisType, 151 { 152 endTs: SpSchedulingAnalysis.endTs, 153 total: SpSchedulingAnalysis.totalDur, 154 }, 155 undefined, 156 handler 157 ); 158 let durTime = new Date().getTime() - cpuAnalysisTime; 159 info(log, durTime); 160 } 161 162 createUsageItem(name: string, value: any) { 163 let div = document.createElement('div'); 164 div.className = 'usage_item_box'; 165 div.innerHTML = `<div class="usage_item">${name}</div><div class="usage_item">${value}</div>`; 166 return div; 167 } 168 169 createUsageChartItem(cpu: number, pie: LitChartPie) { 170 let div = document.createElement('div'); 171 div.className = 'usage_chart'; 172 div.style.cursor = 'pointer'; 173 div.innerHTML = ` 174 <div style="height: 40px;line-height: 40px;margin-left: 10px">CPU: ${cpu}</div> 175 `; 176 div.append(pie); 177 div.addEventListener('click', (event) => { 178 if (this.loadingUsage || this.loadingPieData) { 179 return; 180 } 181 this.drawer!.title = `CPU: ${cpu}`; 182 this.drawer!.visible = true; 183 this.drawerCpuTabs!.init(cpu, this.schedulingSelect!.value); 184 }); 185 return div; 186 } 187 188 initHtml(): string { 189 return TabCpuAnalysisHtml; 190 } 191} 192