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 { BaseStruct } from './BaseStruct.js'; 17import { Utils } from '../component/trace/base/Utils.js'; 18import { drawString } from '../database/ui-worker/ProcedureWorkerCommon.js'; 19 20const padding = 1; 21 22export class ThreadStruct extends BaseStruct { 23 static runningColor: string = '#467b3b'; 24 static rColor = '#a0b84d'; 25 static uninterruptibleSleepColor = '#f19d38'; 26 static sColor = '#FBFBFB'; 27 static hoverThreadStruct: ThreadStruct | undefined; 28 static selectThreadStruct: ThreadStruct | undefined; 29 hasSched: number | undefined; 30 pid: number | undefined; 31 processName: string | undefined; 32 threadName: string | undefined; 33 tid: number | undefined; 34 upid: number | undefined; 35 utid: number | undefined; 36 cpu: number | undefined; 37 dur: number | undefined; 38 end_ts: number | undefined; 39 id: number | undefined; 40 is_main_thread: number | undefined; 41 name: string | undefined; 42 startTime: number | undefined; 43 start_ts: number | undefined; 44 state: string | undefined; 45 type: string | undefined; 46 47 static draw(threadBeanCanvasCtx: CanvasRenderingContext2D, threadBeanStructData: ThreadStruct) { 48 if (threadBeanStructData.frame) { 49 threadBeanCanvasCtx.globalAlpha = 1; 50 let stateText = threadBeanStructData.state || ''; 51 if ('S' == threadBeanStructData.state) { 52 threadBeanCanvasCtx.fillStyle = ThreadStruct.sColor; 53 threadBeanCanvasCtx.globalAlpha = 0.2; // transparency 54 threadBeanCanvasCtx.fillRect(threadBeanStructData.frame.x, threadBeanStructData.frame.y + padding, threadBeanStructData.frame.width, threadBeanStructData.frame.height - padding * 2); 55 threadBeanCanvasCtx.globalAlpha = 1; // transparency 56 } else if ('R' == threadBeanStructData.state) { 57 threadBeanCanvasCtx.fillStyle = ThreadStruct.rColor; 58 this.drawRectAndString(threadBeanCanvasCtx, threadBeanStructData); 59 } else if ('D' == threadBeanStructData.state) { 60 threadBeanCanvasCtx.fillStyle = ThreadStruct.uninterruptibleSleepColor; 61 this.drawRectAndString(threadBeanCanvasCtx, threadBeanStructData); 62 } else if ('Running' == threadBeanStructData.state) { 63 threadBeanCanvasCtx.fillStyle = ThreadStruct.runningColor; 64 this.drawRectAndString(threadBeanCanvasCtx, threadBeanStructData); 65 } else { 66 threadBeanCanvasCtx.fillStyle = ThreadStruct.rColor; 67 this.drawRectAndString(threadBeanCanvasCtx, threadBeanStructData); 68 } 69 if ( 70 ThreadStruct.selectThreadStruct && 71 ThreadStruct.equals(ThreadStruct.selectThreadStruct, threadBeanStructData) && 72 ThreadStruct.selectThreadStruct.state != 'S' 73 ) { 74 threadBeanCanvasCtx.strokeStyle = '#232c5d'; 75 threadBeanCanvasCtx.lineWidth = 2; 76 threadBeanCanvasCtx.strokeRect(threadBeanStructData.frame.x, threadBeanStructData.frame.y + padding, threadBeanStructData.frame.width - 2, threadBeanStructData.frame.height - padding * 2); 77 } 78 } 79 } 80 81 private static drawRectAndString(threadBeanCanvasCtx: CanvasRenderingContext2D, threadBeanStructData: ThreadStruct) { 82 // @ts-ignore 83 threadBeanCanvasCtx.fillRect(threadBeanStructData.frame.x, threadBeanStructData.frame.y + padding, threadBeanStructData.frame.width, threadBeanStructData.frame.height - padding * 2); 84 threadBeanCanvasCtx.fillStyle = '#fff'; 85 // @ts-ignore 86 drawString(threadBeanCanvasCtx, ThreadStruct.getEndState(threadBeanStructData.state || ''), 2, threadBeanStructData.frame, threadBeanStructData); 87 } 88 89 static getEndState(state: string): string { 90 let statusMapElement = Utils.getEndState(state); 91 if (statusMapElement) { 92 return statusMapElement; 93 } else { 94 if ('' == statusMapElement || statusMapElement == null) { 95 return ''; 96 } 97 return 'Unknown State'; 98 } 99 } 100 101 static equals(d1: ThreadStruct, d2: ThreadStruct): boolean { 102 return d1 && 103 d2 && 104 d1.cpu == d2.cpu && 105 d1.tid == d2.tid && 106 d1.state == d2.state && 107 d1.startTime == d2.startTime && 108 d1.dur == d2.dur; 109 } 110} 111