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 { getThreadPool } from '../../SqlLite'; 16import { TraceRow } from '../../../component/trace/base/TraceRow'; 17import { ThreadStruct } from '../../ui-worker/ProcedureWorkerThread'; 18import { Utils } from '../../../component/trace/base/Utils'; 19 20export function threadDataSender( 21 tid: number, 22 pid: number, 23 row: TraceRow<ThreadStruct>, 24 traceId?: string 25): Promise<ThreadStruct[] | boolean> { 26 let trafic: number = TraficEnum.Memory; 27 let width = row.clientWidth || row.parentRowEl!.clientWidth - CHART_OFFSET_LEFT; 28 if (trafic === TraficEnum.SharedArrayBuffer && !row.sharedArrayBuffers) { 29 row.sharedArrayBuffers = { 30 startTime: new SharedArrayBuffer(Float64Array.BYTES_PER_ELEMENT * MAX_COUNT), 31 dur: new SharedArrayBuffer(Float64Array.BYTES_PER_ELEMENT * MAX_COUNT), 32 cpu: new SharedArrayBuffer(Int8Array.BYTES_PER_ELEMENT * MAX_COUNT), 33 id: new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * MAX_COUNT), 34 tid: new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * MAX_COUNT), 35 state: new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * MAX_COUNT), 36 pid: new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * MAX_COUNT), 37 argSetID: new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * MAX_COUNT), 38 }; 39 } 40 return new Promise((resolve): void => { 41 getThreadPool(traceId).submitProto( 42 QueryEnum.ThreadData, 43 { 44 pid: pid, 45 tid: tid, 46 startNS: TraceRow.range?.startNS || 0, 47 endNS: TraceRow.range?.endNS || 0, 48 recordStartNS: Utils.getInstance().getRecordStartNS(traceId), 49 recordEndNS: Utils.getInstance().getRecordEndNS(traceId), 50 width: width, 51 trafic: trafic, 52 sharedArrayBuffers: row.sharedArrayBuffers, 53 }, 54 (res: unknown, len: number, transfer: boolean, isEmpty: boolean): void => { 55 if (isEmpty) { 56 resolve(true); 57 } else { 58 resolve(arrayBufferHandler(transfer ? res : row.sharedArrayBuffers, len)); 59 } 60 } 61 ); 62 }); 63} 64 65function arrayBufferHandler(buffers: unknown, len: number): ThreadStruct[] { 66 let outArr: ThreadStruct[] = []; //@ts-ignore 67 let startTime = new Float64Array(buffers.startTime); //@ts-ignore 68 let dur = new Float64Array(buffers.dur); //@ts-ignore 69 let cpu = new Int8Array(buffers.cpu); //@ts-ignore 70 let id = new Int32Array(buffers.id); //@ts-ignore 71 let tid = new Int32Array(buffers.tid); //@ts-ignore 72 let state = new Int32Array(buffers.state); //@ts-ignore 73 let pid = new Int32Array(buffers.pid); //@ts-ignore 74 let argSetID = new Int32Array(buffers.argSetID); 75 for (let i = 0; i < len; i++) { 76 outArr.push({ 77 startTime: startTime[i], 78 dur: dur[i], 79 cpu: cpu[i], 80 id: id[i], 81 tid: tid[i], 82 state: threadStateToString(state[i]), 83 pid: pid[i], 84 argSetID: argSetID[i], 85 } as ThreadStruct); 86 } 87 return outArr; 88} 89