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 ProcedureLogicWorkerSPT extends LogicHandler { 19 arrTs: Array<ThreadState> = []; 20 arrTp: Array<ThreadProcess> = []; 21 currentEventId: string = "" 22 23 handle(data: any): void { 24 this.currentEventId = data.id 25 if (data && data.type) { 26 switch (data.type) { 27 case "spt-init": 28 this.getThreadState(); 29 break; 30 case "spt-getThreadStateData": 31 this.arrTs = convertJSON(data.params.list) || []; 32 this.getThreadProcessData() 33 break; 34 case "spt-getThreadProcessData": 35 this.arrTp = convertJSON(data.params.list) || []; 36 this.initProcessThreadStateData(); 37 break; 38 } 39 } 40 } 41 42 queryData(queryName: string, sql: string, args: any) { 43 self.postMessage({ 44 id: this.currentEventId, 45 type: queryName, 46 isQuery: true, 47 args: args, 48 sql: sql 49 }) 50 } 51 52 getThreadState(){ 53 this.queryData("spt-getThreadStateData", ` 54 select itid, 55 state, 56 dur, 57 ts, 58 (ts - start_ts + dur) as end_ts, 59 (ts - start_ts) as start_ts, 60 cpu 61from thread_state,trace_range where dur > 0 and (ts - start_ts) >= 0; 62`, {}); 63 } 64 65 getThreadProcessData(){ 66 this.queryData("spt-getThreadProcessData", ` 67 select A.id, 68 A.tid as threadId, 69 A.name as thread, 70 IP.pid as processId, 71 IP.name as process 72from thread as A left join process as IP on A.ipid = IP.id 73where IP.pid not null; 74`, {}); 75 } 76 77 getSPT(){ 78 this.queryData("spt-getStatesProcessThreadData", ` 79 select 80 IP.name as process, 81 IP.pid as processId, 82 A.name as thread, 83 B.state as state, 84 A.tid as threadId, 85 B.dur, 86 (B.ts - TR.start_ts + B.dur) as end_ts, 87 (B.ts - TR.start_ts) as start_ts, 88 B.cpu 89 from 90 thread_state as B 91 left join 92 thread as A 93 on 94 B.itid = A.id 95 left join 96 process as IP 97 on 98 A.ipid = IP.id 99 left join 100 trace_range as TR 101 where 102 B.dur > 0 103 and 104 IP.pid not null 105 and (B.ts - TR.start_ts) >= 0; 106`, {}); 107 } 108 109 initProcessThreadStateData() { 110 let mapTp: Map<number, ThreadProcess> = new Map<number, ThreadProcess>(); 111 for (let tp of this.arrTp) { 112 mapTp.set(tp.id, tp); 113 } 114 let sptArr:Array<SPT> = []; 115 for (let tr of this.arrTs) { 116 if (mapTp.has(tr.itid)) { 117 let tp = mapTp.get(tr.itid); 118 let spt = new SPT(); 119 spt.processId = tp!.processId 120 spt.process = tp!.process 121 spt.thread = tp!.thread 122 spt.threadId = tp!.threadId 123 spt.state = tr.state; 124 spt.dur = tr.dur; 125 spt.end_ts = tr.end_ts; 126 spt.start_ts = tr.start_ts; 127 spt.cpu = tr.cpu; 128 sptArr.push(spt); 129 } 130 } 131 this.arrTp = []; 132 this.arrTs = []; 133 self.postMessage({ 134 id: this.currentEventId, 135 action: "spt-init", 136 results: sptArr 137 }); 138 } 139 140 141} 142 143export class ThreadState{ 144 itid:number = 0 145 state:string = "" 146 dur:number = 0 147 ts:number = 0 148 end_ts:number = 0 149 start_ts:number = 0 150 cpu:number = 0 151} 152 153export class ThreadProcess{ 154 id:number = 0 155 threadId :number = 0 156 thread :string = "" 157 processId : number = 0 158 process : string = "" 159} 160 161export class SPT { 162 process: string = "" 163 processId: number = 0 164 thread: string = "" 165 threadId: number = 0 166 state: string = "" 167 dur: number = 0 168 start_ts: number = 0 169 end_ts: number = 0 170 cpu: number = 0; 171 priority: string = "-" 172 note: string = "-" 173} 174