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'; 17import { Utils } from '../component/trace/base/Utils'; 18import { drawString } from '../database/ui-worker/ProcedureWorkerCommon'; 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 static selectThreaStructList: Array<ThreadStruct> = []; 30 hasSched: number | undefined; 31 pid: number | undefined; 32 processName: string | undefined; 33 threadName: string | undefined; 34 tid: number | undefined; 35 upid: number | undefined; 36 utid: number | undefined; 37 switchCount:number | undefined; 38 cpu: number | undefined; 39 dur: number | undefined; 40 end_ts: number | undefined; 41 id: number | undefined; 42 is_main_thread: number | undefined; 43 name: string | undefined; 44 startTime: number | undefined; 45 start_ts: number | undefined; 46 state: string | undefined; 47 type: string | undefined; 48 49 static draw(threadBeanCanvasCtx: CanvasRenderingContext2D, threadBeanStructData: ThreadStruct) { 50 if (threadBeanStructData.frame) { 51 threadBeanCanvasCtx.globalAlpha = 1; 52 let stateText = threadBeanStructData.state || ''; 53 if ('S' == threadBeanStructData.state) { 54 threadBeanCanvasCtx.fillStyle = ThreadStruct.sColor; 55 threadBeanCanvasCtx.globalAlpha = 0.2; // transparency 56 threadBeanCanvasCtx.fillRect( 57 threadBeanStructData.frame.x, 58 threadBeanStructData.frame.y + padding, 59 threadBeanStructData.frame.width, 60 threadBeanStructData.frame.height - padding * 2 61 ); 62 threadBeanCanvasCtx.globalAlpha = 1; // transparency 63 } else if ('R' == threadBeanStructData.state) { 64 threadBeanCanvasCtx.fillStyle = ThreadStruct.rColor; 65 this.drawRectAndString(threadBeanCanvasCtx, threadBeanStructData); 66 } else if ('D' == threadBeanStructData.state) { 67 threadBeanCanvasCtx.fillStyle = ThreadStruct.uninterruptibleSleepColor; 68 this.drawRectAndString(threadBeanCanvasCtx, threadBeanStructData); 69 } else if ('Running' == threadBeanStructData.state) { 70 threadBeanCanvasCtx.fillStyle = ThreadStruct.runningColor; 71 this.drawRectAndString(threadBeanCanvasCtx, threadBeanStructData); 72 } else { 73 threadBeanCanvasCtx.fillStyle = ThreadStruct.rColor; 74 this.drawRectAndString(threadBeanCanvasCtx, threadBeanStructData); 75 } 76 if ( 77 ThreadStruct.selectThreadStruct && 78 ThreadStruct.equals(ThreadStruct.selectThreadStruct, threadBeanStructData) && 79 ThreadStruct.selectThreadStruct.state != 'S' 80 ) { 81 threadBeanCanvasCtx.strokeStyle = '#232c5d'; 82 threadBeanCanvasCtx.lineWidth = 2; 83 threadBeanCanvasCtx.strokeRect( 84 threadBeanStructData.frame.x, 85 threadBeanStructData.frame.y + padding, 86 threadBeanStructData.frame.width - 2, 87 threadBeanStructData.frame.height - padding * 2 88 ); 89 } 90 } 91 } 92 93 private static drawRectAndString(threadBeanCanvasCtx: CanvasRenderingContext2D, threadBeanStructData: ThreadStruct) { 94 // @ts-ignore 95 threadBeanCanvasCtx.fillRect( 96 threadBeanStructData.frame!.x, 97 threadBeanStructData.frame!.y + padding, 98 threadBeanStructData.frame!.width, 99 threadBeanStructData.frame!.height - padding * 2 100 ); 101 threadBeanCanvasCtx.fillStyle = '#fff'; 102 // @ts-ignore 103 drawString( 104 threadBeanCanvasCtx, 105 ThreadStruct.getEndState(threadBeanStructData.state || ''), 106 2, 107 threadBeanStructData.frame!, 108 threadBeanStructData 109 ); 110 } 111 112 static getEndState(state: string): string { 113 let statusMapElement = Utils.getEndState(state); 114 if (statusMapElement) { 115 return statusMapElement; 116 } else { 117 if ('' == statusMapElement || statusMapElement == null) { 118 return ''; 119 } 120 return 'Unknown State'; 121 } 122 } 123 124 static equals(d1: ThreadStruct, d2: ThreadStruct): boolean { 125 return ( 126 d1 && 127 d2 && 128 d1.cpu == d2.cpu && 129 d1.tid == d2.tid && 130 d1.state == d2.state && 131 d1.startTime == d2.startTime && 132 d1.dur == d2.dur 133 ); 134 } 135} 136