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 {convertJSON, LogicHandler} from "./ProcedureLogicWorkerCommon.js"; 17 18export class ProcedureLogicWorkerCpuState extends LogicHandler { 19 20 currentEventId: string = "" 21 22 handle(data: any): void { 23 this.currentEventId = data.id 24 if (data && data.type) { 25 switch (data.type) { 26 case "CpuState-getCpuState": 27 if(data.params.list){ 28 let arr = convertJSON(data.params.list) || [] 29 self.postMessage({id: data.id, action: data.action, results: this.supplementCpuState(arr)}) 30 arr = []; 31 }else{ 32 this.getCpuState(data.params.cpu); 33 } 34 break; 35 } 36 } 37 } 38 39 queryData(queryName: string, sql: string, args: any) { 40 self.postMessage({ 41 id: this.currentEventId, 42 type: queryName, 43 isQuery: true, 44 args: args, 45 sql: sql 46 }) 47 } 48 49 getCpuState(cpu:number){ 50 this.queryData("CpuState-getCpuState", ` 51 select (A.ts - B.start_ts) as startTs, 52 A.dur, 53 (A.ts - B.start_ts + A.dur) as endTs, 54 (case 55 when state = 'Running' then 0 56 when state = 'S' then 1 57 when state = 'R' or state = 'R+' then 2 58 else 3 end) as value 59 from thread_state A,trace_range B 60 where cpu = $cpu and startTs >= 0; 61 `, {$cpu:cpu}); 62 } 63 64 supplementCpuState(arr:Array<CpuState>):Array<CpuState>{ 65 let source:Array<CpuState> = []; 66 if(arr.length > 0){ 67 let first = arr[0]; 68 if(first.startTs > 0){ 69 let cs:CpuState = new CpuState(); 70 cs.startTs = 0; 71 cs.value = 3; 72 cs.dur = first.startTs; 73 cs.endTs = first.startTs; 74 source.push(cs); 75 } 76 source.push(first); 77 for (let i = 1,len = arr.length; i < len; i++) { 78 let last = arr[i-1]; 79 let current = arr[i]; 80 if(current.startTs > last.endTs){ 81 let cs:CpuState = new CpuState(); 82 cs.startTs = last.endTs; 83 cs.value = 3; 84 cs.dur = current.startTs - last.endTs; 85 cs.endTs = current.startTs; 86 source.push(cs); 87 } 88 source.push(current); 89 } 90 } 91 return source; 92 } 93 94} 95 96export class CpuState{ 97 startTs:number = 0 98 endTs:number = 0 99 dur:number = 0 100 value:number = 0 101} 102 103