1// Copyright (c) 2021 Huawei Device Co., Ltd. 2// Licensed under the Apache License, Version 2.0 (the "License"); 3// you may not use this file except in compliance with the License. 4// You may obtain a copy of the License at 5// 6// http://www.apache.org/licenses/LICENSE-2.0 7// 8// Unless required by applicable law or agreed to in writing, software 9// distributed under the License is distributed on an "AS IS" BASIS, 10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11// See the License for the specific language governing permissions and 12// limitations under the License. 13 14import { CHART_OFFSET_LEFT, MAX_COUNT, QueryEnum, threadStateToString, TraficEnum } from '../utils/QueryEnum'; 15import { threadPool } from '../../SqlLite'; 16import { TraceRow } from '../../../component/trace/base/TraceRow'; 17import { ThreadStruct } from '../../ui-worker/ProcedureWorkerThread'; 18 19export function threadDataSender(tid: number, pid: number, row: TraceRow<ThreadStruct>): Promise<ThreadStruct[]|boolean> { 20 let trafic: number = TraficEnum.Memory; 21 let width = row.clientWidth - CHART_OFFSET_LEFT; 22 if ((trafic === TraficEnum.SharedArrayBuffer) && !row.sharedArrayBuffers) { 23 row.sharedArrayBuffers = { 24 startTime: new SharedArrayBuffer(Float64Array.BYTES_PER_ELEMENT * MAX_COUNT), 25 dur: new SharedArrayBuffer(Float64Array.BYTES_PER_ELEMENT * MAX_COUNT), 26 cpu: new SharedArrayBuffer(Int8Array.BYTES_PER_ELEMENT * MAX_COUNT), 27 id: new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * MAX_COUNT), 28 tid: new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * MAX_COUNT), 29 state: new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * MAX_COUNT), 30 pid: new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * MAX_COUNT), 31 argSetID: new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * MAX_COUNT), 32 }; 33 } 34 return new Promise((resolve): void => { 35 threadPool.submitProto( 36 QueryEnum.ThreadData, 37 { 38 pid: pid, 39 tid: tid, 40 startNS: TraceRow.range?.startNS || 0, 41 endNS: TraceRow.range?.endNS || 0, 42 recordStartNS: window.recordStartNS, 43 recordEndNS: window.recordEndNS, 44 width: width, 45 trafic: trafic, 46 sharedArrayBuffers: row.sharedArrayBuffers, 47 }, 48 (res: any, len: number, transfer: boolean,isEmpty:boolean): void => { 49 if (isEmpty) { 50 resolve(true); 51 }else{ 52 resolve(arrayBufferHandler(transfer ? res : row.sharedArrayBuffers, len)); 53 } 54 } 55 ); 56 }); 57} 58 59function arrayBufferHandler(buffers: any, len: number): ThreadStruct[] { 60 let outArr: ThreadStruct[] = []; 61 let startTime = new Float64Array(buffers.startTime); 62 let dur = new Float64Array(buffers.dur); 63 let cpu = new Int8Array(buffers.cpu); 64 let id = new Int32Array(buffers.id); 65 let tid = new Int32Array(buffers.tid); 66 let state = new Int32Array(buffers.state); 67 let pid = new Int32Array(buffers.pid); 68 let argSetID = new Int32Array(buffers.argSetID); 69 for (let i = 0; i < len; i++) { 70 outArr.push({ 71 startTime: startTime[i], 72 dur: dur[i], 73 cpu: cpu[i], 74 id: id[i], 75 tid: tid[i], 76 state: threadStateToString(state[i]), 77 pid: pid[i], 78 argSetID: argSetID[i], 79 } as ThreadStruct); 80 } 81 return outArr; 82} 83