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 { SpSchedulingAnalysis } from './SpSchedulingAnalysis.js'; 19import { procedurePool } from '../../database/Procedure.js'; 20import { info } from '../../../log/Log.js'; 21import '../../../base-ui/progress-bar/LitProgressBar.js'; 22import { LitProgressBar } from '../../../base-ui/progress-bar/LitProgressBar.js'; 23import './TableNoData.js'; 24import { TableNoData } from './TableNoData.js'; 25 26@element('top20-thread-run-time') 27export class Top20ThreadRunTime extends BaseElement { 28 traceChange: boolean = false; 29 private threadRunTimeTbl: LitTable | null | undefined; 30 private threadRunTimeProgress: LitProgressBar | null | undefined; 31 private nodata: TableNoData | null | undefined; 32 private threadRunTimeData: Array<any> = []; 33 34 initElements(): void { 35 this.threadRunTimeProgress = this.shadowRoot!.querySelector<LitProgressBar>('#loading'); 36 this.threadRunTimeTbl = this.shadowRoot!.querySelector<LitTable>('#tb-thread-run-time'); 37 this.nodata = this.shadowRoot!.querySelector<TableNoData>('#nodata'); 38 39 this.threadRunTimeTbl!.addEventListener('row-click', (evt: any) => { 40 let data = evt.detail.data; 41 data.isSelected = true; 42 // @ts-ignore 43 if ((evt.detail as any).callBack) { 44 // @ts-ignore 45 (evt.detail as any).callBack(true); 46 } 47 }); 48 49 this.threadRunTimeTbl!.addEventListener('column-click', (evt) => { 50 // @ts-ignore 51 this.sortByColumn(evt.detail); 52 }); 53 } 54 55 init() { 56 if (!this.traceChange) { 57 if (this.threadRunTimeTbl!.recycleDataSource.length > 0) { 58 this.threadRunTimeTbl?.reMeauseHeight(); 59 } 60 return; 61 } 62 this.threadRunTimeProgress!.loading = true; 63 this.traceChange = false; 64 this.queryLogicWorker(`scheduling-Thread RunTime`, `query Thread Cpu Run Time Analysis Time:`, (res) => { 65 this.nodata!.noData = res === undefined || res.length === 0; 66 this.threadRunTimeTbl!.recycleDataSource = res; 67 this.threadRunTimeTbl?.reMeauseHeight(); 68 this.threadRunTimeProgress!.loading = false; 69 this.threadRunTimeData = res; 70 }); 71 } 72 73 clearData() { 74 this.traceChange = true; 75 this.threadRunTimeTbl!.recycleDataSource = []; 76 } 77 78 queryLogicWorker(option: string, log: string, handler: (res: any) => void) { 79 let threadRunTime = new Date().getTime(); 80 procedurePool.submitWithName('logic1', option, { cpuMax: SpSchedulingAnalysis.cpuCount - 1 }, undefined, handler); 81 let durTime = new Date().getTime() - threadRunTime; 82 info(log, durTime); 83 } 84 85 sortByColumn(detail: any) { 86 // @ts-ignore 87 function compare(threadRunTimeProperty, sort, type) { 88 return function (a: any, b: any) { 89 if (type === 'number') { 90 // @ts-ignore 91 return sort === 2 92 ? parseFloat(b[threadRunTimeProperty]) - parseFloat(a[threadRunTimeProperty]) 93 : parseFloat(a[threadRunTimeProperty]) - parseFloat(b[threadRunTimeProperty]); 94 } else { 95 if (sort === 2) { 96 return b[threadRunTimeProperty].toString().localeCompare(a[threadRunTimeProperty].toString()); 97 } else { 98 return a[threadRunTimeProperty].toString().localeCompare(b[threadRunTimeProperty].toString()); 99 } 100 } 101 }; 102 } 103 104 if (detail.key === 'maxDurationStr') { 105 detail.key = 'maxDuration'; 106 this.threadRunTimeData.sort(compare(detail.key, detail.sort, 'number')); 107 } else if ( 108 detail.key === 'cpu' || 109 detail.key === 'no' || 110 detail.key === 'pid' || 111 detail.key === 'tid' || 112 detail.key === 'timestamp' 113 ) { 114 this.threadRunTimeData.sort(compare(detail.key, detail.sort, 'number')); 115 } else { 116 this.threadRunTimeData.sort(compare(detail.key, detail.sort, 'string')); 117 } 118 this.threadRunTimeTbl!.recycleDataSource = this.threadRunTimeData; 119 } 120 121 initHtml(): string { 122 return ` 123 <style> 124 :host { 125 width: 100%; 126 height: 100%; 127 background-color: var(--dark-background5,#F6F6F6); 128 } 129 .tb_run_time{ 130 overflow: auto ; 131 border-radius: 5px; 132 border: solid 1px var(--dark-border1,#e0e0e0); 133 margin: 10px 10px 0 10px; 134 padding: 5px 15px 135 } 136 </style> 137 <lit-progress-bar id="loading" style="height: 1px;width: 100%" loading></lit-progress-bar> 138 <div style="height: 5px"></div> 139 <div class="tb_run_time" > 140 <table-no-data id="nodata" contentHeight="500px"> 141 <lit-table id="tb-thread-run-time" style="height: auto;" hideDownload> 142 <lit-table-column width="90px" title="NO" data-index="no" key="no" align="flex-start" order></lit-table-column> 143 <lit-table-column width="140px" title="tid" data-index="tid" key="tid" align="flex-start" order></lit-table-column> 144 <lit-table-column width="240px" title="t_name" data-index="tName" key="tName" align="flex-start" order></lit-table-column> 145 <lit-table-column width="140px" title="pid" data-index="pid" key="pid" align="flex-start" order></lit-table-column> 146 <lit-table-column width="240px" title="p_name" data-index="pName" key="pName" align="flex-start" order></lit-table-column> 147 <lit-table-column width="140px" title="max duration" data-index="maxDurationStr" key="maxDurationStr" align="flex-start" order></lit-table-column> 148 <lit-table-column width="200px" title="timestamp" data-index="timestamp" key="timestamp" align="flex-start" order> 149 <template> 150 <div onclick="{ 151 window.publish(window.SmartEvent.UI.SliceMark,this.parentElement.parentElement.data) 152 }">{{timestamp}}</div> 153 </template> 154 </lit-table-column> 155 <lit-table-column width="140px" title="cpu" data-index="cpu" key="cpu" align="flex-start" order></lit-table-column> 156 </lit-table> 157 </table-no-data> 158 </div> 159 <div style="height: 10px"></div> 160 `; 161 } 162} 163